Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local robot = require("robot")
- local component = require("component")
- local ic = component.inventory_controller
- local args, opts = require("shell").parse(...)
- local inventory = {}
- local placeRow, placeColumn, getInventory, checkItemInInventoryAndSelect, placeStructure, resetPosition, gotoStart, getRequiredItemsFromCache, totalItemQuantityInInventory, haveItemsToCraft, throwCatalyst, moveToCharging, moveToPlacing;
- local itemNames = {
- AIR = "nil",
- GOLD_BLOCK = "minecraft:gold_block",
- COMPACT_WALL = "compactmachines3:wallbreakable",
- REDSTONE = "minecraft:redstone",
- REDSTONE_BLOCK = "minecraft:redstone_block",
- OBISIDAN = "minecraft:obsidian",
- ENDERPEARL = "minecraft:ender_pearl",
- IRON_BLOCK = "minecraft:iron_block"
- }
- local recipes = {
- Compact5 = {
- recipe = {
- {
- {itemNames.COMPACT_WALL, itemNames.COMPACT_WALL, itemNames.COMPACT_WALL},
- {itemNames.COMPACT_WALL, itemNames.COMPACT_WALL, itemNames.COMPACT_WALL},
- {itemNames.COMPACT_WALL, itemNames.COMPACT_WALL, itemNames.COMPACT_WALL}
- },
- {
- {itemNames.COMPACT_WALL, itemNames.COMPACT_WALL, itemNames.COMPACT_WALL},
- {itemNames.COMPACT_WALL, itemNames.GOLD_BLOCK, itemNames.COMPACT_WALL},
- {itemNames.COMPACT_WALL, itemNames.COMPACT_WALL, itemNames.COMPACT_WALL}
- },
- {
- {itemNames.COMPACT_WALL, itemNames.COMPACT_WALL, itemNames.COMPACT_WALL},
- {itemNames.COMPACT_WALL, itemNames.COMPACT_WALL, itemNames.COMPACT_WALL},
- {itemNames.COMPACT_WALL, itemNames.COMPACT_WALL, itemNames.COMPACT_WALL}
- }
- },
- items = {
- {
- name = itemNames.COMPACT_WALL,
- quantity = 26
- },
- {
- name = itemNames.GOLD_BLOCK,
- quantity = 1
- },
- {
- name = itemNames.ENDERPEARL,
- quantity = 1
- }
- },
- catalyst = itemNames.ENDERPEARL,
- restTime = 480 / 20
- },
- EnderPearl = {
- recipe = {
- {
- {itemNames.OBISIDAN, itemNames.OBISIDAN, itemNames.OBISIDAN},
- {itemNames.OBISIDAN, itemNames.OBISIDAN, itemNames.OBISIDAN},
- {itemNames.OBISIDAN, itemNames.OBISIDAN, itemNames.OBISIDAN}
- },
- {
- {itemNames.OBISIDAN, itemNames.OBISIDAN, itemNames.OBISIDAN},
- {itemNames.OBISIDAN, itemNames.REDSTONE_BLOCK, itemNames.OBISIDAN},
- {itemNames.OBISIDAN, itemNames.OBISIDAN, itemNames.OBISIDAN}
- },
- {
- {itemNames.OBISIDAN, itemNames.OBISIDAN, itemNames.OBISIDAN},
- {itemNames.OBISIDAN, itemNames.OBISIDAN, itemNames.OBISIDAN},
- {itemNames.OBISIDAN, itemNames.OBISIDAN, itemNames.OBISIDAN}
- }
- },
- items = {
- {
- name = itemNames.OBISIDAN,
- quantity = 26
- },
- {
- name = itemNames.REDSTONE_BLOCK,
- quantity = 1
- },
- {
- name = itemNames.REDSTONE,
- quantity = 1
- }
- },
- catalyst = itemNames.REDSTONE,
- restTime = 200 / 20
- },
- CompactWall = {
- recipe = {
- {
- {},
- {itemNames.AIR, itemNames.IRON_BLOCK, itemNames.AIR},
- {}
- },
- {
- {},
- {itemNames.AIR, itemNames.REDSTONE, itemNames.AIR},
- {}
- },
- {
- {},
- {},
- {}
- }
- },
- items = {
- {
- name = itemNames.IRON_BLOCK,
- quantity = 1
- },
- {
- name = itemNames.REDSTONE,
- quantity = 2
- }
- },
- catalyst = itemNames.REDSTONE,
- restTime = 100 / 20
- }
- }
- function placeRow(row)
- getInventory()
- for index, block in pairs(row) do
- robot.forward()
- if checkItemInInventoryAndSelect(block) then
- robot.placeDown()
- end
- end
- end
- function placeColumn(column)
- local lastColumn = 1
- for index, row in ipairs(column) do
- if index == 2 then
- robot.turnRight()
- robot.forward()
- robot.turnRight()
- robot.back()
- end
- if index == 3 then
- robot.turnLeft()
- robot.forward()
- robot.turnLeft()
- robot.back()
- end
- lastColumn = index
- if #row ~= 0 then
- placeRow(row)
- else
- robot.forward()
- robot.forward()
- robot.forward()
- end
- end
- return lastColumn
- end
- function getInventory()
- for i = 1, 16 do
- if ic.getStackInInternalSlot(i) then
- inventory[i] = {
- name = ic.getStackInInternalSlot(i).name,
- quantity = robot.count(i) or 1
- }
- else
- inventory[i] = nil
- end
- end
- end
- function checkItemInInventoryAndSelect(itemName)
- local found = false
- for i=1,16 do
- if inventory[i] and inventory[i].name == itemName then
- if robot.count(i) < 2 then inventory[i] = nil end
- found = true
- robot.select(i)
- break
- end
- end
- return found
- end
- function placeStructure(item, quantity, actual)
- if actual == nil then actual = 1 end
- if quantity == nil then quantity = 1 end
- if actual > quantity then return print("Finished crafting") end
- getInventory()
- if not haveItemsToCraft(item) then return print("The are items missing for the craft") end
- moveToPlacing()
- for index, column in ipairs(item.recipe) do
- local lastColumn = placeColumn(column)
- resetPosition(lastColumn)
- if index ~= 3 then robot.up() end
- end
- gotoStart()
- moveToCharging()
- throwCatalyst(item)
- os.sleep(item.restTime)
- getRequiredItemsFromCache(item)
- placeStructure(item, quantity, actual + 1)
- end
- function resetPosition(lastColumn)
- if lastColumn == 1 then
- for i = 1, 3 do
- robot.back()
- end
- else if lastColumn == 2 then
- robot.turnRight()
- robot.forward()
- robot.turnRight()
- robot.back()
- else if lastColumn == 3 then
- for i = 1, 3 do
- robot.back()
- end
- robot.turnLeft()
- for i = 1, 2 do
- robot.forward()
- end
- robot.turnRight()
- end
- end
- end
- end
- function gotoStart()
- for i = 1, 2 do
- robot.down()
- end
- end
- function totalItemQuantityInInventory(itemName)
- local total = 0
- for key, item in pairs(inventory) do
- if item and item.name == itemName then
- if item.quantity then
- total = total + item.quantity
- end
- end
- end
- return total
- end
- function haveItemsToCraft(item)
- local craftable = {}
- for key, value in pairs(item.items) do
- if math.floor(totalItemQuantityInInventory(value.name) / value.quantity) >= 1 then craftable = true else craftable = false break end
- end
- return craftable
- end
- function throwCatalyst(item)
- checkItemInInventoryAndSelect(item.catalyst)
- os.sleep(0.5)
- robot.drop(1)
- end
- function moveToCharging()
- robot.back()
- robot.down()
- end
- function moveToPlacing()
- robot.forward()
- robot.up()
- end
- function getRequiredItemsFromCache(item)
- for index, itemInfo in ipairs(item.items) do
- if index == 1 then
- robot.turnLeft()
- robot.suck(itemInfo.quantity)
- robot.turnRight()
- end
- if index == 2 then
- robot.turnRight()
- robot.suck(itemInfo.quantity)
- robot.turnLeft()
- end
- if index == 3 then
- robot.up()
- robot.turnLeft()
- robot.suck(itemInfo.quantity)
- robot.turnRight()
- robot.down()
- end
- end
- end
- placeStructure(recipes[args[1]], tonumber(args[2]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement