Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --To Do List
- --[[
- Robot/Computer Communication
- Computer API
- Better Inventory Management
- Error Handling
- ]]
- local robot = require("robot");
- local component = require("component");
- local computer = component.computer;
- local sides = require("sides");
- local look_vars = false;
- local serialization = require("serialization");
- local filesystem = require("filesystem");
- local term = require("term");
- local event = require("event");
- robotStatus = "Starting Up...";
- local run = true;
- --Configuration
- local layout = {
- {1, 1, 1, 1, 1, -- bottom layer
- 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1,
- },
- {1, 1, 1, 1, 1, -- middle layer
- 1, 0, 0, 0, 1,
- 1, 0, 0, 0, 1,
- 1, 0, 0, 0, 1,
- 1, 1, 1, 1, 1,
- },
- {1, 1, 1, 1, 1, -- middle layer
- 1, 0, 0, 0, 1,
- 1, 0, 0, 0, 1,
- 1, 0, 0, 0, 1,
- 1, 1, 1, 1, 1,
- },
- {1, 1, 1, 1, 1, -- middle layer
- 1, 0, 0, 0, 1,
- 1, 0, 0, 0, 1,
- 1, 0, 0, 0, 1,
- 1, 1, 1, 1, 1,
- },
- {1, 1, 1, 1, 1, -- top layer
- 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1,
- }}
- local dropSlot = 3 -- the slot where the drop item is
- local dropCount = 1 -- how many items should be dropped
- local waitTime = 20 -- time between creations
- local rsSignalSide = sides.front; -- if no redstone signal, use nil
- local rsModeHigh = false; -- redstons signal means work
- local outputBottomRedstone = true; -- send a rs signal to the bottom
- local beeps = true; -- turn off the beeps
- --Main Code
- local dimensions = #layout;
- local inventorySize = robot.inventorySize();
- function randomBeep()
- if beeps then
- computer.beep(math.random(400, 2000))
- end
- end
- function walk(steps) --Returns true is Successful, ends program if not.
- local success = true;
- if steps == nil then steps = 1; end;
- if steps > 0 then
- for _ = 1, steps do
- if robot.forward() == false then success = false; end
- end
- elseif steps < 0 then
- for _ = steps, -1 do
- if robot.back() == false then success = false; end
- end
- end
- if success == false then
- print("[ERROR] Couldn't Move. Ending Program. (type enableAutostart once you reset it.)");
- filesystem.setAutorunEnabled(false);
- os.exit();
- else
- return true;
- end
- end
- local boolToNum = {[true]=1, [false]=0}
- function isRedstoneSignal(side) --I don't know if I need this
- if side ~= nil then
- if pcall(function () _ = component.redstone end) then
- local redstone = component.redstone
- redstone.setOutput(sides.bottom, boolToNum[outputBottomRedstone])
- return rsModeHigh == (redstone.getInput(side) > 0)
- end
- end
- return true
- end
- function countTable(input)
- local count = 0;
- for _, _ in pairs(input) do
- count = count + 1;
- end
- return count;
- end
- function getNrItems()
- local itemCounts = {};
- for z = 1, dimensions do
- for i = 1, #layout[z] do
- local itemNr = layout[z][i]
- if itemNr ~= 0 then
- if itemCounts[itemNr] == nil then
- print("Found one");
- itemCounts[itemNr] = 1;
- else
- itemCounts[itemNr] = itemCounts[itemNr] + 1;
- end
- end
- end
- end
- if itemCounts[dropSlot] == nil then itemCounts[dropSlot] = dropCount; else itemCounts[dropSlot] = itemCounts[dropSlot] + dropCount; end
- return countTable(itemCounts), itemCounts
- end
- local nrItems, itemCounts = getNrItems() --nrItems = Number Of Item Types, itemCounts = Number Of Each Item
- function writeLine(line, text)
- if text == nil then text = line; _, line = term.getCursor(); line = line + 1; if line == 17 then line = 1; end end
- term.setCursor(1, line);
- term.write(text .. string.rep(" ", 50), false);
- end
- function updateDisplay()
- while true do
- writeLine(1, string.rep("=", 50))
- writeLine(" MARA 2.0 | Running");
- writeLine(" Status: " .. robotStatus);
- writeLine(string.rep("=", 50))
- os.sleep(0);
- end
- end
- term.clear();
- local thread = require("thread");
- thread.create(updateDisplay);
- function closeListener(_, _, key)
- if key == nil then key = 96; end
- if key == 96 then
- print("Killing Program...");
- run = false;
- filesystem.setAutorunEnabled(false)
- event.ignore("key_up", closeListener);
- end
- end
- event.listen("key_up", closeListener);
- function getItems()
- local count = 1;
- for i, _ in pairs(itemCounts) do
- robot.turnLeft();
- robot.select();
- local itemsLeft = itemCounts[i];
- while itemsLeft > 0 do
- itemsLeft = itemsLeft - robot.suck(itemsLeft);
- end
- robot.turnRight();
- if count ~= nrItems then
- walk();
- end
- count = count + 1;
- end
- end
- local function placeDown(slot)
- if slot == 0 then
- return true
- end
- robot.select(slot)
- return robot.placeDown()
- end
- local function buildOneLayer(layer, layerNr)
- for i = 1, #layer do
- if layerNr == 1 and i == math.ceil(#layer/2) then
- robot.select(inventorySize)
- robot.suckDown()
- randomBeep()
- end
- if not placeDown(layer[i]) then
- for t=robot.select(),inventorySize-robot.select() do
- if robot.count(robot.select() + t) > 0 then robot.select(layer[i]+t); break; end
- end
- robot.transferTo(layer[i])
- placeDown(layer[i])
- end
- robot.forward()
- if i % dimensions == 0 and i ~= dimensions^2 then
- if i % (dimensions*2) == 0 then
- robot.turnLeft()
- else
- robot.turnRight()
- end
- robot.forward()
- if i % (dimensions*2) == 0 then
- robot.turnLeft()
- else
- robot.turnRight()
- end
- robot.forward()
- end
- end
- randomBeep()
- end
- local function buildLayers()
- for i = 1, dimensions do
- robot.turnAround()
- robot.up()
- robot.forward()
- buildOneLayer(layout[i], i)
- end
- randomBeep()
- end
- robotStatus = "Waiting...";
- local activate = true;
- while run do
- if activate then
- robotStatus = "Grabbing Items";
- walk(-1); --Pull out of charging bay.
- robot.turnRight();
- walk(); --Get in Inventory Collecting Position
- getItems(); --Get All the Required Items
- walk((#layout + 1)-nrItems) --Walk to starting position.
- robot.turnLeft();
- robotStatus = "Building";
- buildLayers();
- robotStatus = "Activating";
- walk(-1);
- robot.turnLeft();
- walk(-2);
- while not robot.detectDown() do robot.down() end
- robot.select(dropSlot);
- robot.drop(dropCount);
- robot.turnLeft();
- robotStatus = "Returning Home"
- robot.select(1);
- while not robot.detect() do walk(1); end
- robotStatus = "Waiting For Craft To Complete";
- os.sleep(waitTime);
- robotStatus = "Waiting...";
- end
- os.sleep(0);
- end
- --term.clear();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement