CMapInfo.cpp 6.0 KB

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