Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- turtlemovement.lua, my version of lama.
- this library tracks the turtle position and facing just like lama, but because lama is broken, I can not use it.
- tData table:
- tData[1] is turtle position
- {1,2,3,"east"}
- ]]
- local settingsPath = ".tm/settings"
- if not fs.exists(settingsPath) then
- fs.open(settingsPath,"w")
- end
- local tm = {}
- local function save(tTbl,sFilepath)
- local file = fs.open(sFilepath,"w")
- file.write(textutils.serialise(tTbl))
- file.close()
- end
- local function load(sFilepath)
- local file = fs.open(sFilepath,"r")
- local data = file.readAll()
- file.close()
- return textutils.unserialise(data)
- end
- local tData = load(settingsPath)
- local tLoc = tData[1]
- local x,y,z,facing = tLoc[1],tLoc[2],tLoc[3],tLoc[4]
- local function savePos()
- tLoc[1],tLoc[2],tLoc[3],tLoc[4] = x,y,z,facing
- tData[1] = tLoc
- end
- function tm.forward(nDistance)
- nDistance = nDistance or 1
- for i=1,nDistance do
- turtle.forward()
- if facing == "north" then z = z - 1
- elseif facing == "west" then x = x - 1
- elseif facing == "south" then z = z + 1
- elseif facing == "east" then x = x + 1 end
- end
- end
- function tm.back(nDistance)
- nDistance = nDistance or 1
- for i=1,nDistance do
- turtle.forward()
- if facing == "north" then z = z + 1
- elseif facing == "west" then x = x + 1
- elseif facing == "south" then z = z - 1
- elseif facing == "east" then x = x - 1 end
- end
- end
- function tm.up(nDistance)
- nDistance = nDistance or 1
- for i=1,nDistance do
- turtle.up()
- y = y + 1
- end
- end
- function tm.down(nDistance)
- nDistance = nDistance or 1
- for i=1,nDistance do
- turtle.up()
- y = y - 1
- end
- end
- function tm.turnLeft()
- turtle.turnLeft()
- if facing == "north" then facing = "west"
- elseif facing == "west" then facing = "south"
- elseif facing == "south" then facing = "east"
- elseif facing == "east" then facing = "north" end
- end
- function tm.turnLeft()
- turtle.turnLeft()
- if facing == "north" then facing = "east"
- elseif facing == "west" then facing = "north"
- elseif facing == "south" then facing = "west"
- elseif facing == "east" then facing = "south" end
- end
- function tm.setPosition(x,y,z,facing)
- tLoc[1],tLoc[2],tLoc[3],tLoc[4] = x,y,z,facing
- end
- function tm.getPositon()
- return x,y,z,facing
- end
- function tm.save()
- savePos()
- end
- return tm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement