Skip to content

Commit

Permalink
Allow to lower difficulty down of campaign during gameplay (#7401)
Browse files Browse the repository at this point in the history
  • Loading branch information
ihhub authored Jul 15, 2023
1 parent d9dcfde commit 5157772
Showing 1 changed file with 59 additions and 42 deletions.
101 changes: 59 additions & 42 deletions src/fheroes2/game/game_campaign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ namespace
}
}

int32_t setCampaignDifficulty( int32_t currentDifficulty, const bool isSelectionAllowed )
int32_t setCampaignDifficulty( int32_t currentDifficulty, const int32_t maximumAllowedDifficulty )
{
const fheroes2::StandardWindow frameborder( 234, 270, true );
const fheroes2::Rect & windowRoi = frameborder.activeArea();
Expand Down Expand Up @@ -962,39 +962,38 @@ namespace
case Campaign::CampaignDifficulty::Easy:
currentDescription = easyDescription;
selection.setPosition( difficultyArea[0].x, difficultyArea[0].y );
if ( !isSelectionAllowed ) {
fheroes2::ApplyPalette( display, iconArea[1].x, iconArea[1].y, display, iconArea[1].x, iconArea[1].y, iconArea[1].width, iconArea[1].height,
PAL::GetPalette( PAL::PaletteType::GRAY ) );
fheroes2::ApplyPalette( display, iconArea[2].x, iconArea[2].y, display, iconArea[2].x, iconArea[2].y, iconArea[2].width, iconArea[2].height,
PAL::GetPalette( PAL::PaletteType::GRAY ) );
}
break;
case Campaign::CampaignDifficulty::Normal:
currentDescription = normalDescription;
selection.setPosition( difficultyArea[1].x, difficultyArea[1].y );
if ( !isSelectionAllowed ) {
fheroes2::ApplyPalette( display, iconArea[0].x, iconArea[0].y, display, iconArea[0].x, iconArea[0].y, iconArea[0].width, iconArea[0].height,
PAL::GetPalette( PAL::PaletteType::GRAY ) );
fheroes2::ApplyPalette( display, iconArea[2].x, iconArea[2].y, display, iconArea[2].x, iconArea[2].y, iconArea[2].width, iconArea[2].height,
PAL::GetPalette( PAL::PaletteType::GRAY ) );
}
break;
case Campaign::CampaignDifficulty::Hard:
currentDescription = hardDescription;
selection.setPosition( difficultyArea[2].x, difficultyArea[2].y );
if ( !isSelectionAllowed ) {
fheroes2::ApplyPalette( display, iconArea[0].x, iconArea[0].y, display, iconArea[0].x, iconArea[0].y, iconArea[0].width, iconArea[0].height,
PAL::GetPalette( PAL::PaletteType::GRAY ) );
fheroes2::ApplyPalette( display, iconArea[1].x, iconArea[1].y, display, iconArea[1].x, iconArea[1].y, iconArea[1].width, iconArea[1].height,
PAL::GetPalette( PAL::PaletteType::GRAY ) );
}
break;
default:
// Did you add a new difficulty level for campaigns? Add the logic above!
assert( 0 );
break;
}

const std::array<bool, 3> allowedSelection{ ( maximumAllowedDifficulty >= Campaign::CampaignDifficulty::Easy ),
( maximumAllowedDifficulty >= Campaign::CampaignDifficulty::Normal ),
( maximumAllowedDifficulty >= Campaign::CampaignDifficulty::Hard ) };

if ( !allowedSelection[0] ) {
fheroes2::ApplyPalette( display, iconArea[0].x, iconArea[0].y, display, iconArea[0].x, iconArea[0].y, iconArea[0].width, iconArea[0].height,
PAL::GetPalette( PAL::PaletteType::GRAY ) );
}
if ( !allowedSelection[1] ) {
fheroes2::ApplyPalette( display, iconArea[1].x, iconArea[1].y, display, iconArea[1].x, iconArea[1].y, iconArea[1].width, iconArea[1].height,
PAL::GetPalette( PAL::PaletteType::GRAY ) );
}
if ( !allowedSelection[2] ) {
fheroes2::ApplyPalette( display, iconArea[2].x, iconArea[2].y, display, iconArea[2].x, iconArea[2].y, iconArea[2].width, iconArea[2].height,
PAL::GetPalette( PAL::PaletteType::GRAY ) );
}

const int32_t textWidth = windowRoi.width - 16;
const fheroes2::Point textOffset{ windowRoi.x + 8, windowRoi.y + 135 };

Expand Down Expand Up @@ -1047,25 +1046,23 @@ namespace

bool updateInfo = false;

if ( isSelectionAllowed ) {
if ( le.MouseClickLeft( difficultyArea[0] ) ) {
currentDescription = easyDescription;
selection.setPosition( difficultyArea[0].x, difficultyArea[0].y );
currentDifficulty = Campaign::CampaignDifficulty::Easy;
updateInfo = true;
}
else if ( le.MouseClickLeft( difficultyArea[1] ) ) {
currentDescription = normalDescription;
selection.setPosition( difficultyArea[1].x, difficultyArea[1].y );
currentDifficulty = Campaign::CampaignDifficulty::Normal;
updateInfo = true;
}
else if ( le.MouseClickLeft( difficultyArea[2] ) ) {
currentDescription = hardDescription;
selection.setPosition( difficultyArea[2].x, difficultyArea[2].y );
currentDifficulty = Campaign::CampaignDifficulty::Hard;
updateInfo = true;
}
if ( allowedSelection[0] && le.MouseClickLeft( difficultyArea[0] ) ) {
currentDescription = easyDescription;
selection.setPosition( difficultyArea[0].x, difficultyArea[0].y );
currentDifficulty = Campaign::CampaignDifficulty::Easy;
updateInfo = true;
}
else if ( allowedSelection[1] && le.MouseClickLeft( difficultyArea[1] ) ) {
currentDescription = normalDescription;
selection.setPosition( difficultyArea[1].x, difficultyArea[1].y );
currentDifficulty = Campaign::CampaignDifficulty::Normal;
updateInfo = true;
}
else if ( allowedSelection[2] && le.MouseClickLeft( difficultyArea[2] ) ) {
currentDescription = hardDescription;
selection.setPosition( difficultyArea[2].x, difficultyArea[2].y );
currentDifficulty = Campaign::CampaignDifficulty::Hard;
updateInfo = true;
}

if ( updateInfo ) {
Expand Down Expand Up @@ -1390,7 +1387,6 @@ fheroes2::GameMode Game::SelectCampaignScenario( const fheroes2::GameMode prevMo
buttonViewIntro.draw();
buttonDifficulty.draw();

const bool isDifficultySelectionAllowed = campaignSaveData.isStarting() && !allowToRestart;
const bool isMapPresent = scenario.isMapFilePresent();

if ( allowToRestart ) {
Expand Down Expand Up @@ -1467,6 +1463,8 @@ fheroes2::GameMode Game::SelectCampaignScenario( const fheroes2::GameMode prevMo
const std::array<Game::HotKeyEvent, 3> hotKeyBonusChoice{ Game::HotKeyEvent::CAMPAIGN_SELECT_FIRST_BONUS, Game::HotKeyEvent::CAMPAIGN_SELECT_SECOND_BONUS,
Game::HotKeyEvent::CAMPAIGN_SELECT_THIRD_BONUS };

int32_t currentDifficulty = campaignSaveData.getDifficulty();

while ( le.HandleEvents() ) {
le.MousePressLeft( buttonCancel.area() ) ? buttonCancel.drawOnPress() : buttonCancel.drawOnRelease();
le.MousePressLeft( buttonOk.area() ) ? buttonOk.drawOnPress() : buttonOk.drawOnRelease();
Expand Down Expand Up @@ -1527,15 +1525,27 @@ fheroes2::GameMode Game::SelectCampaignScenario( const fheroes2::GameMode prevMo
}
else if ( le.MousePressRight( buttonDifficulty.area() ) ) {
fheroes2::showMessage( fheroes2::Text( _( "Campaign Difficulty" ), fheroes2::FontType::normalYellow() ),
fheroes2::Text( _( "Select campaign difficulty. It cannot be changed after." ), fheroes2::FontType::normalWhite() ), Dialog::ZERO );
fheroes2::Text( _( "Select the campaign difficulty. This can be lowered at any point during the campaign." ),
fheroes2::FontType::normalWhite() ),
Dialog::ZERO );
}
else if ( buttonRestart.isVisible() && le.MousePressRight( buttonRestart.area() ) ) {
fheroes2::showMessage( fheroes2::Text( _( "Restart" ), fheroes2::FontType::normalYellow() ),
fheroes2::Text( _( "Restart the current scenario." ), fheroes2::FontType::normalWhite() ), Dialog::ZERO );
}
else if ( ( buttonOk.isEnabled() && ( le.MouseClickLeft( buttonOk.area() ) || HotKeyPressEvent( HotKeyEvent::DEFAULT_OKAY ) ) ) || restartButtonClicked ) {
if ( ( !campaignSaveData.isStarting() || allowToRestart ) && currentDifficulty != campaignSaveData.getDifficulty()
&& fheroes2::showStandardTextMessage( _( "Difficulty" ),
_( "You have changed to a lower difficulty for the campaign. You will not be able to revert this after this "
"point. The high score will be calculated based solely on the new difficulty. Do you want to proceed?" ),
Dialog::YES | Dialog::NO )
== Dialog::NO ) {
continue;
}

if ( restartButtonClicked
&& Dialog::Message( _( "Restart" ), _( "Are you sure you want to restart this scenario?" ), Font::BIG, Dialog::YES | Dialog::NO ) == Dialog::NO ) {
&& fheroes2::showStandardTextMessage( _( "Restart" ), _( "Are you sure you want to restart this scenario?" ), Dialog::YES | Dialog::NO )
== Dialog::NO ) {
continue;
}

Expand Down Expand Up @@ -1595,6 +1605,7 @@ fheroes2::GameMode Game::SelectCampaignScenario( const fheroes2::GameMode prevMo
applyObtainedCampaignAwards( currentScenarioInfoId, campaignSaveData.getObtainedCampaignAwards() );

campaignSaveData.setCurrentScenarioInfo( currentScenarioInfoId, scenarioBonusId.value_or( -1 ) );
campaignSaveData.setDifficulty( currentDifficulty );

return fheroes2::GameMode::START_GAME;
}
Expand All @@ -1618,7 +1629,13 @@ fheroes2::GameMode Game::SelectCampaignScenario( const fheroes2::GameMode prevMo
fheroes2::Text( _( "The number of days spent on this campaign." ), fheroes2::FontType::normalWhite() ), Dialog::ZERO );
}
else if ( le.MouseClickLeft( buttonDifficulty.area() ) || HotKeyPressEvent( HotKeyEvent::CAMPAIGN_SELECT_DIFFICULTY ) ) {
campaignSaveData.setDifficulty( setCampaignDifficulty( campaignSaveData.getDifficulty(), isDifficultySelectionAllowed ) );
if ( campaignSaveData.isStarting() && !allowToRestart ) {
currentDifficulty = setCampaignDifficulty( currentDifficulty, Campaign::CampaignDifficulty::Hard );
}
else {
currentDifficulty = setCampaignDifficulty( currentDifficulty, campaignSaveData.getDifficulty() );
}

display.render();
}
}
Expand Down

0 comments on commit 5157772

Please sign in to comment.