Files

69 lines
3.1 KiB
C++

#pragma once
#include "gateway/protocol.hpp"
#include "gateway/source_skill_profile.hpp"
#include <array>
#include <set>
#include <vector>
namespace gateway {
// Tortoise::_LoadSkills writes only the skill ID in the first word. Forever
// Player::_LoadSkills reconstructs profession steps from max / 75 when absent.
// This is destination metadata only; never change source rank, cap or bonuses.
inline uint16_t profession_skill_step(uint32_t id_step,uint32_t rank_max){
const auto step=uint16_t(id_step>>16);
if(step || !source_skills::is_ranked_profession(id_step&0xffff))return step;
return uint16_t((rank_max>>16)/75);
}
struct ProfessionChildView {
unsigned slot;
uint16_t id,step,rank,max_rank,temp_bonus,perm_bonus;
};
// Reserve destination slots >=128 so every source slot retains its identity.
// These are views of learned source professions, not extra server skills.
template<class ReadField,class Emit>
void profession_child_views(ReadField read,uint32_t skill_start,Emit emit){
static_assert(sizeof(source_skills::ProfessionChildren)/sizeof(source_skills::ProfessionChildren[0])<=300-128);
unsigned child_slot=128;
for(const auto& pair:source_skills::ProfessionChildren){
bool found=false;
for(unsigned slot=0;slot<128;++slot){
const auto id_step=read(skill_start+3*slot);
if((id_step&0xffff)==pair.child)throw Error("Source skill collides with destination profession view");
if((id_step&0xffff)!=pair.parent)continue;
if(found)throw Error("Duplicate source profession view");
found=true;
const auto rank_max=read(skill_start+3*slot+1),bonus=read(skill_start+3*slot+2);
if(!(rank_max&0xffff))continue;
emit(ProfessionChildView{child_slot,pair.child,profession_skill_step(id_step,rank_max),
uint16_t(rank_max),uint16_t(rank_max>>16),uint16_t(bonus),uint16_t(bonus>>16)});
}
++child_slot;
}
}
// Keep the complete learned set separate from the two native summary slots.
// Sparse updates need the complete set to preserve still-learned slot owners.
template<class ReadField>
std::vector<int32_t> learned_primary_professions(ReadField read,uint32_t skill_start){
std::vector<int32_t> result;
std::set<uint32_t> seen;
for(unsigned slot=0;slot<128;++slot){
const uint32_t id=read(skill_start+3*slot)&0xffff;
if(!id || !(read(skill_start+3*slot+1)&0xffff) || !source_skills::is_primary_profession(id))continue;
if(!seen.insert(id).second)throw Error("Duplicate source primary profession");
result.push_back(int32_t(id));
}
return result;
}
// Initial self-create picks the first two in Source slot order. All SkillInfo
// rows and child views remain independent; extra professions never reject login.
template<class ReadField>
std::array<int32_t,2> primary_profession_lines(ReadField read,uint32_t skill_start){
std::array<int32_t,2> result{};unsigned count=0;
for(auto id:learned_primary_professions(read,skill_start)){
if(count==result.size())break;
result[count++]=id;
}
return result;
}
}