solar.lua 11 KB

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