Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Functions --
- function store(sName, data)
- local filepath = fs.combine("data", sName)
- if data == nil then
- return fs.delete(filepath)
- end
- local handle = fs.open(filepath, "w")
- handle.write(textutils.serialize(data))
- handle.close()
- end
- function pull(sName)
- local filepath = fs.combine("data", sName)
- local handle = fs.open(filepath, "r")
- local data = handle.readAll()
- handle.close()
- return textutils.unserialize(data)
- end
- function reset()
- local location = {x = 0, y = 0, z = 0, face = 0}
- store("location", location)
- end
- function display()
- local location = pull("location")
- print("x: ", location["x"])
- print("y: ", location["y"])
- print("z: ", location["z"])
- print("face: ", location["face"])
- end
- function left()
- local location = pull("location")
- -- print("face: ", location[4])
- if location["face"] == 1 then
- location["face"] = 0
- elseif location["face"] == 2 then
- location["face"] = 1
- elseif location["face"] == 3 then
- location["face"] = 2
- elseif location["face"] == 0 then
- location["face"] = 3
- end
- store("location", location)
- turtle.turnLeft()
- end
- function right()
- local location = pull("location")
- -- print("face: ",location[4])
- if location["face"] == 1 then
- location["face"] = 2
- elseif location["face"] == 2 then
- location["face"] = 3
- elseif location["face"] == 3 then
- location["face"] = 0
- elseif location["face"] == 0 then
- location["face"] = 1
- end
- store("location", location)
- turtle.turnRight()
- end
- function forward()
- while not turtle.forward() do
- sleep(1)
- end
- local location = pull("location")
- if location["face"] == 0 then
- location["z"] = location["z"] + 1
- elseif location["face"] == 1 then
- location["x"] = location["x"] -1
- elseif location["face"] == 2 then
- location["z"] = location["z"] -1
- elseif location["face"] == 3 then
- location["x"] = location["x"] +1
- end
- store("location", location)
- end
- function back()
- while not turtle.back() do
- sleep(1)
- end
- local location = pull("location")
- if location["face"] == 0 then
- location["z"] = location["z"] - 1
- elseif location["face"] == 1 then
- location["x"] = location["x"] + 1
- elseif location["face"] == 2 then
- location["z"] = location["z"] + 1
- elseif location["face"] == 3 then
- location["x"] = location["x"] - 1
- end
- store("location", location)
- end
- function up()
- while not turtle.up() do
- sleep(1)
- end
- location = pull("location")
- location["y"] = location["y"] + 1
- store("location", location)
- end
- function down()
- while not turtle.down() do
- sleep(1)
- end
- location = pull("location")
- location["y"] = location["y"] - 1
- store("location", location)
- end
- -- Program --
- if not fs.exists("data") then
- fs.makeDir("data")
- end
- if not fs.exists("data/location") then
- reset()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement