CBonusSelection.cpp 26 KB

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