CMapInfo.cpp 5.9 KB

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