diff --git a/source/glest_game/gui/gui.cpp b/source/glest_game/gui/gui.cpp index 1147c2ad..39c1161d 100644 --- a/source/glest_game/gui/gui.cpp +++ b/source/glest_game/gui/gui.cpp @@ -97,7 +97,6 @@ Gui::Gui(){ if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] START\n",__FILE__,__FUNCTION__); lastGroupRecall = -1; - numberCommands=0; posObjWorld= Vec2i(54, 14); validPosObjWorld= false; activeCommandType= NULL; @@ -454,15 +453,11 @@ void Gui::hotKey(SDL_KeyboardEvent key) { if(isKeyPressed(configKeys.getSDLKey(name.c_str()),key) == true) { if(activeCommandType != NULL && activeCommandType->getClass() == ccBuild) { mouseDownDisplayUnitBuild(i); - computeDisplay(); - break; } else { - if (i < numberCommands) { - mouseDownDisplayUnitSkills(i); - computeDisplay(); - } - break; + mouseDownDisplayUnitSkills(i); } + computeDisplay(); + break; } } } @@ -720,7 +715,7 @@ void Gui::mouseDownDisplayUnitSkills(int posDisplay) { } //give orders depending on command type - if(!selection.isEmpty()){ + if(activeCommandClass != ccNull){ const CommandType *ct= selection.getUnit(0)->getType()->getFirstCtOfClass(activeCommandClass); if (activeCommandClass == ccAttack) { ct = selection.getUnitFromCC(ccAttack)->getType()->getFirstCtOfClass(activeCommandClass); @@ -737,7 +732,7 @@ void Gui::mouseDownDisplayUnitSkills(int posDisplay) { selectingPos= true; activePos= posDisplay; } - } + } else { posDisplay= invalidPos;} } else{ activePos= posDisplay; @@ -994,10 +989,11 @@ void Gui::computeDisplay(){ if(u->isBuilt()){ //printf("u->isBuilt()\n"); - int morphPos= 8; - for(int i= 0; i < ut->getCommandTypeCount(); ++i){ + int morphPos= CommandHelper::getMorphPos(); + for(int i= 0; i < ut->getCommandTypeSortedCount(); ++i){ int displayPos= i; - const CommandType *ct= ut->getCommandType(i); + const CommandType *ct= ut->getCommandTypeSorted(i); + if(ct == NULL) continue; if(ct->getClass() == ccMorph) { displayPos= morphPos++; } @@ -1040,38 +1036,38 @@ void Gui::computeDisplay(){ } } } - numberCommands = displayPos; } - numberCommands++; } } else{ //printf("selection.isUniform() == FALSE\n"); //non uniform selection - int lastCommand= 0; - for(int i= 0; i < ccCount; ++i){ - CommandClass cc= static_cast (i); + int basicPos= CommandHelper::getBasicPos(); + // only basics can be shared + for(auto &&cc : CommandHelper::getBasicsCC()){ - //printf("computeDisplay i = %d cc = %d isshared = %d lastCommand = %d\n",i,cc,isSharedCommandClass(cc),lastCommand); + //printf("computeDisplay i = %d cc = %d isshared = %d basicPos = %d\n",i,cc,isSharedCommandClass(cc),basicPos); const Unit* attackingUnit = NULL; if (cc == ccAttack) { attackingUnit = selection.getUnitFromCC(ccAttack); } + auto ccPos = CommandHelper::getBasicPos(cc); + if((cc == ccAttack && attackingUnit != NULL) || (isSharedCommandClass(cc) && cc != ccBuild)){ - display.setDownLighted(lastCommand, true); + display.setDownLighted(basicPos + ccPos, true); if (cc == ccAttack && attackingUnit != NULL) { - display.setDownImage(lastCommand, attackingUnit->getType()->getFirstCtOfClass(cc)->getImage()); + display.setDownImage(basicPos + ccPos, attackingUnit->getType()->getFirstCtOfClass(cc)->getImage()); } else { - display.setDownImage(lastCommand, ut->getFirstCtOfClass(cc)->getImage()); + display.setDownImage(basicPos + ccPos, ut->getFirstCtOfClass(cc)->getImage()); } + display.setCommandClass(basicPos + ccPos, cc); display.setCommandClass(lastCommand, cc); lastCommand++; } } - numberCommands = lastCommand; } } else if (activeCommandType != NULL && activeCommandType->getClass() == ccBuild) { diff --git a/source/glest_game/gui/gui.h b/source/glest_game/gui/gui.h index a731d26e..d44831b1 100644 --- a/source/glest_game/gui/gui.h +++ b/source/glest_game/gui/gui.h @@ -125,7 +125,6 @@ private: CommandClass activeCommandClass; int activePos; int lastPosDisplay; - int numberCommands; //composite Display display; diff --git a/source/glest_game/types/command_type.cpp b/source/glest_game/types/command_type.cpp index b254a368..4bda6c0c 100644 --- a/source/glest_game/types/command_type.cpp +++ b/source/glest_game/types/command_type.cpp @@ -31,6 +31,20 @@ using namespace Shared::Util; namespace Glest{ namespace Game{ +// ===================================================== +// class CommandHelper +// ===================================================== + +int CommandHelper::getBasicPos(CommandClass cc){ + switch(cc) { + case ccAttack: return 0; break; + case ccStop: return 1; break; + case ccMove: return 2; break; + case ccAttackStopped: return 3; break; + default: + return ccNull; + } +} // ===================================================== // class CommandType diff --git a/source/glest_game/types/command_type.h b/source/glest_game/types/command_type.h index 913272c5..5fec4979 100644 --- a/source/glest_game/types/command_type.h +++ b/source/glest_game/types/command_type.h @@ -66,6 +66,18 @@ enum Queueability { qAlways }; +class CommandHelper {// TODO put magic numbers to settings +public: + inline static int getCorePos() { return 0; } + inline static int getBasicPos() { return 4; } + inline static int getMorphPos() { return 8; } + static int getBasicPos(CommandClass cc); + inline static vector getBasicsCC() { return { ccAttack, ccStop, ccMove, ccAttackStopped }; } + +private: + CommandHelper(){ } +}; + // ===================================================== // class CommandType // diff --git a/source/glest_game/types/unit_type.cpp b/source/glest_game/types/unit_type.cpp index ca682ed7..9bce2081 100644 --- a/source/glest_game/types/unit_type.cpp +++ b/source/glest_game/types/unit_type.cpp @@ -880,6 +880,7 @@ void UnitType::loaddd(int id,const string &dir, const TechTree *techTree, } } } + sortCommandTypes(commandTypes); computeFirstStOfClass(); computeFirstCtOfClass(); @@ -1236,6 +1237,78 @@ void UnitType::computeFirstCtOfClass() { } } +void UnitType::sortCommandTypes(CommandTypes cts){ + CommandTypes ctBuf(cts); + + CommandTypes ctCores; + CommandTypes ctBasics = {NULL,NULL,NULL,NULL}; + CommandTypes ctMorphs; + + CommandTypes ctAttack; + CommandTypes ctBuild; + + //Morphs + for(int i = (int)ctBuf.size(); i --> 0; ) { + if(ctBuf[i]->getClass() == ccMorph) { + ctMorphs.insert(ctMorphs.begin(), ctBuf[i]); + ctBuf.erase(ctBuf.begin() + i); + } + } + + //Attacks + CommandTypeFilter(ctBuf, ctAttack, ccAttack); + if(ctAttack.size() > 0) { + ctBasics[0] = ctAttack[0];// first attack to basics + ctAttack.erase(ctAttack.begin());// another to cores + ctCores.insert(ctCores.end(), ctAttack.begin(), ctAttack.end()); + } + auto basicsCC = CommandHelper::getBasicsCC(); + // removing attack cuz we catch all attacks above + basicsCC.erase( basicsCC.begin() + CommandHelper::getBasicPos(ccAttack)); + + //Basics + for(int i = (int)ctBuf.size(); i --> 0; ) { + for(auto &&cc : basicsCC ){ + if(ctBuf[i]->getClass() == cc) { + auto ccPos = CommandHelper::getBasicPos(cc); + ctBasics[ccPos] = ctBuf[i]; + ctBuf.erase(ctBuf.begin() + i); + } + } + } + +// //Cores + CommandTypeFilter(ctBuf, ctCores, ccProduce); + CommandTypeFilter(ctBuf, ctCores, ccUpgrade); + CommandTypeFilter(ctBuf, ctCores, ccSwitchTeam); + CommandTypeFilter(ctBuf, ctCores, ccHarvest); + CommandTypeFilter(ctBuf, ctCores, ccRepair); + + //Build + CommandTypeFilter(ctBuf, ctBuild, ccBuild);// Build position always 4 in cores + if(ctCores.size() == 4) {/*do nothing*/ } + else if(ctCores.size() < 4) { + int nullCount = 4 - ctCores.size(); + for(int i=0; i 0){ + ctCores[3] = ctBuild[0]; + } + } + commandTypesSorted.insert(commandTypesSorted.end(), ctCores.begin(), ctCores.end()); + commandTypesSorted.insert(commandTypesSorted.end(), ctBasics.begin(), ctBasics.end()); + commandTypesSorted.insert(commandTypesSorted.end(), ctMorphs.begin(), ctMorphs.end()); +} + +void UnitType::CommandTypeFilter(CommandTypes &input, CommandTypes &output, CommandClass cc){ + std::copy_if(input.begin(), input.end(), std::back_inserter(output), [cc](CommandType* i) { + if(i->getClass() == cc) + return true; + else return false; + }); +} + const CommandType* UnitType::findCommandTypeById(int id) const{ const HarvestEmergencyReturnCommandType *result = dynamic_cast(ctHarvestEmergencyReturnCommandType.get()); if(result != NULL && id == result->getId()) { @@ -1260,6 +1333,15 @@ const CommandType *UnitType::getCommandType(int i) const { return commandTypes[i]; } +const CommandType *UnitType::getCommandTypeSorted(int i) const { + if(i >= (int)commandTypesSorted.size()) { + char szBuf[8096]=""; + 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()); + throw megaglest_runtime_error(szBuf); + } + return commandTypesSorted[i]; +} + string UnitType::getCommandTypeListDesc() const { string desc = "Commands: "; for(int i=0; i