TwoThe

OC Mining

Sep 13th, 2014
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. local component = require "component"
  2. local compRobot = assert(component.robot, "Unable to find robot component. Is this a robot?")
  3. local robot = require "robot"
  4. local sides = require "sides"
  5. local robotBase = require "robotBase"
  6. local crafting = require "crafting"
  7. local inventory = assert(component.inventory_controller, "Requires inventory controller upgrade to run!")
  8.  
  9. local ID = { stone = 1 }
  10. local pickaxeByMaterial = { [crafting.ID.cobblestone] = 274, [crafting.ID.diamond] = 278, }
  11. local slotAssigns = { [4] = robotBase.ID.coal, [8] = ID.stone, [12] = crafting.ID.cobblestone, [13] = pickaxeByMaterial[crafting.ID.diamond], [16] = crafting.ID.plank, }
  12.  
  13. function isStone(direction)
  14.   robot.select(8)
  15.   return compRobot.compareTo(direction)
  16. end
  17.  
  18. function getStackInfo(slot)
  19.   return inventory.getStackInSlot(sides.back, slot)
  20. end
  21.  
  22. function getFreeSlot()
  23.   for slot=1,robotBase.getInventorySize() do
  24.     if ((not slotAssigns[slot]) and (robot.count(slot) == 0)) then
  25.       return slot
  26.     end    
  27.   end
  28.   return nil
  29. end
  30.  
  31. function assignSlots()
  32.   local stack
  33.   local assignTo = {}
  34.   for slot, id in pairs(slotAssigns) do
  35.     assignTo[id] = slot
  36.     stack = getStackInfo(slot)
  37.     if (stack and (stack.id ~= id)) then
  38.       local freeSlot = getFreeSlot()
  39.       if (not freeSlot) then
  40.         return false
  41.       else
  42.         robot.select(slot)
  43.         robot.transferTo(freeSlot)
  44.       end
  45.     end
  46.   end
  47.  
  48.   local assignSlot
  49.   for slot=1,robotBase.getInventorySize() do
  50.     stack = getStackInfo(slot)
  51.     if (stack) then
  52.       assignSlot = assignTo[stack.id]
  53.       if (assignSlot) then
  54.         robot.select(slot)
  55.         robot.transferTo(assignSlot, robot.space(assignSlot))
  56.       end  
  57.     end
  58.   end
  59.  
  60.   for slot, id in pairs(slotAssigns) do
  61.     if (robot.count(slot) <= 0) then
  62.       return false
  63.     end
  64.   end
  65.  
  66.   return true
  67. end
  68.  
  69. function equipPickaxe(material)
  70.   if (robot.durability()) then
  71.     local freeSlot = getFreeSlot()
  72.     if (not freeSlot) then
  73.       return false, "Not enough free inventory space"
  74.     end
  75.     robot.select(freeSlot)
  76.     inventory.equip()
  77.   end
  78.   local slotWithPickaxe = crafting.findItems(pickaxeByMaterial[material], 1)
  79.   if (not slotWithPickaxe) then
  80.     if (not crafting.craftPickaxe(material)) then
  81.       return false, "Unable to craft a pickaxe"
  82.     end
  83.     slotWithPickaxe = crafting.findItems(pickaxeByMaterial[material], 1)
  84.     if (not slotWithPickaxe) then
  85.       return false, "Crafted pickaxe vanished"
  86.     end
  87.   end
  88.   robot.select(slotWithPickaxe)
  89.   return inventory.equip()  
  90. end
  91.  
  92. function initialize()
  93.   print("Checking fuel...")
  94.   if (not robotBase.checkFuel()) then
  95.     print("Unable to find fuel, please add some coal.")
  96.     return
  97.   end
  98.   print("Fuel found.")
  99.   print("Sorting inventory...")
  100.   if (not assignSlots()) then
  101.     print("Either not enough free inventory space left, or missing one of coal, cobblestone, planks , (clean) stone or diamond pickaxe.")
  102.     return
  103.   end
  104.   print("Inventory sorted.")
  105.   print("Grabbing me a pickaxe...")
  106.   local success, errorMessage = equipPickaxe(crafting.ID.cobblestone)
  107.   if (not success) then
  108.     print(errorMessage)
  109.     return false
  110.   end
  111.   print("Pickaxe equipped, let's go!")
  112. end
  113.  
  114. initialize()
Advertisement
Add Comment
Please, Sign In to add comment