Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(gameclient): offer a command the multi-selection can do, not just its representative

Olcay Seygan12af87a99a65
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6fd79bffe..915456c18 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -113,6 +113,10 @@ shows that as the one thing you paid for and the one thing a cancel takes back.
Around it, the whole production loop got tightened:
+- **An upgrade you can buy is offered even if one of the selected buildings already has it.** Select
+ every barracks you own and the upgrade button used to grey out the moment the first one in the
+ group had bought it, leaving you to hunt the rest down one at a time. The button now lights if
+ *any* of them can still buy it, and the click goes to one that can.
- **Select several factories, click a unit, and it goes to whichever has the shortest queue.**
Shift-click queues five, spread the same way.
- **The nearest free worker takes a new job** — and a worker already halfway through a building does
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/ControlBar.h b/GeneralsMD/Code/GameEngine/Include/GameClient/ControlBar.h
index fa52d612e..3811fd362 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/ControlBar.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/ControlBar.h
@@ -889,6 +889,31 @@ protected:
// the following methods are for updating the currently showing context
CommandAvailability getCommandAvailability( const CommandButton *command, Object *obj, GameWindow *win, GameWindow *applyToWin = NULL, Bool forceDisabledEvaluation = FALSE ) const;
+
+public:
+ /** How permissive an availability is. The enum is not written in that order - COMMAND_RESTRICTED
+ * is 0 and COMMAND_AVAILABLE is 1, with HIDDEN and the two "nearly" states after them - so a
+ * group cannot just take the numeric max. Inline and static so a test can reach it. */
+ static Int commandAvailabilityRank( CommandAvailability a )
+ {
+ switch( a )
+ {
+ case COMMAND_ACTIVE: return 5;
+ case COMMAND_AVAILABLE: return 4;
+ case COMMAND_CANT_AFFORD: return 3; // the money is the player's; the button reads live
+ case COMMAND_NOT_READY: return 2;
+ case COMMAND_RESTRICTED: return 1;
+ default: return 0; // COMMAND_HIDDEN
+ }
+ }
+
+protected:
+ /** The same question asked of a whole multi-selection: a command one of the selected buildings
+ * can carry out is offered, not greyed out because the group's representative happens to have
+ * it already. Returns the best availability over the focused type group, and hands back the
+ * member that earned it so the press can be sent there. Outside a multi-selection it is just
+ * getCommandAvailability on the one selected object. */
+ CommandAvailability getGroupCommandAvailability( const CommandButton *command, Object *obj, GameWindow *win, Object **ableObj = NULL ) const;
void updateContextMultiSelect( void );
void updateContextPurchaseScience( void );
void updateContextCommand( void );
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommand.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommand.cpp
index 5be1a5b3e..82982009b 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommand.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommand.cpp
@@ -772,8 +772,8 @@ void ControlBar::updateContextCommand( void )
win->winClearStatus( WIN_STATUS_ALWAYS_COLOR );
}
- // is the command available
- CommandAvailability availability = getCommandAvailability( command, obj, win );
+ // is the command available - to any of the selected, not just to the representative
+ CommandAvailability availability = getGroupCommandAvailability( command, obj, win );
// enable/disable the window control
switch( availability )
@@ -943,6 +943,73 @@ static Int getRappellerCount(Object* obj)
return num;
}
+//-------------------------------------------------------------------------------------------------
+/** Ask a whole multi-selection whether it can do this, instead of asking its representative.
+ *
+ * The command bar picks one member of the focused type group to drive the context
+ * (ControlBar::setMultiSelectFocus), and every button was judged against that one object. Select
+ * four barracks where the first already has its upgrade and the button greys out, even though
+ * three of them are waiting to buy it - and clicking it would have gone to the first one anyway.
+ *
+ * So: take the best availability any member of the focused group can offer, and hand back the
+ * member that offered it so processCommandUI can send the order there. Outside a multi-selection
+ * this is exactly getCommandAvailability on the one selected object. */
+//-------------------------------------------------------------------------------------------------
+CommandAvailability ControlBar::getGroupCommandAvailability( const CommandButton *command,
+ Object *obj,
+ GameWindow *win,
+ Object **ableObj ) const
+{
+ if( ableObj )
+ *ableObj = obj;
+
+ CommandAvailability best = getCommandAvailability( command, obj, win );
+
+ if( m_currContext != CB_CONTEXT_MULTI_SELECT || best == COMMAND_AVAILABLE ||
+ m_multiSelectFocus < 0 || m_multiSelectFocus >= m_multiSelectGroupCount )
+ return best;
+
+ const ThingTemplate *focused = m_multiSelectGroupTemplate[ m_multiSelectFocus ];
+ if( focused == NULL )
+ return best;
+
+ //
+ // Hidden means the command does not belong on this bar at all - a different answer in kind
+ // from "this one cannot right now" - so a hidden representative stays hidden. Everything else
+ // is a matter of degree and the most permissive answer in the group wins.
+ //
+ if( best == COMMAND_HIDDEN )
+ return best;
+
+ const DrawableList *selected = TheInGameUI->getAllSelectedDrawables();
+ for( DrawableListCIt it = selected->begin(); it != selected->end(); ++it )
+ {
+ Drawable *draw = *it;
+ if( draw == NULL || draw->getTemplate() != focused )
+ continue;
+
+ Object *other = draw->getObject();
+ if( other == NULL || other == obj )
+ continue;
+
+ CommandAvailability avail = getCommandAvailability( command, other, win );
+ if( avail == COMMAND_HIDDEN )
+ continue; // it is on the bar for the group; this member just cannot say
+
+ if( commandAvailabilityRank( avail ) > commandAvailabilityRank( best ) )
+ {
+ best = avail;
+ if( ableObj )
+ *ableObj = other;
+ if( best == COMMAND_AVAILABLE )
+ break;
+ }
+ }
+
+ return best;
+
+} // end getGroupCommandAvailability
+
//-------------------------------------------------------------------------------------------------
/** What's the status between 'obj' and the 'command' at present. Can we do it? Are
* we already doing it? Can ya dig it? */
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cpp
index 42301acf9..75e4f13be 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cpp
@@ -205,7 +205,15 @@ CBCommandStatus ControlBar::processCommandUI( GameWindow *control,
obj = m_currentSelectedDrawable->getObject();
else if( commandButton->getCommandType() != GUI_COMMAND_UNIT_BUILD &&
m_currentSelectedDrawable != NULL )
+ {
+ //
+ // the button was offered because *someone* in the focused group can do this
+ // (getGroupCommandAvailability); send the order to that one rather than to the
+ // representative, which may be the very one that already has the upgrade
+ //
obj = m_currentSelectedDrawable->getObject();
+ getGroupCommandAvailability( commandButton, obj, control, &obj );
+ }
}
//@todo Kris -- Special case code so convoy trucks can detonate nuke trucks -- if other things need this,
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 8a0293717..4a85185b6 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -984,6 +984,37 @@ TEST(placement_snap_takes_the_nearest_45_on_both_halves_of_the_circle)
* two. Get that backwards and every second structure straddles a cell it only half fills,
* which is the gap-you-cannot-walk-through this snap exists to remove.
*/
+TEST(command_availability_rank_is_not_the_enum_order)
+{
+ /* the whole point of the helper: the enum is declared RESTRICTED, AVAILABLE, ACTIVE, HIDDEN,
+ * NOT_READY, CANT_AFFORD, so taking the numeric max over a multi-selection would rank
+ * "cannot afford" above "available" and "hidden" above both */
+ CHECK((Int)COMMAND_HIDDEN > (Int)COMMAND_AVAILABLE);
+ CHECK((Int)COMMAND_CANT_AFFORD > (Int)COMMAND_AVAILABLE);
+
+ /* permissiveness, most to least */
+ CHECK(ControlBar::commandAvailabilityRank(COMMAND_ACTIVE) >
+ ControlBar::commandAvailabilityRank(COMMAND_AVAILABLE));
+ CHECK(ControlBar::commandAvailabilityRank(COMMAND_AVAILABLE) >
+ ControlBar::commandAvailabilityRank(COMMAND_CANT_AFFORD));
+ CHECK(ControlBar::commandAvailabilityRank(COMMAND_CANT_AFFORD) >
+ ControlBar::commandAvailabilityRank(COMMAND_NOT_READY));
+ CHECK(ControlBar::commandAvailabilityRank(COMMAND_NOT_READY) >
+ ControlBar::commandAvailabilityRank(COMMAND_RESTRICTED));
+ CHECK(ControlBar::commandAvailabilityRank(COMMAND_RESTRICTED) >
+ ControlBar::commandAvailabilityRank(COMMAND_HIDDEN));
+
+ /* a group of four barracks where only the last can still buy the upgrade: the button is
+ * offered, because the best answer in the group wins */
+ CommandAvailability group[] = { COMMAND_RESTRICTED, COMMAND_RESTRICTED,
+ COMMAND_RESTRICTED, COMMAND_AVAILABLE };
+ CommandAvailability best = group[0];
+ for (Int i = 1; i < 4; i++)
+ if (ControlBar::commandAvailabilityRank(group[i]) > ControlBar::commandAvailabilityRank(best))
+ best = group[i];
+ CHECK_EQ((Int)best, (Int)COMMAND_AVAILABLE);
+}
+
TEST(placement_grid_snap_puts_footprint_edges_on_cell_lines)
{
const Real cell = 10.0f;