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

165 lines
8.7 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.
-- 玩家可以穿过随从: the app switch (set_companion_passage). On (the default), a companion's capsule ignores the
-- collision channel of the real player's capsule, so the player walks through companions instead of being blocked
-- in a narrow place; enemies collide with companions as before, and hits are unchanged (they are traced against the
-- body mesh's physics bodies, not the capsule). Off, as in the game. Saved in Runtime/companion-passage.tsv.
-- Only the companion's own capsule is written (and its mount's): its response to one channel, with the value it had
-- recorded first. The player's capsule is only read, for its object type. A companion that leaves the team, the
-- switch going off and the mod unloading on the game thread put the recorded response back and read it again.
-- The same call on a capsule (SetCollisionResponseToChannel) is what the free camera's hold uses on the player's.
local U=require('ue_observe')
local Passage={}
local HEADER='CompanionPassageV1'
local IGNORE=0 -- ECollisionResponse: Ignore 0, Overlap 1, Block 2
local LINES=24 -- apply / restore lines in a load
local function call(o,m,...)
if not U.valid(o)then return false end
local a,n={...},select('#',...);local ok,value=pcall(function()return o[m](o,table.unpack(a,1,n))end)
return ok,U.unwrap(value)
end
local function nameOf(o)local ok,n=call(o,'GetFullName');return ok and type(n)=='string'and n or nil end
local function short(n)return n and(n:match('[%.:]([^%.:]+)$')or n)or'?'end
-- options: path, log, io, members() -> the arena's member list, player() -> the real player pawn,
-- mount(actor) -> the vehicle a companion rides, or nil.
function Passage.new(options)
local log=options.log or function()end
local fs,clock=options.io or io,options.clock or os.clock
local file=options.path..'/companion-passage.tsv'
local self={on=true,alive=true,message=''}
local records={} -- capsule address -> {capsule, name, channel, old}
local lines,noChannel=0,false
local function say(text)if lines<LINES then lines=lines+1;log('[CompanionPassage] '..text)end end
local function readFile()
local f=fs.open(file,'r');if not f then return nil end
local value=f:read('*a');f:close();return value
end
do
local saved=readFile()
if type(saved)=='string' and saved:sub(1,#HEADER+1)==HEADER..'\t' then self.on=saved:find('\ton=1',1,true)~=nil end
end
local function responseOf(capsule,channel)
local ok,value=call(capsule,'GetCollisionResponseToChannel',channel)
return ok and tonumber(value) or nil
end
-- Puts one capsule's recorded response back, when it is still the same actor's capsule and still ignores.
local function retry(r,operation)
r.delay=r.operation==operation and math.min(8,r.delay*2) or .5
r.operation=operation;r.retryAt=clock()+r.delay
end
local function restore(key,why,force)
local r=records[key]
if not r then return true end
if r.applied==false or not U.valid(r.capsule) then records[key]=nil;return true end
if not force and r.operation=='restore' and clock()<r.retryAt then return false end
local _,owner=call(r.capsule,'GetOwner')
local name=nameOf(owner)
if not name then retry(r,'restore');return false end
if name~=r.name or U.key(owner)~=r.ownerKey then records[key]=nil;return true end
local current=responseOf(r.capsule,r.channel)
if current==nil then retry(r,'restore');return false end
if current~=IGNORE then records[key]=nil;return true end
call(r.capsule,'SetCollisionResponseToChannel',r.channel,r.old)
local after=responseOf(r.capsule,r.channel)
say('还原 '..short(r.name)..' 对玩家通道'..r.channel..'的碰撞 '..IGNORE..'→'..tostring(after)..'(原值'..r.old..') 原因='..why..' 已还原='..tostring(after==r.old))
if after==r.old then records[key]=nil;return true end
retry(r,'restore');return false
end
local function restoreAll(why,force)
for key in pairs(records)do restore(key,why,force)end
end
local function apply(actor,channel,wanted)
local capsule=U.field(actor,'CapsuleComponent')
local key=U.key(capsule)
if not key then return end
wanted[key]=true
local r=records[key]
local name=nameOf(actor);local ownerKey=U.key(actor)
if not name or not ownerKey then return end
if r and (r.name~=name or r.ownerKey~=ownerKey) then records[key]=nil;r=nil end
if r and r.channel~=channel then
if not restore(key,'玩家通道已切换') then return end
r=nil
end
if r and r.operation=='apply' and clock()<r.retryAt then return end
local current=responseOf(capsule,channel)
if current==nil then return end
if current==IGNORE then
if r and r.applied==false then records[key]=nil
elseif r then r.applied=true;r.operation=nil end
return
end
local _,owner=call(capsule,'GetOwner')
if U.key(owner)~=ownerKey then return end
if not r then r={capsule=capsule,name=name,ownerKey=ownerKey,channel=channel};records[key]=r end
r.old=current
say('即将 让玩家穿过 '..short(name)..':胶囊体对玩家通道'..channel..'的碰撞 '..current..'→'..IGNORE..'(SetCollisionResponseToChannel)')
local called=call(capsule,'SetCollisionResponseToChannel',channel,IGNORE)
local after=responseOf(capsule,channel)
say('让玩家穿过 读回 '..short(name)..' 通道'..channel..'='..tostring(after)..' verified='..tostring(after==IGNORE)..';能否走过去以画面为准')
if after==IGNORE then r.applied=true;r.operation=nil
else
r.applied=false
if called and after==nil then r.applied=nil end
retry(r,'apply')
end
end
function self:set(value)
if value~='ON' and value~='OFF' then return {ok=false,message='穿过随从开关只接受 ON 或 OFF',spawned_count=0} end
local wanted=value=='ON'
local text=HEADER..'\ton='..(wanted and '1' or '0')..'\n'
local f=fs.open(file,'w')
local wrote=f and f:write(text)
local closed=f and f:close()
local saved=f~=nil and wrote~=nil and wrote~=false and closed==true and readFile()==text
if saved then
self.on=wanted
if not wanted then restoreAll('开关关闭',true)end
end
log('[CompanionPassage] 设置 开关='..value..' 保存读回='..tostring(saved)..' 当前='..tostring(self.on))
return {ok=saved,spawned_count=0,message=saved and (wanted and '已开启:你可以直接穿过随从' or (next(records) and '已保存关闭,正在恢复随从原有碰撞' or '已关闭:随从会像原版一样挡住你')) or '穿过随从的设置没有保存'}
end
-- Support tick: every companion (and its mount) ignores the player's channel; whoever left gets its response back.
function self:tick()
if not self.alive then return end
if not self.on then
if next(records)then restoreAll('开关关闭')end
return
end
local companions={}
for _,member in ipairs(options.members() or {})do
if member.team=='companion' and not member.reserve and U.valid(member.actor)then companions[#companions+1]=member.actor end
end
if #companions==0 then
if next(records)then restoreAll('随从离队')end
return
end
local player=options.player()
local ok,channel=call(U.field(player,'CapsuleComponent'),'GetCollisionObjectType')
channel=ok and tonumber(channel) or nil
if not channel then
if not noChannel then noChannel=true;say('读不到玩家胶囊体的碰撞通道,这一轮不改')end
return
end
noChannel=false
local wanted={}
for _,actor in ipairs(companions)do
apply(actor,channel,wanted)
local mount=options.mount and options.mount(actor)
if U.valid(mount)then apply(mount,channel,wanted)end
end
for key in pairs(records)do
if not wanted[key]then restore(key,'随从离队')end
end
end
function self:status()
local count=0;for _,r in pairs(records)do if r.applied then count=count+1 end end
return {on=self.on,applied=count}
end
-- Unloading: the recorded responses go back only on the game thread; otherwise nothing is written.
function self:silence(onGameThread)
if onGameThread==true and self.alive then restoreAll('Mod 卸载',true)end
self.alive=false;records={}
end
return self
end
return Passage