Added names to objects

This commit is contained in:
2026-06-08 19:50:43 -05:00
parent 04415211c3
commit f5ed8f2edf
7 changed files with 149 additions and 3 deletions
+22
View File
@@ -194,6 +194,9 @@ pub(crate) struct MapFileObjectEntry {
/// Open-ended string labels. Serialized as a TOML array; omitted when empty.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
tags: Vec<String>,
/// Optional unique human-readable name. Omitted from TOML when absent.
#[serde(default, skip_serializing_if = "Option::is_none")]
name: Option<String>,
}
fn default_true() -> bool {
@@ -474,6 +477,8 @@ impl TryFrom<MapFile> for Board {
// load order, preserving the documented "lowest id wins a collision" rule).
let mut objects: BTreeMap<ObjectId, ObjectDef> = BTreeMap::new();
let mut next_object_id: ObjectId = 1;
// Track claimed names so duplicates can be caught and cleared (nonfatal).
let mut seen_names: HashMap<String, usize> = HashMap::new();
for (i, e) in mf.objects.into_iter().enumerate() {
if skip[i] {
continue;
@@ -499,6 +504,21 @@ impl TryFrom<MapFile> for Board {
}
}
};
// Validate name uniqueness: first claimant keeps the name, later ones
// have their name cleared and a nonfatal error is recorded.
let name = e.name.and_then(|n| {
use std::collections::hash_map::Entry;
match seen_names.entry(n.clone()) {
Entry::Vacant(v) => { v.insert(i); Some(n) }
Entry::Occupied(o) => {
load_errors.push(LogLine::error(format!(
"object {i}: name {n:?} already used by object {}; clearing name",
o.get()
)));
None
}
}
});
let obj = ObjectDef {
id: 0, // stamped by Board::add_object
x,
@@ -513,6 +533,7 @@ impl TryFrom<MapFile> for Board {
pushable: e.pushable,
script_name: e.script_name,
tags: e.tags.into_iter().collect(),
name,
};
// A solid object may not share a cell with another solid.
if obj.solid {
@@ -652,6 +673,7 @@ impl From<&Board> for MapFile {
script_name: o.script_name.clone(),
// Sort for stable TOML output; HashSet iteration order is non-deterministic.
tags: { let mut v: Vec<String> = o.tags.iter().cloned().collect(); v.sort(); v },
name: o.name.clone(),
})
.collect();