CMapHeader.cpp 6.2 KB

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