schwiti100

BasicTaskModule

Jul 25th, 2021 (edited)
790
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ---@field ItemNames table
  2. ItemNames = {
  3.     torch = "minecraft:torch",
  4.     cobble = "minecraft:cobblestone",
  5.     chest = "minecraft:chest",
  6.     lavaBucket = "lava_bucket",
  7.     lava = "lava"
  8. }
  9.  
  10. ItemTags = {
  11.     logs = "minecraft:logs",
  12.     saplings = "minecraft:saplings"
  13. }
  14.  
  15.  
  16. ---@class TaskModule
  17. TaskModule = Class()
  18.  
  19. ---@param states table
  20. function TaskModule:initStates(states)
  21.     if self.states == nil then self.states = {} end
  22.     for key,state in pairs(states) do
  23.         self.states[key] = {name = tostring(key), properties = state}
  24.     end
  25. end
  26.  
  27. ---@param newState table
  28. function TaskModule:changeState(newState)
  29.     print(("Last state = %s, new State = %s"):format(self.state.name,newState.name))
  30.     self.state = newState
  31. end
  32.  
  33. ---@param ... table
  34. function TaskModule:debug(...)
  35.     print(string.format(...))
  36. end
  37.  
  38. ---@param ... table
  39. function TaskModule:debugSparse(...)
  40.     if self.updateLoopIx % self.debugTicks == 0 then
  41.         self:debug(...)
  42.     end
  43. end
  44.  
  45.  
  46. ---@param item string
  47. function TaskModule:fillGround(item)
  48.     if item and not turtle.detectDown() then
  49.         if self:selectItemSlot(item) then
  50.             turtle.placeDown()
  51.         end
  52.     end
  53. end
  54.  
  55. ---@param fillGroundItem string
  56. function TaskModule:moveForward(fillGroundItem)
  57.     self:fillGround(fillGroundItem)
  58.    
  59.     while not turtle.forward() do
  60.         turtle.dig()
  61.     end
  62. end
  63.  
  64. ---@param fillGroundItem string
  65. function TaskModule:moveForwardAndDigUp(fillGroundItem)
  66.     turtle.digUp()
  67.     self:moveForward(fillGroundItem)
  68. end
  69.  
  70. ---@param fillGroundItem string
  71. function TaskModule:moveDown(fillGroundItem)
  72.     while not turtle.down() do
  73.         turtle.digDown()
  74.     end
  75.     self:fillGround(fillGroundItem)
  76. end
  77.  
  78. function TaskModule:moveUp()
  79.     while not turtle.up() do
  80.         turtle.digUp()
  81.     end
  82. end
  83.  
  84. ---@param fillGroundItem string
  85. function TaskModule:moveBack(fillGroundItem)
  86.     self:fillGround(fillGroundItem)
  87.  
  88.     if turtle.back() then
  89.         return
  90.     end
  91.     turtle.turnLeft()
  92.     turtle.turnLeft()
  93.     turtle.dig()
  94.     turtle.turnLeft()
  95.     turtle.turnLeft()
  96. end
  97.  
  98. ---@param fillGroundItem string
  99. function TaskModule:moveLeft(fillGroundItem)
  100.     turtle.turnLeft()
  101.     self:moveForward(fillGroundItem)
  102. end
  103.  
  104. ---@param fillGroundItem string
  105. function TaskModule:moveRight(fillGroundItem)
  106.     turtle.turnRight()
  107.     self:moveForward(fillGroundItem)
  108. end
  109.  
  110. function TaskModule:place()
  111.     while not turtle.place() do
  112.         turtle.dig()
  113.     end
  114. end
  115.  
  116. ---@param itemName string
  117. ---@return boolean Item placed ?
  118. function TaskModule:placeItem(itemName)
  119.     if self:selectItemSlot(itemName) then
  120.         self:place()
  121.         return true
  122.     end
  123.     return false
  124. end
  125.  
  126. ---@return boolean Chest placed ?
  127. function TaskModule:placeChest()
  128.     self:moveForward(ItemNames.cobble)
  129.     turtle.digUp()
  130.     turtle.back()
  131.     return self:placeItem(ItemNames.chest)
  132. end
  133.  
  134. function TaskModule:rotate180()
  135.     turtle.turnRight()
  136.     turtle.turnRight()
  137. end
  138.  
  139. ---@param itemName string
  140. ---@return boolean Item selected
  141. function TaskModule:selectItemSlot(itemName)
  142.     local ix = 0
  143.     for i=1,16 do
  144.         local detail = turtle.getItemDetail(i)
  145.         if detail and detail.name == itemName then
  146.             ix = i
  147.             break
  148.         end
  149.     end
  150.     if ix>0 then
  151.         turtle.select(ix)
  152.         return true
  153.     end
  154.     print(("No item with name %s found."):format(itemName))
  155.     return false
  156. end
  157.  
  158. ---@param tagName string
  159. function TaskModule:selectItemSlotByTag(tagName)
  160.     local ix = 0
  161.     for i=1,16 do
  162.         local detail = turtle.getItemDetail(i,true)
  163.         if detail and detail.tags and detail.tags[tagName] then
  164.             ix = i
  165.             break
  166.         end
  167.     end
  168.     if ix>0 then
  169.         turtle.select(ix)
  170.         return true
  171.     end
  172.     print(("No item with tag %s found."):format(tagName))
  173.     return false
  174. end
  175.  
  176. ---@return boolean Torch selected
  177. function TaskModule:selectTorchSlot()
  178.     return self:selectItemSlot(ItemNames.torch)
  179. end
  180.  
  181. ---@return boolean Cobble selected
  182. function TaskModule:selectCobbleSlot()
  183.     return self:selectItemSlot(ItemNames.cobble)
  184. end
  185.  
  186. function TaskModule:placeTorchRight()
  187.     turtle.turnRight()
  188.     if self:selectTorchSlot() then
  189.         self:place()
  190.     end
  191.     turtle.turnLeft()
  192. end
  193.  
  194. ---@param ix number
  195. function TaskModule:dropSlot(ix)
  196.     turtle.select(ix)
  197.     turtle.drop()
  198. end
  199.  
  200. ---@param itemName string
  201. function TaskModule:dropItem(itemName)
  202.     local hasDropped = false
  203.     for i=1,16 do
  204.         local itemDetail = turtle.getItemDetail(i)
  205.         if itemDetail and itemDetail.name == itemName then
  206.             turtle.select(i)
  207.             hasDropped = turtle.drop()
  208.         end
  209.     end
  210.     return hasDropped
  211. end
  212.  
  213. ---@param itemTag string
  214. function TaskModule:dropItemByTag(itemTag)
  215.     local hasDropped = false
  216.     for i=1,16 do
  217.         local itemDetail = turtle.getItemDetail(i,true)
  218.         if itemDetail and itemDetail.tags and itemDetail.tags[itemTag] then
  219.             turtle.select(i)
  220.             hasDropped = turtle.drop()
  221.         end
  222.     end
  223.     return hasDropped
  224. end
  225.  
  226. ---@return boolean Fuel level okay ?
  227. function TaskModule:isFuelLevelOkay()
  228.     return turtle.getFuelLevel() >= turtle.getFuelLimit()/4
  229. end
  230.  
  231. ---@return boolean Inventory full ?
  232. function TaskModule:isInventoryFull()
  233.     return turtle.getItemCount(16) > 1
  234. end
  235.  
  236. function TaskModule:refuel()
  237.     while(not self:isFuelLevelOkay()) do
  238.         for i=1,16 do
  239.             turtle.select(i)
  240.             local oldFillLevel = turtle.getFuelLevel()
  241.             local ok, err = turtle.refuel()
  242.             if ok then
  243.                 local newFillLevel = turtle.getFuelLevel()
  244.                 print(("Refuelled %d, current level is %d"):format(newFillLevel - oldFillLevel, newFillLevel))
  245.             end
  246.         end
  247.     end
  248. end
  249.  
  250. function TaskModule:createStairCaseStepDown()
  251.     self:moveDown(ItemNames.cobble)
  252.     self:moveForward()
  253.     turtle.digUp()
  254. end
  255.  
  256. function TaskModule:createStairCaseDown()
  257.     local blocksMoved = 0
  258.     local torchCounter = 4
  259.     while true do
  260.         if not self:isFuelLevelOkay() then
  261.             return
  262.         end
  263.         self:createStairCaseStepDown()
  264.         blocksMoved = blocksMoved + 1
  265.         if blocksMoved % torchCounter == 0 then
  266.             self:placeTorchRight()
  267.         end
  268.     end
  269. end
  270.  
RAW Paste Data