|
|
|
|
@@ -4,9 +4,20 @@ const CHUNK_TYPE_BIOME: int = 0
|
|
|
|
|
const CHUNK_TYPE_STRAIGHT_TRACK: int = 1
|
|
|
|
|
const CHUNK_TYPE_CURVED_TRACK: int = 2
|
|
|
|
|
|
|
|
|
|
const CHUNK_GENERATION_STEPS_PER_FRAME: int = 2
|
|
|
|
|
const CHUNK_CLEANUP_STEPS_PER_FRAME: int = 4
|
|
|
|
|
const LAMPPOST_WIRE_STEPS_PER_FRAME: int = 1
|
|
|
|
|
const CHUNK_GENERATION_FRAME_BUDGET_USEC: int = 2000 #2 ms/frame to generate chunks
|
|
|
|
|
const CHUNK_CLEANUP_FRAME_BUDGET_USEC: int = 1000 #1 ms/frame per cleanup
|
|
|
|
|
const LAMPPOST_WIRE_FRAME_BUDGET_USEC: int = 1000 #1 ms/frame per i fili dei lampioni
|
|
|
|
|
#(about 4 ms o work for frame)
|
|
|
|
|
#Se compaiono chunk troppo lentamente davanti al treno:
|
|
|
|
|
#aumentare generazione a 3000 o 4000
|
|
|
|
|
#Se ci sono micro-scatti:
|
|
|
|
|
#abbassare generazione a 1000 o 1500
|
|
|
|
|
#Se i fili appaiono in ritardo:
|
|
|
|
|
#aumentare wire a 1500 o 2000
|
|
|
|
|
#Se il cleanup causa scatti:
|
|
|
|
|
#abbassare cleanup a 500
|
|
|
|
|
|
|
|
|
|
const MAX_CHUNK_UNIQUENESS: int = 5
|
|
|
|
|
const RAILWAY_SCENE_DIRECTORY: String = "res://tgcc/chunk/railway/scene"
|
|
|
|
|
const RIVER_SIDE_ORDER: Array[String] = ["north", "est", "south", "west"]
|
|
|
|
|
const RIVER_DIRECTIONS: Dictionary = {
|
|
|
|
|
@@ -23,33 +34,36 @@ const RIVER_NEIGHBOUR_OFFSETS: Dictionary = {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@export_group("Rails")
|
|
|
|
|
@export var rail_path: Path3D
|
|
|
|
|
@export var rail_path: Path3D #rail path
|
|
|
|
|
|
|
|
|
|
@export_group("Biomes")
|
|
|
|
|
@export var biome_list: Array[Biome]
|
|
|
|
|
@export var biome_list: Array[Biome] #list of scenes for the biome
|
|
|
|
|
|
|
|
|
|
@export_group("Grid and Area")
|
|
|
|
|
@export var chunk_size: float = 20.0
|
|
|
|
|
@export var eye_line: int = 3
|
|
|
|
|
@export var district_scale: float = 0.05
|
|
|
|
|
@export var chunk_size: float = 20.0 #size of a cell; default 20 (global position is defined dividing x and z for this value)
|
|
|
|
|
@export var eye_line: int = 3 #cells to be considerated around the train (e.g. 3 => a square of cells from -3 to 3 around train cell)
|
|
|
|
|
@export var district_scale: float = 0.05 #noise value to distribuite biome; low values create large area, high values create biome with more variants
|
|
|
|
|
|
|
|
|
|
@export_group("Lamppost")
|
|
|
|
|
@export var lamppost_wire_material: ShaderMaterial
|
|
|
|
|
@export_range(0.01, 1.0) var wire_thickness: float = 0.05
|
|
|
|
|
@export var lamppost_dist_factor: int = 10
|
|
|
|
|
@export var lamppost_dist_factor: int = 10 #max distance of connections
|
|
|
|
|
|
|
|
|
|
var board: Dictionary = {}
|
|
|
|
|
var last_pos_train: Vector2i = Vector2i(999999, 999999)
|
|
|
|
|
var noise_generator: FastNoiseLite
|
|
|
|
|
var altitude_generator: FastNoiseLite
|
|
|
|
|
var wire_connections: Dictionary = {}
|
|
|
|
|
var chunk_candidate_cache: Dictionary = {}
|
|
|
|
|
var chunk_candidate_cache: Dictionary = {} #node cache (metadata)
|
|
|
|
|
var prop_candidate_cache: Dictionary = {}
|
|
|
|
|
var pending_generation_cells: Array[Vector2i] = []
|
|
|
|
|
var pending_cleanup_cells: Array[Vector2i] = []
|
|
|
|
|
var pending_wire_cells: Array[Vector2i] = []
|
|
|
|
|
var pending_generation_cursor: int = 0
|
|
|
|
|
var pending_cleanup_cursor: int = 0
|
|
|
|
|
var pending_wire_cursor: int = 0
|
|
|
|
|
var pending_wire_lookup: Dictionary = {}
|
|
|
|
|
var pending_radar_update: bool = false
|
|
|
|
|
var rail_chunk_catalogue: Dictionary = {}
|
|
|
|
|
var rail_chunk_catalogue: Dictionary = {} #rails chunk list
|
|
|
|
|
|
|
|
|
|
var manual_biome: Biome = null
|
|
|
|
|
|
|
|
|
|
@@ -59,6 +73,7 @@ func _ready() -> void:
|
|
|
|
|
#connect events
|
|
|
|
|
UIEvents.update_rail_chunks.connect(_update_rail_chunks)
|
|
|
|
|
|
|
|
|
|
#noise generator for biome and altitute
|
|
|
|
|
noise_generator = FastNoiseLite.new()
|
|
|
|
|
noise_generator.noise_type = FastNoiseLite.TYPE_PERLIN
|
|
|
|
|
noise_generator.seed = randi()
|
|
|
|
|
@@ -69,8 +84,10 @@ func _ready() -> void:
|
|
|
|
|
altitude_generator.seed = randi()
|
|
|
|
|
altitude_generator.frequency = district_scale * 0.5
|
|
|
|
|
|
|
|
|
|
#fill cache with available chunk
|
|
|
|
|
_warm_chunk_candidate_cache()
|
|
|
|
|
_warm_rail_chunk_catalogue()
|
|
|
|
|
#set unique pieces
|
|
|
|
|
_update_set_pieces()
|
|
|
|
|
|
|
|
|
|
func _update_set_pieces() -> void:
|
|
|
|
|
@@ -85,7 +102,7 @@ func _update_set_pieces() -> void:
|
|
|
|
|
|
|
|
|
|
var local_biome = ""
|
|
|
|
|
if manual_biome != null:
|
|
|
|
|
local_biome = manual_biome.nome
|
|
|
|
|
local_biome = manual_biome.name
|
|
|
|
|
else:
|
|
|
|
|
var grid_x = roundi(sp.global_position.x / chunk_size)
|
|
|
|
|
var grid_z = roundi(sp.global_position.z / chunk_size)
|
|
|
|
|
@@ -111,7 +128,7 @@ func _destroy_and_regenrate_world() -> void:
|
|
|
|
|
#Destroy all models
|
|
|
|
|
for pos in board.keys():
|
|
|
|
|
var cella = board[pos]
|
|
|
|
|
if cella["type"] != "obstacle":
|
|
|
|
|
if not _is_persistent_obstacle(cella):
|
|
|
|
|
if cella.has("node") and is_instance_valid(cella["node"]):
|
|
|
|
|
cella["node"].queue_free()
|
|
|
|
|
|
|
|
|
|
@@ -125,16 +142,13 @@ func _destroy_and_regenrate_world() -> void:
|
|
|
|
|
var train_pos = rail_path.train_instance.global_position
|
|
|
|
|
var current_pos = Vector2i(roundi(train_pos.x / chunk_size), roundi(train_pos.z / chunk_size))
|
|
|
|
|
_refresh_world(current_pos)
|
|
|
|
|
pending_radar_update = true
|
|
|
|
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
|
|
|
#based on time budgets the queues are evaluated by frame and not all together
|
|
|
|
|
_drain_cleanup_queue()
|
|
|
|
|
_drain_generation_queue()
|
|
|
|
|
_drain_wire_queue()
|
|
|
|
|
|
|
|
|
|
if pending_radar_update:
|
|
|
|
|
pending_radar_update = false
|
|
|
|
|
|
|
|
|
|
func _physics_process(_delta: float) -> void:
|
|
|
|
|
if rail_path == null or rail_path.train_instance == null: return
|
|
|
|
|
|
|
|
|
|
@@ -146,7 +160,6 @@ func _physics_process(_delta: float) -> void:
|
|
|
|
|
if current_pos != last_pos_train:
|
|
|
|
|
last_pos_train = current_pos
|
|
|
|
|
_refresh_world(current_pos)
|
|
|
|
|
pending_radar_update = true
|
|
|
|
|
|
|
|
|
|
func collect_all_chunkinfo(root: Node, list: Array[Node]) -> void:
|
|
|
|
|
if root == null: return
|
|
|
|
|
@@ -157,6 +170,15 @@ func collect_all_chunkinfo(root: Node, list: Array[Node]) -> void:
|
|
|
|
|
for child in root.get_children():
|
|
|
|
|
collect_all_chunkinfo(child, list)
|
|
|
|
|
|
|
|
|
|
func collect_all_propinfo(root: Node, list: Array[Node]) -> void:
|
|
|
|
|
if root == null: return
|
|
|
|
|
|
|
|
|
|
if "available_props" in root:
|
|
|
|
|
list.append(root)
|
|
|
|
|
|
|
|
|
|
for child in root.get_children():
|
|
|
|
|
collect_all_propinfo(child, list)
|
|
|
|
|
|
|
|
|
|
func _warm_chunk_candidate_cache() -> void:
|
|
|
|
|
var unique_scenes: Dictionary = {}
|
|
|
|
|
|
|
|
|
|
@@ -239,6 +261,7 @@ func _get_chunk_scene_metadata(scene: PackedScene) -> Dictionary:
|
|
|
|
|
"info_path": info_path,
|
|
|
|
|
"rotations": rotations,
|
|
|
|
|
"has_lamppost": "have_lamppost" in info_node and info_node.have_lamppost,
|
|
|
|
|
"uniqueness": _get_chunk_uniqueness_from_info(info_node),
|
|
|
|
|
}
|
|
|
|
|
preview_chunk.queue_free()
|
|
|
|
|
chunk_candidate_cache[key] = metadata
|
|
|
|
|
@@ -272,8 +295,10 @@ func _clear_pending_world_work() -> void:
|
|
|
|
|
pending_generation_cells.clear()
|
|
|
|
|
pending_cleanup_cells.clear()
|
|
|
|
|
pending_wire_cells.clear()
|
|
|
|
|
pending_generation_cursor = 0
|
|
|
|
|
pending_cleanup_cursor = 0
|
|
|
|
|
pending_wire_cursor = 0
|
|
|
|
|
pending_wire_lookup.clear()
|
|
|
|
|
pending_radar_update = false
|
|
|
|
|
|
|
|
|
|
func _collect_replaceable_rail_chunks(root: Node, result: Array[Node3D]) -> void:
|
|
|
|
|
if root == null:
|
|
|
|
|
@@ -340,6 +365,8 @@ func _replace_rail_chunk_instance(chunk_root: Node3D) -> bool:
|
|
|
|
|
chunk_root.queue_free()
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
#rebuild the scene catalogue from res://tgcc/chunk/railway/scene,
|
|
|
|
|
#than serach on the current scene which chunks can be changed and for each one chose a new scene (the same kind)
|
|
|
|
|
func _refresh_rail_chunks() -> void:
|
|
|
|
|
print("update rail chunks")
|
|
|
|
|
_warm_rail_chunk_catalogue()
|
|
|
|
|
@@ -367,8 +394,12 @@ func _refresh_world(center: Vector2i) -> void:
|
|
|
|
|
_rebuild_generation_queue(center)
|
|
|
|
|
_rebuild_cleanup_queue(center)
|
|
|
|
|
|
|
|
|
|
#check cells around the train by rings:
|
|
|
|
|
#first the center, then borders with distance = 1, the distance = 2 and so to eye_line.
|
|
|
|
|
#Use maxi(abs(x), abs(z)) to know the border of the ring. Cells closest to the train have more priority
|
|
|
|
|
func _rebuild_generation_queue(center: Vector2i) -> void:
|
|
|
|
|
pending_generation_cells.clear()
|
|
|
|
|
pending_generation_cursor = 0
|
|
|
|
|
|
|
|
|
|
for radius in range(eye_line + 1):
|
|
|
|
|
for x in range(-radius, radius + 1):
|
|
|
|
|
@@ -381,13 +412,17 @@ func _rebuild_generation_queue(center: Vector2i) -> void:
|
|
|
|
|
continue
|
|
|
|
|
pending_generation_cells.append(grid_pos)
|
|
|
|
|
|
|
|
|
|
#For each cells add to cleanup queue the cells too far
|
|
|
|
|
#Use eye_line plus a margin value to hide chunks not instantly when go out of the eye line
|
|
|
|
|
#Persistent obstacle cells are not deleted (rails or set_pieces)
|
|
|
|
|
func _rebuild_cleanup_queue(center: Vector2i) -> void:
|
|
|
|
|
pending_cleanup_cells.clear()
|
|
|
|
|
pending_cleanup_cursor = 0
|
|
|
|
|
var safe_margin = 2
|
|
|
|
|
|
|
|
|
|
for grid_pos in board.keys():
|
|
|
|
|
var cell = board[grid_pos]
|
|
|
|
|
if cell["type"] == "obstacle":
|
|
|
|
|
if _is_persistent_obstacle(cell):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
var dist_x = abs(grid_pos.x - center.x)
|
|
|
|
|
@@ -395,10 +430,16 @@ func _rebuild_cleanup_queue(center: Vector2i) -> void:
|
|
|
|
|
if dist_x > eye_line + safe_margin or dist_z > eye_line + safe_margin:
|
|
|
|
|
pending_cleanup_cells.append(grid_pos)
|
|
|
|
|
|
|
|
|
|
#Check if a queue can continue to work based on time budget setted
|
|
|
|
|
func _queue_has_frame_budget(start_usec: int, budget_usec: int, processed: int) -> bool:
|
|
|
|
|
return processed == 0 or Time.get_ticks_usec() - start_usec < budget_usec
|
|
|
|
|
|
|
|
|
|
func _drain_generation_queue() -> void:
|
|
|
|
|
var processed = 0
|
|
|
|
|
while processed < CHUNK_GENERATION_STEPS_PER_FRAME and not pending_generation_cells.is_empty():
|
|
|
|
|
var grid_pos = pending_generation_cells.pop_front()
|
|
|
|
|
var processed: int = 0
|
|
|
|
|
var start_usec: int = Time.get_ticks_usec()
|
|
|
|
|
while pending_generation_cursor < pending_generation_cells.size() and _queue_has_frame_budget(start_usec, CHUNK_GENERATION_FRAME_BUDGET_USEC, processed):
|
|
|
|
|
var grid_pos: Vector2i = pending_generation_cells[pending_generation_cursor]
|
|
|
|
|
pending_generation_cursor += 1
|
|
|
|
|
if board.has(grid_pos):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
@@ -407,15 +448,21 @@ func _drain_generation_queue() -> void:
|
|
|
|
|
_add_compatible_biome(grid_pos)
|
|
|
|
|
processed += 1
|
|
|
|
|
|
|
|
|
|
if pending_generation_cursor >= pending_generation_cells.size():
|
|
|
|
|
pending_generation_cells.clear()
|
|
|
|
|
pending_generation_cursor = 0
|
|
|
|
|
|
|
|
|
|
func _drain_cleanup_queue() -> void:
|
|
|
|
|
var processed = 0
|
|
|
|
|
while processed < CHUNK_CLEANUP_STEPS_PER_FRAME and not pending_cleanup_cells.is_empty():
|
|
|
|
|
var grid_pos = pending_cleanup_cells.pop_front()
|
|
|
|
|
var processed: int = 0
|
|
|
|
|
var start_usec: int = Time.get_ticks_usec()
|
|
|
|
|
while pending_cleanup_cursor < pending_cleanup_cells.size() and _queue_has_frame_budget(start_usec, CHUNK_CLEANUP_FRAME_BUDGET_USEC, processed):
|
|
|
|
|
var grid_pos: Vector2i = pending_cleanup_cells[pending_cleanup_cursor]
|
|
|
|
|
pending_cleanup_cursor += 1
|
|
|
|
|
if not board.has(grid_pos):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
var cell = board[grid_pos]
|
|
|
|
|
if cell["type"] == "obstacle":
|
|
|
|
|
if _is_persistent_obstacle(cell):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if cell.has("node") and is_instance_valid(cell["node"]):
|
|
|
|
|
@@ -423,6 +470,10 @@ func _drain_cleanup_queue() -> void:
|
|
|
|
|
board.erase(grid_pos)
|
|
|
|
|
processed += 1
|
|
|
|
|
|
|
|
|
|
if pending_cleanup_cursor >= pending_cleanup_cells.size():
|
|
|
|
|
pending_cleanup_cells.clear()
|
|
|
|
|
pending_cleanup_cursor = 0
|
|
|
|
|
|
|
|
|
|
func _queue_lamppost_wire_connection(grid_pos: Vector2i) -> void:
|
|
|
|
|
if pending_wire_lookup.has(grid_pos):
|
|
|
|
|
return
|
|
|
|
|
@@ -430,9 +481,11 @@ func _queue_lamppost_wire_connection(grid_pos: Vector2i) -> void:
|
|
|
|
|
pending_wire_cells.append(grid_pos)
|
|
|
|
|
|
|
|
|
|
func _drain_wire_queue() -> void:
|
|
|
|
|
var processed = 0
|
|
|
|
|
while processed < LAMPPOST_WIRE_STEPS_PER_FRAME and not pending_wire_cells.is_empty():
|
|
|
|
|
var grid_pos = pending_wire_cells.pop_front()
|
|
|
|
|
var processed: int = 0
|
|
|
|
|
var start_usec: int = Time.get_ticks_usec()
|
|
|
|
|
while pending_wire_cursor < pending_wire_cells.size() and _queue_has_frame_budget(start_usec, LAMPPOST_WIRE_FRAME_BUDGET_USEC, processed):
|
|
|
|
|
var grid_pos: Vector2i = pending_wire_cells[pending_wire_cursor]
|
|
|
|
|
pending_wire_cursor += 1
|
|
|
|
|
pending_wire_lookup.erase(grid_pos)
|
|
|
|
|
|
|
|
|
|
if not board.has(grid_pos):
|
|
|
|
|
@@ -443,6 +496,10 @@ func _drain_wire_queue() -> void:
|
|
|
|
|
_connect_lamppost_wires(grid_pos)
|
|
|
|
|
processed += 1
|
|
|
|
|
|
|
|
|
|
if pending_wire_cursor >= pending_wire_cells.size():
|
|
|
|
|
pending_wire_cells.clear()
|
|
|
|
|
pending_wire_cursor = 0
|
|
|
|
|
|
|
|
|
|
func _generate_pieces_around_train(center: Vector2i) -> void:
|
|
|
|
|
for x in range(-eye_line, eye_line + 1):
|
|
|
|
|
for z in range(-eye_line, eye_line + 1):
|
|
|
|
|
@@ -453,6 +510,8 @@ func _generate_pieces_around_train(center: Vector2i) -> void:
|
|
|
|
|
if not ce_obstacle:
|
|
|
|
|
_add_compatible_biome(grid_pos)
|
|
|
|
|
|
|
|
|
|
#Decice which catalogue of chunks use for a cell. If manual_biome is set use always it
|
|
|
|
|
#Otherwise read noise_generator.get_noise_2d to give an index for biome_list
|
|
|
|
|
func _choose_catalogue_by_cell(grid_pos: Vector2i) -> Array[PackedScene]:
|
|
|
|
|
if manual_biome != null:
|
|
|
|
|
return manual_biome.available_chunks
|
|
|
|
|
@@ -470,6 +529,143 @@ func _get_procedural_biome_name(value: float) -> String:
|
|
|
|
|
var index = clamp(int(normalized_value * biome_list.size()), 0, biome_list.size() - 1)
|
|
|
|
|
return biome_list[index].name
|
|
|
|
|
|
|
|
|
|
func _get_chunk_uniqueness_from_info(info_node: Node) -> int:
|
|
|
|
|
if info_node != null and "uniqueness" in info_node:
|
|
|
|
|
return clampi(info_node.uniqueness, 0, MAX_CHUNK_UNIQUENESS)
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
func _get_prop_uniqueness_from_info(info_node: Node) -> int:
|
|
|
|
|
if info_node != null and "uniqueness" in info_node:
|
|
|
|
|
return clampi(info_node.uniqueness, 0, MAX_CHUNK_UNIQUENESS)
|
|
|
|
|
return -1
|
|
|
|
|
|
|
|
|
|
func _is_persistent_obstacle(cell: Dictionary) -> bool:
|
|
|
|
|
return cell.get("type", "") == "obstacle" and cell.get("persistent", true)
|
|
|
|
|
|
|
|
|
|
func _has_nearby_unique_chunk(grid_pos: Vector2i, uniqueness: int) -> bool:
|
|
|
|
|
if uniqueness <= 0:
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
for nearby_pos in board.keys():
|
|
|
|
|
var nearby_uniqueness = int(board[nearby_pos].get("uniqueness", 0))
|
|
|
|
|
if nearby_uniqueness <= 0:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
var min_distance = maxi(uniqueness, nearby_uniqueness)
|
|
|
|
|
var dist_x = abs(nearby_pos.x - grid_pos.x)
|
|
|
|
|
var dist_z = abs(nearby_pos.y - grid_pos.y)
|
|
|
|
|
if dist_x <= min_distance and dist_z <= min_distance:
|
|
|
|
|
return true
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
func _get_uniqueness_pick_weight(uniqueness: int) -> float:
|
|
|
|
|
return 1.0 / pow(float(uniqueness + 1), 2.0)
|
|
|
|
|
|
|
|
|
|
func _pick_weighted_candidate(candidates: Array) -> Dictionary:
|
|
|
|
|
var total_weight = 0.0
|
|
|
|
|
for candidate in candidates:
|
|
|
|
|
total_weight += candidate.weight
|
|
|
|
|
|
|
|
|
|
if total_weight <= 0.0:
|
|
|
|
|
return candidates.pick_random()
|
|
|
|
|
|
|
|
|
|
var target_weight = randf() * total_weight
|
|
|
|
|
var current_weight = 0.0
|
|
|
|
|
for candidate in candidates:
|
|
|
|
|
current_weight += candidate.weight
|
|
|
|
|
if current_weight >= target_weight:
|
|
|
|
|
return candidate
|
|
|
|
|
return candidates.back()
|
|
|
|
|
|
|
|
|
|
func _pick_backup_scene(zone_catalogue: Array[PackedScene], grid_pos: Vector2i) -> PackedScene:
|
|
|
|
|
for scene in zone_catalogue:
|
|
|
|
|
var metadata = _get_chunk_scene_metadata(scene)
|
|
|
|
|
var uniqueness = int(metadata.get("uniqueness", 0))
|
|
|
|
|
if not _has_nearby_unique_chunk(grid_pos, uniqueness):
|
|
|
|
|
return scene
|
|
|
|
|
return zone_catalogue[0]
|
|
|
|
|
|
|
|
|
|
func _get_prop_scene_cache_key(scene: PackedScene) -> String:
|
|
|
|
|
if scene == null:
|
|
|
|
|
return ""
|
|
|
|
|
if scene.resource_path != "":
|
|
|
|
|
return scene.resource_path
|
|
|
|
|
return "prop_scene_%s" % scene.get_instance_id()
|
|
|
|
|
|
|
|
|
|
func _get_prop_scene_uniqueness(scene: PackedScene) -> int:
|
|
|
|
|
if scene == null:
|
|
|
|
|
return -1
|
|
|
|
|
|
|
|
|
|
var key = _get_prop_scene_cache_key(scene)
|
|
|
|
|
if prop_candidate_cache.has(key):
|
|
|
|
|
return prop_candidate_cache[key]
|
|
|
|
|
|
|
|
|
|
var preview_prop = scene.instantiate()
|
|
|
|
|
var uniqueness = _get_prop_uniqueness_from_info(preview_prop)
|
|
|
|
|
if uniqueness == -1:
|
|
|
|
|
var prop_info_list: Array[Node] = []
|
|
|
|
|
collect_all_propinfo(preview_prop, prop_info_list)
|
|
|
|
|
if not prop_info_list.is_empty():
|
|
|
|
|
uniqueness = _get_prop_uniqueness_from_info(prop_info_list[0])
|
|
|
|
|
preview_prop.queue_free()
|
|
|
|
|
|
|
|
|
|
prop_candidate_cache[key] = uniqueness
|
|
|
|
|
return uniqueness
|
|
|
|
|
|
|
|
|
|
func _get_marker_prop_uniqueness(marker: Node) -> int:
|
|
|
|
|
var uniqueness = _get_prop_uniqueness_from_info(marker)
|
|
|
|
|
if uniqueness == -1:
|
|
|
|
|
return 0
|
|
|
|
|
return uniqueness
|
|
|
|
|
|
|
|
|
|
func _pick_weighted_prop_scene(marker: Node) -> PackedScene:
|
|
|
|
|
var candidates = []
|
|
|
|
|
var fallback_uniqueness = _get_marker_prop_uniqueness(marker)
|
|
|
|
|
|
|
|
|
|
for prop_scene in marker.available_props:
|
|
|
|
|
if prop_scene == null:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
var uniqueness = _get_prop_scene_uniqueness(prop_scene)
|
|
|
|
|
if uniqueness == -1:
|
|
|
|
|
uniqueness = fallback_uniqueness
|
|
|
|
|
|
|
|
|
|
candidates.append({
|
|
|
|
|
"scene": prop_scene,
|
|
|
|
|
"weight": _get_uniqueness_pick_weight(uniqueness),
|
|
|
|
|
"uniqueness": uniqueness
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if candidates.is_empty():
|
|
|
|
|
return null
|
|
|
|
|
return _pick_weighted_candidate(candidates).scene
|
|
|
|
|
|
|
|
|
|
func _spawn_props_for_chunk(root: Node) -> void:
|
|
|
|
|
var prop_markers: Array[Node] = []
|
|
|
|
|
collect_all_propinfo(root, prop_markers)
|
|
|
|
|
|
|
|
|
|
for marker in prop_markers:
|
|
|
|
|
_spawn_prop_for_marker(marker)
|
|
|
|
|
|
|
|
|
|
func _spawn_prop_for_marker(marker: Node) -> void:
|
|
|
|
|
if marker.available_props.is_empty():
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
var prop_scene = _pick_weighted_prop_scene(marker)
|
|
|
|
|
if prop_scene == null:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
var prop_instance = prop_scene.instantiate()
|
|
|
|
|
var prop_node = prop_instance as Node3D
|
|
|
|
|
if prop_node == null:
|
|
|
|
|
prop_instance.queue_free()
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
marker.add_child(prop_node)
|
|
|
|
|
prop_node.transform = Transform3D.IDENTITY
|
|
|
|
|
|
|
|
|
|
#Using a vertical raycast from the top to the bottom at the center of the cell
|
|
|
|
|
#If there is a collision search for a new chunk node
|
|
|
|
|
func _register_cell_with_ray(grid_pos: Vector2i) -> bool:
|
|
|
|
|
if board.has(grid_pos): return true
|
|
|
|
|
|
|
|
|
|
@@ -517,6 +713,7 @@ func _register_cell_with_ray(grid_pos: Vector2i) -> bool:
|
|
|
|
|
var river_exit_found = {"north": false, "est": false, "south": false, "west": false}
|
|
|
|
|
var height_found = {"north": 0, "est": 0, "south": 0, "west": 0}
|
|
|
|
|
var have_lamppost = false
|
|
|
|
|
var uniqueness = 0
|
|
|
|
|
|
|
|
|
|
#Get node info
|
|
|
|
|
if right_info_node != null:
|
|
|
|
|
@@ -530,6 +727,8 @@ func _register_cell_with_ray(grid_pos: Vector2i) -> bool:
|
|
|
|
|
if "have_lamppost" in right_info_node:
|
|
|
|
|
have_lamppost = right_info_node.have_lamppost
|
|
|
|
|
|
|
|
|
|
uniqueness = _get_chunk_uniqueness_from_info(right_info_node)
|
|
|
|
|
|
|
|
|
|
#Add piece to the grid
|
|
|
|
|
board[grid_pos] = {
|
|
|
|
|
"type": "obstacle",
|
|
|
|
|
@@ -538,7 +737,9 @@ func _register_cell_with_ray(grid_pos: Vector2i) -> bool:
|
|
|
|
|
"heights": height_found,
|
|
|
|
|
"node": root_chunk,
|
|
|
|
|
"info": right_info_node,
|
|
|
|
|
"have_lamppost": have_lamppost
|
|
|
|
|
"have_lamppost": have_lamppost,
|
|
|
|
|
"uniqueness": uniqueness,
|
|
|
|
|
"persistent": true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if have_lamppost:
|
|
|
|
|
@@ -547,6 +748,11 @@ func _register_cell_with_ray(grid_pos: Vector2i) -> bool:
|
|
|
|
|
return true
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
#For the cell to fill check the contrains for the four neighbors:
|
|
|
|
|
#If the neighbor at north have and exit on south then new chunk should have exit to north (the same for east and ovest)
|
|
|
|
|
#Contrains: 1 -> connection is mandatory; 0 -> no connection; -1 -> no contrain because there is no neighbor or it already exits
|
|
|
|
|
#When generator know the contrains take all chunks and try all different rotations
|
|
|
|
|
#A candidate is valid only if all contrains are correct
|
|
|
|
|
func _add_compatible_biome(grid_pos: Vector2i) -> void:
|
|
|
|
|
var req_conn_north = _needed_connection(grid_pos + Vector2i(0, -1), "south")
|
|
|
|
|
var req_conn_est = _needed_connection(grid_pos + Vector2i(1, 0), "west")
|
|
|
|
|
@@ -579,6 +785,9 @@ func _add_compatible_biome(grid_pos: Vector2i) -> void:
|
|
|
|
|
var u_conn = data["connections"]
|
|
|
|
|
var u_river_conn = data["river_connections"]
|
|
|
|
|
var u_height = data["heights"]
|
|
|
|
|
var uniqueness = int(metadata.get("uniqueness", 0))
|
|
|
|
|
if _has_nearby_unique_chunk(grid_pos, uniqueness):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
var match_conn_n = (req_conn_north == -1) or ((req_conn_north == 1) == u_conn["north"])
|
|
|
|
|
var match_conn_e = (req_conn_est == -1) or ((req_conn_est == 1) == u_conn["est"])
|
|
|
|
|
@@ -607,7 +816,14 @@ func _add_compatible_biome(grid_pos: Vector2i) -> void:
|
|
|
|
|
if req_height_south == -1 and u_height["south"] == height_target: score += 1
|
|
|
|
|
if req_height_west == -1 and u_height["west"] == height_target: score += 1
|
|
|
|
|
|
|
|
|
|
valid_candidates.append({"scene": scene, "rotation": rot, "data": data, "score": score})
|
|
|
|
|
valid_candidates.append({
|
|
|
|
|
"scene": scene,
|
|
|
|
|
"rotation": rot,
|
|
|
|
|
"data": data,
|
|
|
|
|
"score": score,
|
|
|
|
|
"weight": _get_uniqueness_pick_weight(uniqueness),
|
|
|
|
|
"uniqueness": uniqueness
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if valid_candidates.size() > 0:
|
|
|
|
|
var max_score = -1
|
|
|
|
|
@@ -618,11 +834,12 @@ func _add_compatible_biome(grid_pos: Vector2i) -> void:
|
|
|
|
|
for c in valid_candidates:
|
|
|
|
|
if c.score == max_score: best_candidate.append(c)
|
|
|
|
|
|
|
|
|
|
var choise = best_candidate.pick_random()
|
|
|
|
|
var choise = _pick_weighted_candidate(best_candidate)
|
|
|
|
|
var new_chunk = choise.scene.instantiate()
|
|
|
|
|
new_chunk.position = Vector3(grid_pos.x * chunk_size, 0, grid_pos.y * chunk_size)
|
|
|
|
|
new_chunk.rotation.y = choise.rotation * (-PI / 2.0)
|
|
|
|
|
add_child(new_chunk)
|
|
|
|
|
_spawn_props_for_chunk(new_chunk)
|
|
|
|
|
|
|
|
|
|
var info_new = _get_cached_chunk_info(new_chunk, choise.scene)
|
|
|
|
|
|
|
|
|
|
@@ -634,25 +851,29 @@ func _add_compatible_biome(grid_pos: Vector2i) -> void:
|
|
|
|
|
_apply_river_flow_direction(new_chunk, river_flow_direction)
|
|
|
|
|
|
|
|
|
|
board[grid_pos] = {
|
|
|
|
|
"type": "bioma",
|
|
|
|
|
"type": "biome",
|
|
|
|
|
"exit": choise.data["connections"],
|
|
|
|
|
"river_exit": choise.data["river_connections"],
|
|
|
|
|
"river_flow_direction": river_flow_direction,
|
|
|
|
|
"heights": choise.data["heights"],
|
|
|
|
|
"node": new_chunk,
|
|
|
|
|
"info": info_new,
|
|
|
|
|
"have_lamppost": have_lamppost
|
|
|
|
|
"have_lamppost": have_lamppost,
|
|
|
|
|
"uniqueness": choise.uniqueness
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if have_lamppost:
|
|
|
|
|
_queue_lamppost_wire_connection(grid_pos)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
var backup = zone_catalogue[0].instantiate()
|
|
|
|
|
var backup_scene = _pick_backup_scene(zone_catalogue, grid_pos)
|
|
|
|
|
var backup = backup_scene.instantiate()
|
|
|
|
|
backup.position = Vector3(grid_pos.x * chunk_size, 0, grid_pos.y * chunk_size)
|
|
|
|
|
add_child(backup)
|
|
|
|
|
_spawn_props_for_chunk(backup)
|
|
|
|
|
|
|
|
|
|
var info_backup = _get_cached_chunk_info(backup, zone_catalogue[0])
|
|
|
|
|
var info_backup = _get_cached_chunk_info(backup, backup_scene)
|
|
|
|
|
var backup_uniqueness = _get_chunk_uniqueness_from_info(info_backup)
|
|
|
|
|
|
|
|
|
|
var safe_heights = {
|
|
|
|
|
"north": req_height_north if req_height_north != -1 else height_target,
|
|
|
|
|
@@ -668,9 +889,12 @@ func _add_compatible_biome(grid_pos: Vector2i) -> void:
|
|
|
|
|
"heights": safe_heights,
|
|
|
|
|
"node": backup,
|
|
|
|
|
"info": info_backup,
|
|
|
|
|
"have_lamppost": false
|
|
|
|
|
"have_lamppost": false,
|
|
|
|
|
"uniqueness": backup_uniqueness
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#Check if a neighbors have a river direction: if yes try to continue it,
|
|
|
|
|
#otherwise create a direction based on connections
|
|
|
|
|
func _calculate_river_flow_direction(grid_pos: Vector2i, river_connections: Dictionary) -> Vector2:
|
|
|
|
|
var connected_sides: Array[String] = []
|
|
|
|
|
for side in RIVER_SIDE_ORDER:
|
|
|
|
|
@@ -856,37 +1080,6 @@ func _connect_lamppost_wires(new_board_pos: Vector2i) -> void:
|
|
|
|
|
p_sx_your_best = closest_sx[i_t]
|
|
|
|
|
p_dx_your_best = closest_dx[i_t]
|
|
|
|
|
|
|
|
|
|
if best_closest_to_root == null:
|
|
|
|
|
for x in range(-ray_research, ray_research + 1):
|
|
|
|
|
for z in range(-ray_research, ray_research + 1):
|
|
|
|
|
if x == 0 and z == 0: continue
|
|
|
|
|
|
|
|
|
|
var closest_pos = new_board_pos + Vector2i(x, z)
|
|
|
|
|
if board.has(closest_pos) and board[closest_pos].get("have_lamppost", false):
|
|
|
|
|
var closest_root = board[closest_pos]["node"]
|
|
|
|
|
var closest_info = board[closest_pos]["info"]
|
|
|
|
|
|
|
|
|
|
if not is_instance_valid(closest_root) or closest_root == new_root or closest_info == null: continue
|
|
|
|
|
|
|
|
|
|
if closest_info is Node3D: closest_info.force_update_transform()
|
|
|
|
|
var closest_sx = _get_lampposts(closest_info, "sx")
|
|
|
|
|
var closest_dx = _get_lampposts(closest_info, "dx")
|
|
|
|
|
if closest_sx.is_empty() or closest_dx.is_empty(): continue
|
|
|
|
|
|
|
|
|
|
for i_m in range(new_sx.size()):
|
|
|
|
|
for i_t in range(closest_sx.size()):
|
|
|
|
|
var c_mio = (new_sx[i_m].global_position + new_dx[i_m].global_position) / 2.0
|
|
|
|
|
var c_tuo = (closest_sx[i_t].global_position + closest_dx[i_t].global_position) / 2.0
|
|
|
|
|
var dist = c_mio.distance_to(c_tuo)
|
|
|
|
|
|
|
|
|
|
if dist < best_distance:
|
|
|
|
|
best_distance = dist
|
|
|
|
|
best_closest_to_root = closest_root
|
|
|
|
|
p_sx_my_best = new_sx[i_m]
|
|
|
|
|
p_dx_my_best = new_dx[i_m]
|
|
|
|
|
p_sx_your_best = closest_sx[i_t]
|
|
|
|
|
p_dx_your_best = closest_dx[i_t]
|
|
|
|
|
|
|
|
|
|
var max_dist = chunk_size * lamppost_dist_factor
|
|
|
|
|
|
|
|
|
|
if best_closest_to_root != null and best_distance < max_dist:
|
|
|
|
|
@@ -905,7 +1098,7 @@ func _connect_lamppost_wires(new_board_pos: Vector2i) -> void:
|
|
|
|
|
if not wire_connections.has(closest_id): wire_connections[closest_id] = 0
|
|
|
|
|
wire_connections[closest_id] += 1
|
|
|
|
|
|
|
|
|
|
#draw lamppost
|
|
|
|
|
#draw lamppost wires
|
|
|
|
|
func _draw_parable(p1: Vector3, p2: Vector3, parent: Node3D) -> void:
|
|
|
|
|
var segments = 15
|
|
|
|
|
var lowering = 1.5
|
|
|
|
|
|