102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
board.c
Go to the documentation of this file.
4
5static bool on_board(int32_t col, int32_t row)
6{
7 return (uint32_t)col < CHECKERS_BOARD_SIDE && (uint32_t)row < CHECKERS_BOARD_SIDE;
8}
9
10static bool allows_row_delta(uint32_t piece, int32_t delta, int32_t distance, bool unknown_piece_result)
11{
12 switch (piece)
13 {
15 return delta == distance;
17 return delta == -distance;
20 return delta == distance || delta == -distance;
21 default:
22 return unknown_piece_result;
23 }
24}
25
26// Original pcdogs.exe: 0x412240
27enum Checkers_MoveValidation Checkers_ValidateSimpleMove(struct Checkers_Board *board, int32_t from_col, int32_t from_row, int32_t to_col, int32_t to_row)
28{
29 if (!on_board(to_col, to_row) || board->playable_by_col[to_col][to_row >> 1])
31
32 int32_t col_delta = to_col - from_col;
33 if (col_delta != 1 && col_delta != -1)
35
36 uint32_t piece = board->playable_by_col[from_col][from_row >> 1];
37 return allows_row_delta(piece, to_row - from_row, 1, false) ? CHECKERS_MOVE_SIMPLE : CHECKERS_MOVE_INVALID;
38}
39
40static bool can_jump(const struct Checkers_Board *board, int32_t col, int32_t row, int32_t dx, int32_t dy, uint32_t opponent)
41{
42 int32_t to_col = col + 2 * dx;
43 int32_t to_row = row + 2 * dy;
44 return on_board(to_col, to_row) && (board->playable_by_col[col + dx][(row + dy) >> 1] & opponent) && !board->playable_by_col[to_col][to_row >> 1];
45}
46
47static bool can_jump_in_row_direction(const struct Checkers_Board *board, int32_t col, int32_t row, int32_t dy, uint32_t opponent)
48{
49 return can_jump(board, col, row, 1, dy, opponent) || can_jump(board, col, row, -1, dy, opponent);
50}
51
52// Original pcdogs.exe: 0x4122f0
53BOOL Checkers_CheckCapturePossible(struct Checkers_Board *board, uint32_t piece, int32_t col, int32_t row)
54{
55 switch (piece)
56 {
58 if (can_jump_in_row_direction(board, col, row, -1, CHECKERS_PLAYER2_BIT))
59 return TRUE;
60 /* fall through */
62 return can_jump_in_row_direction(board, col, row, 1, CHECKERS_PLAYER2_BIT);
63
65 if (can_jump_in_row_direction(board, col, row, 1, CHECKERS_PLAYER1_BIT))
66 return TRUE;
67 /* fall through */
69 return can_jump_in_row_direction(board, col, row, -1, CHECKERS_PLAYER1_BIT);
70
71 default:
72 return FALSE;
73 }
74}
75
76static bool side_has_capture(struct Checkers_Board *board, uint32_t player)
77{
78 for (int32_t col = 0; col < CHECKERS_BOARD_SIDE; ++col)
79 {
80 for (int32_t row = (col ^ 1) & 1; row < CHECKERS_BOARD_SIDE; row += 2)
81 {
82 uint32_t piece = board->playable_by_col[col][row >> 1];
83 if ((piece & CHECKERS_PLAYER_MASK) == player && Checkers_CheckCapturePossible(board, piece, col, row))
84 return true;
85 }
86 }
87 return false;
88}
89
90// Original pcdogs.exe: 0x4125c0
91enum Checkers_MoveValidation Checkers_ValidateMove(struct Checkers_Board *board, int32_t from_col, int32_t from_row, int32_t to_col, int32_t to_row)
92{
93 if (!on_board(to_col, to_row) || board->playable_by_col[to_col][to_row >> 1])
95
96 uint32_t piece = board->playable_by_col[from_col][from_row >> 1];
97 int32_t col_delta = to_col - from_col;
98 int32_t row_delta = to_row - from_row;
99 // This entry assumes a selected piece. Unknown piece codes skip its row-direction test.
100 if (col_delta == 1 || col_delta == -1)
101 {
102 if (!allows_row_delta(piece, row_delta, 1, true))
104 if (g_worldBonusStateArray[3] == 1 && side_has_capture(board, piece & CHECKERS_PLAYER_MASK))
107 }
108 if ((col_delta != 2 && col_delta != -2) || !allows_row_delta(piece, row_delta, 2, true))
110
111 uint32_t jumped = board->playable_by_col[(from_col + to_col) >> 1][(from_row + to_row) >> 2];
112 if (jumped && ((jumped ^ piece) & CHECKERS_PLAYER_MASK))
115}
116
117// Original pcdogs.exe: 0x412760
118enum Checkers_MoveExecutionResult Checkers_ExecuteMove(struct Checkers_Board *board, int32_t from_col, int32_t from_row, int32_t to_col, int32_t to_row)
119{
120 enum Checkers_MoveValidation move = Checkers_ValidateMove(board, from_col, from_row, to_col, to_row);
121 if (move == CHECKERS_MOVE_INVALID)
123
124 enum Checkers_Piece piece = board->playable_by_col[from_col][from_row >> 1];
125 board->playable_by_col[from_col][from_row >> 1] = CHECKERS_PIECE_EMPTY;
127 if (move == CHECKERS_MOVE_CAPTURE)
128 {
129 board->playable_by_col[(from_col + to_col) >> 1][(from_row + to_row) >> 2] = CHECKERS_PIECE_EMPTY;
130 if (Checkers_CheckCapturePossible(board, piece, to_col, to_row))
132 }
133
135 {
138 }
139 else if (piece == CHECKERS_PIECE_PLAYER2_MAN && to_row == 0)
140 {
143 }
144 board->playable_by_col[to_col][to_row >> 1] = piece;
145 return result;
146}
147
148// Original pcdogs.exe: 0x412990
149// Executes the destinations packed into a checkers move sequence, consuming three bits per coordinate and performing at least one move.
151{
152 int32_t from_col = move_sequence->fromCol;
153 int32_t from_row = move_sequence->fromRow;
154 uint32_t packed_to_cols = move_sequence->packedToCols;
155 uint32_t packed_to_rows = move_sequence->packedToRows;
157
158 do
159 {
160 int32_t to_row = packed_to_rows & CHECKERS_COORDINATE_MASK;
161 int32_t to_col = packed_to_cols & CHECKERS_COORDINATE_MASK;
162
163 result = Checkers_ExecuteMove(board, from_col, from_row, to_col, to_row);
164 packed_to_rows >>= CHECKERS_COORDINATE_BITS;
165 from_col = to_col;
166 packed_to_cols >>= CHECKERS_COORDINATE_BITS;
167 from_row = to_row;
168 } while (packed_to_rows | packed_to_cols);
169
170 return result;
171}
172
173// Original pcdogs.exe: 0x412de0
175{
176 for (int32_t col = 0; col < CHECKERS_BOARD_SIDE; ++col)
177 {
178 for (int32_t row = (col ^ 1) & 1; row < CHECKERS_BOARD_SIDE; row += 2)
179 {
181 if (row < 3)
183 else if (row >= 5)
185 board->playable_by_col[col][row >> 1] = piece;
186 }
187 }
188}
static bool on_board(int32_t col, int32_t row)
Definition board.c:5
enum Checkers_MoveValidation Checkers_ValidateSimpleMove(struct Checkers_Board *board, int32_t from_col, int32_t from_row, int32_t to_col, int32_t to_row)
Definition board.c:27
static bool can_jump(const struct Checkers_Board *board, int32_t col, int32_t row, int32_t dx, int32_t dy, uint32_t opponent)
Definition board.c:40
BOOL Checkers_CheckCapturePossible(struct Checkers_Board *board, uint32_t piece, int32_t col, int32_t row)
Definition board.c:53
enum Checkers_MoveExecutionResult Checkers_ExecuteMoveSequence(struct Checkers_Board *board, struct Checkers_MoveSequence const *move_sequence)
Definition board.c:150
enum Checkers_MoveValidation Checkers_ValidateMove(struct Checkers_Board *board, int32_t from_col, int32_t from_row, int32_t to_col, int32_t to_row)
Definition board.c:91
static bool allows_row_delta(uint32_t piece, int32_t delta, int32_t distance, bool unknown_piece_result)
Definition board.c:10
enum Checkers_MoveExecutionResult Checkers_ExecuteMove(struct Checkers_Board *board, int32_t from_col, int32_t from_row, int32_t to_col, int32_t to_row)
Definition board.c:118
static bool side_has_capture(struct Checkers_Board *board, uint32_t player)
Definition board.c:76
void Checkers_InitializeBoard(struct Checkers_Board *board)
Definition board.c:174
static bool can_jump_in_row_direction(const struct Checkers_Board *board, int32_t col, int32_t row, int32_t dy, uint32_t opponent)
Definition board.c:47
#define CHECKERS_COORDINATE_MASK
Definition constants.h:11
#define CHECKERS_COORDINATE_BITS
Definition constants.h:14
#define CHECKERS_BOARD_SIDE
Definition constants.h:12
#define CHECKERS_PLAYER2_BIT
Definition constants.h:9
#define CHECKERS_PLAYER_MASK
Definition constants.h:10
#define CHECKERS_PLAYER1_BIT
Definition constants.h:8
#define CHECKERS_LAST_COORDINATE
Definition constants.h:13
Checkers_Piece
Definition enums.h:85
@ CHECKERS_PIECE_PLAYER1_MAN
Definition enums.h:91
@ CHECKERS_PIECE_PLAYER2_KING
Definition enums.h:100
@ CHECKERS_PIECE_PLAYER1_KING
Definition enums.h:97
@ CHECKERS_PIECE_PLAYER2_MAN
Definition enums.h:94
@ CHECKERS_PIECE_EMPTY
Definition enums.h:88
Checkers_MoveValidation
Definition enums.h:69
@ CHECKERS_MOVE_CAPTURE
Definition enums.h:75
@ CHECKERS_MOVE_SIMPLE
Definition enums.h:78
@ CHECKERS_MOVE_INVALID
Definition enums.h:72
Checkers_MoveExecutionResult
Definition enums.h:50
@ CHECKERS_EXEC_TURN_COMPLETE_PROMOTED
Definition enums.h:62
@ CHECKERS_EXEC_TURN_COMPLETE
Definition enums.h:59
@ CHECKERS_EXEC_CAPTURE_CAN_CONTINUE
Definition enums.h:56
@ CHECKERS_EXEC_INVALID
Definition enums.h:53
enum Checkers_Piece playable_by_col[0x8][0x4]
Definition types.h:11
uint32_t packedToRows
Definition types.h:24
uint32_t packedToCols
Definition types.h:23
#define g_worldBonusStateArray
Definition values.h:828