wat2.lua 9.6 KB

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