geo2.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. -- 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. for k, v in ipairs(targets) do
  96. local node = minetest.get_node(v)
  97. if minetest.get_item_group(node.name, "water") > 0 then
  98. cw = cw + 1
  99. elseif minetest.get_item_group(node.name, "lava") > 0 then
  100. cl = cl + 1
  101. end
  102. end
  103. if cw > 0 and cl > 0 then
  104. -- Randomize time to next nodecheck.
  105. meta:set_int("chktmr", math_random(3, 15))
  106. -- Number of lava nodes must match number of water nodes,
  107. -- for maximum efficiency.
  108. if cw > cl then
  109. cw = cl
  110. end
  111. if cl > cw then
  112. cl = cw
  113. end
  114. meta:set_int("active", 1)
  115. meta:set_int("eups", math_floor((cw + cl)*ENERGY_AMOUNT))
  116. machines.swap_node(pos, "geo2:lv_active")
  117. result = true
  118. else
  119. -- Don't set timer if generator is offline.
  120. -- The next check needs to happen the next time the machine is punched.
  121. meta:set_int("chktmr", 0)
  122. meta:set_int("active", 0)
  123. meta:set_int("eups", 0)
  124. machines.swap_node(pos, "geo2:lv_inactive")
  125. result = false
  126. end
  127. meta:set_string("infotext", geo2.compose_infotext(pos))
  128. return result
  129. end
  130. -- Decrement check timer.
  131. timer = timer - 1
  132. meta:set_int("chktmr", timer)
  133. -- No check performed; just return whatever the result of the last check was.
  134. return (active == 1)
  135. end
  136. geo2.on_timer =
  137. function(pos, elapsed)
  138. local meta = minetest.get_meta(pos)
  139. local owner = meta:get_string("owner")
  140. local inv = meta:get_inventory()
  141. local keeprunning = false
  142. -- Check if we can produce energy from environment.
  143. -- Note that this uses a caching algorithm.
  144. local canrun = geo2.check_environment(pos, meta)
  145. -- If environment is no longer producing energy,
  146. -- unload the buffered energy.
  147. if not canrun then
  148. local energy = inv:get_stack("buffer", 1)
  149. energy:set_count(net2.put_energy(pos, owner, energy:get_count(), "lv"))
  150. inv:set_stack("buffer", 1, energy)
  151. end
  152. -- Produce energy.
  153. local needdischarge = false
  154. if canrun then
  155. local eups = meta:get_int("eups")
  156. local energy = "atomic:energy " .. eups
  157. local stack = inv:get_stack("buffer", 1)
  158. if stack:get_count() >= BUFFER_SIZE then
  159. needdischarge = true
  160. end
  161. if not needdischarge then
  162. if inv:room_for_item("buffer", energy) then
  163. inv:add_item("buffer", energy)
  164. end
  165. end
  166. keeprunning = true
  167. end
  168. -- Discharge energy.
  169. if needdischarge then
  170. local energy = inv:get_stack("buffer", 1)
  171. -- Unload energy onto the network.
  172. local old = energy:get_count()
  173. energy:set_count(net2.put_energy(pos, owner, old, "lv"))
  174. inv:set_stack("buffer", 1, energy)
  175. if energy:get_count() < old then
  176. keeprunning = true
  177. else
  178. -- Batteries full? Go to sleep.
  179. keeprunning = false
  180. end
  181. end
  182. -- Determine mode (active or sleep) and set timer accordingly.
  183. if keeprunning then
  184. minetest.get_node_timer(pos):start(1.0)
  185. else
  186. -- Slow down timer during sleep periods to reduce load.
  187. minetest.get_node_timer(pos):start(math_random(1, 3*60))
  188. meta:set_int("chktmr", 0)
  189. meta:set_int("active", 0)
  190. meta:set_int("eups", 0)
  191. meta:set_string("infotext", geo2.compose_infotext(pos))
  192. machines.swap_node(pos, "geo2:lv_inactive")
  193. end
  194. end
  195. geo2.on_construct =
  196. function(pos)
  197. end
  198. geo2.after_place_node =
  199. function(pos, placer, itemstack, pointed_thing)
  200. local meta = minetest.get_meta(pos)
  201. local node = minetest.get_node(pos)
  202. local owner = placer:get_player_name()
  203. local inv = meta:get_inventory()
  204. meta:set_string("owner", owner)
  205. meta:set_string("nodename", node.name)
  206. inv:set_size("buffer", 1)
  207. net2.clear_caches(pos, owner, "lv")
  208. meta:set_string("formspec", geo2.compose_formspec(pos))
  209. meta:set_string("infotext", geo2.compose_infotext(pos))
  210. nodestore.add_node(pos)
  211. local timer = minetest.get_node_timer(pos)
  212. timer:start(1.0)
  213. end
  214. geo2.on_blast =
  215. function(pos)
  216. local drops = {}
  217. drops[#drops+1] = "geo2:lv_inactive"
  218. minetest.remove_node(pos)
  219. return drops
  220. end
  221. geo2.allow_metadata_inventory_put =
  222. function(pos, listname, index, stack, player)
  223. return 0
  224. end
  225. geo2.allow_metadata_inventory_move =
  226. function(pos, from_list, from_index, to_list, to_index, count, player)
  227. return 0
  228. end
  229. geo2.allow_metadata_inventory_take =
  230. function(pos, listname, index, stack, player)
  231. return 0
  232. end
  233. geo2.on_metadata_inventory_move =
  234. function(pos)
  235. geo2.trigger_update(pos)
  236. end
  237. geo2.on_metadata_inventory_put =
  238. function(pos)
  239. geo2.trigger_update(pos)
  240. end
  241. geo2.on_metadata_inventory_take =
  242. function(pos, listname, index, stack, player)
  243. geo2.trigger_update(pos)
  244. end
  245. geo2.on_destruct =
  246. function(pos)
  247. local meta = minetest.get_meta(pos)
  248. net2.clear_caches(pos, meta:get_string("owner"), "lv")
  249. nodestore.del_node(pos)
  250. end
  251. if not geo2.run_once then
  252. for k, v in ipairs({
  253. {name="inactive", tile="geothermal_generator_top.png"},
  254. {name="active", tile="geothermal_generator_top_active.png"},
  255. }) do
  256. minetest.register_node(":geo2:lv_" .. v.name, {
  257. description = "LV Geothermal Generator",
  258. tiles = {
  259. v.tile, v.tile,
  260. "geothermal_generator_side.png", "geothermal_generator_side.png",
  261. "geothermal_generator_side.png", "geothermal_generator_side.png"
  262. },
  263. groups = utility.dig_groups("machine"),
  264. paramtype2 = "facedir",
  265. is_ground_content = false,
  266. sounds = default.node_sound_metal_defaults(),
  267. drop = "geo2:lv_inactive",
  268. on_energy_get = function(...)
  269. return geo2.on_energy_get(...) end,
  270. on_rotate = function(...)
  271. return screwdriver.rotate_simple(...) end,
  272. allow_metadata_inventory_put = function(...)
  273. return geo2.allow_metadata_inventory_put(...) end,
  274. allow_metadata_inventory_move = function(...)
  275. return geo2.allow_metadata_inventory_move(...) end,
  276. allow_metadata_inventory_take = function(...)
  277. return geo2.allow_metadata_inventory_take(...) end,
  278. on_metadata_inventory_move = function(...)
  279. return geo2.on_metadata_inventory_move(...) end,
  280. on_metadata_inventory_put = function(...)
  281. return geo2.on_metadata_inventory_put(...) end,
  282. on_metadata_inventory_take = function(...)
  283. return geo2.on_metadata_inventory_take(...) end,
  284. on_punch = function(...)
  285. return geo2.on_punch(...) end,
  286. can_dig = function(...)
  287. return geo2.can_dig(...) end,
  288. on_timer = function(...)
  289. return geo2.on_timer(...) end,
  290. on_construct = function(...)
  291. return geo2.on_construct(...) end,
  292. on_destruct = function(...)
  293. return geo2.on_destruct(...) end,
  294. on_blast = function(...)
  295. return geo2.on_blast(...) end,
  296. after_place_node = function(...)
  297. return geo2.after_place_node(...) end,
  298. })
  299. end
  300. local c = "geo2:core"
  301. local f = geo2.modpath .. "/geo2.lua"
  302. reload.register_file(c, f, false)
  303. geo2.run_once = true
  304. end