B4: Populate procedural floors #21
@@ -7,12 +7,15 @@ var hit_points: int
|
||||
var max_hit_points: int
|
||||
var attack_damage: int
|
||||
|
||||
func _init(new_id: StringName, new_cell: Vector2i, new_hit_points: int, new_attack_damage: int) -> void:
|
||||
var content_id: StringName
|
||||
|
||||
func _init(new_id: StringName, new_cell: Vector2i, new_hit_points: int, new_attack_damage: int, new_content_id: StringName = &"") -> void:
|
||||
id = new_id
|
||||
cell = new_cell
|
||||
hit_points = new_hit_points
|
||||
max_hit_points = new_hit_points
|
||||
attack_damage = new_attack_damage
|
||||
content_id = new_content_id if not new_content_id.is_empty() else new_id
|
||||
|
||||
func is_alive() -> bool:
|
||||
return hit_points > 0
|
||||
|
||||
@@ -10,6 +10,8 @@ var _entries := {
|
||||
"loot_table": {},
|
||||
"npc": {},
|
||||
"quest": {},
|
||||
"trap": {},
|
||||
"event": {},
|
||||
}
|
||||
var _diagnostics := PackedStringArray()
|
||||
|
||||
@@ -44,6 +46,12 @@ func get_npc(id: StringName) -> Dictionary:
|
||||
func get_quest(id: StringName) -> Dictionary:
|
||||
return _definition_for("quest", id)
|
||||
|
||||
func get_trap(id: StringName) -> Dictionary:
|
||||
return _definition_for("trap", id)
|
||||
|
||||
func get_event(id: StringName) -> Dictionary:
|
||||
return _definition_for("event", id)
|
||||
|
||||
func get_diagnostics() -> PackedStringArray:
|
||||
return _diagnostics
|
||||
|
||||
|
||||
@@ -30,8 +30,9 @@ const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
|
||||
const FloorStateType = preload("res://scripts/domain/world/floor_state.gd")
|
||||
const RoomStateType = preload("res://scripts/domain/world/room_state.gd")
|
||||
const RoomGraphType = preload("res://scripts/domain/world/room_graph.gd")
|
||||
const FloorPopulatorType = preload("res://scripts/domain/world/floor_populator.gd")
|
||||
|
||||
static func generate(seed: String, floor_number: int, _catalog: ContentCatalog) -> FloorState:
|
||||
static func generate(seed: String, floor_number: int, catalog: ContentCatalog) -> FloorState:
|
||||
var rng := DeterministicRngType.from_seed("%s-floor%d" % [seed, floor_number])
|
||||
var rooms := _place_rooms(rng)
|
||||
var map := GridMapStateType.new(MAP_SIZE, GridMapStateType.CellKind.WALL)
|
||||
@@ -63,6 +64,7 @@ static func generate(seed: String, floor_number: int, _catalog: ContentCatalog)
|
||||
floor.map.set_cell(floor.exit, GridMapStateType.CellKind.STAIRS)
|
||||
floor.metadata["room_graph"] = graph.to_dict()
|
||||
floor.metadata["tree_door_cells"] = tree_door_cells
|
||||
FloorPopulatorType.populate(floor, catalog, seed)
|
||||
return floor
|
||||
|
||||
static func _place_rooms(rng: DeterministicRng) -> Array[RoomState]:
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
class_name FloorPopulator
|
||||
extends RefCounted
|
||||
|
||||
const DeterministicRngType = preload("res://scripts/domain/run/deterministic_rng.gd")
|
||||
const ActorStateType = preload("res://scripts/domain/actor_state.gd")
|
||||
const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
|
||||
const SpawnRulesType = preload("res://scripts/domain/world/spawn_rules.gd")
|
||||
|
||||
static func populate(floor: FloorState, catalog: ContentCatalog, population_seed: String) -> void:
|
||||
_clear_population(floor)
|
||||
var rng := DeterministicRngType.from_seed("%s-population-%d" % [population_seed, floor.floor_number])
|
||||
var occupied: Dictionary[Vector2i, bool] = {
|
||||
floor.entrance: true,
|
||||
floor.exit: true,
|
||||
}
|
||||
var serial := 0
|
||||
for room in floor.rooms:
|
||||
if room.kind == &"entrance":
|
||||
continue
|
||||
match room.kind:
|
||||
&"combat", &"boss", &"arena":
|
||||
serial = _spawn_enemy(floor, catalog, room, rng, occupied, serial)
|
||||
&"treasure":
|
||||
_spawn_chest(floor, catalog, room, rng, occupied)
|
||||
&"trap":
|
||||
_spawn_trap(floor, catalog, room, rng, occupied)
|
||||
&"shop", &"guild", &"sanctuary", &"refuge":
|
||||
serial = _spawn_npc(floor, catalog, room, rng, occupied, serial)
|
||||
&"narrative", &"mystery", &"puzzle":
|
||||
_spawn_event(floor, catalog, room, rng, occupied)
|
||||
|
||||
static func _clear_population(floor: FloorState) -> void:
|
||||
floor.enemies.clear()
|
||||
floor.npcs.clear()
|
||||
floor.chests.clear()
|
||||
floor.traps.clear()
|
||||
floor.events.clear()
|
||||
|
||||
static func _spawn_enemy(floor: FloorState, catalog: ContentCatalog, room: RoomState, rng: DeterministicRng, occupied: Dictionary[Vector2i, bool], serial: int) -> int:
|
||||
var definitions := SpawnRulesType.eligible_enemies(catalog, floor.floor_number)
|
||||
var cell := _available_cell(floor, room, rng, occupied)
|
||||
if definitions.is_empty() or cell == Vector2i(-1, -1):
|
||||
return serial
|
||||
var definition: Dictionary = definitions[rng.next_int() % definitions.size()]
|
||||
var content_id := StringName(definition["id"])
|
||||
var actor_id := StringName("%s_%02d" % [content_id, serial])
|
||||
floor.enemies[actor_id] = ActorStateType.new(actor_id, cell, int(definition.get("hp", 1)), int(definition.get("attack", 0)), content_id)
|
||||
occupied[cell] = true
|
||||
return serial + 1
|
||||
|
||||
static func _spawn_chest(floor: FloorState, catalog: ContentCatalog, room: RoomState, rng: DeterministicRng, occupied: Dictionary[Vector2i, bool]) -> void:
|
||||
var definitions := SpawnRulesType.loot_tables(catalog)
|
||||
var cell := _available_cell(floor, room, rng, occupied)
|
||||
if definitions.is_empty() or cell == Vector2i(-1, -1):
|
||||
return
|
||||
var definition: Dictionary = definitions[rng.next_int() % definitions.size()]
|
||||
floor.chests[cell] = {
|
||||
"loot_table_id": definition["id"],
|
||||
"opened": false,
|
||||
}
|
||||
occupied[cell] = true
|
||||
|
||||
static func _spawn_trap(floor: FloorState, catalog: ContentCatalog, room: RoomState, rng: DeterministicRng, occupied: Dictionary[Vector2i, bool]) -> void:
|
||||
var definitions := SpawnRulesType.traps(catalog)
|
||||
var cell := _available_cell(floor, room, rng, occupied)
|
||||
if definitions.is_empty() or cell == Vector2i(-1, -1):
|
||||
return
|
||||
var definition: Dictionary = definitions[rng.next_int() % definitions.size()]
|
||||
floor.traps[cell] = {"trap_id": definition["id"]}
|
||||
occupied[cell] = true
|
||||
|
||||
static func _spawn_npc(floor: FloorState, catalog: ContentCatalog, room: RoomState, rng: DeterministicRng, occupied: Dictionary[Vector2i, bool], serial: int) -> int:
|
||||
var definition := SpawnRulesType.npc_for_role(catalog, room.kind)
|
||||
var cell := _available_cell(floor, room, rng, occupied)
|
||||
if definition.is_empty() or cell == Vector2i(-1, -1):
|
||||
return serial
|
||||
var content_id := StringName(definition["id"])
|
||||
var actor_id := StringName("%s_%02d" % [content_id, serial])
|
||||
floor.npcs[actor_id] = ActorStateType.new(actor_id, cell, 1, 0, content_id)
|
||||
occupied[cell] = true
|
||||
return serial + 1
|
||||
|
||||
static func _spawn_event(floor: FloorState, catalog: ContentCatalog, room: RoomState, rng: DeterministicRng, occupied: Dictionary[Vector2i, bool]) -> void:
|
||||
var definition := SpawnRulesType.event_for_role(catalog, room.kind)
|
||||
var cell := _available_cell(floor, room, rng, occupied)
|
||||
if definition.is_empty() or cell == Vector2i(-1, -1):
|
||||
return
|
||||
floor.events.append({
|
||||
"event_id": definition["id"],
|
||||
"cell": [cell.x, cell.y],
|
||||
"room_id": String(room.id),
|
||||
"resolved": false,
|
||||
})
|
||||
occupied[cell] = true
|
||||
|
||||
static func _available_cell(floor: FloorState, room: RoomState, rng: DeterministicRng, occupied: Dictionary[Vector2i, bool]) -> Vector2i:
|
||||
var candidates: Array[Vector2i] = []
|
||||
for y in range(room.bounds.position.y, room.bounds.end.y):
|
||||
for x in range(room.bounds.position.x, room.bounds.end.x):
|
||||
var cell := Vector2i(x, y)
|
||||
if floor.map.get_cell(cell) == GridMapStateType.CellKind.FLOOR and not occupied.has(cell):
|
||||
candidates.append(cell)
|
||||
if candidates.is_empty():
|
||||
return Vector2i(-1, -1)
|
||||
return candidates[rng.next_int() % candidates.size()]
|
||||
@@ -0,0 +1 @@
|
||||
uid://dh111xs1bii08
|
||||
@@ -18,6 +18,7 @@ var entities: Dictionary[StringName, ActorState]:
|
||||
enemies = value
|
||||
npcs.clear()
|
||||
var chests: Dictionary[Vector2i, Dictionary] = {}
|
||||
var traps: Dictionary[Vector2i, Dictionary] = {}
|
||||
var ground_items: Dictionary[Vector2i, Array] = {}
|
||||
var events: Array[Dictionary] = []
|
||||
var explored_cells: Dictionary[Vector2i, bool] = {}
|
||||
@@ -42,6 +43,7 @@ func to_dict() -> Dictionary:
|
||||
"enemies": _actors_to_array(enemies),
|
||||
"npcs": _actors_to_array(npcs),
|
||||
"chests": _cell_dictionary_to_array(chests),
|
||||
"traps": _cell_dictionary_to_array(traps),
|
||||
"ground_items": _cell_dictionary_to_array(ground_items),
|
||||
"events": events.duplicate(true),
|
||||
"explored_cells": _explored_cells_to_array(),
|
||||
@@ -62,6 +64,8 @@ static func from_dict(data: Dictionary) -> FloorState:
|
||||
floor.npcs = _actors_from_array(data.get("npcs", []))
|
||||
for chest_data: Dictionary in data.get("chests", []):
|
||||
floor.chests[_array_to_cell(chest_data.get("cell", [0, 0]))] = chest_data.get("value", {}).duplicate(true)
|
||||
for trap_data: Dictionary in data.get("traps", []):
|
||||
floor.traps[_array_to_cell(trap_data.get("cell", [0, 0]))] = trap_data.get("value", {}).duplicate(true)
|
||||
for item_data: Dictionary in data.get("ground_items", []):
|
||||
floor.ground_items[_array_to_cell(item_data.get("cell", [0, 0]))] = item_data.get("value", []).duplicate(true)
|
||||
for event_data: Dictionary in data.get("events", []):
|
||||
@@ -113,6 +117,7 @@ static func _actor_to_dict(actor: ActorState) -> Dictionary:
|
||||
"hit_points": actor.hit_points,
|
||||
"max_hit_points": actor.max_hit_points,
|
||||
"attack_damage": actor.attack_damage,
|
||||
"content_id": String(actor.content_id),
|
||||
}
|
||||
|
||||
static func _actor_from_dict(data: Dictionary) -> ActorState:
|
||||
@@ -120,7 +125,8 @@ static func _actor_from_dict(data: Dictionary) -> ActorState:
|
||||
StringName(data.get("id", "")),
|
||||
_array_to_cell(data.get("cell", [0, 0])),
|
||||
int(data.get("max_hit_points", data.get("hit_points", 0))),
|
||||
int(data.get("attack_damage", 0))
|
||||
int(data.get("attack_damage", 0)),
|
||||
StringName(data.get("content_id", data.get("id", "")))
|
||||
)
|
||||
actor.hit_points = int(data.get("hit_points", actor.max_hit_points))
|
||||
return actor
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
class_name SpawnRules
|
||||
extends RefCounted
|
||||
|
||||
# B4 intentionally keeps authored content as lightweight records. Interaction
|
||||
# semantics belong to later slices; these rules only decide which catalog data
|
||||
# can occupy a generated floor.
|
||||
static func eligible_enemies(catalog: ContentCatalog, floor_number: int) -> Array[Dictionary]:
|
||||
var eligible: Array[Dictionary] = []
|
||||
for id in catalog.get_ids(&"enemy"):
|
||||
var definition := catalog.get_enemy(id)
|
||||
if int(definition.get("min_floor", 1)) <= floor_number:
|
||||
eligible.append(definition)
|
||||
return eligible
|
||||
|
||||
static func loot_tables(catalog: ContentCatalog) -> Array[Dictionary]:
|
||||
return _definitions(catalog, &"loot_table")
|
||||
|
||||
static func npcs(catalog: ContentCatalog) -> Array[Dictionary]:
|
||||
return _definitions(catalog, &"npc")
|
||||
|
||||
static func traps(catalog: ContentCatalog) -> Array[Dictionary]:
|
||||
return _definitions(catalog, &"trap")
|
||||
|
||||
static func events(catalog: ContentCatalog) -> Array[Dictionary]:
|
||||
return _definitions(catalog, &"event")
|
||||
|
||||
static func npc_for_role(catalog: ContentCatalog, role: StringName) -> Dictionary:
|
||||
var preferred_id := {
|
||||
&"shop": &"npc_wandering_merchant",
|
||||
&"guild": &"npc_guild_keeper",
|
||||
&"sanctuary": &"npc_shelter_survivor",
|
||||
&"refuge": &"npc_bobka",
|
||||
}.get(role, &"")
|
||||
if not preferred_id.is_empty():
|
||||
var preferred := catalog.get_npc(preferred_id)
|
||||
if not preferred.is_empty():
|
||||
return preferred
|
||||
var available := npcs(catalog)
|
||||
return available[0] if not available.is_empty() else {}
|
||||
|
||||
static func event_for_role(catalog: ContentCatalog, role: StringName) -> Dictionary:
|
||||
var preferred_id := {
|
||||
&"narrative": &"event_narrative_note",
|
||||
&"mystery": &"event_vending_machine",
|
||||
&"puzzle": &"event_puzzle_pressure_plates",
|
||||
}.get(role, &"")
|
||||
if not preferred_id.is_empty():
|
||||
var preferred := catalog.get_event(preferred_id)
|
||||
if not preferred.is_empty():
|
||||
return preferred
|
||||
var available := events(catalog)
|
||||
return available[0] if not available.is_empty() else {}
|
||||
|
||||
static func _definitions(catalog: ContentCatalog, kind: StringName) -> Array[Dictionary]:
|
||||
var definitions: Array[Dictionary] = []
|
||||
for id in catalog.get_ids(kind):
|
||||
match kind:
|
||||
&"loot_table": definitions.append(catalog.get_loot_table(id))
|
||||
&"npc": definitions.append(catalog.get_npc(id))
|
||||
&"trap": definitions.append(catalog.get_trap(id))
|
||||
&"event": definitions.append(catalog.get_event(id))
|
||||
return definitions
|
||||
@@ -0,0 +1 @@
|
||||
uid://can7v1ir5w5un
|
||||
@@ -7,9 +7,11 @@ const SUPPORTED_CATALOGS := {
|
||||
"spells.json": "spell",
|
||||
"loot_tables.json": "loot_table",
|
||||
"npcs.json": "npc",
|
||||
"traps.json": "trap",
|
||||
}
|
||||
const SUPPORTED_DIRECTORY_CATALOGS := {
|
||||
"quests": "quest",
|
||||
"events": "event",
|
||||
}
|
||||
|
||||
static func load_directory(path: String) -> Dictionary:
|
||||
|
||||
@@ -8,7 +8,9 @@ const SUPPORTED_CATALOGS := {
|
||||
"spells.json": "spell",
|
||||
"loot_tables.json": "loot_table",
|
||||
"npcs.json": "npc",
|
||||
"traps.json": "trap",
|
||||
"quests": "quest",
|
||||
"events": "event",
|
||||
}
|
||||
|
||||
func test_every_supported_ascii_id_has_exactly_one_catalog_entry() -> void:
|
||||
@@ -23,7 +25,7 @@ func test_every_supported_ascii_id_has_exactly_one_catalog_entry() -> void:
|
||||
|
||||
func _source_ids(source_path: String) -> Array[StringName]:
|
||||
var ids: Array[StringName] = []
|
||||
if source_path == "quests":
|
||||
if source_path in ["quests", "events"]:
|
||||
var directory := DirAccess.open(ASCII_DATA_PATH.path_join(source_path))
|
||||
directory.list_dir_begin()
|
||||
var filename := directory.get_next()
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
extends GutTest
|
||||
|
||||
const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd")
|
||||
const FloorGeneratorType = preload("res://scripts/domain/world/floor_generator.gd")
|
||||
const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
|
||||
|
||||
const ASCII_CATALOG_PATH := "res://data/ascii"
|
||||
const CARDINAL_DIRECTIONS := [Vector2i.LEFT, Vector2i.RIGHT, Vector2i.UP, Vector2i.DOWN]
|
||||
|
||||
func test_one_hundred_seeds_have_safe_catalog_backed_reachable_population() -> void:
|
||||
var catalog := ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH)
|
||||
assert_eq(Array(catalog.get_diagnostics()), [])
|
||||
|
||||
for seed_index in range(100):
|
||||
var floor := FloorGeneratorType.generate("B4-safety-%03d" % seed_index, 1 + seed_index % 10, catalog)
|
||||
var reachable := _reachable_cells_treating_closed_doors_openable(floor.map, floor.entrance)
|
||||
var occupied: Dictionary[Vector2i, bool] = {}
|
||||
|
||||
assert_false(_has_blocking_occupant_at(floor, floor.entrance), "seed %d keeps entrance clear" % seed_index)
|
||||
assert_false(_has_blocking_occupant_at(floor, floor.exit), "seed %d keeps exit clear" % seed_index)
|
||||
assert_true(reachable.has(floor.exit), "seed %d keeps exit reachable" % seed_index)
|
||||
|
||||
for enemy in floor.enemies.values():
|
||||
assert_ne(catalog.get_enemy(enemy.content_id), {}, "seed %d enemy is catalog backed" % seed_index)
|
||||
assert_lte(int(catalog.get_enemy(enemy.content_id).get("min_floor", 1)), floor.floor_number)
|
||||
_assert_valid_unique_reachable_cell(floor, enemy.cell, occupied, reachable, seed_index)
|
||||
for npc in floor.npcs.values():
|
||||
assert_ne(catalog.get_npc(npc.content_id), {}, "seed %d NPC is catalog backed" % seed_index)
|
||||
_assert_valid_unique_reachable_cell(floor, npc.cell, occupied, reachable, seed_index)
|
||||
for cell in floor.chests:
|
||||
assert_ne(catalog.get_loot_table(StringName(floor.chests[cell]["loot_table_id"])), {})
|
||||
_assert_valid_unique_reachable_cell(floor, cell, occupied, reachable, seed_index)
|
||||
for cell in floor.traps:
|
||||
assert_ne(catalog.get_trap(StringName(floor.traps[cell]["trap_id"])), {})
|
||||
_assert_valid_unique_reachable_cell(floor, cell, occupied, reachable, seed_index)
|
||||
for event in floor.events:
|
||||
assert_ne(catalog.get_event(StringName(event["event_id"])), {})
|
||||
var cell_data: Array = event["cell"]
|
||||
_assert_valid_unique_reachable_cell(floor, Vector2i(int(cell_data[0]), int(cell_data[1])), occupied, reachable, seed_index)
|
||||
|
||||
func _assert_valid_unique_reachable_cell(floor: FloorState, cell: Vector2i, occupied: Dictionary[Vector2i, bool], reachable: Dictionary[Vector2i, bool], seed_index: int) -> void:
|
||||
assert_false(occupied.has(cell), "seed %d has no overlapping population" % seed_index)
|
||||
occupied[cell] = true
|
||||
assert_ne(cell, floor.entrance)
|
||||
assert_ne(cell, floor.exit)
|
||||
assert_true(reachable.has(cell), "seed %d population is reachable" % seed_index)
|
||||
assert_eq(floor.map.get_cell(cell), GridMapStateType.CellKind.FLOOR)
|
||||
|
||||
func _has_blocking_occupant_at(floor: FloorState, cell: Vector2i) -> bool:
|
||||
for actor in floor.enemies.values() + floor.npcs.values():
|
||||
if actor.cell == cell:
|
||||
return true
|
||||
return floor.chests.has(cell) or floor.traps.has(cell)
|
||||
|
||||
func _reachable_cells_treating_closed_doors_openable(map: GridMapState, start: Vector2i) -> Dictionary[Vector2i, bool]:
|
||||
var reachable: Dictionary[Vector2i, bool] = {start: true}
|
||||
var frontier: Array[Vector2i] = [start]
|
||||
while not frontier.is_empty():
|
||||
var current := frontier.pop_front()
|
||||
for direction in CARDINAL_DIRECTIONS:
|
||||
var next_cell: Vector2i = current + direction
|
||||
if (map.is_walkable(next_cell) or map.get_cell(next_cell) == GridMapStateType.CellKind.CLOSED_DOOR) and not reachable.has(next_cell):
|
||||
reachable[next_cell] = true
|
||||
frontier.append(next_cell)
|
||||
return reachable
|
||||
@@ -0,0 +1 @@
|
||||
uid://dfpcbrduher8h
|
||||
@@ -12,6 +12,12 @@ func test_item_fixture_preserves_rusty_sword_fields() -> void:
|
||||
assert_eq(sword.get("rarity"), "commun")
|
||||
assert_eq(sword.get("damage"), 6)
|
||||
|
||||
func test_catalog_exposes_ascii_traps_and_events() -> void:
|
||||
var catalog := ContentCatalog.load_from_directory("res://data/ascii")
|
||||
|
||||
assert_ne(catalog.get_trap(&"trap_spikes"), {})
|
||||
assert_ne(catalog.get_event(&"event_narrative_note"), {})
|
||||
|
||||
func test_missing_required_id_records_explicit_diagnostic() -> void:
|
||||
var catalog := ContentCatalog.load_from_directory("res://tests/fixtures/content_loader_missing_id")
|
||||
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
extends GutTest
|
||||
|
||||
const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd")
|
||||
const FloorGeneratorType = preload("res://scripts/domain/world/floor_generator.gd")
|
||||
const FloorPopulatorType = preload("res://scripts/domain/world/floor_populator.gd")
|
||||
const SpawnRulesType = preload("res://scripts/domain/world/spawn_rules.gd")
|
||||
const FloorStateType = preload("res://scripts/domain/world/floor_state.gd")
|
||||
const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
|
||||
const RoomStateType = preload("res://scripts/domain/world/room_state.gd")
|
||||
|
||||
const ASCII_CATALOG_PATH := "res://data/ascii"
|
||||
const ROLE_KINDS := [&"entrance", &"combat", &"boss", &"arena", &"treasure", &"trap", &"shop", &"guild", &"sanctuary", &"refuge", &"narrative", &"mystery", &"puzzle"]
|
||||
|
||||
func test_population_is_deterministic_for_an_identical_seed_and_catalog() -> void:
|
||||
var catalog := ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH)
|
||||
var first := FloorGeneratorType.generate("B4-identical", 3, catalog)
|
||||
var second := FloorGeneratorType.generate("B4-identical", 3, catalog)
|
||||
|
||||
assert_eq(first.to_dict(), second.to_dict())
|
||||
|
||||
func test_population_varies_with_the_seed() -> void:
|
||||
var catalog := ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH)
|
||||
var first := FloorGeneratorType.generate("B4-variation-a", 3, catalog)
|
||||
var second := FloorGeneratorType.generate("B4-variation-b", 3, catalog)
|
||||
|
||||
assert_ne(_population_snapshot(first), _population_snapshot(second))
|
||||
|
||||
func test_spawn_rules_filter_enemies_by_minimum_floor() -> void:
|
||||
var catalog := ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH)
|
||||
var first_floor := SpawnRulesType.eligible_enemies(catalog, 1)
|
||||
var late_floor := SpawnRulesType.eligible_enemies(catalog, 20)
|
||||
|
||||
assert_gt(first_floor.size(), 0)
|
||||
assert_gt(late_floor.size(), first_floor.size())
|
||||
for definition: Dictionary in first_floor:
|
||||
assert_lte(int(definition.get("min_floor", 1)), 1)
|
||||
|
||||
func test_role_population_uses_catalog_content_and_respects_reserved_cells() -> void:
|
||||
var catalog := ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH)
|
||||
var floor := _role_fixture_floor()
|
||||
|
||||
FloorPopulatorType.populate(floor, catalog, "B4-role-contract")
|
||||
|
||||
assert_eq(floor.enemies.size(), 3, "combat, boss, and arena rooms receive enemies")
|
||||
assert_eq(floor.chests.size(), 1, "treasure rooms receive a chest")
|
||||
assert_eq(floor.traps.size(), 1, "trap rooms receive a trap")
|
||||
assert_eq(floor.npcs.size(), 4, "shop, guild, sanctuary, and refuge rooms receive NPC records")
|
||||
assert_eq(floor.events.size(), 3, "narrative, mystery, and puzzle rooms receive event records")
|
||||
assert_eq(_npc_content_ids(floor, &"shop"), [&"npc_wandering_merchant"])
|
||||
assert_eq(_npc_content_ids(floor, &"guild"), [&"npc_guild_keeper"])
|
||||
_assert_all_records_are_catalog_backed(catalog, floor)
|
||||
_assert_reserved_cells_are_clear_and_all_occupants_are_distinct(floor)
|
||||
|
||||
func _role_fixture_floor() -> FloorState:
|
||||
var map := GridMapStateType.new(Vector2i(96, 12), GridMapStateType.CellKind.WALL)
|
||||
var floor := FloorStateType.new(map, 44, 3)
|
||||
for index in range(ROLE_KINDS.size()):
|
||||
var bounds := Rect2i(Vector2i(1 + index * 7, 3), Vector2i(5, 5))
|
||||
var room := RoomStateType.new(StringName("room_%02d" % index), bounds, ROLE_KINDS[index])
|
||||
floor.rooms.append(room)
|
||||
for y in range(bounds.position.y, bounds.end.y):
|
||||
for x in range(bounds.position.x, bounds.end.x):
|
||||
var cell := Vector2i(x, y)
|
||||
map.set_cell(cell, GridMapStateType.CellKind.FLOOR)
|
||||
map.set_room_id(cell, room.id)
|
||||
floor.entrance = _room_center(floor.rooms[0].bounds)
|
||||
floor.exit = _room_center(floor.rooms[1].bounds)
|
||||
map.set_cell(floor.exit, GridMapStateType.CellKind.STAIRS)
|
||||
return floor
|
||||
|
||||
func _population_snapshot(floor: FloorState) -> Dictionary:
|
||||
return {
|
||||
"enemies": floor.to_dict()["enemies"],
|
||||
"npcs": floor.to_dict()["npcs"],
|
||||
"chests": floor.to_dict()["chests"],
|
||||
"traps": floor.to_dict()["traps"],
|
||||
"events": floor.events,
|
||||
}
|
||||
|
||||
func _npc_content_ids(floor: FloorState, room_kind: StringName) -> Array[StringName]:
|
||||
var ids: Array[StringName] = []
|
||||
for npc in floor.npcs.values():
|
||||
if _room_for_cell(floor, npc.cell).kind == room_kind:
|
||||
ids.append(npc.content_id)
|
||||
ids.sort()
|
||||
return ids
|
||||
|
||||
func _assert_all_records_are_catalog_backed(catalog: ContentCatalog, floor: FloorState) -> void:
|
||||
for enemy in floor.enemies.values():
|
||||
assert_ne(catalog.get_enemy(enemy.content_id), {})
|
||||
assert_lte(int(catalog.get_enemy(enemy.content_id).get("min_floor", 1)), floor.floor_number)
|
||||
for npc in floor.npcs.values():
|
||||
assert_ne(catalog.get_npc(npc.content_id), {})
|
||||
for chest in floor.chests.values():
|
||||
assert_ne(catalog.get_loot_table(StringName(chest["loot_table_id"])), {})
|
||||
for trap in floor.traps.values():
|
||||
assert_ne(catalog.get_trap(StringName(trap["trap_id"])), {})
|
||||
for event in floor.events:
|
||||
assert_ne(catalog.get_event(StringName(event["event_id"])), {})
|
||||
|
||||
func _assert_reserved_cells_are_clear_and_all_occupants_are_distinct(floor: FloorState) -> void:
|
||||
var occupied: Dictionary[Vector2i, bool] = {}
|
||||
for actor in floor.enemies.values() + floor.npcs.values():
|
||||
assert_false(occupied.has(actor.cell), "actor cells do not overlap")
|
||||
occupied[actor.cell] = true
|
||||
for cell in floor.chests:
|
||||
assert_false(occupied.has(cell), "chests do not overlap actors or each other")
|
||||
occupied[cell] = true
|
||||
for cell in floor.traps:
|
||||
assert_false(occupied.has(cell), "traps do not overlap any other occupant")
|
||||
occupied[cell] = true
|
||||
for event in floor.events:
|
||||
var cell := _event_cell(event)
|
||||
assert_false(occupied.has(cell), "events do not overlap any other occupant")
|
||||
occupied[cell] = true
|
||||
assert_false(occupied.has(floor.entrance))
|
||||
assert_false(occupied.has(floor.exit))
|
||||
|
||||
func _event_cell(event: Dictionary) -> Vector2i:
|
||||
var serialized: Array = event["cell"]
|
||||
return Vector2i(int(serialized[0]), int(serialized[1]))
|
||||
|
||||
func _room_for_cell(floor: FloorState, cell: Vector2i) -> RoomState:
|
||||
var room_id := floor.map.get_room_id(cell)
|
||||
for room in floor.rooms:
|
||||
if room.id == room_id:
|
||||
return room
|
||||
return null
|
||||
|
||||
func _room_center(bounds: Rect2i) -> Vector2i:
|
||||
return bounds.position + Vector2i(bounds.size.x / 2, bounds.size.y / 2)
|
||||
@@ -0,0 +1 @@
|
||||
uid://dnrqxd18bgmdm
|
||||
@@ -18,9 +18,10 @@ func test_floor_state_round_trip_preserves_rooms_tiles_and_floor_collections() -
|
||||
floor.rooms.append(RoomStateType.new(&"exit", Rect2i(5, 3, 2, 2), &"combat"))
|
||||
floor.entrance = Vector2i(2, 2)
|
||||
floor.exit = Vector2i(6, 4)
|
||||
floor.enemies[&"goblin"] = ActorStateType.new(&"goblin", Vector2i(5, 4), 6, 2)
|
||||
floor.npcs[&"guide"] = ActorStateType.new(&"guide", Vector2i(3, 3), 9, 0)
|
||||
floor.enemies[&"goblin"] = ActorStateType.new(&"goblin", Vector2i(5, 4), 6, 2, &"enemy_goblin")
|
||||
floor.npcs[&"guide"] = ActorStateType.new(&"guide", Vector2i(3, 3), 9, 0, &"npc_guide_mordecai")
|
||||
floor.chests[Vector2i(3, 2)] = {"opened": false, "loot": [{"id": "potion_minor", "quantity": 2}]}
|
||||
floor.traps[Vector2i(3, 3)] = {"trap_id": "trap_spikes"}
|
||||
floor.ground_items[Vector2i(4, 2)] = [{"id": "gold", "quantity": 10}]
|
||||
floor.events.append({"id": "entry_event", "resolved": false})
|
||||
floor.explored_cells[Vector2i(2, 2)] = true
|
||||
@@ -45,10 +46,13 @@ func test_floor_state_round_trip_preserves_rooms_tiles_and_floor_collections() -
|
||||
assert_eq(restored.entrance, Vector2i(2, 2))
|
||||
assert_eq(restored.exit, Vector2i(6, 4))
|
||||
assert_eq(restored.enemies[&"goblin"].cell, Vector2i(5, 4))
|
||||
assert_eq(restored.enemies[&"goblin"].content_id, &"enemy_goblin")
|
||||
assert_eq(restored.npcs[&"guide"].cell, Vector2i(3, 3))
|
||||
assert_eq(restored.npcs[&"guide"].content_id, &"npc_guide_mordecai")
|
||||
assert_true(restored.entities.has(&"goblin"))
|
||||
assert_false(restored.entities.has(&"guide"))
|
||||
assert_eq(restored.chests[Vector2i(3, 2)], {"opened": false, "loot": [{"id": "potion_minor", "quantity": 2}]})
|
||||
assert_eq(restored.traps[Vector2i(3, 3)], {"trap_id": "trap_spikes"})
|
||||
assert_eq(restored.ground_items[Vector2i(4, 2)], [{"id": "gold", "quantity": 10}])
|
||||
assert_eq(restored.events, [{"id": "entry_event", "resolved": false}])
|
||||
assert_eq(restored.explored_cells[Vector2i(2, 2)], true)
|
||||
|
||||
Reference in New Issue
Block a user