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 "../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/CAnimation.h"
  31. #include "../gui/CGuiHandler.h"
  32. #include "../gui/Shortcut.h"
  33. #include "../gui/WindowHandler.h"
  34. #include "../../lib/filesystem/Filesystem.h"
  35. #include "../../lib/CGeneralTextHandler.h"
  36. #include "../../lib/CBuildingHandler.h"
  37. #include "../../lib/CSkillHandler.h"
  38. #include "../../lib/CTownHandler.h"
  39. #include "../../lib/CHeroHandler.h"
  40. #include "../../lib/CCreatureHandler.h"
  41. #include "../../lib/StartInfo.h"
  42. #include "../../lib/mapping/CCampaignHandler.h"
  43. #include "../../lib/mapping/CMapService.h"
  44. #include "../../lib/mapping/CMapInfo.h"
  45. #include "../../lib/mapObjects/CGHeroInstance.h"
  46. std::shared_ptr<CCampaignState> CBonusSelection::getCampaign()
  47. {
  48. return CSH->si->campState;
  49. }
  50. CBonusSelection::CBonusSelection()
  51. : CWindowObject(BORDERED)
  52. {
  53. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  54. std::string bgName = getCampaign()->camp->header.campaignRegions.campPrefix + "_BG.BMP";
  55. setBackground(bgName);
  56. panelBackground = std::make_shared<CPicture>("CAMPBRF.BMP", 456, 6);
  57. buttonStart = std::make_shared<CButton>(Point(475, 536), "CBBEGIB.DEF", CButton::tooltip(), std::bind(&CBonusSelection::startMap, this), EShortcut::GLOBAL_ACCEPT);
  58. buttonRestart = std::make_shared<CButton>(Point(475, 536), "CBRESTB.DEF", CButton::tooltip(), std::bind(&CBonusSelection::restartMap, this), EShortcut::GLOBAL_ACCEPT);
  59. buttonBack = std::make_shared<CButton>(Point(624, 536), "CBCANCB.DEF", CButton::tooltip(), std::bind(&CBonusSelection::goBack, this), EShortcut::GLOBAL_CANCEL);
  60. campaignName = std::make_shared<CLabel>(481, 28, FONT_BIG, ETextAlignment::TOPLEFT, Colors::YELLOW, CSH->si->getCampaignName());
  61. iconsMapSizes = std::make_shared<CAnimImage>("SCNRMPSZ", 4, 0, 735, 26);
  62. labelCampaignDescription = std::make_shared<CLabel>(481, 63, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[38]);
  63. campaignDescription = std::make_shared<CTextBox>(getCampaign()->camp->header.description, Rect(480, 86, 286, 117), 1);
  64. mapName = std::make_shared<CLabel>(481, 219, FONT_BIG, ETextAlignment::TOPLEFT, Colors::YELLOW, CSH->mi->getName());
  65. labelMapDescription = std::make_shared<CLabel>(481, 253, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[496]);
  66. mapDescription = std::make_shared<CTextBox>("", Rect(480, 280, 286, 117), 1);
  67. labelChooseBonus = std::make_shared<CLabel>(511, 432, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[71]);
  68. groupBonuses = std::make_shared<CToggleGroup>(std::bind(&IServerAPI::setCampaignBonus, CSH, _1));
  69. flagbox = std::make_shared<CFlagBox>(Rect(486, 407, 335, 23));
  70. std::vector<std::string> difficulty;
  71. std::string difficultyString = CGI->generaltexth->allTexts[492];
  72. boost::split(difficulty, difficultyString, boost::is_any_of(" "));
  73. labelDifficulty = std::make_shared<CLabel>(689, 432, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, difficulty.back());
  74. for(size_t b = 0; b < difficultyIcons.size(); ++b)
  75. {
  76. difficultyIcons[b] = std::make_shared<CAnimImage>("GSPBUT" + std::to_string(b + 3) + ".DEF", 0, 0, 709, 455);
  77. }
  78. if(getCampaign()->camp->header.difficultyChoosenByPlayer)
  79. {
  80. buttonDifficultyLeft = std::make_shared<CButton>(Point(694, 508), "SCNRBLF.DEF", CButton::tooltip(), std::bind(&CBonusSelection::decreaseDifficulty, this));
  81. buttonDifficultyRight = std::make_shared<CButton>(Point(738, 508), "SCNRBRT.DEF", CButton::tooltip(), std::bind(&CBonusSelection::increaseDifficulty, this));
  82. }
  83. for(int g = 0; g < getCampaign()->camp->scenarios.size(); ++g)
  84. {
  85. if(getCampaign()->camp->conquerable(g))
  86. regions.push_back(std::make_shared<CRegion>(g, true, true, getCampaign()->camp->header.campaignRegions));
  87. else if(getCampaign()->camp->scenarios[g].conquered) //display as striped
  88. regions.push_back(std::make_shared<CRegion>(g, false, false, getCampaign()->camp->header.campaignRegions));
  89. }
  90. }
  91. void CBonusSelection::createBonusesIcons()
  92. {
  93. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  94. const CCampaignScenario & scenario = getCampaign()->camp->scenarios[CSH->campaignMap];
  95. const std::vector<CScenarioTravel::STravelBonus> & bonDescs = scenario.travelOptions.bonusesToChoose;
  96. groupBonuses = std::make_shared<CToggleGroup>(std::bind(&IServerAPI::setCampaignBonus, CSH, _1));
  97. static const char * bonusPics[] =
  98. {
  99. "SPELLBON.DEF", // Spell
  100. "TWCRPORT.DEF", // Monster
  101. "", // Building - BO*.BMP
  102. "ARTIFBON.DEF", // Artifact
  103. "SPELLBON.DEF", // Spell scroll
  104. "PSKILBON.DEF", // Primary skill
  105. "SSKILBON.DEF", // Secondary skill
  106. "BORES.DEF", // Resource
  107. "PORTRAITSLARGE", // Hero HPL*.BMP
  108. "PORTRAITSLARGE"
  109. // Player - CREST58.DEF
  110. };
  111. for(int i = 0; i < bonDescs.size(); i++)
  112. {
  113. std::string picName = bonusPics[bonDescs[i].type];
  114. size_t picNumber = bonDescs[i].info2;
  115. std::string desc;
  116. switch(bonDescs[i].type)
  117. {
  118. case CScenarioTravel::STravelBonus::SPELL:
  119. desc = CGI->generaltexth->allTexts[715];
  120. boost::algorithm::replace_first(desc, "%s", CGI->spells()->getByIndex(bonDescs[i].info2)->getNameTranslated());
  121. break;
  122. case CScenarioTravel::STravelBonus::MONSTER:
  123. picNumber = bonDescs[i].info2 + 2;
  124. desc = CGI->generaltexth->allTexts[717];
  125. boost::algorithm::replace_first(desc, "%d", std::to_string(bonDescs[i].info3));
  126. boost::algorithm::replace_first(desc, "%s", CGI->creatures()->getByIndex(bonDescs[i].info2)->getNamePluralTranslated());
  127. break;
  128. case CScenarioTravel::STravelBonus::BUILDING:
  129. {
  130. int faction = -1;
  131. for(auto & elem : CSH->si->playerInfos)
  132. {
  133. if(elem.second.isControlledByHuman())
  134. {
  135. faction = elem.second.castle;
  136. break;
  137. }
  138. }
  139. assert(faction != -1);
  140. BuildingID buildID;
  141. if(getCampaign()->camp->header.version == CampaignVersion::VCMI)
  142. buildID = BuildingID(bonDescs[i].info1);
  143. else
  144. buildID = CBuildingHandler::campToERMU(bonDescs[i].info1, faction, std::set<BuildingID>());
  145. picName = graphics->ERMUtoPicture[faction][buildID];
  146. picNumber = -1;
  147. if(vstd::contains((*CGI->townh)[faction]->town->buildings, buildID))
  148. desc = (*CGI->townh)[faction]->town->buildings.find(buildID)->second->getNameTranslated();
  149. break;
  150. }
  151. case CScenarioTravel::STravelBonus::ARTIFACT:
  152. desc = CGI->generaltexth->allTexts[715];
  153. boost::algorithm::replace_first(desc, "%s", CGI->artifacts()->getByIndex(bonDescs[i].info2)->getNameTranslated());
  154. break;
  155. case CScenarioTravel::STravelBonus::SPELL_SCROLL:
  156. desc = CGI->generaltexth->allTexts[716];
  157. boost::algorithm::replace_first(desc, "%s", CGI->spells()->getByIndex(bonDescs[i].info2)->getNameTranslated());
  158. break;
  159. case CScenarioTravel::STravelBonus::PRIMARY_SKILL:
  160. {
  161. int leadingSkill = -1;
  162. std::vector<std::pair<int, int>> toPrint; //primary skills to be listed <num, val>
  163. const ui8 * ptr = reinterpret_cast<const ui8 *>(&bonDescs[i].info2);
  164. for(int g = 0; g < GameConstants::PRIMARY_SKILLS; ++g)
  165. {
  166. if(leadingSkill == -1 || ptr[g] > ptr[leadingSkill])
  167. {
  168. leadingSkill = g;
  169. }
  170. if(ptr[g] != 0)
  171. {
  172. toPrint.push_back(std::make_pair(g, ptr[g]));
  173. }
  174. }
  175. picNumber = leadingSkill;
  176. desc = CGI->generaltexth->allTexts[715];
  177. std::string substitute; //text to be printed instead of %s
  178. for(int v = 0; v < toPrint.size(); ++v)
  179. {
  180. substitute += std::to_string(toPrint[v].second);
  181. substitute += " " + CGI->generaltexth->primarySkillNames[toPrint[v].first];
  182. if(v != toPrint.size() - 1)
  183. {
  184. substitute += ", ";
  185. }
  186. }
  187. boost::algorithm::replace_first(desc, "%s", substitute);
  188. break;
  189. }
  190. case CScenarioTravel::STravelBonus::SECONDARY_SKILL:
  191. desc = CGI->generaltexth->allTexts[718];
  192. boost::algorithm::replace_first(desc, "%s", CGI->generaltexth->levels[bonDescs[i].info3 - 1]); //skill level
  193. boost::algorithm::replace_first(desc, "%s", CGI->skillh->getByIndex(bonDescs[i].info2)->getNameTranslated()); //skill name
  194. picNumber = bonDescs[i].info2 * 3 + bonDescs[i].info3 - 1;
  195. break;
  196. case CScenarioTravel::STravelBonus::RESOURCE:
  197. {
  198. int serialResID = 0;
  199. switch(bonDescs[i].info1)
  200. {
  201. case 0:
  202. case 1:
  203. case 2:
  204. case 3:
  205. case 4:
  206. case 5:
  207. case 6:
  208. serialResID = bonDescs[i].info1;
  209. break;
  210. case 0xFD: //wood + ore
  211. serialResID = 7;
  212. break;
  213. case 0xFE: //rare resources
  214. serialResID = 8;
  215. break;
  216. }
  217. picNumber = serialResID;
  218. desc = CGI->generaltexth->allTexts[717];
  219. boost::algorithm::replace_first(desc, "%d", std::to_string(bonDescs[i].info2));
  220. std::string replacement;
  221. if(serialResID <= 6)
  222. {
  223. replacement = CGI->generaltexth->restypes[serialResID];
  224. }
  225. else
  226. {
  227. replacement = CGI->generaltexth->allTexts[714 + serialResID];
  228. }
  229. boost::algorithm::replace_first(desc, "%s", replacement);
  230. break;
  231. }
  232. case CScenarioTravel::STravelBonus::HEROES_FROM_PREVIOUS_SCENARIO:
  233. {
  234. auto superhero = getCampaign()->camp->scenarios[bonDescs[i].info2].strongestHero(PlayerColor(bonDescs[i].info1));
  235. if(!superhero)
  236. logGlobal->warn("No superhero! How could it be transferred?");
  237. picNumber = superhero ? superhero->portrait : 0;
  238. desc = CGI->generaltexth->allTexts[719];
  239. boost::algorithm::replace_first(desc, "%s", getCampaign()->camp->scenarios[bonDescs[i].info2].scenarioName);
  240. break;
  241. }
  242. case CScenarioTravel::STravelBonus::HERO:
  243. desc = CGI->generaltexth->allTexts[718];
  244. boost::algorithm::replace_first(desc, "%s", CGI->generaltexth->capColors[bonDescs[i].info1]); //hero's color
  245. if(bonDescs[i].info2 == 0xFFFF)
  246. {
  247. boost::algorithm::replace_first(desc, "%s", CGI->generaltexth->allTexts[101]); //hero's name
  248. picNumber = -1;
  249. picName = "CBONN1A3.BMP";
  250. }
  251. else
  252. {
  253. boost::algorithm::replace_first(desc, "%s", CGI->heroh->objects[bonDescs[i].info2]->getNameTranslated());
  254. }
  255. break;
  256. }
  257. std::shared_ptr<CToggleButton> bonusButton = std::make_shared<CToggleButton>(Point(475 + i * 68, 455), "", CButton::tooltip(desc, desc));
  258. if(picNumber != -1)
  259. picName += ":" + std::to_string(picNumber);
  260. auto anim = std::make_shared<CAnimation>();
  261. anim->setCustom(picName, 0);
  262. bonusButton->setImage(anim);
  263. if(CSH->campaignBonus == i)
  264. bonusButton->setBorderColor(Colors::BRIGHT_YELLOW);
  265. groupBonuses->addToggle(i, bonusButton);
  266. }
  267. if(vstd::contains(getCampaign()->chosenCampaignBonuses, CSH->campaignMap))
  268. {
  269. groupBonuses->setSelected(getCampaign()->chosenCampaignBonuses[CSH->campaignMap]);
  270. }
  271. }
  272. void CBonusSelection::updateAfterStateChange()
  273. {
  274. if(CSH->state != EClientState::GAMEPLAY)
  275. {
  276. buttonRestart->disable();
  277. buttonStart->enable();
  278. if(!getCampaign()->mapsConquered.empty())
  279. buttonBack->block(true);
  280. else
  281. buttonBack->block(false);
  282. }
  283. else
  284. {
  285. buttonStart->disable();
  286. buttonRestart->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()->camp->scenarios[CSH->campaignMap].travelOptions.bonusesToChoose.size());
  296. }
  297. else if(buttonStart->isBlocked())
  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. mapDescription->setText(CSH->mi->getDescription());
  307. for(size_t i = 0; i < difficultyIcons.size(); i++)
  308. {
  309. if(i == CSH->si->difficulty)
  310. difficultyIcons[i]->enable();
  311. else
  312. difficultyIcons[i]->disable();
  313. }
  314. flagbox->recreate();
  315. createBonusesIcons();
  316. }
  317. void CBonusSelection::goBack()
  318. {
  319. if(CSH->state != EClientState::GAMEPLAY)
  320. {
  321. GH.windows().popWindows(2);
  322. }
  323. else
  324. {
  325. close();
  326. }
  327. // TODO: we can actually only pop bonus selection interface for custom campaigns
  328. // Though this would require clearing CLobbyScreen::bonusSel pointer when poping this interface
  329. /*
  330. else
  331. {
  332. close();
  333. CSH->state = EClientState::LOBBY;
  334. }
  335. */
  336. }
  337. void CBonusSelection::startMap()
  338. {
  339. auto showPrologVideo = [=]()
  340. {
  341. auto exitCb = [=]()
  342. {
  343. logGlobal->info("Starting scenario %d", CSH->campaignMap);
  344. CSH->sendStartGame();
  345. };
  346. const CCampaignScenario & scenario = getCampaign()->camp->scenarios[CSH->campaignMap];
  347. if(scenario.prolog.hasPrologEpilog)
  348. {
  349. GH.windows().createAndPushWindow<CPrologEpilogVideo>(scenario.prolog, exitCb);
  350. }
  351. else
  352. {
  353. exitCb();
  354. }
  355. };
  356. //block buttons immediately
  357. buttonStart->block(true);
  358. buttonRestart->block(true);
  359. buttonBack->block(true);
  360. if(LOCPLINT) // we're currently ingame, so ask for starting new map and end game
  361. {
  362. close();
  363. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [=]()
  364. {
  365. showPrologVideo();
  366. }, 0);
  367. }
  368. else
  369. {
  370. showPrologVideo();
  371. }
  372. }
  373. void CBonusSelection::restartMap()
  374. {
  375. close();
  376. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [=]()
  377. {
  378. GH.pushUserEvent(EUserEvent::RESTART_GAME);
  379. }, 0);
  380. }
  381. void CBonusSelection::increaseDifficulty()
  382. {
  383. CSH->setDifficulty(CSH->si->difficulty + 1);
  384. }
  385. void CBonusSelection::decreaseDifficulty()
  386. {
  387. // avoid negative overflow (0 - 1 = 255)
  388. if (CSH->si->difficulty > 0)
  389. CSH->setDifficulty(CSH->si->difficulty - 1);
  390. }
  391. CBonusSelection::CRegion::CRegion(int id, bool accessible, bool selectable, const CampaignRegions & campDsc)
  392. : CIntObject(LCLICK | SHOW_POPUP), idOfMapAndRegion(id), accessible(accessible), selectable(selectable)
  393. {
  394. OBJ_CONSTRUCTION;
  395. static const std::string colors[2][8] =
  396. {
  397. {"R", "B", "N", "G", "O", "V", "T", "P"},
  398. {"Re", "Bl", "Br", "Gr", "Or", "Vi", "Te", "Pi"}
  399. };
  400. const CampaignRegions::RegionDescription & desc = campDsc.regions[idOfMapAndRegion];
  401. pos.x += desc.xpos;
  402. pos.y += desc.ypos;
  403. std::string prefix = campDsc.campPrefix + desc.infix + "_";
  404. std::string suffix = colors[campDsc.colorSuffixLength - 1][CSH->si->campState->camp->scenarios[idOfMapAndRegion].regionColor];
  405. graphicsNotSelected = std::make_shared<CPicture>(prefix + "En" + suffix + ".BMP");
  406. graphicsNotSelected->disable();
  407. graphicsSelected = std::make_shared<CPicture>(prefix + "Se" + suffix + ".BMP");
  408. graphicsSelected->disable();
  409. graphicsStriped = std::make_shared<CPicture>(prefix + "Co" + suffix + ".BMP");
  410. graphicsStriped->disable();
  411. pos.w = graphicsNotSelected->pos.w;
  412. pos.h = graphicsNotSelected->pos.h;
  413. }
  414. void CBonusSelection::CRegion::updateState()
  415. {
  416. if(!accessible)
  417. {
  418. graphicsNotSelected->disable();
  419. graphicsSelected->disable();
  420. graphicsStriped->enable();
  421. }
  422. else if(CSH->campaignMap == idOfMapAndRegion)
  423. {
  424. graphicsNotSelected->disable();
  425. graphicsSelected->enable();
  426. graphicsStriped->disable();
  427. }
  428. else
  429. {
  430. graphicsNotSelected->enable();
  431. graphicsSelected->disable();
  432. graphicsStriped->disable();
  433. }
  434. }
  435. void CBonusSelection::CRegion::clickLeft(tribool down, bool previousState)
  436. {
  437. //select if selectable & clicked inside our graphic
  438. if(indeterminate(down))
  439. return;
  440. if(!down && selectable && !graphicsNotSelected->getSurface()->isTransparent(GH.getCursorPosition() - pos.topLeft()))
  441. {
  442. CSH->setCampaignMap(idOfMapAndRegion);
  443. }
  444. }
  445. void CBonusSelection::CRegion::showPopupWindow()
  446. {
  447. // FIXME: For some reason "down" is only ever contain indeterminate_value
  448. auto text = CSH->si->campState->camp->scenarios[idOfMapAndRegion].regionText;
  449. if(!graphicsNotSelected->getSurface()->isTransparent(GH.getCursorPosition() - pos.topLeft()) && text.size())
  450. {
  451. CRClickPopup::createAndPush(text);
  452. }
  453. }