@@ -27,6 +27,7 @@ static const int MAX_LEVEL_GEN_TRIES_CREATE_ROOM = MAX_LEVEL_ROOM_COUNT * 2;
27
27
static const int MIN_LEVEL_ROOM_COUNT = 10 ;
28
28
static const int MIN_LEVEL_EXIT_DISTANCE = (MAP_WIDTH / 4 ) * 2 ;
29
29
static const int MAP_LEVEL_BLOB_BORDER = MAP_WIDTH / 4 ;
30
+ static const int MAP_LEVEL_CELLULAR_BORDER = 2 ;
30
31
31
32
class Cell ;
32
33
class Room ;
@@ -92,8 +93,8 @@ class Cave
92
93
//
93
94
// Used for cellular automata
94
95
//
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 ];
97
98
98
99
//
99
100
// Keeps track of the largest blob so fat
@@ -1094,7 +1095,7 @@ static void cave_dump(Gamep g, class LevelGen *l)
1094
1095
for (y = 0 ; y < MAP_HEIGHT; y++) {
1095
1096
printf (" |" );
1096
1097
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 ]) {
1098
1099
printf (" x" );
1099
1100
} else {
1100
1101
printf (" " );
@@ -1124,10 +1125,11 @@ static void cave_generation(Gamep g, class LevelGen *l, uint32_t fill_prob, int
1124
1125
//
1125
1126
if (! map_generations) {
1126
1127
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++) {
1129
1131
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 ;
1131
1133
}
1132
1134
}
1133
1135
}
@@ -1138,12 +1140,12 @@ static void cave_generation(Gamep g, class LevelGen *l, uint32_t fill_prob, int
1138
1140
cave_dump (g, l);
1139
1141
}
1140
1142
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++) {
1143
1145
1144
1146
uint8_t adjcount = 0 ;
1145
1147
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 ];
1147
1149
1148
1150
ADJ (-1 , -1 );
1149
1151
ADJ (-1 , 0 );
@@ -1186,7 +1188,7 @@ static void cave_generation(Gamep g, class LevelGen *l, uint32_t fill_prob, int
1186
1188
// prev set to 0 already.
1187
1189
//
1188
1190
} else {
1189
- l->cave .prev [ x ][ y ] = 1 ;
1191
+ l->cave .prev [ x + MAP_LEVEL_CELLULAR_BORDER ][ y + MAP_LEVEL_CELLULAR_BORDER ] = 1 ;
1190
1192
}
1191
1193
}
1192
1194
}
@@ -1211,22 +1213,6 @@ static void cave_generations(Gamep g, class LevelGen *l, uint32_t fill_prob, int
1211
1213
1212
1214
static int cave_generation_fill_blob_cand (Gamep g, class Cave *c, int x, int y, uint16_t size, uint16_t id)
1213
1215
{
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
-
1230
1216
//
1231
1217
// Already walked?
1232
1218
//
@@ -1238,7 +1224,7 @@ static int cave_generation_fill_blob_cand(Gamep g, class Cave *c, int x, int y,
1238
1224
//
1239
1225
// If nothing here, stop the recurse
1240
1226
//
1241
- auto i = c->curr [ x ][ y ];
1227
+ auto i = c->curr [ x + MAP_LEVEL_CELLULAR_BORDER ][ y + MAP_LEVEL_CELLULAR_BORDER ];
1242
1228
if (! i) {
1243
1229
return size;
1244
1230
}
@@ -1247,10 +1233,18 @@ static int cave_generation_fill_blob_cand(Gamep g, class Cave *c, int x, int y,
1247
1233
// Increase the blob size
1248
1234
//
1249
1235
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
+ }
1254
1248
1255
1249
return size;
1256
1250
}
@@ -1278,7 +1272,7 @@ static void cave_generation_keep_largest_blob(Gamep g, class Cave *c)
1278
1272
//
1279
1273
for (x = MAP_LEVEL_BLOB_BORDER; x < MAP_WIDTH - MAP_LEVEL_BLOB_BORDER; x++) {
1280
1274
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 ]) {
1282
1276
//
1283
1277
// Flood fill and get the size of this blob
1284
1278
//
@@ -1302,10 +1296,10 @@ static void cave_generation_keep_largest_blob(Gamep g, class Cave *c)
1302
1296
y = c->blob .largest_at .y ;
1303
1297
uint16_t best_id = c->blob .id [ x ][ y ];
1304
1298
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++) {
1307
1301
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 ;
1309
1303
}
1310
1304
}
1311
1305
}
@@ -1329,9 +1323,9 @@ static void level_gen_water(Gamep g, class LevelGen *l)
1329
1323
//
1330
1324
cave_generation_keep_largest_blob (g, &l->cave );
1331
1325
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 ]) {
1335
1329
1336
1330
switch (l->data [ x ][ y ].c ) {
1337
1331
case CHARMAP_KEY :
0 commit comments