CMapInfo.cpp 4.7 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 "../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. if(!campaignHeader->valid)
  59. campaignHeader.reset();
  60. }
  61. void CMapInfo::countPlayers()
  62. {
  63. for(int i=0; i<PlayerColor::PLAYER_LIMIT_I; i++)
  64. {
  65. if(mapHeader->players[i].canHumanPlay)
  66. {
  67. amountOfPlayersOnMap++;
  68. amountOfHumanControllablePlayers++;
  69. }
  70. else if(mapHeader->players[i].canComputerPlay)
  71. {
  72. amountOfPlayersOnMap++;
  73. }
  74. }
  75. if(scenarioOptionsOfSave)
  76. for(const auto & playerInfo : scenarioOptionsOfSave->playerInfos)
  77. if(playerInfo.second.isControlledByHuman())
  78. amountOfHumanPlayersInSave++;
  79. }
  80. std::string CMapInfo::getName() const
  81. {
  82. if(campaignHeader && campaignHeader->name.length())
  83. return campaignHeader->name;
  84. else if(mapHeader && mapHeader->name.length())
  85. return mapHeader->name;
  86. else
  87. return VLC->generaltexth->allTexts[508];
  88. }
  89. std::string CMapInfo::getNameForList() const
  90. {
  91. if(scenarioOptionsOfSave)
  92. {
  93. // TODO: this could be handled differently
  94. std::vector<std::string> path;
  95. boost::split(path, fileURI, boost::is_any_of("\\/"));
  96. return path[path.size()-1];
  97. }
  98. else
  99. {
  100. return getName();
  101. }
  102. }
  103. std::string CMapInfo::getDescription() const
  104. {
  105. if(campaignHeader)
  106. return campaignHeader->description;
  107. else
  108. return mapHeader->description;
  109. }
  110. int CMapInfo::getMapSizeIconId() const
  111. {
  112. if(!mapHeader)
  113. return 4;
  114. switch(mapHeader->width)
  115. {
  116. case CMapHeader::MAP_SIZE_SMALL:
  117. return 0;
  118. case CMapHeader::MAP_SIZE_MIDDLE:
  119. return 1;
  120. case CMapHeader::MAP_SIZE_LARGE:
  121. return 2;
  122. case CMapHeader::MAP_SIZE_XLARGE:
  123. return 3;
  124. case CMapHeader::MAP_SIZE_HUGE:
  125. return 4;
  126. case CMapHeader::MAP_SIZE_XHUGE:
  127. return 5;
  128. case CMapHeader::MAP_SIZE_GIANT:
  129. return 6;
  130. default:
  131. return 4;
  132. }
  133. }
  134. std::pair<int, int> CMapInfo::getMapSizeFormatIconId() const
  135. {
  136. int frame = -1;
  137. int group = 0;
  138. switch(mapHeader->version)
  139. {
  140. case EMapFormat::ROE:
  141. frame = 0;
  142. break;
  143. case EMapFormat::AB:
  144. frame = 1;
  145. break;
  146. case EMapFormat::SOD:
  147. frame = 2;
  148. break;
  149. case EMapFormat::WOG:
  150. frame = 3;
  151. break;
  152. case EMapFormat::VCMI:
  153. frame = 0;
  154. group = 1;
  155. break;
  156. default:
  157. // Unknown version. Be safe and ignore that map
  158. //logGlobal->warn("Warning: %s has wrong version!", currentItem->fileURI);
  159. break;
  160. }
  161. return std::make_pair(frame, group);
  162. }
  163. std::string CMapInfo::getMapSizeName() const
  164. {
  165. switch(mapHeader->width)
  166. {
  167. case CMapHeader::MAP_SIZE_SMALL:
  168. return "S";
  169. case CMapHeader::MAP_SIZE_MIDDLE:
  170. return "M";
  171. case CMapHeader::MAP_SIZE_LARGE:
  172. return "L";
  173. case CMapHeader::MAP_SIZE_XLARGE:
  174. return "XL";
  175. case CMapHeader::MAP_SIZE_HUGE:
  176. return "H";
  177. case CMapHeader::MAP_SIZE_XHUGE:
  178. return "XH";
  179. case CMapHeader::MAP_SIZE_GIANT:
  180. return "G";
  181. default:
  182. return "C";
  183. }
  184. }
  185. VCMI_LIB_NAMESPACE_END