CMapInfo.cpp 5.5 KB

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