shared.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. switching_station_hv = switching_station_hv or {}
  2. switching_station_mv = switching_station_mv or {}
  3. switching_station_lv = switching_station_lv or {}
  4. -- Create copies of these functions by tier.
  5. for k, v in ipairs({
  6. {tier="hv", up="HV"},
  7. {tier="mv", up="MV"},
  8. {tier="lv", up="LV"},
  9. }) do
  10. -- Which function table are we operating on?
  11. local functable = _G["switching_station_" .. v.tier]
  12. functable.update_formspec =
  13. function(pos)
  14. local meta = minetest.get_meta(pos)
  15. local formspec =
  16. "size[8,9.5]" ..
  17. default.formspec.get_form_colors() ..
  18. default.formspec.get_form_image() ..
  19. default.formspec.get_slot_colors()
  20. formspec = formspec ..
  21. "label[0,0.5;" .. v.up .. " Cable Box Configuration]" ..
  22. "list[context;cfg;0,1;8,1]" ..
  23. "list[current_player;main;0,5.25;8,1;]" ..
  24. "list[current_player;main;0,6.5;8,3;8]" ..
  25. default.get_hotbar_bg(0, 5.25)
  26. meta:set_string("formspec", formspec)
  27. end
  28. functable.update_infotext =
  29. function(pos)
  30. local meta = minetest.get_meta(pos)
  31. local infotext = v.up .. " Cable Box\nRouting: ["
  32. for k, v in ipairs({
  33. {n="n", c="N"},
  34. {n="s", c="S"},
  35. {n="e", c="E"},
  36. {n="w", c="W"},
  37. {n="u", c="U"},
  38. {n="d", c="D"},
  39. }) do
  40. local p = minetest.string_to_pos(meta:get_string(v.n))
  41. if p then
  42. infotext = infotext .. v.c
  43. end
  44. end
  45. infotext = infotext .. "]"
  46. meta:set_string("infotext", infotext)
  47. end
  48. functable.trigger_update =
  49. function(pos)
  50. local timer = minetest.get_node_timer(pos)
  51. if not timer:is_started() then
  52. timer:start(1.0)
  53. end
  54. end
  55. -- Typedata is used when traversing the network, without touching the node.
  56. -- It must contain as much data as needed to get the node even if unloaded.
  57. -- This must be done after node construction.
  58. -- This should also be done when punched, to allow old nodes to be upgraded.
  59. functable.initialize_typedata =
  60. function(pos)
  61. local meta = minetest.get_meta(pos)
  62. meta:set_string("technic_machine", "yes")
  63. meta:set_string("technic_type", "switch")
  64. meta:set_string("technic_tier", v.tier)
  65. meta:set_string("technic_name", "switching_station:" .. v.tier)
  66. end
  67. functable.on_punch =
  68. function(pos, node, puncher, pointed_thing)
  69. functable.initialize_typedata(pos)
  70. functable.trigger_update(pos)
  71. end
  72. functable.can_dig =
  73. function(pos, player)
  74. local meta = minetest.get_meta(pos)
  75. local inv = meta:get_inventory()
  76. return inv:is_empty("cfg")
  77. end
  78. functable.on_metadata_inventory_move =
  79. function(pos, from_list, from_index, to_list, to_index, count, player)
  80. functable.trigger_update(pos)
  81. end
  82. functable.on_metadata_inventory_put =
  83. function(pos, listname, index, stack, player)
  84. functable.trigger_update(pos)
  85. end
  86. functable.on_metadata_inventory_take =
  87. function(pos, listname, index, stack, player)
  88. functable.trigger_update(pos)
  89. end
  90. functable.allow_metadata_inventory_put =
  91. function(pos, listname, index, stack, player)
  92. local pname = player:get_player_name()
  93. if minetest.test_protection(pos, pname) then
  94. return 0
  95. end
  96. if listname == "cfg" and stack:get_name() == "cfg:dev" then
  97. return stack:get_count()
  98. end
  99. return 0
  100. end
  101. functable.allow_metadata_inventory_move =
  102. function(pos, from_list, from_index, to_list, to_index, count, player)
  103. local pname = player:get_player_name()
  104. if minetest.test_protection(pos, pname) then return 0 end
  105. return 0
  106. end
  107. functable.allow_metadata_inventory_take =
  108. function(pos, listname, index, stack, player)
  109. local pname = player:get_player_name()
  110. if minetest.test_protection(pos, pname) then return 0 end
  111. return stack:get_count()
  112. end
  113. functable.on_construct =
  114. function(pos)
  115. end
  116. -- Handle & process incomming power-network events.
  117. functable.on_machine_execute =
  118. function(pos, table_in, table_out, traversal)
  119. -- Prevent infinite recursion.
  120. -- This check must come *before* we mark the switch as processed, because
  121. -- it may be possible to reach the switch (with lesser recursion) through
  122. -- another path on the network.
  123. local recursion = (table_out.recursion or 0)
  124. if recursion > 8 then return end
  125. -- Mark node as proccessed, do not process this node more than once.
  126. -- If this node has less recorded recursion depth than current message,
  127. -- then we don't traverse this switching station. If this switching station
  128. -- doesn't have a depth recorded, then we record the depth of the message,
  129. -- and traverse this station.
  130. local hash = minetest.hash_node_position(pos)
  131. if traversal[hash] and traversal[hash] <= recursion then return end
  132. traversal[hash] = recursion
  133. local meta = minetest.get_meta(pos)
  134. --meta:set_string("infotext", "Recursion Depth: " .. recursion)
  135. -- Distribute event to all adjacent machines of the same tier as the station.
  136. --local adjacent_machines = functable.get_adjacent_machines(pos)
  137. local adjacent_machines = networks.get_adjacent_machines(pos, v.tier)
  138. for j, g in ipairs(adjacent_machines) do
  139. table_out.recursion = recursion
  140. g.on_machine_execute(g.pos, table_in, table_out, traversal)
  141. end
  142. local station_name = "switching_station:" .. v.tier
  143. local ndef = minetest.reg_ns_nodes[station_name]
  144. if not ndef or not ndef.on_machine_execute then return end
  145. -- Find all connected switching stations. Distribute events down the chain.
  146. for j, g in ipairs({
  147. {n="n"},
  148. {n="s"},
  149. {n="e"},
  150. {n="w"},
  151. {n="u"},
  152. {n="d"},
  153. }) do
  154. local p = minetest.string_to_pos(meta:get_string(g.n))
  155. if p then
  156. --local node = minetest.get_node(p)
  157. local target_meta = minetest.get_meta(p)
  158. --if node.name == station_name then
  159. if target_meta:get_string("technic_machine") == "yes" and
  160. target_meta:get_string("technic_type") == "switch" and
  161. target_meta:get_string("technic_tier") == v.tier then
  162. if target_meta:get_string("technic_name") == station_name then
  163. table_out.recursion = recursion + 1
  164. ndef.on_machine_execute(p, table_in, table_out, traversal)
  165. end
  166. end
  167. end
  168. end
  169. end
  170. functable.on_destruct =
  171. function(pos)
  172. networks.invalidate_hubs(pos, v.tier)
  173. end
  174. functable.on_blast =
  175. function(pos, intensity)
  176. networks.invalidate_hubs(pos, v.tier)
  177. local drops = {}
  178. drops[#drops+1] = "switching_station:" .. v.tier
  179. minetest.remove_node(pos)
  180. return drops
  181. end
  182. functable.on_timer =
  183. function(pos, elapsed)
  184. machines.log_update(pos, "Switching Station")
  185. networks.refresh_hubs(pos, v.tier)
  186. functable.update_formspec(pos)
  187. functable.update_infotext(pos)
  188. end
  189. functable.after_place_node =
  190. function(pos, placer, itemstack, pointed_thing)
  191. functable.initialize_typedata(pos)
  192. local meta = minetest.get_meta(pos)
  193. local inv = meta:get_inventory()
  194. inv:set_size("cfg", 8)
  195. networks.invalidate_hubs(pos, v.tier)
  196. networks.refresh_hubs(pos, v.tier)
  197. functable.update_formspec(pos)
  198. functable.update_infotext(pos)
  199. end
  200. end