use crate::debug::DebugBundle;
use crate::digest::ConfigDigest;
use crate::model::chunk::Chunk;
use crate::model::room::{Corridor, DoorAnchor, Room, RoomGraph};
use crate::model::spawn::SpawnPoint;
use crate::model::terrain::Terrain;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct GenerationResult {
pub metadata: ResultMetadata,
pub topology: RoomGraph,
pub rooms: Vec<Room>,
pub door_anchors: Vec<DoorAnchor>,
pub corridors: Vec<Corridor>,
pub terrains: Vec<Terrain>,
pub item_spawns: Vec<SpawnPoint>,
pub enemy_spawns: Vec<SpawnPoint>,
pub chunks: Vec<Chunk>,
pub debug: Option<DebugBundle>,
}
impl GenerationResult {
pub(crate) fn validate_terrain_data(&self) -> crate::error::PcgResult<()> {
use crate::error::PcgError;
use std::collections::HashMap;
let rooms: HashMap<_, _> = self
.rooms
.iter()
.map(|room| (room.id.as_str(), room))
.collect();
let mut total_cells = 0u64;
for terrain in &self.terrains {
terrain.tiles.validate()?;
if terrain.grid_size.width != terrain.tiles.width
|| terrain.grid_size.height != terrain.tiles.height
{
return Err(PcgError::terrain("地形尺寸与瓦片网格不一致"));
}
total_cells += terrain.tiles.data.len() as u64;
if total_cells > 64_000_000 {
return Err(PcgError::terrain("地形总格子数超出生成预算"));
}
let bounds = rooms
.get(terrain.room_id.as_str())
.and_then(|room| room.bounds)
.ok_or_else(|| PcgError::terrain("地形缺少对应的已布局房间"))?;
if i64::from(bounds.max.x) - i64::from(bounds.min.x) != i64::from(terrain.tiles.width)
|| i64::from(bounds.max.y) - i64::from(bounds.min.y)
!= i64::from(terrain.tiles.height)
{
return Err(PcgError::terrain("房间边界与地形尺寸不一致"));
}
}
Ok(())
}
pub fn room_by_id(&self, id: &str) -> Option<&Room> {
self.rooms.iter().find(|r| r.id == id)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ResultMetadata {
pub seed: u64,
pub config_digest: String,
pub schema_version: String,
pub algorithm_version: String,
pub target_engine_version: Option<String>,
pub trace_id: Option<String>,
}
impl ResultMetadata {
pub fn config_digest_typed(&self) -> ConfigDigest {
ConfigDigest::from_string(self.config_digest.clone())
}
}