Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local role = nil
- local server = nil
- local chunkPartner = nil
- local acceptedProtocols = {}
- acceptedProtocols["config"] = 1
- local REMOTE_METADATA_PASTE = "5LPswLxb"
- local ITEM_QUARRY = "quarryplus:quarry"
- local ITEM_POWER = "mekanism:quantum_entangloporter"
- local ITEM_OUTPUT_CHEST = "enderchests:ender_chest"
- local ITEM_FUEL_CHEST = "enderchests:ender_chest"
- local ITEM_FILLER = "minecraft:iron_block"
- local SLOT_QUARRY = 1
- local SLOT_POWER = 2
- local SLOT_OUTPUT_CHEST = 3
- local SLOT_FUEL_CHEST = 4
- local SLOT_FILLER = 5
- local SIDE_DOWN = -1
- local SIDE_FRONT = 0
- local SIDE_UP = 1
- local turtleSlots = {
- miner = {
- {ITEM_QUARRY, 1}, {ITEM_POWER, 1}, {ITEM_OUTPUT_CHEST, 1}, {ITEM_FUEL_CHEST, 1},
- {ITEM_FILLER, 1}, nil, nil, nil,
- nil, nil, nil, nil,
- nil, nil, nil, nil,
- },
- loader = {
- nil, nil, nil, nil,
- nil, nil, nil, nil,
- nil, nil, nil, nil,
- nil, nil, nil, nil,
- }
- }
- function checkForUpdates()
- local updateMetadata
- shell.run("pastebin", "get", REMOTE_METADATA_PASTE, "version.new")
- if not fs.exists("version.new") then
- print("Remote metadata download failure, will not update")
- return nil
- end
- fh = fs.open("version.new", "r")
- updateMetadata = textutils.unserialiseJSON(fh.readAll())
- fh.close()
- fs.delete("version.new")
- if updateMetadata['version'] == nil or updateMetadata['paste'] == nil then
- print("Remote metadata malformed, will not update")
- return nil
- end
- if not fs.exists("version") then
- print("Local version missing, update needed")
- return updateMetadata
- end
- fh = fs.open("version", "r")
- localVersion = fh.readAll()
- fh.close()
- if localVersion ~= updateMetadata['version'] then
- print("Local version: "..localVersion)
- print("Remote version: "..updateMetadata['version'])
- print("Local version differs, update needed")
- return updateMetadata
- end
- print("Local version: "..localVersion)
- print("Remote version: "..updateMetadata['version'])
- print("Local version matches remote, no update needed")
- return nil
- end
- function downloadUpdate(metadata)
- print("Downloading update")
- fs.delete("startup")
- shell.run("pastebin", "get", metadata['paste'], "startup")
- fh = fs.open("version", "w")
- localVersion = fh.write(metadata['version'])
- fh.close()
- os.reboot()
- end
- function setup()
- role = determineRole(args)
- chunkPartner = determinePartner(args)
- print("Ready for initial setup. Insert fuel in the last slot before continuing.")
- print("Hit enter to calibrate")
- waitForKey(keys.enter)
- turtle.select(16)
- turtle.refuel(64)
- setupBTA()
- inventoryAssert()
- print("Setup complete. Fill with correct items and hit enter to begin")
- waitForKey(keys.enter)
- end
- function setupBTA()
- if not fs.exists("bta") then
- shell.run("pastebin", "get", "MdD051uF", "bta")
- end
- if gps.locate(5) == nil then
- error("Failed to locate self. Is GPS setup/reachable?")
- end
- bta = require("bta")
- end
- function determineRole(args)
- local role = nil
- local roleArg = args[1]
- if fs.exists("role") then
- fh = fs.open("role", "r")
- role = fh.readAll()
- fh.close()
- end
- if roleArg ~= nil then
- role = roleArg
- end
- if role == nil then
- error("Must pass a role name as first argument")
- end
- fh = fs.open("role", "w")
- fh.write(role)
- fh.close()
- return role
- end
- function determinePartner(args)
- local partner = nil
- local partnerArg = args[2]
- if fs.exists("partner") then
- fh = fs.open("role", "r")
- partner = fh.readAll()
- fh.close()
- end
- fh = fs.open("partner", "w")
- fh.write(partner)
- fh.close()
- return partner
- end
- function main()
- print("Setup complete, starting main loop")
- if role == "miner" then
- mainLoopMiner()
- elseif role == "loader" then
- mainLoopLoader()
- end
- end
- function mainLoopMiner()
- rednet.open("left")
- while true do
- local r = placeMiningWell()
- if isError(r) then error(r.r) end
- print("Press enter when completed")
- waitForKey(keys.enter)
- r = removeMiningWell()
- if isError(r) then error(r.r) end
- rednet.broadcast("", "loader")
- bta.move("forward", nil, 9)
- r = refuel()
- if isError(r) then error(r.r) end
- end
- end
- function mainLoopLoader()
- rednet.open("left")
- while true do
- print("Waiting for signal from chunk partner")
- id = rednet.receive("loader")
- if id == chunkPartner then
- rednet.send(id, "", "loader")
- bta.move("forward", nil, 9)
- local r = refuel()
- if isError(r) then error(r.r) end
- end
- end
- end
- function handleRednetMessage(sender, message, protocol)
- if acceptedProtocols[protocol] == nil then return end
- server = sender
- local command = textutils.unserialise(message)
- if command.type == "goto" then
- moveToCoordinates(command)
- elseif command.type == "place" then
- placeMiningWell()
- end
- end
- function moveToCoordinates(command)
- bta.goTo(command.payload.position)
- bta.turn(command.payload.direction)
- end
- function placeMiningWell()
- turtle.turnRight()
- turtle.forward()
- turtle.turnLeft()
- turtle.forward()
- r = assertSlotAndSelect(SLOT_OUTPUT_CHEST, ITEM_OUTPUT_CHEST, 1)
- if isError(r) then return r end
- r = assertPlace(SIDE_FRONT)
- if isError(r) then return r end
- turtle.turnLeft()
- turtle.forward()
- turtle.turnRight()
- turtle.forward()
- turtle.digDown()
- turtle.dig()
- inventoryFilter()
- r = assertSlotAndSelect(SLOT_FILLER, ITEM_FILLER, 1)
- if isError(r) then return r end
- r = assertPlace(SIDE_DOWN)
- if isError(r) then return r end
- turtle.back()
- r = assertSlotAndSelect(SLOT_QUARRY, ITEM_QUARRY, 1)
- if isError(r) then return r end
- r = assertPlace(SIDE_FRONT)
- if isError(r) then return r end
- turtle.turnLeft()
- turtle.forward()
- turtle.turnRight()
- r = assertSlotAndSelect(SLOT_POWER, ITEM_POWER, 1)
- if isError(r) then return r end
- r = assertPlace(SIDE_FRONT)
- if isError(r) then return r end
- turtle.back()
- turtle.turnRight()
- turtle.forward()
- turtle.turnLeft()
- end
- function removeMiningWell()
- turtle.forward()
- r = assertSelect(SLOT_QUARRY)
- if isError(r) then return r end
- r = assertDig(SIDE_FRONT)
- if isError(r) then return r end
- inventoryFilter()
- inventoryCrudeSort()
- turtle.turnLeft()
- turtle.forward()
- turtle.turnRight()
- r = assertSelect(SLOT_POWER)
- if isError(r) then return r end
- r = assertDig(SIDE_FRONT)
- if isError(r) then return r end
- turtle.turnRight()
- turtle.forward()
- turtle.forward()
- turtle.turnLeft()
- r = assertSelect(SLOT_OUTPUT_CHEST)
- if isError(r) then return r end
- r = assertDig(SIDE_FRONT)
- if isError(r) then return r end
- turtle.turnLeft()
- turtle.forward()
- turtle.turnRight()
- turtle.forward()
- r = assertSelect(SLOT_FILLER)
- if isError(r) then return r end
- r = assertDig(SIDE_DOWN)
- if isError(r) then return r end
- r = assertDig(SIDE_FRONT)
- if isError(r) then return r end
- turtle.back()
- turtle.back()
- end
- function waitForKey(keyToWaitFor)
- repeat
- local event, key = os.pullEvent("key")
- until key == keyToWaitFor
- end
- function refuel()
- turtle.turnRight()
- turtle.turnRight()
- local r = assertSlotAndSelect(4, ITEM_FUEL_CHEST, 1)
- if isError(r) then return r end
- r = assertPlace(SIDE_FRONT)
- if isError(r) then return r end
- r = assertSelect(16)
- if isError(r) then return r end
- turtle.suck(1)
- turtle.refuel(1)
- r = assertSelect(4)
- if isError(r) then return r end
- r = assertDig(SIDE_FRONT)
- if isError(r) then return r end
- turtle.turnRight()
- turtle.turnRight()
- end
- function assertSlot(slot, item, qty)
- local data = turtle.getItemDetail(slot)
- if item == nil and data ~= nil then
- return commandErrorSlot(slot, "should be nil, has "..data['name'])
- end
- if item ~= nil and data == nil then
- return commandErrorSlot(slot, "is nil, expecting "..item)
- end
- if item ~= data['name'] then
- return commandErrorSlot(slot, "is "..data['name']..", expecting "..item)
- end
- if qty ~= data['count'] then
- return commandErrorSlot(slot, "has "..data['count']..", expecting "..qty)
- end
- return nil
- end
- function assertSlotAndSelect(slot, item, qty)
- local res = assertSlot(slot, item, qty)
- if res ~= nil then return res end
- res = turtle.select(slot)
- if res == false then return commandErrorSlot(slot, "select fail") end
- return nil
- end
- function assertSelect(slot)
- res = turtle.select(slot)
- if res == false then return commandErrorSlot(slot, "select fail") end
- return nil
- end
- function assertPlace(dir)
- local r
- if dir == SIDE_DOWN then
- r = turtle.placeDown()
- elseif dir == SIDE_FRONT then
- r = turtle.place()
- elseif dir == SIDE_UP then
- r = turtle.placeUp()
- end
- if r == false then
- return commandError("place dir "..dir.." fail")
- end
- return nil
- end
- function assertDig(dir)
- local r
- if dir == SIDE_DOWN then
- r = turtle.digDown()
- elseif dir == SIDE_FRONT then
- r = turtle.dig()
- elseif dir == SIDE_UP then
- r = turtle.digUp()
- end
- if r == false then
- return commandError("dig dir "..dir.." fail")
- end
- return nil
- end
- function inventoryAssert()
- if turtleSlots[role] == nil then error(role.." seems to be an invalid role") end
- local r
- for index, slot in pairs(turtleSlots[role]) do
- r = assertSlot(index, slot[1], slot[2])
- if isError(r) then error(r.r) end
- end
- end
- function inventoryFilter()
- if turtleSlots[role] == nil then error(role.." seems to be an invalid role") end
- turtle.turnRight()
- turtle.turnRight()
- for i=1,16 do
- turtle.select(i)
- local data = turtle.getItemDetail()
- if data ~= nil then
- local found = false
- for _, info in pairs(turtleSlots[role]) do
- if data['name'] == info[1] then
- found = true
- end
- end
- if not found then
- local success = turtle.drop()
- if not success then error("failed to drop item") end
- end
- end
- end
- turtle.turnLeft()
- turtle.turnLeft()
- end
- function inventoryCrudeSort()
- if turtleSlots[role] == nil then error(role.." seems to be an invalid role") end
- for i=1,16 do
- turtle.select(i)
- local data = turtle.getItemDetail()
- if data ~= nil then
- if data['name'] ~= ITEM_FUEL_CHEST then
- for destSlot, info in pairs(turtleSlots[role]) do
- if data['name'] == info[1] then
- turtle.transferTo(destSlot)
- end
- end
- end
- end
- end
- end
- function commandErrorSlot(slot, reason)
- return commandError("slot "..slot.." "..reason)
- end
- function commandError(reason)
- local r = {}
- r['s'] = false
- r['r'] = reason
- return r
- end
- function isError(error)
- if error == nil then return false end
- if error['s'] == nil or error['s'] == true then return false end
- return true
- end
- args = {...}
- updateMetadata = checkForUpdates()
- if updateMetadata ~= nil then
- downloadUpdate(updateMetadata)
- end
- setup()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement