CMapInfo.cpp 5.5 KB

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