GameStatistics.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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(nullptr);
  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 << static_cast<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. std::vector<const CGObjectInstance *> ownedObjects;
  160. for(const CGObjectInstance * obj : gs->map->objects)
  161. {
  162. if(obj && obj->tempOwner == ps->color)
  163. ownedObjects.push_back(obj);
  164. }
  165. /// This is code from CPlayerSpecificInfoCallback::getMyObjects
  166. /// I'm really need to find out about callback interface design...
  167. for(const auto * object : ownedObjects)
  168. {
  169. //Mines
  170. if ( object->ID == Obj::MINE )
  171. {
  172. const auto * mine = dynamic_cast<const CGMine *>(object);
  173. assert(mine);
  174. tmp.push_back(mine);
  175. }
  176. }
  177. return tmp;
  178. }
  179. //calculates total number of artifacts that belong to given player
  180. int Statistic::getNumberOfArts(const PlayerState * ps)
  181. {
  182. int ret = 0;
  183. for(auto h : ps->heroes)
  184. {
  185. ret += h->artifactsInBackpack.size() + h->artifactsWorn.size();
  186. }
  187. return ret;
  188. }
  189. // get total strength of player army
  190. si64 Statistic::getArmyStrength(const PlayerState * ps, bool withTownGarrison)
  191. {
  192. si64 str = 0;
  193. for(auto h : ps->heroes)
  194. {
  195. if(!h->inTownGarrison || withTownGarrison) //original h3 behavior
  196. str += h->getArmyStrength();
  197. }
  198. return str;
  199. }
  200. // get total experience of all heroes
  201. si64 Statistic::getTotalExperience(const PlayerState * ps)
  202. {
  203. si64 tmp = 0;
  204. for(auto h : ps->heroes)
  205. tmp += h->exp;
  206. return tmp;
  207. }
  208. // get total gold income
  209. int Statistic::getIncome(const CGameState * gs, const PlayerState * ps)
  210. {
  211. int percentIncome = gs->getStartInfo()->getIthPlayersSettings(ps->color).handicap.percentIncome;
  212. int totalIncome = 0;
  213. //Heroes can produce gold as well - skill, specialty or arts
  214. for(const auto & h : ps->heroes)
  215. totalIncome += h->valOfBonuses(Selector::typeSubtype(BonusType::GENERATE_RESOURCE, BonusSubtypeID(GameResID(GameResID::GOLD)))) * percentIncome / 100;
  216. //Add town income of all towns
  217. for(const auto & t : ps->towns)
  218. totalIncome += t->dailyIncome()[EGameResID::GOLD];
  219. for(const CGMine * mine : getMines(gs, ps))
  220. if(mine->producedResource == EGameResID::GOLD)
  221. totalIncome += mine->getProducedQuantity();
  222. return totalIncome;
  223. }
  224. float Statistic::getMapExploredRatio(const CGameState * gs, PlayerColor player)
  225. {
  226. float visible = 0.0;
  227. float numTiles = 0.0;
  228. for(int layer = 0; layer < (gs->map->twoLevel ? 2 : 1); layer++)
  229. for(int y = 0; y < gs->map->height; ++y)
  230. for(int x = 0; x < gs->map->width; ++x)
  231. {
  232. TerrainTile tile = gs->map->getTile(int3(x, y, layer));
  233. if(tile.blocked && (!tile.visitable))
  234. continue;
  235. if(gs->isVisible(int3(x, y, layer), player))
  236. visible++;
  237. numTiles++;
  238. }
  239. return visible / numTiles;
  240. }
  241. const CGHeroInstance * Statistic::findBestHero(const CGameState * gs, const PlayerColor & color)
  242. {
  243. auto &h = gs->players.at(color).heroes;
  244. if(h.empty())
  245. return nullptr;
  246. //best hero will be that with highest exp
  247. int best = 0;
  248. for(int b=1; b<h.size(); ++b)
  249. {
  250. if(h[b]->exp > h[best]->exp)
  251. {
  252. best = b;
  253. }
  254. }
  255. return h[best];
  256. }
  257. std::vector<std::vector<PlayerColor>> Statistic::getRank(std::vector<std::pair<PlayerColor, si64>> stats)
  258. {
  259. std::sort(stats.begin(), stats.end(), [](const std::pair<PlayerColor, si64> & a, const std::pair<PlayerColor, si64> & b) { return a.second > b.second; });
  260. //put first element
  261. std::vector< std::vector<PlayerColor> > ret;
  262. ret.push_back( { stats[0].first } );
  263. //the rest of elements
  264. for(int g=1; g<stats.size(); ++g)
  265. {
  266. if(stats[g].second == stats[g-1].second)
  267. {
  268. (ret.end()-1)->push_back( stats[g].first );
  269. }
  270. else
  271. {
  272. //create next occupied rank
  273. ret.push_back( { stats[g].first });
  274. }
  275. }
  276. return ret;
  277. }
  278. int Statistic::getObeliskVisited(const CGameState * gs, const TeamID & t)
  279. {
  280. if(gs->map->obelisksVisited.count(t))
  281. return gs->map->obelisksVisited.at(t);
  282. else
  283. return 0;
  284. }
  285. float Statistic::getObeliskVisitedRatio(const CGameState * gs, const TeamID & t)
  286. {
  287. if(!gs->map->obeliskCount)
  288. return 0;
  289. return static_cast<float>(getObeliskVisited(gs, t)) / gs->map->obeliskCount;
  290. }
  291. std::map<EGameResID, int> Statistic::getNumMines(const CGameState * gs, const PlayerState * ps)
  292. {
  293. std::map<EGameResID, int> tmp;
  294. for(auto & res : EGameResID::ALL_RESOURCES())
  295. tmp[res] = 0;
  296. for(const CGMine * mine : getMines(gs, ps))
  297. tmp[mine->producedResource]++;
  298. return tmp;
  299. }
  300. float Statistic::getTownBuiltRatio(const PlayerState * ps)
  301. {
  302. float built = 0.0;
  303. float total = 0.0;
  304. for(const auto & t : ps->towns)
  305. {
  306. built += t->builtBuildings.size();
  307. for(const auto & b : t->town->buildings)
  308. if(!t->forbiddenBuildings.count(b.first))
  309. total += 1;
  310. }
  311. if(total < 1)
  312. return 0;
  313. return built / total;
  314. }
  315. VCMI_LIB_NAMESPACE_END