Advertisement
cc-editor

Train Builder

Aug 31st, 2024 (edited)
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.90 KB | None | 0 0
  1. settings.load()
  2.  
  3. local me_bridge = peripheral.wrap(settings.get("builder.me_bridge"))
  4. local temp_storage = peripheral.wrap(settings.get("builder.item_storage"))
  5. local temp_storage_direction = settings.get("builder.item_storage_direction")
  6. local arm_storage = peripheral.wrap(settings.get("builder.arm_storage"))
  7. local train_selection = settings.get("builder.trains")
  8. local train_station = peripheral.wrap(settings.get("builder.train_station"))
  9.  
  10. local up_down_gear = peripheral.wrap(settings.get("builder.up_down_gear"))
  11. local arm_redstone = peripheral.wrap(settings.get("builder.arm_redstone"))
  12.  
  13. local far_end_reciver_direction = settings.get("builder.far_end_reciver_direction")
  14. local near_end_reciver_direction = settings.get("builder.near_end_reciver_direction")
  15. local arm_mode_direction = settings.get("builder.arm_mode_direction")
  16. local arm_gear_direction = settings.get("builder.arm_gear_direction")
  17.  
  18. local function clear()
  19.     term.clear()
  20.     term.setCursorPos(1, 1)
  21. end
  22.  
  23. local function select_train()
  24.     clear()
  25.     print("Select train to build:")
  26.     for i, train in pairs(train_selection) do
  27.         print("   "..i..". "..train.name)
  28.     end
  29.     print()
  30.     print("Enter number of train to build:")
  31.     local train_number = tonumber(read())
  32.  
  33.     local selected_train = train_selection[train_number]
  34.     if selected_train == nil then
  35.         print("Invalid train number")
  36.         sleep(1)
  37.         return select_train()
  38.     end
  39.     return selected_train
  40. end
  41.  
  42. local function return_excess_items()
  43.     clear()
  44.     print("Returning excess items to storage:")
  45.  
  46.     if arm_storage.list() ~= nil then
  47.         print("   pulling items from arm...")
  48.         for i, item in pairs(arm_storage.list()) do
  49.             arm_storage.pushItems(peripheral.getName(temp_storage), i, 64)
  50.         end
  51.     end
  52.  
  53.     print("   pushing items into me system...")
  54.     for i, item in pairs(temp_storage.list()) do
  55.        me_bridge.importItem(item, temp_storage_direction)
  56.     end
  57.  
  58.     print("   done")
  59.     sleep(1)
  60. end
  61.  
  62. local function craft_items_from_list(items_list)
  63.     clear()
  64.     local crafting_list = {}
  65.  
  66.     print("Retrieving items from me system:")
  67.     for i, item in pairs(items_list) do
  68.         local items_needed = item.count
  69.         print("   "..item.name.." x"..items_needed.."...")
  70.  
  71.         local items_in_storage = me_bridge.getItem(item).amount
  72.         if items_in_storage ~= nil and items_in_storage >= items_needed then
  73.             me_bridge.exportItem(item, temp_storage_direction)
  74.             print("   OK")
  75.         elseif me_bridge.isItemCraftable(item) then
  76.             local craft_amount = items_needed - items_in_storage
  77.             print("   not enough in storage, exporting x"..items_in_storage)
  78.             item.count = items_in_storage
  79.             me_bridge.exportItem(item, temp_storage_direction)
  80.  
  81.             print("   not enough in storage, crafting x"..craft_amount.."...")
  82.             item.count = craft_amount
  83.             me_bridge.craftItem(item)
  84.             table.insert(crafting_list, item)
  85.         else
  86.             print("   not enough in storage and not craftable")
  87.             print("Train build failed - provide "..item.name.." in me system and reorder train")
  88.             print("Press Enter to continue...")
  89.             read('')
  90.             return false
  91.         end
  92.     end
  93.  
  94.     print()
  95.     print("Waiting for crafting to finish:")
  96.     local still_crafting = false
  97.     repeat
  98.         still_crafting = false
  99.         for i, item in pairs(crafting_list) do
  100.             print("   "..item.name.." x"..item.count.."...")
  101.             if me_bridge.isItemCrafting(item) then
  102.                 still_crafting = true
  103.             else
  104.                 local amount = me_bridge.exportItem(item, temp_storage_direction)
  105.                 if amount < item.count then
  106.                     print("   ERROR: Not all items could be crafted")
  107.                     print("Train build failed - provide all items required to craft "..item.name.." in me system and reorder train")
  108.                     print("Press Enter to continue...")
  109.                     read('')
  110.                     return false
  111.                 end
  112.                 print("   OK")
  113.                 table.remove(crafting_list, i)
  114.             end
  115.         end
  116.         print()
  117.         sleep(3)
  118.     until not still_crafting
  119.     print("Crafting and Export finished")
  120.  
  121.     print("Loading build material onto builder arm...")
  122.     if arm_storage.list() == nil then
  123.         print("   ERROR: cannot access arm storage")
  124.         print("Is the arm in loading position?")
  125.         print("Press Enter to continue...")
  126.         read('')
  127.         return false
  128.     end
  129.     for i, _ in pairs(temp_storage.list()) do
  130.         temp_storage.pushItems(peripheral.getName(arm_storage), i, 64)
  131.     end
  132.     print("   DONE")
  133.  
  134.     sleep(1)
  135.     return true
  136. end
  137.  
  138. local function move_arm_y_axis(amount, down)
  139.     arm_redstone.setOutput(arm_mode_direction, true)
  140.     arm_redstone.setOutput(arm_gear_direction, false)
  141.  
  142.     if down then
  143.         print("   moving y-axis down "..amount.."...")
  144.         up_down_gear.move(amount, 1)
  145.     else
  146.         print("   moving y-axis up "..amount.."...")
  147.         up_down_gear.move(amount, -1)
  148.     end
  149.     while up_down_gear.isRunning() do
  150.         sleep(1)
  151.     end
  152.     print("   DONE")
  153. end
  154.  
  155. local function move_arm_x_axis(far_side)
  156.     arm_redstone.setOutput(arm_mode_direction, false)
  157.  
  158.     if far_side then
  159.         print("   moving x-axis to far side...")
  160.         arm_redstone.setOutput(arm_gear_direction, true)
  161.  
  162.         while not arm_redstone.getInput(far_end_reciver_direction) do
  163.             sleep(1)
  164.         end
  165.     else
  166.         print("   moving x-axis to near side...")
  167.         arm_redstone.setOutput(arm_gear_direction, false)
  168.  
  169.         while not arm_redstone.getInput(near_end_reciver_direction) do
  170.             sleep(1)
  171.         end
  172.     end
  173.     print("   DONE")
  174. end
  175.  
  176. local function reset_arm()
  177.     print("Resetting arm...")
  178.     move_arm_y_axis(4, false)
  179.     move_arm_x_axis(false)
  180. end
  181.  
  182. local function build_train(selected_train)
  183.     clear()
  184.     print("Provide blueprint "..selected_train.config.." for train "..selected_train.name.." to building arms")
  185.     move_arm_y_axis(4, true)
  186.     print("Press Enter to continue after providing blueprint...")
  187.     read('')
  188.  
  189.     print("Building train...")
  190.     move_arm_x_axis(true)
  191.     move_arm_y_axis(1, false)
  192.     move_arm_x_axis(false)
  193.     move_arm_y_axis(1, false)
  194.     move_arm_x_axis(true)
  195.     move_arm_y_axis(1, false)
  196.     move_arm_x_axis(false)
  197.     move_arm_y_axis(1, false)
  198.     move_arm_x_axis(true)
  199.  
  200.     print("   DONE")
  201. end
  202.  
  203. local function name_train(selected_train)
  204.     clear()
  205.     print("Enter 3 digit train-id for new train:")
  206.     local id = ""
  207.     repeat
  208.         local temp_id = read()
  209.         if #temp_id ~= 3 or tonumber(temp_id) == nil then
  210.             print("Invalid input. Please enter a 3 digit number.")
  211.         else
  212.             id = temp_id
  213.         end
  214.     until #id == 3
  215.  
  216.     local train_name = "BB-"..selected_train.type..id
  217.     print("Naming train to "..train_name.."...")
  218.     train_station.setTrainName(train_name)
  219.     print("   DONE")
  220. end
  221.  
  222. while true do
  223.     ::continue::
  224.     reset_arm()
  225.     while train_station.isTrainPresent() do
  226.         clear()
  227.         print("Remove train before building another one")
  228.         sleep(3)
  229.     end
  230.     if not train_station.isInAssemblyMode() then
  231.         train_station.setAssemblyMode(true)
  232.     end
  233.     local selected_train = select_train()
  234.     return_excess_items()
  235.     if not craft_items_from_list(require(selected_train.config)) then
  236.         return_excess_items()
  237.         goto continue
  238.     end
  239.  
  240.     build_train(selected_train)
  241.     print("Glue train together on the marked spots and remove marker blocks")
  242.     print("Press Enter when finished...")
  243.     read('')
  244.     name_train(selected_train)
  245.     train_station.assemble()
  246.     clear()
  247.     print("TRAIN BUILD DONE")
  248.     sleep(5)
  249. end
  250.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement