CMapInfo.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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(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.get()) >> 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::unique_ptr<CCampaignHeader>(new 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 (auto i = scenarioOptionsOfSave->playerInfos.cbegin(); i != scenarioOptionsOfSave->playerInfos.cend(); i++)
  73. if(i->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, group = 0;
  133. switch(mapHeader->version)
  134. {
  135. case EMapFormat::ROE:
  136. frame = 0;
  137. break;
  138. case EMapFormat::AB:
  139. frame = 1;
  140. break;
  141. case EMapFormat::SOD:
  142. frame = 2;
  143. break;
  144. case EMapFormat::WOG:
  145. frame = 3;
  146. break;
  147. case EMapFormat::VCMI:
  148. frame = 0;
  149. group = 1;
  150. break;
  151. default:
  152. // Unknown version. Be safe and ignore that map
  153. //logGlobal->warn("Warning: %s has wrong version!", currentItem->fileURI);
  154. break;
  155. }
  156. return std::make_pair(frame, group);
  157. }
  158. std::string CMapInfo::getMapSizeName() const
  159. {
  160. switch(mapHeader->width)
  161. {
  162. case CMapHeader::MAP_SIZE_SMALL:
  163. return "S";
  164. case CMapHeader::MAP_SIZE_MIDDLE:
  165. return "M";
  166. case CMapHeader::MAP_SIZE_LARGE:
  167. return "L";
  168. case CMapHeader::MAP_SIZE_XLARGE:
  169. return "XL";
  170. case CMapHeader::MAP_SIZE_HUGE:
  171. return "H";
  172. case CMapHeader::MAP_SIZE_XHUGE:
  173. return "XH";
  174. case CMapHeader::MAP_SIZE_GIANT:
  175. return "G";
  176. default:
  177. return "C";
  178. }
  179. }
  180. VCMI_LIB_NAMESPACE_END