Advertisement
TwoThe

OC Crafting

Sep 12th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.95 KB | None | 0 0
  1. local component = require "component"
  2. local robot = require "robot"
  3. local sides = require "sides"
  4. local inventory = assert(component.inventory_controller, "Requires inventory controller upgrade to run!")
  5. local crafting = assert(component.crafting, "Requires crafting upgrade to run!")
  6.  
  7. local class = {}
  8. class.ID = { stick = 280, plank = 5, log = 17, cobblestone = 4, ironIngot = 265, diamond = 264, }
  9. class.SLOTS_CRAFTING = {[1]=1, [2]=2, [3]=3, [5]=5, [6]=6, [7]=7, [9]=9, [10]=10, [11]=11}
  10.  
  11. function class.getFreeNonCraftingSlot()
  12.   for slot=1,inventory.getInventorySize(sides.back) do
  13.     if (not class.SLOTS_CRAFTING[slot]) then
  14.       if (robot.count(slot) == 0) then
  15.         return slot
  16.       end
  17.     end
  18.   end
  19.   return nil
  20. end
  21.  
  22. function class.clearCraftingGrid()
  23.   local freeSlot
  24.   for slot in pairs(class.SLOTS_CRAFTING) do
  25.     if (robot.count(slot) > 0) then
  26.       freeSlot = class.getFreeNonCraftingSlot()
  27.       if (freeSlot) then
  28.         robot.select(slot)
  29.         if (not robot.transferTo(freeSlot)) then
  30.           return false
  31.         end
  32.       else
  33.         return false
  34.       end
  35.     end
  36.   end
  37.   return true
  38. end
  39.  
  40. function class.craftPlanks(count)
  41.   if (count % 4 > 0) then
  42.     count = count + 4 - (count % 4)
  43.   end
  44.   if (not class.clearCraftingGrid()) then
  45.     print("Unable to clear crafting area, please make some room")
  46.     return false
  47.   end
  48.   local logCount = math.floor(count / 4)
  49.   local slotLogs = class.requireItem(class.ID.log, logCount)
  50.   if (slotLogs) then
  51.     robot.select(slotLogs)
  52.     if (robot.transferTo(1, logCount)) then
  53.       return crafting.craft(count)
  54.     end
  55.   end  
  56. end
  57.  
  58. function class.craftSticks(count)
  59.   if (count % 4 > 0) then
  60.     count = count + 4 - (count % 4)
  61.   end
  62.   local woodCount = math.floor(count / 2 + 0.5)
  63.   local slotWood = class.requireItem(class.ID.plank, woodCount)
  64.   if (not slotWood) then
  65.     if (not class.craftPlanks(woodCount)) then
  66.       return false
  67.     end
  68.   end
  69.   if (not class.clearCraftingGrid()) then
  70.     print("Unable to clear crafting area, please make some room")
  71.     return false
  72.   end
  73.   slotWood = class.requireItem(class.ID.plank, woodCount)
  74.   if (slotWood) then
  75.     robot.select(slotWood)
  76.     if (robot.transferTo(1, 1) and robot.transferTo(5, 1)) then
  77.       return crafting.craft(count)
  78.     end  
  79.   end
  80. end
  81.  
  82. function class.findItems(itemID, count)
  83.   local stack
  84.   for slot=1,inventory.getInventorySize(sides.back) do
  85.     stack = inventory.getStackInSlot(sides.back, slot)
  86.     if (stack and (stack.id == itemID) and (stack.size >= count)) then
  87.       return slot
  88.     end
  89.   end
  90.   return nil
  91. end
  92.  
  93. function class.requireItem(itemID, count)
  94.   local slot = class.findItems(itemID, count)
  95.   if ((not slot) and (itemID == class.ID.stick)) then
  96.     if (class.craftSticks(count)) then
  97.       return class.findItems(itemID, count)
  98.     end
  99.   else
  100.     return slot
  101.   end
  102.   return nil
  103. end
  104.  
  105. function class.craftPickaxe(material)
  106.   if (not class.clearCraftingGrid()) then
  107.     print("Unable to clear crafting area, please make some room")
  108.     return false
  109.   end
  110.   local slotStick = class.requireItem(class.ID.stick, 2)
  111.   if (slotStick) then
  112.     local slotMaterial = class.requireItem(material, 3)
  113.     if (slotMaterial) then
  114.       robot.select(slotStick)
  115.       if (robot.transferTo(6, 1) and robot.transferTo(10, 1)) then
  116.         robot.select(slotMaterial)
  117.         if (robot.transferTo(1, 1) and robot.transferTo(2, 1) and robot.transferTo(3, 1)) then
  118.           return crafting.craft()
  119.         end
  120.       end
  121.     else
  122.       print("Head material for pickaxe not found.")
  123.     end
  124.   else
  125.     print("No sticks found, also no wood to make them.")
  126.   end
  127.   return false
  128. end
  129.  
  130. function class.craftStonePickaxe()
  131.   return class.craftPickaxe(class.ID.cobblestone)
  132. end
  133.  
  134. function class.craftIronPickaxe()
  135.   return class.craftPickaxe(class.ID.ironIngot)
  136. end
  137.  
  138. function class.craftDiamondPickaxe()
  139.   return class.craftPickaxe(class.ID.diamond)
  140. end
  141.  
  142. return class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement