Each game core snapshots its timed boss regens once a second and shares them over the original HEADER_GG_RELAY addressed to "#mt_boss", so old cores just miss the player lookup instead of dropping the P2P link. New /mt_features query kind "bosses" (capability bit 256, mask 511) lists the player's channel plus the shared channel 99. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
87 lines
4.2 KiB
C++
87 lines
4.2 KiB
C++
#ifndef PORT_BOSS_TRACK_H
|
|
#define PORT_BOSS_TRACK_H
|
|
#include <algorithm>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
// World boss tracking (AVA2 uitrack world-boss tab), read-only. Every core snapshots its own boss regens once a
|
|
// second and shares them over the original HEADER_GG_RELAY addressed to a name no player can have, so cores
|
|
// without this code just find no such player and skip the payload (a new GG header would make them close the link).
|
|
static const char kPortBossRelayName[]="#mt_boss";
|
|
static const unsigned kPortBossSharedChannel=99; // the shared core (game99) serves every channel
|
|
static const unsigned kPortBossMaxRows=64; // per core
|
|
static const unsigned kPortBossMaxTotal=128; // per query
|
|
static const long long kPortBossExpire=150; // a peer that stopped sending (crash, shutdown) drops out
|
|
static const long long kPortBossHeartbeat=60;
|
|
#pragma pack(push,1)
|
|
struct PortBossWire {
|
|
unsigned vnum,map;int x,y;unsigned period,next;unsigned char alive,max;
|
|
};
|
|
struct PortBossHeader {
|
|
unsigned short port;unsigned char channel,count;
|
|
};
|
|
#pragma pack(pop)
|
|
struct PortBossRow {
|
|
PortBossWire wire;unsigned channel;
|
|
};
|
|
static bool PortBossSame(const std::vector<PortBossWire>& a,const std::vector<PortBossWire>& b) {
|
|
if(a.size()!=b.size())return false;
|
|
for(size_t i=0;i<a.size();++i) {
|
|
const PortBossWire& x=a[i];const PortBossWire& y=b[i];
|
|
// next is now + remaining pulses, so it may move by a second between two snapshots.
|
|
if(x.vnum!=y.vnum || x.map!=y.map || x.alive!=y.alive || x.max!=y.max || x.period!=y.period ||
|
|
std::llabs((long long)x.next-(long long)y.next)>2)return false;
|
|
}
|
|
return true;
|
|
}
|
|
class PortBossBoard {
|
|
public:
|
|
// Returns true when the new local snapshot should be broadcast.
|
|
bool SetLocal(const std::vector<PortBossWire>& rows,unsigned channel,long long now) {
|
|
local=rows;if(local.size()>kPortBossMaxRows)local.resize(kPortBossMaxRows);localChannel=channel;
|
|
if(sentAt && PortBossSame(local,sent) && now-sentAt<kPortBossHeartbeat)return false;
|
|
sent=local;sentAt=now;return true;
|
|
}
|
|
std::string Encode(unsigned short port) const {
|
|
PortBossHeader h={port,(unsigned char)localChannel,(unsigned char)local.size()};
|
|
std::string out((const char*)&h,sizeof(h));
|
|
if(!local.empty())out.append((const char*)&local[0],local.size()*sizeof(PortBossWire));
|
|
return out;
|
|
}
|
|
// host is the sender's address; host+port names one core. False for a malformed payload.
|
|
bool Receive(const std::string& host,const char* data,long size,long long now) {
|
|
if(size<(long)sizeof(PortBossHeader))return false;
|
|
PortBossHeader h;memcpy(&h,data,sizeof(h));
|
|
if(h.count>kPortBossMaxRows || size!=(long)(sizeof(h)+h.count*sizeof(PortBossWire)))return false;
|
|
Remote& r=remote[host+":"+std::to_string(h.port)];
|
|
r.channel=h.channel;r.at=now;r.rows.resize(h.count);
|
|
if(h.count)memcpy(&r.rows[0],data+sizeof(h),h.count*sizeof(PortBossWire));
|
|
return true;
|
|
}
|
|
// Rows a player of channel sees: that channel and the shared core, sorted by map then boss.
|
|
std::vector<PortBossRow> Rows(unsigned channel,long long now) {
|
|
std::vector<PortBossRow> out;
|
|
auto add=[&](const std::vector<PortBossWire>& rows,unsigned c) {
|
|
if(c!=channel && c!=kPortBossSharedChannel)return;
|
|
for(const auto& w:rows)out.push_back({w,c});
|
|
};
|
|
add(local,localChannel);
|
|
for(auto it=remote.begin();it!=remote.end();) {
|
|
if(now-it->second.at>kPortBossExpire){it=remote.erase(it);continue;}
|
|
add(it->second.rows,it->second.channel);++it;
|
|
}
|
|
std::stable_sort(out.begin(),out.end(),[](const PortBossRow& a,const PortBossRow& b) {
|
|
return a.wire.map!=b.wire.map?a.wire.map<b.wire.map:a.wire.vnum!=b.wire.vnum?a.wire.vnum<b.wire.vnum:a.channel<b.channel;
|
|
});
|
|
if(out.size()>kPortBossMaxTotal)out.resize(kPortBossMaxTotal);
|
|
return out;
|
|
}
|
|
private:
|
|
struct Remote {unsigned channel=0;long long at=0;std::vector<PortBossWire> rows;};
|
|
std::vector<PortBossWire> local,sent;unsigned localChannel=0;long long sentAt=0;
|
|
std::map<std::string,Remote> remote;
|
|
};
|
|
#endif
|