1
0
mirror of https://github.com/AR2000AR/openComputers_codes.git synced 2025-09-08 14:41:14 +02:00

autocrafter

This commit is contained in:
2022-04-29 20:45:26 +02:00
parent 56c609eecf
commit b04c14a9e8
4 changed files with 226 additions and 1 deletions

View File

@@ -36,6 +36,9 @@ Write the item id of every items in the chest on top of the transposer in a file
### doorCtrl
Control a number of redstone IO blocs with a single touchscreen. Easy to configure with `doorCtrl -c`
### autocrafter
Use a PC, a Robot, linked card and a transposer to craft items
---
## Additional librarys

186
autocrafter/autocrafter.lua Normal file
View File

@@ -0,0 +1,186 @@
local event = require 'event'
local component = require 'component'
local io = require 'io'
local filesystem = require 'filesystem'
local serialization = require 'serialization'
local string = require 'string'
local term = require 'term'
local transposer = component.transposer
local RECIPES_FILE = "/etc/autocraft/recipes.list"
local RECIPES_DIR = "/etc/autocraft/recipes.d/"
--get the keys from a table
local function getKeys(sourceArray)
local keyset = {}
for k,v in pairs(sourceArray) do
table.insert(keyset,k)
end
return keyset;
end
--find item in storage
local function findStack(targetItem,side)
local slot = 1
if(type(targetItem) ~= "table") then targetItem = {targetItem,false} end
for item in transposer.getAllStacks(side) do
if(item.name == targetItem[1] and (not targetItem[2] or targetItem[2] == item.damage)) then return slot end
slot = slot +1
end
return 0
end
local function emptyCrafter(sideStorage,sideRobot)
for slot = 1,transposer.getInventorySize(sideRobot) do
transposer.transferItem(sideRobot,sideStorage,64,slot)
end
end
local function loadRecipe(recipePatern,sideStorage,sideRobot)
local craftingGrid = {1,2,3,5,6,7,9,10,11} --crafting grid slot to robot inventory slot
emptyCrafter(sideStorage,sideRobot)
for i,item in pairs(recipePatern) do
if(item ~= "minecraft:air" and item ~= "") then
local itemSlot = findStack(item,sideStorage)
if(itemSlot == 0) then emptyCrafter(sideStorage,sideRobot) return false end --not enough ressources
transposer.transferItem(sideStorage,sideRobot,1,itemSlot,craftingGrid[i]) --put
end
end
return true
end
--init
if(not filesystem.exists("/etc/autocraft/")) then filesystem.makeDirectory("/etc/autocraft/") end
--load recipes list
local recipes = {}
if(filesystem.exists(RECIPES_FILE)) then
local rFile = io.open(RECIPES_FILE)
local fileRecipes = serialization.unserialize(rFile:read("*a"))
rFile:close()
if(fileRecipes) then
for key,val in pairs(fileRecipes) do
recipes[key] = val
end
end
end
if(filesystem.isDirectory(RECIPES_DIR)) then
for file in filesystem.list(RECIPES_DIR) do
if(string.sub(file,-1) ~= "/") then
local rFile = io.open(RECIPES_DIR..file)
local fileRecipes = serialization.unserialize(rFile:read("*a"))
rFile:close()
if(fileRecipes) then
for key,val in pairs(fileRecipes) do
recipes[key] = val
end
end
end
end
end
--find robot and storage sides
local sideRobot = 1
local sideStorage = 2
for i=0,5 do
local name = transposer.getInventoryName(i)
if(name) then
if(name == "opencomputers:robot") then sideRobot = i
else sideStorage = i end
end
end
-- main loop
local recipesNames = getKeys(recipes)
event.listen("interrupted",function() run = false return false end)
run = true
local pageNumber = 1
local maxPage = 1
while run do
maxPage = math.ceil(#recipesNames / 23.0)
term.clear()
if(pageNumber > maxPage) then pageNumber = maxPage end
if(pageNumber < 1) then pageNumber = 1 end
for i, name in ipairs(recipesNames) do
io.write(text.padRight(name,19))
if(i%4 == 0) then io.write("\n") end
end
if(#recipesNames %4 ~= 0) then io.write("\n") end
print("Page "..pageNumber.."/"..maxPage)
io.write("<item name>|<new> :")
local itemName = io.read()
if(tonumber(itemName))then
pageNumber = tonumber(itemName)
elseif(itemName == "new") then
term.clear()
print("Reading recipe from robot")
local newPatern = {}
for i,slot in pairs({1,2,3,5,6,7,9,10,11}) do
local item = transposer.getStackInSlot(sideRobot,slot)
local itemInf=""
if(item) then
print("slot : "..i,item.name,item.damage)
itemInf={item.name,false}
io.write("Save damage yN ?")
local save = io.read()
if(save == "y" or save == "Y") then itemInf[2]=item.damage end
end
table.insert(newPatern,itemInf)
end
local newName = ""
repeat
io.write("Craft name :")
newName = io.read()
until newName ~= "" and not tonumber(newName) and newName ~= "new" and newName ~= "delete"
recipes[newName] = newPatern
recipesNames = getKeys(recipes)
--save the new recipe in the main file
local fRecipes = {}
if(filesystem.exists(RECIPES_FILE) and not filesystem.isDirectory(RECIPES_FILE)) then
local rFile = io.open(RECIPES_FILE)
fRecipes = serialization.unserialize(rFile:read("*a"))
rFile:close()
end
fRecipes[newName] = newPatern
rFile = io.open(RECIPES_FILE,"w")
rFile:write(serialization.serialize(fRecipes))
rFile:close()
elseif(itemName == "delete") then
term.clear()
print("Chose a recipe to delete or leave blanc to cancel.\nOnly recipes in the main config file can be deleted that way.\nYou will need to restart the program to apply the changes")
io.write(">")
local rName = io.read()
local fRecipes = {}
if(filesystem.exists(RECIPES_FILE) and not filesystem.isDirectory(RECIPES_FILE)) then
local rFile = io.open(RECIPES_FILE)
fRecipes = serialization.unserialize(rFile:read("*a"))
rFile:close()
end
fRecipes[rName] = nil
rFile = io.open(RECIPES_FILE,"w")
rFile:write(serialization.serialize(fRecipes))
rFile:close()
elseif(recipes[itemName]) then
io.write("[item count] (default 1) :")
local count = io.read()
if(not tonumber(count)) then count = 1 end
print("Crafing "..itemName)
local crafted = 0
for i=1,count do
if(loadRecipe(recipes[itemName],sideStorage,sideRobot)) then
component.tunnel.send(1)
crafted = crafted + 1
emptyCrafter(sideStorage,sideRobot)
else
emptyCrafter(sideStorage,sideRobot)
break
end
end
print("Crafted "..crafted.."/"..count.." "..itemName)
io.write("Press enter to continue")
io.read()
end
end
term.clear()

View File

@@ -0,0 +1,26 @@
local function sleep(s)
local t1 = computer.uptime()
while t1+s > computer.uptime() do end
end
local gpu = component.proxy(component.list("gpu")())
local screenAD = component.list("screen")()
gpu.bind(screenAD)
gpu.set(1,1,"GPU OK")
sleep(1)
local tunnel = component.proxy(component.list("tunnel")())
gpu.set(1,2,"tunnel OK")
sleep(1)
local crafter = component.proxy(component.list("crafting")())
gpu.set(1,3,"crafting OK")
sleep(1)
while true do
sigName,localAd,_,_,_,message = computer.pullSignal(1)
if(sigName) then
gpu.set(1,4,sigName)
gpu.set(1,5,tostring(localAd))
gpu.set(1,6,tunnel.address)
end
if(sigName == "modem_message" and localAd == tunnel.address) then
crafter.craft()
end
end

View File

@@ -139,7 +139,7 @@
["door_ctrl"] = {
files = {
["master/doorCtrl/doorCtrl.lua"] = "/bin",
["?master/doorCtrl/doorCtrl.conf"] = "/etc"
["?master/doorCtrl/doorCtrl.conf"] = "//etc"
},
name = "Door controller",
description = "Controll your doors with multiple redstone io block",
@@ -149,5 +149,15 @@
["libgui"] = "/",
["libclass"] = "/"
}
},
["autocrafer"] = {
files = {
["master/autocrafter/autocrafter.lua"] = "/bin",
["master/autocrafter/autocrafterEEPROM.lua"] = "/misc"
},
name = "AutoCrafter",
description = "User a robot linked via a linked card to craft item from a inventory",
author = "AR200AR",
repo = "tree/master/autocrafter"
}
}