Skip to content

Commit

Permalink
Allow a selected group to have the attack command even if some units … (
Browse files Browse the repository at this point in the history
#217)

* Allow a selected group to have the attack command even if some units don't have it

* Add getUnitFromCC function

* return NULL if getFirstCtOfClass founds no unit

* Search for the unit containing the attack command in a non uniform selection

Co-authored-by: Rampoina <[email protected]>
  • Loading branch information
Rampoina and Rampoina authored Aug 8, 2022
1 parent 94fa13c commit 22a474b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
31 changes: 27 additions & 4 deletions source/glest_game/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,11 +695,18 @@ void Gui::mouseDownDisplayUnitSkills(int posDisplay) {
else {
activeCommandType= NULL;
activeCommandClass= display.getCommandClass(posDisplay);
if (activeCommandClass == ccAttack) {
unit= selection.getUnitFromCC(ccAttack);
}

}

//give orders depending on command type
if(!selection.isEmpty()){
const CommandType *ct= selection.getUnit(0)->getType()->getFirstCtOfClass(activeCommandClass);
if (activeCommandClass == ccAttack) {
ct = selection.getUnitFromCC(ccAttack)->getType()->getFirstCtOfClass(activeCommandClass);
}
if(activeCommandType!=NULL && activeCommandType->getClass()==ccBuild){
assert(selection.isUniform());
selectingBuilding= true;
Expand Down Expand Up @@ -832,8 +839,13 @@ void Gui::computeInfoString(int posDisplay){
const UnitType *ut= selection.getFrontUnit()->getType();
CommandClass cc= display.getCommandClass(posDisplay);
if(cc!=ccNull){
display.setInfoText(lang.getString("CommonCommand") + ": " + ut->getFirstCtOfClass(cc)->toString(true));
}
if (cc == ccAttack) {
const Unit* attackingUnit = selection.getUnitFromCC(ccAttack);
display.setInfoText(lang.getString("CommonCommand") + ": " + attackingUnit->getType()->getFirstCtOfClass(cc)->toString(true));
} else {
display.setInfoText(lang.getString("CommonCommand") + ": " + ut->getFirstCtOfClass(cc)->toString(true));
}
}
}
}
}
Expand Down Expand Up @@ -1016,9 +1028,19 @@ void Gui::computeDisplay(){

//printf("computeDisplay i = %d cc = %d isshared = %d lastCommand = %d\n",i,cc,isSharedCommandClass(cc),lastCommand);

if(isSharedCommandClass(cc) && cc != ccBuild){
const Unit* attackingUnit = NULL;
if (cc == ccAttack) {
attackingUnit = selection.getUnitFromCC(ccAttack);
}

if((cc == ccAttack && attackingUnit != NULL) || (isSharedCommandClass(cc) && cc != ccBuild)){
display.setDownLighted(lastCommand, true);
display.setDownImage(lastCommand, ut->getFirstCtOfClass(cc)->getImage());

if (cc == ccAttack && attackingUnit != NULL) {
display.setDownImage(lastCommand, attackingUnit->getType()->getFirstCtOfClass(cc)->getImage());
} else {
display.setDownImage(lastCommand, ut->getFirstCtOfClass(cc)->getImage());
}
display.setCommandClass(lastCommand, cc);
lastCommand++;
}
Expand Down Expand Up @@ -1179,6 +1201,7 @@ bool Gui::isSharedCommandClass(CommandClass commandClass){
return true;
}


void Gui::computeSelected(bool doubleClick, bool force){
Selection::UnitContainer units;

Expand Down
10 changes: 10 additions & 0 deletions source/glest_game/gui/selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,16 @@ void Selection::removeUnitFromGroup(int groupIndex,int unitId) {
}
}

const Unit* Selection::getUnitFromCC(CommandClass commandClass){
const Unit *unit = NULL;
for(int i=0; i<(int)selectedUnits.size(); ++i){
const Unit *unit= selectedUnits[i];
const CommandType *ct= unit->getType()->getFirstCtOfClass(commandClass);
if(ct != NULL && ct->getClass() == commandClass) return unit;
}
return unit;
}

//vector<Unit*> Selection::getUnitsForGroup(int groupIndex) {
// if(groupIndex < 0 || groupIndex >= maxGroups) {
// throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex));
Expand Down
1 change: 1 addition & 0 deletions source/glest_game/gui/selection.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class Selection: public UnitObserver {
const Unit *getUnit(int i) const {return selectedUnits[i];}
Unit *getUnitPtr(int i) {return selectedUnits[i];}
const Unit *getFrontUnit() const {return selectedUnits.front();}
const Unit *getUnitFromCC(CommandClass commandClass);
Vec3f getRefPos() const;
bool hasUnit(const Unit* unit) const;

Expand Down
1 change: 1 addition & 0 deletions source/glest_game/types/unit_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ const Level *UnitType::getLevel(string name) const {

const CommandType *UnitType::getFirstCtOfClass(CommandClass commandClass) const{
if(firstCommandTypeOfClass[commandClass] == NULL) {
return NULL;
//if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] commandClass = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,commandClass);

/*
Expand Down

0 comments on commit 22a474b

Please sign in to comment.