Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- settings.load()
- local me_bridge = peripheral.wrap(settings.get("builder.me_bridge"))
- local temp_storage = peripheral.wrap(settings.get("builder.item_storage"))
- local temp_storage_direction = settings.get("builder.item_storage_direction")
- local arm_storage = peripheral.wrap(settings.get("builder.arm_storage"))
- local train_selection = settings.get("builder.trains")
- local train_station = peripheral.wrap(settings.get("builder.train_station"))
- local up_down_gear = peripheral.wrap(settings.get("builder.up_down_gear"))
- local arm_redstone = peripheral.wrap(settings.get("builder.arm_redstone"))
- local far_end_reciver_direction = settings.get("builder.far_end_reciver_direction")
- local near_end_reciver_direction = settings.get("builder.near_end_reciver_direction")
- local arm_mode_direction = settings.get("builder.arm_mode_direction")
- local arm_gear_direction = settings.get("builder.arm_gear_direction")
- local function clear()
- term.clear()
- term.setCursorPos(1, 1)
- end
- local function select_train()
- clear()
- print("Select train to build:")
- for i, train in pairs(train_selection) do
- print(" "..i..". "..train.name)
- end
- print()
- print("Enter number of train to build:")
- local train_number = tonumber(read())
- local selected_train = train_selection[train_number]
- if selected_train == nil then
- print("Invalid train number")
- sleep(1)
- return select_train()
- end
- return selected_train
- end
- local function return_excess_items()
- clear()
- print("Returning excess items to storage:")
- if arm_storage.list() ~= nil then
- print(" pulling items from arm...")
- for i, item in pairs(arm_storage.list()) do
- arm_storage.pushItems(peripheral.getName(temp_storage), i, 64)
- end
- end
- print(" pushing items into me system...")
- for i, item in pairs(temp_storage.list()) do
- me_bridge.importItem(item, temp_storage_direction)
- end
- print(" done")
- sleep(1)
- end
- local function craft_items_from_list(items_list)
- clear()
- local crafting_list = {}
- print("Retrieving items from me system:")
- for i, item in pairs(items_list) do
- local items_needed = item.count
- print(" "..item.name.." x"..items_needed.."...")
- local items_in_storage = me_bridge.getItem(item).amount
- if items_in_storage ~= nil and items_in_storage >= items_needed then
- me_bridge.exportItem(item, temp_storage_direction)
- print(" OK")
- elseif me_bridge.isItemCraftable(item) then
- local craft_amount = items_needed - items_in_storage
- print(" not enough in storage, exporting x"..items_in_storage)
- item.count = items_in_storage
- me_bridge.exportItem(item, temp_storage_direction)
- print(" not enough in storage, crafting x"..craft_amount.."...")
- item.count = craft_amount
- me_bridge.craftItem(item)
- table.insert(crafting_list, item)
- else
- print(" not enough in storage and not craftable")
- print("Train build failed - provide "..item.name.." in me system and reorder train")
- print("Press Enter to continue...")
- read('')
- return false
- end
- end
- print()
- print("Waiting for crafting to finish:")
- local still_crafting = false
- repeat
- still_crafting = false
- for i, item in pairs(crafting_list) do
- print(" "..item.name.." x"..item.count.."...")
- if me_bridge.isItemCrafting(item) then
- still_crafting = true
- else
- local amount = me_bridge.exportItem(item, temp_storage_direction)
- if amount < item.count then
- print(" ERROR: Not all items could be crafted")
- print("Train build failed - provide all items required to craft "..item.name.." in me system and reorder train")
- print("Press Enter to continue...")
- read('')
- return false
- end
- print(" OK")
- table.remove(crafting_list, i)
- end
- end
- print()
- sleep(3)
- until not still_crafting
- print("Crafting and Export finished")
- print("Loading build material onto builder arm...")
- if arm_storage.list() == nil then
- print(" ERROR: cannot access arm storage")
- print("Is the arm in loading position?")
- print("Press Enter to continue...")
- read('')
- return false
- end
- for i, _ in pairs(temp_storage.list()) do
- temp_storage.pushItems(peripheral.getName(arm_storage), i, 64)
- end
- print(" DONE")
- sleep(1)
- return true
- end
- local function move_arm_y_axis(amount, down)
- arm_redstone.setOutput(arm_mode_direction, true)
- arm_redstone.setOutput(arm_gear_direction, false)
- if down then
- print(" moving y-axis down "..amount.."...")
- up_down_gear.move(amount, 1)
- else
- print(" moving y-axis up "..amount.."...")
- up_down_gear.move(amount, -1)
- end
- while up_down_gear.isRunning() do
- sleep(1)
- end
- print(" DONE")
- end
- local function move_arm_x_axis(far_side)
- arm_redstone.setOutput(arm_mode_direction, false)
- if far_side then
- print(" moving x-axis to far side...")
- arm_redstone.setOutput(arm_gear_direction, true)
- while not arm_redstone.getInput(far_end_reciver_direction) do
- sleep(1)
- end
- else
- print(" moving x-axis to near side...")
- arm_redstone.setOutput(arm_gear_direction, false)
- while not arm_redstone.getInput(near_end_reciver_direction) do
- sleep(1)
- end
- end
- print(" DONE")
- end
- local function reset_arm()
- print("Resetting arm...")
- move_arm_y_axis(4, false)
- move_arm_x_axis(false)
- end
- local function build_train(selected_train)
- clear()
- print("Provide blueprint "..selected_train.config.." for train "..selected_train.name.." to building arms")
- move_arm_y_axis(4, true)
- print("Press Enter to continue after providing blueprint...")
- read('')
- print("Building train...")
- move_arm_x_axis(true)
- move_arm_y_axis(1, false)
- move_arm_x_axis(false)
- move_arm_y_axis(1, false)
- move_arm_x_axis(true)
- move_arm_y_axis(1, false)
- move_arm_x_axis(false)
- move_arm_y_axis(1, false)
- move_arm_x_axis(true)
- print(" DONE")
- end
- local function name_train(selected_train)
- clear()
- print("Enter 3 digit train-id for new train:")
- local id = ""
- repeat
- local temp_id = read()
- if #temp_id ~= 3 or tonumber(temp_id) == nil then
- print("Invalid input. Please enter a 3 digit number.")
- else
- id = temp_id
- end
- until #id == 3
- local train_name = "BB-"..selected_train.type..id
- print("Naming train to "..train_name.."...")
- train_station.setTrainName(train_name)
- print(" DONE")
- end
- while true do
- ::continue::
- reset_arm()
- while train_station.isTrainPresent() do
- clear()
- print("Remove train before building another one")
- sleep(3)
- end
- if not train_station.isInAssemblyMode() then
- train_station.setAssemblyMode(true)
- end
- local selected_train = select_train()
- return_excess_items()
- if not craft_items_from_list(require(selected_train.config)) then
- return_excess_items()
- goto continue
- end
- build_train(selected_train)
- print("Glue train together on the marked spots and remove marker blocks")
- print("Press Enter when finished...")
- read('')
- name_train(selected_train)
- train_station.assemble()
- clear()
- print("TRAIN BUILD DONE")
- sleep(5)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement