wat2.lua 9.3 KB

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