Advertisement
hevohevo

ComputerCraft Tutorial: woodcutter0_1

Apr 23rd, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.48 KB | None | 0 0
  1. local S_WOOD = 1
  2. local S_CCOAL = 16
  3. local S_SAPLING = 2
  4. local S_DIRT = 15
  5. local SLEEP_TIME = 120
  6.  
  7. local qty_wood = 0
  8. local count_checked = 0
  9.  
  10. function waitForEnoughItems(itemName, n, slot)
  11.   turtle.select(slot)
  12.   while turtle.getItemCount(slot) < n do
  13.     if itemName then print(string.format("Insert %s into slot %d",itemName,slot)) end
  14.     os.sleep(0.5)
  15.     os.pullEvent("turtle_inventory")
  16.   end
  17. end
  18.  
  19. function waitForEnoughFuel(minLevel, slot)
  20.   local qty = turtle.getFuelLevel()
  21.   local refuel = function()
  22.     if qty > minLevel then return qty end
  23.     turtle.select(slot)
  24.     turtle.refuel()
  25.     qty = turtle.getFuelLevel()
  26.     print("fuel: ",qty)
  27.     return qty
  28.   end
  29.  
  30.   while refuel() < minLevel do
  31.     print(string.format("Insert fuel-items into slot %d: %d/%d", slot, qty, minLevel))
  32.     os.sleep(1)
  33.     os.pullEvent("turtle_inventory")
  34.   end
  35. end
  36.  
  37.  
  38. function isWood(direction)
  39.   turtle.select(S_WOOD)
  40.   if direction == "top" then
  41.     return turtle.compareUp()
  42.   else
  43.     return turtle.compare()
  44.   end
  45. end
  46.  
  47. function cutWoods()
  48.   turtle.select(S_WOOD)
  49.   turtle.dig()
  50.   turtle.forward()
  51.  
  52.   while turtle.detectUp() do
  53.     turtle.digUp()
  54.     turtle.up()
  55.   end
  56. end
  57.  
  58. function cutLeaves()
  59.   turtle.select(S_SAPLING)
  60.   while not turtle.detectDown() do
  61.     for i=1,4 do
  62.       turtle.dig()
  63.       turtle.turnRight()
  64.     end
  65.     turtle.down()
  66.   end
  67. end
  68.  
  69. function placeItem(slot, direction)
  70.   turtle.select(slot)
  71.   if direction == "down" then
  72.     turtle.placeDown()
  73.   else
  74.     turtle.place()
  75.   end
  76. end
  77.  
  78. function getSapling()
  79.   turtle.select(S_DIRT)
  80.   turtle.digDown()
  81.   turtle.down()
  82.  
  83.   for i=1,4 do
  84.     turtle.select(S_SAPLING)
  85.     turtle.suck()
  86.     turtle.turnRight()
  87.   end
  88.  
  89.   turtle.up()
  90.   placeItem(S_DIRT, "down")
  91.   turtle.back()
  92.   placeItem(S_SAPLING)
  93. end
  94.  
  95.  
  96. function dropItem(slot, nLeaves)
  97.   turtle.select(slot)
  98.   local n = turtle.getItemCount()
  99.   if n > nLeaves then
  100.     if slot == S_WOOD then
  101.       qty_wood = qty_wood + (n-nLeaves)
  102.       print(qty_wood)
  103.     end
  104.     return turtle.drop(n-nLeaves)
  105.   else
  106.     return false, "No items to drop"
  107.   end
  108. end
  109.  
  110. function fillItemSlot(slot, direction)
  111.   turtle.select(slot)
  112.   local n = turtle.getItemSpace()
  113.   if direction == "down" then
  114.     return turtle.suckDown(n)
  115.   else
  116.     return turtle.suck(n)
  117.   end
  118. end
  119.  
  120. function makeCoal()
  121.   turtle.back()
  122.   turtle.back()
  123.   turtle.back()
  124.   turtle.turnRight()
  125.   turtle.turnRight()
  126.  
  127.   fillItemSlot(S_CCOAL, "down")
  128.   dropItem(S_CCOAL, 2)
  129.   if turtle.getFuelLevel() < 80 then
  130.     turtle.select(S_CCOAL)
  131.     assert(turtle.refuel(1), "FuelLevel is low.")
  132.   end
  133.  
  134.   turtle.up()
  135.   turtle.up()
  136.   assert(dropItem(S_WOOD, 2), "The chest for woods is full.")
  137.  
  138.   turtle.down()
  139.   turtle.down()
  140.   turtle.turnLeft()
  141.   dropItem(S_WOOD, 2)
  142.   dropItem(S_SAPLING, 32)
  143.  
  144.   turtle.turnLeft()
  145.   turtle.forward()
  146.   turtle.forward()
  147.   turtle.forward()
  148. end
  149.  
  150.  
  151.  
  152.  
  153. -- main
  154. waitForEnoughItems("BirchSapling", 2, S_SAPLING)
  155. waitForEnoughItems("BirchWood", 1, S_WOOD)
  156. waitForEnoughItems("Dirt", 2, S_DIRT)
  157. waitForEnoughFuel(80, S_CCOAL)
  158.  
  159. while true do
  160.   local isWood = isWood()
  161.   if isWood or count_checked > 1 then
  162.     print("Start cutting.")
  163.     count_checked = 0
  164.     cutWoods()
  165.     cutLeaves()
  166.     getSapling()
  167.  
  168.     if isWood then makeCoal() end
  169.   else
  170.     count_checked = count_checked+1
  171.     print("It isn't wood.")
  172.   end
  173.   turtle.select(S_SAPLING)
  174.   turtle.suckUp()
  175.   turtle.suckDown()
  176.   turtle.suck()
  177.   os.sleep(SLEEP_TIME)
  178. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement