Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --this is an experiment
- --will hold movement help functions
- --and change in position caused by movement
- -- directions
- f="f" -- forward
- b="b" -- back
- u="u" -- up
- d="d" -- down
- tr="r" -- turn right
- tl="l" -- turn left
- r="r" -- right, not used yet
- l="l" -- left, not used yet
- n="n" -- north (the direction that has been set to north, not the in game north)
- s="s" -- south
- w="w" -- west
- e="e" -- east
- sr="sr" -- strafe right, not yet implemented
- sl="sl" -- strafe left, not yet implemented
- -- position data names
- x="x" -- forward, back
- y="y" -- up, down
- z="z" -- right, left (not turn)
- dir="dir" -- direction the turtle is facing, n, e, s, w
- -- lookup table for: function rev(dir)
- revT={
- f=b,
- b=f,
- u=d,
- d=u,
- r=l,
- l=r,
- n=s,
- s=n,
- w=e,
- e=w,
- }
- -- returns the reverse/inverted direction
- function rev(dir)
- return revT[dir]
- end --function
- -- functions to update pos in horizontal movement
- horzT={
- n=function(posT,length)
- posT[x]=posT[x]+length
- end,
- e=function(posT,length)
- posT[z]=posT[z]+length
- end,
- s=function(posT,length)
- posT[x]=posT[x]-length
- end,
- w=function(posT,length)
- posT[z]=posT[z]-length
- end,
- }
- getUpdateFuncT={
- f=function(dir)
- return horzT[dir]
- end,
- b=function(dir)
- return horzT[rev(dir)]
- end,
- tr=function(dir)
- end
- }
- -- lookup tables to quickly get the
- -- turtle function for a given direction
- detectT={
- f=turtle.detect,
- u=turtle.detectUp,
- d=turtle.detectDown,
- }
- compareT={
- f=turtle.compare,
- u=turtle.compareUp,
- d=turtle.compareDown,
- }
- digT={
- f=turtle.dig,
- u=turtle.digUp,
- d=turtle.digDown,
- }
- goT={
- f=turtle.forward,
- b=turtle.back,
- u=turtle.up,
- d=turtle.down,
- tr=turtle.turnRight,
- tl=turtle.turnLeft,
- }
- turnT={
- tr=turtle.turnRight,
- tl=turtle.turnLeft,
- }
- go2T={
- f=function(pos,length)
- -- go forward the given length
- local dist=0
- while dist < length and turtle.forward() do
- dist=dist+1
- end
- -- update the position
- horzT[pos["dir"]](pos,dist)
- -- return the completed length
- return dist
- end
- b=function(length,pos)
- -- go back the given length
- local dist=0
- while dist < length and turtle.back() do
- dist=dist+1
- end
- -- update the position
- horzT[pos[dr]](pos,-dist)
- -- return the completed length
- return dist
- end
- }
- function testGoHelp()
- go=turtle.forward
- update=getUpdateFuncT[f](pos[dr])
- end
- function helpGo(go, update, length,pos)
- local dist=0
- while dist < length and go() do
- dist = dist + 1
- end
- update(pos,dist)
- return dist
- end
- function getNewPoz(x,y,z,dir)
- if x==nil then x=0 end
- if y==nil then y=0 end
- if z==nil then z=0 end
- if rot==nil then dir="n" end
- local pos={
- ["x"]=x,
- ["y"]=y,
- ["z"]=z,
- [dr]=dir,
- }
- end
- -- may change the direction the turtle is facing
- go(dir,length,pos)
- if length < 0 then
- return go(rev(dir),-length,pos)
- end
- --length must be positive here
- gof=goT[dir]
- if gof == nil then
- --turn to right direction
- dir=f
- gof=goT[dir]
- end
- local dist=0
- while dist<length and gof() do
- end
- end
- -- lookup table for knowing the tuen value
- -- when facing direction and want to face other direction
- faceT={
- n={n=0,e=1,s=2,w=-1},
- e={n=-1,e=0,s=1,w=2},
- w={n=1,e=2,s=-1,w=0},
- s={n=2,e=-1,s=0,w=1},
- }
- --warning: does not update pos
- rotate(length)
- if length==0 then
- return true
- end
- local turning
- if length>0 then
- turning=turtle.turnRight
- else
- turning=turle.turnLeft
- end
- local repeats=0
- while repeats<length do
- turning()
- repeats=repeats+1
- end
- return true
- end
- -- turns the turtle so it's facing in the given newDir
- face(newDir, pos)
- local oldDir=pos["dir"]
- rotate(faceT[oldDir][newDir])
- pos["dir"]=newDir
- return true
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement