fix merge scenes

This commit is contained in:
2026-05-04 23:59:27 +02:00
44 changed files with 5010 additions and 226 deletions

View File

@@ -1,8 +1,13 @@
extends Node3D
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 RAILWAY_SCENE_DIRECTORY: String = "res://tgcc/chunk/railway/scene"
@export_group("Rails")
@export var rail_path: Path3D
@@ -18,6 +23,7 @@ const LAMPPOST_WIRE_STEPS_PER_FRAME: int = 1
@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
var board: Dictionary = {}
var last_pos_train: Vector2i = Vector2i(999999, 999999)
@@ -30,11 +36,15 @@ var pending_cleanup_cells: Array[Vector2i] = []
var pending_wire_cells: Array[Vector2i] = []
var pending_wire_lookup: Dictionary = {}
var pending_radar_update: bool = false
var rail_chunk_catalogue: Dictionary = {}
var manual_biome: Biome = null
func _ready() -> void:
if biome_list.is_empty() or rail_path == null: return
#connect events
UIEvents.update_rail_chunks.connect(_update_rail_chunks)
noise_generator = FastNoiseLite.new()
noise_generator.noise_type = FastNoiseLite.TYPE_PERLIN
@@ -47,6 +57,7 @@ func _ready() -> void:
altitude_generator.frequency = district_scale * 0.5
_warm_chunk_candidate_cache()
_warm_rail_chunk_catalogue()
_update_set_pieces()
func _update_set_pieces() -> void:
@@ -100,7 +111,7 @@ func _destroy_and_regenrate_world() -> void:
if rail_path != null and rail_path.train_instance != null:
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_work(current_pos)
_refresh_world(current_pos)
pending_radar_update = true
func _process(_delta: float) -> void:
@@ -110,7 +121,6 @@ func _process(_delta: float) -> void:
if pending_radar_update:
pending_radar_update = false
_train_radar()
func _physics_process(_delta: float) -> void:
if rail_path == null or rail_path.train_instance == null: return
@@ -122,7 +132,7 @@ func _physics_process(_delta: float) -> void:
if current_pos != last_pos_train:
last_pos_train = current_pos
_refresh_world_work(current_pos)
_refresh_world(current_pos)
pending_radar_update = true
func collect_all_chunkinfo(root: Node, list: Array[Node]) -> void:
@@ -154,6 +164,28 @@ func _warm_chunk_candidate_cache() -> void:
for scene in unique_scenes.values():
_get_chunk_scene_metadata(scene)
func _warm_rail_chunk_catalogue() -> void:
rail_chunk_catalogue.clear()
var directory := DirAccess.open(RAILWAY_SCENE_DIRECTORY)
if directory == null:
return
directory.list_dir_begin()
var file_name := directory.get_next()
while file_name != "":
if not directory.current_is_dir() and file_name.ends_with(".tscn"):
var scene_path := "%s/%s" % [RAILWAY_SCENE_DIRECTORY, file_name]
var scene := load(scene_path) as PackedScene
if scene != null:
var chunk_type = _get_chunk_type_from_scene(scene)
if chunk_type == CHUNK_TYPE_STRAIGHT_TRACK or chunk_type == CHUNK_TYPE_CURVED_TRACK:
if not rail_chunk_catalogue.has(chunk_type):
rail_chunk_catalogue[chunk_type] = []
rail_chunk_catalogue[chunk_type].append(scene)
file_name = directory.get_next()
directory.list_dir_end()
func _get_chunk_scene_cache_key(scene: PackedScene) -> String:
if scene == null:
return ""
@@ -209,6 +241,20 @@ func _get_cached_chunk_info(instance: Node, scene: PackedScene) -> Node:
return instance
return instance.get_node_or_null(info_path)
func _get_chunk_type_from_scene(scene: PackedScene) -> int:
if scene == null:
return CHUNK_TYPE_BIOME
var preview_chunk = scene.instantiate()
var info_list: Array[Node] = []
collect_all_chunkinfo(preview_chunk, info_list)
var info_node = info_list[0] if info_list.size() > 0 else null
var chunk_type = CHUNK_TYPE_BIOME
if info_node != null and "chunk_type" in info_node:
chunk_type = info_node.chunk_type
preview_chunk.queue_free()
return chunk_type
func _clear_pending_world_work() -> void:
pending_generation_cells.clear()
pending_cleanup_cells.clear()
@@ -216,7 +262,95 @@ func _clear_pending_world_work() -> void:
pending_wire_lookup.clear()
pending_radar_update = false
func _refresh_world_work(center: Vector2i) -> void:
func _collect_replaceable_rail_chunks(root: Node, result: Array[Node3D]) -> void:
if root == null:
return
if root is Node3D:
var node_3d := root as Node3D
if node_3d.scene_file_path.begins_with(RAILWAY_SCENE_DIRECTORY):
var info_list: Array[Node] = []
collect_all_chunkinfo(node_3d, info_list)
var info_node = info_list[0] if info_list.size() > 0 else null
if info_node != null and "chunk_type" in info_node:
var chunk_type = info_node.chunk_type
if chunk_type == CHUNK_TYPE_STRAIGHT_TRACK or chunk_type == CHUNK_TYPE_CURVED_TRACK:
result.append(node_3d)
return
for child in root.get_children():
_collect_replaceable_rail_chunks(child, result)
func _pick_replacement_rail_scene(chunk_type: int, current_scene_path: String) -> PackedScene:
var candidates: Array = rail_chunk_catalogue.get(chunk_type, [])
if candidates.is_empty():
return null
var alternatives: Array[PackedScene] = []
for candidate in candidates:
var scene := candidate as PackedScene
if scene != null and scene.resource_path != current_scene_path:
alternatives.append(scene)
if not alternatives.is_empty():
return alternatives.pick_random()
return candidates.pick_random() as PackedScene
func _replace_rail_chunk_instance(chunk_root: Node3D) -> bool:
if chunk_root == null or not is_instance_valid(chunk_root):
return false
var info_list: Array[Node] = []
collect_all_chunkinfo(chunk_root, info_list)
var info_node = info_list[0] if info_list.size() > 0 else null
if info_node == null or not "chunk_type" in info_node:
return false
var replacement_scene = _pick_replacement_rail_scene(info_node.chunk_type, chunk_root.scene_file_path)
if replacement_scene == null:
return false
var chunk_parent = chunk_root.get_parent()
var replacement = replacement_scene.instantiate() as Node3D
if chunk_parent == null or replacement == null:
return false
var chunk_index = chunk_root.get_index()
var chunk_name = chunk_root.name
chunk_root.name = "%s_old" % chunk_name
replacement.transform = chunk_root.transform
chunk_parent.add_child(replacement)
chunk_parent.move_child(replacement, chunk_index)
replacement.name = chunk_name
replacement.owner = chunk_root.owner
chunk_root.queue_free()
return true
func _refresh_rail_chunks() -> void:
print("update rail chunks")
_warm_rail_chunk_catalogue()
var current_scene: Node = get_tree().current_scene
if current_scene == null:
return
var replaceable_chunks: Array[Node3D] = []
_collect_replaceable_rail_chunks(current_scene, replaceable_chunks)
var replaced_count = 0
for chunk_root in replaceable_chunks:
if _replace_rail_chunk_instance(chunk_root):
replaced_count += 1
if replaced_count > 0:
await get_tree().process_frame
_destroy_and_regenrate_world()
func _update_rail_chunks() -> void:
call_deferred("_refresh_rail_chunks")
func _refresh_world(center: Vector2i) -> void:
_rebuild_generation_queue(center)
_rebuild_cleanup_queue(center)
@@ -537,32 +671,6 @@ func _needed_heights(near_pos: Vector2i, side_needed: String) -> int:
if board.has(near_pos) and board[near_pos].has("heights"):
return board[near_pos]["heights"][side_needed]
return -1
func _train_radar() -> void:
if rail_path == null or rail_path.curve == null or rail_path.train_instance == null: return
if manual_biome != null:
return
var train_pos = rail_path.train_instance.global_position
var grid_x = roundi(train_pos.x / chunk_size)
var grid_z = roundi(train_pos.z / chunk_size)
var current_biome = _get_procedural_biome_name(noise_generator.get_noise_2d(grid_x, grid_z))
var current_progress = rail_path.train_progress
var total_length = rail_path.curve.get_baked_length()
for step in range(1, 31):
var next_progress = wrapf(current_progress + (step * chunk_size), 0.0, total_length)
var next_local_pos = rail_path.curve.sample_baked(next_progress, true)
var next_global_pos = rail_path.to_global(next_local_pos)
var f_x = roundi(next_global_pos.x / chunk_size)
var f_z = roundi(next_global_pos.z / chunk_size)
var future_biome = _get_procedural_biome_name(noise_generator.get_noise_2d(f_x, f_z))
if future_biome != current_biome:
break
func _get_lampposts(info_node: Node, side: String) -> Array[Node3D]:
var lampposts: Array[Node3D] = []
@@ -597,7 +705,7 @@ func _connect_lamppost_wires(new_board_pos: Vector2i) -> void:
var p_sx_your_best = null; var p_dx_your_best = null
var best_closest_to_root = null
var best_distance = 999999.0
var ray_research = 3
var ray_research = 6
for x in range(-ray_research, ray_research + 1):
for z in range(-ray_research, ray_research + 1):
@@ -665,7 +773,7 @@ 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]
var max_dist = chunk_size * 8.0
var max_dist = chunk_size * lamppost_dist_factor
if best_closest_to_root != null and best_distance < max_dist:
var dist_streight = p_sx_my_best.global_position.distance_to(p_sx_your_best.global_position) + p_dx_my_best.global_position.distance_to(p_dx_your_best.global_position)
@@ -683,6 +791,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
func _draw_parable(p1: Vector3, p2: Vector3, parent: Node3D) -> void:
var segments = 15
var lowering = 1.5

View File

@@ -90,6 +90,9 @@ wind_amount = 50
cloud_speed = 0.01
fireflies_amount = 550
fireflies_spawn_ray = 60.0
water_color_morning = Color(0.33333334, 0.654902, 0.5294118, 1)
water_color_afternoon = Color(0.5803922, 0.5137255, 0.2901961, 1)
water_color_night = Color(0.48235294, 0.45490196, 0.69411767, 1)
metadata/_custom_type_script = "uid://butda6k2tli3o"
[sub_resource type="Gradient" id="Gradient_i3hjl"]

View File

@@ -30,6 +30,10 @@ signal toggle_shadows(value: bool)
@warning_ignore("unused_signal")
signal toggle_fog(value: bool)
#railway signals
@warning_ignore("unused_signal")
signal update_rail_chunks()
#day cycle signals
@warning_ignore("unused_signal")
signal toggle_pause_daytime(value: bool)