Files
LonaRPG-Online-Games/ModScripts/multiplayer/26_npc_visuals.rb
T

689 lines
21 KiB
Ruby
Raw Normal View History

2026-10-29 21:59:06 +04:00
begin
require "json" unless defined?(JSON)
rescue LoadError, ScriptError
end
module WebSocket
def self.mp_npcfx_ready?
return false unless connected?
return false unless worldsync?
true
rescue
false
end
def self.mp_npcfx_event_ready?(ev)
return false unless mp_npcfx_ready?
return false unless ev
return false unless npc_event?(ev)
return true if npc_should_update_locally?(ev)
if map_master?
npc = (ev.respond_to?(:npc) ? ev.npc : nil) rescue nil
state = (npc.action_state rescue nil)
return true if state == :death
end
false
rescue
false
end
# Personal static map NPCs (quest boards, guild employees, town story NPCs)
# keep running locally for every client. Their balloon markers are personal
# progression UI and must not be world-synced, otherwise one player's taken
# quest looks taken for everyone on the map.
def self.mp_npcfx_personal_static_npc?(ev)
return false unless ev
eid = 0
begin
eid = ev.id.to_i if ev.respond_to?(:id)
rescue
eid = 0
end
return false if eid <= 0
return false unless respond_to?(:static_map_event_id?) && static_map_event_id?(eid)
return false if respond_to?(:explicit_net_event?) && explicit_net_event?(ev)
return false if respond_to?(:static_npc_sync_compatible?) && static_npc_sync_compatible?(eid, ev)
true
rescue
false
end
def self.mp_npcfx_balloon_world_sync_allowed?(ev)
!mp_npcfx_personal_static_npc?(ev)
rescue
true
end
def self.mp_npcfx_cache
@npc_fx_cache ||= {}
end
def self.mp_npcfx_cache_key(event_id, key)
"#{event_id.to_i}:#{key}"
end
def self.mp_npcfx_remote_visual_only?(ev)
return false unless connected?
return false unless worldsync?
return false unless ev
return false unless npc_event?(ev)
!npc_should_update_locally?(ev)
rescue
false
end
def self.mp_npcfx_send_cached(event_id, key, wire, force=false)
ck = mp_npcfx_cache_key(event_id, key)
return if !force && mp_npcfx_cache[ck] == wire
mp_npcfx_cache[ck] = wire
if respond_to?(:send_world_line)
send_world_line(wire)
else
send_line(wire)
end
rescue
end
def self.mp_npcfx_state_token(state)
txt = state.nil? ? "" : state.to_s.strip
return "nil" if txt == "" || txt == "idle"
txt
end
def self.mp_npcfx_parse_state_token(token)
txt = token.to_s.strip
return nil if txt == "" || txt == "nil" || txt == "idle"
txt.to_sym rescue nil
end
def self.mp_npcfx_anim_token(anim)
txt = anim.nil? ? "" : anim.to_s.strip
return "nil" if txt == ""
txt
end
def self.mp_npcfx_parse_anim_token(token)
txt = token.to_s.strip
return nil if txt == "" || txt == "nil"
txt.to_sym rescue nil
end
def self.mp_npcfx_balloon_token(balloon_id, repeat_time=0)
"#{balloon_id.to_i},#{repeat_time.to_i}"
end
def self.mp_npcfx_charanim_token(anim_name)
txt = anim_name.nil? ? "" : anim_name.to_s.strip
return "nil" if txt == ""
txt
end
def self.mp_npcfx_parse_charanim_token(token)
txt = token.to_s.strip
return nil if txt == "" || txt == "nil"
txt
end
def self.mp_npcfx_raw_charanim_token(anim_data)
return "nil" if anim_data.nil?
return nil unless anim_data.is_a?(Array)
JSON.generate(anim_data) rescue nil
end
def self.mp_npcfx_parse_raw_charanim_token(token)
txt = token.to_s.strip
return nil if txt == "" || txt == "nil"
data = JSON.parse(txt) rescue nil
data.is_a?(Array) ? data : nil
end
def self.mp_npcfx_parse_balloon_token(token)
parts = token.to_s.split(",", 2)
bid = (parts[0] || "0").to_i
rep = (parts[1] || "0").to_i
[bid, rep]
end
def self.mp_npcfx_send_state(ev, force=false)
return unless mp_npcfx_event_ready?(ev)
npc = (ev.respond_to?(:npc) ? ev.npc : nil) rescue nil
token = mp_npcfx_state_token(npc ? npc.action_state : nil)
mp_npcfx_send_cached(ev.id, "state", "event_state:#{ev.id}:state:#{token}", force)
end
def self.mp_npcfx_send_current_animation(ev, force=false)
return unless mp_npcfx_event_ready?(ev)
npc = (ev.respond_to?(:npc) ? ev.npc : nil) rescue nil
token = mp_npcfx_anim_token(npc ? npc.current_animation : nil)
mp_npcfx_send_cached(ev.id, "curanim", "event_state:#{ev.id}:curanim:#{token}", force)
end
def self.mp_npcfx_send_balloon(ev, balloon_id=nil, repeat_time=nil, force=false)
return unless mp_npcfx_event_ready?(ev)
return unless mp_npcfx_balloon_world_sync_allowed?(ev)
bid = balloon_id
rep = repeat_time
bid = (ev.instance_variable_get(:@balloon_id) rescue 0) if bid.nil?
rep = (ev.instance_variable_get(:@balloon_repeat_time) rescue 0) if rep.nil?
bid = bid.to_i
rep = rep.to_i
ck = mp_npcfx_cache_key(ev.id, "balloon")
return if bid <= 0 && !force && mp_npcfx_cache[ck].nil?
token = mp_npcfx_balloon_token(bid, rep)
mp_npcfx_send_cached(ev.id, "balloon", "event_state:#{ev.id}:balloon:#{token}", force)
end
def self.mp_npcfx_send_anim_id(ev, anim_id=nil, force=false)
return unless mp_npcfx_event_ready?(ev)
aid = anim_id
aid = (ev.animation_id rescue 0) if aid.nil?
aid = aid.to_i
ck = mp_npcfx_cache_key(ev.id, "anim_id")
return if aid <= 0 && !force && mp_npcfx_cache[ck].nil?
mp_npcfx_send_cached(ev.id, "anim_id", "event_state:#{ev.id}:anim_id:#{aid}", force)
end
def self.mp_npcfx_send_charanim(ev, anim_name=nil, force=false)
return unless mp_npcfx_event_ready?(ev)
token = mp_npcfx_charanim_token(anim_name)
mp_npcfx_send_cached(ev.id, "charanim", "event_state:#{ev.id}:charanim:#{token}", force)
end
def self.mp_npcfx_should_send_raw_charanim?(ev, anim_data)
return false unless mp_npcfx_event_ready?(ev)
return false unless anim_data.is_a?(Array)
return false if ev.instance_variable_get(:@mp_npcfx_skip_raw_charanim)
npc = (ev.respond_to?(:npc) ? ev.npc : nil) rescue nil
if npc
action_state = (npc.action_state rescue nil)
current_anim = (npc.current_animation rescue nil)
return false if action_state == :skill && !current_anim.nil?
end
true
rescue
false
end
def self.mp_npcfx_send_raw_charanim(ev, anim_data=nil, force=false)
return unless mp_npcfx_event_ready?(ev)
token = mp_npcfx_raw_charanim_token(anim_data)
return if token.nil?
mp_npcfx_send_cached(ev.id, "charanim_raw", "event_state:#{ev.id}:charanim_raw:#{token}", force)
end
def self.mp_npcfx_track_event(ev, force=false)
return unless mp_npcfx_event_ready?(ev)
mp_npcfx_send_current_animation(ev, force)
mp_npcfx_send_state(ev, force)
mp_npcfx_send_balloon(ev, nil, nil, force)
mp_npcfx_send_anim_id(ev, nil, force)
end
def self.mp_npcfx_find_event(server_event_id)
ev = get_map_event(server_event_id)
if ev.nil? && server_event_id >= SERVER_DYNAMIC_EVENT_ID_BASE
create_remote_event(server_event_id, 0, 0, 0, "NetEvent_#{server_event_id}")
ev = get_map_event(server_event_id)
end
ev
rescue
nil
end
def self.mp_npcfx_apply_remote_state(server_event_id, value)
ev = mp_npcfx_find_event(server_event_id)
return false unless ev
state = mp_npcfx_parse_state_token(value)
ev.instance_variable_set(:@net_state, state.nil? ? "" : state.to_s) rescue nil
ev.instance_variable_set(:@mp_npcfx_remote_state, state) rescue nil
npc = (ev.respond_to?(:npc) ? ev.npc : nil) rescue nil
return true if npc.nil?
if mp_npcfx_remote_visual_only?(ev) && state == :skill
begin
npc.instance_variable_set(:@action_state, state)
npc.instance_variable_set(:@action_state_changed, false)
rescue
end
return true
end
cur = (npc.action_state rescue nil)
return true if cur == state
begin
npc.set_action_state(state, true)
rescue
begin
npc.instance_variable_set(:@action_state, state)
npc.instance_variable_set(:@action_state_changed, true)
rescue
end
end
begin
ev.setup_npc_effect if ev.respond_to?(:setup_npc_effect)
rescue
end
true
end
def self.mp_npcfx_apply_remote_current_animation(server_event_id, value)
ev = mp_npcfx_find_event(server_event_id)
return false unless ev
anim = mp_npcfx_parse_anim_token(value)
ev.instance_variable_set(:@net_current_animation, anim.nil? ? "" : anim.to_s) rescue nil
npc = (ev.respond_to?(:npc) ? ev.npc : nil) rescue nil
return true if npc.nil?
begin
npc.current_animation = anim
rescue
begin
npc.instance_variable_set(:@current_animation, anim)
rescue
end
end
begin
if mp_npcfx_remote_visual_only?(ev) && anim
ev.animation = ev.send(anim) rescue nil
elsif (npc.action_state rescue nil) == :skill && anim && ev.respond_to?(:setup_skill_effect)
ev.setup_skill_effect
end
rescue
end
true
end
def self.mp_npcfx_apply_remote_balloon(server_event_id, value)
ev = mp_npcfx_find_event(server_event_id)
return false unless ev
# Drop personal quest/town NPC balloons from older clients that still send them.
return false if mp_npcfx_personal_static_npc?(ev)
bid, rep = mp_npcfx_parse_balloon_token(value)
begin
# Apply without the networked call_balloon / balloon_id= hooks so a peer
# packet cannot be rebroadcast as our own event_state.
if ev.respond_to?(:mp_npcfx_old_call_balloon)
ev.mp_npcfx_old_call_balloon(bid, rep)
else
ev.instance_variable_set(:@balloon_id, bid.to_i)
ev.instance_variable_set(:@balloon_frame_speed, 4)
ev.instance_variable_set(:@balloon_frame_wait, 12)
ev.instance_variable_set(:@balloon_repeat_time, rep.to_i)
end
rescue
end
true
end
def self.mp_npcfx_apply_remote_charanim(server_event_id, value)
ev = mp_npcfx_find_event(server_event_id)
return false unless ev
anim_name = mp_npcfx_parse_charanim_token(value)
ev.instance_variable_set(:@mp_npcfx_last_charanim, anim_name.to_s) rescue nil
return true if anim_name.nil?
begin
if ev.respond_to?(:set_animation)
ev.set_animation(anim_name)
else
ev.animation = ev.send(anim_name)
end
rescue
end
true
end
def self.mp_npcfx_apply_remote_raw_charanim(server_event_id, value)
ev = mp_npcfx_find_event(server_event_id)
return false unless ev
anim_data = mp_npcfx_parse_raw_charanim_token(value)
return true if anim_data.nil?
begin
ev.animation = anim_data
rescue
end
true
end
def self.mp_npcfx_target_xy(target)
ch = target
ch = target[0] if target.is_a?(Array)
x = 0
y = 0
begin
x = ch.x.to_i unless ch.nil?
rescue
x = 0
end
begin
y = ch.y.to_i unless ch.nil?
rescue
y = 0
end
[x, y]
end
def self.mp_npcfx_send_hit_visual(target)
return unless connected?
return unless worldsync?
x, y = mp_npcfx_target_xy(target)
send_line("effect_area:npc_hit:#{x}:#{y}:0:")
rescue
end
class << self
unless method_defined?(:mp_npcfx_old_apply_event_state)
alias_method :mp_npcfx_old_apply_event_state, :apply_event_state
def apply_event_state(server_event_id, stype, value)
if respond_to?(:incoming_event_state_allowed?) && !incoming_event_state_allowed?(server_event_id, stype)
return
end
case stype.to_s
when "state"
return if mp_npcfx_apply_remote_state(server_event_id, value)
when "curanim"
return if mp_npcfx_apply_remote_current_animation(server_event_id, value)
when "balloon"
return if mp_npcfx_apply_remote_balloon(server_event_id, value)
when "charanim"
return if mp_npcfx_apply_remote_charanim(server_event_id, value)
when "charanim_raw"
return if mp_npcfx_apply_remote_raw_charanim(server_event_id, value)
end
mp_npcfx_old_apply_event_state(server_event_id, stype, value)
end
end
unless method_defined?(:mp_npcfx_old_net_sync_event_send)
alias_method :mp_npcfx_old_net_sync_event_send, :net_sync_event_send
def net_sync_event_send(eid, ev, full=false, force_ai=false)
mp_npcfx_old_net_sync_event_send(eid, ev, full, force_ai)
return unless mp_npcfx_event_ready?(ev)
mp_npcfx_track_event(ev, full)
rescue
end
end
end
end
class Game_NonPlayerCharacter
if method_defined?(:set_action_state) && !method_defined?(:mp_npcfx_old_set_action_state)
alias mp_npcfx_old_set_action_state set_action_state
def set_action_state(action_state, force=false)
prev = (@action_state rescue nil)
mp_npcfx_old_set_action_state(action_state, force)
begin
ev = map_token if respond_to?(:map_token)
return unless defined?(WebSocket) && WebSocket.respond_to?(:mp_npcfx_event_ready?) && WebSocket.mp_npcfx_event_ready?(ev)
return if !force && prev == (@action_state rescue nil)
WebSocket.mp_npcfx_send_current_animation(ev)
WebSocket.mp_npcfx_send_state(ev)
rescue
end
end
end
if method_defined?(:current_animation=) && !method_defined?(:mp_npcfx_old_current_animation_set)
alias mp_npcfx_old_current_animation_set current_animation=
def current_animation=(arg)
prev = (@current_animation rescue nil)
mp_npcfx_old_current_animation_set(arg)
begin
ev = map_token if respond_to?(:map_token)
return unless defined?(WebSocket) && WebSocket.respond_to?(:mp_npcfx_event_ready?) && WebSocket.mp_npcfx_event_ready?(ev)
return if prev == (@current_animation rescue nil)
WebSocket.mp_npcfx_send_current_animation(ev)
rescue
end
end
end
end
class Game_Event < Game_Character
if method_defined?(:set_animation) && !method_defined?(:mp_npcfx_old_set_animation)
alias mp_npcfx_old_set_animation set_animation
def set_animation(animation_name)
begin
if defined?(WebSocket) && WebSocket.respond_to?(:mp_npcfx_event_ready?) &&
WebSocket.mp_npcfx_event_ready?(self)
WebSocket.mp_npcfx_send_charanim(self, animation_name)
end
rescue
end
begin
@mp_npcfx_skip_raw_charanim = true
mp_npcfx_old_set_animation(animation_name)
ensure
@mp_npcfx_skip_raw_charanim = false
end
end
end
if method_defined?(:animation=) && !method_defined?(:mp_npcfx_old_animation_set)
alias mp_npcfx_old_animation_set animation=
def animation=(arg)
begin
if defined?(WebSocket) && WebSocket.respond_to?(:mp_npcfx_should_send_raw_charanim?) &&
WebSocket.mp_npcfx_should_send_raw_charanim?(self, arg)
WebSocket.mp_npcfx_send_raw_charanim(self, arg)
WebSocket.mp_npcfx_send_state(self)
end
rescue
end
mp_npcfx_old_animation_set(arg)
end
end
if method_defined?(:update_npc_skill) && !method_defined?(:mp_npcfx_old_update_npc_skill)
alias mp_npcfx_old_update_npc_skill update_npc_skill
def update_npc_skill
begin
npc_obj = (respond_to?(:npc) ? npc : nil) rescue nil
if npc_obj
skill_obj = (npc_obj.skill rescue nil)
state_obj = (npc_obj.action_state rescue nil)
if state_obj == :skill && skill_obj.nil?
begin
if npc_obj.respond_to?(:reset_skill)
npc_obj.reset_skill(true)
else
npc_obj.set_action_state(:none, true) if npc_obj.respond_to?(:set_action_state)
end
rescue
begin
npc_obj.instance_variable_set(:@skill, nil)
npc_obj.instance_variable_set(:@skill_wait, 0)
npc_obj.instance_variable_set(:@holding_count, -1)
npc_obj.instance_variable_set(:@blocked, false)
npc_obj.instance_variable_set(:@skill_charging_state, 0)
npc_obj.instance_variable_set(:@current_skill_launched, false)
npc_obj.instance_variable_set(:@action_state, :none)
npc_obj.instance_variable_set(:@action_state_changed, true)
rescue
end
end
begin
if defined?(WebSocket) &&
WebSocket.respond_to?(:mp_npcfx_event_ready?) &&
WebSocket.mp_npcfx_event_ready?(self)
WebSocket.mp_npcfx_send_current_animation(self, true)
WebSocket.mp_npcfx_send_state(self, true)
end
rescue
end
return
end
end
if defined?(WebSocket) && WebSocket.respond_to?(:mp_npcfx_remote_visual_only?) &&
WebSocket.mp_npcfx_remote_visual_only?(self)
return if npc_obj && (npc_obj.skill.nil? rescue true)
end
rescue
end
mp_npcfx_old_update_npc_skill
end
end
if method_defined?(:balloon_id=) && !method_defined?(:mp_npcfx_old_balloon_id_set)
alias mp_npcfx_old_balloon_id_set balloon_id=
def balloon_id=(id)
begin
if defined?(WebSocket) && WebSocket.respond_to?(:mp_npcfx_event_ready?) && WebSocket.mp_npcfx_event_ready?(self)
WebSocket.mp_npcfx_send_balloon(self, id, 0)
end
rescue
end
mp_npcfx_old_balloon_id_set(id)
end
end
if method_defined?(:call_balloon) && !method_defined?(:mp_npcfx_old_call_balloon)
alias mp_npcfx_old_call_balloon call_balloon
def call_balloon(id, time=0)
begin
if defined?(WebSocket) && WebSocket.respond_to?(:mp_npcfx_event_ready?) && WebSocket.mp_npcfx_event_ready?(self)
WebSocket.mp_npcfx_send_balloon(self, id, time)
end
rescue
end
mp_npcfx_old_call_balloon(id, time)
end
end
if method_defined?(:animation_id=) && !method_defined?(:mp_npcfx_old_animation_id_set)
alias mp_npcfx_old_animation_id_set animation_id=
def animation_id=(id)
begin
if defined?(WebSocket) && WebSocket.respond_to?(:mp_npcfx_event_ready?) && WebSocket.mp_npcfx_event_ready?(self)
WebSocket.mp_npcfx_send_anim_id(self, id)
end
rescue
end
mp_npcfx_old_animation_id_set(id)
end
end
end
module WebSocket
class << self
if method_defined?(:show_area_effect) && !method_defined?(:mp_npcfx_old_show_area_effect)
alias_method :mp_npcfx_old_show_area_effect, :show_area_effect
def show_area_effect(eff_type, x, y, radius, params)
if eff_type.to_s == "npc_hit"
show_animation_on_xy(12, x, y) rescue nil
return
end
mp_npcfx_old_show_area_effect(eff_type, x, y, radius, params)
end
end
end
end
module Battle_System
unless instance_methods.include?(:mp_npcfx_old_process_skill_result)
alias mp_npcfx_old_process_skill_result process_skill_result
def process_skill_result(skill)
had_launched = false
begin
had_launched = !!@current_skill_launched
rescue
end
result = mp_npcfx_old_process_skill_result(skill)
begin
mt = map_token
return result unless defined?(WebSocket) && WebSocket.respond_to?(:mp_npcfx_event_ready?)
return result unless WebSocket.mp_npcfx_event_ready?(mt)
return result if had_launched
return result unless @current_skill_launched
return result if skill.nil?
return result unless defined?(WebSocket) && WebSocket.respond_to?(:send_skill_cast_visual)
if skill.respond_to?(:is_projectile?) && skill.is_projectile?
tgt = nil
begin
if skill.respond_to?(:is_support) && skill.is_support
tgt = target
else
tgt = mt.get_fake_target
end
rescue
tgt = nil
end
WebSocket.send_skill_cast_visual(skill, tgt) if tgt
else
begin
targets = skill.hit_detection.scan(mt)
if targets && !targets.empty?
targets.each do |tgt|
next if tgt.nil?
WebSocket.send_skill_cast_visual(skill, tgt)
end
else
WebSocket.send_skill_cast_visual(skill, target) if target
end
rescue
WebSocket.send_skill_cast_visual(skill, target) if target rescue nil
end
end
rescue
end
result
end
end
unless instance_methods.include?(:mp_npcfx_old_summon_skill_hit_efx)
alias mp_npcfx_old_summon_skill_hit_efx summon_skill_hit_efx
def summon_skill_hit_efx(skill, target, damage=0)
begin
mt = map_token
if defined?(WebSocket) && WebSocket.respond_to?(:mp_npcfx_event_ready?) &&
WebSocket.mp_npcfx_event_ready?(mt)
WebSocket.mp_npcfx_send_hit_visual(target)
end
rescue
end
mp_npcfx_old_summon_skill_hit_efx(skill, target, damage)
end
end
end