Skip to content

Commit 073c5d7

Browse files
committed
Ensure errors are avoided with blocksize
Blocksize was always to be replaced with elevationMax, which is depth. To avoid crashes, width & height are changed to ensure they are divisible by elevationMax.
1 parent 2fbabdb commit 073c5d7

File tree

1 file changed

+51
-219
lines changed

1 file changed

+51
-219
lines changed

src/TerrainGen/Terrain_Gen.cpp

Lines changed: 51 additions & 219 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,25 @@ Dictionary TerrainGen::generate(
2828
int dx[4] = { 1, -1, 0, 0 };
2929
int dy[4] = { 0, 0, 1, -1 };
3030

31+
if (elevationMax != 0) {
32+
int remH = height % elevationMax;
33+
if (remH != 0)
34+
height += elevationMax - remH;
35+
36+
int remW = width % elevationMax;
37+
if (remW != 0)
38+
width += elevationMax - remW;
39+
}
40+
3141
// Dual Grid System Setup
3242
//
3343
// Produce a 2x Grid for the Data Grid
3444
//
3545
int widthx2 = width * 2;
3646
int heightx2 = height * 2;
3747

38-
// TODO : Fix blocksize so it doesn't cause errors
39-
//
40-
// Noise Conversion Variables
41-
//
42-
int blockSize = 4;
43-
int reducedX = floor(widthx2 / blockSize);
44-
int reducedY = floor(heightx2 / blockSize);
48+
int reducedX = floor(widthx2 / elevationMax);
49+
int reducedY = floor(heightx2 / elevationMax);
4550

4651
//
4752
// Open Area Tracker
@@ -142,8 +147,8 @@ Dictionary TerrainGen::generate(
142147
// Reduce Resolution by sampling one pixel per block
143148
for (int x = 0; x < reducedX; x++) {
144149
for (int y = 0; y < reducedY; y++) {
145-
int sampleX = x * blockSize;
146-
int sampleY = y * blockSize;
150+
int sampleX = x * elevationMax;
151+
int sampleY = y * elevationMax;
147152

148153
lowResMap[x][y] = heightMap[sampleX][sampleY];
149154
}
@@ -160,8 +165,8 @@ Dictionary TerrainGen::generate(
160165
// Upscale back to Original Size
161166
for (int x = 0; x < widthx2; x++) {
162167
for (int y = 0; y < heightx2; y++) {
163-
int srcX = x / blockSize;
164-
int srcY = y / blockSize;
168+
int srcX = x / elevationMax;
169+
int srcY = y / elevationMax;
165170

166171
int elevationValue = lowResMap[srcX][srcY];
167172

@@ -893,8 +898,6 @@ Dictionary TerrainGen::generate(
893898
// Cliff's & Ramp's Corner
894899
//-------------------------//
895900

896-
// TODO : Ramp Corner needs a Ground Tile placed one elevation below it
897-
898901
// Corner EAST
899902
// +----+----+ +---+---+
900903
// | n1 | n2 | | 1 | 1 |
@@ -1066,199 +1069,6 @@ Dictionary TerrainGen::generate(
10661069
}
10671070
}
10681071

1069-
//
1070-
// Phase 2 : Determine the Tile's Rotation
1071-
//
1072-
// Tile Rotations are dependent on the surrounding tile types
1073-
//
1074-
//
1075-
// +----+----+----+ +----+----+----+
1076-
// | m1 | m2 | m3 | | n1 | n2 | n3 |
1077-
// +----+----+----+ +----+----+----+
1078-
// | m4 | c | m5 | | n4 | c | n5 |
1079-
// +----+----+----+ +----+----+----+
1080-
// | m6 | m7 | m8 | | n6 | n7 | n8 |
1081-
// +----+----+----+ +----+----+----+
1082-
//
1083-
// for (int x = 0; x < width; x++) {
1084-
// for (int y = 0; y < height; y++) {
1085-
// int c = elevationMap[y][x];
1086-
// int n = myGridMap->get_cell_item(Vector3i(x, c, y));
1087-
1088-
// if (n == WATER || n == GROUND) { // Water & Ground Don't need rotations
1089-
// break;
1090-
// }
1091-
1092-
// auto safe_height = [&](int xx, int yy, int c_height) {
1093-
// if (xx < 0 || yy < 0 || xx >= width || yy >= height)
1094-
// return c_height; // out of bounds → pretend it's the center
1095-
// return elevationMap[xx][yy];
1096-
// };
1097-
1098-
// int m1 = safe_height(x - 1, y - 1, c);
1099-
// int m2 = safe_height(x - 1, y, c);
1100-
// int m3 = safe_height(x - 1, y + 1, c);
1101-
// int m4 = safe_height(x, y - 1, c);
1102-
// int m5 = safe_height(x, y + 1, c);
1103-
// int m6 = safe_height(x + 1, y - 1, c);
1104-
// int m7 = safe_height(x + 1, y, c);
1105-
// int m8 = safe_height(x + 1, y + 1, c);
1106-
1107-
// // Empty Cell's return -1
1108-
1109-
// int n1 = myGridMap->get_cell_item(Vector3i(x, m1, y));
1110-
// int n2 = myGridMap->get_cell_item(Vector3i(x, m2, y));
1111-
// int n3 = myGridMap->get_cell_item(Vector3i(x, m3, y));
1112-
// int n4 = myGridMap->get_cell_item(Vector3i(x, m4, y));
1113-
// int n5 = myGridMap->get_cell_item(Vector3i(x, m5, y));
1114-
// int n6 = myGridMap->get_cell_item(Vector3i(x, m6, y));
1115-
// int n7 = myGridMap->get_cell_item(Vector3i(x, m7, y));
1116-
// int n8 = myGridMap->get_cell_item(Vector3i(x, m8, y));
1117-
1118-
// // TODO : Determine rotation based on neighbors
1119-
1120-
// myGridMap->set_cell_item(Vector3i(x, elevationMap[x][y], y), tileMap[x][y], NORTH);
1121-
// }
1122-
// }
1123-
1124-
//
1125-
// Phase 2 : Determine the Tile's Rotation
1126-
// Rotations depend on surrounding tile types and elevations.
1127-
// We point uphill toward the highest GROUND neighbor (cardinals only).
1128-
//
1129-
// for (int x = 0; x < width; x++) {
1130-
// for (int y = 0; y < height; y++) {
1131-
// // Current cell elevation and tile id
1132-
// int c_height = elevationMapTiles[x][y];
1133-
// int tile_id = myGridMap->get_cell_item(Vector3i(x, c_height, y));
1134-
1135-
// // Skip tiles that don't need rotation
1136-
// if (tile_id == WATER || tile_id == GROUND) {
1137-
// continue;
1138-
// }
1139-
1140-
// // Safe elevation fetch that treats OOB as flat (center height)
1141-
// auto safe_height = [&](int gx, int gy, int fallback_h) {
1142-
// if (gx < 0 || gy < 0 || gx >= width || gy >= height) {
1143-
// return fallback_h;
1144-
// }
1145-
// return elevationMap[gx][gy];
1146-
// };
1147-
1148-
// // Safe neighbor tile fetch; OOB → -1 (empty)
1149-
// auto safe_tile_at = [&](int gx, int gy) {
1150-
// if (gx < 0 || gy < 0 || gx >= width || gy >= height) {
1151-
// return -1;
1152-
// }
1153-
// int h = elevationMap[gx][gy];
1154-
// return myGridMap->get_cell_item(Vector3i(gx, h, gy));
1155-
// };
1156-
1157-
// int best_dir = NORTH;
1158-
// float best_diff = -1e9f;
1159-
1160-
// {
1161-
// int nx = x + 0;
1162-
// int ny = y - 1;
1163-
// int nh = safe_height(nx, ny, c_height);
1164-
// int nt = safe_tile_at(nx, ny);
1165-
// if (nt == GROUND) {
1166-
// float diff = float(nh) - float(c_height);
1167-
// if (diff > best_diff) {
1168-
// best_diff = diff;
1169-
// best_dir = SOUTH;
1170-
// }
1171-
// }
1172-
// }
1173-
1174-
// {
1175-
// int nx = x + 0;
1176-
// int ny = y + 1;
1177-
// int nh = safe_height(nx, ny, c_height);
1178-
// int nt = safe_tile_at(nx, ny);
1179-
// if (nt == GROUND) {
1180-
// float diff = float(nh) - float(c_height);
1181-
// if (diff > best_diff) {
1182-
// best_diff = diff;
1183-
// best_dir = NORTH;
1184-
// }
1185-
// }
1186-
// }
1187-
1188-
// {
1189-
// int nx = x + 1;
1190-
// int ny = y + 0;
1191-
// int nh = safe_height(nx, ny, c_height);
1192-
// int nt = safe_tile_at(nx, ny);
1193-
// if (nt == GROUND) {
1194-
// float diff = float(nh) - float(c_height);
1195-
// if (diff > best_diff) {
1196-
// best_diff = diff;
1197-
// best_dir = WEST;
1198-
// }
1199-
// }
1200-
// }
1201-
1202-
// // WEST (-1, 0)
1203-
// {
1204-
// int nx = x - 1;
1205-
// int ny = y + 0;
1206-
// int nh = safe_height(nx, ny, c_height);
1207-
// int nt = safe_tile_at(nx, ny);
1208-
// if (nt == GROUND) {
1209-
// float diff = float(nh) - float(c_height);
1210-
// if (diff > best_diff) {
1211-
// best_diff = diff;
1212-
// best_dir = EAST;
1213-
// }
1214-
// }
1215-
// }
1216-
1217-
// // If no GROUND neighbor exists, optionally fall back to steepest slope regardless of type
1218-
// if (best_diff <= -1e8f) {
1219-
// // Evaluate all four directions using elevation only
1220-
// {
1221-
// int nx = x + 0, ny = y - 1;
1222-
// int nh = safe_height(nx, ny, c_height);
1223-
// float diff = float(nh) - float(c_height);
1224-
// if (diff > best_diff) {
1225-
// best_diff = diff;
1226-
// best_dir = SOUTH;
1227-
// }
1228-
// }
1229-
// {
1230-
// int nx = x + 0, ny = y + 1;
1231-
// int nh = safe_height(nx, ny, c_height);
1232-
// float diff = float(nh) - float(c_height);
1233-
// if (diff > best_diff) {
1234-
// best_diff = diff;
1235-
// best_dir = NORTH;
1236-
// }
1237-
// }
1238-
// {
1239-
// int nx = x + 1, ny = y + 0;
1240-
// int nh = safe_height(nx, ny, c_height);
1241-
// float diff = float(nh) - float(c_height);
1242-
// if (diff > best_diff) {
1243-
// best_diff = diff;
1244-
// best_dir = WEST;
1245-
// }
1246-
// }
1247-
// {
1248-
// int nx = x - 1, ny = y + 0;
1249-
// int nh = safe_height(nx, ny, c_height);
1250-
// float diff = float(nh) - float(c_height);
1251-
// if (diff > best_diff) {
1252-
// best_diff = diff;
1253-
// best_dir = EAST;
1254-
// }
1255-
// }
1256-
// }
1257-
1258-
// myGridMap->set_cell_item(Vector3i(x, c_height, y), tile_id, best_dir);
1259-
// }
1260-
// }
1261-
12621072
//
12631073
//
12641074
//
@@ -1329,15 +1139,6 @@ Dictionary TerrainGen::generate(
13291139
int nwHeight = safe_height(x - 1, y + 1, c_height);
13301140
int nwTile = safe_tile_at(x - 1, y + 1); // m1
13311141

1332-
// +----+----+ +----+----+
1333-
// | n1 | n2 | | g1 | r2 |
1334-
// +----+----+ +----+----+
1335-
// | n3 | n4 | | r2 | g2 |
1336-
// +----+----+ +----+----+
1337-
//
1338-
// ? : Above is an edge case that needs fixed
1339-
// TODO : Replace the Ramps in this situation with CLiff edges using the orientation of the ramp's
1340-
13411142
if (tile_id == RAMP || tile_id == CLIFF || tile_id == WATER_EDGE) {
13421143
// +----+----+----+
13431144
// | m1 | m2 | m3 |
@@ -1510,6 +1311,41 @@ Dictionary TerrainGen::generate(
15101311
}
15111312

15121313
myGridMap->set_cell_item(Vector3i(x, c_height, y), tile_id, rotation_val);
1314+
1315+
// ? : Below is 2 Edge case's that need fixed
1316+
// TODO : Fix these Edge Case's
1317+
1318+
// EDGE CASE : Higher Ground has to connect to Cliffs
1319+
// nwTile = m1 (n1) | sTile = m2 (n2) | wTile = m4 (n3) | T = tile_id (n4)
1320+
//
1321+
// +----+----+ +----+----+ +----+----+ +----+----+
1322+
// | n1 | n2 | | g1 | r2 | | r2 | g1 | | g1 | c2 |
1323+
// +----+----+ +----+----+ +----+----+ -> +----+----+
1324+
// | n3 | n4 | | r2 | g2 | | g2 | r2 | | c2 | g2 |
1325+
// +----+----+ +----+----+ +----+----+ +----+----+
1326+
//
1327+
if ((nwTile == GROUND && sTile == RAMP && wTile == RAMP && tile_id == GROUND)) {
1328+
// Get Orientation (Rotation) Value
1329+
int n1O = myGridMap->get_cell_item_orientation(Vector3i(x - 1, nwHeight, y - 1)); // m1
1330+
// int n2O = myGridMap->get_cell_item_orientation(Vector3i(x, sHeight, y - 1)); // m2
1331+
// int n3O = myGridMap->get_cell_item_orientation(Vector3i(x - 1, wHeight, y)); // m4
1332+
int n4O = myGridMap->get_cell_item_orientation(Vector3i(x, c_height, y)); // Target
1333+
1334+
// Set Ramps to Cliffs
1335+
myGridMap->set_cell_item(Vector3i(x - 1, nwHeight, y - 1), CLIFF, n1O); // n1
1336+
// myGridMap->set_cell_item(Vector3i(x, sHeight, y - 1), CLIFF, n2O); // n2
1337+
// myGridMap->set_cell_item(Vector3i(x - 1, wHeight, y), CLIFF, n3O); // n3
1338+
myGridMap->set_cell_item(Vector3i(x, c_height, y), CLIFF, n4O); // n4
1339+
}
1340+
1341+
// EDGE CASE : Higher Ground has to connect to Cliffs
1342+
//
1343+
// +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+
1344+
// | n1 | n2 | | g1 | c2 | | r2 | g1 | | g2 | r2 | | c2 | g2 | | g1 | c2 |
1345+
// +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ -> +----+----+
1346+
// | n3 | n4 | | r2 | g2 | | g2 | c2 | | c2 | g1 | | g1 | r2 | | r2 | c2 |
1347+
// +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+
1348+
//
15131349
}
15141350
}
15151351

@@ -1619,10 +1455,6 @@ Dictionary TerrainGen::generate(
16191455
16201456
*****************************************************/
16211457

1622-
//TODO : Return data needed for object placement's
1623-
//TODO : Return flatZones/openArea's
1624-
//TODO : Return poisson disk sampling results
1625-
16261458
Dictionary result;
16271459

16281460
// Convert poissonSamples → Array<Vector3i>

0 commit comments

Comments
 (0)