XIAOHAOZHI

Untitled

May 9th, 2021
619
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component=require("component")
  2. local event=require("event")
  3. local filesystem = require("filesystem")
  4. local serialization = require("serialization")
  5. local shell=require("shell")
  6. local term=require("term")
  7. local tty=require("tty")
  8.  
  9. local items_IC2={
  10.   ["IC2:reactorUraniumQuaddepleted"]={item="IC2:reactorUraniumQuad"},
  11.   ["IC2:reactorMOXQuaddepleted"]={item="IC2:reactorMOXQuad"},
  12.   ["IC2:reactorUraniumDualdepleted"]={item="IC2:reactorUraniumDual"},
  13.   ["IC2:reactorMOXDualdepleted"]={item="IC2:reactorMOXDual"},
  14.   ["IC2:reactorUraniumSimpledepleted"]={item="IC2:reactorUraniumSimple"},
  15.   ["IC2:reactorMOXSimpledepleted"]={item="IC2:reactorMOXSimple"},
  16.   ["IC2:reactorReflector"]={damage=0.01,item="IC2:reactorReflector"},
  17.   ["IC2:reactorReflectorThick"]={damage=0.01,item="IC2:reactorReflectorThick"},
  18.   ["IC2:reactorCondensator"]={damage=0.01,item="IC2:reactorCondensator"},
  19.   ["IC2:reactorCondensatorLap"]={damage=0.01,item="IC2:reactorCondensatorLap"},
  20.   ["IC2:reactorCoolantSix"]={damage=0.05,item="IC2:reactorCoolantSix"},
  21.   ["IC2:reactorCoolantTriple"]={damage=0.1,item="IC2:reactorCoolantTriple"},
  22.   ["IC2:reactorCoolantSimple"]={damage=0.2,item="IC2:reactorCoolantSimple"},
  23.   ["IC2:reactorVent"]={damage=0.8},
  24.   ["IC2:reactorVentCore"]={damage=0.8},
  25.   ["IC2:reactorVentGold"]={damage=0.8},
  26.   ["IC2:reactorVentDiamond"]={damage=0.8},
  27.   ["IC2:reactorHeatSwitch"]={damage=0.8},
  28.   ["IC2:reactorHeatSwitchCore"]={damage=0.8},
  29.   ["IC2:reactorHeatSwitchSpread"]={damage=0.8},
  30.   ["IC2:reactorHeatSwitchDiamond"]={damage=0.8},
  31.   ["ic2:overclocked_heat_vent"]={damage=0.8},
  32.   ["ic2:overclockedheatvent"]={damage=0.8},
  33.   ["ic2:overClockedHeatvent"]={damage=0.8},
  34.   ["IC2:reactorVentHeatDiamond"]={damage=0.8},
  35. }
  36.  
  37. local items_gt5={
  38.   ["IC2:reactorReflector"]={damage=0.01,item="IC2:reactorReflector"},
  39.   ["IC2:reactorReflectorThick"]={damage=0.01,item="IC2:reactorReflectorThick"},
  40.   ["IC2:reactorCondensator"]={damage=0.01,item="IC2:reactorCondensator"},
  41.   ["IC2:reactorCondensatorLap"]={damage=0.01,item="IC2:reactorCondensatorLap"},
  42.   ["IC2:reactorCoolantSix"]={damage=0.05,item="IC2:reactorCoolantSix"},
  43.   ["IC2:reactorCoolantTriple"]={damage=0.1,item="IC2:reactorCoolantTriple"},
  44.   ["IC2:reactorCoolantSimple"]={damage=0.2,item="IC2:reactorCoolantSimple"},
  45.   ["IC2:reactorVent"]={damage=0.8},
  46.   ["IC2:reactorVentCore"]={damage=0.8},
  47.   ["IC2:reactorVentGold"]={damage=0.8},
  48.   ["IC2:reactorVentDiamond"]={damage=0.8},
  49.   ["IC2:reactorHeatSwitch"]={damage=0.8},
  50.   ["IC2:reactorHeatSwitchCore"]={damage=0.8},
  51.   ["IC2:reactorHeatSwitchSpread"]={damage=0.8},
  52.   ["IC2:reactorHeatSwitchDiamond"]={damage=0.8},
  53.   ["gregtech:gt.60k_Helium_Coolantcell"]={damage=0.2,item="gregtech:gt.60k_Helium_Coolantcell"},
  54.   ["gregtech:gt.180k_Helium_Coolantcell"]={damage=0.1,item="gregtech:gt.180k_Helium_Coolantcell"},
  55.   ["gregtech:gt.360k_Helium_Coolantcell"]={damage=0.05,item="gregtech:gt.360k_Helium_Coolantcell"},
  56.   ["gregtech:gt.60k_NaK_Coolantcell"]={damage=0.2,item="gregtech:gt.60k_NaK_Coolantcell"},
  57.   ["gregtech:gt.180k_NaK_Coolantcell"]={damage=0.1,item="gregtech:gt.180k_NaK_Coolantcell"},
  58.   ["gregtech:gt.360k_NaK_Coolantcell"]={damage=0.05,item="gregtech:gt.360k_NaK_Coolantcell"},
  59.   ["IC2:reactorUraniumQuaddepleted"]={item="gregtech:gt.reactorUraniumQuad"},
  60.   ["IC2:reactorUraniumDualdepleted"]={item="gregtech:gt.reactorUraniumDual"},
  61.   ["IC2:reactorUraniumSimpledepleted"]={item="gregtech:gt.reactorUraniumSimple"},
  62.   ["IC2:reactorMOXQuaddepleted"]={item="gregtech:gt.reactorMOXQuad"},
  63.   ["IC2:reactorMOXDualdepleted"]={item="gregtech:gt.reactorMOXDual"},
  64.   ["IC2:reactorMOXSimpledepleted"]={item="gregtech:gt.reactorMOXSimple"},
  65.   ["gregtech:gt.Quad_NaquadahcellDep"]={item="gregtech:gt.Quad_Naquadahcell"},
  66.   ["gregtech:gt.Double_NaquadahcellDep"]={item="gregtech:gt.Double_Naquadahcell"},
  67.   ["gregtech:gt.NaquadahcellDep"]={item="gregtech:gt.Naquadahcell"},
  68.   ["gregtech:gt.Quad_ThoriumcellDep"]={item="gregtech:gt.Quad_Thoriumcell"},
  69.   ["gregtech:gt.Double_ThoriumcellDep"]={item="gregtech:gt.Double_Thoriumcell"},
  70.   ["gregtech:gt.ThoriumcellDep"]={item="gregtech:gt.Thoriumcell"},
  71. }
  72.  
  73. local function getCom(t)
  74.   if component.list(t) then
  75.     return component.proxy(component.list(t)())
  76.   else
  77.     return nil
  78.   end
  79. end
  80.  
  81. local rs=getCom("redstone")
  82. local reactor=getCom("reactor")
  83. local tra=getCom("trans")
  84.  
  85. function putTable(t,f)
  86.   if type(t)=="table" then
  87.     local file = filesystem.open(f,"w")
  88.     file:write(serialization.serialize(t))
  89.     file:close()
  90.     return true
  91.   else
  92.     return false
  93.   end
  94. end
  95.  
  96. function getTable(f)
  97.   if filesystem.exists(f) then
  98.     local file = io.open(f,"r")
  99.     local fileSize = filesystem.size(f)
  100.     fileRead = file:read(fileSize)
  101.     local fileContent = fileRead
  102.     file:close()
  103.     return serialization.unserialize(fileContent)
  104.   else
  105.     return false
  106.   end
  107. end
  108.  
  109. local function paddingMid(s,t)
  110.   local tt=t
  111.   if not tt then tt=" " end
  112.   local w, h = tty.gpu().getViewport()
  113.   local nLeft=math.floor((w-string.len(s))/2)
  114.   return string.rep(tt,nLeft)..s..string.rep(tt,w-nLeft-string.len(s))
  115. end
  116.  
  117. local function paddingLeft(s,t)
  118.   local tt=t
  119.   if not tt then tt=" " end
  120.   local w, h = tty.gpu().getViewport()
  121.   return s..string.rep(tt,w-string.len(s))
  122. end
  123.  
  124. local function getKey()
  125.   local result=-1
  126.   while (not result) or (result<0) or (result>5) do
  127.     result=tonumber(io.read())
  128.   end
  129.   return result
  130. end
  131.  
  132. local function getConfig()
  133.   -- 进行【方向配置】。如果存在配置文件就读取内容,提示是否使用;如果没有配置文件或不用已存在的配置文件,就重新设置。
  134.   local cfg = getTable("/home/reactor.cfg")
  135.   shell.execute("resolution 50 16")
  136.   term.clear()
  137.   if cfg then
  138.     print("Current configuration:")
  139.     print("------------------------------------------")
  140.     for key,s in pairs(cfg) do
  141.       print(key..": "..s)
  142.     end
  143.     print("------------------------------------------")
  144.     print("0:down 1:top 2:north 3:south 4:west 5:east")
  145.     print("------------------------------------------")
  146.     print("Do you want to use this config? [Y/n]")
  147.     if io.read()=="n" then cfg=nil end
  148.   end
  149.   if not cfg then
  150.     term.clear()
  151.     cfg={}
  152.     print("Please config the sides:")
  153.     print("------------------------------------------")
  154.     print("0:down 1:top 2:north 3:south 4:west 5:east")
  155.     print("------------------------------------------")
  156.     -- 红石信号发射方向(相对红石方块)
  157.     term.write("which side does [REDSTONE] Output? ")
  158.     cfg["redstone"]=getKey()
  159.     -- 反应堆的方向(相对换位器)
  160.     term.write("which side does REACTOR towards [TRANSPOSER]? ")
  161.     cfg["reactor"]=getKey()
  162.     -- 燃料箱子的方向(相对换位器)
  163.     term.write("which side does FuelBox towards [TRANSPOSER]? ")
  164.     cfg["fuelbox"]=getKey()
  165.     -- 废料箱子的方向(相对换位器)
  166.     term.write("which side does WasteBox towards [TRANSPOSER]? ")
  167.     cfg["wastebox"]=getKey()
  168.     -- 过热温度
  169.     term.write("what is overheat temperature?(0.0001~1) ")
  170.     cfg["overheat"]=getKey()
  171.     -- 版本
  172.     term.write("Which MOD are you using?(1=ic2,2=gt5) ")
  173.     cfg["mod"]=getKey()
  174.     --写入配置文档
  175.     putTable(cfg,"/home/reactor.cfg")
  176.   end
  177.   shell.execute("resolution 32 6")
  178.   term.clear()
  179.   return cfg
  180. end
  181.  
  182. local function keyDown(t)
  183.   local result
  184.   if t then
  185.     _,_,result=event.pull(t,"key_down")
  186.   else
  187.     _,_,result=event.pull("key_down")
  188.   end
  189.   if not result then result=0 end
  190.   return result
  191. end
  192.  
  193. ---------------------程序开始-----------------
  194. local w1, h1 = tty.gpu().getViewport()
  195. local items
  196. if rs and reactor and tra then
  197.   local cfg=getConfig()
  198.   -- work start
  199.   local w, h = tty.gpu().getViewport()
  200.   local command="s"
  201.   while true do
  202.     if cfg.mod==1 then items=items_IC2
  203.     elseif cfg.mod==2 then items=items_gt5
  204.     else print("Wrong MOD name! Exit!"); rs.setOutput(cfg["redstone"],0); break;
  205.     end
  206.     local heat=reactor.getHeat()
  207.     local heatMax=reactor.getMaxHeat()
  208.     local running=reactor.producesEnergy()
  209.     term.setCursor(1,1)
  210.     term.write(paddingLeft("DATE/TIME:    "..os.date()))
  211.     term.setCursor(1,2)
  212.     term.write(paddingLeft("Heat:         "..heat.."  / "..heatMax))
  213.     term.setCursor(1,3)
  214.     term.write(paddingLeft(string.rep("#",math.floor( w * heat / heatMax )),"."))
  215.     term.setCursor(1,4)
  216.     term.write(paddingLeft(" "))
  217.     term.setCursor(1,5)
  218.     if running then
  219.       term.write(paddingMid("<<< RUNNING >>>"))
  220.     else
  221.       term.write(paddingMid("<<< STOP >>>"))
  222.     end
  223.     term.setCursor(1,6)
  224.     term.write("[Run] [Stop] [eXit] [Config] "..command)
  225.     if battery then
  226.       term.setCursor(1,7)
  227.       local tmpTime=os.clock()
  228.       local tmpEnergy=math.floor(battery.getEnergy()/10)
  229.       if tmpTime - timeStamp > 0.02 then
  230.         energyRatio=math.floor((tmpEnergy-energyStamp)/(tmpTime-timeStamp)/60/20)
  231.         timeStamp=tmpTime
  232.         energyStamp=tmpEnergy
  233.       end
  234.       term.write(string.sub("Energy:"..tmpEnergy.."EU "..energyRatio.."EU/t                ",1,w))
  235.     end
  236.     if heat/heatMax>cfg["overheat"] then -- 过热则停机。
  237.       if running then rs.setOutput(cfg["redstone"],0) end
  238.     else
  239.       -- 检查是否有燃料棒需要更换
  240.       local item_in_reactor=tra.getAllStacks(cfg["reactor"]).getAll()
  241.       local ready=true
  242.       for i=0,#item_in_reactor-4 do
  243.         if item_in_reactor[i] and items[item_in_reactor[i].name] then -- 如果当前反应堆位置有物品,且该物品在items中有记录
  244.           -- 判断是否 low damage。判断标准:无 damage 或当前物品的剩余耐久小于 damage
  245.           local lowDamage=((not items[item_in_reactor[i].name].damage) or ((item_in_reactor[i].maxDamage-item_in_reactor[i].damage)<=(items[item_in_reactor[i].name].damage*item_in_reactor[i].maxDamage)))
  246.           ready=(ready and (not lowDamage)) -- 只要存在低耐久物品,本轮就不要开机了
  247.           if lowDamage then
  248.             if running then
  249.               rs.setOutput(cfg["redstone"],0)
  250.               running=false
  251.             end
  252.             if items[item_in_reactor[i].name].item then  -- 需要替换
  253.               tra.transferItem(cfg["reactor"],cfg["wastebox"],1,i+1)
  254.               -- 从燃料箱子里查找替换品
  255.               local boxLocation=0
  256.               while boxLocation==0 do
  257.                 local item_in_box=tra.getAllStacks(cfg["fuelbox"]).getAll()
  258.                 local k=0
  259.                 while (boxLocation==0) and (k<=#item_in_box) do
  260.                   if item_in_box[k] and (item_in_box[k].name==items[item_in_reactor[i].name].item) then
  261.                     boxLocation=k+1
  262.                     break
  263.                   end
  264.                   k=k+1
  265.                 end
  266.                 -- 找到了就换,找不到就等
  267.                 if (boxLocation>0) and tra.transferItem(cfg["fuelbox"],cfg["reactor"],1,boxLocation,i+1) then
  268.                   break
  269.                 else
  270.                   term.setCursor(1,4)
  271.                   term.write("         Fuel shortage! ")
  272.                   os.sleep(1)
  273.                 end
  274.               end
  275.             end
  276.           end
  277.         end
  278.       end
  279.       -- command=r且停机且ready,才可以开机
  280.       if (command=="r") and (not running) and ready then rs.setOutput(cfg["redstone"],15) end
  281.     end
  282.     local key=keyDown(0.2)
  283.     if key==115 then command="s"; rs.setOutput(cfg["redstone"],0); -- s=Stop
  284.     elseif key==114 then command="r" -- r=Run
  285.     elseif key==120 then rs.setOutput(cfg["redstone"],0); break; -- x=eXit
  286.     elseif key==99 then -- c=Config, 先停机再配置
  287.       rs.setOutput(cfg["redstone"],0)
  288.       cfg=getConfig()
  289.     end
  290.   end
  291.   shell.execute("resolution "..w1.." "..h1)
  292. else
  293.   -- 组件不完整,退出
  294.   print("Please check components: REDSTONE, TRANSPOSER, REACTOR")
  295. end
RAW Paste Data