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

142 lines
7.4 KiB
Lua

-- Rules set on a unit's card in the app (set_unit_rule), per class, saved in Runtime/unit-rules.tsv. Only what differs from
-- the default is kept.
-- lock: how a unit on its death lock ends. execute (default): an opponent executes it, as far as it has an execution.
-- kill: it dies on the lock (arena_executions.lua).
-- phantom: 琴女's 虚化 in fights of blue, red and companions. off (default): taken away when summoned, since nothing an NPC
-- does can break it (phantom_break.lua). keep: as in the game. XuKong and XianZhe keep native protection by default.
-- remote_reflect: thorns shields only. keep (default): original retaliation; off: no ranged shield retaliation.
local U=require("ue_observe")
local Rules={}
Rules.THORN_COUNTERS={
BP_Monster_JingYe='GE_JingYe_Shield_Counter_C',
BP_Monster_MaleThug_Club_Yellow_Shield='GE_MaleThug_ClubShield_Counter_C',
BP_Monster_MaleThug_Club_Yellow_Shield_Teaching='GE_MaleThug_ClubShield_Counter_C',
}
local HEADER='UnitRulesV1'
Rules.VALUES={lock={execute=true,kill=true},phantom={off=true,keep=true},remote_reflect={keep=true,off=true},menghua_stage={keep=true,skip=true}}
Rules.DEFAULTS={lock='execute',phantom='off',remote_reflect='keep',menghua_stage='keep'}
Rules.VALUES.entourage={off=true,on=true}
Rules.DEFAULTS.entourage='off'
Rules.VALUES.execution_minions={}
for i=0,100 do Rules.VALUES.execution_minions['on_'..i]=true;Rules.VALUES.execution_minions['off_'..i]=true end
Rules.DEFAULTS.execution_minions='on_100'
local LABELS={entourage={off='单体出战(下次召唤生效)',on='原班配合(下次召唤生效)'},lock={execute='锁血后处决',kill='锁血后打死'},phantom={off='虚化解除(下次召唤生效)',keep='虚化保持原版(下次召唤生效)'},
remote_reflect={keep='刺盾反伤保持原版(下次召唤生效)',off='刺盾不反远程(下次召唤生效)'},
menghua_stage={keep='保留梦华剧情联动(下次召唤生效)',skip='跳过梦华剧情联动(下次召唤生效)'}}
local XUKONG='BP_Monster_XuKong'
local XIANZHE='BP_Monster_XianZhe'
local function defaultValue(id,rule)
if (id==XUKONG or id==XIANZHE or id=='BP_Monster_MoTian_LongZhu_01') and rule=='phantom' then return 'keep' end
return Rules.DEFAULTS[rule]
end
local function labelFor(id,rule,value)
if rule=='execution_minions'then return value:sub(1,3)=='on_'and('处决小怪概率 '..value:match('%d+$')..'%')or'已关闭自动处决小怪'end
if id==XIANZHE and rule=='phantom' then
return value=='off' and '关闭技能虚化无敌(下次召唤生效)' or '保留技能虚化无敌(下次召唤生效)'
end
if id==XUKONG and rule=='phantom' then
return value=='off' and '关闭常驻无敌(下次召唤生效)' or '保留常驻无敌(下次召唤生效)'
end
return LABELS[rule][value]
end
local function validId(id)return type(id)=='string' and #id<=128 and id:match('^BP_[%w_]+$')~=nil end
function Rules.new(options)
local log=options.log or function()end
local fs=options.io or io
local file=options.path..'/unit-rules.tsv'
local self={}
local rules={} -- unit id -> {rule=value}
local executionOverrides=false
local function refreshExecution()
executionOverrides=false
for _,row in pairs(rules)do if row.execution_minions then executionOverrides=true;break end end
end
local function readFile()
local f=fs.open(file,'r');if not f then return nil end
local value=f:read(131073);f:close();return type(value)=='string'and #value<=131072 and value or nil
end
local function render(from)
local ids={}
for id in pairs(from)do ids[#ids+1]=id end
table.sort(ids)
local out={HEADER}
for _,id in ipairs(ids)do
local names={}
for rule in pairs(from[id])do names[#names+1]=rule end
table.sort(names)
for _,rule in ipairs(names)do out[#out+1]=id..'\t'..rule..'\t'..from[id][rule] end
end
return table.concat(out,'\n')..'\n'
end
do
local saved=readFile()
if type(saved)=='string' and saved:sub(1,#HEADER+1)==HEADER..'\n' then
for id,rule,value in saved:gmatch('\n([^\t\r\n]+)\t([^\t\r\n]+)\t([^\t\r\n]+)')do
if validId(id) and Rules.VALUES[rule] and Rules.VALUES[rule][value] and value~=defaultValue(id,rule)
and (rule~='entourage' or require('boss_entourage').supports(id))
and (rule~='remote_reflect' or Rules.THORN_COUNTERS[id])
and (rule~='menghua_stage' or id=='BP_Monster_MengHua') then
rules[id]=rules[id] or {};rules[id][rule]=value
end
end
end
end
refreshExecution()
function self:set(id,rule,value)
if not validId(id) then return {ok=false,message='单位规则:单位编号不对',spawned_count=0} end
if not Rules.VALUES[rule] or not Rules.VALUES[rule][value] then return {ok=false,message='单位规则:规则或取值不对',spawned_count=0} end
if rule=='entourage' and not require('boss_entourage').supports(id) then return {ok=false,message='该单位没有配合阵容',spawned_count=0} end
if rule=='remote_reflect' and not Rules.THORN_COUNTERS[id] then return {ok=false,message='该单位没有刺盾反伤开关',spawned_count=0} end
if rule=='menghua_stage' and id~='BP_Monster_MengHua' then return {ok=false,message='只有梦华支持剧情联动设置',spawned_count=0} end
local copy={}
for unit,row in pairs(rules)do
copy[unit]={}
for name,current in pairs(row)do copy[unit][name]=current end
end
copy[id]=copy[id] or {}
copy[id][rule]=value~=defaultValue(id,rule) and value or nil
if not next(copy[id]) then copy[id]=nil end
local text=render(copy)
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 rules=copy;refreshExecution()end
log(string.format('[UnitRules] 设置 单位=%s %s=%s 保存读回=%s',id,rule,value,tostring(saved)))
return {ok=saved,spawned_count=0,message=saved and labelFor(id,rule,value) or '单位规则没有保存'}
end
-- The rule for a unit id or an actor's class.
function self:get(unit,rule)
local id=unit
if type(unit)~='string' then
id=nil
if U.valid(unit) then
local ok,cls=pcall(function()return unit:GetClass()end)
cls=ok and U.unwrap(cls) or nil
if U.valid(cls) then
local named,name=pcall(function()return cls:GetFName():ToString()end)
id=named and type(name)=='string' and name:gsub('_C$','') or nil
end
end
end
return id and rules[id] and rules[id][rule] or defaultValue(id,rule)
end
function self:hasExecutionOverrides()return executionOverrides end
-- For the app: BP_Monster_MoTian:lock=kill;BP_Monster_FemaleDoll_QinNv:phantom=keep
function self:status()
local parts={}
for id,row in pairs(rules)do
for rule,value in pairs(row)do parts[#parts+1]=id..':'..rule..'='..value end
end
table.sort(parts)
return {text=table.concat(parts,';')}
end
return self
end
return Rules