CMapInfo.cpp 5.8 KB

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