279 lines
15 KiB
Lua
279 lines
15 KiB
Lua
-- 不倒下: the units of a team switched on keep their health. Each one gets a native Health_Min floor at
|
||
-- the HP it had when it was locked, written the way the free camera locks the player's HP
|
||
-- (observer_guard.lua, v0.28.2), so the unit still takes hits, loses Sha-Chi and gets broken but never
|
||
-- goes down. Should its HP still drop below the floor, it is filled back and the breach is counted.
|
||
-- Turning the team off puts each unit's own floor back; a unit that leaves the arena is forgotten.
|
||
-- Locked units and their original floors are kept in Runtime/immortal-floors.tsv, so after a mod reload
|
||
-- the next Lua instance can put back the floors of the units the old ledger left standing.
|
||
local U=require('ue_observe')
|
||
local Stats=require('character_stats')
|
||
local Immortal={}
|
||
Immortal.teams={'blue','red','companion'}
|
||
local LABEL={blue='蓝方',red='红方',companion='随从'}
|
||
local SETTINGS='ImmortalTeamsV1'
|
||
local JOURNAL='ImmortalFloorsV1'
|
||
local EPS=0.5 -- HP: a floor or health this close to the target counts as the target
|
||
local RETRIES=3 -- lock attempts per unit before it counts as a failure
|
||
local LOG_CAP=40 -- repeated lines (relocks, health below the floor) per Lua lifetime
|
||
local RESTORE_RETRY=1
|
||
|
||
local function short(actor)
|
||
if not U.valid(actor) then return '?' end
|
||
local cls=U.className(actor)
|
||
return cls and (cls:gsub('_C$','')) or '?'
|
||
end
|
||
local function fmt(v)return v and string.format('%.1f',v) or 'nil' end
|
||
|
||
function Immortal.new(options)
|
||
local log=options.log or function()end
|
||
local fs=options.io or io
|
||
local stats=options.attributes or Stats.new()
|
||
local clock=options.clock or os.clock
|
||
local settings=options.path..'/immortal-teams.tsv'
|
||
local journal=options.path..'/immortal-floors.tsv'
|
||
local self={alive=true,on={blue=false,red=false,companion=false},failures=0,relocks=0,breaches=0,message=''}
|
||
local locked={} -- actor key -> {team, original floor base, target HP, unit name}
|
||
local tries={} -- actor key -> {identity, count} for lock attempts that did not read back
|
||
local recovering={} -- matched reload records whose original floor is not restored yet
|
||
local release
|
||
local lines=0
|
||
local orphansChecked=false
|
||
local function say(m)log('[Immortal] '..m)end
|
||
local function again(m)if lines<LOG_CAP then lines=lines+1;say(m)end end
|
||
local function identity(actor)
|
||
if not U.valid(actor) then return nil end
|
||
local ok,value=pcall(function()return actor:GetFName():ToString()end)
|
||
return ok and type(value)=='string' and value or nil
|
||
end
|
||
local function gone(actor)
|
||
if not U.valid(actor) then return true end
|
||
return options.isDestroying and options.isDestroying(actor)==true or false
|
||
end
|
||
local function readFile(path)
|
||
local f=fs.open(path,'r');if not f then return nil end
|
||
local text=f:read('*a');f:close();return text
|
||
end
|
||
local function writeFile(path,text)
|
||
local f=fs.open(path,'w');if not f then return false end
|
||
local wrote=f:write(text);local closed=f:close()
|
||
return wrote~=nil and wrote~=false and closed==true and readFile(path)==text
|
||
end
|
||
do
|
||
local text=readFile(settings)
|
||
if type(text)=='string' and text:sub(1,#SETTINGS+1)==SETTINGS..'\t' then
|
||
for team,value in text:gmatch('(%l+)=([01])')do
|
||
if self.on[team]~=nil then self.on[team]=value=='1' end
|
||
end
|
||
end
|
||
end
|
||
local function save()
|
||
local parts={SETTINGS}
|
||
for _,team in ipairs(Immortal.teams)do parts[#parts+1]=team..'='..(self.on[team] and '1' or '0') end
|
||
return writeFile(settings,table.concat(parts,'\t')..'\n')
|
||
end
|
||
local function saveJournal()
|
||
local records={}
|
||
for key,record in pairs(recovering)do records[key]=record end
|
||
for key,record in pairs(locked)do records[key]=record end
|
||
local keys={}
|
||
for key in pairs(records)do keys[#keys+1]=key end
|
||
table.sort(keys)
|
||
local rows={JOURNAL}
|
||
for _,key in ipairs(keys)do
|
||
local r=records[key]
|
||
rows[#rows+1]=key..'\t'..string.format('%.3f',r.original)..'\t'..string.format('%.3f',r.target)
|
||
end
|
||
return writeFile(journal,table.concat(rows,'\n')..'\n')
|
||
end
|
||
|
||
-- After a mod reload the old ledger is gone but its units may still stand. Put back the floors the
|
||
-- journal says were raised, only on units that still carry exactly that raised floor.
|
||
local function restoreOrphans()
|
||
local text=readFile(journal)
|
||
if type(text)~='string' or text=='' then return end
|
||
local rows,count,rejected={},0,0
|
||
for line in text:gmatch('[^\r\n]+')do
|
||
if line~=JOURNAL then
|
||
local key,original,target=line:match('^(%S+)\t(%S+)\t(%S+)$')
|
||
original,target=tonumber(original),tonumber(target)
|
||
if key and Stats.finite(original) and Stats.finite(target) then
|
||
rows[key]={original=original,target=target};count=count+1
|
||
else rejected=rejected+1 end
|
||
end
|
||
end
|
||
if rejected>0 then say('读取恢复记录 跳过非法行='..rejected..' 有效记录读回='..count) end
|
||
if count>0 then
|
||
local restored=0
|
||
for _,actor in ipairs(options.collect and options.collect() or {})do
|
||
local key=U.key(actor)
|
||
local row=key and rows[key]
|
||
if row then
|
||
rows[key]=nil
|
||
row.actor,row.identity,row.name=actor,identity(actor),short(actor)
|
||
local done,verified=release(row,actor,'恢复上次留下的下限')
|
||
if verified then restored=restored+1 end
|
||
if not done then recovering[key]=row end
|
||
end
|
||
end
|
||
say(string.format('检查上次留下的不倒下单位:记录%d只,恢复%d只',count,restored))
|
||
end
|
||
if next(recovering) then saveJournal() else writeFile(journal,'') end
|
||
end
|
||
|
||
local function failedLock(actor,key,owner)
|
||
local attempt=tries[key]
|
||
if not attempt or attempt.identity~=owner then attempt={identity=owner,count=0};tries[key]=attempt end
|
||
attempt.count=attempt.count+1
|
||
if attempt.count>=RETRIES then
|
||
self.failures=self.failures+1;self.message=short(actor)..' 没能锁住血量'
|
||
say('放弃锁定 unit='..short(actor)..':'..RETRIES..' 次写入都没有读回')
|
||
end
|
||
end
|
||
local function lock(actor,team,key)
|
||
local owner=identity(actor)
|
||
if not owner then return false end
|
||
local health=stats:read(actor,'Health');local floor=stats:read(actor,'Health_Min')
|
||
-- Not readable yet (just spawned) or already down: nothing to lock this tick.
|
||
if not health or not floor or health.current<=0 then return false end
|
||
local target=health.current
|
||
local record={team=team,original=floor.base,target=target,name=short(actor),actor=actor,identity=owner}
|
||
locked[key]=record
|
||
local ok,err=stats:writeBase(actor,'Health_Min',target)
|
||
local after=stats:read(actor,'Health_Min')
|
||
local good=ok and after and after.current>=target-EPS
|
||
say(string.format('锁定 %s unit=%s HP=%s 原下限=%s 写入=%s 读回=%s %s',LABEL[team],short(actor),fmt(target),fmt(floor.base),
|
||
tostring(ok),after and fmt(after.current) or 'nil',err and ('错误='..tostring(err)) or ''))
|
||
if good then
|
||
return true,true
|
||
end
|
||
-- Not held: put the original floor back and try again on a later tick, a few times at most.
|
||
if release(record,actor,'锁定未确认') then locked[key]=nil end
|
||
failedLock(actor,key,owner)
|
||
return true
|
||
end
|
||
local function hold(record,actor)
|
||
-- Health at the lock means the floor holds: one read, not two full attribute reads per unit.
|
||
if options.hp then
|
||
local ok,hp=pcall(options.hp,actor)
|
||
if ok and type(hp)=='number' and hp>=record.target-EPS then return end
|
||
end
|
||
local floor=stats:read(actor,'Health_Min');local health=stats:read(actor,'Health')
|
||
if floor and floor.current<record.target-EPS then
|
||
local ok=stats:writeBase(actor,'Health_Min',record.target)
|
||
local after=stats:read(actor,'Health_Min')
|
||
self.relocks=self.relocks+1
|
||
again(string.format('重新锁定 unit=%s 下限 %s→%s 写入=%s 读回=%s',record.name,fmt(floor.current),fmt(record.target),
|
||
tostring(ok),after and fmt(after.current) or 'nil'))
|
||
end
|
||
if health and health.current<record.target-EPS then
|
||
-- The floor did not stop this damage. Fill the unit back while it still stands.
|
||
self.breaches=self.breaches+1
|
||
local filled=health.current>0 and stats:writeBase(actor,'Health',record.target)
|
||
local after=stats:read(actor,'Health')
|
||
again(string.format('血量低于下限 unit=%s HP=%s 下限=%s 目标=%s 补回=%s 读回=%s',record.name,fmt(health.current),
|
||
floor and fmt(floor.current) or 'nil',fmt(record.target),tostring(filled),after and fmt(after.current) or 'nil'))
|
||
end
|
||
end
|
||
release=function(record,actor,reason)
|
||
if gone(actor) then return true,false end
|
||
if record.retryAt and clock()<record.retryAt then return false,false end
|
||
local owner=identity(actor)
|
||
if owner and record.identity and owner~=record.identity then return true,false end
|
||
local before=owner and stats:read(actor,'Health_Min')
|
||
if before and math.abs(before.base-record.original)<=EPS then return true,true end
|
||
if before and math.abs(before.base-record.target)>EPS then
|
||
again('原下限已被其他机制改变,不覆盖 unit='..record.name..' 读回='..fmt(before.base))
|
||
return true,false
|
||
end
|
||
local ok,err=false,'对象身份或属性未读回'
|
||
if before then ok,err=stats:writeBase(actor,'Health_Min',record.original) end
|
||
if gone(actor) then return true,false end
|
||
local after=before and stats:read(actor,'Health_Min')
|
||
local verified=ok and after and math.abs(after.base-record.original)<=EPS
|
||
if not verified then
|
||
record.pendingRestore=true;record.retryAt=clock()+RESTORE_RETRY
|
||
if not record.restoreFailed then
|
||
record.restoreFailed=true;self.failures=self.failures+1
|
||
again(string.format('恢复待重试(%s)unit=%s 目标=%s 写入=%s 读回=%s',reason,record.name,fmt(record.original),tostring(ok),after and fmt(after.base) or 'nil'))
|
||
end
|
||
return false,false
|
||
end
|
||
say(string.format('恢复(%s)unit=%s 下限→%s 写入=true 读回=%s',reason,record.name,fmt(record.original),fmt(after.base)))
|
||
return true,true
|
||
end
|
||
|
||
-- Saved and read back here; the units are locked or released on the next tick.
|
||
function self:set(team,value)
|
||
team=type(team)=='string' and team:lower() or ''
|
||
if self.on[team]==nil then return {ok=false,message='不倒下只支持蓝方、红方和随从',spawned_count=0} end
|
||
if value~='ON' and value~='OFF' then return {ok=false,message='不倒下开关只接受 ON 或 OFF',spawned_count=0} end
|
||
local before=self.on[team]
|
||
self.on[team]=value=='ON'
|
||
local saved=save()
|
||
if not saved then self.on[team]=before end
|
||
say(string.format('设置 %s=%s 保存读回=%s',team,value,tostring(saved)))
|
||
return {ok=saved,spawned_count=0,message=saved and (LABEL[team]..'不倒下'..(self.on[team] and '已开启' or '已关闭')) or '不倒下设置没能保存'}
|
||
end
|
||
-- Every support tick (0.5 s): lock new units of a switched-on team, keep the locked ones, release the
|
||
-- ones whose team was switched off and forget the ones that left.
|
||
function self:tick()
|
||
if not self.alive or (options.ready and not options.ready()) then return end
|
||
if not orphansChecked then orphansChecked=true;restoreOrphans() end
|
||
local seen,changed,newLocks={},false,{}
|
||
for key,record in pairs(recovering)do
|
||
if release(record,record.actor,'重试上次恢复') then recovering[key]=nil;changed=true end
|
||
end
|
||
for _,member in ipairs(options.members())do
|
||
local actor=member.actor
|
||
local key=U.key(actor)
|
||
if key and not gone(actor) then
|
||
seen[key]=true
|
||
local record=locked[key]
|
||
local owner=(record or tries[key]) and identity(actor)
|
||
if tries[key] and owner and tries[key].identity~=owner then tries[key]=nil end
|
||
if record and owner and record.identity~=owner then locked[key]=nil;record=nil;tries[key]=nil;changed=true end
|
||
if record and (record.pendingRestore or not self.on[member.team]) then
|
||
if release(record,actor,'已关闭不倒下') then locked[key]=nil;record=nil;changed=true end
|
||
elseif record and owner then hold(record,actor) end
|
||
if not record and not recovering[key] and self.on[member.team] and (not tries[key] or tries[key].count<RETRIES) then
|
||
local touched,held=lock(actor,member.team,key)
|
||
changed=touched or changed
|
||
if held then newLocks[key]=locked[key] end
|
||
end
|
||
end
|
||
end
|
||
for key,record in pairs(locked)do
|
||
if not seen[key] then locked[key]=nil;tries[key]=nil;changed=true;say('单位已离场,不再跟踪 unit='..record.name) end
|
||
end
|
||
for key in pairs(tries)do
|
||
if not seen[key] then tries[key]=nil;again('单位已离场,清除失败重试次数 读回=不在当前名单') end
|
||
end
|
||
if changed then
|
||
local saved=saveJournal()
|
||
if saved then
|
||
for key in pairs(newLocks)do tries[key]=nil end
|
||
else
|
||
-- Keep one journal write for the whole batch. A batch that cannot save its originals
|
||
-- must release its new locks; failed releases stay in the ledger for later retries.
|
||
for key,record in pairs(newLocks)do
|
||
if release(record,record.actor,'原值记录保存未确认') then locked[key]=nil end
|
||
failedLock(record.actor,key,record.identity)
|
||
end
|
||
again('生命下限记录保存未读回,本次新加锁已请求恢复')
|
||
end
|
||
end
|
||
end
|
||
function self:status()
|
||
local n,pending=0,0
|
||
for _,record in pairs(locked)do n=n+1;if record.pendingRestore then pending=pending+1 end end
|
||
for _ in pairs(recovering)do pending=pending+1 end
|
||
return {blue=self.on.blue,red=self.on.red,companion=self.on.companion,locked=n,failures=self.failures,
|
||
breaches=self.breaches,relocks=self.relocks,pending_restore=pending,
|
||
message=pending>0 and ('还有'..pending..'个单位生命下限等待恢复') or self.message}
|
||
end
|
||
-- On unload nothing is written to the game; the journal lets the next instance put the floors back.
|
||
function self:silence()self.alive=false end
|
||
return self
|
||
end
|
||
return Immortal
|