131 lines
4.7 KiB
GDScript
131 lines
4.7 KiB
GDScript
class_name RoomGraph
|
|
extends RefCounted
|
|
|
|
# Tree edges are the only topology used to guarantee a route from the entrance
|
|
# to the exit. Loop edges are deliberately kept separate so future special
|
|
# gates cannot accidentally become part of that guarantee.
|
|
var tree_edges: Array[Dictionary] = []
|
|
var loop_edges: Array[Dictionary] = []
|
|
var edges: Array[Dictionary] = []
|
|
|
|
func connect_each_room_to_nearest_prior(rooms: Array[RoomState]) -> void:
|
|
tree_edges.clear()
|
|
loop_edges.clear()
|
|
for room_index in range(1, rooms.size()):
|
|
var nearest_index := 0
|
|
var nearest_distance := _squared_distance(_room_center(rooms[room_index].bounds), _room_center(rooms[0].bounds))
|
|
for prior_index in range(1, room_index):
|
|
var distance := _squared_distance(_room_center(rooms[room_index].bounds), _room_center(rooms[prior_index].bounds))
|
|
if distance < nearest_distance:
|
|
nearest_index = prior_index
|
|
nearest_distance = distance
|
|
tree_edges.append(_edge(nearest_index, room_index, false))
|
|
_refresh_edges()
|
|
|
|
func add_local_loops(rooms: Array[RoomState], rng: DeterministicRng) -> void:
|
|
var requested_count := rooms.size() / 4
|
|
var attempts := 0
|
|
while loop_edges.size() < requested_count and attempts < rooms.size() * 2:
|
|
attempts += 1
|
|
var from_index := rng.next_int() % rooms.size()
|
|
var candidates := _nearby_unconnected_indices(rooms, from_index)
|
|
if candidates.is_empty():
|
|
continue
|
|
var to_index: int = candidates[rng.next_int() % candidates.size()]
|
|
loop_edges.append(_edge(from_index, to_index, true))
|
|
_refresh_edges()
|
|
|
|
func tree_distances(start_index: int) -> Dictionary[int, int]:
|
|
var adjacency: Dictionary[int, Array] = {}
|
|
for edge in tree_edges:
|
|
var from_index := int(edge["from"])
|
|
var to_index := int(edge["to"])
|
|
if not adjacency.has(from_index):
|
|
adjacency[from_index] = []
|
|
if not adjacency.has(to_index):
|
|
adjacency[to_index] = []
|
|
adjacency[from_index].append(to_index)
|
|
adjacency[to_index].append(from_index)
|
|
var distances: Dictionary[int, int] = {start_index: 0}
|
|
var frontier: Array[int] = [start_index]
|
|
while not frontier.is_empty():
|
|
var current := frontier.pop_front()
|
|
for neighbor in adjacency.get(current, []):
|
|
if not distances.has(neighbor):
|
|
distances[neighbor] = distances[current] + 1
|
|
frontier.append(neighbor)
|
|
return distances
|
|
|
|
func farthest_tree_room(start_index: int) -> int:
|
|
var distances := tree_distances(start_index)
|
|
var farthest_index := start_index
|
|
var farthest_distance := -1
|
|
for room_index in range(distances.size()):
|
|
var distance := distances.get(room_index, -1)
|
|
if distance > farthest_distance:
|
|
farthest_index = room_index
|
|
farthest_distance = distance
|
|
return farthest_index
|
|
|
|
func to_dict() -> Dictionary:
|
|
return {
|
|
"tree_edges": tree_edges.duplicate(true),
|
|
"loop_edges": loop_edges.duplicate(true),
|
|
}
|
|
|
|
static func from_dict(data: Dictionary) -> RoomGraph:
|
|
var graph := RoomGraph.new()
|
|
for edge_data in data.get("tree_edges", []):
|
|
graph.tree_edges.append(edge_data.duplicate(true))
|
|
for edge_data in data.get("loop_edges", []):
|
|
graph.loop_edges.append(edge_data.duplicate(true))
|
|
graph._refresh_edges()
|
|
return graph
|
|
|
|
func _nearby_unconnected_indices(rooms: Array[RoomState], from_index: int) -> Array[int]:
|
|
var ranked: Array[Dictionary] = []
|
|
var from_center := _room_center(rooms[from_index].bounds)
|
|
for candidate_index in range(rooms.size()):
|
|
if candidate_index == from_index:
|
|
continue
|
|
ranked.append({
|
|
"index": candidate_index,
|
|
"distance": _squared_distance(from_center, _room_center(rooms[candidate_index].bounds)),
|
|
})
|
|
ranked.sort_custom(func(first: Dictionary, second: Dictionary) -> bool:
|
|
if int(first["distance"]) == int(second["distance"]):
|
|
return int(first["index"]) < int(second["index"])
|
|
return int(first["distance"]) < int(second["distance"])
|
|
)
|
|
var nearby: Array[int] = []
|
|
for candidate in ranked.slice(0, mini(6, ranked.size())):
|
|
var candidate_index := int(candidate["index"])
|
|
if not _has_edge_between(from_index, candidate_index):
|
|
nearby.append(candidate_index)
|
|
return nearby
|
|
|
|
func _has_edge_between(first_index: int, second_index: int) -> bool:
|
|
for edge in edges:
|
|
if (int(edge["from"]) == first_index and int(edge["to"]) == second_index) \
|
|
or (int(edge["from"]) == second_index and int(edge["to"]) == first_index):
|
|
return true
|
|
return false
|
|
|
|
func _refresh_edges() -> void:
|
|
edges = tree_edges.duplicate(true)
|
|
edges.append_array(loop_edges.duplicate(true))
|
|
|
|
func _edge(from_index: int, to_index: int, future_special: bool) -> Dictionary:
|
|
return {
|
|
"from": from_index,
|
|
"to": to_index,
|
|
"future_special": future_special,
|
|
}
|
|
|
|
func _room_center(bounds: Rect2i) -> Vector2i:
|
|
return bounds.position + Vector2i(bounds.size.x / 2, bounds.size.y / 2)
|
|
|
|
func _squared_distance(from: Vector2i, to: Vector2i) -> int:
|
|
var delta := to - from
|
|
return delta.x * delta.x + delta.y * delta.y
|