-
Notifications
You must be signed in to change notification settings - Fork 0
/
Core.c
500 lines (411 loc) · 10.4 KB
/
Core.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include "Core.h"
#include "stdio.h"
#include <string.h>
#include <sys/time.h>
int prev_rand = 0;
int quickrand()
{
prev_rand = (1664525L * prev_rand + 1013904223L);
return prev_rand;
}
int abs(int a)
{
return (a < 0) ? -a : a;
}
int min(int a, int b)
{
return (a < b) ? a : b;
}
int max(int a, int b)
{
return (a > b) ? a : b;
}
int randRange(int a, int b)
{
return a + (abs(quickrand()) % (b - a));
}
// Need to be able to support multiple board instances. (Maybe, see networking remarks below.)
// Board memory is finite.
// Keep track of current frame (write-only?).
// Keep track of previous frame (read-only).
// Need ability to flush when full.
// Need ability to lock frames.
// When flushing, must copy locked frames into clean buffer and update references.
// Keep track of copied frames so they don't get serialized twice.
// Will need ability to network (both to and from) - might only need latest state for rendering, not full history.
// Use RLE and XOR to serialize history?
// Ability to scrub backwards?
struct Board
{
Board(int rows, int cols, int frames=2)
: rows(rows)
, cols(cols)
, span(rows * cols)
{
//assert(frames > 1, "Cannot support both a previous and current board without two or more frames of history!");
buffer_size = (span * frames);
buffer = new int[buffer_size];
current = buffer;
memset(current, eBlockType_Empty, span * sizeof(int));
Advance();
}
void Advance()
{
previous = current;
current += span;
//!!ARL: Memcpy prev to cur, or let Update take care of copying all values over?
if (current >= (buffer + buffer_size))
Flush();
}
void Flush()
{
//!!ARL: Deal with locked frames.
//!!ARL: Assert no overlap - use memmove instead?
current = buffer;
memcpy(current, previous, span * sizeof(int));
Advance();
}
int* buffer;
int* current;
int* previous;
int rows;
int cols;
int span; // blocks per frame
int buffer_size;
};
struct EachBlock
{
EachBlock(Board* board, int skip=0) // optionally start at row x
{
read = board->previous + (skip * BOARD_COLS);
write = board->current + (skip * BOARD_COLS);
end = write + board->span;
}
inline operator bool() const
{
return (write < end);
}
inline EachBlock& operator++()
{
++read;
++write;
return *this;
}
inline int operator*() const
{
return (*read);
}
inline int GetType() const
{
return (*read) & eBlockFlag_TypeMask;
}
inline void SetType(int type)
{
(*write) = type;
}
inline void ClearFlag(int flag)
{
(*write) = (*read) & ~flag;
}
inline int Decay(int mask, const int offset)
{
int count = (*read) & mask;
if (count)
{
count -= (1 << offset); // push bit-shift up a level to ensure folding? (this whole function will probably go away shortly anyway)
if (count == 0)
return 1;
(*write) = count | ((*read) & ~mask);
}
return 0;
}
const int* read;
int* write;
int const* end; // of write
};
struct EachFreeBlock // skips empty and locked blocks
{
EachFreeBlock(Board* board)
: board(board)
{
i = board->previous - 1;
end = board->previous + board->span;
++(*this); // scan to first valid block
}
inline operator bool() const
{
return (i < end);
}
inline EachFreeBlock& operator++()
{
for (++i; (*this); ++i)
{
const int block = (*i);
if (block == eBlockType_Empty)
continue;
if (block & ~eBlockFlag_TypeMask)
continue;
break;
}
const int offset = i - board->previous;
row = offset / board->span;
col = offset % BOARD_COLS;
return *this;
}
inline int GetType() const
{
return (*i); // no need to mask since we skip non-masked in the increment
}
inline void SetType(int type)
{
board->current[row * BOARD_COLS + col] = type;
}
const int* i;
int const* end;
int row, col;
Board* board;
};
struct EachColConst // read-only
{
EachColConst(Board* board, int row, int min=0, int max=BOARD_COLS)
: i(&board->previous[row * BOARD_COLS + ::max(min, 0)])
, end(&board->previous[row * BOARD_COLS + ::min(max, BOARD_COLS)])
{}
inline operator bool() const
{
return (i < end);
}
inline EachColConst& operator++()
{
++i;
return *this;
}
inline int operator*() const
{
return (*i);
}
const int* i;
const int* const end;
};
struct EachRowConst // read-only
{
EachRowConst(Board* board, int col, int min=0, int max=BOARD_ROWS)
: i(&board->previous[::max(min, 0) * BOARD_COLS + col])
, end(&board->previous[::min(max, BOARD_ROWS) * BOARD_COLS + col])
{}
inline operator bool() const
{
return (i < end);
}
inline EachRowConst& operator++()
{
i += BOARD_COLS;
return *this;
}
inline int operator*() const
{
return (*i);
}
const int* i;
const int* const end;
};
struct EachBlockR // for reverse
{
EachBlockR(Board* board, int skip=0) // optionally skip x bottom rows
{
const int last = (board->span - 1) - (skip * BOARD_COLS);
read = &board->previous[last];
write = &board->current[last];
end = board->current;
}
inline operator bool() const
{
return (write >= end);
}
inline EachBlockR& operator--()
{
--read;
--write;
return *this;
}
inline int operator*() const
{
return (*read);
}
inline int GetType() const
{
return (*read) & eBlockFlag_TypeMask;
}
inline void SetType(int type)
{
(*write) = type;
}
const int* read;
int* write;
int const* end; // of write
};
static Board* board = NULL;
void PPL_Init()
{
//!!ARL: Pass in seed?
struct timeval t;
gettimeofday(&t,0);
prev_rand = t.tv_sec;
if (board)
delete board;
board = new Board(BOARD_ROWS,BOARD_COLS);
}
void PPL_Update(void)
{
//!!ARL: Need to change how matching / breaking is done. Store off a set, and work our way through it.
// Kill the blocks one at a time (left->right, top->bottom). Use two separate flags. Start as matched,
// change to cleared. Don't actually clear the 'cleared' flag until the entire set has been matched.
// Allow multiple sets at once, but keep independent.
// After clear flag is removed, blocks must hang for a frame to give interface code a chance to move a block
// before they start falling.
//!!ARL: Instead of storing separately, keep track of the board that the combo was made on, and use it as
// a sort of template for working through and clearing the blocks on the "current" board.
//!!ARL: Falling happens over a single frame. The visualization is always a frame behind, so we need to
// move the block, and then mark it on the same frame so the visualization can animate it. (Unless we
// start using callbacks instead.) The next frame when we check to see if a given block needs to fall or
// not, then we can simple clear the flag in the "or not" case.
board->Advance();
// decay matched/falling blocks.
for (EachBlock block(board); block; ++block)
{
int type = block.GetType();
if (type == eBlockType_Empty)
continue;
if (block.Decay(eBlockFlag_Matching, MATCHING_OFFSET))
block.SetType(eBlockType_Empty);
else if (block.Decay(eBlockFlag_Falling, FALLING_OFFSET))
block.SetType(type); // clear the flag, done falling.
}
// update falling blocks (by raising empty blocks to the top).
for (EachBlockR block(board,1), below(board); block; --block, --below)
{
//!!ARL: Move to IsEmpty and IsLocked? (EachFreeBlockR?)
if ((*block) == eBlockType_Empty)
continue;
if ((*block) & ~eBlockFlag_TypeMask)
continue;
if ((*below) == eBlockType_Empty)
{
below.SetType((*block) | eBlockFlag_Falling);
block.SetType(eBlockType_Empty);
}
}
// check for matches.
int matches;
for (EachFreeBlock block(board); block; ++block)
{
int type = block.GetType();
matches = 0;
for (EachColConst i(board,block.row,block.col-2,block.col+3); i; ++i)
{
if ((*i) == type)
{
if (++matches == 3)
{
block.SetType(type | eBlockFlag_Matching);
goto NEXT_BLOCK;
}
}
else matches = 0;
}
matches = 0;
for (EachRowConst i(board,block.col,block.row-2,block.row+3); i; ++i)
{
if ((*i) == type)
{
if (++matches == 3)
{
block.SetType(type | eBlockFlag_Matching);
goto NEXT_BLOCK;
}
}
else matches = 0;
}
NEXT_BLOCK:;
}
}
int PPL_Feed(void)
{
// Check for GameOver state (blocks about to be pushed off the top row).
for (EachColConst block(board,0); block; ++block)
if ((*block) != eBlockType_Empty)
return 0;
// Unlock old bottom row.
for (EachBlock block(board,BOARD_ROWS-1); block; ++block)
block.ClearFlag(eBlockFlag_Locked);
// Move rows up one.
for (EachBlock block(board), below(board,1); below; ++block, ++below)
block.SetType(*below);
// Fill in new bottom row and lock it. (!!ARL: Make sure there are no matches.)
for (EachBlock block(board,BOARD_ROWS-1); block; ++block)
block.SetType(randRange(eBlockType_Empty+1, eBlockType_Max) | eBlockFlag_Locked);
// Success!
return 1;
}
//!!ARL: Push these accessors to Board members?
void PPL_SetBlockType(int row, int col, int type)
{
if (row < 0 || row >= BOARD_ROWS)
return;
if (col < 0 || col >= BOARD_COLS)
return;
if (type < 0 || type >= eBlockType_Max)
return;
board->current[row * BOARD_COLS + col] = type;
}
int PPL_GetBlockType(int row, int col)
{
if (row < 0 || row >= BOARD_ROWS)
return eBlockType_Invalid;
if (col < 0 || col >= BOARD_COLS)
return eBlockType_Invalid;
return (board->current[row * BOARD_COLS + col] & eBlockFlag_TypeMask);
}
int PPL_IsLocked(int row, int col)
{
int block = board->current[row * BOARD_COLS + col];
return (block & ~eBlockFlag_TypeMask);
}
int PPL_IsFalling(int row, int col)
{
int block = board->current[row * BOARD_COLS + col];
return (block & eBlockFlag_Falling);
}
int PPL_IsBreaking(int row, int col)
{
int block = board->current[row * BOARD_COLS + col];
return (block & eBlockFlag_Matching);
}
int PPL_MoveRight(int row, int col)
{
if (col == BOARD_COLS-1)
return 0;
int block = board->current[row * BOARD_COLS + col];
if (block & ~eBlockFlag_TypeMask)
return 0;
int other = board->current[row * BOARD_COLS + col+1];
if (other & ~eBlockFlag_TypeMask)
return 0;
board->previous[row * BOARD_COLS + col] = other;
board->previous[row * BOARD_COLS + col+1] = block;
return 1;
}
int PPL_MoveLeft(int row, int col)
{
if (col == 0)
return 0;
int block = board->current[row * BOARD_COLS + col];
if (block & ~eBlockFlag_TypeMask)
return 0;
int other = board->current[row * BOARD_COLS + col-1];
if (other & ~eBlockFlag_TypeMask)
return 0;
board->previous[row * BOARD_COLS + col] = other;
board->previous[row * BOARD_COLS + col-1] = block;
return 1;
}