Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. local comp = require("component")
  2. local event = require("event")
  3. local computer = require("computer")
  4. local file = require("io")
  5.  
  6. local databases = {}
  7. local recipes = {}
  8. local database_keys = {}
  9. local reserved = {}
  10. local craft_ladder = {}
  11.  
  12.  
  13.  
  14.  
  15. local function getRecipes()
  16. items = {}
  17. recipe_file = file.input('recipes.dat')
  18. while true do
  19. line = io.read("*line")
  20. print(line)
  21. if line then
  22. curr_index= 0
  23. name_index = string.find(line,'%(')
  24. name = string.sub(line,1,name_index-1)
  25. curr_index=name_index+1
  26. recipes[name]={}
  27. while true do
  28. end_index = string.find(line,';',curr_index)
  29. if end_index then
  30. split_index = string.find(line,':',curr_index)
  31. number = tonumber(string.sub(line,curr_index,split_index-1))
  32. label = string.sub(line,split_index+1,end_index-1)
  33. items[label]=true
  34. recipes[name][number]=label
  35. curr_index=end_index+1
  36. else break
  37. end
  38. end
  39. else break end
  40. end
  41. -- fillDatabases(items)
  42. end
  43.  
  44. local function clearDatabases()
  45. for n = 1, #databases do
  46. db = comp.proxy(databases[n])
  47. for m = 1,81 do
  48. db.clear(m)
  49. end
  50. end
  51. end
  52.  
  53. local function fillDatabases()
  54. current_database = 1
  55. db = comp.proxy(databases[current_database])
  56. current_database_key = 1
  57. for k,v in pairs(comp.me_controller.getItemsInNetwork()) do
  58. if type(v) == "table" then
  59. --db.clear(current_database_key)
  60. comp.me_controller.store(v,db.address,current_database_key)
  61. current_database_key = current_database_key + 1
  62. if current_database_key > 81 then
  63. current_database = current_database + 1
  64. db = comp.proxy(databases[current_database])
  65. current_database_key = 1
  66. end
  67. end
  68. end
  69. end
  70.  
  71. local function getDatabases()
  72. for k,v in pairs(comp.list()) do
  73. if v == "database" then
  74. table.insert(databases,k)
  75. end
  76. end
  77. end
  78.  
  79. local function databaseLookUp()
  80. current_database_address = {0,0}
  81. for n = 1, #databases do
  82. db = comp.proxy(databases[n])
  83. for m = 1,81 do
  84. item = db.get(m)
  85. if item then
  86. database_keys[item.label]={n,m,0}
  87. else
  88. if current_database_address[1] == 0 then
  89. current_database_address = {n,m}
  90. else
  91. if current_database_address[2] == 1 then
  92. current_database_address = {n,m}
  93. else
  94. if current_database_address[2] ~= 1 then
  95. end
  96. end
  97. end
  98. end
  99. end
  100. end
  101. for k,v in pairs(comp.me_controller.getItemsInNetwork()) do
  102. if type(v) == "table" then
  103. if database_keys[v.label] then
  104. database_keys[v.label][3]=v.size
  105. else
  106. comp.me_controller.store(v,databases[current_database_address[1]],current_database_address[2])
  107. database_keys[v.label]={current_database_address[1],current_database_address[2],v.size}
  108. end
  109. end
  110. end
  111. end
  112.  
  113. local function buildCraftingTree(label,amount,tier)
  114. tier = tier or 0
  115. local crafting = {}
  116. if crafting[label] then
  117. crafting[label][1] = crafting[label][1]+amount
  118. if tier > crafting[label][3] then
  119. crafting[label][3] = tier
  120. end
  121. else
  122. crafting[label] = {amount,0,tier}
  123. end
  124. if recipes[label] then
  125. for number, item in pairs(recipes[label]) do
  126. temp_crafting = buildCraftingTree(item,amount,tier+1)
  127. for name, values in pairs(temp_crafting) do
  128. if crafting[name] then
  129. crafting[name][1] = crafting[name][1]+temp_crafting[name][1]
  130. if temp_crafting[name][3] > crafting[name][3] then
  131. crafting[name][3] = temp_crafting[name][3]
  132. end
  133. else
  134. crafting[name] = {temp_crafting[name][1],0,temp_crafting[name][3]}
  135. end
  136. end
  137. end
  138. else
  139. end
  140. return crafting
  141. end
  142.  
  143.  
  144.  
  145. local function tableDeepCopy(orig)
  146. output = {}
  147. for k,v in pairs(orig) do
  148. new_key = k
  149. new_values = {}
  150. for _,z in pairs(v) do
  151. new_value = z
  152. table.insert(new_values,new_value)
  153. end
  154. output[new_key] = new_values
  155. end
  156. return output
  157. end
  158.  
  159. local function reconcileAvailableItems(basic,non_basic)
  160. -- name:need,have,tier
  161. local non_basic_sorted = {}
  162. for k in pairs(non_basic) do table.insert(non_basic_sorted,k) end
  163. table.sort(non_basic_sorted, function (a,b) return non_basic[a][3] < non_basic[b][3] end)
  164. non_basic_temp = tableDeepCopy(non_basic)
  165. for _,k in ipairs(non_basic_sorted) do
  166. if non_basic[k][2]>0 then
  167. non_basic_temp[k][1]=non_basic_temp[k][1]-non_basic[k][2]
  168. if non_basic_temp[k][1] < 0 then non_basic_temp[k][1] = 0 end
  169. local diff = non_basic_temp[k][1]-non_basic[k][1]
  170. for slot,item in pairs(recipes[k]) do
  171.  
  172. if basic[item] then
  173. basic[item][1] = basic[item][1] + diff
  174. if basic[item][1] < 0 then basic[item][1] = 0 end
  175. else
  176. if non_basic[item] then
  177. non_basic_temp[item][1] = non_basic_temp[item][1]+diff
  178. non_basic_temp[item][2] = non_basic_temp[item][2]+diff
  179. end
  180. end
  181. end
  182. end
  183. end
  184. return basic, non_basic_temp
  185. end
  186.  
  187. local function checkCraftingAvailability(craft_table)
  188. local basic = {}
  189. local non_basic = {}
  190. for name, values in pairs(craft_table) do
  191. if database_keys[name] then
  192. print('here',name)
  193. available = database_keys[name][3]
  194. if available >= values[1] then
  195. values[2] = values[1]
  196. else
  197. values[2] = available
  198. end
  199. if recipes[name] then
  200. non_basic[name]=values
  201. else
  202. basic[name]=values
  203. end
  204. else
  205. if recipes[name] then
  206. non_basic[name]=values
  207. else
  208. basic[name]=values
  209. end
  210. end
  211. end
  212. -- print('Before')
  213. -- for k,v in pairs(non_basic) do print(k,v[1],v[2],v[3]) end
  214. basic2, non_basic2 = reconcileAvailableItems(basic,non_basic)
  215. -- print('After')
  216. -- for k,v in pairs(non_basic) do print(k,v[1],v[2],v[3]) end
  217. local missing = {}
  218. for name,values in pairs(basic2) do
  219. if values[2]-values[1] < 0 then
  220. missing[name] = values[1]-values[2]
  221. end
  222. end
  223. return missing, basic2, non_basic2
  224. end
  225.  
  226.  
  227.  
  228. local function moveToBuffer(label,amount)
  229. output_slot = 0
  230. for slot =1, comp.transposer.getInventorySize(TARGETSIDE) do
  231. item = comp.transposer.getStackInSlot(TARGETSIDE, slot)
  232. if item then
  233. if item.label == label then
  234. output_slot = slot
  235. break
  236. end
  237. else
  238. output_slot = slot
  239. break
  240. end
  241. end
  242.  
  243. if database_keys[label] then
  244. comp.me_exportbus.setExportConfiguration(TARGETSIDE,output_slot,databases[database_keys][label][1],databases[database_keys][label][2])
  245. if database_keys[label][3] >= amount then
  246. for _ = 1,amount do
  247. comp.me_exportbus.exportIntoSlot(TARGETSIDE,output_slot)
  248. end
  249. else
  250. for _ = 1,database_keys[label][3] do
  251. comp.me_exportbus.exportIntoSlot(TARGETSIDE,output_slot)
  252. end
  253. end
  254. end
  255. end
  256.  
  257. getDatabases()
  258. --clearDatabases()
  259. --fillDatabases()
  260. getRecipes()
  261. print("Looking up items in network...")
  262. databaseLookUp()
  263. print("Crafting")
  264. name = "Machine Frame"
  265. crafting = buildCraftingTree(name,2)
  266. missing,basic,non_basic = checkCraftingAvailability(crafting)
  267.  
  268.  
  269.  
  270. --print("Crafting requirements are:")
  271. --print("Name\tNeeded\tAvailable")
  272. --for key,value in pairs(basic) do
  273. -- print(key,'\t',value[1],'\t',value[2])
  274. --end
  275. --print("Will be used to craft:")
  276. --print("Name\tNeeded\tAvailable")
  277. --for key,value in pairs(non_basic) do
  278. -- print(key,'\t',value[1],'\t',value[2])
  279. --end
  280. --print("Missing ingredients:")
  281. --print("Name\tMissing")
  282. --for key,value in pairs(missing) do
  283. -- print(key,'\t',value)
  284. --end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement