Advertisement
BombBloke

Farmer (ComputerCraft)

Jan 2nd, 2014 (edited)
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.88 KB | None | 0 0
  1. -- Farmer
  2. -- ----------------------------------------------------------
  3.  
  4. -- Not very portable, but it does the job.
  5.  
  6. -- ----------------------------------------------------------
  7.  
  8. -- Initialise important values:
  9.  
  10. -- ----------------------------------------------------------
  11.  
  12. -- Locations of interest:
  13. local node = {{-1015,3444,63,"east"}, --   1, loot chest.
  14.     {-1017,3441,63,"south"},       --   2, charcoal barrel.
  15.     {-1013,3442,64,"east"},      --   3, fence top.
  16.     {-1008,3442,64,"north"},       --   4, fence hopping spot.
  17.     {-1008,3440,64,"east"}}      --   5, harvest start point.
  18.  
  19. local facing = {"north","east","south","west"}
  20. local direction,x,y,z,report
  21. local modem = peripheral.wrap("right")
  22.  
  23. -- Number of inventory servers (that should be) online:
  24. local invServerCount, invServer = 0, {}
  25.  
  26. -- ----------------------------------------------------------
  27.  
  28. -- Functions and stuff:
  29.  
  30. -- ----------------------------------------------------------
  31.  
  32. -- Tell the turtle server I'm alive.
  33. local function doTurtleServerReport()
  34.     rednet.send(report,{"Hello TurtleServer",os.getComputerLabel()})
  35.     report = nil
  36. end
  37.  
  38. -- Poll the inventory servers re a given barrel.
  39. local function getAmountOf(barreltype)
  40.     local myMessage
  41.     for i=1,3 do
  42.         for i=1,invServerCount do rednet.send(invServer[i],barreltype) end
  43.         myMessage = {rednet.receive(3)}
  44.         if myMessage and tonumber(myMessage[2]) then break end
  45.     end
  46.    
  47.     if not tonumber(myMessage[2]) then
  48.         print("Warning: Could not get reading for amount of "..barreltype..".")
  49.         return 0
  50.     else return tonumber(myMessage[2]) end
  51. end
  52.  
  53. -- Returns true if the turtle is carrying anything.
  54. local function carrying()
  55.     for i=1,15 do if turtle.getItemCount(i) ~= 0 then return true end end
  56.     return false
  57. end
  58.  
  59. -- Accepts strings representing compass-facings to turn the turtle.
  60. local function faceDirection(targetdirection)
  61.     local tardir = 1
  62.     for i=1,4 do if targetdirection == facing[i] then tardir = i end end
  63.    
  64.     if tardir < direction then
  65.         if tardir == 1 and direction == 4 then
  66.             while not turtle.turnRight() do end
  67.         else
  68.             for i=1,direction-tardir do while not turtle.turnLeft() do end end
  69.         end
  70.     elseif tardir > direction then
  71.         if tardir == 4 and direction == 1 then
  72.             while not turtle.turnLeft() do end
  73.         else
  74.             for i=1,tardir-direction do while not turtle.turnRight() do end end
  75.         end
  76.     end
  77.    
  78.     direction = tardir
  79. end
  80.  
  81. -- Travel to a co-ordinate.
  82. local function goToPos(target,digger)
  83.     while (x ~= node[target][1]) or (y ~= node[target][2]) or (z ~= node[target][3]) do
  84.         if z > node[target][3] then
  85.             if turtle.down() then z = z - 1 elseif digger then turtle.digDown() end
  86.         elseif z < node[target][3] then
  87.             if turtle.up() then z = z + 1 elseif digger then turtle.digUp() end
  88.         end
  89.        
  90.         if x > node[target][1] then
  91.             if direction ~= 4 then faceDirection("west") end
  92.             if turtle.forward() then x = x - 1 elseif digger then turtle.dig() end
  93.         elseif x < node[target][1] then
  94.             if direction ~= 2 then faceDirection("east") end
  95.             if turtle.forward() then x = x + 1 elseif digger then turtle.dig() end
  96.         end
  97.        
  98.         if y > node[target][2] then
  99.             if direction ~= 1 then faceDirection("north") end
  100.             if turtle.forward() then y = y - 1 elseif digger then turtle.dig() end
  101.         elseif y < node[target][2] then
  102.             if direction ~= 3 then faceDirection("south") end
  103.             if turtle.forward() then y = y + 1 elseif digger then turtle.dig() end
  104.         end
  105.     end
  106.    
  107.     faceDirection(node[target][4])
  108. end
  109.  
  110. -- Enters the farm pen.
  111. local function hopInFence()
  112.     goToPos(3)
  113.     goToPos(4)
  114. end
  115.  
  116. -- Leaves the farm pen.
  117. local function hopOutFence()
  118.     goToPos(4,true)
  119.     goToPos(3)
  120. end
  121.  
  122. -- Refuel the turtle itself.
  123. local function goGetFuel()
  124.     print("I'm hungry. Off to eat some EU...")
  125.     if x>-1009 then hopOutFence() end
  126.     goToPos(2)
  127.     print("OMNOMNOM")
  128.     turtle.select(10)
  129.     while turtle.getFuelLevel() < 10000 do
  130.         turtle.suckUp()
  131.         turtle.refuel()
  132.         sleep(1)
  133.     end
  134.     print("Ah, that hit the spot!")
  135.     print("")
  136. end
  137.  
  138. -- Dumps the turtle's inventory into the dropoff chest.
  139. local function dropOff()
  140.     print("Dropping off loot...")
  141.     if x>-1009 then hopOutFence() end
  142.     goToPos(1)
  143.     for i=1,15 do if turtle.getItemCount(i) ~= 0 then
  144.         turtle.select(i)
  145.         turtle.dropDown()
  146.     end end
  147.     print("")
  148. end
  149.  
  150. -- Roam the plot collecting and planting.
  151. local function harvest()
  152.     print("I'm a farmer and I'm OK!")
  153.     print("")
  154.    
  155.     turtle.select(16)
  156.    
  157.     for yy = 1,16 do for xx = 1,16 do
  158.         -- Harvest & replant crop.
  159.         turtle.digDown()
  160.         turtle.placeDown()
  161.  
  162.         -- Move ahead.
  163.         if xx < 16 then
  164.             while not turtle.forward() do
  165.                 turtle.dig()
  166.                 turtle.attack()
  167.             end
  168.         else
  169.             if bit.band(yy,1)==1 then
  170.                 while not turtle.turnRight() do end
  171.                 while not turtle.forward() do
  172.                     turtle.dig()
  173.                     turtle.attack()
  174.                 end
  175.                 while not turtle.turnRight() do end
  176.             elseif yy<16 then
  177.                 while not turtle.turnLeft() do end
  178.                 while not turtle.forward() do
  179.                     turtle.dig()
  180.                     turtle.attack()
  181.                 end
  182.                 while not turtle.turnLeft() do end
  183.             end
  184.         end
  185.        
  186.         if report then doTurtleServerReport() end
  187.     end end
  188.    
  189.     y,direction = 3455,4
  190. end
  191.  
  192. -- ----------------------------------------------------------
  193.  
  194. -- Primary functions:
  195.  
  196. -- ----------------------------------------------------------
  197.  
  198. -- Deal with rednet.
  199. local function turtleServer()
  200.     local turtleServerTimeOut, myEvent = os.startTimer(0)
  201.     while true do
  202.         myEvent = {os.pullEvent()}
  203.         if myEvent[1] == "timer" and myEvent[2] == turtleServerTimeOut then
  204.             report = 65535
  205.             turtleServerTimeOut = os.startTimer(600)
  206.         elseif myEvent[1] == "rednet_message" and myEvent[3] == "Hey, you still there?" then
  207.             report = myEvent[2]
  208.             turtleServerTimeOut = os.startTimer(600)
  209.         end
  210.     end
  211. end
  212.  
  213. -- Primary work loop.
  214. local function main()
  215.     while true do
  216.         if carrying() then dropOff() end      -- Ensure I'm not carrying spare stuff.
  217.  
  218.         if turtle.getFuelLevel() < 2000 then goGetFuel() end
  219.  
  220.         if x<-1008 then hopInFence() end
  221.  
  222.         goToPos(5,true)
  223.  
  224.         for i=60,1,-1 do
  225.             print("Waiting for crops to grow, "..i.." minutes remaining.")
  226.             print("")
  227.             sleep(60)
  228.             if report then doTurtleServerReport() end
  229.         end
  230.        
  231.         ---if getAmountOf("Seeds (295:0)") == 4096 and getAmountOf("Wheat (296:0)") == 4096 then
  232.         --  print("I've filled my barrels, guess I'll take a breather...")
  233.         --  print("")
  234.         --  while getAmountOf("Seeds (295:0)") == 4096 and getAmountOf("Wheat (296:0)") == 4096 do
  235.         --      sleep(60)
  236.         --      if report then doTurtleServerReport() end
  237.         --  end
  238.         --end
  239.  
  240.         harvest()
  241.     end
  242. end
  243.  
  244. -- ----------------------------------------------------------
  245.  
  246. -- I've just booted up. Where am I? Who am I? etc...
  247.  
  248. -- ----------------------------------------------------------
  249.  
  250. -- Ping the GPS servers until I get a valid reading:
  251. do
  252.     local tempx, tempy, tempz
  253.     while tempx == nil or tempy == nil or tempz == nil do
  254.         tempx, tempz, tempy = gps.locate(5)
  255.         sleep(5)
  256.     end
  257.    
  258.     while not turtle.forward() do while not turtle.turnLeft() do end end
  259.     x,z,y = gps.locate(5)
  260.  
  261.     if x < tempx then direction = 4
  262.     elseif x > tempx then direction = 2
  263.     elseif y < tempy then direction = 1
  264.     else direction = 3 end
  265. end
  266.  
  267. -- Broadcast until all inventory servers are identified.
  268. rednet.open("right")
  269. while #invServer < invServerCount do
  270.     rednet.broadcast("Hello InvServer")
  271.     local myTimer = os.startTimer(5)
  272.     while true do
  273.         local myEvent = {os.pullEvent()}
  274.         if myEvent[1] == "timer" and myEvent[2] == myTimer then
  275.             break
  276.         elseif myEvent[1] == "rednet_message" and myEvent[3] == "Hello, InvServerClient!" then
  277.             invServer[#invServer+1] = myEvent[2]
  278.             for i=1,#invServer-1 do if invServer[i] == invServer[#invServer] then invServer[#invServer] = nil end end
  279.             if #invServer == invServerCount then break end
  280.         end
  281.     end
  282. end
  283.  
  284. print("I'm at "..x..","..y..","..z..", I have "..turtle.getFuelLevel().." fuel and I'm facing "..facing[direction]..".")
  285. print("")
  286.  
  287. parallel.waitForAny(main, turtleServer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement