Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component=require("component")
- local event=require("event")
- local filesystem = require("filesystem")
- local serialization = require("serialization")
- local shell=require("shell")
- local term=require("term")
- local tty=require("tty")
- local items={
- ["IC2:reactorUraniumQuaddepleted"]={item="IC2:reactorUraniumQuad"},
- ["IC2:reactorMOXQuaddepleted"]={item="IC2:reactorMOXQuad"},
- ["IC2:reactorUraniumDualdepleted"]={item="IC2:reactorUraniumDual"},
- ["IC2:reactorMOXDualdepleted"]={item="IC2:reactorMOXDual"},
- ["IC2:reactorUraniumSimpledepleted"]={item="IC2:reactorUraniumSimple"},
- ["IC2:reactorMOXSimpledepleted"]={item="IC2:reactorMOXSimple"},
- ["IC2:reactorReflector"]={damage=5,item="IC2:reactorReflector"},
- ["IC2:reactorReflectorThick"]={damage=5,item="IC2:reactorReflectorThick"},
- ["IC2:reactorCondensator"]={damage=5,item="IC2:reactorCondensator"},
- ["IC2:lzh_condensator"]={damage=15000,item="IC2:lzh_condensator"},
- ["IC2:reactorCoolantSix"]={damage=100,item="IC2:reactorCoolantSix"},
- ["IC2:reactorCoolantTriple"]={damage=200,item="IC2:reactorCoolantTriple"},
- ["IC2:reactorCoolantSimple"]={damage=500,item="IC2:reactorCoolantSimple"},
- ["IC2:reactorVent"]={damage=8000},
- ["IC2:reactorVentCore"]={damage=8000},
- ["IC2:reactorVentGold"]={damage=8000},
- ["IC2:reactorVentDiamond"]={damage=8000},
- ["IC2:reactorHeatSwitch"]={damage=8000},
- ["IC2:reactorHeatSwitchCore"]={damage=8000},
- ["IC2:reactorHeatSwitchSpread"]={damage=8000},
- ["IC2:reactorHeatSwitchDiamond"]={damage=8000},
- }
- local function getCom(t)
- if component.list(t) then
- return component.proxy(component.list(t)())
- else
- return nil
- end
- end
- local rs=getCom("redstone")
- local reactor=getCom("reactor")
- local tra=getCom("trans")
- function putTable(t,f)
- if type(t)=="table" then
- local file = filesystem.open(f,"w")
- file:write(serialization.serialize(t))
- file:close()
- return true
- else
- return false
- end
- end
- function getTable(f)
- if filesystem.exists(f) then
- local file = io.open(f,"r")
- local fileSize = filesystem.size(f)
- fileRead = file:read(fileSize)
- local fileContent = fileRead
- file:close()
- return serialization.unserialize(fileContent)
- else
- return false
- end
- end
- local function paddingMid(s,t)
- local tt=t
- if not tt then tt=" " end
- local w, h = tty.gpu().getViewport()
- local nLeft=math.floor((w-string.len(s))/2)
- return string.rep(tt,nLeft)..s..string.rep(tt,w-nLeft-string.len(s))
- end
- local function paddingLeft(s,t)
- local tt=t
- if not tt then tt=" " end
- local w, h = tty.gpu().getViewport()
- return s..string.rep(tt,w-string.len(s))
- end
- local function getKey()
- local result=-1
- while (not result) or (result<0) or (result>5) do
- result=tonumber(io.read())
- end
- return result
- end
- local function getConfig()
- -- 进行【方向配置】。如果存在配置文件就读取内容,提示是否使用;如果没有配置文件或不用已存在的配置文件,就重新设置。
- local cfg = getTable("/home/reactor.cfg")
- shell.execute("resolution 50 16")
- term.clear()
- if cfg then
- print("Current configuration:")
- print("------------------------------------------")
- for key,s in pairs(cfg) do
- print(key..": "..s)
- end
- print("------------------------------------------")
- print("0:down 1:top 2:north 3:south 4:west 5:east")
- print("------------------------------------------")
- print("Do you want to use this config? [Y/n]")
- if io.read()=="n" then cfg=nil end
- end
- if not cfg then
- term.clear()
- cfg={}
- print("Please config the sides:")
- print("------------------------------------------")
- print("0:down 1:top 2:north 3:south 4:west 5:east")
- print("------------------------------------------")
- -- 红石信号发射方向(相对红石方块)
- term.write("which side does [REDSTONE] Output? ")
- cfg["redstone"]=getKey()
- -- 反应堆的方向(相对换位器)
- term.write("which side does REACTOR towards [TRANSPOSER]? ")
- cfg["reactor"]=getKey()
- -- 燃料箱子的方向(相对换位器)
- term.write("which side does FuelBox towards [TRANSPOSER]? ")
- cfg["fuelbox"]=getKey()
- -- 废料箱子的方向(相对换位器)
- term.write("which side does WasteBox towards [TRANSPOSER]? ")
- cfg["wastebox"]=getKey()
- -- 过热温度
- term.write("what is overheat temperature?(0.0001~1) ")
- cfg["overheat"]=getKey()
- putTable(cfg,"/home/reactor.cfg")
- end
- shell.execute("resolution 32 6")
- term.clear()
- return cfg
- end
- local function keyDown(t)
- local result
- if t then
- _,_,result=event.pull(t,"key_down")
- else
- _,_,result=event.pull("key_down")
- end
- if not result then result=0 end
- return result
- end
- local timeStamp, energyStamp,energyRatio = 0,0,0
- if battery then
- timeStamp=os.clock()
- energyStamp=math.floor(battery.getEnergy()/10)
- end
- ---------------------程序开始-----------------
- local w1, h1 = tty.gpu().getViewport()
- if rs and reactor and tra then
- local cfg=getConfig()
- -- work start
- local w, h = tty.gpu().getViewport()
- local command="s"
- while true do
- local heat=reactor.getHeat()
- local heatMax=reactor.getMaxHeat()
- local running=reactor.producesEnergy()
- term.setCursor(1,1)
- term.write(paddingLeft("DATE/TIME: "..os.date()))
- term.setCursor(1,2)
- term.write(paddingLeft("Heat: "..heat.." / "..heatMax))
- term.setCursor(1,3)
- term.write(paddingLeft(string.rep("#",math.floor( w * heat / heatMax )),"."))
- term.setCursor(1,4)
- term.write(paddingLeft(" "))
- term.setCursor(1,5)
- if running then
- term.write(paddingMid("<<< RUNNING >>>"))
- else
- term.write(paddingMid("<<< STOP >>>"))
- end
- term.setCursor(1,6)
- term.write("[Run] [Stop] [eXit] [Config] "..command)
- if heat/heatMax>cfg["overheat"] then -- 过热则停机。
- if running then rs.setOutput(cfg["redstone"],0) end
- else
- -- 检查是否有燃料棒需要更换
- local item_in_reactor=tra.getAllStacks(cfg["reactor"]).getAll()
- local ready=true
- for i=0,#item_in_reactor-4 do
- if item_in_reactor[i] and items[item_in_reactor[i].name] then -- 如果当前反应堆位置有物品,且该物品在items中有记录
- -- 判断是否 low damage。判断标准:无 damage 或当前物品的剩余耐久小于 damage
- 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))
- ready=(ready and (not lowDamage)) -- 只要存在低耐久物品,本轮就不要开机了
- if lowDamage then
- if running then
- rs.setOutput(cfg["redstone"],0)
- running=false
- end
- if items[item_in_reactor[i].name].item then -- 需要替换
- tra.transferItem(cfg["reactor"],cfg["wastebox"],1,i+1)
- -- 从燃料箱子里查找替换品
- local boxLocation=0
- while boxLocation==0 do
- local item_in_box=tra.getAllStacks(cfg["fuelbox"]).getAll()
- local k=0
- while (boxLocation==0) and (k<=#item_in_box) do
- if item_in_box[k] and (item_in_box[k].name==items[item_in_reactor[i].name].item) then
- boxLocation=k+1
- break
- end
- k=k+1
- end
- -- 找到了就换,找不到就等
- if (boxLocation>0) and tra.transferItem(cfg["fuelbox"],cfg["reactor"],1,boxLocation,i+1) then
- break
- else
- term.setCursor(1,4)
- term.write(paddingMid("Fuel shortage!"))
- os.sleep(1)
- end
- end
- end
- end
- end
- end
- -- command=r且停机且ready,才可以开机
- if (command=="r") and (not running) and ready then rs.setOutput(cfg["redstone"],15) end
- end
- local key=keyDown(0.2)
- if key==115 then command="s"; rs.setOutput(cfg["redstone"],0); -- s=Stop
- elseif key==114 then command="r" -- r=Run
- elseif key==120 then rs.setOutput(cfg["redstone"],0); break; -- x=eXit
- elseif key==99 then -- c=Config, 先停机再配置
- rs.setOutput(cfg["redstone"],0)
- cfg=getConfig()
- end
- end
- shell.execute("resolution "..w1.." "..h1)
- else
- -- 组件不完整,退出
- print("Please check components: REDSTONE, TRANSPOSER, REACTOR")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement