feat: standart for cammand grid

This commit is contained in:
pavanvo 2022-08-11 13:00:09 +04:00 committed by Rampoina
parent 46d5904578
commit f1e4a1bf83
6 changed files with 132 additions and 24 deletions

View File

@ -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<CommandClass> (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) {

View File

@ -125,7 +125,6 @@ private:
CommandClass activeCommandClass;
int activePos;
int lastPosDisplay;
int numberCommands;
//composite
Display display;

View File

@ -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

View File

@ -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<CommandClass> getBasicsCC() { return { ccAttack, ccStop, ccMove, ccAttackStopped }; }
private:
CommandHelper(){ }
};
// =====================================================
// class CommandType
//

View File

@ -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<nullCount; i++){
ctCores.push_back(NULL);
}
if(ctBuild.size() > 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<const HarvestEmergencyReturnCommandType *>(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<getCommandTypeCount(); ++i){

View File

@ -215,6 +215,7 @@ private:
//info
SkillTypes skillTypes;
CommandTypes commandTypes;
CommandTypes commandTypesSorted;
StoredResources storedResources;
Levels levels;
LootableResources lootableResources;
@ -273,10 +274,12 @@ public:
inline const ArmorType *getArmorType() const {return armorType;}
inline const SkillType *getSkillType(int i) const {return skillTypes[i];}
const CommandType *getCommandType(int i) const;
const CommandType *getCommandTypeSorted(int i) const;
inline const Level *getLevel(int i) const {return &levels[i];}
const Level *getLevel(string name) const;
inline int getSkillTypeCount() const {return (int)skillTypes.size();}
inline int getCommandTypeCount() const {return (int)commandTypes.size();}
inline int getCommandTypeSortedCount() const {return (int)commandTypesSorted.size();}
inline int getLevelCount() const {return (int)levels.size();}
inline bool getLight() const {return light;}
inline bool getRotationAllowed() const {return rotationAllowed;}
@ -361,6 +364,8 @@ public:
private:
void computeFirstStOfClass();
void computeFirstCtOfClass();
void sortCommandTypes(CommandTypes cts);
void CommandTypeFilter(CommandTypes &input, CommandTypes &output, CommandClass cc);
};
/**