tide.lua 12 KB

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