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