geo2.lua 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. geo2 = geo2 or {}
  2. geo2.modpath = minetest.get_modpath("geothermal_generator")
  3. local BUFFER_SIZE = tech.geothermal.buffer
  4. local ENERGY_AMOUNT = tech.geothermal.power
  5. geo2.on_energy_get =
  6. function(pos, energy)
  7. local meta = minetest.get_meta(pos)
  8. local inv = meta:get_inventory()
  9. local have = inv:get_stack("buffer", 1):get_count()
  10. if have < energy then
  11. inv:set_stack("buffer", 1, ItemStack(""))
  12. geo2.trigger_update(pos)
  13. return have
  14. end
  15. have = have - energy
  16. inv:set_stack("buffer", 1, ItemStack("atomic:energy " .. have))
  17. geo2.trigger_update(pos)
  18. return energy
  19. end
  20. geo2.compose_formspec =
  21. function(pos)
  22. local formspec =
  23. "size[2,2.5]" ..
  24. default.gui_bg ..
  25. default.gui_bg_img ..
  26. default.gui_slots ..
  27. "label[0,0.5;Energy Buffer]" ..
  28. "list[context;buffer;0,1;1,1]"
  29. return formspec
  30. end
  31. geo2.compose_infotext =
  32. function(pos)
  33. local meta = minetest.get_meta(pos)
  34. local eups = meta:get_int("eups")
  35. local state = "Standby"
  36. if meta:get_int("active") == 1 then
  37. state = "Active"
  38. end
  39. local infotext = "LV Geothermal Generator (" .. state .. ")\n" ..
  40. "Output: " .. eups .. " EU Per/Sec"
  41. return infotext
  42. end
  43. geo2.trigger_update =
  44. function(pos)
  45. local timer = minetest.get_node_timer(pos)
  46. -- Restart timer even if already running.
  47. timer:start(1.0)
  48. end
  49. geo2.on_punch =
  50. function(pos, node, puncher, pointed_thing)
  51. geo2.trigger_update(pos)
  52. end
  53. geo2.can_dig =
  54. function(pos, player)
  55. return true
  56. end
  57. geo2.check_environment =
  58. function(pos, meta)
  59. local timer = meta:get_int("chktmr")
  60. local active = meta:get_int("active")
  61. if timer <= 0 then
  62. local result = false
  63. -- Check all 4 sides of the geothermal generator.
  64. local targets = {
  65. {x=pos.x+1, y=pos.y, z=pos.z},
  66. {x=pos.x-1, y=pos.y, z=pos.z},
  67. {x=pos.x, y=pos.y, z=pos.z+1},
  68. {x=pos.x, y=pos.y, z=pos.z-1},
  69. {x=pos.x+1, y=pos.y, z=pos.z+1},
  70. {x=pos.x+1, y=pos.y, z=pos.z-1},
  71. {x=pos.x-1, y=pos.y, z=pos.z+1},
  72. {x=pos.x-1, y=pos.y, z=pos.z-1},
  73. {x=pos.x+1, y=pos.y-1, z=pos.z},
  74. {x=pos.x-1, y=pos.y-1, z=pos.z},
  75. {x=pos.x, y=pos.y-1, z=pos.z+1},
  76. {x=pos.x, y=pos.y-1, z=pos.z-1},
  77. {x=pos.x+1, y=pos.y-1, z=pos.z+1},
  78. {x=pos.x+1, y=pos.y-1, z=pos.z-1},
  79. {x=pos.x-1, y=pos.y-1, z=pos.z+1},
  80. {x=pos.x-1, y=pos.y-1, z=pos.z-1},
  81. {x=pos.x+1, y=pos.y+1, z=pos.z},
  82. {x=pos.x-1, y=pos.y+1, z=pos.z},
  83. {x=pos.x, y=pos.y+1, z=pos.z+1},
  84. {x=pos.x, y=pos.y+1, z=pos.z-1},
  85. {x=pos.x+1, 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. }
  90. local cw = 0
  91. local cl = 0
  92. for k, v in ipairs(targets) do
  93. local node = minetest.get_node(v)
  94. if minetest.get_item_group(node.name, "water") > 0 then
  95. cw = cw + 1
  96. elseif minetest.get_item_group(node.name, "lava") > 0 then
  97. cl = cl + 1
  98. end
  99. end
  100. if cw > 0 and cl > 0 then
  101. -- Randomize time to next nodecheck.
  102. meta:set_int("chktmr", math.random(3, 15))
  103. -- Number of lava nodes must match number of water nodes,
  104. -- for maximum efficiency.
  105. if cw > cl then
  106. cw = cl
  107. end
  108. if cl > cw then
  109. cl = cw
  110. end
  111. meta:set_int("active", 1)
  112. meta:set_int("eups", math.floor((cw + cl)*ENERGY_AMOUNT))
  113. machines.swap_node(pos, "geo2:lv_active")
  114. result = true
  115. else
  116. -- Don't set timer if generator is offline.
  117. -- The next check needs to happen the next time the machine is punched.
  118. meta:set_int("chktmr", 0)
  119. meta:set_int("active", 0)
  120. meta:set_int("eups", 0)
  121. machines.swap_node(pos, "geo2:lv_inactive")
  122. result = false
  123. end
  124. meta:set_string("infotext", geo2.compose_infotext(pos))
  125. return result
  126. end
  127. -- Decrement check timer.
  128. timer = timer - 1
  129. meta:set_int("chktmr", timer)
  130. -- No check performed; just return whatever the result of the last check was.
  131. return (active == 1)
  132. end
  133. geo2.on_timer =
  134. function(pos, elapsed)
  135. local meta = minetest.get_meta(pos)
  136. local owner = meta:get_string("owner")
  137. local inv = meta:get_inventory()
  138. local keeprunning = false
  139. -- Check if we can produce energy from environment.
  140. -- Note that this uses a caching algorithm.
  141. local canrun = geo2.check_environment(pos, meta)
  142. -- If environment is no longer producing energy,
  143. -- unload the buffered energy.
  144. if not canrun then
  145. local energy = inv:get_stack("buffer", 1)
  146. energy:set_count(net2.put_energy(pos, owner, energy:get_count(), "lv"))
  147. inv:set_stack("buffer", 1, energy)
  148. end
  149. -- Produce energy.
  150. local needdischarge = false
  151. if canrun then
  152. local eups = meta:get_int("eups")
  153. local energy = "atomic:energy " .. eups
  154. local stack = inv:get_stack("buffer", 1)
  155. if stack:get_count() >= BUFFER_SIZE then
  156. needdischarge = true
  157. end
  158. if not needdischarge then
  159. if inv:room_for_item("buffer", energy) then
  160. inv:add_item("buffer", energy)
  161. end
  162. end
  163. keeprunning = true
  164. end
  165. -- Discharge energy.
  166. if needdischarge then
  167. local energy = inv:get_stack("buffer", 1)
  168. -- Unload energy onto the network.
  169. local old = energy:get_count()
  170. energy:set_count(net2.put_energy(pos, owner, old, "lv"))
  171. inv:set_stack("buffer", 1, energy)
  172. if energy:get_count() < old then
  173. keeprunning = true
  174. else
  175. -- Batteries full? Go to sleep.
  176. keeprunning = false
  177. end
  178. end
  179. -- Determine mode (active or sleep) and set timer accordingly.
  180. if keeprunning then
  181. minetest.get_node_timer(pos):start(1.0)
  182. else
  183. -- Slow down timer during sleep periods to reduce load.
  184. minetest.get_node_timer(pos):start(math.random(1, 3*60))
  185. meta:set_int("chktmr", 0)
  186. meta:set_int("active", 0)
  187. meta:set_int("eups", 0)
  188. meta:set_string("infotext", geo2.compose_infotext(pos))
  189. machines.swap_node(pos, "geo2:lv_inactive")
  190. end
  191. end
  192. geo2.on_construct =
  193. function(pos)
  194. end
  195. geo2.after_place_node =
  196. function(pos, placer, itemstack, pointed_thing)
  197. local meta = minetest.get_meta(pos)
  198. local node = minetest.get_node(pos)
  199. local owner = placer:get_player_name()
  200. local inv = meta:get_inventory()
  201. meta:set_string("owner", owner)
  202. meta:set_string("nodename", node.name)
  203. inv:set_size("buffer", 1)
  204. net2.clear_caches(pos, owner, "lv")
  205. meta:set_string("formspec", geo2.compose_formspec(pos))
  206. meta:set_string("infotext", geo2.compose_infotext(pos))
  207. nodestore.add_node(pos)
  208. local timer = minetest.get_node_timer(pos)
  209. timer:start(1.0)
  210. end
  211. geo2.on_blast =
  212. function(pos)
  213. local drops = {}
  214. drops[#drops+1] = "geo2:lv_inactive"
  215. minetest.remove_node(pos)
  216. return drops
  217. end
  218. geo2.allow_metadata_inventory_put =
  219. function(pos, listname, index, stack, player)
  220. return 0
  221. end
  222. geo2.allow_metadata_inventory_move =
  223. function(pos, from_list, from_index, to_list, to_index, count, player)
  224. return 0
  225. end
  226. geo2.allow_metadata_inventory_take =
  227. function(pos, listname, index, stack, player)
  228. return 0
  229. end
  230. geo2.on_metadata_inventory_move =
  231. function(pos)
  232. geo2.trigger_update(pos)
  233. end
  234. geo2.on_metadata_inventory_put =
  235. function(pos)
  236. geo2.trigger_update(pos)
  237. end
  238. geo2.on_metadata_inventory_take =
  239. function(pos, listname, index, stack, player)
  240. geo2.trigger_update(pos)
  241. end
  242. geo2.on_destruct =
  243. function(pos)
  244. local meta = minetest.get_meta(pos)
  245. net2.clear_caches(pos, meta:get_string("owner"), "lv")
  246. nodestore.del_node(pos)
  247. end
  248. if not geo2.run_once then
  249. for k, v in ipairs({
  250. {name="inactive", tile="geothermal_generator_top.png"},
  251. {name="active", tile="geothermal_generator_top_active.png"},
  252. }) do
  253. minetest.register_node(":geo2:lv_" .. v.name, {
  254. description = "LV Geothermal Generator",
  255. tiles = {
  256. v.tile, v.tile,
  257. "geothermal_generator_side.png", "geothermal_generator_side.png",
  258. "geothermal_generator_side.png", "geothermal_generator_side.png"
  259. },
  260. groups = utility.dig_groups("machine"),
  261. paramtype2 = "facedir",
  262. is_ground_content = false,
  263. sounds = default.node_sound_metal_defaults(),
  264. drop = "geo2:lv_inactive",
  265. on_energy_get = function(...)
  266. return geo2.on_energy_get(...) end,
  267. on_rotate = function(...)
  268. return screwdriver.rotate_simple(...) end,
  269. allow_metadata_inventory_put = function(...)
  270. return geo2.allow_metadata_inventory_put(...) end,
  271. allow_metadata_inventory_move = function(...)
  272. return geo2.allow_metadata_inventory_move(...) end,
  273. allow_metadata_inventory_take = function(...)
  274. return geo2.allow_metadata_inventory_take(...) end,
  275. on_metadata_inventory_move = function(...)
  276. return geo2.on_metadata_inventory_move(...) end,
  277. on_metadata_inventory_put = function(...)
  278. return geo2.on_metadata_inventory_put(...) end,
  279. on_metadata_inventory_take = function(...)
  280. return geo2.on_metadata_inventory_take(...) end,
  281. on_punch = function(...)
  282. return geo2.on_punch(...) end,
  283. can_dig = function(...)
  284. return geo2.can_dig(...) end,
  285. on_timer = function(...)
  286. return geo2.on_timer(...) end,
  287. on_construct = function(...)
  288. return geo2.on_construct(...) end,
  289. on_destruct = function(...)
  290. return geo2.on_destruct(...) end,
  291. on_blast = function(...)
  292. return geo2.on_blast(...) end,
  293. after_place_node = function(...)
  294. return geo2.after_place_node(...) end,
  295. })
  296. end
  297. local c = "geo2:core"
  298. local f = geo2.modpath .. "/geo2.lua"
  299. reload.register_file(c, f, false)
  300. geo2.run_once = true
  301. end