Advertisement
Rot256

PingPongHarvest

Nov 3rd, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.63 KB | None | 0 0
  1. -- Static variables (Settings)
  2.  
  3. safety = {"minecraft:wooden_slab"}
  4. turnLeft = {"minecraft:planks"}
  5. turnRight = {"minecraft:double_wooden_slab"}
  6. chest = {"minecraft:chest", "EnderStorage:enderChest"}
  7.  
  8. inventory_size = 16
  9. fuel_level = 20000
  10.  
  11. -- Crops (name of planted blocks) and seeds (name of items)
  12.  
  13. seeds = {"minecraft:potato", "minecraft:wheat_seeds", "minecraft:carrot"}
  14. crops = {"minecraft:potatoes", "minecraft:wheat", "minecraft:carrots"}
  15. distribution = {0.5, 0.5, 0.0}
  16. max_metalevel = {7, 7, 7}
  17. cropslots = 3
  18. delay = 1.5
  19.  
  20. -- Functions
  21.  
  22. function isType(e, t)
  23.     if e == nil then
  24.         return false
  25.     end
  26.     for i = 1, #t, 1 do
  27.         if(e.name == t[i]) then
  28.             return i
  29.         end
  30.     end
  31.     return false
  32. end
  33.  
  34. function findCrop(crop)
  35.     for i = 1, inventory_size, 1 do
  36.         item = turtle.getItemDetail(i)
  37.         if isType(item, seeds) and (crop == "any" or item.name == crop) then
  38.             return i
  39.         end
  40.     end
  41.     return nil
  42. end
  43.  
  44. function unload(crop)
  45.     -- Compute max of each type
  46.     remCrops = {}
  47.     lastSlot = {}
  48.     for i = 1, #distribution, 1 do
  49.         remCrops[seeds[i]] = math.ceil(cropslots * distribution[i]) * 64
  50.     end
  51.  
  52.     -- Filter inventory
  53.     for i = 1, inventory_size, 1 do
  54.         item = turtle.getItemDetail(i)
  55.         if isType(item, seeds) and remCrops[item.name] > 0 then
  56.             if lastSlot[item.name] == nil then
  57.                 lastSlot[item.name] = i
  58.                 remCrops[item.name] = remCrops[item.name] - item.count
  59.             else
  60.                 toMove = math.min(item.count, turtle.getItemSpace(lastSlot[item.name]))
  61.                 if toMove > 0 then
  62.                     turtle.select(i)
  63.                     remCrops[item.name] = remCrops[item.name] - toMove
  64.                     turtle.transferTo(lastSlot[item.name])
  65.                 end
  66.                 if remCrops[item.name] > 0 and turtle.getItemCount(i) > 0 then
  67.                     lastSlot[item.name] = i
  68.                     remCrops[item.name] = remCrops[item.name] - turtle.getItemCount(i)
  69.                 end
  70.             end
  71.         elseif item ~= nil then
  72.             turtle.select(i)
  73.             turtle.dropDown()
  74.         end
  75.     end
  76. end
  77.  
  78. function refuel()
  79.     turtle.select(inventory_size)
  80.     turtle.drop()
  81.     while turtle.getFuelLevel() < fuel_level do
  82.         turtle.suckUp(1)
  83.         turtle.refuel()
  84.     end
  85.     turtle.suck()
  86. end
  87.  
  88. function plantCrop()
  89.     cropId = math.random()
  90.     for i = 1, #seeds, 1 do
  91.         if cropId < distribution[i] then
  92.             slot = findCrop(seeds[i])
  93.             if slot == nil then
  94.                 slot = findCrop("any")
  95.             end
  96.             if slot then
  97.                 turtle.select(slot)
  98.                 return turtle.placeDown()
  99.             end
  100.         else
  101.             cropId = cropId - distribution[i]
  102.         end
  103.     end
  104.     return false
  105. end
  106.  
  107. function checkChests()
  108.     chests_found = false
  109.     -- Unload
  110.     _, block = turtle.inspectDown()
  111.     if isType(block, chest) then
  112.         unload()
  113.         chests_found = true
  114.     end
  115.    
  116.     -- Refuel
  117.     _, block = turtle.inspectUp()
  118.     if isType(block, chest) then
  119.         refuel()
  120.         chests_found = true
  121.     end
  122.     return chests_found
  123. end
  124.  
  125. while 1 do
  126.     os.sleep(delay)
  127.     _, up = turtle.inspectUp()
  128.     _, down = turtle.inspectDown()
  129.     _, front = turtle.inspect()
  130.     cropUnder = isType(down, crops)
  131.     if checkChests() then
  132.         turtle.forward()
  133.     elseif cropUnder then
  134.         if down.metadata == max_metalevel[cropUnder] then
  135.             turtle.digDown()
  136.             plantCrop()
  137.         end
  138.         turtle.forward()
  139.     elseif turtle.digDown() then
  140.         plantCrop()
  141.         turtle.forward()
  142.     elseif plantCrop() then
  143.         turtle.forward()
  144.     elseif isType(up, safety) or isType(down, safety) then
  145.         if isType(front, turnLeft) then
  146.             turtle.turnLeft()
  147.             turtle.forward()
  148.             turtle.turnLeft()
  149.         elseif isType(front, turnRight) then
  150.             turtle.turnRight()
  151.             turtle.forward()
  152.             turtle.turnRight()
  153.         else
  154.             turtle.turnLeft()
  155.         end
  156.         turtle.forward()
  157.     elseif isType(front, turnLeft) then
  158.         turtle.turnLeft()
  159.     elseif isType(front, turnRight) then
  160.         turtle.turnRight()
  161.     else
  162.         turtle.forward()
  163.     end
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement