Advertisement
Guest User

startup

a guest
Jul 23rd, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.40 KB | None | 0 0
  1. local coalSide = "left"
  2. local saplingSide = "right"
  3.  
  4. -- More Settings
  5. local fuelSlot = 15
  6. local saplingSlot = 16
  7. local travelDistance = 16
  8. local dirtQuantity = 4
  9.  
  10. -- Directionality Stuff
  11. local facing = 2
  12.  
  13. local function getFacing(f)
  14.     if f == nil then f = facing end
  15.  
  16.     if f == 0 then return "north" end
  17.     if f == 1 then return "east" end
  18.     if f == 2 then return "south" end
  19.     return "west"
  20. end
  21.  
  22. local function getDirectionForSide(side)
  23.     local offset = 0
  24.     if side == "right" then offset = 1 end
  25.     if side == "back" then offset = 2 end
  26.     if side == "left" then offset = 3 end
  27.  
  28.     local f = facing + offset
  29.     if f > 4 then f = f - 4 end
  30.     return getFacing(f)
  31. end
  32.  
  33. local function turnLeft(count)
  34.     if count == nil then count = 1 end
  35.     for i=1,count do
  36.         if turtle.turnLeft() then
  37.             facing = facing - 1
  38.             if facing < 1 then facing = facing + 4 end
  39.         end
  40.     end
  41.     return facing
  42. end
  43.  
  44. local function turnRight(count)
  45.     if count == nil then count = 1 end
  46.     for i=1,count do
  47.         if turtle.turnRight() then
  48.             facing = facing + 1
  49.             if facing > 4 then facing = facing - 4 end
  50.         end
  51.     end
  52.     return facing
  53. end
  54.  
  55. local function forward(count)
  56.     if count == nil then count = 1 end
  57.     for i=1,count do
  58.         if not turtle.forward() then
  59.             error("Could not move.")
  60.         end
  61.     end
  62. end
  63.  
  64. local function up(count)
  65.     if count == nil then count = 1 end
  66.     for i=1,count do
  67.         if not turtle.up() then
  68.             error("Could not move.")
  69.         end
  70.     end
  71. end
  72.  
  73. local function down(count)
  74.     if count == nil then count = 1 end
  75.     for i=1,count do
  76.         if not turtle.down() then
  77.             error("Could not move.")
  78.         end
  79.     end
  80. end
  81.  
  82. local function getOpposite(side)
  83.     if side == "north" then return "south" end
  84.     if side == "east" then return "west" end
  85.     if side == "south" then return "north" end
  86.     if side == "west" then return "east" end
  87.     if side == "up" then return "down" end
  88.     return "up"
  89. end
  90.  
  91. local select = turtle.select
  92. local placeDown = turtle.placeDown
  93.  
  94. -- Inventory Management
  95.  
  96. local function getFromContainer(side, slot, quantity)
  97.     peripheral.call(side, "pushItem", getOpposite(getDirectionForSide(side)), 1, quantity, slot)
  98. end
  99.  
  100. local function getFuel()
  101.     print("Checking fuel...")
  102.     if turtle.getFuelLevel() < travelDistance then
  103.         print("Refueling.")
  104.         getFromContainer(coalSide, fuelSlot, 1)
  105.         select(fuelSlot)
  106.         turtle.refuel(1)
  107.         select(1)
  108.     end
  109. end
  110.  
  111. local function getSapling()
  112.     print("Checking saplings...")
  113.     local count = turtle.getItemCount(saplingSlot)
  114.     if count < 4 then
  115.         print("Getting saplings.")
  116.         getFromContainer(saplingSide, saplingSlot, 4-count)
  117.     end
  118. end
  119.  
  120. -- Plant Saplings
  121. local function plant()
  122.     print("Planting...")
  123.  
  124.     turnRight(2)
  125.     forward(4)
  126.  
  127.     -- Place Dirt
  128.     select(1)
  129.     placeDown()
  130.     forward()
  131.     placeDown()
  132.     turnRight()
  133.     forward()
  134.     placeDown()
  135.     turnRight()
  136.     forward()
  137.     placeDown()
  138.  
  139.     -- Place Saplings
  140.     select(saplingSlot)
  141.     turnRight(2)
  142.     up()
  143.     placeDown()
  144.     forward()
  145.     placeDown()
  146.     turnLeft()
  147.     forward()
  148.     placeDown()
  149.     turnLeft()
  150.     forward()
  151.     placeDown()
  152.  
  153.     -- Return to Start
  154.     forward(4)
  155.     down()
  156.     select(1)
  157. end
  158.  
  159. -- The Loop
  160. print("Getting started...")
  161. while true do
  162.     print("Waiting for inventory event...")
  163.     local event, p1 = os.pullEvent("turtle_inventory")
  164.     if turtle.getItemCount(1) == dirtQuantity then
  165.         print("Found a tree! Sleeping...")
  166.  
  167.         -- Time to Harvest
  168.         sleep(15)
  169.  
  170.         -- Now, Work
  171.         getFuel()
  172.         getSapling()
  173.         plant()
  174.     else
  175.         print("Not what we wanted.")
  176.     end
  177. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement