Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Let dir 0-3 be {N, E, S, W}
- -- east is positive x, south is positive z
- directions = {'north','east','south','west'}
- default = {x=0,y=0,z=0,direction=0}
- function save(table,filename)
- local file = fs.open(filename,"w")
- file.write(textutils.serialize(table))
- file.close()
- end
- function load(filename)
- local file = fs.open(filename,"r")
- local data = textutils.unserialize(file.readAll())
- file.close()
- return data
- end
- function dir(dir)
- for i, v in pairs(directions) do
- if string.lower(dir) == v then
- return i
- end
- end
- return false
- end
- function forward(dist)
- if not turtle then return end
- pos = getPos('locale')
- if turtle.forward() then
- dx = math.floor(math.cos((pos['direction']-1)*math.pi/2))
- dz = -math.floor(math.cos(pos['direction']*math.pi/2))
- pos['x'] = pos['x'] + dx
- pos['z'] = pos['z'] + dz
- save(pos,'locale')
- return true
- end
- return false
- end
- fwd = forward
- function right()
- if not turtle then return end
- pos = getPos('locale')
- if turtle.turnRight() then
- pos['direction'] = (pos['direction'] + 1)%4
- save(pos,'locale')
- return true
- end
- return false
- end
- function left()
- if not turtle then return end
- pos = getPos('locale')
- if turtle.turnLeft() then
- pos['direction'] = (pos['direction'] - 1)%4
- save(pos,'locale')
- return true
- end
- return false
- end
- function up()
- if not turtle then return end
- pos = getPos('locale')
- if turtle.up() then
- pos['y'] = pos['y'] + 1
- save(pos,'locale')
- return true
- end
- return false
- end
- function down()
- if not turtle then return end
- pos = getPos('locale')
- if turtle.down() then
- pos['y'] = pos['y'] - 1
- save(pos,'locale')
- return true
- end
- return false
- end
- function getPos()
- if not turtle then return end
- if not fs.exists('locale') then
- save(default, 'locale')
- end
- return load('locale')
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement