CMapInfo.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 "CMap.h"
  17. #include "CCampaignHandler.h"
  18. #include "../filesystem/Filesystem.h"
  19. #include "../serializer/CMemorySerializer.h"
  20. #include "../CGeneralTextHandler.h"
  21. #include "../rmg/CMapGenOptions.h"
  22. #include "../CCreatureHandler.h"
  23. #include "../CHeroHandler.h"
  24. #include "../CModHandler.h"
  25. VCMI_LIB_NAMESPACE_BEGIN
  26. CMapInfo::CMapInfo()
  27. : scenarioOptionsOfSave(nullptr), amountOfPlayersOnMap(0), amountOfHumanControllablePlayers(0), amountOfHumanPlayersInSave(0), isRandomMap(false)
  28. {
  29. }
  30. CMapInfo::~CMapInfo()
  31. {
  32. vstd::clear_pointer(scenarioOptionsOfSave);
  33. }
  34. void CMapInfo::mapInit(const std::string & fname)
  35. {
  36. fileURI = fname;
  37. CMapService mapService;
  38. mapHeader = mapService.loadMapHeader(ResourceID(fname, EResType::MAP));
  39. countPlayers();
  40. }
  41. void CMapInfo::saveInit(const ResourceID & file)
  42. {
  43. CLoadFile lf(*CResourceHandler::get()->getResourceName(file), MINIMAL_SERIALIZATION_VERSION);
  44. lf.checkMagicBytes(SAVEGAME_MAGIC);
  45. mapHeader = std::make_unique<CMapHeader>();
  46. lf >> *(mapHeader) >> scenarioOptionsOfSave;
  47. fileURI = file.getName();
  48. countPlayers();
  49. std::time_t time = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(file));
  50. date = std::asctime(std::localtime(&time));
  51. // We absolutely not need this data for lobby and server will read it from save
  52. // FIXME: actually we don't want them in CMapHeader!
  53. mapHeader->triggeredEvents.clear();
  54. }
  55. void CMapInfo::campaignInit()
  56. {
  57. campaignHeader = std::make_unique<CCampaignHeader>(CCampaignHandler::getHeader(fileURI));
  58. }
  59. void CMapInfo::countPlayers()
  60. {
  61. for(int i=0; i<PlayerColor::PLAYER_LIMIT_I; i++)
  62. {
  63. if(mapHeader->players[i].canHumanPlay)
  64. {
  65. amountOfPlayersOnMap++;
  66. amountOfHumanControllablePlayers++;
  67. }
  68. else if(mapHeader->players[i].canComputerPlay)
  69. {
  70. amountOfPlayersOnMap++;
  71. }
  72. }
  73. if(scenarioOptionsOfSave)
  74. for(const auto & playerInfo : scenarioOptionsOfSave->playerInfos)
  75. if(playerInfo.second.isControlledByHuman())
  76. amountOfHumanPlayersInSave++;
  77. }
  78. std::string CMapInfo::getName() const
  79. {
  80. if(campaignHeader && campaignHeader->name.length())
  81. return campaignHeader->name;
  82. else if(mapHeader && mapHeader->name.length())
  83. return mapHeader->name;
  84. else
  85. return VLC->generaltexth->allTexts[508];
  86. }
  87. std::string CMapInfo::getNameForList() const
  88. {
  89. if(scenarioOptionsOfSave)
  90. {
  91. // TODO: this could be handled differently
  92. std::vector<std::string> path;
  93. boost::split(path, fileURI, boost::is_any_of("\\/"));
  94. return path[path.size()-1];
  95. }
  96. else
  97. {
  98. return getName();
  99. }
  100. }
  101. std::string CMapInfo::getDescription() const
  102. {
  103. if(campaignHeader)
  104. return campaignHeader->description;
  105. else
  106. return mapHeader->description;
  107. }
  108. int CMapInfo::getMapSizeIconId() const
  109. {
  110. if(!mapHeader)
  111. return 4;
  112. switch(mapHeader->width)
  113. {
  114. case CMapHeader::MAP_SIZE_SMALL:
  115. return 0;
  116. case CMapHeader::MAP_SIZE_MIDDLE:
  117. return 1;
  118. case CMapHeader::MAP_SIZE_LARGE:
  119. return 2;
  120. case CMapHeader::MAP_SIZE_XLARGE:
  121. return 3;
  122. case CMapHeader::MAP_SIZE_HUGE:
  123. return 4;
  124. case CMapHeader::MAP_SIZE_XHUGE:
  125. return 5;
  126. case CMapHeader::MAP_SIZE_GIANT:
  127. return 6;
  128. default:
  129. return 4;
  130. }
  131. }
  132. std::pair<int, int> CMapInfo::getMapSizeFormatIconId() const
  133. {
  134. int frame = -1;
  135. int group = 0;
  136. switch(mapHeader->version)
  137. {
  138. case EMapFormat::ROE:
  139. frame = 0;
  140. break;
  141. case EMapFormat::AB:
  142. frame = 1;
  143. break;
  144. case EMapFormat::SOD:
  145. frame = 2;
  146. break;
  147. case EMapFormat::WOG:
  148. frame = 3;
  149. break;
  150. case EMapFormat::VCMI:
  151. frame = 0;
  152. group = 1;
  153. break;
  154. default:
  155. // Unknown version. Be safe and ignore that map
  156. //logGlobal->warn("Warning: %s has wrong version!", currentItem->fileURI);
  157. break;
  158. }
  159. return std::make_pair(frame, group);
  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