geo2.lua 9.5 KB

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