Files

251 lines
12 KiB
C++

#pragma once
#include "gateway/durability_effect_slots.hpp"
#include "gateway/legacy_stream.hpp"
#include "gateway/turtle_combat_logs.hpp"
#include <algorithm>
#include <cmath>
#include <iterator>
namespace gateway::source_combat_events {
using Bindings=std::map<uint64_t,Guid128>;
// The native log uses the Classic happiness power in DB2 slot 27.
// Mana, rage, focus, energy and the health sentinel retain their numeric IDs.
inline std::optional<int32_t> native_power(int32_t source){
if(source==4)return 27;
if(source==-2 || (source>=0 && source<=3))return source;
return {};
}
inline bool append_bound(Bytes& out,uint64_t raw,const Bindings& known){
auto found=known.find(raw);
if(!raw || found==known.end() || found->second==Guid128{})return false;
append(out,pack_guid(found->second));
return true;
}
inline std::optional<Bytes> xp_gain(std::span<const uint8_t> body,const Bindings& known){
Reader r(body);const auto victim=r.le(8),total=r.le(4),reason=r.le(1);
if((victim?0u:1u)!=reason || total>INT_MAX)return {};
// Player::GiveXP grants rested XP only for a kill. The non-kill
// source form has no group multiplier and its actual base equals total.
uint32_t base=uint32_t(total);
float multiplier=1.0f;
Bytes out;
if(victim){
if(!append_bound(out,victim,known))return {};
base=uint32_t(r.le(4));multiplier=r.f32();
if(base>total || multiplier!=1.0f)return {}; // Source writer's fixed rate.
}else append(out,pack_guid(Guid128{}));
r.end();
le(out,total,4);le(out,reason,1);le(out,base,4);le(out,std::bit_cast<uint32_t>(multiplier),4);
return out;
}
inline std::optional<Bytes> spell_miss(std::span<const uint8_t> body,const Bindings& known){
auto event=turtle_spell_miss_log(body);if(!event)return {};
Bytes out;le(out,event->spell,4);
if(!append_bound(out,event->caster,known))return {};
le(out,event->targets.size(),4);
for(const auto& miss:event->targets){
if(!miss.miss_info || !append_bound(out,miss.target,known))return {};
le(out,miss.miss_info,1);le(out,0,1); // Optional roll debug.
}
le(out,0,1); // Authored miss notification is visible in the combat log.
return out;
}
inline std::optional<Bytes> proc_resist(std::span<const uint8_t> body,const Bindings& known){
auto event=turtle_spell_immune_log(body); // Same raw64 pair, spell and source debug byte.
if(!event)return {};
Bytes out;
if(!append_bound(out,event->caster,known) || !append_bound(out,event->target,known))return {};
le(out,event->spell,4);le(out,0,1); // Rolled and Needed are both absent.
return out;
}
inline std::optional<Bytes> energize(std::span<const uint8_t> body,const Bindings& known){
auto event=turtle_spell_energize_log(body);if(!event)return {};
const auto power=native_power(event->power_type);
if(!power)return {};
// The general source sends the amount before clamping; the native log
// requires both the actual change and overflow. Zero is fully determined.
// Nitrous Boost sends its -10 only AFTER a checked, successful self drain.
if(event->amount && !(event->spell==27746 && event->target==event->caster &&
event->power_type==0 && std::bit_cast<int32_t>(event->amount)==-10))return {};
Bytes out;
if(!append_bound(out,event->target,known) || !append_bound(out,event->caster,known))return {};
le(out,event->spell,4);le(out,uint8_t(*power),1);le(out,event->amount,4);le(out,0,4);
le(out,0,1); // No advanced combat-log statistics were sent.
return out;
}
enum class ExecuteList { PowerDrain,ExtraAttack,Durability,Victim,CreatedItem,FedItem };
inline std::optional<ExecuteList> execute_list(uint32_t effect){
switch(effect){
case 8:return ExecuteList::PowerDrain;
case 19:return ExecuteList::ExtraAttack;
case 111:return ExecuteList::Durability;
// Resurrect, summon, open lock, trans door, summon pet, summon
// object wild, dismiss pet, summon object slot 1. In both versions
// these effects supply this list with the same effect number.
case 18:case 28:case 33:case 50:case 56:case 76:case 102:case 104:return ExecuteList::Victim;
case 24:return ExecuteList::CreatedItem;
case 101:return ExecuteList::FedItem;
default:return {};
}
}
inline bool execute_target(Reader& r,Bytes& out,ExecuteList list,uint32_t effect,uint32_t spell,const Bindings& known,const Bindings& owned){
if(list==ExecuteList::CreatedItem || list==ExecuteList::FedItem){
const auto item=r.le(4);
if(!item || item>INT_MAX)return false;
le(out,item,4);return true;
}
const auto target=r.le(8);
if(!append_bound(out,target,known) && !(effect==33 && append_bound(out,target,owned)))return false;
switch(list){
case ExecuteList::PowerDrain:{
const auto points=r.le(4);
const auto type=native_power(std::bit_cast<int32_t>(uint32_t(r.le(4))));
const auto amplitude=r.f32();
if(!type || *type<0 || !std::isfinite(amplitude))return false;
le(out,points,4);le(out,uint8_t(*type),1);le(out,std::bit_cast<uint32_t>(amplitude),4);
break;
}
case ExecuteList::ExtraAttack:le(out,r.le(4),4);break;
case ExecuteList::Durability:{
const auto item=r.le(4),source_tail=r.le(4);
// Both senders accept only players. append_bound already established this live binding.
if((target>>48)!=0 || (known.at(target).back()>>2)!=2 || source_tail!=UINT32_MAX)return false;
const auto profile=std::find_if(DurabilityEffectSlots.begin(),DurabilityEffectSlots.end(),
[spell](const DurabilityEffectSlot& row){return row.spell==spell;});
uint32_t slot=UINT32_MAX;
if(item==UINT32_MAX){
// The source names the whole equipment group explicitly, even for unknown spells.
if(profile!=DurabilityEffectSlots.end() && !profile->permits_group)return false;
}else{
if(!item || item>INT32_MAX || profile==DurabilityEffectSlots.end() || profile->named_slot<0)return false;
slot=uint32_t(profile->named_slot);
}
// Native calls its second int32 Amount but fills it with the equipment slot.
le(out,item,4);le(out,slot,4);break;
}
default:break;
}
return true;
}
// Remaining capacity of a single native world bundle, including each record's framing.
struct ExecuteBudget {
size_t records,bytes;
bool take(size_t n,size_t size){
if(n>records || size>bytes)return false;
records-=n;bytes-=size;return true;
}
};
// Source effect 68 logs each actually interrupted unit and its interrupted spell.
// The native effect sends one SpellInterruptLog per interruption, outside SpellExecuteLog.
inline bool append_interrupts(Reader& r,uint32_t count,const Bytes& caster,uint32_t spell,
const Bindings& known,ExecuteBudget& budget,std::vector<LegacyPacket>& events){
if(count>r.left()/12 || !budget.take(count,0))return false;
for(uint32_t i=0;i<count;++i){
const auto victim=r.le(8);
const auto interrupted=uint32_t(r.le(4));
// ObjectGuid::IsUnit admits players, creatures and pets.
const auto high=victim>>48;
if((high!=0 && high!=0xf130 && high!=0xf140) || !turtle_combat_detail::spell(interrupted))return false;
Bytes target;
if(!append_bound(target,victim,known) || !budget.take(0,8+caster.size()+target.size()+9))return false;
Bytes out=caster;
append(out,target);
// Spell::SendSpellInterruptLog assigns its own spell to InterruptedSpellID,
// then the victim's spell to SpellID. CombatLogPackets writes them in that order.
le(out,spell,4);le(out,interrupted,4);le(out,0,1); // HideFromCombatLog is false.
events.push_back({0x69000d,std::move(out)});
}
return true;
}
inline std::vector<LegacyPacket> spell_execute(std::span<const uint8_t> body,const Bindings& known,const Bindings& owned,ExecuteBudget budget){
Reader r(body);Bytes out;
if(!append_bound(out,turtle_combat_detail::packed(r),known))return {};
const auto spell=uint32_t(r.le(4)),group_count=uint32_t(r.le(4));
if(!turtle_combat_detail::spell(spell) || !group_count || group_count>3)return {};
struct Group { uint32_t effect,count; ExecuteList list; Bytes entries; };
std::vector<Group> groups;
std::vector<LegacyPacket> events;
for(uint32_t i=0;i<group_count;++i){
const auto effect=uint32_t(r.le(4)),count=uint32_t(r.le(4));
if(!count)return {};
if(effect==68){
if(!append_interrupts(r,count,out,spell,known,budget,events))return {};
continue;
}
const auto list=execute_list(effect);
if(!list || count>r.left()/4)return {};
// Source groups by up to three effect slots; native groups by effect
// type in first-seen order, even when multiple slots have the same type.
auto group=std::find_if(groups.begin(),groups.end(),[&](const auto& g){return g.effect==effect;});
if(group==groups.end()){
// One framed aggregate with caster, spell, group count and log-data bit;
// each distinct effect contributes its ID and six list counts.
if(groups.empty() && !budget.take(1,8+out.size()+4+4+1))return {};
if(!budget.take(0,4+6*4))return {};
groups.push_back({effect,0,*list,{}});
group=std::prev(groups.end());
}
group->count+=count;
Bytes entry;
for(uint32_t n=0;n<count;++n){
entry.clear();
if(!execute_target(r,entry,*list,effect,spell,known,owned) || !budget.take(0,entry.size()))return {};
append(group->entries,entry);
}
}
r.end();
// The native spell issues interrupts during its effects and sends the accumulated
// SpellExecuteLog in FinishTargetProcessing. Publish only after the whole source frame parses.
if(groups.empty())return events;
le(out,spell,4);le(out,groups.size(),4);
for(const auto& group:groups){
le(out,group.effect,4);
for(unsigned i=0;i<6;++i)le(out,i==unsigned(group.list)?group.count:0,4);
append(out,group.entries);
}
le(out,0,1); // Source supplies no advanced log data.
events.push_back({0x690033,std::move(out)});
return events;
}
inline void insert_spell_execute(std::vector<LegacyPacket>& output,size_t index,std::span<const uint8_t> body,
const Bindings& known,const Bindings& owned,size_t record_limit,size_t byte_limit){
if(output.size()>=record_limit || byte_limit<4)return;
ExecuteBudget budget{record_limit-output.size(),byte_limit-4}; // Bundle's packet count.
for(const auto& mandatory:output)
if(!budget.take(0,8) || !budget.take(0,mandatory.body.size()))return;
try{
auto events=spell_execute(body,known,owned,budget);
output.insert(output.begin()+index,std::make_move_iterator(events.begin()),std::make_move_iterator(events.end()));
}catch(const Error&){} // Incomplete evidence never affects other world output.
}
inline std::optional<LegacyPacket> translate(uint32_t opcode,std::span<const uint8_t> body,const Bindings& known){
std::optional<Bytes> payload;uint32_t native=0;
try{
switch(opcode){
case 0x151:native=0x690009;payload=energize(body,known);break;
case 0x1d0:native=0x470190;payload=xp_gain(body,known);break;
case 0x24b:native=0x690034;payload=spell_miss(body,known);break;
case 0x260:native=0x470206;payload=proc_resist(body,known);break;
default:return {};
}
}catch(const Error&){return {};} // A malformed observational notification stays isolated.
if(!payload)return {};
return LegacyPacket{native,std::move(*payload)};
}
}