CMapInfo.cpp 4.9 KB

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