CMapInfo.cpp 4.3 KB

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