117 lines
5.9 KiB
C++
117 lines
5.9 KiB
C++
#pragma once
|
|
#include "gateway/professions.hpp"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <map>
|
|
#include <set>
|
|
#include <vector>
|
|
|
|
namespace gateway {
|
|
inline constexpr unsigned SourceSkillFirst=718; // Pinned by static_assert against the Source SDK.
|
|
// Session-scoped Source SELF skill words. A full create defines omitted words
|
|
// as zero; a later values update only replaces the words that it contains.
|
|
struct ProfessionSkillRow {
|
|
uint16_t slot=0,id=0,step=0,rank=0,starting=0,maximum=0,temporary=0,permanent=0;
|
|
bool operator==(const ProfessionSkillRow&) const = default;
|
|
};
|
|
struct ProfessionDelta {
|
|
std::vector<ProfessionSkillRow> rows;
|
|
std::array<int32_t,2> slots{};
|
|
uint8_t slot_mask=0;
|
|
std::set<unsigned> source_slots;
|
|
};
|
|
inline Bytes profession_delta_input(uint16_t realm,uint32_t map,uint64_t self,const ProfessionDelta& delta){
|
|
if(!realm || realm>8191 || !self || map>8191 || delta.slot_mask>3 || delta.rows.size()>140)
|
|
throw Error("Profession delta context bound");
|
|
Bytes out;le(out,realm,2);le(out,map,4);le(out,self,8);le(out,delta.slot_mask,1);
|
|
for(unsigned i=0;i<2;++i)if(delta.slot_mask&(1u<<i))le(out,uint32_t(delta.slots[i]),4);
|
|
le(out,delta.rows.size(),2);
|
|
for(const auto& r:delta.rows){
|
|
le(out,r.slot,2);le(out,r.id,2);le(out,r.step,2);le(out,r.rank,2);le(out,r.starting,2);
|
|
le(out,r.maximum,2);le(out,r.temporary,2);le(out,r.permanent,2);
|
|
}
|
|
return out;
|
|
}
|
|
class ProfessionSkillState {
|
|
std::array<uint32_t,384> words_{};
|
|
std::array<int32_t,2> primary_{};
|
|
bool ready_=false;
|
|
uint64_t owner_guid_=0;
|
|
static bool has_child(uint16_t id) {
|
|
for(const auto& p:source_skills::ProfessionChildren)if(p.parent==id)return true;
|
|
return false;
|
|
}
|
|
static bool relevant(uint16_t id) {
|
|
return source_skills::is_ranked_profession(id)||has_child(id);
|
|
}
|
|
static ProfessionSkillRow row(unsigned slot,uint16_t id,uint32_t first,uint32_t rank,uint32_t bonus) {
|
|
return {uint16_t(slot),id,profession_skill_step(first,rank),uint16_t(rank),
|
|
uint16_t(id?1:0),uint16_t(rank>>16),uint16_t(bonus),uint16_t(bonus>>16)};
|
|
}
|
|
auto read() const {return [this](uint32_t i){return words_.at(i);};}
|
|
std::map<unsigned,ProfessionSkillRow> children() const {
|
|
std::map<unsigned,ProfessionSkillRow> result;
|
|
profession_child_views(read(),0,[&](const ProfessionChildView& v){
|
|
result.emplace(v.slot,ProfessionSkillRow{uint16_t(v.slot),v.id,v.step,v.rank,1,v.max_rank,v.temp_bonus,v.perm_bonus});
|
|
});
|
|
return result;
|
|
}
|
|
public:
|
|
void clear() noexcept {words_.fill(0);primary_.fill(0);ready_=false;owner_guid_=0;}
|
|
bool initialized() const noexcept {return ready_;}
|
|
bool has_learned_skill(uint64_t owner_guid, uint32_t skill) const noexcept {
|
|
if (!ready_ || !owner_guid || owner_guid != owner_guid_ || !skill) return false;
|
|
for (size_t i = 0; i < words_.size(); i += 3)
|
|
if ((words_[i] & 0xffffu) == skill && (words_[i+1] & 0xffffu)) return true;
|
|
return false;
|
|
}
|
|
template<class Fields> void initialize(const Fields& fields,uint32_t start,uint64_t owner_guid=0) {
|
|
clear(); // A failed replacement create cannot retain the previous owner or skills.
|
|
ProfessionSkillState next;
|
|
for(const auto& [index,value]:fields)if(index>=start && index<start+384)next.words_[index-start]=value;
|
|
next.primary_=primary_profession_lines(next.read(),0);
|
|
(void)next.children(); // Validate duplicate/colliding source rows at the create boundary.
|
|
next.owner_guid_=owner_guid;next.ready_=true;*this=std::move(next);
|
|
}
|
|
template<class Fields> ProfessionDelta observe(const Fields& fields,uint32_t start,uint64_t owner_guid=0) {
|
|
ProfessionDelta delta;
|
|
if (owner_guid != owner_guid_) clear();
|
|
if (!ready_) return delta;
|
|
ProfessionSkillState next=*this;
|
|
std::set<unsigned> touched;
|
|
for(const auto& [index,value]:fields)if(index>=start && index<start+384){
|
|
unsigned offset=index-start;next.words_[offset]=value;touched.insert(offset/3);
|
|
}
|
|
if(touched.empty())return delta;
|
|
// Validate the complete reconstructed snapshot, never a sparse fragment.
|
|
auto actual=learned_primary_professions(next.read(),0);
|
|
auto old_children=children(),new_children=next.children();
|
|
for(auto& id:next.primary_)if(id && std::find(actual.begin(),actual.end(),id)==actual.end())id=0;
|
|
for(auto id:actual)if(id && std::find(next.primary_.begin(),next.primary_.end(),id)==next.primary_.end()){
|
|
auto empty=std::find(next.primary_.begin(),next.primary_.end(),0);
|
|
if(empty==next.primary_.end())break; // Extra learned lines retain SkillInfo/child rows.
|
|
*empty=id;
|
|
}
|
|
for(unsigned i=0;i<2;++i)if(primary_[i]!=next.primary_[i])delta.slot_mask|=uint8_t(1u<<i);
|
|
delta.slots=next.primary_;
|
|
for(auto slot:touched){
|
|
auto before=uint16_t(words_[3*slot]),after=uint16_t(next.words_[3*slot]);
|
|
if(!relevant(before)&&!relevant(after))continue;
|
|
delta.source_slots.insert(slot);
|
|
delta.rows.push_back(row(slot,after,next.words_[3*slot],next.words_[3*slot+1],next.words_[3*slot+2]));
|
|
}
|
|
for(unsigned i=0;i<std::size(source_skills::ProfessionChildren);++i){
|
|
const unsigned slot=128+i;
|
|
auto a=old_children.find(slot),b=new_children.find(slot);
|
|
if(a!=old_children.end() && b!=new_children.end() && a->second==b->second)continue;
|
|
if(b!=new_children.end())delta.rows.push_back(b->second);
|
|
else if(a!=old_children.end()){
|
|
// Native SetSkill deactivation retains line ID/starting rank.
|
|
delta.rows.push_back(ProfessionSkillRow{uint16_t(slot),a->second.id,0,0,1,0,0,0});
|
|
}
|
|
}
|
|
*this=std::move(next);return delta;
|
|
}
|
|
};
|
|
}
|