Advertisement
jacob4408

CC TreeFarmer

Nov 23rd, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.11 KB | None | 0 0
  1. --TreeFarmer needs a 25x25 prepared plot and storage chests structure
  2. local WAR = nil
  3. local colCnt = 4
  4. local smeltCnt = 0
  5. local smeltLoaded = 0
  6. local tName = os.getComputerLabel()
  7. local owner = nil
  8.  
  9.  
  10. local function round(num)
  11.    if num >= 0 then
  12.        return math.floor(num+.5)
  13.    else
  14.       return math.ceil(num-.5)
  15.    end
  16. end
  17.  
  18. local function circleSuck()
  19.    --pick up anything around turtle
  20.    for spin = 1, 4 do
  21.       turtle.suck()
  22.       turtle.turnLeft()
  23.    end
  24. end
  25.  
  26. local function determineAction()
  27.    --if empty to the right then plant
  28.    --if sapling ignore
  29.    --anything else harvest
  30.    local success, blockInfo = turtle.inspect()
  31.    local rc = ""
  32.    if success then
  33.       if string.find(blockInfo.name,":sapling") ~= nil then
  34.          --sapling found so do nothing
  35.                rc = "ignore"        
  36.             else
  37.                rc = "harvest"
  38.         end
  39.      else
  40.         rc = "plant"
  41.    end
  42.    
  43.    return rc
  44. end
  45.  
  46. local function plantSapling()
  47.  
  48.   mta.forward(1)
  49.   circleSuck()
  50.   mta.back(1)
  51.  
  52.     local sSlot = mta.getItemSlot("sapling")
  53.     if sSlot > -1 then
  54.        turtle.select(sSlot)
  55.        turtle.place()
  56.     end
  57. end
  58.  
  59. local function doubleDig()
  60. local rc = false
  61. local xPos = 0
  62. local x = 0
  63.  
  64.    for x = 1,3 do
  65.       if turtle.detect() then
  66.          rc = true
  67.          mta.dig()
  68.          mta.forward()
  69.          xPos = xPos + 1
  70.       else
  71.          break
  72.       end
  73.   end
  74.      
  75.   if xPos > 0 then
  76.      mta.back(xPos)
  77.   end
  78.  
  79.    return rc
  80. end
  81.  
  82. local function harvest()
  83.    --this is a simple linear implementation
  84.    local harvestedSomething = true
  85.    local zPos = 0
  86.    
  87.    --dig and move forward. should put turtle squarely under trunk
  88.    mta.dig()
  89.    mta.forward(1)
  90.    
  91.    while harvestedSomething == true do
  92.      harvestedSomething = false
  93.      --check all four sides and harvest anything found
  94.      for spin = 1, 4 do
  95.          harvestedSomething = doubleDig()
  96.        turtle.turnLeft()
  97.      end
  98.      --check above
  99.      while turtle.detectUp() do
  100.         mta.digUp()
  101.         harvestedSomething = true
  102.      end
  103.      --if found anything at this level then go up a level
  104.      if harvestedSomething == true then
  105.         mta.up(1)
  106.         zPos = zPos + 1
  107.      end
  108.    end
  109.    
  110.    --go back to ground level
  111.    while zPos > 0 do
  112.       mta.down()
  113.       zPos = zPos - 1
  114.    end
  115.    
  116.    circleSuck()
  117.    
  118.    mta.back(1)
  119.    
  120.    --replant sapling
  121.    local sSlot = mta.getItemSlot("sapling")
  122.      if sSlot > -1 then
  123.        turtle.select(sSlot)
  124.        turtle.place()
  125.      end
  126.    
  127. end
  128.  
  129. local function dropOffOther()
  130.    local success, blockInfo = turtle.inspect()
  131.    if success then
  132.       if string.find(blockInfo.name,"chest") ~= nil then
  133.          --in front of a chest so drop off anything not sapling or wood
  134.          for xSlot=1, 16 do
  135.             if turtle.getItemCount(xSlot) > 0 then
  136.                turtle.select(xSlot)
  137.                blockInfo = turtle.getItemDetail()
  138.                if blockInfo then
  139.                   --if not sapling, log or coal
  140.                   if (blockInfo.name ~="minecraft:sapling") and (blockInfo.name ~= "minecraft:log") and (blockInfo.name ~= "minecraft:coal") then
  141.                      if turtle.drop() == false then
  142.                         print("Unable to offload to 'Other'")
  143.                         while turtle.drop() == false do
  144.                             sleep(5)
  145.                         end
  146.                      end
  147.                   end
  148.                end
  149.             end
  150.          end
  151.       end  
  152.    end
  153. end
  154.  
  155. local function dropOffSaplings()
  156.    local success, blockInfo = turtle.inspect()
  157.    if success then
  158.       if string.find(blockInfo.name,"chest") ~= nil then
  159.          --in front of a chest so drop off anything not sapling or wood
  160.          for xSlot=1, 16 do
  161.             if turtle.getItemCount(xSlot) > 0 then
  162.                turtle.select(xSlot)
  163.                blockInfo = turtle.getItemDetail()
  164.                if blockInfo then
  165.                   --if sapling then drop off
  166.                   if blockInfo.name =="minecraft:sapling" then
  167.                      if turtle.drop() == false then
  168.                         print("Unable to offload saplings")
  169.                         while turtle.drop() == false do
  170.                             sleep(5)
  171.                         end
  172.                      end
  173.                   end
  174.                end
  175.             end
  176.          end
  177.          --now load back up with saplings
  178.          turtle.select(16)
  179.          turtle.suck(colCnt * 4)
  180.       end  
  181.    end
  182. end
  183.  
  184. local function dropOffWood()
  185.    local success, blockInfo = turtle.inspect()
  186.    if success then
  187.       if string.find(blockInfo.name,"chest") ~= nil then
  188.          --in front of a chest so drop off anything not sapling or wood
  189.          for xSlot=1, 16 do
  190.             if turtle.getItemCount(xSlot) > 0 then
  191.                turtle.select(xSlot)
  192.                blockInfo = turtle.getItemDetail()
  193.                if blockInfo then
  194.                   --if wood then drop off
  195.                   if blockInfo.name =="minecraft:log" then
  196.                      if turtle.drop() == false then
  197.                         print("Unable to offload wood")
  198.                         while turtle.drop() == false do
  199.                             sleep(5)
  200.                         end
  201.                      end
  202.                   end
  203.                end
  204.             end
  205.          end
  206.       end  
  207.    end
  208. end
  209.  
  210. local function loadSmeltSlot()
  211.    
  212. local slotAmt = 0    
  213. local dropAmt = 0
  214. local x = 0
  215.        
  216.    --try to load
  217.    while smeltLoaded < smeltCnt do
  218.       x = mta.getItemSlot("log")
  219.       if x == -1 then
  220.          --can't find any logs
  221.          print("Smelt loaded not reached but no logs found")
  222.          error()
  223.       else
  224.          turtle.select(x)
  225.          slotAmt = turtle.getItemCount(x)
  226.          if (smeltCnt - smeltLoaded) >= slotAmt then
  227.             dropAmt = slotAmt
  228.          else
  229.             dropAmt = smeltCnt - smeltLoaded
  230.          end
  231.          turtle.drop(dropAmt)
  232.          smeltLoaded = smeltLoaded + dropAmt
  233.       end
  234.    end
  235.  
  236. end
  237.  
  238. local function loadFuelSlot()
  239.   --drop whatever is left in fuel slot
  240.  
  241. local slotAmt = 0    
  242. local x = -1      
  243.  
  244.    --try to load
  245.    x = mta.getItemSlot("log")
  246.    while x > -1 do
  247.          turtle.select(x)
  248.          slotAmt = turtle.getItemCount(x)
  249.          turtle.drop(slotAmt)
  250.          x = mta.getItemSlot("log")
  251.    end
  252.  
  253. end
  254.  
  255. local function CalcFuelAndSmeltAmts()
  256.      
  257.       local logCnt = mta.itemInventoryCount("log")
  258.  
  259.       smeltCnt = round((logCnt/5) * 3)
  260.             smeltLoaded = 0
  261. end
  262.  
  263. local function getOwnerName()
  264.    local name = nil
  265.   --if owner.name file present then extract owner name
  266.    if fs.exists("owner.name") then
  267.       --open and read for user name
  268.       local f = fs.open("owner.name","r")
  269.       if f then
  270.          name = f.readLine()
  271.       end
  272.       f.close()
  273.    end
  274.    
  275.    return name
  276. end
  277.  
  278.  
  279. local function sendWirelessMessage(msg, protocol)
  280.    if msg then
  281.       protocal = protocol or giP
  282.    
  283.       if WAR then
  284.         --send message
  285.         rednet.send(WAR, msg, protocol)
  286.       end
  287.    end
  288. end
  289.  
  290. --*********************MAIN PROGRAM CODE***************
  291. local action = ""
  292.  
  293. --see colCnt parameter at top of file, this sets how many columns to travel
  294.  
  295. print("TreeFarm requires 26X26 area")
  296. print("Plants/harvests 20 trees")
  297. print("Place coal and saplings in any slot")
  298. sleep(3)
  299.  
  300. if _G["cra"] == false then
  301.    error("CRA API not loaded.")
  302. end
  303.  
  304. if _G["mta"] == false then
  305.    error("MTA API not loaded.")
  306. end
  307.  
  308. owner = getOwnerName()
  309.  
  310. local mSide = cra.findPeripheral("modem") --find modem
  311. if mSide then
  312.    --open modem
  313.    rednet.open(mSide)
  314.    --find id of computer responsible for recieving ore alerts and rebroadcasting to user
  315.    WAR = cra.findWAR()
  316.    mta.setWAR(WAR)
  317. end
  318.  
  319.  
  320. local wSpaces = 0
  321.  
  322. --endless loop
  323. while true do
  324.    --if don't have min number of saplings then wait
  325.    if mta.itemInventoryCount("sapling") < (4*colCnt) then
  326.       print("More saplings needed")
  327.       if owner then
  328.           sendWirelessMessage(tName.." needs saplings.|"..owner,"TurtleDistress")
  329.       end
  330.       while mta.itemInventoryCount("sapling") < (4*colCnt) do
  331.           sleep(4)
  332.       end
  333.    end
  334.    
  335.     --fuel check
  336.    if mta.fuelSelfCoal(800) == false then
  337.       print("Fuel Low!")    
  338.       if owner then
  339.           sendWirelessMessage(tName.." is low on fuel.|"..owner,"TurtleDistress")
  340.       end
  341.       while mta.fuelSelfCoal(800) == false do
  342.          sleep(4)
  343.       end
  344.    end
  345.    
  346.    print("Beginning route")
  347.    fuel = turtle.getFuelLevel()
  348.    print("Fuel: "..tostring(fuel))
  349.    
  350.    for cols = 1, colCnt do
  351.      
  352.      
  353.       for targetSpots = 1, 4 do --how many trees per column; this is hardcoded to 4 assuming a 25x25 tree farm
  354.          if targetSpots == 1 then
  355.             wSpaces = 4
  356.          else
  357.             wSpaces = 5
  358.          end
  359.          for walk = 1, wSpaces do
  360.             turtle.suck()
  361.             mta.forward(1)
  362.          end
  363.          
  364.          if math.fmod(cols,2) ~= 0 then
  365.            turtle.turnRight()
  366.          else
  367.            turtle.turnLeft()
  368.          end
  369.          
  370.          action = determineAction()
  371.          if action == "plant" then
  372.             plantSapling()
  373.          elseif action == "harvest" then
  374.             harvest()
  375.          end
  376.          
  377.           if math.fmod(cols,2) ~= 0 then
  378.             turtle.turnLeft()
  379.           else
  380.             turtle.turnRight()
  381.           end
  382.       end
  383.      
  384.       mta.forward(4)
  385.      
  386.      
  387.      --on last column head towards storage otherwise continue on
  388.      if cols ~= colCnt then
  389.         if math.fmod(cols,2) ~= 0 then
  390.            turtle.turnRight()
  391.            mta.forward(5)
  392.            turtle.turnRight()
  393.         else
  394.            turtle.turnLeft()
  395.                mta.forward(5)
  396.            turtle.turnLeft()
  397.         end
  398.      else
  399.        turtle.turnRight()
  400.        mta.forward(13)
  401.      end
  402.    end
  403.    
  404.    --made a circuit of the field now drop off anything picked up and top off supplies
  405.    --should be at first storage chest which is anything not sapling or wood
  406.    turtle.turnLeft()
  407.    dropOffOther()
  408.    
  409.    --go to sapling storage
  410.    turtle.turnRight()
  411.    mta.forward(2)
  412.    turtle.turnLeft()
  413.    dropOffSaplings()
  414.    
  415.    
  416.    CalcFuelAndSmeltAmts()
  417.    
  418.    --drop off 3/5 of wood for smelt slot of furnace
  419.    turtle.turnRight()
  420.    mta.up(4)  
  421.    loadSmeltSlot()
  422.    
  423.    --and the rest for fuel slot of furnace
  424.    turtle.turnRight()
  425.    mta.forward()
  426.    mta.down()
  427.    turtle.turnLeft()
  428.    loadFuelSlot()
  429.    
  430.    
  431.    --get coal for fuel
  432.    mta.down(3)
  433.    turtle.turnLeft()
  434.    mta.forward()
  435.    turtle.turnRight()
  436.      
  437.    --get fuel if needed
  438.      --while mta.fuelSelfCoal(800) == false do
  439.         --try to retrieve fuel
  440.     --      print("retrieving fuel...")
  441.         --  while turtle.suck() == false do
  442.               --wait for fuel to arrive
  443.         --    sleep(10)
  444.     --      end
  445.     --      print("fuel retrieved")
  446.      --end
  447.      
  448.    --get ready to go again...
  449.    turtle.turnRight()
  450.    --...tomorrow at 6:00 am
  451.    local alarm = os.setAlarm(6)
  452.    local sleepyTime = true
  453.    while sleepyTime do
  454.        local evt, arg = os.pullEvent("alarm")
  455.        if arg == alarm then
  456.            sleepyTime = false
  457.        end
  458.    end
  459.    
  460. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement