CHighScoreScreen.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * CHighScoreScreen.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 "CHighScoreScreen.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../gui/WindowHandler.h"
  14. #include "../gui/Shortcut.h"
  15. #include "../widgets/Buttons.h"
  16. #include "../widgets/CTextInput.h"
  17. #include "../widgets/Images.h"
  18. #include "../widgets/GraphicalPrimitiveCanvas.h"
  19. #include "../windows/InfoWindows.h"
  20. #include "../widgets/TextControls.h"
  21. #include "../render/Canvas.h"
  22. #include "../render/IRenderHandler.h"
  23. #include "../CGameInfo.h"
  24. #include "../CVideoHandler.h"
  25. #include "../CMusicHandler.h"
  26. #include "../../lib/CGeneralTextHandler.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include "../../lib/CCreatureHandler.h"
  29. #include "../../lib/constants/EntityIdentifiers.h"
  30. #include "../../lib/TextOperations.h"
  31. #include "../../lib/Languages.h"
  32. auto HighScoreCalculation::calculate()
  33. {
  34. struct Result
  35. {
  36. int basic = 0;
  37. int total = 0;
  38. int sumDays = 0;
  39. bool cheater = false;
  40. };
  41. Result firstResult;
  42. Result summary;
  43. const std::array<double, 5> difficultyMultipliers{0.8, 1.0, 1.3, 1.6, 2.0};
  44. for(auto & param : parameters)
  45. {
  46. double tmp = 200 - (param.day + 10) / (param.townAmount + 5) + (param.allDefeated ? 25 : 0) + (param.hasGrail ? 25 : 0);
  47. firstResult = Result{static_cast<int>(tmp), static_cast<int>(tmp * difficultyMultipliers.at(param.difficulty)), param.day, param.usedCheat};
  48. summary.basic += firstResult.basic * 5.0 / parameters.size();
  49. summary.total += firstResult.total * 5.0 / parameters.size();
  50. summary.sumDays += firstResult.sumDays;
  51. summary.cheater |= firstResult.cheater;
  52. }
  53. if(parameters.size() == 1)
  54. return firstResult;
  55. return summary;
  56. }
  57. struct HighScoreCreature
  58. {
  59. CreatureID creature;
  60. int min;
  61. int max;
  62. };
  63. static std::vector<HighScoreCreature> getHighscoreCreaturesList()
  64. {
  65. JsonNode configCreatures(JsonPath::builtin("CONFIG/highscoreCreatures.json"));
  66. std::vector<HighScoreCreature> ret;
  67. for(auto & json : configCreatures["creatures"].Vector())
  68. {
  69. HighScoreCreature entry;
  70. entry.creature = CreatureID::decode(json["creature"].String());
  71. entry.max = json["max"].isNull() ? std::numeric_limits<int>::max() : json["max"].Integer();
  72. entry.min = json["min"].isNull() ? std::numeric_limits<int>::min() : json["min"].Integer();
  73. ret.push_back(entry);
  74. }
  75. return ret;
  76. }
  77. CreatureID HighScoreCalculation::getCreatureForPoints(int points, bool campaign)
  78. {
  79. static const std::vector<HighScoreCreature> creatures = getHighscoreCreaturesList();
  80. int divide = campaign ? 5 : 1;
  81. for(auto & creature : creatures)
  82. if(points / divide <= creature.max && points / divide >= creature.min)
  83. return creature.creature;
  84. throw std::runtime_error("Unable to find creature for score " + std::to_string(points));
  85. }
  86. CHighScoreScreen::CHighScoreScreen(HighScorePage highscorepage, int highlighted)
  87. : CWindowObject(BORDERED), highscorepage(highscorepage), highlighted(highlighted)
  88. {
  89. addUsedEvents(SHOW_POPUP);
  90. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  91. pos = center(Rect(0, 0, 800, 600));
  92. backgroundAroundMenu = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(-pos.x, -pos.y, GH.screenDimensions().x, GH.screenDimensions().y));
  93. addHighScores();
  94. addButtons();
  95. }
  96. void CHighScoreScreen::showPopupWindow(const Point & cursorPosition)
  97. {
  98. for (int i = 0; i < screenRows; i++)
  99. {
  100. bool currentGameNotInListEntry = i == (screenRows - 1) && highlighted > (screenRows - 1);
  101. Rect r = Rect(80, 40 + i * 50, 635, 50);
  102. if(r.isInside(cursorPosition - pos))
  103. {
  104. std::string tmp = persistentStorage["highscore"][highscorepage == HighScorePage::SCENARIO ? "scenario" : "campaign"][currentGameNotInListEntry ? highlighted : i]["datetime"].String();
  105. if(!tmp.empty())
  106. CRClickPopup::createAndPush(tmp);
  107. }
  108. }
  109. }
  110. void CHighScoreScreen::addButtons()
  111. {
  112. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  113. buttons.clear();
  114. buttons.push_back(std::make_shared<CButton>(Point(31, 113), AnimationPath::builtin("HISCCAM.DEF"), CButton::tooltip(), [&](){ buttonCampaignClick(); }, EShortcut::HIGH_SCORES_CAMPAIGNS));
  115. buttons.push_back(std::make_shared<CButton>(Point(31, 345), AnimationPath::builtin("HISCSTA.DEF"), CButton::tooltip(), [&](){ buttonScenarioClick(); }, EShortcut::HIGH_SCORES_SCENARIOS));
  116. buttons.push_back(std::make_shared<CButton>(Point(726, 113), AnimationPath::builtin("HISCRES.DEF"), CButton::tooltip(), [&](){ buttonResetClick(); }, EShortcut::HIGH_SCORES_RESET));
  117. buttons.push_back(std::make_shared<CButton>(Point(726, 345), AnimationPath::builtin("HISCEXT.DEF"), CButton::tooltip(), [&](){ buttonExitClick(); }, EShortcut::GLOBAL_RETURN));
  118. }
  119. void CHighScoreScreen::addHighScores()
  120. {
  121. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  122. background = std::make_shared<CPicture>(ImagePath::builtin(highscorepage == HighScorePage::SCENARIO ? "HISCORE" : "HISCORE2"));
  123. texts.clear();
  124. images.clear();
  125. // Header
  126. texts.push_back(std::make_shared<CLabel>(115, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.433"))); // rank
  127. texts.push_back(std::make_shared<CLabel>(225, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.434"))); // player
  128. if(highscorepage == HighScorePage::SCENARIO)
  129. {
  130. texts.push_back(std::make_shared<CLabel>(405, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.435"))); // land
  131. texts.push_back(std::make_shared<CLabel>(557, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.436"))); // days
  132. texts.push_back(std::make_shared<CLabel>(627, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75"))); // score
  133. }
  134. else
  135. {
  136. texts.push_back(std::make_shared<CLabel>(405, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.672"))); // campaign
  137. texts.push_back(std::make_shared<CLabel>(592, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75"))); // score
  138. }
  139. // Content
  140. int y = 66;
  141. auto & data = persistentStorage["highscore"][highscorepage == HighScorePage::SCENARIO ? "scenario" : "campaign"];
  142. for (int i = 0; i < screenRows; i++)
  143. {
  144. bool currentGameNotInListEntry = (i == (screenRows - 1) && highlighted > (screenRows - 1));
  145. auto & curData = data[currentGameNotInListEntry ? highlighted : i];
  146. ColorRGBA color = (i == highlighted || currentGameNotInListEntry) ? Colors::YELLOW : Colors::WHITE;
  147. texts.push_back(std::make_shared<CLabel>(115, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string((currentGameNotInListEntry ? highlighted : i) + 1)));
  148. std::string tmp = curData["player"].String();
  149. TextOperations::trimRightUnicode(tmp, std::max(0, (int)TextOperations::getUnicodeCharactersCount(tmp) - 13));
  150. texts.push_back(std::make_shared<CLabel>(225, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, tmp));
  151. if(highscorepage == HighScorePage::SCENARIO)
  152. {
  153. std::string tmp = curData["scenarioName"].String();
  154. TextOperations::trimRightUnicode(tmp, std::max(0, (int)TextOperations::getUnicodeCharactersCount(tmp) - 25));
  155. texts.push_back(std::make_shared<CLabel>(405, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, tmp));
  156. texts.push_back(std::make_shared<CLabel>(557, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(curData["days"].Integer())));
  157. texts.push_back(std::make_shared<CLabel>(627, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(curData["points"].Integer())));
  158. }
  159. else
  160. {
  161. std::string tmp = curData["campaignName"].String();
  162. TextOperations::trimRightUnicode(tmp, std::max(0, (int)TextOperations::getUnicodeCharactersCount(tmp) - 25));
  163. texts.push_back(std::make_shared<CLabel>(405, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, tmp));
  164. texts.push_back(std::make_shared<CLabel>(592, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(curData["points"].Integer())));
  165. }
  166. if(curData["points"].Integer() > 0 && curData["points"].Integer() <= ((highscorepage == HighScorePage::CAMPAIGN) ? 2500 : 500))
  167. images.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("CPRSMALL"), (*CGI->creh)[HighScoreCalculation::getCreatureForPoints(curData["points"].Integer(), highscorepage == HighScorePage::CAMPAIGN)]->getIconIndex(), 0, 670, y - 15 + i * 50));
  168. }
  169. }
  170. void CHighScoreScreen::buttonCampaignClick()
  171. {
  172. highscorepage = HighScorePage::CAMPAIGN;
  173. addHighScores();
  174. addButtons();
  175. redraw();
  176. }
  177. void CHighScoreScreen::buttonScenarioClick()
  178. {
  179. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  180. highscorepage = HighScorePage::SCENARIO;
  181. addHighScores();
  182. addButtons();
  183. redraw();
  184. }
  185. void CHighScoreScreen::buttonResetClick()
  186. {
  187. CInfoWindow::showYesNoDialog(
  188. CGI->generaltexth->allTexts[666],
  189. {},
  190. [this]()
  191. {
  192. Settings entry = persistentStorage.write["highscore"];
  193. entry->clear();
  194. addHighScores();
  195. addButtons();
  196. redraw();
  197. },
  198. 0
  199. );
  200. }
  201. void CHighScoreScreen::buttonExitClick()
  202. {
  203. close();
  204. }
  205. CHighScoreInputScreen::CHighScoreInputScreen(bool won, HighScoreCalculation calc)
  206. : CWindowObject(BORDERED), won(won), calc(calc), videoSoundHandle(-1)
  207. {
  208. addUsedEvents(LCLICK | KEYBOARD);
  209. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  210. pos = center(Rect(0, 0, 800, 600));
  211. backgroundAroundMenu = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(-pos.x, -pos.y, GH.screenDimensions().x, GH.screenDimensions().y));
  212. background = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), Colors::BLACK);
  213. if(won)
  214. {
  215. int border = 100;
  216. int textareaW = ((pos.w - 2 * border) / 4);
  217. std::vector<std::string> t = { "438", "439", "440", "441", "676" }; // time, score, difficulty, final score, rank
  218. for (int i = 0; i < 5; i++)
  219. texts.push_back(std::make_shared<CMultiLineLabel>(Rect(textareaW * i + border - (textareaW / 2), 450, textareaW, 100), FONT_HIGH_SCORE, ETextAlignment::TOPCENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt." + t[i])));
  220. std::string creatureName = (calc.calculate().cheater) ? CGI->generaltexth->translate("core.genrltxt.260") : (*CGI->creh)[HighScoreCalculation::getCreatureForPoints(calc.calculate().total, calc.isCampaign)]->getNameSingularTranslated();
  221. t = { std::to_string(calc.calculate().sumDays), std::to_string(calc.calculate().basic), CGI->generaltexth->translate("core.arraytxt." + std::to_string((142 + calc.parameters[0].difficulty))), std::to_string(calc.calculate().total), creatureName };
  222. for (int i = 0; i < 5; i++)
  223. texts.push_back(std::make_shared<CMultiLineLabel>(Rect(textareaW * i + border - (textareaW / 2), 530, textareaW, 100), FONT_HIGH_SCORE, ETextAlignment::TOPCENTER, Colors::WHITE, t[i]));
  224. CCS->musich->playMusic(AudioPath::builtin("music/Win Scenario"), true, true);
  225. }
  226. else
  227. CCS->musich->playMusic(AudioPath::builtin("music/UltimateLose"), false, true);
  228. video = won ? "HSANIM.SMK" : "LOSEGAME.SMK";
  229. }
  230. int CHighScoreInputScreen::addEntry(std::string text) {
  231. std::vector<JsonNode> baseNode = persistentStorage["highscore"][calc.isCampaign ? "campaign" : "scenario"].Vector();
  232. auto sortFunctor = [](const JsonNode & left, const JsonNode & right)
  233. {
  234. if(left["points"].Integer() == right["points"].Integer())
  235. return left["posFlag"].Bool() > right["posFlag"].Bool();
  236. return left["points"].Integer() > right["points"].Integer();
  237. };
  238. JsonNode newNode = JsonNode();
  239. newNode["player"].String() = text;
  240. if(calc.isCampaign)
  241. newNode["campaignName"].String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].campaignName;
  242. else
  243. newNode["scenarioName"].String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].scenarioName;
  244. newNode["days"].Integer() = calc.calculate().sumDays;
  245. newNode["points"].Integer() = calc.calculate().cheater ? 0 : calc.calculate().total;
  246. newNode["datetime"].String() = TextOperations::getFormattedDateTimeLocal(std::time(nullptr));
  247. newNode["posFlag"].Bool() = true;
  248. baseNode.push_back(newNode);
  249. boost::range::sort(baseNode, sortFunctor);
  250. int pos = -1;
  251. for (int i = 0; i < baseNode.size(); i++)
  252. {
  253. if(!baseNode[i]["posFlag"].isNull())
  254. {
  255. baseNode[i]["posFlag"].clear();
  256. pos = i;
  257. }
  258. }
  259. Settings s = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"];
  260. s->Vector() = baseNode;
  261. return pos;
  262. }
  263. void CHighScoreInputScreen::show(Canvas & to)
  264. {
  265. if(background)
  266. background->show(to);
  267. CCS->videoh->update(pos.x, pos.y, to.getInternalSurface(), true, false,
  268. [&]()
  269. {
  270. if(won)
  271. {
  272. CCS->videoh->close();
  273. video = "HSLOOP.SMK";
  274. auto audioData = CCS->videoh->getAudio(VideoPath::builtin(video));
  275. videoSoundHandle = CCS->soundh->playSound(audioData);
  276. CCS->videoh->open(VideoPath::builtin(video));
  277. }
  278. else
  279. close();
  280. });
  281. if(input)
  282. input->showAll(to);
  283. for(auto & text : texts)
  284. text->showAll(to);
  285. CIntObject::show(to);
  286. }
  287. void CHighScoreInputScreen::activate()
  288. {
  289. auto audioData = CCS->videoh->getAudio(VideoPath::builtin(video));
  290. videoSoundHandle = CCS->soundh->playSound(audioData);
  291. if(!CCS->videoh->open(VideoPath::builtin(video)))
  292. {
  293. if(!won)
  294. close();
  295. }
  296. else
  297. background = nullptr;
  298. CIntObject::activate();
  299. }
  300. void CHighScoreInputScreen::deactivate()
  301. {
  302. CCS->videoh->close();
  303. CCS->soundh->stopSound(videoSoundHandle);
  304. CIntObject::deactivate();
  305. }
  306. void CHighScoreInputScreen::clickPressed(const Point & cursorPosition)
  307. {
  308. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  309. if(!won)
  310. {
  311. close();
  312. return;
  313. }
  314. if(!input)
  315. {
  316. input = std::make_shared<CHighScoreInput>(calc.parameters[0].playerName,
  317. [&] (std::string text)
  318. {
  319. if(!text.empty())
  320. {
  321. int pos = addEntry(text);
  322. close();
  323. GH.windows().createAndPushWindow<CHighScoreScreen>(calc.isCampaign ? CHighScoreScreen::HighScorePage::CAMPAIGN : CHighScoreScreen::HighScorePage::SCENARIO, pos);
  324. }
  325. else
  326. close();
  327. });
  328. }
  329. }
  330. void CHighScoreInputScreen::keyPressed(EShortcut key)
  331. {
  332. clickPressed(Point());
  333. }
  334. CHighScoreInput::CHighScoreInput(std::string playerName, std::function<void(std::string text)> readyCB)
  335. : CWindowObject(NEEDS_ANIMATED_BACKGROUND, ImagePath::builtin("HIGHNAME")), ready(readyCB)
  336. {
  337. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  338. pos = center(Rect(0, 0, 232, 212));
  339. updateShadow();
  340. text = std::make_shared<CMultiLineLabel>(Rect(15, 15, 202, 202), FONT_SMALL, ETextAlignment::TOPCENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.96"));
  341. buttonOk = std::make_shared<CButton>(Point(26, 142), AnimationPath::builtin("MUBCHCK.DEF"), CGI->generaltexth->zelp[560], std::bind(&CHighScoreInput::okay, this), EShortcut::GLOBAL_ACCEPT);
  342. buttonCancel = std::make_shared<CButton>(Point(142, 142), AnimationPath::builtin("MUBCANC.DEF"), CGI->generaltexth->zelp[561], std::bind(&CHighScoreInput::abort, this), EShortcut::GLOBAL_CANCEL);
  343. // FIXME: broken. Never activates?
  344. // statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 186, 218, 18), 7, 186));
  345. textInput = std::make_shared<CTextInput>(Rect(18, 104, 200, 25), FONT_SMALL, ETextAlignment::CENTER, true);
  346. textInput->setText(playerName);
  347. }
  348. void CHighScoreInput::okay()
  349. {
  350. ready(textInput->getText());
  351. }
  352. void CHighScoreInput::abort()
  353. {
  354. ready("");
  355. }