Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local fern_data = require("fern-data")
- local CHUNK_LIST = {}
- local TURTLE
- local HOST
- local REG_X,REG_Z,REGION
- local data_send_queue
- if fs.exists("latest.log") then fs.delete("latest.log") end
- local log_file = fs.open("latest.log","w+")
- local color = colors.yellow
- local function logAction(...)
- local str = ""
- for k,v in ipairs({...}) do
- if type(v) == "table" then
- temp = textutils.serialiseJSON(v)
- if #temp <= 100 then
- v = temp
- end
- end
- str = str .. " " .. tostring(v)
- end
- if color == colors.yellow then color = colors.orange else color = colors.yellow end
- term.setTextColor(color)
- print(str)
- pcall(function() log_file.writeLine(os.date("[%H:%M:%S]:") .. str) end)
- log_file.flush()
- if fs.getSize("latest.log") > 100000 then --Log max size reset
- log_file.close()
- fs.delete("latest.log")
- log_file = fs.open("latest.log","w+")
- end
- end
- --Rot: 0=N,1=E,2=S,3=W
- local function wrapper()
- local function Turtle(x,y,z,rot)
- local turt = {x=x,y=y,z=z,rot=rot}
- function turt:equipItem(item,side)
- if type(item) ~= "string" then error("Must be an item") end
- logAction("equip",item,side)
- for i=1,16 do
- if (turtle.getItemDetail(i) or {}).name == item then
- turtle.select(i)
- if side == "left" then return turtle.equipLeft() end
- return turtle.equipRight()
- end
- end
- return false
- end
- function turt:updatePosition()
- self:equipItem("computercraft:wireless_modem_advanced")
- peripheral.find("modem",rednet.open)
- local x,y,z = gps.locate()
- if not x then error("Can not find turtle") end
- self.x,self.y,self.z = x,y,z
- if turtle.forward() then
- local x2,y2,z2 = gps.locate()
- if not x2 then error("Can not find turtle") end
- if z2 < z then self.rot = 0
- elseif z2 > z then self.rot = 2
- elseif x2 > x then self.rot = 1
- elseif x2 < x then self.rot = 3
- else error("Could not calculate rotation") end
- assert(turtle.back())
- elseif turtle.back() then
- local x2,y2,z2 = gps.locate()
- if not x2 then error("Can not find turtle") end
- if z2 < z then self.rot = 2
- elseif z2 > z then self.rot = 0
- elseif x2 > x then self.rot = 3
- elseif x2 < x then self.rot = 1
- else error("Could not calculate rotation") end
- assert(turtle.forward())
- else
- error("Please make sure that turtle has space to move to perform the rotation calculation")
- end
- logAction("update position",x,y,z,self.rot)
- end
- function turt:rotate(dir)
- logAction("rotate",self.rot,dir)
- if tonumber(dir) then
- local amt = (math.floor(tonumber(dir)) - self.rot)%4
- if amt == 3 then
- assert(turtle.turnLeft())
- else
- for i=1,amt do
- assert(turtle.turnRight())
- end
- end
- return true
- elseif type(dir) == "string" then
- local dirs = {n=0,e=1,s=2,w=3,north=0,east=1,south=2,west=3}
- local amt = dirs[dir:lower()]
- if not amt then return false end
- return self:rotate(amt)
- else
- return false
- end
- end
- function turt:refuel()
- logAction("refuel")
- if turtle.getFuelLimit() - turtle.getFuelLevel() <= 3000 then return turtle.getFuelLevel() end
- for i=1,16 do
- turtle.select(i)
- if turtle.refuel(0) then
- local oldLvl = turtle.getFuelLevel()
- turtle.refuel(1)
- local amt = turtle.getFuelLevel() - oldLvl
- local itemsToBurn = math.floor((turtle.getFuelLimit() - turtle.getFuelLevel())/amt)
- turtle.refuel(math.min(turtle.getItemCount(i),itemsToBurn))
- end
- if turtle.getFuelLimit() - turtle.getFuelLevel() <= 3000 then return turtle.getFuelLevel() end
- end
- return turtle.getFuelLevel()
- end
- turt:refuel()
- if not tonumber(x) or not tonumber(y) or not tonumber(z) or not tonumber(rot) then turt:updatePosition() end
- function turt:moveToPos(x,y,z,verticalLast)
- logAction("move",x,y or 310,z,self.x,self.y,self.z)
- local index = -1
- if not tonumber(x) or not tonumber(z) then error("Must provide coordinates to travel to") end
- x,y,z = math.floor(tonumber(x)), math.floor(tonumber(y) or 310), math.floor(tonumber(z))
- if not verticalLast then
- if math.abs(self.y - y) > self:refuel() then error("Lack sufficient fuel") end
- while self.y < y do
- assert(turtle.up())
- self.y = self.y + 1
- index = index + 1
- if index%100 == 0 then
- logAction("pos",self.x,self.y,self.z,self.rot)
- if fs.exists("pos.txt") then fs.delete("pos.txt") end
- local file = fs.open("pos.txt","w+")
- file.write(self.x .. " " .. self.y .. ",, " .. self.z .. ", "..self.rot)
- file.close()
- index = 0
- end
- end
- while self.y > y do
- assert(turtle.down())
- self.y = self.y - 1
- index = index + 1
- if index%100 == 0 then
- logAction("pos",self.x,self.y,self.z,self.rot)
- if fs.exists("pos.txt") then fs.delete("pos.txt") end
- local file = fs.open("pos.txt","w+")
- file.write(self.x .. " " .. self.y .. ",, " .. self.z .. ", "..self.rot)
- file.close()
- index = 0
- end
- end
- end
- if math.abs(self.x - x) > self:refuel() then error("Lack sufficient fuel") end
- if self.x > x then
- self:rotate("W") --Face west if you are more east than your target
- while self.x ~= x do
- assert(turtle.forward())
- self.x = self.x - 1
- index = index + 1
- if index%100 == 0 then
- logAction("pos",self.x,self.y,self.z,self.rot)
- if fs.exists("pos.txt") then fs.delete("pos.txt") end
- local file = fs.open("pos.txt","w+")
- file.write(self.x .. " " .. self.y .. ",, " .. self.z .. ", "..self.rot)
- file.close()
- index = 0
- end
- end
- elseif self.x < x then
- self:rotate("E")
- while self.x ~= x do
- assert(turtle.forward())
- self.x = self.x + 1
- index = index + 1
- if index%100 == 0 then
- logAction("pos",self.x,self.y,self.z,self.rot)
- if fs.exists("pos.txt") then fs.delete("pos.txt") end
- local file = fs.open("pos.txt","w+")
- file.write(self.x .. " " .. self.y .. ",, " .. self.z .. ", "..self.rot)
- file.close()
- index = 0
- end
- end
- end
- if math.abs(self.z - z) > self:refuel() then error("Lack sufficient fuel") end
- if self.z > z then
- self:rotate("N") --Face north (-z) if you are more south(+z) than your target
- while self.z ~= z do
- assert(turtle.forward())
- self.x = self.x - 1
- index = index + 1
- if index%100 == 0 then
- logAction("pos",self.x,self.y,self.z,self.rot)
- if fs.exists("pos.txt") then fs.delete("pos.txt") end
- local file = fs.open("pos.txt","w+")
- file.write(self.x .. " " .. self.y .. ",, " .. self.z .. ", "..self.rot)
- file.close()
- index = 0
- end
- end
- elseif self.z < z then
- self:rotate("S")
- while self.z ~= z do
- assert(turtle.forward())
- self.z = self.z + 1
- index = index + 1
- if index%100 == 0 then
- logAction("pos",self.x,self.y,self.z,self.rot)
- if fs.exists("pos.txt") then fs.delete("pos.txt") end
- local file = fs.open("pos.txt","w+")
- file.write(self.x .. " " .. self.y .. ",, " .. self.z .. ", "..self.rot)
- file.close()
- index = 0
- end
- end
- end
- if verticalLast then
- if math.abs(self.y - y) > self:refuel() then error("Lack sufficient fuel") end
- while self.y < y do
- assert(turtle.up())
- self.y = self.y + 1
- index = index + 1
- if index%100 == 0 then
- logAction("pos",self.x,self.y,self.z,self.rot)
- if fs.exists("pos.txt") then fs.delete("pos.txt") end
- local file = fs.open("pos.txt","w+")
- file.write(self.x .. " " .. self.y .. ",, " .. self.z .. ", "..self.rot)
- file.close()
- index = 0
- end
- end
- while self.y > y do
- assert(turtle.down())
- self.y = self.y - 1
- index = index + 1
- if index%100 == 0 then
- logAction("pos",self.x,self.y,self.z,self.rot)
- if fs.exists("pos.txt") then fs.delete("pos.txt") end
- local file = fs.open("pos.txt","w+")
- file.write(self.x .. " " .. self.y .. ",, " .. self.z .. ", "..self.rot)
- file.close()
- index = 0
- end
- end
- end
- end
- --Checks and equips the necessary items
- function turt:checkNeededItems()
- logAction("item check")
- if peripheral.getType("left") ~= "chunky" then --If chunkloader not in left slot, try to make sure it is
- if peripheral.getType("right") == "chunky" then --If chunkloader in the wrong slot, put it in the right slot
- if self:equipItem("computercraft:wireless_modem_advanced") or self:equipItem("advancedperipherals:environment_detector") then
- if not self:equipItem("advancedperipherals:chunk_controller","left") then return false end --If you could replace the right slot with its typical peripherals and put the chunk controller in the left slot then you're okay
- else
- return false
- end
- else
- if not self:equipItem("advancedperipherals:chunk_controller","left") then return false end
- end
- end
- if peripheral.getType("right") ~= "environmentDetector" and not self:equipItem("advancedperipherals:environment_detector") then return false end
- if (peripheral.getType("right") ~= "modem" or not peripheral.wrap("right").isWireless()) and not self:equipItem("computercraft:wireless_modem_advanced") then return false end
- return true
- end
- function turt:moveToChunk(cX,cZ)
- logAction("chunkMove",cX,cZ)
- if not tonumber(cX) or not tonumber(cZ) then return false end
- return self:moveToPos(cX*16,nil,cZ*16)
- end
- function turt:openRednet()
- logAction("rednet open")
- self:equipItem("computercraft:wireless_modem_advanced")
- peripheral.find("modem",rednet.open)
- local s = rednet.isOpen()
- local _,r = pcall(function()
- local regions = fs.list("region")
- return regions[1] end)
- if s then pcall(function() rednet.host("fern-scanner-turtle",r or ""..os.getComputerID()) end) end
- return s
- end
- function turt:getBiome()
- logAction("get biome")
- self:equipItem("advancedperipherals:environment_detector")
- local envDet = peripheral.find("environmentDetector")
- if not envDet then error("No environment detector found") end
- return envDet.getBiome()
- end
- function turt:getChunk()
- logAction("calc chunk")
- return {math.floor(self.x/16),math.floor(self.z/16)}
- end
- function turt:diagnostics()
- logAction("diagnostics")
- self:openRednet()
- local pos = {gps.locate()} --gps locate pos
- local ipos = {self.x,self.y,self.z} --What the turtle thinks its at
- local fuel = turtle.getFuelLevel()
- local items = {}
- for i=1,16 do
- items[i] = turtle.getItemDetail(i)
- end
- return {pos=pos,fuel=fuel,items=items,ipos=ipos}
- end
- return turt
- end
- TURTLE = Turtle()
- if not TURTLE:checkNeededItems() then error("Lacking items to start") end
- assert(TURTLE:openRednet())
- HOST = assert(rednet.lookup("FERN-HOST"),"No host found")
- local ss_receive,ss_send
- ss_receive = function()
- TURTLE:openRednet()
- --@2m80_
- local sender,message,protocol = rednet.receive() --temporary solution
- if protocol == "ping" then
- ss_send(s,TURTLE:diagnostics(),"pong")
- elseif protocol == "update" then
- error("Update")
- end
- logAction("ss_receive",sender,message,protocol)
- return sender, message, protocol --Just like rednet?
- end
- ss_send = function(target,message,protocol,response_protocol)
- logAction("ss_send",target,message,protocol,response_protocol)
- TURTLE:openRednet()
- rednet.send(target,message,protocol) --temporary solution
- --@2m80_
- if response_protocol then
- local s,m,p = ss_receive()
- while s ~= target and p ~= response_protocol do
- s,m,p = ss_receive()
- if s == target and p == "error" then error(message) end
- end
- return s,m,p
- end
- return success --Should include?
- end
- local function requireRegion()
- while not fs.isDir("region") or #fs.list("region") <= 0 do
- local _,message = ss_send(HOST,"","get_region","region_assignment")
- if type(message) == "table" and #message == 2 and tonumber(message[1]) and tonumber(message[2]) then
- fern_data.readDat(string.format("region/%d_%d.dat",tonumber(message[1]),tonumber(message[2])),0,0)
- end
- end
- local region = assert(fs.list("region")[1],"failed to get region")
- local rX,rZ = region:match("(-?%d+)_(-?%d+)")
- rX,rZ = assert(tonumber(rX)),assert(tonumber(rZ))
- logAction("region requirement",rX,rZ,region)
- return rX,rZ,region
- end
- REG_X,REG_Z,REGION = requireRegion()
- --Gets the chunk list of chunks needed to explore
- local function getChunkList()
- if not fs.exists("region/"..REGION) then error("No region file") end
- local file = fs.open("region/"..REGION,"rb")
- local chunkList = {}
- --region file
- for x=0,127 do
- local row = {}
- for z=0,127 do
- local char = file.read()
- if not char then error("invalid region file") end --If the file doesnt contain all 16384 chunks, error
- if char == 0 then table.insert(row,{REG_X + x,REG_Z+z}) --If chunk isnt filled out yet, add to list
- elseif char == 256 then --If variable char, continue eating bytes until you are at the next char
- repeat
- char = file.read()
- until char ~= 256
- end
- end
- local lb,ub,step = 1,#row,1
- if x%2 == 0 then lb,ub,step = #row,1,-1 end --switch order so the turtle follows a snake path through a region
- for i=lb,ub,step do
- table.insert(chunkList,row[i])
- end
- end
- return chunkList
- end
- CHUNK_LIST = getChunkList()
- local function synchronize_data()
- logAction("synchronize")
- local data = fern_data.readDat("region/"..REGION)
- local file = fs.open(fern_data.BIOME_METADATA_PATH,"r")
- local metadata = {}
- if file then
- metadata = textutils.unserialiseJSON(file.readAll()) or {}
- file.close()
- end
- local _,message = ss_send(HOST,{data=data,metadata=metadata},"synchronize","synchronize_response")
- if type(message) == "table" and message.sync then
- if type(message.data) == "string" and type(message.metadata) == table then
- local file = fs.open(fern_data.BIOME_METADATA_PATH,"w+")
- file.write(textutils.serialiseJSON(message.metadata))
- file.close()
- local file = fs.open("region/"..REGION,"w+")
- file.write(message.data)
- file.close()
- end
- end
- end
- local function sendData()
- local biome = TURTLE:getBiome()
- logAction("data send")
- local chunk = TURTLE:getChunk()
- fern_data.writeDat("region/"..REGION,chunk[1],chunk[2],biome)
- local _,message = ss_send(HOST,{chunk=chunk,biome=biome},"data","data_success")
- end
- local function main()
- synchronize_data()
- while #CHUNK_LIST > 0 do
- local chunk = table.remove(CHUNK_LIST)
- parallel.waitForAny(function() TURTLE:moveToChunk(chunk[1],chunk[2]) end,function() while true do ss_receive() end end)
- sendData()
- end
- synchronize_data()
- end
- main()
- end
- local s,errMsg = pcall(wrapper)
- if not s then
- logAction("ERROR",errMsg)
- pcall(function()
- local turtleStats = {}
- for k,v in pairs(TURTLE) do
- if type(v) ~= "function" then
- table.insert(turtleStats,k .. tostring(v))
- end
- end
- logAction("stats",unpack(turtleStats))
- end)
- pcall(function() logAction("gps",gps.locate()) end)
- pcall(function() logAction("diagnostics",TURTLE:diagnostics()) end)
- pcall(function() TURTLE:openRednet() ss_send(rednet.CHANNEL_BROADCAST,errMsg,"scanner crash") end)
- end
Advertisement
Add Comment
Please, Sign In to add comment