Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sides={"north","south","west","east"}
- --Choose which row turtle occupies
- if (fs.exists("choices") == false) then --Check if this is first time run, no saved choices on file
- while true do
- print("Which row?")
- local answer = read()
- if tonumber(answer)==nill then
- print("-Wrong answer, input not a number-")
- else
- local answerv = tonumber(string.format("%." .. 0 .. "f", answer))
- row=answerv
- print("Row ", row, " chosen")
- break
- end
- end
- while true do --Asks for side for attached inventory
- print("Which direction is the back of the turtle facing?")
- for i=1, #sides do
- print(i..". "..sides[i])
- end
- local answer = read()
- if tonumber(answer)==nill then
- print("-Wrong answer, input not a number-")
- else
- local answerv = tonumber(string.format("%." .. 0 .. "f", answer))
- if (0 < answerv and answerv <= 4) then
- side1=answerv
- --print("Turtle facing: ", sides[side1])
- break
- else
- print("-Wrong answer, input does not match number of sides-")
- end
- end
- end
- local file = fs.open("choices","w")
- file.writeLine("row="..tostring(row))
- file.writeLine("side1="..tostring(side1))
- file.close()
- end
- os.loadAPI("choices") -- Reads choices from file
- local row = choices.row
- local side1 = choices.side1
- ---------------Functions-------------------
- function loader(jobs)
- local slotcounter=0
- inputChest=peripheral.wrap("front")
- for i=1,#jobs do
- for j=1,inputChest.size() do
- inputItem=inputChest.getItemMeta(j)
- if inputItem~= nil then
- --print(inputItem.name)
- --print(jobs[i][1])
- if inputItem.name == jobs[i][1] and inputItem.damage == jobs[i][2] then
- inputChest.pushItems(sides[side1],j,64)
- slotcounter=slotcounter+1
- end
- end
- if slotcounter==16 then
- return
- end
- end
- end
- end
- function empty(jobs)
- local pos=0
- for i=1,#jobs do
- if jobs[i][3]-pos>0 then
- for j=1,jobs[i][3]-pos do
- while not turtle.up() do
- sleep(1)
- end
- pos=pos+1
- end
- for j=1,16 do
- turtle.select(j)
- turtle.drop()
- end
- elseif jobs[i][3]-pos<0 then
- for j=1,pos-jobs[i][3] do
- while not turtle.down() do
- sleep(1)
- end
- pos=pos-1
- end
- for j=1,16 do
- turtle.select(j)
- turtle.drop()
- end
- end
- end
- for j=1,pos do
- while not turtle.down() do
- sleep(1)
- end
- end
- for i=1,16 do
- if turtle.getItemCount(i)>0 then
- turtle.select(i)
- turtle.dropDown()
- end
- end
- end
- local function refuel()
- if turtle.getFuelLevel() < 200 then
- turtle.down()
- turtle.select(1)
- while turtle.getFuelLevel() < 200 do
- turtle.suckDown(64)
- if turtle.refuel() then
- print("Refueled to "..turtle.getFuelLevel())
- else
- print("Refuel failed, no fuel!")
- print("Add fuel and hold down X to continue")
- repeat
- event,key= os.pullEvent("key")
- sleep(0.5)
- until key==keys.x
- end
- end
- turtle.up()
- end
- end
- local function home()
- local home=false
- while home==false do
- if not turtle.down() then
- home=true
- print("home found")
- end
- end
- end
- ---------------Execute-------------------
- -- Initialize rednet
- rednet.open("right")
- rednet.host("ItemSortCrate","row"..row)
- sleep(3)
- home()
- --messageformat: {{itemname,damage,height},.....}
- while true do
- local senderId, message, protocol = rednet.receive()
- --print(message[1][1]..", "..message[1][2].." , "..message[1][3])
- loader(message)
- empty(message)
- refuel()
- end
Add Comment
Please, Sign In to add comment