Advertisement
HPWebcamAble

[CC][2.0]Advanced Redstone Control

Aug 5th, 2014
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.42 KB | None | 0 0
  1. --[[
  2. Coded by HPWebcamAble
  3. http://pastebin.com/u/HPWebcamAble
  4.  
  5. WARNING! USE THE INSTALLER ON MY PASTEBIN, DO NOT DOWNLOAD THIS ALONE! IT WONT WORK BY ITSELF.
  6.  
  7. Installer:
  8. In a CC Computer (Advanced only!):
  9. pastebin get 5Tfpzcbz installer
  10.  
  11. From pastebin site:
  12. http://pastebin.com/5Tfpzcbz
  13.  
  14. Description:
  15. This program allows for (slightly) custom buttons that control redstone using bundled cable (or a similar item)
  16. When it first starts, it will ask you to add buttons
  17. To use it, run the installer - That will tell you what to do
  18.  
  19.  
  20. Q:What is this?
  21. A:See description
  22.  
  23. Q:Im confused
  24. A:I don't have a video yet, so you'll have to figure it out :P
  25.  
  26. Q:Auto-Update?
  27. A:Not yet, and not planned
  28.  
  29. Q:Where do I report bugs?
  30. A:I don't have a place for this program. If you find any then you can message me on
  31.   youtube, but I most likely won't do anything because version 3.0 is comming soon.
  32.  
  33.  
  34.  
  35. VERSIONS:
  36.  
  37. NOTE:I am still adding to the program! This is just to get out the changes that work so far.
  38.  
  39. |2.0| <-- This program
  40. Date: 8/5/2014
  41.   Changes:
  42.   -Redesigned code
  43.     *Program now uses an installer
  44.     *Creates a config file for the buttons
  45.     *Buttons are a bit easier to set up
  46.      -To redo, just delete the config file in the ARC directory
  47.   Bugs:
  48.   -None so far
  49.  
  50.  
  51.  
  52. |1.0 (Release)|
  53. Date: Unknown
  54.   Bugs:
  55.   -Not super easy to set up
  56.     *Does work though
  57.   Installation:
  58.   -This program
  59.   -My button API
  60. ]]
  61.  
  62. --Variables--
  63. local version = "2.0"
  64. local outputNames = {}
  65. local currentOut = 0
  66. local monSide
  67. local cableSide = "right"
  68. local buttons = {}
  69. local tLines = {}
  70. cReference = {
  71.   ["colors.white"] = 1,
  72.   ["colors.orange"] = 2,
  73.   ["colors.magenta"] = 4,
  74.   ["colors.lightBlue"] = 8,
  75.   ["colors.yellow"] = 16,
  76.   ["colors.lime"] = 32,
  77.   ["colors.pink"] = 64,
  78.   ["colors.gray"] = 128,
  79.   ["colors.lightGray"] = 256,
  80.   ["colors.cyan"] = 512,
  81.   ["colors.purple"] = 1024,
  82.   ["colors.blue"] = 2048,
  83.   ["colors.brown"] = 4096,
  84.   ["colors.green"] = 8192,
  85.   ["colors.red"] = 16384,
  86.   ["colors.black"] = 32768
  87. }
  88.  
  89. --Functions--
  90. function clear() shell.run("clear") end
  91. function cp(...) term.setCursorPos(...) end
  92. function tc(...) term.setTextColor(...) end
  93. function bc(...) term.setBackgroundColor(...) end
  94. function w(...) term.write(...) end
  95.  
  96. function printC(text,y)
  97.   if not y then
  98.     error("printC:No Y value specified")
  99.   end
  100.   cp(math.ceil(w/2-#tostring(text)/2),y)
  101.   w(text)
  102. end
  103.  
  104. function check(color)
  105.   for a,b in pairs(cReference) do
  106.     if a == color then
  107.       return true
  108.     end
  109.   end
  110.   return false
  111. end
  112.  
  113. function setup()
  114.   clear()
  115.   print("Welcome to RedstoneControl Setup")
  116.   print("How many buttons? (1-5)")
  117.   while true do
  118.     event,p1 = os.pullEvent("char")
  119.     if tonumber(p1) > 0 and tonumber(p1) < 6 then
  120.       number = p1
  121.       break
  122.     end
  123.   end
  124.    f = fs.open("/ARC/config","w")
  125.   for i = 1, number do
  126.     print("Enter the name of button "..i)
  127.     print("Works best when less than 8 characters")
  128.     w("> ")
  129.     repeat
  130.       input1 = read()
  131.     until input1 ~= nil
  132.     while true do
  133.       print("Enter color for button "..input1.." to activate")
  134.       print("Format: 'colors.blue'")
  135.       w("> ")
  136.       repeat
  137.         input2 = read()
  138.       until input2 ~= nil
  139.       if check(input2) then
  140.         break
  141.       else
  142.         clear()
  143.         tc(colors.red)
  144.         print("Not a color")
  145.         tc(colors.white)
  146.       end
  147.     end
  148.     f.write(input1..[[=]]..input2.."\n")
  149.     print("Added "..input1.." to config file")
  150.   end
  151.   f.close()
  152.   print("Config saved")
  153. end
  154.  
  155. function main() --Having this as a function allows error catching
  156.   clear()
  157.   print("Loading config...")
  158.   buttons = {}
  159.   f = fs.open("/ARC/config","r")
  160.   local l = 0
  161.   repeat
  162.     l = l+1
  163.     tLines[l] = f.readLine()
  164.   until tLines[l] == nil
  165.   f.close()
  166.   local tempvar
  167.   for i = 1, #tLines do
  168.     tempvar = string.find(tLines[i],"=")
  169.     if not tempvar then
  170.       print("ERROR ERROR ERROR")
  171.       print(tLines[i].." doesn't have an = sign!")
  172.       sleep(1)
  173.       print("One sec...")
  174.       print("Config must be redone")
  175.       shell.run("delete RedstoneControl/config")
  176.       sleep(1)
  177.       shell.run(shell.getRunningProgram())
  178.       error("Program Self-Terminated")
  179.     end
  180.     bName = string.sub(tLines[i],1,tempvar-1)
  181.     bColor = string.sub(tLines[i],tempvar+1)
  182.     buttons[i] = {}
  183.     buttons[i]["name"] = bName
  184.     buttons[i]["color"] = cReference[bColor]
  185.     buttons[i]["state"] = false
  186.     print("Added button "..bName.." that activates color "..bColor)
  187.   end
  188.   print("Searching for monitors...")
  189.   local mSides = {}
  190.   for a,b in pairs(rs.getSides()) do
  191.     if peripheral.getType(b) == "monitor" then
  192.       print("Found monitor on "..b.." side")
  193.       mSides[#mSides+1] = b
  194.     end
  195.   end
  196.   if #mSides == 0 then
  197.     error("No monitors. Try adding one.")
  198.   elseif #mSides > 1 then
  199.     print("More than 1 monitor detected...")
  200.     print("Located on the following sides:")
  201.     for a,b in pairs(mSides) do
  202.       print(b)
  203.     end
  204.     error("Remove the extras...")
  205.   else
  206.     m = peripheral.wrap(mSides[1])
  207.     m.setBackgroundColor(colors.black)
  208.     m.clear()
  209.   end
  210.   print("Starting program...")
  211.   print("Running. Use ctrl + t to stop")
  212.   print("Log:")
  213.   while true do --Main loop
  214.     rs.setBundledOutput(cableSide,currentOut)
  215.     for i = 1, #buttons do
  216.       if buttons[i]["state"] then m.setBackgroundColor(colors.lime) else m.setBackgroundColor(colors.red) end
  217.       m.setCursorPos(1,i)
  218.       m.write(string.rep(" ",7))
  219.       m.setCursorPos(1,i)
  220.       m.write(buttons[i]["name"])
  221.     end
  222.     print("Waiting for event...")
  223.     event,p1,p2,p3,p4 = os.pullEvent()
  224.     w("Event:") print(event)
  225.     if event == "monitor_touch" then
  226.       term.write("Location:") print(p1)
  227.       term.write("X:") term.write(p2) term.write(" Y:") print(p3)
  228.       if p3 <= #buttons then
  229.         for i = 1, #buttons do
  230.           if i == p3 then
  231.             buttons[i]["state"] = not buttons[i]["state"]
  232.             if buttons[i]["state"] then
  233.               print(currentOut)
  234.               print(buttons[i]["color"])
  235.               currentOut = currentOut + buttons[i]["color"]
  236.             else
  237.               currentOut =  currentOut - buttons[i]["color"]
  238.             end
  239.           end
  240.         end
  241.       else
  242.         print("Nope")
  243.       end
  244.     end
  245.     print(" ")
  246.   end
  247. end
  248.  
  249. --Program--
  250. clear()
  251. print("Redstone Control "..version)
  252. print("Initialize...")
  253. print("Loading Config...")
  254. if not fs.exists("/ARC/config") then
  255.   tc(colors.red)
  256.   print("Missing file: config")
  257.   tc(colors.white)
  258.   print("Fixing...")
  259.   f = fs.open("/ARC/config","w")
  260.   f.close()
  261. end
  262. f = fs.open("/ARC/config","r")
  263. local firstLine = f.readLine()
  264. if firstLine == nil then
  265.   f.close()
  266.   tc(colors.red)
  267.   print("Config file blank. Running setup...")
  268.   tc(colors.white)
  269.   sleep(2)
  270.   setup()
  271. else
  272.   f.close()
  273. end
  274.  
  275. --Capture any errors
  276. state,err = pcall(function() main() end)
  277.  
  278. if err then
  279.   if string.find("Terminated",err) then
  280.     print("User terminated the program")
  281.   elseif string.find("Program Self-Terminated",err) then
  282.     print("Program Self-Terminated")
  283.   else
  284.     tc(colors.red)
  285.     print("CRITICAL ERROR:")
  286.     print(err)
  287.     tc(colors.white)
  288.   end
  289. end
  290.  
  291. --Cleanup
  292. f.close()
  293. if m then
  294.   m.setBackgroundColor(colors.black)
  295.   m.clear()
  296.   m.setCursorPos(1,1)
  297.   m.write("Program")
  298.   m.setCursorPos(1,2)
  299.   m.write("termina-")
  300.   m.setCursorPos(1,3)
  301.   m.write("ted")
  302. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement