Ticket #2936: relativetemplates.patch

File relativetemplates.patch, 8.3 KB (added by scythetwirler, 9 years ago)

leper's patch with scythetwirler's fill-in-the-blanks.

  • source/simulation2/components/CCmpFootprint.cpp

     
    5959            "<choice>"
    6060                "<element name='Square' a:help='Set the footprint to a square of the given size'>"
    6161                    "<attribute name='width' a:help='Size of the footprint along the left/right direction (in metres)'>"
    62                         "<ref name='positiveDecimal'/>"
     62                        "<ref name='positiveDecimalAttribute'/>"
    6363                    "</attribute>"
    6464                    "<attribute name='depth' a:help='Size of the footprint along the front/back direction (in metres)'>"
    65                         "<ref name='positiveDecimal'/>"
     65                        "<ref name='positiveDecimalAttribute'/>"
    6666                    "</attribute>"
    6767                "</element>"
    6868                "<element name='Circle' a:help='Set the footprint to a circle of the given size'>"
    6969                    "<attribute name='radius' a:help='Radius of the footprint (in metres)'>"
    70                         "<ref name='positiveDecimal'/>"
     70                        "<ref name='positiveDecimalAttribute'/>"
    7171                    "</attribute>"
    7272                "</element>"
    7373            "</choice>"
  • source/simulation2/components/CCmpObstruction.cpp

     
    112112            "<choice>"
    113113                "<element name='Static'>"
    114114                    "<attribute name='width'>"
    115                         "<ref name='positiveDecimal'/>"
     115                        "<ref name='positiveDecimalAttribute'/>"
    116116                    "</attribute>"
    117117                    "<attribute name='depth'>"
    118                         "<ref name='positiveDecimal'/>"
     118                        "<ref name='positiveDecimalAttribute'/>"
    119119                    "</attribute>"
    120120                "</element>"
    121121                "<element name='Unit'>"
    122122                    "<attribute name='radius'>"
    123                         "<ref name='positiveDecimal'/>"
     123                        "<ref name='positiveDecimalAttribute'/>"
    124124                    "</attribute>"
    125125                "</element>"
    126126                "<element name='Obstructions'>"
     
    138138                                "</attribute>"
    139139                            "</optional>"
    140140                            "<attribute name='width'>"
    141                                 "<ref name='positiveDecimal'/>"
     141                                "<ref name='positiveDecimalAttribute'/>"
    142142                            "</attribute>"
    143143                            "<attribute name='depth'>"
    144                                 "<ref name='positiveDecimal'/>"
     144                                "<ref name='positiveDecimalAttribute'/>"
    145145                            "</attribute>"
    146146                        "</element>"
    147147                    "</zeroOrMore>"
  • source/simulation2/components/CCmpVisualActor.cpp

     
    146146                        "</element>"
    147147                        "<element name='Box' a:help='Sets the selection shape to a box of specified dimensions'>"
    148148                            "<attribute name='width'>"
    149                                 "<ref name='positiveDecimal' />"
     149                                "<ref name='positiveDecimalAttribute'/>"
    150150                            "</attribute>"
    151151                            "<attribute name='height'>"
    152                                 "<ref name='positiveDecimal' />"
     152                                "<ref name='positiveDecimalAttribute'/>"
    153153                            "</attribute>"
    154154                            "<attribute name='depth'>"
    155                                 "<ref name='positiveDecimal' />"
     155                                "<ref name='positiveDecimalAttribute'/>"
    156156                            "</attribute>"
    157157                        "</element>"
    158158                        "<element name='Cylinder' a:help='Sets the selection shape to a cylinder of specified dimensions'>"
    159159                            "<attribute name='radius'>"
    160                                 "<ref name='positiveDecimal' />"
     160                                "<ref name='positiveDecimalAttribute'/>"
    161161                            "</attribute>"
    162162                            "<attribute name='height'>"
    163                                 "<ref name='positiveDecimal' />"
     163                                "<ref name='positiveDecimalAttribute'/>"
    164164                            "</attribute>"
    165165                        "</element>"
    166166                    "</choice>"
  • source/simulation2/docs/SimulationDocs.h

     
    314314For early development of a new component, you can set the schema to <code>&lt;ref name='anything'/></code> to allow any content.
    315315If you don't define @c GetSchema, then the default is <code>&lt;empty/></code> (i.e. there must be no elements).
    316316
     317nonNegativeDecimal supports a basic operation attribute that is performed upon its parent's elements. Valid methods are "add",
     318"sub", "mul", and "div". If the parent templates appears like so:
     319@code
     320<Entity>
     321  <Example>
     322    <Name>Semi-Humanoids</Name>
     323    <Height>9000</Height>
     324    <Eyes/>
     325  </Example>
     326  <!-- ... other components ... -->
     327</Entity>
     328@endcode
     329and the child template appears like so:
     330@code
     331<Entity>
     332  <Example>
     333    <Name>Barney</Name>
     334    <Height method="add">5</Height>
     335    <Eyes/>
     336  </Example>
     337  <!-- ... other components ... -->
     338</Entity>
     339@endcode
     340then Barney would have a height of 9005.
    317341
    318342@section allowing-js-interfaces Allowing interfaces to be implemented in JS
    319343
  • source/simulation2/system/ComponentManager.cpp

     
    10851085    std::string schema =
    10861086        "<grammar xmlns='http://relaxng.org/ns/structure/1.0' xmlns:a='http://ns.wildfiregames.com/entity' datatypeLibrary='http://www.w3.org/2001/XMLSchema-datatypes'>"
    10871087            "<define name='nonNegativeDecimal'>"
     1088                "<optional><attribute name='method'/></optional>"
    10881089                "<data type='decimal'><param name='minInclusive'>0</param></data>"
    10891090            "</define>"
    10901091            "<define name='positiveDecimal'>"
     1092                "<optional><attribute name='method'/></optional>"
    10911093                "<data type='decimal'><param name='minExclusive'>0</param></data>"
    10921094            "</define>"
     1095            "<define name='positiveDecimalAttribute'>"
     1096                "<data type='decimal'><param name='minExclusive'>0</param></data>"
     1097            "</define>"
    10931098            "<define name='anything'>"
    10941099                "<zeroOrMore>"
    10951100                      "<choice>"
  • source/simulation2/system/ParamNode.cpp

     
    7878
    7979    bool hasSetValue = false;
    8080
    81     // Look for special attributes
    8281    int at_disable = xmb.GetAttributeID("disable");
    8382    int at_replace = xmb.GetAttributeID("replace");
     83    int at_method = xmb.GetAttributeID("method");
    8484    int at_datatype = xmb.GetAttributeID("datatype");
    8585    bool replacing = false;
     86    enum method {
     87        ADD,
     88        SUB,
     89        MUL,
     90        DIV,
     91        INVALID
     92    } method = INVALID;
    8693    {
    8794        XERO_ITER_ATTR(element, attr)
    8895        {
     
    96103                m_Childs.erase(name);
    97104                replacing = true;
    98105            }
     106            else if (attr.Name == at_method)
     107            {
     108                if (std::wstring(attr.Value.begin(), attr.Value.end()) == L"add")
     109                    method = ADD;
     110                else if (std::wstring(attr.Value.begin(), attr.Value.end()) == L"sub")
     111                    method = SUB;
     112                else if (std::wstring(attr.Value.begin(), attr.Value.end()) == L"mul")
     113                    method = MUL;
     114                else if (std::wstring(attr.Value.begin(), attr.Value.end()) == L"div")
     115                    method = DIV;
     116                else
     117                    LOGWARNING(L"Invalid method '%ls'", attr.Value.c_str());
     118            }
    99119        }
    100120    }
    101121    {
     
    142162
    143163    // Add this element as a child node
    144164    CParamNode& node = m_Childs[name];
     165    if (method != INVALID)
     166    {
     167        // TODO: Support parsing of data types other than fixed; log warnings in other cases
     168        fixed oldval = node.ToFixed();
     169        fixed mod = fixed::FromString(CStrW(value));
     170        switch (method)
     171        {
     172        case SUB: mod = -mod;
     173        case ADD:
     174            node.m_Value = (oldval + mod).ToString().FromUTF8();
     175            break;
     176        case MUL:
     177            node.m_Value = (oldval.Multiply(mod)).ToString().FromUTF8();
     178            break;
     179        case DIV:
     180            if (mod.IsZero())
     181            {
     182                // Even though it's undefined, just return the original value
     183                LOGWARNING(L"Attempted divide-by-zero in '%hs'%ls'.", name.c_str(), sourceIdentifier ? (L" in '" + std::wstring(sourceIdentifier) + L"'").c_str() : L"");
     184                break;
     185            }
     186            node.m_Value = (oldval / mod).ToString().FromUTF8();
     187            break;
     188        }
     189        hasSetValue = true;
     190    }
    145191    if (!hasSetValue)
    146192        node.m_Value = value;
    147193
     
    155201    XERO_ITER_ATTR(element, attr)
    156202    {
    157203        // Skip special attributes
    158         if (attr.Name == at_replace) continue;
     204        if (attr.Name == at_replace || attr.Name == at_method)
     205            continue;
    159206        // Add any others
    160207        std::string attrName = xmb.GetAttributeString(attr.Name);
    161208        node.m_Childs["@" + attrName].m_Value = attr.Value.FromUTF8();