forked from munificent/game-programming-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspatial-partition.h
More file actions
577 lines (483 loc) · 10.8 KB
/
Copy pathspatial-partition.h
File metadata and controls
577 lines (483 loc) · 10.8 KB
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#include <vector>
#include "expect.h"
namespace SpatialPartition
{
class Unit
{
public:
Unit(const char* name, int position)
: name(name),
position_(position),
hit(NULL)
{}
const char* name;
Unit* hit;
int position() const { return position_; }
private:
int position_;
};
class Vector
{
public:
Vector(int position)
: position(position)
{}
int position;
};
bool operator==(int left, Vector right)
{
return left == right.position;
}
namespace NaiveCollision
{
std::vector<std::pair<Unit*, Unit*> > hits;
void handleAttack(Unit* a, Unit* b)
{
hits.push_back(std::make_pair(a, b));
}
//^pairwise
void handleMelee(Unit* units[], int numUnits)
{
for (int a = 0; a < numUnits - 1; a++)
{
for (int b = a + 1; b < numUnits; b++)
{
if (units[a]->position() == units[b]->position())
{
handleAttack(units[a], units[b]);
}
}
}
}
//^pairwise
void test()
{
Unit* units[5];
units[0] = new Unit("a", 1);
units[1] = new Unit("b", 2);
units[2] = new Unit("c", 3);
units[3] = new Unit("d", 2);
units[4] = new Unit("e", 1);
handleMelee(units, 5);
EXPECT(hits[0].first == units[0]);
EXPECT(hits[0].second == units[4]);
EXPECT(hits[1].first == units[1]);
EXPECT(hits[1].second == units[3]);
}
}
namespace SimpleUnit
{
class Grid;
//^unit-simple
class Unit
{
friend class Grid;
public:
Unit(Grid* grid, double x, double y)
: grid_(grid),
x_(x),
y_(y)
{}
void move(double x, double y);
private:
double x_, y_;
Grid* grid_;
};
//^unit-simple
}
namespace SimpleGrid
{
class Grid;
//^grid-simple
class Grid
{
public:
Grid()
{
// Clear the grid.
for (int x = 0; x < NUM_CELLS; x++)
{
for (int y = 0; y < NUM_CELLS; y++)
{
cells_[x][y] = NULL;
}
}
}
static const int NUM_CELLS = 10;
static const int CELL_SIZE = 20;
private:
Unit* cells_[NUM_CELLS][NUM_CELLS];
};
//^grid-simple
}
namespace LinkedUnit
{
//^unit-linked
class Unit
{
// Previous code...
private:
Unit* prev_;
Unit* next_;
};
//^unit-linked
}
namespace AddToGrid
{
class Unit;
//^add-decl
class Grid
{
public:
void add(Unit* unit);
// Previous code...
//^omit
static const int NUM_CELLS = 10;
static const int CELL_SIZE = 20;
Unit* cells_[NUM_CELLS][NUM_CELLS];
//^omit
};
//^add-decl
class Unit
{
public:
Unit(Grid* grid, double x, double y);
// Previous code...
//^omit
friend class Grid;
private:
double x_, y_;
Grid* grid_;
Unit* prev_;
Unit* next_;
//^omit
};
//^unit-ctor
Unit::Unit(Grid* grid, double x, double y)
: grid_(grid),
x_(x),
y_(y),
prev_(NULL),
next_(NULL)
{
grid_->add(this);
}
//^unit-ctor
//^add
void Grid::add(Unit* unit)
{
// Determine which grid cell it's in.
int cellX = (int)(unit->x_ / Grid::CELL_SIZE);
int cellY = (int)(unit->y_ / Grid::CELL_SIZE);
// Add to the front of list for the cell it's in.
unit->prev_ = NULL;
unit->next_ = cells_[cellX][cellY];
cells_[cellX][cellY] = unit;
if (unit->next_ != NULL)
{
unit->next_->prev_ = unit;
}
}
//^add
}
namespace FixedGrid
{
class Unit;
void handleAttack(Unit* unit, Unit* other)
{
}
class Grid
{
public:
Grid()
{
// Clear the grid.
for (int x = 0; x < NUM_CELLS; x++)
{
for (int y = 0; y < NUM_CELLS; y++)
{
cells_[x][y] = NULL;
}
}
}
static const int CELL_SIZE = 20;
void move(Unit* unit, double x, double y);
void add(Unit* unit);
Unit* findAt(double x, double y);
void handleMelee();
void handleCell(Unit* unit);
//^omit
void dump();
//^omit
private:
static const int NUM_CELLS = 10;
Unit* cells_[NUM_CELLS][NUM_CELLS];
};
//^unit
class Unit
{
friend class Grid;
public:
//^omit
const char* name;
//^omit
Unit(Grid* grid, double x, double y)
: grid_(grid),
//^omit
name(NULL),
//^omit
x_(x),
y_(y),
prev_(NULL),
next_(NULL)
{
grid_->add(this);
}
void move(double x, double y);
private:
double x_, y_;
Grid* grid_;
Unit* prev_;
Unit* next_;
};
//^unit
//^unit-move
void Unit::move(double x, double y)
{
grid_->move(this, x, y);
}
//^unit-move
//^grid-move
void Grid::move(Unit* unit, double x, double y)
{
// See which cell it was in.
int oldCellX = (int)(unit->x_ / Grid::CELL_SIZE);
int oldCellY = (int)(unit->y_ / Grid::CELL_SIZE);
// See which cell it's moving to.
int cellX = (int)(x / Grid::CELL_SIZE);
int cellY = (int)(y / Grid::CELL_SIZE);
unit->x_ = x;
unit->y_ = y;
// If it didn't change cells, we're done.
if (oldCellX == cellX && oldCellY == cellY) return;
// Unlink it from the list of its old cell.
if (unit->prev_ != NULL)
{
unit->prev_->next_ = unit->next_;
}
if (unit->next_ != NULL)
{
unit->next_->prev_ = unit->prev_;
}
// If it's the head of a list, remove it.
if (cells_[oldCellX][oldCellY] == unit)
{
cells_[oldCellX][oldCellY] = unit->next_;
}
// Add it back to the grid at its new cell.
add(unit);
}
//^grid-move
void Grid::add(Unit* unit)
{
// Determine which grid cell it's in.
int cellX = (int)(unit->x_ / Grid::CELL_SIZE);
int cellY = (int)(unit->y_ / Grid::CELL_SIZE);
// Add to the front of list for the cell its in.
unit->prev_ = NULL;
unit->next_ = cells_[cellX][cellY];
cells_[cellX][cellY] = unit;
if (unit->next_ != NULL)
{
unit->next_->prev_ = unit;
}
}
Unit* Grid::findAt(double x, double y)
{
int cellX = (int)(x / Grid::CELL_SIZE);
int cellY = (int)(y / Grid::CELL_SIZE);
Unit* unit = cells_[cellX][cellY];
while (unit != NULL)
{
if (unit->x_ == x && unit->y_ == y) return unit;
unit = unit->next_;
}
return NULL;
}
void Grid::dump()
{
for (int y = 0; y < NUM_CELLS; y++)
{
for (int x = 0; x < NUM_CELLS; x++)
{
Unit* unit = cells_[x][y];
if (unit == NULL) continue;
printf("%d, %d : ", x, y);
while (unit != NULL)
{
printf("%s ", unit->name);
unit = unit->next_;
}
printf("\n");
}
}
printf("---\n");
}
// TODO(bob): Need tests for this.
//^grid-melee
void Grid::handleMelee()
{
for (int x = 0; x < NUM_CELLS; x++)
{
for (int y = 0; y < NUM_CELLS; y++)
{
handleCell(cells_[x][y]);
}
}
}
//^grid-melee
//^handle-cell
void Grid::handleCell(Unit* unit)
{
while (unit != NULL)
{
Unit* other = unit->next_;
while (other != NULL)
{
if (unit->x_ == other->x_ &&
unit->y_ == other->y_)
{
handleAttack(unit, other);
}
other = other->next_;
}
unit = unit->next_;
}
}
//^handle-cell
void test()
{
Grid grid;
Unit a(&grid, 0, 0); a.name = "a";
Unit b(&grid, 0, 0); b.name = "b";
Unit c(&grid, 0, 0); c.name = "c";
b.move(50, 65);
c.move(55, 65);
a.move(20, 100);
c.move(22, 100);
EXPECT(grid.findAt(20, 100) == &a);
EXPECT(grid.findAt(50, 65) == &b);
EXPECT(grid.findAt(22, 100) == &c);
}
}
namespace AttackDistance
{
const int ATTACK_DISTANCE = 2;
class Unit;
void handleAttack(Unit* unit, Unit* other)
{
}
int distance(Unit* a, Unit* b) { return 3; }
class Grid
{
public:
Grid()
{
// Clear the grid.
for (int x = 0; x < NUM_CELLS; x++)
{
for (int y = 0; y < NUM_CELLS; y++)
{
cells_[x][y] = NULL;
}
}
}
static const int CELL_SIZE = 20;
void move(Unit* unit, double x, double y);
void add(Unit* unit);
Unit* findAt(double x, double y);
void handleMelee();
void handleCell(int x, int y);
void handleUnit(Unit* unit, Unit* other);
private:
static const int NUM_CELLS = 10;
Unit* cells_[NUM_CELLS][NUM_CELLS];
};
class Unit
{
friend class Grid;
public:
Unit(Grid* grid, double x, double y)
: grid_(grid),
x_(x),
y_(y),
prev_(NULL),
next_(NULL)
{
grid_->add(this);
}
void move(double x, double y);
private:
double x_, y_;
Grid* grid_;
Unit* prev_;
Unit* next_;
};
// TODO(bob): Need tests for this.
void Grid::handleMelee()
{
for (int x = 0; x < NUM_CELLS; x++)
{
for (int y = 0; y < NUM_CELLS; y++)
{
handleCell(x, y);
}
}
}
//^handle-neighbor
//^handle-cell-unit
void Grid::handleCell(int x, int y)
{
Unit* unit = cells_[x][y];
while (unit != NULL)
{
// Handle other units in this cell.
handleUnit(unit, unit->next_);
//^omit handle-cell-unit
// Also try the neighboring cells.
if (x > 0 && y > 0) handleUnit(unit, cells_[x - 1][y - 1]);
if (x > 0) handleUnit(unit, cells_[x - 1][y]);
if (y > 0) handleUnit(unit, cells_[x][y - 1]);
if (x > 0 && y < NUM_CELLS - 1)
{
handleUnit(unit, cells_[x - 1][y + 1]);
}
//^omit handle-cell-unit
unit = unit->next_;
}
}
//^handle-cell-unit
//^handle-neighbor
//^handle-unit
void Grid::handleUnit(Unit* unit, Unit* other)
{
while (other != NULL)
{
//^handle-distance
if (distance(unit, other) < ATTACK_DISTANCE)
{
handleAttack(unit, other);
}
//^handle-distance
other = other->next_;
}
}
//^handle-unit
}
void test()
{
printf("Testing Spatial Partition\n");
NaiveCollision::test();
FixedGrid::test();
}
}