Ticket #2531: default_formations.diff

File default_formations.diff, 7.2 KB (added by sanderd17, 10 years ago)
  • binaries/data/mods/public/simulation/components/Identity.js

     
    5757        "</element>" +
    5858    "</optional>" +
    5959    "<optional>" +
    60         "<element name='Formations' a:help='Optional list of space-separated formations this unit is allowed to use. Choices include: Scatter, Box, ColumnClosed, LineClosed, ColumnOpen, LineOpen, Flank, Skirmish, Wedge, Testudo, Phalanx, Syntagma, BattleLine'>" +
     60        "<element name='Formations' a:help='Optional list of space-separated formations templates (see simulation/templates/formations for possible templates)'>" +
    6161            "<attribute name='datatype'>" +
    6262                "<value>tokens</value>" +
    6363            "</attribute>" +
     
    6565        "</element>" +
    6666    "</optional>" +
    6767    "<optional>" +
     68            "<element name='DefaultFormation'>" +
     69                "<text/>" +
     70            "</element>" +
     71    "</optional>" +
     72    "<optional>" +
    6873        "<element name='Icon'>" +
    6974            "<text/>" +
    7075        "</element>" +
     
    106111    return this.GetClassesList().indexOf(name) != -1;
    107112};
    108113
     114Identity.prototype.GetDefaultFormation = function()
     115{
     116    if (this.template.DefaultFormation)
     117        return this.template.DefaultFormation;
     118    return "formations/line_closed";
     119};
     120
     121
    109122Identity.prototype.GetFormationsList = function()
    110123{
    111124    if (this.template.Formations && "_string" in this.template.Formations)
  • binaries/data/mods/public/simulation/helpers/Commands.js

     
    11571157}
    11581158
    11591159/**
     1160 * Get an alternative formation for this list of entities
     1161 * If the entities had a prior formation, this will be preferred,
     1162 * if they don't the default formation will be preferred
     1163 */
     1164function GetAlternativeFormation(ents)
     1165{
     1166    var r = "formations/line_closed";
     1167    var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
     1168    // object in the form
     1169    // {"formations/line_closed": 5, "formations/testudo": 10}
     1170    var defaultFormations = {};
     1171    // count the default formations of the members
     1172    for each (var ent in ents)
     1173    {
     1174        var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
     1175        if (!cmpUnitAI)
     1176            continue;
     1177        var formation = cmpUnitAI.GetLastFormationTemplate();
     1178        if (formation)
     1179        {
     1180            if (defaultFormations[formation])
     1181                defaultFormations[formation]++;
     1182            else
     1183                defaultFormations[formation] = 1;
     1184            continue;
     1185        }
     1186        var cmpIdentity = Engine.QueryInterface(ent, IID_Identity);
     1187        if (!cmpIdentity)
     1188            continue;
     1189        var formation = cmpIdentity.GetDefaultFormation();
     1190        if (formation)
     1191        {
     1192            if (defaultFormations[formation])
     1193                defaultFormations[formation]++;
     1194            else
     1195                defaultFormations[formation] = 1;
     1196        }
     1197    }
     1198    var mostWantedFormationCount = 0;
     1199    for (let formation in defaultFormations)
     1200    {
     1201        if (defaultFormations[formation] > mostWantedFormationCount)
     1202        {
     1203            r = formation;
     1204            mostWantedFormationCount = defaultFormations[formation];
     1205        }
     1206    }
     1207    return r;
     1208}
     1209
     1210/**
    11601211 * Returns a list of UnitAI components, each belonging either to a
    11611212 * selected unit or to a formation entity for groups of the selected units.
    11621213 */
     
    11761227        return [ cmpUnitAI ];
    11771228    }
    11781229
     1230    var defaultFormationTemplate = GetAlternativeFormation(ents);
    11791231    // Separate out the units that don't support the chosen formation
    11801232    var formedEnts = [];
    11811233    var nonformedUnitAIs = [];
     
    11901242        // TODO: We only check if the formation is usable by some units
    11911243        // if we move them to it. We should check if we can use formations
    11921244        // for the other cases.
    1193         if (cmpIdentity && cmpIdentity.CanUseFormation(formationTemplate || "formations/line_closed"))
     1245        if (cmpIdentity && cmpIdentity.CanUseFormation(formationTemplate || defaultFormationTemplate))
    11941246            formedEnts.push(ent);
    11951247        else
    11961248            nonformedUnitAIs.push(cmpUnitAI);
     
    12421294        {
    12431295            if (!formationTemplate || !CanMoveEntsIntoFormation(cluster, formationTemplate))
    12441296            {
    1245                 // get the most recently used formation, or default to line closed
    1246                 var lastFormationTemplate = undefined;
    1247                 for each (var ent in cluster)
    1248                 {
    1249                     var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
    1250                     if (cmpUnitAI)
    1251                     {
    1252                         var template = cmpUnitAI.GetLastFormationTemplate();
    1253                         if (lastFormationTemplate === undefined)
    1254                         {
    1255                             lastFormationTemplate = template;
    1256                         }
    1257                         else if (lastFormationTemplate != template)
    1258                         {
    1259                             lastFormationTemplate = undefined;
    1260                             break;
    1261                         }
    1262                     }
    1263                 }
    1264                 if (lastFormationTemplate && CanMoveEntsIntoFormation(cluster, lastFormationTemplate))
    1265                     formationTemplate = lastFormationTemplate;
     1297                // get the most recently used formation
     1298                if (defaultFormationTemplate && CanMoveEntsIntoFormation(cluster, defaultFormationTemplate))
     1299                    formationTemplate = defaultFormationTemplate;
    12661300                else
    12671301                    formationTemplate = "formations/line_closed";
    12681302            }
  • binaries/data/mods/public/simulation/templates/units/rome_infantry_spearman_b.xml

     
    1616    <Icon>units/rome_infantry_spearman.png</Icon>
    1717    <History>The Triarii were the third line of heavy infantry in the Republican Roman army. Made up of the veterans from previous campaigns, they often possessed the most fashionable arms and armor. Armed with a spear and gladius, the Triarii usually did not fight unless the battle was going poorly or enemy forces required the use of a spear to fend them off. In many cases the Triarii were left at the camp instead of marching out with the rest of the army.</History>
    1818    <RequiredTechnology>phase_town</RequiredTechnology>
     19    <DefaultFormation>formations/testudo</DefaultFormation>
    1920  </Identity>
    2021  <Promotion>
    2122    <Entity>units/rome_infantry_spearman_a</Entity>
  • binaries/data/mods/public/simulation/templates/units/rome_infantry_swordsman_b.xml

     
    2828    <SpecificName>Hastatus</SpecificName>
    2929    <Icon>units/rome_infantry_swordsman.png</Icon>
    3030    <History>The hastati were the first line of heavy infantry in the early Republican Roman army. They used pila and gladii as their main weapons, throwing the former into the enemy formation and then closing with the sword. For defense they used a large shield called a scutum and wore the best armor they could afford. Hastati were recruited from the young men of Rome, a small part of the 700,000 troops Rome could bring to bear against its opponents.</History>
     31    <DefaultFormation>formations/testudo</DefaultFormation>
    3132  </Identity>
    3233  <Promotion>
    3334    <Entity>units/rome_infantry_swordsman_a</Entity>