CMapHeader.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * CMapHeader.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 "CMapHeader.h"
  12. #include "MapFormat.h"
  13. #include "../GameLibrary.h"
  14. #include "../entities/faction/CTownHandler.h"
  15. #include "../entities/hero/CHeroHandler.h"
  16. #include "../json/JsonUtils.h"
  17. #include "../modding/CModHandler.h"
  18. #include "../texts/CGeneralTextHandler.h"
  19. #include "../texts/Languages.h"
  20. VCMI_LIB_NAMESPACE_BEGIN
  21. SHeroName::SHeroName() : heroId(-1)
  22. {
  23. }
  24. PlayerInfo::PlayerInfo(): canHumanPlay(false), canComputerPlay(false),
  25. aiTactic(EAiTactic::RANDOM), isFactionRandom(false), hasRandomHero(false), mainCustomHeroPortrait(-1), mainCustomHeroId(-1), hasMainTown(false),
  26. generateHeroAtMainTown(false), posOfMainTown(-1), team(TeamID::NO_TEAM)
  27. {
  28. allowedFactions = LIBRARY->townh->getAllowedFactions();
  29. }
  30. FactionID PlayerInfo::defaultCastle() const
  31. {
  32. //if random allowed set it as default
  33. if(isFactionRandom)
  34. return FactionID::RANDOM;
  35. assert(!allowedFactions.empty());
  36. if(!allowedFactions.empty())
  37. return *allowedFactions.begin();
  38. // fall back to random
  39. return FactionID::RANDOM;
  40. }
  41. HeroTypeID PlayerInfo::defaultHero() const
  42. {
  43. // we will generate hero in front of main town
  44. if((generateHeroAtMainTown && hasMainTown) || hasRandomHero)
  45. {
  46. //random hero
  47. return HeroTypeID::RANDOM;
  48. }
  49. return HeroTypeID::NONE;
  50. }
  51. bool PlayerInfo::canAnyonePlay() const
  52. {
  53. return canHumanPlay || canComputerPlay;
  54. }
  55. bool PlayerInfo::hasCustomMainHero() const
  56. {
  57. return mainCustomHeroId.isValid();
  58. }
  59. EventCondition::EventCondition(EWinLoseType condition):
  60. value(-1),
  61. position(-1, -1, -1),
  62. condition(condition)
  63. {
  64. }
  65. EventCondition::EventCondition(EWinLoseType condition, si32 value, TargetTypeID objectType, const int3 & position):
  66. value(value),
  67. objectType(objectType),
  68. position(position),
  69. condition(condition)
  70. {}
  71. void CMapHeader::setupEvents()
  72. {
  73. EventCondition victoryCondition(EventCondition::STANDARD_WIN);
  74. EventCondition defeatCondition(EventCondition::DAYS_WITHOUT_TOWN);
  75. defeatCondition.value = 7;
  76. //Victory condition - defeat all
  77. TriggeredEvent standardVictory;
  78. standardVictory.effect.type = EventEffect::VICTORY;
  79. standardVictory.effect.toOtherMessage.appendTextID("core.genrltxt.5");
  80. standardVictory.identifier = "standardVictory";
  81. standardVictory.description.clear(); // TODO: display in quest window
  82. standardVictory.onFulfill.appendTextID("core.genrltxt.659");
  83. standardVictory.trigger = EventExpression(victoryCondition);
  84. //Loss condition - 7 days without town
  85. TriggeredEvent standardDefeat;
  86. standardDefeat.effect.type = EventEffect::DEFEAT;
  87. standardDefeat.effect.toOtherMessage.appendTextID("core.genrltxt.8");
  88. standardDefeat.identifier = "standardDefeat";
  89. standardDefeat.description.clear(); // TODO: display in quest window
  90. standardDefeat.onFulfill.appendTextID("core.genrltxt.7");
  91. standardDefeat.trigger = EventExpression(defeatCondition);
  92. triggeredEvents.push_back(standardVictory);
  93. triggeredEvents.push_back(standardDefeat);
  94. victoryIconIndex = 11;
  95. victoryMessage.appendTextID("core.vcdesc.0");
  96. defeatIconIndex = 3;
  97. defeatMessage.appendTextID("core.lcdesc.0");
  98. }
  99. CMapHeader::CMapHeader()
  100. : version(EMapFormat::VCMI)
  101. , height(72)
  102. , width(72)
  103. , mapLevels(2)
  104. , difficulty(EMapDifficulty::NORMAL)
  105. , levelLimit(0)
  106. , victoryIconIndex(0)
  107. , defeatIconIndex(0)
  108. , howManyTeams(0)
  109. , areAnyPlayers(false)
  110. {
  111. setupEvents();
  112. allowedHeroes = LIBRARY->heroh->getDefaultAllowed();
  113. players.resize(PlayerColor::PLAYER_LIMIT_I);
  114. }
  115. CMapHeader::~CMapHeader() = default;
  116. ui8 CMapHeader::levels() const
  117. {
  118. return mapLevels;
  119. }
  120. void CMapHeader::registerMapStrings()
  121. {
  122. //get supported languages. Assuming that translation containing most strings is the base language
  123. std::set<std::string, std::less<>> mapLanguages;
  124. std::set<std::string, std::less<>> mapBaseLanguages;
  125. int maxStrings = 0;
  126. for(auto & translation : translations.Struct())
  127. {
  128. if(translation.first.empty() || !translation.second.isStruct() || translation.second.Struct().empty())
  129. continue;
  130. if(translation.second.Struct().size() > maxStrings)
  131. maxStrings = translation.second.Struct().size();
  132. mapLanguages.insert(translation.first);
  133. }
  134. if(maxStrings == 0 || mapLanguages.empty())
  135. {
  136. logGlobal->trace("Map %s doesn't have any supported translation", name.toString());
  137. return;
  138. }
  139. //identifying base languages
  140. for(auto & translation : translations.Struct())
  141. {
  142. if(translation.second.isStruct() && translation.second.Struct().size() == maxStrings)
  143. mapBaseLanguages.insert(translation.first);
  144. }
  145. std::string baseLanguage;
  146. std::string language;
  147. //english is preferable as base language
  148. if(mapBaseLanguages.count(Languages::getLanguageOptions(Languages::ELanguages::ENGLISH).identifier))
  149. baseLanguage = Languages::getLanguageOptions(Languages::ELanguages::ENGLISH).identifier;
  150. else
  151. baseLanguage = *mapBaseLanguages.begin();
  152. if(mapBaseLanguages.count(CGeneralTextHandler::getPreferredLanguage()))
  153. {
  154. language = CGeneralTextHandler::getPreferredLanguage(); //preferred language is base language - use it
  155. baseLanguage = language;
  156. }
  157. else
  158. {
  159. if(mapLanguages.count(CGeneralTextHandler::getPreferredLanguage()))
  160. language = CGeneralTextHandler::getPreferredLanguage();
  161. else
  162. language = baseLanguage; //preferred language is not supported, use base language
  163. }
  164. assert(!language.empty());
  165. JsonNode data = translations[baseLanguage];
  166. if(language != baseLanguage)
  167. JsonUtils::mergeCopy(data, translations[language]);
  168. for(auto & s : data.Struct())
  169. texts.registerString("map", TextIdentifier(s.first), s.second.String());
  170. }
  171. std::string mapRegisterLocalizedString(const std::string & modContext, CMapHeader & mapHeader, const TextIdentifier & UID, const std::string & localized)
  172. {
  173. return mapRegisterLocalizedString(modContext, mapHeader, UID, localized, LIBRARY->modh->getModLanguage(modContext));
  174. }
  175. std::string mapRegisterLocalizedString(const std::string & modContext, CMapHeader & mapHeader, const TextIdentifier & UID, const std::string & localized, const std::string & language)
  176. {
  177. mapHeader.texts.registerString(modContext, UID, localized);
  178. mapHeader.translations.Struct()[language].Struct()[UID.get()].String() = localized;
  179. return UID.get();
  180. }
  181. VCMI_LIB_NAMESPACE_END