Advertisement
HPWebcamAble

[CC][1.1] Redstone Engine

Sep 29th, 2014
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.80 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 eMCqJEBs installer
  12.  
  13. From pastebin site:
  14. http://pastebin.com/eMCqJEBs
  15.  
  16.  
  17. **********
  18. Description
  19. **********
  20. This program allows the user to turn bundled cable on and off in a set order.
  21. 'Configs' are created with the 'Config Editor'. They are run with this program (Redstone Engine)
  22. 'Configs' contain 'Events'.
  23. Possible events:
  24. Sleep (In config: s<time>) - Makes the computer pause for the specified time
  25. Set (In config: =<color>) - Sets the output to the color
  26. Add (In config: +<color>) - Adds a color to the current output
  27. Remove (In config: -<color>) - Removes a color from the current output
  28.  
  29. Type 're' to edit or run configs
  30.  
  31. To run configs:
  32.   (In console)
  33.   re run <config name>
  34.  
  35.   (In a program)
  36.   shell.run("re run <config name>")
  37.  
  38. Cable may be attached to any side of the computer
  39.  
  40.  
  41. **********
  42. FAQ
  43. **********
  44. Q:What is this?
  45. A:See description
  46.  
  47. Q:How does bundled/rednet cable work?
  48. A:Look it up. Probably best on youtube.
  49.  
  50. Q:The bundled cable isn't attaching to the computer correctly/it doesnt work
  51. A:At this moment, you need to use ComputerCraft 1.5, not 1.6
  52.   See http://pastebin.com/1XY12jQZ for more info
  53.  
  54. Q:Im confused
  55. A:Here's a video! (Leave a comment with questions):
  56.   http://youtu.be/zPvUVptSnro
  57.  
  58. Q:Where do I report bugs?
  59. A:On its Youtube Video (Leave it as a comment):
  60.   http://youtu.be/zPvUVptSnro
  61.  
  62.  
  63. **********
  64. Version
  65. **********
  66. |1.1| <--This program
  67. Date:10/4/14
  68.   -Bug fixes
  69.     *Output could be negative easily
  70.   -Tweaked what it says when cable is still on after program finishes
  71.   -Cleaned up code a bit
  72.  
  73. |1.0 (Release)|
  74. Date: 9/28/14
  75. Date Format: MM/DD/YY
  76.  
  77.  
  78. **********
  79. Bugs
  80. **********
  81. None! (Yet)
  82. ]]
  83.  
  84. --Variables--
  85. local actions = {}
  86. w,h = term.getSize()
  87. args = {...}
  88.  
  89. if not output then output = 0 end
  90.  
  91. --Functions--
  92. function color(txt,bck)
  93.   if txt then
  94.     term.setTextColor(txt)
  95.   end
  96.   if bck then
  97.     term.setBackgroundColor(bck)
  98.   end
  99. end
  100. function clear() shell.run("clear") end
  101. function tw(...) term.write(...) end
  102. function cp(...) term.setCursorPos(...) end
  103.  
  104.  
  105. function printC(text,y)
  106.   if not y then
  107.     error("printC:No Y value specified")
  108.   end
  109.   tLenght = #tostring(text)
  110.   local sStart = math.ceil(w/2-tLenght/2)
  111.   local sEnd = sStart + tLenght
  112.   cp(sStart,y)
  113.   tw(text)
  114.   return sStart,sEnd
  115. end
  116.  
  117. function bar(backColor,height,text,textColor)
  118.   cp(1,height)
  119.   color(textColor,backColor)
  120.   tw(string.rep(" ",w))
  121.   printC(text,height)
  122. end
  123.  
  124. function drawScreen()
  125.   clear()
  126.   bar(colors.white,1,"Redstone Engine",colors.black)
  127.   color(colors.white,colors.black)
  128.   printC("Current Output:"..output,7)
  129.   printC("Current Action:"..action,9)
  130.   printC("Events completed:"..completeEvents,11)
  131.   printC("Events left:"..totalEvents-completeEvents,13)
  132.   color(colors.red)
  133.   printC("Errors:"..#errors,h)
  134. end
  135.  
  136. function update(cOutput,cAction)
  137.   if cOutput then
  138.     output = cOutput
  139.     sides = rs.getSides()
  140.     for i = 1, #sides do
  141.       rs.setBundledOutput(sides[i],cOutput)
  142.     end
  143.   end
  144.   action = cAction
  145.   if action ~= "Starting..." then completeEvents = completeEvents+1 end
  146.   drawScreen()
  147. end
  148.  
  149. --Program--
  150. clear()
  151. color(colors.red,colors.black)
  152. if #args ~= 1 then
  153.   print("No config specified")
  154.   return
  155. elseif not fs.isDir("/RED/configs") then
  156.   print("configs directory is missing. Run installer again")
  157.   return
  158. elseif not fs.exists("/RED/configs/"..args[1]) then
  159.   print("Could not find config '"..args[1].."'")
  160.   return
  161. end
  162.  
  163. --Load config
  164. f = fs.open("/RED/configs/"..args[1],"r")
  165. if f then
  166.   local l = 0
  167.   repeat
  168.     l = l+1
  169.     actions[l] = f.readLine()
  170.   until actions[l] == nil
  171.   f.close()
  172. else
  173.   color(colors.red,colors.black)
  174.   print("Unable to open config '"..args[1].."'")
  175.   return
  176. end
  177.  
  178. totalEvents = #actions
  179. completeEvents = 0
  180. errors = {}
  181. update(nil,"Starting...")
  182.  
  183. --Main loop (As a function for error catching)
  184. function main()
  185.   for i = 1, #actions do
  186.     drawScreen()
  187.     firstChar = string.sub(actions[i],1,1)
  188.     if  firstChar == "s" then --Sleep
  189.       sTime = tonumber(string.sub(actions[i],2))
  190.       if not sTime then
  191.         table.insert(errors,i)
  192.       else
  193.         update(nil,"Sleeping for "..sTime.." seconds...")
  194.         sleep(sTime)
  195.       end
  196.     elseif firstChar == "+" then --Add
  197.       addColor = tonumber(string.sub(actions[i],2))
  198.       if not addColor then
  199.         table.insert(errors,i)
  200.       else
  201.         update(output+addColor,"Color "..addColor.." added to output")
  202.       end
  203.     elseif firstChar == "-" then --Subtract
  204.       subColor = tonumber(string.sub(actions[i],2))
  205.       if not subColor then
  206.         table.insert(errors,i)
  207.       else
  208.         update(output-subColor,"Color "..subColor.." removed from output")
  209.       end
  210.     elseif firstChar == "=" then --Set
  211.       setColor = tonumber(string.sub(actions[i],2))
  212.       if not setColor then
  213.         table.insert(errors,i)
  214.       else
  215.         update(setColor,"Output set to "..setColor)
  216.       end
  217.     else
  218.       table.insert(errors,i)
  219.     end
  220.   end
  221. end
  222.  
  223. state,err = pcall(function() main() end)
  224.  
  225. if err then
  226.   if string.find(err,"Terminated") then
  227.     bar(colors.white,1,"Terminated - Click to clear screen",colors.red)
  228.     os.pullEvent("mouse_click")
  229.     clear()
  230.   else
  231.     clear()
  232.     printC("The program encountered a critical error:",1)
  233.     cp(1,2)
  234.     print(err)
  235.   end
  236. else
  237.   clear()
  238.   color(colors.white)
  239.   print("Finished config '"..args[1].."'")
  240.   if #errors > 0 then
  241.     print("Errors in config:"..#errors)
  242.   end
  243.   if output > 0 then
  244.     print("Output is still on (color:"..output..")")
  245.   end
  246. end
  247.  
  248. --Cleanup
  249. if f then f.close() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement