-- 不倒下: 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 lines0 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.current0 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()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].count0 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