functions.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. -- Function tables, per tier.
  2. battery_lv = battery_lv or {}
  3. battery_mv = battery_mv or {}
  4. battery_hv = battery_hv or {}
  5. for k, v in ipairs({
  6. {tier="lv", up="LV", name="Low-Voltage"},
  7. {tier="mv", up="MV", name="Medium-Voltage"},
  8. {tier="hv", up="HV", name="High-Voltage"},
  9. }) do
  10. -- Which function table are we operating on?
  11. local functable = _G["battery_" .. v.tier]
  12. -- Typedata is used when traversing the network, without touching the node.
  13. -- It must contain as much data as needed to get the node even if unloaded.
  14. -- This must be done after node construction.
  15. -- This should also be done when punched, to allow old nodes to be upgraded.
  16. functable.initialize_typedata =
  17. function(pos)
  18. local meta = minetest.get_meta(pos)
  19. meta:set_string("technic_machine", "yes")
  20. meta:set_string("technic_type", "battery")
  21. meta:set_string("technic_tier", v.tier)
  22. -- This will technically be the wrong name, most of the time, but we should
  23. -- only ever use it to look up the node definition in order to get functions.
  24. -- And the functions for arrays 0 through 12 should be all the same.
  25. meta:set_string("technic_name", "battery:array0_" .. v.tier)
  26. end
  27. functable.compose_infotext =
  28. function(pos)
  29. local meta = minetest.get_meta(pos)
  30. local inv = meta:get_inventory()
  31. local chg = meta:get_int("energy")
  32. local max = meta:get_int("max")
  33. local size = inv:get_size("batteries")
  34. local cnt = functable.get_battery_count(inv)
  35. local infotext = v.up .. " Battery Array\n" ..
  36. "Internal Battery Units: " .. cnt .. "/" .. size .. "\n" ..
  37. "Energy: " .. chg .. "/" .. max .. " EUs\n"
  38. if max > 0 then
  39. local percent = math.floor(chg / max * 100)
  40. infotext = infotext .. "Charge: " .. percent .. "%"
  41. else
  42. infotext = infotext .. "Charge: 0%"
  43. end
  44. meta:set_string("infotext", infotext)
  45. end
  46. functable.compose_formspec =
  47. function(pos)
  48. local meta = minetest.get_meta(pos)
  49. local chg, max = functable.get_energy_status(meta)
  50. local charge_desc = v.name .. " Charge Status: " ..
  51. chg .. "/" .. max .. " EUs"
  52. local formspec =
  53. "size[8,8.5]" ..
  54. default.formspec.get_form_colors() ..
  55. default.formspec.get_form_image() ..
  56. default.formspec.get_slot_colors() ..
  57. "label[0,0;" .. minetest.formspec_escape(charge_desc) .. "]" ..
  58. "label[0,0.5;NRAIB (Non-Redundant Array of Independant Batteries)]" ..
  59. "list[context;batteries;0,1;6,2;]" ..
  60. "label[7,0.5;Config]" ..
  61. "list[context;cfg;7,1;1,2;]" ..
  62. "list[current_player;main;0,4.25;8,1;]" ..
  63. "list[current_player;main;0,5.5;8,3;8]" ..
  64. default.get_hotbar_bg(0, 4.25)
  65. meta:set_string("formspec", formspec)
  66. end
  67. functable.update_charge_visual =
  68. function(pos)
  69. local meta = minetest.get_meta(pos)
  70. local chg, max = functable.get_energy_status(meta)
  71. local name = "battery:array0_" .. v.tier
  72. if max > 0 then -- Avoid divide-by-zero.
  73. local percent = math.floor((chg / max) * 100)
  74. local sz = math.ceil(100 / 12)
  75. for i = 0, 12, 1 do
  76. if percent <= sz*i then
  77. name = "battery:array" .. i .. "_" .. v.tier
  78. break
  79. end
  80. end
  81. end
  82. machines.swap_node(pos, name)
  83. end
  84. functable.on_punch =
  85. function(pos, node, puncher, pointed_thing)
  86. functable.initialize_typedata(pos)
  87. functable.trigger_update(pos)
  88. end
  89. functable.can_dig =
  90. function(pos, player)
  91. local meta = minetest.get_meta(pos)
  92. local inv = meta:get_inventory()
  93. return inv:is_empty("batteries") and
  94. inv:is_empty("cfg")
  95. end
  96. functable.allow_metadata_inventory_put =
  97. function(pos, listname, index, stack, player)
  98. local NCONF = "cfg:dev"
  99. local NBATT = "battery:battery"
  100. local pname = player:get_player_name()
  101. if minetest.test_protection(pos, pname) then
  102. return 0
  103. end
  104. if listname == "cfg" and stack:get_name() == NCONF then
  105. return stack:get_count()
  106. elseif listname == "batteries" and stack:get_name() == NBATT then
  107. return stack:get_count()
  108. end
  109. return 0
  110. end
  111. functable.allow_metadata_inventory_move =
  112. function(pos, from_list, from_index, to_list, to_index, count, player)
  113. local meta = minetest.get_meta(pos)
  114. local inv = meta:get_inventory()
  115. local stack = inv:get_stack(from_list, from_index)
  116. return functable.allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
  117. end
  118. functable.allow_metadata_inventory_take =
  119. function(pos, listname, index, stack, player)
  120. local pname = player:get_player_name()
  121. if minetest.test_protection(pos, pname) then
  122. return 0
  123. end
  124. return stack:get_count()
  125. end
  126. functable.get_battery_count =
  127. function(inv)
  128. local batteries = inv:get_list("batteries")
  129. local count = 0
  130. for k, v in ipairs(batteries) do
  131. if v:get_name() == "battery:battery" then
  132. -- Only 1 battery allowed per stack.
  133. count = count + 1
  134. end
  135. end
  136. return count
  137. end
  138. functable.update_maximum_charge =
  139. function(meta)
  140. local count = functable.get_battery_count(meta:get_inventory())
  141. local max
  142. if v.tier == "lv" then
  143. max = count * 10000
  144. elseif v.tier == "mv" then
  145. max = count * 50000
  146. elseif v.tier == "hv" then
  147. max = count * 120000
  148. end
  149. local chg = meta:get_int("energy")
  150. -- Ensure charge isn't over max. This can happen if user removed a battery.
  151. if chg > max then
  152. meta:set_int("energy", max)
  153. end
  154. meta:set_int("max", max)
  155. end
  156. functable.on_timer =
  157. function(pos, elapsed)
  158. machines.log_update(pos, "Battery Array")
  159. local meta = minetest.get_meta(pos)
  160. local current_amount = meta:get_int("energy")
  161. local old_eu_amount = meta:get_int("old_eu")
  162. -- Todo: here, we can respond to changes in EU amount since last update.
  163. meta:set_int("old_eu", current_amount)
  164. -- Needed in case the operator removes or adds a battery.
  165. -- Also, EUs can be added/drained from batteries without going through a distributer.
  166. functable.update_maximum_charge(meta)
  167. functable.update_listeners(pos)
  168. functable.update_charge_visual(pos)
  169. functable.compose_infotext(pos)
  170. functable.compose_formspec(pos)
  171. end
  172. functable.update_listeners =
  173. function(pos)
  174. local table_in = {
  175. purpose = "refresh_eu_status",
  176. }
  177. local table_out = {}
  178. local traversal = {}
  179. -- Do not process self.
  180. local hash = minetest.hash_node_position(pos)
  181. traversal[hash] = 0
  182. -- Update any listening EU observers on the same network tier.
  183. local hubs = machines.get_adjacent_network_hubs(pos, {v.tier})
  184. if hubs then
  185. for k, v in ipairs(hubs) do
  186. local node = minetest.get_node(v)
  187. local def = minetest.reg_ns_nodes[node.name]
  188. if def and def.on_machine_execute then
  189. def.on_machine_execute(v, table_in, table_out, traversal)
  190. end
  191. end
  192. end
  193. end
  194. functable.on_blast =
  195. function(pos)
  196. local drops = {}
  197. default.get_inventory_drops(pos, "batteries", drops)
  198. default.get_inventory_drops(pos, "cfg", drops)
  199. drops[#drops+1] = "battery:array0_" .. v.tier
  200. minetest.remove_node(pos)
  201. return drops
  202. end
  203. functable.on_construct =
  204. function(pos)
  205. functable.initialize_typedata(pos)
  206. local meta = minetest.get_meta(pos)
  207. local inv = meta:get_inventory()
  208. inv:set_size("batteries", 6*2)
  209. inv:set_size("cfg", 2)
  210. functable.update_maximum_charge(meta)
  211. functable.compose_infotext(pos)
  212. functable.compose_formspec(pos)
  213. end
  214. functable.after_place_node =
  215. function(pos, placer, itemstack, pointed_thing)
  216. end
  217. functable.on_metadata_inventory_move =
  218. function(pos)
  219. functable.trigger_update(pos)
  220. end
  221. functable.on_metadata_inventory_put =
  222. function(pos)
  223. functable.trigger_update(pos)
  224. end
  225. functable.on_metadata_inventory_take =
  226. function(pos)
  227. functable.trigger_update(pos)
  228. end
  229. functable.trigger_update =
  230. function(pos)
  231. local timer = minetest.get_node_timer(pos)
  232. if not timer:is_started() then
  233. timer:start(1.0)
  234. end
  235. end
  236. -- Read the current & max charge of the battery, but do not trigger any update.
  237. -- Function shall be used internally ONLY.
  238. functable.get_energy_status =
  239. function(meta)
  240. local chg = meta:get_int("energy")
  241. local max = meta:get_int("max")
  242. return chg, max
  243. end
  244. functable.on_machine_execute =
  245. function(pos, table_in, table_out, traversal)
  246. -- We do not check for recursion depth for battery array boxes because
  247. -- these cannot be chains, so there is no problem with network size.
  248. -- Do not process this node more than once.
  249. local hash = minetest.hash_node_position(pos)
  250. if traversal[hash] then return end
  251. traversal[hash] = 0
  252. local meta = minetest.get_meta(pos)
  253. local purpose = table_in.purpose
  254. if purpose == "get_eu_status" then
  255. local chg, max = functable.get_energy_status(meta)
  256. table_out.eu_chg = (table_out.eu_chg or 0) + chg
  257. table_out.eu_max = (table_out.eu_max or 0) + max
  258. table_out.num_batteries = (table_out.num_batteries or 0) + 1
  259. elseif purpose == "store_eu" then
  260. if table_out.amount_eu <= 0 then return end
  261. local chg = meta:get_int("energy")
  262. local max = meta:get_int("max")
  263. -- Don't trigger an update cascade unless something changed.
  264. if chg < max then
  265. local amount = (table_out.amount_eu or 0)
  266. -- Clamp the amount the battery receives to its max capacity.
  267. -- This allows us to correctly calculate the remaining charge,
  268. -- after putting as much as possible in the battery.
  269. if amount + chg > max then
  270. amount = max - chg
  271. end
  272. if amount >= 1 then
  273. local chg = chg + amount
  274. if chg > max then
  275. chg = max
  276. end
  277. meta:set_int("energy", chg)
  278. functable.trigger_update(pos)
  279. end
  280. -- Calculate remaining charge.
  281. table_out.amount_eu = (table_out.amount_eu or 0) - amount
  282. end
  283. elseif purpose == "retrieve_eu" then
  284. if (table_out.wanted_eu or 0) <= 0 then return end
  285. local wanted_eu = (table_out.wanted_eu or 0)
  286. local current_eu = meta:get_int("energy")
  287. local eu_retrieved = 0
  288. -- Do *not* drain battery if we don't have enough EU. Just return 0.
  289. if current_eu >= wanted_eu then
  290. eu_retrieved = wanted_eu
  291. current_eu = current_eu - wanted_eu
  292. table_out.gotten_eu = (table_out.gotten_eu or 0) + eu_retrieved
  293. table_out.wanted_eu = (table_out.wanted_eu or 0) - eu_retrieved
  294. meta:set_int("energy", current_eu)
  295. end
  296. -- Don't trigger an update cascade unless something changed.
  297. if eu_retrieved > 0 then -- Energy was drained; need update.
  298. functable.trigger_update(pos)
  299. end
  300. end
  301. end
  302. if not battery.functions_loaded then
  303. local c = "battery:core"
  304. local f = battery.modpath .. "/functions.lua"
  305. reload.register_file(c, f, false)
  306. battery.functions_loaded = true
  307. end
  308. end