init.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. controler = controler or {}
  2. controler.modpath = minetest.get_modpath("controler")
  3. controler.compose_formspec =
  4. function(pos)
  5. local meta = minetest.get_meta(pos)
  6. local formspec =
  7. "size[8,8.5]" ..
  8. default.formspec.get_form_colors() ..
  9. default.formspec.get_form_image() ..
  10. default.formspec.get_slot_colors() ..
  11. "label[0,0.5;Controler Configuration]" ..
  12. "list[context;cfg;0,1;8,2;]" ..
  13. "list[current_player;main;0,4.25;8,1;]" ..
  14. "list[current_player;main;0,5.5;8,3;8]" ..
  15. "listring[context;cfg]" ..
  16. "listring[current_player;main]" ..
  17. default.get_hotbar_bg(0, 4.25)
  18. meta:set_string("formspec", formspec)
  19. end
  20. controler.compose_infotext =
  21. function(pos)
  22. local meta = minetest.get_meta(pos)
  23. local infotext = "Machine Controler"
  24. meta:set_string("infotext", infotext)
  25. end
  26. controler.can_dig =
  27. function(pos, player)
  28. local meta = minetest.get_meta(pos)
  29. local inv = meta:get_inventory()
  30. return inv:is_empty('cfg')
  31. end
  32. controler.allow_metadata_inventory_put =
  33. function(pos, listname, index, stack, player)
  34. local pname = player:get_player_name()
  35. if minetest.test_protection(pos, pname) then
  36. return 0
  37. end
  38. if stack:get_name() == "cfg:dev" then
  39. return stack:get_count()
  40. end
  41. return 0
  42. end
  43. controler.allow_metadata_inventory_move =
  44. function(pos, from_list, from_index, to_list, to_index, count, player)
  45. local meta = minetest.get_meta(pos)
  46. local inv = meta:get_inventory()
  47. local stack = inv:get_stack(from_list, from_index)
  48. return controler.allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
  49. end
  50. controler.allow_metadata_inventory_take =
  51. function(pos, listname, index, stack, player)
  52. local pname = player:get_player_name()
  53. if minetest.test_protection(pos, pname) then
  54. return 0
  55. end
  56. return stack:get_count()
  57. end
  58. controler.on_timer =
  59. function(pos, elapsed)
  60. machines.log_update(pos, "Machine Controler")
  61. local table_in = {
  62. purpose = "autostart_trigger",
  63. }
  64. local table_out = {}
  65. local traversal = {}
  66. -- Do not process self.
  67. local hash = minetest.hash_node_position(pos)
  68. traversal[hash] = 0
  69. -- Update all machines on adjacent networks of any tier.
  70. local hubs = machines.get_adjacent_network_hubs(pos)
  71. if hubs then
  72. for k, v in ipairs(hubs) do
  73. local node = minetest.get_node(v)
  74. local def = minetest.reg_ns_nodes[node.name]
  75. if def and def.on_machine_execute then
  76. def.on_machine_execute(v, table_in, table_out, traversal)
  77. end
  78. end
  79. end
  80. controler.compose_formspec(pos)
  81. controler.compose_infotext(pos)
  82. end
  83. controler.on_blast =
  84. function(pos)
  85. local drops = {}
  86. default.get_inventory_drops(pos, "cfg", drops)
  87. drops[#drops+1] = "controler:controler"
  88. minetest.remove_node(pos)
  89. return drops
  90. end
  91. controler.on_construct =
  92. function(pos)
  93. controler.initialize_typedata(pos)
  94. local meta = minetest.get_meta(pos)
  95. local inv = meta:get_inventory()
  96. inv:set_size('cfg', 16)
  97. controler.compose_formspec(pos)
  98. controler.compose_infotext(pos)
  99. end
  100. controler.after_place_node =
  101. function(pos, placer, itemstack, pointed_thing)
  102. end
  103. controler.trigger_update =
  104. function(pos)
  105. local timer = minetest.get_node_timer(pos)
  106. if not timer:is_started() then
  107. timer:start(1.0)
  108. end
  109. end
  110. controler.on_metadata_inventory_move =
  111. function(pos)
  112. controler.trigger_update(pos)
  113. end
  114. controler.on_metadata_inventory_put =
  115. function(pos)
  116. controler.trigger_update(pos)
  117. end
  118. controler.on_metadata_inventory_take =
  119. function(pos)
  120. controler.trigger_update(pos)
  121. end
  122. controler.on_machine_execute =
  123. function(pos, table_in, table_out, traversal)
  124. -- No recursion check, because controlers do not take part in chains.
  125. -- This also allows them to function at the very end of a switching chain.
  126. -- Do not process this node more than once.
  127. local hash = minetest.hash_node_position(pos)
  128. if traversal[hash] then return end
  129. traversal[hash] = 0
  130. local purpose = table_in.purpose
  131. if purpose == "controler_update" then
  132. controler.trigger_update(pos)
  133. end
  134. end
  135. -- Typedata is used when traversing the network, without touching the node.
  136. -- It must contain as much data as needed to get the node even if unloaded.
  137. -- This must be done after node construction.
  138. -- This should also be done when punched, to allow old nodes to be upgraded.
  139. controler.initialize_typedata =
  140. function(pos)
  141. local meta = minetest.get_meta(pos)
  142. meta:set_string("technic_machine", "yes")
  143. meta:set_string("technic_type", "utility")
  144. meta:set_string("technic_tier", "lv|mv|hv")
  145. meta:set_string("technic_name", "controler:controler")
  146. end
  147. controler.on_punch =
  148. function(pos, node, puncher, pointed_thing)
  149. controler.initialize_typedata(pos)
  150. controler.trigger_update(pos)
  151. end
  152. if not controler.run_once then
  153. minetest.register_node("controler:controler", {
  154. description = "Machine Controler",
  155. tiles = {
  156. "controler_top.png", "controler_bottom.png",
  157. "controler_side.png", "controler_side.png",
  158. "controler_side.png", "controler_front.png"
  159. },
  160. groups = utility.dig_groups("machine", {
  161. immovable = 1,
  162. tier_lv = 1, tier_mv = 1, tier_hv = 1,
  163. }),
  164. paramtype2 = "facedir",
  165. on_rotate = function(...) return screwdriver.rotate_simple(...) end,
  166. is_ground_content = false,
  167. sounds = default.node_sound_metal_defaults(),
  168. on_punch = function(...)
  169. return controler.on_punch(...) end,
  170. can_dig = function(...)
  171. return controler.can_dig(...) end,
  172. on_timer = function(...)
  173. return controler.on_timer(...) end,
  174. on_construct = function(...)
  175. return controler.on_construct(...) end,
  176. after_place_node = function(...)
  177. return controler.after_place_node(...) end,
  178. on_metadata_inventory_move = function(...)
  179. return controler.on_metadata_inventory_move(...) end,
  180. on_metadata_inventory_put = function(...)
  181. return controler.on_metadata_inventory_put(...) end,
  182. on_metadata_inventory_take = function(...)
  183. return controler.on_metadata_inventory_take(...) end,
  184. on_blast = function(...)
  185. return controler.on_blast(...) end,
  186. allow_metadata_inventory_put = function(...)
  187. return controler.allow_metadata_inventory_put(...) end,
  188. allow_metadata_inventory_move = function(...)
  189. return controler.allow_metadata_inventory_move(...) end,
  190. allow_metadata_inventory_take = function(...)
  191. return controler.allow_metadata_inventory_take(...) end,
  192. on_machine_execute = function(...)
  193. return controler.on_machine_execute(...) end,
  194. })
  195. -- Not used anymore. Ready for repurposing.
  196. --[[
  197. minetest.register_craft({
  198. output = 'controler:controler',
  199. recipe = {
  200. {'fine_wire:silver', 'default:glass', 'fine_wire:gold'},
  201. {'techcrafts:control_logic_unit', 'techcrafts:machine_casing', 'techcrafts:control_logic_unit'},
  202. {'carbon_steel:ingot', 'battery:battery', 'carbon_steel:ingot'},
  203. },
  204. })
  205. --]]
  206. local c = "controler:core"
  207. local f = controler.modpath .. "/init.lua"
  208. reload.register_file(c, f, false)
  209. controler.run_once = true
  210. end