wat2.lua 9.4 KB

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