funkd0ct0r

Untitled

Sep 5th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.83 KB | None | 0 0
  1. -- Turtle Interface for Applied Energistics
  2. -- place a Turtle next to a ME Interface,
  3. -- place a Trash Can next to the ME Interface
  4.  
  5. -- all configuration is done in the application
  6. -- the Turtle can craft items up to a limit
  7. -- or void items down to a limit
  8. -- fuzzy void works with vanilla armor and potions etc
  9.  
  10. -- ENTER for options, SPACE to stop page, ANY key for next page
  11. -- released into the public domain
  12. --
  13.  
  14. print("booting...")
  15. sleep(1)
  16. print("initializing...")
  17.  
  18.  
  19. --todo
  20. --testing batch program, display_names, and new crafting loop (also when missing items)
  21. --add 4color render to header line etc
  22. --sometimes startup crashes on world load and needs to be restart manually, even afer sleeping
  23. --a seperate table could be indexed by integer order_num, to accelerate searches and maybe display during loop
  24. --a crafty turtle could craft compressed cobble or basically anything, if i knew the export direction to turtle
  25.  
  26. saveFile = "meautosettings"
  27.  
  28. fuzzy_list = {}
  29. craft_list = {}
  30. void_list = {}
  31. cur_crafting_index = 0
  32. reserve_cpus = 0
  33. width,height = term.getSize()
  34.  
  35. function options()
  36. while true do
  37. term.clear()
  38. term.setCursorPos(1,1)
  39. print("1: Auto-Craft an Item")
  40. print("2: Void an Item")
  41. print("3: Fuzzy Void an Item")
  42. print("4: Remove an Order #")
  43. print("5: Change Interface Sides")
  44. print("6: Back to Main Menu")
  45. print("")
  46. print("Enter your choice: ")
  47. choice = tonumber(io.read())
  48. if choice and choice >= 1 and choice <= 6 then
  49. break
  50. end
  51. end
  52.  
  53. if choice == 6 then
  54. return
  55. end
  56. if choice == 1 then
  57. term.clear()
  58. term.setCursorPos(1,1)
  59. print("Place item to be crafted in slot 1")
  60. print("Press ENTER to continue")
  61. io.read()
  62.  
  63. item_list = {}
  64. for slot = 1, 16 do
  65. d = turtle.getItemDetail(1)
  66. if d then
  67. table.insert(item_list, d)
  68. end
  69. end
  70. if #item_list == 0 then
  71. return
  72. end
  73.  
  74. print("Enter amount to auto-craft")
  75. amt = tonumber(io.read())
  76. print("Enter batch size to craft (with available memory and materials)")
  77. batch = tonumber(io.read())
  78.  
  79. while true do
  80. for k,v in pairs(item_list) do
  81. print("Auto-Craft " .. amt .. " " .. v.name .. ":" .. v.damage .. " with batchsize " .. batch)
  82. end
  83. print("Is this correct? (y/n)")
  84. correct = io.read()
  85. if correct == "y" or correct == "n" then
  86. break
  87. end
  88. end
  89. if correct == "n" then
  90. return
  91. end
  92. for k,v in pairs(item_list) do
  93. longname = v.name .. ":" .. v.damage
  94. craft_list[longname] = {}
  95. craft_list[longname].id = v.name
  96. craft_list[longname].dmg = v.damage
  97. craft_list[longname].qty = amt
  98. craft_list[longname].batch = batch
  99. craft_list[longname].longname = longname
  100. craft_list[longname].display_name = ""
  101. end
  102. return
  103. end
  104.  
  105. if choice == 2 then
  106. term.clear()
  107. term.setCursorPos(1,1)
  108. print("Place item to be voided in slot 1")
  109. print("Press ENTER to continue")
  110. io.read()
  111.  
  112. item_list = {}
  113. for slot = 1, 16 do
  114. d = turtle.getItemDetail(1)
  115. if d then
  116. table.insert(item_list, d)
  117. end
  118. end
  119. if #item_list == 0 then
  120. return
  121. end
  122.  
  123. print("Enter amount to keep before voiding")
  124. amt = tonumber(io.read())
  125.  
  126. while true do
  127. for k,v in pairs(item_list) do
  128. print("Void " .. v.name .. ":" .. v.damage .. " when qty reaches " .. amt)
  129. end
  130. print("Is this correct? (y/n)")
  131. correct = io.read()
  132. if correct == "y" or correct == "n" then
  133. break
  134. end
  135. end
  136. if correct == "n" then
  137. return
  138. end
  139. for k,v in pairs(item_list) do
  140. longname = v.name .. ":" .. v.damage
  141. void_list[longname] = {}
  142. void_list[longname].id = v.name
  143. void_list[longname].dmg = v.damage
  144. void_list[longname].qty = amt
  145. void_list[longname].longname = longname
  146. void_list[longname].display_name = ""
  147. end
  148. return
  149. end
  150.  
  151. if choice == 3 then
  152. term.clear()
  153. term.setCursorPos(1,1)
  154. print("Place item to be fuzzy voided in slot 1")
  155. print("Press ENTER to continue")
  156. io.read()
  157.  
  158. item_list = {}
  159. for slot = 1, 16 do
  160. d = turtle.getItemDetail(1)
  161. if d then
  162. table.insert(item_list, d)
  163. end
  164. end
  165. if #item_list == 0 then
  166. return
  167. end
  168.  
  169. print("Enter amount to keep before voiding")
  170. amt = tonumber(io.read())
  171.  
  172. while true do
  173. for k,v in pairs(item_list) do
  174. print("Fuzzy Void " .. v.name .. " when qty reaches " .. amt)
  175. end
  176. print("Is this correct? (y/n)")
  177. correct = io.read()
  178. if correct == "y" or correct == "n" then
  179. break
  180. end
  181. end
  182. if correct == "n" then
  183. return
  184. end
  185. for k,v in pairs(item_list) do
  186. fuzzy_list[v.name] = {}
  187. fuzzy_list[v.name].id = v.name
  188. fuzzy_list[v.name].qty = amt
  189. fuzzy_list[v.name].display_name = ""
  190. end
  191. return
  192. end
  193.  
  194. if choice == 4 then
  195. term.clear()
  196. term.setCursorPos(1,1)
  197. print("Enter Order # to remove")
  198. ordernum = tonumber(io.read())
  199.  
  200. index = 1
  201. for k,v in pairs(craft_list) do
  202. if index == ordernum then
  203. while true do
  204. print("Remove this Item:")
  205. print("Auto-Craft " .. v.qty .. " " .. v.id .. ":" .. v.dmg .. " with batchsize " .. v.batch)
  206. print("Is this correct? (y/n)")
  207. correct = io.read()
  208. if correct == "y" or correct == "n" then
  209. break
  210. end
  211. end
  212. if correct == "n" then
  213. return
  214. end
  215. craft_list[v.longname] = nil
  216. return
  217. end
  218. index = index + 1
  219. end
  220. for k,v in pairs(void_list) do
  221. if index == ordernum then
  222. while true do
  223. print("Remove this Item:")
  224. print("Void " .. v.id .. ":" .. v.dmg .. " when qty reaches " .. v.qty)
  225. print("Is this correct? (y/n)")
  226. correct = io.read()
  227. if correct == "y" or correct == "n" then
  228. break
  229. end
  230. end
  231. if correct == "n" then
  232. return
  233. end
  234. void_list[v.longname] = nil
  235. return
  236. end
  237. index = index + 1
  238. end
  239. for k,v in pairs(fuzzy_list) do
  240. if index == ordernum then
  241. while true do
  242. print("Remove this Item:")
  243. print("Fuzzy Void " .. v.id .. " when qty reaches " .. v.qty)
  244. print("Is this correct? (y/n)")
  245. correct = io.read()
  246. if correct == "y" or correct == "n" then
  247. break
  248. end
  249. end
  250. if correct == "n" then
  251. return
  252. end
  253. fuzzy_list[v.id] = nil
  254. return
  255. end
  256. index = index + 1
  257. end
  258. print("Order # not found")
  259. io.read()
  260. return
  261. end
  262. if choice == 5 then
  263. configOptions()
  264. return
  265. end
  266. end
  267.  
  268. function configOptions()
  269. while true do
  270. while true do
  271. term.clear()
  272. term.setCursorPos(1,1)
  273. print("Enter the side the ME Interface is on, from the turtle")
  274. print("")
  275. print("front, back, left, right")
  276. print("top, or bottom")
  277. print("")
  278. w = tostring(io.read())
  279. if w == "front" or w == "back" or w == "left" or w == "right" or w == "top" or w == "bottom" then
  280. break
  281. end
  282. end
  283. while true do
  284. term.clear()
  285. term.setCursorPos(1,1)
  286. print("Enter the direction from ME Interface to the trashcan")
  287. print("")
  288. print("north, south, east, west, up, or down")
  289. print("")
  290. e = tostring(io.read())
  291. if e == "north" or e == "south" or e == "east" or e == "west" or e == "up" or e == "down" then
  292. break
  293. end
  294. end
  295. i = peripheral.wrap(w)
  296. c = false
  297. if i and i.canExport then
  298. c = i.canExport(e)
  299. end
  300.  
  301. term.clear()
  302. term.setCursorPos(1,1)
  303. print("Interface Side: " .. w)
  304. term.setCursorPos(width - 9, 1)
  305. if i ~= nil and i.canExport then
  306. print(" FOUND")
  307. else
  308. print("NOT FOUND")
  309. end
  310. term.setCursorPos(1,2)
  311. print("Trash Direction: " .. e)
  312. term.setCursorPos(width - 9, 2)
  313. if c == true then
  314. print(" FOUND")
  315. else
  316. print("NOT FOUND")
  317. end
  318. print("")
  319. while true do
  320. print("Is this correct? (y/n)")
  321. correct = io.read()
  322. if correct == "y" or correct == "n" then
  323. break
  324. end
  325. end
  326. if correct == "y" then
  327. interface_wrap_side = w
  328. interface_export_dir = e
  329. interface = peripheral.wrap(interface_wrap_side)
  330. break
  331. end
  332. end
  333. end
  334.  
  335. print_line = 1
  336. function printScreen()
  337. term.clear()
  338. term.setCursorPos(1,1)
  339. print("Order# type Item ID:Dmg cur max")
  340.  
  341. lines_printed = 0
  342. lines_to_print = height - 2
  343. index = 1
  344. for k,v in pairs(craft_list) do
  345. if lines_printed < lines_to_print and index == print_line then
  346. term.setCursorPos(1,2+lines_printed)
  347. if v.display_name == "" then
  348. term.write(index .. " craft " .. v.longname)
  349. else
  350. term.write(index .. " craft " .. v.display_name)
  351. end
  352. right = " " .. v.cur_qty .. " " .. v.qty
  353. term.setCursorPos(width + 1 - #right, 2+lines_printed)
  354. term.write(right)
  355. print_line = print_line + 1
  356. lines_printed = lines_printed + 1
  357. end
  358. index = index + 1
  359. end
  360. for k,v in pairs(void_list) do
  361. if lines_printed < lines_to_print and index == print_line then
  362. term.setCursorPos(1,2+lines_printed)
  363. if v.display_name == "" then
  364. term.write(index .. " void " .. v.longname)
  365. else
  366. term.write(index .. " void " .. v.display_name)
  367. end
  368. right = " " .. v.cur_qty .. " " .. v.qty
  369. term.setCursorPos(width + 1 - #right, 2+lines_printed)
  370. term.write(right)
  371. print_line = print_line + 1
  372. lines_printed = lines_printed + 1
  373. end
  374. index = index + 1
  375. end
  376. for k,v in pairs(fuzzy_list) do
  377. if lines_printed < lines_to_print and index == print_line then
  378. term.setCursorPos(1,2+lines_printed)
  379. if v.display_name == "" then
  380. term.write(index .. " fuzzy " .. v.id)
  381. else
  382. term.write(index .. " fuzzy " .. v.display_name)
  383. end
  384. right = " " .. v.cur_qty .. " " .. v.qty
  385. term.setCursorPos(width + 1 - #right, 2+lines_printed)
  386. term.write(right)
  387. print_line = print_line + 1
  388. lines_printed = lines_printed + 1
  389. end
  390. index = index + 1
  391. end
  392. if print_line >= index then
  393. print_line = 1
  394. end
  395.  
  396. term.setCursorPos(1,height)
  397. term.write("press ENTER for options")
  398. end
  399.  
  400. local function loadSettings()
  401. linenum = 0
  402. if fs.exists(saveFile) then
  403. local file = fs.open(saveFile,"r")
  404.  
  405. interface_wrap_side = tostring(file.readLine())
  406. interface_export_dir = tostring(file.readLine())
  407. while true do
  408. input = file.readLine()
  409. linenum = linenum + 1
  410. if input == nil then
  411. break
  412. end
  413. if input == "craft" then
  414. item = {}
  415. item.id = tostring(file.readLine())
  416. item.dmg = tonumber(file.readLine())
  417. item.qty = tonumber(file.readLine())
  418. item.batch = tonumber(file.readLine())
  419. item.longname = item.id .. ":" .. item.dmg
  420. item.display_name = tostring(file.readLine())
  421. craft_list[item.longname] = item
  422. linenum = linenum + 4
  423. elseif input == "void" then
  424. item = {}
  425. item.id = tostring(file.readLine())
  426. item.dmg = tonumber(file.readLine())
  427. item.qty = tonumber(file.readLine())
  428. item.longname = item.id .. ":" .. item.dmg
  429. item.display_name = tostring(file.readLine())
  430. void_list[item.longname] = item
  431. linenum = linenum + 3
  432. elseif input == "fuzzy" then
  433. item = {}
  434. item.id = tostring(file.readLine())
  435. item.qty = tonumber(file.readLine())
  436. item.display_name = tostring(file.readLine())
  437. fuzzy_list[item.id] = item
  438. linenum = linenum + 2
  439. else
  440. print("Settings load failed at line " .. linenum)
  441. sleep(3)
  442. break
  443. end
  444. end
  445. file.close()
  446. return true
  447. end
  448. return false
  449. end
  450.  
  451. local function saveSettings()
  452. local file = fs.open(saveFile,"w")
  453.  
  454. file.write(tostring(interface_wrap_side).."\n")
  455. file.write(tostring(interface_export_dir).."\n")
  456. for k,v in pairs(craft_list) do
  457. file.write("craft".."\n")
  458. file.write(tostring(v.id).."\n")
  459. file.write(tostring(v.dmg).."\n")
  460. file.write(tostring(v.qty).."\n")
  461. file.write(tostring(v.batch).."\n")
  462. file.write(tostring(v.display_name).."\n")
  463. end
  464. for k,v in pairs(void_list) do
  465. file.write("void".."\n")
  466. file.write(tostring(v.id).."\n")
  467. file.write(tostring(v.dmg).."\n")
  468. file.write(tostring(v.qty).."\n")
  469. file.write(tostring(v.display_name).."\n")
  470. end
  471. for k,v in pairs(fuzzy_list) do
  472. file.write("fuzzy".."\n")
  473. file.write(tostring(v.id).."\n")
  474. file.write(tostring(v.qty).."\n")
  475. file.write(tostring(v.display_name).."\n")
  476. end
  477.  
  478. file.close()
  479. end
  480.  
  481. if not loadSettings() then
  482. configOptions()
  483. saveSettings()
  484. else
  485. interface = peripheral.wrap(interface_wrap_side)
  486. if not interface then
  487. configOptions()
  488. saveSettings()
  489. end
  490. end
  491.  
  492. turtle.select(1)
  493. while true do
  494.  
  495. --clear cur amounts in craft_list
  496. for i,c in pairs(craft_list) do
  497. c.cur_qty = 0
  498. c.is_craftable = false
  499. end
  500. --clear cur amounts in void_list
  501. for i,v in pairs(void_list) do
  502. v.cur_qty = 0
  503. end
  504. --clear cur amounts in fuzzy_list
  505. for i,v in pairs(fuzzy_list) do
  506. v.cur_qty = 0
  507. end
  508.  
  509. avail = interface.getAvailableItems()
  510. for k,v in pairs(avail) do
  511. item = craft_list[v.fingerprint.id .. ":" .. v.fingerprint.dmg]
  512. if item then
  513. if item.display_name == "" then
  514. d = interface.getItemDetail{v.fingerprint)
  515. if d then
  516. item.display_name = d.basic().display_name
  517. end
  518. end
  519. item.cur_qty = item.cur_qty + v.size
  520. if v.is_craftable then
  521. item.is_craftable = true
  522. end
  523. end
  524. item = void_list[v.fingerprint.id .. ":" .. v.fingerprint.dmg]
  525. if item then
  526. if item.display_name == "" then
  527. d = interface.getItemDetail{v.fingerprint)
  528. if d then
  529. item.display_name = d.basic().display_name
  530. end
  531. end
  532. item.cur_qty = item.cur_qty + v.size
  533. while item.cur_qty - item.qty > 0 do
  534. qty = math.min(item.cur_qty - item.qty, 64)
  535. item.cur_qty = item.cur_qty - qty
  536. interface.exportItem(v.fingerprint, interface_export_dir, qty)
  537. end
  538. end
  539. item = fuzzy_list[v.fingerprint.id]
  540. if item then
  541. if item.display_name == "" then
  542. d = interface.getItemDetail{v.fingerprint)
  543. if d then
  544. item.display_name = d.basic().display_name
  545. end
  546. end
  547. item.cur_qty = item.cur_qty + v.size
  548. while item.cur_qty - item.qty > 0 do
  549. qty = math.min(item.cur_qty - item.qty, 64)
  550. item.cur_qty = item.cur_qty - qty
  551. interface.exportItem(v.fingerprint, interface_export_dir, qty)
  552. end
  553. end
  554. end
  555.  
  556. cpus = interface.getCraftingCPUs()
  557. cpus_available = -reserve_cpus
  558. for k,v in pairs(cpus) do
  559. if v.busy == false then
  560. cpus_available = cpus_available + 1
  561. end
  562. end
  563. --this might count recipes that are uncraftable because lack of resources
  564. --so it may also decrement avail_cpus tho request is failing
  565. --testing return of request should help
  566. if cpus_available > 0 then
  567. recipe_index = 0
  568. for i,c in pairs(craft_list) do
  569. if c.is_craftable and c.qty - c.cur_qty > 0 then
  570. if cur_crafting_index == recipe_index then
  571. qty = math.min(c.batch, c.qty - c.cur_qty)
  572. interface.requestCrafting(c, qty)
  573.  
  574. cur_crafting_index = cur_crafting_index + 1
  575. cpus_available = cpus_available - 1
  576. if cpus_available <= 0 then
  577. break
  578. end
  579. end
  580. recipe_index = recipe_index + 1
  581. end
  582. end
  583. if cpus_available > 0 then
  584. cur_crafting_index = 0
  585. end
  586. end
  587.  
  588. printScreen()
  589. mytimer = os.startTimer(4)
  590. while true do
  591. local event, key = os.pullEvent()
  592. if event == "key" then
  593. if key == 28 then --ENTER
  594. os.cancelTimer(mytimer)
  595. options()
  596. saveSettings()
  597. break
  598. elseif key == 57 then --SPACE
  599. os.cancelTimer(mytimer)
  600. mytimer = os.startTimer(4)
  601. else
  602. os.cancelTimer(mytimer)
  603. break
  604. end
  605. end
  606. if event == "timer" then
  607. break
  608. end
  609. end
  610. end
Advertisement
Add Comment
Please, Sign In to add comment