Skip to content

Commit 7138884

Browse files
committed
cellular auto to the edges
1 parent fdde549 commit 7138884

File tree

2 files changed

+38
-44
lines changed

2 files changed

+38
-44
lines changed

src/level_gen.cpp

+32-38
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ static const int MAX_LEVEL_GEN_TRIES_CREATE_ROOM = MAX_LEVEL_ROOM_COUNT * 2;
2727
static const int MIN_LEVEL_ROOM_COUNT = 10;
2828
static const int MIN_LEVEL_EXIT_DISTANCE = (MAP_WIDTH / 4) * 2;
2929
static const int MAP_LEVEL_BLOB_BORDER = MAP_WIDTH / 4;
30+
static const int MAP_LEVEL_CELLULAR_BORDER = 2;
3031

3132
class Cell;
3233
class Room;
@@ -92,8 +93,8 @@ class Cave
9293
//
9394
// Used for cellular automata
9495
//
95-
uint8_t curr[ MAP_WIDTH ][ MAP_HEIGHT ];
96-
uint8_t prev[ MAP_WIDTH ][ MAP_HEIGHT ];
96+
uint8_t curr[ MAP_WIDTH + MAP_LEVEL_CELLULAR_BORDER ][ MAP_HEIGHT + MAP_LEVEL_CELLULAR_BORDER ];
97+
uint8_t prev[ MAP_WIDTH + MAP_LEVEL_CELLULAR_BORDER ][ MAP_HEIGHT + MAP_LEVEL_CELLULAR_BORDER ];
9798

9899
//
99100
// Keeps track of the largest blob so fat
@@ -1094,7 +1095,7 @@ static void cave_dump(Gamep g, class LevelGen *l)
10941095
for (y = 0; y < MAP_HEIGHT; y++) {
10951096
printf("|");
10961097
for (x = 0; x < MAP_WIDTH; x++) {
1097-
if (l->cave.curr[ x ][ y ]) {
1098+
if (l->cave.curr[ x + MAP_LEVEL_CELLULAR_BORDER ][ y + MAP_LEVEL_CELLULAR_BORDER ]) {
10981099
printf("x");
10991100
} else {
11001101
printf(" ");
@@ -1124,10 +1125,11 @@ static void cave_generation(Gamep g, class LevelGen *l, uint32_t fill_prob, int
11241125
//
11251126
if (! map_generations) {
11261127
memset(l->cave.curr, 0, sizeof(l->cave.curr));
1127-
for (x = 2; x < MAP_WIDTH - 2; x++) {
1128-
for (y = 2; y < MAP_HEIGHT - 2; y++) {
1128+
1129+
for (x = 0; x < MAP_WIDTH; x++) {
1130+
for (y = 0; y < MAP_HEIGHT; y++) {
11291131
if (pcg_random_range(0, 10000) < fill_prob) {
1130-
l->cave.curr[ x ][ y ] = 1;
1132+
l->cave.curr[ x + MAP_LEVEL_CELLULAR_BORDER ][ y + MAP_LEVEL_CELLULAR_BORDER ] = 1;
11311133
}
11321134
}
11331135
}
@@ -1138,12 +1140,12 @@ static void cave_generation(Gamep g, class LevelGen *l, uint32_t fill_prob, int
11381140
cave_dump(g, l);
11391141
}
11401142

1141-
for (x = 2; x < MAP_WIDTH - 2; x++) {
1142-
for (y = 2; y < MAP_HEIGHT - 2; y++) {
1143+
for (x = 0; x < MAP_WIDTH; x++) {
1144+
for (y = 0; y < MAP_HEIGHT; y++) {
11431145

11441146
uint8_t adjcount = 0;
11451147

1146-
#define ADJ(i, j) adjcount += l->cave.curr[ x + i ][ y + j ];
1148+
#define ADJ(i, j) adjcount += l->cave.curr[ x + i + MAP_LEVEL_CELLULAR_BORDER ][ y + j + MAP_LEVEL_CELLULAR_BORDER ];
11471149

11481150
ADJ(-1, -1);
11491151
ADJ(-1, 0);
@@ -1186,7 +1188,7 @@ static void cave_generation(Gamep g, class LevelGen *l, uint32_t fill_prob, int
11861188
// prev set to 0 already.
11871189
//
11881190
} else {
1189-
l->cave.prev[ x ][ y ] = 1;
1191+
l->cave.prev[ x + MAP_LEVEL_CELLULAR_BORDER ][ y + MAP_LEVEL_CELLULAR_BORDER ] = 1;
11901192
}
11911193
}
11921194
}
@@ -1211,22 +1213,6 @@ static void cave_generations(Gamep g, class LevelGen *l, uint32_t fill_prob, int
12111213

12121214
static int cave_generation_fill_blob_cand(Gamep g, class Cave *c, int x, int y, uint16_t size, uint16_t id)
12131215
{
1214-
//
1215-
// Out of bounds?
1216-
//
1217-
if (x < 2) {
1218-
return size;
1219-
}
1220-
if (y < 2) {
1221-
return size;
1222-
}
1223-
if (x > MAP_WIDTH - 2) {
1224-
return size;
1225-
}
1226-
if (x > MAP_HEIGHT - 2) {
1227-
return size;
1228-
}
1229-
12301216
//
12311217
// Already walked?
12321218
//
@@ -1238,7 +1224,7 @@ static int cave_generation_fill_blob_cand(Gamep g, class Cave *c, int x, int y,
12381224
//
12391225
// If nothing here, stop the recurse
12401226
//
1241-
auto i = c->curr[ x ][ y ];
1227+
auto i = c->curr[ x + MAP_LEVEL_CELLULAR_BORDER ][ y + MAP_LEVEL_CELLULAR_BORDER ];
12421228
if (! i) {
12431229
return size;
12441230
}
@@ -1247,10 +1233,18 @@ static int cave_generation_fill_blob_cand(Gamep g, class Cave *c, int x, int y,
12471233
// Increase the blob size
12481234
//
12491235
size += i;
1250-
size = cave_generation_fill_blob_cand(g, c, x - 1, y, size, id);
1251-
size = cave_generation_fill_blob_cand(g, c, x + 1, y, size, id);
1252-
size = cave_generation_fill_blob_cand(g, c, x, y - 1, size, id);
1253-
size = cave_generation_fill_blob_cand(g, c, x, y + 1, size, id);
1236+
if (x > 0) {
1237+
size = cave_generation_fill_blob_cand(g, c, x - 1, y, size, id);
1238+
}
1239+
if (x < MAP_WIDTH - 1) {
1240+
size = cave_generation_fill_blob_cand(g, c, x + 1, y, size, id);
1241+
}
1242+
if (y > 0) {
1243+
size = cave_generation_fill_blob_cand(g, c, x, y - 1, size, id);
1244+
}
1245+
if (y < MAP_HEIGHT - 1) {
1246+
size = cave_generation_fill_blob_cand(g, c, x, y + 1, size, id);
1247+
}
12541248

12551249
return size;
12561250
}
@@ -1278,7 +1272,7 @@ static void cave_generation_keep_largest_blob(Gamep g, class Cave *c)
12781272
//
12791273
for (x = MAP_LEVEL_BLOB_BORDER; x < MAP_WIDTH - MAP_LEVEL_BLOB_BORDER; x++) {
12801274
for (y = MAP_LEVEL_BLOB_BORDER; y < MAP_HEIGHT - MAP_LEVEL_BLOB_BORDER; y++) {
1281-
if (c->curr[ x ][ y ] && ! c->blob.id[ x ][ y ]) {
1275+
if (c->curr[ x + MAP_LEVEL_CELLULAR_BORDER ][ y + MAP_LEVEL_CELLULAR_BORDER ] && ! c->blob.id[ x ][ y ]) {
12821276
//
12831277
// Flood fill and get the size of this blob
12841278
//
@@ -1302,10 +1296,10 @@ static void cave_generation_keep_largest_blob(Gamep g, class Cave *c)
13021296
y = c->blob.largest_at.y;
13031297
uint16_t best_id = c->blob.id[ x ][ y ];
13041298

1305-
for (x = 2; x < MAP_WIDTH - 2; x++) {
1306-
for (y = 2; y < MAP_HEIGHT - 2; y++) {
1299+
for (x = 0; x < MAP_WIDTH; x++) {
1300+
for (y = 0; y < MAP_HEIGHT; y++) {
13071301
if (c->blob.id[ x ][ y ] != best_id) {
1308-
c->curr[ x ][ y ] = 0;
1302+
c->curr[ x + MAP_LEVEL_CELLULAR_BORDER ][ y + MAP_LEVEL_CELLULAR_BORDER ] = 0;
13091303
}
13101304
}
13111305
}
@@ -1329,9 +1323,9 @@ static void level_gen_water(Gamep g, class LevelGen *l)
13291323
//
13301324
cave_generation_keep_largest_blob(g, &l->cave);
13311325

1332-
for (x = 2; x < MAP_WIDTH - 2; x++) {
1333-
for (y = 2; y < MAP_HEIGHT - 2; y++) {
1334-
if (l->cave.curr[ x ][ y ]) {
1326+
for (x = 0; x < MAP_WIDTH; x++) {
1327+
for (y = 0; y < MAP_HEIGHT; y++) {
1328+
if (l->cave.curr[ x + MAP_LEVEL_CELLULAR_BORDER ][ y + MAP_LEVEL_CELLULAR_BORDER ]) {
13351329

13361330
switch (l->data[ x ][ y ].c) {
13371331
case CHARMAP_KEY :

src/rooms.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,7 @@ void rooms_init(Gamep g)
16061606
(const char *)" ^ ",
16071607
nullptr);
16081608

1609-
room_add(g, __FUNCTION__, __LINE__, "",
1609+
room_add(g, __FUNCTION__, __LINE__,
16101610
(const char *)" ^ ",
16111611
(const char *)" 5555.......555555 ",
16121612
(const char *)" 5555..........5555 ",
@@ -1628,7 +1628,7 @@ void rooms_init(Gamep g)
16281628
(const char *)" ^ ",
16291629
nullptr);
16301630

1631-
room_add(g, __FUNCTION__, __LINE__, "",
1631+
room_add(g, __FUNCTION__, __LINE__,
16321632
(const char *)" ^ ",
16331633
(const char *)" 5555.......555555 ",
16341634
(const char *)" 5555.......C..5555 ",
@@ -1650,7 +1650,7 @@ void rooms_init(Gamep g)
16501650
(const char *)" ^ ",
16511651
nullptr);
16521652

1653-
room_add(g, __FUNCTION__, __LINE__, "",
1653+
room_add(g, __FUNCTION__, __LINE__,
16541654
(const char *)" ^ ",
16551655
(const char *)" 5555.......555555 ",
16561656
(const char *)" 5555..........5555 ",
@@ -1676,7 +1676,7 @@ void rooms_init(Gamep g)
16761676
(const char *)" ^ ",
16771677
nullptr);
16781678

1679-
room_add(g, __FUNCTION__, __LINE__, "",
1679+
room_add(g, __FUNCTION__, __LINE__,
16801680
(const char *)" ^ ",
16811681
(const char *)" 5555.... $.555555 ",
16821682
(const char *)" 5555..... ....5555 ",
@@ -1702,7 +1702,7 @@ void rooms_init(Gamep g)
17021702
(const char *)" ^ ",
17031703
nullptr);
17041704

1705-
room_add(g, __FUNCTION__, __LINE__, "",
1705+
room_add(g, __FUNCTION__, __LINE__,
17061706
(const char *)" ^ ",
17071707
(const char *)" 5555..........55555 ",
17081708
(const char *)" 5555.............5555 ",
@@ -1728,7 +1728,7 @@ void rooms_init(Gamep g)
17281728
(const char *)" ^ ",
17291729
nullptr);
17301730

1731-
room_add(g, __FUNCTION__, __LINE__, "",
1731+
room_add(g, __FUNCTION__, __LINE__,
17321732
(const char *)" ^ ",
17331733
(const char *)" 5555..........55555 ",
17341734
(const char *)" 5555.............5555 ",

0 commit comments

Comments
 (0)