<?xml version="1.0" encoding="UTF-8"?>
<!--
    *  Copyright (C) 2002-2021  The FreeCol Team
    *
    *  This file is part of FreeCol.
    *
    *  FreeCol is free software: you can redistribute it and/or modify
    *  it under the terms of the GNU General Public License as published by
    *  the Free Software Foundation, either version 2 of the License, or
    *  (at your option) any later version.
    *
    *  FreeCol is distributed in the hope that it will be useful,
    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    *  GNU General Public License for more details.
    *
    *  You should have received a copy of the GNU General Public License
    *  along with FreeCol.  If not, see <http://www.gnu.org/licenses/>.

    In case of incompatible changes, please update version number and
    XSD schema for validation.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

  <!-- Useful complex types that turn up in multiple places -->
  <xs:complexType name="feature">
    <xs:sequence>
      <xs:element ref="scope" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
    <xs:attribute name="id" use="required" type="xs:string"/>
    <xs:attribute name="delete" type="xs:boolean"/>
    <xs:attribute name="preserve" use="optional" type="xs:boolean"/>

    <xs:attribute name="duration" type="xs:int"/>
    <xs:attribute name="firstTurn" type="xs:int" use="optional"/>
    <xs:attribute name="lastTurn" type="xs:int" use="optional"/>
    <xs:attribute name="source" type="xs:string"/>
    <xs:attribute name="temporary" type="xs:boolean"/>
  </xs:complexType>

  <xs:element name="ability">
    <xs:complexType>
      <xs:complexContent>
        <xs:extension base="feature">
          <xs:attribute name="value" type="xs:boolean"/>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>

  <xs:element name="modifier">
    <xs:complexType>
      <xs:complexContent>
        <xs:extension base="feature">
          <xs:attribute name="increment" use="optional" type="xs:float"/>
          <xs:attribute name="increment-type" type="ModifierType"/>
          <xs:attribute name="index" type="xs:nonNegativeInteger"/>
          <xs:attribute name="type" type="ModifierType"/>
          <xs:attribute name="value" type="xs:float"/>
          <!-- @compat 0.11.3 -->
          <xs:attribute name="incrementType" type="ModifierType" use="optional"/>
          <!-- @compat 0.11.3 -->
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>

  <xs:simpleType name="ModifierType">
    <xs:restriction base="xs:NMTOKEN">
      <xs:enumeration value="additive" />
      <xs:enumeration value="multiplicative" />
      <xs:enumeration value="percentage" />
      <!-- @compat 0.10.7 -->
      <xs:enumeration value="ADDITIVE" />
      <xs:enumeration value="MULTIPLICATIVE" />
      <xs:enumeration value="PERCENTAGE" />
      <!-- end @compat -->
    </xs:restriction>
  </xs:simpleType>

  <xs:element name="limit">
    <xs:complexType>
      <xs:choice minOccurs="1" maxOccurs="1">
        <xs:sequence>
          <xs:element name="left-hand-side" type="Operand" />
          <xs:element name="right-hand-side" type="Operand" />
        </xs:sequence>
        <xs:sequence>
          <xs:element name="leftHandSide" type="Operand" />
          <xs:element name="rightHandSide" type="Operand" />
        </xs:sequence>
      </xs:choice>
      <xs:attribute name="id" type="xs:string" />

      <xs:attribute name="operator" use="required">
        <xs:simpleType>
          <xs:restriction base="xs:token">
            <xs:enumeration value="lt"/>
            <xs:enumeration value="le"/>
            <xs:enumeration value="gt"/>
            <xs:enumeration value="ge"/>
            <xs:enumeration value="eq"/>
            <!-- @compat 0.11.3 -->
            <xs:enumeration value="LT"/>
            <xs:enumeration value="LE"/>
            <xs:enumeration value="GT"/>
            <xs:enumeration value="GE"/>
            <xs:enumeration value="EQ"/>
            <!-- end @compat 0.11.3 -->
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
    </xs:complexType>
  </xs:element>

  <xs:element name="production">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="input" type="AbstractGoodsType"/>
        <xs:element name="output" type="AbstractGoodsType"/>
      </xs:choice>

      <xs:attribute name="delete" use="optional" type="xs:string"/>

      <xs:attribute name="production-level" type="xs:string"/>
      <xs:attribute name="unattended" type="xs:boolean"/>
      <!-- @compat 0.11.3 -->
      <xs:attribute name="productionLevel" type="xs:string"/>
      <!-- end @compat 0.11.3 -->
    </xs:complexType>
  </xs:element>

  <xs:element name="required-ability">
    <xs:complexType>
      <xs:attribute name="id" use="required" type="xs:string"/>

      <xs:attribute name="value" type="xs:boolean"/>
    </xs:complexType>
  </xs:element>

  <!-- @compat 0.10.0 -->
  <xs:element name="required-location-ability">
    <xs:complexType>
      <xs:attribute name="id" use="required" type="xs:string"/>

      <xs:attribute name="value" type="xs:boolean"/>
    </xs:complexType>
  </xs:element>
  <!-- end @compat 0.10.0 -->

  <xs:element name="required-goods">
    <xs:complexType>
      <xs:attribute name="id" use="required" type="GoodsTypeId"/>

      <xs:attribute name="value" type="xs:nonNegativeInteger"/>
    </xs:complexType>
  </xs:element>

  <xs:element name="scope" type="ScopeType" />


  <!-- Low level types.  FunkyCapitalized -->
  <xs:complexType name="AbstractGoodsType">
    <xs:attribute name="goods-type" use="required" type="GoodsTypeId"/>
    <xs:attribute name="value" use="required" type="xs:nonNegativeInteger"/>
  </xs:complexType>

  <xs:complexType name="Operand">
    <xs:complexContent>
      <xs:extension base="ScopeType">
        <xs:attribute name="operand-type">
          <xs:simpleType>
            <xs:restriction base="xs:token">
              <xs:enumeration value="buildings" />
              <xs:enumeration value="founding_fathers" />
              <xs:enumeration value="none" />
              <xs:enumeration value="option" />
              <xs:enumeration value="settlements" />
              <xs:enumeration value="units" />
              <xs:enumeration value="year" />
              <!-- @compat 0.11.3 -->
              <xs:enumeration value="UNITS" />
              <xs:enumeration value="BUILDINGS" />
              <xs:enumeration value="SETTLEMENTS" />
              <xs:enumeration value="FOUNDING_FATHERS" />
              <xs:enumeration value="YEAR" />
              <xs:enumeration value="OPTION" />
              <xs:enumeration value="NONE" />
              <!-- end @compat 0.11.3 -->
            </xs:restriction>
          </xs:simpleType>
        </xs:attribute>
        <!-- @compat 0.11.3 -->
        <xs:attribute name="operandType">
          <xs:simpleType>
            <xs:restriction base="xs:token">
              <xs:enumeration value="UNITS" />
              <xs:enumeration value="BUILDINGS" />
              <xs:enumeration value="SETTLEMENTS" />
              <xs:enumeration value="FOUNDING_FATHERS" />
              <xs:enumeration value="YEAR" />
              <xs:enumeration value="OPTION" />
              <xs:enumeration value="NONE" />
            </xs:restriction>
          </xs:simpleType>
        </xs:attribute>
        <!-- end @compat 0.11.3 -->
        <xs:attribute name="scope-level">
          <xs:simpleType>
            <xs:restriction base="xs:token">
              <xs:enumeration value="settlement" />
              <xs:enumeration value="player" />
              <xs:enumeration value="game" />
              <xs:enumeration value="none" />
              <!-- @compat 0.11.3 -->
              <xs:enumeration value="SETTLEMENT" />
              <xs:enumeration value="PLAYER" />
              <xs:enumeration value="GAME" />
              <xs:enumeration value="NONE" />
              <!-- end @compat 0.11.3 -->
            </xs:restriction>
          </xs:simpleType>
        </xs:attribute>
        <!-- @compat 0.11.3 -->
        <xs:attribute name="scopeLevel">
          <xs:simpleType>
            <xs:restriction base="xs:token">
              <xs:enumeration value="SETTLEMENT" />
              <xs:enumeration value="PLAYER" />
              <xs:enumeration value="GAME" />
              <xs:enumeration value="NONE" />
            </xs:restriction>
          </xs:simpleType>
        </xs:attribute>
        <!-- end @compat 0.11.3 -->
        <xs:attribute name="value" type="xs:integer" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  
  <xs:simpleType name="PercentageType">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="0"/>
      <xs:maxInclusive value="100"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="RandomRange">
    <xs:sequence minOccurs="0">
      <xs:element name="scope" type="ScopeType"/>
    </xs:sequence>
    <xs:attribute name="factor" use="required" type="xs:integer"/>
    <xs:attribute name="maximum" use="required" type="xs:integer"/>
    <xs:attribute name="minimum" use="required" type="xs:integer"/>
    <xs:attribute name="probability" use="required" type="PercentageType"/>
  </xs:complexType>

  <xs:complexType name="ScopeType">
    <xs:attribute name="ability-id" type="AbilityId"/>
    <xs:attribute name="ability-value" type="xs:boolean"/>
    <xs:attribute name="matches-null" type="xs:boolean"/>
    <xs:attribute name="match-negated" type="xs:boolean"/>
    <xs:attribute name="method-name" type="xs:string"/>
    <xs:attribute name="method-value" type="xs:string"/>
    <xs:attribute name="type" type="xs:string"/>
    <!-- @compat 0.11.3 -->
    <xs:attribute name="matchesNull" type="xs:boolean"/>
    <xs:attribute name="matchNegated" type="xs:boolean"/>
    <!-- end @compat 0.11.3 -->
  </xs:complexType>

</xs:schema>
