Advertisement
hevohevo

ComputerCraft Tutorial: woodcutter_0_2

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