Adds basic camp generation and placement

Adds basic camp generation and placement logic to the map generation process.

It attempts to place the camp in a valid location, avoiding paths and water bodies. It also sets the player's spawn point to the center of the generated camp, including some basic camp props like a tent, campfire, and bed.

Additionally, vegetation spawning is now dependent on the `should_spawn_*` methods of the `CellDataResource`, allowing more control over what spawns where.
This commit is contained in:
Dan Baker 2025-06-29 14:05:48 +01:00
parent 3959333534
commit a1efaf6294
8 changed files with 402 additions and 125 deletions

View file

@ -5,13 +5,134 @@ extends Resource
@export var x: int = 0
@export var z: int = 0
@export var vegetation_density: float = 0.5
@export var ground_compaction: float = 0.0
@export var water: float = 0
@export var moisture_level: float = 0.6
# Core cell type properties - these will trigger automatic updates
@export var camp: bool = false: set = set_camp
@export var path: bool = false: set = set_trail
@export var water: bool = false: set = set_water
# Dependent properties that get automatically managed
@export var vegetation_density: float = 0.5: set = set_vegetation_density
@export var ground_compaction: float = 0.0: set = set_ground_compaction
@export var moisture_level: float = 0.6
@export var trees: Array = []
# Internal flag to prevent infinite recursion during initialization
var _initializing: bool = false
func _init():
_initializing = true
# Set default values
vegetation_density = 0.5
ground_compaction = 0.0
camp = false
path = false
water = false
_initializing = false
func set_camp(value: bool):
camp = value
if not _initializing:
_update_dependent_properties()
## Using a different name due to Resource using the function set_path in Godot
func set_trail(value: bool):
path = value
if not _initializing:
_update_dependent_properties()
func set_water(value: bool):
water = value
if not _initializing:
_update_dependent_properties()
func set_vegetation_density(value: float):
# Only allow manual setting if not a special cell type
if _initializing or not (camp or path or water):
vegetation_density = value
else:
vegetation_density = 0.0
func set_ground_compaction(value: float):
# Camp always has maximum compaction, others can be set manually
if camp:
ground_compaction = 1.0
elif not _initializing:
ground_compaction = value
func _update_dependent_properties():
# Update vegetation density - always 0 for camp, path, or water
if camp or path or water:
vegetation_density = 0.0
# Update ground compaction - camp always has maximum compaction
if camp:
ground_compaction = 1.0
# Utility function to set cell type and ensure only one is active
func set_cell_type(cell_type: String):
_initializing = true
# Clear all cell types first
camp = false
path = false
water = false
# Set the specified type
match cell_type.to_lower():
"camp":
camp = true
"path":
path = true
"water":
water = true
"terrain", "normal", "":
pass # Leave all false for normal terrain
_:
push_warning("Unknown cell type: " + cell_type)
_initializing = false
_update_dependent_properties()
# Utility function to get the primary cell type
func get_cell_type() -> String:
if camp:
return "camp"
elif path:
return "path"
elif water:
return "water"
else:
return "terrain"
# Check if this is a special cell type (not normal terrain)
func is_special_cell() -> bool:
return camp or path or water
func add_trees(tree: TreeDataResource, qty: int) -> void:
for i in qty:
trees.append(tree)
for i in qty:
trees.append(tree)
# Override vegetation density and ground compaction for special cases
func force_set_vegetation_density(value: float):
# Allows bypassing the automatic management if absolutely needed
vegetation_density = value
func force_set_ground_compaction(value: float):
# Allows bypassing the automatic management if absolutely needed
ground_compaction = value
func should_spawn_trees() -> bool:
# Only spawn trees if this is not a special cell type
return not (camp or path or water) and vegetation_density > 0.0
func should_spawn_grass() -> bool:
# Grass can spawn in any terrain cell, but not in special cells
return not (camp or path or water) and vegetation_density > 0.0
func should_spawn_bushes() -> bool:
# Bushes can spawn in any terrain cell, but not in special cells
return not (camp or path or water) and vegetation_density > 0.1
func should_spawn_flowers() -> bool:
# Flowers can spawn in any terrain cell, but not in special cells
return not (camp or path or water) and vegetation_density > 0.1

View file

@ -14,41 +14,15 @@ var map_data: Array = Global.map_data
## Density 0.1 to 0.6 - add bushes, varying quantity TBD
## Density 0.1 to 0.4 - add flowers, varying quantity TBD
# Weighted random selection based on tree chances
static func select_weighted_tree(tree_preferences: Dictionary):
if tree_preferences.is_empty():
return null
# Calculate total weight
var total_weight = 0.0
for tree_name in tree_preferences.keys():
total_weight += tree_preferences[tree_name]["chance"]
if total_weight <= 0.0:
return null
# Generate random number between 0 and total_weight
var random_value = randf() * total_weight
# Find which tree this random value corresponds to
var cumulative_weight = 0.0
for tree_name in tree_preferences.keys():
cumulative_weight += tree_preferences[tree_name]["chance"]
if random_value <= cumulative_weight:
return tree_preferences[tree_name]["resource"]
# Fallback (shouldn't happen, but just in case)
var first_key = tree_preferences.keys()[0]
return tree_preferences[first_key]["resource"]
# Pre-calculate tree distribution for the cell
static func generate_cell_with_distribution(x: int, z: int, density: float, path: bool = false, water: bool = false):
static func generate_cell_with_distribution(x: int, z: int, density: float, path: bool = false, water: bool = false, camp: bool = false):
var cell_data = CellDataResource.new()
cell_data.x = x
cell_data.z = z
cell_data.vegetation_density = density
cell_data.camp = camp
if not (path or water):
if not (path or water or camp):
if density >= 0.6:
var tree_preferences = BiomeData.calculate_tree_probabilities(x, z)
var tree_distribution = calculate_tree_distribution(tree_preferences, density)