Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Farmer
- -- ----------------------------------------------------------
- -- Not very portable, but it does the job.
- -- ----------------------------------------------------------
- -- Initialise important values:
- -- ----------------------------------------------------------
- -- Locations of interest:
- local node = {{-1015,3444,63,"east"}, -- 1, loot chest.
- {-1017,3441,63,"south"}, -- 2, charcoal barrel.
- {-1013,3442,64,"east"}, -- 3, fence top.
- {-1008,3442,64,"north"}, -- 4, fence hopping spot.
- {-1008,3440,64,"east"}} -- 5, harvest start point.
- local facing = {"north","east","south","west"}
- local direction,x,y,z,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
- -- Returns true if the turtle is carrying anything.
- local function carrying()
- for i=1,15 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 < direction then
- if tardir == 1 and direction == 4 then
- while not turtle.turnRight() do end
- else
- for i=1,direction-tardir do while not turtle.turnLeft() do end end
- end
- elseif tardir > direction then
- if tardir == 4 and direction == 1 then
- while not turtle.turnLeft() do end
- else
- for i=1,tardir-direction do while not turtle.turnRight() do end end
- end
- end
- direction = tardir
- end
- -- Travel to a co-ordinate.
- local function goToPos(target,digger)
- while (x ~= node[target][1]) or (y ~= node[target][2]) or (z ~= node[target][3]) do
- if z > node[target][3] then
- if turtle.down() then z = z - 1 elseif digger then turtle.digDown() end
- elseif z < node[target][3] then
- if turtle.up() then z = z + 1 elseif digger then turtle.digUp() end
- end
- if x > node[target][1] then
- if direction ~= 4 then faceDirection("west") end
- if turtle.forward() then x = x - 1 elseif digger then turtle.dig() end
- elseif x < node[target][1] then
- if direction ~= 2 then faceDirection("east") end
- if turtle.forward() then x = x + 1 elseif digger then turtle.dig() end
- end
- if y > node[target][2] then
- if direction ~= 1 then faceDirection("north") end
- if turtle.forward() then y = y - 1 elseif digger then turtle.dig() end
- elseif y < node[target][2] then
- if direction ~= 3 then faceDirection("south") end
- if turtle.forward() then y = y + 1 elseif digger then turtle.dig() end
- end
- end
- faceDirection(node[target][4])
- end
- -- Enters the farm pen.
- local function hopInFence()
- goToPos(3)
- goToPos(4)
- end
- -- Leaves the farm pen.
- local function hopOutFence()
- goToPos(4,true)
- goToPos(3)
- end
- -- Refuel the turtle itself.
- local function goGetFuel()
- print("I'm hungry. Off to eat some EU...")
- if x>-1009 then hopOutFence() end
- goToPos(2)
- print("OMNOMNOM")
- turtle.select(10)
- while turtle.getFuelLevel() < 10000 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...")
- if x>-1009 then hopOutFence() end
- goToPos(1)
- for i=1,15 do if turtle.getItemCount(i) ~= 0 then
- turtle.select(i)
- turtle.dropDown()
- end end
- print("")
- end
- -- Roam the plot collecting and planting.
- local function harvest()
- print("I'm a farmer and I'm OK!")
- print("")
- turtle.select(16)
- for yy = 1,16 do for xx = 1,16 do
- -- Harvest & replant crop.
- turtle.digDown()
- turtle.placeDown()
- -- Move ahead.
- if xx < 16 then
- while not turtle.forward() do
- turtle.dig()
- turtle.attack()
- end
- else
- if bit.band(yy,1)==1 then
- while not turtle.turnRight() do end
- while not turtle.forward() do
- turtle.dig()
- turtle.attack()
- end
- while not turtle.turnRight() do end
- elseif yy<16 then
- while not turtle.turnLeft() do end
- while not turtle.forward() do
- turtle.dig()
- turtle.attack()
- end
- while not turtle.turnLeft() do end
- end
- end
- if report then doTurtleServerReport() end
- end end
- y,direction = 3455,4
- 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
- if carrying() then dropOff() end -- Ensure I'm not carrying spare stuff.
- if turtle.getFuelLevel() < 2000 then goGetFuel() end
- if x<-1008 then hopInFence() end
- goToPos(5,true)
- for i=60,1,-1 do
- print("Waiting for crops to grow, "..i.." minutes remaining.")
- print("")
- sleep(60)
- if report then doTurtleServerReport() end
- end
- ---if getAmountOf("Seeds (295:0)") == 4096 and getAmountOf("Wheat (296:0)") == 4096 then
- -- print("I've filled my barrels, guess I'll take a breather...")
- -- print("")
- -- while getAmountOf("Seeds (295:0)") == 4096 and getAmountOf("Wheat (296:0)") == 4096 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 tempx, tempy, tempz
- 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 end
- x,z,y = gps.locate(5)
- if x < tempx then direction = 4
- elseif x > tempx then direction = 2
- elseif y < tempy then direction = 1
- else 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 "..x..","..y..","..z..", I have "..turtle.getFuelLevel().." fuel and I'm facing "..facing[direction]..".")
- print("")
- parallel.waitForAny(main, turtleServer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement