/*
 * Lords of the Realm 2 - application network message frame
 *
 * Custom payload carried inside DirectPlay 3 Send/Receive (not a DirectPlay
 * struct). Every game-level message is a 4-byte header followed by a
 * fixed-length payload. The whole frame is handed to IDirectPlay3::Send and
 * read back from IDirectPlay3::Receive.
 *
 * Provenance: docs/structs/lotr2_net_msg.md
 * Confidence: likely (inferred from client code; no wire capture yet)
 */
#ifndef LOTR2_NET_MSG_H
#define LOTR2_NET_MSG_H

#include <stdint.h>

#pragma pack(push, 1)

/*
 * Frame header. Size = 4 bytes.
 *
 * Builder:  lotr2_net_send_message      // was FUN_00448641 @ 0x00448641
 * Parser:   lotr2_net_dispatch_app_message // was FUN_00448362 @ 0x00448362
 * Checksum: lotr2_net_checksum          // was FUN_00448c6c @ 0x00448c6c
 */
typedef struct lotr2_net_msg_header {
    uint8_t opcode;     /* +0x00 index into send/recv handler + length tables   */
    uint8_t length;     /* +0x01 payload byte count (== lotr2_net_opcode_len)   */
    uint8_t checksum;   /* +0x02 8-bit sum of the payload bytes only            */
    uint8_t seq_flags;  /* +0x03 bits0-5 send-slot/sequence; 0x40/0x80 = retry  */
} lotr2_net_msg_header; /* size 0x04 */

/*
 * Full frame as stored in a send slot. Max total size 0x104 (260) bytes,
 * so payload is capped at 0x100 (256); the 1-byte length field caps usable
 * payload at 255. Slot stride in the send-buffer array is 0x104.
 */
typedef struct lotr2_net_msg {
    lotr2_net_msg_header header; /* +0x00 */
    uint8_t payload[256];        /* +0x04 length-prefixed body, opcode-specific */
} lotr2_net_msg; /* max size 0x104 */

#pragma pack(pop)

/* seq_flags bit meanings (see lotr2_net_send_message / lotr2_net_dispatch_app_message) */
#define LOTR2_NET_SEQ_MASK        0x3f /* low 6 bits: rolling send-slot / sequence id */
#define LOTR2_NET_FLAG_RETRY_1    0x40 /* second of three reliable transmits          */
#define LOTR2_NET_FLAG_RETRY_2    0x80 /* third of three reliable transmits           */

/* Special opcodes */
#define LOTR2_NET_OPCODE_UNRELIABLE 10 /* opcode 10 is sent once (unreliable)         */

#endif /* LOTR2_NET_MSG_H */
