BILLPC2684

ReactorTurbine.lua

Jun 19th, 2024 (edited)
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.58 KB | Source Code | 0 0
  1. local reactor = peripheral.wrap("BigReactors-Reactor_0")
  2. local turbine = peripheral.wrap("BigReactors-Turbine_0")
  3. local tank = peripheral.wrap("dynamicValve_0")
  4.  
  5. function fixedRound(x) return math.floor(x*10000)/10000 end
  6. function denull(x) if x == nil then return 0 else return x end end
  7. toboolean={[true]=true, ["true"]=true, [1]=true, ["1"]=true, [false]=false, ["false"]=false, [0]=false, ["false"]=false}
  8.  
  9.  
  10. block_chars = {'\128','\144','\143 ','\139 ','\131 ','\130 ','\128 '}
  11. local bars = {}
  12. function make_bar(name,center,x,y,start,length,maxi,vertical)
  13.  local bar = {
  14.   name = tostring(name),
  15.   center_name = toboolean[center],
  16.   length = tonumber(length),
  17.   amount = 0,
  18.   pre_amount = 0,
  19.   maximum = tonumber(maxi),
  20.   adjusting = tonumber(start),
  21.   fill_frame = 0,
  22.   render_x = tonumber(x),
  23.   render_y = tonumber(y),
  24.   vertical = toboolean[vertical],
  25.   percentage = 0,
  26.   speed = 1,
  27.  }
  28.  function bar:update()
  29.   self.percentage = self.amount/self.maximum
  30.   local block_percent = (self.maximum/self.length)/6/self.maximum
  31.   local blocks = self.percentage/block_percent
  32.   local blocks_round = math.floor(blocks+0.5)
  33.   local tmpx = self.render_x
  34.   if self.vertical then
  35.    term.setCursorPos(self.render_x,self.render_y)
  36.    io.write(self.name)
  37.    self.render_x = self.render_x+math.floor(#self.name/2)-1
  38.    term.setCursorPos(self.render_x,self.render_y+self.length+1)
  39.    io.write("'\045'")
  40.   else
  41.    term.setCursorPos(self.render_x         ,self.render_y  ); io.write(string.format(".%s.",string.rep("-",self.length)))
  42.    term.setCursorPos(self.render_x         ,self.render_y+1); io.write("|")
  43.    term.setCursorPos(self.render_x+length+1,self.render_y+1); io.write("|")
  44.    term.setCursorPos(self.render_x         ,self.render_y+2); io.write(string.format("'%s'",string.rep("\045",self.length)))
  45.    term.setCursorPos(self.render_x+math.floor((#self.name/2)/(self.length/2)),self.render_y); io.write(self.name)
  46.    term.setCursorPos(self.render_x+1,self.render_y+1)
  47.   end
  48.   for i=1,self.length do
  49.    if self.vertical then
  50.     --term.setCursorPos(1,17);
  51.     --term.clearLine()
  52.     --io.write(fixedRound(self.percentage),fixedRound(block_percent),fixedRound(blocks),i*6,math.min(blocks-((i-1)*6),6)+1,block_chars[math.min(math.max(blocks_round-((i-1)*6),0),6)+1])
  53.     term.setCursorPos(self.render_x,self.length-i+self.render_y+1);
  54.     io.write('|')
  55.     if #block_chars[math.min(math.max(blocks_round-((i-1)*6),0),5)+1] > 1 then term.setBackgroundColor(colors.white); term.setTextColor(colors.black) end
  56.     io.write(string.sub(block_chars[math.min(math.max(blocks_round-((i-1)*6),0),6)+1],1,1))
  57.     term.setBackgroundColor(colors.black); term.setTextColor(colors.white)
  58.     io.write('|')
  59. --    io.write('|',math.min(math.max(blocks-(i*6),0),5)+1,'|')
  60.    else
  61.     --term.setCursorPos(1,1);
  62.     --term.clearLine()
  63.     --print(fixedRound(self.percentage),fixedRound(block_percent),fixedRound(blocks),i*6,math.min(blocks-((i-1)*6),6)+1,block_chars[math.min(math.max(blocks_round-((i-1)*6),0),6)+1])
  64.     --term.setCursorPos(self.render_x+i,self.render_y+1);
  65.     if #block_chars[math.min(math.max(blocks_round-((i-1)*6),0),5)+1] > 1 then term.setBackgroundColor(colors.white); term.setTextColor(colors.black) end
  66.     io.write(string.sub(block_chars[math.min(math.max(blocks_round-((i-1)*6),0),6)+1],1,1))
  67.     --io.write(math.min(math.max(blocks-(i*6),0),5)+1)
  68.     term.setBackgroundColor(colors.black); term.setTextColor(colors.white)
  69.    end
  70.   end self.render_x = tmpx
  71.   --adjusting now
  72.   if self.adjusting > 0 then
  73.    self.adjusting = self.adjusting - self.speed
  74.    self.amount = self.amount + self.speed
  75.    if self.speed < self.adjusting/8 then
  76.     self.speed = self.speed * 2
  77.    else
  78.     self.speed = math.max(self.speed/2,1)
  79.    end
  80.   elseif self.adjusting < 0 then
  81.    self.adjusting = self.adjusting + self.speed
  82.    self.amount = self.amount - self.speed
  83.    if self.speed > self.adjusting/8 then
  84.     self.speed = self.speed * 2
  85.    else
  86.     self.speed = math.max(self.speed/2,1)
  87.    end
  88.   end
  89.  end
  90.  function bar:move(x,y) self.render_x = tonumber(x); self.render_y = tonumber(y) end
  91.  function bar:rename(str) self.name = tostring(str) end
  92.  function bar:sub(x) self.adjusting = math.min(self.adjusting - tonumber(x),-self.amount) end
  93.  function bar:add(x) self.adjusting = math.max(self.adjusting + tonumber(x),self.maximum-self.amount) end
  94.  function bar:set(x) self.adjusting = math.min(math.max(tonumber(x) - self.amount, self.maximum-self.amount),-self.amount) end
  95.  function bar:setmax(x) self.maximum = tonumber(x) end
  96.  table.insert(bars,bar)
  97. end
  98.  
  99. function updateBars()
  100.  for i=1,#bars do bars[i]:update() end
  101. end
  102.  
  103. function findBar(str)
  104.  for i=1,#bars do
  105.   if bars[i].name == tostring(str) then
  106.    return i
  107.   else
  108.    print(string.format("Could Not Find Bar: \"%s\"",tostring(str)))
  109.   end
  110.  end
  111. end
  112.  
  113. term.clear()
  114. --make_bar("Test1",false, 3,3, 50, 8,100, true)
  115. make_bar("Steam",false, 3,3,tank.getTankCapacity(),12,tank.getTankCapacity(), true)
  116. make_bar("Test2", true,10,3,255,12,256,false)
  117.  
  118. local steam_bar = findBar("Steam") if not steam_bar then return end steam_bar = bars[steam_bar]
  119. while true do
  120.  --term.clear()
  121.  local tank_level = tank.getFilledPercentage()
  122.  --bars[steam_bar]:set(denull(tank.getStored().amount))
  123.  
  124.  term.setCursorPos(1,1)
  125.  term.clearLine() print(steam_bar.speed,steam_bar.amount,fixedRound(steam_bar.percentage),steam_bar.adjusting)
  126.  term.clearLine() print(tank.getTankCapacity(),tank_level,reactor.getActive())
  127.  
  128.  if tank_level < 70 then
  129.   reactor.setActive(True)
  130.  elseif tank_level > 90 then
  131.   reactor.setActive(false)
  132.  end
  133.  updateBars()
  134.  os.sleep(0.01)
  135. end
Advertisement
Add Comment
Please, Sign In to add comment