-- Fun features for the player's own character; the arena's units are never touched. -- * Infinite Shachi: the player's Shachi_Min floor is raised to Shachi_Max (CharacterBasics.hpp) -- through the same native override the observer guard uses for Health_Min. Turning it off puts -- the original floor back; a journal keeps that floor across mod reloads. -- * Auto perfect deflect / auto dodge of attacks that cannot be deflected: attackers announce every hit ahead of -- time in the player's DeflectComponent ReceiveFrames (HitGameTime, IsDodge; PBZDeflectSystem.hpp). -- While either is on, a game-thread loop reads them every frame and, just before the hit, presses -- IA_Deflect or IA_Dodge through Enhanced Input the way a key press arrives. It presses during the -- player's own attacks too. Attacks the player can deflect are deflected; the rest (IsDodge frames, -- can't-deflect and red-eye attacks) are dodged. local U=require('ue_observe') local Stats=require('character_stats') local Fun={} Fun.features={'SHACHI','DEFLECT','DODGE'} local HEADER='FunFeaturesV1' local INPUT={DEFLECT='/PBZDeflectSystem/Input/IA_Deflect.IA_Deflect',DODGE='/PBZDeflectSystem/Input/IA_Dodge.IA_Dodge'} local INTERFACE='/Script/EnhancedInput.EnhancedInputSubsystemInterface' local INJECT=INTERFACE..':InjectInputVectorForAction' local SUBSYSTEM='/Script/EnhancedInput.EnhancedInputLocalPlayerSubsystem' local SUBSYSTEMS='/Script/Engine.Default__SubsystemBlueprintLibrary' local KISMET='/Script/Engine.Default__KismetSystemLibrary' local STATICS='/Script/Engine.Default__GameplayStatics' -- The player's passive abilities that turn these presses into a deflect or a perfect dodge. local PASSIVE={DEFLECT='BP_PlayerDeflectPassive_C',DODGE='BP_PerfectDodgePassive_C'} -- Seconds before the announced hit at which the key goes in. Deflect uses half the passive's own -- Perfect Window when that reads back as seconds; these are the fallbacks. Fun.LEAD={DEFLECT=0.06,DODGE=0.10} -- Deflect lead by the attack's strength tag. In v0.36.3 a press 0.04-0.06 s ahead made all 9 Light -- attacks perfect deflects but only 6 of 30 Normal/Knockback ones. Earlier presses for the heavier -- ones (0.09 s and 0.13 s in turn) were set up in v0.36.6 but never ran in game, so the release -- presses every strength at the measured 0.06 s. Several leads listed here are tried in turn. Fun.LIGHT_LEAD=0.06 Fun.HEAVY_LEADS={0.06} local COOLDOWN=0.12 -- one press per kind per this many seconds; the game punishes quick re-clicks local STALE=1 -- a frame still listed this long after its hit time is treated as ended local LATE=0.03 -- a hit already this far past is not pressed for local CHECK=0.8 -- seconds (wall clock) a press is watched for the game's answer -- The deflect ability instance holds the result of each deflect (Current Result Data.DeflectResult) and -- its timing windows (BP_StanceAction_Base_Player.hpp); it exists once the player has deflected. local STANCE='BP_StanceAction_Base_Player_C' local LOG_CAP=300 -- per-frame lines per Lua lifetime; the minute summary keeps counting local EPS=0.01 local function name(obj) if not U.valid(obj) then return 'nil' end local ok,n=pcall(function()return obj:GetFName():ToString()end) return ok and type(n)=='string' and n or '?' end local function className(obj) if not U.valid(obj) then return 'nil' end local ok,cls=pcall(function()return obj:GetClass()end) return ok and name(cls) or '?' end local function tagName(tag) local n=U.struct(tag,'TagName') if n==nil then return '' end local ok,s=pcall(function()return n:ToString()end) return ok and type(s)=='string' and s or tostring(n) end local function weak(value) if value==nil then return nil end local ok,obj=pcall(function()return value:Get()end) if ok and U.valid(obj) then return obj end return U.valid(value) and value or nil end local function num(v)return type(v)=='number' and v==v and v or nil end -- EDeflectBulletType (PBZDeflectSystem_enums.hpp), read from the frame's SourceAction. local TYPES={[0]='不能格挡',[1]='普通',[2]='蓝光',[3]='红光',[4]='重击',[5]='按住格挡'} -- The only rule: an attack the player cannot deflect is dodged, everything else is deflected. -- It cannot be deflected when its frame is IsDodge or its type is can't-deflect or red-eye. local NO_DEFLECT={[0]=true,[3]=true} local function deflectable(record)return not record.dodge and not (record.type and NO_DEFLECT[record.type]) end local function describe(record) return (record.type and TYPES[record.type] or '类型未知')..(deflectable(record) and '→格挡' or '→闪避') end local function fmt(v)return v and string.format('%.3f',v) or 'nil' end function Fun.new(options) local log=options.log or function()end local fs=options.io or io local clock=options.clock or os.clock local stats=options.attributes or Stats.new() local settings=options.path..'/fun-features.tsv' local journal=options.path..'/fun-shachi-floor.tsv' local self={alive=true,on={SHACHI=false,DEFLECT=false,DODGE=false}, shachi={state='off',message='',relocks=0,refills=0,failures=0}, guard={state='off',message='',frames=0,pressed={DEFLECT=0,DODGE=0},received=0,perfectDodges=0, consumed=0,hits=0,failures=0,results=0,perfect=0,damage=0,unannounced=0}} local function say(m)log('[Fun] '..m)end local function readFile(path) local f=fs.open(path,'r');if not f then return nil end local text=f:read('*a');f:close();return text end local function writeFile(path,text) local f=fs.open(path,'w');if not f then return false end local wrote=f:write(text);local closed=f:close() return wrote~=nil and wrote~=false and closed==true and readFile(path)==text end do local text=readFile(settings) if type(text)=='string' and text:sub(1,#HEADER+1)==HEADER..'\t' then for feature,value in text:gmatch('([A-Z]+)=([01])')do if self.on[feature]~=nil then self.on[feature]=value=='1' end end end end local function save() local parts={HEADER} for _,feature in ipairs(Fun.features)do parts[#parts+1]=feature..'='..(self.on[feature] and '1' or '0') end return writeFile(settings,table.concat(parts,'\t')..'\n') end -- ---------- Infinite Shachi ---------- -- lock = the pawn whose floor is raised and the floor it had before. local lock={} local releaseShachi local logged={relock=0,refill=0} local function readJournal() local key,floor=(readFile(journal) or ''):match('^ShachiFloorV1\t(%S+)\t(%S+)\n$') floor=tonumber(floor) if key and Stats.finite(floor) then return key,floor end end local function pawnIdentity(pawn) local value=name(pawn) return value~='?' and value~='nil' and value or nil end local function lockShachi(pawn) if lock.restoring and not releaseShachi('重新开启前恢复') then return end local key=U.key(pawn);if not key then return end local owner=pawnIdentity(pawn);if not owner then return end local shachi,why=stats:read(pawn,'Shachi') local floor=stats:read(pawn,'Shachi_Min');local max=stats:read(pawn,'Shachi_Max') if not shachi or not floor or not max or max.current<=0 then if self.shachi.state~='failed' then say('无限杀气:属性不可读 '..tostring(why)) end self.shachi.state='failed';self.shachi.message='杀气属性不可读';return end if lock.key~=key or (lock.identity and lock.identity~=owner) then -- A new pawn (first lock, map change, load). A floor journaled for this same pawn is its -- original: this one was raised by an earlier Lua instance before a mod reload. local journaled,original=readJournal() original=journaled==key and lock.key~=key and original or floor.base local kept=writeFile(journal,'ShachiFloorV1\t'..key..'\t'..string.format('%.6f',original)..'\n') if not kept then if self.shachi.message~='原始杀气下限没能保存,未加锁' then say('无限杀气:原始下限保存未读回,不修改属性') end self.shachi.state='failed';self.shachi.message='原始杀气下限没能保存,未加锁';return end lock={key=key,floor=original,identity=owner} say(string.format('无限杀气开始 pawn=%s 杀气=%.1f 下限=%.1f(原始%.1f) 上限=%.1f 记录原始下限=%s', key,shachi.current,floor.current,original,max.current,tostring(kept))) end local target=max.current lock.target=target if floor.current>=target-EPS then lock.raised=true end if floor.current=target-EPS if not good then self.shachi.failures=self.shachi.failures+1 end if lock.raised then self.shachi.relocks=self.shachi.relocks+1 end if not lock.raised or logged.relock<10 then if lock.raised then logged.relock=logged.relock+1 end say(string.format('%s杀气下限 写入=%s 目标=%.1f 读回=%s/%s %s',lock.raised and '重新锁定' or '锁定', tostring(ok),target,after and fmt(after.base) or 'nil',after and fmt(after.current) or 'nil',err and ('错误='..tostring(err)) or '')) end lock.raised=lock.raised or good end if shachi.currentEPS then if lock.target and math.abs(before.base-lock.target)>EPS then say('无限杀气关闭:下限已被其他机制改变,不覆盖 读回='..fmt(before.base)) else local ok,err=stats:writeBase(pawn,'Shachi_Min',lock.floor) local after=stats:read(pawn,'Shachi_Min') local verified=ok and after and math.abs(after.base-lock.floor)<=EPS if verified or not lock.restoreFailed then say(string.format('无限杀气关闭(%s)恢复下限 写入=%s 目标=%.1f 读回=%s/%s %s',reason,tostring(ok),lock.floor, after and fmt(after.base) or 'nil',after and fmt(after.current) or 'nil',err and ('错误='..tostring(err)) or '')) end if not verified then return restorePending('恢复写入未读回') end end end else say('无限杀气关闭('..reason..'):锁定过的角色已不在,不用恢复') end if not writeFile(journal,'') then return restorePending('恢复记录清理未读回') end lock={};self.shachi.state='off';self.shachi.message='' return true end -- ---------- Auto deflect / dodge ---------- local input={} local player={} local frames={} -- handle -> what was seen and pressed for it local presses={} -- presses still waiting for the game's answer local ended={} -- the last frames that ended, to tell where damage came from local health -- the player's HP on the previous frame local damageLines=0 -- Hit readback: the app's units (refreshed by the slow tick) and their HP last frame, the player's -- Shachi last frame. Logs each HP drop with the Shachi the player had before and after it. local targets,targetHp,lastShachi={}, {},nil local hitLines,hitTotals=0,{on=0,off=0,onHits=0,offHits=0,spent=0} local last={DEFLECT=-math.huge,DODGE=-math.huge} local lines=0 local function line(m)if linesCHECK then if p.kind=='DEFLECT' and U.valid(player.stance) and not p.resultSeen then line('读回 handle='..tostring(p.handle)..' '..CHECK..' 秒内没有新的格挡结果') end table.remove(presses,i) end end end -- Every frame: a drop in the player's HP is logged with the frames around it, so a hit can be -- told apart as unannounced (no frame), from a frame that was pressed for, or from one that was not. local function checkHealth() local hp=num(U.struct(U.field(U.field(player.pawn,'AttributeSetBase'),'Health'),'CurrentValue')) if not hp then return end if health and hpSTALE then stale[handle]=true;seen[handle]=nil line('预告 handle='..tostring(handle)..' 命中后 '..fmt(now-record.hit)..' 秒仍未消失,按已结束处理') end end for handle,entry in pairs(seen)do local record=frames[handle] if not record then record={first=now,hit=entry.hit,dodge=entry.dodge,type=entry.type,counter=entry.counter,strength=entry.strength} frames[handle]=record;self.guard.frames=self.guard.frames+1 line(string.format('预告 handle=%s %s 类型值=%s IsDodge=%s 距命中=%s 强度=%s 方向=%s 来源=%s 反击=%s',tostring(handle),describe(record), tostring(entry.type),tostring(entry.dodge),entry.hit and fmt(entry.hit-now) or 'nil',entry.strength,entry.direction,entry.source,tostring(entry.counter))) if not timeChecked and entry.hit then timeChecked=true if math.abs(entry.hit-now)>30 then say('时间基准对不上:HitGameTime='..fmt(entry.hit)..' 游戏时间='..fmt(now)..';不会自动按键') end end end record.hit=entry.hit or record.hit;record.dodge=entry.dodge;record.isHit=record.isHit or entry.isHit local kind=deflectable(record) and 'DEFLECT' or 'DODGE' if self.on[kind] and not record.pressed and record.hit and math.abs(record.hit-now)<=30 then local remaining=record.hit-now if remaining<=lead(kind,record) and remaining>-LATE and now-last[kind]>=COOLDOWN then press(kind,handle,record,now,remaining) end end end for handle,record in pairs(frames)do if not seen[handle] then local early=record.hit and now=2 and slow<20 then slow=slow+1;say(string.format('[Perf] 逐帧检查耗时 %.1fms',spent)) end if not reported and clock()-started>=2 then reported=true;say(string.format('逐帧检查实测:%.1f 秒内 %d 次',clock()-started,calls)) end end) if not ok or handle==nil or handle==false then guardFail('逐帧检查注册失败 '..tostring(handle));return end loopHandle=handle;self.guard.state='waiting';self.guard.message='' say('逐帧检查已注册 handle='..tostring(handle)) elseif not want and loopHandle~=nil then local handle=loopHandle;loopHandle=nil;frames={} local ok,result=pcall(CancelDelayedAction,handle) say('逐帧检查已取消 调用='..tostring(ok)..' 返回='..tostring(result)) self.guard.state='off';self.guard.message='' end end function self:set(feature,value) if self.on[feature]==nil then return {ok=false,message='未知功能:'..tostring(feature),spawned_count=0} end if value~='ON' and value~='OFF' then return {ok=false,message='开关值无效:'..tostring(value),spawned_count=0} end local before=self.on[feature] self.on[feature]=value=='ON' if not save() then self.on[feature]=before return {ok=false,message='趣味功能设置保存未读回,未切换',spawned_count=0} end say('设置读回 '..feature..'='..value) if feature=='SHACHI' and not self.on.SHACHI then releaseShachi('开关关闭') end syncLoop() local labels={SHACHI='无限杀气',DEFLECT='自动精准防御',DODGE='自动闪避红光'} local pending=feature=='SHACHI' and not self.on.SHACHI and lock.restoring return {ok=true,message=pending and '无限杀气已关闭,原下限恢复待确认' or labels[feature]..(self.on[feature] and '已开启' or '已关闭'),spawned_count=0} end local minute=clock() function self:tick() if not self.alive then return end if options.ready and not options.ready() then return end syncLoop() local pawn=options.getPlayer and options.getPlayer() self.pawn=U.valid(pawn) and pawn or nil local fresh,count={},0 for _,unit in ipairs(options.members and options.members() or {})do local key=U.key(unit.actor);if key then fresh[key]=unit.actor;count=count+1 end end -- The per-frame hit probe reads every unit's health: a one-on-one diagnostic (v0.36.4), stopped -- with more than four units on the field (50 would mean 50 health reads every frame). if count>4 then fresh={} end for key in pairs(targetHp)do if not fresh[key] then targetHp[key]=nil end end targets=fresh if self.on.SHACHI then if U.valid(pawn) then lockShachi(pawn) end elseif lock.key or not self.journalChecked then self.journalChecked=true releaseShachi('开关未开启') end if self.on.DEFLECT or self.on.DODGE then if U.valid(pawn) then refreshPlayer(pawn) end resolveInput() if self.guard.pressed.DEFLECT>0 then findStance() end end if clock()-minute>=60 then minute=clock() if self.on.SHACHI or self.on.DEFLECT or self.on.DODGE then local g=self.guard say(string.format('每分钟汇总 杀气=%s 重锁=%d 补满=%d 预告=%d 按格挡=%d 格挡收到=%d 格挡结果=%d 完美格挡=%d 按闪避=%d 完美闪避=%d 提前结束=%d 被打中=%d 掉血=%d 无预告掉血=%d 失败=%d', self.shachi.state,self.shachi.relocks,self.shachi.refills,g.frames,g.pressed.DEFLECT,g.received,g.results,g.perfect, g.pressed.DODGE,g.perfectDodges,g.consumed,g.hits,g.damage,g.unannounced,g.failures)) say(string.format('每分钟汇总 召唤单位受伤 无限杀气开=%.1f(%d次) 关=%.1f(%d次) 受伤时玩家杀气减少合计=%.1f', hitTotals.on,hitTotals.onHits,hitTotals.off,hitTotals.offHits,hitTotals.spent)) end end end function self:status() return {shachi=self.on.SHACHI,deflect=self.on.DEFLECT,dodge=self.on.DODGE, shachi_state=self.shachi.state,shachi_failures=self.shachi.failures, guard_state=(self.on.DEFLECT or self.on.DODGE) and self.guard.state or 'off',guard_message=self.guard.message, deflect_pressed=self.guard.pressed.DEFLECT,deflect_received=self.guard.received, dodge_pressed=self.guard.pressed.DODGE,perfect_dodges=self.guard.perfectDodges} end -- Mod unload: only stop the loop. The raised floor stays journaled; the next instance restores it -- on the game thread if the switch is off by then. function self:silence() self.alive=false if loopHandle~=nil then local handle=loopHandle;loopHandle=nil;pcall(CancelDelayedAction,handle) end end return self end return Fun