Advertisement
jacob4408

CC CropFarmer

Nov 29th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.63 KB | None | 0 0
  1. --Farmer needs a 10x10 prepared plot and storage chests structure
  2.  
  3. local function isItemSlot(slotToCheck,itemType,exactMatch)
  4. --slotToCheck: numeric, slot to examine
  5. --itemType: string, base item type example "log","coal","sapling"
  6. --[exactMatch]: boolean, item type must be exact, i.e. "minecraft:wheat"
  7.      local rc = false
  8.      if exactMatch == nil then exactMatch = false end
  9.      
  10.    blockInfo = turtle.getItemDetail(slotToCheck)
  11.    if blockInfo then
  12.       --if item then return true
  13.       if exactMatch == false then
  14.          if string.find(blockInfo.name,itemType) ~= nil then
  15.             rc = true
  16.          end
  17.       else
  18.          if blockInfo.name == itemType then
  19.             rc = true
  20.          end
  21.       end
  22.    end
  23.    
  24.    return rc
  25. end
  26.  
  27. local function getItemSlot(itemType, exactMatch)
  28. --returns slot with item or -1 if no item found
  29. --itemType: string, type of item example"log","coal","sapling"
  30. --[exactMatch]: boolean, item type must be exact, i.e. "minecraft:wheat"
  31. if exactMatch == nil then exactMatch = false end
  32.  
  33.     for x = 1,16 do
  34.        if isItemSlot(x,itemType,exactMatch) then
  35.          return x
  36.        end
  37.     end
  38.     return -1
  39. end
  40.  
  41. local function itemInventoryCount(itemType)
  42.   local total = 0
  43.   local current = 0
  44.     for slot = 1, 16 do
  45.      current = turtle.getItemCount(slot)
  46.      if current > 0 then
  47.         if isItemSlot(slot,itemType) then
  48.              total = total + current
  49.           end
  50.        end
  51.   end
  52.  
  53.   return total
  54. end
  55.  
  56.  
  57. local function dropExcessSeeds()
  58.    
  59. local overage = 0    
  60. local dropAmt = 0
  61. local x = 0
  62. local seedCnt = 0
  63.        
  64.    --try to load
  65.    seedCnt = itemInventoryCount("seeds")
  66.    while seedCnt > 120 do
  67.       overage = seedCnt - 120
  68.       x = getItemSlot("seeds")
  69.       if x == -1 then
  70.         --can't find any so how is count > 180 ????
  71.       else
  72.          turtle.select(x)
  73.          slotAmt = turtle.getItemCount(x)
  74.          if (overage) >= slotAmt then
  75.             dropAmt = slotAmt
  76.          else
  77.             dropAmt = overage
  78.          end
  79.          turtle.drop(dropAmt)
  80.       end
  81.       seedCnt = seedCnt - dropAmt
  82.    end
  83. end
  84.  
  85. local function fuelSelf(minFuel)
  86.      --min fuel is number representing the minimum amount of fuel we want turtle to have at start of this process
  87.    local rc = false
  88.  
  89.    if turtle.getFuelLevel() < minFuel then
  90.       --only burn coal
  91.       x = getItemSlot("coal")
  92.       if x ~= -1 then
  93.          turtle.select(x)
  94.          if turtle.refuel() then
  95.             rc = true
  96.          end
  97.       end
  98.    else
  99.       --fuel above minimum so return true
  100.       rc = true
  101.    end
  102.    return rc
  103. end
  104.  
  105.  
  106. --*********************MAIN PROGRAM CODE***************
  107.  
  108. local action = ""
  109.  
  110. --see colCnt parameter at top of file, this sets how many columns to travel
  111.  
  112. print("CropFarm requires 10X10 area of dirt and either flax or wheat seeds")
  113. print("Place coal and seeds in any slot")
  114. sleep(3)
  115.  
  116.  
  117. if _G["mta"] == false then
  118.    error("MTA API not loaded.")
  119. end
  120.  
  121.  
  122. local success = false
  123. local info
  124. local xSlot
  125.  
  126. while true do  
  127.    
  128.      if fuelSelf(200) == false then
  129.       print("Low fuel level!")    
  130.       while fuelSelf(200) == false do
  131.          sleep(4)
  132.       end
  133.    end
  134.    
  135.    print("Fuel: "..tostring(turtle.getFuelLevel()))
  136.    
  137.    for cols = 1,10 do
  138.      
  139.       if cols == 1 then
  140.         mta.forward()
  141.       end
  142.      
  143.       for rows = 1,10 do
  144.          success, info = turtle.inspectDown()
  145.          if success then
  146.             --if wheat or flax then is it ready to harvest
  147.             if (info.name == "minecraft:wheat") or (info.name == "yegamolchattels:flax_plant") then
  148.                if info.metadata == 7 then
  149.                   --harvest it
  150.                   turtle.digDown()
  151.                   local seedSlot = getItemSlot("seeds")
  152.                   if seedSlot > -1 then
  153.                      turtle.select(seedSlot)
  154.                      turtle.placeDown()
  155.                   end
  156.                end
  157.             else
  158.                --not sure what to do here?
  159.             end
  160.          else
  161.             --nothing there so till and plant
  162.             turtle.digDown()
  163.             local seedSlot = getItemSlot("seeds")
  164.             if seedSlot > -1 then
  165.                turtle.select(seedSlot)
  166.                turtle.placeDown()
  167.             end
  168.          end
  169.          if rows < 10 then
  170.             mta.forward()
  171.          end
  172.       end
  173.      
  174.       --on last column head towards storage otherwise continue on
  175.       if cols ~= 10 then
  176.          if math.fmod(cols,2) ~= 0 then
  177.             turtle.turnRight()
  178.             mta.forward()
  179.             turtle.turnRight()
  180.          else
  181.             turtle.turnLeft()
  182.                    mta.forward()
  183.             turtle.turnLeft()
  184.          end
  185.       else
  186.         mta.forward(1)
  187.         turtle.turnRight()
  188.         mta.forward(9)
  189.       end
  190.    end
  191.     --drop anything over 120 seeds
  192.       turtle.turnLeft()
  193.       dropExcessSeeds()
  194.      
  195.       --drop all wheat into chest
  196.       turtle.turnRight()
  197.       xSlot = getItemSlot("minecraft:wheat", true)
  198.       while  xSlot > -1 do
  199.          turtle.select(xSlot)
  200.          turtle.drop()
  201.         xSlot = getItemSlot("minecraft:wheat", true)
  202.       end
  203.      
  204.       --drop all flax fiber
  205.       xSlot = getItemSlot(":flax_fiber")
  206.       while  xSlot > -1 do
  207.          turtle.select(xSlot)
  208.          turtle.drop()
  209.         xSlot = getItemSlot("flax_fiber")
  210.       end
  211.      
  212.       turtle.turnRight()
  213.      
  214.       --get ready to do it all again at 6 am
  215.       local alarm = os.setAlarm(6)
  216.       local sleepyTime = true
  217.       while sleepyTime do
  218.          local evt, arg = os.pullEvent("alarm")
  219.          if arg == alarm then
  220.            sleepyTime = false
  221.          end
  222.       end
  223. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement