Files
2026-11-30 10:44:01 +02:00

214 lines
13 KiB
C++

#pragma once
#include <algorithm>
#include <memory>
#include <chrono>
#include "spell_channels.hpp"
#include "gateway/legacy_client.hpp"
#include "gateway/roster_translation.hpp"
#include "gateway/queries.hpp"
#include "gateway/peer_item_visual.hpp"
#include "gateway/splines.hpp"
#include "gateway/object_updates.hpp"
#include "gateway/unit_presentation.hpp"
#include "gateway/quest_log.hpp"
#include "gateway/profession_delta.hpp"
#include "gateway/persistent_ground.hpp"
#include <deque>
#include <functional>
#include <map>
#include <optional>
#include <set>
#include <bitset>
#include "gateway/undercity_transport_spawns.hpp"
/* Pure authenticated-wire presentation state; owns no engine/socket/account key. */
struct NativeInventoryTranslation;
struct NativeEntityTranslation {
struct Actor {
uint32_t entry=0;uint8_t kind=0,unit_class=0;uint64_t generation=0;
uint32_t gameobject_type=0; // Source GO create type, valid only for kind5.
uint16_t presentation_realm=0;uint32_t presentation_map=0;
uint64_t corpse_owner=0;
// Source effect97 vanity and the local permanent stat-window pet have separate roles.
bool companion=false,owned_stat_pet=false,companion_template_sent=false;uint32_t pet_original_entry=0;uint64_t pet_initial_owner=0;
std::map<uint16_t,uint32_t> companion_dirty;
gateway::Guid128 modern{};bool eligible=false,sent=false,map_object=false;uint8_t power_type=255;
// Source UNIT_NPC_FLAGS, not a name/entry list, identifies services
// that the native living view must defer until ghost state is known.
bool spirit_service=false,questgiver=false;
uint32_t display=0; // Source UNIT_FIELD_DISPLAYID (create + deltas): legacy scale deltas are divided by its client model scale
uint32_t npc_flags=0; // Source UNIT_NPC_FLAGS at create (service shortcut: no-gossip NPCs open their service directly)
std::shared_ptr<const gateway::LegacyPacket> source_create;
std::map<uint16_t,uint32_t> pending_values;
// Genuine create/movement melee-victim state is separate from Target.
// Owned by this actor generation; pending initial animation waits for
// both real unit bindings and is cancelled by later source stops.
uint64_t melee_victim=0;bool melee_pending=false;
void observe_movement_victim(uint64_t owner,uint64_t victim,std::vector<gateway::LegacyPacket>& out);
// This generation has a parsed source motion since its last stable
// XYZ sample. It is not a predicted/current coordinate.
bool source_pose_moving=false;
};
struct Batch {gateway::LegacyPacket packet;std::vector<std::pair<uint64_t,uint64_t>> actors;};
struct PeerValues {gateway::LegacyPacket packet;std::map<uint16_t,uint32_t> fields;};
struct PeerPlayer {
uint64_t generation=0;gateway::Character identity{};bool named=false,blocked=false;
std::map<uint16_t,uint32_t> fields,create_fields;
// A live slot retains only its public render tuple, independently of full template cache turnover.
std::array<gateway::peer_item_visual::Template,19> visible_looks{};
std::shared_ptr<const gateway::LegacyPacket> create;
std::deque<PeerValues> deferred;std::optional<gateway::LegacyPacket> movement;size_t pending_bytes=0;
};
std::map<uint64_t,PeerPlayer> peer_players;
// The authenticated character's own visible slots. Source transmog shows a
// generated per-transmog item entry that has no native Item row; resolve it
// through the same queried template as peers and refresh only those slots.
PeerPlayer self_look;bool self_look_dirty=false;
// A response is accepted only for the generation which actually queried it.
// Retired outstanding queries are not rebound to a recreated actor.
std::map<uint64_t,uint64_t> peer_name_queries;
size_t peer_pending_bytes=0;
uint32_t peer_received=0,peer_sent=0,peer_held=0,peer_rejected=0;
std::map<uint64_t,Actor> actors;
// Whether a native UpdateObject body carries this actor (its packed 70334 GUID).
static bool created_in(const gateway::LegacyPacket& packet,const Actor& actor){
const auto guid=gateway::pack_guid(actor.modern);
return std::search(packet.body.begin(),packet.body.end(),guid.begin(),guid.end())!=packet.body.end();
}
// Private native-qualification switch; no spell-id or character exception.
bool persistent_ground_enabled=false;
std::map<uint64_t,gateway::PersistentGround> persistent_ground;
std::map<const gateway::LegacyPacket*,unsigned> retained_create_refs;
size_t retained_create_bytes=0;
// Source GUID halves remain authoritative across sparse updates. The
// native target is emitted only after its real unit binding exists.
struct TargetState {
std::array<uint32_t,2> words{};
uint8_t known=0;
std::optional<gateway::Guid128> presented;
uint64_t source_guid() const noexcept {return uint64_t(words[0])|(uint64_t(words[1])<<32);}
};
gateway::NativeSpellChannels spell_channels;
// Selected character's UNIT_FIELD_CHARM and UNIT_FIELD_SUMMON.
std::array<TargetState,2> self_controlled;
// Selected character's PLAYER_FIELD_COMBO_TARGET (Tortoise Player::SetComboPoints).
TargetState self_combo;
// Selected character's PLAYER_FARSIGHT (Tortoise Player::SetFarSightGUID): Mind Vision / possess camera.
TargetState self_farsight;
// Turtle resets the camera at once but sends the next Mind Vision target only after CAMERA_UPDATE_DELAY
// (Player::ScheduleCameraUpdate, 400 ms + tick): hold an empty viewpoint briefly so a hop goes straight A->B.
std::optional<std::chrono::steady_clock::time_point> self_farsight_clear_since;
// The native victim of the last published owned attack-start survives loss
// of victim visibility only until the source stops or replaces that attack.
struct MeleeTarget {uint64_t source;gateway::Guid128 native;};
std::optional<MeleeTarget> self_melee_target;
// Selected character's PLAYER_SELF_RES_SPELL (soulstone/ankh/reincarnation offer); 0 = none.
uint32_t self_res_spell=0;
bool self_res_submitted=false;
std::map<uint64_t,TargetState> targets;
std::set<uint64_t> target_dirty;
std::map<uint32_t,gateway::CreatureTemplateReply> templates;
std::map<uint32_t,uint8_t> classes;
std::set<uint32_t> queries;
// Native CMSG_QUERY_CREATURE for entries the client asked about (often not visible). Each is
// re-queried from Source by entry (zero GUID). A genuine miss is answered; a positive template
// is announced only with an observed class (template_unannounced until then), never a guessed one.
std::set<uint32_t> native_asked,template_unannounced;
bool native_creature_query(std::span<const uint8_t>);
std::deque<gateway::LegacyPacket> requests;
std::deque<Batch> pending;
uint64_t generation=0;size_t pending_bytes=0;
uint32_t removals=0,unsupported=0;
uint32_t self_faction=0; // Genuine self UnitData context for source corpses lacking this field.
uint8_t self_power_type=255;size_t pending_value_fields=0;
// Query-only source Pet words cannot consume the renderer's value budget.
size_t pet_source_value_fields=0;
// Only two source words participate in self VisFlags. A sparse update to
// either must not clear the other's invisibility/creep/untrackable bits.
uint32_t self_unit_bytes1=0,self_player_bytes2=0;
uint32_t self_appearance_identity=0,self_appearance=0,self_appearance2=0;
bool self_appearance_after_morph=false;
std::array<uint32_t,64> self_explored_zone_words{};
// Authoritative self display words, scoped to a genuine source create.
// Only destination codec values use the player-default projection.
uint64_t self_display_guid=0,self_display_generation=0;
uint16_t self_display_realm=0;
uint32_t self_display_map=0,self_display_bytes0=0,self_display_id=0,self_native_display_id=0;
mutable uint32_t self_source_scale=0; // last raw legacy OBJECT_FIELD_SCALE_X of self (re-sent when the display changes)
uint8_t self_display_known=0;
gateway::EquipmentVisibilityState equipment_visibility;
void observe_equipment_visibility(const gateway::LegacyObjectUpdate&,uint64_t,uint16_t,uint32_t);
void invalidate_self_display() noexcept;
void observe_self_display(const gateway::LegacyObjectUpdate&,uint64_t,uint16_t,uint32_t);
void project_self_display(const gateway::LegacyObjectUpdate&,uint64_t,uint16_t,uint32_t,std::map<uint16_t,uint32_t>&) const;
bool self_presentation_ready=false;
gateway::SourceQuestLogState self_quest_log;
gateway::ProfessionSkillState self_profession_skills;
struct SourcePosition {
uint64_t generation=0;uint16_t realm=0;uint32_t map=0;
std::array<float,3> xyz{};
// A sent, validated owned MovementInfo is current XYZ, not a future
// spline endpoint or a source-server acknowledgment.
bool owned_projection=false;
};
std::map<uint64_t,SourcePosition> source_positions;
uint64_t self_position_generation=0;
uint16_t position_realm=0;uint32_t position_map=0;
void invalidate_source_position(uint64_t guid){source_positions.erase(guid);}
bool observe_owned_source_position(const gateway::GroundMovement&,uint64_t self,uint16_t realm,uint32_t map) noexcept;
struct Motion {gateway::MonsterSpline spline;gateway::Guid128 modern;};
std::deque<Motion> motion;
struct ActionDiagnostic {
uint64_t action=0,target=0,mover=0;
uint32_t kind=0,spell=0,opcode=0,status=0,reason=0,spline=0,flags=0,match=0,available=0,facing=0;
uint64_t sequence=0;
};
struct ActionBinding {uint64_t action=0,target=0,mover=0;uint32_t spell=0,realm=0,map=0;};
struct PendingActions {std::deque<ActionBinding> entries;bool ambiguous=false;};
std::deque<ActionDiagnostic> action_journal;
std::map<uint32_t,PendingActions> pending_actions;
std::optional<ActionBinding> latest_action;
uint64_t action_counter=0;
uint64_t diagnostic_self=0;uint16_t diagnostic_realm=0;uint32_t diagnostic_map=0;
bool diagnostic_scope_known=false;
void clear_action_scope();
void set_action_scope(uint64_t,uint16_t,uint32_t);
void note_spell_queued(uint32_t,uint64_t);
void note_spell_result(uint32_t,uint32_t,uint32_t);
void note_spell_go(uint32_t,uint64_t);
void note_spell_damage(uint32_t,uint64_t,uint32_t,uint32_t);
void note_self_motion(const gateway::SplineParseObservation&,uint32_t,bool,uint32_t,uint32_t=0);
void record_action(ActionDiagnostic);
uint32_t motion_received=0,motion_sent=0,motion_unknown=0,motion_rejected=0;
struct SourceField {
uint64_t legacy=0;
uint32_t sequence=0,source_packet=0,opcode=0,kind=0,index=0,value=0;
};
std::deque<SourceField> source_fields;
uint32_t source_packets=0,source_field_count=0;
bool source_field_overflow=false;
void observe_source_fields(const std::vector<gateway::LegacyObjectUpdate>&,uint64_t self,uint32_t opcode);
using UndercityEntries=std::bitset<gateway::UndercityTransportSpawns.size()>;
static uint64_t undercity_source_guid(const gateway::UndercityTransportSpawn& row){
return (uint64_t(0xf110)<<48)|(uint64_t(row.entry)<<24)|row.spawn_guid;
}
// Source visibility history and client-only fills have separate lifetimes.
UndercityEntries undercity_seen, undercity_filled;
using Codec=std::function<gateway::Bytes(uint32_t,const gateway::Bytes&)>;
void fill_undercity(const gateway::Character&,uint16_t,uint32_t,const Codec&,std::vector<gateway::LegacyPacket>&);
void leave_undercity(uint16_t,bool,std::vector<gateway::LegacyPacket>&);
void flush_channels(uint64_t self, uint16_t realm, uint32_t map, const Codec&,
std::vector<gateway::LegacyPacket>&);
std::vector<gateway::LegacyPacket> translate(const gateway::LegacyPacket&,const gateway::Character&,uint16_t,uint32_t,uint32_t,const Codec&,bool viewer_ghost=false,NativeInventoryTranslation* peer_items=nullptr);
static bool peer_movement_opcode(uint16_t);
std::vector<gateway::LegacyPacket> refresh_peer_item(uint32_t entry,uint16_t realm,uint32_t address,uint32_t map,
NativeInventoryTranslation&,const Codec&);
std::vector<gateway::LegacyPacket> self_look_refresh(uint16_t realm,uint32_t address,uint32_t map,
const Codec&,NativeInventoryTranslation*,uint32_t only_entry,bool request);
std::vector<gateway::LegacyPacket> translate_peers(const gateway::LegacyPacket&,
const gateway::Character&,uint16_t,uint32_t,uint32_t,const Codec&,NativeInventoryTranslation* peer_items=nullptr);
std::optional<gateway::Bytes> companion_name_query(std::span<const uint8_t>,uint16_t,uint32_t) const;
gateway::Bytes snapshot() const;
gateway::Bytes motion_snapshot() const;
};