panel.lua 8.5 KB

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