Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local comp = require("component")
- local event = require("event")
- local computer = require("computer")
- local file = require("io")
- local databases = {}
- local recipes = {}
- local database_keys = {}
- local reserved = {}
- local craft_ladder = {}
- local function getRecipes()
- items = {}
- recipe_file = file.input('recipes.dat')
- while true do
- line = io.read("*line")
- print(line)
- if line then
- curr_index= 0
- name_index = string.find(line,'%(')
- name = string.sub(line,1,name_index-1)
- curr_index=name_index+1
- recipes[name]={}
- while true do
- end_index = string.find(line,';',curr_index)
- if end_index then
- split_index = string.find(line,':',curr_index)
- number = tonumber(string.sub(line,curr_index,split_index-1))
- label = string.sub(line,split_index+1,end_index-1)
- items[label]=true
- recipes[name][number]=label
- curr_index=end_index+1
- else break
- end
- end
- else break end
- end
- -- fillDatabases(items)
- end
- local function clearDatabases()
- for n = 1, #databases do
- db = comp.proxy(databases[n])
- for m = 1,81 do
- db.clear(m)
- end
- end
- end
- local function fillDatabases()
- current_database = 1
- db = comp.proxy(databases[current_database])
- current_database_key = 1
- for k,v in pairs(comp.me_controller.getItemsInNetwork()) do
- if type(v) == "table" then
- --db.clear(current_database_key)
- comp.me_controller.store(v,db.address,current_database_key)
- current_database_key = current_database_key + 1
- if current_database_key > 81 then
- current_database = current_database + 1
- db = comp.proxy(databases[current_database])
- current_database_key = 1
- end
- end
- end
- end
- local function getDatabases()
- for k,v in pairs(comp.list()) do
- if v == "database" then
- table.insert(databases,k)
- end
- end
- end
- local function databaseLookUp()
- current_database_address = {0,0}
- for n = 1, #databases do
- db = comp.proxy(databases[n])
- for m = 1,81 do
- item = db.get(m)
- if item then
- database_keys[item.label]={n,m,0}
- else
- if current_database_address[1] == 0 then
- current_database_address = {n,m}
- else
- if current_database_address[2] == 1 then
- current_database_address = {n,m}
- else
- if current_database_address[2] ~= 1 then
- end
- end
- end
- end
- end
- end
- for k,v in pairs(comp.me_controller.getItemsInNetwork()) do
- if type(v) == "table" then
- if database_keys[v.label] then
- database_keys[v.label][3]=v.size
- else
- comp.me_controller.store(v,databases[current_database_address[1]],current_database_address[2])
- database_keys[v.label]={current_database_address[1],current_database_address[2],v.size}
- end
- end
- end
- end
- local function buildCraftingTree(label,amount,tier)
- tier = tier or 0
- local crafting = {}
- if crafting[label] then
- crafting[label][1] = crafting[label][1]+amount
- if tier > crafting[label][3] then
- crafting[label][3] = tier
- end
- else
- crafting[label] = {amount,0,tier}
- end
- if recipes[label] then
- for number, item in pairs(recipes[label]) do
- temp_crafting = buildCraftingTree(item,amount,tier+1)
- for name, values in pairs(temp_crafting) do
- if crafting[name] then
- crafting[name][1] = crafting[name][1]+temp_crafting[name][1]
- if temp_crafting[name][3] > crafting[name][3] then
- crafting[name][3] = temp_crafting[name][3]
- end
- else
- crafting[name] = {temp_crafting[name][1],0,temp_crafting[name][3]}
- end
- end
- end
- else
- end
- return crafting
- end
- local function reconcileAvailableItems(basic,non_basic)
- -- name:need,have,tier
- local non_basic_sorted = {}
- for k in pairs(non_basic) do table.insert(non_basic_sorted,k) end
- table.sort(non_basic_sorted, function (a,b) return non_basic[a][3] < non_basic[b][3] end)
- local non_basic_temp = non_basic
- for _,k in ipairs(non_basic_sorted) do
- if non_basic[k][2]>0 then
- non_basic_temp[k][1]=non_basic_temp[k][1]-non_basic_temp[k][2]
- if non_basic_temp[k][1] < 0 then non_basic_temp[k][1] = 0 end
- local diff = non_basic_temp[k][1]-non_basic[k][1]
- for slot,item in pairs(recipes[k]) do
- if basic[item] then
- print(item,basic[item][1])
- basic[item][1] = basic[item][1] - diff
- print(item,basic[item][1])
- if basic[item][1] < 0 then basic[item][1] = 0 end
- else
- if non_basic[item] then
- non_basic_temp[item][1] = non_basic_temp[item][1]-diff
- end
- end
- end
- end
- end
- return basic, non_basic_temp
- end
- local function checkCraftingAvailability(craft_table)
- local basic = {}
- local non_basic = {}
- for name, values in pairs(craft_table) do
- if database_keys[name] then
- print('here',name)
- available = database_keys[name][3]
- if available >= values[1] then
- values[2] = values[1]
- else
- values[2] = available
- end
- if recipes[name] then
- non_basic[name]=values
- else
- basic[name]=values
- end
- else
- non_basic[name]=values
- end
- end
- basic2, non_basic2 = reconcileAvailableItems(basic,non_basic)
- local missing = {}
- for name,values in pairs(basic2) do
- if values[2]-values[1] < 0 then
- missing[name] = values[1]-values[2]
- end
- end
- return missing, basic2, non_basic2
- end
- local function moveToBuffer(label,amount)
- output_slot = 0
- for slot =1, comp.transposer.getInventorySize(TARGETSIDE) do
- item = comp.transposer.getStackInSlot(TARGETSIDE, slot)
- if item then
- if item.label == label then
- output_slot = slot
- break
- end
- else
- output_slot = slot
- break
- end
- end
- if database_keys[label] then
- comp.me_exportbus.setExportConfiguration(TARGETSIDE,output_slot,databases[database_keys][label][1],databases[database_keys][label][2])
- if database_keys[label][3] >= amount then
- for _ = 1,amount do
- comp.me_exportbus.exportIntoSlot(TARGETSIDE,output_slot)
- end
- else
- for _ = 1,database_keys[label][3] do
- comp.me_exportbus.exportIntoSlot(TARGETSIDE,output_slot)
- end
- end
- end
- end
- --clearDatabases()
- --fillDatabases()
- getDatabases()
- getRecipes()
- print("Looking up items in network...")
- databaseLookUp()
- print("Crafting")
- crafting = buildCraftingTree("Machine Frame",2)
- missing,basic,non_basic = checkCraftingAvailability(crafting)
- print("Crafting requirements are:")
- print("Name\tNeeded\tAvailable")
- for key,value in pairs(basic) do
- print(key,'\t',value[1],'\t',value[2])
- end
- print("Will be used to craft:")
- print("Name\tNeeded\tAvailable")
- for key,value in pairs(non_basic) do
- print(key,'\t',value[1],'\t',value[2])
- end
- print("Missing ingredients:")
- print("Name\tMissing")
- for key,value in pairs(missing) do
- print(key,'\t',value)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement