functions.lua 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. distributer.update_formspec =
  2. function(pos, chg, max, cnt)
  3. local meta = minetest.get_meta(pos)
  4. local inv = meta:get_inventory()
  5. local status_string = "Network EU Status: Total Arrays: " .. cnt ..
  6. ", Total Charge: " .. chg .. "/" .. max
  7. local formspec =
  8. "size[8,8.5]" ..
  9. default.formspec.get_form_colors() ..
  10. default.formspec.get_form_image() ..
  11. default.formspec.get_slot_colors() ..
  12. "label[0,0;" .. minetest.formspec_escape(status_string) .. "]" ..
  13. "label[0,0.5;Configuration Slots]" ..
  14. "list[context;config;0,1;8,2;]" ..
  15. "list[current_player;main;0,4.25;8,1;]" ..
  16. "list[current_player;main;0,5.5;8,3;8]" ..
  17. "listring[context;config]" ..
  18. "listring[current_player;main]" ..
  19. default.get_hotbar_bg(0, 4.25)
  20. meta:set_string("formspec", formspec)
  21. end
  22. -- Typedata is used when traversing the network, without touching the node.
  23. -- It must contain as much data as needed to get the node even if unloaded.
  24. -- This must be done after node construction.
  25. -- This should also be done when punched, to allow old nodes to be upgraded.
  26. distributer.initialize_typedata =
  27. function(pos)
  28. local meta = minetest.get_meta(pos)
  29. meta:set_string("technic_machine", "yes")
  30. meta:set_string("technic_type", "utility")
  31. meta:set_string("technic_tier", "lv|mv|hv")
  32. meta:set_string("technic_name", "distributer:distributer")
  33. end
  34. distributer.on_punch =
  35. function(pos, node, puncher, pointed_thing)
  36. distributer.initialize_typedata(pos)
  37. distributer.trigger_update(pos)
  38. end
  39. distributer.can_dig =
  40. function(pos, player)
  41. local meta = minetest.get_meta(pos)
  42. local inv = meta:get_inventory()
  43. return inv:is_empty("config")
  44. end
  45. distributer.allow_metadata_inventory_put =
  46. function(pos, listname, index, stack, player)
  47. local pname = player:get_player_name()
  48. if minetest.test_protection(pos, pname) then
  49. return 0
  50. end
  51. local NCONFIG = "cfg:dev"
  52. if listname == "config" and stack:get_name() == NCONFIG then
  53. return stack:get_count()
  54. end
  55. return 0
  56. end
  57. distributer.allow_metadata_inventory_move =
  58. function(pos, from_list, from_index, to_list, to_index, count, player)
  59. local pname = player:get_player_name()
  60. if minetest.test_protection(pos, pname) then
  61. return 0
  62. end
  63. return count
  64. end
  65. distributer.allow_metadata_inventory_take =
  66. function(pos, listname, index, stack, player)
  67. local pname = player:get_player_name()
  68. if minetest.test_protection(pos, pname) then
  69. return 0
  70. end
  71. return stack:get_count()
  72. end
  73. distributer.update_infotext =
  74. function(pos, chg, max, cnt)
  75. local meta = minetest.get_meta(pos)
  76. local inv = meta:get_inventory()
  77. local infotext = "Network EU Observer\n" ..
  78. "Total Arrays: " .. cnt .. "\n" ..
  79. "Stored EUs: " .. chg .. "/" .. max .. "\n"
  80. if max > 0 then
  81. local percent = math.floor(chg / max * 100)
  82. infotext = infotext .. "Total Charge: " .. percent .. "%"
  83. else
  84. infotext = infotext .. "Total Charge: 0%"
  85. end
  86. meta:set_string("infotext", infotext)
  87. end
  88. distributer.on_timer =
  89. function(pos, elapsed)
  90. machines.log_update(pos, "Observer")
  91. local table_in = {
  92. purpose = "controler_update",
  93. }
  94. local table_out = {}
  95. local traversal = {}
  96. -- Do not process self.
  97. local hash = minetest.hash_node_position(pos)
  98. traversal[hash] = 0
  99. -- TODO: why do we unconditionally update all controlers?
  100. -- This may cause a huge update cascade, usually for no reason, since energy
  101. -- may not have changed. Also, we should only update generators when energy
  102. -- is drained. Consumers should only be auto-updated with energy increases.
  103. -- Currenty, we're just updating everybody whenever the distributer updates.
  104. -- Update controlers on all adjacent networks of any tier.
  105. local hubs = machines.get_adjacent_network_hubs(pos)
  106. if hubs then
  107. for k, v in ipairs(hubs) do
  108. local node = minetest.get_node(v)
  109. local def = minetest.reg_ns_nodes[node.name]
  110. if def and def.on_machine_execute then
  111. def.on_machine_execute(v, table_in, table_out, traversal)
  112. end
  113. end
  114. end
  115. local chg, max, cnt = distributer.get_energy_status(pos)
  116. distributer.update_infotext(pos, chg, max, cnt)
  117. distributer.update_formspec(pos, chg, max, cnt)
  118. end
  119. distributer.on_blast =
  120. function(pos)
  121. local drops = {}
  122. default.get_inventory_drops(pos, "config", drops)
  123. drops[#drops+1] = "distributer:distributer"
  124. minetest.remove_node(pos)
  125. return drops
  126. end
  127. distributer.on_construct =
  128. function(pos)
  129. distributer.initialize_typedata(pos)
  130. local meta = minetest.get_meta(pos)
  131. local inv = meta:get_inventory()
  132. inv:set_size("config", 8*2)
  133. distributer.update_infotext(pos, 0, 0, 0)
  134. distributer.update_formspec(pos, 0, 0, 0)
  135. end
  136. distributer.after_place_node =
  137. function(pos, placer, itemstack, pointed_thing)
  138. end
  139. distributer.on_metadata_inventory_move =
  140. function(pos)
  141. distributer.trigger_update(pos)
  142. end
  143. distributer.on_metadata_inventory_put =
  144. function(pos)
  145. distributer.trigger_update(pos)
  146. end
  147. distributer.on_metadata_inventory_take =
  148. function(pos)
  149. distributer.trigger_update(pos)
  150. end
  151. distributer.trigger_update =
  152. function(pos)
  153. local timer = minetest.get_node_timer(pos)
  154. if not timer:is_started() then
  155. timer:start(1.0)
  156. end
  157. end
  158. -- Read the combined energy storage and max possible storage for all batteries.
  159. -- Do not trigger an update. This function shall not be called externally.
  160. distributer.get_energy_status =
  161. function(pos)
  162. local table_in = {
  163. purpose = "get_eu_status",
  164. }
  165. local table_out = {}
  166. local traversal = {}
  167. -- Do not process self.
  168. local hash = minetest.hash_node_position(pos)
  169. traversal[hash] = 0
  170. -- Get data from all batteries on network. Any tier is allowed.
  171. local hubs = machines.get_adjacent_network_hubs(pos)
  172. if hubs then
  173. for k, v in ipairs(hubs) do
  174. local node = minetest.get_node(v)
  175. local def = minetest.reg_ns_nodes[node.name]
  176. if def and def.on_machine_execute then
  177. def.on_machine_execute(v, table_in, table_out, traversal)
  178. end
  179. end
  180. end
  181. return (table_out.eu_chg or 0),
  182. (table_out.eu_max or 0),
  183. (table_out.num_batteries or 0)
  184. end
  185. distributer.on_machine_execute =
  186. function(pos, table_in, table_out, traversal)
  187. -- No recursion check, because distributers do not take part in chains.
  188. -- This also allows them to function at the very end of a switching chain.
  189. -- Do not process this node more than once.
  190. local hash = minetest.hash_node_position(pos)
  191. if traversal[hash] then return end
  192. traversal[hash] = 0
  193. local purpose = table_in.purpose
  194. if purpose == "refresh_eu_status" then
  195. distributer.trigger_update(pos)
  196. end
  197. end
  198. if not distributer.functions_loaded then
  199. local c = "distributer:core"
  200. local f = distributer.modpath .. "/functions.lua"
  201. reload.register_file(c, f, false)
  202. distributer.functions_loaded = true
  203. end