313 lines
15 KiB
Lua
313 lines
15 KiB
Lua
package.path='Mods/PhantomGladiator/Scripts/?.lua;'..package.path
|
|
local Immortal=require('immortal_teams')
|
|
local address=0
|
|
local function actor(name)
|
|
address=address+1;local a=address
|
|
return {IsValid=function()return true end,GetAddress=function()return a end,
|
|
GetFName=function()return {ToString=function()return name..'_'..a end}end,
|
|
GetClass=function()return {IsValid=function()return true end,GetFName=function()return {ToString=function()return name..'_C' end}end}end}
|
|
end
|
|
local function harness(files)
|
|
local h={files=files or {},lines={},writes={},attrs={},members={},ready=true,now=0,journalWrites=0}
|
|
h.io={open=function(path,mode)
|
|
if mode=='w' and (h.failSave or h.failPath==path) then return nil end
|
|
if mode=='r' and h.files[path]==nil then return nil end
|
|
local text=h.files[path] or ''
|
|
return {read=function()return text end,write=function(_,s)text=s;return true end,
|
|
close=function()
|
|
if mode=='w' then
|
|
h.files[path]=text
|
|
if path=='R/immortal-floors.tsv' then h.journalWrites=h.journalWrites+1 end
|
|
end
|
|
return true
|
|
end}
|
|
end}
|
|
-- Per-actor attributes; a stuck field ignores writes, like a floor the game holds elsewhere.
|
|
function h.unit(name,hp,floor)
|
|
local a=actor(name)
|
|
h.attrs[a]={Health={base=hp,current=hp},Health_Min={base=floor or 0,current=floor or 0}}
|
|
return a
|
|
end
|
|
h.stats={read=function(_,a,field)local v=h.attrs[a] and h.attrs[a][field];return v and {base=v.base,current=v.current} end,
|
|
writeBase=function(_,a,field,value)
|
|
h.writes[#h.writes+1]=field..'='..value
|
|
if h.stuck==field then return true end
|
|
h.attrs[a][field]={base=value,current=value};return true
|
|
end}
|
|
function h.new()
|
|
return Immortal.new({path='R',io=h.io,attributes=h.stats,log=function(s)h.lines[#h.lines+1]=s end,
|
|
clock=function()return h.now end,isDestroying=function(actor)return actor.destroying==true end,
|
|
members=function()return h.members end,collect=function()return h.npcs or {} end,ready=function()return h.ready end})
|
|
end
|
|
function h.all()return table.concat(h.lines,'\n')end
|
|
return h
|
|
end
|
|
|
|
do
|
|
local h=harness();local im=h.new();im:set('blue','ON')
|
|
local old=h.unit('Old',100);h.members={{actor=old,team='blue'}};h.stuck='Health_Min'
|
|
for _=1,4 do im:tick()end
|
|
assert(im:status().locked==0,'fixture must exhaust failed attempts')
|
|
h.members={};im:tick()
|
|
local fresh=h.unit('New',200);fresh.GetAddress=old.GetAddress
|
|
h.stuck=nil;h.members={{actor=fresh,team='blue'}};im:tick()
|
|
assert(h.attrs[fresh].Health_Min.current==200,'new unit inherited exhausted lock attempts')
|
|
print('ok - failed immortal attempts are pruned before an address is reused')
|
|
end
|
|
do
|
|
local h=harness();local im=h.new();im:set('blue','ON')
|
|
local old=h.unit('ExhaustedOwner',100);h.members={{actor=old,team='blue'}};h.stuck='Health_Min'
|
|
for _=1,4 do im:tick()end
|
|
local fresh=h.unit('ReplacementOwner',300);fresh.GetAddress=old.GetAddress
|
|
h.stuck=nil;h.members={{actor=fresh,team='blue'}};im:tick()
|
|
assert(h.attrs[fresh].Health_Min.current==300,'a replacement observed without an empty roster tick must not inherit exhausted attempts')
|
|
print('ok - failed immortal attempt budgets follow identity even without an empty roster tick')
|
|
end
|
|
|
|
do
|
|
local h=harness()
|
|
local bad=h.unit('BadRow',100,100);local good=h.unit('GoodRow',200,200)
|
|
h.npcs={bad,good}
|
|
h.files['R/immortal-floors.tsv']='ImmortalFloorsV1\n'..bad.GetAddress()..'\t0\t..\n'
|
|
..good.GetAddress()..'\t10\t200\nfuture\t-\t1\n'
|
|
local im=h.new();im:tick()
|
|
assert(h.attrs[bad].Health_Min.base==100,'invalid recovery rows must not write attributes')
|
|
assert(h.attrs[good].Health_Min.base==10,'valid rows must survive a malformed neighbour')
|
|
assert(#h.lines<8,'bad file diagnostics must stay bounded')
|
|
print('ok - malformed immortal recovery numbers are ignored without losing valid rows')
|
|
end
|
|
|
|
-- Settings: off by default, saved with readback, a failed save keeps the old value, bad input refused.
|
|
do
|
|
local h=harness()
|
|
local im=h.new()
|
|
local s=im:status()
|
|
assert(not s.blue and not s.red and not s.companion and s.locked==0)
|
|
local r=im:set('blue','ON')
|
|
assert(r.ok and r.message=='蓝方不倒下已开启' and im:status().blue)
|
|
assert(h.files['R/immortal-teams.tsv']=='ImmortalTeamsV1\tblue=1\tred=0\tcompanion=0\n',h.files['R/immortal-teams.tsv'])
|
|
assert(not im:set('neutral','ON').ok,'no-team units are not offered')
|
|
assert(not im:set('red','YES').ok)
|
|
h.failSave=true
|
|
assert(not im:set('red','ON').ok and not im:status().red,'a failed save keeps the old value')
|
|
h.failSave=false
|
|
assert(h.new():status().blue,'a new Lua instance reads the saved switch')
|
|
end
|
|
|
|
-- Locking: a unit of a switched-on team gets a floor at its HP, read back and journaled; the other
|
|
-- team and no-team units are left alone; a dead or unreadable unit waits.
|
|
local h=harness()
|
|
local im=h.new()
|
|
local mob=h.unit('BP_Monster_BaiheDisciple_Knife',640)
|
|
local boss=h.unit('BP_Monster_AHua_GuiTouDao',104652)
|
|
local solo=h.unit('BP_Monster_MoTian',12000)
|
|
local fallen=h.unit('BP_Monster_MaleThug',0)
|
|
h.members={{actor=mob,team='blue'},{actor=boss,team='red'},{actor=solo,team='neutral'},{actor=fallen,team='blue'}}
|
|
assert(im:set('blue','ON').ok)
|
|
im:tick()
|
|
assert(h.attrs[mob].Health_Min.base==640 and h.attrs[boss].Health_Min.base==0 and h.attrs[solo].Health_Min.base==0)
|
|
assert(h.attrs[fallen].Health_Min.base==0,'a fallen unit is not locked')
|
|
assert(im:status().locked==1,h.all())
|
|
assert(h.all():find('锁定 蓝方 unit=BP_Monster_BaiheDisciple_Knife HP=640.0 原下限=0.0 写入=true 读回=640.0',1,true),h.all())
|
|
local key=tostring(mob.GetAddress())
|
|
assert(h.files['R/immortal-floors.tsv']=='ImmortalFloorsV1\n'..key..'\t0.000\t640.000\n',h.files['R/immortal-floors.tsv'])
|
|
local writes=#h.writes
|
|
im:tick()
|
|
assert(#h.writes==writes,'a held floor is not written again')
|
|
|
|
-- Holding: a floor the game lowered is raised again; health that got below the floor is filled back.
|
|
h.attrs[mob].Health_Min={base=640,current=1}
|
|
im:tick()
|
|
assert(h.attrs[mob].Health_Min.current==640 and im:status().relocks==1,h.all())
|
|
h.attrs[mob].Health={base=300,current=300}
|
|
im:tick()
|
|
assert(h.attrs[mob].Health.current==640 and im:status().breaches==1,h.all())
|
|
assert(h.all():find('血量低于下限 unit=BP_Monster_BaiheDisciple_Knife HP=300.0',1,true))
|
|
|
|
-- Units joining later are locked on the next tick; a unit that leaves is forgotten.
|
|
local late=h.unit('BP_Monster_BaiheDisciple_FishRod',700)
|
|
h.members[#h.members+1]={actor=late,team='blue'}
|
|
im:tick()
|
|
assert(im:status().locked==2 and h.attrs[late].Health_Min.base==700)
|
|
table.remove(h.members)
|
|
im:tick()
|
|
assert(im:status().locked==1 and h.all():find('单位已离场,不再跟踪 unit=BP_Monster_BaiheDisciple_FishRod',1,true))
|
|
assert(not h.files['R/immortal-floors.tsv']:find(tostring(late.GetAddress())..'\t',1,true))
|
|
|
|
-- Turning the team off restores each unit's own floor.
|
|
assert(im:set('blue','OFF').ok)
|
|
im:tick()
|
|
assert(h.attrs[mob].Health_Min.base==0 and im:status().locked==0,h.all())
|
|
assert(h.files['R/immortal-floors.tsv']=='ImmortalFloorsV1\n')
|
|
assert(h.all():find('恢复(已关闭不倒下)unit=BP_Monster_BaiheDisciple_Knife 下限→0.0 写入=true 读回=0.0',1,true))
|
|
|
|
-- Companions can be locked too.
|
|
local follower=h.unit('BP_Monster_MoTian',12000)
|
|
h.members={{actor=follower,team='companion'}}
|
|
assert(im:set('companion','ON').ok)
|
|
im:tick()
|
|
assert(h.attrs[follower].Health_Min.base==12000 and im:status().locked==1)
|
|
|
|
-- A floor that never reads back: the original is put back after each try, three tries, one failure.
|
|
do
|
|
local s=harness()
|
|
local stuck=s.unit('BP_Monster_Vessel_Child',400)
|
|
s.members={{actor=stuck,team='red'}}
|
|
s.stuck='Health_Min'
|
|
local x=s.new()
|
|
assert(x:set('red','ON').ok)
|
|
for _=1,5 do x:tick() end
|
|
local tries=0
|
|
for _,w in ipairs(s.writes)do if w=='Health_Min=400' then tries=tries+1 end end
|
|
assert(tries==3 and x:status().failures==1 and x:status().locked==0,s.all())
|
|
assert(s.all():find('放弃锁定 unit=BP_Monster_Vessel_Child',1,true))
|
|
end
|
|
|
|
-- Nothing happens before the game is ready.
|
|
do
|
|
local s=harness()
|
|
local unit=s.unit('BP_Monster_MaleThug',500)
|
|
s.members={{actor=unit,team='blue'}}
|
|
s.ready=false
|
|
local x=s.new();x:set('blue','ON');x:tick()
|
|
assert(s.attrs[unit].Health_Min.base==0 and #s.writes==0)
|
|
end
|
|
|
|
-- After a reload: units the old journal names that still carry exactly the raised floor get their own
|
|
-- floor back; one whose floor changed since is left alone; the journal is emptied either way.
|
|
do
|
|
local s=harness()
|
|
local left=s.unit('BP_Monster_BaiheDisciple_Knife',640,640)
|
|
local changed=s.unit('BP_Monster_AHua_GuiTouDao',9000,5)
|
|
s.files['R/immortal-floors.tsv']='ImmortalFloorsV1\n'..tostring(left.GetAddress())..'\t0.000\t640.000\n'
|
|
..tostring(changed.GetAddress())..'\t0.000\t9000.000\n'..'999999\t0.000\t100.000\n'
|
|
s.npcs={left,changed}
|
|
local x=s.new()
|
|
x:tick()
|
|
assert(s.attrs[left].Health_Min.base==0 and s.attrs[changed].Health_Min.base==5,s.all())
|
|
assert(s.files['R/immortal-floors.tsv']=='')
|
|
assert(s.all():find('检查上次留下的不倒下单位:记录3只,恢复1只',1,true),s.all())
|
|
local count=#s.lines
|
|
x:tick()
|
|
assert(#s.lines==count,'the journal is checked once per Lua lifetime')
|
|
end
|
|
|
|
-- With a health reader, a locked unit at its lock costs one read per tick and no attribute reads; once
|
|
-- its health is below the lock the full check refills it.
|
|
do
|
|
local s=harness()
|
|
local unit=s.unit('BP_Monster_FemaleThug_TwinHoe',19767.6)
|
|
s.members={{actor=unit,team='blue'}}
|
|
local reads=0
|
|
local real=s.stats.read
|
|
s.stats.read=function(...)reads=reads+1;return real(...)end
|
|
local x=Immortal.new({path='R',io=s.io,attributes=s.stats,log=function(l)s.lines[#s.lines+1]=l end,
|
|
members=function()return s.members end,ready=function()return true end,
|
|
hp=function(actor)return s.attrs[actor].Health.current end})
|
|
x:set('blue','ON');x:tick()
|
|
local afterLock=reads
|
|
for _=1,5 do x:tick() end
|
|
assert(reads==afterLock,'five ticks at the lock: no attribute reads')
|
|
s.attrs[unit].Health={base=900,current=900}
|
|
x:tick()
|
|
assert(reads>afterLock and s.attrs[unit].Health.current==19767.6 and x:status().breaches==1,s.all())
|
|
end
|
|
|
|
-- Unloading writes nothing; the instance stops.
|
|
do
|
|
local s=harness()
|
|
local unit=s.unit('BP_Monster_MaleThug',500)
|
|
s.members={{actor=unit,team='blue'}}
|
|
local x=s.new();x:set('blue','ON');x:silence();x:tick()
|
|
assert(#s.writes==0)
|
|
end
|
|
do
|
|
local s=harness()
|
|
local unit=s.unit('ReleaseRetry',400)
|
|
s.members={{actor=unit,team='blue'}}
|
|
local x=s.new();assert(x:set('blue','ON').ok);x:tick()
|
|
s.stuck='Health_Min';assert(x:set('blue','OFF').ok);x:tick()
|
|
assert(s.attrs[unit].Health_Min.base==400)
|
|
assert(s.files['R/immortal-floors.tsv']:find('\t0.000\t400.000',1,true),'a failed unlock must keep its recovery journal')
|
|
s.stuck=nil;s.now=2;x:tick()
|
|
assert(s.attrs[unit].Health_Min.base==0 and x:status().locked==0,'a later tick must retry the failed unlock')
|
|
end
|
|
|
|
do
|
|
local s=harness()
|
|
local unit=s.unit('ReloadRetry',400,400)
|
|
s.npcs={unit}
|
|
s.files['R/immortal-floors.tsv']='ImmortalFloorsV1\n'..unit.GetAddress()..'\t0.000\t400.000\n'
|
|
s.stuck='Health_Min'
|
|
local x=s.new();x:tick()
|
|
assert(s.files['R/immortal-floors.tsv']:find('\t0.000\t400.000',1,true),'a failed reload recovery must not erase the journal')
|
|
s.stuck=nil;s.now=2;x:tick()
|
|
assert(s.attrs[unit].Health_Min.base==0,'reload recovery must retry even when the actor is outside the new roster')
|
|
end
|
|
do
|
|
local s=harness();local unit=s.unit('JournalDenied',500)
|
|
s.members={{actor=unit,team='blue'}}
|
|
local x=s.new();assert(x:set('blue','ON').ok)
|
|
s.failPath='R/immortal-floors.tsv';x:tick()
|
|
assert(s.attrs[unit].Health_Min.base==0,'a unit must not stay locked if its original floor cannot be journaled')
|
|
for _=1,20 do x:tick()end
|
|
assert(x:status().failures==1 and #s.writes==6,'journal failures must stop after three lock-and-restore attempts')
|
|
end
|
|
|
|
do
|
|
local s=harness();local unit=s.unit('ReloadEnabled',400,400)
|
|
s.npcs={unit};s.members={{actor=unit,team='blue'}}
|
|
s.files['R/immortal-floors.tsv']='ImmortalFloorsV1\n'..unit.GetAddress()..'\t0.000\t400.000\n'
|
|
local x=s.new();assert(x:set('blue','ON').ok);s.stuck='Health_Min';x:tick()
|
|
local writes=#s.writes
|
|
for _=1,50 do x:tick()end
|
|
assert(#s.writes==writes,'failed reload recovery must be rate limited and must not re-lock the raised floor')
|
|
assert(s.files['R/immortal-floors.tsv']:find('\t0.000\t400.000',1,true))
|
|
s.stuck=nil;s.now=2;x:tick();assert(x:set('blue','OFF').ok);x:tick()
|
|
assert(s.attrs[unit].Health_Min.base==0,'re-enabled units must keep their original floor after recovery')
|
|
end
|
|
|
|
do
|
|
local s=harness();local unit=s.unit('ExternalFloor',400)
|
|
s.members={{actor=unit,team='blue'}}
|
|
local x=s.new();x:set('blue','ON');x:tick();s.stuck='Health_Min';x:set('blue','OFF');x:tick()
|
|
s.stuck=nil;s.attrs[unit].Health_Min={base=50,current=50};s.now=2;x:tick()
|
|
assert(s.attrs[unit].Health_Min.base==50 and x:status().pending_restore==0,'retry must not overwrite an external floor change')
|
|
end
|
|
|
|
do
|
|
local s=harness();local old=s.unit('OldOwner',400)
|
|
s.members={{actor=old,team='blue'}}
|
|
local x=s.new();x:set('blue','ON');x:tick();s.stuck='Health_Min';x:set('blue','OFF');x:tick()
|
|
local fresh=s.unit('NewOwner',400,400);fresh.GetAddress=old.GetAddress
|
|
s.members={{actor=fresh,team='blue'}};s.stuck=nil;s.now=2;x:tick()
|
|
assert(s.attrs[fresh].Health_Min.base==400,'address reuse must not restore an old actor floor into a new actor')
|
|
end
|
|
|
|
do
|
|
local s=harness();local unit=s.unit('Destroying',400)
|
|
s.members={{actor=unit,team='blue'}}
|
|
local x=s.new();x:set('blue','ON');x:tick();unit.destroying=true
|
|
local writes=#s.writes;x:set('blue','OFF');x:tick()
|
|
assert(#s.writes==writes and x:status().pending_restore==0,'destroying actors must not receive restore writes')
|
|
end
|
|
|
|
do
|
|
local s=harness();local unit=s.unit('IdentityWait',400)
|
|
s.members={{actor=unit,team='blue'}}
|
|
local x=s.new();x:set('blue','ON');x:tick();x:set('blue','OFF')
|
|
local getName=unit.GetFName;unit.GetFName=function()error('identity not readable')end;x:tick()
|
|
assert(x:status().pending_restore==1,'a temporary identity read failure must keep the recovery record')
|
|
unit.GetFName=getName;s.now=2;x:tick()
|
|
assert(s.attrs[unit].Health_Min.base==0)
|
|
end
|
|
do
|
|
local s=harness()
|
|
for i=1,100 do s.members[i]={actor=s.unit('Batch'..i,400),team='blue'}end
|
|
local x=s.new();x:set('blue','ON');x:tick()
|
|
assert(x:status().locked==100 and s.journalWrites==1,'a 100-unit batch must persist its floor journal once')
|
|
x:tick()
|
|
assert(s.journalWrites==1,'holding a stable batch must not rewrite the journal')
|
|
end
|
|
print('immortal_teams: PASS (settings, lock, hold, refill, join/leave, release, companions, retries, ready gate, reload journal, unload)')
|