-- The Hun body's own footwork may not end a move this mod started on it. -- AIConfig_Hun lists AITask_CancelAbilities("Ability.Action") under PreMoveToExecuteTasks. BTTask_BaseMoveToTask and -- BTTask_MoveAwayFromTarget run that list before every move: an NPC has no locomotion driver, so the abilities' own -- CancelAbilityOnMoveInput never fires for it and the game needs another way to walk out of a recovery. The weapon -- chains, shadow weapons and guards of this mod run beside the behaviour tree, so each step of it ended them in the -- middle of the swing. With a short circling time the body restarted the first swing several times a second. -- ExecuteTask_Internal is called by blueprint and cancels synchronously. For that one call the running action is made -- uncancellable; hits, stuns, executions, the native attacks and death cancel it at any other time as before. A step -- refused this way is granted through the ability's own Move Cancel Ability once its Cancel phase has nothing to chain. -- -- The other interruption is the body's native attack pool (AbilityPool_Hun_JueYing). Its conditions read the body's own -- tags (CheckTargetTags is off), and every weapon's light A carries the tag the pool's AA waits for: in a player's log -- GA_Hun_Double_AAAA began 0.31 s into the soft sword's AAA, whose strike phase lasts until 0.80 s, and ended it through -- CancelAbilitiesWithTag. While a move of the mod runs, the pool's nine abilities of this body are on cooldown: the -- game's own check in BP_AbilityBase (Cooldown Data, Cooldown Mark Time, both per instance) refuses them. Short -- cooldowns are renewed only while the action runs. Read-back failures retain the original values for another try. local U=require('ue_observe') local Vitals=require('hun_vitals') local Guard={HOOK='/Script/SGameAI.SGameAITask:ExecuteTask_Internal',UTILS='/Script/SGameUtils.Default__AbilityUtils'} local CANCEL={AITask_CancelAbilities_C=true,SGameAITask_CancelAbility=true} -- AbilityPool_Hun_JueYing: the attacks the body's own AI starts. It may walk out of those as it always could. local NATIVE={} for _,s in ipairs({'A','AA','AAA','AAAA','AAAAA','B','BB','BBB','BBBB'})do NATIVE['GA_Hun_Double_'..s..'_C']=true end local EXECUTING={'Ability.Action.Execution','Ability.Executed','CharacterState.HitReact.State.Executing'} local COMBO,FINISH='CharacterState.Action.Phase.Combo','CharacterState.Action.Phase.Cancel' local CHAINING=.45 -- s: the weapon loop pressed or looked for a chain this recently local LONGEST=12 -- s: one action is not kept longer than this local OWN_PUSH=6 -- s: a GA_Hun_Double_* the weapon loop pressed counts as the mod's local LINES=10 -- log lines of kept actions in a load local COOLDOWN='Cooldown_2_C711E94E40D3CE24089AD884023A69F9' local LEGACY_COOLDOWN=1e6 local GATE_SECONDS=2 local RENEW_SECONDS=1 local NO_COOLDOWN,NO_MARK=0,-10000 -- what all nine classes ship with local NEAR=1e-3 local RECOVERY='PhantomGladiator.hun_action_recovery.v1' local MAX_RECOVERY=2*1024*1024 local RESTORE_LOOKUPS=4 local MISSING_LOOKUPS=3 -- transient objects can disappear on a hot reload local function finite(n)return type(n)=='number' and n==n and math.abs(n)0 then local ability=U.struct(spec,'Ability') local class=U.className(ability) if class and (not NATIVE[class] or pushed(r,class,now)) and isAction(class,ability) then for _,field in ipairs({'NonReplicatedInstances','ReplicatedInstances'})do for _,instance in ipairs(U.array(U.struct(spec,field)) or {})do local ok,avatar=call(instance,'GetAvatarActorFromActorInfo') if ok and key and U.key(avatar)==key then list[#list+1]={object=instance,key=U.key(instance),class=class} end end end end end end return list end local function chaining(r,now) local w=r.wep return w~=nil and w.comboWall~=nil and not w.comboOff and now-w.comboWall<=CHAINING end local function cooldownOf(instance) local value,mark=U.struct(U.field(instance,'Cooldown Data'),COOLDOWN),U.field(instance,'Cooldown Mark Time') if finite(value) and finite(mark) then return value,mark end end local function setDuration(instance,value) local data=U.field(instance,'Cooldown Data') local wrote=pcall(function()data[COOLDOWN]=value end) return wrote and near(cooldownOf(instance),value) end local function setMark(instance,value) if not U.valid(instance) then return false end local wrote=pcall(function()instance['Cooldown Mark Time']=value end) return wrote and near(U.field(instance,'Cooldown Mark Time'),value) end local function worldKey() if not o.world then return nil end local ok,world=pcall(o.world) return ok and U.key(world) or nil end local function ownerOf(actor) return {key=U.key(actor),name=fullName(actor),world=worldKey()} end local function receipt(kind,item,owner) item.kind,item.owner,item.name=kind,owner,fullName(item.object) end local function restoreKey(item)return item.kind..':'..item.key end local function wroteMark(item,mark)return near(mark,item.written) or near(mark,item.pendingMark) end local function safeField(v)return type(v)=='string' and v~='' and not v:find('%c') end -- UE4SS may unload Lua off the game thread. The next load gets plain identities and values, never wrappers. local function saveRecovery() local rows,seen={},{} local function add(item) local k=restoreKey(item);local owner=item.owner if seen[k] or not (safeField(owner.world) and safeField(owner.key) and safeField(owner.name) and safeField(item.key) and safeField(item.name) and item.name:match('^%S+ /')) then return end seen[k]=true rows[#rows+1]=table.concat({item.kind,owner.world,owner.key,item.key,owner.name,item.name, tostring(item.value or 0),tostring(item.mark or 0),tostring(item.written or 0),item.legacy and '1' or '0', tostring(item.pendingMark or '-')},'\t') end for _,item in pairs(restoring)do add(item) end for _,entry in ipairs(open)do for _,item in ipairs(entry.list)do add(item) end end for _,b in pairs(bodies)do if b.gate then for _,item in ipairs(b.gate.list)do add(item) end end end if #rows>0 and shared then local data=table.concat(rows,'\n') local ok,verified=pcall(function() shared:SetSharedVariable(RECOVERY,data) return shared:GetSharedVariable(RECOVERY)==data end) say('跨重载状态登记='..#rows..' 读回='..tostring(ok and verified==true)) end end if shared then local ok,saved=pcall(function()return shared:GetSharedVariable(RECOVERY)end) if ok and type(saved)=='string' and #saved>0 and #saved<=MAX_RECOVERY then for row in saved:gmatch('[^\n]+')do -- A prior beta saved the same record without a pending mark field. local kind,world,owner,key,avatar,name,value,mark,written,legacy,tail=row:match( '^(%a+)\t(%d+)\t(%d+)\t(%d+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([01])(.*)$') local pendingMark=tail and tail:match('^\t([^\t]+)$') value,mark,written=tonumber(value),tonumber(mark),tonumber(written) if (kind=='cancel' or kind=='cooldown') and safeField(avatar) and safeField(name) and name:match('^%S+ /') and finite(value) and finite(mark) and finite(written) and (tail=='' or pendingMark) then pendingMark=tonumber(pendingMark) local item={kind=kind,key=key,name=name,owner={key=owner,name=avatar,world=world},value=value, mark=mark,written=written,pendingMark=finite(pendingMark) and pendingMark or nil,legacy=legacy=='1'} restoring[restoreKey(item)]=item end end pcall(function()shared:SetSharedVariable(RECOVERY,'')end) end end local function restoreValue(item,currentWorld) local expected=item.owner if expected.world and currentWorld and expected.world~=currentWorld then return true end if expected.world and not currentWorld then return false end local object=item.object if not object then local path=item.name:match('^%S+ (/.+)$') local ok,found=pcall(find,path) if not ok or not U.valid(found) then item.misses=(item.misses or 0)+1 return item.misses>=MISSING_LOOKUPS,'missing' end item.misses=nil object=found end if U.key(object)~=item.key then return true end if item.name then local name=fullName(object) if not name then return false end if name~=item.name then return true end end local ok,avatar=call(object,'GetAvatarActorFromActorInfo') local avatarKey=ok and U.key(avatar) if not avatarKey then return false end if avatarKey~=expected.key then return true end if expected.name then local name=fullName(avatar) if not name then return false end if name~=expected.name then return true end end item.object=object if item.kind=='cancel' then local cancellable=U.field(object,'bIsCancelable') if cancellable==true then return true end if cancellable~=false then return false end call(object,'SetCanBeCanceled',true) return U.field(object,'bIsCancelable')==true end local value,mark=cooldownOf(object) if not value then return false end local ours=near(value,GATE_SECONDS) or item.legacy and near(value,LEGACY_COOLDOWN) if ours and not near(value,item.value) and not setDuration(object,item.value) then return false end if wroteMark(item,mark) and not near(mark,item.mark) and not setMark(object,item.mark) then return false end return true end local function restore(item,currentWorld) local key=restoreKey(item) local done,reason=restoreValue(item,currentWorld) if done then restoring[key]=nil if reason=='missing' and restoreLines0 then return end end b.since=b.since or now local push=r.wep and r.wep.push if now-math.max(b.since,push and push.at or b.since)>LONGEST then return end local list=running(r,now) local last local owner for _,item in ipairs(list)do if item.key and not restoring['cancel:'..item.key] and U.field(item.object,'bIsCancelable')==true then owner=owner or ownerOf(r.actor) receipt('cancel',item,owner) call(item.object,'SetCanBeCanceled',false) local result=U.field(item.object,'bIsCancelable') if result~=true then held[#held+1]=item end if result==false then last=item end end end if not last then return end b.refused=last b.kept=(b.kept or 0)+1;self.kept=self.kept+1 if lines=item.retryAt then if item.object then restore(item,world) elseif lookups0 do release(table.remove(open)) end local b=bodies[r] if not b then b={};bodies[r]=b end local key=U.key(U.field(r.actor,'Controller')) if key~=b.controller then if b.controller and byController[b.controller]==r then byController[b.controller]=nil end b.controller=key end if key then byController[key]=r end install() if b.gate then self:observe(r) end end -- The mod has just started a move on this body. function self:pressed(r) local b=bodies[r] if not self.alive or not b then return end local now=wall() b.pressedAt,b.since,b.refused=now,now,nil if not b.gate then closeGate(r,b) else renewGate(b) end end -- The move of the mod is over: nothing runs, its recovery is free, or it has run for too long. function self:observe(r,busy,window) local b=bodies[r] if not b then return end if r.dead or not o.current(r) then openGate(b);return end if busy==nil then busy,window=Vitals.actionState(r.actor,fname) end local age=wall()-(b.pressedAt or 0) -- A press reads as busy only once its montage runs: the first moment is not its end. if age>=0 and age<.15 then return end if not busy then b.since,b.refused=nil,nil end if not busy or window and Vitals.phase(r.actor,fname)==FINISH or age>LONGEST or age<0 then openGate(b) elseif b.gate then renewGate(b) end end function self:stop(r) local b=bodies[r] if not b then return end for i=#open,1,-1 do if open[i].record==r then release(table.remove(open,i)) end end openGate(b) bodies[r]=nil if b.controller and byController[b.controller]==r then byController[b.controller]=nil end if (b.kept or 0)>0 then say('出招保护统计 实例='..tostring(r.name)..' 保留='..b.kept..' 收招后放行移动='..(b.granted or 0)) end if not next(bodies) then uninstall() end end -- The weapon loop, once per pass: it pressed something new, or it found nothing to press. function self:settle(r,chained) local b=bodies[r] if not chained then self:observe(r) end local wanted=b and b.refused if not wanted then return end if chained then b.refused=nil;return end if not o.current(r) then b.refused=nil;return end local busy,window=Vitals.actionState(r.actor,fname) if not busy then b.refused,b.since=nil,nil;return end if not window or Vitals.phase(r.actor,fname)~=FINISH then return end b.refused=nil local ok,avatar=call(wanted.object,'GetAvatarActorFromActorInfo') if not ok or U.key(wanted.object)~=wanted.key or U.key(avatar)~=U.key(r.actor) then return end local read,active=call(find(Guard.UTILS),'IsAbilityActive',wanted.object) if not read or active~=true then return end if call(wanted.object,'Move Cancel Ability') then b.granted=(b.granted or 0)+1;self.granted=self.granted+1 end end function self:status()return {kept=self.kept,granted=self.granted,gated=self.gated,hooked=hook~=nil}end function self:silence(onGameThread) self.alive=false -- Off the game thread nothing native may be touched; an action's own end resets its cancellability. if onGameThread==true then while #open>0 do release(table.remove(open)) end for r,b in pairs(bodies)do openGate(b) end uninstall() end saveRecovery() open,bodies,byController,restoring={},{},{},{} end return self end return Guard