Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- theinsektAPIs/poz2
- -- turtle API for going in different directions while keeping track of the changes
- -- in position and direction, compared to a starting point
- -- useful when digging, building etc.
- -- this api has the following useful functions
- -- getNewPosT: get a table with default values, (x=0,y=0,z=0,f="n"), this represents a position
- --The info below is old. Now uses the same directions as in minecraft.
- -- x is the coordinate with a coordinate axis from west to east (larger values => further east)
- -- y is the coordinate with a coordinate axis from south to north (larger values => further north)
- -- z is the height coordinate (larger values => higher up)
- -- f is the facing, posible directions are "n" (north), "s" (south), "e" (east) and "w" (west)
- -- "n" is only what the turtle thinks is north, it does not have to be the ingame north
- -- go(direction,posT):
- -- goes in the given direction
- -- direction:, possible directions are: f,b,r,l,u,d,n,s,e,w
- -- (forward, back, right, left, up ,down, north, south, east, west)
- -- posT: a table that contains the x, y, z, and f (facing)
- ---Help function used by the other functions in this api
- --directions: n,s,e,w,u,d
- --assumes no turning is neccesary
- function goUpdate(direction,posT)
- --goUpdateT[direction](posT)
- local posTFieldName, change = unpack(goUpdateT[direction])
- posT[posTFieldName]=posT[posTFieldName]+change
- return true
- end
- function getCoordinateChange(direction,length)
- local tmp=goUpdateT[direction]
- if tmp==nil then Error("Error: unrecognised direction (valid: n,e,s,w,u,d)") end
- local posTFieldName, change = unpack(tmp)
- return posTFieldName,change*length
- 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,posT)
- local facing=getTurnRes(direction,posT)
- if facing==nil then
- return false
- end
- posT["f"]=facing
- return true
- end
- -- return the resulting facing after doing a turn in the direction
- -- as a second return value: returns true if facing changed else false
- function getTurnRes(direction,posT)
- if direction==nil then error("direction must not be nil") end
- if posT==nil then error("Error: posT is nil") end
- --u or d dosen't change so return old facing
- if direction=="u" or direction=="d" then
- return posT["f"]
- end
- --n,e,s,w are alreadyfacings
- if turnResT[direction] ~= nil then --check if n,e,s,w
- return direction
- end
- --convert f,b,r,l to facing (n,e,s,w)
- return turnResT[posT["f"]][direction]
- end
- function getAbsoluteDirection(direction,posT)
- if direction==nil then error("Error: direction is nil") end
- if posT==nil then error("Error: posT is nil") end
- if direction=="u" or direction=="d" then
- return direction
- end
- return getTurnRes(direction,posT)
- end
- --help function, turns if neccesary, 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,posT,optional)
- if direction==nil then error("direction must not be nil") end
- if posT==nil then error("Error: posT is nil") end
- if doT[direction] == nil then -- if does not have action in that direction then turn
- direction=turn2(direction,posT) -- turn and get "after turn action direction"
- end
- --if optional==nil then
- -- return doT[direction](posT) -- do the action in the given direction
- --end
- return doT[direction](posT,optional)--has an optional argument so do the optional version
- end
- -- turns and then return the "after turn action direction" (f, u or d)
- function turn2(direction,posT)
- turn(direction,posT)
- return turnToDoT[direction]
- end
- --- API functions
- --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 getNewPosT(x0,y0,z0,f0)
- 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
- if faceT[f0]==nil then error("Error: unrecognised direction") end
- return {x=x0,y=y0,z=z0,f=f0}
- end
- function copyPos(posT)
- if posT==nil then error("Error: posT is nil") end
- return {x=posT["x"],y=posT["y"],z=posT["z"],f=posT["f"]}
- end
- --get the reverse direction
- --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 tr,tl,f,b,l,r
- function getReverse(direction)
- return reverseT[direction]
- end
- --turns x,y,z into s,e,u
- --negative length reverses the direction
- function getFieldToDirection(fieldName, length)
- local direction=fieldToDirectionT[fieldName]
- if length<0 then
- direction=getReverse(direction)
- end
- return direction
- end
- --turn in/to the given direction
- --directions: n,s,e,w,u,d,f,b,l,r
- --the direction is a string
- --posT is a table
- function turn(direction,posT)
- if direction==nil then error("direction must not be 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](posT) -- r,l,u,d,f,b
- end
- --will turn if neccesary, to move in the given direction
- --if you want to restore the current facing you have to save the current facing and
- -- then use turn afterwards example:
- -- local myFacing=posT["f"]
- -- if go("e",posT) then turn(myFacing,posT) end
- function go(direction,posT)
- return turnDo(goT,direction,posT)
- end
- function attack(direction,posT)
- return turnDo(attackT,direction,posT)
- end
- function dig(direction,posT)
- return turnDo(digT,direction,posT)
- end
- --sign text should be nil, or not specified if direction is u or d
- function place(direction,posT,signText)
- return turnDo(placeT,direction,posT,signText)
- end
- function detect(direction,posT)
- return turnDo(detectT,direction,posT)
- end
- function compare(direction,posT)
- return turnDo(compareT,direction,posT)
- end
- function drop(direction,posT,anumber)
- return turnDo(dropT,direction,posT,anumber)
- end
- function suck(direction,posT)
- return turnDo(suckT,direction,posT)
- end
- --returns the number of succesful iterations
- --turns the direction into n,e,s,w and then
- --repetedly calls the doFunction with that direction
- function doLoop(iters,doFunction,direction,posT,optional)
- direction=getAbsoluteDirection(direction,posT)--turn into n,e,s,w,u,d
- if direction==nil then error("Error: unrecognized direction") end
- if doFunction==nil then error("Error: called doLoop with doFunction==nil") end
- --print(direction)
- local i=0
- --while( i<iters and doFunction(direction,posT,optional)) do i=i+1 end
- turn(direction,posT)
- while( i<iters) do
- --print(direction," ",posT," ",optional)
- if not doFunction(direction,posT,optional) then
- --print("failed detectdiggo")
- break
- end
- i=i+1
- end
- return i
- end
- --first tries detect, digs if detected and if clear then tries to go
- function detectDigGo(direction,posT)
- if(detect(direction,posT)) then
- if(not dig(direction,posT)) then
- return false
- end
- end
- return go(direction,posT)
- end
- function doCommand(commandName,direction,posT,optional)
- return commandNameT[commandName](direction,posT,optional)
- end
- commandNameT={
- ["turn"]=turn,
- ["go"]=go,
- ["attack"]=attack,
- ["dig"]=dig,
- ["place"]=place,
- ["detect"]=detect,
- ["compare"]=compare,
- ["drop"]=drop,
- ["suck"]=suck,
- ["ddgloop"]=function(direction,posT,optional) return doLoop(tonumber(optional),detectDigGo,direction,posT,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(posT)
- if(turtle.forward()) then
- goUpdate(posT["f"],posT)
- return true
- end
- return false
- end,
- --go up then update position
- u=function(posT)
- if(turtle.up()) then
- goUpdate("u",posT)
- return true
- end
- return false
- end,
- --go down then update position
- d=function(posT)
- if(turtle.down()) then
- goUpdate("d",posT)
- return true
- end
- return false
- end,
- }
- turnT={
- --turn right and then update position
- r=function(posT)
- if(turtle.turnRight()) then
- turnUpdate("r",posT)
- return true
- end
- return false
- end,
- --turn left and then update position
- l=function(posT)
- if(turtle.turnLeft()) then
- turnUpdate("l",posT)
- return true
- end
- return false
- end,
- --does nothing, but may avoid special case in your code
- u=function(posT) return true end,
- --does nothing, but may avoid special case in your code
- d=function(posT) return true end,
- --does nothing, keeps facing forward
- f=function(posT) return true end,
- --calls turn right twice, used to turn 180 degrees
- b=function(posT)
- return turn("r",posT) and turn("r",posT)
- 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"]),
- --}
- --get the do direction that should be used after turning
- --do the action in this direction after having done the turn
- turnToDoT={
- f="f",
- b="f",
- n="f",
- s="f",
- e="f",
- w="f",
- u="u",
- d="d",
- r="f",
- l="f",
- }
- --used to update the position when moving
- --goUpdateT={
- --n=function(posT) posT["y"]=posT["y"]+1 end,
- --s=function(posT) posT["y"]=posT["y"]-1 end,
- --e=function(posT) posT["x"]=posT["x"]+1 end,
- --w=function(posT) posT["x"]=posT["x"]-1 end,
- --u=function(posT) posT["z"]=posT["z"]+1 end,
- --d=function(posT) posT["z"]=posT["z"]-1 end,
- --}
- goUpdateT={
- n={"z",-1},
- s={"z",1},
- e={"x",1},
- w={"x",-1},
- u={"y",1},
- d={"y",-1},
- }
- fieldToDirectionT={
- x="e",
- y="u",
- z="s",
- }
- attackT={
- f=function(PosT) return turtle.attack() end,
- u=function(PosT) return turtle.attackUp() end,
- d=function(PosT) return turtle.attackDown() end,
- }
- digT={
- f=function(posT) return turtle.dig() end,
- u=function(posT) return turtle.digUp() end,
- d=function(posT) return turtle.digDown() end,
- }
- placeT={
- f=function(posT,signText) return turtle.place(signText) end,--TEST
- u=function(posT) return turtle.placeUp() end,
- d=function(posT) return turtle.placeDown() end,
- }
- detectT={
- f=function(PosT) return turtle.detect() end,
- u=function(PosT) return turtle.detectUp() end,
- d=function(PosT) return turtle.detectDown() end,
- }
- compareT={
- f=function(PosT) return turtle.compare() end,
- u=function(PosT) return turtle.compareUp() end,
- d=function(PosT) return turtle.compareDown() end,
- }
- dropT={
- f=function(PosT,count) return turtle.drop(count) end, --TEST
- u=function(PosT,count) return turtle.dropUp(count) end,--TEST
- d=function(PosT,count) return turtle.dropDown(count) end,--TEST
- }
- suckT={
- f=function(PosT) return turtle.suck() end,
- u=function(PosT) return turtle.suckUp() end,
- d=function(PosT) return turtle.suckDown() end,
- }
- detectDigGoT={
- f=function(PosT) return detectDigGo("f") end,
- u=function(PosT) return detectDigGo("u") end,
- d=function(PosT) return detectDigGo("d") end,
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement