CBonusSelection.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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/faction/CFaction.h"
  47. #include "../../lib/entities/faction/CTown.h"
  48. #include "../../lib/entities/faction/CTownHandler.h"
  49. #include "../../lib/entities/hero/CHeroHandler.h"
  50. #include "../../lib/spells/CSpellHandler.h"
  51. #include "../../lib/filesystem/Filesystem.h"
  52. #include "../../lib/mapObjects/CGHeroInstance.h"
  53. #include "../../lib/mapping/CMapHeader.h"
  54. #include "../../lib/mapping/CMapInfo.h"
  55. #include "../../lib/mapping/CMapService.h"
  56. #include "../../lib/texts/CGeneralTextHandler.h"
  57. #include "mapping/MapFormatSettings.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 = bonusValue.buildingDecoded;
  193. if (bonusValue.buildingH3M.hasValue())
  194. {
  195. auto mapping = LIBRARY->mapFormat->getMapping(getCampaign()->getFormat());
  196. buildID = mapping.remapBuilding(faction, bonusValue.buildingH3M);
  197. }
  198. for (const auto & townStructure : faction.toFaction()->town->clientInfo.structures)
  199. if (townStructure->building && townStructure->building->bid == buildID)
  200. picName = townStructure->campaignBonus.getOriginalName();
  201. picNumber = -1;
  202. if(vstd::contains(faction.toFaction()->town->buildings, buildID))
  203. desc.appendTextID(faction.toFaction()->town->buildings.find(buildID)->second->getNameTextID());
  204. break;
  205. }
  206. case CampaignBonusType::ARTIFACT:
  207. {
  208. const auto & bonusValue = bonus.getValue<CampaignBonusArtifact>();
  209. const auto * artifact = bonusValue.artifact.toArtifact();
  210. if (!artifact->scenarioBonus.empty())
  211. picName = artifact->scenarioBonus;
  212. else
  213. picNumber = bonusValue.artifact.getNum();
  214. desc.appendLocalString(EMetaText::GENERAL_TXT, 715);
  215. desc.replaceName(bonusValue.artifact);
  216. break;
  217. }
  218. case CampaignBonusType::SPELL_SCROLL:
  219. {
  220. const auto & bonusValue = bonus.getValue<CampaignBonusSpellScroll>();
  221. const auto * spell = bonusValue.spell.toSpell();
  222. if (!spell->getIconScenarioBonus().empty())
  223. picName = spell->getIconScenarioBonus();
  224. else
  225. picNumber = bonusValue.spell.getNum();
  226. desc.appendLocalString(EMetaText::GENERAL_TXT, 716);
  227. desc.replaceName(bonusValue.spell);
  228. break;
  229. }
  230. case CampaignBonusType::PRIMARY_SKILL:
  231. {
  232. const auto & bonusValue = bonus.getValue<CampaignBonusPrimarySkill>();
  233. int leadingSkill = -1;
  234. std::vector<std::pair<int, int>> toPrint; //primary skills to be listed <num, val>
  235. for(int g = 0; g < bonusValue.amounts.size(); ++g)
  236. {
  237. if(leadingSkill == -1 || bonusValue.amounts[g] > bonusValue.amounts[leadingSkill])
  238. {
  239. leadingSkill = g;
  240. }
  241. if(bonusValue.amounts[g] != 0)
  242. {
  243. toPrint.push_back(std::make_pair(g, bonusValue.amounts[g]));
  244. }
  245. }
  246. picNumber = leadingSkill;
  247. desc.appendLocalString(EMetaText::GENERAL_TXT, 715);
  248. std::string substitute; //text to be printed instead of %s
  249. for(int v = 0; v < toPrint.size(); ++v)
  250. {
  251. substitute += std::to_string(toPrint[v].second);
  252. substitute += " " + LIBRARY->generaltexth->primarySkillNames[toPrint[v].first];
  253. if(v != toPrint.size() - 1)
  254. {
  255. substitute += ", ";
  256. }
  257. }
  258. desc.replaceRawString(substitute);
  259. break;
  260. }
  261. case CampaignBonusType::SECONDARY_SKILL:
  262. {
  263. const auto & bonusValue = bonus.getValue<CampaignBonusSecondarySkill>();
  264. const auto * skill = bonusValue.skill.toSkill();
  265. desc.appendLocalString(EMetaText::GENERAL_TXT, 718);
  266. desc.replaceTextID(TextIdentifier("core", "skilllev", bonusValue.mastery - 1).get());
  267. desc.replaceName(bonusValue.skill);
  268. if (!skill->at(bonusValue.mastery).scenarioBonus.empty())
  269. picName = skill->at(bonusValue.mastery).scenarioBonus.empty();
  270. else
  271. picNumber = bonusValue.skill.getNum() * 3 + bonusValue.mastery - 1;
  272. break;
  273. }
  274. case CampaignBonusType::RESOURCE:
  275. {
  276. const auto & bonusValue = bonus.getValue<CampaignBonusStartingResources>();
  277. desc.appendLocalString(EMetaText::GENERAL_TXT, 717);
  278. switch(bonusValue.resource)
  279. {
  280. case EGameResID::COMMON: //wood + ore
  281. {
  282. desc.replaceLocalString(EMetaText::GENERAL_TXT, 721);
  283. picNumber = 7;
  284. break;
  285. }
  286. case EGameResID::RARE: //mercury + sulfur + crystal + gems
  287. {
  288. desc.replaceLocalString(EMetaText::GENERAL_TXT, 722);
  289. picNumber = 8;
  290. break;
  291. }
  292. default:
  293. {
  294. desc.replaceName(bonusValue.resource);
  295. picNumber = bonusValue.resource.getNum();
  296. }
  297. }
  298. desc.replaceNumber(bonusValue.amount);
  299. break;
  300. }
  301. case CampaignBonusType::HEROES_FROM_PREVIOUS_SCENARIO:
  302. {
  303. const auto & bonusValue = bonus.getValue<CampaignBonusHeroesFromScenario>();
  304. auto superhero = getCampaign()->strongestHero(bonusValue.scenario, bonusValue.startingPlayer);
  305. if(!superhero)
  306. logGlobal->warn("No superhero! How could it be transferred?");
  307. picNumber = superhero ? superhero->getIconIndex() : 0;
  308. desc.appendLocalString(EMetaText::GENERAL_TXT, 719);
  309. desc.replaceRawString(getCampaign()->scenario(bonusValue.scenario).scenarioName.toString());
  310. break;
  311. }
  312. case CampaignBonusType::HERO:
  313. {
  314. const auto & bonusValue = bonus.getValue<CampaignBonusStartingHero>();
  315. if(bonusValue.hero == HeroTypeID::CAMP_RANDOM.getNum())
  316. {
  317. desc.appendLocalString(EMetaText::GENERAL_TXT, 720); // Start with random hero
  318. picNumber = -1;
  319. picName = "CBONN1A3.BMP";
  320. }
  321. else
  322. {
  323. desc.appendLocalString(EMetaText::GENERAL_TXT, 715); // Start with %s
  324. desc.replaceTextID(bonusValue.hero.toHeroType()->getNameTextID());
  325. picNumber = bonusValue.hero.getNum();
  326. }
  327. break;
  328. }
  329. }
  330. 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](){
  331. if(buttonStart->isActive() && !buttonStart->isBlocked())
  332. CBonusSelection::startMap();
  333. });
  334. if(picNumber != -1)
  335. bonusButton->setOverlay(std::make_shared<CAnimImage>(AnimationPath::builtin(picName), picNumber));
  336. else
  337. bonusButton->setOverlay(std::make_shared<CPicture>(ImagePath::builtin(picName)));
  338. if(GAME->server().campaignBonus == i)
  339. bonusButton->setBorderColor(Colors::BRIGHT_YELLOW);
  340. groupBonuses->addToggle(i, bonusButton);
  341. }
  342. if(getCampaign()->getBonusID(GAME->server().campaignMap))
  343. groupBonuses->setSelected(*getCampaign()->getBonusID(GAME->server().campaignMap));
  344. }
  345. void CBonusSelection::updateAfterStateChange()
  346. {
  347. if(GAME->server().getState() != EClientState::GAMEPLAY)
  348. {
  349. buttonRestart->disable();
  350. buttonVideo->disable();
  351. buttonStart->enable();
  352. buttonBack->block(!getCampaign()->conqueredScenarios().empty());
  353. }
  354. else
  355. {
  356. buttonStart->disable();
  357. buttonRestart->enable();
  358. buttonVideo->enable();
  359. buttonBack->block(false);
  360. if(buttonDifficultyLeft)
  361. buttonDifficultyLeft->disable();
  362. if(buttonDifficultyRight)
  363. buttonDifficultyRight->disable();
  364. }
  365. if(GAME->server().campaignBonus == -1)
  366. {
  367. buttonStart->block(getCampaign()->scenario(GAME->server().campaignMap).travelOptions.bonusesToChoose.size());
  368. }
  369. else
  370. {
  371. buttonStart->block(false);
  372. }
  373. for(auto region : regions)
  374. region->updateState();
  375. if(!GAME->server().mi)
  376. return;
  377. iconsMapSizes->setFrame(GAME->server().mi->getMapSizeIconId());
  378. mapName->setText(GAME->server().mi->getNameTranslated());
  379. mapDescription->setText(GAME->server().mi->getDescriptionTranslated());
  380. for(size_t i = 0; i < difficultyIcons.size(); i++)
  381. {
  382. if(i == GAME->server().si->difficulty)
  383. {
  384. difficultyIcons[i]->enable();
  385. difficultyIconAreas[i]->enable();
  386. }
  387. else
  388. {
  389. difficultyIcons[i]->disable();
  390. difficultyIconAreas[i]->disable();
  391. }
  392. }
  393. flagbox->recreate();
  394. createBonusesIcons();
  395. }
  396. void CBonusSelection::goBack()
  397. {
  398. if(GAME->server().getState() != EClientState::GAMEPLAY)
  399. {
  400. ENGINE->windows().popWindows(2);
  401. GAME->mainmenu()->playMusic();
  402. }
  403. else
  404. {
  405. close();
  406. if(adventureInt)
  407. adventureInt->onAudioResumed();
  408. }
  409. // TODO: we can actually only pop bonus selection interface for custom campaigns
  410. // Though this would require clearing CLobbyScreen::bonusSel pointer when poping this interface
  411. /*
  412. else
  413. {
  414. close();
  415. GAME->server().state = EClientState::LOBBY;
  416. }
  417. */
  418. }
  419. void CBonusSelection::startMap()
  420. {
  421. if (!GAME->server().validateGameStart())
  422. return;
  423. auto showPrologVideo = [this]()
  424. {
  425. auto exitCb = []()
  426. {
  427. logGlobal->info("Starting scenario %d", GAME->server().campaignMap.getNum());
  428. GAME->server().sendStartGame();
  429. };
  430. const CampaignScenario & scenario = getCampaign()->scenario(GAME->server().campaignMap);
  431. if(scenario.prolog.hasPrologEpilog)
  432. {
  433. ENGINE->windows().createAndPushWindow<CPrologEpilogVideo>(scenario.prolog, exitCb);
  434. }
  435. else
  436. {
  437. exitCb();
  438. }
  439. };
  440. //block buttons immediately
  441. buttonStart->block(true);
  442. buttonRestart->block(true);
  443. buttonVideo->block(true);
  444. buttonBack->block(true);
  445. if(GAME->interface()) // we're currently ingame, so ask for starting new map and end game
  446. {
  447. close();
  448. GAME->interface()->showYesNoDialog(LIBRARY->generaltexth->allTexts[67], [=]()
  449. {
  450. showPrologVideo();
  451. }, 0);
  452. }
  453. else
  454. {
  455. showPrologVideo();
  456. }
  457. }
  458. void CBonusSelection::restartMap()
  459. {
  460. close();
  461. GAME->interface()->showYesNoDialog(
  462. LIBRARY->generaltexth->allTexts[67],
  463. [=]()
  464. {
  465. ENGINE->dispatchMainThread(
  466. []()
  467. {
  468. GAME->server().sendRestartGame();
  469. }
  470. );
  471. },
  472. 0
  473. );
  474. }
  475. void CBonusSelection::increaseDifficulty()
  476. {
  477. GAME->server().setDifficulty(GAME->server().si->difficulty + 1);
  478. }
  479. void CBonusSelection::decreaseDifficulty()
  480. {
  481. // avoid negative overflow (0 - 1 = 255)
  482. if (GAME->server().si->difficulty > 0)
  483. GAME->server().setDifficulty(GAME->server().si->difficulty - 1);
  484. }
  485. CBonusSelection::CRegion::CRegion(CampaignScenarioID id, bool accessible, bool selectable, bool labelOnly, const CampaignRegions & campDsc)
  486. : CIntObject(LCLICK | SHOW_POPUP | TIME), idOfMapAndRegion(id), accessible(accessible), selectable(selectable), labelOnly(labelOnly), blinkAnim({})
  487. {
  488. OBJECT_CONSTRUCTION;
  489. pos += campDsc.getPosition(id);
  490. auto color = GAME->server().si->campState->scenario(idOfMapAndRegion).regionColor;
  491. graphicsNotSelected = std::make_shared<CPicture>(campDsc.getAvailableName(id, color));
  492. graphicsNotSelected->disable();
  493. graphicsSelected = std::make_shared<CPicture>(campDsc.getSelectedName(id, color));
  494. graphicsSelected->disable();
  495. graphicsStriped = std::make_shared<CPicture>(campDsc.getConqueredName(id, color));
  496. graphicsStriped->disable();
  497. pos.w = graphicsNotSelected->pos.w;
  498. pos.h = graphicsNotSelected->pos.h;
  499. auto labelPos = campDsc.getLabelPosition(id);
  500. if(labelPos)
  501. {
  502. auto mapHeader = GAME->server().si->campState->getMapHeader(idOfMapAndRegion);
  503. label = std::make_shared<CLabel>((*labelPos).x, (*labelPos).y, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, mapHeader->name.toString());
  504. }
  505. }
  506. void CBonusSelection::CRegion::updateState(bool disableAll)
  507. {
  508. if(labelOnly)
  509. return;
  510. if(disableAll)
  511. {
  512. graphicsNotSelected->disable();
  513. graphicsSelected->disable();
  514. graphicsStriped->disable();
  515. }
  516. else if(!accessible)
  517. {
  518. graphicsNotSelected->disable();
  519. graphicsSelected->disable();
  520. graphicsStriped->enable();
  521. }
  522. else if(GAME->server().campaignMap == idOfMapAndRegion)
  523. {
  524. graphicsNotSelected->disable();
  525. graphicsSelected->enable();
  526. graphicsStriped->disable();
  527. }
  528. else
  529. {
  530. graphicsNotSelected->enable();
  531. graphicsSelected->disable();
  532. graphicsStriped->disable();
  533. }
  534. }
  535. void CBonusSelection::CRegion::tick(uint32_t msPassed)
  536. {
  537. if(!accessible)
  538. {
  539. removeUsedEvents(TIME);
  540. return;
  541. }
  542. blinkAnim.msPassed += msPassed;
  543. if(blinkAnim.msPassed >= 150)
  544. {
  545. blinkAnim.state = !blinkAnim.state;
  546. blinkAnim.msPassed -= 150;
  547. if(blinkAnim.state)
  548. blinkAnim.count++;
  549. else if(blinkAnim.count >= 3)
  550. removeUsedEvents(TIME);
  551. }
  552. updateState(blinkAnim.state);
  553. setRedrawParent(true);
  554. redraw();
  555. }
  556. void CBonusSelection::CRegion::clickReleased(const Point & cursorPosition)
  557. {
  558. if(!labelOnly && selectable && !graphicsNotSelected->getSurface()->isTransparent(cursorPosition - pos.topLeft()))
  559. {
  560. GAME->server().setCampaignMap(idOfMapAndRegion);
  561. }
  562. }
  563. void CBonusSelection::CRegion::showPopupWindow(const Point & cursorPosition)
  564. {
  565. // FIXME: For some reason "down" is only ever contain indeterminate_value
  566. auto & text = GAME->server().si->campState->scenario(idOfMapAndRegion).regionText;
  567. if(!labelOnly && !graphicsNotSelected->getSurface()->isTransparent(cursorPosition - pos.topLeft()) && !text.empty())
  568. {
  569. CRClickPopup::createAndPush(text.toString());
  570. }
  571. }