CMapInfo.cpp 5.9 KB

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