-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorld.cpp
More file actions
565 lines (522 loc) · 20.8 KB
/
World.cpp
File metadata and controls
565 lines (522 loc) · 20.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
#include "Arduino.h"
#include "World.h"
const byte World::numRows = 32;
const byte World::numCols = 24;
World::World() {
}
World::World(LedControl* lc, Player* player, byte difficulty) : lc(lc), player(player), difficulty(difficulty),
difficultyStepY(Player::getYRange() / 3), difficultyStepX(Player::getXRange() / 3), justDisappeared(false) {
this->worldMap = FakeMatrix(World::numRows, World::numCols / 8);
this->first = nullptr;
this->generateFromLast(true);
this->generateFromLast();
this->generateFromLast();
// Serial.println(this->difficultyStepX);
// Serial.println(this->difficultyStepY);
// Serial.println(this->difficulty);
// for(int i = 0; i < World::numRows; ++i) {
// for(int j = 0; j < World::numCols; ++j) {
// Serial.print(this->worldMap[Pos(i, j)].check() != 0);
// Serial.print(" ");
// }
// Serial.println();
// }
// Serial.print(this->last.x); Serial.print(" "); Serial.print(this->last.y); Serial.println();
}
// World& World::operator=(const World& other) {
// this->lc = other.lc;
// this->player = other.player;
// this->worldMap = other.worldMap;
// this->difficultyStepX = other.difficultyStepX;
// this->difficultyStepY = other.difficultyStepY;
// this->difficulty = other.difficulty;
// this->last = other.last;
// this->secondLast = other.secondLast;
// this->first = other.first;
// return *this;
// }
// World::World(const World& other) {
// this->lc = other.lc;
// this->player = other.player;
// this->worldMap = other.worldMap;
// this->difficultyStepX = other.difficultyStepX;
// this->difficultyStepY = other.difficultyStepY;
// this->difficulty = other.difficulty;
// this->last = other.last;
// this->secondLast = other.secondLast;
// this->first = other.first;
// }
void World::scrollUp() {
if(!this->player->jumpedThroughPlatform())
this->worldMap[this->player->getPos()] = 0;
else
this->player->setPassedPlatform(false);
for(int i = World::numRows - 2; i >= 0; --i) {
for(int j = 0; j < World::numCols; j += 8) {
this->worldMap.setByte(Pos(i + 1, j), this->worldMap.getByte(Pos(i, j)));
}
}
if(this->worldMap[this->player->getPos()].check()) {
this->player->setPassedPlatform(true);
}
this->last.y += 1;
this->secondLast.y += 1;
this->player->setLastPos(Pos(this->player->getLastPos().i + 1, this->player->getLastPos().j));
for(int j = 0; j < World::numCols; j += 8) {
this->worldMap.setByte(Pos(0, j), B00000000);
}
this->moveAllStructsBy(1, 0);
this->redrawStructs();
this->checkCamera();
}
void World::scrollDown() {
if(!this->player->jumpedThroughPlatform())
this->worldMap[this->player->getPos()] = 0;
else
this->player->setPassedPlatform(false);
this->last.y -= 1;
this->secondLast.y -= 1;
this->player->setLastPos(Pos(this->player->getLastPos().i - 1, this->player->getLastPos().j));
for(int i = 1; i < World:: numRows; ++i) {
for(int j = 0; j < World::numCols; j += 8) {
this->worldMap.setByte(Pos(i - 1, j), this->worldMap.getByte(Pos(i, j)));
}
}
for(int j = 0; j < World::numCols; j += 8) {
this->worldMap.setByte(Pos(World::numRows - 1, j), B00000000);
}
this->moveAllStructsBy(-1, 0);
this->checkCamera();
}
void World::scrollRight() {
if(!this->player->jumpedThroughPlatform())
this->worldMap[this->player->getPos()] = 0;
else
this->player->setPassedPlatform(false);
this->last.x -= 1;
this->secondLast.x -= 1;
this->player->setLastPos(Pos(this->player->getLastPos().i, this->player->getLastPos().j - 1));
for(int i = 0; i < World::numRows; ++i) {
for(int j = 1; j < World::numCols; ++j) {
this->worldMap[Pos(i, j - 1)] = this->worldMap[Pos(i, j)];
}
}
if(this->worldMap[this->player->getPos()].check()) {
this->player->setPassedPlatform(true);
}
for(int i = 0; i < World::numRows; ++i) {
this->worldMap[Pos(i, World::numCols - 1)] = 0;
}
this->moveAllStructsBy(0, -1);
this->redrawStructs();
this->checkCamera();
}
void World::scrollLeft() {
if(!this->player->jumpedThroughPlatform())
this->worldMap[this->player->getPos()] = 0;
else
this->player->setPassedPlatform(false);
this->last.x += 1;
this->secondLast.x += 1;
this->player->setLastPos(Pos(this->player->getLastPos().i, this->player->getLastPos().j + 1));
for(int i = 0; i < World::numRows; ++i) {
for(int j = World::numCols - 2; j >= 0; --j) {
this->worldMap[Pos(i, j + 1)] = this->worldMap[Pos(i, j)];
}
}
if(this->worldMap[this->player->getPos()].check()) {
this->player->setPassedPlatform(true);
}
for(int i = 0; i < World::numRows; ++i) {
this->worldMap[Pos(i, 0)] = 0;
}
this->moveAllStructsBy(0, 1);
this->redrawStructs();
this->checkCamera();
}
bool World::intersect(BoundingBox s1, BoundingBox s2) {
bool xTrue = false, yTrue = false;
if(s1.x < s2.x && s1.x + s1.width >= s2.x) {
xTrue = true;
}
if(s1.x >= s2.x && s2.x + s2.width >= s1.x) {
xTrue = true;
}
if(s1.y < s2.y && s1.y + s1.height >= s2.y) {
yTrue = true;
}
if(s1.y >= s2.y && s2.y + s2.height >= s1.y) {
yTrue = true;
}
return xTrue && yTrue;
}
// this function will redraw structs that ended up outside the map matrix
// this is needed because structs will get quite big and there is no way to prevent them from disappearing anymore
void World::redrawStructs() {
StructNode* current = this->first;
while(current != nullptr) {
BoundingBox box = current->structure->getBoundingBox();
if(box.x <= 0 || box.y <= 0) {
current->structure->draw(this);
}
current = current->next;
}
}
void World::moveAllStructsBy(int8_t yVal, int8_t xVal) {
StructNode* current = this->first;
while(current != nullptr) {
current->structure->setPos(Pos(current->structure->getPos().i + yVal, current->structure->getPos().j + xVal));
current = current->next;
}
}
// check if the last structure is now in the panning camera, in which case generate a new last structure
void World::checkCamera() {
// if(this->last.x >= 8 && this->last.x < 16 && this->last.y >= 12 && this->last.y < 20) {
if(this->intersect(last, BoundingBox(CAMERA_X, CAMERA_Y, CAMERA_DIM - 1, CAMERA_DIM - 1))) {
// Serial.println("huh");
// Serial.print(this->last.x); Serial.print(" "); Serial.print(this->last.y); Serial.print(" "); Serial.print(this->last.height); Serial.print(" "); Serial.print(this->last.width); Serial.println();
// Serial.print(this->secondLast.x); Serial.print(" "); Serial.print(this->secondLast.y); Serial.print(" "); Serial.print(this->secondLast.height); Serial.print(" "); Serial.print(this->secondLast.width); Serial.println();
// for(int i = 0; i < World::numRows; ++i) {
// for(int j = 0; j < World::numCols; ++j) {
// Serial.print(this->worldMap[Pos(i, j)].check() != 0);
// Serial.print(" ");
// }
// Serial.println();
// }
this->generateFromLast();
}
}
// recenter the matrix so that pos becomes (12, 12)
void World::recenter(Pos pos) {
// Serial.print(pos.i); Serial.print(" "); Serial.println(pos.j);
while(pos.j < PLAYER_X) {
this->scrollLeft();
pos.j += 1;
}
while(pos.j > PLAYER_X) {
this->scrollRight();
pos.j -= 1;
}
while(pos.i < PLAYER_Y) {
this->scrollUp();
pos.i += 1;
}
while(pos.i > PLAYER_Y) {
this->scrollDown();
pos.i -= 1;
}
this->player->setLastPos(pos);
}
BoundingBox World::generateLine(int8_t i, int8_t jst, int8_t jend, int8_t anchor) {
int8_t where = random(jst, jend);
for(int8_t j = min(where, anchor); j <= max(where, anchor); ++j) {
this->worldMap[Pos(i, j)] = 1;
}
return BoundingBox(min(where, anchor), i, max(where, anchor) - min(where, anchor), 0);
}
// pointy lines look something like:
//*___*
//_***_
BoundingBox World::generatePointyLine(int8_t i, int8_t jst, int8_t jend, int8_t anchor) {
int8_t where = random(jst, jend);
if(max(where, anchor) - min(where, anchor) < 2) {
where = anchor + 2;
}
for(int8_t j = min(where, anchor); j <= max(where, anchor); ++j) {
if(j == min(where, anchor) || j == max(where, anchor)) {
this->worldMap[Pos(i - 1, j)] = 1;
}
else {
this->worldMap[Pos(i, j)] = 1;
}
}
return BoundingBox(min(where, anchor), i - 1, max(where, anchor) - min(where, anchor), 1);
}
BoundingBox World::generatePagoda(int8_t i, int8_t j) {
StructNode* last = this->first;
while(last != nullptr && last->next != nullptr) {
last = last->next;
}
Pagoda* pagoda = new Pagoda(i, j);
pagoda->draw(this);
StructNode* newNode = new StructNode(pagoda, nullptr);
if(last == nullptr) {
this->first = newNode;
}
else {
last->next = newNode;
}
return pagoda->getBoundingBox();
}
BoundingBox World::generateMovingPlatform(int8_t i, int8_t j) {
StructNode* last = this->first;
while(last != nullptr && last->next != nullptr) {
last = last->next;
}
MovingPlatform* movingPlatform = new MovingPlatform(i, j);
movingPlatform->draw(this);
StructNode* newNode = new StructNode(movingPlatform, nullptr);
if(last == nullptr) {
this->first = newNode;
}
else {
last->next = newNode;
}
return movingPlatform->getBoundingBox();
}
BoundingBox World::generateDisappPlatform(int8_t i, int8_t j) {
StructNode* last = this->first;
while(last != nullptr && last->next != nullptr) {
last = last->next;
}
DisappearingPlatform* disappearingPlatform = new DisappearingPlatform(i, j);
disappearingPlatform->draw(this);
StructNode* newNode = new StructNode(disappearingPlatform, nullptr);
if(last == nullptr) {
this->first = newNode;
}
else {
last->next = newNode;
}
return disappearingPlatform->getBoundingBox();
}
BoundingBox World::generateCanon(int8_t i, int8_t j) {
StructNode* last = this->first;
while(last != nullptr && last->next != nullptr) {
last = last->next;
}
Canon* canon = new Canon(i, j, random(1, 4));
canon->draw(this);
StructNode* newNode = new StructNode(canon, nullptr);
if(last == nullptr) {
this->first = newNode;
}
else {
last->next = newNode;
}
return canon->getBoundingBox();
}
// generate a structure in given params
BoundingBox World::generateStructure(int8_t x_first, int8_t y_first, int8_t x_last, int8_t y_last, int8_t xMax, int8_t yMax) {
byte x = random(x_first, x_last), y = random(y_first, y_last); // anchor point of structure
if(y == World::numRows - 1) {
return this->generateLine(y, x_first, xMax, x);
}
else {
byte max;
if(player->getHeight() >= CANON) {
max = 6;
}
else if(player->getHeight() >= DISAPPEARING) {
max = 5;
}
else if(player->getHeight() >= MOVING) {
max = 4;
}
else if(player->getHeight() >= PAGODAS) {
max = 3;
}
else {
max = 2;
}
byte dice = random(max);
if(dice == GEN_LINE) {
return this->generateLine(y, x_first, xMax, x);
}
else if(dice == GEN_POINTY) {
if(x == x_last - 1) {
x -= 3;
if(y == y_first)
y += 1;
}
else {
if(x != x_first)
x -= 1;
}
return this->generatePointyLine(y, x_first, xMax, x);
}
else if(dice == GEN_PAGODA) {
return this->generatePagoda(y, x);
}
else if(dice == GEN_MOVING) {
return this->generateMovingPlatform(y, x);
}
else if(dice == GEN_DISAPP) {
// avoid chaining disappearing platforms because it's honestly very confusing to navigate
if(this->justDisappeared) {
this->justDisappeared = false;
return this->generateMovingPlatform(y, x);
}
// disappearing platforms have preset shapes that require a certain positioning, relative to the last generated structure
this->justDisappeared = true;
return this->generateDisappPlatform(y_first, x_first + 4);
}
else if(dice == GEN_CANON) {
return this->generateCanon(y, x);
}
}
}
// get range from highest point to the left
BoundingBox World::getBestRange(BoundingBox structure) {
int8_t topy = 100, leftx = 100, height, width;
for(int8_t y = structure.y; y <= structure.y + structure.height; ++y) {
for(int8_t x = structure.x; x <= structure.x + structure.width; ++x) {
if(this->worldMap[Pos(y, x)].check() || structure.fillOnes) {
if(topy > max(y - Player::maxJump / Player::jumpInterval, 0)) {
topy = max(y - Player::maxJump / Player::jumpInterval, 0);
height = y - topy;
leftx = max(x - Player::maxJump / Player::moveIntervalInAir, 0);
width = min(2 * Player::maxJump / Player::moveIntervalInAir, World::numCols - 1 - leftx);
}
}
}
}
return BoundingBox(leftx, topy, width, height);
}
BoundingBox World::getMinDiff(BoundingBox structure) {
int8_t topy = 100, leftx = 100, height, width;
for(int8_t y = structure.y; y <= structure.y + structure.height; ++y) {
for(int8_t x = structure.x; x <= structure.x + structure.width; ++x) {
if(this->worldMap[Pos(y, x)].check() || structure.fillOnes) {
if(topy > max(y - this->difficultyStepY * this->difficulty, 0)) {
topy = max(y - this->difficultyStepY * this->difficulty, 0);
height = y - topy;
leftx = max(x -this->difficultyStepX * this->difficulty, 0);
width = min(2 * this->difficultyStepX * this->difficulty, World::numCols - 1 - leftx);
}
}
}
}
return BoundingBox(leftx, topy, width, height);
}
// get total jump range from a point
BoundingBox World::getTotalRange(int8_t y, int8_t x) {
int8_t left = max((x - Player::maxJump / Player::moveIntervalInAir), 0);
int8_t right = min(x + Player::maxJump / Player::moveIntervalInAir, World::numCols - 1);
int8_t up = max(y - Player::maxJump / Player::jumpInterval, 0);
return BoundingBox(left, up, right - left, y - up);
}
// get area which is reachable from last generated structure, but not from second last structure
// it's going to be a rought estimate, since we need a rectangle
BoundingBox World::withoutIntersection(BoundingBox s1, BoundingBox s2) {
if(s1.x <= s2.x) {
// look to the left
int8_t x = s1.x, y = s1.y, w = 0, hw = 0, wh = 0, h = 0;
/**
* @brief
* Since the other area of action is a square too, to find the optimal range without intersection we need to first try to make
* the width as big as possible, and try to make a rect with that, and subsequently do the same thing with the height. In the end,
* we'll be left with the biggest possible rectangle.
*/
if(s1.y < s2.y) {
w = s1.width;
wh = min(s2.y - s1.y - 1, s1.height);
}
else {
w = min(s2.x - s1.x - 1, s1.width);
wh = s1.height;
}
return BoundingBox(x, y, w, wh);
}
else {
// look to the right
int8_t x = s1.x + s1.width, y = s1.y, w = 0, hw = 0, wh = 0, h = 0;
if(s1.y < s2.y) {
w = s1.width;
wh = min(s2.y - s1.y - 1, s1.height);
}
else {
w = min(s2.x - s1.x - 1, s1.width);
wh = s1.height;
}
return BoundingBox(x - w, y, w, wh);
}
}
void World::generateFromLast(bool first = false) {
if(first) {
this->secondLast = BoundingBox(-100, -100, -100, -100);
this->last = this->generateLine(PLAYER_Y + 1, 8, 16, PLAYER_X);
this->player->setPosition(Pos(PLAYER_Y, PLAYER_X));
}
else {
if(this->secondLast.x == -100) {
// no second last yet, can generate anywhere within range for the first structure
this->secondLast = this->last;
// Serial.print("last is: x = "); Serial.print(last.x); Serial.print(" y = "); Serial.print(last.y); Serial.print(" height = "); Serial.print(last.height); Serial.print(" width = "); Serial.println(last.width);
BoundingBox range = this->getBestRange(this->last);
range = this->withoutIntersection(range, this->getMinDiff(this->last));
// Serial.print("range is: x = "); Serial.print(range.x); Serial.print(" y = "); Serial.print(range.y); Serial.print(" height = "); Serial.print(range.height); Serial.print(" width = "); Serial.println(range.width);
this->last = this->generateStructure(range.x, range.y, min(range.x + range.width + 1, World::numCols - 1),
min(range.y + range.height + 1, World::numRows - 1), min(range.x + range.width + 1 + 8, World::numCols - 1), min(range.y + range.height + 1, World::numRows - 1));
}
else {
BoundingBox rangeLast = this->getBestRange(this->last);
// Serial.print("last x = "); Serial.print(last.x); Serial.print(" last y = "); Serial.println(last.y);
// Serial.print("range last is: x = "); Serial.print(rangeLast.x); Serial.print(" y = "); Serial.print(rangeLast.y); Serial.print(" height = "); Serial.print(rangeLast.height); Serial.print(" width = "); Serial.println(rangeLast.width);
BoundingBox rangeSecondLast = this->getBestRange(this->secondLast);
// Serial.print("range second last is: x = "); Serial.print(rangeSecondLast.x); Serial.print(" y = "); Serial.print(rangeSecondLast.y); Serial.print(" height = "); Serial.print(rangeSecondLast.height); Serial.print(" width = "); Serial.println(rangeSecondLast.width);
BoundingBox range = this->withoutIntersection(rangeLast, rangeSecondLast);
// Serial.print("range is: x = "); Serial.print(range.x); Serial.print(" y = "); Serial.print(range.y); Serial.print(" height = "); Serial.print(range.height); Serial.print(" width = "); Serial.println(range.width);
range = this->withoutIntersection(range, this->getMinDiff(this->last));
this->secondLast = this->last;
this->last = this->generateStructure(range.x, range.y, min(range.x + range.width + 1, World::numCols - 1),
min(range.y + range.height + 1, World::numRows - 1), min(range.x + range.width + 1 + 8, World::numCols - 1), min(range.y + range.height + 1, World::numRows - 1));
}
}
}
void World::drawOnMatrix() {
int row = 0;
for(int i = CAMERA_Y; i < CAMERA_Y + CAMERA_DIM; ++i) {
int _col = 0;
for(int j = CAMERA_X; j < CAMERA_X + CAMERA_DIM; ++j) {
int actual_row = _col;
int col = row;
this->lc->setLed(0, col, actual_row, this->worldMap[Pos(i, j)].check());
_col++;
}
row += 1;
}
}
// called on object destructor, free all structures
void World::freeAllStructures() {
while(this->first) {
StructNode* toFree = this->first;
this->first = this->first->next;
delete toFree->structure;
delete toFree;
}
}
// free the structures that were "passed", i.e. they are below the last line or column
void World::freeStructures() {
StructNode* current = this->first;
while(current != nullptr) {
if(current == this->first) {
BoundingBox coords = current->structure->getBoundingBox();
if(coords.y >= World::numRows) {
StructNode* toFree = current;
this->first = this->first->next;
current = current->next;
delete toFree->structure;
delete toFree;
continue;
}
}
if(current->next != nullptr) {
BoundingBox coords = current->next->structure->getBoundingBox();
if(coords.y >= World::numRows) {
StructNode* toFree = current->next;
current->next = current->next->next;
delete toFree->structure;
delete toFree;
}
}
current = current->next;
}
}
void World::activateStructures() {
StructNode* current = this->first;
while(current) {
current->structure->activate(this->player);
current = current->next;
}
}