{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://foundryapparatus.affonso.dev/schemas/v1/machine.schema.json",
  "title": "Foundry Apparatus v1 - machine (apparatus)",
  "description": "A machine (foundry_apparatus/machines/) in one of three carrier formats: single_block, multiblock or item.",
  "type": "object",
  "allOf": [
    {
      "$ref": "common.schema.json#/$defs/definition_header"
    }
  ],
  "required": [
    "display_name",
    "carrier"
  ],
  "properties": {
    "previous_ids": {
      "$ref": "common.schema.json#/$defs/previous_ids"
    },
    "display_name": {
      "description": "Translation key or literal English name.",
      "type": "string",
      "minLength": 1
    },
    "carrier": {
      "oneOf": [
        {
          "$ref": "#/$defs/single_block"
        },
        {
          "$ref": "#/$defs/multiblock"
        },
        {
          "$ref": "#/$defs/item"
        }
      ]
    },
    "storages": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/storage"
      }
    },
    "on_break": {
      "description": "What happens to the machine's contents when it is broken. A single value applies to every resource type: 'keep' stores everything in the machine's item, 'drop' spills items and experience on the ground and loses fluids and energy, 'discard' loses everything. An object sets one rule per type. Without it, items and experience drop and fluids and energy stay in the item.",
      "oneOf": [
        {
          "enum": [
            "drop",
            "keep",
            "discard"
          ]
        },
        {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "items": {
              "description": "Item storages: 'drop' (default), 'keep' or 'discard'.",
              "enum": [
                "drop",
                "keep",
                "discard"
              ]
            },
            "fluids": {
              "description": "Fluid storages: 'keep' (default) or 'discard'. Fluids cannot be dropped.",
              "enum": [
                "keep",
                "discard"
              ]
            },
            "energy": {
              "description": "Energy storages: 'keep' (default) or 'discard'. Energy cannot be dropped.",
              "enum": [
                "keep",
                "discard"
              ]
            },
            "xp": {
              "description": "Experience storages: 'drop' (default, as orbs), 'keep' or 'discard'.",
              "enum": [
                "drop",
                "keep",
                "discard"
              ]
            }
          }
        }
      ]
    },
    "variables": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/variable"
      }
    },
    "gui": {
      "description": "Id of a GUI layout in guis/, or an inline layout.",
      "oneOf": [
        {
          "$ref": "common.schema.json#/$defs/identifier"
        },
        {
          "$ref": "gui.schema.json#/$defs/inline_layout"
        }
      ]
    },
    "logic": {
      "$ref": "logic.schema.json#/$defs/inline_body"
    },
    "logic_refs": {
      "type": "array",
      "items": {
        "$ref": "common.schema.json#/$defs/identifier"
      },
      "uniqueItems": true
    },
    "visual": {
      "$ref": "common.schema.json#/$defs/visual"
    },
    "upgrades": {
      "$ref": "#/$defs/upgrades"
    }
  },
  "unevaluatedProperties": false,
  "$defs": {
    "side_mode": {
      "enum": [
        "in",
        "out",
        "both",
        "none"
      ]
    },
    "single_block": {
      "type": "object",
      "required": [
        "type"
      ],
      "properties": {
        "type": {
          "const": "single_block"
        },
        "properties": {
          "$ref": "common.schema.json#/$defs/block_properties"
        },
        "rotation": {
          "enum": [
            "none",
            "horizontal",
            "all_directions"
          ],
          "default": "horizontal"
        },
        "default_sides": {
          "description": "Faces relative to the machine front.",
          "type": "object",
          "propertyNames": {
            "enum": [
              "front",
              "back",
              "left",
              "right",
              "top",
              "bottom"
            ]
          },
          "additionalProperties": {
            "$ref": "#/$defs/side_mode"
          }
        },
        "allow_player_side_config": {
          "type": "boolean",
          "default": true
        }
      },
      "additionalProperties": false
    },
    "multiblock": {
      "type": "object",
      "required": [
        "type",
        "mode",
        "pattern",
        "key"
      ],
      "properties": {
        "type": {
          "const": "multiblock"
        },
        "mode": {
          "enum": [
            "controller",
            "controllerless"
          ]
        },
        "controller_properties": {
          "$ref": "common.schema.json#/$defs/block_properties"
        },
        "pattern": {
          "description": "layers[y][z] is a row of characters along x, from the bottom layer up. A space matches any block.",
          "type": "object",
          "required": [
            "layers"
          ],
          "properties": {
            "layers": {
              "type": "array",
              "minItems": 1,
              "items": {
                "type": "array",
                "minItems": 1,
                "items": {
                  "type": "string",
                  "minLength": 1
                }
              }
            }
          },
          "additionalProperties": false
        },
        "key": {
          "type": "object",
          "propertyNames": {
            "type": "string",
            "pattern": "^[^ ]$"
          },
          "additionalProperties": {
            "$ref": "#/$defs/pattern_key_entry"
          }
        },
        "allowed_rotations": {
          "type": "array",
          "items": {
            "enum": [
              "none",
              "horizontal",
              "all_directions"
            ]
          },
          "uniqueItems": true
        },
        "allow_mirror": {
          "type": "boolean",
          "default": false
        },
        "assembly_tool": {
          "type": "object",
          "properties": {
            "item": {
              "$ref": "common.schema.json#/$defs/identifier"
            },
            "tag": {
              "$ref": "common.schema.json#/$defs/identifier"
            },
            "components": {
              "$ref": "common.schema.json#/$defs/components_predicate"
            },
            "strict": {
              "type": "boolean",
              "default": false
            },
            "consume_durability": {
              "type": "boolean",
              "default": false
            },
            "damage_amount": {
              "type": "integer",
              "minimum": 0,
              "default": 0
            }
          },
          "oneOf": [
            {
              "required": [
                "item"
              ],
              "not": {
                "required": [
                  "tag"
                ]
              }
            },
            {
              "required": [
                "tag"
              ],
              "not": {
                "required": [
                  "item"
                ]
              }
            }
          ],
          "additionalProperties": false
        },
        "wall_sharing": {
          "type": "boolean",
          "default": false
        },
        "allow_player_side_config": {
          "type": "boolean",
          "default": true
        }
      },
      "additionalProperties": false
    },
    "pattern_key_entry": {
      "description": "What a pattern symbol matches. Exactly one symbol marks the controller (controller mode) or the key block (controllerless mode).",
      "type": "object",
      "properties": {
        "block": {
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "tag": {
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "properties": {
          "description": "Block state properties the block must have (e.g. {\"facing\": \"north\"}); unlisted properties are ignored.",
          "type": "object",
          "additionalProperties": {
            "type": "string"
          }
        },
        "controller": {
          "type": "boolean"
        },
        "key_block": {
          "type": "boolean"
        }
      },
      "anyOf": [
        {
          "required": [
            "block"
          ]
        },
        {
          "required": [
            "tag"
          ]
        },
        {
          "required": [
            "controller"
          ],
          "properties": {
            "controller": {
              "const": true
            }
          }
        }
      ],
      "not": {
        "required": [
          "block",
          "tag"
        ]
      },
      "additionalProperties": false
    },
    "item": {
      "type": "object",
      "required": [
        "type",
        "operation_modes"
      ],
      "properties": {
        "type": {
          "const": "item"
        },
        "operation_modes": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "enum": [
              "on_use",
              "in_hand",
              "in_inventory",
              "curio_trinket"
            ]
          }
        },
        "max_stack_size": {
          "type": "integer",
          "minimum": 1,
          "maximum": 99,
          "default": 1
        },
        "bar_binding": {
          "$ref": "common.schema.json#/$defs/local_name"
        }
      },
      "additionalProperties": false
    },
    "storage": {
      "type": "object",
      "required": [
        "id",
        "resource_type"
      ],
      "properties": {
        "id": {
          "$ref": "common.schema.json#/$defs/local_name"
        },
        "resource_type": {
          "$ref": "common.schema.json#/$defs/vocabulary_type"
        },
        "capacity": {
          "description": "In the JSON unit of the resource type: fluids = mB, energy = FE, xp = points. Item storages use 'slots' instead.",
          "type": "integer",
          "minimum": 1
        },
        "slots": {
          "description": "Item storages only: number of slots, each holding one item variant.",
          "type": "integer",
          "minimum": 1,
          "maximum": 1024
        },
        "slot_limit": {
          "description": "Item storages only: how many items one slot holds, which may exceed the item's stack size (for recipes needing thousands of an item). Without it, a slot holds one stack of its item.",
          "type": "integer",
          "minimum": 1,
          "maximum": 1000000000
        },
        "direction": {
          "enum": [
            "in",
            "out",
            "both"
          ],
          "default": "both"
        },
        "filter": {
          "type": "array",
          "items": {
            "$ref": "common.schema.json#/$defs/resource_ref"
          }
        },
        "accept": {
          "description": "Item input storages only ('direction': 'in'): what players, hoppers and pipes may put in. 'recipe_inputs' (default) takes only items some recipe of the machine uses in this storage; 'any' takes any item the filter allows.",
          "enum": [
            "recipe_inputs",
            "any"
          ],
          "default": "recipe_inputs"
        },
        "on_break": {
          "description": "This storage's own rule when the machine is broken, overriding the machine's 'on_break' for its type. Only item and experience storages can use 'drop'.",
          "enum": [
            "drop",
            "keep",
            "discard"
          ]
        }
      },
      "if": {
        "properties": {
          "resource_type": {
            "const": "foundry_apparatus:item"
          }
        }
      },
      "then": {
        "required": [
          "slots"
        ],
        "properties": {
          "capacity": false
        }
      },
      "else": {
        "required": [
          "capacity"
        ],
        "properties": {
          "slots": false,
          "slot_limit": false,
          "accept": false
        },
        "if": {
          "properties": {
            "resource_type": {
              "const": "foundry_apparatus:xp"
            }
          }
        },
        "then": {},
        "else": {
          "properties": {
            "on_break": {
              "enum": [
                "keep",
                "discard"
              ]
            }
          }
        }
      },
      "additionalProperties": false
    },
    "variable": {
      "type": "object",
      "required": [
        "name",
        "type",
        "default"
      ],
      "properties": {
        "name": {
          "$ref": "common.schema.json#/$defs/local_name"
        },
        "type": {
          "enum": [
            "number",
            "boolean",
            "string"
          ]
        },
        "hud_visible": {
          "type": "boolean",
          "default": false
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "type": {
                "const": "number"
              }
            }
          },
          "then": {
            "required": [
              "min",
              "max"
            ],
            "properties": {
              "name": true,
              "type": true,
              "hud_visible": true,
              "min": {
                "type": "number"
              },
              "max": {
                "type": "number"
              },
              "default": {
                "type": "number"
              },
              "unit": {
                "type": "string"
              }
            },
            "additionalProperties": false
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "boolean"
              }
            }
          },
          "then": {
            "properties": {
              "name": true,
              "type": true,
              "hud_visible": true,
              "default": {
                "type": "boolean"
              }
            },
            "additionalProperties": false
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "string"
              }
            }
          },
          "then": {
            "properties": {
              "name": true,
              "type": true,
              "hud_visible": true,
              "default": {
                "type": "string"
              },
              "allowed_values": {
                "type": "array",
                "items": {
                  "type": "string"
                },
                "uniqueItems": true
              }
            },
            "additionalProperties": false
          }
        }
      ]
    },
    "upgrades": {
      "type": "object",
      "properties": {
        "slots": {
          "type": "integer",
          "minimum": 0
        },
        "max_parallelism": {
          "type": "integer",
          "minimum": 1
        },
        "overclock": {
          "type": "object",
          "properties": {
            "preset": {
              "enum": [
                "standard",
                "perfect"
              ]
            },
            "speed_per_tier": {
              "type": "number",
              "exclusiveMinimum": 0
            },
            "cost_per_tier": {
              "type": "number",
              "exclusiveMinimum": 0
            },
            "max_tiers": {
              "type": "integer",
              "minimum": 1
            }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    }
  }
}
