CBonusSelection.cpp 16 KB

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