CBonusSelection.cpp 17 KB

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