diff --git a/Generals/Code/GameEngine/Include/GameNetwork/GUIUtil.h b/Generals/Code/GameEngine/Include/GameNetwork/GUIUtil.h index 862270b69a8..d6c536b877f 100644 --- a/Generals/Code/GameEngine/Include/GameNetwork/GUIUtil.h +++ b/Generals/Code/GameEngine/Include/GameNetwork/GUIUtil.h @@ -50,3 +50,16 @@ void EnableAcceptControls(Bool Enabled, GameInfo *myGame, GameWindow *comboPlaye GameWindow *comboColor[], GameWindow *comboPlayerTemplate[], GameWindow *comboTeam[], GameWindow *buttonAccept[], GameWindow *buttonStart, GameWindow *buttonMapStartPosition[], Int slotNum = -1); + +// TheSuperHackers @feature Gives every occupied, non-observer slot a random army from the armies +// selectable in the army combo box. Returns TRUE if any slot was changed. +Bool RandomizeArmies( GameInfo *myGame ); + +// TheSuperHackers @feature Returns a random army from the armies selectable in the army combo box, +// or PLAYERTEMPLATE_RANDOM if there are none. +Int GetRandomSelectableArmy( GameInfo *myGame ); + +// TheSuperHackers @feature Creates a "Random Armies" button styled like 'startButton', with the same +// height, centered between 'startButton' and 'backButton'. Clicks go to the start button's owner. +GameWindow *CreateRandomArmiesButton( GameWindow *startButton, GameWindow *backButton, + const char *layoutFilename ); diff --git a/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanGameOptionsMenu.cpp b/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanGameOptionsMenu.cpp index 6f2c14b5964..dec92a8850c 100644 --- a/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanGameOptionsMenu.cpp +++ b/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanGameOptionsMenu.cpp @@ -128,6 +128,7 @@ static GameWindow *comboBoxColor[MAX_SLOTS] = {0}; static GameWindow *comboBoxPlayerTemplate[MAX_SLOTS] = {0}; static GameWindow *comboBoxTeam[MAX_SLOTS] = {0}; +static GameWindow *buttonRandomArmies = nullptr; //static GameWindow *buttonStartPosition[MAX_SLOTS] = {0}; // @@ -712,11 +713,16 @@ void InitLanGameGadgets() if( buttonAccept[0] ) GadgetButtonSetEnabledColor(buttonAccept[0], acceptTrueColor ); + // only the host rolls the armies; everybody else sees the result in the army column + buttonRandomArmies = CreateRandomArmiesButton( buttonStart, buttonBack, "LanGameOptionsMenu.wnd" ); + if (buttonRandomArmies) + buttonRandomArmies->winHide( !TheLAN->AmIHost() ); } void DeinitLanGameGadgets() { parentLanGameOptions = nullptr; + buttonRandomArmies = nullptr; buttonEmote = nullptr; buttonSelectMap = nullptr; buttonStart = nullptr; @@ -1144,6 +1150,19 @@ WindowMsgHandledType LanGameOptionsMenuSystem( GameWindow *window, UnsignedInt m if (!txtInput.isEmpty()) TheLAN->RequestChat(txtInput, LANAPIInterface::LANCHAT_EMOTE); } + else if ( buttonRandomArmies && control == buttonRandomArmies ) + { + LANGameInfo *myGame = TheLAN->GetMyGame(); + if (myGame && myGame->amIHost() && RandomizeArmies( myGame )) + { + // the armies can be rolled only once per lobby + buttonRandomArmies->winEnable( FALSE ); + // send around the new slotlist; everyone has to accept the new armies again + myGame->resetAccepted(); + TheLAN->RequestGameOptions(GenerateGameOptionsString(), true); + lanUpdateSlotList(); + } + } else if ( controlID == buttonSelectMapID ) { //buttonBack->winEnable( false ); diff --git a/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanMapSelectMenu.cpp b/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanMapSelectMenu.cpp index 87dfe8b7b51..a92cb8b58ac 100644 --- a/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanMapSelectMenu.cpp +++ b/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanMapSelectMenu.cpp @@ -72,6 +72,7 @@ static const char *gadgetsToHide[] = "MapWindow", //"StaticTextTitle", "StaticTextTeam", + "ButtonRandomArmies", "StaticTextFaction", "StaticTextColor", "TextEntryMapDisplay", diff --git a/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp b/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp index 4ade01370cb..805967f20d6 100644 --- a/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp +++ b/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp @@ -128,6 +128,7 @@ static GameWindow *comboBoxColor[MAX_SLOTS] = {0}; static GameWindow *comboBoxPlayerTemplate[MAX_SLOTS] = {0}; static GameWindow *comboBoxTeam[MAX_SLOTS] = {0}; +static GameWindow *buttonRandomArmies = nullptr; //static GameWindow *buttonStartPosition[MAX_SLOTS] = {0}; // @@ -1083,6 +1084,8 @@ void InitSkirmishGameGadgets() // buttonStartPosition[i]->winHide(TRUE); // not using these right now } + buttonRandomArmies = CreateRandomArmiesButton( buttonStart, buttonExit, "SkirmishGameOptionsMenu.wnd" ); + populateSkirmishBattleHonors(); } @@ -1131,6 +1134,7 @@ static const char *gadgetsToHide[] = "StaticTextColor", "StaticTextTeam", "StaticTextFaction", + "ButtonRandomArmies", nullptr }; static const char *perPlayerGadgetsToHide[] = @@ -1524,6 +1528,15 @@ WindowMsgHandledType SkirmishGameOptionsMenuSystem( GameWindow *window, Unsigned // GameWindow *sliderGameSpeed = TheWindowManager->winGetWindowFromId( parentSkirmishGameOptions, sliderGameSpeedID ); // GadgetSliderSetPosition( sliderGameSpeed, TheGlobalData->m_framesPerSecondLimit ); // } + else if ( buttonRandomArmies && control == buttonRandomArmies ) + { + if (RandomizeArmies( TheSkirmishGameInfo )) + { + // the armies can be rolled only once per lobby + buttonRandomArmies->winEnable( FALSE ); + skirmishUpdateSlotList(); + } + } else if ( controlID == buttonSelectMapID ) { sandboxOk = FALSE; diff --git a/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishMapSelectMenu.cpp b/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishMapSelectMenu.cpp index 4e94afc580b..e8e45a2601a 100644 --- a/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishMapSelectMenu.cpp +++ b/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishMapSelectMenu.cpp @@ -143,6 +143,10 @@ void showSkirmishGameOptionsUnderlyingGUIElements( Bool show ) win = TheWindowManager->winGetWindowFromId( parent, TheNameKeyGenerator->nameToKey("SkirmishGameOptionsMenu.wnd:StaticTextTeam") ); win->winHide( !show ); + win = TheWindowManager->winGetWindowFromId( parent, TheNameKeyGenerator->nameToKey("SkirmishGameOptionsMenu.wnd:ButtonRandomArmies") ); + if (win) + win->winHide( !show ); + win = TheWindowManager->winGetWindowFromId( parent, TheNameKeyGenerator->nameToKey("SkirmishGameOptionsMenu.wnd:StaticTextFaction") ); win->winHide( !show ); diff --git a/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLGameSetupMenu.cpp b/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLGameSetupMenu.cpp index f0f4b8df4a5..5f49b1fdaaf 100644 --- a/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLGameSetupMenu.cpp +++ b/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLGameSetupMenu.cpp @@ -211,6 +211,7 @@ static GameWindow *comboBoxColor[MAX_SLOTS] = {0}; static GameWindow *comboBoxPlayerTemplate[MAX_SLOTS] = {0}; static GameWindow *comboBoxTeam[MAX_SLOTS] = {0}; +static GameWindow *buttonRandomArmies = nullptr; //static GameWindow *buttonStartPosition[MAX_SLOTS] = {0}; // @@ -1112,11 +1113,17 @@ void InitWOLGameGadgets() buttonBack->winEnable(TRUE); } //GadgetButtonSetEnabledColor(buttonAccept[0], GameSpyColor[GSCOLOR_ACCEPT_TRUE]); + + // only the host rolls the armies; everybody else sees the result in the army column + buttonRandomArmies = CreateRandomArmiesButton( buttonStart, buttonBack, "GameSpyGameOptionsMenu.wnd" ); + if (buttonRandomArmies) + buttonRandomArmies->winHide( !TheGameSpyInfo->amIHost() ); } void DeinitWOLGameGadgets() { parentWOLGameSetup = nullptr; + buttonRandomArmies = nullptr; buttonEmote = nullptr; buttonSelectMap = nullptr; buttonStart = nullptr; @@ -2518,6 +2525,19 @@ WindowMsgHandledType WOLGameSetupMenuSystem( GameWindow *window, UnsignedInt msg if (!txtInput.isEmpty()) TheGameSpyInfo->sendChat(txtInput, FALSE, nullptr); // 'emote' button is now carriage-return } + else if ( buttonRandomArmies && control == buttonRandomArmies ) + { + GameInfo *myGame = TheGameSpyInfo->getCurrentStagingRoom(); + if (myGame && TheGameSpyInfo->amIHost() && RandomizeArmies( myGame )) + { + // the armies can be rolled only once per lobby + buttonRandomArmies->winEnable( FALSE ); + // send around the new slotlist; everyone has to accept the new armies again + myGame->resetAccepted(); + TheGameSpyInfo->setGameOptions(); + WOLDisplaySlotList(); + } + } else if ( controlID == buttonSelectMapID ) { WOLMapSelectLayout = TheWindowManager->winCreateLayout( "Menus/WOLMapSelectMenu.wnd" ); diff --git a/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLMapSelectMenu.cpp b/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLMapSelectMenu.cpp index 3cfc71b607a..a2068dd5080 100644 --- a/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLMapSelectMenu.cpp +++ b/Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLMapSelectMenu.cpp @@ -90,6 +90,7 @@ static const char *gadgetsToHide[] = "MapWindow", //"StaticTextGameName", "StaticTextTeam", + "ButtonRandomArmies", "StaticTextFaction", "StaticTextColor", "TextEntryMapDisplay", diff --git a/Generals/Code/GameEngine/Source/GameNetwork/GUIUtil.cpp b/Generals/Code/GameEngine/Source/GameNetwork/GUIUtil.cpp index befeb2e56a6..4ecaf79e4be 100644 --- a/Generals/Code/GameEngine/Source/GameNetwork/GUIUtil.cpp +++ b/Generals/Code/GameEngine/Source/GameNetwork/GUIUtil.cpp @@ -43,6 +43,7 @@ #include "GameNetwork/GameInfo.h" #include "Common/PlayerTemplate.h" #include "GameNetwork/LANAPICallbacks.h" // for acceptTrueColor, etc +#include "GameClient/ClientRandomValue.h" // ----------------------------------------------------------------------------- @@ -229,17 +230,10 @@ void PopulateColorComboBox(Int comboBox, GameWindow *comboArray[], GameInfo *myG // ----------------------------------------------------------------------------- -void PopulatePlayerTemplateComboBox(Int comboBox, GameWindow *comboArray[], GameInfo *myGame, Bool allowObservers) +// Collects the player template indices that can be picked in the army combo box. +static void getSelectablePlayerTemplates(std::vector &templates) { Int numPlayerTemplates = ThePlayerTemplateStore->getPlayerTemplateCount(); - UnicodeString playerTemplateName; - - GadgetComboBoxReset(comboArray[comboBox]); - - MultiplayerColorDefinition *def = TheMultiplayerSettings->getColor(PLAYERTEMPLATE_RANDOM); - Int newIndex = GadgetComboBoxAddEntry(comboArray[comboBox], TheGameText->fetch("GUI:Random"), def->getColor()); - GadgetComboBoxSetItemData(comboArray[comboBox], newIndex, (void *)PLAYERTEMPLATE_RANDOM); - std::set seenSides; for (Int c=0; cgetColor(PLAYERTEMPLATE_RANDOM); + Int newIndex = GadgetComboBoxAddEntry(comboArray[comboBox], TheGameText->fetch("GUI:Random"), def->getColor()); + GadgetComboBoxSetItemData(comboArray[comboBox], newIndex, (void *)PLAYERTEMPLATE_RANDOM); + + std::vector templates; + getSelectablePlayerTemplates(templates); + + for (size_t t = 0; t < templates.size(); ++t) + { + const Int c = templates[t]; + const PlayerTemplate *fac = ThePlayerTemplateStore->getNthPlayerTemplate(c); + AsciiString side; + side.format("SIDE:%s", fac->getSide().str()); newIndex = GadgetComboBoxAddEntry(comboArray[comboBox], TheGameText->fetch(side), def->getColor()); GadgetComboBoxSetItemData(comboArray[comboBox], newIndex, (void *)c); } - seenSides.clear(); // disabling observers for Multiplayer test if (allowObservers) @@ -306,6 +320,145 @@ void PopulateTeamComboBox(Int comboBox, GameWindow *comboArray[], GameInfo *myGa // ----------------------------------------------------------------------------- +Int GetRandomSelectableArmy( GameInfo *myGame ) +{ + if (!myGame) + return PLAYERTEMPLATE_RANDOM; + + // pick from exactly the armies the army combo box offers + std::vector templates; + getSelectablePlayerTemplates(templates); + if (templates.empty()) + return PLAYERTEMPLATE_RANDOM; + + return templates[GameClientRandomValue(0, (Int)templates.size() - 1)]; +} + +Bool RandomizeArmies( GameInfo *myGame ) +{ + if (!myGame) + return FALSE; + + Bool changed = FALSE; + for (Int i = 0; i < MAX_SLOTS; ++i) + { + GameSlot *slot = myGame->getSlot(i); + + // every slot that will actually play (humans and AI, but no observers) + if (!slot || !slot->isOccupied() || slot->getPlayerTemplate() == PLAYERTEMPLATE_OBSERVER) + continue; + + const Int army = GetRandomSelectableArmy(myGame); + if (army == PLAYERTEMPLATE_RANDOM) + return changed; + + slot->setPlayerTemplate(army); + changed = TRUE; + } + + return changed; +} + +// ----------------------------------------------------------------------------- + +GameWindow *CreateRandomArmiesButton( GameWindow *startButton, GameWindow *backButton, + const char *layoutFilename ) +{ + if (!startButton || !backButton) + return nullptr; + + GameWindow *parent = startButton->winGetParent(); + + AsciiString buttonName; + buttonName.format("%s:ButtonRandomArmies", layoutFilename); + const NameKeyType buttonID = NAMEKEY(buttonName); + + // the menu may be initialized again while its windows still exist; reuse the button then. + // A new lobby gets a new roll, so make it clickable again. + if (GameWindow *existing = TheWindowManager->winGetWindowFromId( parent, buttonID )) + { + existing->winEnable( TRUE ); + return existing; + } + + // copy the look of the start button (frame, images, text colors and font) + WinInstanceData *styleData = startButton->winGetInstanceData(); + WinInstanceData instData; + instData.init(); + BitSet( instData.m_style, GWS_PUSH_BUTTON | GWS_MOUSE_TRACK ); + for (Int d = 0; d < MAX_DRAW_DATA; ++d) + { + instData.m_enabledDrawData[d] = styleData->m_enabledDrawData[d]; + instData.m_disabledDrawData[d] = styleData->m_disabledDrawData[d]; + instData.m_hiliteDrawData[d] = styleData->m_hiliteDrawData[d]; + } + instData.m_enabledText = styleData->m_enabledText; + instData.m_disabledText = styleData->m_disabledText; + instData.m_hiliteText = styleData->m_hiliteText; + instData.m_imageOffset = styleData->m_imageOffset; + instData.setTooltipText( TheGameText->FETCH_OR_SUBSTITUTE("TOOLTIP:RandomArmies", + L"Give every player a random army now, so everyone can see who plays what before the game starts.") ); + + UnsignedInt status = WIN_STATUS_ENABLED; + if (BitIsSet( startButton->winGetStatus(), WIN_STATUS_IMAGE )) + status |= WIN_STATUS_IMAGE; + + // start out with exactly the size and row of the start button + Int startX, startY, startW, startH; + startButton->winGetScreenPosition( &startX, &startY ); + startButton->winGetSize( &startW, &startH ); + + GameWindow *button = TheWindowManager->gogoGadgetPushButton( parent, status, + 0, 0, + startW, startH, + &instData, startButton->winGetFont(), FALSE ); + + if (!button) + return nullptr; + + button->winSetWindowId( buttonID ); + + // Route GBM_SELECTED to the same window that receives the start button messages. + button->winSetOwner( startButton->winGetOwner() ); + GadgetButtonSetText( button, TheGameText->FETCH_OR_SUBSTITUTE("GUI:RandomArmies", L"RANDOM ARMIES") ); + + // Same height as the start button and slightly narrower, which still leaves + // enough room for the text on a single line. + Int width = startW * 85 / 100; + + // center it between the start and the back button, without overlapping them + Int backX, backY, backW, backH; + backButton->winGetScreenPosition( &backX, &backY ); + backButton->winGetSize( &backW, &backH ); + + Int leftEdge = startX + startW; + Int rightEdge = backX; + if (backX < startX) + { + // the back button is on the left side + leftEdge = backX + backW; + rightEdge = startX; + } + const Int maxWidth = (rightEdge - leftEdge) - startH; + if (maxWidth > 0 && width > maxWidth) + width = maxWidth; + + Int screenX = (leftEdge + rightEdge) / 2 - width / 2; + Int screenY = startY; + + // convert to coordinates relative to the parent + Int parentX = 0, parentY = 0; + if (parent) + parent->winGetScreenPosition( &parentX, &parentY ); + + button->winSetSize( width, startH ); + button->winSetPosition( screenX - parentX, screenY - parentY ); + + return button; +} + +// ----------------------------------------------------------------------------- + // ----------------------------------------------------------------------------------------- // The slot list displaying function //------------------------------------------------------------------------------------------------- diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/GUIUtil.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/GUIUtil.h index d09d47d1532..75286e5bc36 100644 --- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/GUIUtil.h +++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/GUIUtil.h @@ -50,3 +50,16 @@ void EnableAcceptControls(Bool Enabled, GameInfo *myGame, GameWindow *comboPlaye GameWindow *comboColor[], GameWindow *comboPlayerTemplate[], GameWindow *comboTeam[], GameWindow *buttonAccept[], GameWindow *buttonStart, GameWindow *buttonMapStartPosition[], Int slotNum = -1); + +// TheSuperHackers @feature Gives every occupied, non-observer slot a random army from the armies +// selectable in the army combo box. Returns TRUE if any slot was changed. +Bool RandomizeArmies( GameInfo *myGame ); + +// TheSuperHackers @feature Returns a random army from the armies selectable in the army combo box, +// or PLAYERTEMPLATE_RANDOM if there are none. +Int GetRandomSelectableArmy( GameInfo *myGame ); + +// TheSuperHackers @feature Creates a "Random Armies" button styled like 'startButton', with the same +// height, centered between 'startButton' and 'backButton'. Clicks go to the start button's owner. +GameWindow *CreateRandomArmiesButton( GameWindow *startButton, GameWindow *backButton, + const char *layoutFilename ); diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanGameOptionsMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanGameOptionsMenu.cpp index 53cda5a2a2e..41d04c76f47 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanGameOptionsMenu.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanGameOptionsMenu.cpp @@ -135,6 +135,7 @@ static GameWindow *comboBoxColor[MAX_SLOTS] = {0}; static GameWindow *comboBoxPlayerTemplate[MAX_SLOTS] = {0}; static GameWindow *comboBoxTeam[MAX_SLOTS] = {0}; +static GameWindow *buttonRandomArmies = nullptr; //static GameWindow *buttonStartPosition[MAX_SLOTS] = {0}; // @@ -786,6 +787,10 @@ void InitLanGameGadgets() if( buttonAccept[0] ) GadgetButtonSetEnabledColor(buttonAccept[0], acceptTrueColor ); + // only the host rolls the armies; everybody else sees the result in the army column + buttonRandomArmies = CreateRandomArmiesButton( buttonStart, buttonBack, "LanGameOptionsMenu.wnd" ); + if (buttonRandomArmies) + buttonRandomArmies->winHide( !TheLAN->AmIHost() ); } void DeinitLanGameGadgets() @@ -800,6 +805,7 @@ void DeinitLanGameGadgets() textEntryMapDisplay = nullptr; checkboxLimitSuperweapons = nullptr; comboBoxStartingCash = nullptr; + buttonRandomArmies = nullptr; if (windowMap) { windowMap->winSetUserData(nullptr); @@ -1247,6 +1253,19 @@ WindowMsgHandledType LanGameOptionsMenuSystem( GameWindow *window, UnsignedInt m if (!txtInput.isEmpty()) TheLAN->RequestChat(txtInput, LANAPIInterface::LANCHAT_EMOTE); } + else if ( buttonRandomArmies && control == buttonRandomArmies ) + { + LANGameInfo *myGame = TheLAN->GetMyGame(); + if (myGame && myGame->amIHost() && RandomizeArmies( myGame )) + { + // the armies can be rolled only once per lobby + buttonRandomArmies->winEnable( FALSE ); + // send around the new slotlist; everyone has to accept the new armies again + myGame->resetAccepted(); + TheLAN->RequestGameOptions(GenerateGameOptionsString(), true); + lanUpdateSlotList(); + } + } else if ( controlID == buttonSelectMapID ) { //buttonBack->winEnable( false ); diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanMapSelectMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanMapSelectMenu.cpp index b79272a6a66..9c91824bcb3 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanMapSelectMenu.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanMapSelectMenu.cpp @@ -72,6 +72,7 @@ static const char *gadgetsToHide[] = "MapWindow", //"StaticTextTitle", "StaticTextTeam", + "ButtonRandomArmies", "StaticTextFaction", "StaticTextColor", "TextEntryMapDisplay", diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp index d071904490d..37cff877102 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp @@ -136,6 +136,7 @@ static GameWindow *comboBoxColor[MAX_SLOTS] = {0}; static GameWindow *comboBoxPlayerTemplate[MAX_SLOTS] = {0}; static GameWindow *comboBoxTeam[MAX_SLOTS] = {0}; +static GameWindow *buttonRandomArmies = nullptr; //static GameWindow *buttonStartPosition[MAX_SLOTS] = {0}; // @@ -1171,6 +1172,8 @@ void InitSkirmishGameGadgets() // buttonStartPosition[i]->winHide(TRUE); // not using these right now } + buttonRandomArmies = CreateRandomArmiesButton( buttonStart, buttonExit, "SkirmishGameOptionsMenu.wnd" ); + populateSkirmishBattleHonors(); } @@ -1219,6 +1222,7 @@ static const char *gadgetsToHide[] = "StaticTextColor", "StaticTextTeam", "StaticTextFaction", + "ButtonRandomArmies", nullptr }; static const char *perPlayerGadgetsToHide[] = @@ -1649,6 +1653,15 @@ WindowMsgHandledType SkirmishGameOptionsMenuSystem( GameWindow *window, Unsigned // GameWindow *sliderGameSpeed = TheWindowManager->winGetWindowFromId( parentSkirmishGameOptions, sliderGameSpeedID ); // GadgetSliderSetPosition( sliderGameSpeed, TheGlobalData->m_framesPerSecondLimit ); // } + else if ( buttonRandomArmies && control == buttonRandomArmies ) + { + if (RandomizeArmies( TheSkirmishGameInfo )) + { + // the armies can be rolled only once per lobby + buttonRandomArmies->winEnable( FALSE ); + skirmishUpdateSlotList(); + } + } else if ( controlID == buttonSelectMapID ) { sandboxOk = FALSE; diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishMapSelectMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishMapSelectMenu.cpp index c58852bd572..13cc1223f2c 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishMapSelectMenu.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishMapSelectMenu.cpp @@ -146,6 +146,10 @@ void showSkirmishGameOptionsUnderlyingGUIElements( Bool show ) win = TheWindowManager->winGetWindowFromId( parent, TheNameKeyGenerator->nameToKey("SkirmishGameOptionsMenu.wnd:StaticTextTeam") ); win->winHide( !show ); + win = TheWindowManager->winGetWindowFromId( parent, TheNameKeyGenerator->nameToKey("SkirmishGameOptionsMenu.wnd:ButtonRandomArmies") ); + if (win) + win->winHide( !show ); + win = TheWindowManager->winGetWindowFromId( parent, TheNameKeyGenerator->nameToKey("SkirmishGameOptionsMenu.wnd:StaticTextFaction") ); win->winHide( !show ); diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLGameSetupMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLGameSetupMenu.cpp index d8c0b2c2eba..a21448f45e1 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLGameSetupMenu.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLGameSetupMenu.cpp @@ -247,6 +247,8 @@ static GameWindow *comboBoxTeam[MAX_SLOTS] = {NULL,NULL,NULL,NULL, //static GameWindow *buttonStartPosition[MAX_SLOTS] = {NULL,NULL,NULL,NULL, // NULL,NULL,NULL,NULL }; + +static GameWindow *buttonRandomArmies = NULL; // static GameWindow *buttonMapStartPosition[MAX_SLOTS] = {NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL }; @@ -1821,6 +1823,11 @@ void InitWOLGameGadgets() { buttonBack->winEnable(TRUE); } + + // GeneralsOnline: the lobby service only lets each player change their own army (and the host + // the AI armies), so every player gets the button and rolls their own army. Everybody sees the + // result in the army column. + buttonRandomArmies = CreateRandomArmiesButton( buttonStart, buttonBack, "GameSpyGameOptionsMenu.wnd" ); //GadgetButtonSetEnabledColor(buttonAccept[0], GameSpyColor[GSCOLOR_ACCEPT_TRUE]); // TODO_NGMP: Where does this happen in the normal game? @@ -1853,6 +1860,7 @@ void DeinitWOLGameGadgets() checkBoxUseStats = NULL; checkBoxLimitSuperweapons = NULL; comboBoxStartingCash = NULL; + buttonRandomArmies = NULL; // GameWindow *staticTextTitle = NULL; for (Int i = 0; i < MAX_SLOTS; i++) @@ -4130,6 +4138,48 @@ WindowMsgHandledType WOLGameSetupMenuSystem( GameWindow *window, UnsignedInt msg } } } + else if ( buttonRandomArmies && control == buttonRandomArmies ) + { + NGMP_OnlineServices_LobbyInterface* pLobbyInterface = NGMP_OnlineServicesManager::GetInterface(); + NGMPGame* myGame = pLobbyInterface == nullptr ? nullptr : pLobbyInterface->GetCurrentGame(); + if (myGame) + { + // roll my own army, and as host also the AI armies + Bool rolled = FALSE; + for (Int i = 0; i < MAX_SLOTS; ++i) + { + GameSlot *slot = myGame->getSlot(i); + if (!slot || slot->getPlayerTemplate() == PLAYERTEMPLATE_OBSERVER) + continue; + + const Bool isMine = (i == myGame->getLocalSlotNum()); + const Bool isMyAI = slot->isAI() && pLobbyInterface->IsHost(); + if (!isMine && !isMyAI) + continue; + + const Int army = GetRandomSelectableArmy( myGame ); + if (army == PLAYERTEMPLATE_RANDOM) + break; + + // select the army in the combo box and apply it like a manual selection, + // which also sends it to the lobby service + for (Int idx = 0; idx < GadgetComboBoxGetLength(comboBoxPlayerTemplate[i]); ++idx) + { + if ((Int)(intptr_t)GadgetComboBoxGetItemData(comboBoxPlayerTemplate[i], idx) == army) + { + GadgetComboBoxSetSelectedPos(comboBoxPlayerTemplate[i], idx, TRUE); + handlePlayerTemplateSelection(i); + rolled = TRUE; + break; + } + } + } + + // the army can be rolled only once per lobby + if (rolled) + buttonRandomArmies->winEnable( FALSE ); + } + } else if ( controlID == buttonSelectMapID ) { WOLMapSelectLayout = TheWindowManager->winCreateLayout( "Menus/WOLMapSelectMenu.wnd" ); diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLMapSelectMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLMapSelectMenu.cpp index d898c52b9ff..6d2bbc3f8e8 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLMapSelectMenu.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLMapSelectMenu.cpp @@ -94,6 +94,7 @@ static const char *gadgetsToHide[] = "MapWindow", //"StaticTextGameName", "StaticTextTeam", + "ButtonRandomArmies", "StaticTextFaction", "StaticTextColor", "TextEntryMapDisplay", diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GUIUtil.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GUIUtil.cpp index 7e3231fcee1..915bac20e75 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GUIUtil.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GUIUtil.cpp @@ -45,6 +45,7 @@ #include "Common/PlayerTemplate.h" #include "GameNetwork/LANAPICallbacks.h" // for acceptTrueColor, etc #include "GameClient/ChallengeGenerals.h" +#include "GameClient/ClientRandomValue.h" // ----------------------------------------------------------------------------- @@ -73,7 +74,7 @@ void EnableAcceptControls(Bool Enabled, GameInfo *myGame, GameWindow *comboPlaye Bool isObserver = myGame->getConstSlot(slotNum)->getPlayerTemplate() == PLAYERTEMPLATE_OBSERVER; - if( !myGame->amIHost() && (buttonStart != nullptr) ) + if( !myGame->amIHost() && (buttonStart != nullptr) ) buttonStart->winEnable(Enabled); if(comboColor[slotNum]) { @@ -134,7 +135,7 @@ void ShowUnderlyingGUIElements( Bool show, const char *layoutFilename, const cha AsciiString parentNameStr; parentNameStr.format("%s:%s", layoutFilename, parentName); NameKeyType parentID = NAMEKEY(parentNameStr); - GameWindow *parent = TheWindowManager->winGetWindowFromId( nullptr, parentID ); + GameWindow *parent = TheWindowManager->winGetWindowFromId( nullptr, parentID ); if (!parent) { DEBUG_CRASH(("Window %s not found", parentNameStr.str())); @@ -238,17 +239,10 @@ void PopulateColorComboBox(Int comboBox, GameWindow *comboArray[], GameInfo *myG // ----------------------------------------------------------------------------- -void PopulatePlayerTemplateComboBox(Int comboBox, GameWindow *comboArray[], GameInfo *myGame, Bool allowObservers) +// Collects the player template indices that can be picked in the army combo box. +static void getSelectablePlayerTemplates(GameInfo *myGame, std::vector &templates) { Int numPlayerTemplates = ThePlayerTemplateStore->getPlayerTemplateCount(); - UnicodeString playerTemplateName; - - GadgetComboBoxReset(comboArray[comboBox]); - - MultiplayerColorDefinition *def = TheMultiplayerSettings->getColor(PLAYERTEMPLATE_RANDOM); - Int newIndex = GadgetComboBoxAddEntry(comboArray[comboBox], TheGameText->fetch("GUI:Random"), def->getColor()); - GadgetComboBoxSetItemData(comboArray[comboBox], newIndex, (void *)PLAYERTEMPLATE_RANDOM); - std::set seenSides; for (Int c=0; cgetColor(PLAYERTEMPLATE_RANDOM); + Int newIndex = GadgetComboBoxAddEntry(comboArray[comboBox], TheGameText->fetch("GUI:Random"), def->getColor()); + GadgetComboBoxSetItemData(comboArray[comboBox], newIndex, (void *)PLAYERTEMPLATE_RANDOM); + + std::vector templates; + getSelectablePlayerTemplates(myGame, templates); + + for (size_t t = 0; t < templates.size(); ++t) + { + const Int c = templates[t]; + const PlayerTemplate *fac = ThePlayerTemplateStore->getNthPlayerTemplate(c); + AsciiString side; + side.format("SIDE:%s", fac->getSide().str()); newIndex = GadgetComboBoxAddEntry(comboArray[comboBox], TheGameText->fetch(side), def->getColor()); GadgetComboBoxSetItemData(comboArray[comboBox], newIndex, (void *)c); } - seenSides.clear(); // disabling observers for Multiplayer test if (allowObservers) @@ -348,6 +362,145 @@ void PopulateTeamComboBox(Int comboBox, GameWindow *comboArray[], GameInfo *myGa } } +// ----------------------------------------------------------------------------- + +Int GetRandomSelectableArmy( GameInfo *myGame ) +{ + if (!myGame) + return PLAYERTEMPLATE_RANDOM; + + // pick from exactly the armies the army combo box offers + std::vector templates; + getSelectablePlayerTemplates(myGame, templates); + if (templates.empty()) + return PLAYERTEMPLATE_RANDOM; + + return templates[GameClientRandomValue(0, (Int)templates.size() - 1)]; +} + +Bool RandomizeArmies( GameInfo *myGame ) +{ + if (!myGame) + return FALSE; + + Bool changed = FALSE; + for (Int i = 0; i < MAX_SLOTS; ++i) + { + GameSlot *slot = myGame->getSlot(i); + + // every slot that will actually play (humans and AI, but no observers) + if (!slot || !slot->isOccupied() || slot->getPlayerTemplate() == PLAYERTEMPLATE_OBSERVER) + continue; + + const Int army = GetRandomSelectableArmy(myGame); + if (army == PLAYERTEMPLATE_RANDOM) + return changed; + + slot->setPlayerTemplate(army); + changed = TRUE; + } + + return changed; +} + +// ----------------------------------------------------------------------------- + +GameWindow *CreateRandomArmiesButton( GameWindow *startButton, GameWindow *backButton, + const char *layoutFilename ) +{ + if (!startButton || !backButton) + return nullptr; + + GameWindow *parent = startButton->winGetParent(); + + AsciiString buttonName; + buttonName.format("%s:ButtonRandomArmies", layoutFilename); + const NameKeyType buttonID = NAMEKEY(buttonName); + + // the menu may be initialized again while its windows still exist; reuse the button then. + // A new lobby gets a new roll, so make it clickable again. + if (GameWindow *existing = TheWindowManager->winGetWindowFromId( parent, buttonID )) + { + existing->winEnable( TRUE ); + return existing; + } + + // copy the look of the start button (frame, images, text colors and font) + WinInstanceData *styleData = startButton->winGetInstanceData(); + WinInstanceData instData; + instData.init(); + BitSet( instData.m_style, GWS_PUSH_BUTTON | GWS_MOUSE_TRACK ); + for (Int d = 0; d < MAX_DRAW_DATA; ++d) + { + instData.m_enabledDrawData[d] = styleData->m_enabledDrawData[d]; + instData.m_disabledDrawData[d] = styleData->m_disabledDrawData[d]; + instData.m_hiliteDrawData[d] = styleData->m_hiliteDrawData[d]; + } + instData.m_enabledText = styleData->m_enabledText; + instData.m_disabledText = styleData->m_disabledText; + instData.m_hiliteText = styleData->m_hiliteText; + instData.m_imageOffset = styleData->m_imageOffset; + instData.setTooltipText( TheGameText->FETCH_OR_SUBSTITUTE("TOOLTIP:RandomArmies", + L"Give every player a random army now, so everyone can see who plays what before the game starts.") ); + + UnsignedInt status = WIN_STATUS_ENABLED; + if (BitIsSet( startButton->winGetStatus(), WIN_STATUS_IMAGE )) + status |= WIN_STATUS_IMAGE; + + // start out with exactly the size and row of the start button + Int startX, startY, startW, startH; + startButton->winGetScreenPosition( &startX, &startY ); + startButton->winGetSize( &startW, &startH ); + + GameWindow *button = TheWindowManager->gogoGadgetPushButton( parent, status, + 0, 0, + startW, startH, + &instData, startButton->winGetFont(), FALSE ); + + if (!button) + return nullptr; + + button->winSetWindowId( buttonID ); + + // Route GBM_SELECTED to the same window that receives the start button messages. + button->winSetOwner( startButton->winGetOwner() ); + GadgetButtonSetText( button, TheGameText->FETCH_OR_SUBSTITUTE("GUI:RandomArmies", L"RANDOM ARMIES") ); + + // Same height as the start button and slightly narrower, which still leaves + // enough room for the text on a single line. + Int width = startW * 85 / 100; + + // center it between the start and the back button, without overlapping them + Int backX, backY, backW, backH; + backButton->winGetScreenPosition( &backX, &backY ); + backButton->winGetSize( &backW, &backH ); + + Int leftEdge = startX + startW; + Int rightEdge = backX; + if (backX < startX) + { + // the back button is on the left side + leftEdge = backX + backW; + rightEdge = startX; + } + const Int maxWidth = (rightEdge - leftEdge) - startH; + if (maxWidth > 0 && width > maxWidth) + width = maxWidth; + + Int screenX = (leftEdge + rightEdge) / 2 - width / 2; + Int screenY = startY; + + // convert to coordinates relative to the parent + Int parentX = 0, parentY = 0; + if (parent) + parent->winGetScreenPosition( &parentX, &parentY ); + + button->winSetSize( width, startH ); + button->winSetPosition( screenX - parentX, screenY - parentY ); + + return button; +} + // ----------------------------------------------------------------------------- static UnicodeString formatMoneyForStartingCashComboBox( const Money & moneyAmount ) { @@ -505,10 +658,10 @@ void UpdateSlotList( GameInfo *myGame, GameWindow *comboPlayer[], comboPlayer[i]->winEnable( FALSE ); } //if( i == myGame->getLocalSlotNum()) - if((comboColor[i] != nullptr) && BitIsSet(comboColor[i]->winGetStatus(), WIN_STATUS_ENABLED)) + if((comboColor[i] != nullptr) && BitIsSet(comboColor[i]->winGetStatus(), WIN_STATUS_ENABLED)) PopulateColorComboBox(i, comboColor, myGame, myGame->getConstSlot(i)->getPlayerTemplate() == PLAYERTEMPLATE_OBSERVER); Int max, idx; - if (comboColor[i] != nullptr) { + if (comboColor[i] != nullptr) { max = GadgetComboBoxGetLength(comboColor[i]); for (idx=0; idx