Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Made by Unarelith
- -- https://github.com/Unarelith
- local robot_api = require("robot")
- local component = require("component")
- local sides = require("sides")
- local term = require("term")
- local Recipes = {
- ["Empowered Emeradic Crystal Block"] = {
- "Emeradic Crystal Block",
- "Emerald",
- "Beryllium Dust",
- "Cactus Green",
- "Ethetic Green Block"
- },
- ["Empowered Void Crystal Block"] = {
- "Void Crystal Block",
- "Block of Black Quartz",
- "Basalt",
- "Block of Black Iron",
- "Ink Sac"
- },
- ["Empowered Restonia Crystal Block"] = {
- "Restonia Crystal Block",
- "Redstone Reception Coil",
- "Rhodochrosite",
- "Ardite Tool Rod",
- "Red Nether Brick"
- },
- ["Empowered Enori Crystal Block"] = {
- "Enori Crystal Block",
- "Bone Block",
- "Osmium Ingot",
- "Fluorite",
- "Block of Quartz"
- },
- ["Empowered Palis Crystal Block"] = {
- "Palis Crystal Block",
- "Dense Lapis Lazuli Plate",
- "Sapphire",
- "Congealed Slime Block",
- "Cobalt Ingot"
- },
- ["Empowered Diamatine Crystal Block"] = {
- "Diamatine Crystal Block",
- "Mana Diamond",
- "Manyullyn Ingot",
- "Malachite",
- "Zirconium Dust"
- },
- ["Hardened Cell Frame"] = {
- "Energy Cell Frame",
- "Invar Plate",
- "Invar Gear",
- "Steel Casing",
- "Steel Rod"
- },
- ["Ultimate Control Circuit"] = {
- "Elite Control Circuit",
- "Atomic Alloy",
- "Atomic Alloy",
- "Atomic Alloy",
- "Atomic Alloy"
- }
- }
- local Rotation = {
- Front = 0,
- Right = 1,
- Back = 2,
- Left = 3
- }
- local Robot = {
- new = function(_x, _y, _z, _rotation)
- local x = _x
- local y = _y
- local z = _z
- local rotation = _rotation
- local rotateTo = function(self, rotation)
- if self.rotation ~= rotation then
- local right = (4 - self.rotation + rotation) % 4
- local left = (4 + self.rotation - rotation) % 4
- if right == 2 or left == 2 then
- robot_api.turnAround()
- self.rotation = (self.rotation + 2) % 4
- elseif right < left then
- robot_api.turnRight()
- self.rotation = (self.rotation + 1) % 4
- else
- robot_api.turnLeft()
- self.rotation = (4 + self.rotation - 1) % 4
- end
- end
- end
- local moveUp = function(self)
- local ok, error = robot_api.up()
- if ok then
- self.y = self.y + 1
- end
- return ok, error
- end
- local moveDown = function(self)
- local ok, error = robot_api.down()
- if ok then
- self.y = self.y - 1
- end
- return ok, error
- end
- local moveLeft = function(self)
- self:rotateTo(Rotation.Left)
- local ok, error = robot_api.forward()
- if ok then
- self.x = self.x - 1
- end
- return ok, error
- end
- local moveRight = function(self)
- self:rotateTo(Rotation.Right)
- local ok, error = robot_api.forward()
- if ok then
- self.x = self.x + 1
- end
- return ok, error
- end
- local moveFront = function(self)
- self:rotateTo(Rotation.Front)
- local ok, error = robot_api.forward()
- if ok then
- self.z = self.z + 1
- end
- return ok, error
- end
- local moveBack = function(self)
- self:rotateTo(Rotation.Back)
- local ok, error = robot_api.forward()
- if ok then
- self.z = self.z - 1
- end
- return ok, error
- end
- local tryMove = function(self, pos, condition, movement)
- while condition(self, pos) do
- local ok, error = movement(self)
- if not ok then
- return false
- end
- end
- return true
- end
- local moveTo = function(self, pos)
- while self.x ~= pos.x or self.y ~= pos.y or self.z ~= pos.z do
- self:tryMove(pos, function(self, pos) return self.x < pos.x end, self.moveRight)
- self:tryMove(pos, function(self, pos) return self.x > pos.x end, self.moveLeft)
- self:tryMove(pos, function(self, pos) return self.y < pos.y end, self.moveUp)
- self:tryMove(pos, function(self, pos) return self.y > pos.y end, self.moveDown)
- self:tryMove(pos, function(self, pos) return self.z < pos.z end, self.moveFront)
- self:tryMove(pos, function(self, pos) return self.z > pos.z end, self.moveBack)
- end
- end
- return {
- -- Members
- x = x,
- y = y,
- z = z,
- rotation = rotation,
- -- Methods
- rotateTo = rotateTo,
- moveUp = moveUp,
- moveDown = moveDown,
- moveLeft = moveLeft,
- moveRight = moveRight,
- moveFront = moveFront,
- moveBack = moveBack,
- tryMove = tryMove,
- moveTo = moveTo
- }
- end
- }
- local checkInventoryForCraft = function()
- local items = component.inventory_controller.getAllStacks(sides.front)
- if not items then
- print("No items on side front")
- else
- local recipe = nil
- for slot, item in ipairs(items) do
- if item.size > 0 then
- if not recipe then
- for name, items in pairs(Recipes) do
- for n, comp in ipairs(items) do
- if item.label == comp then
- print("Found recipe '" .. name .. "'")
- print("Found component '" .. item.label .. "'")
- recipe = {name = name, items = items, components = {}}
- recipe.components[1] = {name = item.label, slot = slot, main = (n == 1)}
- end
- end
- end
- else
- for n, comp in ipairs(recipe.items) do
- if item.label == comp then
- print("Found component '" .. item.label .. "'")
- recipe.components[#recipe.components + 1] = {name = item.label, slot = slot, main = (n == 1)}
- if #recipe.components == 5 then
- print("Found all 5 items, let's craft!")
- return recipe
- end
- end
- end
- end
- end
- end
- end
- if not recipe then
- print("No valid recipe found.")
- end
- end
- local takeRecipeItems = function(recipe)
- local mainItemTook = false
- for n, item in ipairs(recipe.components) do
- if not item.main and not mainItemTook then
- robot_api.select(n + 1)
- elseif item.main then
- robot_api.select(1)
- mainItemTook = true
- else
- robot_api.select(n)
- end
- component.inventory_controller.suckFromSlot(sides.front, item.slot, 1)
- end
- end
- local chest = {x = 0, y = 0, z = 2}
- local charger = {x = 0, y = 0, z = 0}
- local robot = Robot.new(0, 0, 0, Rotation.Front)
- local tryToCraft = function()
- robot:moveTo(chest)
- robot:rotateTo(Rotation.Left)
- local recipe = checkInventoryForCraft()
- if recipe then
- takeRecipeItems(recipe)
- local locations = {
- [1] = {x = 0, y = 1, z = 5, rotation = Rotation.Right}, -- Empowerer
- [2] = {x = -1, y = 1, z = 5, rotation = Rotation.Left}, -- Display Stand 1
- [3] = {x = 1, y = 1, z = 7, rotation = Rotation.Front}, -- Display Stand 2
- [4] = {x = 3, y = 1, z = 5, rotation = Rotation.Right}, -- Display Stand 3
- [5] = {x = 1, y = 1, z = 3, rotation = Rotation.Back}, -- Display Stand 4
- [6] = {x = 0, y = 0, z = 3, rotation = Rotation.Left} -- Output chest
- }
- for n, location in ipairs(locations) do
- if n < 6 then
- robot:moveTo(location)
- robot:rotateTo(location.rotation)
- robot_api.select(n)
- component.inventory_controller.dropIntoSlot(sides.front, 1, 1)
- end
- end
- -- Now move to Empowerer and wait the craft to finish
- robot:moveTo(locations[1])
- robot:rotateTo(locations[1].rotation)
- while component.inventory_controller.getStackInSlot(sides.front, 1).label ~= recipe.name do
- term.clear()
- print("Waiting for '" .. recipe.name .. "'...")
- os.sleep(5)
- end
- term.clear()
- print("Crafted '" .. recipe.name .. "' successfully!")
- robot_api.select(6)
- component.inventory_controller.suckFromSlot(sides.front, 1, 1)
- -- Move to the output chest and drop the crafted item
- robot:moveTo(locations[6])
- robot:rotateTo(locations[6].rotation)
- component.inventory_controller.dropIntoSlot(sides.front, 1, 1)
- end
- robot:moveTo(charger)
- robot:rotateTo(Rotation.Front)
- end
- while true do
- term.clear()
- print("Looking for a new craft to do...")
- tryToCraft()
- print("Resting for 10 seconds...")
- os.sleep(10)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement