Skip to content

Commit

Permalink
Rework rebaking, #74
Browse files Browse the repository at this point in the history
 - hide rebaking behind disabled feature flag for now as there are
   still few issues to be addressed
 - introduce 'StaticMovementObstacle' trait that handles rebaking
 - make rebaking deferred to bundle calls within the same frame
 - use cylindrical static colliders to avoid GPU fetches (which is
   the case in case of meshes)
  • Loading branch information
Scony committed Jan 14, 2024
1 parent b9ea9b5 commit 166bd2a
Show file tree
Hide file tree
Showing 21 changed files with 138 additions and 117 deletions.
3 changes: 3 additions & 0 deletions source/FeatureFlags.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ extends Node
@export_group("Game")
@export var show_logos_on_startup = true
@export var save_user_files_in_tmp = false

@export_group("Match")
@export var allow_resources_deficit_spending = false
@export var handle_match_end = true
@export var show_minimap = true
@export var allow_navigation_rebaking = true

@export_group("Match/Debug")
@export var frame_incrementer = false
@export var god_mode = false
1 change: 1 addition & 0 deletions source/FeatureFlags.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

[node name="FeatureFlags" type="Node"]
script = ExtResource("1_wihie")
allow_navigation_rebaking = false
god_mode = true
15 changes: 0 additions & 15 deletions source/match/Match.gd
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,13 @@ 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()
visible_player = get_tree().get_nodes_in_group("players")[settings.visible_player]
_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):
Expand Down Expand Up @@ -150,8 +146,6 @@ 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)


Expand Down Expand Up @@ -208,12 +202,3 @@ 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():
navigation.update_terrain()
3 changes: 1 addition & 2 deletions source/match/Match.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ 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 = 0.6
agent_radius = 1.0
agent_max_climb = 0.0

[sub_resource type="ViewportTexture" id="ViewportTexture_vu2gm"]
Expand Down
9 changes: 7 additions & 2 deletions source/match/MatchConstants.gd
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ const MAPS = {
class Navigation:
enum Domain { AIR, TERRAIN }

const DOMAIN_TO_GROUP_MAPPING = {
Domain.AIR: "air_navigation_input",
Domain.TERRAIN: "terrain_navigation_input",
}


class Air:
const Y = 1.5
Expand All @@ -36,9 +41,9 @@ class Terrain:
const PLANE = Plane(Vector3.UP, 0)

class Navmesh:
const CELL_SIZE = 0.2
const CELL_SIZE = 0.25
const CELL_HEIGHT = 0.25
const MAX_AGENT_RADIUS = 0.6 # max radius of movable units
const MAX_AGENT_RADIUS = 1.0 # max radius of movable units


class Resources:
Expand Down
2 changes: 1 addition & 1 deletion source/match/MatchSignals.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extends Node
signal deselect_all_units
signal setup_and_spawn_unit(unit, transform, player)
signal place_structure(structure_prototype)
signal schedule_navigation_rebake(domain)

# notifications
signal terrain_targeted(position)
Expand All @@ -12,4 +13,3 @@ signal unit_targeted(unit)
signal unit_selected(unit)
signal unit_deselected(unit)
signal unit_died(unit)
signal resource_depleted
12 changes: 1 addition & 11 deletions source/match/Navigation.gd
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,12 @@ 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(false)
for obstacle in obstacles:
obstacle.add_to_group("terrain_navigation_input")
terrain.rebake()
_clear_static_obstacles()
_setup_static_obstacles()


func update_terrain():
terrain.rebake(true)


func _clear_static_obstacles():
for static_obstacle in _static_obstacles:
NavigationServer3D.free_rid(static_obstacle)
Expand Down
33 changes: 31 additions & 2 deletions source/match/TerrainNavigation.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
extends Node3D

const DOMAIN = Constants.Match.Navigation.Domain.TERRAIN

var _earliest_frame_to_perform_next_rebake = null
var _is_baking = false

@onready var navigation_map_rid = get_world_3d().navigation_map

@onready var _navigation_region = find_child("NavigationRegion3D")
Expand All @@ -14,10 +19,23 @@ func _ready():
navigation_map_rid, Constants.Match.Terrain.Navmesh.CELL_HEIGHT
)
NavigationServer3D.map_force_update(navigation_map_rid)
MatchSignals.schedule_navigation_rebake.connect(_on_schedule_navigation_rebake)
_navigation_region.bake_finished.connect(_on_bake_finished)


func _process(_delta):
if (
not _is_baking
and _earliest_frame_to_perform_next_rebake != null
and get_tree().get_frame() >= _earliest_frame_to_perform_next_rebake
):
_is_baking = true
_earliest_frame_to_perform_next_rebake = null
_navigation_region.bake_navigation_mesh(true)


func rebake(on_thread: bool):
_navigation_region.bake_navigation_mesh(on_thread)
func rebake():
_navigation_region.bake_navigation_mesh(false)


func _safety_checks():
Expand All @@ -42,3 +60,14 @@ func _safety_checks():
"Navmesh 'cell_height' must match established constant"
)
return true


func _on_schedule_navigation_rebake(domain):
if domain != DOMAIN or not is_inside_tree() or not FeatureFlags.allow_navigation_rebaking:
return
if _earliest_frame_to_perform_next_rebake == null:
_earliest_frame_to_perform_next_rebake = get_tree().get_frame() + 1


func _on_bake_finished():
_is_baking = false
17 changes: 8 additions & 9 deletions source/match/units/AircraftFactory.tscn
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[gd_scene load_steps=12 format=3 uid="uid://cxilu6668nda6"]
[gd_scene load_steps=13 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"]
[ext_resource type="PackedScene" uid="uid://3c1h14nqdumt" path="res://source/match/units/traits/Selection.tscn" id="2_56ml3"]
[ext_resource type="PackedScene" uid="uid://bklfapayb1ryk" path="res://source/match/units/structure-geometries/AircraftFactory.tscn" id="2_mlisu"]
[ext_resource type="PackedScene" uid="uid://d4cwip5hpxlmo" path="res://source/match/units/traits/MovementObstacle.tscn" id="3_j1buj"]
[ext_resource type="PackedScene" uid="uid://c3ssj2p6voauk" path="res://source/match/units/traits/HealthBar.tscn" id="4_igtfr"]
[ext_resource type="PackedScene" uid="uid://c471ahpwuxv57" path="res://source/match/units/traits/StaticMovementObstacle.tscn" id="6_rvkkl"]
[ext_resource type="PackedScene" uid="uid://b8jwlpdvxgrr6" path="res://source/match/units/traits/RallyPoint.tscn" id="8_opfgx"]
[ext_resource type="PackedScene" uid="uid://cd3v6508vjcu3" path="res://source/match/units/traits/ProductionQueue.tscn" id="9_c3qd5"]
[ext_resource type="PackedScene" uid="uid://d4cm4yhtf11ur" path="res://source/match/units/traits/Targetability.tscn" id="9_qqlap"]
Expand All @@ -14,8 +15,9 @@
height = 1.0
radius = 1.5

[sub_resource type="BoxMesh" id="BoxMesh_oki0p"]
size = Vector3(2, 1, 2)
[sub_resource type="CylinderShape3D" id="CylinderShape3D_ea0ur"]
height = 1.0
radius = 1.5

[node name="AircraftFactory" type="Area3D"]
collision_layer = 2
Expand All @@ -40,6 +42,9 @@ radius = 1.5
radius = 1.5
path_height_offset = 0.5

[node name="StaticMovementObstacle" parent="." instance=ExtResource("6_rvkkl")]
shape = SubResource("CylinderShape3D_ea0ur")

[node name="HealthBar" parent="." instance=ExtResource("4_igtfr")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0)
size = Vector2(250, 10)
Expand All @@ -52,10 +57,4 @@ 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"]
17 changes: 7 additions & 10 deletions source/match/units/AntiAirTurret.tscn
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
[gd_scene load_steps=11 format=3 uid="uid://cfa8cpnpk5pcb"]
[gd_scene load_steps=12 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"]
[ext_resource type="PackedScene" uid="uid://cm3iqdsdeo625" path="res://source/match/units/structure-geometries/AntiAirTurret.tscn" id="2_bhouy"]
[ext_resource type="PackedScene" uid="uid://3c1h14nqdumt" path="res://source/match/units/traits/Selection.tscn" id="2_ookx0"]
[ext_resource type="PackedScene" uid="uid://d4cwip5hpxlmo" path="res://source/match/units/traits/MovementObstacle.tscn" id="3_hgb44"]
[ext_resource type="PackedScene" uid="uid://c3ssj2p6voauk" path="res://source/match/units/traits/HealthBar.tscn" id="4_qhoj1"]
[ext_resource type="PackedScene" uid="uid://c471ahpwuxv57" path="res://source/match/units/traits/StaticMovementObstacle.tscn" id="6_tt140"]
[ext_resource type="PackedScene" uid="uid://d4cm4yhtf11ur" path="res://source/match/units/traits/Targetability.tscn" id="7_6bghh"]
[ext_resource type="PackedScene" uid="uid://cd66t0u7kf84j" path="res://source/match/units/traits/RotateRandomlyWhenLookingForTargetsIdle.tscn" id="8_hqxqv"]

[sub_resource type="CylinderShape3D" id="CylinderShape3D_xa2u4"]
height = 0.8
radius = 0.6

[sub_resource type="CylinderMesh" id="CylinderMesh_v80fm"]
top_radius = 0.6
bottom_radius = 0.6
[sub_resource type="CylinderShape3D" id="CylinderShape3D_65kle"]
height = 0.8
radius = 0.6

[node name="AntiAirTurret" type="Area3D"]
collision_layer = 2
Expand Down Expand Up @@ -62,6 +62,9 @@ radius = 0.6
radius = 0.6
path_height_offset = 0.5

[node name="StaticMovementObstacle" parent="." instance=ExtResource("6_tt140")]
shape = SubResource("CylinderShape3D_65kle")

[node name="HealthBar" parent="." instance=ExtResource("4_qhoj1")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.1, 0)
size = Vector2(110, 10)
Expand All @@ -73,11 +76,5 @@ 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"]
17 changes: 7 additions & 10 deletions source/match/units/AntiGroundTurret.tscn
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
[gd_scene load_steps=11 format=3 uid="uid://b8pckq1xn44ss"]
[gd_scene load_steps=12 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"]
[ext_resource type="PackedScene" uid="uid://3c1h14nqdumt" path="res://source/match/units/traits/Selection.tscn" id="2_f1d12"]
[ext_resource type="PackedScene" uid="uid://bmiqncjpgsw2e" path="res://source/match/units/structure-geometries/AntiGroundTurret.tscn" id="2_tlome"]
[ext_resource type="PackedScene" uid="uid://d4cwip5hpxlmo" path="res://source/match/units/traits/MovementObstacle.tscn" id="3_ds80w"]
[ext_resource type="PackedScene" uid="uid://c3ssj2p6voauk" path="res://source/match/units/traits/HealthBar.tscn" id="4_r6emo"]
[ext_resource type="PackedScene" uid="uid://c471ahpwuxv57" path="res://source/match/units/traits/StaticMovementObstacle.tscn" id="6_w6nbo"]
[ext_resource type="PackedScene" uid="uid://d4cm4yhtf11ur" path="res://source/match/units/traits/Targetability.tscn" id="7_4hifs"]
[ext_resource type="PackedScene" uid="uid://cd66t0u7kf84j" path="res://source/match/units/traits/RotateRandomlyWhenLookingForTargetsIdle.tscn" id="8_l7mop"]

[sub_resource type="CylinderShape3D" id="CylinderShape3D_7usmv"]
height = 0.8
radius = 0.6

[sub_resource type="CylinderMesh" id="CylinderMesh_ymrt0"]
top_radius = 0.6
bottom_radius = 0.6
[sub_resource type="CylinderShape3D" id="CylinderShape3D_t2frt"]
height = 0.8
radius = 0.6

[node name="AntiGroundTurret" type="Area3D"]
collision_layer = 2
Expand Down Expand Up @@ -56,6 +56,9 @@ radius = 0.6
radius = 0.6
path_height_offset = 0.5

[node name="StaticMovementObstacle" parent="." instance=ExtResource("6_w6nbo")]
shape = SubResource("CylinderShape3D_t2frt")

[node name="HealthBar" parent="." instance=ExtResource("4_r6emo")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.1, 0)
size = Vector2(110, 10)
Expand All @@ -67,11 +70,5 @@ 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"]
16 changes: 7 additions & 9 deletions source/match/units/CommandCenter.tscn
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[gd_scene load_steps=12 format=3 uid="uid://ct0ejt0trkhrf"]
[gd_scene load_steps=13 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"]
[ext_resource type="PackedScene" uid="uid://bxx81l3t16rrs" path="res://source/match/units/structure-geometries/CommandCenter.tscn" id="2_hwq4d"]
[ext_resource type="PackedScene" uid="uid://3c1h14nqdumt" path="res://source/match/units/traits/Selection.tscn" id="3_slmue"]
[ext_resource type="PackedScene" uid="uid://d4cwip5hpxlmo" path="res://source/match/units/traits/MovementObstacle.tscn" id="4_f5u8t"]
[ext_resource type="PackedScene" uid="uid://c3ssj2p6voauk" path="res://source/match/units/traits/HealthBar.tscn" id="5_xuulo"]
[ext_resource type="PackedScene" uid="uid://c471ahpwuxv57" path="res://source/match/units/traits/StaticMovementObstacle.tscn" id="6_5gbe5"]
[ext_resource type="PackedScene" uid="uid://b8jwlpdvxgrr6" path="res://source/match/units/traits/RallyPoint.tscn" id="8_8qw5y"]
[ext_resource type="PackedScene" uid="uid://cd3v6508vjcu3" path="res://source/match/units/traits/ProductionQueue.tscn" id="9_y6bg8"]
[ext_resource type="PackedScene" uid="uid://d4cm4yhtf11ur" path="res://source/match/units/traits/Targetability.tscn" id="17_dco2r"]
Expand All @@ -14,10 +15,9 @@
height = 0.8
radius = 1.8

[sub_resource type="CylinderMesh" id="CylinderMesh_tf5as"]
top_radius = 1.8
bottom_radius = 1.8
[sub_resource type="CylinderShape3D" id="CylinderShape3D_2xy8f"]
height = 0.8
radius = 1.8

[node name="CommandCenter" type="Area3D"]
collision_layer = 2
Expand All @@ -42,6 +42,9 @@ radius = 2.0
radius = 2.0
path_height_offset = 0.5

[node name="StaticMovementObstacle" parent="." instance=ExtResource("6_5gbe5")]
shape = SubResource("CylinderShape3D_2xy8f")

[node name="HealthBar" parent="." instance=ExtResource("5_xuulo")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0)
size = Vector2(380, 10)
Expand All @@ -54,9 +57,4 @@ 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"]
4 changes: 2 additions & 2 deletions source/match/units/Tank.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

[sub_resource type="CylinderShape3D" id="CylinderShape3D_sjc11"]
height = 0.6
radius = 0.6
radius = 0.8

[node name="Tank" type="Area3D"]
collision_layer = 2
Expand Down Expand Up @@ -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 = 0.6
radius = 1.0
neighbor_distance = 8.0
max_neighbors = 40
time_horizon_agents = 3.0
Expand Down
Loading

0 comments on commit 166bd2a

Please sign in to comment.