Guest User

comCenter

a guest
Feb 26th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.29 KB | None | 0 0
  1. -------------Initialization--------
  2. local digTime = 9 --seconds
  3. local iterations = 0
  4. local cell = peripheral.wrap("back")
  5. local glass = peripheral.wrap("right")
  6. local numCapacitors = 197
  7.  
  8. local glassTextColor = 0x000000
  9. local oldPowerNum = cell.getEnergyStored() * numCapacitors
  10. local powerNum = cell.getEnergyStored() * numCapacitors
  11. local power = ""  --this is a string
  12. local in_outNum = (powerNum - oldPowerNum) / 2
  13. local in_out = "" --this is a string
  14. local isNegative = " " -- is ' ' if in-out is negative and "+" if positive
  15. local maxPowerNum = cell.getMaxEnergyStored() * numCapacitors
  16. local maxPower = "" --this is a string
  17. local percentage = (powerNum / maxPowerNum) * 100
  18. local in_outPercentage = (in_outNum / maxPowerNum) * 100
  19.  
  20. local aeCutOff = colors.test (redstone.getBundledInput("top"), colors.white)
  21. local isIn_OutPercentage = false --whether to display percent gain/loss or RF gain/loss
  22.  
  23. local commandString = "null"
  24. local commandNum = 0
  25. local digProgress = 0
  26. local commaResult = "" --output of addComma() function
  27.  
  28. local dontPrintData = false
  29. local isError = false
  30. local error = " "
  31. local errorTime = 5 --# of seconds error will be shown on glass
  32. local tempErrorTime = errorTime
  33. local isChatCommand = false
  34.  
  35. local blueInput = colors.test(rs.getBundledInput("top"),colors.blue)
  36.  
  37. -------------Functions-------------
  38. local function addComma(num)
  39.   local numA = 0
  40.   local numB = 0
  41.   local numC = 0
  42.   local numD = 0
  43.   local divA = 1
  44.   local divB = 1
  45.   local divC = 1
  46.   local divD = 1
  47.   local stringB = ""
  48.   local stringC = ""
  49.   local stringD = ""
  50.   if num >= 1000000000 then
  51.     divA = 1000000000
  52.     divB = 1000000
  53.     divC = 1000
  54.     divD = 1
  55.   elseif num >= 1000000 then
  56.     divA = 1000000
  57.     divB = 1000
  58.     divC = 1
  59.   elseif num >= 1000 then
  60.     divA = 1000
  61.     divB = 1
  62.   else
  63.     divA = 1
  64.   end
  65.   numA = num / divA
  66.   numA = math.floor(numA)
  67.   if num >= 1000 then
  68.     numB = (num - numA * divA) / divB
  69.     numB = math.floor(numB)
  70.     if numB < 1 then
  71.       stringB = "000"
  72.     elseif numB < 10 then
  73.       stringB = "00"..numB
  74.     elseif numB < 100 then
  75.       stringB = "0"..numB
  76.     elseif numB < 1000 then
  77.       stringB = numB
  78.     end
  79.   else
  80.     commaResult = stringA
  81.   end
  82.   if num >= 1000000 then
  83.     numC = (num - numA * divA - numB * divB) / divC
  84.     numC = math.floor(numC)
  85.     if numC < 1 then
  86.       stringC = "000"
  87.     elseif numC < 10 then
  88.       stringC = "00"..numC
  89.     elseif numC < 100 then
  90.       stringC = "0"..numC
  91.     elseif numC < 1000 then
  92.       stringC = numC
  93.     end
  94.   else
  95.     commaResult = stringA..","..stringB
  96.   end
  97.   if num >= 1000000000 then
  98.     numD = (num - numA * divA - numB * divB - numC * divC) / divD
  99.     math.floor(numD)
  100.     if numD < 1 then
  101.       stringD = "000"
  102.     elseif numD < 10 then
  103.       stringD = "00"..numD
  104.     elseif numD < 100 then
  105.       stringD = "0"..numD
  106.     elseif numD < 1000 then
  107.       stringD = numD
  108.     end
  109.     commaResult = stringA..","..stringB..","..stringC..","..stringD
  110.   else
  111.     commaResult = stringA..","..stringB..","..stringC
  112.   end
  113. end
  114.  
  115. local function listenForCommand()
  116.   while true do
  117.     event, command = os.pullEvent("chat_command")
  118.     print(event.." "..command)
  119.     isChatCommand = true
  120.   end
  121. end
  122.  
  123. local function checkCommand()
  124.   local tempCount = 0
  125.   for i in string.gmatch(command, "%S+") do
  126.     tempCount = tempCount + 1
  127.     print(i)
  128.     if tempCount == 1 then
  129.       commandString = i
  130.     elseif tempCount == 2 then
  131.       commandNum = i
  132.     end
  133.     if commandString == "mine" then
  134.       commandNum = tonumber(commandNum)
  135.     end
  136.   end
  137.   print("commandNum:"..commandNum)
  138. end
  139.  
  140. local function getData()
  141.   term.setTextColor(colors.white)
  142.  
  143.   powerNum = cell.getEnergyStored() * numCapacitors
  144.   addComma(powerNum)
  145.   power = commaResult
  146.   in_outNum = (powerNum - oldPowerNum) / 2
  147.   addComma(in_outNum)
  148.   in_out = commaResult
  149.   oldPowerNum = powerNum
  150.   maxPowerNum = cell.getMaxEnergyStored() * numCapacitors
  151.   addComma(maxPowerNum)
  152.   maxPower = commaResult
  153.   if dontPrintData == false then
  154.     print(power .. "RF out of " .. maxPower .. "RF")
  155.   end
  156.  
  157.   in_outPercentage = (in_outNum / maxPowerNum) * 100
  158.  
  159.   aeCutOff = (colors.test (redstone.getBundledInput("top"), colors.white))
  160.    
  161.   percentage = (powerNum / maxPowerNum) * 100
  162.   if dontPrintData == false then
  163.     print("aka " .. percentage .. "%")
  164.     if aeCutOff == true then
  165.       print("AE Cut Off is true")
  166.     else
  167.       print("AE Cut Off is false")
  168.     end
  169.   end
  170. end
  171.  
  172. local function updateGlass()
  173.   glass.clear()
  174.   glass.addBox(1,1,167,75,0x000000,0.2)
  175.   glass.addText(6,6,"Current Power",0xCC4C4C)
  176.   glass.addText(6,20,power .. "RF / " .. maxPower .. "RF")
  177.   glass.addText(17,34,percentage .. " %",0xF2B233)
  178.  
  179.   if isError == true and tempErrorTime > 0 then
  180.     glass.addBox(162,1,221,20,0xEDEDED,0.9)
  181.     errorText = glass.addText(167,7,error,0xCC4C4C)
  182.     tempErrorTime = tempErrorTime - 1
  183.   else
  184.     isError = false
  185.     tempErrorTime = errorTime
  186.     error = " "
  187.   end
  188.  
  189.   if colors.test(rs.getBundledInput("top"),colors.green) == true then
  190.     glass.addText(73,46,"GENERATORS ON",0xCC4444)
  191.   end
  192.  
  193.   if in_outNum >= 0 then
  194.     glassTextColor = 0x00FF00
  195.     isNegative = "+"
  196.   else
  197.     glassTextColor = 0xCC4C4C
  198.     isNegative = ""
  199.   end
  200.   if isIn_OutPercentage == true then
  201.     glass.addText(90,34,isNegative..in_outPercentage.."%",glassTextColor)
  202.   else
  203.     glass.addText(90,34,isNegative..in_out.."RF",glassTextColor)
  204.   end
  205.  
  206.   if aeCutOff == true then
  207.     glass.addText(6,58,"Emergency AE Cut Off is Active",0xCC0000)
  208.   end
  209.  
  210.  
  211.   if iterations > 0 or iterations == -1 then
  212.     if digProgress == 0 then
  213.       if colors.test(rs.getBundledInput("top"),colors.blue) == false then
  214.         miningTextColor = 0x00FF00
  215.       else
  216.         miningTextColor = 0xCC4444
  217.       end
  218.     end
  219.     if digProgress == 0 then
  220.       glass.addText(4,46,"Mining "..iterations,miningTextColor)
  221.     elseif digProgress == 1 then
  222.       glass.addText(4,46,"Mining "..iterations..".",miningTextColor)
  223.     elseif digProgress == 2 then
  224.       glass.addText(4,46,"Mining "..iterations.."..",miningTextColor)
  225.     elseif digProgress == 3 then
  226.       glass.addText(4,46,"Mining "..iterations.."...",miningTextColor)
  227.     end
  228.     if colors.test(rs.getBundledInput("top"),colors.blue) == true then
  229.      
  230.     end
  231.   end
  232. end
  233.  
  234. local function moveFrame()
  235.   local blueInput = colors.test(rs.getBundledInput("top"),colors.blue)
  236.   if blueInput == false then
  237.     rs.setBundledOutput("top",colors.red) --> wireless transmitter
  238.    
  239.     print("set red")
  240.    
  241.     sleep(1)
  242.     rs.setBundledOutput("top",0)
  243.    
  244.     print("set all off moveFrame")
  245.    
  246.   end
  247. end
  248.  
  249. local function dig()
  250.   local blueInput = colors.test(rs.getBundledInput("top"),colors.blue)
  251.   local splitDigTime = digTime / 8
  252.   if blueInput == false then
  253.     digProgress = 0 --used to display iteration's progress on glass
  254.    
  255.     print("set black")-------------
  256.    
  257.     rs.setBundledOutput("top",colors.black) -->allows tesseract to transmit power
  258.     term.setTextColor(colors.green)
  259.     term.write("Iteration "..iterations)
  260.     dontPrintData = true
  261.     sleep(splitDigTime)
  262.     getData()
  263.     updateGlass()
  264.     sleep(splitDigTime)
  265.     for i=1,2 do
  266.       term.write(".")
  267.       digProgress = digProgress + 1
  268.       getData()
  269.       updateGlass()
  270.       term.setTextColor(colors.green)
  271.       sleep(splitDigTime)
  272.       getData()
  273.       updateGlass()
  274.       sleep(splitDigTime)
  275.     end
  276.     print(".")
  277.     digProgress = 3
  278.     dontPrintData = false
  279.     for i=1,2 do
  280.       getData()
  281.       updateGlass()
  282.       sleep(splitDigTime)
  283.     end
  284.     rs.setBundledOutput("top",0)
  285.    
  286.     print("set all off dig")--------------------
  287.    
  288.   elseif blueInput == true then
  289.     term.setTextColor(colors.red)
  290.     digProgress = 0
  291.     term.write("Skipped Iteration "..iterations)
  292.     sleep(splitDigTime)
  293.     dontPrintData = true
  294.     for i=1,2 do
  295.       term.write(".")
  296.       digProgress = digProgress + 1
  297.       getData()
  298.       updateGlass()
  299.       term.setTextColor(colors.red)
  300.       sleep(splitDigTime)
  301.       getData()
  302.       updateGlass()
  303.       sleep(splitDigTime)
  304.     end
  305.     print(",")
  306.     digProgress = 3
  307.     dontPrintData = false
  308.     for i=1,2 do
  309.       getData()
  310.       updateGlass()
  311.       sleep(splitDigTime)
  312.     end
  313.     digProgress = 0
  314.   end
  315. end
  316.  
  317. local function executeCommandInMine()
  318.   if commandString == "stop" then
  319.     iterations = 0
  320.   elseif commandString == "help" then
  321.     isError = true
  322.     error = "commands: 'mine <#>','help','stop','toggle <>'"
  323.   elseif commandString == "mine" then
  324.     isError = true
  325.     error = "Please enter 'stop' before mining again"
  326.   elseif commandString == "toggle" then  
  327.     if commandNum == "in-out" then
  328.       if isIn_OutPercentage == true then
  329.         isIn_OutPercentage = false
  330.       else
  331.         isIn_OutPercentage = true
  332.       end
  333.     elseif commandNum == "ae" then
  334.       rs.setBundledOutput("top",colors.yellow)
  335.       sleep(.5)
  336.       rs.setBundledOutput("top",0)
  337.     else
  338.       isError = true
  339.       error = "Usage: 'toggle <in-out/ae>'"
  340.     end
  341.   else
  342.     isError = true
  343.     error = "that is not a valid command, enter 'help'"
  344.   end
  345.   term.setTextColor(colors.red)
  346.   print(error)
  347.   term.setTextColor(colors.white)
  348.   commandString = "null"
  349.   commandNum = 0
  350. end
  351.  
  352. local function mine()
  353.   local isIterations = true
  354.   print("Mining Process Started with "..iterations.." iterations...")
  355.   if iterations == 0 then
  356.     term.setTextColor(colors.red)
  357.     error = "Error: 0 Iterations?"
  358.     print(error)
  359.     isError = true
  360.     term.setTextColor(colors.white)
  361.     isIterations = false
  362.   elseif iterations <= -1 then
  363.     iterations = -1
  364.   else
  365.     isIterations = true
  366.   end  
  367.   while isIterations == true do
  368.     getData()
  369.     updateGlass()
  370.     moveFrame()
  371.     dig()
  372.     if iterations > 0 then
  373.       iterations = iterations - 1
  374.     end
  375.     if iterations == 0 then
  376.       isIterations = false
  377.     end
  378.     if isChatCommand == true then
  379.       isChatCommand = false
  380.       checkCommand()
  381.       executeCommandInMine()
  382.     end
  383.   end
  384. end
  385.  
  386. local function executeCommand()
  387.   if commandString == "mine" then
  388.     iterations = commandNum
  389.     mine()
  390.   elseif commandString == "stop" then
  391.     iterations = 0
  392.   elseif commandString == "help" then
  393.     isError = true
  394.     error = "commands: 'help','mine <#>','stop','toggle <>'"
  395.   elseif commandString == "toggle" then
  396.     if commandNum == "in-out" then
  397.       if isIn_OutPercentage == true then
  398.         isIn_OutPercentage = false
  399.       else
  400.         isIn_OutPercentage = true
  401.       end
  402.     elseif commandNum == "ae" then
  403.       rs.setBundledOutput("top",colors.yellow)
  404.       sleep(.5)
  405.       rs.setBundledOutput("top",0)
  406.     else
  407.       isError = true
  408.       error = "Usage: 'toggle <in-out/ae>'"
  409.     end  
  410.   else
  411.     isError = true
  412.     error = "that is not a valid command, enter 'help'"
  413.   end
  414.   commandString = "null"
  415.   commandNum = 0
  416. end  
  417. ------------Main Function---------
  418. local function main()
  419.   while true do
  420.     getData()
  421.     updateGlass()
  422.     if isChatCommand == true then
  423.       isChatCommand = false
  424.       checkCommand()
  425.       executeCommand()
  426.     end
  427.     sleep(1)
  428.   end
  429. end
  430.  
  431. ------------Program-------
  432. parallel.waitForAny(main,listenForCommand) --runs main funtion and listens for commands at the same time
Advertisement
Add Comment
Please, Sign In to add comment