concinnity-world 0.18.64

Authored world source, args schema, validation, and spec builders for Concinnity
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! Physics authoring checks: collider shape strings, and the collision-layer
//! names a world's PhysicsConfig declares against every reference to them.
//! Also the authored shape the build sizes a world's spawn reservation from:
//! which of its assets can create collider-bearing props while it runs.

use crate::world::WorldJsonlAsset;

const COLLIDER_SHAPES: [&str; 5] = ["aabb", "cuboid", "ball", "sphere", "capsule"];
const BUILTIN_LAYERS: [&str; 4] = ["world", "prop", "character", "trigger"];
// An interaction group is 32 bits: the built-ins plus at most 28 more.
const MAX_USER_LAYERS: usize = 32 - BUILTIN_LAYERS.len();

// A `collider` object's `shape`, when present, must be a recognized shape
// name. Shared by the Prop and TriggerVolume checks.
pub(crate) fn check_collider_shape(name: &str, args: &serde_json::Value) -> Result<(), String> {
    let Some(shape) = args
        .get("collider")
        .and_then(|c| c.get("shape"))
        .and_then(|s| s.as_str())
    else {
        return Ok(());
    };
    if COLLIDER_SHAPES.contains(&shape) {
        return Ok(());
    }
    Err(format!(
        "Asset '{}': unknown collider shape '{}'; expected one of {}",
        name,
        shape,
        COLLIDER_SHAPES.join(", ")
    ))
}

/// Check a physics asset's authored args.
pub fn check(name: &str, args: &serde_json::Value) -> Result<(), String> {
    check_collider_shape(name, args)
}

// World-shape rule: every layer name a world uses must resolve. Declared
// names must be unique, not shadow a built-in, and fit the 32-bit group
// space; `no_collide` pairs and collider `layer` fields must name a declared
// or built-in layer.
pub(crate) fn check_layers(assets: &[WorldJsonlAsset], errors: &mut Vec<String>) {
    let norm = |t: &str| t.to_lowercase().replace('_', "");
    let config = assets
        .iter()
        .find(|a| norm(&a.asset_type) == "physicsconfig");

    let mut known: Vec<String> = BUILTIN_LAYERS.iter().map(|s| s.to_string()).collect();
    if let Some(config) = config {
        let declared: Vec<&str> = config
            .args
            .get("layers")
            .and_then(|v| v.as_array())
            .map(|list| list.iter().filter_map(|v| v.as_str()).collect())
            .unwrap_or_default();
        if declared.len() > MAX_USER_LAYERS {
            errors.push(format!(
                "PhysicsConfig '{}': {} extra layers declared; at most {} fit the 32-bit group space",
                config.name,
                declared.len(),
                MAX_USER_LAYERS
            ));
        }
        for layer in declared {
            if layer.is_empty() {
                errors.push(format!("PhysicsConfig '{}': empty layer name", config.name));
            } else if known.iter().any(|k| k == layer) {
                errors.push(format!(
                    "PhysicsConfig '{}': layer '{}' is already defined",
                    config.name, layer
                ));
            } else {
                known.push(layer.to_string());
            }
        }
        if let Some(pairs) = config.args.get("no_collide").and_then(|v| v.as_array()) {
            for pair in pairs {
                let names = pair
                    .as_array()
                    .map(|p| p.iter().filter_map(|v| v.as_str()).collect::<Vec<&str>>());
                let Some(names) = names.filter(|n| n.len() == 2) else {
                    errors.push(format!(
                        "PhysicsConfig '{}': no_collide entries are [\"layer_a\", \"layer_b\"] pairs",
                        config.name
                    ));
                    continue;
                };
                for layer in names {
                    if !known.iter().any(|k| k == layer) {
                        errors.push(format!(
                            "PhysicsConfig '{}': no_collide names unknown layer '{}'",
                            config.name, layer
                        ));
                    }
                }
            }
        }
        if let Some(min) = config
            .args
            .get("contact_min_impulse")
            .and_then(|v| v.as_f64())
            && min < 0.0
        {
            errors.push(format!(
                "PhysicsConfig '{}': contact_min_impulse must not be negative",
                config.name
            ));
        }
    }

    for asset in assets.iter().filter(|a| norm(&a.asset_type) == "prop") {
        let Some(layer) = asset
            .args
            .get("collider")
            .and_then(|c| c.get("layer"))
            .and_then(|l| l.as_str())
            .filter(|l| !l.is_empty())
        else {
            continue;
        };
        if !known.iter().any(|k| k == layer) {
            errors.push(format!(
                "Prop '{}': collider layer '{}' is not a built-in layer or declared in PhysicsConfig `layers`",
                asset.name, layer
            ));
        }
    }
}

/// A `Spawner` whose template names a `Prop` that carries a collider.
///
/// The cadence fields are what decide how many copies can be alive at once,
/// which is what the build reserves physics bodies against.
#[derive(Debug, Clone, PartialEq)]
pub struct ColliderSpawner<'a> {
    /// Name of the spawner asset.
    pub name: &'a str,
    /// Seconds between spawns.
    pub interval: f32,
    /// Seconds each copy lives before auto-removal; 0 keeps it forever.
    pub lifetime: f32,
}

/// Every authored way a world creates collider-bearing props while it runs.
///
/// A `Spawner` over a `SkinnedMesh` is not here: a skinned copy claims a
/// pre-reserved render instance and carries no collider, so it never reaches
/// physics.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ColliderSpawnSources<'a> {
    /// Spawners copying a collider-bearing prop on a fixed cadence.
    pub spawners: Vec<ColliderSpawner<'a>>,
    /// Names of the `Behavior`s with a `spawn` node over such a prop.
    pub behaviors: Vec<&'a str>,
}

/// Collect the world's runtime sources of collider-bearing props.
///
/// Every copy such a source emits needs a physics body the authored content
/// did not account for. The build reserves for the ones whose cadence bounds
/// their population and warns about the rest, because the simulation refuses
/// bodies past its reservation.
pub fn collider_spawn_sources(assets: &[WorldJsonlAsset]) -> ColliderSpawnSources<'_> {
    let norm = |t: &str| t.to_lowercase().replace('_', "");
    let collider_props: Vec<&str> = assets
        .iter()
        .filter(|a| norm(&a.asset_type) == "prop")
        .filter(|a| a.args.get("collider").is_some_and(|c| !c.is_null()))
        .map(|a| a.name.as_str())
        .collect();
    let mut sources = ColliderSpawnSources::default();
    if collider_props.is_empty() {
        return sources;
    }
    let is_collider_prop =
        |name: &serde_json::Value| name.as_str().is_some_and(|n| collider_props.contains(&n));

    let defaults = concinnity_asset::cook::Spawner::default();
    for asset in assets {
        match norm(&asset.asset_type).as_str() {
            "spawner" if asset.args.get("template").is_some_and(is_collider_prop) => {
                let secs = |field: &str, fallback: f32| {
                    asset
                        .args
                        .get(field)
                        .and_then(|v| v.as_f64())
                        .map_or(fallback, |v| v as f32)
                };
                sources.spawners.push(ColliderSpawner {
                    name: &asset.name,
                    interval: secs("interval", defaults.interval),
                    lifetime: secs("lifetime", defaults.lifetime),
                });
            }
            "behavior"
                if spawn_templates(&asset.args)
                    .iter()
                    .any(|t| is_collider_prop(t)) =>
            {
                sources.behaviors.push(&asset.name);
            }
            _ => {}
        }
    }
    sources
}

// Every `template` value of a `spawn` node anywhere in a behavior's args.
// Nodes nest (inside `if` branches, `repeat` bodies), so the walk is
// structural rather than a fixed path.
fn spawn_templates(args: &serde_json::Value) -> Vec<&serde_json::Value> {
    let mut out = Vec::new();
    let mut stack = vec![args];
    while let Some(value) = stack.pop() {
        match value {
            serde_json::Value::Object(map) => {
                for (key, child) in map {
                    if key == "spawn"
                        && let Some(template) = child.get("template")
                    {
                        out.push(template);
                    }
                    stack.push(child);
                }
            }
            serde_json::Value::Array(items) => stack.extend(items),
            _ => {}
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    fn asset(name: &str, asset_type: &str, args: serde_json::Value) -> WorldJsonlAsset {
        WorldJsonlAsset {
            name: name.to_string(),
            asset_type: asset_type.to_string(),
            args,
        }
    }

    fn layer_errors(assets: &[WorldJsonlAsset]) -> Vec<String> {
        let mut errors = Vec::new();
        check_layers(assets, &mut errors);
        errors
    }

    #[test]
    fn known_shapes_pass_and_unknown_shapes_fail() {
        let ok = serde_json::json!({"collider": {"shape": "capsule"}});
        assert!(check_collider_shape("p", &ok).is_ok());
        // No collider or no shape field: nothing to judge.
        assert!(check_collider_shape("p", &serde_json::json!({})).is_ok());
        let err = check_collider_shape("p", &serde_json::json!({"collider": {"shape": "mesh"}}))
            .unwrap_err();
        assert!(err.contains("unknown collider shape 'mesh'"), "got: {err}");
    }

    #[test]
    fn declared_layers_resolve_everywhere() {
        let assets = [
            asset(
                "physics",
                "PhysicsConfig",
                serde_json::json!({"layers": ["debris"], "no_collide": [["debris", "character"]]}),
            ),
            asset(
                "rock",
                "Prop",
                serde_json::json!({"mesh": "m", "collider": {"shape": "ball", "layer": "debris"}}),
            ),
        ];
        assert!(layer_errors(&assets).is_empty());
    }

    #[test]
    fn unknown_layer_references_fail() {
        let assets = [
            asset(
                "physics",
                "PhysicsConfig",
                serde_json::json!({"no_collide": [["ghost", "world"]]}),
            ),
            asset(
                "rock",
                "Prop",
                serde_json::json!({"mesh": "m", "collider": {"shape": "ball", "layer": "ghost"}}),
            ),
        ];
        let errors = layer_errors(&assets);
        assert_eq!(errors.len(), 2, "{errors:?}");
        assert!(errors[0].contains("no_collide names unknown layer 'ghost'"));
        assert!(errors[1].contains("collider layer 'ghost'"));
    }

    #[test]
    fn prop_layers_work_without_a_physics_config() {
        // Built-ins resolve with no config declared; unknown names still fail.
        let ok = [asset(
            "rock",
            "Prop",
            serde_json::json!({"mesh": "m", "collider": {"shape": "ball", "layer": "prop"}}),
        )];
        assert!(layer_errors(&ok).is_empty());
        let bad = [asset(
            "rock",
            "Prop",
            serde_json::json!({"mesh": "m", "collider": {"shape": "ball", "layer": "ghost"}}),
        )];
        assert_eq!(layer_errors(&bad).len(), 1);
    }

    #[test]
    fn duplicate_and_overflowing_layer_declarations_fail() {
        let dup = [asset(
            "physics",
            "PhysicsConfig",
            serde_json::json!({"layers": ["debris", "debris", "world"]}),
        )];
        let errors = layer_errors(&dup);
        assert_eq!(errors.len(), 2, "{errors:?}");
        assert!(errors.iter().all(|e| e.contains("already defined")));

        let many: Vec<String> = (0..29).map(|i| format!("layer{i}")).collect();
        let over = [asset(
            "physics",
            "PhysicsConfig",
            serde_json::json!({"layers": many}),
        )];
        let errors = layer_errors(&over);
        assert_eq!(errors.len(), 1, "{errors:?}");
        assert!(errors[0].contains("32-bit group space"));
    }

    #[test]
    fn a_spawner_naming_a_collider_prop_can_spawn_bodies() {
        let crate_ = [
            asset(
                "crate",
                "Prop",
                serde_json::json!({"mesh": "m", "collider": {"shape": "ball"}}),
            ),
            asset(
                "drop",
                "Spawner",
                serde_json::json!({"template": "crate", "interval": 2.0, "lifetime": 4.0}),
            ),
        ];
        let sources = collider_spawn_sources(&crate_);
        assert_eq!(
            sources.spawners,
            vec![ColliderSpawner {
                name: "drop",
                interval: 2.0,
                lifetime: 4.0,
            }]
        );
        assert!(sources.behaviors.is_empty());

        // The same spawner over a prop with no collider spawns nothing the
        // simulation has to hold.
        let bare = [
            asset("banner", "Prop", serde_json::json!({"mesh": "m"})),
            asset("drop", "Spawner", serde_json::json!({"template": "banner"})),
        ];
        assert_eq!(
            collider_spawn_sources(&bare),
            ColliderSpawnSources::default()
        );
    }

    // An unauthored cadence is the authored Spawner default: one a second, and
    // copies that are never removed.
    #[test]
    fn an_unauthored_cadence_reads_back_as_the_spawner_default() {
        let assets = [
            asset(
                "crate",
                "Prop",
                serde_json::json!({"mesh": "m", "collider": {"shape": "ball"}}),
            ),
            asset("drop", "Spawner", serde_json::json!({"template": "crate"})),
        ];
        let defaults = concinnity_asset::cook::Spawner::default();
        let spawner = &collider_spawn_sources(&assets).spawners[0];
        assert_eq!(spawner.interval, defaults.interval);
        assert_eq!(spawner.lifetime, defaults.lifetime);
    }

    // An explicit null collider is not a collider: the load-time decomposition
    // gives such a prop no Collider component, so no spawn of it needs a body.
    #[test]
    fn an_explicitly_null_collider_is_not_a_source() {
        let assets = [
            asset(
                "banner",
                "Prop",
                serde_json::json!({"mesh": "m", "collider": null}),
            ),
            asset("drop", "Spawner", serde_json::json!({"template": "banner"})),
        ];
        assert_eq!(
            collider_spawn_sources(&assets),
            ColliderSpawnSources::default()
        );
    }

    #[test]
    fn a_nested_behavior_spawn_node_counts_too() {
        let assets = [
            asset(
                "crate",
                "Prop",
                serde_json::json!({"mesh": "m", "collider": {"shape": "ball"}}),
            ),
            asset(
                "thrower",
                "Behavior",
                serde_json::json!({"on": "tick", "do": [
                    {"if": {"cond": "ready", "then": [{"spawn": {"template": "crate"}}]}}
                ]}),
            ),
        ];
        let sources = collider_spawn_sources(&assets);
        assert_eq!(sources.behaviors, vec!["thrower"], "a nested node spawns");
        assert!(sources.spawners.is_empty());

        // No spawn source at all: the world only ever holds what it declares.
        let quiet = [asset(
            "crate",
            "Prop",
            serde_json::json!({"mesh": "m", "collider": {"shape": "ball"}}),
        )];
        assert_eq!(
            collider_spawn_sources(&quiet),
            ColliderSpawnSources::default()
        );
    }

    #[test]
    fn malformed_no_collide_and_negative_impulse_fail() {
        let assets = [asset(
            "physics",
            "PhysicsConfig",
            serde_json::json!({"no_collide": [["world"]], "contact_min_impulse": -1.0}),
        )];
        let errors = layer_errors(&assets);
        assert_eq!(errors.len(), 2, "{errors:?}");
        assert!(errors[0].contains("pairs"));
        assert!(errors[1].contains("must not be negative"));
    }
}