module WebSocket # WORLD: Remote events/NPC #-------------------------------------------------------------------------- # ID-пространства: # 1..19999 — локальные map/summon id мастера (часто summon-боссы плагина) # 20000+ — drops # 40000+ — локальные id remote stand-in на ЭТОМ клиенте # 50000+ — server-dynamic id # Summon с id < 20000 раньше ошибочно считался «static map event» и не # получал remote host → невидимость + блок event_move. Теперь для # несовместимых/отсутствующих static id создаём stand-in в @remote_events. #-------------------------------------------------------------------------- def self.remote_local_event_id(server_event_id) eid = server_event_id.to_i if eid >= SERVER_DYNAMIC_EVENT_ID_BASE return REMOTE_EVENT_LOCAL_ID_BASE + (eid - SERVER_DYNAMIC_EVENT_ID_BASE) end # Stand-in для master-local summon id: 45000..64999 REMOTE_EVENT_LOCAL_ID_BASE + 5000 + (eid % 15000) end def self.remote_event_host?(server_event_id) @remote_events ||= {} @remote_events.has_key?(server_event_id.to_i) rescue false end def self.get_map_event(server_event_id) eid = server_event_id.to_i @remote_events ||= {} # Remote stand-in имеет приоритет: иначе event_move уходит в чужой # локальный map-event с тем же числовым id. return @remote_events[eid] if @remote_events.has_key?(eid) return nil unless $game_map && $game_map.events $game_map.events[eid] end # Графика remote-NPC: сначала шаблон из $data_EventLib (плагинный/summon # босс), затем $chs_data / эвристики по типу. Пустой character_name = невидимый. def self.remote_npc_graphic_from_event_lib(npc_type) token = npc_type.to_s return nil if token == "" return nil unless defined?($data_EventLib) && $data_EventLib candidates = [token] begin candidates << token.sub(/\ANpc_/i, "") if token =~ /\ANpc_/i candidates << ("Npc_" + token) unless token =~ /\ANpc_/i rescue end candidates.uniq.each do |name| begin entry = $data_EventLib[name] next if entry.nil? rpg_ev = entry.is_a?(Array) ? entry[1] : entry next if rpg_ev.nil? || !rpg_ev.respond_to?(:pages) pages = rpg_ev.pages next if pages.nil? || pages.empty? pages.each do |pg| next if pg.nil? || !pg.respond_to?(:graphic) g = pg.graphic next if g.nil? gname = g.character_name.to_s next if gname == "" return [gname, g.character_index.to_i] end rescue end end nil rescue nil end def self.remote_npc_graphic_for_type(npc_type) token = npc_type.to_s return nil if token.nil? || token.strip == "" lib = remote_npc_graphic_from_event_lib(token) return lib[0] if lib.is_a?(Array) && !lib[0].to_s.empty? begin if defined?($chs_data) && $chs_data return token if $chs_data[token] soft = token.sub(/\ANpc_/i, "") return soft if soft != "" && $chs_data[soft] end rescue end case token when /BossMama|UniqueBossMama/i "-char-F-TEEN01" when /WarBoss|Warboss|OrkindWarBoss|OgreWarBoss/i "-char-M-ORC01" when /HobgoblinRaider/i "-char-M-HOBGOBLIN-RD01" when /Hobgoblin/i "-char-M-HOBGOBLIN01" when /Goblin/i "-char-M-GOBLIN01" when /Orc|Ogre/i "-char-M-ORC01" when /Undead/i "-char-M-REG_UNDEAD01" when /Fishkind|Deepone|FistMaster/i "-char-M-FISHKIND01" when /Abom/i "-char-AbomSmC01" when /Dog|Wolf/i "-char-CreaturesDOG76M" when /Fat|Butcher|Executioner/i "-char-M-FAT01" when /Boss/i "-char-M-TEEN01" when /Human|GangHuman|MobHuman|SaintHuman/i "-char-M-TEEN01" else nil end rescue nil end # Применить явную графику с мастера (пакет npc_gfx). Надёжнее EventLib. def self.apply_npc_gfx(server_event_id, graphic_name, graphic_index=0) eid = server_event_id.to_i name = graphic_name.to_s return if name == "" ev = resolve_remote_npc_event(eid, 0, 0, "NetSummon_#{eid}") return unless ev ev.instance_variable_set(:@net_forced_graphic, name) ev.instance_variable_set(:@net_forced_graphic_index, graphic_index.to_i) apply_character_graphic_to_event(ev, name, graphic_index.to_i) rescue end def self.apply_character_graphic_to_event(ev, graphic_name, graphic_index=0) return unless ev && ev.respond_to?(:event) graphic_name = graphic_name.to_s return if graphic_name == "" page = nil begin page = ev.event.pages[0] if ev.event && ev.event.pages && ev.event.pages[0] rescue page = nil end return if page.nil? is_chs = graphic_name.include?("-char-") begin page.graphic.character_name = graphic_name page.graphic.character_index = graphic_index.to_i page.priority_type = 1 page.walk_anime = true page.step_anime = true page.direction_fix = false page.through = false rescue end begin ev.instance_variable_set(:@page, page) # ВАЖНО: manual_character_name="" НЕ nil — setup_page_settings берёт "" # вместо page.graphic и спрайт остаётся пустым. ev.instance_variable_set(:@manual_character_name, nil) ev.instance_variable_set(:@manual_page_index, nil) ev.instance_variable_set(:@manual_character_index, nil) ev.instance_variable_set(:@manual_CHS_build, false) ev.instance_variable_set(:@erased, false) rescue nil ev.instance_variable_set(:@opacity, 255) rescue nil ev.opacity = 255 if ev.respond_to?(:opacity=) ev.transparent = false if ev.respond_to?(:transparent=) if is_chs ev.instance_variable_set(:@chs_type, graphic_name) # character_name для CHS — сам -char- токен (как у ванильного setup) ev.instance_variable_set(:@character_name, graphic_name) begin map_id = ($game_map.map_id rescue 0).to_i cache_key = "NetNpcM" + map_id.to_s + "E" + ev.id.to_i.to_s + "T" + graphic_name.to_s ev.instance_variable_set(:@chs_name, cache_key) ev.instance_variable_set(:@chs_name_h, cache_key + "_H") rescue end begin ev.set_char_type(graphic_name, false) if ev.respond_to?(:set_char_type) rescue end begin idx = (ev.instance_variable_get(:@charset_index) rescue 0).to_i ev.create_chs_configuration(idx) if ev.respond_to?(:create_chs_configuration) rescue if !(ev.instance_variable_get(:@chs_configuration) rescue nil).is_a?(Hash) ev.instance_variable_set(:@chs_configuration, { "sex_pos" => -1, "sex_position" => -1 }) end end else ev.instance_variable_set(:@chs_configuration, nil) ev.instance_variable_set(:@chs_type, nil) ev.instance_variable_set(:@character_name, graphic_name) ev.instance_variable_set(:@manual_character_name, graphic_name) ev.instance_variable_set(:@character_index, graphic_index.to_i) end ev.instance_variable_set(:@chs_need_update, true) begin # Не полный refresh: он может пересобрать page и сбросить graphic. if ev.respond_to?(:setup_page_settings) && !is_chs ev.setup_page_settings elsif is_chs && ev.respond_to?(:setup_charset_page_settings) # Уже выставили page.graphic — charset setup подхватит -char- ev.instance_variable_set(:@manual_CHS_build, true) ev.setup_page_settings if ev.respond_to?(:setup_page_settings) ev.instance_variable_set(:@manual_CHS_build, false) end rescue end ev.instance_variable_set(:@character_name, graphic_name) if is_chs ev.instance_variable_set(:@chs_need_update, true) ev.transparent = false if ev.respond_to?(:transparent=) ev.instance_variable_set(:@opacity, 255) rescue nil begin sc = respond_to?(:scene_with_map_spriteset) ? scene_with_map_spriteset : nil if sc.nil? && defined?(SceneManager) && SceneManager.scene_is?(Scene_Map) sc = SceneManager.scene end sc.add_net_sprite(ev) if sc && sc.respond_to?(:add_net_sprite) rescue end rescue end end def self.apply_remote_npc_graphic(ev, npc_type) return unless ev # Уже прислали npc_gfx с мастера — не затираем. begin forced = ev.instance_variable_get(:@net_forced_graphic).to_s if forced != "" apply_character_graphic_to_event(ev, forced, (ev.instance_variable_get(:@net_forced_graphic_index) rescue 0).to_i) return end rescue end graphic_name = nil graphic_index = 0 lib = remote_npc_graphic_from_event_lib(npc_type) if lib.is_a?(Array) graphic_name = lib[0].to_s graphic_index = lib[1].to_i end if graphic_name.nil? || graphic_name == "" graphic_name = remote_npc_graphic_for_type(npc_type).to_s graphic_index = 0 end # Последний видимый fallback — иначе hitbox без спрайта. if graphic_name.nil? || graphic_name == "" graphic_name = "!$_LonaSample.png" graphic_index = 0 end apply_character_graphic_to_event(ev, graphic_name, graphic_index) end # Создать/вернуть remote host для любого server event id (в т.ч. summon < 20000). def self.ensure_remote_npc_host(server_event_id, x=0, y=0, name=nil) eid = server_event_id.to_i return nil if eid <= 0 @remote_events ||= {} return @remote_events[eid] if @remote_events[eid] create_remote_event(eid, 0, x.to_i, y.to_i, name.to_s) @remote_events[eid] rescue nil end def self.create_remote_event(server_event_id, template_id, x, y, name="") eid = server_event_id.to_i return if eid <= 0 @remote_events ||= {} return if @remote_events[eid] return unless $game_map local_id = remote_local_event_id(eid) # Разрешить коллизии локальных id begin while $game_map.events && $game_map.events[local_id] local_id += 1 break if local_id > REMOTE_EVENT_LOCAL_ID_BASE + 19999 end rescue end dummy = RPG::Event.new(0, 0) dummy.id = local_id dummy.name = (name.nil? || name.empty?) ? "NetEvent_#{eid}" : name.to_s page = RPG::Event::Page.new page.trigger = 0 page.priority_type = 1 page.move_type = 0 page.walk_anime = true page.step_anime = true page.direction_fix = false page.through = false page.graphic.character_name = "" page.graphic.character_index = 0 page.list = [RPG::EventCommand.new(0, 0, [])] dummy.pages = [page] ev = Game_Event.new($game_map.map_id, dummy) ev.moveto(x, y) ev.transparent = false ev.instance_variable_set(:@opacity, 255) rescue nil ev.instance_variable_set(:@net_server_event_id, eid) ev.instance_variable_set(:@net_template_id, template_id) ev.instance_variable_set(:@net_hp, 0) ev.instance_variable_set(:@net_max_hp, 0) ev.instance_variable_set(:@net_state, "idle") ev.instance_variable_set(:@net_summon_standin, eid < SERVER_DYNAMIC_EVENT_ID_BASE) $game_map.events[local_id] = ev @remote_events[eid] = ev sc = respond_to?(:scene_with_map_spriteset) ? scene_with_map_spriteset : nil if sc.nil? && SceneManager.scene_is?(Scene_Map) sc = SceneManager.scene end sc.add_net_sprite(ev) if sc && sc.respond_to?(:add_net_sprite) puts "[EV] created remote event server=#{eid} local=#{local_id} template=#{template_id} x=#{x} y=#{y} standin=#{eid < SERVER_DYNAMIC_EVENT_ID_BASE}" end def self.remote_event_dead?(ev) return false unless ev return true if (ev.instance_variable_get(:@net_dead) rescue false) hp = ev.instance_variable_get(:@net_hp) rescue nil st = ev.instance_variable_get(:@net_state) rescue nil st.to_s == "death" && !hp.nil? && hp.to_i <= 0 rescue false end def self.apply_remote_death_visual(ev) return unless ev ev.instance_variable_set(:@net_dead, true) rescue nil begin n = ev.npc if ev.respond_to?(:npc) if n && n.respond_to?(:process_death) return if n.respond_to?(:action_state) && n.action_state == :death $mp_ws_remote_death_visual = true begin n.process_death ensure $mp_ws_remote_death_visual = false end end rescue $mp_ws_remote_death_visual = false end end def self.resolve_remote_npc_event(server_event_id, x=0, y=0, create_name=nil) eid = server_event_id.to_i @remote_events ||= {} return @remote_events[eid] if @remote_events[eid] local = nil begin local = $game_map.events[eid] if $game_map && $game_map.events rescue local = nil end # Настоящий shared static NPC (тот же тип на карте) — используем его. if local && static_map_event_id?(eid) && static_npc_sync_compatible?(eid, local) return local end # Summon id мастера / несовместимый static / отсутствует — stand-in. ensure_remote_npc_host(eid, x, y, create_name || "NetSummon_#{eid}") rescue nil end def self.move_remote_event(server_event_id, x, y, dir, pattern) eid = server_event_id.to_i if respond_to?(:incoming_event_move_allowed?) && !incoming_event_move_allowed?(eid) return end ev = resolve_remote_npc_event(eid, x, y, "NetEvent_#{eid}") return unless ev return if remote_event_dead?(ev) ev.moveto(x, y) rescue nil ev.set_direction(dir) rescue nil ev.instance_variable_set(:@pattern, pattern) rescue nil ev.transparent = false rescue nil ev.instance_variable_set(:@opacity, 255) rescue nil begin cname = (ev.instance_variable_get(:@character_name) rescue "").to_s ntype = (ev.instance_variable_get(:@net_npc_type) rescue "").to_s forced = (ev.instance_variable_get(:@net_forced_graphic) rescue "").to_s if cname == "" if forced != "" apply_character_graphic_to_event(ev, forced, (ev.instance_variable_get(:@net_forced_graphic_index) rescue 0).to_i) elsif ntype != "" apply_remote_npc_graphic(ev, ntype) end end rescue end end def self.apply_remote_event_page(ev, page_idx) return false unless ev && page_idx.to_i > 0 return false unless ev.respond_to?(:event) && ev.event && ev.event.pages page = ev.event.pages[page_idx.to_i - 1] rescue nil return false unless page ev.instance_variable_set(:@page_index, page_idx.to_i) rescue nil ev.instance_variable_set(:@page, page) rescue nil begin g = page.graphic if g ev.instance_variable_set(:@tile_id, g.tile_id.to_i) rescue nil ev.instance_variable_set(:@character_name, g.character_name.to_s) rescue nil ev.instance_variable_set(:@character_index, g.character_index.to_i) rescue nil ev.instance_variable_set(:@direction, g.direction.to_i) rescue nil if g.respond_to?(:direction) ev.instance_variable_set(:@pattern, g.pattern.to_i) rescue nil if g.respond_to?(:pattern) end rescue end begin ev.instance_variable_set(:@move_type, page.move_type) rescue nil ev.instance_variable_set(:@move_speed, page.move_speed) rescue nil ev.instance_variable_set(:@move_frequency, page.move_frequency) rescue nil ev.instance_variable_set(:@move_route, page.move_route) rescue nil ev.instance_variable_set(:@move_route_index, 0) rescue nil ev.instance_variable_set(:@move_route_forcing, false) rescue nil ev.instance_variable_set(:@walk_anime, page.walk_anime) rescue nil ev.instance_variable_set(:@step_anime, page.step_anime) rescue nil ev.instance_variable_set(:@direction_fix, page.direction_fix) rescue nil ev.instance_variable_set(:@through, page.through) rescue nil ev.instance_variable_set(:@priority_type, page.priority_type) rescue nil ev.instance_variable_set(:@trigger, page.trigger) rescue nil ev.instance_variable_set(:@list, page.list) rescue nil ev.instance_variable_set(:@interpreter, nil) rescue nil rescue end true rescue false end def self.apply_event_state(server_event_id, stype, value) eid = server_event_id.to_i if respond_to?(:incoming_event_state_allowed?) && !incoming_event_state_allowed?(eid, stype) return end # hp/state/target для summon-босса должны попадать в stand-in, не в дверь. if %w[hp max_hp state target dead anim_id].include?(stype.to_s) || remote_event_host?(eid) ev = resolve_remote_npc_event(eid, 0, 0, "NetEvent_#{eid}") else ev = get_map_event(eid) if ev.nil? ev = resolve_remote_npc_event(eid, 0, 0, "NetEvent_#{eid}") end end return unless ev case stype when "hp" hpv = value.to_i ev.instance_variable_set(:@net_hp, hpv) rescue nil if hpv <= 0 apply_remote_death_visual(ev) else ev.instance_variable_set(:@net_dead, false) rescue nil ev.remove_instance_variable(:@mp_ws_death_drop_done) rescue nil end # apply to real NPC if present (health is authoritative from master) begin if ev.respond_to?(:npc) && (n = ev.npc) && n.respond_to?(:health=) n.health = hpv end rescue end when "max_hp" mhv = value.to_i ev.instance_variable_set(:@net_max_hp, mhv) rescue nil begin if ev.respond_to?(:npc) && (n = ev.npc) && n.respond_to?(:battle_stat) && n.battle_stat && defined?(ActorStat) n.battle_stat.set_stat("health", mhv, ActorStat::MAX_STAT) rescue nil # clamp current hp if n.respond_to?(:health) && n.respond_to?(:health=) cur = n.health.to_i n.health = mhv if cur > mhv end end rescue end when "state" ev.instance_variable_set(:@net_state, value.to_s) rescue nil apply_remote_death_visual(ev) if value.to_s == "death" when "target" ev.instance_variable_set(:@net_target_id, value.to_i) rescue nil when "page" begin page_idx = value.to_i if page_idx > 0 && ev.event && ev.event.pages current = (ev.instance_variable_get(:@page_index) rescue 0).to_i if current != page_idx apply_remote_event_page(ev, page_idx) end end rescue end when "erased" if value.to_s == "1" erase_event(server_event_id) else begin if ev.respond_to?(:erased?) && ev.erased? ev.instance_variable_set(:@erased, false) page_idx = (ev.instance_variable_get(:@page_index) rescue 0).to_i apply_remote_event_page(ev, page_idx) if page_idx > 0 end rescue end end when "transparent" flag = (value.to_i == 1) begin ev.transparent = flag if ev.respond_to?(:transparent=) rescue ev.instance_variable_set(:@transparent, flag) rescue nil end when "through" ev.instance_variable_set(:@through, (value.to_i == 1)) rescue nil when "direction_fix" ev.instance_variable_set(:@direction_fix, (value.to_i == 1)) rescue nil when "walk_anime" ev.instance_variable_set(:@walk_anime, (value.to_i == 1)) rescue nil when "step_anime" ev.instance_variable_set(:@step_anime, (value.to_i == 1)) rescue nil when "dead" if value.to_s == "1" apply_remote_death_visual(ev) else ev.instance_variable_set(:@net_dead, false) rescue nil end when "anim_id" play_event_animation(server_event_id, value.to_i) end end def self.play_event_animation(server_event_id, anim_id) if respond_to?(:incoming_event_state_allowed?) && !incoming_event_state_allowed?(server_event_id, "anim_id") return end ev = get_map_event(server_event_id) return unless ev ev.animation_id = anim_id rescue nil end def self.erase_event(server_event_id) eid = server_event_id.to_i if respond_to?(:incoming_event_lifecycle_allowed?) && !incoming_event_lifecycle_allowed?(eid) return end # Stand-in (в т.ч. summon id < 20000) удаляем из @remote_events. if remote_event_host?(eid) remove_remote_event(eid) return end ev = get_map_event(eid) return unless ev begin ev.erase ev.refresh rescue end end def self.respawn_event(server_event_id, x, y) eid = server_event_id.to_i if respond_to?(:incoming_event_lifecycle_allowed?) && !incoming_event_lifecycle_allowed?(eid) return end ev = resolve_remote_npc_event(eid, x, y, "NetEvent_#{eid}") return unless ev begin ev.moveto(x, y) ev.instance_variable_set(:@erased, false) ev.instance_variable_set(:@net_dead, false) rescue nil ev.remove_instance_variable(:@mp_ws_death_drop_done) rescue nil ev.transparent = false ntype = (ev.instance_variable_get(:@net_npc_type) rescue "").to_s apply_remote_npc_graphic(ev, ntype) if ntype != "" rescue end end def self.remove_remote_event(server_event_id) eid = server_event_id.to_i @remote_events ||= {} ev = @remote_events[eid] return unless ev return unless $game_map local_id = ev.id $game_map.events.delete(local_id) rescue nil 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 == ev sprite.dispose rescue nil true else false end end end @remote_events.delete(eid) $game_map.need_refresh = true rescue nil puts "[EV] removed remote event server=#{eid} local=#{local_id}" end #-------------------------------------------------------------------------- # WORLD: apply npc type/stateful skin #-------------------------------------------------------------------------- def self.apply_npc_type(server_event_id, npc_type) eid = server_event_id.to_i token = npc_type.to_s return if token.nil? || token.empty? # Совместимый static map NPC — только fingerprint, без подмены. if static_map_event_id?(eid) local = nil begin local = $game_map.events[eid] if $game_map && $game_map.events rescue local = nil end if local && respond_to?(:remember_remote_static_npc_type) compatible = remember_remote_static_npc_type(eid, token) return if compatible end # summon / чужой id → remote stand-in (не трогаем локальную дверь/квест) end ev = resolve_remote_npc_event(eid, 0, 0, "NetSummon_#{token}") return unless ev ev.instance_variable_set(:@net_npc_type, token) rescue nil # Графика НЕ зависит от наличия $data_npcs — иначе плагинный токен # остаётся невидимым hitbox. begin apply_remote_npc_graphic(ev, token) if ev.instance_variable_get(:@net_server_event_id) rescue end has_npc_data = defined?($data_npcs) && $data_npcs && $data_npcs[token] unless has_npc_data # Без NPC-данных всё равно держим видимый stand-in + hp/move. return end current_type = "" begin current_type = ev.npc_name.to_s if ev.respond_to?(:npc_name) rescue current_type = "" end need_apply = false begin need_apply = !ev.respond_to?(:npc?) || !ev.npc? || current_type != token rescue need_apply = true end if need_apply begin $game_map.delete_npc(ev) if $game_map && $game_map.respond_to?(:delete_npc) && ev.respond_to?(:npc?) && ev.npc? rescue end begin ev.instance_variable_set(:@npc, nil) ev.instance_variable_set(:@npc_name, nil) ev.instance_variable_set(:@npc_detached, false) ev.instance_variable_set(:@force_update_actor, true) rescue end begin ev.set_npc(token) if ev.respond_to?(:set_npc) rescue => e puts "[EV] set_npc failed type=#{token} err=#{e.class}: #{e.message}" rescue nil end end begin maxhp = (ev.instance_variable_get(:@net_max_hp) rescue nil) npc = (ev.respond_to?(:npc) ? ev.npc : nil) rescue nil if npc && !maxhp.nil? && npc.respond_to?(:battle_stat) && npc.battle_stat && defined?(ActorStat) npc.battle_stat.set_stat("health", maxhp.to_i, ActorStat::MAX_STAT) rescue nil end rescue end begin hpv = (ev.instance_variable_get(:@net_hp) rescue nil) npc = (ev.respond_to?(:npc) ? ev.npc : nil) rescue nil npc.health = hpv.to_i if npc && !hpv.nil? && npc.respond_to?(:health=) rescue end # После set_npc снова накатываем графику (refresh мог обнулить). begin apply_remote_npc_graphic(ev, token) if ev.instance_variable_get(:@net_server_event_id) rescue end # Fallback AI movement на remote: если event_move редкий, sensor выставит # move_type=6. Не форсим здесь — owner шлёт event_ai/event_move. begin if respond_to?(:mp_npcfx_apply_remote_state) st = (ev.instance_variable_get(:@net_state) rescue "").to_s mp_npcfx_apply_remote_state(eid, st) elsif ev.respond_to?(:setup_npc_effect) ev.setup_npc_effect end rescue end end #-------------------------------------------------------------------------- # WORLD: apply AI (movement parameters) from master # event_ai::ms:mf:mt:df:th:wa:sa:sc:wc:mri:mrf #-------------------------------------------------------------------------- def self.apply_event_ai(server_event_id, ai_parts) eid = server_event_id.to_i if respond_to?(:incoming_event_move_allowed?) && !incoming_event_move_allowed?(eid) return end ev = resolve_remote_npc_event(eid, 0, 0, "NetEvent_#{eid}") return unless ev return if ai_parts.nil? begin ms = ai_parts[0].to_i mf = ai_parts[1].to_i mt_token = ai_parts[2].to_s mt = if mt_token.start_with?("sym_") mt_token[4..-1].to_sym elsif mt_token =~ /\A-?\d+\z/ mt_token.to_i else (mt_token.to_sym rescue mt_token.to_i) end df = ai_parts[3].to_i th = ai_parts[4].to_i wa = ai_parts[5].to_i sa = ai_parts[6].to_i sc = ai_parts[7].to_i wc = ai_parts[8].to_i mri = ai_parts[9].to_i mrf = ai_parts[10].to_i # speed/frequency begin ev.move_speed = ms if ev.respond_to?(:move_speed=) rescue ev.instance_variable_set(:@move_speed, ms) rescue nil end begin ev.move_frequency = mf if ev.respond_to?(:move_frequency=) rescue ev.instance_variable_set(:@move_frequency, mf) rescue nil end ev.instance_variable_set(:@move_type, mt) rescue nil # flags ev.instance_variable_set(:@direction_fix, (df == 1)) rescue nil ev.instance_variable_set(:@through, (th == 1)) rescue nil ev.instance_variable_set(:@walk_anime, (wa == 1)) rescue nil ev.instance_variable_set(:@step_anime, (sa == 1)) rescue nil # timers / route state ev.instance_variable_set(:@stop_count, sc) rescue nil ev.instance_variable_set(:@wait_count, wc) rescue nil ev.instance_variable_set(:@move_route_index, mri) rescue nil ev.instance_variable_set(:@move_route_forcing, (mrf == 1)) rescue nil rescue end end #-------------------------------------------------------------------------- # NPC-owner-only: apply incoming hit from a client (forwarded by server) #-------------------------------------------------------------------------- def self.apply_npc_hit(attacker_id, event_id, delta) return if defined?(MPSafeZone) && MPSafeZone.active? return unless @worldsync return unless npc_owned_by_me?(event_id) ev = get_map_event(event_id) return unless ev begin if ev.respond_to?(:npc) && (n = ev.npc) && n.respond_to?(:health) && n.respond_to?(:health=) old = n.health.to_i newv = old + delta.to_i # clamp to [0, max] begin if n.respond_to?(:battle_stat) && n.battle_stat && defined?(ActorStat) mx = n.battle_stat.get_stat("health", ActorStat::MAX_STAT) rescue nil newv = mx.to_i if !mx.nil? && newv > mx.to_i end rescue end newv = 0 if newv < 0 n.health = newv send_line("event_state:#{event_id}:hp:#{newv}") else hpv = (ev.instance_variable_get(:@net_hp) rescue 0).to_i + delta.to_i hpv = 0 if hpv < 0 ev.instance_variable_set(:@net_hp, hpv) rescue nil send_line("event_state:#{event_id}:hp:#{hpv}") end rescue end end end