tide.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. tide = tide or {}
  2. tide.modpath = minetest.get_modpath("machines")
  3. -- Localize for performance.
  4. local math_random = math.random
  5. local BUFFER_SIZE = tech.tidal.buffer
  6. local ENERGY_AMOUNT = tech.tidal.power
  7. local function is_water(nn)
  8. if minetest.get_item_group(nn, "water") ~= 0 then
  9. return true
  10. end
  11. end
  12. -- Queued algorithm.
  13. local function count_nearby_ocean(startpos)
  14. local traversal = {}
  15. local queue = {}
  16. local curpos, hash, exists, name, found, depth
  17. local first = true
  18. local get_node_hash = minetest.hash_node_position
  19. local get_node = minetest.get_node
  20. local count = 0
  21. startpos.d = 1
  22. queue[#queue+1] = startpos
  23. ::continue::
  24. curpos = queue[#queue]
  25. queue[#queue] = nil
  26. depth = curpos.d
  27. curpos.d = nil
  28. hash = get_node_hash(curpos)
  29. exists = false
  30. if traversal[hash] then
  31. exists = true
  32. if depth >= traversal[hash] then
  33. goto next
  34. end
  35. end
  36. if depth >= 20 then
  37. goto next
  38. end
  39. name = get_node(curpos).name
  40. found = false
  41. if is_water(name) then
  42. found = true
  43. end
  44. if not found then
  45. goto next
  46. end
  47. traversal[hash] = depth
  48. if not exists then
  49. count = count + 1
  50. end
  51. -- Queue up adjacent locations.
  52. queue[#queue+1] = {x=curpos.x+1, y=curpos.y, z=curpos.z, d=depth+1}
  53. queue[#queue+1] = {x=curpos.x-1, y=curpos.y, z=curpos.z, d=depth+1}
  54. queue[#queue+1] = {x=curpos.x, y=curpos.y+1, z=curpos.z, d=depth+1}
  55. queue[#queue+1] = {x=curpos.x, y=curpos.y-1, z=curpos.z, d=depth+1}
  56. queue[#queue+1] = {x=curpos.x, y=curpos.y, z=curpos.z+1, d=depth+1}
  57. queue[#queue+1] = {x=curpos.x, y=curpos.y, z=curpos.z-1, d=depth+1}
  58. ::next::
  59. first = false
  60. if #queue > 0 then
  61. goto continue
  62. end
  63. return count
  64. end
  65. tide.on_energy_get =
  66. function(pos, energy)
  67. local meta = minetest.get_meta(pos)
  68. local inv = meta:get_inventory()
  69. local have = inv:get_stack("buffer", 1):get_count()
  70. if have < energy then
  71. inv:set_stack("buffer", 1, ItemStack(""))
  72. tide.trigger_update(pos)
  73. return have
  74. end
  75. have = have - energy
  76. inv:set_stack("buffer", 1, ItemStack("atomic:energy " .. have))
  77. tide.trigger_update(pos)
  78. return energy
  79. end
  80. tide.compose_formspec =
  81. function(pos)
  82. local formspec =
  83. "size[2,2.5]" ..
  84. default.gui_bg ..
  85. default.gui_bg_img ..
  86. default.gui_slots ..
  87. "label[0,0.5;Energy Buffer]" ..
  88. "list[context;buffer;0,1;1,1]"
  89. return formspec
  90. end
  91. tide.compose_infotext =
  92. function(pos)
  93. local meta = minetest.get_meta(pos)
  94. local eups = meta:get_int("eups")
  95. local state = "Standby"
  96. if meta:get_int("active") == 1 then
  97. state = "Active"
  98. end
  99. local infotext = "MV Tidal Generator (" .. state .. ")\n" ..
  100. "Output: " .. eups .. " EU Per/Sec"
  101. local err = meta:get_string("error")
  102. if err ~= "" and err ~= "DUMMY" then
  103. infotext = infotext .. "\n" .. err
  104. end
  105. return infotext
  106. end
  107. tide.trigger_update =
  108. function(pos)
  109. local timer = minetest.get_node_timer(pos)
  110. -- Restart timer even if already running.
  111. timer:start(1.0)
  112. end
  113. tide.on_punch =
  114. function(pos, node, puncher, pointed_thing)
  115. tide.trigger_update(pos)
  116. --minetest.get_meta(pos):set_int("chktmr", 0)
  117. --minetest.chat_send_player("MustTest", "TESTING!")
  118. tide.privatize(minetest.get_meta(pos))
  119. end
  120. tide.can_dig =
  121. function(pos, player)
  122. return true
  123. end
  124. tide.check_environment =
  125. function(pos, meta)
  126. local timer = meta:get_int("chktmr")
  127. local active = meta:get_int("active")
  128. if timer <= 0 then
  129. local result = false
  130. local good = false
  131. local eups = ENERGY_AMOUNT
  132. local sides = {
  133. {x=pos.x+1, y=pos.y, z=pos.z},
  134. {x=pos.x-1, y=pos.y, z=pos.z},
  135. {x=pos.x, y=pos.y, z=pos.z+1},
  136. {x=pos.x, y=pos.y, z=pos.z-1},
  137. }
  138. local sidewater = 0
  139. for k, v in ipairs(sides) do
  140. if is_water(minetest.get_node(v).name) then
  141. sidewater = sidewater + 1
  142. end
  143. end
  144. local ocean = count_nearby_ocean({x=pos.x, y=pos.y-1, z=pos.z})
  145. --minetest.chat_send_player("MustTest", "# Server: Ocean: " .. ocean)
  146. if ocean >= 500 and sidewater >= 4 then
  147. good = true
  148. end
  149. if good then
  150. --minetest.chat_send_all("# Server: Good!")
  151. -- Randomize time to next nodecheck.
  152. meta:set_int("chktmr", math_random(1, 60*3))
  153. meta:set_int("active", 1)
  154. meta:set_int("eups", eups)
  155. meta:set_string("error", "DUMMY")
  156. result = true
  157. else
  158. ---minetest.chat_send_all("# Server: Bad!")
  159. -- Don't set timer if generator is offline.
  160. -- The next check needs to happen the next time the machine is punched.
  161. meta:set_int("chktmr", 0)
  162. meta:set_int("active", 0)
  163. meta:set_int("eups", 0)
  164. if sidewater < 4 then
  165. meta:set_string("error", "Machine not properly submerged!")
  166. elseif ocean == 0 then
  167. meta:set_string("error", "Turbine has insufficient contact with water!")
  168. elseif ocean < 500 then
  169. meta:set_string("error", "Insufficient current strength (" .. ocean .. ")!")
  170. else
  171. meta:set_string("error", "Unknown issue, please contact admin.")
  172. end
  173. result = false
  174. end
  175. meta:set_string("infotext", tide.compose_infotext(pos))
  176. return result
  177. end
  178. -- Decrement check timer.
  179. timer = timer - 1
  180. meta:set_int("chktmr", timer)
  181. -- No check performed; just return whatever the result of the last check was.
  182. return (active == 1)
  183. end
  184. tide.on_timer =
  185. function(pos, elapsed)
  186. local meta = minetest.get_meta(pos)
  187. local owner = meta:get_string("owner")
  188. local inv = meta:get_inventory()
  189. local keeprunning = false
  190. -- Check if we can produce energy from environment.
  191. -- Note that this uses a caching algorithm.
  192. local canrun = tide.check_environment(pos, meta)
  193. -- If environment is no longer producing energy,
  194. -- unload the buffered energy.
  195. if not canrun then
  196. local energy = inv:get_stack("buffer", 1)
  197. energy:set_count(net2.put_energy(pos, owner, energy:get_count(), "mv"))
  198. inv:set_stack("buffer", 1, energy)
  199. end
  200. -- Produce energy.
  201. local needdischarge = false
  202. if canrun then
  203. local eups = meta:get_int("eups")
  204. local energy = "atomic:energy " .. eups
  205. local stack = inv:get_stack("buffer", 1)
  206. if stack:get_count() >= BUFFER_SIZE then
  207. needdischarge = true
  208. end
  209. if not needdischarge then
  210. if inv:room_for_item("buffer", energy) then
  211. inv:add_item("buffer", energy)
  212. end
  213. end
  214. keeprunning = true
  215. end
  216. -- Discharge energy.
  217. if needdischarge then
  218. --minetest.chat_send_player("MustTest", "DISCHARGING!")
  219. local energy = inv:get_stack("buffer", 1)
  220. -- Unload energy onto the network.
  221. local old = energy:get_count()
  222. energy:set_count(net2.put_energy(pos, owner, old, "mv"))
  223. inv:set_stack("buffer", 1, energy)
  224. if energy:get_count() < old then
  225. keeprunning = true
  226. else
  227. -- Batteries full? Go to sleep.
  228. keeprunning = false
  229. end
  230. end
  231. -- Determine mode (active or sleep) and set timer accordingly.
  232. if keeprunning then
  233. minetest.get_node_timer(pos):start(1.0)
  234. else
  235. -- Slow down timer during sleep periods to reduce load.
  236. minetest.get_node_timer(pos):start(math_random(1, 3*60))
  237. meta:set_int("chktmr", 0)
  238. meta:set_int("active", 0)
  239. meta:set_int("eups", 0)
  240. meta:set_string("infotext", tide.compose_infotext(pos))
  241. end
  242. end
  243. tide.on_construct =
  244. function(pos)
  245. local meta = minetest.get_meta(pos)
  246. meta:set_int("eups", 0)
  247. meta:set_int("active", 0)
  248. meta:set_int("chktmr", 0)
  249. meta:set_string("error", "DUMMY")
  250. meta:set_string("owner", "DUMMY")
  251. meta:set_string("nodename", "DUMMY")
  252. tide.privatize(meta)
  253. end
  254. tide.privatize =
  255. function(meta)
  256. meta:mark_as_private({
  257. "nodename",
  258. "owner",
  259. "chktmr",
  260. "error",
  261. "eups",
  262. "active",
  263. })
  264. end
  265. tide.after_place_node =
  266. function(pos, placer, itemstack, pointed_thing)
  267. local meta = minetest.get_meta(pos)
  268. local node = minetest.get_node(pos)
  269. local owner = placer:get_player_name()
  270. local inv = meta:get_inventory()
  271. meta:set_string("owner", owner)
  272. meta:set_string("nodename", node.name)
  273. inv:set_size("buffer", 1)
  274. net2.clear_caches(pos, owner, "mv")
  275. meta:set_string("formspec", tide.compose_formspec(pos))
  276. meta:set_string("infotext", tide.compose_infotext(pos))
  277. nodestore.add_node(pos)
  278. local timer = minetest.get_node_timer(pos)
  279. timer:start(1.0)
  280. end
  281. tide.on_blast =
  282. function(pos)
  283. local drops = {}
  284. drops[#drops+1] = "tide:tide"
  285. minetest.remove_node(pos)
  286. return drops
  287. end
  288. tide.allow_metadata_inventory_put =
  289. function(pos, listname, index, stack, player)
  290. return 0
  291. end
  292. tide.allow_metadata_inventory_move =
  293. function(pos, from_list, from_index, to_list, to_index, count, player)
  294. return 0
  295. end
  296. tide.allow_metadata_inventory_take =
  297. function(pos, listname, index, stack, player)
  298. return 0
  299. end
  300. tide.on_metadata_inventory_move =
  301. function(pos)
  302. tide.trigger_update(pos)
  303. end
  304. tide.on_metadata_inventory_put =
  305. function(pos)
  306. tide.trigger_update(pos)
  307. end
  308. tide.on_metadata_inventory_take =
  309. function(pos, listname, index, stack, player)
  310. tide.trigger_update(pos)
  311. end
  312. tide.on_destruct =
  313. function(pos)
  314. local meta = minetest.get_meta(pos)
  315. net2.clear_caches(pos, meta:get_string("owner"), "mv")
  316. nodestore.del_node(pos)
  317. end
  318. if not tide.run_once then
  319. local nodebox = {
  320. {0, 16, 0, 16, 15, 16}, -- Base.
  321. {2, 15, 2, 14, 10, 14}, -- Box shaft.
  322. {7, 15, 7, 9, -1, 9}, -- Shaft.
  323. {-6, 3, 6, 22, 4, 10}, -- Upper blade.
  324. {6, 0, -6, 10, 1, 22}, -- Lower blade.
  325. }
  326. for k, v in ipairs(nodebox) do
  327. for m, n in ipairs(v) do
  328. local p = nodebox[k][m]
  329. p = p / 16
  330. p = p - 0.5
  331. nodebox[k][m] = p
  332. end
  333. end
  334. minetest.register_node(":tide:tide", {
  335. drawtype = "nodebox",
  336. description = "MV Tidal Generator",
  337. tiles = {"technic_carbon_steel_block.png"},
  338. groups = utility.dig_groups("machine"),
  339. node_box = {
  340. type = "fixed",
  341. fixed = nodebox,
  342. },
  343. paramtype = "light",
  344. paramtype2 = "facedir",
  345. is_ground_content = false,
  346. sounds = default.node_sound_metal_defaults(),
  347. drop = "tide:tide",
  348. on_energy_get = function(...)
  349. return tide.on_energy_get(...) end,
  350. on_rotate = function(...)
  351. return screwdriver.rotate_simple(...) end,
  352. allow_metadata_inventory_put = function(...)
  353. return tide.allow_metadata_inventory_put(...) end,
  354. allow_metadata_inventory_move = function(...)
  355. return tide.allow_metadata_inventory_move(...) end,
  356. allow_metadata_inventory_take = function(...)
  357. return tide.allow_metadata_inventory_take(...) end,
  358. on_metadata_inventory_move = function(...)
  359. return tide.on_metadata_inventory_move(...) end,
  360. on_metadata_inventory_put = function(...)
  361. return tide.on_metadata_inventory_put(...) end,
  362. on_metadata_inventory_take = function(...)
  363. return tide.on_metadata_inventory_take(...) end,
  364. on_punch = function(...)
  365. return tide.on_punch(...) end,
  366. can_dig = function(...)
  367. return tide.can_dig(...) end,
  368. on_timer = function(...)
  369. return tide.on_timer(...) end,
  370. on_construct = function(...)
  371. return tide.on_construct(...) end,
  372. on_destruct = function(...)
  373. return tide.on_destruct(...) end,
  374. on_blast = function(...)
  375. return tide.on_blast(...) end,
  376. after_place_node = function(...)
  377. return tide.after_place_node(...) end,
  378. })
  379. minetest.register_craft({
  380. output = 'tide:tide',
  381. recipe = {
  382. {'', 'cb2:mv', ''},
  383. {'carbon_steel:ingot', 'carbon_steel:block', 'carbon_steel:ingot'},
  384. {'', 'techcrafts:electric_motor', ''},
  385. }
  386. })
  387. local c = "tide:core"
  388. local f = tide.modpath .. "/tide.lua"
  389. reload.register_file(c, f, false)
  390. tide.run_once = true
  391. end