Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 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. local function tableDeepCopy(orig)
  144. output = {}
  145. for k,v in pairs(orig) do
  146. output[k] = v
  147. end
  148. return output
  149. end
  150.  
  151. local function reconcileAvailableItems(basic,non_basic)
  152. -- name:need,have,tier
  153. local non_basic_sorted = {}
  154. for k in pairs(non_basic) do table.insert(non_basic_sorted,k) end
  155. table.sort(non_basic_sorted, function (a,b) return non_basic[a][3] < non_basic[b][3] end)
  156. local non_basic_temp = tableDeepCopy(non_basic)
  157. for _,k in ipairs(non_basic_sorted) do
  158. if non_basic[k][2]>0 then
  159.  
  160. non_basic_temp[k][1]=non_basic_temp[k][1]-non_basic[k][2]
  161. if non_basic_temp[k][1] < 0 then non_basic_temp[k][1] = 0 end
  162. local diff = non_basic_temp[k][1]-non_basic[k][1]
  163. print('diff',diff)
  164. for slot,item in pairs(recipes[k]) do
  165.  
  166. if basic[item] then
  167. print(item,basic[item][1])
  168. basic[item][1] = basic[item][1] + diff
  169. print(item,basic[item][1])
  170. if basic[item][1] < 0 then basic[item][1] = 0 end
  171. else
  172. if non_basic[item] then
  173. non_basic_temp[item][1] = non_basic_temp[item][1]+diff
  174. end
  175. end
  176. end
  177. end
  178. end
  179. return basic, non_basic_temp
  180. end
  181.  
  182. local function checkCraftingAvailability(craft_table)
  183. local basic = {}
  184. local non_basic = {}
  185. for name, values in pairs(craft_table) do
  186. if database_keys[name] then
  187. print('here',name)
  188. available = database_keys[name][3]
  189. if available >= values[1] then
  190. values[2] = values[1]
  191. else
  192. values[2] = available
  193. end
  194. if recipes[name] then
  195. non_basic[name]=values
  196. else
  197. basic[name]=values
  198. end
  199. else
  200. non_basic[name]=values
  201. end
  202. end
  203. basic2, non_basic2 = reconcileAvailableItems(basic,non_basic)
  204. local missing = {}
  205. for name,values in pairs(basic2) do
  206. if values[2]-values[1] < 0 then
  207. missing[name] = values[1]-values[2]
  208. end
  209. end
  210. return missing, basic2, non_basic2
  211. end
  212.  
  213.  
  214.  
  215. local function moveToBuffer(label,amount)
  216. output_slot = 0
  217. for slot =1, comp.transposer.getInventorySize(TARGETSIDE) do
  218. item = comp.transposer.getStackInSlot(TARGETSIDE, slot)
  219. if item then
  220. if item.label == label then
  221. output_slot = slot
  222. break
  223. end
  224. else
  225. output_slot = slot
  226. break
  227. end
  228. end
  229.  
  230. if database_keys[label] then
  231. comp.me_exportbus.setExportConfiguration(TARGETSIDE,output_slot,databases[database_keys][label][1],databases[database_keys][label][2])
  232. if database_keys[label][3] >= amount then
  233. for _ = 1,amount do
  234. comp.me_exportbus.exportIntoSlot(TARGETSIDE,output_slot)
  235. end
  236. else
  237. for _ = 1,database_keys[label][3] do
  238. comp.me_exportbus.exportIntoSlot(TARGETSIDE,output_slot)
  239. end
  240. end
  241. end
  242. end
  243. --clearDatabases()
  244. --fillDatabases()
  245. getDatabases()
  246. getRecipes()
  247. print("Looking up items in network...")
  248. databaseLookUp()
  249. print("Crafting")
  250. crafting = buildCraftingTree("Machine Frame",2)
  251. missing,basic,non_basic = checkCraftingAvailability(crafting)
  252.  
  253. print("Crafting requirements are:")
  254. print("Name\tNeeded\tAvailable")
  255. for key,value in pairs(basic) do
  256. print(key,'\t',value[1],'\t',value[2])
  257. end
  258. print("Will be used to craft:")
  259. print("Name\tNeeded\tAvailable")
  260. for key,value in pairs(non_basic) do
  261. print(key,'\t',value[1],'\t',value[2])
  262. end
  263. print("Missing ingredients:")
  264. print("Name\tMissing")
  265. for key,value in pairs(missing) do
  266. print(key,'\t',value)
  267. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement