Advertisement
Guest User

Untitled

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