Godleydemon

Untitled

Jun 12th, 2013
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.07 KB | None | 0 0
  1. local tArgs={...}
  2. local turtleId
  3. local isWirelessTurtle
  4. local messageOutputFileName
  5.  
  6.  
  7. local function printUsage()
  8.   write([[
  9. Usage:
  10. farm <length> <width> [timer]
  11.   <length> is the length of each row
  12. of the field
  13.   <width> is the number of rows
  14.   [timer] is an optional number of
  15. seconds to wait before repeating.
  16. -- Press any key to continue --]])
  17.   os.pullEvent("key")
  18.   print([[
  19.                                  
  20. Placement
  21. The turtle should begin above a chest facing towards the first row, and will move right for each subsequent row. The chest should contain the item to be planted. If the plant has seeds different from the harvested items, like wheat, place a 2nd chest above the turtle for the output.
  22. Note: If area is not already plowed or
  23. planted it must be a farming turtle.]])
  24. end
  25.  
  26.  
  27. if #tArgs<2 then
  28.   printUsage()
  29.   return
  30. end
  31.  
  32. local length=tonumber(tArgs[1])
  33. local width=tonumber(tArgs[2])
  34. local repeatTime=tonumber(tArgs[3]) or 0
  35.  
  36.  
  37. isWirelessTurtle = peripheral.isPresent("right")
  38. if (isWirelessTurtle == true) then
  39.   turtleId = os.getComputerLabel()
  40.   rednet.open("right")
  41. end
  42.  
  43. function writeMessage(message)
  44.     print(message)
  45.  
  46.     -- If this turtle has a modem, then write the message to red net
  47.     if (isWirelessTurtle == true) then
  48.       if (turtleId == nil) then
  49.         rednet.broadcast(message)
  50.       else
  51.         -- Broadcast the message (prefixed with the turtle's id)
  52.         rednet.broadcast("[".. turtleId.."] "..message)
  53.       end
  54.     end
  55.  
  56.     if (messageOutputFileName ~= nil) then
  57.       -- Open file, write message and close file (flush doesn't seem to work!)
  58.       local outputFile = io.open(messageOutputFileName, "a")
  59.       outputFile:write(message)
  60.       outputFile:write("\n")
  61.       outputFile:close()
  62.     end
  63. end
  64.  
  65.  
  66. local function repeatUntil(func)
  67.   while not func() do sleep(.5) end
  68. end
  69.  
  70. local function step()
  71.   repeatUntil(turtle.forward)
  72.   --dry soil occassionally untils between dig and plant,
  73.   --so repeat until successful on planting (or out of things to plant)
  74.   local tryCount=0
  75.   repeat
  76.     turtle.digDown()
  77.     tryCount=tryCount+1
  78.   until turtle.placeDown() or turtle.getItemCount(1)==0 or tryCount==3
  79. end
  80.  
  81. local function waitForKey(prompt)
  82.     writeMessage(prompt)
  83.   os.pullEvent("key")
  84. end
  85.  
  86.  
  87. local function harvest()
  88.   --do I have enough fuel?
  89.   local cost=(length+1)*width
  90.   --if odd rows, add return trip
  91.   if width%2==1 then
  92.     cost=cost+length
  93.   end
  94.   while type(turtle.getFuelLevel())=="number" and cost>turtle.getFuelLevel() do
  95.     waitForKey("Insufficient fuel. Place fuel in any slot and press a key to continue...")
  96.  
  97.     for i=2,16 do
  98.       turtle.select(i)
  99.       if turtle.refuel() then
  100.         break
  101.       end
  102.     end
  103.     turtle.select(1)
  104.     term.clear()
  105.   end
  106.   writeMessage("Harvesting...")
  107.  
  108.   step()
  109.   local turnNext=turtle.turnRight
  110.   local turnOther=turtle.turnLeft
  111.   for row=1,width do
  112.     for pos=1,length-1 do
  113.       step()
  114.     end
  115.     if row<width then
  116.       turnNext()
  117.       step()
  118.       turnNext()
  119.     end
  120.     local temp=turnNext
  121.     turnNext=turnOther
  122.     turnOther=temp
  123.   end
  124.  
  125.   --return to start position
  126.   --if odd number of rows, we're at far end of rows, return
  127.   if width%2==1 then
  128.     for i=1,length do
  129.       --if we hit something, just wait a bit and retry;
  130.       repeatUntil(turtle.back)
  131.     end
  132.     turtle.turnLeft()
  133.   else
  134.     --was even rows, so at right end of rows facing opposite way
  135.     repeatUntil(turtle.forward)
  136.     turtle.turnRight()
  137.   end
  138.   --head back to first row
  139.   for i=1,width-1 do
  140.     repeatUntil(turtle.forward)
  141.   end
  142.   --turn to face starting direction
  143.   turtle.turnRight()
  144.  
  145.   --unload!
  146.   writeMessage("Unloading...")
  147.   --anythign matching what's in slot 1 goes down, rest goes up
  148.   for i=2,16 do
  149.     turtle.select(i)
  150.     if turtle.getItemCount(i)>0 then
  151.       if turtle.compareTo(1) then
  152.         while not turtle.dropDown() do
  153.           waitForKey("input chest full, empty it and press any key to continue...")
  154.     writeMessage("input chest full, empty it and press any key to continue...")
  155.         end
  156.       else
  157.         while not turtle.dropUp() do
  158.           waitForKey("output chest full, empty it and press any key to continue...")
  159.     writeMessage("output chest full, empty it and press any key to continue...")
  160.         end
  161.       end
  162.     end
  163.   end
  164.   turtle.select(1)
  165. end
  166.  
  167. --load up with stuffs
  168. turtle.select(1)
  169. turtle.suckDown()
  170.  
  171. if repeatTime>0 then
  172.   while true do
  173.     harvest()
  174.     term.clear()
  175.     local i=repeatTime
  176.     while i>0 do
  177.       local tick,interval=1,"second"
  178.       if i>3600 then
  179.         tick,interval=3600,"hour"
  180.       elseif i>60 then
  181.         tick,interval=60,"minute"
  182.       end
  183.       local count=math.floor(i/tick)
  184.       if count==1 and tick>1 then
  185.         tick=i-tick
  186.       else
  187.         interval=interval.."s"
  188.       end
  189.       term.setCursorPos(1,1)
  190.       term.clearLine()
  191.       write("Harvesting again in "..count.." "..interval)
  192.       sleep(tick)
  193.       i=i-tick
  194.     end
  195.   end
  196. else
  197.   harvest()
  198.   --drop seeds back
  199.   turtle.select(1)
  200.   turtle.dropDown()
  201. end
Advertisement
Add Comment
Please, Sign In to add comment