Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- TAMS Turtle Advanced Movment Script
- writen in Lua by Big Shiny Toys
- I realease this Open source and public domain
- notes:
- this is a turtle mining and legistics system
- designed to allow simpe controll of mutiple turtles
- -- stuff to add / repair / ect..
- 1 ) send function to net ALPHA
- 1.1 ) add phone book of addresseses update
- when reciving new messsage from user.
- 2 ) goto GPS in movment system
- 3 ) check multi tasking
- 4 ) Controller side system / authentication
- 5 ) turtle task controll for ^
- 6 ) request for refuel
- 7 ) call for help
- 8 ) allow access to files remotle
- 9 ) automatic update distrobution
- 10 ) checking system in turtle tracking system
- 11 ) mining script with send on complet -- ask for fuel and ask for item reteval
- 12 ) parth finding using pipes and roads
- 13 ) turtle taks build road send on completion
- 14 ) turtle task mine 1,1 12,12 will take from Z 70 down to bead rock
- 15 ) save position
- ]]--
- -- test --
- term.clear()
- term.setCursorPos(1,1)
- if not turtle then
- print("Not a turtle")
- return
- end
- -- Indexes --
- local tFacesRAW = {"+Y","+X","-Y","-X"}
- local tFaces = {"north","south","west","east"}
- -- varibles --
- local bRunning = true
- local machineID = os.getComputerID()
- local modemOn
- local turX,turY,turZ,turF = nil,nil,nil,1
- local task = {} -- so task function can be called from all programs
- local home = nil
- local tThreads = {}
- -- Functions --
- local netWork = {}
- netWork.open = function()
- local listOfSides = rs.getSides()
- for i = 1,6 do
- if peripheral.isPresent(listOfSides[i]) and peripheral.getType(listOfSides[i]) == "modem" then
- rednet.open(listOfSides[i])
- return listOfSides[i]
- end
- end
- return false
- end
- local move = {}
- move.up = function()
- if turtle.up() then
- turZ = turZ + 1
- return true
- else
- return false
- end
- end
- move.down = function()
- if turtle.down() then
- turZ = turZ - 1
- return true
- else
- return false
- end
- end
- move.forward = function()
- if turtle.forward() then
- if turF == 1 then
- turY = turY + 1
- elseif turF == 2 then
- turX = turX + 1
- elseif turF == 3 then
- turY = turY - 1
- elseif turF == 4 then
- turX = turX - 1
- end
- return true
- else
- return false
- end
- end
- move.back = function()
- if turtle.back() then
- if turF == 1 then
- turY = turY - 1
- elseif turF == 2 then
- turX = turX - 1
- elseif turF == 3 then
- turY = turY + 1
- elseif turF == 4 then
- turX = turX + 1
- end
- return true
- else
- return false
- end
- end
- move.currentPos = function()
- return turX,turY,turZ,turF
- end
- local turn = {}
- turn.left = function()
- if turtle.turnLeft() then
- turF = turF - 1
- if turF < 1 then
- turF = 4
- end
- return true
- else
- return false
- end
- end
- turn.right = function()
- if turtle.turnRight() then
- turF = turF + 1
- if turF > 4 then
- turF = 1
- end
- return true
- else
- return false
- end
- end
- turn.face = function(direction)
- if direction > 4 or direction < 1 then return false end
- if direction == turF then return true end
- if direction == turF - 2 or direction == turF + 2 then
- turn.right()
- turn.right()
- else
- local temp = direction-1
- for p = 1,4 do
- temp = temp+1
- if temp > 4 then
- temp = 1
- end
- if turF == temp then
- if p == 1 or p == 4 then
- turn.right()
- elseif p == 2 or p == 3 then
- turn.left()
- end
- end
- end
- end
- end
- move.gotoPos = function(gotoX,gotoY,gotoZ,gotoF) -- turX,turY,turZ,turF
- local rep = 0
- local happy
- if gotoF == nil then
- local happy = true
- else
- happy = function()
- return turF ~= gotoF
- end
- end
- while turX ~= gotoX or turY ~= gotoY or turZ ~= gotoZ or happy do
- if turX > gotoX then
- local dif = turX - gotoX
- turn.face(4)
- for i = 1,dif do
- move.forward()
- end
- end
- if turX < gotoX then
- local dif = gotoX - turX
- turn.face(2)
- for i = 1,dif do
- move.forward()
- end
- end
- if turY > gotoY then
- local dif = turY - gotoY
- turn.face(3)
- for i = 1,dif do
- move.forward()
- end
- end
- if turY < gotoY then
- local dif = gotoY - turY
- turn.face(1)
- for i = 1,dif do
- move.forward()
- end
- end
- if turZ > gotoZ then
- local dif = turZ - gotoZ
- for i = 1,dif do
- move.down()
- end
- end
- if turZ < gotoZ then
- local dif = gotoZ - turZ
- for i = 1,dif do
- move.up()
- end
- end
- if gotoF then
- turn.face(gotoF)
- end
- if rep > 5 then
- return false
- end
- rep = rep + 1
- end
- end
- local function CommandDecoder(Input)
- local tWords = {}
- for match in string.gmatch(Input, "[^ \t]+") do
- table.insert( tWords, match )
- end
- if tWords[1] == "run" then
- local program = tostring(tWords[2])
- table.remove(tWords,1)
- table.remove(tWords,1)
- if task.run(program,unpack(tWords)) then
- print("Running "..tostring(program))
- else
- print("program not started")
- end
- elseif tWords[1] == "kill" then
- if task.kill(tostring(tWords[2])) then
- print("Killed "..tostring(tWords[2]))
- else
- print("program not killed")
- end
- elseif tWords[1] == "pause" then
- if task.pause(tostring(tWords[2])) then
- print("Paused "..tostring(tWords[2]))
- else
- print("program not paused")
- end
- elseif tWords[1] == "resume" then
- if task.resume(tostring(tWords[2])) then
- print("Resuming "..tostring(tWords[2]))
- else
- print("program not Resumed")
- end
- elseif tWords[1] == "tasks" then
- for i = 1,#tThreads do
- --term.setCursorPos(1,sizY)
- print(tThreads[i]["name"].." req "..tostring(tThreads[i]["req"]).." "..tostring(tThreads[i]["status"]))
- end
- elseif tWords[1] == "goto" then
- if tWords[2] and tWords[2] == "home" then
- if home then
- print("going home")
- move.gotoPos(home.x,home.y,home.z,home.f)
- else
- print("home not set")
- end
- else
- if #tWords == 4 then
- --term.setCursorPos(1,sizY)
- print("going to X-"..tWords[2].." Y-"..tWords[3].." Z-"..tWords[4])
- move.gotoPos(tonumber(tWords[2]),tonumber(tWords[3]),tonumber(tWords[4]))
- elseif #tWords == 5 then
- --term.setCursorPos(1,sizY)
- print("going to X-"..tWords[2].." Y-"..tWords[3].." Z-"..tWords[4].." F-"..tWords[5])
- move.gotoPos(tonumber(tWords[2]),tonumber(tWords[3]),tonumber(tWords[4]),tonumber(tWords[5]))
- end
- end
- elseif tWords[1] == "exit" then
- --term.setCursorPos(1,sizY)
- print("exiting")
- os.pullEvent("exit")
- --[[elseif tWords[1] == "help" then
- print("Alavible Function's")
- for i,v in ipairs(tPrograms) do
- print(tostring(i).." "..tostring(v))
- end]]--
- elseif tWords[1] == "setHome" then -- needs work
- home = {}
- home.x = tonumber(tWords[2])
- home.y = tonumber(tWords[3])
- home.z = tonumber(tWords[4])
- home.f = tonumber(tWords[5])
- print("home set")
- print("Home is X : "..home.x.." Y : "..home.y.." Z : "..home.z.." F : "..home.f)
- elseif tWords[1] == "clearHome" then
- if home then
- home = nil
- print("Home cleared")
- else
- print("home is already nil")
- end
- elseif tWords[1] == "home" then
- if home then
- print("Home is X : "..home.x.." Y : "..home.y.." Z : "..home.z.." F : "..home.f)
- else
- print("Home not set")
- end
- elseif tWords[1] == "fuel" then
- print(turtle.getFuelLevel())
- end
- end
- -- sub programs --
- -- netALPHA --
- local tPrograms = {}
- tPrograms["netALPHA"] = function()
- netALPHA = true -- so programs can test for NetALPHA
- --local
- local ver = 0.2
- local Status = "unstable"
- local off = true -- debug Mode is false no print is true
- local MachineID = os.getComputerID()
- local bSendMode = false
- local tNodes = {} -- stores all avalible routes
- local adressBook = {}
- local maxHop = 5
- -- functions
- local function sPrint(Input)
- if off then
- return
- end
- if type(Input) == "table" then
- print(tostring(Input).." "..#Input)
- elseif type(Input) == "string" then
- print(tostring(Input))
- elseif Input == nil then
- print("Nil Value fucked up here lol XD")
- elseif type(Input) == "number" then
- print(tostring(Input))
- elseif type(Input) == "function" then
- print(tostring(Input))
- elseif type(Input) == "thread" then
- print(tostring(Input))
- else
- print("Unknown Print request")
- end
- end
- local function DistanceMesure(set1X,set1Y,set1Z,set2X,set2Y,set2Z)
- local iMeters = math.sqrt((math.sqrt(((set1X - set2X)^2) + ((set1Y - set2Y)^2)))^2 + (set1Z - set2Z)^2)
- return iMeters
- end
- local function findRoute(DesID,DesX,DesY,DesZ) -- find clostest router to destination.
- local shortestM = nil
- local shortestID = nil
- for i = 1,#tNodes do
- local Distance = math.sqrt((math.sqrt(((DesX - tNodes[i][2])^2) + ((DesY - tNodes[i][3])^2)))^2 + (DesZ - tNodes[i][4])^2)
- if tNodes[i][1] == DesID then
- shortestM = Distance
- shortestID = tNodes[i][1]
- return shortestID , shortestM
- elseif shortestM == nil then
- shortestM = Distance
- shortestID = tNodes[i][1]
- elseif Distance < shortestM then
- shortestM = Distance
- shortestID = tNodes[i][1]
- end
- end
- if shortestM then
- return shortestID , shortestM
- else
- return false
- end
- end
- local function PacketDecode(sPacket)
- if string.sub(sPacket,1,8) == "netALPHA" then
- local sTemp = string.find(sPacket,"{")
- --sPrint(sTemp)
- local sTemp3 = string.sub(sPacket,9,sTemp-1)
- --sPrint(sTemp3)
- local sTemp2 = tonumber(sTemp3)
- --sPrint(sTemp2)
- local tOutput = textutils.unserialize(string.sub(sPacket,sTemp,sTemp2+8+#sTemp3))
- --sPrint(tOutput)
- local sOutput = string.sub(sPacket,sTemp2+9+#sTemp3,#sPacket)
- --sPrint(sOutput)
- if type(tOutput) == "table" and type(sOutput) == "string" then
- if sOutput == "" then
- return tOutput
- else
- return tOutput,sOutput
- end
- end
- end
- error()
- end
- local function printD(...)
- if off then
- return
- else
- print(...)
- end
- end
- local function addRoutBook(tAddress)
- for i = 1,#tNodes do
- if tNodes[i][1] == tAddress[1] then
- tNodes[i] = tAddress
- return
- end
- end
- table.insert(tNodes,tAddress)
- printD(type(tAddress))
- end
- local function PacketBuilder(tInput,sInput)
- if sInput == nil then
- sInput = ""
- end
- if type(tInput) == "table" and type(sInput) == "string" then
- local temp = textutils.serialize(tInput)
- printD(temp)
- local sOut = "netALPHA"..tostring(#temp)..temp..sInput
- return sOut
- else
- return false
- end
- end
- local function sendPing()
- rednet.broadcast(PacketBuilder({"INS","PING",MachineID,turX,turY,turZ}))
- end
- os.startTimer(120+math.random(1,30)) -- 2 mins +- 15 secs
- sendPing()
- -- main function
- local function netALPHACore()
- while true do
- local sEvent,isendID,sMessage,iDistance = coroutine.yield()
- if sEvent == "rednet_message" then
- local bStatus,tPacket,sPacket = pcall(PacketDecode,sMessage)
- if bStatus then
- if tPacket[1] == "PKT" and #tPacket == 10 then
- if tPacket[7] == MachineID then
- printD("Packet form me")
- printD("From ID: "..tPacket[3].." loc X: "..tPacket[4].." Y: "..tPacket[5].." Z: "..tPacket[6])
- printD("sent To: "..tPacket[7].." loc X: "..tPacket[8].." Y: "..tPacket[9].." Z: "..tPacket[10])
- os.queueEvent("netALPHA",tPacket[3],sPacket,DistanceMesure(tPacket[4],tPacket[5],tPacket[6],turX,turY,turZ))
- else
- printD("packet to forward")
- printD("From ID: "..tPacket[3].." loc X: "..tPacket[4].." Y: "..tPacket[5].." Z: "..tPacket[6])
- printD("sent To: "..tPacket[7].." loc X: "..tPacket[8].." Y: "..tPacket[9].." Z: "..tPacket[10])
- if tPacket[2] <= maxHop then
- local forwardID,forwardM = findRoute(tPacket[7],tPacket[8],tPacket[9],tPacket[10])
- sPrint("forwded to")
- sPrint(forwardID)
- sPrint(forwardM)
- tPacket[2] = tPacket[2]+1 -- incrementing hop
- rednet.send(forwardID,PacketBuilder(tPacket,sPacket))
- else
- printD("MaxHop Ignore Packet")
- end
- end
- elseif tPacket[1] == "INS" then
- printD("instruction "..tPacket[2])
- if tPacket[2] == "PING" and #tPacket == 6 then
- addRoutBook({tPacket[3],tPacket[4],tPacket[5],tPacket[6]})
- rednet.send(isendID,PacketBuilder({"INS","PONG",MachineID,turX,turY,turZ}))
- elseif tPacket[2] == "PONG" and #tPacket == 6 then
- addRoutBook({tPacket[3],tPacket[4],tPacket[5],tPacket[6]})
- elseif tPacket[2] == "CHK-" and #tPacket == 10 then
- elseif tPacket[2] == "RPL-" and #tPacket == 10 then
- end
- end
- end
- elseif sEvent == "timer" then
- os.startTimer(120+math.random(1,30))
- tNodes = {}
- sendPing()
- end
- if #tNodes >= 1 then
- for i = 1,#tNodes do
- printD("T "..i.." of "..#tNodes.." ID "..tNodes[i][1].." X"..tNodes[i][2].." Y"..tNodes[i][3].." Z"..tNodes[i][4])
- end
- end
- end
- end
- netALPHACore()
- end
- tPrograms["manual"] = function()
- while true do
- local e,e1,e2,e3 = os.pullEvent()
- if e == "netALPHA" then
- print(tostring(e1).." "..tostring(e2).." "..tostring(e3))
- elseif e == "char" then
- if e1 == "w" then
- move.forward()
- elseif e1 == "s" then
- move.back()
- elseif e1 == "a" then
- turn.left()
- elseif e1 == "d" then
- turn.right()
- elseif e1 == "q" then
- move.up()
- elseif e1 == "e" then
- move.down()
- end
- end
- end
- end
- tPrograms["console"] = function()
- local tCommandHistory = {}
- while true do
- local sizX,sizY = term.getSize()
- term.setCursorPos(1,sizY)
- term.clearLine()
- write("> ")
- local sLine = read( nil, tCommandHistory )
- CommandDecoder(sLine)
- table.insert( tCommandHistory, sLine )
- end
- end
- tPrograms["mine"] = function(...)
- local tArgs = {...}
- for i = 1,#tArgs do
- tArgs[i] = tonumber(tArgs[i])
- end
- local startX,startY,startZ,face,lenX,lenY,depZ = unpack(tArgs)
- -- local startX,startY,startZ,face,lenX,lenY,depZ = 15,-65,72,3,10,6,18
- move.gotoPos(startX,startY,startZ,face)
- local neg = false
- if math.fmod(lenY,2) ~= 0 then
- neg = true
- end
- for p = 1,depZ do
- for a = 1,lenY do
- for i = 1,lenX-1 do
- turtle.dig()
- move.forward()
- end
- if a ~= lenY then
- if math.fmod(p,2) == 0 or neg then
- if math.fmod(a,2) == 0 then
- turn.left()
- turtle.dig()
- move.forward()
- turn.left()
- else
- turn.right()
- turtle.dig()
- move.forward()
- turn.right()
- end
- else
- if math.fmod(a,2) ~= 0 then
- turn.left()
- turtle.dig()
- move.forward()
- turn.left()
- else
- turn.right()
- turtle.dig()
- move.forward()
- turn.right()
- end
- end
- else
- if math.fmod(a,2) == 0 then
- turn.left()
- turn.left()
- else
- turn.right()
- turn.right()
- end
- end
- end
- if p ~= depZ then
- turtle.digDown()
- move.down()
- end
- end
- if home then
- move.gotoPos(home.x,home.y,home.z,home.f)
- end
- end
- tPrograms["netCON"] = function()
- while true do
- local event,e1,e2,e3,e4,e5 = os.pullEvent("netALPHA")
- print("RMT ins "..tostring(e1).." dst "..tostring(e3))
- print(tostring(e2))
- CommandDecoder(e2)
- end
- end
- -- task mangment-- local task = {}
- -- needs work
- task.run = function(program,...)
- local tArgs = {...}
- if tPrograms[program] then
- local nNunber = #tThreads+1
- tThreads[nNunber] = {}
- tThreads[nNunber]["name"] = program
- tThreads[nNunber]["thread"] = coroutine.create(tPrograms[program])
- tThreads[nNunber]["req"] = nil
- tThreads[nNunber]["status"] = "run"
- if #tArgs == 0 then
- tThreads[nNunber]["next"] = nil
- else
- tThreads[nNunber]["next"] = tArgs
- end
- return true
- end
- return false
- end
- task.pause = function(program)
- for i = 1,#tThreads do
- if tThreads[i]["name"] == program then
- tThreads[i]["status"] = "pause"
- return true
- end
- end
- return false
- end
- task.resume = function(program)
- for i = 1,#tThreads do
- if tThreads[i]["name"] == program then
- tThreads[i]["status"] = "run"
- return true
- end
- end
- return false
- end
- task.kill = function(program)
- for i = 1,#tThreads do
- if tThreads[i]["name"] == program then
- table.remove(tThreads,i)
- return true
- end
- end
- return false
- end
- -- boot process --
- print("Booting turtle NO: "..machineID)
- modemOn = netWork.open()
- if not modemOn then
- print("No WIFI Modem\nPress any key to exit")
- os.pullEvent("key")
- return
- else
- print("Opened wifi on "..modemOn.." side")
- end
- turX,turY,turZ = gps.locate(2,true) -- EDIT this line to > turX,turY,turZ = 0,0,0
- if not turX then
- print("GPS locate failed\nPress any key to exit")
- os.pullEvent("key")
- return
- end
- print("Starting Networking")
- task.run("console")
- task.run("netALPHA")
- print("netPLPHA ID :".." "..machineID.." "..turX.." "..turY..""..turZ)
- print("boot compleet")
- term.clear()
- term.setCursorPos(1,1)
- -- main loop --
- while bRunning do
- local tEvent = {os.pullEvent()}
- -- print(tEvent[1])
- local loop = 1
- while true do
- if loop == #tThreads + 1 then
- break
- end
- if coroutine.status(tThreads[loop]["thread"]) ~= "dead" then
- if tThreads[loop]["status"] == "run" and type(tThreads[loop]["next"]) == "table" then
- --print("MARKER two")
- test,tThreads[loop]["req"] = coroutine.resume(tThreads[loop]["thread"],unpack(tThreads[loop]["next"])) -- fix
- tThreads[loop]["next"] = nil
- end
- if tThreads[loop]["req"] == "exit" then
- bRunning = false
- end
- if ( tThreads[loop]["req"] == nil or tEvent[1] == tThreads[loop]["req"] ) and tThreads[loop]["status"] == "run" then
- test,tThreads[loop]["req"] = coroutine.resume(tThreads[loop]["thread"],unpack(tEvent)) -- fix
- elseif tThreads[loop]["status"] == "pause" and tEvent[1] == tThreads[loop]["req"] and tThreads[loop]["next"] == nil then
- --print("here marker")
- tThreads[loop]["next"] = tEvent
- end
- if tThreads[loop]["req"] == "exit" then
- bRunning = false
- end
- loop = loop + 1
- else
- print("Thread Crash")
- print(tostring(tThreads[loop]["name"]))
- print(tostring(tThreads[loop]["req"]))
- table.remove(tThreads,loop)
- end
- end
- local oldX,oldY = term.getCursorPos()
- term.setCursorPos(1,1)
- write(machineID.." threds: "..#tThreads.." "..tostring(tThreads[1]["req"]))
- term.setCursorPos(oldX,oldY)
- end
- term.clear()
- term.setCursorPos(1,1)
- print("TAMS ended")
- -- test --
- --[[
- turn.face(1)
- turn.face(2)
- turn.face(3)
- turn.face(4)
- turn.face(1)
- turn.face(2)
- turn.face(3)
- turn.face(4)
- print("pasued")
- os.pullEvent("key")
- for i = 1,5 do
- move.gotoPos(0,0,74)
- move.gotoPos(-6,0,70)
- move.gotoPos(-6,-13,68)
- move.gotoPos(2,-4,80)
- end
- print("pasued")
- os.pullEvent("key")
- ]]--
- -- test script --
- --[[
- local function positionPrint()
- print("X "..turX.." Y "..turY.." Z "..turZ.." heading "..tFaces[turF])
- end
- local function sPrint(input)
- print(type(input).." "..tostring(input))
- end
- positionPrint()
- move.forward()
- positionPrint()
- turn.left()
- positionPrint()
- move.forward()
- positionPrint()
- move.back()
- positionPrint()
- turn.right()
- positionPrint()
- move.back()
- positionPrint()
- move.up()
- positionPrint()
- move.down()
- positionPrint()
- ]]--
- --[[
- FAILED DIGGER script
- local startX,startY,startZ,face,lenX,lenY,depZ = 15,-29,72,3,10,6,20
- move.gotoPos(startX,startY,startZ,face)
- for p = 1,depZ do
- for a = 1,lenY do
- for i = 1,lenX do
- turtle.dig()
- move.forward()
- end
- if math.fmod(p,2) == 0 then
- if math.fmod(a,2) == 0 then
- turn.left()
- turtle.dig()
- move.forward()
- turn.left()
- else
- turn.right()
- turtle.dig()
- move.forward()
- turn.right()
- end
- else
- if math.fmod(a,2) ~= 0 then
- turn.left()
- turtle.dig()
- move.forward()
- turn.left()
- else
- turn.right()
- turtle.dig()
- move.forward()
- turn.right()
- end
- end
- end
- turtle.digDown()
- move.down()
- end
- move.gotoPos(0,0,70)
- ]]--
Advertisement
Add Comment
Please, Sign In to add comment