CBonusSelection.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * CBonusSelection.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CBonusSelection.h"
  12. #include <vcmi/spells/Spell.h>
  13. #include <vcmi/spells/Service.h>
  14. #include "CSelectionBase.h"
  15. #include "ExtraOptionsTab.h"
  16. #include "../CPlayerInterface.h"
  17. #include "../CServerHandler.h"
  18. #include "../mainmenu/CMainMenu.h"
  19. #include "../mainmenu/CPrologEpilogVideo.h"
  20. #include "../media/IMusicPlayer.h"
  21. #include "../widgets/CComponent.h"
  22. #include "../widgets/Buttons.h"
  23. #include "../widgets/MiscWidgets.h"
  24. #include "../widgets/ObjectLists.h"
  25. #include "../widgets/TextControls.h"
  26. #include "../widgets/VideoWidget.h"
  27. #include "../windows/GUIClasses.h"
  28. #include "../windows/InfoWindows.h"
  29. #include "../render/IImage.h"
  30. #include "../render/IRenderHandler.h"
  31. #include "../render/CAnimation.h"
  32. #include "../render/Graphics.h"
  33. #include "../GameEngine.h"
  34. #include "../GameInstance.h"
  35. #include "../gui/Shortcut.h"
  36. #include "../gui/WindowHandler.h"
  37. #include "../adventureMap/AdventureMapInterface.h"
  38. #include "../../lib/CConfigHandler.h"
  39. #include "../../lib/CCreatureHandler.h"
  40. #include "../../lib/CSkillHandler.h"
  41. #include "../../lib/GameLibrary.h"
  42. #include "../../lib/StartInfo.h"
  43. #include "../../lib/campaign/CampaignState.h"
  44. #include "../../lib/entities/artifact/CArtifact.h"
  45. #include "../../lib/entities/building/CBuilding.h"
  46. #include "../../lib/entities/building/CBuildingHandler.h"
  47. #include "../../lib/entities/faction/CFaction.h"
  48. #include "../../lib/entities/faction/CTown.h"
  49. #include "../../lib/entities/faction/CTownHandler.h"
  50. #include "../../lib/entities/hero/CHeroHandler.h"
  51. #include "../../lib/spells/CSpellHandler.h"
  52. #include "../../lib/filesystem/Filesystem.h"
  53. #include "../../lib/mapObjects/CGHeroInstance.h"
  54. #include "../../lib/mapping/CMapHeader.h"
  55. #include "../../lib/mapping/CMapInfo.h"
  56. #include "../../lib/mapping/CMapService.h"
  57. #include "../../lib/texts/CGeneralTextHandler.h"
  58. std::shared_ptr<CampaignState> CBonusSelection::getCampaign()
  59. {
  60. return GAME->server().si->campState;
  61. }
  62. CBonusSelection::CBonusSelection()
  63. : CWindowObject(BORDERED)
  64. {
  65. OBJECT_CONSTRUCTION;
  66. setBackground(getCampaign()->getRegions().getBackgroundName());
  67. panelBackground = std::make_shared<CPicture>(ImagePath::builtin("CAMPBRF.BMP"), 456, 6);
  68. const auto & playVideo = [this]()
  69. {
  70. ENGINE->windows().createAndPushWindow<CPrologEpilogVideo>(
  71. getCampaign()->scenario(GAME->server().campaignMap).prolog,
  72. [this]() { redraw(); } );
  73. };
  74. buttonStart = std::make_shared<CButton>(
  75. Point(475, 536), AnimationPath::builtin("CBBEGIB.DEF"), CButton::tooltip(), std::bind(&CBonusSelection::startMap, this), EShortcut::GLOBAL_ACCEPT
  76. );
  77. buttonRestart = std::make_shared<CButton>(Point(475, 536), AnimationPath::builtin("CBRESTB.DEF"), CButton::tooltip(), std::bind(&CBonusSelection::restartMap, this), EShortcut::GLOBAL_ACCEPT);
  78. buttonVideo = std::make_shared<CButton>(Point(705, 214), AnimationPath::builtin("CBVIDEB.DEF"), CButton::tooltip(), playVideo, EShortcut::LOBBY_REPLAY_VIDEO);
  79. buttonBack = std::make_shared<CButton>(Point(624, 536), AnimationPath::builtin("CBCANCB.DEF"), CButton::tooltip(), std::bind(&CBonusSelection::goBack, this), EShortcut::GLOBAL_CANCEL);
  80. campaignName = std::make_shared<CLabel>(481, 28, FONT_BIG, ETextAlignment::TOPLEFT, Colors::YELLOW, GAME->server().si->getCampaignName(), 250);
  81. iconsMapSizes = std::make_shared<CAnimImage>(AnimationPath::builtin("SCNRMPSZ"), 4, 0, 735, 26);
  82. labelCampaignDescription = std::make_shared<CLabel>(481, 63, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, LIBRARY->generaltexth->allTexts[38]);
  83. campaignDescription = std::make_shared<CTextBox>(getCampaign()->getDescriptionTranslated(), Rect(480, 86, 286, 117), 1);
  84. bool videoButtonActive = GAME->server().getState() == EClientState::GAMEPLAY;
  85. int availableSpace = videoButtonActive ? 225 : 285;
  86. mapName = std::make_shared<CLabel>(481, 219, FONT_BIG, ETextAlignment::TOPLEFT, Colors::YELLOW, GAME->server().mi->getNameTranslated(), availableSpace );
  87. labelMapDescription = std::make_shared<CLabel>(481, 253, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, LIBRARY->generaltexth->allTexts[496]);
  88. mapDescription = std::make_shared<CTextBox>("", Rect(480, 278, 286, 108), 1);
  89. labelChooseBonus = std::make_shared<CLabel>(475, 432, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, LIBRARY->generaltexth->allTexts[71]);
  90. groupBonuses = std::make_shared<CToggleGroup>(std::bind(&IServerAPI::setCampaignBonus, &GAME->server(), _1));
  91. flagbox = std::make_shared<CFlagBox>(Rect(486, 407, 335, 23));
  92. std::vector<std::string> difficulty;
  93. std::string difficultyString = LIBRARY->generaltexth->allTexts[492];
  94. boost::split(difficulty, difficultyString, boost::is_any_of(" "));
  95. labelDifficulty = std::make_shared<CLabel>(724, settings["general"]["enableUiEnhancements"].Bool() ? 457 : 432, FONT_MEDIUM, ETextAlignment::TOPCENTER, Colors::WHITE, difficulty.back());
  96. for(size_t b = 0; b < difficultyIcons.size(); ++b)
  97. {
  98. difficultyIcons[b] = std::make_shared<CAnimImage>(AnimationPath::builtinTODO("GSPBUT" + std::to_string(b + 3) + ".DEF"), 0, 0, 709, settings["general"]["enableUiEnhancements"].Bool() ? 480 : 455);
  99. difficultyIconAreas[b] = std::make_shared<LRClickableArea>(difficultyIcons[b]->pos - pos.topLeft(), nullptr, [b]() { CRClickPopup::createAndPush(LIBRARY->generaltexth->zelp[24 + b].second); });
  100. }
  101. if(getCampaign()->playerSelectedDifficulty())
  102. {
  103. Point posLeft = settings["general"]["enableUiEnhancements"].Bool() ? Point(693, 495) : Point(694, 508);
  104. Point posRight = settings["general"]["enableUiEnhancements"].Bool() ? Point(739, 495) : Point(738, 508);
  105. buttonDifficultyLeft = std::make_shared<CButton>(posLeft, AnimationPath::builtin("SCNRBLF.DEF"), CButton::tooltip(), std::bind(&CBonusSelection::decreaseDifficulty, this), EShortcut::MOVE_LEFT);
  106. buttonDifficultyRight = std::make_shared<CButton>(posRight, AnimationPath::builtin("SCNRBRT.DEF"), CButton::tooltip(), std::bind(&CBonusSelection::increaseDifficulty, this), EShortcut::MOVE_RIGHT);
  107. }
  108. for(auto scenarioID : getCampaign()->allScenarios())
  109. {
  110. if(getCampaign()->isAvailable(scenarioID))
  111. regions.push_back(std::make_shared<CRegion>(scenarioID, true, true, false, getCampaign()->getRegions()));
  112. else if(getCampaign()->isConquered(scenarioID)) //display as striped
  113. regions.push_back(std::make_shared<CRegion>(scenarioID, false, false, false, getCampaign()->getRegions()));
  114. else
  115. regions.push_back(std::make_shared<CRegion>(scenarioID, false, false, true, getCampaign()->getRegions()));
  116. }
  117. if (!getCampaign()->getMusic().empty())
  118. ENGINE->music().playMusic( getCampaign()->getMusic(), true, false);
  119. if(GAME->server().getState() != EClientState::GAMEPLAY && settings["general"]["enableUiEnhancements"].Bool())
  120. {
  121. tabExtraOptions = std::make_shared<ExtraOptionsTab>();
  122. tabExtraOptions->recActions = UPDATE | SHOWALL | LCLICK | RCLICK_POPUP;
  123. tabExtraOptions->recreate(true);
  124. tabExtraOptions->setEnabled(false);
  125. buttonExtraOptions = std::make_shared<CButton>(Point(643, 431), AnimationPath::builtin("GSPBUT2.DEF"), LIBRARY->generaltexth->zelp[46], [this]{ tabExtraOptions->setEnabled(!tabExtraOptions->isActive()); ENGINE->windows().totalRedraw(); }, EShortcut::LOBBY_EXTRA_OPTIONS);
  126. buttonExtraOptions->setTextOverlay(LIBRARY->generaltexth->translate("vcmi.optionsTab.extraOptions.hover"), FONT_SMALL, Colors::WHITE);
  127. }
  128. }
  129. void CBonusSelection::createBonusesIcons()
  130. {
  131. OBJECT_CONSTRUCTION;
  132. const CampaignScenario & scenario = getCampaign()->scenario(GAME->server().campaignMap);
  133. const std::vector<CampaignBonus> & bonDescs = scenario.travelOptions.bonusesToChoose;
  134. groupBonuses = std::make_shared<CToggleGroup>(std::bind(&IServerAPI::setCampaignBonus, &GAME->server(), _1));
  135. constexpr std::array bonusPics =
  136. {
  137. "SPELLBON.DEF", // Spell
  138. "TWCRPORT.DEF", // Monster
  139. "", // Building - BO*.BMP
  140. "ARTIFBON.DEF", // Artifact
  141. "SPELLBON.DEF", // Spell scroll
  142. "PSKILBON.DEF", // Primary skill
  143. "SSKILBON.DEF", // Secondary skill
  144. "BORES.DEF", // Resource
  145. "PORTRAITSLARGE", // Hero HPL*.BMP
  146. "PORTRAITSLARGE"
  147. // Player - CREST58.DEF
  148. };
  149. for(int i = 0; i < bonDescs.size(); i++)
  150. {
  151. const CampaignBonus & bonus = bonDescs[i];
  152. CampaignBonusType bonusType = bonus.getType();
  153. std::string picName = bonusPics[static_cast<int>(bonusType)];
  154. size_t picNumber = -1;
  155. MetaString desc;
  156. switch(bonusType)
  157. {
  158. case CampaignBonusType::SPELL:
  159. {
  160. const auto & bonusValue = bonus.getValue<CampaignBonusSpell>();
  161. const auto * spell = bonusValue.spell.toSpell();
  162. if (!spell->getIconScenarioBonus().empty())
  163. picName = spell->getIconScenarioBonus();
  164. else
  165. picNumber = bonusValue.spell.getNum();
  166. desc.appendLocalString(EMetaText::GENERAL_TXT, 715);
  167. desc.replaceName(bonusValue.spell);
  168. break;
  169. }
  170. case CampaignBonusType::MONSTER:
  171. {
  172. const auto & bonusValue = bonus.getValue<CampaignBonusCreatures>();
  173. picNumber = bonusValue.creature.getNum() + 2;
  174. desc.appendLocalString(EMetaText::GENERAL_TXT, 717);
  175. desc.replaceNumber(bonusValue.amount);
  176. desc.replaceNamePlural(bonusValue.creature);
  177. break;
  178. }
  179. case CampaignBonusType::BUILDING:
  180. {
  181. const auto & bonusValue = bonus.getValue<CampaignBonusBuilding>();
  182. FactionID faction;
  183. for(auto & elem : GAME->server().si->playerInfos)
  184. {
  185. if(elem.second.isControlledByHuman())
  186. {
  187. faction = elem.second.castle;
  188. break;
  189. }
  190. }
  191. assert(faction.hasValue());
  192. BuildingID buildID;
  193. if(getCampaign()->formatVCMI())
  194. buildID = bonusValue.building;
  195. else
  196. buildID = CBuildingHandler::campToERMU(bonusValue.building, faction, std::set<BuildingID>());
  197. picName = graphics->ERMUtoPicture[faction.getNum()][buildID.getNum()];
  198. picNumber = -1;
  199. if(vstd::contains((*LIBRARY->townh)[faction]->town->buildings, buildID))
  200. desc.appendTextID((*LIBRARY->townh)[faction]->town->buildings.find(buildID)->second->getNameTextID());
  201. break;
  202. }
  203. case CampaignBonusType::ARTIFACT:
  204. {
  205. const auto & bonusValue = bonus.getValue<CampaignBonusArtifact>();
  206. const auto * artifact = bonusValue.artifact.toArtifact();
  207. if (!artifact->scenarioBonus.empty())
  208. picName = artifact->scenarioBonus;
  209. else
  210. picNumber = bonusValue.artifact.getNum();
  211. desc.appendLocalString(EMetaText::GENERAL_TXT, 715);
  212. desc.replaceName(bonusValue.artifact);
  213. break;
  214. }
  215. case CampaignBonusType::SPELL_SCROLL:
  216. {
  217. const auto & bonusValue = bonus.getValue<CampaignBonusSpellScroll>();
  218. const auto * spell = bonusValue.spell.toSpell();
  219. if (!spell->getIconScenarioBonus().empty())
  220. picName = spell->getIconScenarioBonus();
  221. else
  222. picNumber = bonusValue.spell.getNum();
  223. desc.appendLocalString(EMetaText::GENERAL_TXT, 716);
  224. desc.replaceName(bonusValue.spell);
  225. break;
  226. }
  227. case CampaignBonusType::PRIMARY_SKILL:
  228. {
  229. const auto & bonusValue = bonus.getValue<CampaignBonusPrimarySkill>();
  230. int leadingSkill = -1;
  231. std::vector<std::pair<int, int>> toPrint; //primary skills to be listed <num, val>
  232. for(int g = 0; g < bonusValue.amounts.size(); ++g)
  233. {
  234. if(leadingSkill == -1 || bonusValue.amounts[g] > bonusValue.amounts[leadingSkill])
  235. {
  236. leadingSkill = g;
  237. }
  238. if(bonusValue.amounts[g] != 0)
  239. {
  240. toPrint.push_back(std::make_pair(g, bonusValue.amounts[g]));
  241. }
  242. }
  243. picNumber = leadingSkill;
  244. desc.appendLocalString(EMetaText::GENERAL_TXT, 715);
  245. std::string substitute; //text to be printed instead of %s
  246. for(int v = 0; v < toPrint.size(); ++v)
  247. {
  248. substitute += std::to_string(toPrint[v].second);
  249. substitute += " " + LIBRARY->generaltexth->primarySkillNames[toPrint[v].first];
  250. if(v != toPrint.size() - 1)
  251. {
  252. substitute += ", ";
  253. }
  254. }
  255. desc.replaceRawString(substitute);
  256. break;
  257. }
  258. case CampaignBonusType::SECONDARY_SKILL:
  259. {
  260. const auto & bonusValue = bonus.getValue<CampaignBonusSecondarySkill>();
  261. const auto * skill = bonusValue.skill.toSkill();
  262. desc.appendLocalString(EMetaText::GENERAL_TXT, 718);
  263. desc.replaceTextID(TextIdentifier("core", "skilllev", bonusValue.mastery - 1).get());
  264. desc.replaceName(bonusValue.skill);
  265. if (!skill->at(bonusValue.mastery).scenarioBonus.empty())
  266. picName = skill->at(bonusValue.mastery).scenarioBonus.empty();
  267. else
  268. picNumber = bonusValue.skill.getNum() * 3 + bonusValue.mastery - 1;
  269. break;
  270. }
  271. case CampaignBonusType::RESOURCE:
  272. {
  273. const auto & bonusValue = bonus.getValue<CampaignBonusStartingResources>();
  274. desc.appendLocalString(EMetaText::GENERAL_TXT, 717);
  275. switch(bonusValue.resource)
  276. {
  277. case EGameResID::COMMON: //wood + ore
  278. {
  279. desc.replaceLocalString(EMetaText::GENERAL_TXT, 721);
  280. picNumber = 7;
  281. break;
  282. }
  283. case EGameResID::RARE: //mercury + sulfur + crystal + gems
  284. {
  285. desc.replaceLocalString(EMetaText::GENERAL_TXT, 722);
  286. picNumber = 8;
  287. break;
  288. }
  289. default:
  290. {
  291. desc.replaceName(bonusValue.resource);
  292. picNumber = bonusValue.resource.getNum();
  293. }
  294. }
  295. desc.replaceNumber(bonusValue.amount);
  296. break;
  297. }
  298. case CampaignBonusType::HEROES_FROM_PREVIOUS_SCENARIO:
  299. {
  300. const auto & bonusValue = bonus.getValue<CampaignBonusHeroesFromScenario>();
  301. auto superhero = getCampaign()->strongestHero(bonusValue.scenario, bonusValue.startingPlayer);
  302. if(!superhero)
  303. logGlobal->warn("No superhero! How could it be transferred?");
  304. picNumber = superhero ? superhero->getIconIndex() : 0;
  305. desc.appendLocalString(EMetaText::GENERAL_TXT, 719);
  306. desc.replaceRawString(getCampaign()->scenario(bonusValue.scenario).scenarioName.toString());
  307. break;
  308. }
  309. case CampaignBonusType::HERO:
  310. {
  311. const auto & bonusValue = bonus.getValue<CampaignBonusStartingHero>();
  312. if(bonusValue.hero == HeroTypeID::CAMP_RANDOM.getNum())
  313. {
  314. desc.appendLocalString(EMetaText::GENERAL_TXT, 720); // Start with random hero
  315. picNumber = -1;
  316. picName = "CBONN1A3.BMP";
  317. }
  318. else
  319. {
  320. desc.appendLocalString(EMetaText::GENERAL_TXT, 715); // Start with %s
  321. desc.replaceTextID(bonusValue.hero.toHeroType()->getNameTextID());
  322. picNumber = bonusValue.hero.getNum();
  323. }
  324. break;
  325. }
  326. }
  327. std::shared_ptr<CToggleButton> bonusButton = std::make_shared<CToggleButton>(Point(475 + i * 68, 455), AnimationPath::builtin("campaignBonusSelection"), CButton::tooltip(desc.toString(), desc.toString()), nullptr, EShortcut::NONE, false, [this](){
  328. if(buttonStart->isActive() && !buttonStart->isBlocked())
  329. CBonusSelection::startMap();
  330. });
  331. if(picNumber != -1)
  332. bonusButton->setOverlay(std::make_shared<CAnimImage>(AnimationPath::builtin(picName), picNumber));
  333. else
  334. bonusButton->setOverlay(std::make_shared<CPicture>(ImagePath::builtin(picName)));
  335. if(GAME->server().campaignBonus == i)
  336. bonusButton->setBorderColor(Colors::BRIGHT_YELLOW);
  337. groupBonuses->addToggle(i, bonusButton);
  338. }
  339. if(getCampaign()->getBonusID(GAME->server().campaignMap))
  340. groupBonuses->setSelected(*getCampaign()->getBonusID(GAME->server().campaignMap));
  341. }
  342. void CBonusSelection::updateAfterStateChange()
  343. {
  344. if(GAME->server().getState() != EClientState::GAMEPLAY)
  345. {
  346. buttonRestart->disable();
  347. buttonVideo->disable();
  348. buttonStart->enable();
  349. buttonBack->block(!getCampaign()->conqueredScenarios().empty());
  350. }
  351. else
  352. {
  353. buttonStart->disable();
  354. buttonRestart->enable();
  355. buttonVideo->enable();
  356. buttonBack->block(false);
  357. if(buttonDifficultyLeft)
  358. buttonDifficultyLeft->disable();
  359. if(buttonDifficultyRight)
  360. buttonDifficultyRight->disable();
  361. }
  362. if(GAME->server().campaignBonus == -1)
  363. {
  364. buttonStart->block(getCampaign()->scenario(GAME->server().campaignMap).travelOptions.bonusesToChoose.size());
  365. }
  366. else
  367. {
  368. buttonStart->block(false);
  369. }
  370. for(auto region : regions)
  371. region->updateState();
  372. if(!GAME->server().mi)
  373. return;
  374. iconsMapSizes->setFrame(GAME->server().mi->getMapSizeIconId());
  375. mapName->setText(GAME->server().mi->getNameTranslated());
  376. mapDescription->setText(GAME->server().mi->getDescriptionTranslated());
  377. for(size_t i = 0; i < difficultyIcons.size(); i++)
  378. {
  379. if(i == GAME->server().si->difficulty)
  380. {
  381. difficultyIcons[i]->enable();
  382. difficultyIconAreas[i]->enable();
  383. }
  384. else
  385. {
  386. difficultyIcons[i]->disable();
  387. difficultyIconAreas[i]->disable();
  388. }
  389. }
  390. flagbox->recreate();
  391. createBonusesIcons();
  392. }
  393. void CBonusSelection::goBack()
  394. {
  395. if(GAME->server().getState() != EClientState::GAMEPLAY)
  396. {
  397. ENGINE->windows().popWindows(2);
  398. GAME->mainmenu()->playMusic();
  399. }
  400. else
  401. {
  402. close();
  403. if(adventureInt)
  404. adventureInt->onAudioResumed();
  405. }
  406. // TODO: we can actually only pop bonus selection interface for custom campaigns
  407. // Though this would require clearing CLobbyScreen::bonusSel pointer when poping this interface
  408. /*
  409. else
  410. {
  411. close();
  412. GAME->server().state = EClientState::LOBBY;
  413. }
  414. */
  415. }
  416. void CBonusSelection::startMap()
  417. {
  418. if (!GAME->server().validateGameStart())
  419. return;
  420. auto showPrologVideo = [this]()
  421. {
  422. auto exitCb = []()
  423. {
  424. logGlobal->info("Starting scenario %d", GAME->server().campaignMap.getNum());
  425. GAME->server().sendStartGame();
  426. };
  427. const CampaignScenario & scenario = getCampaign()->scenario(GAME->server().campaignMap);
  428. if(scenario.prolog.hasPrologEpilog)
  429. {
  430. ENGINE->windows().createAndPushWindow<CPrologEpilogVideo>(scenario.prolog, exitCb);
  431. }
  432. else
  433. {
  434. exitCb();
  435. }
  436. };
  437. //block buttons immediately
  438. buttonStart->block(true);
  439. buttonRestart->block(true);
  440. buttonVideo->block(true);
  441. buttonBack->block(true);
  442. if(GAME->interface()) // we're currently ingame, so ask for starting new map and end game
  443. {
  444. close();
  445. GAME->interface()->showYesNoDialog(LIBRARY->generaltexth->allTexts[67], [=]()
  446. {
  447. showPrologVideo();
  448. }, 0);
  449. }
  450. else
  451. {
  452. showPrologVideo();
  453. }
  454. }
  455. void CBonusSelection::restartMap()
  456. {
  457. close();
  458. GAME->interface()->showYesNoDialog(
  459. LIBRARY->generaltexth->allTexts[67],
  460. [=]()
  461. {
  462. ENGINE->dispatchMainThread(
  463. []()
  464. {
  465. GAME->server().sendRestartGame();
  466. }
  467. );
  468. },
  469. 0
  470. );
  471. }
  472. void CBonusSelection::increaseDifficulty()
  473. {
  474. GAME->server().setDifficulty(GAME->server().si->difficulty + 1);
  475. }
  476. void CBonusSelection::decreaseDifficulty()
  477. {
  478. // avoid negative overflow (0 - 1 = 255)
  479. if (GAME->server().si->difficulty > 0)
  480. GAME->server().setDifficulty(GAME->server().si->difficulty - 1);
  481. }
  482. CBonusSelection::CRegion::CRegion(CampaignScenarioID id, bool accessible, bool selectable, bool labelOnly, const CampaignRegions & campDsc)
  483. : CIntObject(LCLICK | SHOW_POPUP | TIME), idOfMapAndRegion(id), accessible(accessible), selectable(selectable), labelOnly(labelOnly), blinkAnim({})
  484. {
  485. OBJECT_CONSTRUCTION;
  486. pos += campDsc.getPosition(id);
  487. auto color = GAME->server().si->campState->scenario(idOfMapAndRegion).regionColor;
  488. graphicsNotSelected = std::make_shared<CPicture>(campDsc.getAvailableName(id, color));
  489. graphicsNotSelected->disable();
  490. graphicsSelected = std::make_shared<CPicture>(campDsc.getSelectedName(id, color));
  491. graphicsSelected->disable();
  492. graphicsStriped = std::make_shared<CPicture>(campDsc.getConqueredName(id, color));
  493. graphicsStriped->disable();
  494. pos.w = graphicsNotSelected->pos.w;
  495. pos.h = graphicsNotSelected->pos.h;
  496. auto labelPos = campDsc.getLabelPosition(id);
  497. if(labelPos)
  498. {
  499. auto mapHeader = GAME->server().si->campState->getMapHeader(idOfMapAndRegion);
  500. label = std::make_shared<CLabel>((*labelPos).x, (*labelPos).y, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, mapHeader->name.toString());
  501. }
  502. }
  503. void CBonusSelection::CRegion::updateState(bool disableAll)
  504. {
  505. if(labelOnly)
  506. return;
  507. if(disableAll)
  508. {
  509. graphicsNotSelected->disable();
  510. graphicsSelected->disable();
  511. graphicsStriped->disable();
  512. }
  513. else if(!accessible)
  514. {
  515. graphicsNotSelected->disable();
  516. graphicsSelected->disable();
  517. graphicsStriped->enable();
  518. }
  519. else if(GAME->server().campaignMap == idOfMapAndRegion)
  520. {
  521. graphicsNotSelected->disable();
  522. graphicsSelected->enable();
  523. graphicsStriped->disable();
  524. }
  525. else
  526. {
  527. graphicsNotSelected->enable();
  528. graphicsSelected->disable();
  529. graphicsStriped->disable();
  530. }
  531. }
  532. void CBonusSelection::CRegion::tick(uint32_t msPassed)
  533. {
  534. if(!accessible)
  535. {
  536. removeUsedEvents(TIME);
  537. return;
  538. }
  539. blinkAnim.msPassed += msPassed;
  540. if(blinkAnim.msPassed >= 150)
  541. {
  542. blinkAnim.state = !blinkAnim.state;
  543. blinkAnim.msPassed -= 150;
  544. if(blinkAnim.state)
  545. blinkAnim.count++;
  546. else if(blinkAnim.count >= 3)
  547. removeUsedEvents(TIME);
  548. }
  549. updateState(blinkAnim.state);
  550. setRedrawParent(true);
  551. redraw();
  552. }
  553. void CBonusSelection::CRegion::clickReleased(const Point & cursorPosition)
  554. {
  555. if(!labelOnly && selectable && !graphicsNotSelected->getSurface()->isTransparent(cursorPosition - pos.topLeft()))
  556. {
  557. GAME->server().setCampaignMap(idOfMapAndRegion);
  558. }
  559. }
  560. void CBonusSelection::CRegion::showPopupWindow(const Point & cursorPosition)
  561. {
  562. // FIXME: For some reason "down" is only ever contain indeterminate_value
  563. auto & text = GAME->server().si->campState->scenario(idOfMapAndRegion).regionText;
  564. if(!labelOnly && !graphicsNotSelected->getSurface()->isTransparent(cursorPosition - pos.topLeft()) && !text.empty())
  565. {
  566. CRClickPopup::createAndPush(text.toString());
  567. }
  568. }