2
0

CBonusSelection.cpp 17 KB

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