bat2.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. bat2 = bat2 or {}
  2. bat2_lv = bat2_lv or {}
  3. bat2_mv = bat2_mv or {}
  4. bat2_hv = bat2_hv or {}
  5. for k, v in ipairs({
  6. {tier="lv", up="LV", name="LV", buffer=tech.battery_lv.buffer},
  7. {tier="mv", up="MV", name="MV", buffer=tech.battery_mv.buffer},
  8. {tier="hv", up="HV", name="HV", buffer=tech.battery_hv.buffer},
  9. }) do
  10. -- Which function table are we operating on?
  11. local functable = _G["bat2_" .. v.tier]
  12. functable.on_energy_put =
  13. function(pos, energy)
  14. --minetest.chat_send_all("# Server: Got " .. energy .. " energy!")
  15. local meta = minetest.get_meta(pos)
  16. local chg, max = functable.get_energy_status(meta)
  17. local canfit = max - chg
  18. local toput = energy
  19. if toput > canfit then
  20. toput = canfit
  21. end
  22. local total = chg + toput
  23. meta:set_int("energy", total)
  24. energy = energy - toput
  25. functable.trigger_update(pos)
  26. return energy
  27. end
  28. functable.on_energy_get =
  29. function(pos, energy)
  30. local meta = minetest.get_meta(pos)
  31. local have = meta:get_int("energy")
  32. if have < energy then
  33. meta:set_int("energy", 0)
  34. functable.trigger_update(pos)
  35. return have
  36. end
  37. have = have - energy
  38. meta:set_int("energy", have)
  39. functable.trigger_update(pos)
  40. return energy
  41. end
  42. functable.compose_infotext =
  43. function(pos)
  44. local meta = minetest.get_meta(pos)
  45. local inv = meta:get_inventory()
  46. local chg, max = functable.get_energy_status(meta)
  47. local size = inv:get_size("batteries")
  48. local cnt = functable.get_battery_count(inv)
  49. local infotext = v.up .. " Battery Array\n" ..
  50. "Internal Battery Units: " .. cnt .. "/" .. size .. "\n" ..
  51. "Energy: " .. chg .. "/" .. max .. " EUs\n"
  52. if max > 0 then
  53. local percent = math.floor(chg / max * 100)
  54. infotext = infotext .. "Charge: " .. percent .. "%"
  55. else
  56. infotext = infotext .. "Charge: 0%"
  57. end
  58. meta:set_string("infotext", infotext)
  59. end
  60. functable.compose_formspec =
  61. function(pos)
  62. local meta = minetest.get_meta(pos)
  63. local chg, max = functable.get_energy_status(meta)
  64. local charge_desc = v.name .. " Charge Status: " ..
  65. chg .. "/" .. max .. " EUs"
  66. local formspec =
  67. "size[8,8.5]" ..
  68. default.formspec.get_form_colors() ..
  69. default.formspec.get_form_image() ..
  70. default.formspec.get_slot_colors() ..
  71. "label[0,0;" .. minetest.formspec_escape(charge_desc) .. "]" ..
  72. "label[0,0.5;NRAIB (Non-Redundant Array of Independant Batteries)]" ..
  73. "item_image[7,0;1,1;battery:battery]" ..
  74. "list[context;batteries;0,1;8,2;]" ..
  75. "list[current_player;main;0,4.25;8,1;]" ..
  76. "list[current_player;main;0,5.5;8,3;8]" ..
  77. "listring[context;batteries]"..
  78. "listring[current_player;main]"..
  79. default.get_hotbar_bg(0, 4.25)
  80. meta:set_string("formspec", formspec)
  81. end
  82. functable.update_charge_visual =
  83. function(pos)
  84. local meta = minetest.get_meta(pos)
  85. local chg, max = functable.get_energy_status(meta)
  86. local name = "bat2:bt0_" .. v.tier
  87. if max > 0 then -- Avoid divide-by-zero.
  88. local percent = math.floor((chg / max) * 100)
  89. local sz = math.ceil(100 / 12)
  90. for i = 0, 12, 1 do
  91. if percent <= sz*i then
  92. name = "bat2:bt" .. i .. "_" .. v.tier
  93. break
  94. end
  95. end
  96. end
  97. machines.swap_node(pos, name)
  98. end
  99. functable.on_punch =
  100. function(pos, node, puncher, pointed_thing)
  101. functable.trigger_update(pos)
  102. functable.privatize(minetest.get_meta(pos))
  103. end
  104. functable.can_dig =
  105. function(pos, player)
  106. local meta = minetest.get_meta(pos)
  107. local inv = meta:get_inventory()
  108. return inv:is_empty("batteries")
  109. end
  110. functable.allow_metadata_inventory_put =
  111. function(pos, listname, index, stack, player)
  112. local NBATT = "battery:battery"
  113. local pname = player:get_player_name()
  114. if minetest.test_protection(pos, pname) then
  115. return 0
  116. end
  117. if stack:get_name() == NBATT then
  118. return stack:get_count()
  119. end
  120. return 0
  121. end
  122. functable.allow_metadata_inventory_move =
  123. function(pos, from_list, from_index, to_list, to_index, count, player)
  124. local meta = minetest.get_meta(pos)
  125. local inv = meta:get_inventory()
  126. local stack = inv:get_stack(from_list, from_index)
  127. return functable.allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
  128. end
  129. functable.allow_metadata_inventory_take =
  130. function(pos, listname, index, stack, player)
  131. local pname = player:get_player_name()
  132. if minetest.test_protection(pos, pname) then
  133. return 0
  134. end
  135. return stack:get_count()
  136. end
  137. functable.get_battery_count =
  138. function(inv)
  139. local batteries = inv:get_list("batteries")
  140. local count = 0
  141. for k, v in ipairs(batteries) do
  142. if v:get_name() == "battery:battery" then
  143. -- Only 1 battery allowed per stack.
  144. count = count + 1
  145. end
  146. end
  147. return count
  148. end
  149. functable.update_maximum_charge =
  150. function(meta)
  151. local count = functable.get_battery_count(meta:get_inventory())
  152. local max
  153. if v.tier == "lv" then
  154. max = count * v.buffer
  155. elseif v.tier == "mv" then
  156. max = count * v.buffer
  157. elseif v.tier == "hv" then
  158. max = count * v.buffer
  159. end
  160. local chg = meta:get_int("energy")
  161. -- Ensure charge isn't over max. This can happen if user removed a battery.
  162. if chg > max then
  163. meta:set_int("energy", max)
  164. end
  165. meta:set_int("max", max)
  166. end
  167. functable.on_timer =
  168. function(pos, elapsed)
  169. local meta = minetest.get_meta(pos)
  170. local current_amount = meta:get_int("energy")
  171. local old_eu_amount = meta:get_int("old_eu")
  172. -- Todo: here, we can respond to changes in EU amount since last update.
  173. meta:set_int("old_eu", current_amount)
  174. -- Needed in case the operator removes or adds a battery.
  175. -- Also, EUs can be added/drained from batteries without going through a distributer.
  176. functable.update_maximum_charge(meta)
  177. functable.update_charge_visual(pos)
  178. functable.compose_infotext(pos)
  179. functable.compose_formspec(pos)
  180. end
  181. functable.on_blast =
  182. function(pos)
  183. local drops = {}
  184. default.get_inventory_drops(pos, "batteries", drops)
  185. drops[#drops+1] = "bat2:bt0_" .. v.tier
  186. minetest.remove_node(pos)
  187. return drops
  188. end
  189. functable.on_construct =
  190. function(pos)
  191. local meta = minetest.get_meta(pos)
  192. local inv = meta:get_inventory()
  193. inv:set_size("batteries", 8*2)
  194. functable.update_maximum_charge(meta)
  195. functable.compose_infotext(pos)
  196. functable.compose_formspec(pos)
  197. meta:set_string("nodename", "DUMMY")
  198. meta:set_string("owner", "DUMMY")
  199. meta:set_int("energy", 0)
  200. meta:set_int("old_eu", 0)
  201. meta:set_int("max", 0)
  202. functable.privatize(meta)
  203. end
  204. functable.privatize =
  205. function(meta)
  206. meta:mark_as_private({
  207. "nodename",
  208. "owner",
  209. "energy",
  210. "old_eu",
  211. "max",
  212. })
  213. end
  214. functable.on_destruct =
  215. function(pos)
  216. local meta = minetest.get_meta(pos)
  217. net2.clear_caches(pos, meta:get_string("owner"), v.tier)
  218. nodestore.del_node(pos)
  219. end
  220. functable.after_place_node =
  221. function(pos, placer, itemstack, pointed_thing)
  222. local meta = minetest.get_meta(pos)
  223. local owner = placer:get_player_name()
  224. meta:set_string("nodename", minetest.get_node(pos).name)
  225. meta:set_string("owner", owner)
  226. net2.clear_caches(pos, owner, v.tier)
  227. nodestore.add_node(pos)
  228. end
  229. functable.on_metadata_inventory_move =
  230. function(pos)
  231. functable.trigger_update(pos)
  232. end
  233. functable.on_metadata_inventory_put =
  234. function(pos)
  235. functable.trigger_update(pos)
  236. end
  237. functable.on_metadata_inventory_take =
  238. function(pos)
  239. functable.trigger_update(pos)
  240. end
  241. functable.trigger_update =
  242. function(pos)
  243. local timer = minetest.get_node_timer(pos)
  244. if not timer:is_started() then
  245. timer:start(1.0)
  246. end
  247. end
  248. -- Read the current & max charge of the battery, but do not trigger any update.
  249. -- Function shall be used internally ONLY.
  250. functable.get_energy_status =
  251. function(meta)
  252. local chg = meta:get_int("energy")
  253. local max = meta:get_int("max")
  254. return chg, max
  255. end
  256. end
  257. if not bat2.run_once then
  258. local nodebox = {
  259. {0, 0, 0, 5, 16, 5},
  260. {11, 0, 0, 16, 16, 5},
  261. {0, 0, 11, 5, 16, 16},
  262. {11, 0, 11, 16, 16, 16},
  263. {1, 1, 1, 15, 15, 15},
  264. {0, 0, 0, 16, 1, 16},
  265. {0, 15, 0, 16, 16, 16},
  266. }
  267. local selectbox = {
  268. {0, 0, 0, 16, 16, 16},
  269. }
  270. utility.transform_nodebox(nodebox)
  271. utility.transform_nodebox(selectbox)
  272. for k, v in ipairs({
  273. {tier="lv", title="LV"},
  274. {tier="mv", title="MV"},
  275. {tier="hv", title="HV"},
  276. }) do
  277. -- Register 13 nodes for each tier; each node has a different texture set to show the charge level.
  278. for i = 0, 12, 1 do
  279. -- Which function table are we operating on?
  280. local functable = _G["bat2_" .. v.tier]
  281. minetest.register_node(":bat2:bt" .. i .. "_" .. v.tier, {
  282. drawtype = "nodebox",
  283. description = v.title .. " Battery Box",
  284. tiles = {
  285. "technic_" .. v.tier .. "_battery_box_top.png",
  286. "technic_" .. v.tier .. "_battery_box_bottom.png",
  287. "technic_" .. v.tier .. "_battery_box_side.png^battery_meter" .. i .. ".png",
  288. "technic_" .. v.tier .. "_battery_box_side.png^battery_meter" .. i .. ".png",
  289. "technic_" .. v.tier .. "_battery_box_side.png^battery_meter" .. i .. ".png",
  290. "technic_" .. v.tier .. "_battery_box_side.png^battery_meter" .. i .. ".png",
  291. },
  292. groups = utility.dig_groups("machine"),
  293. paramtype = "light",
  294. paramtype2 = "facedir",
  295. is_ground_content = false,
  296. sounds = default.node_sound_metal_defaults(),
  297. drop = "bat2:bt0_" .. v.tier,
  298. node_box = {
  299. type = "fixed",
  300. fixed = nodebox,
  301. },
  302. selection_box = {
  303. type = "fixed",
  304. fixed = selectbox,
  305. },
  306. on_energy_put = function(...)
  307. return functable.on_energy_put(...) end,
  308. on_energy_get = function(...)
  309. return functable.on_energy_get(...) end,
  310. on_punch = function(...)
  311. return functable.on_punch(...) end,
  312. can_dig = function(...)
  313. return functable.can_dig(...) end,
  314. on_timer = function(...)
  315. return functable.on_timer(...) end,
  316. on_construct = function(...)
  317. return functable.on_construct(...) end,
  318. on_destruct = function(...)
  319. return functable.on_destruct(...) end,
  320. after_place_node = function(...)
  321. return functable.after_place_node(...) end,
  322. on_metadata_inventory_move = function(...)
  323. return functable.on_metadata_inventory_move(...) end,
  324. on_metadata_inventory_put = function(...)
  325. return functable.on_metadata_inventory_put(...) end,
  326. on_metadata_inventory_take = function(...)
  327. return functable.on_metadata_inventory_take(...) end,
  328. on_blast = function(...)
  329. return functable.on_blast(...) end,
  330. on_rotate = function(...)
  331. return screwdriver.rotate_simple(...) end,
  332. allow_metadata_inventory_put = function(...)
  333. return functable.allow_metadata_inventory_put(...) end,
  334. allow_metadata_inventory_move = function(...)
  335. return functable.allow_metadata_inventory_move(...) end,
  336. allow_metadata_inventory_take = function(...)
  337. return functable.allow_metadata_inventory_take(...) end,
  338. })
  339. end
  340. end
  341. local c = "bat2:core"
  342. local f = battery.modpath .. "/bat2.lua"
  343. reload.register_file(c, f, false)
  344. bat2.run_once = true
  345. end