-- Read a companion's own current enemy; never select the player or mutate AI state. local U=require('ue_observe') local Targets={} -- How long the enemy a companion was last read fighting may stand in for an AI target key that is empty for a moment. Targets.MEMORY_SECONDS=20 local function nameOf(object) if not U.valid(object) then return nil end local ok,name=pcall(function()return object:GetFullName()end) return ok and type(name)=='string' and name or nil end local function alive(options,actor) if not U.valid(actor) then return false end local ok,hp=pcall(options.hp,actor) return ok and type(hp)=='number' and hp==hp and hp>0 and hp '..enemyName..';来源='..field..';敌对=2') end if remember then last[key]={target=target,name=enemyName,at=clock()}end return target,true end local empty=true for _,field in ipairs({'Target Key','LastSensedActorKey'})do local targetKey,known=U.field(handler,field) if not known or targetKey==nil then return nil end local ok,target=pcall(function()return U.unwrap(bb:GetValueAsObject(targetKey))end) if not ok then return nil end local enemy,known=hostile(target,field,true) if enemy then return enemy,true end if not known then empty=false end end -- An already launched volley may outlast the AI's target key. Revalidate its own enemy. if castName and castName==nameOf(castTarget) then return hostile(castTarget,'本次施法或刚才的敌人'),false end return nil,empty end function self:inspect(actor)return query(actor)end function self:current(actor,castTarget,castName) local target=query(actor,castTarget,castName) return target end -- For a skill that takes its own lock while the AI's target key is empty for a moment (左殇's 万剑归宗 keeps the -- first target it selects, and the game's own choice then can be the player): the current enemy, else the enemy -- this companion was read fighting within MEMORY_SECONDS, checked again now as any target is (alive, hostile, -- in this world, never the player or a companion). The memory is only what current() read from the AI itself. function self:lastEnemy(actor) local enemy=self:current(actor) if enemy then return enemy end local key=U.key(actor);local memory=key and last[key] if not memory or clock()-memory.at>Targets.MEMORY_SECONDS then return nil end return self:current(actor,memory.target,memory.name) end return self end return Targets