schwiti100

TurtleTreeCutting

Jul 26th, 2021 (edited)
228
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require("Class")
  2. require("BasicTaskModule")
  3.  
  4. ---@class TreeCuttingTaskModule : TaskModule
  5. TreeCuttingTaskModule = Class(TaskModule)
  6. TreeCuttingTaskModule.states = {
  7.     PLANTING = {},
  8.     CUTTING = {},
  9.     WAITING = {},
  10.     UNLOADING = {},
  11.     IDLE = {}
  12. }
  13. TreeCuttingTaskModule.WAIT_TIME = 60
  14. function TreeCuttingTaskModule:init()
  15.     self:initStates(TreeCuttingTaskModule.states)
  16.     self.state = self.states.IDLE
  17.     self.waitingIx = 0
  18.     if self:handleStart() then
  19.         while true do
  20.             if self:isWaiting() then
  21.                 self:handleWaiting()
  22.             elseif self:isPlanting() then
  23.                 if not self:handlePlating() then
  24.                     self:debug("Error: no more saplings.")
  25.                     break
  26.                 end
  27.             elseif self:isCutting() then
  28.                 self:handleCutting()
  29.             else
  30.                 if not self:handleUnloading() then
  31.                     self:debug("Error: no more free space in the chest.")
  32.                     break
  33.                 end
  34.             end
  35.         end
  36.     end
  37. end
  38.  
  39. function TreeCuttingTaskModule:isWaiting()
  40.     return self.state == self.states.WAITING
  41. end
  42.  
  43. function TreeCuttingTaskModule:isCutting()
  44.     return self.state == self.states.CUTTING
  45. end
  46.  
  47. function TreeCuttingTaskModule:isPlanting()
  48.     return self.state == self.states.PLANTING
  49. end
  50.  
  51. function TreeCuttingTaskModule:isUnloading()
  52.     return self.state == self.states.UNLOADING
  53. end
  54.  
  55. function TreeCuttingTaskModule:handleStart()
  56.     local hasBlock,itemDetails = turtle.inspect()
  57.     if hasBlock then
  58.         if self:isLog(itemDetails) then
  59.             self:changeState(self.states.CUTTING)
  60.         elseif self:isSapling(itemDetails) then
  61.             self:changeState(self.states.WAITING)
  62.         else
  63.             self:debug("Error: invalid block in front.")
  64.             return false
  65.         end
  66.     else
  67.         self:changeState(self.states.PLANTING)
  68.     end
  69.     self:rotate180()
  70.     if not self:placeChest() then
  71.         self:rotate180()
  72.         self:debug("Error: no chest in inventory.")
  73.         return false
  74.     end
  75.     self:rotate180()
  76.     return true
  77. end
  78.  
  79. function TreeCuttingTaskModule:handleCutting()
  80.     self:moveForward()
  81.     local ix = 0
  82.     while true do
  83.         ix = ix + 1
  84.         self:moveUp()
  85.         local hasBlock,itemDetails = turtle.inspectUp()
  86.         if not self:isLog(itemDetails) then
  87.             break
  88.         end
  89.     end
  90.     for i=1,ix do
  91.         self:moveDown()
  92.     end
  93.     self:moveBack()
  94.     self:changeState(self.states.UNLOADING)
  95. end
  96.  
  97. ---@param itemDetails table
  98. function TreeCuttingTaskModule:isLog(itemDetails)
  99.     return itemDetails and itemDetails.tags and itemDetails.tags[ItemTags.logs]
  100. end
  101.  
  102. ---@param itemDetails table
  103. function TreeCuttingTaskModule:isSapling(itemDetails)
  104.     return itemDetails and itemDetails.tags and itemDetails.tags[ItemTags.saplings]
  105. end
  106.  
  107. function TreeCuttingTaskModule:handlePlating()
  108.     if self:selectItemSlotByTag(ItemTags.saplings) then
  109.         self:place()
  110.         self:changeState(self.states.WAITING)
  111.     else
  112.         return false
  113.     end
  114.     return true
  115. end
  116.  
  117. function TreeCuttingTaskModule:handleUnloading()
  118.     local itemsDropped = false
  119.     self:rotate180()
  120.     itemsDropped = self:dropItemByTag(ItemTags.logs)
  121.     self:rotate180()
  122.     self:changeState(self.states.PLANTING)
  123.     return itemsDropped
  124. end
  125.  
  126. function TreeCuttingTaskModule:handleWaiting()
  127.     local hasBlock,itemDetails = turtle.inspect()
  128.     if not hasBlock then
  129.         self:changeState(self.states.PLANTING)
  130.         self.waitingIx = 0
  131.     else
  132.         if self:isLog(itemDetails) then
  133.             self:changeState(self.states.CUTTING)
  134.             self.waitingIx = 0
  135.         else
  136.             sleep(self.WAIT_TIME)
  137.             self.waitingIx = self.waitingIx + 1
  138.             self:debug("Waiting iteration: %d",self.waitingIx)
  139.         end
  140.     end
  141. end
  142. TreeCuttingTaskModule()
RAW Paste Data