CMapInfo.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. lastWrite = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(resource));
  71. date = TextOperations::getFormattedDateTimeLocal(lastWrite);
  72. }
  73. void CMapInfo::countPlayers()
  74. {
  75. for(int i=0; i<PlayerColor::PLAYER_LIMIT_I; i++)
  76. {
  77. if(mapHeader->players[i].canHumanPlay)
  78. {
  79. amountOfPlayersOnMap++;
  80. amountOfHumanControllablePlayers++;
  81. }
  82. else if(mapHeader->players[i].canComputerPlay)
  83. {
  84. amountOfPlayersOnMap++;
  85. }
  86. }
  87. if(scenarioOptionsOfSave)
  88. for(const auto & playerInfo : scenarioOptionsOfSave->playerInfos)
  89. if(playerInfo.second.isControlledByHuman())
  90. amountOfHumanPlayersInSave++;
  91. }
  92. std::string CMapInfo::getNameTranslated() const
  93. {
  94. if(campaign && !campaign->getNameTranslated().empty())
  95. return campaign->getNameTranslated();
  96. else if(mapHeader && !mapHeader->name.empty())
  97. {
  98. mapHeader->registerMapStrings();
  99. return mapHeader->name.toString();
  100. }
  101. else
  102. return LIBRARY->generaltexth->allTexts[508];
  103. }
  104. std::string CMapInfo::getNameForList() const
  105. {
  106. if(scenarioOptionsOfSave)
  107. {
  108. // TODO: this could be handled differently
  109. std::vector<std::string> path;
  110. boost::split(path, originalFileURI, boost::is_any_of("\\/"));
  111. return path[path.size()-1];
  112. }
  113. else
  114. {
  115. return getNameTranslated();
  116. }
  117. }
  118. std::string CMapInfo::getDescriptionTranslated() const
  119. {
  120. if(campaign)
  121. return campaign->getDescriptionTranslated();
  122. else
  123. return mapHeader->description.toString();
  124. }
  125. int CMapInfo::getMapSizeIconId() const
  126. {
  127. if(!mapHeader)
  128. return 4;
  129. switch(mapHeader->width)
  130. {
  131. case CMapHeader::MAP_SIZE_SMALL:
  132. return 0;
  133. case CMapHeader::MAP_SIZE_MIDDLE:
  134. return 1;
  135. case CMapHeader::MAP_SIZE_LARGE:
  136. return 2;
  137. case CMapHeader::MAP_SIZE_XLARGE:
  138. return 3;
  139. case CMapHeader::MAP_SIZE_HUGE:
  140. return 4;
  141. case CMapHeader::MAP_SIZE_XHUGE:
  142. return 5;
  143. case CMapHeader::MAP_SIZE_GIANT:
  144. return 6;
  145. default:
  146. return 4;
  147. }
  148. }
  149. int CMapInfo::getMapSizeFormatIconId() const
  150. {
  151. switch(mapHeader->version)
  152. {
  153. case EMapFormat::ROE:
  154. return LIBRARY->engineSettings()->getValue(EGameSettings::MAP_FORMAT_RESTORATION_OF_ERATHIA)["iconIndex"].Integer();
  155. case EMapFormat::AB:
  156. return LIBRARY->engineSettings()->getValue(EGameSettings::MAP_FORMAT_ARMAGEDDONS_BLADE)["iconIndex"].Integer();
  157. case EMapFormat::SOD:
  158. return LIBRARY->engineSettings()->getValue(EGameSettings::MAP_FORMAT_SHADOW_OF_DEATH)["iconIndex"].Integer();
  159. case EMapFormat::CHR:
  160. return LIBRARY->engineSettings()->getValue(EGameSettings::MAP_FORMAT_CHRONICLES)["iconIndex"].Integer();
  161. case EMapFormat::WOG:
  162. return LIBRARY->engineSettings()->getValue(EGameSettings::MAP_FORMAT_IN_THE_WAKE_OF_GODS)["iconIndex"].Integer();
  163. case EMapFormat::HOTA:
  164. return LIBRARY->engineSettings()->getValue(EGameSettings::MAP_FORMAT_HORN_OF_THE_ABYSS)["iconIndex"].Integer();
  165. case EMapFormat::VCMI:
  166. return LIBRARY->engineSettings()->getValue(EGameSettings::MAP_FORMAT_JSON_VCMI)["iconIndex"].Integer();
  167. }
  168. return 0;
  169. }
  170. std::string CMapInfo::getMapSizeName() const
  171. {
  172. switch(mapHeader->width)
  173. {
  174. case CMapHeader::MAP_SIZE_SMALL:
  175. return "S";
  176. case CMapHeader::MAP_SIZE_MIDDLE:
  177. return "M";
  178. case CMapHeader::MAP_SIZE_LARGE:
  179. return "L";
  180. case CMapHeader::MAP_SIZE_XLARGE:
  181. return "XL";
  182. case CMapHeader::MAP_SIZE_HUGE:
  183. return "H";
  184. case CMapHeader::MAP_SIZE_XHUGE:
  185. return "XH";
  186. case CMapHeader::MAP_SIZE_GIANT:
  187. return "G";
  188. default:
  189. return "C";
  190. }
  191. }
  192. VCMI_LIB_NAMESPACE_END