solar.lua 11 KB

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