CBonusSelection.cpp 16 KB

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