Advertisement
Putnam

turbine+reactor script #2

Dec 16th, 2015
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.72 KB | None | 0 0
  1. local metaturbine={__index=function(self,key) return self.subTurbine[key] end}
  2.  
  3. interval=0.5
  4.  
  5. --[[
  6.  
  7. States:
  8.  
  9. 0: No reactor running. On standby (freely spinning, no steam) until power is needed.
  10. 1: Generating power. Will turn on and off reactor as needed to keep turbine at maximum efficiency.
  11. 2: Spinning up. Will keep reactor on and coils off until the turbine is above ideal and sufficiently inefficient, at which point it will switch to 1.
  12.  
  13. ]]
  14.  
  15. function percentify(num)
  16.     return tostring(math.floor(((num)*1000)+0.5)/10)..'%'
  17. end
  18.  
  19. function buildTurbine(turbinePeripheral)
  20.     local superTurbine={subTurbine=turbinePeripheral}
  21.     superTurbine.getEfficiency=function(self)
  22.         local rotorSpeed=self.getRotorSpeed()
  23.         return rotorSpeed<500 and 0.5 or 0.25*(math.cos(rotorSpeed/(45.5*math.pi))+3)
  24.     end
  25.     superTurbine.turbineActivities=function(self)
  26.         local RPM=self.subTurbine.getRotorSpeed()
  27.         local energyStored=self.subTurbine.getEnergyStored()
  28.         if energyStored<100000 then
  29.             reactorState=1
  30.         elseif energyStored>900000 then
  31.             reactorState=0
  32.         end
  33.         local efficiency=self:getEfficiency()
  34.         if reactorState==0 then
  35.             self.setInductorEngaged(false)
  36.             term.setCursorPos(1,3)
  37.             self.standbyTicks=self.standbyTicks+1
  38.             term.write('Current status: standby')
  39.             powerMode=false
  40.         elseif reactorState==1 then
  41.             if efficiency<0.999 then
  42.                 if RPM<self.idealAmount then
  43.                     term.setCursorPos(1,3)
  44.                     term.write('Current status: spinning up')
  45.                     self.setInductorEngaged(false)
  46.                     self.spinUpTicks=self.spinUpTicks+1
  47.                     self.subTurbine.setFluidFlowRateMax(self.subTurbine.getFluidFlowRateMaxMax())
  48.                     powerMode=true
  49.                     reactorState=2
  50.                 else
  51.                     term.setCursorPos(1,3)
  52.                     term.write('Current status: slowing down')
  53.                     self.slowTicks=self.slowTicks+1
  54.                     powerMode=false
  55.                     self.setInductorEngaged(true)
  56.                 end
  57.             else
  58.                 term.setCursorPos(1,3)
  59.                 term.write('Current status: generating power')
  60.                 self.upTicks=self.upTicks+1
  61.                 powerMode=true
  62.                 self.setInductorEngaged(true)
  63.             end
  64.             self.rotorNumber=math.max(self.rotorNumber,math.floor(self.getFluidFlowRate()/25))
  65.         else
  66.             term.setCursorPos(1,3)
  67.             term.write('Current status: spinning up')
  68.             self.setInductorEngaged(false)
  69.             powerMode=true
  70.             self.spinUpTicks=self.spinUpTicks+1
  71.             self.subTurbine.setFluidFlowRateMax(self.subTurbine.getFluidFlowRateMaxMax())
  72.             if RPM>self.idealAmount and efficiency<0.999 then
  73.                 reactorState=1
  74.             end
  75.         end
  76.         term.setCursorPos(1,4)
  77.         term.write('Turbine powergen time: ' .. percentify(self.upTicks/tickCount))
  78.         term.setCursorPos(1,6)
  79.         term.write('Turbine slowdown time: ' .. percentify(self.slowTicks/tickCount))
  80.         term.setCursorPos(1,5)
  81.         term.write('Turbine spinup time: ' .. percentify(self.spinUpTicks/tickCount))
  82.         term.setCursorPos(1,7)
  83.         term.write('Turbine standby time: ' .. percentify(self.standbyTicks/tickCount))
  84.     end
  85.     superTurbine.idealAmount=1800
  86.     superTurbine.rotorNumber=0
  87.     superTurbine.spinUpTicks=0
  88.     superTurbine.standbyTicks=0
  89.     superTurbine.upTicks=0
  90.     superTurbine.slowTicks=0
  91.     setmetatable(superTurbine,metaturbine)
  92.     return superTurbine
  93. end
  94.  
  95. local function findPeripherals()
  96.   local reactor,turbines=nil,{}
  97.   turbines={}
  98.   for k,v in pairs(peripheral.getNames()) do
  99.     if (peripheral.getType(v) == "BigReactors-Reactor") then
  100.       reactor = peripheral.wrap(v)
  101.       print('reactor found!')
  102.     elseif (peripheral.getType(v) == "BigReactors-Turbine") then
  103.       local turbine=buildTurbine(peripheral.wrap(v))
  104.       table.insert(turbines,turbine)
  105.       print('turbine ' .. #turbines .. ' found!')
  106.     end
  107.   end
  108.   turbines.getConnected=function(self)
  109.     for _,turbineTable in pairs(self) do
  110.       if type(turbineTable)=='table' then
  111.         if not turbineTable.turbine.getConnected() then return false end
  112.       end
  113.     end
  114.     return true
  115.   end
  116.   turbines.getEnergyStored=function(self)
  117.     local sum=0
  118.     for _,turbineTable in pairs(self) do
  119.       if type(turbineTable)=='table' then
  120.         sum=sum+turbineTable.turbine.getEnergyStored()
  121.       end
  122.     end
  123.     return sum
  124.   end
  125.   turbines.getHotFluidRequired=function(self)
  126.     local sum=0
  127.     for _,turbinetable in pairs(self) do
  128.       if type(turbineTable)=='table' then
  129.         sum=sum+turbineTable.getFluidFlowRateMax()
  130.       end
  131.     end
  132.     return sum
  133.   end
  134.   return reactor, turbines
  135. end
  136.  
  137. local reactor,turbines=findPeripherals()
  138.  
  139. tickCount,reactorUpTicks=0,0
  140.  
  141. local function reactorActivities()
  142.     if not powerMode then
  143.         reactor.setActive(false)
  144.     else
  145.         reactorUpTicks=reactorUpTicks+1
  146.         reactor.setActive(true)
  147.     end
  148.     term.setCursorPos(1,1)
  149.     term.write('The reactor is currently: ' .. (powerMode and 'on' or 'off'))
  150.     term.setCursorPos(1,2)
  151.     term.write('Tick count: '..tickCount)
  152.     term.setCursorPos(1,8)
  153.     term.write('Reactor uptime percent: '..percentify(reactorUpTicks/tickCount))
  154. end
  155.  
  156. local function activities()
  157.     term.clear()
  158.     for k,turbine in ipairs(turbines) do turbine:turbineActivities(tickCount) end
  159.     reactorActivities()
  160. end
  161.  
  162. while true do
  163.     os.sleep(interval)
  164.     activities()
  165.     tickCount=tickCount+1
  166. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement