Files

293 lines
19 KiB
C++

#include "aura_translation.hpp"
#include "object_update_layout_guard.hpp"
#include "gateway/turtle_entity_profile.hpp"
#include "gateway/aura_definition_profile.hpp"
#include "gateway/shop_illusion_aura_profile.hpp"
#include "gateway/source_mount_aura_profile.hpp"
#include "gateway/source_ghost_aura_profile.hpp"
#include "gateway/source_self_applied_aura_profile.hpp"
#include "gateway/source_self_only_aura_profile.hpp"
#include "gateway/server_cast_identity.hpp"
#include "gateway/aura_visual_profile.hpp"
#include "gateway/forever_opcode_profile.hpp"
#include <algorithm>
#include <chrono>
using namespace gateway;
namespace F=gateway::turtle_entity_fields;
namespace {
// Original and 70334 definitions agree on a single movement-speed aura (31),
// implicit caster target (1,0), a single active effect, and ribbon state kit 697.
bool caster_only_speed_trail(uint32_t spell){
constexpr uint32_t spells[]={11,47,2983,4104,4105,8696,11305,12568};
return std::binary_search(std::begin(spells),std::end(spells),spell);
}
}
uint32_t NativeAuraTranslation::Unit::active_mask(uint32_t spell) const {
// A source mount holder can have separate mounted/speed/swim effects.
// Expose only its reviewed mounted effect, and only after the actual
// source object confirms a mount display. Never infer companion speeds.
if(auto mounted=gateway::source_mount_aura_profile::mask(spell))return mount_display?mounted:0;
// The genuine ghost holder also contains speed/breathing effects. Only
// its reviewed same-ID GHOST effect is eligible here. No synthetic aura.
if(auto ghost=gateway::source_ghost_aura_profile::mask(spell))return ghost;
// Only old-client shop illusions with an exact same-ID native transform
// effect are admitted; their holder is still supplied by the source.
if(auto illusion=gateway::shop_illusion_aura_profile::mask(spell))return illusion;
return gateway::aura_definition_profile::mask(spell);
}
Bytes NativeAuraTranslation::snapshot(uint64_t guid) const {
// Public source/translation metadata only, no engine memory or auth data.
auto found=units.find(guid);Bytes out;unsigned occupied=0,shown=0;
if(found!=units.end()){for(auto s:found->second.spells)occupied+=s!=0;shown=found->second.shown.count();}
for(uint32_t value:{1u,1376u,updates_sent,unsupported,uint32_t(found!=units.end()),occupied,shown,0u})le(out,value,4);
for(unsigned slot=0;slot<48;++slot){
if(found==units.end()){append(out,Bytes(28));continue;}
const auto& u=found->second;auto spell=u.spells[slot];
for(uint32_t value:{spell,(u.flags[slot/8]>>((slot%8)*4))&15,(u.levels[slot/4]>>((slot%4)*8))&255,(u.applications[slot/4]>>((slot%4)*8))&255,u.active_mask(spell),aura_visual_profile::link(spell),uint32_t(u.shown[slot])})le(out,value,4);
}
return out;
}
uint64_t NativeAuraTranslation::clock_ms(){return uint64_t(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count());}
void NativeAuraTranslation::consume_duration(uint64_t guid,std::span<const uint8_t> body,uint64_t now){
Reader input(body);auto slot=input.le(1);auto remaining=input.le(4);input.end();
if(!guid || guid>0xffffffffULL || slot>=48 || (remaining>0x7fffffff && remaining!=0xffffffff))throw Error("invalid source aura duration");
if(auto unit=units.find(guid);unit!=units.end() && unit->second.source_type!=4)throw Error("source player duration on creature");
if(durations.size()>=4096 && !durations.contains(guid))throw Error("native aura duration cache bound");
auto& duration=durations[guid][slot];
if(now<duration.received_ms)throw Error("source aura duration clock regressed");
duration={remaining==0xffffffff?std::nullopt:std::optional<uint32_t>(remaining),now,true};
if(auto unit=units.find(guid);unit!=units.end()){
if(unit->second.spells[slot]){unit->second.dirty.set(slot);dirty.insert(guid);}
}
}
void NativeAuraTranslation::forget(uint64_t id){
units.erase(id);durations.erase(id);dirty.erase(id);
std::erase_if(pending_self_hits,[&](const auto& entry){return entry.second.unit==id;});
}
bool NativeAuraTranslation::observe_equip_cast(uint64_t guid,uint32_t spell,const Guid128& cast,const Guid128& item){
AuraSelfCastContext context{cast,item};auto found=units.find(guid);
if(!valid_self_equip_aura_context(spell,context) || !aura_definition_profile::mask(spell) ||
found==units.end() || found->second.initial || found->second.source_type!=4)return false;
auto& unit=found->second;
Reader identity(cast);identity.le(8);auto high=identity.le(8);
if(!unit.presented_realm || ((high>>42)&0x1fff)!=unit.presented_realm)return false;
if(unit.equip_candidate){unit.equip_candidate->ambiguous=true;return false;}
if(std::find(unit.spells.begin(),unit.spells.end(),spell)!=unit.spells.end())return false;
unit.equip_candidate=EquipCandidate{spell,context,false};return true;
}
bool NativeAuraTranslation::bind_initial_equip_cast(uint64_t guid,uint32_t spell,const Guid128& cast,const Guid128& item,uint32_t realm){
AuraSelfCastContext context{cast,item};auto found=units.find(guid);
if(!realm || realm>0x1fff || !valid_self_equip_aura_context(spell,context) || !aura_definition_profile::mask(spell) ||
found==units.end() || !found->second.initial || found->second.source_type!=4)return false;
Reader identity(cast);identity.le(8);auto high=identity.le(8);
if(((high>>42)&0x1fff)!=realm)return false;
auto& unit=found->second;
if(std::count(unit.spells.begin(),unit.spells.end(),spell)!=1)return false;
auto slot=std::find(unit.spells.begin(),unit.spells.end(),spell)-unit.spells.begin();
if(unit.self_cast[slot])return false;
unit.self_cast[slot]=context;return true;
}
bool NativeAuraTranslation::bind_server_self_cast(uint64_t guid,uint32_t spell,const Guid128& cast,uint32_t realm){
auto found=units.find(guid);
if(!realm || realm>0x1fff || !valid_server_self_aura_cast(spell,cast) ||
(!source_ghost_aura_profile::mask(spell) && !caster_only_speed_trail(spell)) || found==units.end() || found->second.source_type!=4)return false;
Reader id(cast);id.le(8);if(((id.le(8)>>42)&0x1fff)!=realm)return false;
auto& u=found->second;
if(std::count(u.spells.begin(),u.spells.end(),spell)!=1)return false;
auto slot=std::find(u.spells.begin(),u.spells.end(),spell)-u.spells.begin();
if(u.self_cast[slot])return false;
// This only enriches an already genuine holder. Caller must supply an
// observed successful self GO; neither a cast nor an aura is generated.
u.server_self_cast[slot]=cast;u.dirty.set(slot);dirty.insert(guid);return true;
}
void NativeAuraTranslation::observe_speed_aura_hit(uint64_t unit,uint32_t spell,const Guid128& cast,uint32_t realm){
if(!caster_only_speed_trail(spell) || !valid_server_self_aura_cast(spell,cast))return;
const auto actor=units.find(unit);
if(actor==units.end() || actor->second.source_type!=4 || actor->second.initial ||
!realm || realm!=actor->second.presented_realm)return;
const auto& slots=actor->second.spells;
const auto slot=std::find(slots.begin(),slots.end(),spell);
if(slot!=slots.end()){
// A periodic refresh keeps the identity of the existing application.
if(!actor->second.server_self_cast[slot-slots.begin()])bind_server_self_cast(unit,spell,cast,realm);
pending_self_hits.erase(spell);
return;
}
const auto now=clock_ms();
auto prior=pending_self_hits.find(spell);
if(prior!=pending_self_hits.end() && prior->second.unit==unit && now<prior->second.expires_ms){
prior->second.ambiguous=true;
return;
}
pending_self_hits[spell]={unit,now+2000,cast,realm,false};
}
unsigned NativeAuraTranslation::bind_self_applied(uint64_t guid,uint32_t realm,uint32_t map,uint64_t& counter){
auto found=units.find(guid);
if(!realm || realm>0x1fff || map>0x1fff || found==units.end() || found->second.source_type!=4)return 0;
auto& u=found->second;unsigned bound=0;
for(unsigned slot=0;slot<48;++slot){
auto spell=u.spells[slot];
if(!source_self_applied_aura_profile::contains(spell) || !aura_definition_profile::mask(spell) ||
u.self_cast[slot] || u.server_self_cast[slot] || counter<0x8000000000ULL || counter>=0xffffffffffULL ||
std::count(u.spells.begin(),u.spells.end(),spell)!=1)continue;
// Enriches only the existing genuine holder; reset with the slot.
u.server_self_cast[slot]=forever_server_cast_guid(realm,map,spell,++counter);
u.dirty.set(slot);dirty.insert(guid);++bound;
}
return bound;
}
void NativeAuraTranslation::remember_caster(uint64_t caster,const Guid128& identity){
if(!caster)return;
if(caster_identity.size()>=1024 && !caster_identity.contains(caster))caster_identity.clear(); // bounded
caster_identity[caster]=identity;
}
void NativeAuraTranslation::observe_go(uint64_t caster,uint32_t spell,const std::vector<uint64_t>& hits,uint64_t now){
if(!caster || !spell)return;
if(pending_casters.size()>=1024)std::erase_if(pending_casters,[&](const auto& e){return now>=e.second.expires_ms;});
for(auto target:hits){
if(!target)continue;
// Owner 2026-11-29 (raid frames: own Prayer of Spirit/Fortitude icons missing, Shadow Protection shown): a
// recast that only refreshes an aura already held (known at login / unit creation with no caster) changes
// no aura field, so the slot kept its empty caster and the client's sourceUnit stayed nil. Rebind the one
// slot holding this spell to the GO's caster now; several slots with the spell stay as they are.
if(auto u=units.find(target);u!=units.end()){
auto& unit=u->second;unsigned held=0,slot=0;
for(unsigned i=0;i<48;++i)if(unit.spells[i]==spell){++held;slot=i;}
// A slot already presented as the holder's own (self-only profile) keeps its identity on a self GO.
const bool already_own=caster==target && unit.server_self_cast[slot];
if(held==1 && !unit.self_cast[slot] && !already_own && unit.caster[slot]!=caster){
unit.caster[slot]=caster;unit.server_self_cast[slot].reset();unit.caster_cast[slot].reset();
unit.dirty.set(slot);dirty.insert(target);
}
}
auto [it,fresh]=pending_casters.try_emplace({target,spell},PendingCaster{caster,now+2000,false});
if(fresh)continue;
auto& p=it->second;
if(now>=p.expires_ms)p={caster,now+2000,false};
else if(p.caster!=caster)p.ambiguous=true;
}
if(pending_casters.size()>1024)pending_casters.clear(); // Undrained flood: keep no stale facts.
}
unsigned NativeAuraTranslation::bind_casters(uint32_t realm,uint32_t map,uint64_t& counter,uint64_t deferred_owner){
if(!realm || realm>0x1fff || map>0x1fff)return 0;
unsigned bound=0;
for(auto guid:dirty){
if(guid==deferred_owner)continue;
auto found=units.find(guid);if(found==units.end())continue;auto& u=found->second;
for(unsigned slot=0;slot<48;++slot){
const auto spell=u.spells[slot];
if(!spell || !u.dirty[slot] || u.self_cast[slot] || u.server_self_cast[slot] || spell>0x7fffff)continue;
const bool own=source_self_only_aura_profile::contains(spell) || u.caster[slot]==guid;
// Gitea #12: an aura of an unknown caster still gets a CastID; the client asserts on an empty one
// (Unit_C.cpp GetAuraDataBySpellID, castGUID != WOWGUID_NULL) when it looks the aura up by spell.
if(!own && u.caster_cast[slot])continue;
if(counter<0x8000000000ULL || counter>=0xffffffffffULL)return bound;
(own?u.server_self_cast[slot]:u.caster_cast[slot])=forever_server_cast_guid(realm,map,spell,++counter);++bound;
}
}
return bound;
}
void NativeAuraTranslation::consume(const std::vector<LegacyObjectUpdate>& updates){
for(const auto& update:updates){
if(update.kind==4){for(auto id:update.removed)forget(id);continue;}
if(update.kind==2 || update.kind==3){
if(update.type!=3 && update.type!=4)continue;
if(units.size()>=4096 && !units.contains(update.guid))throw Error("native aura unit bound");
if(units.contains(update.guid))forget(update.guid); // New object generation.
units[update.guid]=Unit{};units.at(update.guid).source_type=update.type;units.at(update.guid).dirty.set();dirty.insert(update.guid);
}else if(update.kind!=0)continue;
auto found=units.find(update.guid);if(found==units.end())continue;auto& unit=found->second;
auto before=unit.spells;auto candidate=unit.equip_candidate;unit.equip_candidate.reset();
const auto before_applications=unit.applications;
for(auto [index,value]:update.fields){
if(index>=F::UNIT_FIELD_AURA && index<F::UNIT_FIELD_AURA+48){
auto slot=index-F::UNIT_FIELD_AURA;
if(auto found_duration=durations.find(update.guid);found_duration!=durations.end()){
auto& duration=found_duration->second[slot];
if(!value || (unit.spells[slot]!=value && !duration.awaiting_field))duration={};
duration.awaiting_field=false;
}
if(unit.spells[slot]!=value){
unit.spells[slot]=value;unit.self_cast[slot].reset();unit.server_self_cast[slot].reset();unit.caster[slot]=0;unit.caster_cast[slot].reset();unit.dirty.set(slot);
if(auto hit=pending_casters.find({update.guid,value});value && hit!=pending_casters.end()){
if(!hit->second.ambiguous && clock_ms()<hit->second.expires_ms)unit.caster[slot]=hit->second.caster;
pending_casters.erase(hit);
}
}
}
else if(index>=F::UNIT_FIELD_AURAFLAGS && index<F::UNIT_FIELD_AURAFLAGS+6){auto word=index-F::UNIT_FIELD_AURAFLAGS;if(unit.flags[word]!=value){unit.flags[word]=value;for(unsigned i=0;i<8;++i)unit.dirty.set(word*8+i);}}
else if(index>=F::UNIT_FIELD_AURALEVELS && index<F::UNIT_FIELD_AURALEVELS+12){auto word=index-F::UNIT_FIELD_AURALEVELS;if(unit.levels[word]!=value){unit.levels[word]=value;for(unsigned i=0;i<4;++i)unit.dirty.set(word*4+i);}}
else if(index>=F::UNIT_FIELD_AURAAPPLICATIONS && index<F::UNIT_FIELD_AURAAPPLICATIONS+12){auto word=index-F::UNIT_FIELD_AURAAPPLICATIONS;if(unit.applications[word]!=value){unit.applications[word]=value;for(unsigned i=0;i<4;++i)unit.dirty.set(word*4+i);}}
else if(index==F::UNIT_FIELD_MOUNTDISPLAYID && unit.mount_display!=value){
unit.mount_display=value;
for(unsigned slot=0;slot<48;++slot)if(source_mount_aura_profile::mask(unit.spells[slot]))unit.dirty.set(slot);
}
}
if(update.kind==0){
auto stacks=[](const std::array<uint32_t,12>& a,unsigned slot){return ((a[slot/4]>>((slot%4)*8))&0xffu)+1;};
for(unsigned slot=0;slot<48;++slot){
const auto old=before[slot],now=unit.spells[slot];
if(transitions.size()>=4096)transitions.clear(); // Undrained: keep only fresh facts.
if(old!=now){
if(old)transitions.push_back({update.guid,old,1,1});
if(now)transitions.push_back({update.guid,now,0,stacks(unit.applications,slot)});
}else if(now){
const auto a=stacks(before_applications,slot),b=stacks(unit.applications,slot);
if(a!=b)transitions.push_back({update.guid,now,uint8_t(b>a?2:3),b});
}
}
}
if(candidate && !candidate->ambiguous && std::count(unit.spells.begin(),unit.spells.end(),candidate->spell)==1){
for(unsigned slot=0;slot<48;++slot)if(!before[slot] && unit.spells[slot]==candidate->spell)unit.self_cast[slot]=candidate->context;
}
if(!pending_self_hits.empty() && unit.source_type==4){
const auto now=clock_ms();
std::erase_if(pending_self_hits,[&](const auto& entry){
const auto& [spell,hit]=entry;
if(now>=hit.expires_ms)return true;
if(hit.unit!=update.guid || std::find(unit.spells.begin(),unit.spells.end(),spell)==unit.spells.end())return false;
if(!hit.ambiguous)bind_server_self_cast(hit.unit,spell,hit.cast,hit.realm);
return true;
});
}
if(unit.dirty.any())dirty.insert(update.guid);
}
}
std::vector<LegacyPacket> NativeAuraTranslation::flush(const std::map<uint64_t,Guid128>& known,uint64_t now){
std::vector<LegacyPacket> output;
for(auto it=dirty.begin();it!=dirty.end();){
auto bound=known.find(*it);if(bound==known.end()){++it;continue;}
auto& unit=units.at(*it);AuraSlotUpdate update;update.unit=bound->second;update.all=unit.initial;
Reader identity(bound->second);identity.le(8);auto high=identity.le(8);unit.presented_realm=(high>>58)==2?uint16_t((high>>42)&0xffff):0;
for(unsigned slot=0;slot<48;++slot)if(unit.dirty[slot]){
auto spell=unit.spells[slot];auto flags=(unit.flags[slot/8]>>((slot%8)*4))&15;auto level=(unit.levels[slot/4]>>((slot%4)*8))&255;auto applications=(unit.applications[slot/4]>>((slot%4)*8))&255;auto mask=unit.active_mask(spell);
bool positive=flags&4,negative=flags&8;
if(!spell || !mask || positive==negative || (flags&2) || applications==255){
if(spell)++unsupported;
if(unit.shown[slot])update.slots.push_back({uint16_t(slot),std::nullopt});
unit.shown.reset(slot);continue;
}
AuraSlotData data{spell,mask,uint16_t(positive?2:16),uint16_t(level),uint8_t(applications+1),unit.self_cast[slot]};
data.server_self_cast=unit.server_self_cast[slot];
if(!data.self_cast && !data.server_self_cast && unit.caster[slot] && unit.caster[slot]!=*it){
if(auto caster=known.find(unit.caster[slot]);caster!=known.end())data.cast_unit=caster->second;
else if(auto departed=caster_identity.find(unit.caster[slot]);departed!=caster_identity.end())data.cast_unit=departed->second;
if(data.cast_unit)data.cast_id=unit.caster_cast[slot];
}
if(!data.self_cast && !data.server_self_cast && !data.cast_unit)data.cast_id=unit.caster_cast[slot];
if(auto found=durations.find(*it);found!=durations.end()){
const auto& timing=found->second[slot];
if(timing.remaining_ms){
if(now<timing.received_ms)throw Error("aura flush clock regressed");
auto elapsed=now-timing.received_ms;
data.remaining_ms=elapsed>=*timing.remaining_ms?0u:uint32_t(*timing.remaining_ms-elapsed);
}
}
update.slots.push_back({uint16_t(slot),data});unit.shown.set(slot);
}
if(update.all || !update.slots.empty()){output.push_back({forever_opcodes::SMSG_AURA_UPDATE,forever_aura_update(update)});++updates_sent;}
unit.dirty.reset();unit.initial=false;it=dirty.erase(it);
}
return output;
}