Files
phantom-gladiator/Mods/PhantomGladiator/Scripts/companion_bars.lua
T

176 lines
9.6 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Companions' health bars above their heads. Map NPCs carry the game's own bar (BP_RenewalNPC.hpbar, a
-- PBZLazyWidgetComponent that creates WB_EnemyHPBar on demand), and the game shows it for enemies only. For a
-- companion it is set up through the NPC's own EnsureInitHpBar and kept visible while the companion lives,
-- showing the companion's health. Only the companion's own component and widget are written.
-- The first pass on each companion logs the bar's state before and after; later passes log only when the game
-- had hidden the bar again, so the log shows whether the game keeps hiding a friendly unit's bar.
local U=require('ue_observe')
local Bars={}
local SHOWN={[0]=true,[3]=true,[4]=true} -- ESlateVisibility: Visible, HitTestInvisible, SelfHitTestInvisible
local VISIBLE,COLLAPSED=3,1 -- HitTestInvisible as the private boss bars use (arena_hud_widget.lua); Collapsed
local LINES,RESHOW_LINES=40,10
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 call(object,name,...)
if not U.valid(object) then return false,nil end
local args=table.pack(...)
local ok,result=pcall(function()return object[name](object,table.unpack(args,1,args.n))end)
if not ok then return false,result end
return true,U.unwrap(result)
end
function Bars.new(options)
local log=options.log or function()end
local clock=options.clock or os.clock
local self={records={},count=0,shown=0,reshown=0,failures=0,lines=0,reshowLines=0}
local function say(m)
if self.lines>=LINES then return end
self.lines=self.lines+1;log('[CompanionBar] '..m)
end
local function widgetOf(component)
local ok,widget=call(component,'GetUserWidgetObject')
return ok and U.valid(widget) and widget or nil
end
local function bound(widget,actor)
local key=U.key(actor);return key~=nil and U.key((U.field(widget,'SelfActor')))==key
end
local function describe(component,widget,actor)
local _,visible=call(component,'IsVisible')
local text='组件可见='..tostring(visible)..' 组件隐藏='..tostring((U.field(component,'bHiddenInGame')))
if not widget then return text..' 控件=未创建' end
local _,visibility=call(widget,'GetVisibility')
local out={}
local okCan=pcall(function()widget:CanDisplayHpBar(out)end)
return text..' 控件='..className(widget)..' 可见性='..tostring(visibility)
..' WantToShow='..tostring((U.field(widget,'WantToShow')))..' 绑定本单位='..tostring(bound(widget,actor))
..' 游戏判断可显示='..(okCan and tostring(U.unwrap(out.CanDisplay)) or '读不到')
..' 血条比例='..tostring((U.field(U.field(widget,'hpbar'),'CurPercent')))
end
local function show(record,actor,ratio,vitals)
local component=U.field(actor,'hpbar')
if not U.valid(component) then
if not record.noComponent then record.noComponent=true;say('没有头顶血条组件 unit='..record.path..' 类='..className(actor))end
return
end
local widget=widgetOf(component)
local first=not record.seen
if first then record.seen=true;say('开始显示 unit='..record.path..' 之前:'..describe(component,widget,actor))end
if not widget then
record.shown=false
if record.createAfter and clock()<record.createAfter then return end
local okInit=call(actor,'EnsureInitHpBar')
widget=widgetOf(component)
if not widget then call(component,'EnsureWidgetCreated');widget=widgetOf(component)end
say('创建血条 unit='..record.path..' EnsureInitHpBar调用='..tostring(okInit)..' 读回控件='..className(widget))
if not widget then
self.failures=self.failures+1
record.createDelay=math.min(8,(record.createDelay or .25)*2)
record.createAfter=clock()+record.createDelay
return
end
end
record.createAfter,record.createDelay=nil,nil
if not bound(widget,actor) then
record.shown=false
if record.bindAfter and clock()<record.bindAfter and U.key(widget)==record.bindKey then return end
local okInit=call(widget,'Init',actor)
say('控件没绑定本单位,调用Init='..tostring(okInit)..' 读回绑定='..tostring(bound(widget,actor))..' unit='..record.path)
if not bound(widget,actor) then
self.failures=self.failures+1;record.bindKey=U.key(widget)
record.bindDelay=math.min(8,(record.bindDelay or .25)*2)
record.bindAfter=clock()+record.bindDelay
return
end
end
record.bindAfter,record.bindDelay,record.bindKey=nil,nil,nil
local _,componentVisible=call(component,'IsVisible')
local _,visibility=call(widget,'GetVisibility')
if componentVisible~=true or SHOWN[visibility]~=true then
if not first then
self.reshown=self.reshown+1
if self.reshowLines<RESHOW_LINES then
self.reshowLines=self.reshowLines+1
say('血条被游戏收起,重新显示 第'..self.reshown..'次 unit='..record.path..' 当时:'..describe(component,widget,actor))
end
end
if U.field(component,'bHiddenInGame')==true then call(component,'SetHiddenInGame',false,false)end
if componentVisible~=true then call(component,'SetVisibility',true,false)end
call(widget,'ForSeq_ShowEnemyHPBar',actor)
local _,now=call(widget,'GetVisibility')
if SHOWN[now]~=true then call(widget,'SetVisibility',VISIBLE)end
end
local actual=U.field(U.field(widget,'hpbar'),'CurPercent')
if vitals then
local sync=require('hun_vitals').syncBar
local hp=sync(widget,'hpbar','ForSeq_UpdateHealth',vitals.hp,vitals.maximum)
local shachi=sync(widget,'BreakmeterBar','ForSeq_UpdateShachi',vitals.shachi,vitals.shachiMax)
local state=tostring(hp)..'/'..tostring(shachi)
if state~=record.hunBarState then
record.hunBarState=state;say('魂头顶条 生命/杀气显示读回='..state..' 实例='..tostring(U.key(actor)))
end
elseif ratio and (record.ratio==nil or math.abs(record.ratio-ratio)>0.001 or type(actual)~='number' or math.abs(actual-ratio)>0.001) then
local wrote=call(widget,'ForSeq_UpdateHealth',ratio)
local after=U.field(U.field(widget,'hpbar'),'CurPercent')
local verified=wrote and type(after)=='number' and math.abs(after-ratio)<=0.001
record.ratio=verified and ratio or nil
if not verified then
self.failures=self.failures+1
say('血条比例未确认 unit='..record.path..' 目标='..ratio..' 读回='..tostring(after))
end
end
if first then say('显示后 unit='..record.path..':'..describe(component,widget,actor))end
record.widget,record.shown=widget,true
end
local function hide(record)
if record.shown and U.valid(record.widget) and bound(record.widget,record.actor) then
call(record.widget,'SetVisibility',COLLAPSED)
say('随从倒下,收起血条 unit='..record.path)
end
record.shown=false
end
-- members: the arena's units this support tick; only companions themselves get a bar (not their summons).
function self:sync(members)
local seen,count,shown={},0,0
for _,unit in ipairs(members or {})do
local companion=unit.team=='companion'
local key=not unit.summoner and (companion or options.arenaBar and options.arenaBar(unit)) and U.key(unit.actor)
if key and not seen[key] then
seen[key]=true;if companion then count=count+1 end
local record=self.records[key]
if not record then
record={path=tostring((unit.class_path or ''):match('([^/%.]+)_C$') or unit.class_path or key)}
self.records[key]=record
end
record.actor=unit.actor
local v=options.vitals and options.vitals(unit)
local okHP,hp,okMax,maximum
if v then okHP,hp,okMax,maximum=true,v.hp,true,v.maximum
else okHP,hp=pcall(options.hp,unit.actor);okMax,maximum=pcall(options.maxHP,unit.actor) end
hp,maximum=okHP and tonumber(hp) or nil,okMax and tonumber(maximum) or nil
if hp and hp<=0 then hide(record)
else
local ratio=hp and maximum and maximum>0 and math.max(0,math.min(1,hp/maximum)) or nil
local ok,err=pcall(show,record,unit.actor,ratio,v)
if not ok then self.failures=self.failures+1;say('显示出错 unit='..record.path..' error='..tostring(err))end
if companion and record.shown then shown=shown+1 end
end
end
end
for key in pairs(self.records)do if not seen[key] then self.records[key]=nil end end
self.count,self.shown=count,shown
end
function self:status()return {companions=self.count,shown=self.shown,reshown=self.reshown,failures=self.failures}end
return self
end
return Bars