-- Per-unit AI difficulty. The game's AI asks "is the difficulty at least X" through two blueprint -- checks, both `GetDifficulty() >= self.Difficulty` (docs/REVERSE_ENGINEERING.md v0.32.1/v0.33.0): -- AICondition_AboveDifficulty_C.EvaluateCondition(AIController) shared ability-pool condition -- BTDecorator_AboveDifficulty_C.PerformConditionCheckAI(OwnerController, ControlledPawn) -- Both call UPBZBlueprintFunctionLibrary.GetDifficulty(ContextObject). -- For a unit this app summoned at a chosen difficulty, the hooks answer with that unit's level: -- the checks return `level >= Difficulty`, and GetDifficulty returns the level when its context -- belongs to the unit. UE4SS turns a callback's non-nil return value into the function's return value; -- nil keeps the game's answer. The player, map enemies and the world keep the game's difficulty. -- Verified in game (v0.33.1): a value returned from the GetDifficulty *pre* callback becomes the result; -- the post callback runs too but never sees a usable context, so it stays empty. Nothing is written. -- Every map NPC asks too, so the hooks are hot: they exist only while a leveled unit does (installed -- right before such a unit finishes spawning, removed 10 s after the last one leaves), and attribution -- is a lookup in a per-tick index of the units' and their controllers' addresses (v0.35.3; the -- earlier outer-chain walk with engine validity calls made the game stutter). local U=require('ue_observe') local AI={} local LIBRARY='/Script/ProjectPBZ.PBZBlueprintFunctionLibrary' local DEFAULT='/Script/ProjectPBZ.Default__PBZBlueprintFunctionLibrary' -- GetDiffcultMode was only ever asked with the World as context (v0.32.1–v0.35 logs), never by a unit. local FUNCTIONS={'GetDifficulty'} local UNHOOK_SECONDS=10 -- A hook UE4SS would not remove is tried again this often, not every tick (v0.38.2 logged 150 lines in 78 s). local STALE_RETRY=10 -- The blueprint checks are looked up every 15 s for a minute after a leveled unit is summoned, then no more -- until the next one: a lookup that misses costs 50–60 ms (v0.37.6 log), a stutter every 15 s otherwise. local CHECK_EVERY,CHECK_WINDOW=15,60 local VALUES={Handicapped=0,Easy=1,Normal=2,Hard=3,Nightmare=4} -- EDifficultMode -- Blueprint checks: the hook's single callback runs after the function; `subject` picks the argument -- that names the AI being checked. local CHECKS={ {name='EvaluateCondition',path='/PBZCombatAsset/Prototypes/AI/AICondition_AboveDifficulty.AICondition_AboveDifficulty_C:EvaluateCondition', subject=function(controller)return controller end,call=function(node,controller)return node:EvaluateCondition(controller)end}, {name='PerformConditionCheckAI',path='/PBZCombatAsset/Prototypes/AI/BTDecorator_AboveDifficulty.BTDecorator_AboveDifficulty_C:PerformConditionCheckAI', subject=function(controller,pawn)return pawn or controller end,call=function(node,controller,pawn)return node:PerformConditionCheckAI(controller,pawn)end}, } local MAX_LINES=50 AI.values=VALUES AI.checks=CHECKS local function fname(object) local ok,name=pcall(function()return object:GetFName():ToString()end) return ok and type(name)=='string' and name or '?' end local function className(object) if not U.valid(object) then return 'invalid' end local ok,class=pcall(function()return object:GetClass()end) return ok and U.valid(class) and fname(class) or '?' end local function show(v) v=U.unwrap(v) if type(v)=='number' then return string.format('%.6g',v) end if v==nil or type(v)=='boolean' or type(v)=='string' then return tostring(v) end if U.valid(v) then return fname(v) end local ok,s=pcall(function()return v:ToString()end) return ok and type(s)=='string' and s or U.kind(v) end function AI.new(options) local log=options.log or function()end local clock=options.clock or os.time local self={state='idle',alive=true,overrides=0,verified=0,failed=0,message='还没有按所选难度召唤的单位', calls={},answers={}} local hooks,cache,logged,lines,installed,samples={},{},{},0,{},{} local index={} local nextCheck,retryChecks=nil,{} -- stale: hooks whose removal failed, tried again every STALE_RETRY s. results/checkResults: the last readback -- of each unit on the field and of each check, so the app warns about what is wrong now, not since load. local stale,results,checkResults,currentFailed={},{},{},0 local summarizedAt,summarizedCount,emptySince,checksAt,checksUntil,staleAt local function say(m)log('[AIDifficulty] '..m)end -- The message is built only the first time a signature is seen. local function note(signature,message) if not logged[signature] and lines=threshold self.overrides=self.overrides+1 bump(self.answers,spec.name..' 门槛='..threshold..' → '..tostring(answer)) if checkResults[spec.name]==nil and not samples[spec.name] then samples[spec.name]={node=node,args={controller,pawn},threshold=threshold,level=level} end if lines=sample.threshold local game=gameValue() local gameAnswer=game and game>=sample.threshold local ok=result==expected if ok then self.verified=self.verified+1 else self.failed=self.failed+1 end checkResults[spec.name]=ok say('读回 '..spec.name..' 门槛='..sample.threshold..' 单位难度='..sample.level..' 期望='..tostring(expected)..' 返回='..tostring(result) ..' 游戏难度下应为='..tostring(gameAnswer)..'('..(expected~=gameAnswer and '可区分' or '与游戏答案相同,不能区分')..')结果='..tostring(ok)) end function self:summary(reason) local parts={} for k,v in pairs(self.calls)do parts[#parts+1]=k..'='..v end table.sort(parts) local answers={} for k,v in pairs(self.answers)do answers[#answers+1]=k..' ×'..v end table.sort(answers) say('汇总 reason='..reason..' 改写='..self.overrides..' 读回成功='..self.verified..' 失败='..self.failed..' 回调='..table.concat(parts,' ')) if #answers>0 then say(' 检查节点改写:'..table.concat(answers,';'))end end -- The app's warning: units on the field whose readback failed, and failed check readbacks while units are out. local function countFailed(leveled) local kept,bad={},0 for _,unit in ipairs(leveled)do local result=results[unit.key] if result then kept[unit.key]=result;if not result.ok then bad=bad+1 end end end results=kept for _,ok in pairs(checkResults)do if ok==false then bad=bad+1 end end currentFailed=bad end function self:tick() if not self.alive then return end cache={} retryStale() if options.ready and not options.ready() then index={};results={};samples={};checkResults={};currentFailed=0;nextCheck=nil return end local leveled,fresh,pending={},{},false for _,entry in ipairs(options.members and options.members() or {})do local level=options.levelOf(entry.actor) local key=level and VALUES[level] and addr(entry.actor) if key then fresh[key]=level local previous=results[key] local needsRead=not previous or previous.level~=level or (previous.retryAt and clock()>=previous.retryAt) leveled[#leveled+1]={actor=entry.actor,level=level,key=key,needsRead=needsRead} pending=pending or needsRead local controllerKey=addr(U.field(entry.actor,'Controller')) if controllerKey then fresh[controllerKey]=level end end end index=fresh if #leveled==0 then results={};samples={};checkResults={};currentFailed=0;nextCheck=nil emptySince=emptySince or clock() if self.state=='running' and clock()-emptySince>=UNHOOK_SECONDS then uninstall('召唤单位已全部离场') end return end emptySince=nil if self.state=='idle' then install() end if self.state~='running' then return end -- The blueprint checks load with the first AI that uses them. -- Looking up a function whose class is not loaded is slow in UE4SS (60–70 ms a tick in v0.35.5 -- with only 魔渊 out, whose AI does not use these checks), so try every 15 s, and only in the minute -- after a leveled unit was summoned. local now=clock() local discover=checksUntil and now<=checksUntil if not nextCheck and (discover or next(retryChecks)) and (not checksAt or now-checksAt>=CHECK_EVERY) then checksAt=now;nextCheck=1 end while nextCheck do local i=nextCheck local spec=CHECKS[i] nextCheck=i<#CHECKS and i+1 or nil if not installed[spec.name] and ((checksUntil and checksAt<=checksUntil) or retryChecks[spec.name]) then local ok,rejected=register(spec.path,spec.name,checkCallback(spec)) installed[spec.name]=ok;retryChecks[spec.name]=rejected break end end if pending or next(samples) then local found,lib=pcall(StaticFindObject,DEFAULT) library=found and lib world=options.getWorld and options.getWorld() if U.valid(library) and U.valid(world) then for _,unit in ipairs(leveled)do if unit.needsRead then readbackUnit(unit.actor,unit.level) end end for _,spec in ipairs(CHECKS)do local sample=samples[spec.name] if sample then readbackCheck(spec,sample);samples[spec.name]=nil end end end library,world=nil,nil end countFailed(leveled) -- A running tally once a minute while it changes, so a fight is readable without a reload. if not summarizedAt then summarizedAt=clock() end if clock()-summarizedAt>=60 and summarizedCount~=self.overrides then summarizedAt=clock();summarizedCount=self.overrides;self:summary('每分钟') end end function self:silence(reason) if not self.alive then return end self.alive=false if self.state=='running' then self:summary(reason or 'Mod卸载')end end function self:status() -- failed: what the app warns about (units on the field now); failed_total: every failure since load, for logs. return {state=self.state,overrides=self.overrides,verified=self.verified,failed=currentFailed,failed_total=self.failed, stale=#stale,message=self.message} end return self end return AI