-- 不死不休: blue and red keep fighting until one side is out (docs/REVERSE_ENGINEERING.md v0.37.2-v0.37.8). -- Without help a unit left the fight about every 10 s and walked back to where it spawned: -- * after every action the AI asks BP_SGameAIComponent_C.ShouldExitCombatAfterAction; -- * the hatred manager refuses another monster as a target (CheckCanHatredTarget/CheckActorCanBeHatred); -- * the state machine itself calls CallTransition(AI.Transition.CombatToIdle), which empties the -- hatred list. The per-instance Idle guard is evaluated before this post-hook. -- While a fight runs, these hooks answer for this mod's blue and red units only: no to leaving combat, -- yes to hating a unit of the other side, and on CombatToIdle the unit is sent straight back at its -- target on the next game-thread tick (options.onDropped); after a perception event the unit's fallback target goes -- back to its opponent (options.onSensed). A blueprint hook's callback runs after the -- function and a non-nil return becomes its result, the way the AI difficulty checks are answered -- (ai_difficulty.lua, verified in v0.33.1); every other AI keeps the game's answers. The hooks exist -- only while a fight runs and are removed a little after it ends. Hooks that were only there to find -- the exit path, and the attack token condition (never asked for these units in v0.37.8), are gone. -- A corpse stays an actor for about 10 s (BP_RenewalNPC DestroyDelay). The game takes a dead unit off the lists of those in -- its BeHatredList (OnDead), but an entry written straight into a HatredList is not in there, and AIScript_TargetSelector -- picks the nearest actor of the HatredList: a unit went on standing over the corpse. So a dead unit is not hatable by this -- fight's units, and comes off their lists the moment it is seen dead. -- A rider's mount (options.vehicles) is an AI of its own that carries the rider: it gets the same answers as a hater, stays -- in combat and is sent back, but it is nobody's target, so its rider is what the other side goes for. local U=require('ue_observe') local Lock={} local AI='/SGameAI/BP_SGameAIComponent.BP_SGameAIComponent_C:' local HATRED='/SGameAI/Tools/Hatred/BP_SGameHatredManagerComponent.BP_SGameHatredManagerComponent_C:' Lock.path=AI..'ShouldExitCombatAfterAction' Lock.paths={exit=Lock.path,canHate=HATRED..'CheckCanHatredTarget',canBeHated=HATRED..'CheckActorCanBeHatred', transition=AI..'CallTransition',sensed=AI..'OnTargetPerceptionUpdated',sensedInfo=AI..'OnTargetPerceptionInfoUpdated', removed=HATRED..'RemoveActorFromHatredList',cleared=HATRED..'RemoveAllActorsFromHatredList'} local ORDER={'exit','canHate','canBeHated','transition','sensed','sensedInfo'} local LABEL={exit='留在战斗中',canHate='允许仇恨',canBeHated='允许被仇恨',transition='脱战后拉回',sensed='感知后改回对手', sensedInfo='感知后改回对手(信息)',removed='处决或无敌时补回条目',cleared='处决或无敌时整表清除后补回'} local UNHOOK_SECONDS,LINES,SUMMARY_SECONDS=10,8,10 local DROP='AI.Transition.CombatToIdle' local DROP_SECONDS=0.5 -- at most one send-back per unit in this time, so a quick re-drop cannot loop local FORGET_SECONDS=0.5 -- failed/unreadable corpse removals retry without rescanning every support tick local function tagName(value) local name=U.struct(U.unwrap(value),'TagName') if name==nil then return nil end if type(name)=='string' then return name end local ok,text=pcall(function()return name:ToString()end) return ok and type(text)=='string' and text or nil end local function addr(object) return U.key(object) end local function identity(actor) if not U.valid(actor) then return nil end local ok,name=pcall(function()return actor:GetFullName()end) return ok and type(name)=='string' and name or nil end function Lock.new(options) local order={table.unpack(ORDER)} if options.guard and options.guard.onHatredRemoved then order[#order+1]='removed';order[#order+1]='cleared' end local log=options.log or function()end local clock=options.clock or os.clock local self={alive=true,state='idle',held=0,passed=0,refused=0,message='没有进行中的战斗',counts={},window={}} local corpseLines=0 for _,name in ipairs(order)do self.counts[name]=0;self.window[name]=0 end local hooks={} -- name -> {pre=, post=} while installed local components={} -- AI component address -> team, for this mod's blue and red units in a fight local managers={} -- hatred manager address -> team local teams={} -- unit address -> team local corpses={} -- unit address -> team, a fighting unit with no health left local forgotten={} -- unit address -> true once confirmed, else next retry time local mounts={} -- mount address -> true: hates, is not hated local owners={} -- AI component address -> unit address local cache={} -- unit address -> {ai=, hatred=} component addresses (a unit keeps its components) local dropped={} -- unit address -> clock() of the last send-back local pendingSense,pendingDrop={},{} local identities={} -- names only for addresses with corpse or send-back state local actorObjects={} local objectNames,componentNames={},{} local identityLines=0 local installAt,installDelay,unhookAt,unhookLines=0,1,0,0 local installWarnings={} local lines,quietSince,summaryAt={},nil,nil for _,name in ipairs(order)do lines[name]=0 end local function say(m)log('[CombatLock] '..m)end local function count(name,text) self.counts[name]=self.counts[name]+1;self.window[name]=self.window[name]+1 if lines[name]0 and corpseLines=forgotten[key]) then fresh[#fresh+1]=member end elseif key then forgotten[key]=nil living[#living+1]=member if fighting then local c=component_cache(key,member.actor,now) known[key]=c if not member.untargetable then units[key]=member.team end if c.ai then ai[c.ai]=member.team;own[c.ai]=key;boundNames[c.ai]=c.aiName end if c.hatred then hatred[c.hatred]=member.team;own[c.hatred]=key;boundNames[c.hatred]=c.hatredName end end end end end if fighting then for _,vehicle in ipairs(options.vehicles and options.vehicles(members) or {})do local key=U.key(vehicle.actor) if key then objects[key]=vehicle.actor;names[key]=identity(vehicle.actor);refreshIdentity(key,vehicle.actor) local c=component_cache(key,vehicle.actor,now) known[key]=c horses[key]=true if c.ai then ai[c.ai]=vehicle.team;own[c.ai]=key;boundNames[c.ai]=c.aiName end if c.hatred then hatred[c.hatred]=vehicle.team;own[c.hatred]=key;boundNames[c.hatred]=c.hatredName end end end end components,managers,teams,cache,owners,corpses,mounts,actorObjects=ai,hatred,units,known,own,bodies,horses,objects objectNames,componentNames=names,boundNames -- Departed actors must not retain corpse completion or send-back throttles at a reused address. for key in pairs(forgotten)do if not present[key] then forgotten[key]=nil end end for key in pairs(dropped)do if not known[key] then dropped[key]=nil end end for key in pairs(pendingSense)do if not known[key] then pendingSense[key]=nil end end for key in pairs(pendingDrop)do if not known[key] then pendingDrop[key]=nil end end if #fresh>0 then forgetFresh(fresh,living,members,fighting,now) end for key in pairs(identities)do if not forgotten[key] and not dropped[key] then identities[key]=nil end end if next(teams) then quietSince=nil if missing_hooks() then install() end elseif next(hooks) then quietSince=quietSince or now if now-quietSince>=UNHOOK_SECONDS then uninstall('战斗已结束') end end summaryAt=summaryAt or now if now-summaryAt>=SUMMARY_SECONDS then if next(hooks) then local parts={} for _,name in ipairs(order)do parts[#parts+1]=LABEL[name]..'='..self.window[name];self.window[name]=0 end if options.guard then parts[#parts+1]='状态与仇恨持续保留累计='..tostring(options.guard.retained) end say(string.format('近 %.0f 秒:%s',now-summaryAt,table.concat(parts,' '))) end summaryAt=now end end function self:status()return {state=self.state,held=self.held,refused=self.refused,message=self.message,counts=self.counts}end function self:silence(reason)self.alive=false;uninstall(reason or 'Mod卸载',true) end return self end return Lock