Advertisement
Jo__2001__Bo

CC Tweaked Reactor Control Wireless Client

Dec 13th, 2020 (edited)
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.90 KB | None | 0 0
  1. local button = {}
  2. local filleds = {}
  3. local boxes = {}
  4. local lines = {}
  5. local texts = {}
  6.  
  7. local refresh = true
  8.  
  9. local infosSize = {}
  10. local controlsSize = {}
  11. local numbersSize = {}
  12. local currentRfTotal = 1
  13. local currentRfTick = 1
  14. local currentFuelConsumedLastTick = 0.00001
  15.  
  16. local rfPerTickMax = 1
  17. local minPowerRod = 0
  18. local maxPowerRod = 100
  19. local currentRodLevel = 1
  20.  
  21. local monitors = {}
  22. local modems = {}
  23.  
  24. monitors = {peripheral.find("monitor")}
  25. modems = {peripheral.find("modem")}
  26.  
  27. local mon = monitors[1]
  28.  
  29. term.redirect(mon)
  30. mon.clear()
  31. mon.setTextColor(colors.white)
  32. mon.setBackgroundColor(colors.black)
  33.  
  34. function openChannel()
  35.     local modem = modems[1]
  36.     modem.open(1)
  37. end
  38.  
  39. function clearTable()
  40.     button = {}
  41. end
  42.  
  43. function setButton(name, title, func, xmin, ymin, xmax, ymax, elem, elem2, color)
  44.     button[name] = {}
  45.     button[name]["title"] = title
  46.     button[name]["func"] = func
  47.     button[name]["active"] = false
  48.     button[name]["xmin"] = xmin
  49.     button[name]["ymin"] = ymin
  50.     button[name]["xmax"] = xmax
  51.     button[name]["ymax"] = ymax
  52.     button[name]["color"] = color
  53.     button[name]["elem"] = elem
  54.     button[name]["elem2"] = elem2
  55. end
  56.  
  57. function fill(text, color, bData)
  58.    mon.setBackgroundColor(color)
  59.    mon.setTextColor(colors.white)
  60.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  61.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(bData["title"])) /2) +1
  62.    for j = bData["ymin"], bData["ymax"] do
  63.       mon.setCursorPos(bData["xmin"], j)
  64.       if j == yspot then
  65.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(bData["title"]) +1 do
  66.             if k == xspot then
  67.                mon.write(bData["title"])
  68.             else
  69.                mon.write(" ")
  70.             end
  71.          end
  72.       else
  73.          for i = bData["xmin"], bData["xmax"] do
  74.             mon.write(" ")
  75.          end
  76.       end
  77.    end
  78.    mon.setBackgroundColor(colors.black)
  79. end
  80.  
  81. function screen()
  82.    local currColor
  83.    for name,data in pairs(button) do
  84.       local on = data["active"]
  85.       currColor = data["color"]
  86.       fill(name, currColor, data)
  87.    end
  88. end
  89.  
  90. function flash(name)
  91.     screen()
  92. end
  93.  
  94. function checkxy(x, y)
  95.    for name, data in pairs(button) do
  96.       if y>=data["ymin"] and  y <= data["ymax"] then
  97.          if x>=data["xmin"] and x<= data["xmax"] then
  98.             data["func"](data["elem"], data["elem2"])
  99.             flash(data['name'])
  100.             return true
  101.          end
  102.       end
  103.    end
  104.    return false
  105. end
  106.  
  107. function draw()
  108.     for key,value in pairs(filleds) do
  109.         local counter = 1
  110.         for i=value[2],value[4],1 do
  111.             paintutils.drawLine(value[1] , value[2]+counter, value[3], value[2]+counter, value[5])
  112.             counter = counter + 1
  113.         end
  114.     end
  115.  
  116.     for key,value in pairs(boxes) do
  117.         paintutils.drawLine(value[1] , value[2], value[1], value[4], value[5])
  118.         paintutils.drawLine(value[1] , value[2], value[3], value[2], value[5])
  119.         paintutils.drawLine(value[1] , value[4], value[3], value[4], value[5])
  120.         paintutils.drawLine(value[3] , value[2], value[3], value[4], value[5])
  121.     end
  122.  
  123.     for key,value in pairs(lines) do
  124.         paintutils.drawLine(value[1] , value[2], value[3], value[4], value[5])
  125.     end
  126.  
  127.     for key,value in pairs(texts) do
  128.         mon.setCursorPos(value[1], value[2])
  129.         mon.setTextColor(value[4])
  130.         mon.setBackgroundColor(value[5])
  131.         mon.write(value[3])
  132.     end
  133.     screen()
  134.     resetDraw()
  135. end
  136.  
  137. function resetDraw()
  138.     filleds = {}
  139.     boxes = {}
  140.     lines = {}
  141.     texts = {}
  142. end
  143.  
  144. function clickEvent()
  145.     local myEvent={os.pullEvent("monitor_touch")}
  146.     checkxy(myEvent[3], myEvent[4])
  147. end
  148.  
  149. function powerUp(m,n)
  150.     local modem = modems[1]
  151.     modem.transmit(3, 1, {"start", 1})
  152. end
  153.  
  154. function powerDown(m,n)
  155.     local modem = modems[1]
  156.     modem.transmit(3, 1, {"stop", 1})
  157. end
  158.  
  159. function modifyRods(limit, number)
  160.     local tempLevel = 0
  161.  
  162.     if limit == "min" then
  163.         tempLevel = minPowerRod + number
  164.         if tempLevel <= 0 then
  165.             minPowerRod = 0
  166.         end
  167.  
  168.         if tempLevel >= maxPowerRod then
  169.             minPowerRod = maxPowerRod -10
  170.         end
  171.  
  172.         if tempLevel < maxPowerRod and tempLevel > 0 then
  173.             minPowerRod = tempLevel
  174.         end
  175.     else
  176.         tempLevel = maxPowerRod + number
  177.         if tempLevel <= minPowerRod then
  178.             maxPowerRod = minPowerRod +10
  179.         end
  180.  
  181.         if tempLevel >= 100 then
  182.             maxPowerRod = 100
  183.         end
  184.  
  185.         if tempLevel > minPowerRod and tempLevel < 100 then
  186.             maxPowerRod = tempLevel
  187.         end
  188.     end
  189.  
  190.     table.insert(lines, {controlsSize['inX'], controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, controlsSize['inX'] + controlsSize['width'], controlsSize['inY']+(controlsSize['sectionHeight']*1)+4, colors.black})
  191.  
  192.     table.insert(texts, {controlsSize['inX']+5, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, minPowerRod .. '%', colors.lightBlue, colors.black})
  193.     table.insert(texts, {controlsSize['inX']+13, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, '--', colors.white, colors.black})
  194.     table.insert(texts, {controlsSize['inX']+20, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, maxPowerRod .. '%', colors.purple, colors.black})
  195.    
  196.     transmitMinMax()
  197. end
  198.  
  199. function transmitMinMax()
  200.     local modem = modems[1]
  201.     modem.transmit(3, 1, {"min", minPowerRod, maxPowerRod})
  202. end
  203.  
  204. function addDrawBoxesSingleReactor()
  205.     local w, h = mon.getSize()
  206.     local margin = math.floor((w/100)*2)
  207.  
  208.     infosSize['startX'] = margin + 1
  209.     infosSize['startY'] =  margin + 1
  210.     infosSize['endX'] = (((w-(margin*2))/3)*2)-margin
  211.     infosSize['endY'] = h - margin
  212.     infosSize['height'] = infosSize['endY']-infosSize['startY']-(margin*2)-2
  213.     infosSize['width'] = infosSize['endX']-infosSize['startX']-(margin*2)-2
  214.     infosSize['inX'] = infosSize['startX'] + margin +1
  215.     infosSize['inY'] = infosSize['startY'] + margin +1
  216.     infosSize['sectionHeight'] = math.floor(infosSize['height']/3)
  217.  
  218.     table.insert(boxes, {infosSize['startX'] , infosSize['startY'], infosSize['endX'], infosSize['endY'], colors.gray})
  219.     local name = "INFOS"
  220.     table.insert(lines, {infosSize['startX'] + margin , infosSize['startY'], infosSize['startX'] + (margin*2) + #name+1, infosSize['startY'], colors.black})
  221.     table.insert(texts, {infosSize['startX'] + (margin*2), infosSize['startY'], name, colors.white, colors.black})
  222.  
  223.     local names = {}
  224.     names[1] = 'ENERGY LAST TICK'
  225.     names[2] = 'ENERGY STORED'
  226.     names[3] = 'CONTROL ROD LEVEL'
  227.  
  228.     for i=0,2,1 do
  229.         table.insert(texts, {infosSize['inX'] + margin, infosSize['inY'] + (infosSize['sectionHeight']*i) +i, names[i+1], colors.white, colors.black})
  230.         table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 2 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
  231.     end
  232.  
  233.  
  234.     -- Controls
  235.  
  236.     controlsSize['startX'] = infosSize['endX'] + margin + 1
  237.     controlsSize['startY'] =  margin + 1
  238.     controlsSize['endX'] = w-margin
  239.     controlsSize['endY'] = (((h - (margin*2))/3)*2) +1
  240.     controlsSize['height'] = controlsSize['endY']-controlsSize['startY']-(margin)-1
  241.     controlsSize['width'] = controlsSize['endX']-controlsSize['startX']-(margin*2)-2
  242.     controlsSize['inX'] = controlsSize['startX'] + margin +1
  243.     controlsSize['inY'] = controlsSize['startY'] + margin
  244.  
  245.     table.insert(boxes, {controlsSize['startX'] , controlsSize['startY'], controlsSize['endX'], controlsSize['endY'], colors.gray})
  246.     name = "CONTROLS"
  247.     table.insert(lines, {controlsSize['startX'] + margin , controlsSize['startY'], controlsSize['startX'] + (margin*2) + #name+1, controlsSize['startY'], colors.black})
  248.     table.insert(texts, {controlsSize['startX'] + (margin*2), controlsSize['startY'], name, colors.white, colors.black})
  249.  
  250.     controlsSize['sectionHeight'] = math.floor(controlsSize['height']/4)
  251.  
  252.     mon.setTextColor(colors.white)
  253.     setButton("ON","ON", powerUp, controlsSize['inX'], controlsSize['inY'], controlsSize['inX'] + math.floor(controlsSize['width']/2) -1, controlsSize['inY'] +2, 0, 0, colors.green)
  254.     setButton("OFF","OFF", powerDown, controlsSize['inX'] + math.floor(controlsSize['width']/2) +2, controlsSize['inY'], controlsSize['inX'] + controlsSize['width'], controlsSize['inY'] +2,0, 0, colors.red)
  255.  
  256.     table.insert(texts, {controlsSize['inX']+8, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+1, 'AUTO-CONTROL', colors.white, colors.black})
  257.  
  258.     table.insert(texts, {controlsSize['inX']+5, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+3, 'MIN', colors.lightBlue, colors.black})
  259.     table.insert(texts, {controlsSize['inX']+5, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, minPowerRod..'%', colors.lightBlue, colors.black})
  260.  
  261.     table.insert(texts, {controlsSize['inX']+13, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, '--', colors.white, colors.black})
  262.     table.insert(texts, {controlsSize['inX']+20, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+3, 'MAX', colors.purple, colors.black})
  263.     table.insert(texts, {controlsSize['inX']+20, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, maxPowerRod..'%', colors.purple, colors.black})
  264.     mon.setTextColor(colors.white)
  265.  
  266.     setButton("low-10","-10", modifyRods, controlsSize['inX'], controlsSize['inY'] +(controlsSize['sectionHeight']*2)+2, controlsSize['inX'] + math.floor(controlsSize['width']/2) -1, controlsSize['inY'] +(controlsSize['sectionHeight']*2)+4, "min", -10, colors.lightBlue)
  267.     setButton("high-10","-10", modifyRods, controlsSize['inX'] + math.floor(controlsSize['width']/2) +2, controlsSize['inY'] +(controlsSize['sectionHeight']*2)+2, controlsSize['inX'] + controlsSize['width'], controlsSize['inY'] +(controlsSize['sectionHeight']*2)+4, "max", -10, colors.purple)
  268.  
  269.     setButton("low+10","+10", modifyRods, controlsSize['inX'], controlsSize['inY'] +(controlsSize['sectionHeight']*3)+2, controlsSize['inX'] + math.floor(controlsSize['width']/2) -1, controlsSize['inY'] +(controlsSize['sectionHeight']*3)+4, "min", 10, colors.lightBlue)
  270.     setButton("high+10","+10", modifyRods, controlsSize['inX'] + math.floor(controlsSize['width']/2) +2, controlsSize['inY'] +(controlsSize['sectionHeight']*3)+2, controlsSize['inX'] + controlsSize['width'], controlsSize['inY'] +(controlsSize['sectionHeight']*3)+4, "max", 10, colors.purple)
  271.  
  272.     numbersSize['startX'] = infosSize['endX'] + margin + 1
  273.     numbersSize['startY'] = controlsSize['endY'] + margin + 1
  274.     numbersSize['endX'] = w-margin
  275.     numbersSize['endY'] = h-margin
  276.     numbersSize['height'] = numbersSize['endY']-numbersSize['startY']-(margin)-1
  277.     numbersSize['width'] = numbersSize['endX']-numbersSize['startX']-(margin*2)-2
  278.     numbersSize['inX'] = numbersSize['startX'] + margin +1
  279.     numbersSize['inY'] = numbersSize['startY'] + margin
  280.  
  281.     table.insert(boxes, {numbersSize['startX'] , numbersSize['startY'], numbersSize['endX'], numbersSize['endY'], colors.gray})
  282.     name = "NUMBERS"
  283.     table.insert(lines, {numbersSize['startX'] + margin , numbersSize['startY'], numbersSize['startX'] + (margin*2) + #name+1, numbersSize['startY'], colors.black})
  284.     table.insert(texts, {numbersSize['startX'] + (margin*2), numbersSize['startY'], name, colors.white, colors.black})
  285.  
  286.     refresh = true
  287.     while refresh do
  288.         parallel.waitForAny(refreshSingleReactor,clickEvent)
  289.     end
  290. end
  291.  
  292. function getAllStats()
  293.     local stats = {}
  294.     local modem = modems[1]
  295.  
  296.     local event, side, send, reply, message, distance = os.pullEvent("modem_message")
  297.     stats["rfTotal"] = message[1]
  298.     stats["rfPerTick"] = message[2]
  299.     stats["rodLevel"] = message[3]
  300.     stats["fuelPerTick"] = message[4]
  301.     stats["energyCapacity"] = message[5]
  302.    
  303.     return stats
  304. end
  305.  
  306. function refreshSingleReactor()
  307.     local rfPerTick = 0
  308.     local rfTotal = 0
  309.  
  310.     local allStats = getAllStats()
  311.     rfTotal = allStats["rfTotal"]
  312.     rfPerTick = allStats["rfPerTick"]
  313.     rodLevel = allStats["rodLevel"]
  314.     fuelPerTick = allStats["fuelPerTick"]
  315.     rfTotalMax = allStats["energyCapacity"]
  316.  
  317.     local i = 0
  318.     local infotoAdd = 'FE PER TICK : '
  319.  
  320.     if currentRfTick ~= rfPerTick then
  321.         currentRfTick = rfPerTick
  322.         if rfPerTick > rfPerTickMax then
  323.             rfPerTickMax = rfPerTick
  324.         end
  325.  
  326.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'],numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'], colors.black})
  327.         table.insert(texts, {numbersSize['inX'], numbersSize['inY'], infotoAdd .. rfPerTick .. " FE", colors.white, colors.black})
  328.         table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
  329.  
  330.         width = math.floor((infosSize['width'] / rfPerTickMax)*rfPerTick)
  331.         table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + width, infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.green})
  332.  
  333.     end
  334.  
  335.     i = 1
  336.     infotoAdd = 'ENERGY STORED : '
  337.     if currentRfTotal ~= rfTotal then
  338.         currentRfTotal = rfTotal
  339.  
  340.         table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
  341.  
  342.         width = math.floor((infosSize['width'] / rfTotalMax)*rfTotal)
  343.         table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + width, infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.green})
  344.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'] +2 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +2, colors.black})
  345.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 2 , infotoAdd .. rfTotal .. " FE", colors.white, colors.black})
  346.     end
  347.  
  348.     i = 2
  349.     infotoAdd = 'CONTROL ROD LEVEL : '
  350.     if currentRodLevel ~= rodLevel then
  351.         currentRodLevel = rodLevel
  352.  
  353.         table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
  354.  
  355.         width = math.floor((infosSize['width'] / 100)*rodLevel)
  356.         table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + width, infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.green})
  357.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+4 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +4, colors.black})
  358.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 4 , infotoAdd .. rodLevel .. "%", colors.white, colors.black})
  359.     end
  360.  
  361.     i = 3
  362.     infotoAdd = 'FUEL USAGE : '
  363.     if currentFuelConsumedLastTick ~= fuelPerTick then
  364.         currentFuelConsumedLastTick = fuelPerTick
  365.  
  366.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+6 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +6, colors.black})
  367.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 6 , infotoAdd .. format_num(tonumber(fuelPerTick),3) .. "mb/t", colors.white, colors.black})
  368.     end
  369.  
  370.     mon.setTextColor(colors.white)
  371.  
  372.     draw()
  373.  
  374.     sleep(2)
  375. end
  376.  
  377. function comma_value(amount)
  378.   local formatted = amount
  379.   while true do
  380.     formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  381.     if (k==0) then
  382.       break
  383.     end
  384.   end
  385.   return formatted
  386. end
  387.  
  388. function round(val, decimal)
  389.   if (decimal) then
  390.     return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  391.   else
  392.     return math.floor(val+0.5)
  393.   end
  394. end
  395.  
  396. function format_num(amount, decimal, prefix, neg_prefix)
  397.   local str_amount,  formatted, famount, remain
  398.  
  399.   decimal = decimal or 2
  400.   neg_prefix = neg_prefix or "-"
  401.  
  402.   famount = math.abs(round(amount,decimal))
  403.   famount = math.floor(famount)
  404.  
  405.   remain = round(math.abs(amount) - famount, decimal)
  406.  
  407.   formatted = comma_value(famount)
  408.  
  409.   if (decimal > 0) then
  410.     remain = string.sub(tostring(remain),3)
  411.     formatted = formatted .. "." .. remain ..
  412.                 string.rep("0", decimal - string.len(remain))
  413.   end
  414.  
  415.   formatted = (prefix or "") .. formatted
  416.  
  417.   if (amount<0) then
  418.     if (neg_prefix=="()") then
  419.       formatted = "("..formatted ..")"
  420.     else
  421.       formatted = neg_prefix .. formatted
  422.     end
  423.   end
  424.  
  425.   return formatted
  426. end
  427.  
  428. mon.setBackgroundColor(colors.black)
  429. mon.clear()
  430. mon.setTextScale(0.5)
  431.  
  432. openChannel()
  433.  
  434. addDrawBoxesSingleReactor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement