Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("navigation")
- local tnav = navigation.newNav()
- local id = os.getComputerID()
- local serverID = nil
- local home = {}
- local group = {}
- rednet.open("right")
- function save(name, data)
- local file = fs.open(name, "w")
- file.write(textutils.serialize(data))
- file.close()
- end
- function load(name)
- local file = fs.open(name, "r")
- local data = file.readAll()
- file.close()
- return textutils.unserialize(data)
- end
- function copyTable(inTable)
- local outTable = {}
- for key, value in pairs(inTable) do
- outTable[key] = value
- end
- return outTable
- end
- function createCoordinateTable(a, b)
- local moveTable = {}
- local vector = {b[1] - a[1], b[2] - a[2], b[3] - a[3]}
- local unitVector = {}
- for i = 1,3 do
- unitVector[i] = vector[i]/math.abs(vector[i])
- end
- -- direction of length and width
- local mLen = nil
- local mWid = nil
- if math.abs(vector[1]) > math.abs(vector[3]) then
- mLen = 1
- mWid = 3
- else
- mLen = 3
- mWid = 1
- end
- local curDir = 1 -- direction relative to vector to add next coordinate
- local curWDir = 1 -- number to keep track of direction on height layers
- local lastCoord = copyTable(a)
- local nextCoord = {}
- for height = a[2], b[2], unitVector[2] do -- loop through height layers
- for width = a[mWid], b[mWid], unitVector[mWid] do -- loop through width lines
- local realWidth = nil
- if curWDir == 1 then realWidth = width
- else realWidth = a[mWid] + b[mWid] - width end
- nextCoord[mWid] = realWidth
- nextCoord[mLen] = lastCoord[mLen]
- nextCoord[2] = height
- table.insert(moveTable, copyTable(nextCoord))
- lastCoord = copyTable(nextCoord)
- nextCoord[mWid] = realWidth
- nextCoord[mLen] = lastCoord[mLen] + curDir * vector[mLen]
- nextCoord[2] = height
- table.insert(moveTable, copyTable(nextCoord))
- lastCoord = copyTable(nextCoord)
- curDir = curDir * -1
- end
- curWDir = curWDir * -1
- end
- table.insert(moveTable, home)
- return moveTable
- end
- function fullInv()
- group = load("invgroups")
- local curType = 0
- local curSum = 64
- for i = 1, 16 do
- if curType == group[i] then
- curSum = curSum + turtle.getItemSpace(i)
- else
- if curSum < 5 then
- return true
- end
- curType = group[i]
- curSum = turtle.getItemSpace(i)
- end
- end
- return false
- end
- function emptyInventory()
- tnav.moveTo(home)
- for i = 1, 16 do
- turtle.select(i)
- if turtle.getItemCount(i) > 1 then
- turtle.dropDown(turtle.getItemCount(i)-1)
- end
- end
- turtle.select(1)
- end
- function needRefuel()
- if turtle.getFuelLevel() < 5000 then
- return true
- end
- return false
- end
- function refuel()
- for i = 1, 3 do
- turtle.select(i)
- turtle.refuel(turtle.getItemCount(i)-1)
- end
- turtle.select(1)
- end
- function mineArea(a, b)
- local areaData = {}
- local progress = 0
- local moveTable = {}
- local tableSize = 0
- if a == nil then -- resume
- areaData = load("areadata")
- progress = areaData.progress
- moveTable = areaData.moveTable
- else -- start new
- moveTable = createCoordinateTable(a, b)
- end
- tableSize = table.getn(moveTable)
- while progress < tableSize do
- areaData.progress = progress
- areaData.moveTable = moveTable
- save("areadata", areaData)
- tnav.moveTo(moveTable[progress+1])
- progress = progress + 1
- if progress % 50 == 0 then
- if needRefuel() then
- refuel()
- end
- end
- if progress % 10 == 0 then
- if fullInv() then
- emptyInventory()
- end
- end
- end
- emptyInventory()
- end
- function setHome()
- home = tnav.getPos()
- save("home", home)
- end
- if fs.exists("home") then
- home = load("home")
- end
- if fs.exists("serverID") then
- serverID = load("serverID")
- else
- write("Type in server ID: ")
- serverID = tonumber(read())
- save("serverID", serverID)
- end
- if fs.exists("invgroups") then
- groups = load("invgroups")
- else
- groups = {1,1,1,2,3,3,3,3,4,4,5,5,6,7,8,9}
- save("invgroups", groups)
- end
- -- connect to server
- rednet.send(serverID, "me turtle, you server")
- print("sent handshake message to server")
- if fs.exists("areadata") then
- mineArea()
- end
- while true do
- id, message = rednet.receive()
- print("received from "..tostring(id)..": "..message)
- local command = {}
- if id == serverID then
- for word in message:gmatch("%S+") do
- table.insert(command, word)
- end
- if command[1] == "move" then
- if command[2] == "home" then -- go home
- tnav.moveTo(home)
- elseif tonumber(command[2]) ~= nil then -- go to coordinate
- local x = tonumber(command[2])
- local y = tonumber(command[3])
- local z = tonumber(command[4])
- if x ~= nil and y ~= nil and z ~= nil then
- tnav.moveTo(x, y, z)
- end
- end
- elseif command[1] == "mine" then
- local a = {tonumber(command[2]), tonumber(command[3]), tonumber(command[4])}
- local b = {tonumber(command[5]), tonumber(command[6]), tonumber(command[7])}
- if a[1] ~= nil and a[2] ~= nil and a[3] ~= nil and
- b[1] ~= nil and b[2] ~= nil and b[3] ~= nil then
- mineArea(a, b)
- end
- elseif command[1] == "sethome" then
- setHome()
- elseif command[1] == "resume" then
- tnav.resumeMove()
- mineArea()
- elseif command[1] == "setposition" then
- local p = {}
- for i = 2,5 do
- table.insert(p, tonumber(command[i]))
- end
- tnav.setPos(p[1], p[2], p[3], p[4])
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement