Advertisement
THEdarkkman

CC mekanism Induction Battery Monitoring V1

May 10th, 2024
646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.33 KB | Software | 0 0
  1. -- Terminal
  2. term.clear()
  3. term.setCursorPos(1,1)
  4.  
  5. -- Monitor preparation
  6. local monitor = peripheral.find("monitor")
  7. if monitor then
  8.     monitor.setBackgroundColour(colors.gray)
  9.     monitor.clear()
  10.     monitor.setCursorPos(1,1)
  11. end
  12.  
  13. -- Functions
  14. -- Print text serialised
  15. local function printText(text)
  16.     print(textutils.serialise(text))
  17. end
  18.  
  19. -- Convert Joules to RF(Redstone Flux)
  20. local function convertToRF(amntEnergy)
  21.     return math.floor(amntEnergy / 2.5)
  22. end
  23.  
  24. -- Find suffix and convert value
  25. local function suffixFinder(val)
  26.     val = val + 1       -- bugProof
  27.     local suffix
  28.     local energy = 1    -- bugProof
  29.     if val < 1000 then
  30.         suffix = ''
  31.     elseif val < 1000000 then
  32.         energy = val / 1000
  33.         suffix = 'k'
  34.     elseif val < 1000000000 then
  35.         energy = val / 1000000
  36.         suffix = 'M'
  37.     elseif val < 1000000000000 then
  38.         energy = val / 1000000000
  39.         suffix = 'G'
  40.     else
  41.         energy = val / 1000000000000
  42.         suffix = 'T'
  43.     end
  44.     return suffix, energy
  45. end
  46.  
  47. -- Round a number to a specific number of decimal places
  48. local function roundToDecimal(number, decimalPlaces)
  49.     local powerOf10 = 10 ^ decimalPlaces
  50.     return math.floor(number * powerOf10 + 0.5) / powerOf10
  51. end
  52.  
  53. -- Input X amount of blank spaces
  54. local function blankSpaces(num1)
  55.     local spaces = ""
  56.     for i = 1, num1 do
  57.         spaces = spaces .. " "
  58.     end
  59.     return spaces
  60. end
  61.  
  62. --[[ Monitor draw region
  63. local function drawRegion(posX, posY, color)
  64.     local xx, xy = posX[1], posX[2]
  65.     local yx, yy = posY[1], posY[2]
  66.     for i = 1, yx - xx + 1, 1 do
  67.         monitor.setCursorPos(xx, xy)
  68.         monitor.setBackgroundColour(color)
  69.         monitor.write(blankSpaces(yy - xy))
  70.     end
  71.     monitor.setBackgroundColour(colors.gray)
  72. end]]
  73.  
  74. -- Monitor draw region
  75. --[[
  76.     - pos1          Top-left coordinates
  77.     - pos2          Bottom-right corner coordinates
  78.     - monitorColor  Color you want to draw with
  79.     - monitor       Monitor you want to draw to
  80. ]]  
  81. local function drawRegion(pos1, pos2, monitorColor, monitor)
  82.     local x1, y1 = pos1[1], pos1[2]     -- Top-left corner coordinates
  83.     local x2, y2 = pos2[1], pos2[2]     -- Bottom-right corner coordinates
  84.     local numSpaces = y2 - y1 + 1       -- Calculate the number of blank spaces to write
  85.     if monitor then
  86.         printText("Monitor is working")
  87.         printText("Color: " .. monitorColor)
  88.         printText("Drawing region at x1:" .. x1 .. " x2:"..x2.." y1:"..y1.." y2:"..y2)
  89.         printText("Drawed region is "..numSpaces.. " pixels long")
  90.     end
  91.     monitor.setBackgroundColour(monitorColor)  -- Set the color for the draw
  92.  
  93.     -- Loop through each line of the region
  94.     for y = y1, y2 do
  95.         -- Loop through each column of the region
  96.         for x = x1, x2 do
  97.             monitor.setCursorPos(x,y)
  98.             monitor.write(" ")
  99.         end
  100.     end
  101.     -- Reset the background color to the default
  102.     monitor.setBackgroundColour(colors.gray)
  103. end
  104.  
  105.  
  106.  
  107. -- Variable declaration
  108. local inductionPort
  109. local test = true
  110. local pos1
  111. local pos2
  112.  
  113. -- Check for induction port in every side
  114. for _, side in pairs(peripheral.getNames()) do
  115.     if "inductionPort" == peripheral.getType(side) then
  116.         printText(peripheral.getType(side))
  117.         inductionPort = peripheral.wrap(side)
  118.     end
  119. end
  120.  
  121. -- Get current + max energy
  122. local currentEnergySuffix, currentEnergy = suffixFinder(convertToRF(inductionPort.getEnergy()))
  123. currentEnergy = roundToDecimal(currentEnergy, 2)
  124. local maxEnergySuffix, maxEnergy = suffixFinder(convertToRF(inductionPort.getMaxEnergy()))
  125. maxEnergy = roundToDecimal(maxEnergy, 1)
  126.  
  127. -- Get input + output + difference
  128. local inputSuffix, energyInput = suffixFinder(convertToRF(inductionPort.getLastInput()))
  129. energyInput = roundToDecimal(energyInput, 0)
  130. local outputSuffix, energyOutput  = suffixFinder(convertToRF(inductionPort.getLastOutput()))
  131. energyOutput = roundToDecimal(energyOutput, 0)
  132. local ioDiff = energyInput - energyOutput
  133.  
  134. -- Monitor presentation
  135. -- Title
  136. monitor.setCursorPos(1,1)
  137. monitor.write("INDUCTION BATTERY")
  138. monitor.setCursorPos(1,2)
  139. monitor.write("-----------------")
  140. -- Stored energy
  141. monitor.setCursorPos(1,3)
  142. monitor.write("Energy")
  143. monitor.setCursorPos(10,3)
  144. monitor.write(":")
  145. monitor.setCursorPos(11,3)
  146. monitor.write(currentEnergy ..currentEnergySuffix .."RF / " .. maxEnergy ..maxEnergySuffix .."RF")
  147. -- Input
  148. monitor.setCursorPos(1,4)
  149. monitor.write("Input")
  150. monitor.setCursorPos(10,4)
  151. monitor.write(":")
  152. monitor.setCursorPos(11,4)
  153. monitor.write(energyInput .. inputSuffix .. "RF")
  154. -- Output
  155. monitor.setCursorPos(1,5)
  156. monitor.write("Output")
  157. monitor.setCursorPos(10,5)
  158. monitor.write(":")
  159. monitor.setCursorPos(11,5)
  160. monitor.write(energyOutput .. outputSuffix .. "RF")
  161. -- Draw a progress bar
  162. -- Draw the initial bar
  163. local monitorColor = colors.black
  164. pos1 = {1, 18}
  165. pos2 = {29, 19}
  166. drawRegion(pos1, pos2, monitorColor, monitor)
  167. -- Draw the actual bar
  168. monitorColor = colors.red
  169. local progressBar = roundToDecimal(inductionPort.getEnergyFilledPercentage() * 28 + 1, 1)
  170. pos1 = {1, 18}
  171. pos2 = {progressBar, 19}
  172. drawRegion(pos1, pos2, monitorColor, monitor)
  173.  
  174. if test then
  175.     -- Test and printing
  176.     printText("Testing print:")
  177.     printText("color is " .. monitorColor)
  178.     printText(inductionPort.getEnergyFilledPercentage())
  179. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement