Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local native=turtle
- --[[ unchanged methods
- function attack()
- function attackUp()
- function attackDown()
- --]]
- local dirMap={north=0, east=1, south=2, west=3}
- local dirVMap={}
- dirVMap[0]= { x= 0, y= 0, z=-1, }
- dirVMap[1]= { x= 1, y= 0, z= 0, }
- dirVMap[2]= { x= 0, y= 0, z= 1, }
- dirVMap[3]= { x=-1, y= 0, z= 0, }
- local position=vector.new(0,0,0)
- local direction=0
- local selectedSlot = 1
- local myInventory=nil
- local posStack={}
- --load and setup the inventory
- local function loadPosition()
- if fs.exists(".position") then
- local file=fs.open(".position","r")
- position.x=file.readLine()+0
- position.y=file.readLine()+0
- position.z=file.readLine()+0
- direction=file.readLine()+0
- file.close()
- directionV=dirVMap[direction]
- end
- end
- local function loadSelected()
- if fs.exists(".selectedSlot") then
- file=fs.open(".selectedSlot","r")
- selectedSlot=file.readLine()+0
- file.close()
- end
- end
- local function loadPosStack()
- if fs.exists(".posStack") then
- posStack={}
- local file=fs.open(".posStack","r")
- local count=file.readLine()+0
- for i=1,count,1 do
- posStack[i]={vector.new(0,0,0),0}
- posStack[i].x=file.readLine()+0
- posStack[i].y=file.readLine()+0
- posStack[i].z=file.readLine()+0
- posStack[i][2]=file.readLine()+0
- file.close()
- end
- end
- end
- local function loadState()
- loadPosition()
- loadSelected()
- loadPosStack()
- --[[if fs.exists(".inventory") then
- myInventory=inventory.load(".inventory")
- myInventory.isMe=true
- else
- myInventory=inventory.create(16,true)
- end]]
- myInventory=inventory.load(".inventory")
- if myInventory==nil then
- myInventory=inventory.create(16,true)
- else
- myInventory.isMe=true
- end
- myInventory.autoSave=true
- end
- loadState()
- local function savePosition()
- local file=fs.open(".position","w")
- file.write(position.x.."\n"..position.y.."\n"..position.z.."\n"..direction.."\n")
- file.close()
- end
- local function saveSelected()
- local file=fs.open(".selectedSlot","w")
- file.write(selectedSlot.."\n")
- file.close()
- end
- local function savePosStack()
- local file=fs.open(".posStack","w")
- file.write(#posStack.."\n")
- for i=1,#posStack,1 do
- file.write(posStack[i][1].x.."\n"..posStack[i][1].y.."\n"..posStack[i][1].z.."\n"..posStack[i][2])
- end
- file.close()
- end
- local function saveState()
- savePosition()
- saveSelected()
- savePosStack()
- inventory.save(myInventory,".inventory")
- end
- --[[
- compares actual turtle inventory to myInventory, updates all quantities,
- 0s type for newly-empty slots, and attempts to identify any new items, attaching "unknown" tags
- to those that do not match available items
- --]]
- function updateInventory()
- --save selected slot to reselect when done
- local changed=false
- local prevSlot=selectedSlot
- local addedStuff={}
- local removedStuff={}
- for i=1,16,1 do
- local turtleCount=native.getItemCount(i)
- local invCount=myInventory.slots[i].quantity
- if invCount~=turtleCount then
- --item count has changed...
- changed=true
- if invCount==0 then
- --we had none before, have to compare to other types
- select(i)
- local found=false
- for j=1,16,1 do
- if i~=j then
- if myInventory.slots[j].type~=0 and compareTo(j) then
- --got a match
- myInventory.slots[i].type=myInventory.slots[j].type
- found=true
- break
- end
- end
- end
- if not found then
- myInventory.slots[i].type=myInventory:getNextUnknownType()
- end
- elseif turtleCount==0 then
- --add to dropped
- removedStuff[#removedStuff+1]={
- type=myInventory.slots[i].type,
- count=myInventory.slots[i].quantity,
- slot=i,
- }
- --we don't have anymore, zero type
- myInventory.slots[i].type=0
- end
- --if we have more now...
- if turtleCount>invCount then
- --add to newstuff list
- addedStuff[#addedStuff+1]= {
- type=myInventory.slots[i].type,
- count=turtleCount-invCount,
- slot=i,
- }
- end
- --in ALL cases, update quantity
- myInventory.slots[i].quantity=native.getItemCount(i)
- end
- end
- if changed then
- inventory.save(myInventory,".inventory")
- end
- select(prevSlot)
- return changed, addedStuff, removedStuff
- end
- local function movehelp(moveFunc, unmoveFunc, digFunc, detectFunc, dirv, count, onFail)
- if count==nil then count=1 end
- for i=1,count,1 do
- if moveFunc()==false then
- if onFail=="return" then
- local succ,dist
- succ,dist=unmoveFunc(i-1,"stop")
- return false, i-1-dist
- elseif onFail=="dig" then
- local res=false
- local tries=0
- repeat
- --retry..
- if detectFunc() then
- digFunc()
- end
- os.sleep(.2)
- res=moveFunc()
- tries=tries+1
- until res==true or tries>=10
- if res==false then
- return false,i-1
- end
- else --default stop
- return false, i-1
- end
- end
- position=position+dirv
- end
- return true, count
- end
- function forward(count, onFail)
- local res1,res2=movehelp(native.forward,back,dig,native.detect,dirVMap[direction], count, onFail)
- savePosition()
- return res1,res2
- end
- function back(count, onFail)
- local res1,res2=movehelp(native.back,forward,digBack,detectBack,(dirVMap[(direction+2)%4]), count, onFail)
- savePosition()
- return res1,res2
- end
- function up(count, onFail)
- local res1,res2=movehelp(native.up,down,digUp,native.detectUp,vector.new(0,1,0), count, onFail)
- savePosition()
- return res1,res2
- end
- function down(count, onFail)
- local res1,res2=movehelp(native.down,up,digDown,native.detectDown,vector.new(0,-1,0), count, onFail)
- savePosition()
- return res1,res2
- end
- function left(count, onFail)
- turnLeft()
- local res,c=forward(count,onFail)
- turnRight()
- savePosition()
- return res,c
- end
- function right(count, onFail)
- turnRight()
- local res,c=forward(count,onFail)
- turnLeft()
- return res,c
- end
- function turnLeft()
- native.turnLeft()
- direction=(direction+3)%4
- savePosition()
- end
- function turnRight()
- native.turnRight()
- direction=(direction+1)%4
- savePosition()
- end
- function turnAround()
- native.turnLeft()
- direction=(direction+1)%4
- native.turnLeft()
- direction=(direction+1)%4
- savePosition()
- end
- function getPosition()
- return position.x,position.y,position.z
- end
- function getDirection()
- return direction
- end
- function move(x, y, z, onBlocked)
- local succ, dist, blocked, done
- local prevDir=direction
- local moves={}
- local dig=false
- while true do
- blocked=true
- done=true
- if x>0 then
- face("east")
- succ,dist=forward(x,"stop")
- x=x-dist
- if dist>0 then
- moves[#moves+1]={dir="east",dist=dist}
- blocked=false
- end
- done=done and succ
- elseif x<0 then
- face("west")
- succ,dist=forward(-x,"stop")
- x=x+dist
- if dist>0 then
- moves[#moves+1]={dir="west",dist=dist}
- blocked=false
- end
- done=done and succ
- end
- if y>0 then
- succ,dist=up(y,"stop")
- y=y-dist
- if dist>0 then
- moves[#moves+1]={dir="up",dist=dist}
- blocked=false
- end
- done=done and succ
- elseif y<0 then
- succ,dist=down(-y,"stop")
- y=y+dist
- if dist>0 then
- moves[#moves+1]={dir="down",dist=dist}
- blocked=false
- end
- done=done and succ
- end
- if z>0 then
- face("south")
- succ,dist=forward(z,"stop")
- z=z-dist
- if dist>0 then
- moves[#moves+1]={dir="south",dist=dist}
- blocked=false
- end
- done=done and succ
- elseif z<0 then
- face("north")
- succ,dist=forward(-z,"stop")
- z=z+dist
- if dist>0 then
- moves[#moves+1]={dir="north",dist=dist}
- blocked=false
- end
- done=done and succ
- end
- if done then
- return true
- end
- if blocked then
- if onBlocked=="return" then
- for i=#moves,1,-1 do
- if moves[i].dir=="north" then
- face("south")
- forward(moves[i].dist)
- elseif moves[i].dir=="south" then
- face("north")
- forward(moves[i].dist)
- elseif moves[i].dir=="east" then
- face("west")
- forward(moves[i].dist)
- elseif moves[i].dir=="west" then
- face("east")
- forward(moves[i].dist)
- elseif moves[i].dir=="up" then
- down(moves[i].dist)
- elseif moves[i].dir=="down" then
- up(moves[i].dist)
- end
- end
- face(prevDir)
- return false
- elseif onBlocked=="dig" then
- succ=true
- if x>0 then
- face("east")
- succ=succ and forward(x,"dig")
- elseif x<0 then
- face("west")
- succ=succ and forward(x,"dig")
- end
- if z>0 then
- face("north")
- succ=succ and forward(z,"dig")
- elseif x<0 then
- face("south")
- succ=succ and forward(z,"dig")
- end
- if y>0 then
- succ=succ and up(y,"dig")
- elseif y<0 then
- succ=succ and down(y,"dig")
- end
- return succ
- else
- return false
- end
- end
- end
- end
- function goto(x,y,z,onBlocked)
- return move(x-position.x,y-position.y,z-position.z,onBlocked)
- end
- function face(newDirection)
- if type(newDirection)=="string" then
- newDirection=dirMap[newDirection]
- end
- turns=newDirection-direction
- if turns>2 then turns=turns-4 end
- if turns<-2 then turns=turns+4 end
- if turns<0 then
- for i=1,-turns,1 do
- native.turnLeft()
- end
- elseif turns>0 then
- for i=1,turns,1 do
- native.turnRight()
- end
- end
- direction=newDirection
- savePosition()
- end
- function pushPos()
- posStack[#posStack+1]={position,direction}
- savePosStack()
- end
- function popPos(go)
- if go==nil then go=true end
- local t=posStack[#posStack]
- local pos=t[1]
- local dir=t[2]
- posStack[#posStack]=nil
- savePosStack()
- if go then
- local res=goto(pos.x,pos.y,pos.z)
- face(dir)
- return res
- else
- return true
- end
- end
- function topPos()
- return posStack[#posStack]
- end
- function getPosStackSize()
- return #posStack
- end
- function getPosStackItem(index)
- if type(index)~="number" then
- error("turtlex:getPosStackItem: parameter must be a number!")
- end
- if index<=0 or index>#posStack then
- error("turtlex:getPosStackItem: index out of range!")
- end
- return posStack[index]
- end
- function clearStack()
- posStack={}
- savePosStack()
- end
- function select(index)
- succ,err = pcall(native.select,index)
- if succ then
- selectedSlot=index
- saveSelected()
- return succ
- else
- return false, err
- end
- end
- function getSelected()
- return selectedSlot, myInventory.slots[selectedSlot].type
- end
- function getInventory()
- return myInventory
- end
- function getItemType(slot)
- local prevSlot=selectedSlot
- if slot~=nil then
- select(slot)
- else
- slot=selectedSlot
- end
- local res=myInventory.slots[selectedSlot].type
- if slot~=prevSlot then
- select(prevSlot)
- end
- return res
- end
- function setItemType(type, slot)
- if slot==nil then
- slot=selectedSlot
- end
- myInventory:setType(slot,type)
- end
- function getItemCount(slot)
- if slot==nil then slot=selectedSlot end
- return native.getItemCount(slot)
- end
- function getItemSpace(slot)
- if slot==nil then slot=selectedSlot end
- return native.getItemSpace(slot)
- end
- function findItemType(type, after)
- return myInventory:findSlot(type,after)
- end
- function displayInv()
- myInventory:display()
- end
- local function digHelp(slot,digFunc)
- local prev=selectedSlot
- local items={}
- if slot~=nil then
- select(slot)
- else
- slot=prev
- end
- local res=digFunc()
- if res then
- --successfully dug, attempt to identify
- local suc,add,sub=updateInventory()
- if suc then
- items=add
- end
- end
- --reselect previous slot
- if slot~=prev then
- select(prev)
- end
- return res, items
- end
- function dig(slot)
- return digHelp(slot,native.dig)
- end
- function digUp(slot)
- return digHelp(slot,native.digUp)
- end
- function digDown(slot)
- return digHelp(slot,native.digDown)
- end
- function digBack(slot)
- turnAround()
- res=dig(slot)
- turnAround()
- return res
- end
- function digLeft(slot)
- turnLeft()
- res=dig(slot)
- turnRight()
- return res
- end
- function digRight(slot)
- turnRight()
- res=dig(slot)
- turnLeft()
- return res
- end
- local function placeHelp(slot,placeFunc)
- local prev=selectedSlot
- if slot~=nil then
- select(slot)
- else
- slot=prev
- end
- res=placeFunc()
- if res then
- --successfully placed
- updateInventory()
- end
- --restore selected slot
- if slot~=prev then
- select(prev)
- end
- return res
- end
- function place(index)
- placeHelp(index,native.place)
- end
- function placeUp(index)
- placeHelp(index,native.placeUp)
- end
- function placeDown(index)
- placeHelp(index,native.placeDown)
- end
- function placeBack(slot)
- turnAround()
- res=place(slot)
- turnAround()
- return res
- end
- function placeLeft(slot)
- turnLeft()
- res=place(slot)
- turnRight()
- return res
- end
- function placeRight(slot)
- turnRight()
- res=place(slot)
- turnLeft()
- return res
- end
- local function indexHelper(index)
- local slot=selectedSlot
- if index~=nil then
- if type(index)=="string" then
- slot=findItemType(index)
- if slot==0 then
- return nil,"turtlex:compare: no reference sample for type '"..index.."' in inventory!"
- end
- elseif type(index)=="number" then
- slot=index
- else
- return nil, "turtlex:compare: invalid parameter, expected number or string, got "..type(index)
- end
- end
- return slot
- end
- local function compareHelp(index,compareFunc)
- local prevSlot=selectedSlot
- local slot, err=indexHelper(index)
- if slot==nil then
- return slot, err
- end
- if prevSlot~=slot then
- select(slot)
- end
- local res=compareFunc()
- if slot~=prevSlot then
- select(prevSlot)
- end
- return res
- end
- function compare(index)
- return compareHelp(index,native.compare)
- end
- function compareUp(index)
- return compareHelp(index,native.compareUp)
- end
- function compareDown(index)
- return compareHelp(index,native.compareDown)
- end
- function compareLeft(index)
- turtle.turnLeft()
- local res,err=compare(index)
- turtle.turnRight()
- return res, err
- end
- function compareRight(index)
- turtle.turnRight()
- local res,err=compare(index)
- turtle.turnLeft()
- return res
- end
- function compareBack(index)
- turtle.turnAround()
- local res,err=compare(index)
- turtle.turnAround()
- return res, err
- end
- function compareAll(index)
- local prevSlot=selectedSlot
- local slot, err=indexHelper(index)
- if slot==nil then
- return slot, err
- end
- if prevSlot~=slot then
- select(slot)
- end
- dirs={"forward","right","back","left"}
- matches={}
- if native.compareUp() then
- matches[#matches+1]="up"
- end
- if native.compareDown() then
- matches[#matches+1]="down"
- end
- for i=1,4,1 do
- if native.compare() then
- matches[#matches+1]=dirs[i]
- end
- turtle.turnRight()
- end
- if prevSlot~=slot then
- select(prevSlot)
- end
- return #matches, matches
- end
- function compareTo(slotNum, index)
- local prevSlot=selectedSlot
- local slot,err=indexHelper(index)
- if slot==nil then
- return slot,err
- end
- if prevSlot~=slot then
- select(slot)
- end
- res=native.compareTo(slotNum)
- if prevSlot~=slot then
- select(prevSlot)
- end
- return res
- end
- detect=native.detect
- detectUp=detectUp
- detectDown=detectDown
- function detectLeft()
- turnLeft()
- local res=detect()
- turnRight()
- return res
- end
- function detectRight()
- turnRight()
- local res=detect()
- turnLeft()
- return res
- end
- function detectBack()
- turnAround()
- local res=detect()
- turnAround()
- return res
- end
- local function dropHelp(quantity, index, dropFunc)
- if quantity==0 then
- return true
- end
- local prevSlot=selectedSlot
- local slot,err=indexHelper(index)
- if slot==nil then
- return false, err
- end
- if slot~=prevSlot then
- select(slot)
- end
- local res=turtle.drop(quantity)
- if res then
- updateInventory()
- end
- if slot~=prevSlot then
- select(prevSlot)
- end
- return res
- end
- function drop(quantity, index)
- return dropHelp(quantity,index,native.drop)
- end
- function dropUp(quantity, index)
- return dropHelp(quantity,index,native.dropUp)
- end
- function dropDown(quantity, index)
- return dropHelp(quantity,index,native.dropDown)
- end
- function dropLeft(quantity,index)
- turnLeft()
- local res=dropHelp(quantity,index,native.drop)
- turnRight()
- return res
- end
- function dropRight(quantity,index)
- turnRight()
- local res=dropHelp(quantity,index,native.drop)
- turnLeft()
- return res
- end
- function dropBack(quantity,index)
- turnAround()
- local res=dropHelp(quantity,index,native.drop)
- turnAround()
- return res
- end
- local function suckHelp(slot)
- local prevSlot=selectedSlot
- if slot==nil then
- slot=selectedSlot
- end
- if slot~=prevSlot then
- select(slot)
- end
- local res=suckFunc()
- local succ, add={}, rem;
- if res then
- succ,add,rem=updateInventory()
- end
- if slot~=prevSlot then
- select(prevSlot)
- end
- return res, add
- end
- function suck(slot)
- return suckHelp(slot,native.suck)
- end
- function suckUp(slot)
- return suckHelp(slot,native.suckUp)
- end
- function suckDown(slot)
- return suckHelp(slot,native.suckDown)
- end
- function suckLeft(slot)
- turnLeft()
- local res, qty=suckHelp(slot,native.suck)
- turnRight()
- return res,qty
- end
- function suckRight(slot)
- turnRight()
- local res, qty=suckHelp(slot,native.suck)
- turnLeft()
- return res,qty
- end
- function suckBack(slot)
- turnAround()
- local res, qty=suckHelp(slot,native.suck)
- turnAround()
- return res,qty
- end
- getFuelLevel=native.getFuelLevel
- function refuel(quantity, index)
- local prevSlot=selectedSlot
- local slot,err=indexHelper(index)
- local res
- if slot==nil then
- return false, err
- end
- if prevSlot~=slot then
- select(slot)
- end
- res=native.refuel(quantity)
- if res==true then
- updateInventory()
- end
- if prevSlot~=slot then
- select(prevSlot)
- end
- return res
- end
- function craft(quantity, slot, outputType)
- if turtle.craft==nil then
- return false, "turtlex:craft: Only crafting turtles can craft!"
- end
- local res
- prevSlot=selectedSlot
- if slot==nil then
- slot=selectedSlot
- elseif slot~=prevSlot then
- select(slot)
- end
- if quantity==nil then
- res=native.craft()
- else
- res=native.craft(quantity)
- end
- if slot~=prevSlot then
- select(prevSlot)
- end
- if res==true then
- local wasChange, addedStuff, removedStuff = updateInventory()
- if wasChange==false then
- return res, "inconsistent results from updateInventory - craft reports success, but no change or no added items reported?"
- end
- --did they give us an expected output type?
- if outputType~=nil and outputType~="" and outputType~=0 and outputType~="unknown" then
- local addedType
- if #addedStuff>0 then
- addedType=addedStuff[1].type
- else
- addedType=myInventory.getItemType(slot)
- end
- for i=2,#addedStuff,1 do
- if addedStuff[i].type~=addedType then
- return res, "inconsistent results from updateInventory - multiple new item types detected!"
- end
- end
- setItemType(outputType,addedStuff[1].slot)
- end
- end
- return res
- end
Advertisement
Add Comment
Please, Sign In to add comment