From 049f9e89582ec231ed480b91c81ac21b8b388021 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Fri, 29 Jun 2018 00:46:32 +0800 Subject: [PATCH] Add auto crafting programs --- auto_crafting.lua | 101 ++++++++++++++++++++++++++++++++++++++++ auto_crafting_robot.lua | 14 ++++++ 2 files changed, 115 insertions(+) create mode 100644 auto_crafting.lua create mode 100644 auto_crafting_robot.lua diff --git a/auto_crafting.lua b/auto_crafting.lua new file mode 100644 index 0000000..485c9e1 --- /dev/null +++ b/auto_crafting.lua @@ -0,0 +1,101 @@ +-- Storage Center and Auto Crafting +-- Requires: +-- Computer with wireless network card +-- Robot with craft upgrade and inventory upgrade, wireless network card +-- Transposer + +require("libevent") +require("util") +local component=require("component") + +local modem=component.list("modem") +if(modem==nil) then + error("Modem is required") +else + modem=component.proxy("modem") +end + +-- Config +local craft_trans=proxy("transposer","") +local craft_trans_side_in=sides.west +local craft_trans_side_out=sides.east +local craft_trans_side_interface=sides.south + +-- End of Config + +local craft_db={} + +local function readCraftTable() + local tb={} + tb.input={} + for i=1,3,1 do + for j=1,3,1 do + local p=craft_trans.getStackInSlot(sides.up,(i-1)*9+j) + if(p~=nil) then + table.insert(tb.input,{ + ["i"]=i, + ["j"]=j, + ["name"]=p.name, + ["size"]=p.size}) + end + end + end + + local xp=craft_trans.getStackInSlot(sides.up,2*9+5) + if(p~=nil) then + tb.output=xp.name + else + return false,"No craft target found. Invalid Plan." + end + + return true,tb +end + +local function netRobotCraft() + if(not modem.isOpen(2801)) then + if(not modem.open(2801)) then + return false,"Port can't be opened." + end + end + + local bus=CreateEventBus() + bus:listen("modem_message") + local craft_started=false + modem.broadcast(2802,"robotCraft") + local e=bus:next(3) -- Wait for 3 second + if(e~=nil) then + if(e.data[1]=="craft_started") then + craft_started=true + break + end + end + + if(not craft_started) then + return false,"Cannot tell robot to start craft" + end + + local e=bus:next() + if(e.data[1]~="craft_finished") then + return false,"Craft Failed" + end +end + +local function doCraftOnce(craft_table) + for k,v in pairs(craft_table) do + transferFromBaseToInterface(v.name,v.size,(v.i-1)*9+j) + end + local ret,msg=netRobotCraft() + if(not ret) then + return false,"Craft Failed. " .. msg + end +end + +local function craftOnce(name) + for k,v in pairs(craft_tb) do + if(v.output==name) then + doCraftOnce(v.input) + return + end + end + error("Craft table not found") +end diff --git a/auto_crafting_robot.lua b/auto_crafting_robot.lua new file mode 100644 index 0000000..633cba7 --- /dev/null +++ b/auto_crafting_robot.lua @@ -0,0 +1,14 @@ +-- Auto Crafting Robot Client +-- Requires: +-- Craft upgrade and inventory upgrade +-- Wireless network card + +require("libevent") +local component=require("component") +local modem=component.list("modem") +if(modem==nil) then + error("Modem is required") +else + modem=component.proxy("modem") +end +