Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function readFile(fileName)
- file = assert(fs.open(fileName, "r"))
- for i=1,9 do
- qtysReq[i]=0
- end
- level=1
- row=1
- column=1
- if ( file ) then
- line = file.readAll()
- plan[level] = {}
- plan[level][row] = {}
- i=1
- while ( i <= string.len(line) ) do
- char = string.sub(line, i,i)
- if ( char == "\n" ) then
- if ( string.sub(line, i+1,i+1) == "\n" ) then
- level = level + 1
- row = 1
- column = 1
- plan[level] = {}
- plan[level][row] = {}
- i=i+1
- else
- row=row+1
- column = 1
- plan[level][row] = {}
- end
- else
- plan[level][row][column] = tonumber(char)
- if ( tonumber(char) ~= 0 ) then
- qtysReq[tonumber(char)] = qtysReq[tonumber(char)] + 1
- end
- column = column + 1
- end
- i = i + 1
- end
- file.close()
- else
- io.write("Error reading file")
- os.exit()
- end
- getSubsitutes()
- for i = 1,table.getn(qtysReq) do
- if ( qtysReq[i] ~= 0 ) then
- qty = 0
- io.write("slot " .. i .. " requires " .. qtysReq[i] .. " blocks")
- for j = 1,#subs[i] do
- qty = qty + turtle.getItemCount(subs[i][j])
- end
- if ( qtysReq[i] > qty ) then
- io.write(", only " .. qty .. " found, a refill will be required")
- else
- io.write(", sufficient blocks in subsitute slots")
- end
- io.write("\n")
- end
- end
- return plan
- end
- function getSubsitutes()
- k=1
- for i = 1,16 do
- if ( qtysReq[i] == nil or qtysReq[i] == 0 ) then
- if ( turtle.getItemCount(i) > 0 ) then
- availSlots[k] = i
- k = k + 1
- else
- if ( freeSlot == 0 ) then
- freeSlot = i
- end
- end
- else
- end
- end
- for i = 1, #qtysReq do
- if ( qtysReq[i] > 0 ) then
- turtle.select(i)
- k=2
- subs[i]={}
- subs[i][1]=i
- for j = 1, #availSlots do
- if ( turtle.compareTo(availSlots[j])) then
- subs[i][k] = availSlots[j]
- k = k + 1
- end
- end
- end
- end
- end
- function move(direction, count)
- if ( checkFuel() ) then
- io.write("Insufficient Fuel, please place fuel in slot 16")
- while ( not checkFuel() ) do
- refuelTurtle()
- sleep(1)
- end
- end
- if ( direction == "forward" ) then
- for i=1,count do
- while ( not turtle.forward() ) do
- turtle.dig()
- checkInventory()
- end
- end
- elseif ( direction == "up" ) then
- for i=1,count do
- while ( not turtle.up() ) do
- turtle.digUp()
- checkInventory()
- end
- end
- elseif ( direction == "down" ) then
- for i=1,count do
- while ( not turtle.down() ) do
- turtle.digDown()
- checkInventory()
- end
- end
- else
- io.write("Unknown Direction Specified - " .. direction .. "\n")
- os.exit(1)
- end
- end
- function turn(direction, count)
- if ( direction == "left" ) then
- for i=1,count do
- turtle.turnLeft()
- end
- elseif ( direction == "right" ) then
- for i=1,count do
- turtle.turnRight()
- end
- else
- io.write("Unknown Turn Direction Specified - " .. direction .. "\n")
- end
- end
- function checkInventory()
- if ( invFull() ) then
- io.write("Inventory Full, Please remove extra items\n")
- while ( invFull() ) do
- sleep(1)
- end
- end
- end
- function invFull()
- full=true
- for i=freeSlot,16 do
- full = (turtle.getItemCount(i) > 0 and full)
- end
- return full
- end
- function getSlot(slot)
- for i = 1,table.getn(subs[slot]) do
- if ( turtle.getItemCount(subs[slot][i]) > 0 and qtysReq[slot] > 0 ) then
- return subs[slot][i]
- end
- end
- return 0
- end
- function dig(direction)
- if ( direction == "forward" ) then
- while ( turtle.detect() ) do
- turtle.dig()
- checkInventory()
- end
- elseif ( direction == "up" ) then
- while ( turtle.detectUp() ) do
- turtle.digUp()
- checkInventory()
- end
- elseif ( direction == "down" ) then
- while ( turtle.detectDown() ) do
- turtle.digDown()
- checkInventory()
- end
- else
- io.write("Unknown Dig Direction Specified - " .. direction .. "\n")
- os.exit(1)
- end
- end
- function placeItem(slot)
- slotNumber = getSlot(slot)
- if ( slotNumber == 0 ) then
- io.write("There is insufficient stock for slot " .. slot .. ". Please replenish stock in one of the following Slots\n")
- for i = 1,table.getn(subs[slot]) do
- io.write(subs[slot][i] .. "\n")
- end
- while ( slotNumber == 0 ) do
- sleep(1)
- slotNumber = getSlot(slot)
- end
- end
- turtle.select(slotNumber)
- dig("down")
- turtle.placeDown()
- qtysReq[slot] = qtysReq[slot] - 1
- turtle.select(freeSlot)
- end
- function checkFuel()
- return (turtle.getFuelLevel() <= 1)
- end
- function refuelTurtle()
- turtle.select(16)
- turtle.refuel()
- end
- local args={ ... }
- availSlots={}
- plan={}
- qtysReq={}
- subs={}
- fileName=args[1]
- rowCount=1
- level=0
- freeSlot=0
- plan = readFile(fileName)
- turnDirection="right"
- for level = 1,#plan do
- if ( level % 2 == 1 ) then
- rowStart = 1
- rowEnd = table.getn(plan[level])
- rowStep = 1
- else
- rowStart = table.getn(plan[level])
- rowEnd = 1
- rowStep = -1
- end
- for row = rowStart,rowEnd,rowStep do
- if ( row % 2 == level % 2 ) then
- columnStart = 1
- columnEnd = table.getn(plan[level][row])
- columnStep = 1
- else
- columnStart = table.getn(plan[level][row])
- columnEnd = 1
- columnStep = -1
- end
- for column = columnStart,columnEnd, columnStep do
- dig("down")
- if ( plan[level][row][column] ~= 0 ) then
- placeItem(plan[level][row][column])
- end
- if ( column ~= columnEnd ) then
- move("forward", 1)
- end
- end
- if ( row ~= rowEnd ) then
- turn(turnDirection, 1)
- move("forward", 1)
- turn(turnDirection, 1)
- if ( turnDirection == "right" ) then
- turnDirection = "left"
- else
- turnDirection = "right"
- end
- end
- end
- if ( level ~= table.getn(plan) ) then
- move("up", 1)
- turn("left", 2)
- if ( level % 2 == 1 ) then
- if ( table.getn(plan[level]) % 2 == 0 ) then
- turnDirection = "left"
- else
- turnDirection = "right"
- end
- else
- turnDirection = "right"
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment