Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("left")
- os.loadAPI('json')
- function get()
- local object = json.decodeFromFile("bot/cords.json")
- cords = {object["x"], object["y"], object["z"], object["direction"]}
- return cords
- end
- function add_to_cords(x, y, z, newdir)
- old_cords = get()
- old_x = old_cords[1]
- old_y = old_cords[2]
- old_z = old_cords[3]
- if newdir ~= -1 then
- dir = newdir
- else
- dir = old_cords[4]
- end
- x = tostring(tonumber(old_x) + x)
- y = tostring(tonumber(old_y) + y)
- z = tostring(tonumber(old_z) + z)
- obj = "{\n \"x\": \"" .. tostring(x) .. "\",\n \"y\": \"" .. tostring(y) .. "\",\n \"z\": \"".. tostring(z) .. "\",\n \"direction\": \"" .. tostring(dir) .. "\"\n}"
- f = fs.open("bot/cords.json", 'w')
- f.write(obj)
- f.close()
- print(obj)
- sleep(2)
- end
- function turnLeft()
- cords = get()
- dir = cords[4]
- new_dir = ""
- if turtle.turnLeft() then
- if dir == "1" then
- new_dir = "4"
- elseif dir == "2" then
- new_dir = "1"
- elseif dir == "3" then
- new_dir = "2"
- elseif dir == "4" then
- new_dir = "3"
- end
- add_to_cords(0, 0, 0, new_dir)
- end
- end
- function turnRight()
- if turtle.turnRight() then
- cords = get()
- dir = cords[4]
- new_dir = ""
- if dir == "1" then
- new_dir = "2"
- elseif dir == "2" then
- new_dir = "3"
- elseif dir == "3" then
- new_dir = "4"
- elseif dir == "4" then
- new_dir = "1"
- end
- add_to_cords(0, 0, 0, new_dir)
- end
- end
- function forward()
- cords = get()
- old_x = cords[1]
- old_2 = cords[2]
- old_3 = cords[3]
- dir = cords[4]
- if turtle.forward() then
- if dir == "1" then
- -- neg Z
- add_to_cords(0, 0, -1, -1)
- elseif dir == "2" then
- -- pos X
- add_to_cords(1, 0, 0, -1)
- elseif dir == "3" then
- -- pos Z
- add_to_cords(0, 0, 1, -1)
- elseif dir == "4" then
- -- neg X
- add_to_cords(-1, 0, 0, -1)
- end
- end
- end
- function back()
- cords = get()
- old_x = cords[1]
- old_2 = cords[2]
- old_3 = cords[3]
- dir = cords[4]
- if turtle.back() then
- if dir == "1" then
- -- pos Z
- add_to_cords(0, 0, 1, -1)
- elseif dir == "2" then
- -- neg X
- add_to_cords(-1, 0, 0, -1)
- elseif dir == "3" then
- -- neg Z
- add_to_cords(0, 0, -1, -1)
- elseif dir == "4" then
- -- pos X
- add_to_cords(1, 0, 0, -1)
- end
- end
- end
- while true do
- local sender, message, method = rednet.receive()
- if message == "w" then
- forward()
- elseif message == "s" then
- back()
- elseif message == "a" then
- turnLeft()
- elseif message == "d" then
- turnRight()
- elseif message == "r" then
- turtle.refuel()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement