-- 连携技: 铁鬼 and 玉妖 attack together through GA_TieGui_AllOutAttack / GA_Yuu_AllOutAttack (class GA_AutoComboGroup). Their mob -- group decides it, and summoned units have none. A real group cannot be given to them: a valid MobGroup makes the AI call -- iface(group.MobDomain).GetActorsInMobDomain and IsActorInMobDomain, and a group that is not owned by a BP_MobDomain ends in -- a fatal "Failed to find function" (AIScript_UpdateAwareness, Condition_TargetInMobDomain). So MobGroup and Spawner stay empty -- and the two places that ask for the group are done from here, while two such units of one team fight: -- * K2_CanActivateAbility wants GetMobGroup().GetInCombatMembers() above 1: answered true while the partner lives. -- * The picked trees start by waiting on the partner in the blackboard key Vehicle (BTTask_WaitGameplayTagRemoveFromActor): with -- the key empty that task never finishes and has no timeout, so the unit circles for good. Each gets the other there, read -- back; a dead partner is taken out again, since AIScript_TargetSelector copies the target from whoever is in that key. -- That wait is also given a timeout on these trees, or a partner dying with the awaited tag would hold the survivor as well. -- * The picked tree's BTTask_AssignBehaviorTreesToGroupMember finds nobody, and ends with FinishExecute(AlwaysSuccess). Its -- copies on this pair's trees get AlwaysSuccess, so the caster goes on with its part. Right after, the partner gets what the -- task would have given it: the blackboard's assign keys and the event CustomState carrying its tree. Other trees that use the -- same two task classes are left alone: they keep stopping or going on as the game set them. local U=require('ue_observe') local Retry=require('lookup_retry') local Combos={} local HEADER='BossCombosV1' local ASSIGN_CLASS='BTTask_AssignBehaviorTreesToGroupMember_C' local WAIT_CLASS='BTTask_WaitGameplayTagRemoveFromActor_C' local WAIT_SECONDS=8 local EVENT='GameplayEvent.AI.EventTransition.CustomState' local NOT_ASSIGNED='AI.Assignment.CanNotBeAssigned' local PARTNER_KEY='AI.BlackboardKey.Object.Vehicle' local AI_LIB='/Script/SGameAI.Default__SGameAIFunctionLibrary' local BB_LIB='/Script/AIModule.Default__AIBlueprintHelperLibrary' local ABILITY_LIB='/Script/GameplayAbilities.Default__AbilitySystemBlueprintLibrary' Combos.PAIRS={{name='铁鬼+玉妖',classes={'BP_Monster_TieGui_C','BP_Monster_Yuu_C'}}} Combos.paths={ gateTieGui='/PBZCombatAsset/ActorData/Mobs/LuanShi/Ability/TieGui/Abilities/GA_TieGui_AllOutAttack.GA_TieGui_AllOutAttack_C:K2_CanActivateAbility', gateYuu='/PBZCombatAsset/ActorData/Mobs/046_Yuu/Abilities/GA_Yuu_AllOutAttack.GA_Yuu_AllOutAttack_C:K2_CanActivateAbility', assign='/SGameAI/BehaviorTree/Task/BTTask_AssignBehaviorTreesToGroupMember.BTTask_AssignBehaviorTreesToGroupMember_C:ExecuteInternal', } -- What each caster's tree assigns (AssignToTransformAndBehaviorTree, read offline); all use this offset from the caster. local YUU='/PBZCombatAsset/ActorData/Mobs/046_Yuu/BehaviorTree/AllOutAttack/' local TIEGUI='/PBZCombatAsset/ActorData/Mobs/LuanShi/BehaviorTree/TieGui/' Combos.TREES={ BT_Yuu_AllOutAttack_01=YUU..'01/BT_Yuu_AllOutAttack_01_TieGui.BT_Yuu_AllOutAttack_01_TieGui', BT_Yuu_AllOutAttack_02=YUU..'BT_Yuu_AllOutAttack_02_TieGui.BT_Yuu_AllOutAttack_02_TieGui', BT_Yuu_AllOutAttack_03=YUU..'BT_Yuu_AllOutAttack_03_TieGui.BT_Yuu_AllOutAttack_03_TieGui', BT_TieGui_AllOutAttack_01=TIEGUI..'BT_TieGui_AllOutAttack_01_Yuu.BT_TieGui_AllOutAttack_01_Yuu', BT_TieGui_AllOutAttack_02=TIEGUI..'BT_TieGui_AllOutAttack_02_Yuu.BT_TieGui_AllOutAttack_02_Yuu', } -- Every tree of the pair that opens by waiting on the partner (Timeout -1). Combos.WAITING={BT_TieGui_Troop_01=true,BT_TieGui_Troop_02=true} for name in pairs(Combos.TREES)do Combos.WAITING[name]=true end local OFFSET={X=150,Y=-150} local LINES=48 local function nameOf(object) if not U.valid(object) then return nil end local ok,name=pcall(function()return object:GetFName():ToString()end) return ok and type(name)=='string' and name or nil end local function className(object) return U.className(object) or '?' end function Combos.new(options) local log=options.log or function()end local self={alive=true,enabled=false,linked=0,gates=0,assigned=0,failures=0} local links,members,hooks={},{},{} -- pair key -> link; unit address -> {link,partner}; path -> hook ids local seenGate,lines,dispatched={},0,{} local taskWrites={} local function say(text)log('[Combos] '..text)end local retry=Retry.new({clock=options.clock,log=say}) local function find(path)return retry:find(path)end local function sayOften(text) if lines 搭档=%s 树=%s 黑板写入=%s 发事件=%s',className(caster),treeName,className(partner),nameOf(tree) or '?',tostring(wrote==true),tostring(sent))) end function self.onAssign(context) if not self.alive or not self.enabled then return nil end local task=U.unwrap(context) if not U.valid(task) then return nil end local pawn=U.field(task,'ControlledPawn') local entry=U.valid(pawn) and members[U.key(pawn)] if not eligible(pawn,entry) or entry.link.partnerSet[U.key(pawn)]~=true or entry.link.partnerSet[U.key(entry.partner)]~=true then return nil end local tree=U.field(task,'TreeAsset') assign(pawn,entry.partner,U.valid(tree) and nameOf(tree) or nil) return nil end -- Writes the partner key of one unit and reads it back; nil partner empties it. local function setPartner(actor,partner,release) local bbLib,aiLib=find(BB_LIB),find(AI_LIB) if not bbLib or not aiLib or not U.valid(actor) then return false,'缺函数库' end local okBoard,board=pcall(function()return bbLib:GetBlackboard(actor)end) board=okBoard and U.unwrap(board) or nil if not U.valid(board) then return false,'还没有黑板' end local read,old=pcall(function()return U.unwrap(aiLib:GetValueAsObjectByTag(board,tag(PARTNER_KEY)))end) if not read then return false,'原搭档键不可读' end if release then if U.key(board)~=release.board or U.key(old)~=release.partner then return true,'原搭档键已更换' end elseif U.valid(old) then return U.key(old)==U.key(partner),'保留已有搭档键' end local owned=partner and {board=U.key(board),partner=U.key(partner)} or nil local wrote=pcall(function()aiLib:SetValueAsObjectByTag(board,tag(PARTNER_KEY),partner)end) local okBack,back=pcall(function()return aiLib:GetValueAsObjectByTag(board,tag(PARTNER_KEY))end) back=okBack and U.unwrap(back) or nil local same=(partner==nil and not U.valid(back)) or (partner~=nil and U.key(back)==U.key(partner)) return wrote and okBack and same,'写入='..tostring(wrote)..' 读回='..tostring(same),owned end -- What a linked unit is doing: the tree its brain runs, named when it changes, so a stuck tree shows in the log. local function runningTree(actor) local okC,controller=pcall(function()return actor:GetController()end) controller=okC and U.unwrap(controller) or nil local aiLib=find(AI_LIB) if not U.valid(controller) or not aiLib then return nil end local ok,tree=pcall(function()return aiLib:GetRunningBehaviorTree(controller)end) tree=ok and U.unwrap(tree) or nil return U.valid(tree) and nameOf(tree) or nil end local function watchTrees(record) for _,actor in ipairs({record.a,record.b})do local key=U.key(actor) if key and U.valid(actor) then local name=runningTree(actor) if name and record.trees[key]~=name then record.trees[key]=name if name:find('AllOutAttack',1,true) or name:find('Troop',1,true) then sayOften('行为树 单位='..className(actor)..' -> '..name) end end end end end -- What the partner runs on the tick after it got its tree: a tree that ends at once would otherwise not show at all. local function checkDispatched() for key,check in pairs(dispatched)do dispatched[key]=nil if U.valid(check.actor) then sayOften(string.format('分派后 搭档=%s 要跑=%s 实际在跑=%s',className(check.actor),check.want,tostring(runningTree(check.actor)))) end end end local function holdPartners(record) for _,pair in ipairs({{record.a,record.b},{record.b,record.a}})do local actor,partner=pair[1],pair[2] local key=U.key(actor) if key and U.valid(actor) then local living=alive(partner) local state=record.partnerSet[key] if living and state==true and record.team=='companion' and not boundPartner(actor,partner) then state=false end if living and state~=true then local ok,note,owned=setPartner(actor,partner) if owned then record.ownedKeys[key]=owned end if ok then record.partnerSet[key]=true end if ok or state==nil then say('搭档键 单位='..className(actor)..' 搭档='..className(partner)..' '..note) end if not ok then record.partnerSet[key]=false end elseif not living and state==true then local note='无需还原' if record.ownedKeys[key] then local ok,detail=setPartner(actor,nil,record.ownedKeys[key]);note=detail if ok then record.ownedKeys[key]=nil end end record.partnerSet[key]=nil say('搭档不在了,清空搭档键 单位='..className(actor)..' '..note) end end end end local function needed(name) return self.enabled and next(links)~=nil and not hooks[Combos.paths[name]] end local function register(name) if not needed(name) then return end local path=Combos.paths[name] if not retry:ready(path) then return end local okFind,fn=pcall(StaticFindObject,path) retry:result(path,okFind and U.valid(fn)) if not okFind or not U.valid(fn) then return end local handler=name=='assign' and self.onAssign or self.onGate local ok,pre,post=pcall(RegisterHook,path,handler,function()end) if ok and type(pre)=='number' and type(post)=='number' then hooks[path]={pre=pre,post=post};say('挂钩注册ID读回 '..name..' pre='..pre..' post='..post) else retry:result(path..':register',false,tostring(pre)) end end local function installHooks() for name in pairs(Combos.paths)do register(name) end end local function removeHooks() for path,hook in pairs(hooks)do pcall(UnregisterHook,path,hook.pre,hook.post);hooks[path]=nil end end local function link(pair,team,a,b,key,enemy) retry:reset() links[key]={key=key,pair=pair,team=team,a=a,b=b,partnerSet={},ownedKeys={},trees={}} seenGate={} members[U.key(a)]={link=links[key],partner=b};members[U.key(b)]={link=links[key],partner=a} self.linked=self.linked+1 installHooks() holdPartners(links[key]) local target=team=='companion' and (';共同敌人='..tostring(nameOf(enemy) or className(enemy))) or '' say(string.format('连携 %s 队伍=%s:%s + %s;%s%s',pair.name,team,className(a),className(b),prepareTrees(),target)) end local function unlink(key,reason) local record=links[key] if not record then return end record.retiring=true for address,entry in pairs(members)do if entry.link==record then members[address]=nil end end local done=true for _,actor in ipairs({record.a,record.b})do local address=U.key(actor) if address and record.ownedKeys[address] and U.valid(actor) then local ok,note=setPartner(actor,nil,record.ownedKeys[address]) if ok then record.ownedKeys[address]=nil else done=false end sayOften('解除时核对搭档键 单位='..className(actor)..' '..note) end end if not done then return end links[key]=nil say(string.format('解除连携 %s(%s)放行=%d 分派=%d',record.pair.name,reason,self.gates,self.assigned)) end function self:tick() if not self.alive then return end local keep={} if self.enabled then local byTeam={} local groups={} local fighting=options.fighting() for _,member in ipairs(options.members())do if fighting and (member.team=='blue' or member.team=='red') and not member.reserve and U.valid(member.actor) then local team=byTeam[member.team] or {} byTeam[member.team]=team local name=className(member.actor) if not team[name] or (not alive(team[name]) and alive(member.actor)) then team[name]=member.actor end elseif member.team=='companion' and not member.reserve and not member.stand and not member.aid and U.valid(member.actor) then local cls=className(member.actor) for _,pair in ipairs(Combos.PAIRS)do local side=cls==pair.classes[1] and 1 or cls==pair.classes[2] and 2 local enemy,target if side then enemy,target=companionEnemy(member.actor) end if enemy then groups[pair]=groups[pair] or {} local group=groups[pair][enemy] or {{},{},target=target} groups[pair][enemy]=group table.insert(group[side],member.actor) end end end end for teamName,team in pairs(byTeam)do for _,pair in ipairs(Combos.PAIRS)do local a,b=team[pair.classes[1]],team[pair.classes[2]] if a and b then local key=tostring(U.key(a))..':'..tostring(U.key(b)) keep[key]={pair,teamName,a,b} end end end for pair,enemies in pairs(groups)do for _,group in pairs(enemies)do for i=1,math.min(#group[1],#group[2])do local a,b=group[1][i],group[2][i] keep[U.key(a)..':'..U.key(b)]={pair,'companion',a,b,group.target} end end end end for key,record in pairs(links)do if record.retiring or not keep[key] or record.team~=keep[key][2] then unlink(key,self.enabled and '战斗结束或单位离场' or '开关已关') end end for key,p in pairs(keep)do if not links[key] then link(p[1],p[2],p[3],p[4],key,p[5]) elseif not links[key].retiring then installHooks();holdPartners(links[key]);watchTrees(links[key]) end end if next(keep) then checkDispatched() end if not next(links) then if next(taskWrites) then restoreTrees() end if next(hooks) then removeHooks() end end end function self:set(value) self.enabled=value==true local saved=save() say('开关 '..(self.enabled and '开' or '关')..' 保存='..tostring(saved)) self:tick() local state=self.enabled and '连携技已开启' or '连携技已关闭' return {ok=saved,message=saved and state or state..',但没保存下来,下次启动会变回去',spawned_count=0} end function self:status()return {enabled=self.enabled,links=self.linked,gates=self.gates,assigned=self.assigned,failures=self.failures}end function self:silence(onGameThread) self.alive=false;removeHooks() if onGameThread then for key in pairs(links)do unlink(key,'模组卸载') end restoreTrees() end end return self end return Combos