solar.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. solar = solar or {}
  2. solar.modpath = minetest.get_modpath("machines")
  3. solar_lv = solar_lv or {}
  4. solar_mv = solar_mv or {}
  5. solar_hv = solar_hv or {}
  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. player:set_hp(player:get_hp() - 1)
  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 owner == "MustTest" then
  140. -- if canrun then
  141. -- minetest.chat_send_player("MustTest", "YES")
  142. -- else
  143. -- minetest.chat_send_player("MustTest", "NO")
  144. -- end
  145. --end
  146. -- If environment is no longer producing energy,
  147. -- unload the buffered energy.
  148. if not canrun then
  149. local energy = inv:get_stack("buffer", 1)
  150. energy:set_count(net2.put_energy(pos, owner, energy:get_count(), v.tier))
  151. inv:set_stack("buffer", 1, "atomic:energy " .. energy:get_count())
  152. end
  153. -- Produce energy.
  154. local needdischarge = false
  155. if canrun then
  156. local eups = meta:get_int("eups")
  157. local energy = "atomic:energy " .. eups
  158. local stack = inv:get_stack("buffer", 1)
  159. if stack:get_count() >= v.buffer then
  160. needdischarge = true
  161. end
  162. if not needdischarge then
  163. if inv:room_for_item("buffer", energy) then
  164. inv:add_item("buffer", energy)
  165. end
  166. end
  167. keeprunning = true
  168. end
  169. -- Discharge energy.
  170. if needdischarge then
  171. local energy = inv:get_stack("buffer", 1)
  172. -- Unload energy onto the network.
  173. local old = energy:get_count()
  174. energy:set_count(net2.put_energy(pos, owner, old, v.tier))
  175. inv:set_stack("buffer", 1, "atomic:energy " .. energy:get_count())
  176. if energy:get_count() < old then
  177. keeprunning = true
  178. else
  179. -- Batteries full? Go to sleep.
  180. keeprunning = false
  181. end
  182. end
  183. -- Determine mode (active or sleep) and set timer accordingly.
  184. if keeprunning then
  185. minetest.get_node_timer(pos):start(1.0)
  186. else
  187. -- Slow down timer during sleep periods to reduce load.
  188. minetest.get_node_timer(pos):start(math_random(1, 3*60))
  189. meta:set_int("chktmr", 0)
  190. meta:set_int("active", 0)
  191. meta:set_int("eups", 0)
  192. meta:set_string("infotext", func.compose_infotext(pos))
  193. end
  194. end
  195. func.on_construct =
  196. function(pos)
  197. end
  198. func.after_place_node =
  199. function(pos, placer, itemstack, pointed_thing)
  200. local meta = minetest.get_meta(pos)
  201. local node = minetest.get_node(pos)
  202. local owner = placer:get_player_name()
  203. local inv = meta:get_inventory()
  204. meta:set_string("owner", owner)
  205. meta:set_string("nodename", node.name)
  206. --meta:set_string("infotext", v.up2 .. " Solar Array")
  207. inv:set_size("buffer", 1)
  208. net2.clear_caches(pos, owner, v.tier)
  209. meta:set_string("formspec", func.compose_formspec(pos))
  210. meta:set_string("infotext", func.compose_infotext(pos))
  211. nodestore.add_node(pos)
  212. -- Start timer.
  213. local timer = minetest.get_node_timer(pos)
  214. timer:start(1.0)
  215. end
  216. func.on_blast =
  217. function(pos)
  218. local drops = {}
  219. drops[#drops+1] = "solar:" .. v.tier
  220. minetest.remove_node(pos)
  221. return drops
  222. end
  223. func.allow_metadata_inventory_put =
  224. function(pos, listname, index, stack, player)
  225. return 0
  226. end
  227. func.allow_metadata_inventory_move =
  228. function(pos, from_list, from_index, to_list, to_index, count, player)
  229. return 0
  230. end
  231. func.allow_metadata_inventory_take =
  232. function(pos, listname, index, stack, player)
  233. return 0
  234. end
  235. func.on_metadata_inventory_move =
  236. function(pos)
  237. func.trigger_update(pos)
  238. end
  239. func.on_metadata_inventory_put =
  240. function(pos)
  241. func.trigger_update(pos)
  242. end
  243. func.on_metadata_inventory_take =
  244. function(pos, listname, index, stack, player)
  245. func.trigger_update(pos)
  246. end
  247. func.on_destruct =
  248. function(pos)
  249. local meta = minetest.get_meta(pos)
  250. net2.clear_caches(pos, meta:get_string("owner"), v.tier)
  251. nodestore.del_node(pos)
  252. end
  253. end
  254. if not solar.run_once then
  255. for k, v in ipairs({
  256. {tier="lv", up="LV"},
  257. {tier="mv", up="MV"},
  258. {tier="hv", up="HV"},
  259. }) do
  260. -- Which function table are we operating on?
  261. local func = _G["solar_" .. v.tier]
  262. minetest.register_node(":solar:" .. v.tier, {
  263. drawtype = "nodebox",
  264. description = "Arrayed Solar " .. v.up .. " Generator",
  265. tiles = {
  266. "technic_" .. v.tier .. "_solar_array_top.png",
  267. "technic_" .. v.tier .. "_solar_array_bottom.png",
  268. "technic_" .. v.tier .. "_solar_array_side.png",
  269. "technic_" .. v.tier .. "_solar_array_side.png",
  270. "technic_" .. v.tier .. "_solar_array_side.png",
  271. "technic_" .. v.tier .. "_solar_array_side.png",
  272. },
  273. node_box = {
  274. type = "fixed",
  275. fixed = {
  276. {-0.5, -0.5, -0.5, 0.5, -((0.5/8)*4), 0.5},
  277. {-0.5, -((0.5/8)*6), -((0.5/8)*3), 0.5, -((0.5/8)*3), ((0.5/8)*3)},
  278. {-((0.5/8)*3), -((0.5/8)*6), -0.5, ((0.5/8)*3), -((0.5/8)*3), 0.5},
  279. },
  280. },
  281. groups = utility.dig_groups("machine"),
  282. paramtype = "light",
  283. paramtype2 = "facedir",
  284. is_ground_content = false,
  285. sounds = default.node_sound_metal_defaults(),
  286. drop = "solar:" .. v.tier,
  287. on_energy_get = function(...)
  288. return func.on_energy_get(...) end,
  289. on_rotate = function(...)
  290. return screwdriver.rotate_simple(...) end,
  291. allow_metadata_inventory_put = function(...)
  292. return func.allow_metadata_inventory_put(...) end,
  293. allow_metadata_inventory_move = function(...)
  294. return func.allow_metadata_inventory_move(...) end,
  295. allow_metadata_inventory_take = function(...)
  296. return func.allow_metadata_inventory_take(...) end,
  297. on_metadata_inventory_move = function(...)
  298. return func.on_metadata_inventory_move(...) end,
  299. on_metadata_inventory_put = function(...)
  300. return func.on_metadata_inventory_put(...) end,
  301. on_metadata_inventory_take = function(...)
  302. return func.on_metadata_inventory_take(...) end,
  303. on_punch = function(...)
  304. return func.on_punch(...) end,
  305. can_dig = function(...)
  306. return func.can_dig(...) end,
  307. on_timer = function(...)
  308. return func.on_timer(...) end,
  309. on_construct = function(...)
  310. return func.on_construct(...) end,
  311. on_destruct = function(...)
  312. return func.on_destruct(...) end,
  313. on_blast = function(...)
  314. return func.on_blast(...) end,
  315. after_place_node = function(...)
  316. return func.after_place_node(...) end,
  317. on_player_walk_over = function(...)
  318. return func.on_player_walk_over(...) end,
  319. })
  320. end
  321. minetest.register_craft({
  322. output = 'solar:lv',
  323. recipe = {
  324. {'solar:panel', 'solar:panel', 'solar:panel'},
  325. {'carbon_steel:ingot', 'transformer:lv', 'carbon_steel:ingot'},
  326. {'', 'cb2:lv', ''},
  327. }
  328. })
  329. minetest.register_craft({
  330. output = 'solar:mv',
  331. recipe = {
  332. {'solar:lv', 'solar:lv', 'solar:lv'},
  333. {'carbon_steel:ingot', 'transformer:mv', 'carbon_steel:ingot'},
  334. {'', 'cb2:mv', ''},
  335. }
  336. })
  337. minetest.register_craft({
  338. output = 'solar:hv',
  339. recipe = {
  340. {'solar:mv', 'solar:mv', 'solar:mv'},
  341. {'techcrafts:carbon_plate', 'transformer:hv', 'techcrafts:composite_plate'},
  342. {'', 'cb2:hv', ''},
  343. }
  344. })
  345. local c = "solar:core"
  346. local f = solar.modpath .. "/solar.lua"
  347. reload.register_file(c, f, false)
  348. solar.run_once = true
  349. end