CMapInfo.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * CMapInfo.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 "CMapInfo.h"
  12. #include "../filesystem/ResourceID.h"
  13. #include "../StartInfo.h"
  14. #include "../GameConstants.h"
  15. #include "CMapService.h"
  16. #include "CMapHeader.h"
  17. #include "MapFormat.h"
  18. #include "../campaign/CampaignHandler.h"
  19. #include "../filesystem/Filesystem.h"
  20. #include "../serializer/CMemorySerializer.h"
  21. #include "../CGeneralTextHandler.h"
  22. #include "../rmg/CMapGenOptions.h"
  23. #include "../CCreatureHandler.h"
  24. #include "../GameSettings.h"
  25. #include "../CHeroHandler.h"
  26. VCMI_LIB_NAMESPACE_BEGIN
  27. CMapInfo::CMapInfo()
  28. : scenarioOptionsOfSave(nullptr), amountOfPlayersOnMap(0), amountOfHumanControllablePlayers(0), amountOfHumanPlayersInSave(0), isRandomMap(false)
  29. {
  30. }
  31. CMapInfo::~CMapInfo()
  32. {
  33. vstd::clear_pointer(scenarioOptionsOfSave);
  34. }
  35. void CMapInfo::mapInit(const std::string & fname)
  36. {
  37. fileURI = fname;
  38. CMapService mapService;
  39. mapHeader = mapService.loadMapHeader(ResourceID(fname, EResType::MAP));
  40. countPlayers();
  41. }
  42. void CMapInfo::saveInit(const ResourceID & file)
  43. {
  44. CLoadFile lf(*CResourceHandler::get()->getResourceName(file), MINIMAL_SERIALIZATION_VERSION);
  45. lf.checkMagicBytes(SAVEGAME_MAGIC);
  46. mapHeader = std::make_unique<CMapHeader>();
  47. lf >> *(mapHeader) >> scenarioOptionsOfSave;
  48. fileURI = file.getName();
  49. countPlayers();
  50. std::time_t time = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(file));
  51. date = std::asctime(std::localtime(&time));
  52. // We absolutely not need this data for lobby and server will read it from save
  53. // FIXME: actually we don't want them in CMapHeader!
  54. mapHeader->triggeredEvents.clear();
  55. }
  56. void CMapInfo::campaignInit()
  57. {
  58. campaign = CampaignHandler::getHeader(fileURI);
  59. }
  60. void CMapInfo::countPlayers()
  61. {
  62. for(int i=0; i<PlayerColor::PLAYER_LIMIT_I; i++)
  63. {
  64. if(mapHeader->players[i].canHumanPlay)
  65. {
  66. amountOfPlayersOnMap++;
  67. amountOfHumanControllablePlayers++;
  68. }
  69. else if(mapHeader->players[i].canComputerPlay)
  70. {
  71. amountOfPlayersOnMap++;
  72. }
  73. }
  74. if(scenarioOptionsOfSave)
  75. for(const auto & playerInfo : scenarioOptionsOfSave->playerInfos)
  76. if(playerInfo.second.isControlledByHuman())
  77. amountOfHumanPlayersInSave++;
  78. }
  79. std::string CMapInfo::getName() const
  80. {
  81. if(campaign && !campaign->getName().empty())
  82. return campaign->getName();
  83. else if(mapHeader && mapHeader->name.length())
  84. return mapHeader->name;
  85. else
  86. return VLC->generaltexth->allTexts[508];
  87. }
  88. std::string CMapInfo::getNameForList() const
  89. {
  90. if(scenarioOptionsOfSave)
  91. {
  92. // TODO: this could be handled differently
  93. std::vector<std::string> path;
  94. boost::split(path, fileURI, boost::is_any_of("\\/"));
  95. return path[path.size()-1];
  96. }
  97. else
  98. {
  99. return getName();
  100. }
  101. }
  102. std::string CMapInfo::getDescription() const
  103. {
  104. if(campaign)
  105. return campaign->getDescription();
  106. else
  107. return mapHeader->description;
  108. }
  109. int CMapInfo::getMapSizeIconId() const
  110. {
  111. if(!mapHeader)
  112. return 4;
  113. switch(mapHeader->width)
  114. {
  115. case CMapHeader::MAP_SIZE_SMALL:
  116. return 0;
  117. case CMapHeader::MAP_SIZE_MIDDLE:
  118. return 1;
  119. case CMapHeader::MAP_SIZE_LARGE:
  120. return 2;
  121. case CMapHeader::MAP_SIZE_XLARGE:
  122. return 3;
  123. case CMapHeader::MAP_SIZE_HUGE:
  124. return 4;
  125. case CMapHeader::MAP_SIZE_XHUGE:
  126. return 5;
  127. case CMapHeader::MAP_SIZE_GIANT:
  128. return 6;
  129. default:
  130. return 4;
  131. }
  132. }
  133. int CMapInfo::getMapSizeFormatIconId() const
  134. {
  135. switch(mapHeader->version)
  136. {
  137. case EMapFormat::ROE:
  138. return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_RESTORATION_OF_ERATHIA)["iconIndex"].Integer();
  139. case EMapFormat::AB:
  140. return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_ARMAGEDDONS_BLADE)["iconIndex"].Integer();
  141. case EMapFormat::SOD:
  142. return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_SHADOW_OF_DEATH)["iconIndex"].Integer();
  143. case EMapFormat::WOG:
  144. return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_IN_THE_WAKE_OF_GODS)["iconIndex"].Integer();
  145. case EMapFormat::HOTA:
  146. return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_HORN_OF_THE_ABYSS)["iconIndex"].Integer();
  147. case EMapFormat::VCMI:
  148. return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_JSON_VCMI)["iconIndex"].Integer();
  149. }
  150. return 0;
  151. }
  152. std::string CMapInfo::getMapSizeName() const
  153. {
  154. switch(mapHeader->width)
  155. {
  156. case CMapHeader::MAP_SIZE_SMALL:
  157. return "S";
  158. case CMapHeader::MAP_SIZE_MIDDLE:
  159. return "M";
  160. case CMapHeader::MAP_SIZE_LARGE:
  161. return "L";
  162. case CMapHeader::MAP_SIZE_XLARGE:
  163. return "XL";
  164. case CMapHeader::MAP_SIZE_HUGE:
  165. return "H";
  166. case CMapHeader::MAP_SIZE_XHUGE:
  167. return "XH";
  168. case CMapHeader::MAP_SIZE_GIANT:
  169. return "G";
  170. default:
  171. return "C";
  172. }
  173. }
  174. VCMI_LIB_NAMESPACE_END