CBonusSelection.cpp 15 KB

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