From b7b05d0ad4f52a0d193cf408249cf39336aa7642 Mon Sep 17 00:00:00 2001 From: kbendler Date: Wed, 10 Jan 2024 15:49:14 +0100 Subject: [PATCH] Update Navigation. Buildings and resources are now included in the navigation mesh. This improves unit pathfinding. Also the navigation radius of the tank was lowered to make it more manouverable and close the gap between big and small units since the NavMesh is baked for both of them but orients itself on the bigger units --- source/match/Match.gd | 15 ++++++++++++++- source/match/Match.tscn | 5 +++-- source/match/MatchConstants.gd | 4 ++-- source/match/MatchSignals.gd | 1 + source/match/Navigation.gd | 13 +++++++++++-- source/match/TerrainNavigation.gd | 4 ++-- source/match/units/AircraftFactory.tscn | 11 ++++++++++- source/match/units/AntiAirTurret.tscn | 13 ++++++++++++- source/match/units/AntiGroundTurret.tscn | 13 ++++++++++++- source/match/units/CommandCenter.tscn | 12 +++++++++++- source/match/units/Tank.tscn | 4 ++-- source/match/units/VehicleFactory.tscn | 13 ++++++++++++- source/match/units/non-player/ResourceA.gd | 1 + source/match/units/non-player/ResourceA.tscn | 13 ++++++++++++- source/match/units/non-player/ResourceB.gd | 1 + source/match/units/non-player/ResourceB.tscn | 13 ++++++++++++- 16 files changed, 118 insertions(+), 18 deletions(-) diff --git a/source/match/Match.gd b/source/match/Match.gd index 7f1ec54e..8122c2f4 100644 --- a/source/match/Match.gd +++ b/source/match/Match.gd @@ -35,6 +35,8 @@ func _enter_tree(): func _ready(): MatchSignals.setup_and_spawn_unit.connect(_setup_and_spawn_unit) + MatchSignals.unit_died.connect(_on_unit_died) + MatchSignals.resource_depleted.connect(_on_resource_depleted) _setup_subsystems_dependent_on_map() _setup_players() _setup_player_units() @@ -42,6 +44,8 @@ func _ready(): _move_camera_to_initial_position() if settings.visibility == settings.Visibility.FULL: fog_of_war.reveal() + await get_tree().process_frame + navigation.update_terrain() func _unhandled_input(event): @@ -126,7 +130,6 @@ func _setup_player_units(): player, map.find_child("SpawnPoints").get_child(player_index).global_transform ) - func _spawn_player_units(player, spawn_transform): _setup_and_spawn_unit(CommandCenter.instantiate(), spawn_transform, player, false) _setup_and_spawn_unit( @@ -146,6 +149,8 @@ func _setup_and_spawn_unit(unit, a_transform, player, mark_structure_under_const unit.mark_as_under_construction() _setup_unit_groups(unit, player) player.add_child(unit) + if unit is Structure and mark_structure_under_construction: + navigation.update_terrain() MatchSignals.unit_spawned.emit(unit) @@ -202,3 +207,11 @@ func _conceal_player_units(player): func(a_unit): return a_unit.player == player ): unit.remove_from_group("revealed_units") + +func _on_unit_died(unit): + if unit is Structure: + navigation.update_terrain() + +func _on_resource_depleted(): + print("die") + navigation.update_terrain() diff --git a/source/match/Match.tscn b/source/match/Match.tscn index 54af3f5a..b0dcfe8e 100644 --- a/source/match/Match.tscn +++ b/source/match/Match.tscn @@ -27,7 +27,7 @@ [ext_resource type="PackedScene" uid="uid://bocb7hjilvri5" path="res://source/match/handlers/ArealUnitSelectionHandler.tscn" id="24_aug7m"] [ext_resource type="PackedScene" uid="uid://pi813oou7xim" path="res://source/match/handlers/DoubleClickUnitSelectionHandler.tscn" id="25_ldkhw"] [ext_resource type="PackedScene" uid="uid://yn470qvc3eak" path="res://source/match/handlers/MatchEndHandler.tscn" id="26_4d7im"] -[ext_resource type="PackedScene" path="res://source/match/handlers/UnitGroupSelectionHandler.tscn" id="27_j2drv"] +[ext_resource type="PackedScene" uid="uid://ck6vrgdyg7hja" path="res://source/match/handlers/UnitGroupSelectionHandler.tscn" id="27_j2drv"] [ext_resource type="PackedScene" uid="uid://b8p6lcwubx1tp" path="res://source/match/handlers/UnitVisibilityHandler.tscn" id="32_fci1c"] [sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_ysb0j"] @@ -101,8 +101,9 @@ geometry_parsed_geometry_type = 2 geometry_collision_mask = 4278190082 geometry_source_geometry_mode = 2 geometry_source_group_name = &"terrain_navigation_input" +cell_size = 0.2 agent_height = 2.0 -agent_radius = 1.0 +agent_radius = 0.6 agent_max_climb = 0.0 [sub_resource type="ViewportTexture" id="ViewportTexture_vu2gm"] diff --git a/source/match/MatchConstants.gd b/source/match/MatchConstants.gd index 975a0bee..595423cb 100644 --- a/source/match/MatchConstants.gd +++ b/source/match/MatchConstants.gd @@ -36,9 +36,9 @@ class Terrain: const PLANE = Plane(Vector3.UP, 0) class Navmesh: - const CELL_SIZE = 0.25 + const CELL_SIZE = 0.2 const CELL_HEIGHT = 0.25 - const MAX_AGENT_RADIUS = 1.0 # max radius of movable units + const MAX_AGENT_RADIUS = 0.6 # max radius of movable units class Resources: diff --git a/source/match/MatchSignals.gd b/source/match/MatchSignals.gd index b514608e..fcf66729 100644 --- a/source/match/MatchSignals.gd +++ b/source/match/MatchSignals.gd @@ -12,3 +12,4 @@ signal unit_targeted(unit) signal unit_selected(unit) signal unit_deselected(unit) signal unit_died(unit) +signal resource_depleted() diff --git a/source/match/Navigation.gd b/source/match/Navigation.gd index 69a64a6b..31753387 100644 --- a/source/match/Navigation.gd +++ b/source/match/Navigation.gd @@ -21,11 +21,20 @@ func get_navigation_map_rid_by_domain(domain): func rebake(map): + var obstacles = get_tree().get_nodes_in_group("terrain_navigation_input") + for obstacle in obstacles: + if obstacle.name.to_lower() != "terrain": + obstacle.remove_from_group("terrain_navigation_input") air.rebake(map) - terrain.rebake() + terrain.rebake(false) + for obstacle in obstacles: + obstacle.add_to_group("terrain_navigation_input") _clear_static_obstacles() _setup_static_obstacles() - + +func update_terrain(): + terrain.rebake(true) + print("update_terrain") func _clear_static_obstacles(): for static_obstacle in _static_obstacles: diff --git a/source/match/TerrainNavigation.gd b/source/match/TerrainNavigation.gd index ee0824d0..c78a4842 100644 --- a/source/match/TerrainNavigation.gd +++ b/source/match/TerrainNavigation.gd @@ -16,8 +16,8 @@ func _ready(): NavigationServer3D.map_force_update(navigation_map_rid) -func rebake(): - _navigation_region.bake_navigation_mesh(false) +func rebake(on_thread:bool): + _navigation_region.bake_navigation_mesh(on_thread) func _safety_checks(): diff --git a/source/match/units/AircraftFactory.tscn b/source/match/units/AircraftFactory.tscn index 0c5a19d4..4a933a14 100644 --- a/source/match/units/AircraftFactory.tscn +++ b/source/match/units/AircraftFactory.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=11 format=3 uid="uid://cxilu6668nda6"] +[gd_scene load_steps=12 format=3 uid="uid://cxilu6668nda6"] [ext_resource type="Script" path="res://source/match/units/AircraftFactory.gd" id="1_n6n1v"] [ext_resource type="PackedScene" uid="uid://cgsi062w5fjia" path="res://source/match/units/traits/Highlight.tscn" id="1_r5i7t"] @@ -14,6 +14,9 @@ height = 1.0 radius = 1.5 +[sub_resource type="BoxMesh" id="BoxMesh_oki0p"] +size = Vector3(2, 1, 2) + [node name="AircraftFactory" type="Area3D"] collision_layer = 2 collision_mask = 0 @@ -49,4 +52,10 @@ radius = 1.5 [node name="ProductionQueue" parent="." instance=ExtResource("9_c3qd5")] +[node name="NavMeshObstacle" type="MeshInstance3D" parent="." groups=["terrain_navigation_input"]] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0) +layers = 0 +mesh = SubResource("BoxMesh_oki0p") +skeleton = NodePath("../CollisionShape3D") + [editable path="Geometry"] diff --git a/source/match/units/AntiAirTurret.tscn b/source/match/units/AntiAirTurret.tscn index b8250429..1a350cf8 100644 --- a/source/match/units/AntiAirTurret.tscn +++ b/source/match/units/AntiAirTurret.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=10 format=3 uid="uid://cfa8cpnpk5pcb"] +[gd_scene load_steps=11 format=3 uid="uid://cfa8cpnpk5pcb"] [ext_resource type="Script" path="res://source/match/units/AntiAirTurret.gd" id="1_ec2s5"] [ext_resource type="PackedScene" uid="uid://cgsi062w5fjia" path="res://source/match/units/traits/Highlight.tscn" id="1_tbjvv"] @@ -13,6 +13,11 @@ height = 0.8 radius = 0.6 +[sub_resource type="CylinderMesh" id="CylinderMesh_v80fm"] +top_radius = 0.6 +bottom_radius = 0.6 +height = 0.8 + [node name="AntiAirTurret" type="Area3D"] collision_layer = 2 collision_mask = 0 @@ -68,5 +73,11 @@ radius = 0.6 [node name="RotateRandomlyWhenLookingForTargetsIdle" parent="." instance=ExtResource("8_hqxqv")] node_to_rotate = NodePath("../DetachTransform/Geometry/turret_double/tmpParent/turret_double2/turret") +[node name="NavMeshObstacle" type="MeshInstance3D" parent="." groups=["terrain_navigation_input"]] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.4, 0) +layers = 0 +mesh = SubResource("CylinderMesh_v80fm") +skeleton = NodePath("../CollisionShape3D") + [editable path="DetachTransform/Geometry"] [editable path="DetachTransform/Geometry/turret_double"] diff --git a/source/match/units/AntiGroundTurret.tscn b/source/match/units/AntiGroundTurret.tscn index 4cbd0fb3..caaac03a 100644 --- a/source/match/units/AntiGroundTurret.tscn +++ b/source/match/units/AntiGroundTurret.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=10 format=3 uid="uid://b8pckq1xn44ss"] +[gd_scene load_steps=11 format=3 uid="uid://b8pckq1xn44ss"] [ext_resource type="PackedScene" uid="uid://cgsi062w5fjia" path="res://source/match/units/traits/Highlight.tscn" id="1_5gp2d"] [ext_resource type="Script" path="res://source/match/units/AntiGroundTurret.gd" id="1_6hw4x"] @@ -13,6 +13,11 @@ height = 0.8 radius = 0.6 +[sub_resource type="CylinderMesh" id="CylinderMesh_ymrt0"] +top_radius = 0.6 +bottom_radius = 0.6 +height = 0.8 + [node name="AntiGroundTurret" type="Area3D"] collision_layer = 2 collision_mask = 0 @@ -62,5 +67,11 @@ radius = 0.6 [node name="RotateRandomlyWhenLookingForTargets" parent="." instance=ExtResource("8_l7mop")] node_to_rotate = NodePath("../DetachTransform/Geometry/turret_single/tmpParent/turret_single2/turret") +[node name="NavMeshObstacle" type="MeshInstance3D" parent="." groups=["terrain_navigation_input"]] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.4, 0) +layers = 0 +mesh = SubResource("CylinderMesh_ymrt0") +skeleton = NodePath("../CollisionShape3D") + [editable path="DetachTransform/Geometry"] [editable path="DetachTransform/Geometry/turret_single"] diff --git a/source/match/units/CommandCenter.tscn b/source/match/units/CommandCenter.tscn index 92960c3d..1130658a 100644 --- a/source/match/units/CommandCenter.tscn +++ b/source/match/units/CommandCenter.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=11 format=3 uid="uid://ct0ejt0trkhrf"] +[gd_scene load_steps=12 format=3 uid="uid://ct0ejt0trkhrf"] [ext_resource type="Script" path="res://source/match/units/CommandCenter.gd" id="1_hpo2s"] [ext_resource type="PackedScene" uid="uid://cgsi062w5fjia" path="res://source/match/units/traits/Highlight.tscn" id="2_c41i5"] @@ -14,6 +14,11 @@ height = 0.8 radius = 1.8 +[sub_resource type="CylinderMesh" id="CylinderMesh_tf5as"] +top_radius = 1.8 +bottom_radius = 1.8 +height = 0.8 + [node name="CommandCenter" type="Area3D"] collision_layer = 2 collision_mask = 0 @@ -49,4 +54,9 @@ radius = 2.0 [node name="ProductionQueue" parent="." instance=ExtResource("9_y6bg8")] +[node name="NavMeshObstacle" type="MeshInstance3D" parent="." groups=["terrain_navigation_input"]] +transform = Transform3D(0.707107, 0, 0.707107, 0, 1, 0, -0.707107, 0, 0.707107, 0, 0.4, 0) +layers = 0 +mesh = SubResource("CylinderMesh_tf5as") + [editable path="Geometry"] diff --git a/source/match/units/Tank.tscn b/source/match/units/Tank.tscn index e5f35b1e..a42a4f54 100644 --- a/source/match/units/Tank.tscn +++ b/source/match/units/Tank.tscn @@ -11,7 +11,7 @@ [sub_resource type="CylinderShape3D" id="CylinderShape3D_sjc11"] height = 0.6 -radius = 0.8 +radius = 0.6 [node name="Tank" type="Area3D"] collision_layer = 2 @@ -45,7 +45,7 @@ path_desired_distance = 0.5 target_desired_distance = 0.5 path_height_offset = 0.5 path_max_distance = 0.51 -radius = 1.0 +radius = 0.6 neighbor_distance = 8.0 max_neighbors = 40 time_horizon_agents = 3.0 diff --git a/source/match/units/VehicleFactory.tscn b/source/match/units/VehicleFactory.tscn index 278db3bb..71705c0e 100644 --- a/source/match/units/VehicleFactory.tscn +++ b/source/match/units/VehicleFactory.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=11 format=3 uid="uid://h5lqor1xl2sf"] +[gd_scene load_steps=12 format=3 uid="uid://h5lqor1xl2sf"] [ext_resource type="Script" path="res://source/match/units/VehicleFactory.gd" id="1_1a20j"] [ext_resource type="PackedScene" uid="uid://cgsi062w5fjia" path="res://source/match/units/traits/Highlight.tscn" id="1_8fcgb"] @@ -14,6 +14,11 @@ height = 1.0 radius = 1.5 +[sub_resource type="CylinderMesh" id="CylinderMesh_nlnof"] +top_radius = 1.5 +bottom_radius = 1.5 +height = 1.0 + [node name="VehicleFactory" type="Area3D"] collision_layer = 2 collision_mask = 0 @@ -49,4 +54,10 @@ radius = 1.5 [node name="ProductionQueue" parent="." instance=ExtResource("9_3s532")] +[node name="NavMeshObstacle" type="MeshInstance3D" parent="." groups=["terrain_navigation_input"]] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0) +layers = 0 +mesh = SubResource("CylinderMesh_nlnof") +skeleton = NodePath("../CollisionShape3D") + [editable path="Geometry"] diff --git a/source/match/units/non-player/ResourceA.gd b/source/match/units/non-player/ResourceA.gd index 95643e7e..699b4c37 100644 --- a/source/match/units/non-player/ResourceA.gd +++ b/source/match/units/non-player/ResourceA.gd @@ -7,6 +7,7 @@ const MATERIAL_ALBEDO_TO_REPLACE_EPSILON = 0.05 set(value): resource_a = max(0, value) if resource_a == 0: + tree_exited.connect(func(): MatchSignals.resource_depleted.emit()) queue_free() var color = Constants.Match.Resources.A.COLOR: diff --git a/source/match/units/non-player/ResourceA.tscn b/source/match/units/non-player/ResourceA.tscn index 76e21c06..2f9958af 100644 --- a/source/match/units/non-player/ResourceA.tscn +++ b/source/match/units/non-player/ResourceA.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=8 format=3 uid="uid://bf3jjdafqvh0w"] +[gd_scene load_steps=9 format=3 uid="uid://bf3jjdafqvh0w"] [ext_resource type="Script" path="res://source/match/units/non-player/ResourceA.gd" id="1_3daqx"] [ext_resource type="PackedScene" uid="uid://cgsi062w5fjia" path="res://source/match/units/traits/Highlight.tscn" id="1_d3pi2"] @@ -10,6 +10,11 @@ [sub_resource type="CylinderShape3D" id="CylinderShape3D_msmnm"] height = 0.6 +[sub_resource type="CylinderMesh" id="CylinderMesh_ep7dh"] +top_radius = 0.4 +bottom_radius = 0.4 +height = 1.0 + [node name="ResourceA" type="Area3D" groups=["resource_units"]] script = ExtResource("1_3daqx") @@ -37,3 +42,9 @@ path_height_offset = 0.5 [node name="Targetability" parent="." instance=ExtResource("5_vqxvf")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.05, 0) radius = 0.6 + +[node name="NavMeshObstacle" type="MeshInstance3D" parent="." groups=["terrain_navigation_input"]] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0) +layers = 0 +mesh = SubResource("CylinderMesh_ep7dh") +skeleton = NodePath("../CollisionShape3D") diff --git a/source/match/units/non-player/ResourceB.gd b/source/match/units/non-player/ResourceB.gd index 53a1c455..4f6780d9 100644 --- a/source/match/units/non-player/ResourceB.gd +++ b/source/match/units/non-player/ResourceB.gd @@ -7,6 +7,7 @@ const MATERIAL_ALBEDO_TO_REPLACE_EPSILON = 0.05 set(value): resource_b = max(0, value) if resource_b == 0: + tree_exited.connect(func(): MatchSignals.resource_depleted.emit()) queue_free() var color = Constants.Match.Resources.B.COLOR: diff --git a/source/match/units/non-player/ResourceB.tscn b/source/match/units/non-player/ResourceB.tscn index b17a6ebf..bc553b3d 100644 --- a/source/match/units/non-player/ResourceB.tscn +++ b/source/match/units/non-player/ResourceB.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=8 format=3 uid="uid://bufgjfa3ne3uk"] +[gd_scene load_steps=9 format=3 uid="uid://bufgjfa3ne3uk"] [ext_resource type="Script" path="res://source/match/units/non-player/ResourceB.gd" id="1_57n8v"] [ext_resource type="PackedScene" uid="uid://cgsi062w5fjia" path="res://source/match/units/traits/Highlight.tscn" id="2_c8mwe"] @@ -10,6 +10,11 @@ [sub_resource type="CylinderShape3D" id="CylinderShape3D_msmnm"] height = 0.8 +[sub_resource type="CylinderMesh" id="CylinderMesh_ajrhd"] +top_radius = 0.4 +bottom_radius = 0.4 +height = 1.0 + [node name="ResourceB" type="Area3D" groups=["resource_units"]] script = ExtResource("1_57n8v") @@ -37,3 +42,9 @@ path_height_offset = 0.5 [node name="Targetability" parent="." instance=ExtResource("5_y6273")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.05, 0) radius = 0.6 + +[node name="NavMeshObstacle" type="MeshInstance3D" parent="." groups=["terrain_navigation_input"]] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0) +layers = 0 +mesh = SubResource("CylinderMesh_ajrhd") +skeleton = NodePath("../CollisionShape3D")