2
0

CMapInfo.cpp 4.6 KB

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