server: 0023-attack-preview-server

TICKET-378: the attack preview over the wire. CTOS/STOC 0x5E: the seat whose battle prompt is open asks for pairs, the host answers from its embedded core (ygol_preview_attack). Capability bit 0x400, protocol 8.

Replayed from Tools/YGOLegacy/patches/0023-attack-preview-server.patch in the Unity repo.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
donnyandClaude Fable 5.1 committed 2026-10-14 20:59:00 -07:00
1 parent b7c5cb2467
commit 3993b8c5e5
7 files changed
+89 -4

No files matched your search

+38
View File
@@ -500,6 +500,15 @@ void NetServer::HandleCTOSPacket(DuelPlayer* dp, unsigned char* data, size_t len
duel_mode->Profile(dp, pdata, len - 1);
break;
}
// YGOL attack preview: header + exactly count pairs, or dropped
case CTOS_ATTACK_PREVIEW: {
if(!dp->game || !duel_mode->pduel)
return;
if (len < 1 + 2 || len != 1 + 2 + pdata[1] * sizeof(CTOS_AttackPreviewPair))
return;
duel_mode->AttackPreview(dp, pdata, len - 1);
break;
}
case CTOS_UPDATE_DECK: {
if(!dp->game)
return;
@@ -690,6 +699,35 @@ size_t NetServer::CreateStampedPacket(unsigned char* src, int src_size, unsigned
std::memcpy(pdst, src, src_size);
return sizeof(dst_player_type) + src_size;
}
// YGOL attack preview: one answer per pair the open battle prompt could offer and the engine agrees to preview; the
// rest are left out, so the count is the number of answers. A header the client got wrong is dropped whole.
void NetServer::AnswerAttackPreview(DuelPlayer* dp, intptr_t pduel, uint8_t playerid, const std::vector<uint16_t>& attackers, unsigned char* pdata, int len) {
if(len < 2 || pdata[0] != ATTACK_PREVIEW_VERSION)
return;
const int count = pdata[1];
if(count == 0 || count > ATTACK_PREVIEW_MAX_PAIRS || len != 2 + count * (int)sizeof(CTOS_AttackPreviewPair))
return;
std::vector<unsigned char> buf;
buf.reserve(2 + count * (sizeof(CTOS_AttackPreviewPair) + ATTACK_PREVIEW_ANSWER_SIZE));
buf.push_back(ATTACK_PREVIEW_VERSION);
buf.push_back(0);
uint8_t answered = 0;
for(int i = 0; i < count; ++i) {
CTOS_AttackPreviewPair pair;
std::memcpy(&pair, pdata + 2 + i * sizeof pair, sizeof pair);
const uint16_t attacker = (uint16_t)(pair.attacker_location << 8 | pair.attacker_sequence);
if(std::find(attackers.begin(), attackers.end(), attacker) == attackers.end())
continue;
unsigned char answer[ATTACK_PREVIEW_ANSWER_SIZE];
if(ygol_preview_attack(pduel, playerid, pair.attacker_location, pair.attacker_sequence, pair.target_location, pair.target_sequence, answer) != 1)
continue;
buf.insert(buf.end(), (unsigned char*)&pair, (unsigned char*)&pair + sizeof pair);
buf.insert(buf.end(), answer, answer + sizeof answer);
++answered;
}
buf[1] = answered;
SendBufferToPlayer(dp, STOC_ATTACK_PREVIEW, buf.data(), buf.size());
}
// YGOL setup pairs
std::vector<std::string> setup_pairs;
bool IsSetupPairToken(const char* token) {
+1
View File
@@ -48,6 +48,7 @@ public:
static size_t CreateChatPacket(unsigned char* src, int src_size, unsigned char* dst, uint16_t dst_player_type);
static size_t CreateStampedPacket(unsigned char* src, int src_size, unsigned char* dst, uint16_t dst_player_type); // YGOL feature messages
static void SendSetupPairs(DuelPlayer* dp); // YGOL setup pairs
static void AnswerAttackPreview(DuelPlayer* dp, intptr_t pduel, uint8_t playerid, const std::vector<uint16_t>& attackers, unsigned char* pdata, int len); // YGOL attack preview
static inline bool ShouldHideFacedownCode(uint8_t position) {
return (position & POS_FACEDOWN) != 0 && (position & POS_REVEAL) == 0;
}
+24 -2
View File
@@ -224,7 +224,7 @@ static_assert(sizeof(CTOS_Appearance) == 352, "size mismatch: CTOS_Appearance");
check_trivially_copyable(CTOS_Dialogue);
static_assert(sizeof(CTOS_Dialogue) == 204, "size mismatch: CTOS_Dialogue");
constexpr int SIZE_STOC_FEATURE = sizeof(uint16_t) + sizeof(CTOS_Appearance);
constexpr uint16_t FEATURE_PROTOCOL = 7; // YGOL host seats: 1 -> 2; YGOL seat state: 2 -> 3; YGOL lobby after match: 3 -> 4; YGOL coin toss: 4 -> 5; YGOL deck lists: 5 -> 6; YGOL profile: 6 -> 7
constexpr uint16_t FEATURE_PROTOCOL = 8; // YGOL host seats: 1 -> 2; YGOL seat state: 2 -> 3; YGOL lobby after match: 3 -> 4; YGOL coin toss: 4 -> 5; YGOL deck lists: 5 -> 6; YGOL profile: 6 -> 7; YGOL attack preview: 7 -> 8
constexpr uint32_t FEATURE_APPEARANCE = 0x1;
constexpr uint32_t FEATURE_DIALOGUE = 0x2;
constexpr uint16_t FEATURE_SENDER_SERVER = 0xff;
@@ -446,6 +446,22 @@ check_trivially_copyable(CTOS_Profile);
static_assert(sizeof(CTOS_Profile) == 98, "size mismatch: CTOS_Profile");
constexpr uint32_t FEATURE_PROFILE = 0x200;
constexpr uint8_t PROFILE_VERSION = 1;
// YGOL attack preview: the seat whose battle prompt is open asks for pairs; the host's embedded core answers each one through ygol_preview_attack
#pragma pack(push, 1)
struct CTOS_AttackPreviewPair {
uint8_t attacker_location{};
uint8_t attacker_sequence{};
uint8_t target_location{}; // ATTACK_PREVIEW_DIRECT = a direct attack
uint8_t target_sequence{};
};
#pragma pack(pop)
check_trivially_copyable(CTOS_AttackPreviewPair);
static_assert(sizeof(CTOS_AttackPreviewPair) == 4, "size mismatch: CTOS_AttackPreviewPair");
constexpr uint32_t FEATURE_ATTACK_PREVIEW = 0x400;
constexpr uint8_t ATTACK_PREVIEW_VERSION = 1;
constexpr uint8_t ATTACK_PREVIEW_DIRECT = 0xFF;
constexpr int ATTACK_PREVIEW_MAX_PAIRS = 64;
constexpr int ATTACK_PREVIEW_ANSWER_SIZE = 24; // the ygol_preview_attack answer, echoed after the pair
// the one-use bootstrap secret of a replacement server: a host-string token parsed apart from the ygol.* pairs, never stored anywhere else
extern uint8_t bootstrap_secret[16];
extern bool bootstrap_closed;
@@ -507,6 +523,8 @@ public:
virtual void SeatState(DuelPlayer* dp, unsigned char* pdata, int len) = 0;
// YGOL profile
virtual void Profile(DuelPlayer* dp, unsigned char* pdata, int len) = 0;
// YGOL attack preview
virtual void AttackPreview(DuelPlayer* dp, unsigned char* pdata, int len) = 0;
// YGOL coin toss
virtual void ChooseTimeout() = 0;
virtual void JoinGame(DuelPlayer* dp, unsigned char* pdata, bool is_creater) = 0;
@@ -595,13 +613,16 @@ public:
#define CTOS_SEAT_STATE 0x59 // CTOS_SeatState, own seat only
// YGOL profile
#define CTOS_PROFILE 0x5D // CTOS_Profile, own seat only
// YGOL attack preview
#define CTOS_ATTACK_PREVIEW 0x5E // uint8_t version, uint8_t count, CTOS_AttackPreviewPair[count]; own open battle prompt only
inline bool IsFeatureMessage(unsigned char pktType) {
return pktType == CTOS_CAPABILITY || pktType == CTOS_APPEARANCE || pktType == CTOS_DIALOGUE
|| pktType == CTOS_SEAT_PLAN || pktType == CTOS_SEAT_REQUEST || pktType == CTOS_SEAT_SWAP || pktType == CTOS_SEAT_MOVE // YGOL host seats
|| pktType == CTOS_RESTART_PREPARE || pktType == CTOS_RESTART_ACK || pktType == CTOS_RESTART_CONTROL
|| pktType == CTOS_SEAT_RESUME || pktType == CTOS_RESTART_OPEN // YGOL rules restart
|| pktType == CTOS_SEAT_STATE // YGOL seat state
|| pktType == CTOS_PROFILE; // YGOL profile
|| pktType == CTOS_PROFILE // YGOL profile
|| pktType == CTOS_ATTACK_PREVIEW; // YGOL attack preview
}
#define STOC_GAME_MSG 0x1 // byte array
@@ -644,6 +665,7 @@ inline bool IsFeatureMessage(unsigned char pktType) {
#define STOC_COIN_TOSS 0x5B // STOC_CoinToss, no sender stamp (YGOL coin toss)
#define STOC_DECK_LIST 0x5C // STOC_DeckList + uint32_t codes[count], no sender stamp (YGOL deck lists)
#define STOC_PROFILE 0x5D // uint16_t + CTOS_Profile (YGOL profile)
#define STOC_ATTACK_PREVIEW 0x5E // uint8_t version, uint8_t count, count x (CTOS_AttackPreviewPair + 24-byte answer), no sender stamp, to the asker only (YGOL attack preview)
// STOC_GAME_MSG header
#define MSG_WAITING 3
+10 -1
View File
@@ -89,10 +89,16 @@ void SingleDuel::Profile(DuelPlayer* dp, unsigned char* pdata, int len) {
has_profile[dp->type] = true;
BroadcastFeature(STOC_PROFILE, pdata, len, dp->type);
}
// YGOL attack preview: only the seat whose battle prompt is open; the answer goes to that seat alone
void SingleDuel::AttackPreview(DuelPlayer* dp, unsigned char* pdata, int len) {
if(dp->type > 1 || duel_stage != DUEL_STAGE_DUELING || !pduel || last_game_msg != MSG_SELECT_BATTLECMD || dp != players[last_response])
return;
NetServer::AnswerAttackPreview(dp, pduel, (uint8_t)last_response, preview_attackers, pdata, len);
}
void SingleDuel::SendServerCapability(DuelPlayer* dp) {
CTOS_Capability cap;
cap.protocol = FEATURE_PROTOCOL;
cap.flags = FEATURE_APPEARANCE | FEATURE_DIALOGUE | FEATURE_SETUP_PAIRS | FEATURE_SEAT_PLAN | FEATURE_RULES_RESTART | FEATURE_SEAT_STATE | FEATURE_LOBBY_AFTER_MATCH | FEATURE_COIN_TOSS | FEATURE_DECK_LIST | FEATURE_PROFILE; // YGOL setup pairs + host seats + rules restart + seat state + lobby after match + coin toss + deck lists + profile
cap.flags = FEATURE_APPEARANCE | FEATURE_DIALOGUE | FEATURE_SETUP_PAIRS | FEATURE_SEAT_PLAN | FEATURE_RULES_RESTART | FEATURE_SEAT_STATE | FEATURE_LOBBY_AFTER_MATCH | FEATURE_COIN_TOSS | FEATURE_DECK_LIST | FEATURE_PROFILE | FEATURE_ATTACK_PREVIEW; // YGOL setup pairs + host seats + rules restart + seat state + lobby after match + coin toss + deck lists + profile + attack preview
unsigned char buf[SIZE_STOC_FEATURE];
const auto size = NetServer::CreateStampedPacket((unsigned char*)&cap, sizeof cap, buf, FEATURE_SENDER_SERVER);
NetServer::SendBufferToPlayer(dp, STOC_CAPABILITY, buf, size);
@@ -1594,6 +1600,9 @@ int SingleDuel::Analyze(unsigned char* msgbuffer, unsigned int len) {
count = BufferIO::Read<uint8_t>(pbuf);
pbuf += count * 11;
count = BufferIO::Read<uint8_t>(pbuf);
preview_attackers.clear(); // YGOL attack preview: 8 bytes per attacker, location at 5, sequence at 6
for(int i = 0; i < count; ++i)
preview_attackers.push_back((uint16_t)(pbuf[i * 8 + 5] << 8 | pbuf[i * 8 + 6]));
pbuf += count * 8 + 2;
RefreshMzone(0);
RefreshMzone(1);
+3
View File
@@ -31,6 +31,8 @@ public:
void SeatState(DuelPlayer* dp, unsigned char* pdata, int len) override;
// YGOL profile
void Profile(DuelPlayer* dp, unsigned char* pdata, int len) override;
// YGOL attack preview
void AttackPreview(DuelPlayer* dp, unsigned char* pdata, int len) override;
// YGOL coin toss
void ChooseTimeout() override;
void JoinGame(DuelPlayer* dp, unsigned char* pdata, bool is_creater) override;
@@ -96,6 +98,7 @@ protected:
// YGOL profile
bool has_profile[2]{};
CTOS_Profile profile[2]{};
std::vector<uint16_t> preview_attackers; // YGOL attack preview: location << 8 | sequence of every attacker the open battle prompt lists
void BeginChoose(int chooser, uint8_t face); // YGOL coin toss
// YGOL deck lists
void MarkDeckLists();
+10 -1
View File
@@ -87,10 +87,16 @@ void TagDuel::Profile(DuelPlayer* dp, unsigned char* pdata, int len) {
has_profile[dp->type] = true;
BroadcastFeature(STOC_PROFILE, pdata, len, dp->type);
}
// YGOL attack preview: only the seat whose battle prompt is open (the team's current hand); the answer goes to that seat alone
void TagDuel::AttackPreview(DuelPlayer* dp, unsigned char* pdata, int len) {
if(dp->type > 3 || duel_stage != DUEL_STAGE_DUELING || !pduel || last_game_msg != MSG_SELECT_BATTLECMD || dp != cur_player[last_response])
return;
NetServer::AnswerAttackPreview(dp, pduel, (uint8_t)last_response, preview_attackers, pdata, len);
}
void TagDuel::SendServerCapability(DuelPlayer* dp) {
CTOS_Capability cap;
cap.protocol = FEATURE_PROTOCOL;
cap.flags = FEATURE_APPEARANCE | FEATURE_DIALOGUE | FEATURE_SETUP_PAIRS | FEATURE_SEAT_PLAN | FEATURE_RULES_RESTART | FEATURE_SEAT_STATE | FEATURE_LOBBY_AFTER_MATCH | FEATURE_COIN_TOSS | FEATURE_DECK_LIST | FEATURE_PROFILE; // YGOL setup pairs + host seats + rules restart + seat state + lobby after match + coin toss + deck lists + profile
cap.flags = FEATURE_APPEARANCE | FEATURE_DIALOGUE | FEATURE_SETUP_PAIRS | FEATURE_SEAT_PLAN | FEATURE_RULES_RESTART | FEATURE_SEAT_STATE | FEATURE_LOBBY_AFTER_MATCH | FEATURE_COIN_TOSS | FEATURE_DECK_LIST | FEATURE_PROFILE | FEATURE_ATTACK_PREVIEW; // YGOL setup pairs + host seats + rules restart + seat state + lobby after match + coin toss + deck lists + profile + attack preview
unsigned char buf[SIZE_STOC_FEATURE];
const auto size = NetServer::CreateStampedPacket((unsigned char*)&cap, sizeof cap, buf, FEATURE_SENDER_SERVER);
NetServer::SendBufferToPlayer(dp, STOC_CAPABILITY, buf, size);
@@ -1556,6 +1562,9 @@ int TagDuel::Analyze(unsigned char* msgbuffer, unsigned int len) {
count = BufferIO::Read<uint8_t>(pbuf);
pbuf += count * 11;
count = BufferIO::Read<uint8_t>(pbuf);
preview_attackers.clear(); // YGOL attack preview: 8 bytes per attacker, location at 5, sequence at 6
for(int i = 0; i < count; ++i)
preview_attackers.push_back((uint16_t)(pbuf[i * 8 + 5] << 8 | pbuf[i * 8 + 6]));
pbuf += count * 8 + 2;
RefreshMzone(0);
RefreshMzone(1);
+3
View File
@@ -31,6 +31,8 @@ public:
void SeatState(DuelPlayer* dp, unsigned char* pdata, int len) override;
// YGOL profile
void Profile(DuelPlayer* dp, unsigned char* pdata, int len) override;
// YGOL attack preview
void AttackPreview(DuelPlayer* dp, unsigned char* pdata, int len) override;
// YGOL coin toss
void ChooseTimeout() override;
void JoinGame(DuelPlayer* dp, unsigned char* pdata, bool is_creater) override;
@@ -96,6 +98,7 @@ protected:
// YGOL profile
bool has_profile[4]{};
CTOS_Profile profile[4]{};
std::vector<uint16_t> preview_attackers; // YGOL attack preview: location << 8 | sequence of every attacker the open battle prompt lists
// YGOL coin toss
void BeginChoose(int chooser, uint8_t face);
int choose_seat{};