GameStatistics.cpp 7.4 KB

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