CMapInfo.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "CCampaignHandler.h"
  18. #include "MapFormat.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 = std::make_unique<CampaignHeader>(CampaignHandler::getHeader(fileURI));
  60. if(!campaignHeader->valid)
  61. campaignHeader.reset();
  62. }
  63. void CMapInfo::countPlayers()
  64. {
  65. for(int i=0; i<PlayerColor::PLAYER_LIMIT_I; i++)
  66. {
  67. if(mapHeader->players[i].canHumanPlay)
  68. {
  69. amountOfPlayersOnMap++;
  70. amountOfHumanControllablePlayers++;
  71. }
  72. else if(mapHeader->players[i].canComputerPlay)
  73. {
  74. amountOfPlayersOnMap++;
  75. }
  76. }
  77. if(scenarioOptionsOfSave)
  78. for(const auto & playerInfo : scenarioOptionsOfSave->playerInfos)
  79. if(playerInfo.second.isControlledByHuman())
  80. amountOfHumanPlayersInSave++;
  81. }
  82. std::string CMapInfo::getName() const
  83. {
  84. if(campaignHeader && campaignHeader->name.length())
  85. return campaignHeader->name;
  86. else if(mapHeader && mapHeader->name.length())
  87. return mapHeader->name;
  88. else
  89. return VLC->generaltexth->allTexts[508];
  90. }
  91. std::string CMapInfo::getNameForList() const
  92. {
  93. if(scenarioOptionsOfSave)
  94. {
  95. // TODO: this could be handled differently
  96. std::vector<std::string> path;
  97. boost::split(path, fileURI, boost::is_any_of("\\/"));
  98. return path[path.size()-1];
  99. }
  100. else
  101. {
  102. return getName();
  103. }
  104. }
  105. std::string CMapInfo::getDescription() const
  106. {
  107. if(campaignHeader)
  108. return campaignHeader->description;
  109. else
  110. return mapHeader->description;
  111. }
  112. int CMapInfo::getMapSizeIconId() const
  113. {
  114. if(!mapHeader)
  115. return 4;
  116. switch(mapHeader->width)
  117. {
  118. case CMapHeader::MAP_SIZE_SMALL:
  119. return 0;
  120. case CMapHeader::MAP_SIZE_MIDDLE:
  121. return 1;
  122. case CMapHeader::MAP_SIZE_LARGE:
  123. return 2;
  124. case CMapHeader::MAP_SIZE_XLARGE:
  125. return 3;
  126. case CMapHeader::MAP_SIZE_HUGE:
  127. return 4;
  128. case CMapHeader::MAP_SIZE_XHUGE:
  129. return 5;
  130. case CMapHeader::MAP_SIZE_GIANT:
  131. return 6;
  132. default:
  133. return 4;
  134. }
  135. }
  136. int CMapInfo::getMapSizeFormatIconId() const
  137. {
  138. switch(mapHeader->version)
  139. {
  140. case EMapFormat::ROE:
  141. return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_RESTORATION_OF_ERATHIA)["iconIndex"].Integer();
  142. case EMapFormat::AB:
  143. return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_ARMAGEDDONS_BLADE)["iconIndex"].Integer();
  144. case EMapFormat::SOD:
  145. return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_SHADOW_OF_DEATH)["iconIndex"].Integer();
  146. case EMapFormat::WOG:
  147. return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_IN_THE_WAKE_OF_GODS)["iconIndex"].Integer();
  148. case EMapFormat::HOTA:
  149. return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_HORN_OF_THE_ABYSS)["iconIndex"].Integer();
  150. case EMapFormat::VCMI:
  151. return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_JSON_VCMI)["iconIndex"].Integer();
  152. }
  153. return 0;
  154. }
  155. std::string CMapInfo::getMapSizeName() const
  156. {
  157. switch(mapHeader->width)
  158. {
  159. case CMapHeader::MAP_SIZE_SMALL:
  160. return "S";
  161. case CMapHeader::MAP_SIZE_MIDDLE:
  162. return "M";
  163. case CMapHeader::MAP_SIZE_LARGE:
  164. return "L";
  165. case CMapHeader::MAP_SIZE_XLARGE:
  166. return "XL";
  167. case CMapHeader::MAP_SIZE_HUGE:
  168. return "H";
  169. case CMapHeader::MAP_SIZE_XHUGE:
  170. return "XH";
  171. case CMapHeader::MAP_SIZE_GIANT:
  172. return "G";
  173. default:
  174. return "C";
  175. }
  176. }
  177. VCMI_LIB_NAMESPACE_END