Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require "component"
- local robot = require "robot"
- local sides = require "sides"
- local inventory = assert(component.inventory_controller, "Requires inventory controller upgrade to run!")
- local crafting = assert(component.crafting, "Requires crafting upgrade to run!")
- local class = {}
- class.ID = { stick = 280, plank = 5, log = 17, cobblestone = 4, ironIngot = 265, diamond = 264, }
- class.SLOTS_CRAFTING = {[1]=1, [2]=2, [3]=3, [5]=5, [6]=6, [7]=7, [9]=9, [10]=10, [11]=11}
- function class.getFreeNonCraftingSlot()
- for slot=1,inventory.getInventorySize(sides.back) do
- if (not class.SLOTS_CRAFTING[slot]) then
- if (robot.count(slot) == 0) then
- return slot
- end
- end
- end
- return nil
- end
- function class.clearCraftingGrid()
- local freeSlot
- for slot in pairs(class.SLOTS_CRAFTING) do
- if (robot.count(slot) > 0) then
- freeSlot = class.getFreeNonCraftingSlot()
- if (freeSlot) then
- robot.select(slot)
- if (not robot.transferTo(freeSlot)) then
- return false
- end
- else
- return false
- end
- end
- end
- return true
- end
- function class.craftPlanks(count)
- if (count % 4 > 0) then
- count = count + 4 - (count % 4)
- end
- if (not class.clearCraftingGrid()) then
- print("Unable to clear crafting area, please make some room")
- return false
- end
- local logCount = math.floor(count / 4)
- local slotLogs = class.requireItem(class.ID.log, logCount)
- if (slotLogs) then
- robot.select(slotLogs)
- if (robot.transferTo(1, logCount)) then
- return crafting.craft(count)
- end
- end
- end
- function class.craftSticks(count)
- if (count % 4 > 0) then
- count = count + 4 - (count % 4)
- end
- local woodCount = math.floor(count / 2 + 0.5)
- local slotWood = class.requireItem(class.ID.plank, woodCount)
- if (not slotWood) then
- if (not class.craftPlanks(woodCount)) then
- return false
- end
- end
- if (not class.clearCraftingGrid()) then
- print("Unable to clear crafting area, please make some room")
- return false
- end
- slotWood = class.requireItem(class.ID.plank, woodCount)
- if (slotWood) then
- robot.select(slotWood)
- if (robot.transferTo(1, 1) and robot.transferTo(5, 1)) then
- return crafting.craft(count)
- end
- end
- end
- function class.findItems(itemID, count)
- local stack
- for slot=1,inventory.getInventorySize(sides.back) do
- stack = inventory.getStackInSlot(sides.back, slot)
- if (stack and (stack.id == itemID) and (stack.size >= count)) then
- return slot
- end
- end
- return nil
- end
- function class.requireItem(itemID, count)
- local slot = class.findItems(itemID, count)
- if ((not slot) and (itemID == class.ID.stick)) then
- if (class.craftSticks(count)) then
- return class.findItems(itemID, count)
- end
- else
- return slot
- end
- return nil
- end
- function class.craftPickaxe(material)
- if (not class.clearCraftingGrid()) then
- print("Unable to clear crafting area, please make some room")
- return false
- end
- local slotStick = class.requireItem(class.ID.stick, 2)
- if (slotStick) then
- local slotMaterial = class.requireItem(material, 3)
- if (slotMaterial) then
- robot.select(slotStick)
- if (robot.transferTo(6, 1) and robot.transferTo(10, 1)) then
- robot.select(slotMaterial)
- if (robot.transferTo(1, 1) and robot.transferTo(2, 1) and robot.transferTo(3, 1)) then
- return crafting.craft()
- end
- end
- else
- print("Head material for pickaxe not found.")
- end
- else
- print("No sticks found, also no wood to make them.")
- end
- return false
- end
- function class.craftStonePickaxe()
- return class.craftPickaxe(class.ID.cobblestone)
- end
- function class.craftIronPickaxe()
- return class.craftPickaxe(class.ID.ironIngot)
- end
- function class.craftDiamondPickaxe()
- return class.craftPickaxe(class.ID.diamond)
- end
- return class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement