CBonusSelection.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 "../CGameInfo.h"
  17. #include "../CMusicHandler.h"
  18. #include "../CVideoHandler.h"
  19. #include "../CPlayerInterface.h"
  20. #include "../CServerHandler.h"
  21. #include "../mainmenu/CMainMenu.h"
  22. #include "../mainmenu/CPrologEpilogVideo.h"
  23. #include "../widgets/CComponent.h"
  24. #include "../widgets/Buttons.h"
  25. #include "../widgets/MiscWidgets.h"
  26. #include "../widgets/ObjectLists.h"
  27. #include "../widgets/TextControls.h"
  28. #include "../windows/GUIClasses.h"
  29. #include "../windows/InfoWindows.h"
  30. #include "../render/IImage.h"
  31. #include "../render/IRenderHandler.h"
  32. #include "../render/CAnimation.h"
  33. #include "../render/Graphics.h"
  34. #include "../gui/CGuiHandler.h"
  35. #include "../gui/Shortcut.h"
  36. #include "../gui/WindowHandler.h"
  37. #include "../../lib/filesystem/Filesystem.h"
  38. #include "../../lib/CGeneralTextHandler.h"
  39. #include "../../lib/CBuildingHandler.h"
  40. #include "../../lib/CSkillHandler.h"
  41. #include "../../lib/CTownHandler.h"
  42. #include "../../lib/CHeroHandler.h"
  43. #include "../../lib/CCreatureHandler.h"
  44. #include "../../lib/StartInfo.h"
  45. #include "../../lib/campaign/CampaignState.h"
  46. #include "../../lib/mapping/CMapService.h"
  47. #include "../../lib/mapping/CMapInfo.h"
  48. #include "../../lib/mapObjects/CGHeroInstance.h"
  49. std::shared_ptr<CampaignState> CBonusSelection::getCampaign()
  50. {
  51. return CSH->si->campState;
  52. }
  53. CBonusSelection::CBonusSelection()
  54. : CWindowObject(BORDERED)
  55. {
  56. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  57. setBackground(getCampaign()->getRegions().getBackgroundName());
  58. panelBackground = std::make_shared<CPicture>(ImagePath::builtin("CAMPBRF.BMP"), 456, 6);
  59. buttonStart = std::make_shared<CButton>(Point(475, 536), AnimationPath::builtin("CBBEGIB.DEF"), CButton::tooltip(), std::bind(&CBonusSelection::startMap, this), EShortcut::GLOBAL_ACCEPT);
  60. buttonRestart = std::make_shared<CButton>(Point(475, 536), AnimationPath::builtin("CBRESTB.DEF"), CButton::tooltip(), std::bind(&CBonusSelection::restartMap, this), EShortcut::GLOBAL_ACCEPT);
  61. buttonVideo = std::make_shared<CButton>(Point(705, 214), AnimationPath::builtin("CBVIDEB.DEF"), CButton::tooltip(), [this](){ GH.windows().createAndPushWindow<CPrologEpilogVideo>(getCampaign()->scenario(CSH->campaignMap).prolog, [this](){ redraw(); }); });
  62. buttonBack = std::make_shared<CButton>(Point(624, 536), AnimationPath::builtin("CBCANCB.DEF"), CButton::tooltip(), std::bind(&CBonusSelection::goBack, this), EShortcut::GLOBAL_CANCEL);
  63. campaignName = std::make_shared<CLabel>(481, 28, FONT_BIG, ETextAlignment::TOPLEFT, Colors::YELLOW, CSH->si->getCampaignName());
  64. iconsMapSizes = std::make_shared<CAnimImage>(AnimationPath::builtin("SCNRMPSZ"), 4, 0, 735, 26);
  65. labelCampaignDescription = std::make_shared<CLabel>(481, 63, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[38]);
  66. campaignDescription = std::make_shared<CTextBox>(getCampaign()->getDescriptionTranslated(), Rect(480, 86, 286, 117), 1);
  67. mapName = std::make_shared<CLabel>(481, 219, FONT_BIG, ETextAlignment::TOPLEFT, Colors::YELLOW, CSH->mi->getNameTranslated());
  68. labelMapDescription = std::make_shared<CLabel>(481, 253, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[496]);
  69. mapDescription = std::make_shared<CTextBox>("", Rect(480, 278, 292, 108), 1);
  70. labelChooseBonus = std::make_shared<CLabel>(475, 432, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[71]);
  71. groupBonuses = std::make_shared<CToggleGroup>(std::bind(&IServerAPI::setCampaignBonus, CSH, _1));
  72. flagbox = std::make_shared<CFlagBox>(Rect(486, 407, 335, 23));
  73. std::vector<std::string> difficulty;
  74. std::string difficultyString = CGI->generaltexth->allTexts[492];
  75. boost::split(difficulty, difficultyString, boost::is_any_of(" "));
  76. labelDifficulty = std::make_shared<CLabel>(724, settings["general"]["enableUiEnhancements"].Bool() ? 457 : 432, FONT_MEDIUM, ETextAlignment::TOPCENTER, Colors::WHITE, difficulty.back());
  77. for(size_t b = 0; b < difficultyIcons.size(); ++b)
  78. {
  79. difficultyIcons[b] = std::make_shared<CAnimImage>(AnimationPath::builtinTODO("GSPBUT" + std::to_string(b + 3) + ".DEF"), 0, 0, 709, settings["general"]["enableUiEnhancements"].Bool() ? 480 : 455);
  80. }
  81. if(getCampaign()->playerSelectedDifficulty())
  82. {
  83. buttonDifficultyLeft = std::make_shared<CButton>(settings["general"]["enableUiEnhancements"].Bool() ? Point(693, 495) : Point(694, 508), AnimationPath::builtin("SCNRBLF.DEF"), CButton::tooltip(), std::bind(&CBonusSelection::decreaseDifficulty, this));
  84. buttonDifficultyRight = std::make_shared<CButton>(settings["general"]["enableUiEnhancements"].Bool() ? Point(739, 495) : Point(738, 508), AnimationPath::builtin("SCNRBRT.DEF"), CButton::tooltip(), std::bind(&CBonusSelection::increaseDifficulty, this));
  85. }
  86. for(auto scenarioID : getCampaign()->allScenarios())
  87. {
  88. if(getCampaign()->isAvailable(scenarioID))
  89. regions.push_back(std::make_shared<CRegion>(scenarioID, true, true, getCampaign()->getRegions()));
  90. else if(getCampaign()->isConquered(scenarioID)) //display as striped
  91. regions.push_back(std::make_shared<CRegion>(scenarioID, false, false, getCampaign()->getRegions()));
  92. }
  93. if (!getCampaign()->getMusic().empty())
  94. CCS->musich->playMusic( getCampaign()->getMusic(), true, false);
  95. if(CSH->getState() != EClientState::GAMEPLAY && settings["general"]["enableUiEnhancements"].Bool())
  96. {
  97. tabExtraOptions = std::make_shared<ExtraOptionsTab>();
  98. tabExtraOptions->recActions = UPDATE | SHOWALL | LCLICK | RCLICK_POPUP;
  99. tabExtraOptions->recreate(true);
  100. tabExtraOptions->setEnabled(false);
  101. buttonExtraOptions = std::make_shared<CButton>(Point(643, 431), AnimationPath::builtin("GSPBUT2.DEF"), CGI->generaltexth->zelp[46], [this]{ tabExtraOptions->setEnabled(!tabExtraOptions->isActive()); GH.windows().totalRedraw(); }, EShortcut::NONE);
  102. buttonExtraOptions->setTextOverlay(CGI->generaltexth->translate("vcmi.optionsTab.extraOptions.hover"), FONT_SMALL, Colors::WHITE);
  103. }
  104. }
  105. void CBonusSelection::createBonusesIcons()
  106. {
  107. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  108. const CampaignScenario & scenario = getCampaign()->scenario(CSH->campaignMap);
  109. const std::vector<CampaignBonus> & bonDescs = scenario.travelOptions.bonusesToChoose;
  110. groupBonuses = std::make_shared<CToggleGroup>(std::bind(&IServerAPI::setCampaignBonus, CSH, _1));
  111. constexpr std::array bonusPics =
  112. {
  113. "SPELLBON.DEF", // Spell
  114. "TWCRPORT.DEF", // Monster
  115. "", // Building - BO*.BMP
  116. "ARTIFBON.DEF", // Artifact
  117. "SPELLBON.DEF", // Spell scroll
  118. "PSKILBON.DEF", // Primary skill
  119. "SSKILBON.DEF", // Secondary skill
  120. "BORES.DEF", // Resource
  121. "PORTRAITSLARGE", // Hero HPL*.BMP
  122. "PORTRAITSLARGE"
  123. // Player - CREST58.DEF
  124. };
  125. for(int i = 0; i < bonDescs.size(); i++)
  126. {
  127. int bonusType = static_cast<size_t>(bonDescs[i].type);
  128. std::string picName = bonusPics[bonusType];
  129. size_t picNumber = bonDescs[i].info2;
  130. MetaString desc;
  131. switch(bonDescs[i].type)
  132. {
  133. case CampaignBonusType::SPELL:
  134. desc.appendLocalString(EMetaText::GENERAL_TXT, 715);
  135. desc.replaceName(SpellID(bonDescs[i].info2));
  136. break;
  137. case CampaignBonusType::MONSTER:
  138. picNumber = bonDescs[i].info2 + 2;
  139. desc.appendLocalString(EMetaText::GENERAL_TXT, 717);
  140. desc.replaceNumber(bonDescs[i].info3);
  141. desc.replaceNamePlural(bonDescs[i].info2);
  142. break;
  143. case CampaignBonusType::BUILDING:
  144. {
  145. FactionID faction;
  146. for(auto & elem : CSH->si->playerInfos)
  147. {
  148. if(elem.second.isControlledByHuman())
  149. {
  150. faction = elem.second.castle;
  151. break;
  152. }
  153. }
  154. assert(faction != -1);
  155. BuildingID buildID;
  156. if(getCampaign()->formatVCMI())
  157. buildID = BuildingID(bonDescs[i].info1);
  158. else
  159. buildID = CBuildingHandler::campToERMU(bonDescs[i].info1, faction, std::set<BuildingID>());
  160. picName = graphics->ERMUtoPicture[faction][buildID];
  161. picNumber = -1;
  162. if(vstd::contains((*CGI->townh)[faction]->town->buildings, buildID))
  163. desc.appendTextID((*CGI->townh)[faction]->town->buildings.find(buildID)->second->getNameTextID());
  164. break;
  165. }
  166. case CampaignBonusType::ARTIFACT:
  167. desc.appendLocalString(EMetaText::GENERAL_TXT, 715);
  168. desc.replaceName(ArtifactID(bonDescs[i].info2));
  169. break;
  170. case CampaignBonusType::SPELL_SCROLL:
  171. desc.appendLocalString(EMetaText::GENERAL_TXT, 716);
  172. desc.replaceName(ArtifactID(bonDescs[i].info2));
  173. break;
  174. case CampaignBonusType::PRIMARY_SKILL:
  175. {
  176. int leadingSkill = -1;
  177. std::vector<std::pair<int, int>> toPrint; //primary skills to be listed <num, val>
  178. const ui8 * ptr = reinterpret_cast<const ui8 *>(&bonDescs[i].info2);
  179. for(int g = 0; g < GameConstants::PRIMARY_SKILLS; ++g)
  180. {
  181. if(leadingSkill == -1 || ptr[g] > ptr[leadingSkill])
  182. {
  183. leadingSkill = g;
  184. }
  185. if(ptr[g] != 0)
  186. {
  187. toPrint.push_back(std::make_pair(g, ptr[g]));
  188. }
  189. }
  190. picNumber = leadingSkill;
  191. desc.appendLocalString(EMetaText::GENERAL_TXT, 715);
  192. std::string substitute; //text to be printed instead of %s
  193. for(int v = 0; v < toPrint.size(); ++v)
  194. {
  195. substitute += std::to_string(toPrint[v].second);
  196. substitute += " " + CGI->generaltexth->primarySkillNames[toPrint[v].first];
  197. if(v != toPrint.size() - 1)
  198. {
  199. substitute += ", ";
  200. }
  201. }
  202. desc.replaceRawString(substitute);
  203. break;
  204. }
  205. case CampaignBonusType::SECONDARY_SKILL:
  206. desc.appendLocalString(EMetaText::GENERAL_TXT, 718);
  207. desc.replaceTextID(TextIdentifier("core", "genrltxt", "levels", bonDescs[i].info3 - 1).get());
  208. desc.replaceName(SecondarySkill(bonDescs[i].info2));
  209. picNumber = bonDescs[i].info2 * 3 + bonDescs[i].info3 - 1;
  210. break;
  211. case CampaignBonusType::RESOURCE:
  212. {
  213. desc.appendLocalString(EMetaText::GENERAL_TXT, 717);
  214. switch(bonDescs[i].info1)
  215. {
  216. case 0xFD: //wood + ore
  217. {
  218. desc.replaceLocalString(EMetaText::GENERAL_TXT, 721);
  219. picNumber = 7;
  220. break;
  221. }
  222. case 0xFE: //wood + ore
  223. {
  224. desc.replaceLocalString(EMetaText::GENERAL_TXT, 722);
  225. picNumber = 8;
  226. break;
  227. }
  228. default:
  229. {
  230. desc.replaceName(GameResID(bonDescs[i].info1));
  231. picNumber = bonDescs[i].info1;
  232. }
  233. }
  234. desc.replaceNumber(bonDescs[i].info2);
  235. break;
  236. }
  237. case CampaignBonusType::HEROES_FROM_PREVIOUS_SCENARIO:
  238. {
  239. auto superhero = getCampaign()->strongestHero(static_cast<CampaignScenarioID>(bonDescs[i].info2), PlayerColor(bonDescs[i].info1));
  240. if(!superhero)
  241. logGlobal->warn("No superhero! How could it be transferred?");
  242. picNumber = superhero ? superhero->getIconIndex() : 0;
  243. desc.appendLocalString(EMetaText::GENERAL_TXT, 719);
  244. desc.replaceRawString(getCampaign()->scenario(static_cast<CampaignScenarioID>(bonDescs[i].info2)).scenarioName.toString());
  245. break;
  246. }
  247. case CampaignBonusType::HERO:
  248. if(bonDescs[i].info2 == 0xFFFF)
  249. {
  250. desc.appendLocalString(EMetaText::GENERAL_TXT, 720); // Start with random hero
  251. picNumber = -1;
  252. picName = "CBONN1A3.BMP";
  253. }
  254. else
  255. {
  256. desc.appendLocalString(EMetaText::GENERAL_TXT, 715); // Start with %s
  257. desc.replaceTextID(CGI->heroh->objects[bonDescs[i].info2]->getNameTextID());
  258. }
  259. break;
  260. }
  261. std::shared_ptr<CToggleButton> bonusButton = std::make_shared<CToggleButton>(Point(475 + i * 68, 455), AnimationPath::builtin("campaignBonusSelection"), CButton::tooltip(desc.toString(), desc.toString()));
  262. if(picNumber != -1)
  263. bonusButton->setOverlay(std::make_shared<CAnimImage>(AnimationPath::builtin(picName), picNumber));
  264. else
  265. bonusButton->setOverlay(std::make_shared<CPicture>(ImagePath::builtin(picName)));
  266. if(CSH->campaignBonus == i)
  267. bonusButton->setBorderColor(Colors::BRIGHT_YELLOW);
  268. groupBonuses->addToggle(i, bonusButton);
  269. }
  270. if(getCampaign()->getBonusID(CSH->campaignMap))
  271. groupBonuses->setSelected(*getCampaign()->getBonusID(CSH->campaignMap));
  272. }
  273. void CBonusSelection::updateAfterStateChange()
  274. {
  275. if(CSH->getState() != EClientState::GAMEPLAY)
  276. {
  277. buttonRestart->disable();
  278. buttonVideo->disable();
  279. buttonStart->enable();
  280. buttonBack->block(!getCampaign()->conqueredScenarios().empty());
  281. }
  282. else
  283. {
  284. buttonStart->disable();
  285. buttonRestart->enable();
  286. buttonVideo->enable();
  287. buttonBack->block(false);
  288. if(buttonDifficultyLeft)
  289. buttonDifficultyLeft->disable();
  290. if(buttonDifficultyRight)
  291. buttonDifficultyRight->disable();
  292. }
  293. if(CSH->campaignBonus == -1)
  294. {
  295. buttonStart->block(getCampaign()->scenario(CSH->campaignMap).travelOptions.bonusesToChoose.size());
  296. }
  297. else
  298. {
  299. buttonStart->block(false);
  300. }
  301. for(auto region : regions)
  302. region->updateState();
  303. if(!CSH->mi)
  304. return;
  305. iconsMapSizes->setFrame(CSH->mi->getMapSizeIconId());
  306. mapName->setText(CSH->mi->getNameTranslated());
  307. mapDescription->setText(CSH->mi->getDescriptionTranslated());
  308. for(size_t i = 0; i < difficultyIcons.size(); i++)
  309. {
  310. if(i == CSH->si->difficulty)
  311. difficultyIcons[i]->enable();
  312. else
  313. difficultyIcons[i]->disable();
  314. }
  315. flagbox->recreate();
  316. createBonusesIcons();
  317. }
  318. void CBonusSelection::goBack()
  319. {
  320. if(CSH->getState() != EClientState::GAMEPLAY)
  321. {
  322. GH.windows().popWindows(2);
  323. }
  324. else
  325. {
  326. close();
  327. }
  328. // TODO: we can actually only pop bonus selection interface for custom campaigns
  329. // Though this would require clearing CLobbyScreen::bonusSel pointer when poping this interface
  330. /*
  331. else
  332. {
  333. close();
  334. CSH->state = EClientState::LOBBY;
  335. }
  336. */
  337. }
  338. void CBonusSelection::startMap()
  339. {
  340. if (!CSH->validateGameStart())
  341. return;
  342. auto showPrologVideo = [=]()
  343. {
  344. auto exitCb = [=]()
  345. {
  346. logGlobal->info("Starting scenario %d", static_cast<int>(CSH->campaignMap));
  347. CSH->sendStartGame();
  348. };
  349. const CampaignScenario & scenario = getCampaign()->scenario(CSH->campaignMap);
  350. if(scenario.prolog.hasPrologEpilog)
  351. {
  352. GH.windows().createAndPushWindow<CPrologEpilogVideo>(scenario.prolog, exitCb);
  353. }
  354. else
  355. {
  356. exitCb();
  357. }
  358. };
  359. //block buttons immediately
  360. buttonStart->block(true);
  361. buttonRestart->block(true);
  362. buttonVideo->block(true);
  363. buttonBack->block(true);
  364. if(LOCPLINT) // we're currently ingame, so ask for starting new map and end game
  365. {
  366. close();
  367. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [=]()
  368. {
  369. showPrologVideo();
  370. }, 0);
  371. }
  372. else
  373. {
  374. showPrologVideo();
  375. }
  376. }
  377. void CBonusSelection::restartMap()
  378. {
  379. close();
  380. LOCPLINT->showYesNoDialog(
  381. CGI->generaltexth->allTexts[67],
  382. [=]()
  383. {
  384. GH.dispatchMainThread(
  385. []()
  386. {
  387. CSH->sendRestartGame();
  388. }
  389. );
  390. },
  391. 0
  392. );
  393. }
  394. void CBonusSelection::increaseDifficulty()
  395. {
  396. CSH->setDifficulty(CSH->si->difficulty + 1);
  397. }
  398. void CBonusSelection::decreaseDifficulty()
  399. {
  400. // avoid negative overflow (0 - 1 = 255)
  401. if (CSH->si->difficulty > 0)
  402. CSH->setDifficulty(CSH->si->difficulty - 1);
  403. }
  404. CBonusSelection::CRegion::CRegion(CampaignScenarioID id, bool accessible, bool selectable, const CampaignRegions & campDsc)
  405. : CIntObject(LCLICK | SHOW_POPUP), idOfMapAndRegion(id), accessible(accessible), selectable(selectable)
  406. {
  407. OBJ_CONSTRUCTION;
  408. pos += campDsc.getPosition(id);
  409. auto color = CSH->si->campState->scenario(idOfMapAndRegion).regionColor;
  410. graphicsNotSelected = std::make_shared<CPicture>(campDsc.getAvailableName(id, color));
  411. graphicsNotSelected->disable();
  412. graphicsSelected = std::make_shared<CPicture>(campDsc.getSelectedName(id, color));
  413. graphicsSelected->disable();
  414. graphicsStriped = std::make_shared<CPicture>(campDsc.getConqueredName(id, color));
  415. graphicsStriped->disable();
  416. pos.w = graphicsNotSelected->pos.w;
  417. pos.h = graphicsNotSelected->pos.h;
  418. }
  419. void CBonusSelection::CRegion::updateState()
  420. {
  421. if(!accessible)
  422. {
  423. graphicsNotSelected->disable();
  424. graphicsSelected->disable();
  425. graphicsStriped->enable();
  426. }
  427. else if(CSH->campaignMap == idOfMapAndRegion)
  428. {
  429. graphicsNotSelected->disable();
  430. graphicsSelected->enable();
  431. graphicsStriped->disable();
  432. }
  433. else
  434. {
  435. graphicsNotSelected->enable();
  436. graphicsSelected->disable();
  437. graphicsStriped->disable();
  438. }
  439. }
  440. void CBonusSelection::CRegion::clickReleased(const Point & cursorPosition)
  441. {
  442. if(selectable && !graphicsNotSelected->getSurface()->isTransparent(cursorPosition - pos.topLeft()))
  443. {
  444. CSH->setCampaignMap(idOfMapAndRegion);
  445. }
  446. }
  447. void CBonusSelection::CRegion::showPopupWindow(const Point & cursorPosition)
  448. {
  449. // FIXME: For some reason "down" is only ever contain indeterminate_value
  450. auto & text = CSH->si->campState->scenario(idOfMapAndRegion).regionText;
  451. if(!graphicsNotSelected->getSurface()->isTransparent(cursorPosition - pos.topLeft()) && !text.empty())
  452. {
  453. CRClickPopup::createAndPush(text.toString());
  454. }
  455. }