Ramog

Main Reactor Program

Aug 24th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.62 KB | None | 0 0
  1. term.clear()
  2.  
  3. local battery
  4. local Tankinfos
  5. local TankCapacity
  6. local batteryCapacity
  7. local batteryLevel
  8. local batteyLevelPercent
  9. local reachedKick = false
  10. local savefile = "Backup.txt"
  11. local firststart
  12. local bundledcabel = 0
  13. local isInSettingmode = false
  14. local input
  15.  
  16.  
  17. function EditFile(text, action, file)
  18.     local h
  19.     if action == write then
  20.         h = fs.open(file, fs.exists(file) and "w")
  21.         h.write(text)
  22.     elseif action == read then
  23.         h = fs.open(file, "r")
  24.         filecontent = h.readAll()
  25.         return filecontent
  26.     end
  27.     h.close()
  28. end
  29.  
  30. function converttofile(array)
  31.     local Datatofile = textutils.serialize(array)
  32.     EditFile(Datatofile,write,savefile)
  33. end
  34.  
  35. function converttotable()
  36.     local Datafromfile = EditFile("", read,savefile)
  37.     local array = textutils.unserialize(Datafromfile)
  38.     return array
  39. end
  40.  
  41. if fs.exists(savefile) then
  42.     firststart = false
  43. else
  44.     fs.open(savefile, "w")
  45.     firststart = true
  46.     fs.open(savefile, "w").close()
  47. end
  48.  
  49. function startedfirst()
  50. term.clear()
  51.     term.setCursorPos(1,1)
  52.     print("How many reactors do you have/plan to have?(max. 16) ")
  53.    
  54.     local numberofreactors = tonumber(io.read())
  55.     if numberofreactors > 16 then
  56.         term.clear()
  57.         term.setBackgroundColor(colors.red)
  58.         print("Number is to High!")
  59.         sleep(1)
  60.         startedfirst()
  61.     end
  62.     local Reactors = {}
  63.     local percentstart = 55
  64.     local percentincrease = math.ceil(55 / numberofreactors)
  65.     local overwrite = false
  66.     for n = 1, numberofreactors do 
  67.         Reactors[n] = {percentstart,overwrite}
  68.         percentstart = percentstart - percentincrease
  69.     end
  70.     converttofile(Reactors)
  71. end
  72.  
  73.  
  74. if firststart then
  75.     startedfirst()
  76. else
  77. --  local newTabID = multishell.launch({},"editor",)
  78. --  multishell.setTitle(newTabID, Editor)
  79. end
  80.  
  81. function getValues()
  82.     battery = peripheral.find("mfsu")
  83.     Tank = peripheral.find("thermalexpansion_tank")
  84.     local array = Tank.getTankInfo()
  85.     TankCapacity = array[1].capacity
  86.     batteryCapacity = battery.getEUCapacity()
  87.     batteryLevel = battery.getEUStored()
  88.     batteryLevelPercent = batteryLevel / batteryCapacity
  89.    
  90.     if array[1].contents == nil then
  91.         TankLevel = 0
  92.     else   
  93.         TankLevel = array[1].contents.amount
  94.     end
  95. end
  96.  
  97. function ReactorControl()
  98.     if TankLevel < TankCapacity - 10000 then
  99.        
  100.         local reactors = converttotable()
  101.         local percentvalues = {}       
  102.        
  103.         if TankLevel > 50000 then
  104.             rs.setOutput("left",true)
  105.         elseif TankLevel < 10000 then
  106.             rs.setOutput("left",false)
  107.         end
  108.         bundledcabel = 0
  109.         for n = 1, #reactors do    
  110.             local percent = reactors[n][1]
  111.             if batteryLevelPercent * 100 >= 100 then
  112.                 bundledcabel = 0
  113.             elseif batteryLevelPercent * 100 < percent then
  114.                 bundledcabel = bundledcabel + (2^n)/2
  115.             elseif reactors[n][2] == true then
  116.                 bundledcabel = bundledcabel + (2^n)/2
  117.             end
  118.         end
  119.         redstone.setBundledOutput("right",bundledcabel)    
  120.        
  121.         term.setBackgroundColor(colors.blue)
  122.         term.clear()   
  123.         term.setCursorPos(1,1)
  124.         term.setBackgroundColor(colors.orange)
  125.         textutils.tabulate(colors.blue, {"Energy","Tank"}, colors.white,{batteryLevel.." of "..batteryCapacity,TankLevel.." of "..TankCapacity})
  126.         term.setTextColor(colors.white)
  127.  
  128.         sleep(0.5)
  129.     end
  130. end
  131.  
  132. function input()
  133.     input = io.read()
  134.     if input == "s" then
  135.         isInSettingmode = true
  136.     end
  137.     sleep(0,5)
  138. end
  139.  
  140. function settingMenu()
  141.     local reactors = converttotable()
  142.     local reactorstatus
  143.    
  144.     term.clear()
  145.     term.setCursorPos(1,1)
  146.     for n = 1, #reactors do
  147.         if reactors[n][1] then
  148.             reactorstatus = "on"
  149.         else
  150.             reactorstatus = "off"
  151.         end
  152.         print("Reactor "..n..": "..reactorstatus)
  153.     end
  154.     term.clear()
  155. end
  156.  
  157.  
  158.  
  159. while true do  
  160.         getValues()
  161.         ReactorControl()
  162.         sleep(0.5)
  163. end
Advertisement
Add Comment
Please, Sign In to add comment