cyber_Ahn

Ficsit-Networks-Industrie-Main-Control

Apr 13th, 2022 (edited)
2,710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.95 KB | None | 0 0
  1. screen_name = "Main_Screen"
  2. emergency_power_limit = 4500
  3. power_text_length = 54
  4. cont_text_length = 64
  5. line_per_page = 48
  6. local switch = component.proxy(component.findComponent("Switch"))
  7. local gen = component.proxy(component.findComponent("Generator"))
  8. local akw = component.proxy(component.findComponent("AKW"))
  9. local storage = component.proxy(component.findComponent("Storage"))
  10. local constructor = component.proxy(component.findComponent("Constructor"))
  11. local fabricator = component.proxy(component.findComponent("Fabricator"))
  12. local powerpole = component.proxy(component.findComponent("PowerPole"))
  13. local panel = component.proxy(component.findComponent("Panel"))
  14. local machine = component.proxy(component.findComponent("Machine"))
  15. local fluid = component.proxy(component.findComponent("F_Storage"))
  16. local bat = component.proxy(component.findComponent("Battery"))
  17. local pole = component.proxy(component.findComponent("Pole"))
  18. local maschine_stat = "Offline"
  19. local page = 1
  20. local max_page = 1
  21. --------------main functions-----------
  22. function tableConcat( t1, t2 )
  23.     for i=1, #t2 do
  24.        t1[#t1+1] = t2[i]
  25.     end
  26.     return t1
  27. end
  28.  
  29. function tableHasValue( t, value )
  30.     if t == nil or value == nil then
  31.         return false
  32.     end
  33.  
  34.     for _,v in pairs( t ) do
  35.         if v == value then
  36.             return true
  37.         end
  38.     end
  39.  
  40.     return false
  41. end
  42.  
  43. function getComponentsByClass( class, getOne )
  44.     local results = {}
  45.  
  46.     if ( getOne == nil ) then
  47.         getOne = false
  48.     end
  49.  
  50.     if type( class ) == "table" then
  51.  
  52.         for _, c in pairs( class ) do
  53.             local proxies = getComponentsByClass( c, getOne )
  54.             if not getOne then
  55.                 tableConcat( results, proxies )
  56.             else
  57.                 if( proxies ~= nil ) then
  58.                     return proxies
  59.                 end
  60.             end
  61.         end
  62.  
  63.     elseif type( class ) == "string" then
  64.  
  65.         local ctype = classes[ class ]
  66.         if ctype ~= nil then
  67.             local comps = component.findComponent( ctype )
  68.             for _, c in pairs( comps ) do
  69.                 local proxy = component.proxy( c )
  70.                 if getOne and proxy ~= nil then
  71.                     return proxy
  72.                 elseif not tableHasValue( results, proxy ) then
  73.                     table.insert( results, proxy )
  74.                 end
  75.             end
  76.         end
  77.  
  78.     end
  79.  
  80.     if ( getOne ) then
  81.         return {}
  82.     end
  83.  
  84.     return results
  85. end
  86.  
  87. function getComponentsByClassAndNick( class, nickParts )
  88.     if type( nickParts ) == 'string' then
  89.         nickParts = { nickParts }
  90.     end
  91.  
  92.     local classComponents = getComponentsByClass( class )
  93.     local results = {}
  94.  
  95.     for _, component in pairs( classComponents ) do
  96.         for _, nickPart in pairs( nickParts ) do
  97.             if component.nick:find( nickPart, 1, true ) == nil then
  98.                 goto nextComponent
  99.             end
  100.         end
  101.  
  102.         table.insert( results, component )
  103.  
  104.         ::nextComponent::
  105.     end
  106.  
  107.     return results
  108. end
  109.  
  110. function settingsFromString( str, lowerKeys )
  111.     lowerKeys = lowerKeys or false
  112.     local results = {}
  113.     if str == nil or type( str ) ~= "string" then return results end
  114.     for key, value in string.gmatch( str, '(%w+)=(%b"")' ) do
  115.         if lowerKeys then key = string.lower( key ) end
  116.         results[ key ] = string.sub( value, 2, string.len( value ) - 1 )
  117.     end
  118.     return results
  119. end
  120.  
  121. function settingsFromComponentNickname( proxy, lowerKeys )
  122.     if proxy == nil then return nil end
  123.     return settingsFromString( proxy[ "nick" ], lowerKeys )
  124. end
  125.  
  126. Vector2d = {
  127.     x = 0,
  128.     y = 0,
  129.     pattern = '{x=%d,y=%d}',
  130. }
  131. Vector2d.__index = Vector2d
  132. function Vector2d.new( x, y )
  133.     if x == nil or type( x ) ~= "number" then return nil end
  134.     if y == nil or type( y ) ~= "number" then return nil end
  135.     local o = { x = math.floor( x ), y = math.floor( y ) }
  136.     setmetatable( o, { __index = Vector2d } )
  137.     return o
  138. end
  139.  
  140. function split(s, delimiter)
  141.  result = {};
  142.  for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  143.   table.insert(result, match);
  144.  end
  145.  return result;
  146. end
  147. --------------GPU----------------------
  148. local gpu = computer.getPCIDevices( classes.GPU_T2_C )[1]
  149. if gpu == nil then
  150.     computer.panic( "No GPU T2 found. Cannot continue." )
  151. end
  152. print(gpu)
  153. --------------Screen-------------------
  154. local computerSettings = settingsFromComponentNickname( computer.getInstance() )
  155. local screens = getComponentsByClassAndNick( {
  156.     "ModuleScreen_C",
  157.     "Build_Screen_C",
  158. }, computerSettings.screen or '' )
  159. if #screens == 0 then
  160.     computer.panic( "No screen found. Cannot continue." )
  161. end
  162.  
  163. if screen_name ~= "empty" then
  164.     for i=1, #screens do
  165.         screen_data = screens[i]
  166.         screen_table_data = split(tostring(screen_data),'"')
  167.         if screen_name == screen_table_data[2] then
  168.             gpu:bindScreen( screens[i] )
  169.             screenSize = gpu:getScreenSize()
  170.             print( 'Screen resolution: ' .. screenSize.x .. 'x' .. screenSize.y )
  171.         end
  172.     end
  173. else
  174.     gpu:bindScreen( screens[1] )
  175.     screenSize = gpu:getScreenSize()
  176.     print( 'Screen resolution: ' .. screenSize.x .. 'x' .. screenSize.y )
  177. end
  178. --------------line calc----------------
  179. function get_line(line)
  180.     spacer = (size + 10) * line
  181.     return spacer
  182. end
  183. --------------row calc-----------------
  184. function get_row(row)
  185.     spacer = (size - 25) * row
  186.     return spacer
  187. end
  188. ---------------------------------------
  189.  
  190. -------------------------------------------------------------------------------------------------------------------------------------
  191. function draw_frame(x,y,le,dire,size_d,color_d)
  192.     for i=0, le do
  193.         if dire == "right" then
  194.             gpu:drawText(Vector2d.new(get_row(x+i),get_line(y)),"_",size_d,color_d,TRUE)
  195.         end
  196.         if dire == "down" then
  197.             gpu:drawText(Vector2d.new(get_row(x),get_line(y+i)),"|",size_d,color_d,TRUE)
  198.         end
  199.     end
  200. end
  201.  
  202. function round(x)
  203.     return math.floor(x + 0.5)
  204. end
  205.  
  206. function get_max_power(n)
  207.     connector = powerpole[n]:getPowerConnectors()[1]
  208.     circuit = connector:getCircuit()
  209.     return circuit.capacity
  210. end
  211.  
  212. function bar(color_bar, x, y, w, h, fill,size_d,color_d)
  213.     fillLine = round((fill * h) / 100)
  214.     for i=1, h do
  215.         gpu:drawText(Vector2d.new(get_row(x-1),get_line(y)),"|",size_d,color_d,TRUE)
  216.         gpu:drawText(Vector2d.new(get_row(x+w+1),get_line(y)),"|",size_d,color_d,TRUE)
  217.         y = y - 1
  218.     end
  219.     y = y + h
  220.     for i=1, fillLine do
  221.         for i=1, w do
  222.             gpu:drawText(Vector2d.new(get_row(x),get_line(y)),"■",size_d,color_bar,TRUE)
  223.             x = x + 1
  224.         end
  225.         x = x - w
  226.         y = y - 1
  227.     end
  228. end
  229. ----------Parameters------------------
  230. size = 48
  231. color_white = {1.000, 1.000, 1.000, 1.0}
  232. color_red = {1.000, 0.000, 0.000, 1.0}
  233. color_yellow = {1.000, 1.000, 0.000, 1.0}
  234. color_green = {0.000, 1.000, 0.000, 1.0}
  235. w = screenSize.x
  236. h = screenSize.y
  237. ---------------------------------------
  238. gpu:flush()
  239. gpu:drawText(Vector2d.new(get_row(50),get_line(20)),"Start Main_PC......",size,color_white,TRUE)
  240. gpu:drawText(Vector2d.new(get_row(50),get_line(21)),"Please wait......",size,color_red,TRUE)
  241. gpu:flush()
  242.  
  243. ---------------Screen-Functions---------
  244. function bios()
  245.     for i=1, #switch do
  246.         print("Switch_"..i.."-ID: "..switch[i].id)
  247.         switch[i].isSwitchOn = false
  248.     end
  249.     for i=1, #gen do
  250.         print("Bio_Generator_"..i.."-ID: "..gen[i].id)
  251.     end
  252.     for i=1, #storage do
  253.         print("Storage_"..i.."-ID: "..storage[i].id)
  254.     end
  255.     for i=1, #constructor do
  256.         print("Constructor_"..i.."-ID: "..constructor[i].id)
  257.     end
  258.     for i=1, #fabricator do
  259.         print("Fabricator_"..i.."-ID: "..fabricator[i].id)
  260.     end
  261.     for i=1, #fabricator do
  262.         print("Power_Powle_"..i.."-ID: "..powerpole[i].id)
  263.     end
  264.     for i=1, #akw do
  265.         print("AKW_"..i.."-ID: "..akw[i].id)
  266.     end
  267.     for i=1, #panel do
  268.         print("Panel_"..i.."-ID: "..panel[i].id)
  269.     end
  270.     for i=1, #machine do
  271.         print("Machine_"..i.."-ID: "..machine[i].id)
  272.     end
  273.     for i=1, #fluid do
  274.         print("Fluid_Storage_"..i.."-ID: "..fluid[i].id)
  275.     end
  276.     for i=1, #bat do
  277.         print("Battery_"..i.."-ID: "..bat[i].id)
  278.     end
  279.     for i=1, #pole do
  280.         print("Pole_"..i.."-ID: "..pole[i].id)
  281.         pole[i]:setcolor(1,0,0,5)
  282.     end
  283.     gpu:flush()
  284.     gpu:drawText(Vector2d.new(get_row(0),get_line(0)),"caworks Modular Bios V.10",size,color_white,TRUE)
  285.     gpu:drawText(Vector2d.new(get_row(0),get_line(1)),"2011 - 2013 caworks Software Inc",size,color_white,TRUE)
  286.     gpu:drawText(Vector2d.new(get_row(0),get_line(18)),"Processor caCore 2,4 GHZ x4",size,color_white,TRUE)
  287.     gpu:drawText(Vector2d.new(get_row(0),get_line(19)),"Memory Testing: ok  2096084K",size,color_white,TRUE)
  288.     gpu:drawText(Vector2d.new(get_row(0),get_line(20)),"----------------------------------------",size,color_white,TRUE)
  289.     gpu:drawText(Vector2d.new(get_row(0),get_line(21)),"IDE Channel 0: Master  CW34342525 500GB",size,color_white,TRUE)
  290.     gpu:drawText(Vector2d.new(get_row(0),get_line(22)),"IDE Channel 1: NONE",size,color_white,TRUE)
  291.     gpu:drawText(Vector2d.new(get_row(0),get_line(50)),"Press F12 for Bios Setup",size,color_white,TRUE)
  292.     gpu:flush()
  293.     event.pull(5)
  294.     gpu:flush()
  295.     main_prog()
  296. end
  297. function main_prog()
  298.      event.pull(3)
  299.     for i=1, #akw do
  300.         akw[i].standby = false
  301.     end
  302.     for i=1, #gen do
  303.         gen[i].standby = true
  304.     end
  305.     for i=1, #machine do
  306.         machine[i].standby = false
  307.         maschine_stat = "Online"
  308.     end
  309.     for i=1, #switch do
  310.         switch[i]:setIsSwitchOn(true)
  311.     end
  312.     for i=1, #pole do
  313.         pole[i]:setcolor(0,1,0,5)
  314.     end
  315.     while true do
  316.         gpu:drawText(Vector2d.new(get_row(45),get_line(0)),"caworks Control V1.0",size,color_white,TRUE)
  317.         power = 0
  318.         if #powerpole > 0 then
  319.             power = get_max_power(1)
  320.         end
  321.         if(power < emergency_power_limit) then
  322.             for i=1, #gen do
  323.                 gen[i].standby = false
  324.             end
  325.             for i=1, #machine do
  326.                 machine[i].standby = true
  327.                 maschine_stat = "Offline"
  328.             end
  329.             for i=1, #switch do
  330.                 switch[i]:setIsSwitchOn(false)
  331.             end
  332.             gpu:drawText(Vector2d.new(get_row(1),get_line(2)),"Current Max Power: "..power.." MWh",size,color_red,TRUE)
  333.         end
  334.         if(power > emergency_power_limit) then
  335.             for i=1, #gen do
  336.                 gen[i].standby = true
  337.             end
  338.             for i=1, #switch do
  339.                 switch[i]:setIsSwitchOn(true)
  340.             end
  341.             for i=1, #machine do
  342.                 machine[i].standby = false
  343.                 maschine_stat = "Online"
  344.             end
  345.             gpu:drawText(Vector2d.new(get_row(1),get_line(2)),"Current Max Power: "..power.." MWh",size,color_white,TRUE)
  346.         end
  347.         lines = 0
  348.         for i=1, #akw do
  349.             cach = akw[i].standby
  350.             out = "Offline"
  351.             if cach == false then
  352.                 out = "Online"
  353.                 gpu:drawText(Vector2d.new(get_row(1+12),get_line(i+3)),out,size,color_green,TRUE)
  354.                 gpu:drawText(Vector2d.new(get_row(1),get_line(i+3)),"AKW "..i.." is ",size,color_white,TRUE)
  355.             else
  356.                 gpu:drawText(Vector2d.new(get_row(1+12),get_line(i+3)),out,size,color_red,TRUE)
  357.                 gpu:drawText(Vector2d.new(get_row(1),get_line(i+3)),"AKW "..i.." is ",size,color_white,TRUE)
  358.             end
  359.             lines = i
  360.         end
  361.         for i=1, #gen do
  362.             cach = gen[i].standby
  363.             out = "Offline"
  364.             if cach == false then
  365.                 out = "Online"
  366.                 gpu:drawText(Vector2d.new(get_row(25+20),get_line(i+3)),out,size,color_green,TRUE)
  367.                 gpu:drawText(Vector2d.new(get_row(25),get_line(i+3)),"Generator "..i.." is ",size,color_white,TRUE)
  368.             else
  369.                 gpu:drawText(Vector2d.new(get_row(25+20),get_line(i+3)),out,size,color_red,TRUE)
  370.                 gpu:drawText(Vector2d.new(get_row(25),get_line(i+3)),"Generator "..i.." is ",size,color_white,TRUE)
  371.             end
  372.             if lines < i then
  373.                 lines = i
  374.             end
  375.         end
  376.         draw_frame(1,1,power_text_length-2,"right",size,color_white)
  377.         draw_frame(0,2,lines+1,"down",size,color_white)
  378.         draw_frame(power_text_length,2,lines+1,"down",size,color_white)
  379.         draw_frame(1,lines+3,power_text_length-2,"right",size,color_white)
  380.         linesB = 0 
  381.         line_start = 1
  382.         line_end = line_per_page
  383.         max_page = math.floor(#storage / line_per_page)
  384.         if max_page < (#storage / line_per_page) then
  385.             max_page = max_page + 1
  386.         end
  387.         if page > 1 then
  388.             line_start = line_per_page * (page-1)
  389.             line_end = line_per_page * page
  390.         end
  391.         if line_end > #storage then
  392.             line_end = #storage
  393.         end
  394.         for i=line_start, line_end do
  395.             text_line = i+1
  396.             if page > 1 then
  397.                 text_line = (i-line_per_page)+1
  398.             end
  399.             inv = storage[i]:getInventories()[1]
  400.             inv:sort()
  401.             amount = inv.itemCount
  402.             itemName = "Empty"
  403.             max = 0
  404.             if amount > 1 then
  405.                 max = inv:getStack(0).count * inv.size
  406.                 itemName = inv:getStack(0).item.type.name
  407.             end
  408.             gpu:drawText(Vector2d.new(get_row(power_text_length+2),get_line(text_line)),"Container_"..i,size,color_white,TRUE)
  409.             gpu:drawText(Vector2d.new(get_row(power_text_length+20),get_line(text_line)),itemName,size,color_white,TRUE)
  410.             if (max/2) > amount then
  411.                 gpu:drawText(Vector2d.new(get_row(power_text_length+48),get_line(text_line)),amount.."/"..max,size,color_red,TRUE)
  412.             elseif amount == 0 then
  413.                 gpu:drawText(Vector2d.new(get_row(power_text_length+48),get_line(text_line)),amount.."/"..max,size,color_red,TRUE)
  414.             elseif amount == max then
  415.                 gpu:drawText(Vector2d.new(get_row(power_text_length+48),get_line(text_line)),amount.."/"..max,size,color_green,TRUE)
  416.             elseif (max/2) < amount then
  417.                 gpu:drawText(Vector2d.new(get_row(power_text_length+48),get_line(text_line)),amount.."/"..max,size,color_yellow,TRUE)
  418.             end
  419.             gpu:drawText(Vector2d.new(get_row(power_text_length+55),get_line(0)),"Page: "..page,size,color_white,TRUE)
  420.             linesB = i
  421.         end
  422.         draw_frame(power_text_length+1,1,cont_text_length,"right",size,color_white)
  423.         draw_frame(power_text_length,2,linesB,"down",size,color_white)
  424.         draw_frame(power_text_length+cont_text_length+1,2,linesB,"down",size,color_white)
  425.         draw_frame(power_text_length+1,linesB+2,cont_text_length-1,"right",size,color_white)
  426.         num_machine = #machine
  427.         gpu:drawText(Vector2d.new(get_row(1),get_line(lines+5)),"Machine found: "..num_machine,size,color_white,TRUE)
  428.         gpu:drawText(Vector2d.new(get_row(1),get_line(lines+6)),"Machine are ",size,color_white,TRUE)
  429.         if maschine_stat == "Offline" then
  430.             gpu:drawText(Vector2d.new(get_row(18),get_line(lines+6)),maschine_stat,size,color_red,TRUE)
  431.         end
  432.         if maschine_stat == "Online" then
  433.             gpu:drawText(Vector2d.new(get_row(18),get_line(lines+6)),maschine_stat,size,color_green,TRUE)
  434.         end
  435.         draw_frame(0,lines+4,3,"down",size,color_white)
  436.         draw_frame(power_text_length,lines+4,3,"down",size,color_white)
  437.         draw_frame(1,lines+7,power_text_length-2,"right",size,color_white)
  438.         linesC = 0 
  439.         for i=1, #fluid do
  440.             itemName = "Empty"
  441.             isF = fluid[i].fluidContent
  442.             maxF = 0
  443.             if isF > 1 then
  444.                 maxF = fluid[i].maxFluidContent
  445.                 itemName = fluid[i]:getFluidType().name
  446.             end
  447.             gpu:drawText(Vector2d.new(get_row(1),get_line(lines+8+i)),"Fluid_"..i,size,color_white,TRUE)
  448.             gpu:drawText(Vector2d.new(get_row(13),get_line(lines+8+i)),itemName,size,color_white,TRUE)
  449.             if (maxF/2) > isF then
  450.                 gpu:drawText(Vector2d.new(get_row(40),get_line(lines+8+i)),round(isF).."/"..round(maxF),size,color_RED,TRUE)
  451.             elseif (maxF/2) < isF then
  452.                 gpu:drawText(Vector2d.new(get_row(40),get_line(lines+8+i)),round(isF).."/"..round(maxF),size,color_yellow,TRUE)
  453.             elseif isF == 0 then
  454.                 gpu:drawText(Vector2d.new(get_row(40),get_line(lines+8+i)),round(isF).."/"..round(maxF),size,color_red,TRUE)
  455.             elseif isF == maxF then
  456.                 gpu:drawText(Vector2d.new(get_row(40),get_line(lines+8+i)),round(isF).."/"..round(maxF),size,color_green,TRUE)
  457.             elseif isF > maxF then
  458.                 gpu:drawText(Vector2d.new(get_row(40),get_line(lines+8+i)),round(isF).."/"..round(maxF),size,color_white,TRUE)
  459.             end
  460.             if itemName == "Sulfuric Acid" and isF >= maxF then
  461.                 print("flush")
  462.                 fluid[i]:flush()
  463.             end
  464.             if itemName == "Water" and isF >= maxF then
  465.                 print("flush")
  466.                 fluid[i]:flush()
  467.             end
  468.             if itemName == "Alumina Solutionw" and isF >= maxF then
  469.                 print("flush")
  470.                 fluid[i]:flush()
  471.             end
  472.             linesC = i
  473.         end
  474.         draw_frame(1,lines+9+linesC,power_text_length-2,"right",size,color_white)
  475.         draw_frame(0,lines+8,linesC+1,"down",size,color_white)
  476.         draw_frame(power_text_length,lines+8,linesC+1,"down",size,color_white)  
  477.         if #bat > 0 then
  478.             maxCap = 0
  479.             maxStored = 0
  480.             for i=1, #bat do
  481.                 store = bat[i].powerStore
  482.                 cap = bat[i].powerCapacity
  483.                 storeMW = (store * cap) / 100
  484.                 maxCap = maxCap + cap
  485.                 maxStored = maxStored + storeMW
  486.             end
  487.             maxPre = (maxStored * 100) / maxCap
  488.             gpu:drawText(Vector2d.new(get_row(120),get_line(0)),round(maxPre).."%",size,color_white,TRUE)
  489.             gpu:drawText(Vector2d.new(get_row(120),get_line(1)),round(maxCap).."MW",size,color_white,TRUE)
  490.             if maxPre == 100 then
  491.                 bar(color_green, 122, 49, 4, 48, maxPre,size,color_white)
  492.             else
  493.                 bar(color_red, 122, 49, 4, 48, maxPre,size,color_white)
  494.             end
  495.         end
  496.  -----------------------------------------------------------------------------------------------------------------------------------
  497.         gpu:flush()
  498.         if #panel ~= 0 then
  499.             button_1 = panel[1]:getModule(0,0)
  500.             event.listen(button_1)
  501.             e, s = event.pull(30)
  502.             if s == button_1 then
  503.                 computer.beep(5)
  504.                 page = page + 1
  505.                 if page > max_page then
  506.                     page = 1
  507.                 end
  508.             end
  509.         end
  510.     end
  511. end
  512.  
  513. ---------------------------------------
  514.  
  515. bios()
Advertisement
Add Comment
Please, Sign In to add comment