pokeemerald/include/link.h

349 lines
11 KiB
C
Raw Normal View History

2017-09-11 11:35:41 +02:00
#ifndef GUARD_LINK_H
#define GUARD_LINK_H
#define MAX_LINK_PLAYERS 4
2017-11-18 21:38:57 +01:00
#define MAX_RFU_PLAYERS 5
2017-09-11 11:35:41 +02:00
#define CMD_LENGTH 8
#define QUEUE_CAPACITY 50
#define OVERWORLD_RECV_QUEUE_MAX 3
2017-09-11 11:35:41 +02:00
#define BLOCK_BUFFER_SIZE 0x100
2017-11-14 14:31:37 +01:00
#define LINK_SLAVE 0
#define LINK_MASTER 8
2017-09-11 11:35:41 +02:00
#define LINK_STAT_LOCAL_ID 0x00000003
#define LINK_STAT_PLAYER_COUNT 0x0000001C
#define LINK_STAT_PLAYER_COUNT_SHIFT 2
#define LINK_STAT_MASTER 0x00000020
#define LINK_STAT_MASTER_SHIFT 5
#define LINK_STAT_CONN_ESTABLISHED 0x00000040
#define LINK_STAT_CONN_ESTABLISHED_SHIFT 6
#define LINK_STAT_RECEIVED_NOTHING 0x00000100
#define LINK_STAT_RECEIVED_NOTHING_SHIFT 8
2017-11-14 14:31:37 +01:00
#define LINK_STAT_UNK_FLAG_9 0x00000200
#define LINK_STAT_UNK_FLAG_9_SHIFT 9
2017-09-11 11:35:41 +02:00
#define LINK_STAT_ERRORS 0x0007F000
2017-11-13 05:16:51 +01:00
#define LINK_STAT_ERRORS_SHIFT 12
2017-09-11 11:35:41 +02:00
2017-11-14 14:31:37 +01:00
#define LINK_STAT_ERROR_HARDWARE 0x00001000
#define LINK_STAT_ERROR_HARDWARE_SHIFT 12
#define LINK_STAT_ERROR_CHECKSUM 0x00002000
#define LINK_STAT_ERROR_CHECKSUM_SHIFT 13
#define LINK_STAT_ERROR_QUEUE_FULL 0x00004000
#define LINK_STAT_ERROR_QUEUE_FULL_SHIFT 14
#define LINK_STAT_ERROR_LAG_MASTER 0x00010000
#define LINK_STAT_ERROR_LAG_MASTER_SHIFT 16
#define LINK_STAT_ERROR_INVALID_ID 0x00020000
#define LINK_STAT_ERROR_INVALID_ID_SHIFT 17
#define LINK_STAT_ERROR_LAG_SLAVE 0x00040000
#define LINK_STAT_ERROR_LAG_SLAVE_SHIFT 18
2017-09-11 11:35:41 +02:00
#define EXTRACT_PLAYER_COUNT(status) \
(((status) & LINK_STAT_PLAYER_COUNT) >> LINK_STAT_PLAYER_COUNT_SHIFT)
#define EXTRACT_MASTER(status) \
(((status) >> LINK_STAT_MASTER_SHIFT) & 1)
#define EXTRACT_CONN_ESTABLISHED(status) \
(((status) >> LINK_STAT_CONN_ESTABLISHED_SHIFT) & 1)
#define EXTRACT_RECEIVED_NOTHING(status) \
(((status) >> LINK_STAT_RECEIVED_NOTHING_SHIFT) & 1)
2017-11-13 05:16:51 +01:00
#define EXTRACT_LINK_ERRORS(status) \
(((status) & LINK_STAT_ERRORS) >> LINK_STAT_ERRORS_SHIFT)
2017-09-11 11:35:41 +02:00
2020-08-24 20:52:33 +02:00
#define LINKCMD_BLENDER_STOP 0x1111
#define LINKCMD_SEND_LINK_TYPE 0x2222
#define LINKCMD_BLENDER_SCORE_MISS 0x2345
#define LINKCMD_READY_EXIT_STANDBY 0x2FFE
#define LINKCMD_SEND_PACKET 0x2FFF
#define LINKCMD_BLENDER_SEND_KEYS 0x4444
#define LINKCMD_BLENDER_SCORE_BEST 0x4523
#define LINKCMD_BLENDER_SCORE_GOOD 0x5432
2021-03-19 19:10:13 +01:00
#define LINKCMD_DUMMY_1 0x5555
#define LINKCMD_DUMMY_2 0x5566
2020-08-24 20:52:33 +02:00
#define LINKCMD_READY_CLOSE_LINK 0x5FFF
2021-03-19 19:10:13 +01:00
#define LINKCMD_SEND_EMPTY 0x6666
#define LINKCMD_SEND_0xEE 0x7777
2020-08-24 20:52:33 +02:00
#define LINKCMD_BLENDER_PLAY_AGAIN 0x7779
2021-03-11 20:16:40 +01:00
#define LINKCMD_COUNTDOWN 0x7FFF
2020-08-24 20:52:33 +02:00
#define LINKCMD_CONT_BLOCK 0x8888
#define LINKCMD_BLENDER_NO_BERRIES 0x9999
#define LINKCMD_BLENDER_NO_PBLOCK_SPACE 0xAAAA
2021-03-19 19:10:13 +01:00
#define LINKCMD_SEND_ITEM 0xAAAB
2020-08-24 20:52:33 +02:00
#define LINKCMD_READY_TO_TRADE 0xAABB
#define LINKCMD_READY_FINISH_TRADE 0xABCD
#define LINKCMD_INIT_BLOCK 0xBBBB
#define LINKCMD_READY_CANCEL_TRADE 0xBBCC
#define LINKCMD_SEND_HELD_KEYS 0xCAFE
#define LINKCMD_SEND_BLOCK_REQ 0xCCCC
#define LINKCMD_START_TRADE 0xCCDD
#define LINKCMD_CONFIRM_FINISH_TRADE 0xDCBA
#define LINKCMD_SET_MONS_TO_TRADE 0xDDDD
2021-03-19 19:10:13 +01:00
#define LINKCMD_PLAYER_CANCEL_TRADE 0xDDEE
2020-08-24 20:52:33 +02:00
#define LINKCMD_REQUEST_CANCEL 0xEEAA
2021-03-19 19:10:13 +01:00
#define LINKCMD_BOTH_CANCEL_TRADE 0xEEBB
#define LINKCMD_PARTNER_CANCEL_TRADE 0xEECC
#define LINKCMD_NONE 0xEFFF
2019-10-07 04:58:25 +02:00
2020-08-24 20:52:33 +02:00
#define LINKTYPE_TRADE 0x1111
#define LINKTYPE_TRADE_CONNECTING 0x1122
#define LINKTYPE_TRADE_SETUP 0x1133
#define LINKTYPE_TRADE_DISCONNECTED 0x1144
#define LINKTYPE_BATTLE 0x2211
2021-03-19 19:10:13 +01:00
#define LINKTYPE_UNUSED_BATTLE 0x2222 // Unused, inferred from gap
2020-08-24 20:52:33 +02:00
#define LINKTYPE_SINGLE_BATTLE 0x2233
#define LINKTYPE_DOUBLE_BATTLE 0x2244
#define LINKTYPE_MULTI_BATTLE 0x2255
#define LINKTYPE_BATTLE_TOWER_50 0x2266
#define LINKTYPE_BATTLE_TOWER_OPEN 0x2277
2020-08-24 20:52:33 +02:00
#define LINKTYPE_BATTLE_TOWER 0x2288
#define LINKTYPE_RECORD_MIX_BEFORE 0x3311
#define LINKTYPE_RECORD_MIX_AFTER 0x3322
#define LINKTYPE_BERRY_BLENDER_SETUP 0x4411
#define LINKTYPE_BERRY_BLENDER 0x4422
#define LINKTYPE_MYSTERY_EVENT 0x5501
2021-03-19 19:10:13 +01:00
#define LINKTYPE_UNUSED_EREADER 0x5502 // Unused, inferred from gap
2020-08-24 20:52:33 +02:00
#define LINKTYPE_EREADER 0x5503
#define LINKTYPE_CONTEST_GMODE 0x6601
#define LINKTYPE_CONTEST_EMODE 0x6602
2017-11-14 14:31:37 +01:00
enum {
BLOCK_REQ_SIZE_NONE, // Identical to 200
BLOCK_REQ_SIZE_200,
BLOCK_REQ_SIZE_100,
BLOCK_REQ_SIZE_220,
BLOCK_REQ_SIZE_40,
};
2017-11-11 04:34:23 +01:00
struct LinkStatus
{
u32 localId:2;
u32 playerCount:3;
u32 master:1;
u32 connEstablished:1;
u32 unused_7:1;
u32 receivedNothing:1;
u32 unused_9:7;
u32 errors:7;
};
2017-09-11 11:35:41 +02:00
2021-10-26 19:37:24 +02:00
#define MASTER_HANDSHAKE 0x8FFF
#define SLAVE_HANDSHAKE 0xB9A0
#define EREADER_HANDSHAKE 0xCCD0
2017-09-11 11:35:41 +02:00
2017-11-15 14:44:11 +01:00
#define SIO_MULTI_CNT ((struct SioMultiCnt *)REG_ADDR_SIOCNT)
2017-11-11 04:08:17 +01:00
2017-09-11 11:35:41 +02:00
enum
{
LINK_STATE_START0,
LINK_STATE_START1,
LINK_STATE_HANDSHAKE,
LINK_STATE_INIT_TIMER,
LINK_STATE_CONN_ESTABLISHED,
};
enum
{
EXCHANGE_NOT_STARTED,
EXCHANGE_COMPLETE,
EXCHANGE_TIMED_OUT,
2020-06-04 00:00:53 +02:00
EXCHANGE_DIFF_SELECTIONS,
2019-10-10 09:50:51 +02:00
EXCHANGE_PLAYER_NOT_READY,
EXCHANGE_PARTNER_NOT_READY,
2020-06-04 00:00:53 +02:00
EXCHANGE_WRONG_NUM_PLAYERS,
2018-10-11 23:02:58 +02:00
EXCHANGE_STAT_7
2017-09-11 11:35:41 +02:00
};
enum
{
QUEUE_FULL_NONE,
QUEUE_FULL_SEND,
QUEUE_FULL_RECV,
};
enum
{
LAG_NONE,
LAG_MASTER,
LAG_SLAVE,
};
struct LinkPlayer
{
/* 0x00 */ u16 version;
/* 0x02 */ u16 lp_field_2;
/* 0x04 */ u32 trainerId;
2019-10-09 11:56:44 +02:00
/* 0x08 */ u8 name[PLAYER_NAME_LENGTH + 1];
2019-10-10 09:50:51 +02:00
/* 0x10 */ u8 progressFlags; // (& 0x0F) is hasNationalDex, (& 0xF0) is hasClearedGame
2019-10-09 11:56:44 +02:00
/* 0x11 */ u8 neverRead;
/* 0x12 */ u8 progressFlagsCopy;
2017-09-11 11:35:41 +02:00
/* 0x13 */ u8 gender;
/* 0x14 */ u32 linkType;
2018-06-04 12:07:32 +02:00
/* 0x18 */ u16 id; // battler id in battles
2017-09-11 11:35:41 +02:00
/* 0x1A */ u16 language;
};
struct LinkPlayerBlock
{
2017-11-13 07:20:38 +01:00
char magic1[16];
2017-09-11 11:35:41 +02:00
struct LinkPlayer linkPlayer;
2017-11-13 07:20:38 +01:00
char magic2[16];
2017-09-11 11:35:41 +02:00
};
// circular queues
struct SendQueue
{
2017-11-13 07:12:34 +01:00
/* 0x000 */ u16 data[CMD_LENGTH][QUEUE_CAPACITY];
/* 0x320 */ u8 pos;
/* 0x321 */ u8 count;
2017-09-11 11:35:41 +02:00
};
struct RecvQueue
{
u16 data[MAX_LINK_PLAYERS][CMD_LENGTH][QUEUE_CAPACITY];
u8 pos;
u8 count;
};
struct Link
{
2017-11-13 07:12:34 +01:00
/* 0x000 */ u8 isMaster; // 0: slave, 8: master
/* 0x001 */ u8 state;
/* 0x002 */ u8 localId; // local multi-player ID
/* 0x003 */ u8 playerCount;
2021-10-26 19:37:24 +02:00
/* 0x004 */ u16 handshakeBuffer[MAX_LINK_PLAYERS];
2017-11-13 07:12:34 +01:00
/* 0x00c */ bool8 receivedNothing;
/* 0x00d */ s8 serialIntrCounter;
/* 0x00e */ bool8 handshakeAsMaster;
/* 0x00f */ u8 link_field_F;
2017-09-11 11:35:41 +02:00
// error conditions
2017-11-13 07:12:34 +01:00
/* 0x010 */ bool8 hardwareError; // hardware reported an error
/* 0x011 */ bool8 badChecksum; // checksum didn't match between devices
/* 0x012 */ u8 queueFull; // send or recv queue out of space
/* 0x013 */ u8 lag; // connection is lagging
2017-09-11 11:35:41 +02:00
2017-11-13 07:12:34 +01:00
/* 0x014 */ u16 checksum;
2017-09-11 11:35:41 +02:00
2017-11-13 07:12:34 +01:00
/* 0x016 */ u8 sendCmdIndex;
/* 0x017 */ u8 recvCmdIndex;
2017-09-11 11:35:41 +02:00
2017-11-13 07:12:34 +01:00
/* 0x018 */ struct SendQueue sendQueue;
/* 0x33c */ struct RecvQueue recvQueue;
2017-09-11 11:35:41 +02:00
};
struct BlockRequest
{
void * address;
u32 size;
};
extern struct Link gLink;
2017-11-18 21:38:57 +01:00
extern u16 gRecvCmds[MAX_RFU_PLAYERS][CMD_LENGTH];
2017-09-11 11:35:41 +02:00
extern u8 gBlockSendBuffer[BLOCK_BUFFER_SIZE];
extern u16 gLinkType;
extern u32 gLinkStatus;
2017-11-11 04:34:23 +01:00
extern u16 gBlockRecvBuffer[MAX_RFU_PLAYERS][BLOCK_BUFFER_SIZE / 2];
2017-09-11 11:35:41 +02:00
extern u16 gSendCmd[CMD_LENGTH];
extern struct LinkPlayer gLinkPlayers[MAX_RFU_PLAYERS];
2017-09-11 11:35:41 +02:00
extern bool8 gReceivedRemoteLinkPlayers;
2020-08-24 20:52:33 +02:00
extern u32 gBerryBlenderKeySendAttempts;
2017-10-13 17:22:50 +02:00
extern bool8 gLinkVSyncDisabled;
2018-01-30 23:23:35 +01:00
extern u32 gLinkStatus;
2017-09-11 11:35:41 +02:00
2018-02-11 23:19:36 +01:00
2018-07-15 13:23:38 +02:00
bool8 IsWirelessAdapterConnected(void);
2018-01-30 23:23:35 +01:00
void Task_DestroySelf(u8 taskId);
2017-09-11 11:35:41 +02:00
void OpenLink(void);
void CloseLink(void);
2017-11-11 06:05:44 +01:00
u16 LinkMain2(const u16 *);
2017-09-11 11:35:41 +02:00
void ClearLinkCallback(void);
void ClearLinkCallback_2(void);
u8 GetLinkPlayerCount(void);
void OpenLinkTimed(void);
2017-11-11 06:05:44 +01:00
u8 GetLinkPlayerDataExchangeStatusTimed(int lower, int upper);
2017-09-11 11:35:41 +02:00
bool8 IsLinkPlayerDataExchangeComplete(void);
u32 GetLinkPlayerTrainerId(u8);
void ResetLinkPlayers(void);
u8 GetMultiplayerId(void);
u8 BitmaskAllOtherLinkPlayers(void);
2017-11-11 04:08:17 +01:00
bool8 SendBlock(u8, const void *, u16);
2017-09-11 11:35:41 +02:00
u8 GetBlockReceivedStatus(void);
void ResetBlockReceivedFlags(void);
void ResetBlockReceivedFlag(u8);
u8 GetLinkPlayerCount_2(void);
bool8 IsLinkMaster(void);
void CB2_LinkError(void);
bool8 GetSioMultiSI(void);
2017-09-11 11:35:41 +02:00
bool8 IsLinkConnectionEstablished(void);
bool8 HasLinkErrorOccurred(void);
void ResetSerial(void);
2017-11-13 07:46:22 +01:00
u32 LinkMain1(u8 *shouldAdvanceLinkState, u16 *sendCmd, u16 (*recvCmds)[CMD_LENGTH]);
2017-09-11 11:35:41 +02:00
void LinkVSync(void);
void Timer3Intr(void);
void SerialCB(void);
2017-11-17 22:28:55 +01:00
bool32 InUnionRoom(void);
2019-04-04 23:05:46 +02:00
void LoadWirelessStatusIndicatorSpriteGfx(void);
2018-12-31 09:22:21 +01:00
bool8 IsLinkTaskFinished(void);
2022-09-13 02:14:15 +02:00
void CreateWirelessStatusIndicatorSprite(u8 x, u8 y);
2020-08-13 09:09:47 +02:00
void SetLinkStandbyCallback(void);
2020-05-30 10:09:21 +02:00
void SetWirelessCommType1(void);
void CheckShouldAdvanceLinkState(void);
2020-08-13 09:09:47 +02:00
void SetCloseLinkCallback(void);
2017-11-13 07:01:27 +01:00
bool8 HandleLinkConnection(void);
2017-11-11 06:05:44 +01:00
void SetLinkDebugValues(u32 seed, u32 flags);
2020-08-24 20:52:33 +02:00
void SetBerryBlenderLinkCallback(void);
2017-11-11 06:05:44 +01:00
void SetSuppressLinkErrorMessage(bool8 flag);
2021-03-19 19:10:13 +01:00
void ConvertLinkPlayerName(struct LinkPlayer *linkPlayer);
void ClearSavedLinkPlayers(void);
2021-10-07 20:55:15 +02:00
void SetLinkErrorBuffer(u32 status, u8 lastSendQueueCount, u8 lastRecvQueueCount, bool8 disconnected);
2021-03-19 19:10:13 +01:00
void LocalLinkPlayerToBlock(void);
2020-06-06 22:46:19 +02:00
void LinkPlayerFromBlock(u32 who);
bool32 Link_AnyPartnersPlayingFRLG_JP(void);
void ResetLinkPlayerCount(void);
2022-06-01 18:41:57 +02:00
void SaveLinkPlayers(u8 playerCount);
2020-06-06 22:46:19 +02:00
void SetWirelessCommType0(void);
bool32 IsLinkRecvQueueAtOverworldMax(void);
2017-11-08 02:25:24 +01:00
2017-11-14 14:31:37 +01:00
extern u16 gLinkPartnersHeldKeys[6];
2017-11-11 06:25:05 +01:00
extern u32 gLinkDebugSeed;
2017-11-11 06:05:44 +01:00
extern struct LinkPlayerBlock gLocalLinkPlayerBlock;
extern bool8 gLinkErrorOccurred;
2017-11-11 06:25:05 +01:00
extern u32 gLinkDebugFlags;
2017-11-14 14:31:37 +01:00
extern bool8 gRemoteLinkPlayersNotReceived[MAX_LINK_PLAYERS];
extern u8 gBlockReceivedStatus[MAX_LINK_PLAYERS];
2017-11-11 06:05:44 +01:00
extern u16 gLinkHeldKeys;
extern u32 gLinkStatus;
2020-08-13 09:09:47 +02:00
extern bool8 gReadyToExitStandby[MAX_LINK_PLAYERS];
extern bool8 gReadyToCloseLink[MAX_LINK_PLAYERS];
extern u16 gReadyCloseLinkType;
2017-11-11 06:05:44 +01:00
extern u8 gSuppressLinkErrorMessage;
2018-01-21 00:43:58 +01:00
extern u8 gWirelessCommType;
2017-11-13 02:57:21 +01:00
extern bool8 gSavedLinkPlayerCount;
2017-11-13 03:31:08 +01:00
extern u8 gSavedMultiplayerId;
2017-11-11 06:05:44 +01:00
extern struct LinkTestBGInfo gLinkTestBGInfo;
extern void (*gLinkCallback)(void);
2021-04-23 23:04:44 +02:00
extern u8 gShouldAdvanceLinkState;
2017-11-11 06:25:05 +01:00
extern u16 gLinkTestBlockChecksums[MAX_LINK_PLAYERS];
2017-11-11 06:05:44 +01:00
extern u8 gBlockRequestType;
2017-11-13 05:16:51 +01:00
extern u8 gLastSendQueueCount;
extern u8 gLastRecvQueueCount;
2017-11-14 14:31:37 +01:00
extern u16 gLinkSavedIme;
2018-10-13 19:41:10 +02:00
extern struct LinkPlayer gLocalLinkPlayer;
2017-10-01 01:12:42 +02:00
2017-11-04 17:20:40 +01:00
bool32 Link_AnyPartnersPlayingRubyOrSapphire(void);
bool32 LinkDummy_Return2(void);
2019-03-01 07:18:58 +01:00
void SetLocalLinkPlayerId(u8);
u8 GetSavedPlayerCount(void);
2020-08-13 09:09:47 +02:00
bool8 SendBlockRequest(u8 type);
u8 GetLinkPlayerCountAsBitFlags(void);
u8 GetSavedLinkPlayerCountAsBitFlags(void);
2020-08-13 09:09:47 +02:00
void SetCloseLinkCallbackHandleJP(void);
void CheckLinkPlayersMatchSaved(void);
void StartSendingKeysToLink(void);
bool8 DoesLinkPlayerCountMatchSaved(void);
2020-08-13 09:09:47 +02:00
void SetCloseLinkCallbackAndType(u16 type);
bool32 IsSendingKeysToLink(void);
u32 GetLinkRecvQueueLength(void);
2017-10-01 01:12:42 +02:00
2017-09-11 11:35:41 +02:00
#endif // GUARD_LINK_H