GameStatistics.cpp 11 KB

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