Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- theinsektAPIs/poz3
- --experimental, not tested
- --stores posT internally to simplify the usage of the functions
- -- turtle API for going in different directions while keeping track of the changes
- -- in position and direction, compared to a starting point
- -- also keeps track of the number of steps and turns that have been performed
- -- useful when digging, building etc.
- -- New feature: enterSimulationMode(), disables all actions, but still returns true and
- --changes position, steps and turns, exitSimukationMode() to restore everyhting and
- --be able to move again
- --Useful if you have two algorithms and want to test which uses the fewest steps
- --and then exiting simulation mode and running the best of them
- --to start a new simulation while in simulation you have to exit simulation mode then enter
- --again
- -- this api has the following useful functions
- --using wtf style:
- --manipulate position manually
- --setPos(x0,y0,z0,f0)
- --getpos()->(x,y,z,f)
- --resetpos()
- --test properties of the direction
- --validFacing(direction)->(result)
- --validDirection(direction)->(result)
- --validAbsoluteDirection(direction)->(result)
- --manipulate directions
- --getReverse(direction)->(reverseDirection)
- --getAbsoluteDirection(direction)->(absoluteDirection)
- --do actions (valid direction usually: n,s,)
- --valid direction for most are: f,b,r,l,u,d,n,s,e,w
- --(forward, back, right, left, up ,down, north, south, east, west)
- --go(direction)->(succes)
- --turn(direction)->(succes)
- --etc...
- --the current position!!!
- --and stats!
- --can be changed with setPos, getPos and resetPos
- local posT={x=0,y=0,z=0,f="n"}
- local statsT={steps=0,turns=0}
- --simulation, is probably to simple
- --todo: make it so default return values can be set for
- --actions
- inSimulation=false
- local backupPosT=nil
- local backupStatsT=nil
- --in simulation mode will pretend to do the action
- --then return true
- --work in progress
- function enterSimulationMode()
- if not inSimulation then
- print("Entered simulation mode")
- inSimulation=true
- backupPosT=copyTable(posT)
- backupStatsT=copyTable(statsT)
- end
- end
- function exitSimulationMode()
- if inSimulation then
- print("Exited simulation mode")
- inSimulation=false
- posT=copyTable(backupPosT)
- statsT=copyTable(backupStatsT)
- end
- end
- function incrementSteps()
- statsT["steps"]=statsT["steps"]+1
- end
- function incrementTurns()
- statsT["turns"]=statsT["turns"]+1
- end
- function getStats()
- return statsT["steps"], statsT["turns"]
- end
- function setStats(steps,turns)
- statsT["steps"]=steps
- statsT["turns"]=turns
- end
- function resetStats()
- statsT["steps"]=0
- statsT["turns"]=0
- end
- --change coordinates
- --sets the position values
- function setPos(x0,y0,z0,f0)
- --check that none of the inputs are nil
- if(x0==nil) then error("Error: x0 is nil") end
- if(y0==nil) then error("Error: y0 is nil") end
- if(z0==nil) then error("Error: z0 is nil") end
- if(f0==nil) then error("Error: f0 is nil") end
- --check that the f0 direction is recognized
- if(not validFacing(f0)) then error("Error: f0 isn't a valid facing (valid: n,s,w,e)") end
- --check that coordinates are numbers
- if(type(x0) ~= "number") then error("Error: x0 isn't a number") end
- if(type(y0) ~= "number") then error("Error: y0 isn't a number") end
- if(type(z0) ~= "number") then error("Error: z0 isn't a number") end
- --set the position
- posT["x"]=x0
- posT["y"]=y0
- posT["z"]=z0
- posT["f"]=f0
- end
- function setPos2(posT)
- setPos(posT["x"],posT["y"],posT["z"],posT["f"])
- end
- function resetPos()
- setPos(0,0,0,"n")
- end
- --return x,y,z,f
- --uses the same conventions as minecraft
- --but the facing north is determined by the starting direction
- function getPos()
- return posT["x"],posT["y"],posT["z"],posT["f"]
- end
- function copyTable(inTable)
- local outTable={}
- for k,v in pairs(inTable) do
- outTable[k]=v
- end
- return outTable
- end
- function getPos2()
- return copyPos(posT)
- --return {x=posT["x"],y=posT["y"],z=posT["z"],f=posT["f"]}
- end
- function getPosField(fieldName)
- return posT[fieldName]
- end
- function copyPos(aPosT)
- if aPosT==nil then error("Error: aPosT is nil") end
- return {x=aPosT["x"],y=aPosT["y"],z=aPosT["z"],f=aPosT["f"]}
- end
- --check directions
- function validFacing(direction)
- return faceT[direction]~=nil
- end
- function validDirection(direction)
- return reverseT[direction]~=nil
- end
- function validAbsoluteDirection(direction)
- return goUpdateT[direction]~=nil
- end
- function isHorizontal(direction)
- return not isVertical(direction)
- end
- function isVertical(direction)
- direction=getAbsoluteDirection(direction)
- return direction=="u" or direction=="d"
- end
- --various direction calculations
- --directions: n,s,e,w,u,d
- --returns which coordinate would be changed and by how much
- function getCoordinateChange(direction,length)
- local tmp=goUpdateT[direction]
- if tmp==nil then Error("Error: unrecognised direction (valid: n,s,e,w,u,d)") end
- local posTFieldName, change = unpack(tmp)
- return posTFieldName,change*length
- end
- --return the resulting facing after doing a turn in the given direction
- --returns nil if the direction is not valid/recognized
- --directions: n,s,e,w,u,d,f,b,l,r
- function getTurnRes(direction)
- --check inputs
- if direction==nil then error("Error: direction is nil") end
- if not validDirection(direction) then return nil end
- if posT==nil then error("Error: posT is nil") end
- --u or d doesn’t change so return old facing
- if direction=="u" or direction=="d" then
- return posT["f"]
- end
- --n,e,s,w are already facings
- if turnResT[direction] ~= nil then
- return direction --direction is one of n,e,s,w
- end
- --convert f,b,r,l to facing (n,e,s,w)
- local tmp = turnResT[posT["f"]][direction]
- if tmp==nil then error("Error: internal bug") end
- return tmp
- end
- --turns: n,s,e,w,u,d,f,b,l,r into n,s,e,w,u,d
- --directions: n,s,e,w,u,d,f,b,l,r
- --returns nil if the direction isn't recognized
- function getAbsoluteDirection(direction)
- --check input
- if direction==nil then error("Error: direction is nil") end
- if posT==nil then error("Error: posT is nil") end
- --u and d are already absolute direction
- if direction=="u" or direction=="d" then
- return direction
- end
- --get the cardinal direction
- return getTurnRes(direction)
- end
- --get the reverse direction
- --directions:
- --works best for n,s,e,w,u,d, because if you use go on the reverse of those you will go to your
- --previous position
- --also works for f,b,l,r
- function getReverse(direction)
- return reverseT[direction]
- end
- --get a new table with position 0
- --all the parameters are optional, just use nil to
- --to set the default value
- --x0,y0,z0 are integer number
- --f0 is a string that can be n,e,s,w
- function getNewPos(x0,y0,z0,f0)
- --replace nil with default value
- if x0==nil then x0=0 end
- if y0==nil then y0=0 end
- if z0==nil then z0=0 end
- if f0==nil then f0="n" end
- --check that the f0 direction is recognized
- if(not validFacing(f0)) then error("Error: f0 isn't a valid facing (valid: n,s,w,e)") end
- --check that coordinates are numbers
- if(type(x0) ~= "number") then error("Error: x0 isn't a number") end
- if(type(y0) ~= "number") then error("Error: y0 isn't a number") end
- if(type(z0) ~= "number") then error("Error: z0 isn't a number") end
- --return new posT
- return {x=x0,y=y0,z=z0,f=f0}
- end
- --get the direction and distance needed to get to a coord value
- function coordToGoValues(coordName,wantedCoordValue)
- return coordChangeToDirection(coordName, wantedCoordValue-posT[coordName])
- end
- --turns x,y,z into s,e,u
- --negative length reverses the direction
- --can be used to get the direction and distance if you want to
- --go to a certain coordinate
- function coordChangeToDirection(coordName, length)
- local direction=fieldToDirectionT[coordName]
- if length<0 then
- direction=getReverse(direction)
- length=-length
- end
- return direction, length
- end
- --turn, go, detect etc...
- --turn in/to the given direction
- --directions: n,s,e,w,u,d,f,b,l,r
- function turn(direction)
- if direction==nil then error("Error: direction is nil") end
- if posT==nil then error("Error: posT is nil") end
- if turnT[direction]==nil then
- direction=faceT[posT["f"]][direction] -- translates n,s,e,w into eqvivialent r,l,u,d,f,b
- end
- if direction==nil then error("Error: unrecognized direction") end
- return turnT[direction]() -- r,l,u,d,f,b
- end
- --goes in the given direction
- --uses turn if necessary
- --directions: n,s,e,w,u,d,f,b,l,r
- function go(direction)
- if direction==nil then error("Error: direction is nil") end
- return turnDo(goT,direction)
- end
- function attack(direction)
- return turnDo(attackT,direction)
- end
- function dig(direction)
- return turnDo(digT,direction)
- end
- --sign text should be nil, or not specified if direction is u or d
- function place(direction,signText)
- return turnDo(placeT,direction,signText)
- end
- function detect(direction)
- return turnDo(detectT,direction)
- end
- function compare(direction)
- return turnDo(compareT,direction)
- end
- function drop(direction,anumber)
- return turnDo(dropT,direction,anumber)
- end
- function suck(direction)
- return turnDo(suckT,direction)
- end
- --available in cc1.64 and later
- function inspect(direction)
- return turnDo(inspectT,direction)
- end
- --help functions
- --help function, turns if necessary, then looks up an action in the doT (f,u or d) and does the --action. The do table (doT) should have f,u and d
- -- turn do returns the result of the do action
- function turnDo(doT,direction,optional)
- if posT==nil then error("Error: posT is nil") end
- if direction==nil then error("Error: direction is nil") end
- if doT==nil then error("Error: doT is nil") end
- if doT[direction] == nil then -- if does not have action in that direction then turn
- direction=turn2(direction) -- turn and get "after turn action direction"
- end
- return doT[direction](optional)--do the function
- end
- -- turns and then return the "after turn action direction" (f, u or d)
- function turn2(direction)
- turn(direction)
- return getTurnDoDirection(direction)
- end
- function getTurnDoDirection(direction)
- if direction=="u" or direction=="d" then
- return direction --up or down
- else
- return "f"--forward
- end
- end
- --directions: n,s,e,w,u,d
- function goUpdate(direction)
- local posTFieldName, change = getCoordinateChange(direction,1)
- posT[posTFieldName]=posT[posTFieldName]+change
- return true
- end
- --updates the facing in pos if it’s a valid turn direction
- --returns true if it was a valid turn direction, and posT was updated
- --directions: n,s,e,w,u,d,f,b,l,r
- function turnUpdate(direction)
- local facing=getTurnRes(direction)
- if facing==nil then
- return false
- end
- posT["f"]=facing
- return true
- end
- function doCommand(commandName,direction,optional)
- return commandNameT[commandName](direction,optional)
- end
- commandNameT={
- ["turn"]=turn,
- ["go"]=go,
- ["attack"]=attack,
- ["dig"]=dig,
- ["place"]=place,
- ["detect"]=detect,
- ["compare"]=compare,
- ["drop"]=drop,
- ["suck"]=suck,
- ["ddgloop"]=function(direction,optional) return doLoop(tonumber(optional),detectDigGo,direction,nil) end,
- }
- commandUsageT={
- ["turn"]="usage: turn <direction> a \nexplanation: turns in the given direction",
- ["go"]="usage: go <direction> a \nexplanation: tries to go in the given direction",
- ["attack"]="usage: attack <direction> a \nexplanation: tries to attack in the given direction",
- ["dig"]="usage: dig <direction> a \nexplanation: tries to dig a block in the given direction",
- ["place"]="usage: place <direction> <optionalSignText> \nexplanation: places an item from the selected slot in the given direction",
- ["detect"]="usage: detect <direction> a \nexplanation: returns true if there is a block in the given direction else false",
- ["compare"]="usage: compare <direction> a \nexplanation: compares the block in the given direction to the item in the selected slot",
- ["drop"]="usage: drop <direction> <count> \nexplanation: drops count items in the given direction",
- ["suck"]="usage: suck <direction> a \nexplanation:",
- ["ddgloop"]="usage: ddgloop <direction> <length> \nexplanation: digs and moves in the given direction for the given length",
- }
- -- reverse: return the reverse of the given action, useful you want to move in one direction and then back
- --this table is used look up the reverse action
- -- the reverse function uses this table to look up what the reverse action is
- reverseT={
- f="b",
- b="f",
- n="s", --turn north, reverse: turn south
- s="n", --turn south, reverse: turn north
- e="w", --turn east, reverse: turn west
- w="e", --turn west, reverse: turn east
- u="d", --turn up, reverse: turn down (tu does nothing)
- d="u", --turn down, reverse: turn up (td does nothing)
- r="l", --turn right, reverse: turn left
- l="r", --turn left, reverse: turn right
- }
- -- This is a lookup table containing functions
- -- go uses this table to simply look up what function is needed to do the given action
- goT={
- --go forward then update position
- f=function()
- if(inSimulation or turtle.forward()) then
- incrementSteps()
- goUpdate(posT["f"])
- return true
- end
- return false
- end,
- --go up then update position
- u=function()
- if(inSimulation or turtle.up()) then
- incrementSteps()
- goUpdate("u")
- return true
- end
- return false
- end,
- --go down then update position
- d=function()
- if(inSimulation or turtle.down()) then
- incrementSteps()
- goUpdate("d")
- return true
- end
- return false
- end,
- }
- turnT={
- --turn right and then update position
- r=function()
- if(inSimulation or turtle.turnRight()) then
- incrementTurns()
- turnUpdate("r")
- return true
- end
- return false
- end,
- --turn left and then update position
- l=function()
- if(inSimulation or turtle.turnLeft()) then
- incrementTurns()
- turnUpdate("l")
- return true
- end
- return false
- end,
- --does nothing, but may avoid special case in your code
- u=function() return true end,
- --does nothing, but may avoid special case in your code
- d=function() return true end,
- --does nothing, keeps facing forward
- f=function() return true end,
- --calls turn right twice, used to turn 180 degrees
- b=function()
- return turn("r") and turn("r")
- end,
- }
- --given current facing and wanted facing
- --used to look up the needed turn action
- faceT={
- n={
- n="f",
- s="b",
- e="r",
- w="l",
- },
- s={
- s="f",
- n="b",
- w="r",
- e="l",
- },
- e={
- e="f",
- w="b",
- s="r",
- n="l",
- },
- w={
- w="f",
- e="b",
- n="r",
- s="l",
- },
- }
- --lookup table to se what the resulting facing is after doing a turn in the given direction
- --does not include u or d, because they don’t change the facing
- --does not include n,e,s,w becuase they are alredy facings
- --each subtable is the inverse of the corresponding subtable in faceT
- turnResT=
- {
- n={
- f="n",
- b="s",
- r="e",
- l="w",
- },
- s={
- f="s",
- b="n",
- r="w",
- l="e",
- },
- e={
- f="e",
- b="w",
- r="s",
- l="n",
- },
- w={
- f="w",
- b="e",
- r="n",
- l="s",
- },
- }
- --http://stackoverflow.com/questions/7925090/lua-find-a-key-from-a-value
- --function table_invert(t)
- -- local s={}
- -- for k,v in pairs(t) do
- -- s[v]=k
- -- end
- -- return s
- --end
- --turnResT={
- --n=table_invert(faceT["n"]),
- --s=table_invert(faceT["s"]),
- --e=table_invert(faceT["e"]),
- --w=table_invert(faceT["w"]),
- --}
- --use to look up the coordinate change
- --going one step in the given direction
- goUpdateT={
- n={"z",-1},
- s={"z",1},
- e={"x",1},
- w={"x",-1},
- u={"y",1},
- d={"y",-1},
- }
- --used to look up the direction
- --to increase the given coordinate
- fieldToDirectionT={
- x="e",
- y="u",
- z="s",
- }
- attackT={
- f=function() return inSimulation or turtle.attack() end,
- u=function() return inSimulation or turtle.attackUp() end,
- d=function() return inSimulation or turtle.attackDown() end,
- }
- digT={
- f=function() return inSimulation or turtle.dig() end,
- u=function() return inSimulation or turtle.digUp() end,
- d=function() return inSimulation or turtle.digDown() end,
- }
- placeT={
- f=function(signText) return inSimulation or turtle.place(signText) end,--TEST
- u=function() return inSimulation or turtle.placeUp() end,
- d=function() return inSimulation or turtle.placeDown() end,
- }
- detectT={
- f=function() return inSimulation or turtle.detect() end,
- u=function() return inSimulation or turtle.detectUp() end,
- d=function() return inSimulation or turtle.detectDown() end,
- }
- compareT={
- f=function() return inSimulation or turtle.compare() end,
- u=function() return inSimulation or turtle.compareUp() end,
- d=function() return inSimulation or turtle.compareDown() end,
- }
- dropT={
- f=function(count) return inSimulation or turtle.drop(count) end, --TEST
- u=function(count) return inSimulation or turtle.dropUp(count) end,--TEST
- d=function(count) return inSimulation or turtle.dropDown(count) end,--TEST
- }
- suckT={
- f=function() return inSimulation or turtle.suck() end,
- u=function() return inSimulation or turtle.suckUp() end,
- d=function() return inSimulation or turtle.suckDown() end,
- }
- inspectT={
- f=function() return turtle.inspect() end,
- u=function() return turtle.inspectUp() end,
- d=function() return turtle.inspectDown() end,
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement