functions.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. converter.update_formspec =
  2. function(pos)
  3. local meta = minetest.get_meta(pos)
  4. local formspec =
  5. "size[8,9.5]" ..
  6. default.formspec.get_form_colors() ..
  7. default.formspec.get_form_image() ..
  8. default.formspec.get_slot_colors()
  9. formspec = formspec ..
  10. "label[0,0.5;Supply Converter Configuration]" ..
  11. "list[context;cfg;0,1;8,1]" ..
  12. "list[current_player;main;0,5.25;8,1;]" ..
  13. "list[current_player;main;0,6.5;8,3;8]" ..
  14. default.get_hotbar_bg(0, 5.25)
  15. meta:set_string("formspec", formspec)
  16. end
  17. converter.update_infotext =
  18. function(pos)
  19. local meta = minetest.get_meta(pos)
  20. local infotext = "Supply Converter"
  21. meta:set_string("infotext", infotext)
  22. end
  23. converter.trigger_update =
  24. function(pos)
  25. local timer = minetest.get_node_timer(pos)
  26. if not timer:is_started() then
  27. timer:start(1.0)
  28. end
  29. end
  30. -- Typedata is used when traversing the network, without touching the node.
  31. -- It must contain as much data as needed to get the node even if unloaded.
  32. -- This must be done after node construction.
  33. -- This should also be done when punched, to allow old nodes to be upgraded.
  34. converter.initialize_typedata =
  35. function(pos)
  36. local meta = minetest.get_meta(pos)
  37. meta:set_string("technic_machine", "yes")
  38. meta:set_string("technic_type", "utility")
  39. meta:set_string("technic_tier", "lv|mv|hv")
  40. meta:set_string("technic_name", "converter:converter")
  41. end
  42. converter.on_punch =
  43. function(pos, node, puncher, pointed_thing)
  44. converter.initialize_typedata(pos)
  45. converter.trigger_update(pos)
  46. end
  47. converter.can_dig =
  48. function(pos, player)
  49. local meta = minetest.get_meta(pos)
  50. local inv = meta:get_inventory()
  51. return inv:is_empty("cfg")
  52. end
  53. converter.on_metadata_inventory_move =
  54. function(pos, from_list, from_index, to_list, to_index, count, player)
  55. converter.trigger_update(pos)
  56. end
  57. converter.on_metadata_inventory_put =
  58. function(pos, listname, index, stack, player)
  59. converter.trigger_update(pos)
  60. end
  61. converter.on_metadata_inventory_take =
  62. function(pos, listname, index, stack, player)
  63. converter.trigger_update(pos)
  64. end
  65. converter.allow_metadata_inventory_put =
  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. if listname == "cfg" and stack:get_name() == "cfg:dev" then
  72. return stack:get_count()
  73. end
  74. return 0
  75. end
  76. converter.allow_metadata_inventory_move =
  77. function(pos, from_list, from_index, to_list, to_index, count, player)
  78. local pname = player:get_player_name()
  79. if minetest.test_protection(pos, pname) then return 0 end
  80. return 0
  81. end
  82. converter.allow_metadata_inventory_take =
  83. function(pos, listname, index, stack, player)
  84. local pname = player:get_player_name()
  85. if minetest.test_protection(pos, pname) then return 0 end
  86. return stack:get_count()
  87. end
  88. converter.on_construct =
  89. function(pos)
  90. end
  91. -- Handle & process incomming power-network events.
  92. -- Important: converters behave similarly to switching stations.
  93. converter.on_machine_execute =
  94. function(pos, table_in, table_out, traversal)
  95. -- Prevent infinite recursion.
  96. -- This check must come *before* we mark the switch as processed, because
  97. -- it may be possible to reach the switch (with lesser recursion) through
  98. -- another path on the network.
  99. local recursion = (table_out.recursion or 0)
  100. if recursion > 8 then return end
  101. -- Mark node as proccessed, do not process this node more than once.
  102. -- If this node has less recorded recursion depth than current message,
  103. -- then we don't traverse this switching station. If this switching station
  104. -- doesn't have a depth recorded, then we record the depth of the message,
  105. -- and traverse this station.
  106. local hash = minetest.hash_node_position(pos)
  107. if traversal[hash] and traversal[hash] <= recursion then return end
  108. traversal[hash] = recursion
  109. -- Get surrounding network hubs. All tiers allowed.
  110. --local hubs = machines.get_adjacent_network_hubs(pos)
  111. local hubs = networks.get_adjacent_hubs(pos)
  112. -- Pass message to all surrounding hubs.
  113. for k, v in ipairs(hubs) do
  114. -- Recursion must be +2 in order to give the supply converter the same
  115. -- recursion-cost to traverse as a switching station.
  116. table_out.recursion = recursion + 2
  117. v.on_machine_execute(v.pos, table_in, table_out, traversal)
  118. end
  119. end
  120. converter.on_destruct =
  121. function(pos)
  122. end
  123. converter.on_blast =
  124. function(pos, intensity)
  125. local drops = {}
  126. default.get_inventory_drops(pos, "cfg", drops)
  127. drops[#drops+1] = "converter:converter"
  128. minetest.remove_node(pos)
  129. return drops
  130. end
  131. converter.on_timer =
  132. function(pos, elapsed)
  133. machines.log_update(pos, "Supply Converter")
  134. converter.update_formspec(pos)
  135. converter.update_infotext(pos)
  136. end
  137. converter.after_place_node =
  138. function(pos, placer, itemstack, pointed_thing)
  139. converter.initialize_typedata(pos)
  140. local meta = minetest.get_meta(pos)
  141. local inv = meta:get_inventory()
  142. inv:set_size("cfg", 8)
  143. converter.update_formspec(pos)
  144. converter.update_infotext(pos)
  145. end