CMapInfo.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/ResourcePath.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 "../rmg/CMapGenOptions.h"
  21. #include "../serializer/CLoadFile.h"
  22. #include "../texts/CGeneralTextHandler.h"
  23. #include "../texts/TextOperations.h"
  24. #include "../CCreatureHandler.h"
  25. #include "../IGameSettings.h"
  26. #include "../CConfigHandler.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. ResourcePath resource = ResourcePath(fname, EResType::MAP);
  41. originalFileURI = resource.getOriginalName();
  42. fullFileURI = boost::filesystem::canonical(*CResourceHandler::get()->getResourceName(resource)).string();
  43. mapHeader = mapService.loadMapHeader(resource);
  44. countPlayers();
  45. }
  46. void CMapInfo::saveInit(const ResourcePath & file)
  47. {
  48. CLoadFile lf(*CResourceHandler::get()->getResourceName(file), ESerializationVersion::MINIMAL);
  49. lf.checkMagicBytes(SAVEGAME_MAGIC);
  50. mapHeader = std::make_unique<CMapHeader>();
  51. lf >> *(mapHeader) >> scenarioOptionsOfSave;
  52. fileURI = file.getName();
  53. originalFileURI = file.getOriginalName();
  54. fullFileURI = boost::filesystem::canonical(*CResourceHandler::get()->getResourceName(file)).string();
  55. countPlayers();
  56. lastWrite = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(file));
  57. date = TextOperations::getFormattedDateTimeLocal(lastWrite);
  58. // We absolutely not need this data for lobby and server will read it from save
  59. // FIXME: actually we don't want them in CMapHeader!
  60. mapHeader->triggeredEvents.clear();
  61. }
  62. void CMapInfo::campaignInit()
  63. {
  64. ResourcePath resource = ResourcePath(fileURI, EResType::CAMPAIGN);
  65. originalFileURI = resource.getOriginalName();
  66. fullFileURI = boost::filesystem::canonical(*CResourceHandler::get()->getResourceName(resource)).string();
  67. campaign = CampaignHandler::getHeader(fileURI);
  68. }
  69. void CMapInfo::countPlayers()
  70. {
  71. for(int i=0; i<PlayerColor::PLAYER_LIMIT_I; i++)
  72. {
  73. if(mapHeader->players[i].canHumanPlay)
  74. {
  75. amountOfPlayersOnMap++;
  76. amountOfHumanControllablePlayers++;
  77. }
  78. else if(mapHeader->players[i].canComputerPlay)
  79. {
  80. amountOfPlayersOnMap++;
  81. }
  82. }
  83. if(scenarioOptionsOfSave)
  84. for(const auto & playerInfo : scenarioOptionsOfSave->playerInfos)
  85. if(playerInfo.second.isControlledByHuman())
  86. amountOfHumanPlayersInSave++;
  87. }
  88. std::string CMapInfo::getNameTranslated() const
  89. {
  90. if(campaign && !campaign->getNameTranslated().empty())
  91. return campaign->getNameTranslated();
  92. else if(mapHeader && !mapHeader->name.empty())
  93. {
  94. mapHeader->registerMapStrings();
  95. return mapHeader->name.toString();
  96. }
  97. else
  98. return VLC->generaltexth->allTexts[508];
  99. }
  100. std::string CMapInfo::getNameForList() const
  101. {
  102. if(scenarioOptionsOfSave)
  103. {
  104. // TODO: this could be handled differently
  105. std::vector<std::string> path;
  106. boost::split(path, originalFileURI, boost::is_any_of("\\/"));
  107. return path[path.size()-1];
  108. }
  109. else
  110. {
  111. return getNameTranslated();
  112. }
  113. }
  114. std::string CMapInfo::getDescriptionTranslated() const
  115. {
  116. if(campaign)
  117. return campaign->getDescriptionTranslated();
  118. else
  119. return mapHeader->description.toString();
  120. }
  121. int CMapInfo::getMapSizeIconId() const
  122. {
  123. if(!mapHeader)
  124. return 4;
  125. switch(mapHeader->width)
  126. {
  127. case CMapHeader::MAP_SIZE_SMALL:
  128. return 0;
  129. case CMapHeader::MAP_SIZE_MIDDLE:
  130. return 1;
  131. case CMapHeader::MAP_SIZE_LARGE:
  132. return 2;
  133. case CMapHeader::MAP_SIZE_XLARGE:
  134. return 3;
  135. case CMapHeader::MAP_SIZE_HUGE:
  136. return 4;
  137. case CMapHeader::MAP_SIZE_XHUGE:
  138. return 5;
  139. case CMapHeader::MAP_SIZE_GIANT:
  140. return 6;
  141. default:
  142. return 4;
  143. }
  144. }
  145. int CMapInfo::getMapSizeFormatIconId() const
  146. {
  147. switch(mapHeader->version)
  148. {
  149. case EMapFormat::ROE:
  150. return VLC->engineSettings()->getValue(EGameSettings::MAP_FORMAT_RESTORATION_OF_ERATHIA)["iconIndex"].Integer();
  151. case EMapFormat::AB:
  152. return VLC->engineSettings()->getValue(EGameSettings::MAP_FORMAT_ARMAGEDDONS_BLADE)["iconIndex"].Integer();
  153. case EMapFormat::SOD:
  154. return VLC->engineSettings()->getValue(EGameSettings::MAP_FORMAT_SHADOW_OF_DEATH)["iconIndex"].Integer();
  155. case EMapFormat::CHR:
  156. return VLC->engineSettings()->getValue(EGameSettings::MAP_FORMAT_CHRONICLES)["iconIndex"].Integer();
  157. case EMapFormat::WOG:
  158. return VLC->engineSettings()->getValue(EGameSettings::MAP_FORMAT_IN_THE_WAKE_OF_GODS)["iconIndex"].Integer();
  159. case EMapFormat::HOTA:
  160. return VLC->engineSettings()->getValue(EGameSettings::MAP_FORMAT_HORN_OF_THE_ABYSS)["iconIndex"].Integer();
  161. case EMapFormat::VCMI:
  162. return VLC->engineSettings()->getValue(EGameSettings::MAP_FORMAT_JSON_VCMI)["iconIndex"].Integer();
  163. }
  164. return 0;
  165. }
  166. std::string CMapInfo::getMapSizeName() const
  167. {
  168. switch(mapHeader->width)
  169. {
  170. case CMapHeader::MAP_SIZE_SMALL:
  171. return "S";
  172. case CMapHeader::MAP_SIZE_MIDDLE:
  173. return "M";
  174. case CMapHeader::MAP_SIZE_LARGE:
  175. return "L";
  176. case CMapHeader::MAP_SIZE_XLARGE:
  177. return "XL";
  178. case CMapHeader::MAP_SIZE_HUGE:
  179. return "H";
  180. case CMapHeader::MAP_SIZE_XHUGE:
  181. return "XH";
  182. case CMapHeader::MAP_SIZE_GIANT:
  183. return "G";
  184. default:
  185. return "C";
  186. }
  187. }
  188. VCMI_LIB_NAMESPACE_END