Advertisement
Miner8149

AutoCraft

Jun 24th, 2020 (edited)
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.02 KB | None | 0 0
  1. --AutoCraft
  2. --Miner8149
  3. --THIS IS A WORK IN PROGRESS FOR COMPUTERCRAFT 1.7.10
  4.  
  5.  
  6. --local craftsActive = false --getCrafting
  7. os.loadAPI("button")
  8. local config = {} --pullConfig()
  9. local keys = {}
  10. local monitorSide = "top"
  11. local chestSide = "back"
  12. local interfaceSide = "bottom"
  13. local progActive = true -- needs to get from file
  14. local functionName = mainScreen
  15. local param = ""
  16.  
  17. interface = peripheral.wrap(interfaceSide)
  18. c = peripheral.wrap(chestSide)
  19.  
  20. m = peripheral.wrap(monitorSide)
  21. m.clear()
  22. m.setCursorPos(1, 1)
  23. m.setTextScale(0.5)
  24. local xMax, yMax = m.getSize()
  25. local centerLeft = tonumber(string.format("%d", xMax/2.0))
  26. local leftAlign = tonumber(string.format("%d", centerLeft/2.0))
  27. local listMax = tonumber(string.format("%d", ((yMax-9)/4.0)))
  28.  
  29. --funtion to add to table
  30. --funtion to remove from table
  31. --tracking how big list is actively
  32.  
  33. --config[items["display_name"]]= { Fingerprint = items}
  34. --config[items["display_name"]]["Availiable"] = (interface.getItemDetail(config[items["display_name"]]["Fingerprint"],false))["qty"]
  35.  
  36. --*****************************************************************
  37. --*****************************************************************
  38.  
  39. function refresh()  --refresh variables and clear screen for new print
  40.     m.clear()
  41.     m.setCursorPos(1, 1)
  42.     m.setTextScale(0.5)
  43.     button.clearTable()
  44.     xMax, yMax = m.getSize()
  45.     centerLeft = tonumber(string.format("%d", xMax/2.0))
  46.     leftAlign = tonumber(string.format("%d", centerLeft/2.0))
  47.     listMax = tonumber(string.format("%d", ((yMax-9)/4.0)))
  48. end
  49.  
  50. --*****************************************************************
  51.  
  52. function get_Keys() -- gets keys from config table
  53.     local n = 0
  54.     for k, v in pairs(config) do
  55.         n=n+1
  56.         keys[n] = k
  57.     end
  58.     table.sort(keys)
  59. end
  60.  
  61. --*****************************************************************
  62.  
  63. function get_amounts()
  64.     --gets amounts of items for disignated crafting list
  65. end
  66.  
  67. --*****************************************************************
  68.  
  69. function getCrafting()-- determines if anything is crafting
  70.     local array = {}
  71.     array = interface.getCraftingCPUs()
  72.     for _xx, _yy in pairs(array) do
  73.         if (array[_xx]["busy"] == true) then
  74.             return true
  75.         end
  76.     end
  77.     return false
  78. end
  79.  
  80. --*****************************************************************
  81.  
  82. function makeCrafts() -- request crafts for the first 8 things that are low
  83.     print("making crafts")
  84. end
  85.  
  86. --*****************************************************************
  87.  
  88. function pullConfig() --load config into table
  89.     local file = fs.open(configAuto,"r")
  90.     local data = file.readAll()
  91.     file.close()
  92.     return textutils.unserialize(data)
  93. end
  94.  
  95. --*****************************************************************
  96.  
  97. function saveConfig() --Save config table to file
  98.     local file = fs.open(configAuto,"w")
  99.     file.write(textutils.serialize(config))
  100.     file.close()
  101. end
  102.  
  103. --*****************************************************************
  104.  
  105. function printLines()
  106.     get_Keys()
  107.     local inc = 0
  108.     local yLine = 7
  109.     for xk in keys do
  110.         if((inc < listMax) and (yMax>22)) then
  111.             m.setCursorPos(2, yLine+(inc*3))
  112.             m.write(xk)
  113.             m.setCursorPos(xMax-22, yLine+(inc*3))
  114.             m.write("0000")
  115.             m.setCursorPos(xMax-17, yLine+(inc*3))
  116.             m.write("/")
  117.             m.setCursorPos(xMax-15, yLine+(inc*3))
  118.             m.write("1111")
  119.             button.setTable("editList"..tostring(inc), "Edit", editNumbers, xk, xMax-7, xMax-1, yLine+(inc*3)-1, yLine+(inc*3)+1)
  120.  
  121.  
  122.             inc=inc+1
  123.         else
  124.             break
  125.         end
  126.     end
  127.     print(listMax)
  128. end
  129. --*****************************************************************
  130.  
  131. function mainScreen() -- main screen
  132.     refresh()
  133.     functionName = mainScreen
  134.     param = ""
  135.     button.setTable("Power", "On/Off", shutdown, "", xMax-8, xMax-1, 2, 4)
  136.     button.setButton("Power", progActive)
  137.     button.setTable("Edit", "Edit", addANDRemoveScreen, "", 2, 7, yMax-3, yMax-1)
  138.     button.setTable("Up", " /\\ ", up, "", centerLeft, centerLeft+3, 2, 4)
  139.     button.setTable("Down", " \\/ ", down, "", centerLeft, centerLeft+3, yMax-3, yMax-1)
  140.     printLines()
  141.     button.screen()
  142. end
  143.  
  144. --*****************************************************************
  145.  
  146. function addANDRemoveScreen() -- screen for editing list
  147.     refresh()
  148.     functionName = addANDRemoveScreen
  149.     param = ""
  150.     button.setTable("Up", " /\\ ", up, "", centerLeft-1, centerLeft+3, 2, 4)
  151.     button.setTable("Down", " \\/ ", down, "", centerLeft-1, centerLeft+3, yMax-3, yMax-1)
  152.     button.setTable("Add", "Add", addNew, "", 2, 6, yMax-3, yMax-1)
  153.     button.setTable("Done", "Done", mainScreen, "", xMax-8, xMax-1, yMax-3, yMax-1)
  154.     printLines()
  155.     button.screen()
  156. end
  157.  
  158. --*****************************************************************
  159. function addNew()
  160.     button.flash("Add")
  161.     refresh()
  162.     functionName = addNew
  163.     param = ""
  164.    
  165.     if(yMax>25) then
  166.         button.setTable("plusOne",  " +1 ", plus1,   config[itemKey]["Target"], centerLeft-17, centerLeft-11, yMax-14, yMax-12)
  167.         button.setTable("plusTen",  "+10",  plus10,  config[itemKey]["Target"], centerLeft-9, centerLeft-4, yMax-14, yMax-12)
  168.         button.setTable("plus100",  "+100", plus100, config[itemKey]["Target"], centerLeft-2, centerLeft+3, yMax-14, yMax-12)
  169.         button.setTable("plusOneK", "+1K",  plus1K,  config[itemKey]["Target"], centerLeft+5, centerLeft+9, yMax-14, yMax-12)
  170.         button.setTable("plusTenK", "+10K", plus10K, config[itemKey]["Target"], centerLeft+11, centerLeft+17, yMax-14, yMax-12)
  171.         --
  172.         button.setTable("minusOne",  " -1 ", minus1,   config[itemKey]["Target"], centerLeft-17, centerLeft-11, yMax-8, yMax-6)
  173.         button.setTable("minusTen",  "-10",  minus10,  config[itemKey]["Target"], centerLeft-9, centerLeft-4, yMax-8, yMax-6)
  174.         button.setTable("minus100",  "-100", minus100, config[itemKey]["Target"], centerLeft-2, centerLeft+3, yMax-8, yMax-6)
  175.         button.setTable("minusOneK", "-1k",  minus1K,  config[itemKey]["Target"], centerLeft+5, centerLeft+9, yMax-8, yMax-6)
  176.         button.setTable("minusTenK", "-10k", minus10K, config[itemKey]["Target"], centerLeft+11, centerLeft+17, yMax-8, yMax-6)
  177.     end
  178.     button.setTable("Save", "Save", save, "", centerLeft-2, centerLeft+3, yMax-3, yMax-1)
  179.     button.setTable("Cancel", "Cancel", cancel, "", xMax-9, xMax-1, yMax-3, yMax-1)     -- cancel button
  180.    
  181. end
  182.  
  183. --*****************************************************************
  184. function editNumbers(itemKey) -- can add or subtract from numbers here
  185.     -- display item name at top, with curent amount, and Target
  186.  
  187.     --config[itemKey]["Target"]
  188.     refresh()
  189.     functionName = editNumbers
  190.     param = ""
  191.     m.setCursorPos(1, 1)
  192.     m.write(itemKey)
  193.     m.setCursorPos(1, 2)
  194.     m.write(config[itemKey]["Availiable"])
  195.     m.setCursorPos(1, 3)
  196.     m.write(config[itemKey]["Target"])
  197.     if(yMax>25) then
  198.         button.setTable("plusOne",  " +1 ", plus1,   config[itemKey]["Target"], centerLeft-17, centerLeft-11, yMax-14, yMax-12)
  199.         button.setTable("plusTen",  "+10",  plus10,  config[itemKey]["Target"], centerLeft-9, centerLeft-4, yMax-14, yMax-12)
  200.         button.setTable("plus100",  "+100", plus100, config[itemKey]["Target"], centerLeft-2, centerLeft+3, yMax-14, yMax-12)
  201.         button.setTable("plusOneK", "+1K",  plus1K,  config[itemKey]["Target"], centerLeft+5, centerLeft+9, yMax-14, yMax-12)
  202.         button.setTable("plusTenK", "+10K", plus10K, config[itemKey]["Target"], centerLeft+11, centerLeft+17, yMax-14, yMax-12)
  203.         --
  204.         button.setTable("minusOne",  " -1 ", minus1,   config[itemKey]["Target"], centerLeft-17, centerLeft-11, yMax-8, yMax-6)
  205.         button.setTable("minusTen",  "-10",  minus10,  config[itemKey]["Target"], centerLeft-9, centerLeft-4, yMax-8, yMax-6)
  206.         button.setTable("minus100",  "-100", minus100, config[itemKey]["Target"], centerLeft-2, centerLeft+3, yMax-8, yMax-6)
  207.         button.setTable("minusOneK", "-1k",  minus1K,  config[itemKey]["Target"], centerLeft+5, centerLeft+9, yMax-8, yMax-6)
  208.         button.setTable("minusTenK", "-10k", minus10K, config[itemKey]["Target"], centerLeft+11, centerLeft+17, yMax-8, yMax-6)
  209.     end
  210.     button.setTable("Save", "Save", save, "", centerLeft-2, centerLeft+3, yMax-3, yMax-1)
  211.     button.setTable("Cancel", "Cancel", cancel, "", xMax-9, xMax-1, yMax-3, yMax-1)     -- cancel button
  212.  
  213.     button.screen()
  214. end
  215.  
  216. --*****************************************************************
  217.  
  218. function cancel()
  219.     button.flash("Cancel")
  220.     mainScreen()
  221. end
  222.  
  223. --*****************************************************************
  224.  
  225. function save()
  226.     button.flash("Save")
  227.     mainScreen()
  228. end
  229.  
  230. --*****************************************************************
  231.  
  232. function plus1(var)
  233.     button.flash("plusOne")
  234.     var=var+1
  235. end
  236.  
  237. --*****************************************************************
  238.  
  239. function plus10(var)
  240.     button.flash("plusTen")
  241.     var=var+10
  242. end
  243.  
  244. --*****************************************************************
  245.  
  246. function plus100(var)
  247.     button.flash("plus100")
  248.     var=var+100
  249. end
  250.  
  251. --*****************************************************************
  252.  
  253. function plus1K(var)
  254.     button.flash("plusOneK")
  255.     var=var+1000
  256. end
  257.  
  258. --*****************************************************************
  259.  
  260. function plus10K(var)
  261.     button.flash("plusTenK")
  262.     var=var+10000
  263. end
  264.  
  265. --*****************************************************************
  266.  
  267. function minus1(var)
  268.     button.flash("minusOne")
  269.     var=var-1
  270. end
  271.  
  272. --*****************************************************************
  273.  
  274. function minus10(var)
  275.     button.flash("minusTen")
  276.     var=var-10
  277. end
  278.  
  279. --*****************************************************************
  280.  
  281. function minus100(var)
  282.     button.flash("minus100")
  283.     var=var-100
  284. end
  285.  
  286. --*****************************************************************
  287.  
  288. function minus1K(var)
  289.     button.flash("minus1K")
  290.     var=var-1000
  291. end
  292.  
  293. --*****************************************************************
  294.  
  295. function minus10K(var)
  296.     button.flash("minus10K")
  297.     var=var-10000
  298. end
  299.  
  300. --*****************************************************************
  301. function shutdown()
  302.     progActive = not progActive
  303.     button.setButton("Power", progActive)
  304.     if(progActive) then
  305.         print("ON")
  306.     else
  307.         print("OFF")
  308.     end
  309. end
  310.  
  311. --*****************************************************************
  312.  
  313. function up()
  314.     print("Up")
  315.     button.flash("Up")
  316. end
  317.  
  318. --*****************************************************************
  319.  
  320. function down()
  321.     print("Down")
  322.     button.flash("Down")
  323. end
  324.  
  325. --*****************************************************************
  326.  
  327. function addItem()
  328.     print("Adding")
  329.     button.flash("Add")
  330. end
  331.  
  332.  
  333. --*****************************************************************
  334.  
  335. function main()
  336.     local event, side, xCoord, yCoord
  337.     timerCode = os.startTimer(1)
  338.     repeat
  339.         event, side, xCoord, yCoord = os.pullEvent()
  340.     until event ~= "timer" or timerCode == side
  341.     if (event == "timer") then
  342.         if(param == "") then
  343.             (functionName)()
  344.         else
  345.             (functionName)(param)
  346.         end
  347.  
  348.         if (progActive == true) then
  349.             --code
  350.             if (getCrafting() == false) then
  351.                 makeCrafts()
  352.             else
  353.                 print("Crafting CPUs are making something")
  354.             end
  355.         else
  356.             print("Inactive")
  357.         end
  358.     elseif (event == "monitor_touch") then
  359.         print(event)
  360.         button.checkxy(xCoord, yCoord)
  361.     end
  362. end
  363.  
  364. --*****************************************************************
  365.  
  366. mainScreen()
  367. while true do main() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement