Much stuff wow
This commit is contained in:
parent
7255cbdf64
commit
734730beee
45 changed files with 697 additions and 119 deletions
|
|
@ -7,14 +7,42 @@ var map_data: Array = Global.map_data
|
|||
## Setup the X and Z
|
||||
## If it's a path or water we do nothing else for now
|
||||
## If it's anything else then we need to:
|
||||
## Set grass density directly from the vegetation density
|
||||
## Then do the following:
|
||||
## Density < 0.5 - chance of spawning special stuff and nothing else
|
||||
## Density > 0.6 - add trees, varying quantity from 0.6 to 1
|
||||
## Density 0.1 to 0.6 - add bushes, varying quantity TBD
|
||||
## Density 0.1 to 0.4 - add flowers, varying quantity TBD
|
||||
## Set grass density directly from the vegetation density
|
||||
## Then do the following:
|
||||
## Density < 0.5 - chance of spawning special stuff and nothing else
|
||||
## Density > 0.6 - add trees, varying quantity from 0.6 to 1
|
||||
## Density 0.1 to 0.6 - add bushes, varying quantity TBD
|
||||
## Density 0.1 to 0.4 - add flowers, varying quantity TBD
|
||||
|
||||
static func generate_cell(x: int, z: int, density: float, path: bool = false, water: bool = false):
|
||||
# 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):
|
||||
var cell_data = CellDataResource.new()
|
||||
cell_data.x = x
|
||||
cell_data.z = z
|
||||
|
|
@ -22,6 +50,51 @@ static func generate_cell(x: int, z: int, density: float, path: bool = false, wa
|
|||
|
||||
if not (path or water):
|
||||
if density >= 0.6:
|
||||
cell_data.add_trees(int(density * 10 / 2))
|
||||
var tree_preferences = BiomeData.calculate_tree_probabilities(x, z)
|
||||
var tree_distribution = calculate_tree_distribution(tree_preferences, density)
|
||||
|
||||
# Add trees based on calculated distribution
|
||||
for tree_type in tree_distribution.keys():
|
||||
var quantity = tree_distribution[tree_type]["quantity"]
|
||||
var resource = tree_distribution[tree_type]["resource"].duplicate()
|
||||
if quantity > 0:
|
||||
cell_data.add_trees(resource, quantity)
|
||||
|
||||
Global.map_data[x][z] = cell_data
|
||||
|
||||
# Calculate how many of each tree type to spawn
|
||||
static func calculate_tree_distribution(tree_preferences: Dictionary, density: float) -> Dictionary:
|
||||
var distribution = {}
|
||||
|
||||
if tree_preferences.is_empty():
|
||||
return distribution
|
||||
|
||||
# Calculate total number of trees for this cell
|
||||
var total_trees = int((density / 2) * 10) + randi_range(1, 3)
|
||||
#var total_trees = int((density - 0.6) * 25) + randi_range(3, 8)
|
||||
|
||||
# Normalize the chances to get proportions
|
||||
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 distribution
|
||||
|
||||
# Distribute trees based on weighted probabilities
|
||||
for tree_name in tree_preferences.keys():
|
||||
var proportion = tree_preferences[tree_name]["chance"] / total_weight
|
||||
var base_quantity = int(total_trees * proportion)
|
||||
|
||||
# Add some randomness - chance to get +1 tree based on remainder
|
||||
var remainder = (total_trees * proportion) - base_quantity
|
||||
if randf() < remainder:
|
||||
base_quantity += 1
|
||||
|
||||
if base_quantity > 0:
|
||||
distribution[tree_name] = {
|
||||
"quantity": base_quantity,
|
||||
"resource": tree_preferences[tree_name]["resource"]
|
||||
}
|
||||
|
||||
return distribution
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue