{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "OpenWorld Format exchange file (.owf)",
  "description": "A whole world as one flat list of objects. See SPEC.md §14.1.",
  "type": "object",
  "required": ["format", "version", "world", "objects"],
  "properties": {
    "$schema": { "type": "string" },
    "format": { "const": "openworld" },
    "version": { "$ref": "#/$defs/version" },
    "world": { "$ref": "#/$defs/world-info" },
    "objects": {
      "type": "array",
      "items": { "$ref": "#/$defs/object", "unevaluatedProperties": false }
    }
  },
  "additionalProperties": false,

  "$defs": {
    "version": { "type": "string", "pattern": "^[0-9]+\\.[0-9]+$" },

    "world-info": {
      "type": "object",
      "required": ["title"],
      "properties": {
        "title": { "type": "string" },
        "system": { "type": "string", "description": "Game system the world is written for. Known values: dnd5e (D&D 5th edition, 2014 rules), dnd5e-2024 (2024 rules). See SPEC.md §5.2." },
        "description": { "$ref": "#/$defs/markdown" },
        "grid": { "$ref": "#/$defs/grid" },
        "orientation": { "$ref": "#/$defs/orientation" }
      },
      "additionalProperties": false
    },

    "meta-file": {
      "description": "Content of world.yaml at the root of a YAML folder. See SPEC.md §14.2.",
      "type": "object",
      "required": ["format", "version", "world"],
      "properties": {
        "$schema": { "type": "string" },
        "format": { "const": "openworld" },
        "version": { "$ref": "#/$defs/version" },
        "world": { "$ref": "#/$defs/world-info" }
      },
      "additionalProperties": false
    },

    "id": {
      "type": "string",
      "pattern": "^[a-z0-9]+(-[a-z0-9]+)*$",
      "description": "Stable unique id: lowercase letters, digits and single hyphens."
    },
    "ids": { "type": "array", "items": { "$ref": "#/$defs/id" } },
    "markdown": { "type": "string", "description": "CommonMark. > [!read] callouts mark read-aloud text." },
    "catalog": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9.-]*:.+$",
      "description": "Catalog hint <catalog>:<entry>, e.g. srd-5.1:dire-wolf."
    },
    "check": {
      "description": "How something is found, opened or resisted. See SPEC.md §4.3.",
      "oneOf": [
        { "type": "string", "minLength": 1 },
        {
          "type": "object",
          "required": ["skill", "dc"],
          "properties": {
            "skill": { "type": "string", "minLength": 1 },
            "dc": { "type": "integer", "minimum": 0 },
            "note": { "type": "string" }
          },
          "additionalProperties": false
        }
      ]
    },
    "visibility": { "enum": ["visible", "hidden", "dm-only"] },
    "ext": {
      "description": "Game-system extensions, keyed by namespace. Readers ignore unknown namespaces.",
      "type": "object",
      "propertyNames": { "pattern": "^[a-z][a-z0-9-]*$" },
      "additionalProperties": { "type": "object" }
    },
    "grid": {
      "description": "Size of one cell of a shape. See SPEC.md §8.5.",
      "type": "object",
      "required": ["size", "unit"],
      "properties": {
        "size": { "type": "number", "exclusiveMinimum": 0 },
        "unit": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },
    "orientation": {
      "description": "Compass direction at the top of the drawings. See SPEC.md §8.5.",
      "type": "object",
      "required": ["up"],
      "properties": { "up": { "enum": ["n", "e", "s", "w"] } },
      "additionalProperties": false
    },

    "base": {
      "description": "Attributes shared by every object. See SPEC.md §6.2.",
      "type": "object",
      "required": ["id", "type"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "type": { "type": "string" },
        "name": { "type": "string" },
        "parent": { "$ref": "#/$defs/id" },
        "description": { "$ref": "#/$defs/markdown" },
        "notes": { "$ref": "#/$defs/markdown" },
        "tags": { "type": "array", "items": { "type": "string" } },
        "visibility": { "$ref": "#/$defs/visibility" },
        "discovery": { "$ref": "#/$defs/check" },
        "presence": { "type": "string" },
        "where": { "type": "string" },
        "template": { "type": "boolean" },
        "from": { "$ref": "#/$defs/id" },
        "ext": { "$ref": "#/$defs/ext" }
      }
    },

    "object": {
      "oneOf": [
        { "$ref": "#/$defs/location" },
        { "$ref": "#/$defs/passage" },
        { "$ref": "#/$defs/quest" },
        { "$ref": "#/$defs/node" },
        { "$ref": "#/$defs/contained" }
      ]
    },

    "nestable": {
      "description": "What a YAML file may nest in `contents`: contained objects, and the nodes and sub-quests of a quest.",
      "oneOf": [
        { "$ref": "#/$defs/quest" },
        { "$ref": "#/$defs/node" },
        { "$ref": "#/$defs/contained" }
      ]
    },

    "quest": {
      "description": "A quest of the scenario; its parent, if any, is a larger quest. See SPEC.md §12.1.",
      "allOf": [{ "$ref": "#/$defs/base" }],
      "properties": {
        "type": { "const": "quest" }
      }
    },

    "node": {
      "description": "A scene of the scenario; its parent is the one quest it belongs to. See SPEC.md §12.2.",
      "allOf": [{ "$ref": "#/$defs/base" }],
      "properties": {
        "type": { "const": "node" },
        "locations": { "$ref": "#/$defs/ids" },
        "start": { "type": "boolean", "default": false },
        "end": { "type": "boolean", "default": false },
        "proactive": { "type": "boolean", "default": false }
      }
    },

    "contained": {
      "oneOf": [
        { "$ref": "#/$defs/mob" },
        { "$ref": "#/$defs/loot" },
        { "$ref": "#/$defs/clue" },
        { "$ref": "#/$defs/npc" },
        { "$ref": "#/$defs/trap" },
        { "$ref": "#/$defs/fixture" },
        { "$ref": "#/$defs/event" },
        { "$ref": "#/$defs/encounter-table" },
        { "$ref": "#/$defs/service" },
        { "$ref": "#/$defs/document" }
      ]
    },

    "location": {
      "allOf": [{ "$ref": "#/$defs/base" }],
      "properties": {
        "type": { "const": "location" },
        "access": { "enum": ["open", "passages"], "default": "open" },
        "conditions": { "type": "array", "items": { "type": "string" } },
        "shape": {
          "type": "string",
          "pattern": "^[^\\t]*$",
          "description": "Text floor plan: . floor, # wall, space outside, other glyphs from legend. See SPEC.md §8."
        },
        "legend": {
          "type": "object",
          "propertyNames": { "pattern": "^[^.#> \\t\\r\\n]$" },
          "additionalProperties": { "$ref": "#/$defs/id" }
        },
        "origin": {
          "type": "object",
          "required": ["x", "y"],
          "properties": {
            "x": { "type": "integer", "minimum": 0 },
            "y": { "type": "integer", "minimum": 0 }
          },
          "additionalProperties": false
        },
        "grid": { "$ref": "#/$defs/grid" },
        "orientation": { "$ref": "#/$defs/orientation" }
      }
    },

    "direction": { "enum": ["n", "ne", "e", "se", "s", "sw", "w", "nw", "up", "down", "in", "out"] },

    "side": {
      "type": "object",
      "properties": {
        "direction": { "$ref": "#/$defs/direction" },
        "label": { "type": "string" },
        "description": { "$ref": "#/$defs/markdown" },
        "visibility": { "enum": ["visible", "hidden"], "default": "visible" },
        "discovery": { "$ref": "#/$defs/check" },
        "revealed_by": { "$ref": "#/$defs/id", "description": "other-side, or the id of an event" }
      },
      "additionalProperties": false
    },

    "passage": {
      "allOf": [{ "$ref": "#/$defs/base" }],
      "required": ["a", "b"],
      "properties": {
        "type": { "const": "passage" },
        "a": { "$ref": "#/$defs/id" },
        "b": { "$ref": "#/$defs/id" },
        "nature": {
          "type": "string",
          "examples": ["door", "gate", "stairs", "ladder", "trapdoor", "tunnel", "corridor", "street", "road", "path", "bridge", "window", "portal"]
        },
        "flow": { "enum": ["both", "a-to-b", "b-to-a"], "default": "both" },
        "closure": { "enum": ["open", "closed", "locked", "barred"], "default": "open" },
        "lock": { "$ref": "#/$defs/check" },
        "key": { "$ref": "#/$defs/ids" },
        "side_a": { "$ref": "#/$defs/side" },
        "side_b": { "$ref": "#/$defs/side" }
      }
    },

    "creature": {
      "type": "object",
      "required": ["name"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "count": { "type": "integer", "minimum": 1, "default": 1 },
        "catalog": { "$ref": "#/$defs/catalog" },
        "ext": { "$ref": "#/$defs/ext" }
      },
      "additionalProperties": false
    },

    "mob": {
      "description": "A group of creatures. Since version 0.3 it may hold, as its children, the loot, documents and clues it carries, reachable once it is defeated. See SPEC.md §10.1 and §6.3.",
      "allOf": [{ "$ref": "#/$defs/base" }],
      "anyOf": [{ "required": ["creatures"] }, { "required": ["from"] }],
      "properties": {
        "type": { "const": "mob" },
        "creatures": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/creature" } },
        "behavior": { "$ref": "#/$defs/markdown" }
      }
    },

    "item": {
      "type": "object",
      "required": ["name"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "quantity": { "type": "integer", "minimum": 1, "default": 1 },
        "catalog": { "$ref": "#/$defs/catalog" },
        "rarity": { "enum": ["common", "uncommon", "rare", "very-rare", "legendary", "artifact"] },
        "value": { "type": "string" },
        "notes": { "$ref": "#/$defs/markdown" },
        "ext": { "$ref": "#/$defs/ext" }
      },
      "additionalProperties": false
    },

    "loot": {
      "allOf": [{ "$ref": "#/$defs/base" }],
      "properties": {
        "type": { "const": "loot" },
        "items": { "type": "array", "items": { "$ref": "#/$defs/item" } },
        "coins": {
          "type": "object",
          "properties": {
            "pp": { "type": "integer", "minimum": 0 },
            "gp": { "type": "integer", "minimum": 0 },
            "ep": { "type": "integer", "minimum": 0 },
            "sp": { "type": "integer", "minimum": 0 },
            "cp": { "type": "integer", "minimum": 0 }
          },
          "additionalProperties": false
        },
        "container": { "type": "string" }
      }
    },

    "clue": {
      "allOf": [{ "$ref": "#/$defs/base" }],
      "anyOf": [{ "required": ["content"] }, { "required": ["from"] }],
      "properties": {
        "type": { "const": "clue" },
        "content": { "$ref": "#/$defs/markdown" },
        "targets": { "$ref": "#/$defs/ids" },
        "node": { "$ref": "#/$defs/id", "description": "The node where it is found. See SPEC.md §12.3." },
        "leads_to": { "$ref": "#/$defs/ids", "description": "The nodes it points to. See SPEC.md §12.3." }
      }
    },

    "npc": {
      "allOf": [{ "$ref": "#/$defs/base" }],
      "properties": {
        "type": { "const": "npc" },
        "catalog": { "$ref": "#/$defs/catalog" },
        "role": { "type": "string" },
        "goals": { "$ref": "#/$defs/markdown" },
        "personality": { "$ref": "#/$defs/markdown" },
        "attitude": { "enum": ["friendly", "neutral", "hostile"], "default": "neutral" }
      }
    },

    "trap": {
      "allOf": [{ "$ref": "#/$defs/base" }],
      "properties": {
        "type": { "const": "trap" },
        "trigger": { "type": "string" },
        "detect": { "$ref": "#/$defs/check" },
        "disarm": { "$ref": "#/$defs/check" },
        "save": { "$ref": "#/$defs/check" },
        "effect": { "$ref": "#/$defs/markdown" },
        "armed": { "type": "boolean", "default": true },
        "resets": { "type": "boolean", "default": false }
      }
    },

    "fixture": {
      "allOf": [{ "$ref": "#/$defs/base" }],
      "properties": {
        "type": { "const": "fixture" },
        "interactions": { "$ref": "#/$defs/markdown" }
      }
    },

    "event": {
      "allOf": [{ "$ref": "#/$defs/base" }],
      "anyOf": [{ "required": ["trigger"] }, { "required": ["from"] }],
      "properties": {
        "type": { "const": "event" },
        "trigger": { "type": "string" },
        "effect": { "$ref": "#/$defs/markdown" },
        "repeat": { "enum": ["once", "each"], "default": "once" }
      }
    },

    "encounter-entry": {
      "type": "object",
      "properties": {
        "range": {
          "oneOf": [
            { "type": "integer", "minimum": 1 },
            { "type": "string", "pattern": "^[0-9]+-[0-9]+$" }
          ]
        },
        "text": { "$ref": "#/$defs/markdown" },
        "mob": { "$ref": "#/$defs/id" }
      },
      "additionalProperties": false
    },

    "encounter-table": {
      "allOf": [{ "$ref": "#/$defs/base" }],
      "anyOf": [
        { "required": ["die", "entries"] },
        { "required": ["mode", "entries"], "properties": { "mode": { "const": "sequence" } } },
        { "required": ["from"] }
      ],
      "properties": {
        "type": { "const": "encounter-table" },
        "mode": { "enum": ["roll", "sequence"], "default": "roll" },
        "die": { "type": "string", "pattern": "^d[0-9]+$" },
        "when": { "type": "string" },
        "entries": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/encounter-entry" } }
      }
    },

    "offer": {
      "type": "object",
      "required": ["name"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "price": { "type": "string" },
        "catalog": { "$ref": "#/$defs/catalog" },
        "ext": { "$ref": "#/$defs/ext" }
      },
      "additionalProperties": false
    },

    "service": {
      "allOf": [{ "$ref": "#/$defs/base" }],
      "properties": {
        "type": { "const": "service" },
        "offers": { "type": "array", "items": { "$ref": "#/$defs/offer" } },
        "hours": { "type": "string" }
      }
    },

    "document": {
      "description": "Something handed to the players, which they keep: a letter, a map, a journal. The clues it holds are its children. Since version 0.2. See SPEC.md §10.10.",
      "allOf": [{ "$ref": "#/$defs/base" }],
      "properties": {
        "type": { "const": "document" },
        "form": { "type": "string", "description": "What it is: letter, map, journal… Free text." },
        "text": { "$ref": "#/$defs/markdown", "description": "Its content, as the players read it." }
      }
    }
  }
}
