CMapInfo.cpp 5.6 KB

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