CMapInfo.cpp 6.1 KB

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