{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/Living-Proof-Labs/livingproof-worlds/schema/world.schema.json",
  "title": "Living Proof Labs world, formats livingproof-world/0.1 and livingproof-world/0.2",
  "description": "Engine-neutral static world. Every object is closed: unknown fields, geometry types and place types are rejected, never ignored. livingproof-world/0.1 is a straight hallway along x and declares length and exterior_width. livingproof-world/0.2 is any layout of axis-aligned box floors and walls, with named places and a route polyline named route-centreline (robot-capability-specs world contract 0.3.0, section Any layout); it declares neither length nor exterior_width.",
  "type": "object",
  "additionalProperties": false,
  "required": ["format", "id", "content_version", "title", "description", "specification", "units", "frame", "dimensions", "geometry", "places", "tolerances", "provenance", "license"],
  "allOf": [
    {"if": {"required": ["format"], "properties": {"format": {"const": "livingproof-world/0.1"}}}, "then": {"properties": {"dimensions": {"required": ["length", "exterior_width"]}}}},
    {"if": {"required": ["format"], "properties": {"format": {"const": "livingproof-world/0.2"}}}, "then": {"properties": {"dimensions": {"not": {"anyOf": [{"required": ["length"]}, {"required": ["exterior_width"]}]}}, "frame": {"properties": {"axes": {"properties": {"z": {"const": "up"}}}, "pose": {"const": "position (x, y, z) plus yaw in degrees, positive about +z"}}}}}}
  ],
  "$defs": {
    "vec3": {"type": "array", "items": {"type": "number"}, "minItems": 3, "maxItems": 3},
    "interval": {"type": "array", "items": {"type": "number"}, "minItems": 2, "maxItems": 2},
    "positive": {"type": "number", "exclusiveMinimum": 0},
    "name": {"type": "string", "pattern": "^[a-z][a-z0-9-]*$"}
  },
  "properties": {
    "format": {"enum": ["livingproof-world/0.1", "livingproof-world/0.2"]},
    "id": {"$ref": "#/$defs/name"},
    "content_version": {"type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$"},
    "title": {"type": "string", "minLength": 1},
    "description": {"type": "string", "minLength": 1},
    "specification": {
      "type": "object", "additionalProperties": false,
      "required": ["repository", "path", "tag", "commit", "document_revision", "world_contract_revision"],
      "properties": {
        "repository": {"type": "string", "pattern": "^[A-Za-z0-9-]+/[A-Za-z0-9._-]+$"},
        "path": {"type": "string"},
        "tag": {"type": "string", "pattern": "^v[0-9]+\\.[0-9]+\\.[0-9]+$"},
        "commit": {"type": "string", "pattern": "^[0-9a-f]{40}$"},
        "document_revision": {"type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$"},
        "world_contract_revision": {"type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$"}
      }
    },
    "units": {"const": "m"},
    "frame": {
      "type": "object", "additionalProperties": false,
      "required": ["handedness", "origin", "axes", "pose"],
      "properties": {
        "handedness": {"const": "right"},
        "origin": {"type": "string"},
        "axes": {
          "type": "object", "additionalProperties": false, "required": ["x", "y", "z"],
          "properties": {"x": {"type": "string"}, "y": {"type": "string"}, "z": {"type": "string"}}
        },
        "pose": {"type": "string"}
      }
    },
    "dimensions": {
      "type": "object", "additionalProperties": false,
      "required": ["clear_width", "wall_height", "wall_thickness", "floor_thickness"],
      "properties": {
        "clear_width": {"$ref": "#/$defs/positive"},
        "length": {"$ref": "#/$defs/positive"},
        "wall_height": {"$ref": "#/$defs/positive"},
        "wall_thickness": {"$ref": "#/$defs/positive"},
        "exterior_width": {"$ref": "#/$defs/positive"},
        "floor_thickness": {"$ref": "#/$defs/positive"}
      }
    },
    "geometry": {
      "type": "array", "minItems": 1,
      "items": {
        "type": "object", "additionalProperties": false,
        "required": ["name", "type", "role", "centre", "size"],
        "properties": {
          "name": {"$ref": "#/$defs/name"},
          "type": {"enum": ["box"]},
          "role": {"enum": ["floor", "wall"]},
          "centre": {"$ref": "#/$defs/vec3"},
          "size": {"type": "array", "items": {"$ref": "#/$defs/positive"}, "minItems": 3, "maxItems": 3}
        }
      }
    },
    "places": {
      "type": "array", "minItems": 1,
      "items": {
        "type": "object",
        "required": ["name", "type"],
        "properties": {"type": {"enum": ["segment", "region", "polyline", "pose"]}},
        "oneOf": [
          {"additionalProperties": false, "required": ["points", "reference"], "properties": {"name": {"$ref": "#/$defs/name"}, "type": {"const": "segment"}, "points": {"type": "array", "items": {"$ref": "#/$defs/vec3"}, "minItems": 2, "maxItems": 2}, "reference": {"$ref": "#/$defs/vec3"}}},
          {"additionalProperties": false, "required": ["x", "y"], "properties": {"name": {"$ref": "#/$defs/name"}, "type": {"const": "region"}, "x": {"$ref": "#/$defs/interval"}, "y": {"$ref": "#/$defs/interval"}}},
          {"additionalProperties": false, "required": ["points"], "properties": {"name": {"$ref": "#/$defs/name"}, "type": {"const": "polyline"}, "points": {"type": "array", "items": {"$ref": "#/$defs/vec3"}, "minItems": 2}}},
          {"additionalProperties": false, "required": ["position", "yaw_deg"], "properties": {"name": {"$ref": "#/$defs/name"}, "type": {"const": "pose"}, "position": {"$ref": "#/$defs/vec3"}, "yaw_deg": {"type": "number"}}}
        ]
      }
    },
    "tolerances": {
      "type": "object", "additionalProperties": false, "required": ["distance_m", "angle_deg"],
      "properties": {"distance_m": {"$ref": "#/$defs/positive"}, "angle_deg": {"$ref": "#/$defs/positive"}}
    },
    "provenance": {
      "type": "object", "additionalProperties": false, "required": ["authors", "method", "assets"],
      "properties": {
        "authors": {"type": "array", "items": {"type": "string"}, "minItems": 1},
        "method": {"type": "string", "minLength": 1},
        "assets": {
          "type": "array",
          "items": {
            "type": "object", "additionalProperties": false, "required": ["path", "source", "license", "sha256"],
            "properties": {"path": {"type": "string", "minLength": 1}, "source": {"type": "string", "minLength": 1}, "license": {"type": "string", "minLength": 1}, "sha256": {"type": "string", "pattern": "^[0-9a-f]{64}$"}}
          }
        }
      }
    },
    "license": {"type": "string", "minLength": 1}
  }
}
