2
0

CMapInfo.cpp 6.0 KB

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