panel.lua 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. if not minetest.global_exists("panel") then panel = {} end
  2. panel.modpath = minetest.get_modpath("machines")
  3. local BUFFER_SIZE = tech.solar_panel.buffer
  4. local POWER_OUTPUT = tech.solar_panel.power
  5. -- Localize for performance.
  6. local math_floor = math.floor
  7. local math_random = math.random
  8. panel.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. panel.trigger_update(pos)
  16. return have
  17. end
  18. have = have - energy
  19. inv:set_stack("buffer", 1, ItemStack("atomic:energy " .. have))
  20. panel.trigger_update(pos)
  21. return energy
  22. end
  23. panel.on_player_walk_over =
  24. function(pos, player)
  25. utility.damage_player(player, "electrocute", 1*500)
  26. end
  27. panel.compose_formspec =
  28. function(pos)
  29. local formspec =
  30. "size[2,2.5]" ..
  31. default.gui_bg ..
  32. default.gui_bg_img ..
  33. default.gui_slots ..
  34. "label[0,0.5;Energy Buffer]" ..
  35. "list[context;buffer;0,1;1,1]"
  36. return formspec
  37. end
  38. panel.compose_infotext =
  39. function(pos)
  40. local meta = minetest.get_meta(pos)
  41. local eups = meta:get_int("eups")
  42. local state = "Standby"
  43. if meta:get_int("active") == 1 then
  44. state = "Active"
  45. end
  46. local infotext = "LV Solar Panel (" .. state .. ")\n" ..
  47. "Output: " .. eups .. " EU Per/Sec"
  48. return infotext
  49. end
  50. panel.trigger_update =
  51. function(pos)
  52. local timer = minetest.get_node_timer(pos)
  53. -- Start timer anew even if already running.
  54. timer:start(1.0)
  55. end
  56. panel.on_punch =
  57. function(pos, node, puncher, pointed_thing)
  58. panel.trigger_update(pos)
  59. end
  60. panel.can_dig =
  61. function(pos, player)
  62. return true
  63. end
  64. panel.check_environment =
  65. function(pos, meta)
  66. local timer = meta:get_int("chktmr")
  67. local active = meta:get_int("active")
  68. if timer <= 0 then
  69. -- Check environment.
  70. local goodenv = false
  71. local eu_rate = 0
  72. local result = false
  73. local above = {x=pos.x, y=pos.y+1, z=pos.z}
  74. local light = minetest.get_node_light(above, nil) or 0
  75. local tod = minetest.get_timeofday()
  76. local success, groundlevel = rc.get_ground_level_at_pos(pos)
  77. if success then
  78. if light >= 15 and tod >= 0.24 and tod <= 0.76 and pos.y >= -10 then
  79. goodenv = true
  80. local h = (pos.y - groundlevel)
  81. if h > 60 then h = 60 end
  82. if h < 0 then h = 0 end
  83. -- Normalize.
  84. h = h / 60
  85. -- Add scaling to power output.
  86. eu_rate = math_floor(POWER_OUTPUT * h)
  87. -- Clamp.
  88. if eu_rate < 1 then eu_rate = 1 end
  89. end
  90. end
  91. if goodenv then
  92. --minetest.chat_send_all("# Server: Good env!")
  93. -- Randomize time to next nodecheck.
  94. meta:set_int("chktmr", math_random(3, 15))
  95. meta:set_int("active", 1)
  96. meta:set_int("eups", eu_rate)
  97. result = true
  98. else
  99. --minetest.chat_send_all("# Server: Bad env!")
  100. meta:set_int("chktmr", 0)
  101. meta:set_int("active", 0)
  102. meta:set_int("eups", 0)
  103. result = false
  104. end
  105. meta:set_string("infotext", panel.compose_infotext(pos))
  106. return result
  107. end
  108. -- Decrement check timer.
  109. timer = timer - 1
  110. meta:set_int("chktmr", timer)
  111. -- No check performed; just return whatever the result of the last check was.
  112. return (active == 1)
  113. end
  114. panel.on_timer =
  115. function(pos, elapsed)
  116. local meta = minetest.get_meta(pos)
  117. local owner = meta:get_string("owner")
  118. local inv = meta:get_inventory()
  119. local keeprunning = false
  120. -- Check if we can produce energy from environment.
  121. -- Note that this uses a caching algorithm.
  122. local canrun = panel.check_environment(pos, meta)
  123. -- If environment is no longer producing energy,
  124. -- unload the buffered energy.
  125. if not canrun then
  126. local energy = inv:get_stack("buffer", 1)
  127. energy:set_count(net2.put_energy(pos, owner, energy:get_count(), "lv"))
  128. inv:set_stack("buffer", 1, energy)
  129. end
  130. -- Produce energy.
  131. local needdischarge = false
  132. if canrun then
  133. local eups = meta:get_int("eups")
  134. local energy = "atomic:energy " .. eups
  135. local stack = inv:get_stack("buffer", 1)
  136. if stack:get_count() >= BUFFER_SIZE then
  137. needdischarge = true
  138. end
  139. if not needdischarge then
  140. if inv:room_for_item("buffer", energy) then
  141. inv:add_item("buffer", energy)
  142. end
  143. end
  144. keeprunning = true
  145. end
  146. -- Discharge energy.
  147. if needdischarge then
  148. local energy = inv:get_stack("buffer", 1)
  149. -- Unload energy onto the network.
  150. local old = energy:get_count()
  151. energy:set_count(net2.put_energy(pos, owner, old, "lv"))
  152. inv:set_stack("buffer", 1, energy)
  153. if energy:get_count() < old then
  154. keeprunning = true
  155. else
  156. -- Batteries full? Go to sleep.
  157. keeprunning = false
  158. end
  159. end
  160. -- Determine mode (active or sleep) and set timer accordingly.
  161. if keeprunning then
  162. minetest.get_node_timer(pos):start(1.0)
  163. else
  164. -- Slow down timer during sleep periods to reduce load.
  165. minetest.get_node_timer(pos):start(math_random(1, 3*60))
  166. meta:set_int("chktmr", 0)
  167. meta:set_int("active", 0)
  168. meta:set_int("eups", 0)
  169. meta:set_string("infotext", panel.compose_infotext(pos))
  170. end
  171. end
  172. panel.on_construct =
  173. function(pos)
  174. end
  175. panel.after_place_node =
  176. function(pos, placer, itemstack, pointed_thing)
  177. local meta = minetest.get_meta(pos)
  178. local node = minetest.get_node(pos)
  179. local owner = placer:get_player_name()
  180. local inv = meta:get_inventory()
  181. meta:set_string("owner", owner)
  182. meta:set_string("nodename", node.name)
  183. inv:set_size("buffer", 1)
  184. net2.clear_caches(pos, owner, "lv")
  185. meta:set_string("formspec", panel.compose_formspec(pos))
  186. meta:set_string("infotext", panel.compose_infotext(pos))
  187. nodestore.add_node(pos)
  188. -- Start timer.
  189. local timer = minetest.get_node_timer(pos)
  190. timer:start(1.0)
  191. end
  192. panel.on_blast =
  193. function(pos)
  194. local drops = {}
  195. drops[#drops+1] = "solar:panel"
  196. minetest.remove_node(pos)
  197. return drops
  198. end
  199. panel.allow_metadata_inventory_put =
  200. function(pos, listname, index, stack, player)
  201. return 0
  202. end
  203. panel.allow_metadata_inventory_move =
  204. function(pos, from_list, from_index, to_list, to_index, count, player)
  205. return 0
  206. end
  207. panel.allow_metadata_inventory_take =
  208. function(pos, listname, index, stack, player)
  209. return 0
  210. end
  211. panel.on_metadata_inventory_move =
  212. function(pos)
  213. panel.trigger_update(pos)
  214. end
  215. panel.on_metadata_inventory_put =
  216. function(pos)
  217. panel.trigger_update(pos)
  218. end
  219. panel.on_metadata_inventory_take =
  220. function(pos, listname, index, stack, player)
  221. panel.trigger_update(pos)
  222. end
  223. panel.on_destruct =
  224. function(pos)
  225. local meta = minetest.get_meta(pos)
  226. net2.clear_caches(pos, meta:get_string("owner"), "lv")
  227. nodestore.del_node(pos)
  228. end
  229. if not panel.run_once then
  230. local nodebox = {
  231. {0, 0, 0, 16, 2, 16},
  232. }
  233. for k, v in ipairs(nodebox) do
  234. for m, n in ipairs(v) do
  235. local p = nodebox[k][m]
  236. p = p / 16
  237. p = p - 0.5
  238. nodebox[k][m] = p
  239. end
  240. end
  241. minetest.register_node(":solar:panel", {
  242. drawtype = "nodebox",
  243. description = "LV Solar Panel",
  244. tiles = {
  245. "solar_panel_top.png",
  246. "solar_panel_side.png",
  247. "solar_panel_side.png",
  248. "solar_panel_side.png",
  249. "solar_panel_side.png",
  250. "solar_panel_side.png",
  251. },
  252. node_box = {
  253. type = "fixed",
  254. fixed = nodebox,
  255. },
  256. groups = utility.dig_groups("machine"),
  257. paramtype = "light",
  258. paramtype2 = "facedir",
  259. is_ground_content = false,
  260. sounds = default.node_sound_metal_defaults(),
  261. drop = "solar:panel",
  262. on_energy_get = function(...)
  263. return panel.on_energy_get(...) end,
  264. on_rotate = function(...)
  265. return screwdriver.rotate_simple(...) end,
  266. allow_metadata_inventory_put = function(...)
  267. return panel.allow_metadata_inventory_put(...) end,
  268. allow_metadata_inventory_move = function(...)
  269. return panel.allow_metadata_inventory_move(...) end,
  270. allow_metadata_inventory_take = function(...)
  271. return panel.allow_metadata_inventory_take(...) end,
  272. on_metadata_inventory_move = function(...)
  273. return panel.on_metadata_inventory_move(...) end,
  274. on_metadata_inventory_put = function(...)
  275. return panel.on_metadata_inventory_put(...) end,
  276. on_metadata_inventory_take = function(...)
  277. return panel.on_metadata_inventory_take(...) end,
  278. on_punch = function(...)
  279. return panel.on_punch(...) end,
  280. can_dig = function(...)
  281. return panel.can_dig(...) end,
  282. on_timer = function(...)
  283. return panel.on_timer(...) end,
  284. on_construct = function(...)
  285. return panel.on_construct(...) end,
  286. on_destruct = function(...)
  287. return panel.on_destruct(...) end,
  288. on_blast = function(...)
  289. return panel.on_blast(...) end,
  290. after_place_node = function(...)
  291. return panel.after_place_node(...) end,
  292. on_player_walk_over = function(...)
  293. return panel.on_player_walk_over(...) end,
  294. })
  295. minetest.register_craft({
  296. output = 'solar:panel',
  297. recipe = {
  298. {'silicon:doped_wafer', 'silicon:doped_wafer', 'silicon:doped_wafer'},
  299. {'fine_wire:silver', 'cb2:lv', 'fine_wire:gold'},
  300. }
  301. })
  302. local c = "panel:core"
  303. local f = panel.modpath .. "/panel.lua"
  304. reload.register_file(c, f, false)
  305. panel.run_once = true
  306. end