Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.clear()
- term.setCursorPos(1,1)
- print("Do you want to instal the coördinate based movement system made by Cookiebal?")
- local answer = read()
- if answer == "yes" then
- file = io.open("movesys", "w")
- file:write([[
- maxx, maxy = term.getSize()
- pervarpath = "/movementsystem/pervar/"
- function mspervarcreate(pervarname)
- pervarfile = io.open(pervarpath..pervarname..".txt", "r")
- if pervarfile
- then
- pervarfile:close()
- return "Variable already exists"
- else
- pervarfile = io.open(pervarpath..pervarname..".txt","w")
- end
- end
- function mspervardelete(pervarname)
- pervarfile = io.open(pervarpath..pervarname..".txt", "r")
- if pervarfile
- then
- pervarfile:close()
- fs.delete(pervarpath..pervarname..".txt")
- else
- return "Variable doesn't exist"
- end
- end
- function mspervarchange(pervarname, newvalue)
- pervarfile = io.open(pervarpath..pervarname..".txt", "r")
- if pervarfile
- then
- pervarfile:close()
- pervarfile = io.open(pervarpath..pervarname..".txt","w")
- pervarfile:write(newvalue)
- pervarfile:close()
- else
- return "Variable doesn't exist"
- end
- end
- function mspervarread(pervarname)
- pervarfile = io.open(pervarpath..pervarname..".txt", "r")
- if pervarfile
- then
- pervarcontent = pervarfile:read()
- pervarfile:close()
- return pervarcontent
- else
- return "Variable doesn't exist"
- end
- end
- function facenorth()
- local currentface = tonumber(mspervarread("currentface"))
- if currentface == 0 then turtle.turnRight() turtle.turnRight() end
- if currentface == 1 then turtle.turnRight() end
- if currentface == 3 then turtle.turnLeft() end
- mspervarchange("currentface", 2)
- end
- function faceeast()
- local currentface = tonumber(mspervarread("currentface"))
- if currentface == 1 then turtle.turnRight() turtle.turnRight() end
- if currentface == 2 then turtle.turnRight() end
- if currentface == 0 then turtle.turnLeft() end
- mspervarchange("currentface", 3)
- end
- function facesouth()
- local currentface = tonumber(mspervarread("currentface"))
- if currentface == 2 then turtle.turnRight() turtle.turnRight() end
- if currentface == 3 then turtle.turnRight() end
- if currentface == 1 then turtle.turnLeft() end
- mspervarchange("currentface", 0)
- end
- function facewest()
- local currentface = tonumber(mspervarread("currentface"))
- if currentface == 3 then turtle.turnRight() turtle.turnRight() end
- if currentface == 0 then turtle.turnRight() end
- if currentface == 2 then turtle.turnLeft() end
- mspervarchange("currentface", 1)
- end
- function movenorth()
- facenorth()
- didmove = turtle.forward()
- if didmove then
- local newzcoord = mspervarread("coordz") - 1
- mspervarchange("coordz", newzcoord)
- else
- return "blocked"
- end
- end
- function movesouth()
- facesouth()
- didmove = turtle.forward()
- if didmove then
- local newzcoord = mspervarread("coordz") + 1
- mspervarchange("coordz", newzcoord)
- else
- return "blocked"
- end
- end
- function movewest()
- facewest()
- didmove = turtle.forward()
- if didmove then
- local newxcoord = mspervarread("coordx") - 1
- mspervarchange("coordx", newxcoord)
- else
- return "blocked"
- end
- end
- function moveeast()
- faceeast()
- didmove = turtle.forward()
- if didmove then
- local newxcoord = mspervarread("coordx") + 1
- mspervarchange("coordx", newxcoord)
- else
- return "blocked"
- end
- end
- function moveup()
- didmove = turtle.up()
- if didmove then
- local newxcoord = mspervarread("coordy") + 1
- mspervarchange("coordy", newxcoord)
- else
- return "blocked"
- end
- end
- function movedown()
- didmove = turtle.down()
- if didmove then
- local newxcoord = mspervarread("coordy") - 1
- mspervarchange("coordy", newxcoord)
- else
- return "blocked"
- end
- end
- function printui()
- term.clear()
- for rawr=1,1 do
- if maxx > 16 then
- if maxx > 34 then
- if maxx > 50 then
- term.setCursorPos(((maxx-47)/2)+1, 1)
- write("Coordinate Based Movement System - by Cookiebal")
- break
- end
- term.setCursorPos((maxx-32)/2, 1)
- write("Movement System - by Cookiebal")
- break
- end
- term.setCursorPos((maxx-15)/2, 1)
- write("Movement System")
- end
- end
- term.setCursorPos(1,2)
- for n=1, maxx-1 do write("=") end
- term.setCursorPos(1,3)
- end
- function coordchoicequestion()
- print("Do you want to use the -current- coördinates, -locate- them via gps or enter them -manual-ly?")
- while true do
- input = read()
- if input == "current" or input == "locate" or input == "manual" then
- coordchoice = input
- break
- else print('Type either "current", "locate" or "manual"')
- end
- end
- end
- function coordmanual()
- print("Give the coördinates and which direction the turtle is facing.")
- write("X: ")
- xcoord = read()
- write("Y: ")
- ycoord = read()
- write("Z: ")
- zcoord = read()
- write("Direction: ")
- direction = read()
- mspervarchange("coordx", xcoord)
- mspervarchange("coordy", ycoord)
- mspervarchange("coordz", zcoord)
- mspervarchange("currentface", direction)
- end
- function coordlocate()
- print("Getting Coördinates with GPS")
- rednet.open("right")
- xcoord, ycoord, zcoord = gps.locate(2)
- if xcoord ~= nil then
- mspervarchange("coordx", xcoord)
- mspervarchange("coordy", ycoord)
- mspervarchange("coordz", zcoord)
- for n=1, 4 do
- local getdirtry = turtle.forward()
- if getdirtry == true then
- xcoord2, ycoord2, zcoord2 = gps.locate(2)
- xcoorddif = xcoord2-xcoord
- zcoorddif = zcoord2-zcoord
- if xcoorddif == 1 then direction = 3
- elseif xcoorddif == -1 then direction = 1
- elseif zcoorddif == 1 then direction = 0
- elseif zcoord == -1 then direction = 2
- end
- mspervarchange("coordx", xcoord2)
- mspervarchange("coordy", ycoord2)
- mspervarchange("coordz", zcoord2)
- mspervarchange("currentface", direction)
- break
- end
- turtle.turnRight()
- end
- else
- print("Couldn't find coördinates")
- print("Please enter them manually")
- sleep(1)
- os.pullEvent()
- coordmanual()
- end
- end
- function showwaypointlist()
- term.setCursorPos(1,6)
- write(mspervarread("coordx")..","..mspervarread("coordy")..","..mspervarread("coordz"))
- term.setCursorPos(1,7)
- for n=1, #waypoint+1 do
- print(waypoint[n])
- end
- end
- function getnextwaypoint()
- term.setCursorPos(1,3)
- term.clearLine()
- term.setCursorPos(1,4)
- term.clearLine()
- term.setCursorPos(1,5)
- term.clearLine()
- term.setCursorPos(1,3)
- print('Give the next waypoint in x,y,z format (ex: -43,78,62) or type "done"')
- write("Waypoint Coördinate: ")
- newwaypoint = read()
- if newwaypoint ~= "done" then
- wpn = wpn+1
- waypoint[wpn] = newwaypoint
- end
- end
- function movetowaypoints()
- file = io.open("/movementsystem/waypoints.txt", "w")
- for n=1, #waypoint do
- file:write(waypoint[n].."\n")
- end
- file:close()
- end
- printui()
- coordchoicequestion()
- printui()
- if coordchoice == "manual" then coordmanual() end
- if coordchoice == "locate" then coordlocate() end
- printui()
- waypoint = {}
- wpn = 0
- while true do
- getnextwaypoint()
- if newwaypoint == "done" then break end
- showwaypointlist()
- end
- movetowaypoints()
- term.clear()
- printui()
- shell.run("/movementsystem/msfollowwaypoints")
- ]])
- file:close()
- fs.makeDir("movementsystem")
- fs.makeDir("movementsystem/pervar")
- file = io.open("movementsystem/mspathfinding", "w")
- file:write([[
- tArgs = {...}
- ngtx = tArgs[1]
- ngty = tArgs[2]
- ngtz = tArgs[3]
- facenumbertostring = {"south","west","north","east"}
- function createvars()
- mspervarcreate("coordx") -- +:east -: west
- mspervarcreate("coordy") -- +:up -: down
- mspervarcreate("coordz") -- +:south -:north
- mspervarcreate("currentface")
- mspervarcreate("gotox")
- mspervarcreate("gotoy")
- mspervarcreate("gotoz")
- mspervarchange("coordx", 0)
- mspervarchange("coordy", 0)
- mspervarchange("coordz", 0)
- mspervarchange("currentface", 0) -- 0=south,1=west,2=north,3=east
- end
- function showcoords()
- print("The current coordinates are: ("..mspervarread("coordx")..","..mspervarread("coordy")..","..mspervarread("coordz")..") and it faces to the "..facenumbertostring[mspervarread("currentface")+1]..".")
- end
- function avoidsmallobstaclehorizontal()
- stntbl = ("print(\"rawr\")")
- movedirnum = tonumber(mspervarread("currentface"))
- c = moveup()
- if c == "blocked"
- then
- c = movedown()
- if c == "blocked"
- then
- if movedirnum == 3 then c = movesouth()
- elseif movedirnum == 2 then c = moveeast()
- elseif movedirnum == 1 then c = movenorth()
- elseif movedirnum == 0 then c = movewest()
- end
- if c == "blocked"
- then
- if movedirnum == 0 then c = moveeast()
- elseif movedirnum == 1 then c = movesouth()
- elseif movedirnum == 2 then c = movewest()
- elseif movedirnum == 3 then c = movenorth()
- end
- end
- end
- end
- end
- function avoidsmallobstacleup()
- passedobstacle = false
- c = movenorth()
- if c ~= "blocked"
- then
- d = moveup()
- if d ~= "blocked"
- then
- passedobstacle = true
- else
- movesouth()
- end
- end
- if passedobstacle ~= true
- then
- c = movesouth()
- if c ~= "blocked"
- then
- d = moveup()
- if d ~= "blocked"
- then
- passedobstacle = true
- else
- movenorth()
- end
- end
- end
- if passedobstacle ~= true
- then
- c = moveeast()
- if c ~= "blocked"
- then
- d = moveup()
- if d ~= "blocked"
- then
- passedobstacle = true
- else
- movewest()
- end
- end
- end
- if passedobstacle ~= true
- then
- c = movewest()
- if c ~= "blocked"
- then
- d = moveup()
- if d ~= "blocked"
- then
- passedobstacle = true
- else
- moveeast()
- end
- end
- end
- end
- function avoidsmallobstacledown()
- passedobstacle = false
- c = movenorth()
- if c ~= "blocked"
- then
- d = movedown()
- if d ~= "blocked"
- then
- passedobstacle = true
- else
- movesouth()
- end
- end
- if passedobstacle ~= true
- then
- c = moveeast()
- if c ~= "blocked"
- then
- d = movedown()
- if d ~= "blocked"
- then
- passedobstacle = true
- else
- movewest()
- end
- end
- end
- if passedobstacle ~= true
- then
- c = movewest()
- if c ~= "blocked"
- then
- d = movedown()
- if d ~= "blocked"
- then
- passedobstacle = true
- else
- moveeast()
- end
- end
- end
- if passedobstacle ~= true
- then
- c = movesouth()
- if c ~= "blocked"
- then
- d = movedown()
- if d ~= "blocked"
- then
- passedobstacle = true
- else
- movenorth()
- end
- end
- end
- end
- function settargetcoords(gtx,gty,gtz)
- mspervarchange("gotox", gtx)
- mspervarchange("gotoy", gty)
- mspervarchange("gotoz", gtz)
- end
- function moveclosertotargetx()
- currentx = tonumber(mspervarread("coordx"))
- targetx = tonumber(mspervarread("gotox"))
- if currentx == targetx
- then
- return "reached x"
- elseif currentx > targetx
- then
- b = movewest()
- elseif currentx < targetx
- then
- b = moveeast()
- end
- end
- function moveclosertotargetz()
- currentz = tonumber(mspervarread("coordz"))
- targetz = tonumber(mspervarread("gotoz"))
- if currentz == targetz
- then
- return "reached z"
- elseif currentz > targetz
- then
- b = movenorth()
- elseif currentz < targetz
- then
- b = movesouth()
- end
- end
- function moveclosertotargety()
- currenty = tonumber(mspervarread("coordy"))
- targety = tonumber(mspervarread("gotoy"))
- if currenty == targety
- then
- return "reached y"
- elseif currenty > targety
- then
- b = movedown()
- if b ~= nil then b = (b.."down") end
- elseif currenty < targety
- then
- b = moveup()
- if b ~= nil then b = (b.."up") end
- end
- end
- function movetotarget()
- repeat
- repeat
- a = moveclosertotargetx()
- if b == "blocked" then avoidsmallobstaclehorizontal() end
- until a == "reached x"
- repeat
- a = moveclosertotargetz()
- if b == "blocked" then avoidsmallobstaclehorizontal() end
- until a == "reached z"
- repeat
- a = moveclosertotargety()
- if b == "blockedup" then avoidsmallobstacleup() end
- if b == "blockeddown" then avoidsmallobstacledown() end
- until a == "reached y"
- until mspervarread("coordx") == mspervarread("gotox") and mspervarread("coordz") == mspervarread("gotoz") and mspervarread("coordy") == mspervarread("gotoy")
- end
- settargetcoords(ngtx,ngty,ngtz)
- movetotarget()
- ]])
- file:close()
- file = io.open("movementsystem/msfollowwaypoints", "w")
- file:write([[
- function readwaypointlist()
- waypointlist = {}
- file = io.open("movementsystem/waypoints.txt", "r")
- n = 0
- while true do
- n = n + 1
- nextwaypointline = file:read()
- if nextwaypointline == nil then break end
- waypointlist[n] = nextwaypointline
- end
- end
- function formatwaypoint(waypointstring)
- wpsx = nil
- wpsy = nil
- wpsz = nil
- wpsh2 = waypointstring
- for i=1, 3 do
- wpsh = wpsh2
- n = 0
- while true do
- n = n + 1
- fwpsc = string.sub(wpsh,1,1)
- wpsh = string.sub(wpsh,2)
- if fwpsc == "" then break end
- if fwpsc == "," then break end
- end
- if i == 1 then wpsx = tonumber(string.sub(wpsh2,1,n-1)) wpsh2 = string.sub(wpsh2,n+1)
- elseif i == 2 then wpsy = tonumber(string.sub(wpsh2,1,n-1)) wpsh2 = string.sub(wpsh2,n+1)
- elseif i == 3 then wpsz = tonumber(string.sub(wpsh2,1,n-1))
- end
- end
- return wpsx, wpsy, wpsz
- end
- function formatwaypointlist()
- waypointx = {}
- waypointy = {}
- waypointz = {}
- for j=1, #waypointlist do
- waypointx[j], waypointy[j], waypointz[j] = formatwaypoint(waypointlist[j])
- end
- end
- function followwaypointlist()
- for n=1, #waypointlist do
- print(waypointx[n].." "..waypointy[n].." "..waypointz[n])
- shell.run("/movementsystem/mspathfinding", waypointx[n], waypointy[n], waypointz[n])
- end
- end
- readwaypointlist()
- formatwaypointlist()
- followwaypointlist()
- ]])
- file:close()
- file = io.open("movementsystem/waypoints.txt", "w")
- file:close()
- file = io.open("movementsystem/pervar/coordx.txt", "w")
- file:close()
- file = io.open("movementsystem/pervar/coordy.txt", "w")
- file:close()
- file = io.open("movementsystem/pervar/coordz.txt", "w")
- file:close()
- file = io.open("movementsystem/pervar/currentface.txt", "w")
- file:close()
- file = io.open("movementsystem/pervar/gotox.txt", "w")
- file:close()
- file = io.open("movementsystem/pervar/gotoy.txt", "w")
- file:close()
- file = io.open("movementsystem/pervar/gotoz.txt", "w")
- file:close()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement