funkd0ct0r

Untitled

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