init.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. geothermal_generator = geothermal_generator or {}
  2. geothermal_generator.modpath = minetest.get_modpath("geothermal_generator")
  3. -- Localize for performance.
  4. local math_random = math.random
  5. geothermal_generator.update_formspec =
  6. function(pos)
  7. local meta = minetest.get_meta(pos)
  8. local formspec =
  9. "size[8,8.5]" ..
  10. default.gui_bg ..
  11. default.gui_bg_img ..
  12. default.gui_slots ..
  13. "label[1.5,0.5;Upgrades]" ..
  14. "list[context;upg;1.5,1;1,2]" ..
  15. "label[5.5,0.5;Configuration]" ..
  16. "list[context;cfg;5.5,1;1,2]" ..
  17. "list[current_player;main;0,4.25;8,1;]" ..
  18. "list[current_player;main;0,5.5;8,3;8]" ..
  19. default.get_hotbar_bg(0, 4.25)
  20. meta:set_string("formspec", formspec)
  21. end
  22. geothermal_generator.update_infotext =
  23. function(pos)
  24. local meta = minetest.get_meta(pos)
  25. local active = "Standby"
  26. if meta:get_int("active") == 1 then
  27. active = "Active"
  28. end
  29. local eups = meta:get_int("eups")
  30. local energy = meta:get_int("energy")
  31. local infotext = "Geothermal Generator (" .. active .. ")\n" ..
  32. "EUs per/sec: " .. eups .. "\n" ..
  33. "Buffered: " .. energy .. " EUs"
  34. meta:set_string("infotext", infotext)
  35. end
  36. geothermal_generator.trigger_update =
  37. function(pos)
  38. local timer = minetest.get_node_timer(pos)
  39. if not timer:is_started() then
  40. timer:start(1.0)
  41. end
  42. end
  43. -- Typedata is used when traversing the network, without touching the node.
  44. -- It must contain as much data as needed to get the node even if unloaded.
  45. -- This must be done after node construction.
  46. -- This should also be done when punched, to allow old nodes to be upgraded.
  47. geothermal_generator.initialize_typedata =
  48. function(pos)
  49. local meta = minetest.get_meta(pos)
  50. meta:set_string("technic_machine", "yes")
  51. meta:set_string("technic_type", "generator")
  52. meta:set_string("technic_tier", "lv")
  53. -- The active nodetype should have all same properties and functions.
  54. meta:set_string("technic_name", "geothermal_generator:inactive")
  55. end
  56. geothermal_generator.on_punch =
  57. function(pos, node, puncher, pointed_thing)
  58. geothermal_generator.initialize_typedata(pos)
  59. geothermal_generator.trigger_update(pos)
  60. end
  61. geothermal_generator.can_dig =
  62. function(pos, player)
  63. local meta = minetest.get_meta(pos)
  64. local inv = meta:get_inventory()
  65. return inv:is_empty("upg") and inv:is_empty("cfg")
  66. end
  67. geothermal_generator.can_run =
  68. function(pos, meta)
  69. local timer = meta:get_int("chktmr")
  70. local active = meta:get_int("active")
  71. if timer <= 0 then
  72. -- Check all 4 sides of the geothermal generator.
  73. local targets = {
  74. {x=pos.x+1, y=pos.y, z=pos.z},
  75. {x=pos.x-1, y=pos.y, z=pos.z},
  76. {x=pos.x, y=pos.y, z=pos.z+1},
  77. {x=pos.x, y=pos.y, z=pos.z-1},
  78. {x=pos.x+1, y=pos.y, z=pos.z+1},
  79. {x=pos.x+1, y=pos.y, z=pos.z-1},
  80. {x=pos.x-1, y=pos.y, z=pos.z+1},
  81. {x=pos.x-1, y=pos.y, z=pos.z-1},
  82. {x=pos.x+1, y=pos.y-1, z=pos.z},
  83. {x=pos.x-1, y=pos.y-1, z=pos.z},
  84. {x=pos.x, y=pos.y-1, z=pos.z+1},
  85. {x=pos.x, y=pos.y-1, z=pos.z-1},
  86. {x=pos.x+1, y=pos.y-1, z=pos.z+1},
  87. {x=pos.x+1, y=pos.y-1, z=pos.z-1},
  88. {x=pos.x-1, y=pos.y-1, z=pos.z+1},
  89. {x=pos.x-1, y=pos.y-1, z=pos.z-1},
  90. }
  91. local count_water = 0
  92. local count_lava = 0
  93. for k, v in ipairs(targets) do
  94. local node = minetest.get_node(v)
  95. if minetest.get_item_group(node.name, "water") > 0 then
  96. count_water = count_water + 1
  97. elseif minetest.get_item_group(node.name, "lava") > 0 then
  98. count_lava = count_lava + 1
  99. end
  100. end
  101. if count_water > 0 and count_lava > 0 then
  102. -- Randomize time to next nodecheck.
  103. meta:set_int("chktmr", math_random(3, 15))
  104. meta:set_int("active", 1)
  105. meta:set_int("eups", (count_water + count_lava) * 10)
  106. machines.swap_node(pos, "geothermal_generator:active")
  107. return true
  108. else
  109. -- Don't set timer if generator is offline.
  110. -- The next check needs to happen the next time the machine is punched.
  111. meta:set_int("chktmr", 0)
  112. meta:set_int("active", 0)
  113. meta:set_int("eups", 0)
  114. machines.swap_node(pos, "geothermal_generator:inactive")
  115. return false
  116. end
  117. end
  118. -- Decrement check timer.
  119. timer = timer - 1
  120. meta:set_int("chktmr", timer)
  121. -- No check performed; just return whatever the result of the last check was.
  122. return (active == 1)
  123. end
  124. geothermal_generator.on_timer =
  125. function(pos, elapsed)
  126. machines.log_update(pos, "Geothermal Generator")
  127. local meta = minetest.get_meta(pos)
  128. local result = geothermal_generator.can_run(pos, meta)
  129. local bufmax = meta:get_int("bmax")
  130. local energy = meta:get_int("energy")
  131. if energy >= bufmax then
  132. -- Unload energy onto the network.
  133. -- Geothermal is a low-voltage EU producer.
  134. local hubs = machines.get_adjacent_network_hubs(pos, {"lv"})
  135. if hubs then
  136. for k, v in ipairs(hubs) do
  137. if energy > 0 then
  138. -- Gets back the energy left over.
  139. energy = machines.deliver_charge_to_network(pos, v, energy)
  140. end
  141. end
  142. end
  143. end
  144. -- If energy couldn't be offloaded, then shutdown the generator.
  145. if energy >= bufmax then
  146. meta:set_int("eups", 0)
  147. meta:set_int("active", 0)
  148. meta:set_int("chktmr", 0)
  149. machines.swap_node(pos, "geothermal_generator:inactive")
  150. result = false
  151. end
  152. -- Produce energy.
  153. local eups = meta:get_int("eups")
  154. energy = energy + eups
  155. if energy > bufmax then
  156. energy = bufmax
  157. end
  158. meta:set_int("energy", energy)
  159. -- Update GUI stuff.
  160. geothermal_generator.update_formspec(pos)
  161. geothermal_generator.update_infotext(pos)
  162. return result
  163. end
  164. geothermal_generator.on_construct =
  165. function(pos)
  166. end
  167. geothermal_generator.after_place_node =
  168. function(pos, placer, itemstack, pointed_thing)
  169. geothermal_generator.initialize_typedata(pos)
  170. local meta = minetest.get_meta(pos)
  171. local inv = meta:get_inventory()
  172. meta:set_int("bmax", 10000)
  173. inv:set_size("upg", 2)
  174. inv:set_size("cfg", 2)
  175. geothermal_generator.update_formspec(pos)
  176. geothermal_generator.update_infotext(pos)
  177. end
  178. geothermal_generator.on_blast =
  179. function(pos)
  180. local drops = {}
  181. default.get_inventory_drops(pos, "upg", drops)
  182. default.get_inventory_drops(pos, "cfg", drops)
  183. drops[#drops+1] = "geothermal_generator:inactive"
  184. minetest.remove_node(pos)
  185. return drops
  186. end
  187. geothermal_generator.allow_metadata_inventory_put =
  188. function(pos, listname, index, stack, player)
  189. local pname = player:get_player_name()
  190. if minetest.test_protection(pos, pname) then return 0 end
  191. if listname == "cfg" and stack:get_name() == "cfg:dev" then
  192. return stack:get_count()
  193. elseif listname == "upg" and stack:get_name() == "battery:battery" then
  194. return stack:get_count()
  195. end
  196. return 0
  197. end
  198. geothermal_generator.allow_metadata_inventory_move =
  199. function(pos, from_list, from_index, to_list, to_index, count, player)
  200. local pname = player:get_player_name()
  201. if minetest.test_protection(pos, pname) then
  202. return 0
  203. end
  204. if from_list == to_list then
  205. return count
  206. end
  207. return 0
  208. end
  209. geothermal_generator.allow_metadata_inventory_take =
  210. function(pos, listname, index, stack, player)
  211. local pname = player:get_player_name()
  212. if minetest.test_protection(pos, pname) then
  213. return 0
  214. end
  215. return stack:get_count()
  216. end
  217. geothermal_generator.on_metadata_inventory_move =
  218. function(pos)
  219. geothermal_generator.trigger_update(pos)
  220. end
  221. geothermal_generator.on_metadata_inventory_put =
  222. function(pos)
  223. geothermal_generator.trigger_update(pos)
  224. end
  225. geothermal_generator.on_metadata_inventory_take =
  226. function(pos, listname, index, stack, player)
  227. geothermal_generator.trigger_update(pos)
  228. end
  229. if not geothermal_generator.run_once then
  230. for k, v in ipairs({
  231. {name="inactive", tile="geothermal_generator_top.png"},
  232. {name="active", tile="geothermal_generator_top_active.png"},
  233. }) do
  234. minetest.register_node("geothermal_generator:" .. v.name, {
  235. description = "LV Geothermal Generator",
  236. tiles = {
  237. v.tile, v.tile,
  238. "geothermal_generator_side.png", "geothermal_generator_side.png",
  239. "geothermal_generator_side.png", "geothermal_generator_side.png"
  240. },
  241. groups = utility.dig_groups("machine", {
  242. immovable = 1,
  243. tier_lv = 1,
  244. }),
  245. paramtype2 = "facedir",
  246. on_rotate = function(...) return screwdriver.rotate_simple(...) end,
  247. is_ground_content = false,
  248. sounds = default.node_sound_metal_defaults(),
  249. drop = "geo2:lv_inactive",
  250. allow_metadata_inventory_put = function(...)
  251. return geothermal_generator.allow_metadata_inventory_put(...) end,
  252. allow_metadata_inventory_move = function(...)
  253. return geothermal_generator.allow_metadata_inventory_move(...) end,
  254. allow_metadata_inventory_take = function(...)
  255. return geothermal_generator.allow_metadata_inventory_take(...) end,
  256. on_metadata_inventory_move = function(...)
  257. return geothermal_generator.on_metadata_inventory_move(...) end,
  258. on_metadata_inventory_put = function(...)
  259. return geothermal_generator.on_metadata_inventory_put(...) end,
  260. on_metadata_inventory_take = function(...)
  261. return geothermal_generator.on_metadata_inventory_take(...) end,
  262. on_punch = function(...)
  263. return geothermal_generator.on_punch(...) end,
  264. can_dig = function(...)
  265. return geothermal_generator.can_dig(...) end,
  266. on_timer = function(...)
  267. return geothermal_generator.on_timer(...) end,
  268. on_construct = function(...)
  269. return geothermal_generator.on_construct(...) end,
  270. on_blast = function(...)
  271. return geothermal_generator.on_blast(...) end,
  272. after_place_node = function(...)
  273. return geothermal_generator.after_place_node(...) end,
  274. on_machine_execute = function(...)
  275. return machines.on_machine_execute(...) end,
  276. })
  277. end
  278. minetest.register_alias("geothermal_generator:geothermal_generator", "geothermal_generator:inactive")
  279. minetest.register_craft({
  280. output = 'geo2:lv_inactive',
  281. recipe = {
  282. {'morerocks:granite', 'default:diamond', 'morerocks:granite'},
  283. {'fine_wire:copper', 'techcrafts:machine_casing', 'fine_wire:copper'},
  284. {'morerocks:granite', 'cb2:lv', 'morerocks:granite'},
  285. }
  286. })
  287. local c = "geothermal_generator:core"
  288. local f = geothermal_generator.modpath .. "/init.lua"
  289. reload.register_file(c, f, false)
  290. dofile(geothermal_generator.modpath .. "/geo2.lua")
  291. dofile(geothermal_generator.modpath .. "/wat2.lua")
  292. geothermal_generator.run_once = true
  293. end