Advertisement
HPWebcamAble

[CC][3.1] Advanced Redstone Control

Aug 15th, 2014
937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.38 KB | None | 0 0
  1. --[[
  2. Coded by HPWebcamAble
  3. http://pastebin.com/u/HPWebcamAble
  4.  
  5. WARNING! USE THE INSTALLER, DO NOT DOWNLOAD THIS ALONE! IT WONT WORK BY ITSELF.
  6.  
  7. **********
  8. Installer
  9. **********
  10. In a CC Computer (Advanced only!):
  11. pastebin get pVyr7LjW installer
  12.  
  13. From pastebin site:
  14. http://pastebin.com/pVyr7LjW
  15.  
  16. **********
  17. Description
  18. **********
  19. This program allows for (slightly) custom buttons that control redstone using bundled cable (or a similar item)
  20. When it first starts, it will allow you to configure everything.
  21. If you are not familiar with bundled cable, look it up.
  22.  
  23. **********
  24. FAQ
  25. **********
  26. Q:What is this?
  27. A:See description
  28.  
  29. Q:Im confused
  30. A:http://youtu.be/o_mzKx-7dS0
  31.  
  32. Q:How do I make buttons?
  33. A:Use the config editor program
  34.  
  35. Q:Auto-Update?
  36. A:Not yet, and not planned
  37.  
  38. Q:Where do I report bugs?
  39. A:On it's Youtube video
  40.   http://youtu.be/o_mzKx-7dS0
  41.  
  42. **********
  43. Program Version History
  44. **********
  45. |3.1| <-- This program
  46. Date: 9/12/2014
  47.   Changes:
  48.   -Support for ConfigEditor 1.1
  49.   Bugs:
  50.   -Debug is always shown, even when disabled
  51.   -log sometimes goes off screen (in multiple ways)
  52.   None of these break anything, it still works fine :)
  53.  
  54. |3.0|
  55. Date:8/15/2014
  56.   Changes:
  57.   -Added a config editor gui
  58.   -Smarter monitor support
  59.   Bugs:
  60.   -Debug always shows even when disabled
  61.   -log has problem displaying
  62.     *Program still works tho
  63.  
  64. |2.0|
  65. Date: 8/5/2014
  66.   Changes:
  67.   -Redesigned code
  68.     *Program now uses an installer
  69.     *Creates a config file for the buttons
  70.     *Buttons are a bit easier to set up
  71.      -To redo, just delete the config file in the ARC directory
  72.   Bugs:
  73.   -None so far
  74.  
  75. |1.0 (Release)|
  76. Date: Unknown
  77.   Bugs:
  78.   -Not super easy to set up
  79.     *Does work though
  80.   Installation:
  81.   -This program
  82.   -My button API
  83. ]]
  84.  
  85. --Variables--
  86. local version = "3.1"
  87. local outputNames = {}
  88. local currentOut = 0
  89. local cableSide
  90. local buttons = {}
  91. local tLines = {}
  92. local doDebug = false
  93. w,h = term.getSize()
  94. local tLog = {}
  95. local logSev = {
  96.   [1] = {
  97.     "Info",
  98.     "Warning",
  99.     "Severe",
  100.     "Debug",
  101.     "End"
  102.   },
  103.   [2] = {
  104.     colors.white,
  105.     colors.orange,
  106.     colors.red,
  107.     colors.magenta,
  108.     colors.red
  109.   }
  110. }
  111.  
  112. --Functions--
  113. function clear() shell.run("clear") end
  114. function tc(...) term.setTextColor(...) end
  115. function bc(...) term.setBackgroundColor(...) end
  116. function tw(...) term.write(...) end
  117. function cp(...) term.setCursorPos(...) end
  118.  
  119. function printC(text,y)
  120.   if not y then
  121.     error("printC:No Y value specified")
  122.   end
  123.   cp(math.ceil(w/2-#tostring(text)/2),y)
  124.   tw(text)
  125. end
  126.  
  127. function log(text,sev)
  128.   numLog = #tLog+1
  129.   tLog[numLog] = {}
  130.   if sev then
  131.     if type(sev) ~= "number" then
  132.       error("expected string,number of string,nil")
  133.     end
  134.     tLog[numLog]["text"] = text
  135.     tLog[numLog]["sev"] = sev
  136.   else
  137.     tLog[numLog]["text"] = text
  138.     tLog[numLog]["sev"] = 1
  139.   end
  140.   clear()
  141.   tc(colors.white)
  142.   printC("Advanced Redstone Control",1)
  143.   printC("v"..version,2)
  144.   term.setCursorPos(1,3)
  145.   tw(string.rep("-",w))
  146.   local hud = 3
  147.   local space = h-hud
  148.   if numLog <= space then
  149.     for i = 1, numLog do
  150.       cp(1,i+hud)
  151.       tc(logSev[2][tLog[i]["sev"]])
  152.       tw("["..logSev[1][tLog[i]["sev"]].."] "..tLog[i]["text"])
  153.       print(" ")
  154.       if tLog[i]["sev"] == 3 then
  155.         error("end")
  156.       end
  157.     end
  158.   else
  159.     for i = 1, space do
  160.       cp(1,i+hud)
  161.       tc(logSev[2][tLog[i+numLog-space]["sev"]])
  162.       tw("["..logSev[1][tLog[i+numLog-space]["sev"]].."] "..tLog[i+numLog-space]["text"])
  163.       print(" ")
  164.       if tLog[i+numLog-space]["sev"] == 3 then
  165.         error("end")
  166.       elseif tLog[i+numLog-space]["sev"] == 5 then
  167.         error()
  168.       end
  169.     end
  170.   end  
  171. end
  172.  
  173. function isColor(color)
  174.   for a,b in pairs(colors) do
  175.     if b == color then
  176.       return true
  177.     end
  178.   end
  179.   return false
  180. end
  181.  
  182. function isSide(side)
  183.   for a,b in pairs(rs.getSides()) do
  184.     if b == side then
  185.       return true
  186.     end
  187.   end
  188.   return false
  189. end
  190.  
  191. function main() --Having this as a function allows error catching
  192.   if doDebug then
  193.     log("Debugging enabled",2)
  194.   end
  195.   log("Loading Config...")
  196.   --Read the config
  197.   if not fs.exists("/ARC/config") then
  198.     log("Config file missing, run config creator",3)
  199.   end
  200.   f = fs.open("/ARC/config","r")
  201.   local l = 0
  202.   repeat
  203.     l = l+1
  204.     tLines[l] = f.readLine()
  205.   until tLines[l] == nil
  206.   f.close()
  207.   if tLines[1] ~= "This is the Advanced Redstone Control config file" or tLines[2] ~= "Do not change anything in it!" or tLines[3] ~= "Use the provided config editor" then
  208.     --Quite an if statement right?
  209.     log("Config is blank or incorrectly formated, run config creator",3)
  210.   end
  211.   --Wrap the monitor
  212.   if peripheral.getType(tLines[4]) ~= "monitor" then
  213.     log("Could not find monitor '"..tLines[1].."'",3)
  214.   end
  215.   m = peripheral.wrap(tLines[4])
  216.   log("Monitor set to '"..tLines[4].."'")
  217.   --Set the side of the bundled cable (or its equivalent)
  218.   if not isSide(tLines[5]) then
  219.     log("'"..tLines[5].."' is not a side, run config creator",3)
  220.   end
  221.   log("Bundled cable set to side "..tLines[5])
  222.   cableSide = tLines[5]
  223.   --Load the buttons
  224.   local equalPos
  225.   for i = 1, tLines[6] do
  226.     c = i+6
  227.     equalPos = string.find(tLines[c],"=")
  228.     if not equalPos then
  229.       log("Something is wrong with line "..c.." in the config file, run config creator",3)
  230.     end
  231.     bName = string.sub(tLines[c],1,equalPos-1)
  232.     bColor = string.sub(tLines[c],equalPos+1)
  233.     buttons[i] = {}
  234.     buttons[i]["name"] = bName
  235.     buttons[i]["color"] = bColor
  236.     buttons[i]["state"] = false
  237.     log("Added button "..bName.." that activates color "..bColor,4)
  238.   end
  239.   log("Buttons added")
  240.   log("Program running. Use Ctrl+t to stop")
  241.   --Main loop
  242.   while true do
  243.     mw,mh = m.getSize()
  244.     m.setBackgroundColor(colors.black)
  245.     m.clear()
  246.     rs.setBundledOutput(cableSide,currentOut)
  247.     for i = 1, #buttons do
  248.       if buttons[i]["state"] then m.setBackgroundColor(colors.lime) else m.setBackgroundColor(colors.red) end
  249.       m.setCursorPos(1,i)
  250.       m.write(string.rep(" ",mw))
  251.       m.setCursorPos(1,i)
  252.       m.write(buttons[i]["name"])
  253.     end
  254.     log("Waiting for event...",4)
  255.     event,p1,p2,p3,p4 = os.pullEvent()
  256.     log("Event:"..event,4)
  257.     if event == "monitor_touch" then
  258.       log("Monitor:"..p1,4)
  259.       log("X:"..p2.." Y:"..p3,4)
  260.       if p3 <= #buttons then
  261.         for i = 1, #buttons do
  262.           if i == p3 then
  263.             buttons[i]["state"] = not buttons[i]["state"]
  264.             log("Button '"..buttons[i]["name"].."' toggled",4)
  265.             if buttons[i]["state"] then
  266.               currentOut = currentOut + buttons[i]["color"]
  267.             else
  268.               currentOut =  currentOut - buttons[i]["color"]
  269.             end
  270.             log("Bundled output set to "..currentOut,4)
  271.           end
  272.         end
  273.       end
  274.     end
  275.   end
  276. end
  277.  
  278. --Program--
  279. --Capture any errors
  280. state,err = pcall(function() main() end)
  281.  
  282. if err then
  283.   if string.find(err,"Terminated") then
  284.     log("User terminated the program",5)
  285.   elseif string.find(err,"end") then
  286.     log("Program stopped",5)
  287.   else
  288.     tc(colors.blue)
  289.     print("CRITICAL ERROR:")
  290.     print(err)
  291.     tc(colors.white)
  292.   end
  293. end
  294.  
  295. --Cleanup
  296. if f then
  297.   f.close()
  298. end
  299.  
  300. if m then
  301.   m.setBackgroundColor(colors.black)
  302.   m.clear()
  303.   m.setCursorPos(1,1)
  304.   m.write("Program")
  305.   m.setCursorPos(1,2)
  306.   m.write("Stopped")
  307. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement