Advertisement
XIAOHAOZHI

OC

Feb 18th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.58 KB | None | 0 0
  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={
  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=5,item="IC2:reactorReflector"},
  17.   ["IC2:reactorReflectorThick"]={damage=5,item="IC2:reactorReflectorThick"},
  18.   ["IC2:reactorCondensator"]={damage=5,item="IC2:reactorCondensator"},
  19.   ["IC2:lzh_condensator"]={damage=15000,item="IC2:lzh_condensator"},
  20.   ["IC2:reactorCoolantSix"]={damage=100,item="IC2:reactorCoolantSix"},
  21.   ["IC2:reactorCoolantTriple"]={damage=200,item="IC2:reactorCoolantTriple"},
  22.   ["IC2:reactorCoolantSimple"]={damage=500,item="IC2:reactorCoolantSimple"},
  23.   ["IC2:reactorVent"]={damage=8000},
  24.   ["IC2:reactorVentCore"]={damage=8000},
  25.   ["IC2:reactorVentGold"]={damage=8000},
  26.   ["IC2:reactorVentDiamond"]={damage=8000},
  27.   ["IC2:reactorHeatSwitch"]={damage=8000},
  28.   ["IC2:reactorHeatSwitchCore"]={damage=8000},
  29.   ["IC2:reactorHeatSwitchSpread"]={damage=8000},
  30.   ["IC2:reactorHeatSwitchDiamond"]={damage=8000},
  31. }
  32.  
  33. local function getCom(t)
  34.   if component.list(t) then
  35.     return component.proxy(component.list(t)())
  36.   else
  37.     return nil
  38.   end
  39. end
  40.  
  41. local rs=getCom("redstone")
  42. local reactor=getCom("reactor")
  43. local tra=getCom("trans")
  44.  
  45. function putTable(t,f)
  46.   if type(t)=="table" then
  47.     local file = filesystem.open(f,"w")
  48.     file:write(serialization.serialize(t))
  49.     file:close()
  50.     return true
  51.   else
  52.     return false
  53.   end
  54. end
  55.  
  56. function getTable(f)
  57.   if filesystem.exists(f) then
  58.     local file = io.open(f,"r")
  59.     local fileSize = filesystem.size(f)
  60.     fileRead = file:read(fileSize)
  61.     local fileContent = fileRead
  62.     file:close()
  63.     return serialization.unserialize(fileContent)
  64.   else
  65.     return false
  66.   end
  67. end
  68.  
  69. local function paddingMid(s,t)
  70.   local tt=t
  71.   if not tt then tt=" " end
  72.   local w, h = tty.gpu().getViewport()
  73.   local nLeft=math.floor((w-string.len(s))/2)
  74.   return string.rep(tt,nLeft)..s..string.rep(tt,w-nLeft-string.len(s))
  75. end
  76.  
  77. local function paddingLeft(s,t)
  78.   local tt=t
  79.   if not tt then tt=" " end
  80.   local w, h = tty.gpu().getViewport()
  81.   return s..string.rep(tt,w-string.len(s))
  82. end
  83.  
  84. local function getKey()
  85.   local result=-1
  86.   while (not result) or (result<0) or (result>5) do
  87.     result=tonumber(io.read())
  88.   end
  89.   return result
  90. end
  91.  
  92. local function getConfig()
  93.   -- 进行【方向配置】。如果存在配置文件就读取内容,提示是否使用;如果没有配置文件或不用已存在的配置文件,就重新设置。
  94.   local cfg = getTable("/home/reactor.cfg")
  95.   shell.execute("resolution 50 16")
  96.   term.clear()
  97.   if cfg then
  98.     print("Current configuration:")
  99.     print("------------------------------------------")
  100.     for key,s in pairs(cfg) do
  101.       print(key..": "..s)
  102.     end
  103.     print("------------------------------------------")
  104.     print("0:down 1:top 2:north 3:south 4:west 5:east")
  105.     print("------------------------------------------")
  106.     print("Do you want to use this config? [Y/n]")
  107.     if io.read()=="n" then cfg=nil end
  108.   end
  109.   if not cfg then
  110.     term.clear()
  111.     cfg={}
  112.     print("Please config the sides:")
  113.     print("------------------------------------------")
  114.     print("0:down 1:top 2:north 3:south 4:west 5:east")
  115.     print("------------------------------------------")
  116.     -- 红石信号发射方向(相对红石方块)
  117.     term.write("which side does [REDSTONE] Output? ")
  118.     cfg["redstone"]=getKey()
  119.     -- 反应堆的方向(相对换位器)
  120.     term.write("which side does REACTOR towards [TRANSPOSER]? ")
  121.     cfg["reactor"]=getKey()
  122.     -- 燃料箱子的方向(相对换位器)
  123.     term.write("which side does FuelBox towards [TRANSPOSER]? ")
  124.     cfg["fuelbox"]=getKey()
  125.     -- 废料箱子的方向(相对换位器)
  126.     term.write("which side does WasteBox towards [TRANSPOSER]? ")
  127.     cfg["wastebox"]=getKey()
  128.     -- 过热温度
  129.     term.write("what is overheat temperature?(0.0001~1) ")
  130.     cfg["overheat"]=getKey()
  131.     putTable(cfg,"/home/reactor.cfg")
  132.   end
  133.   shell.execute("resolution 32 6")
  134.   term.clear()
  135.   return cfg
  136. end
  137.  
  138. local function keyDown(t)
  139.   local result
  140.   if t then
  141.     _,_,result=event.pull(t,"key_down")
  142.   else
  143.     _,_,result=event.pull("key_down")
  144.   end
  145.   if not result then result=0 end
  146.   return result
  147. end
  148.  
  149. local timeStamp, energyStamp,energyRatio = 0,0,0
  150. if battery then
  151.   timeStamp=os.clock()
  152.   energyStamp=math.floor(battery.getEnergy()/10)
  153. end
  154. ---------------------程序开始-----------------
  155. local w1, h1 = tty.gpu().getViewport()
  156. if rs and reactor and tra then
  157.   local cfg=getConfig()
  158.   -- work start
  159.   local w, h = tty.gpu().getViewport()
  160.   local command="s"
  161.   while true do
  162.     local heat=reactor.getHeat()
  163.     local heatMax=reactor.getMaxHeat()
  164.     local running=reactor.producesEnergy()
  165.     term.setCursor(1,1)
  166.     term.write(paddingLeft("DATE/TIME:    "..os.date()))
  167.     term.setCursor(1,2)
  168.     term.write(paddingLeft("Heat:         "..heat.."  / "..heatMax))
  169.     term.setCursor(1,3)
  170.     term.write(paddingLeft(string.rep("#",math.floor( w * heat / heatMax )),"."))
  171.     term.setCursor(1,4)
  172.     term.write(paddingLeft(" "))
  173.     term.setCursor(1,5)
  174.     if running then
  175.       term.write(paddingMid("<<< RUNNING >>>"))
  176.     else
  177.       term.write(paddingMid("<<< STOP >>>"))
  178.     end
  179.     term.setCursor(1,6)
  180.     term.write("[Run] [Stop] [eXit] [Config] "..command)
  181.     if heat/heatMax>cfg["overheat"] then -- 过热则停机。
  182.       if running then rs.setOutput(cfg["redstone"],0) end
  183.     else
  184.       -- 检查是否有燃料棒需要更换
  185.       local item_in_reactor=tra.getAllStacks(cfg["reactor"]).getAll()
  186.       local ready=true
  187.       for i=0,#item_in_reactor-4 do
  188.         if item_in_reactor[i] and items[item_in_reactor[i].name] then -- 如果当前反应堆位置有物品,且该物品在items中有记录
  189.           -- 判断是否 low damage。判断标准:无 damage 或当前物品的剩余耐久小于 damage
  190.           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))
  191.           ready=(ready and (not lowDamage)) -- 只要存在低耐久物品,本轮就不要开机了
  192.           if lowDamage then
  193.             if running then
  194.               rs.setOutput(cfg["redstone"],0)
  195.               running=false
  196.             end
  197.             if items[item_in_reactor[i].name].item then  -- 需要替换
  198.               tra.transferItem(cfg["reactor"],cfg["wastebox"],1,i+1)
  199.               -- 从燃料箱子里查找替换品
  200.               local boxLocation=0
  201.               while boxLocation==0 do
  202.                 local item_in_box=tra.getAllStacks(cfg["fuelbox"]).getAll()
  203.                 local k=0
  204.                 while (boxLocation==0) and (k<=#item_in_box) do
  205.                   if item_in_box[k] and (item_in_box[k].name==items[item_in_reactor[i].name].item) then
  206.                     boxLocation=k+1
  207.                     break
  208.                   end
  209.                   k=k+1
  210.                 end
  211.                 -- 找到了就换,找不到就等
  212.                 if (boxLocation>0) and tra.transferItem(cfg["fuelbox"],cfg["reactor"],1,boxLocation,i+1) then
  213.                   break
  214.                 else
  215.                   term.setCursor(1,4)
  216.                   term.write(paddingMid("Fuel shortage!"))
  217.                   os.sleep(1)
  218.                 end
  219.               end
  220.             end
  221.           end
  222.         end
  223.       end
  224.       -- command=r且停机且ready,才可以开机
  225.       if (command=="r") and (not running) and ready then rs.setOutput(cfg["redstone"],15) end
  226.     end
  227.     local key=keyDown(0.2)
  228.     if key==115 then command="s"; rs.setOutput(cfg["redstone"],0); -- s=Stop
  229.     elseif key==114 then command="r" -- r=Run
  230.     elseif key==120 then rs.setOutput(cfg["redstone"],0); break; -- x=eXit
  231.     elseif key==99 then -- c=Config, 先停机再配置
  232.       rs.setOutput(cfg["redstone"],0)
  233.       cfg=getConfig()
  234.     end
  235.   end
  236.   shell.execute("resolution "..w1.." "..h1)
  237. else
  238.   -- 组件不完整,退出
  239.   print("Please check components: REDSTONE, TRANSPOSER, REACTOR")
  240. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement