CBonusSelection.cpp 15 KB

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