@@ -880,6 +880,7 @@ void UnitType::loaddd(int id,const string &dir, const TechTree *techTree,
880880 }
881881 }
882882 }
883+ sortCommandTypes (commandTypes);
883884
884885 computeFirstStOfClass ();
885886 computeFirstCtOfClass ();
@@ -1235,6 +1236,78 @@ void UnitType::computeFirstCtOfClass() {
12351236 }
12361237}
12371238
1239+ void UnitType::sortCommandTypes (CommandTypes cts){
1240+ CommandTypes ctBuf (cts);
1241+
1242+ CommandTypes ctCores;
1243+ CommandTypes ctBasics = {NULL ,NULL ,NULL ,NULL };
1244+ CommandTypes ctMorphs;
1245+
1246+ CommandTypes ctAttack;
1247+ CommandTypes ctBuild;
1248+
1249+ // Morphs
1250+ for (int i = (int )ctBuf.size (); i --> 0 ; ) {
1251+ if (ctBuf[i]->getClass () == ccMorph) {
1252+ ctMorphs.insert (ctMorphs.begin (), ctBuf[i]);
1253+ ctBuf.erase (ctBuf.begin () + i);
1254+ }
1255+ }
1256+
1257+ // Attacks
1258+ CommandTypeFilter (ctBuf, ctAttack, ccAttack);
1259+ if (ctAttack.size () > 0 ) {
1260+ ctBasics[0 ] = ctAttack[0 ];// first attack to basics
1261+ ctAttack.erase (ctAttack.begin ());// another to cores
1262+ ctCores.insert (ctCores.end (), ctAttack.begin (), ctAttack.end ());
1263+ }
1264+ auto basicsCC = CommandHelper::getBasicsCC ();
1265+ // removing attack cuz we catch all attacks above
1266+ basicsCC.erase ( basicsCC.begin () + CommandHelper::getBasicPos (ccAttack));
1267+
1268+ // Basics
1269+ for (int i = (int )ctBuf.size (); i --> 0 ; ) {
1270+ for (auto &&cc : basicsCC ){
1271+ if (ctBuf[i]->getClass () == cc) {
1272+ auto ccPos = CommandHelper::getBasicPos (cc);
1273+ ctBasics[ccPos] = ctBuf[i];
1274+ ctBuf.erase (ctBuf.begin () + i);
1275+ }
1276+ }
1277+ }
1278+
1279+ // //Cores
1280+ CommandTypeFilter (ctBuf, ctCores, ccProduce);
1281+ CommandTypeFilter (ctBuf, ctCores, ccUpgrade);
1282+ CommandTypeFilter (ctBuf, ctCores, ccSwitchTeam);
1283+ CommandTypeFilter (ctBuf, ctCores, ccHarvest);
1284+ CommandTypeFilter (ctBuf, ctCores, ccRepair);
1285+
1286+ // Build
1287+ CommandTypeFilter (ctBuf, ctBuild, ccBuild);// Build position always 4 in cores
1288+ if (ctCores.size () == 4 ) {/* do nothing*/ }
1289+ else if (ctCores.size () < 4 ) {
1290+ int nullCount = 4 - ctCores.size ();
1291+ for (int i=0 ; i<nullCount; i++){
1292+ ctCores.push_back (NULL );
1293+ }
1294+ if (ctBuild.size () > 0 ){
1295+ ctCores[3 ] = ctBuild[0 ];
1296+ }
1297+ }
1298+ commandTypesSorted.insert (commandTypesSorted.end (), ctCores.begin (), ctCores.end ());
1299+ commandTypesSorted.insert (commandTypesSorted.end (), ctBasics.begin (), ctBasics.end ());
1300+ commandTypesSorted.insert (commandTypesSorted.end (), ctMorphs.begin (), ctMorphs.end ());
1301+ }
1302+
1303+ void UnitType::CommandTypeFilter (CommandTypes &input, CommandTypes &output, CommandClass cc){
1304+ std::copy_if (input.begin (), input.end (), std::back_inserter (output), [cc](CommandType* i) {
1305+ if (i->getClass () == cc)
1306+ return true ;
1307+ else return false ;
1308+ });
1309+ }
1310+
12381311const CommandType* UnitType::findCommandTypeById (int id) const {
12391312 const HarvestEmergencyReturnCommandType *result = dynamic_cast <const HarvestEmergencyReturnCommandType *>(ctHarvestEmergencyReturnCommandType.get ());
12401313 if (result != NULL && id == result->getId ()) {
@@ -1259,6 +1332,15 @@ const CommandType *UnitType::getCommandType(int i) const {
12591332 return commandTypes[i];
12601333}
12611334
1335+ const CommandType *UnitType::getCommandTypeSorted (int i) const {
1336+ if (i >= (int )commandTypesSorted.size ()) {
1337+ char szBuf[8096 ]=" " ;
1338+ snprintf (szBuf,8096 ," In [%s::%s Line: %d] i >= commandTypesSorted.size(), i = %d, commandTypesSorted.size() = " MG_SIZE_T_SPECIFIER " " ,extractFileFromDirectoryPath (__FILE__).c_str (),__FUNCTION__,__LINE__,i,commandTypesSorted.size ());
1339+ throw megaglest_runtime_error (szBuf);
1340+ }
1341+ return commandTypesSorted[i];
1342+ }
1343+
12621344string UnitType::getCommandTypeListDesc () const {
12631345 string desc = " Commands: " ;
12641346 for (int i=0 ; i<getCommandTypeCount (); ++i){
0 commit comments