Skip to content

Commit f7d2afb

Browse files
committed
Add fix for floating grounds, begin rework of Cardinals
1 parent 1677b43 commit f7d2afb

File tree

3 files changed

+196
-35
lines changed

3 files changed

+196
-35
lines changed

demo/TerrainTiles/Grid_Meshes_Loader.tscn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ visible = false
3535
visible = false
3636

3737
[node name="Cliffs_Corner" parent="." instance=ExtResource("8_2ix0w")]
38-
visible = false
3938

4039
[node name="Cliff_Corner_Inner" parent="." instance=ExtResource("9_vsn3x")]
4140
visible = false

demo/Test.tscn

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
[gd_scene load_steps=5 format=3 uid="uid://cs6xj3i70yh0f"]
1+
[gd_scene load_steps=10 format=3 uid="uid://cs6xj3i70yh0f"]
22

33
[ext_resource type="Script" uid="uid://ba0phkf16vg6l" path="res://terrain_gen_script.gd" id="1_myinc"]
44
[ext_resource type="MeshLibrary" uid="uid://bubhdqje2s6rn" path="res://TerrainTiles/3D_Terrain_Tiles.tres" id="2_njov2"]
55
[ext_resource type="Script" uid="uid://txvq7hrqtxyd" path="res://camera_3d.gd" id="3_2au82"]
66

77
[sub_resource type="Environment" id="Environment_njov2"]
88

9+
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_2au82"]
10+
albedo_color = Color(0.633333, 0, 1, 1)
11+
12+
[sub_resource type="BoxMesh" id="BoxMesh_8tn2x"]
13+
14+
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_aq4fw"]
15+
albedo_color = Color(1, 0, 0.0166664, 1)
16+
17+
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_ivycn"]
18+
albedo_color = Color(0, 1, 0.183333, 1)
19+
20+
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_q1uvj"]
21+
albedo_color = Color(0, 0.3, 1, 1)
22+
923
[node name="Test" type="Node3D"]
1024

1125
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
12-
transform = Transform3D(1, 0, 0, 0, 0.00963406, 0.999954, 0, -0.999954, 0.00963406, 0, 2.1677, 0)
26+
transform = Transform3D(1, 0, 0, 0, 0.00963406, 0.999954, 0, -0.999954, 0.00963406, 46.8027, 2.1677, 53.3998)
1327

1428
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
1529
environment = SubResource("Environment_njov2")
@@ -28,3 +42,23 @@ metadata/_editor_floor_ = Vector3(0, 0, 0)
2842
[node name="Camera3D" type="Camera3D" parent="."]
2943
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 22.3218, 36.7455)
3044
script = ExtResource("3_2au82")
45+
46+
[node name="-X" type="MeshInstance3D" parent="."]
47+
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 42.3511, 5.07252, 53.3998)
48+
material_override = SubResource("StandardMaterial3D_2au82")
49+
mesh = SubResource("BoxMesh_8tn2x")
50+
51+
[node name="X" type="MeshInstance3D" parent="."]
52+
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 50.372, 5.07252, 53.3998)
53+
material_override = SubResource("StandardMaterial3D_aq4fw")
54+
mesh = SubResource("BoxMesh_8tn2x")
55+
56+
[node name="-Z" type="MeshInstance3D" parent="."]
57+
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 45.9526, 5.07252, 48.5286)
58+
material_override = SubResource("StandardMaterial3D_ivycn")
59+
mesh = SubResource("BoxMesh_8tn2x")
60+
61+
[node name="Z" type="MeshInstance3D" parent="."]
62+
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 45.9526, 5.07252, 56.6065)
63+
material_override = SubResource("StandardMaterial3D_q1uvj")
64+
mesh = SubResource("BoxMesh_8tn2x")

src/TerrainGen/Terrain_Gen.cpp

Lines changed: 160 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,8 +1115,8 @@ Dictionary TerrainGen::generate(
11151115

11161116
for (int x = 0; x < width; x++) {
11171117
for (int y = 0; y < height; y++) {
1118-
int c_height = elevationMap[x][y];
1119-
int tile_id = myGridMap->get_cell_item(Vector3i(x, c_height, y));
1118+
int t_height = elevationMap[x][y];
1119+
int tile_id = myGridMap->get_cell_item(Vector3i(x, t_height, y));
11201120
if (tile_id == -1 || tile_id == GROUND)
11211121
continue;
11221122

@@ -1141,44 +1141,75 @@ Dictionary TerrainGen::generate(
11411141

11421142
//
11431143
// +--------------------+---------------+--------------------+
1144-
// | SW (x - 1 , y - 1) | S (x , y - 1) | SE (x + 1, y - 1) |
1144+
// | NE (x - 1 , y - 1) | E (x , y - 1) | SE (x + 1, y - 1) |
11451145
// +--------------------+---------------+--------------------+
1146-
// | W (x - 1 , y) | T (x,y) | E (x + 1) , y |
1146+
// | N (x - 1 , y) | T (x,y) | S (x + 1) , y |
11471147
// +--------------------+---------------+--------------------+
1148-
// | NW (x - 1 , y + 1) | N (x , y + 1) | NE (x + 1 , y + 1) |
1148+
// | NW (x - 1 , y + 1) | W (x , y + 1) | SW (x + 1 , y + 1) |
11491149
// +--------------------+---------------+--------------------+
11501150
//
1151+
// +----+----+----+ +----+----+----+
1152+
// | m1 | m2 | m3 | | NE | E | SE |
1153+
// +----+----+----+ +----+----+----+
1154+
// | m4 | T | m5 | | N | T | S |
1155+
// +----+----+----+ +----+----+----+
1156+
// | m6 | m7 | m8 | | NW | W | SW |
1157+
// +----+----+----+ +----+----+----+
11511158
//
11521159

11531160
//
11541161
// Cardinal Neighbors
11551162
//
11561163
//
1157-
int sHeight = safe_height(x, y - 1, c_height);
1158-
int sTile = safe_tile_at(x, y - 1); // m2
1164+
// int nHeight = safe_height(x, y + 1, t_height);
1165+
// int nTile = safe_tile_at(x, y + 1); // m7
1166+
1167+
// int eHeight = safe_height(x + 1, y, t_height);
1168+
// int eTile = safe_tile_at(x + 1, y); // m5
1169+
1170+
// int sHeight = safe_height(x, y - 1, t_height);
1171+
// int sTile = safe_tile_at(x, y - 1); // m2
1172+
1173+
// int wHeight = safe_height(x - 1, y, t_height);
1174+
// int wTile = safe_tile_at(x - 1, y); // m4
11591175

1160-
int nHeight = safe_height(x, y + 1, c_height);
1176+
int nHeight = safe_height(x, y + 1, t_height);
11611177
int nTile = safe_tile_at(x, y + 1); // m7
11621178

1163-
int eHeight = safe_height(x + 1, y, c_height);
1179+
int eHeight = safe_height(x + 1, y, t_height);
11641180
int eTile = safe_tile_at(x + 1, y); // m5
11651181

1166-
int wHeight = safe_height(x - 1, y, c_height);
1182+
int sHeight = safe_height(x, y - 1, t_height);
1183+
int sTile = safe_tile_at(x, y - 1); // m2
1184+
1185+
int wHeight = safe_height(x - 1, y, t_height);
11671186
int wTile = safe_tile_at(x - 1, y); // m4
11681187

11691188
//
11701189
// Diagonal Neighbors
11711190
//
1172-
int neHeight = safe_height(x + 1, y + 1, c_height);
1191+
// int neHeight = safe_height(x + 1, y + 1, t_height);
1192+
// int neTile = safe_tile_at(x + 1, y + 1); // m3
1193+
1194+
// int seHeight = safe_height(x + 1, y - 1, t_height);
1195+
// int seTile = safe_tile_at(x + 1, y - 1); // m8
1196+
1197+
// int swHeight = safe_height(x - 1, y - 1, t_height);
1198+
// int swTile = safe_tile_at(x - 1, y - 1); // m6
1199+
1200+
// int nwHeight = safe_height(x - 1, y + 1, t_height);
1201+
// int nwTile = safe_tile_at(x - 1, y + 1); // m1
1202+
1203+
int neHeight = safe_height(x + 1, y + 1, t_height);
11731204
int neTile = safe_tile_at(x + 1, y + 1); // m3
11741205

1175-
int seHeight = safe_height(x + 1, y - 1, c_height);
1206+
int seHeight = safe_height(x + 1, y - 1, t_height);
11761207
int seTile = safe_tile_at(x + 1, y - 1); // m8
11771208

1178-
int swHeight = safe_height(x - 1, y - 1, c_height);
1209+
int swHeight = safe_height(x - 1, y - 1, t_height);
11791210
int swTile = safe_tile_at(x - 1, y - 1); // m6
11801211

1181-
int nwHeight = safe_height(x - 1, y + 1, c_height);
1212+
int nwHeight = safe_height(x - 1, y + 1, t_height);
11821213
int nwTile = safe_tile_at(x - 1, y + 1); // m1
11831214

11841215
//
@@ -1354,37 +1385,134 @@ Dictionary TerrainGen::generate(
13541385

13551386
if (tile_id == RAMP_CORNER) {
13561387
// Place Ground under Ramp Tiles
1357-
myGridMap->set_cell_item(Vector3i(x, c_height - 1, y), GROUND, rotation_val);
1388+
myGridMap->set_cell_item(Vector3i(x, t_height - 1, y), GROUND, rotation_val);
13581389
}
13591390

1360-
myGridMap->set_cell_item(Vector3i(x, c_height, y), tile_id, rotation_val);
1391+
myGridMap->set_cell_item(Vector3i(x, t_height, y), tile_id, rotation_val);
13611392

13621393
// ? : Below is 2 Edge case's that need fixed
13631394
// TODO : Fix these Edge Case's
13641395

1365-
// EDGE CASE : Higher Ground has to connect to Cliffs
1366-
// nwTile = m1 (n1) | sTile = m2 (n2) | wTile = m4 (n3) | T = tile_id (n4)
1396+
/*****************************************************
1397+
1398+
EDGE CASE FIX :
1399+
1400+
Floating Ground Tile Fix
1401+
1402+
*****************************************************/
1403+
1404+
// EDGE CASE : Higher Ground has to connect to Cliffs 1
1405+
//
1406+
// +--------------------+---------------+--------------------+
1407+
// | NE (x - 1 , y - 1) | E (x , y - 1) | SE (x + 1, y - 1) |
1408+
// +--------------------+---------------+--------------------+
1409+
// | N (x - 1 , y) | T (x,y) | S (x + 1) , y |
1410+
// +--------------------+---------------+--------------------+
1411+
// | NW (x - 1 , y + 1) | W (x , y + 1) | SW (x + 1 , y + 1) |
1412+
// +--------------------+---------------+--------------------+
13671413
//
1368-
// +----+----+ +----+----+ +----+----+ +----+----+
1369-
// | n1 | n2 | | g1 | r2 | | r2 | g1 | | g1 | c2 |
1370-
// +----+----+ +----+----+ +----+----+ -> +----+----+
1371-
// | n3 | n4 | | r2 | g2 | | g2 | r2 | | c2 | g2 |
1372-
// +----+----+ +----+----+ +----+----+ +----+----+
1414+
// +----+----+----+ +----+----+----+ +----+----+----+
1415+
// | m1 | m2 | m3 | | G | R | X | | R | G | X |
1416+
// +----+----+----+ +----+----+----+ +----+----+----+
1417+
// | m4 | T | m5 | | R | G | X | | G | R | X |
1418+
// +----+----+----+ +----+----+----+ +----+----+----+
1419+
// | m6 | m7 | m8 | | X | X | X | | X | X | X |
1420+
// +----+----+----+ +----+----+----+ +----+----+----+
13731421
//
1374-
if ((nwTile == GROUND && sTile == RAMP && wTile == RAMP && tile_id == GROUND)) {
1422+
1423+
if ((swTile == GROUND && sTile == RAMP && wTile == RAMP && tile_id == GROUND)) {
1424+
// Set Floating Ground to Cliff Corner
1425+
if (swHeight > t_height) {
1426+
myGridMap->set_cell_item(Vector3i(x - 1, swHeight, y - 1), CLIFF_CORNER, WEST); // m1
1427+
} else {
1428+
myGridMap->set_cell_item(Vector3i(x, t_height, y), CLIFF_CORNER, EAST); // T
1429+
}
1430+
13751431
// Get Orientation (Rotation) Value
1376-
int n1O = myGridMap->get_cell_item_orientation(Vector3i(x - 1, nwHeight, y - 1)); // m1
1377-
// int n2O = myGridMap->get_cell_item_orientation(Vector3i(x, sHeight, y - 1)); // m2
1378-
// int n3O = myGridMap->get_cell_item_orientation(Vector3i(x - 1, wHeight, y)); // m4
1379-
int n4O = myGridMap->get_cell_item_orientation(Vector3i(x, c_height, y)); // Target
1432+
int rot1 = myGridMap->get_cell_item_orientation(Vector3i(x, sHeight, y - 1)); // m2
1433+
int rot2 = myGridMap->get_cell_item_orientation(Vector3i(x - 1, wHeight, y)); // m4
13801434

13811435
// Set Ramps to Cliffs
1382-
myGridMap->set_cell_item(Vector3i(x - 1, nwHeight, y - 1), CLIFF, n1O); // n1
1383-
// myGridMap->set_cell_item(Vector3i(x, sHeight, y - 1), CLIFF, n2O); // n2
1384-
// myGridMap->set_cell_item(Vector3i(x - 1, wHeight, y), CLIFF, n3O); // n3
1385-
myGridMap->set_cell_item(Vector3i(x, c_height, y), CLIFF, n4O); // n4
1436+
myGridMap->set_cell_item(Vector3i(x, sHeight, y - 1), CLIFF, rot1); // m2
1437+
myGridMap->set_cell_item(Vector3i(x - 1, wHeight, y), CLIFF, rot2); // m4
13861438
}
13871439

1440+
if ((swTile == RAMP && sTile == GROUND && wTile == GROUND && tile_id == RAMP)) {
1441+
// Set Floating Ground to Cliff Corner
1442+
if (sHeight > wHeight) {
1443+
myGridMap->set_cell_item(Vector3i(x, sHeight, y - 1), CLIFF_CORNER, NORTH); // m3
1444+
} else {
1445+
myGridMap->set_cell_item(Vector3i(x - 1, wHeight, y), CLIFF_CORNER, WEST); // m4
1446+
}
1447+
1448+
// Get Orientation (Rotation) Value
1449+
int rot1 = myGridMap->get_cell_item_orientation(Vector3i(x - 1, swHeight, y - 1)); // m3
1450+
int rot2 = myGridMap->get_cell_item_orientation(Vector3i(x, t_height, y)); // T
1451+
1452+
// Set Ramps to Cliffs
1453+
myGridMap->set_cell_item(Vector3i(x - 1, swHeight, y - 1), CLIFF, rot1); // m3
1454+
myGridMap->set_cell_item(Vector3i(x, t_height, y), CLIFF, rot2); // T
1455+
}
1456+
1457+
// EDGE CASE : Higher Ground has to connect to Cliffs 2
1458+
//
1459+
// +--------------------+---------------+--------------------+
1460+
// | NE (x - 1 , y - 1) | E (x , y - 1) | SE (x + 1, y - 1) |
1461+
// +--------------------+---------------+--------------------+
1462+
// | N (x - 1 , y) | T (x,y) | S (x + 1) , y |
1463+
// +--------------------+---------------+--------------------+
1464+
// | NW (x - 1 , y + 1) | W (x , y + 1) | SW (x + 1 , y + 1) |
1465+
// +--------------------+---------------+--------------------+
1466+
//
1467+
// +----+----+----+ +----+----+----+ +----+----+----+
1468+
// | m1 | m2 | m3 | | X | G | R | | X | R | G |
1469+
// +----+----+----+ +----+----+----+ +----+----+----+
1470+
// | m4 | T | m5 | | X | R | G | | X | G | R |
1471+
// +----+----+----+ +----+----+----+ +----+----+----+
1472+
// | m6 | m7 | m8 | | X | X | X | | X | X | X |
1473+
// +----+----+----+ +----+----+----+ +----+----+----+
1474+
1475+
if ((seTile == RAMP && sTile == GROUND && eTile == GROUND && tile_id == RAMP)) {
1476+
// Set Floating Ground to Cliff Corner
1477+
if (sHeight > eHeight) {
1478+
myGridMap->set_cell_item(Vector3i(x, sHeight, y - 1), CLIFF_CORNER, WEST); // m3
1479+
} else {
1480+
myGridMap->set_cell_item(Vector3i(x + 1, eHeight, y), CLIFF_CORNER, EAST); // T
1481+
}
1482+
1483+
// Get Orientation (Rotation) Value
1484+
int rot1 = myGridMap->get_cell_item_orientation(Vector3i(x + 1, seHeight, y - 1)); // m3
1485+
int rot2 = myGridMap->get_cell_item_orientation(Vector3i(x, t_height, y)); // T
1486+
1487+
// Set Ramps to Cliffs
1488+
myGridMap->set_cell_item(Vector3i(x + 1, seHeight, y - 1), CLIFF, rot1); // m3
1489+
myGridMap->set_cell_item(Vector3i(x, t_height, y), CLIFF, rot2); // T
1490+
}
1491+
1492+
if ((seTile == GROUND && sTile == RAMP && eTile == RAMP && tile_id == GROUND)) {
1493+
// Set Floating Ground to Cliff Corner
1494+
if (seHeight > t_height) {
1495+
myGridMap->set_cell_item(Vector3i(x + 1, seHeight, y - 1), CLIFF_CORNER, NORTH); // m3
1496+
} else {
1497+
myGridMap->set_cell_item(Vector3i(x, t_height, y), CLIFF_CORNER, SOUTH); // T
1498+
}
1499+
1500+
// Set Ramp's to Cliffs
1501+
int rot1 = myGridMap->get_cell_item_orientation(Vector3i(x, sHeight, y - 1)); // m2
1502+
int rot2 = myGridMap->get_cell_item_orientation(Vector3i(x + 1, eHeight, y)); // m5
1503+
1504+
myGridMap->set_cell_item(Vector3i(x, sHeight, y - 1), CLIFF, rot1); // m2
1505+
myGridMap->set_cell_item(Vector3i(x + 1, eHeight, y), CLIFF, rot2); // m5
1506+
}
1507+
1508+
/*****************************************************
1509+
1510+
EDGE CASE FIX :
1511+
1512+
1513+
1514+
*****************************************************/
1515+
13881516
// EDGE CASE : Higher Ground has to connect to Cliffs
13891517
//
13901518
// +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+

0 commit comments

Comments
 (0)