# encoding: UTF-8 #============================================================================== # PvPCombatUXFix 1.1.0 # - suppresses vanilla low-stamina / overfatigue cutscenes when stamina was # exhausted by another player in PvP; # - highlights the current PvP aggressor red only on the victim's client. # # Client-only. Does not alter damage, stamina, capture/slavery, saves, chat, # ClanEstate, or server state. #============================================================================== return unless defined?(WebSocket) module WebSocket MP_PVP_AGGRESSOR_HOLD_SEC = 8.0 unless const_defined?(:MP_PVP_AGGRESSOR_HOLD_SEC) class << self if method_defined?(:_mp_pvp_apply_skill_payload) && !method_defined?(:_mp_ux_old_apply_skill_payload) alias_method :_mp_ux_old_apply_skill_payload, :_mp_pvp_apply_skill_payload end if method_defined?(:handle_being_attacked) && !method_defined?(:_mp_ux_old_handle_being_attacked) alias_method :_mp_ux_old_handle_being_attacked, :handle_being_attacked end end def self._mp_ux_now Time.now.to_f rescue 0.0 end def self._mp_ux_local_actor begin return _mp_pvp_local_actor if respond_to?(:_mp_pvp_local_actor) rescue end begin return _mp_local_actor if respond_to?(:_mp_local_actor) rescue end begin return $game_player.actor if $game_player && $game_player.respond_to?(:actor) rescue end begin return $game_party.leader rescue end nil end def self._mp_ux_actor_sta(actor=nil) actor ||= _mp_ux_local_actor return nil if actor.nil? || !actor.respond_to?(:sta) actor.sta.to_f rescue nil end # Called before an incoming PvP hit is applied. This makes the suppression # active even if the original damage code performs a nested player refresh. def self._mp_ux_begin_incoming(attacker_id) @mp_ux_incoming_depth = @mp_ux_incoming_depth.to_i + 1 @mp_ux_incoming_attacker = attacker_id.to_i rescue end def self._mp_ux_finish_incoming(attacker_id, before_sta=nil, after_sta=nil) aid = attacker_id.to_i # Mark attacker for victim-only red tint. Refresh on every received hit. if aid > 0 @mp_ux_aggressors ||= {} @mp_ux_aggressors[aid] = _mp_ux_now + MP_PVP_AGGRESSOR_HOLD_SEC.to_f end # If the incoming player hit leaves us with no stamina, remember that this # exhaustion belongs to PvP. Keep the latch until stamina is positive again # so the vanilla scene cannot pop a few seconds later after combat TTL ends. cur = after_sta cur = _mp_ux_actor_sta if cur.nil? if !cur.nil? && cur.to_f <= 0.0 @mp_ux_pvp_exhausted = true @mp_ux_pvp_exhausted_by = aid if aid > 0 end rescue ensure @mp_ux_incoming_depth = [@mp_ux_incoming_depth.to_i - 1, 0].max @mp_ux_incoming_attacker = nil if @mp_ux_incoming_depth.to_i <= 0 end def self.mp_pvp_stamina_cutscene_suppressed? # Suppress during the actual incoming damage call too. return true if @mp_ux_incoming_depth.to_i > 0 actor = _mp_ux_local_actor return false if actor.nil? sta = _mp_ux_actor_sta(actor) return false if sta.nil? # Recovery clears the PvP exhaustion latch. A later normal exhaustion may # therefore use the vanilla low-stamina event as usual. if sta.to_f > 0.0 @mp_ux_pvp_exhausted = false @mp_ux_pvp_exhausted_by = nil return false end @mp_ux_pvp_exhausted ? true : false rescue false end def self.mp_pvp_aggressor?(player_id) pid = player_id.to_i return false if pid <= 0 @mp_ux_aggressors ||= {} until_t = @mp_ux_aggressors[pid] return false if until_t.nil? if until_t.to_f <= _mp_ux_now @mp_ux_aggressors.delete(pid) return false end true rescue false end # Extended PvP skill payload path (98_pvp_skill_sync.rb). if respond_to?(:_mp_ux_old_apply_skill_payload) def self._mp_pvp_apply_skill_payload(attacker_id, payload_hash) return false if defined?(MPSafeZone) && MPSafeZone.active? actor = _mp_ux_local_actor before_sta = _mp_ux_actor_sta(actor) _mp_ux_begin_incoming(attacker_id) result = false begin result = _mp_ux_old_apply_skill_payload(attacker_id, payload_hash) ensure after_sta = _mp_ux_actor_sta(actor) _mp_ux_finish_incoming(attacker_id, before_sta, after_sta) end result end end # Legacy / basic attack path. 98_pvp_skill_sync also routes fallback hits here. if respond_to?(:_mp_ux_old_handle_being_attacked) def self.handle_being_attacked(attacker_id, damage, opts=nil) return if defined?(MPSafeZone) && MPSafeZone.active? actor = _mp_ux_local_actor before_sta = _mp_ux_actor_sta(actor) _mp_ux_begin_incoming(attacker_id) result = nil begin begin result = _mp_ux_old_handle_being_attacked(attacker_id, damage, opts) rescue ArgumentError result = _mp_ux_old_handle_being_attacked(attacker_id, damage) end ensure after_sta = _mp_ux_actor_sta(actor) _mp_ux_finish_incoming(attacker_id, before_sta, after_sta) end result end end end #------------------------------------------------------------------------------ # Vanilla stamina events # check_MainStats_event contains BOTH: # sta <= -100 -> OverEvent_Overfatigue.rb # sta <= 0 -> low-stamina portrait/dialogue # We only bypass it while health is still positive and exhaustion is PvP-owned. # Health-death handling remains untouched. #------------------------------------------------------------------------------ if defined?(Game_Interpreter) && Game_Interpreter.instance_methods.include?(:check_MainStats_event) class Game_Interpreter unless instance_methods.include?(:mp_pvp_ux_old_check_MainStats_event) alias mp_pvp_ux_old_check_MainStats_event check_MainStats_event end def check_MainStats_event(parallel=false) begin if defined?(WebSocket) && WebSocket.respond_to?(:mp_pvp_stamina_cutscene_suppressed?) && WebSocket.mp_pvp_stamina_cutscene_suppressed? && defined?($game_player) && $game_player && $game_player.respond_to?(:actor) && $game_player.actor && $game_player.actor.respond_to?(:health) && $game_player.actor.respond_to?(:sta) && $game_player.actor.health.to_f > 0.0 && $game_player.actor.sta.to_f <= 0.0 return end rescue end mp_pvp_ux_old_check_MainStats_event(parallel) end end end #------------------------------------------------------------------------------ # Victim-only aggressor highlight. # Remote players are Game_Event objects with @net_player_id. We tint only their # character sprite, not the nickname/status UI. No packet is sent to server. #------------------------------------------------------------------------------ if defined?(Sprite_Character) class Sprite_Character unless instance_methods.include?(:mp_pvp_ux_old_update) alias mp_pvp_ux_old_update update end def update mp_pvp_ux_old_update mp_pvp_ux_update_aggressor_tint end def mp_pvp_ux_update_aggressor_tint pid = 0 begin if @character && @character.instance_variable_defined?(:@net_player_id) pid = @character.instance_variable_get(:@net_player_id).to_i end rescue pid = 0 end active = false begin active = pid > 0 && defined?(WebSocket) && WebSocket.respond_to?(:mp_pvp_aggressor?) && WebSocket.mp_pvp_aggressor?(pid) rescue active = false end if active unless @mp_pvp_ux_red_tint begin c = self.color @mp_pvp_ux_saved_color = [c.red, c.green, c.blue, c.alpha] rescue @mp_pvp_ux_saved_color = [0, 0, 0, 0] end @mp_pvp_ux_red_tint = true end begin self.color.set(255, 0, 0, 112) rescue self.color = Color.new(255, 0, 0, 112) rescue nil end elsif @mp_pvp_ux_red_tint vals = @mp_pvp_ux_saved_color || [0, 0, 0, 0] begin self.color.set(vals[0], vals[1], vals[2], vals[3]) rescue self.color = Color.new(vals[0], vals[1], vals[2], vals[3]) rescue nil end @mp_pvp_ux_red_tint = false @mp_pvp_ux_saved_color = nil end rescue end end end puts "[PVP-UX] loaded 1.0.0; PvP stamina cutscenes suppressed, aggressor tint active" rescue nil #============================================================================== # PvPCombatUXFix 1.1.0 additions # 1) STA <= -100 from PvP is NOT treated as Lona death while HEALTH > 0. # This fixes the old "first exhausted hit works, further HP damage stops until # map reload" bug caused by Game_Actor#lonaDeath? short-circuiting PvP hits. # 2) Victim pushes a lightweight player-stat sync after every PvP hit. # 3) Attacker sees the same flying HEALTH/STA numbers above the remote target. # 4) Attacker keeps a local mirror of the remote target HEALTH/STA so advanced # PvP skill calculations do not use a stale map-entry snapshot. #============================================================================== module WebSocket def self.mp_pvp_local_exhaustion_alive?(actor=nil) actor ||= _mp_ux_local_actor rescue nil return false if actor.nil? begin local = _mp_ux_local_actor return false unless actor.equal?(local) rescue return false end hp = actor.respond_to?(:health) ? actor.health.to_f : 0.0 sta = actor.respond_to?(:sta) ? actor.sta.to_f : 0.0 return false unless hp > 0.0 && sta <= -100.0 # During the incoming hit the latch may not have been set yet. Afterwards # @mp_ux_pvp_exhausted keeps the exception alive until stamina recovers. @mp_ux_incoming_depth.to_i > 0 || !!@mp_ux_pvp_exhausted rescue false end def self._mp_ux_sync_local_stats_after_hit begin send_playersync_full("pvp_damage", true) if respond_to?(:send_playersync_full) rescue end end def self._mp_ux_remote_actor(player_id) pid = player_id.to_i return nil if pid <= 0 || !@players || !@players[pid] rp = @players[pid] begin return rp.actor if rp.respond_to?(:actor) rescue end begin return rp.instance_variable_get(:@net_actor) rescue end nil rescue nil end def self._mp_ux_apply_remote_damage_mirror(target_id, hp_damage, sta_damage) actor = _mp_ux_remote_actor(target_id) return false if actor.nil? hd = hp_damage.to_i sd = sta_damage.to_i begin if hd > 0 && actor.respond_to?(:health) && actor.respond_to?(:health=) cur = actor.health actor.health = cur.to_f - hd.to_f unless cur.nil? end rescue end begin if sd > 0 && actor.respond_to?(:sta) && actor.respond_to?(:sta=) cur = actor.sta actor.sta = cur.to_f - sd.to_f unless cur.nil? end rescue end true rescue false end def self._mp_ux_show_outgoing_popups(x, y, direction, hp_damage, sta_damage) return unless defined?($game_damage_popups) && $game_damage_popups hd = hp_damage.to_i sd = sta_damage.to_i dir = direction.to_i dir = 2 unless [2,4,6,8].include?(dir) begin $game_damage_popups.add(hd, x.to_i, y.to_i, dir, MP_PVP_POPUP_HP_TYPE) if hd > 0 rescue end begin $game_damage_popups.add(sd, x.to_i, y.to_i, dir, MP_PVP_POPUP_STA_TYPE) if sd > 0 rescue end end class << self # WebSocket.parse is installed with prepend at the end of this file. Using # alias here can recurse if ClanEstate already prepended its parser during a # mobile loading-frame Graphics.update. if method_defined?(:send_deistvia) && !method_defined?(:mp_pvp_ux11_send_deistvia_without_outgoing_popup) alias_method :mp_pvp_ux11_send_deistvia_without_outgoing_popup, :send_deistvia end end # Extended PvP skills already know their exact hp/sta deltas in payload. # The server excludes the sender from deistvia relay, so render/mirror here. def self._mp_ux_payload_is_heal?(payload) return false if payload.nil? if payload.is_a?(Hash) return true if payload["heal"].to_s == "1" return true if payload["sup"].to_s == "1" && ( payload["skill_name"].to_s.include?("NunHeal") || payload["skill"].to_s.include?("NunHeal") || payload["hp"].to_i < 0 || payload["sta"].to_i < 0 ) return true if payload["skill_name"].to_s == "BasicNunHeal" return false end s = payload.to_s return true if s.include?("heal=1") return true if s.include?("BasicNunHeal") return true if s.include?("sup=1") && (s.include?("NunHeal") || s.include?("hp=-") || s.include?("sta=-")) false rescue false end if respond_to?(:mp_pvp_ux11_send_deistvia_without_outgoing_popup) def self.send_deistvia(action_id, phase, payload) begin if action_id.to_s == "pvp_skill_hit" && phase.to_s != "stop" && payload.is_a?(Hash) target_id = (payload["target"] || payload["tgt"] || payload["target_id"]).to_i if target_id > 0 && (!@my_id || target_id != @my_id.to_i) hp_raw = (payload["hp"] || 0).to_i sta_raw = (payload["sta"] || 0).to_i tx = (payload["tx"] || 0).to_i ty = (payload["ty"] || 0).to_i dir = ($game_player.direction rescue 2) if _mp_ux_payload_is_heal?(payload) # Heal amounts may be positive (heal=1) or negative (stock deltas). hh = hp_raw.abs sh = sta_raw.abs begin if defined?($game_damage_popups) && $game_damage_popups $game_damage_popups.add(hh, tx, ty, dir, (defined?(MP_PVP_POPUP_PLUS_HP_TYPE) ? MP_PVP_POPUP_PLUS_HP_TYPE : 10)) if hh > 0 $game_damage_popups.add(sh, tx, ty, dir, (defined?(MP_PVP_POPUP_PLUS_STA_TYPE) ? MP_PVP_POPUP_PLUS_STA_TYPE : 11)) if sh > 0 end rescue end # Mirror heal onto local remote actor preview (add, don't subtract). begin actor = _mp_ux_remote_actor(target_id) if actor actor.health = actor.health.to_f + hh if hh > 0 && actor.respond_to?(:health=) actor.sta = actor.sta.to_f + sh if sh > 0 && actor.respond_to?(:sta=) end rescue end else _mp_ux_show_outgoing_popups(tx, ty, dir, hp_raw, sta_raw) _mp_ux_apply_remote_damage_mirror(target_id, hp_raw, sta_raw) end end end rescue end mp_pvp_ux11_send_deistvia_without_outgoing_popup(action_id, phase, payload) end end end # PvP stamina exhaustion must not trip the stock death predicate. Without this, # multiplayer/04_remote_players.rb returns before applying the next hit whenever # STA <= -100, which is why HP starts decreasing again only after a map reload. if defined?(Game_Actor) class Game_Actor if method_defined?(:lonaDeath?) && !method_defined?(:mp_pvp_ux11_lonaDeath_without_fix) alias_method :mp_pvp_ux11_lonaDeath_without_fix, :lonaDeath? def lonaDeath? begin if defined?(WebSocket) && WebSocket.respond_to?(:mp_pvp_local_exhaustion_alive?) && WebSocket.mp_pvp_local_exhaustion_alive?(self) return false end rescue end mp_pvp_ux11_lonaDeath_without_fix end end if method_defined?(:determine_death) && !method_defined?(:mp_pvp_ux11_determine_death_without_fix) alias_method :mp_pvp_ux11_determine_death_without_fix, :determine_death def determine_death begin if defined?(WebSocket) && WebSocket.respond_to?(:mp_pvp_local_exhaustion_alive?) && WebSocket.mp_pvp_local_exhaustion_alive?(self) # If an older hit already put the actor in :death solely because of # stamina, recover the action state. HEALTH death is never masked. if respond_to?(:health) && health.to_f > 0.0 begin set_action_state(:none, true) if respond_to?(:set_action_state) && respond_to?(:action_state) && action_state == :death rescue end return end end rescue end mp_pvp_ux11_determine_death_without_fix end end end end # Add the lightweight stat broadcast to the already-installed incoming-hit # finalizer without changing cloud-save behavior. module WebSocket class << self if method_defined?(:_mp_ux_finish_incoming) && !method_defined?(:mp_pvp_ux11_finish_incoming_without_stat_sync) alias_method :mp_pvp_ux11_finish_incoming_without_stat_sync, :_mp_ux_finish_incoming end end if respond_to?(:mp_pvp_ux11_finish_incoming_without_stat_sync) def self._mp_ux_finish_incoming(attacker_id, before_sta=nil, after_sta=nil) result = mp_pvp_ux11_finish_incoming_without_stat_sync(attacker_id, before_sta, after_sta) _mp_ux_sync_local_stats_after_hit result end end end puts "[PVP-UX] loaded v1.1.0; PvP exhaustion stays alive, outgoing damage popups + stat sync active" rescue nil #============================================================================== # PvPCombatUXFix 1.3.0 additions # - mark the aggressor directly from authoritative network traffic; # - show a separate red aura BEHIND the aggressor on the victim's client; # - do not recolor/tint the aggressor character itself. #============================================================================== module WebSocket MP_PVP_AGGRESSOR_HOLD_SEC = 8.0 unless const_defined?(:MP_PVP_AGGRESSOR_HOLD_SEC) def self.mp_pvp_mark_aggressor(player_id, seconds=nil) pid = player_id.to_i return false if pid <= 0 hold = seconds.nil? ? MP_PVP_AGGRESSOR_HOLD_SEC.to_f : seconds.to_f hold = MP_PVP_AGGRESSOR_HOLD_SEC.to_f if hold <= 0.0 @mp_ux_aggressors ||= {} @mp_ux_aggressors[pid] = _mp_ux_now + hold puts "[PVP-UX] aggressor aura pid=#{pid} hold=#{hold}" rescue nil true rescue false end class << self if method_defined?(:handle_remote_deistvia) && !method_defined?(:mp_pvp_ux13_remote_deistvia_without_aggressor_mark) alias_method :mp_pvp_ux13_remote_deistvia_without_aggressor_mark, :handle_remote_deistvia end end if respond_to?(:mp_pvp_ux13_remote_deistvia_without_aggressor_mark) def self.handle_remote_deistvia(player_id, action_id, phase, payload) # Safe zone: block hostile PvP hits, but always allow support heals # (BasicNunHeal / sup=1 / heal=1). The old blanket return prevented # Исцеление души from ever applying on the receiver. if action_id.to_s == "pvp_skill_hit" && defined?(MPSafeZone) && MPSafeZone.active? allow_heal = _mp_ux_payload_is_heal?(payload) return unless allow_heal end begin if action_id.to_s == "pvp_skill_hit" && phase.to_s != "stop" target_id = 0 if payload.is_a?(Hash) target_id = (payload["target"] || payload["tgt"] || payload["target_id"]).to_i elsif payload.is_a?(String) begin h = respond_to?(:_deistvia_unpack) ? _deistvia_unpack(payload.to_s) : {} target_id = (h["target"] || h["tgt"] || h["target_id"]).to_i rescue target_id = 0 end end # Do not mark heal casters as aggressors. unless _mp_ux_payload_is_heal?(payload) if @my_id && target_id == @my_id.to_i && player_id.to_i != @my_id.to_i mp_pvp_mark_aggressor(player_id.to_i) end end end rescue end mp_pvp_ux13_remote_deistvia_without_aggressor_mark(player_id, action_id, phase, payload) end end end # Runtime-generated pixel aura. A single cached bitmap is shared by all remote # character sprites, so there is no per-frame bitmap allocation. module MPPvPAggressorAura WIDTH = 52 HEIGHT = 58 def self.bitmap return @bitmap if @bitmap && !(@bitmap.disposed? rescue false) bmp = Bitmap.new(WIDTH, HEIGHT) cx = (WIDTH - 1) / 2.0 cy = (HEIGHT - 1) / 2.0 rx = WIDTH * 0.46 ry = HEIGHT * 0.46 y = 0 while y < HEIGHT x = 0 while x < WIDTH dx = (x - cx) / rx dy = (y - cy) / ry d = Math.sqrt(dx * dx + dy * dy) alpha = 0 # A hollow halo: brightest around the character silhouette, fading both # inward and outward. The character itself stays completely untinted. if d >= 0.48 && d <= 1.08 ring = 1.0 - ((d - 0.76).abs / 0.32) ring = 0.0 if ring < 0.0 alpha = (ring * 118.0).to_i end # Small ground glow behind the feet. if y >= HEIGHT - 12 gx = (x - cx).abs / (WIDTH * 0.42) gy = (y - (HEIGHT - 7)).abs / 7.0 gd = gx * gx + gy * gy if gd <= 1.0 g_alpha = ((1.0 - gd) * 72.0).to_i alpha = g_alpha if g_alpha > alpha end end bmp.set_pixel(x, y, Color.new(255, 24, 24, alpha)) if alpha > 0 x += 1 end y += 1 end @bitmap = bmp rescue nil end end if defined?(Sprite_Character) class Sprite_Character if method_defined?(:dispose) && !method_defined?(:mp_pvp_ux13_dispose_without_aura) alias mp_pvp_ux13_dispose_without_aura dispose def dispose begin if @mp_pvp_ux_aura_sprite @mp_pvp_ux_aura_sprite.dispose unless @mp_pvp_ux_aura_sprite.disposed? @mp_pvp_ux_aura_sprite = nil end rescue end mp_pvp_ux13_dispose_without_aura end end def mp_pvp_ux_remote_player_id return 0 unless @character return 0 unless @character.instance_variable_defined?(:@net_player_id) @character.instance_variable_get(:@net_player_id).to_i rescue 0 end def mp_pvp_ux_ensure_aura_sprite return @mp_pvp_ux_aura_sprite if @mp_pvp_ux_aura_sprite && !(@mp_pvp_ux_aura_sprite.disposed? rescue true) aura = Sprite.new(self.viewport) aura.bitmap = MPPvPAggressorAura.bitmap aura.ox = MPPvPAggressorAura::WIDTH / 2 aura.oy = MPPvPAggressorAura::HEIGHT - 2 aura.visible = false aura.opacity = 0 @mp_pvp_ux_aura_sprite = aura rescue nil end # Kept under the old method name because the base part of this patch calls # it from Sprite_Character#update. It now manages ONLY the background aura. def mp_pvp_ux_update_aggressor_tint pid = mp_pvp_ux_remote_player_id active = false begin active = pid > 0 && defined?(WebSocket) && WebSocket.respond_to?(:mp_pvp_aggressor?) && WebSocket.mp_pvp_aggressor?(pid) rescue active = false end aura = @mp_pvp_ux_aura_sprite if active aura = mp_pvp_ux_ensure_aura_sprite return if aura.nil? aura.x = self.x aura.y = self.y + 2 aura.z = self.z.to_i - 1 aura.zoom_x = self.zoom_x rescue 1.0 aura.zoom_y = self.zoom_y rescue 1.0 aura.visible = self.visible # Gentle pulse, not a full-character flash. phase = ((Graphics.frame_count rescue 0).to_f / 8.0) aura.opacity = 175 + (Math.sin(phase) * 28).to_i elsif aura aura.visible = false aura.opacity = 0 end rescue begin @mp_pvp_ux_aura_sprite.visible = false if @mp_pvp_ux_aura_sprite rescue end end end end #------------------------------------------------------------------------------ # Order-safe WebSocket parser layer for mobile/desktop. #------------------------------------------------------------------------------ module MPPvPCombatUXWebSocketParsePatch def parse(msg) begin line = msg.to_s # Victim: remember who hit us so the aura can be shown locally. if line =~ /\Aattack:(\d+):(\d+):/ attacker_id = $1.to_i target_id = $2.to_i if @my_id && target_id == @my_id.to_i && attacker_id != @my_id.to_i mp_pvp_mark_aggressor(attacker_id) if respond_to?(:mp_pvp_mark_aggressor) end end # Attacker: mirror accepted basic hit and show outgoing HP/STA numbers. if line =~ /^attack:(\d+):(\d+):(\d+):(\d+):(\d+)/ attacker_id = $1.to_i target_id = $2.to_i tx = $3.to_i ty = $4.to_i raw = $5.to_i if @my_id && attacker_id == @my_id.to_i && target_id != @my_id.to_i raw = 1 if raw < 1 hp_dmg = (raw + 1) / 2 sta_dmg = raw * 2 dir = ($game_player.direction rescue 2) _mp_ux_show_outgoing_popups(tx, ty, dir, hp_dmg, sta_dmg) _mp_ux_apply_remote_damage_mirror(target_id, hp_dmg, sta_dmg) end end rescue end super(msg) end end begin sc = WebSocket.singleton_class sc.prepend(MPPvPCombatUXWebSocketParsePatch) unless sc.ancestors.include?(MPPvPCombatUXWebSocketParsePatch) rescue => e puts "[PVP-UX] parser prepend failed #{e.class}: #{e.message}" rescue nil end puts "[PVP-UX] loaded v1.3.0; aggressor shown with victim-local red background aura" rescue nil