{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://wristdope.com/schemas/reticle-1.schema.json",
  "title": "Wrist Dope reticle file, version 1",
  "description": "One scope reticle as a list of marks. See FORMAT.md. The schema checks the shape of a file; the limits that depend on expanding ladders and mirrors (6,000 marks, 200 shapes, 60,000 repetitions) are checked by a reader.",
  "type": "object",
  "required": ["format", "schemaVersion", "name", "unit", "lineThickness", "marks"],
  "properties": {
    "format": {"const": "wristdope.reticle", "description": "What the file is. Every Wrist Dope file starts with this field."},
    "schemaVersion": {"const": 1, "description": "The version of the reticle format: the oldest reader that can read this file."},
    "name": {"type": "string", "minLength": 1, "maxLength": 40},
    "unit": {"enum": ["mil", "moa"], "description": "The unit of every number in the file. MOA is true MOA, 1/60 of a degree."},
    "lineThickness": {"type": "number", "exclusiveMinimum": 0, "maximum": 5000, "description": "The thickness of the reticle's lines, in unit."},
    "kind": {"enum": ["etched", "dot"], "default": "etched"},
    "illumination": {"enum": ["redParts", "whole"], "default": "redParts"},
    "extent": {"type": "number", "exclusiveMinimum": 0, "maximum": 5000},
    "source": {"type": "string", "maxLength": 120, "description": "Where the dimensions came from."},
    "assumptions": {
      "type": "array",
      "maxItems": 24,
      "items": {"type": "string", "maxLength": 160},
      "description": "One sentence for each dimension the author could not read and had to assume."
    },
    "marks": {"type": "array", "minItems": 1, "items": {"$ref": "#/$defs/mark"}}
  },
  "additionalProperties": true,
  "$defs": {
    "number": {"type": "number", "minimum": -5000, "maximum": 5000},
    "positive": {"type": "number", "exclusiveMinimum": 0, "maximum": 5000},
    "point": {"type": "array", "prefixItems": [{"$ref": "#/$defs/number"}, {"$ref": "#/$defs/number"}], "minItems": 2, "maxItems": 2},
    "arcSpec": {
      "type": "array",
      "description": "radius, from degrees, to degrees. 0 is to the right, 90 is straight down.",
      "prefixItems": [{"$ref": "#/$defs/positive"}, {"$ref": "#/$defs/number"}, {"$ref": "#/$defs/number"}],
      "minItems": 3,
      "maxItems": 3
    },
    "arcStep": {"type": "number", "minimum": 1, "maximum": 90},
    "lineCoordinate": {
      "description": "A number, or {c, t}: c plus t times the line thickness.",
      "oneOf": [
        {"$ref": "#/$defs/number"},
        {
          "type": "object",
          "required": ["t"],
          "properties": {"c": {"$ref": "#/$defs/number"}, "t": {"$ref": "#/$defs/number"}},
          "additionalProperties": false
        }
      ]
    },
    "mirror": {"enum": ["x", "y", "xy"]},
    "ink": {"enum": ["black", "red"]},
    "condition": {
      "type": "object",
      "description": "A test of the rung number n, inside a ladder. Every condition given must hold.",
      "properties": {
        "every": {"type": "integer", "minimum": 1},
        "rem": {"type": "array", "items": {"type": "integer"}, "maxItems": 64},
        "is": {"type": "array", "items": {"type": "integer"}, "maxItems": 64},
        "in": {"type": "array", "prefixItems": [{"type": "integer"}, {"type": "integer"}], "minItems": 2, "maxItems": 2}
      },
      "dependentRequired": {"rem": ["every"]},
      "additionalProperties": false
    },
    "mark": {
      "description": "One mark: exactly one of line, dot, label, shape, bar, arc, ladder. An object with none of them is a comment (or, in a ladder's \"first\", a rung left empty).",
      "oneOf": [
        {"$ref": "#/$defs/line"},
        {"$ref": "#/$defs/dot"},
        {"$ref": "#/$defs/label"},
        {"$ref": "#/$defs/shape"},
        {"$ref": "#/$defs/bar"},
        {"$ref": "#/$defs/arc"},
        {"$ref": "#/$defs/ladder"},
        {"$ref": "#/$defs/nothing"}
      ]
    },
    "nothing": {
      "type": "object",
      "properties": {
        "note": {"type": "string"},
        "mirror": {"$ref": "#/$defs/mirror"},
        "ink": {"$ref": "#/$defs/ink"},
        "if": {"$ref": "#/$defs/condition"}
      },
      "additionalProperties": false
    },
    "line": {
      "type": "object",
      "required": ["line"],
      "properties": {
        "line": {"type": "array", "items": {"$ref": "#/$defs/lineCoordinate"}, "minItems": 4, "maxItems": 4, "description": "x0, y0, x1, y1"},
        "w": {"$ref": "#/$defs/positive", "description": "Its own width. Default: lineThickness."},
        "note": {"type": "string"},
        "mirror": {"$ref": "#/$defs/mirror"},
        "ink": {"$ref": "#/$defs/ink"},
        "if": {"$ref": "#/$defs/condition"}
      },
      "additionalProperties": false
    },
    "dot": {
      "type": "object",
      "required": ["dot"],
      "properties": {
        "dot": {
          "type": "array",
          "description": "x, y, diameter. \"dot\" in place of the diameter is the sight's own dot size.",
          "prefixItems": [{"$ref": "#/$defs/number"}, {"$ref": "#/$defs/number"}, {"oneOf": [{"$ref": "#/$defs/positive"}, {"const": "dot"}]}],
          "minItems": 3,
          "maxItems": 3
        },
        "note": {"type": "string"},
        "mirror": {"$ref": "#/$defs/mirror"},
        "ink": {"$ref": "#/$defs/ink"},
        "if": {"$ref": "#/$defs/condition"}
      },
      "additionalProperties": false
    },
    "label": {
      "type": "object",
      "required": ["label"],
      "properties": {
        "label": {
          "type": "array",
          "description": "x, y, height, text. {n} in the text is the rung number.",
          "prefixItems": [
            {"$ref": "#/$defs/number"},
            {"$ref": "#/$defs/number"},
            {"$ref": "#/$defs/positive"},
            {"type": "string", "pattern": "^([0-9A-Za-z.+-]|\\{n\\}){1,6}$"}
          ],
          "minItems": 4,
          "maxItems": 4
        },
        "light": {"type": "boolean"},
        "note": {"type": "string"},
        "mirror": {"$ref": "#/$defs/mirror"},
        "ink": {"$ref": "#/$defs/ink"},
        "if": {"$ref": "#/$defs/condition"}
      },
      "additionalProperties": false
    },
    "shape": {
      "type": "object",
      "required": ["shape"],
      "properties": {
        "shape": {
          "type": "array",
          "minItems": 1,
          "items": {
            "oneOf": [
              {"$ref": "#/$defs/point"},
              {
                "type": "object",
                "required": ["arc"],
                "properties": {"arc": {"$ref": "#/$defs/arcSpec"}, "at": {"$ref": "#/$defs/point"}},
                "additionalProperties": false
              }
            ]
          }
        },
        "step": {"$ref": "#/$defs/arcStep"},
        "post": {"type": "boolean"},
        "note": {"type": "string"},
        "mirror": {"$ref": "#/$defs/mirror"},
        "ink": {"$ref": "#/$defs/ink"},
        "if": {"$ref": "#/$defs/condition"}
      },
      "additionalProperties": false
    },
    "bar": {
      "type": "object",
      "required": ["bar"],
      "properties": {
        "bar": {
          "type": "array",
          "description": "x0, y0, x1, y1, thickness. Straight across or straight up and down.",
          "prefixItems": [
            {"$ref": "#/$defs/number"},
            {"$ref": "#/$defs/number"},
            {"$ref": "#/$defs/number"},
            {"$ref": "#/$defs/number"},
            {"$ref": "#/$defs/positive"}
          ],
          "minItems": 5,
          "maxItems": 5
        },
        "post": {"type": "boolean"},
        "note": {"type": "string"},
        "mirror": {"$ref": "#/$defs/mirror"},
        "ink": {"$ref": "#/$defs/ink"},
        "if": {"$ref": "#/$defs/condition"}
      },
      "additionalProperties": false
    },
    "arc": {
      "type": "object",
      "required": ["arc"],
      "properties": {
        "arc": {"$ref": "#/$defs/arcSpec"},
        "at": {"$ref": "#/$defs/point"},
        "step": {"$ref": "#/$defs/arcStep"},
        "w": {"$ref": "#/$defs/positive"},
        "note": {"type": "string"},
        "mirror": {"$ref": "#/$defs/mirror"},
        "ink": {"$ref": "#/$defs/ink"},
        "if": {"$ref": "#/$defs/condition"}
      },
      "additionalProperties": false
    },
    "ladder": {
      "type": "object",
      "required": ["ladder"],
      "oneOf": [{"required": ["each"]}, {"required": ["first"]}],
      "properties": {
        "ladder": {
          "type": "object",
          "required": ["n", "step"],
          "properties": {
            "n": {
              "type": "array",
              "description": "first rung, last rung",
              "prefixItems": [{"type": "integer", "minimum": -10000, "maximum": 10000}, {"type": "integer", "minimum": -10000, "maximum": 10000}],
              "minItems": 2,
              "maxItems": 2
            },
            "by": {"type": "integer", "minimum": 1, "default": 1},
            "step": {"$ref": "#/$defs/point", "description": "Rung n sits at at + n x step."},
            "at": {"$ref": "#/$defs/point"}
          },
          "additionalProperties": false
        },
        "each": {"type": "array", "items": {"$ref": "#/$defs/mark"}, "description": "Every entry is drawn at every rung."},
        "first": {"type": "array", "items": {"$ref": "#/$defs/mark"}, "description": "At each rung, only the first entry whose \"if\" holds."},
        "note": {"type": "string"},
        "mirror": {"$ref": "#/$defs/mirror"},
        "ink": {"$ref": "#/$defs/ink"},
        "if": {"$ref": "#/$defs/condition"}
      },
      "additionalProperties": false
    }
  }
}
