Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Chopper
- -- ----------------------------------------------------------
- -- Efficiency test; can this turtle provide a semi-decent
- -- income of wood/charcoal?
- -- For those reading, plan is to add a companion turtle that
- -- uses the resulting (minor) power generation to produce
- -- supplies of various building blocks. Stone bricks,
- -- glass, planks, etc.
- -- And then a third that'll build a base from those blocks.
- -- ----------------------------------------------------------
- -- Initialise important values:
- -- ----------------------------------------------------------
- -- Load locations of interest:
- shell.run("node")
- local facing = {"north","east","south","west"}
- local pos, report = {}
- local modem = peripheral.wrap("right")
- -- Number of inventory servers (that should be) online:
- local invServerCount, invServer = 0, {}
- -- ----------------------------------------------------------
- -- Functions and stuff:
- -- ----------------------------------------------------------
- -- Tell the turtle server I'm alive.
- local function doTurtleServerReport()
- rednet.send(report,{"Hello TurtleServer",os.getComputerLabel()})
- report = nil
- end
- -- Poll the inventory servers re a given barrel.
- local function getAmountOf(barreltype)
- local myMessage
- for i=1,3 do
- for i=1,invServerCount do rednet.send(invServer[i],barreltype) end
- myMessage = {rednet.receive(3)}
- if myMessage and tonumber(myMessage[2]) then break end
- end
- if not tonumber(myMessage[2]) then
- print("Warning: Could not get reading for amount of "..barreltype..".")
- return 0
- else return tonumber(myMessage[2]) end
- end
- -- Check if the barrels I fill are filled.
- local function barrelsFull()
- for i=1,#barrel do if getAmountOf(barrel[i]) < 4096 then return false end end
- return true
- end
- -- Returns true if the turtle is carrying anything.
- local function carrying()
- for i=1,16 do if turtle.getItemCount(i) ~= 0 then return true end end
- return false
- end
- -- Accepts strings representing compass-facings to turn the turtle.
- local function faceDirection(targetdirection)
- local tardir = 1
- for i=1,4 do if targetdirection == facing[i] then tardir = i end end
- if tardir < pos.direction then
- if tardir == 1 and pos.direction == 4 then
- while not turtle.turnRight() do end
- else
- for i=1,pos.direction-tardir do while not turtle.turnLeft() do end end
- end
- elseif tardir > pos.direction then
- if tardir == 4 and pos.direction == 1 then
- while not turtle.turnLeft() do end
- else
- for i=1,tardir-pos.direction do while not turtle.turnRight() do end end
- end
- end
- pos.direction = tardir
- end
- -- Move ahead a block, keeping track of the turtle's position.
- local function forward()
- while not turtle.forward() do
- turtle.dig()
- turtle.attack()
- end
- if pos.direction == 1 then pos.y = pos.y - 1
- elseif pos.direction == 2 then pos.x = pos.x + 1
- elseif pos.direction == 3 then pos.y = pos.y + 1
- else pos.x = pos.x - 1 end
- end
- local function turn(tardir)
- if tardir == "left" then
- while not turtle.turnLeft() do end
- pos.direction = pos.direction - 1
- if pos.direction < 1 then pos.direction = 4 end
- elseif tardir == "right" then
- while not turtle.turnRight() do end
- pos.direction = pos.direction + 1
- if pos.direction > 4 then pos.direction = 1 end
- end
- end
- -- Travel to a co-ordinate.
- local function goToPos(target,digger)
- while (pos.x ~= node[target][1]) or (pos.y ~= node[target][2]) or (pos.z ~= node[target][3]) do
- if pos.z > node[target][3] then
- if digger then
- while not turtle.down() do
- turtle.digDown()
- turtle.attackDown()
- end
- pos.z = pos.z - 1
- elseif turtle.down() then pos.z = pos.z - 1 end
- elseif pos.z < node[target][3] then
- if digger then
- while not turtle.up() do
- turtle.digUp()
- turtle.attackUp()
- end
- pos.z = pos.z + 1
- elseif turtle.up() then pos.z = pos.z + 1 end
- end
- if pos.x > node[target][1] then
- if pos.direction ~= 4 then faceDirection("west") end
- if digger then forward() elseif turtle.forward() then pos.x = pos.x - 1 end
- elseif pos.x < node[target][1] then
- if pos.direction ~= 2 then faceDirection("east") end
- if digger then forward() elseif turtle.forward() then pos.x = pos.x + 1 end
- end
- if pos.y > node[target][2] then
- if pos.direction ~= 1 then faceDirection("north") end
- if digger then forward() elseif turtle.forward() then pos.y = pos.y - 1 end
- elseif pos.y < node[target][2] then
- if pos.direction ~= 3 then faceDirection("south") end
- if digger then forward() elseif turtle.forward() then pos.y = pos.y + 1 end
- end
- end
- faceDirection(node[target][4])
- end
- -- Refuel the turtle itself.
- local function goGetFuel()
- print("I'm hungry. Off to eat some EU...")
- goToPos(3)
- print("OMNOMNOM")
- turtle.select(10)
- while turtle.getFuelLevel() < 5000 do
- turtle.suckUp()
- turtle.refuel()
- sleep(1)
- end
- print("Ah, that hit the spot!")
- print("")
- end
- -- Dumps the turtle's inventory into the dropoff chest.
- local function dropOff()
- print("Dropping off loot...")
- local safe = false
- if (node[1][4] == "north" and pos.y < node[8][2]) or
- (node[1][4] == "east" and pos.x > node[8][1]) or
- (node[1][4] == "south" and pos.y > node[8][2]) or
- (node[1][4] == "west" and pos.x < node[8][1]) then safe = true end
- goToPos(8,safe)
- while not turtle.down() do turtle.attackDown() end
- pos.z = pos.z - 1
- for i=1,16 do if turtle.getItemCount(i) ~= 0 then
- turtle.select(i)
- turtle.dropDown()
- end end
- turtle.select(1)
- print("")
- end
- -- Roam the plot collecting and planting.
- local function harvest()
- print("I'm a lumberjack and I'm OK!")
- print("")
- if node[10][1] then
- goToPos(10,true)
- node[10] = {}
- else
- forward()
- pos.lastX, pos.lastY = 1, 1
- end
- for xx = pos.lastX,16 do
- for yy = pos.lastY,16 do
- -- Check if a return to base is required.
- if turtle.getFuelLevel() < 1000 or turtle.getItemCount(15) == 0 or turtle.getItemCount(14) > 0 then
- node[10][1] = pos.x
- node[10][2] = pos.y
- node[10][3] = pos.z
- node[10][4] = facing[pos.direction]
- pos.lastX = xx
- pos.lastY = yy
- return
- end
- -- Combine sapling piles.
- if turtle.getItemCount(15) < 5 then
- for i=1,14 do if turtle.getItemCount(i) ~= 0 then
- turtle.select(i)
- if turtle.compareTo(15) then turtle.transferTo(15) end
- if turtle.getItemCount(15) == 64 then break end
- end end
- end
- if turtle.detectDown() then
- -- Dig out the current column if need be.
- turtle.select(1)
- while turtle.detectUp() do
- while not turtle.up() do turtle.digUp() end
- pos.z = pos.z + 1
- for i=1,4 do
- while turtle.detect() do turtle.dig() end
- while not turtle.turnRight() do end
- end
- end
- while pos.z > node[1][3] do
- while not turtle.down() do turtle.digDown() end
- pos.z = pos.z - 1
- end
- end
- -- Re-plant.
- turtle.select(16)
- if turtle.compareDown() or not turtle.detectDown() then
- while turtle.compareDown() do turtle.digDown() end
- turtle.select(15)
- turtle.placeDown()
- end
- while turtle.suckDown() do end
- -- Move ahead.
- if yy < 16 then
- forward()
- elseif xx < 16 then
- if facing[pos.direction] == node[1][4] then
- turn("right")
- forward()
- turn("right")
- else
- turn("left")
- forward()
- turn("left")
- end
- end
- if report then doTurtleServerReport() end
- end
- end
- end
- -- ----------------------------------------------------------
- -- Primary functions:
- -- ----------------------------------------------------------
- -- Deal with rednet.
- local function turtleServer()
- local turtleServerTimeOut, myEvent = os.startTimer(0)
- while true do
- myEvent = {os.pullEvent()}
- if myEvent[1] == "timer" and myEvent[2] == turtleServerTimeOut then
- report = 65535
- turtleServerTimeOut = os.startTimer(600)
- elseif myEvent[1] == "rednet_message" and myEvent[3] == "Hey, you still there?" then
- report = myEvent[2]
- turtleServerTimeOut = os.startTimer(600)
- end
- end
- end
- -- Primary work loop.
- local function main()
- while true do
- print("Base maintenance...")
- print("")
- if carrying() then dropOff() end -- Ensure I'm not carrying spare stuff.
- if turtle.getFuelLevel() < 1000 then goGetFuel() end
- goToPos(4) -- Goto log barrel, get logs.
- turtle.select(1)
- turtle.suckUp()
- goToPos(6) -- Goto furnace, drop logs.
- turtle.drop()
- goToPos(9) -- Collect charcoal from furnace.
- turtle.select(2)
- turtle.suckDown()
- goToPos(7) -- Goto generator, drop charcoal.
- turtle.drop()
- if carrying() then dropOff() end
- goToPos(2) -- Goto sapling barrel, get saplings.
- turtle.select(15)
- turtle.suckUp()
- while turtle.getItemCount(15) == 0 do
- print("Oy! No saplings! Waiting a minute...")
- sleep(60)
- turtle.suckUp()
- end
- if turtle.getItemCount(16) == 0 then
- goToPos(4) -- Goto log barrel, get log(s) for comparisons.
- turtle.select(16)
- turtle.suckUp()
- end
- goToPos(8)
- goToPos(1,true) -- Prepare to harvest.
- --if barrelsFull() then
- -- print("I've filled my barrels, guess I'll take a breather...")
- -- print("")
- -- while barrelsFull() do
- -- sleep(60)
- -- if report then doTurtleServerReport() end
- -- end
- --end
- harvest()
- end
- end
- -- ----------------------------------------------------------
- -- I've just booted up. Where am I? Who am I? etc...
- -- ----------------------------------------------------------
- -- Ping the GPS servers until I get a valid reading:
- do
- local counter, tempx, tempy, tempz = 0
- while tempx == nil or tempy == nil or tempz == nil do
- tempx, tempz, tempy = gps.locate(5)
- sleep(5)
- end
- while not turtle.forward() do
- while not turtle.turnLeft() do end
- counter = counter + 1
- if counter == 20 then -- I really don't wanna do this
- counter = 0
- turtle.dig()
- end
- end
- pos.x,pos.z,pos.y = gps.locate(5)
- if pos.x < tempx then pos.direction = 4
- elseif pos.x > tempx then pos.direction = 2
- elseif pos.y < tempy then pos.direction = 1
- else pos.direction = 3 end
- end
- -- Broadcast until all inventory servers are identified.
- rednet.open("right")
- while #invServer < invServerCount do
- rednet.broadcast("Hello InvServer")
- local myTimer = os.startTimer(5)
- while true do
- local myEvent = {os.pullEvent()}
- if myEvent[1] == "timer" and myEvent[2] == myTimer then
- break
- elseif myEvent[1] == "rednet_message" and myEvent[3] == "Hello, InvServerClient!" then
- invServer[#invServer+1] = myEvent[2]
- for i=1,#invServer-1 do if invServer[i] == invServer[#invServer] then invServer[#invServer] = nil end end
- if #invServer == invServerCount then break end
- end
- end
- end
- print("I'm at "..pos.x..","..pos.y..","..pos.z..", I have "..turtle.getFuelLevel().." fuel and I'm facing "..facing[pos.direction]..".")
- print("")
- parallel.waitForAny(main, turtleServer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement