windy.lua 12 KB

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