2026-08-08 01:55:16 +02:00
|
|
|
extends Node3D
|
2026-08-13 17:06:15 +02:00
|
|
|
class_name ProceduralMapGenerator
|
|
|
|
|
|
|
|
|
|
@export_group("Player")
|
|
|
|
|
@export var player_scene: PackedScene
|
|
|
|
|
@export var player_spawn_height: float = 1.5
|
2026-08-08 01:55:16 +02:00
|
|
|
|
|
|
|
|
@export_group("Map Pieces")
|
|
|
|
|
@export var start_room_scene: PackedScene
|
|
|
|
|
@export var cap_scene: PackedScene
|
|
|
|
|
@export var room_scenes: Array[PackedScene]
|
|
|
|
|
@export var corridor_scenes: Array[PackedScene]
|
|
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
@export_group("Vining Rules")
|
|
|
|
|
@export var max_attempts: int = 2000
|
|
|
|
|
@export var room_threshold: int = 20
|
|
|
|
|
@export var min_corridor_chain: int = 1
|
|
|
|
|
@export var max_corridor_chain: int = 4
|
2026-08-08 01:55:16 +02:00
|
|
|
|
|
|
|
|
var generated_rooms: Array[Node3D] = []
|
|
|
|
|
var open_connections: Array[Dictionary] = []
|
|
|
|
|
var total_pieces: int = 0
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
|
generate_map()
|
|
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
func generate_map() -> void:
|
2026-08-08 01:55:16 +02:00
|
|
|
if not start_room_scene or not cap_scene:
|
|
|
|
|
push_error("MapGenerator needs both a Start Room Scene and a Cap Scene!")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
var start_room = start_room_scene.instantiate()
|
|
|
|
|
add_child(start_room)
|
|
|
|
|
generated_rooms.append(start_room)
|
2026-08-13 17:06:15 +02:00
|
|
|
_register_open_connections(start_room, "room", 0, 0)
|
2026-08-08 01:55:16 +02:00
|
|
|
total_pieces += 1
|
|
|
|
|
|
|
|
|
|
var attempts = 0
|
|
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
while open_connections.size() > 0 and attempts < max_attempts:
|
2026-08-08 01:55:16 +02:00
|
|
|
attempts += 1
|
2026-08-13 17:06:15 +02:00
|
|
|
var target_dict = open_connections.pop_back()
|
2026-08-08 01:55:16 +02:00
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
var target_marker: Node3D = target_dict["marker"]
|
|
|
|
|
if _is_marker_blocked(target_marker):
|
2026-08-08 01:55:16 +02:00
|
|
|
continue
|
|
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
var categories_to_try = _get_desired_categories(target_dict)
|
|
|
|
|
var placed = false
|
2026-08-08 01:55:16 +02:00
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
# Try to place pieces cleanly
|
|
|
|
|
for category in categories_to_try:
|
|
|
|
|
if _try_place_category(category, target_dict):
|
|
|
|
|
placed = true
|
2026-08-08 01:55:16 +02:00
|
|
|
break
|
|
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
# THE ABSOLUTE FAILSAFE:
|
|
|
|
|
# If it's a dead end and nothing fits cleanly, FORCE a cap so there are no voids!
|
|
|
|
|
if not placed:
|
|
|
|
|
_force_cap_on_marker(target_dict)
|
|
|
|
|
|
|
|
|
|
_seal_remaining_connections()
|
|
|
|
|
_cull_clipping_caps()
|
|
|
|
|
print("Map Generation Complete! Total pieces: ", total_pieces)
|
|
|
|
|
|
|
|
|
|
# Spawn the Player!
|
|
|
|
|
if player_scene and generated_rooms.size() > 0:
|
|
|
|
|
var player = player_scene.instantiate()
|
|
|
|
|
get_tree().current_scene.add_child(player)
|
|
|
|
|
player.global_position = generated_rooms[0].global_position + Vector3(0, player_spawn_height, 0)
|
|
|
|
|
|
|
|
|
|
# --- VINING LOGIC ---
|
|
|
|
|
|
|
|
|
|
func _get_desired_categories(target_dict: Dictionary) -> Array[String]:
|
|
|
|
|
if total_pieces >= room_threshold:
|
|
|
|
|
return ["cap"]
|
|
|
|
|
|
|
|
|
|
var current = target_dict["current_chain"]
|
|
|
|
|
var target = target_dict["target_chain"]
|
|
|
|
|
|
|
|
|
|
if current < target:
|
|
|
|
|
return ["corridor", "room", "cap"]
|
|
|
|
|
else:
|
|
|
|
|
return ["room", "cap"]
|
|
|
|
|
|
|
|
|
|
func _register_open_connections(room: Node3D, type: String, current_chain: int, target_chain: int) -> void:
|
|
|
|
|
var markers = _get_connection_markers(room)
|
|
|
|
|
for marker in markers:
|
|
|
|
|
var next_current = current_chain
|
|
|
|
|
var next_target = target_chain
|
|
|
|
|
|
|
|
|
|
if type == "room":
|
|
|
|
|
next_current = 0
|
|
|
|
|
next_target = randi_range(min_corridor_chain, max_corridor_chain)
|
|
|
|
|
elif type == "corridor":
|
|
|
|
|
next_current += 1
|
2026-08-08 01:55:16 +02:00
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
open_connections.append({
|
|
|
|
|
"marker": marker,
|
|
|
|
|
"type": type,
|
|
|
|
|
"current_chain": next_current,
|
|
|
|
|
"target_chain": next_target
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
# --- PLACEMENT LOGIC ---
|
|
|
|
|
|
|
|
|
|
func _try_place_category(category: String, target_dict: Dictionary) -> bool:
|
|
|
|
|
var target_marker: Node3D = target_dict["marker"]
|
|
|
|
|
var scenes_to_try: Array[PackedScene] = []
|
|
|
|
|
|
|
|
|
|
match category:
|
|
|
|
|
"cap": scenes_to_try = [cap_scene]
|
|
|
|
|
"corridor": scenes_to_try = corridor_scenes.duplicate()
|
|
|
|
|
"room": scenes_to_try = room_scenes.duplicate()
|
|
|
|
|
|
|
|
|
|
scenes_to_try.shuffle()
|
|
|
|
|
|
|
|
|
|
for scene in scenes_to_try:
|
|
|
|
|
var new_room = scene.instantiate()
|
|
|
|
|
var new_room_markers = _get_connection_markers(new_room)
|
|
|
|
|
|
|
|
|
|
if new_room_markers.is_empty() and category != "cap":
|
|
|
|
|
new_room.queue_free()
|
|
|
|
|
continue
|
2026-08-08 01:55:16 +02:00
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
new_room_markers.shuffle()
|
|
|
|
|
|
|
|
|
|
if new_room_markers.is_empty() and category == "cap":
|
|
|
|
|
_align_room(new_room, new_room, target_marker)
|
|
|
|
|
if not _do_rooms_overlap(new_room):
|
|
|
|
|
_lock_in_piece(new_room, category, [], target_dict)
|
|
|
|
|
return true
|
|
|
|
|
new_room.queue_free()
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
for incoming_marker in new_room_markers:
|
|
|
|
|
_align_room(new_room, incoming_marker, target_marker)
|
|
|
|
|
|
|
|
|
|
if not _do_rooms_overlap(new_room):
|
|
|
|
|
if _can_seal_outgoing_doors(new_room, incoming_marker):
|
|
|
|
|
var remaining_markers = new_room_markers.duplicate()
|
|
|
|
|
remaining_markers.erase(incoming_marker)
|
|
|
|
|
_lock_in_piece(new_room, category, remaining_markers, target_dict)
|
|
|
|
|
return true
|
2026-08-08 01:55:16 +02:00
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
new_room.queue_free()
|
|
|
|
|
return false
|
2026-08-08 01:55:16 +02:00
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
func _lock_in_piece(new_room: Node3D, category: String, remaining_markers: Array, target_dict: Dictionary) -> void:
|
|
|
|
|
add_child(new_room)
|
|
|
|
|
generated_rooms.append(new_room)
|
|
|
|
|
total_pieces += 1
|
|
|
|
|
|
|
|
|
|
if category == "cap":
|
|
|
|
|
new_room.set_meta("is_cap", true) # TAG THE CAP!
|
|
|
|
|
else:
|
|
|
|
|
new_room.set_meta("temp_markers", remaining_markers)
|
|
|
|
|
_register_open_connections(new_room, category, target_dict["current_chain"], target_dict["target_chain"])
|
|
|
|
|
new_room.remove_meta("temp_markers")
|
2026-08-08 01:55:16 +02:00
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
func _force_cap_on_marker(target_dict: Dictionary) -> void:
|
2026-08-08 01:55:16 +02:00
|
|
|
var target_marker = target_dict["marker"]
|
|
|
|
|
var cap = cap_scene.instantiate()
|
2026-08-13 17:06:15 +02:00
|
|
|
var markers = _get_connection_markers(cap)
|
|
|
|
|
|
|
|
|
|
if markers.size() > 0:
|
|
|
|
|
_align_room(cap, markers[0], target_marker)
|
|
|
|
|
else:
|
|
|
|
|
_align_room(cap, cap, target_marker)
|
|
|
|
|
|
2026-08-08 01:55:16 +02:00
|
|
|
add_child(cap)
|
2026-08-13 17:06:15 +02:00
|
|
|
cap.set_meta("is_cap", true) # TAG THE FORCED CAP!
|
2026-08-08 01:55:16 +02:00
|
|
|
generated_rooms.append(cap)
|
2026-08-13 17:06:15 +02:00
|
|
|
total_pieces += 1
|
2026-08-08 01:55:16 +02:00
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
func _can_seal_outgoing_doors(new_room: Node3D, incoming_marker: Node3D) -> bool:
|
|
|
|
|
var markers = _get_connection_markers(new_room)
|
|
|
|
|
for out_marker in markers:
|
|
|
|
|
if out_marker == incoming_marker: continue
|
|
|
|
|
|
|
|
|
|
var test_cap = cap_scene.instantiate()
|
|
|
|
|
var cap_markers = _get_connection_markers(test_cap)
|
|
|
|
|
var cap_fits = false
|
|
|
|
|
|
|
|
|
|
if cap_markers.size() > 0:
|
|
|
|
|
_align_room(test_cap, cap_markers[0], out_marker)
|
|
|
|
|
if not _do_rooms_overlap(test_cap): cap_fits = true
|
|
|
|
|
else:
|
|
|
|
|
_align_room(test_cap, test_cap, out_marker)
|
|
|
|
|
if not _do_rooms_overlap(test_cap): cap_fits = true
|
|
|
|
|
|
|
|
|
|
test_cap.queue_free()
|
|
|
|
|
if not cap_fits: return false
|
|
|
|
|
return true
|
2026-08-08 01:55:16 +02:00
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
func _seal_remaining_connections() -> void:
|
2026-08-08 01:55:16 +02:00
|
|
|
for i in range(open_connections.size() - 1, -1, -1):
|
|
|
|
|
var target_dict = open_connections[i]
|
2026-08-13 17:06:15 +02:00
|
|
|
if not _is_marker_blocked(target_dict["marker"]):
|
|
|
|
|
if not _try_place_category("cap", target_dict):
|
|
|
|
|
_force_cap_on_marker(target_dict)
|
2026-08-08 01:55:16 +02:00
|
|
|
open_connections.clear()
|
|
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
# --- MATH & BOUNDS ---
|
2026-08-08 01:55:16 +02:00
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
func _align_room(new_room: Node3D, incoming_marker: Node3D, target_marker: Node3D) -> void:
|
|
|
|
|
var target_global = _get_safe_global_transform(target_marker)
|
|
|
|
|
var desired_incoming_transform = target_global.rotated_local(Vector3.UP, PI)
|
|
|
|
|
|
|
|
|
|
var room_to_marker = incoming_marker.transform
|
|
|
|
|
var parent = incoming_marker.get_parent()
|
|
|
|
|
while parent and parent is Node3D and parent != new_room:
|
|
|
|
|
room_to_marker = parent.transform * room_to_marker
|
|
|
|
|
parent = parent.get_parent()
|
2026-08-08 01:55:16 +02:00
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
new_room.transform = desired_incoming_transform * room_to_marker.affine_inverse()
|
2026-08-08 01:55:16 +02:00
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
func _do_rooms_overlap(new_room: Node3D) -> bool:
|
|
|
|
|
var aabb_new = _get_room_global_aabb(new_room)
|
|
|
|
|
if aabb_new.size == Vector3.ZERO: return false
|
2026-08-08 01:55:16 +02:00
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
aabb_new = _safe_shrink_aabb(aabb_new, 0.1)
|
|
|
|
|
if aabb_new.size == Vector3.ZERO: return false
|
2026-08-08 01:55:16 +02:00
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
for existing_room in generated_rooms:
|
|
|
|
|
var aabb_existing = _get_room_global_aabb(existing_room)
|
|
|
|
|
if aabb_existing.size != Vector3.ZERO:
|
|
|
|
|
aabb_existing = _safe_shrink_aabb(aabb_existing, 0.1)
|
|
|
|
|
if aabb_new.intersects(aabb_existing):
|
|
|
|
|
return true
|
|
|
|
|
return false
|
2026-08-08 01:55:16 +02:00
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
# Godot 4 native bounding box shrinking
|
|
|
|
|
func _safe_shrink_aabb(aabb: AABB, shrink_by: float) -> AABB:
|
|
|
|
|
var new_aabb = aabb.grow(-shrink_by)
|
|
|
|
|
# Prevent negative sizes which break overlap physics
|
|
|
|
|
if new_aabb.size.x <= 0 or new_aabb.size.y <= 0 or new_aabb.size.z <= 0:
|
|
|
|
|
return AABB(aabb.position, Vector3.ZERO)
|
|
|
|
|
return new_aabb
|
2026-08-08 01:55:16 +02:00
|
|
|
|
2026-08-13 17:06:15 +02:00
|
|
|
func _get_room_global_aabb(room: Node3D) -> AABB:
|
2026-08-08 01:55:16 +02:00
|
|
|
var combined_aabb := AABB()
|
|
|
|
|
var is_first_shape = true
|
2026-08-13 17:06:15 +02:00
|
|
|
var volume_node = room.get_node_or_null("Volume")
|
|
|
|
|
if not volume_node: return combined_aabb
|
|
|
|
|
|
|
|
|
|
for child in volume_node.get_children():
|
2026-08-08 01:55:16 +02:00
|
|
|
if child is CollisionShape3D and child.shape is BoxShape3D:
|
|
|
|
|
var shape = child.shape as BoxShape3D
|
|
|
|
|
var local_aabb = AABB(-shape.size / 2.0, shape.size)
|
2026-08-13 17:06:15 +02:00
|
|
|
var child_global_t = _get_safe_global_transform(child)
|
|
|
|
|
var global_aabb = child_global_t * local_aabb
|
2026-08-08 01:55:16 +02:00
|
|
|
|
|
|
|
|
if is_first_shape:
|
|
|
|
|
combined_aabb = global_aabb
|
|
|
|
|
is_first_shape = false
|
|
|
|
|
else:
|
|
|
|
|
combined_aabb = combined_aabb.merge(global_aabb)
|
|
|
|
|
return combined_aabb
|
2026-08-13 17:06:15 +02:00
|
|
|
|
|
|
|
|
func _is_marker_blocked(marker: Node3D) -> bool:
|
|
|
|
|
for room in generated_rooms:
|
|
|
|
|
var aabb = _get_room_global_aabb(room)
|
|
|
|
|
if aabb.size != Vector3.ZERO:
|
|
|
|
|
aabb = _safe_shrink_aabb(aabb, 0.1)
|
|
|
|
|
if aabb.has_point(marker.global_position): return true
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
func _get_connection_markers(room: Node3D) -> Array:
|
|
|
|
|
if room.has_meta("temp_markers"): return room.get_meta("temp_markers")
|
|
|
|
|
var markers = []
|
|
|
|
|
_find_markers_recursive(room, markers)
|
|
|
|
|
return markers
|
|
|
|
|
|
|
|
|
|
func _find_markers_recursive(node: Node, markers: Array) -> void:
|
|
|
|
|
var is_valid_door = false
|
|
|
|
|
if node is Marker3D:
|
|
|
|
|
is_valid_door = true
|
|
|
|
|
elif node is Node3D and ("Marker" in node.name or "WallHole" in node.name):
|
|
|
|
|
is_valid_door = true
|
|
|
|
|
|
|
|
|
|
if is_valid_door and not markers.has(node):
|
|
|
|
|
markers.append(node)
|
|
|
|
|
return # Prevents doubling-up if a WallHole contains a Marker inside it!
|
|
|
|
|
|
|
|
|
|
for child in node.get_children():
|
|
|
|
|
_find_markers_recursive(child, markers)
|
|
|
|
|
|
|
|
|
|
func _get_safe_global_transform(node: Node3D) -> Transform3D:
|
|
|
|
|
if node.is_inside_tree(): return node.global_transform
|
|
|
|
|
var t = node.transform
|
|
|
|
|
var parent = node.get_parent()
|
|
|
|
|
while parent and parent is Node3D:
|
|
|
|
|
t = parent.transform * t
|
|
|
|
|
parent = parent.get_parent()
|
|
|
|
|
return t
|
|
|
|
|
|
|
|
|
|
func _cull_clipping_caps() -> void:
|
|
|
|
|
# Loop backward so we can safely delete items from the array while iterating
|
|
|
|
|
for i in range(generated_rooms.size() - 1, -1, -1):
|
|
|
|
|
var room = generated_rooms[i]
|
|
|
|
|
|
|
|
|
|
# Only check the pieces we explicitly tagged as caps
|
|
|
|
|
if room.has_meta("is_cap"):
|
|
|
|
|
var cap_aabb = _get_room_global_aabb(room)
|
|
|
|
|
if cap_aabb.size == Vector3.ZERO: continue
|
|
|
|
|
|
|
|
|
|
var is_clipping = false
|
|
|
|
|
|
|
|
|
|
# Check this cap against every other room in the map
|
|
|
|
|
for j in range(generated_rooms.size()):
|
|
|
|
|
if i == j: continue
|
|
|
|
|
|
|
|
|
|
var other_room = generated_rooms[j]
|
|
|
|
|
var other_aabb = _get_room_global_aabb(other_room)
|
|
|
|
|
|
|
|
|
|
if other_aabb.size != Vector3.ZERO:
|
|
|
|
|
# Shrink the other room's AABB.
|
|
|
|
|
# This ensures caps sitting perfectly on the edge are ignored,
|
|
|
|
|
# but caps shoved through the wall are caught!
|
|
|
|
|
other_aabb = _safe_shrink_aabb(other_aabb, 0.4)
|
|
|
|
|
|
|
|
|
|
if other_aabb.size != Vector3.ZERO and cap_aabb.intersects(other_aabb):
|
|
|
|
|
is_clipping = true
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
# If it's inside another room, destroy it!
|
|
|
|
|
if is_clipping:
|
|
|
|
|
room.queue_free()
|
|
|
|
|
generated_rooms.remove_at(i)
|
|
|
|
|
total_pieces -= 1
|