Files
LonaRPG-Online-Games/ModScripts/multiplayer/07_effects.rb
T
2026-10-29 21:59:06 +04:00

822 lines
21 KiB
Ruby

module WebSocket
# WORLD: Effects / Skills
#--------------------------------------------------------------------------
class NetEffectCharacter < Game_Character
def initialize(x, y, anim_id)
super()
moveto(x, y)
self.animation_id = anim_id
@transparent = true
@through = true
end
end
def self.show_skill_cast(skill_id, caster_id, tx, ty, target_id)
return unless $data_skills && $data_skills[skill_id]
anim_id = ($data_skills[skill_id].animation_id rescue 0).to_i
return if anim_id <= 0
if target_id && target_id > 0
if target_id == @my_id
$game_player.animation_id = anim_id rescue nil
return
end
if @players[target_id]
@players[target_id].animation_id = anim_id rescue nil
return
end
elsif target_id && target_id < 0
ev = get_map_event(-target_id)
if ev
ev.animation_id = anim_id rescue nil
return
end
end
show_animation_on_xy(anim_id, tx, ty)
end
def self.show_area_effect(eff_type, x, y, radius, params)
case eff_type
when "damage"
$game_map.screen.start_shake(3, 3, 8) rescue nil
show_animation_on_xy(12, x, y) rescue nil
when "heal"
show_animation_on_xy(13, x, y) rescue nil
when "sound"
begin
a = params.to_s.split(",")
name = a[0] || ""
vol = (a[1] || "80").to_i
pit = (a[2] || "100").to_i
RPG::SE.new(name, vol, pit).play if name && !name.empty?
rescue
end
else
show_animation_on_xy(11, x, y) rescue nil
end
end
def self.show_animation_on_xy(anim_id, x, y)
sc = respond_to?(:scene_with_map_spriteset) ? scene_with_map_spriteset : nil
if sc.nil? && SceneManager.scene_is?(Scene_Map)
sc = SceneManager.scene
end
return unless sc && sc.respond_to?(:add_net_sprite)
char = NetEffectCharacter.new(x, y, anim_id)
sc.add_net_sprite(char)
expire = (Graphics.frame_count rescue 0) + 90
@net_effects << [char, expire]
end
def self.cleanup_net_effects
return if @net_effects.nil? || @net_effects.empty?
now = (Graphics.frame_count rescue 0)
@net_effects.delete_if do |pair|
char, expire = pair
if now >= expire
sc = respond_to?(:scene_with_map_spriteset) ? scene_with_map_spriteset : nil
if sc.nil? && SceneManager.scene_is?(Scene_Map) && SceneManager.scene.spriteset
sc = SceneManager.scene
end
if sc && sc.spriteset
sprites = sc.spriteset.instance_variable_get(:@character_sprites)
sprites.delete_if do |sprite|
if sprite.character == char
sprite.dispose rescue nil
true
else
false
end
end
end
true
else
false
end
end
end
#--------------------------------------------------------------------------
end
#------------------------------------------------------------------------------
# Дополнение: синхронизация реальных player-эффектов / action-анимаций
# - не требует правок сервера (использует уже существующий канал deistvia)
# - синкает custom charset animation (упал / лежит / overfatigue / hold / etc)
# - синкает animation_id (обычные RPG Maker эффекты: удар, огонь, вспышки)
# - синкает balloon/call_balloon
#------------------------------------------------------------------------------
module WebSocket
MPFX_RESEND_LOOP_EVERY = 0.75
MPFX_DEDUP_WINDOW = 0.06
MPFX_MAX_CHAR_FRAMES = 72
MPFX_MAX_PAYLOAD_BYTES = 3000
class << self
begin
alias_method :_mpfx_old_parse, :parse unless method_defined?(:_mpfx_old_parse)
rescue
end
begin
alias_method :_mpfx_old_update, :update unless method_defined?(:_mpfx_old_update)
rescue
end
end
def self._mpfx_now
begin
Time.now.to_f
rescue
0.0
end
end
def self._mpfx_send(action_id, phase, payload)
return unless @connected
return if @my_id.nil?
send_line("deistvia:" + esc(action_id.to_s) + ":" + esc(phase.to_s) + ":" + esc(payload.to_s))
end
def self._mpfx_extract_char_anim_source(arg)
if arg.is_a?(Array)
frames = nil
loops = 0
begin
frames = arg[0]
rescue
frames = nil
end
begin
loops = arg[1].to_i
rescue
loops = 0
end
return [frames, loops]
end
if arg.respond_to?(:frames) && arg.respond_to?(:loops)
frames = nil
loops = 0
begin
frames = arg.frames
rescue
frames = nil
end
begin
loops = arg.loops.to_i
rescue
loops = 0
end
return [frames, loops]
end
nil
rescue
nil
end
def self._mpfx_char_anim_sig(arg)
src = _mpfx_extract_char_anim_source(arg)
return nil if src.nil?
frames = src[0]
loops = src[1].to_i
return nil unless frames.is_a?(Array)
encoded = []
count = 0
frames.each do |frame|
next unless frame.is_a?(Array)
cx = (frame[0] || 0).to_i
cy = (frame[1] || 0).to_i
dur = (frame[2] || 0).to_i
ox = (frame[3] || 0).to_i
oy = (frame[4] || 0).to_i
encoded << [cx, cy, dur, ox, oy].join(",")
count += 1
break if count >= MPFX_MAX_CHAR_FRAMES
end
return nil if encoded.empty?
loops.to_i.to_s + "|" + encoded.join(";")
rescue
nil
end
def self._mpfx_local_attack_like_animation?(player, arg)
return false if player.nil? || arg.nil?
arg_sig = _mpfx_char_anim_sig(arg)
return false if arg_sig.nil?
[:animation_atk_mh, :animation_atk_mh_long, :animation_atk_sh, :animation_atk_sh_long].each do |meth|
begin
next unless player.respond_to?(meth)
return true if arg_sig == _mpfx_char_anim_sig(player.send(meth))
rescue
end
end
false
end
def self._mpfx_char_loops(arg)
src = _mpfx_extract_char_anim_source(arg)
return 0 if src.nil?
src[1].to_i
rescue
0
end
def self._mpfx_pack_char_anim(arg, owner=nil)
src = _mpfx_extract_char_anim_source(arg)
return "" if src.nil?
frames = src[0]
loops = src[1].to_i
return "" unless frames.is_a?(Array)
encoded = []
count = 0
frames.each do |frame|
next unless frame.is_a?(Array)
cx = (frame[0] || 0).to_i
cy = (frame[1] || 0).to_i
dur = (frame[2] || 0).to_i
ox = (frame[3] || 0).to_i
oy = (frame[4] || 0).to_i
encoded << [cx, cy, dur, ox, oy].join(",")
count += 1
break if count >= MPFX_MAX_CHAR_FRAMES
end
return "" if encoded.empty?
angle = 0.0
zoom_x = 1.0
zoom_y = 1.0
mirror = false
reset_fx = false
if owner
begin
angle = owner.instance_variable_get(:@angle).to_f
rescue
angle = 0.0
end
begin
zoom_x = owner.instance_variable_get(:@zoom_x).to_f
rescue
zoom_x = 1.0
end
begin
zoom_y = owner.instance_variable_get(:@zoom_y).to_f
rescue
zoom_y = 1.0
end
begin
mirror = owner.instance_variable_get(:@mirror) ? true : false
rescue
mirror = false
end
begin
reset_fx = owner.instance_variable_get(:@charEffectReset) ? true : false
rescue
reset_fx = false
end
end
header = [
loops.to_i,
(angle * 100.0).round.to_i,
(zoom_x * 1000.0).round.to_i,
(zoom_y * 1000.0).round.to_i,
(mirror ? 1 : 0),
(reset_fx ? 1 : 0)
].join(",")
payload = header + "|" + encoded.join(";")
return "" if payload.bytesize > MPFX_MAX_PAYLOAD_BYTES
payload
end
def self._mpfx_unpack_char_anim(txt)
raw = txt.to_s
return nil if raw.empty?
parts = raw.split("|", 2)
return nil if parts.empty?
header = parts[0].to_s.split(",")
loops = header.size >= 1 ? header[0].to_i : 0
angle = header.size >= 2 ? header[1].to_i / 100.0 : 0.0
zoom_x = header.size >= 3 ? header[2].to_i / 1000.0 : 1.0
zoom_y = header.size >= 4 ? header[3].to_i / 1000.0 : 1.0
mirror = header.size >= 5 ? header[4].to_i != 0 : false
reset_fx = header.size >= 6 ? header[5].to_i != 0 : false
frames_raw = parts.size >= 2 ? parts[1].to_s : ""
return nil if frames_raw.empty?
frames = []
frames_raw.split(";").each do |chunk|
next if chunk.nil? || chunk.empty?
vals = chunk.split(",")
next if vals.size < 3
cx = vals[0].to_i
cy = vals[1].to_i
dur = vals[2].to_i
ox = vals.size >= 4 ? vals[3].to_i : 0
oy = vals.size >= 5 ? vals[4].to_i : 0
if ox == 0 && oy == 0
frames << [cx, cy, dur]
else
frames << [cx, cy, dur, ox, oy]
end
end
return nil if frames.empty?
{
:anim => [frames, loops],
:angle => angle,
:zoom_x => zoom_x,
:zoom_y => zoom_y,
:mirror => mirror,
:reset_fx => reset_fx
}
end
def self._mpfx_remote_player(player_id)
return nil if player_id.nil?
begin
create_remote_player(player_id) unless @players && @players[player_id]
rescue
end
return nil unless @players
@players[player_id]
end
def self._mpfx_remote_state(player_id)
@mpfx_remote_state ||= {}
@mpfx_remote_state[player_id] ||= {}
end
def self._mpfx_clear_local_char_state(send_stop=false)
begin
_mpfx_send("fx_anim", "stop", "") if send_stop && @mpfx_local_char_active
rescue
end
@mpfx_local_char_active = false
@mpfx_local_char_payload = nil
@mpfx_local_char_loop = 0
@mpfx_local_char_last_sent_at = 0.0
@mpfx_live_char_object_id = nil
end
def self.mpfx_on_local_charset_animation_assign(player, arg)
return unless @connected
return if @my_id.nil?
return if player.nil? || player != $game_player
if arg.nil?
_mpfx_clear_local_char_state(true)
return
end
return if _mpfx_local_attack_like_animation?(player, arg)
payload = _mpfx_pack_char_anim(arg, player)
return if payload.nil? || payload.empty?
now = _mpfx_now
prev = @mpfx_local_char_payload.to_s
if prev == payload && (now - @mpfx_local_char_last_sent_at.to_f) < MPFX_DEDUP_WINDOW
@mpfx_local_char_active = true
@mpfx_local_char_loop = _mpfx_char_loops(arg)
return
end
@mpfx_local_char_active = true
@mpfx_local_char_payload = payload
@mpfx_local_char_loop = _mpfx_char_loops(arg)
@mpfx_local_char_last_sent_at = now
phase = (prev == payload ? "sync" : "start")
_mpfx_send("fx_anim", phase, payload)
end
def self.mpfx_on_local_animation_id_assign(player, anim_id)
return unless @connected
return if @my_id.nil?
return if player.nil? || player != $game_player
aid = anim_id.to_i
return if aid <= 0
now = _mpfx_now
sig = aid.to_i.to_s
if @mpfx_last_db_sig.to_s == sig && (now - @mpfx_last_db_sent_at.to_f) < MPFX_DEDUP_WINDOW
return
end
@mpfx_last_db_sig = sig
@mpfx_last_db_sent_at = now
_mpfx_send("fx_db", "play", sig)
end
def self.mpfx_on_local_balloon(player, balloon_id, repeat_time=0)
return unless @connected
return if @my_id.nil?
bid = balloon_id.to_i
if bid <= 0
return unless player.nil? || player == $game_player
begin
if @mpfx_local_balloon_active
_mpfx_send("fx_balloon", "stop", "")
end
rescue
end
@mpfx_local_balloon_active = false
@mpfx_last_balloon_sig = nil
@mpfx_live_balloon_sig = nil
return
end
return if player.nil? || player != $game_player
payload = bid.to_i.to_s + "," + repeat_time.to_i.to_s
now = _mpfx_now
if @mpfx_last_balloon_sig.to_s == payload && (now - @mpfx_last_balloon_sent_at.to_f) < MPFX_DEDUP_WINDOW
return
end
@mpfx_last_balloon_sig = payload
@mpfx_last_balloon_sent_at = now
@mpfx_live_balloon_sig = payload
@mpfx_local_balloon_active = true
_mpfx_send("fx_balloon", "play", payload)
end
def self._mpfx_apply_remote_charanim(player_id, payload)
rp = _mpfx_remote_player(player_id)
return if rp.nil?
data = _mpfx_unpack_char_anim(payload)
return if data.nil?
st = _mpfx_remote_state(player_id)
if st[:char_active] && st[:char_payload].to_s == payload.to_s
return
end
begin
rp.instance_variable_set(:@angle, data[:angle].to_f)
rescue
end
begin
rp.instance_variable_set(:@zoom_x, data[:zoom_x].to_f)
rescue
end
begin
rp.instance_variable_set(:@zoom_y, data[:zoom_y].to_f)
rescue
end
begin
rp.instance_variable_set(:@mirror, data[:mirror] ? true : false)
rescue
end
begin
rp.instance_variable_set(:@charEffectReset, data[:reset_fx] ? true : false)
rescue
end
begin
rp.animation = data[:anim]
st[:char_active] = true
st[:char_payload] = payload.to_s
rescue
end
end
def self._mpfx_stop_remote_charanim(player_id)
rp = _mpfx_remote_player(player_id)
return if rp.nil?
begin
rp.animation = nil
rescue
end
st = _mpfx_remote_state(player_id)
st[:char_active] = false
st[:char_payload] = nil
end
def self._mpfx_apply_remote_dbanim(player_id, payload)
rp = _mpfx_remote_player(player_id)
return if rp.nil?
anim_id = payload.to_i
return if anim_id <= 0
begin
rp.animation_id = anim_id
rescue
end
end
def self._mpfx_apply_remote_balloon(player_id, payload)
rp = _mpfx_remote_player(player_id)
return if rp.nil?
parts = payload.to_s.split(",", 2)
balloon_id = parts[0].to_i
repeat_time = (parts.size >= 2 ? parts[1].to_i : 0)
return if balloon_id <= 0
begin
if rp.respond_to?(:call_balloon)
rp.call_balloon(balloon_id, repeat_time)
else
rp.balloon_id = balloon_id
end
rescue
begin
rp.balloon_id = balloon_id
rescue
end
end
end
def self._mpfx_stop_remote_balloon(player_id)
rp = _mpfx_remote_player(player_id)
return if rp.nil?
begin
rp.balloon_repeat_time = 0 if rp.respond_to?(:balloon_repeat_time=)
rescue
end
begin
rp.balloon_id = -1
rescue
begin
rp.instance_variable_set(:@balloon_id, -1)
rescue
end
end
end
def self._mpfx_handle_deistvia_effect(player_id, action_id, phase, payload)
act = action_id.to_s
return false unless act == "fx_anim" || act == "fx_db" || act == "fx_balloon"
return true if @my_id && player_id.to_i == @my_id.to_i
case act
when "fx_anim"
if phase.to_s == "stop"
_mpfx_stop_remote_charanim(player_id.to_i)
else
_mpfx_apply_remote_charanim(player_id.to_i, payload.to_s)
end
when "fx_db"
_mpfx_apply_remote_dbanim(player_id.to_i, payload.to_s)
when "fx_balloon"
if phase.to_s == "stop"
_mpfx_stop_remote_balloon(player_id.to_i)
else
_mpfx_apply_remote_balloon(player_id.to_i, payload.to_s)
end
end
true
end
def self._mpfx_tick_local_effects
return unless @connected
return if @my_id.nil?
player = nil
begin
player = $game_player
rescue
player = nil
end
if player.nil?
begin
mpfx_on_local_balloon(nil, 0, 0) if @mpfx_local_balloon_active
rescue
end
_mpfx_clear_local_char_state(false)
return
end
local_anim = nil
begin
local_anim = player.animation
rescue
local_anim = nil
end
if local_anim.nil?
_mpfx_clear_local_char_state(true)
else
live_char_object_id = nil
begin
live_char_object_id = local_anim.object_id
rescue
live_char_object_id = nil
end
unless _mpfx_local_attack_like_animation?(player, local_anim)
if !@mpfx_local_char_active || @mpfx_live_char_object_id.to_i != live_char_object_id.to_i
mpfx_on_local_charset_animation_assign(player, local_anim)
end
end
@mpfx_live_char_object_id = live_char_object_id
end
begin
live_anim_id = player.animation_id.to_i
if live_anim_id > 0
if @mpfx_live_db_anim_id.to_i != live_anim_id
mpfx_on_local_animation_id_assign(player, live_anim_id)
@mpfx_live_db_anim_id = live_anim_id
end
else
@mpfx_live_db_anim_id = 0
end
rescue
end
begin
live_balloon_id = player.balloon_id.to_i
live_balloon_repeat = player.balloon_repeat_time.to_i
if live_balloon_id > 0
sig = live_balloon_id.to_s + "," + live_balloon_repeat.to_s
if @mpfx_live_balloon_sig.to_s != sig || !@mpfx_local_balloon_active
mpfx_on_local_balloon(player, live_balloon_id, live_balloon_repeat)
end
elsif @mpfx_local_balloon_active
mpfx_on_local_balloon(player, 0, 0)
end
rescue
end
return unless @mpfx_local_char_active
return unless @mpfx_local_char_loop.to_i < 0
now = _mpfx_now
if (now - @mpfx_local_char_last_sent_at.to_f) >= MPFX_RESEND_LOOP_EVERY
payload = @mpfx_local_char_payload.to_s
if !payload.empty?
_mpfx_send("fx_anim", "sync", payload)
@mpfx_local_char_last_sent_at = now
end
end
end
def self.parse(msg)
begin
if msg.to_s =~ /^deistvia:(\d+):([^:]+):([^:]+):(.*)$/
player_id = $1.to_i
action_id = unesc($2.to_s)
phase = unesc($3.to_s)
payload = unesc($4.to_s)
return if _mpfx_handle_deistvia_effect(player_id, action_id, phase, payload)
end
rescue
end
_mpfx_old_parse(msg) if self.respond_to?(:_mpfx_old_parse)
end if self.respond_to?(:_mpfx_old_parse)
def self.update
_mpfx_old_update if self.respond_to?(:_mpfx_old_update)
begin
_mpfx_tick_local_effects
rescue
end
end if self.respond_to?(:_mpfx_old_update)
end
class Game_CharacterBase
if method_defined?(:animation_id=) && !method_defined?(:_mpfx_old_animation_id_set)
alias _mpfx_old_animation_id_set animation_id=
def animation_id=(arg)
begin
if defined?(WebSocket) && self == $game_player && WebSocket.respond_to?(:mpfx_on_local_animation_id_assign)
WebSocket.mpfx_on_local_animation_id_assign(self, arg)
end
rescue
end
_mpfx_old_animation_id_set(arg)
end
end
if method_defined?(:animation=) && !method_defined?(:_mpfx_old_animation_set)
alias _mpfx_old_animation_set animation=
def animation=(arg)
begin
if defined?(WebSocket) && self == $game_player && WebSocket.respond_to?(:mpfx_on_local_charset_animation_assign)
WebSocket.mpfx_on_local_charset_animation_assign(self, arg)
end
rescue
end
_mpfx_old_animation_set(arg)
end
end
end
class Game_Character
if method_defined?(:balloon_id=) && !method_defined?(:_mpfx_old_balloon_id_set)
alias _mpfx_old_balloon_id_set balloon_id=
def balloon_id=(id)
begin
if defined?(WebSocket) && self == $game_player && WebSocket.respond_to?(:mpfx_on_local_balloon)
WebSocket.mpfx_on_local_balloon(self, id, 0)
end
rescue
end
_mpfx_old_balloon_id_set(id)
end
end
if method_defined?(:call_balloon) && !method_defined?(:_mpfx_old_call_balloon)
alias _mpfx_old_call_balloon call_balloon
def call_balloon(id, time=0)
begin
if defined?(WebSocket) && self == $game_player && WebSocket.respond_to?(:mpfx_on_local_balloon)
WebSocket.mpfx_on_local_balloon(self, id, time)
end
rescue
end
_mpfx_old_call_balloon(id, time)
end
end
if method_defined?(:anim) && !method_defined?(:_mpfx_old_anim_shortcut)
alias _mpfx_old_anim_shortcut anim
def anim(id)
begin
if defined?(WebSocket) && self == $game_player && WebSocket.respond_to?(:mpfx_on_local_animation_id_assign)
WebSocket.mpfx_on_local_animation_id_assign(self, id)
end
rescue
end
_mpfx_old_anim_shortcut(id)
end
end
if method_defined?(:balloon) && !method_defined?(:_mpfx_old_balloon_shortcut)
alias _mpfx_old_balloon_shortcut balloon
def balloon(id)
begin
if defined?(WebSocket) && self == $game_player && WebSocket.respond_to?(:mpfx_on_local_balloon)
WebSocket.mpfx_on_local_balloon(self, id, 0)
end
rescue
end
_mpfx_old_balloon_shortcut(id)
end
end
end
if defined?(Sprite_Character)
class Sprite_Character < Sprite_Base
if method_defined?(:setup_new_effect) && !method_defined?(:_mpfx_old_setup_new_effect)
alias _mpfx_old_setup_new_effect setup_new_effect
def setup_new_effect
_mpfx_old_setup_new_effect
begin
if !animation? && @character && @character.animation_id.to_i > 0
animation = $data_animations[@character.animation_id.to_i] rescue nil
if animation
start_animation(animation)
@character.animation_id = 0 rescue @character.instance_variable_set(:@animation_id, 0)
end
end
rescue
end
end
end
end
end