GameStatistics.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * GameStatistics.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 "GameStatistics.h"
  12. #include "../CPlayerState.h"
  13. #include "../constants/StringConstants.h"
  14. #include "CGameState.h"
  15. #include "TerrainHandler.h"
  16. #include "CHeroHandler.h"
  17. #include "StartInfo.h"
  18. #include "HighScore.h"
  19. #include "../mapObjects/CGHeroInstance.h"
  20. #include "../mapObjects/CGTownInstance.h"
  21. #include "../mapObjects/CGObjectInstance.h"
  22. #include "../mapObjects/MiscObjects.h"
  23. #include "../mapping/CMap.h"
  24. VCMI_LIB_NAMESPACE_BEGIN
  25. void StatisticDataSet::add(StatisticDataSetEntry entry)
  26. {
  27. data.push_back(entry);
  28. }
  29. StatisticDataSetEntry StatisticDataSet::createEntry(const PlayerState * ps, const CGameState * gs)
  30. {
  31. StatisticDataSetEntry data;
  32. HighScoreParameter param = HighScore::prepareHighScores(gs, ps->color, false);
  33. HighScoreCalculation scenarioHighScores;
  34. scenarioHighScores.parameters.push_back(param);
  35. scenarioHighScores.isCampaign = false;
  36. data.map = gs->map->name.toString();
  37. data.timestamp = std::time(0);
  38. data.day = gs->getDate(Date::DAY);
  39. data.player = ps->color;
  40. data.team = ps->team;
  41. data.isHuman = ps->isHuman();
  42. data.status = ps->status;
  43. data.resources = ps->resources;
  44. data.numberHeroes = ps->heroes.size();
  45. data.numberTowns = gs->howManyTowns(ps->color);
  46. data.numberArtifacts = Statistic::getNumberOfArts(ps);
  47. data.armyStrength = Statistic::getArmyStrength(ps, true);
  48. data.income = Statistic::getIncome(gs, ps);
  49. data.mapExploredRatio = Statistic::getMapExploredRatio(gs, ps->color);
  50. data.obeliskVisitedRatio = Statistic::getObeliskVisitedRatio(gs, ps->team);
  51. data.numMines = Statistic::getNumMines(gs, ps);
  52. data.score = scenarioHighScores.calculate().total;
  53. data.maxHeroLevel = Statistic::findBestHero(gs, ps->color) ? Statistic::findBestHero(gs, ps->color)->level : 0;
  54. data.numBattlesNeutral = gs->statistic.values.numBattlesNeutral.count(ps->color) ? gs->statistic.values.numBattlesNeutral.at(ps->color) : 0;
  55. data.numBattlesPlayer = gs->statistic.values.numBattlesPlayer.count(ps->color) ? gs->statistic.values.numBattlesPlayer.at(ps->color) : 0;
  56. data.numWinBattlesNeutral = gs->statistic.values.numWinBattlesNeutral.count(ps->color) ? gs->statistic.values.numWinBattlesNeutral.at(ps->color) : 0;
  57. data.numWinBattlesPlayer = gs->statistic.values.numWinBattlesPlayer.count(ps->color) ? gs->statistic.values.numWinBattlesPlayer.at(ps->color) : 0;
  58. data.numHeroSurrendered = gs->statistic.values.numHeroSurrendered.count(ps->color) ? gs->statistic.values.numHeroSurrendered.at(ps->color) : 0;
  59. data.numHeroEscaped = gs->statistic.values.numHeroEscaped.count(ps->color) ? gs->statistic.values.numHeroEscaped.at(ps->color) : 0;
  60. return data;
  61. }
  62. std::string StatisticDataSet::toCsv()
  63. {
  64. std::stringstream ss;
  65. auto resources = std::vector<EGameResID>{EGameResID::GOLD, EGameResID::WOOD, EGameResID::MERCURY, EGameResID::ORE, EGameResID::SULFUR, EGameResID::CRYSTAL, EGameResID::GEMS};
  66. ss << "Map" << ";";
  67. ss << "Timestamp" << ";";
  68. ss << "Day" << ";";
  69. ss << "Player" << ";";
  70. ss << "Team" << ";";
  71. ss << "IsHuman" << ";";
  72. ss << "Status" << ";";
  73. ss << "NumberHeroes" << ";";
  74. ss << "NumberTowns" << ";";
  75. ss << "NumberArtifacts" << ";";
  76. ss << "ArmyStrength" << ";";
  77. ss << "Income" << ";";
  78. ss << "MapExploredRatio" << ";";
  79. ss << "ObeliskVisitedRatio" << ";";
  80. ss << "Score" << ";";
  81. ss << "MaxHeroLevel" << ";";
  82. ss << "NumBattlesNeutral" << ";";
  83. ss << "NumBattlesPlayer" << ";";
  84. ss << "NumWinBattlesNeutral" << ";";
  85. ss << "NumWinBattlesPlayer" << ";";
  86. ss << "NumHeroSurrendered" << ";";
  87. ss << "NumHeroEscaped";
  88. for(auto & resource : resources)
  89. ss << ";" << GameConstants::RESOURCE_NAMES[resource];
  90. for(auto & resource : resources)
  91. ss << ";" << GameConstants::RESOURCE_NAMES[resource] + "Mines";
  92. ss << "\r\n";
  93. for(auto & entry : data)
  94. {
  95. ss << entry.map << ";";
  96. ss << vstd::getFormattedDateTime(entry.timestamp, "%Y-%m-%dT%H:%M:%S") << ";";
  97. ss << entry.day << ";";
  98. ss << GameConstants::PLAYER_COLOR_NAMES[entry.player] << ";";
  99. ss << entry.team.getNum() << ";";
  100. ss << entry.isHuman << ";";
  101. ss << (int)entry.status << ";";
  102. ss << entry.numberHeroes << ";";
  103. ss << entry.numberTowns << ";";
  104. ss << entry.numberArtifacts << ";";
  105. ss << entry.armyStrength << ";";
  106. ss << entry.income << ";";
  107. ss << entry.mapExploredRatio << ";";
  108. ss << entry.obeliskVisitedRatio << ";";
  109. ss << entry.score << ";";
  110. ss << entry.maxHeroLevel << ";";
  111. ss << entry.numBattlesNeutral << ";";
  112. ss << entry.numBattlesPlayer << ";";
  113. ss << entry.numWinBattlesNeutral << ";";
  114. ss << entry.numWinBattlesPlayer << ";";
  115. ss << entry.numHeroSurrendered << ";";
  116. ss << entry.numHeroEscaped;
  117. for(auto & resource : resources)
  118. ss << ";" << entry.resources[resource];
  119. for(auto & resource : resources)
  120. ss << ";" << entry.numMines[resource];
  121. ss << "\r\n";
  122. }
  123. return ss.str();
  124. }
  125. std::vector<const CGMine *> Statistic::getMines(const CGameState * gs, const PlayerState * ps)
  126. {
  127. std::vector<const CGMine *> tmp;
  128. /// FIXME: Dirty dirty hack
  129. /// Stats helper need some access to gamestate.
  130. std::vector<const CGObjectInstance *> ownedObjects;
  131. for(const CGObjectInstance * obj : gs->map->objects)
  132. {
  133. if(obj && obj->tempOwner == ps->color)
  134. ownedObjects.push_back(obj);
  135. }
  136. /// This is code from CPlayerSpecificInfoCallback::getMyObjects
  137. /// I'm really need to find out about callback interface design...
  138. for(const auto * object : ownedObjects)
  139. {
  140. //Mines
  141. if ( object->ID == Obj::MINE )
  142. {
  143. const auto * mine = dynamic_cast<const CGMine *>(object);
  144. assert(mine);
  145. tmp.push_back(mine);
  146. }
  147. }
  148. return tmp;
  149. }
  150. //calculates total number of artifacts that belong to given player
  151. int Statistic::getNumberOfArts(const PlayerState * ps)
  152. {
  153. int ret = 0;
  154. for(auto h : ps->heroes)
  155. {
  156. ret += (int)h->artifactsInBackpack.size() + (int)h->artifactsWorn.size();
  157. }
  158. return ret;
  159. }
  160. // get total strength of player army
  161. si64 Statistic::getArmyStrength(const PlayerState * ps, bool withTownGarrison)
  162. {
  163. si64 str = 0;
  164. for(auto h : ps->heroes)
  165. {
  166. if(!h->inTownGarrison || withTownGarrison) //original h3 behavior
  167. str += h->getArmyStrength();
  168. }
  169. return str;
  170. }
  171. // get total gold income
  172. int Statistic::getIncome(const CGameState * gs, const PlayerState * ps)
  173. {
  174. int percentIncome = gs->getStartInfo()->getIthPlayersSettings(ps->color).handicap.percentIncome;
  175. int totalIncome = 0;
  176. const CGObjectInstance * heroOrTown = nullptr;
  177. //Heroes can produce gold as well - skill, specialty or arts
  178. for(const auto & h : ps->heroes)
  179. {
  180. totalIncome += h->valOfBonuses(Selector::typeSubtype(BonusType::GENERATE_RESOURCE, BonusSubtypeID(GameResID(GameResID::GOLD)))) * percentIncome / 100;
  181. if(!heroOrTown)
  182. heroOrTown = h;
  183. }
  184. //Add town income of all towns
  185. for(const auto & t : ps->towns)
  186. {
  187. totalIncome += t->dailyIncome()[EGameResID::GOLD];
  188. if(!heroOrTown)
  189. heroOrTown = t;
  190. }
  191. for(const CGMine * mine : getMines(gs, ps))
  192. {
  193. if (mine->producedResource == EGameResID::GOLD)
  194. totalIncome += mine->getProducedQuantity();
  195. }
  196. return totalIncome;
  197. }
  198. float Statistic::getMapExploredRatio(const CGameState * gs, PlayerColor player)
  199. {
  200. float visible = 0.0;
  201. float numTiles = 0.0;
  202. for(int layer = 0; layer < (gs->map->twoLevel ? 2 : 1); layer++)
  203. for(int y = 0; y < gs->map->height; ++y)
  204. for(int x = 0; x < gs->map->width; ++x)
  205. {
  206. TerrainTile tile = gs->map->getTile(int3(x, y, layer));
  207. if(tile.blocked && (!tile.visitable))
  208. continue;
  209. if(gs->isVisible(int3(x, y, layer), player))
  210. visible++;
  211. numTiles++;
  212. }
  213. return visible / numTiles;
  214. }
  215. const CGHeroInstance * Statistic::findBestHero(const CGameState * gs, const PlayerColor & color)
  216. {
  217. auto &h = gs->players.at(color).heroes;
  218. if(h.empty())
  219. return nullptr;
  220. //best hero will be that with highest exp
  221. int best = 0;
  222. for(int b=1; b<h.size(); ++b)
  223. {
  224. if(h[b]->exp > h[best]->exp)
  225. {
  226. best = b;
  227. }
  228. }
  229. return h[best];
  230. }
  231. std::vector<std::vector<PlayerColor>> Statistic::getRank(std::vector<std::pair<PlayerColor, si64>> stats)
  232. {
  233. std::sort(stats.begin(), stats.end(), [](const std::pair<PlayerColor, si64> & a, const std::pair<PlayerColor, si64> & b) { return a.second > b.second; });
  234. //put first element
  235. std::vector< std::vector<PlayerColor> > ret;
  236. std::vector<PlayerColor> tmp;
  237. tmp.push_back( stats[0].first );
  238. ret.push_back( tmp );
  239. //the rest of elements
  240. for(int g=1; g<stats.size(); ++g)
  241. {
  242. if(stats[g].second == stats[g-1].second)
  243. {
  244. (ret.end()-1)->push_back( stats[g].first );
  245. }
  246. else
  247. {
  248. //create next occupied rank
  249. std::vector<PlayerColor> tmp;
  250. tmp.push_back(stats[g].first);
  251. ret.push_back(tmp);
  252. }
  253. }
  254. return ret;
  255. }
  256. int Statistic::getObeliskVisited(const CGameState * gs, const TeamID & t)
  257. {
  258. if(gs->map->obelisksVisited.count(t))
  259. return gs->map->obelisksVisited.at(t);
  260. else
  261. return 0;
  262. }
  263. float Statistic::getObeliskVisitedRatio(const CGameState * gs, const TeamID & t)
  264. {
  265. if(!gs->map->obeliskCount)
  266. return 0;
  267. return (float)getObeliskVisited(gs, t) / (float)gs->map->obeliskCount;
  268. }
  269. std::map<EGameResID, int> Statistic::getNumMines(const CGameState * gs, const PlayerState * ps)
  270. {
  271. std::map<EGameResID, int> tmp;
  272. for(auto & res : EGameResID::ALL_RESOURCES())
  273. tmp[res] = 0;
  274. for(const CGMine * mine : getMines(gs, ps))
  275. tmp[mine->producedResource]++;
  276. return tmp;
  277. }
  278. VCMI_LIB_NAMESPACE_END