Skip to content

Commit 76dcb12

Browse files
committed
chance of adding bridges modified
1 parent c8b1cba commit 76dcb12

File tree

2 files changed

+342
-140
lines changed

2 files changed

+342
-140
lines changed

src/level_gen.cpp

+132-83
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
//
1919
// How many times to try creating a single level
2020
//
21-
static const int MAX_LEVELS = 100;
21+
static const int MAX_LEVELS = 1;
2222
static const int MAX_LEVEL_GEN_TRIES_FOR_SAME_SEED = 1000;
2323
static const int MAX_LEVEL_GEN_ROOM_PLACE_TRIES = 500;
24-
static const int MAX_LEVEL_GEN_CORRIDOR_LEN = 20;
2524
static const int MAX_LEVEL_GEN_MIN_BRIDGE_LEN = 6;
2625
static const int MAX_LEVEL_ROOM_COUNT = 25;
2726
static const int MAX_LEVEL_GEN_TRIES_CREATE_ROOM = MAX_LEVEL_ROOM_COUNT * 2;
@@ -1135,8 +1134,9 @@ static bool level_gen_trim_dead_tiles(Gamep g, class LevelGen *l, bool debug)
11351134
walkable_tile += level_gen_tile_is_walkable(g, l, x, y + 1) ? 1 : 0;
11361135

11371136
if (walkable_tile <= 1) {
1138-
l->data[ x ][ y ].c = CHARMAP_EMPTY;
1139-
did_something = true;
1137+
l->data[ x ][ y ].c = CHARMAP_EMPTY;
1138+
l->data[ x ][ y ].room = nullptr;
1139+
did_something = true;
11401140
}
11411141
break;
11421142
}
@@ -1154,8 +1154,9 @@ static bool level_gen_trim_dead_tiles(Gamep g, class LevelGen *l, bool debug)
11541154
empty_tile += (l->data[ x ][ y + 1 ].c == CHARMAP_EMPTY) ? 1 : 0;
11551155

11561156
if (empty_tile >= 3) {
1157-
l->data[ x ][ y ].c = CHARMAP_EMPTY;
1158-
did_something = true;
1157+
l->data[ x ][ y ].c = CHARMAP_EMPTY;
1158+
l->data[ x ][ y ].room = nullptr;
1159+
did_something = true;
11591160
}
11601161
break;
11611162
}
@@ -1216,111 +1217,145 @@ static void level_gen_scan_connected_rooms(Gamep g, class LevelGen *l, bool debu
12161217

12171218
//
12181219
// Add corridors between rooms that are not connected
1220+
// Shortest distance is 2, which is ". ."
12191221
//
1220-
static void level_gen_connect_adjacent_rooms(Gamep g, class LevelGen *l, bool debug)
1222+
static void level_gen_connect_adjacent_rooms_with_distance_and_chance(Gamep g, class LevelGen *l, int dist,
1223+
int chance, bool debug)
12211224
{
12221225
TRACE_NO_INDENT();
12231226

12241227
const std::initializer_list< point > directions = {point(-1, 0), point(1, 0), point(0, -1), point(0, 1)};
12251228

1226-
//
1227-
// Shortest distance is 2, which is ". ."
1228-
//
1229-
for (int dist = 2; dist < MAX_LEVEL_GEN_CORRIDOR_LEN; dist++) {
1230-
for (int y = dist; y < MAP_HEIGHT - dist - 1; y++) {
1231-
for (int x = dist; x < MAP_WIDTH - dist - 1; x++) {
1232-
switch (l->data[ x ][ y ].c) {
1233-
case CHARMAP_FLOOR :
1234-
{
1229+
for (int y = dist; y < MAP_HEIGHT - dist - 1; y++) {
1230+
for (int x = dist; x < MAP_WIDTH - dist - 1; x++) {
1231+
switch (l->data[ x ][ y ].c) {
1232+
case CHARMAP_FLOOR :
1233+
{
1234+
//
1235+
// Decrease the chance of connecting leaf rooms so we don't get too many
1236+
//
1237+
if (d100() > chance - l->rooms_adj_connected * 5) {
1238+
continue;
1239+
}
1240+
1241+
for (auto direction : directions) {
12351242
//
1236-
// Decrease the chance of connecting leaf rooms so we don't get too many
1243+
// Check there is nothing in the way
12371244
//
1238-
if (d100() > 10 - l->rooms_adj_connected) {
1245+
bool has_clear_path = true;
1246+
for (auto d = 1; d < dist; d++) {
1247+
point adj(x + direction.x * d, y + direction.y * d);
1248+
if ((l->data[ adj.x ][ adj.y ].c != CHARMAP_EMPTY)) {
1249+
has_clear_path = false;
1250+
break;
1251+
}
1252+
}
1253+
if (! has_clear_path) {
12391254
continue;
12401255
}
12411256

1242-
for (auto direction : directions) {
1243-
//
1244-
// Check there is nothing in the way
1245-
//
1246-
bool has_clear_path = true;
1247-
for (auto d = 1; d < dist; d++) {
1248-
point adj(x + direction.x * d, y + direction.y * d);
1249-
if ((l->data[ adj.x ][ adj.y ].c != CHARMAP_EMPTY)) {
1250-
has_clear_path = false;
1251-
break;
1252-
}
1253-
}
1254-
if (! has_clear_path) {
1255-
continue;
1256-
}
1257+
//
1258+
// Check there is a room at the end of the blank space
1259+
//
1260+
point dest(x + direction.x * dist, y + direction.y * dist);
1261+
if (l->data[ dest.x ][ dest.y ].c != CHARMAP_FLOOR) {
1262+
continue;
1263+
}
12571264

1258-
//
1259-
// Check there is a room at the end of the blank space
1260-
//
1261-
point dest(x + direction.x * dist, y + direction.y * dist);
1262-
if (l->data[ dest.x ][ dest.y ].c != CHARMAP_FLOOR) {
1263-
continue;
1264-
}
1265+
auto room_a = l->data[ x ][ y ].room;
1266+
auto room_b = l->data[ dest.x ][ dest.y ].room;
12651267

1266-
auto room_a = l->data[ x ][ y ].room;
1267-
auto room_b = l->data[ dest.x ][ dest.y ].room;
1268+
//
1269+
// Check the two rooms at either end of the corridor are different.
1270+
//
1271+
if (room_a == room_b) {
1272+
continue;
1273+
}
12681274

1269-
//
1270-
// Check the two rooms at either end of the corridor are different.
1271-
//
1272-
if (room_a == room_b) {
1273-
continue;
1274-
}
1275+
//
1276+
// Keep connections to the start room minimal
1277+
//
1278+
if (room_a->room_type == ROOM_TYPE_START) {
1279+
continue;
1280+
}
1281+
if (room_b->room_type == ROOM_TYPE_START) {
1282+
continue;
1283+
}
12751284

1276-
//
1277-
// Keep connections to the start room minimal
1278-
//
1279-
if (room_a->room_type != room_b->room_type) {
1280-
continue;
1285+
std::pair< class Room *, class Room * > conn;
1286+
if (room_a && room_b) {
1287+
if (room_a < room_b) {
1288+
conn.first = room_a;
1289+
conn.second = room_b;
1290+
} else {
1291+
conn.first = room_b;
1292+
conn.second = room_a;
12811293
}
1294+
}
12821295

1283-
std::pair< class Room *, class Room * > conn;
1284-
if (room_a && room_b) {
1285-
if (room_a < room_b) {
1286-
conn.first = room_a;
1287-
conn.second = room_b;
1296+
//
1297+
// If the rooms are not connected, then join them via a corridor
1298+
//
1299+
if (l->rooms_connected.find(conn) == l->rooms_connected.end()) {
1300+
l->rooms_adj_connected++;
1301+
l->rooms_connected[ conn ] = true;
1302+
for (auto d = 1; d < dist; d++) {
1303+
point adj(x + direction.x * d, y + direction.y * d);
1304+
1305+
if (dist >= MAX_LEVEL_GEN_MIN_BRIDGE_LEN) {
1306+
l->data[ adj.x ][ adj.y ].c = CHARMAP_BRIDGE;
12881307
} else {
1289-
conn.first = room_b;
1290-
conn.second = room_a;
1308+
l->data[ adj.x ][ adj.y ].c = CHARMAP_CORRIDOR;
12911309
}
1292-
}
12931310

1294-
//
1295-
// If the rooms are not connected, then join them via a corridor
1296-
//
1297-
if (l->rooms_connected.find(conn) == l->rooms_connected.end()) {
1298-
l->rooms_adj_connected++;
1299-
l->rooms_connected[ conn ] = true;
1300-
for (auto d = 1; d < dist; d++) {
1301-
point adj(x + direction.x * d, y + direction.y * d);
1302-
1303-
if (dist >= MAX_LEVEL_GEN_MIN_BRIDGE_LEN) {
1304-
l->data[ adj.x ][ adj.y ].c = CHARMAP_BRIDGE;
1305-
} else {
1306-
l->data[ adj.x ][ adj.y ].c = CHARMAP_CORRIDOR;
1307-
}
1308-
1309-
l->data[ adj.x ][ adj.y ].room = room_a;
1310-
}
1311+
l->data[ adj.x ][ adj.y ].room = room_a;
13111312
}
13121313
}
1313-
1314-
break;
13151314
}
1316-
}
1315+
1316+
break;
1317+
}
13171318
}
13181319
}
13191320
}
13201321
}
13211322

13221323
//
1323-
// Make bridges dramatic...
1324+
// Add corridors between rooms that are not connected
1325+
// Shortest distance is 2, which is ". ."
1326+
//
1327+
static void level_gen_connect_adjacent_rooms(Gamep g, class LevelGen *l, bool debug)
1328+
{
1329+
TRACE_NO_INDENT();
1330+
1331+
const std::initializer_list< std::pair< int, int > > dists = {
1332+
std::pair(2 /* corridor length */, 100 /* percentage chance of occuring */),
1333+
std::pair(3 /* corridor length */, 100 /* percentage chance of occuring */),
1334+
std::pair(4 /* corridor length */, 100 /* percentage chance of occuring */),
1335+
std::pair(5 /* corridor length */, 90 /* percentage chance of occuring */),
1336+
std::pair(6 /* corridor length */, 90 /* percentage chance of occuring */),
1337+
std::pair(7 /* corridor length */, 90 /* percentage chance of occuring */),
1338+
std::pair(8 /* corridor length */, 50 /* percentage chance of occuring */),
1339+
std::pair(9 /* corridor length */, 50 /* percentage chance of occuring */),
1340+
std::pair(10 /* corridor length */, 50 /* percentage chance of occuring */),
1341+
std::pair(11 /* corridor length */, 50 /* percentage chance of occuring */),
1342+
std::pair(12 /* corridor length */, 10 /* percentage chance of occuring */),
1343+
std::pair(13 /* corridor length */, 10 /* percentage chance of occuring */),
1344+
std::pair(14 /* corridor length */, 10 /* percentage chance of occuring */),
1345+
std::pair(15 /* corridor length */, 10 /* percentage chance of occuring */),
1346+
std::pair(16 /* corridor length */, 5 /* percentage chance of occuring */),
1347+
std::pair(17 /* corridor length */, 5 /* percentage chance of occuring */),
1348+
std::pair(18 /* corridor length */, 5 /* percentage chance of occuring */),
1349+
std::pair(19 /* corridor length */, 5 /* percentage chance of occuring */),
1350+
std::pair(20 /* corridor length */, 5 /* percentage chance of occuring */),
1351+
};
1352+
for (auto d : dists) {
1353+
level_gen_connect_adjacent_rooms_with_distance_and_chance(g, l, d.first, d.second, debug);
1354+
}
1355+
}
1356+
1357+
//
1358+
// Make bridges dramatic by adding chasms around them
13241359
//
13251360
static void level_gen_add_chasms_around_bridges(Gamep g, class LevelGen *l, bool debug)
13261361
{
@@ -1364,10 +1399,24 @@ static class LevelGen *level_gen(Gamep g, int which)
13641399
return l;
13651400
}
13661401

1402+
//
1403+
// Get rid of tiles that go nowhere
1404+
//
13671405
while (level_gen_trim_dead_tiles(g, l, debug)) {}
13681406

1407+
//
1408+
// Keep track of which room is connected to another via a door
1409+
//
13691410
level_gen_scan_connected_rooms(g, l, debug);
1411+
1412+
//
1413+
// Add corridors between rooms that are not connected
1414+
//
13701415
level_gen_connect_adjacent_rooms(g, l, debug);
1416+
1417+
//
1418+
// Make bridges dramatic by adding chasms around them
1419+
//
13711420
level_gen_add_chasms_around_bridges(g, l, debug);
13721421

13731422
return l;
@@ -1395,7 +1444,7 @@ static void level_gen_create_level(Gamep g, int which)
13951444
// Per thread seed
13961445
//
13971446
auto seed = game_get_seed_num(g);
1398-
game_set_seed_for_thread(g, seed * which);
1447+
game_set_seed_for_thread(g, seed + seed * which);
13991448

14001449
auto l = level_gen(g, which);
14011450
levels[ which ] = l;

0 commit comments

Comments
 (0)