CMapHeader.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 "../CTownHandler.h"
  15. #include "../CGeneralTextHandler.h"
  16. #include "../modding/CModHandler.h"
  17. #include "../CHeroHandler.h"
  18. #include "../Languages.h"
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. SHeroName::SHeroName() : heroId(-1)
  21. {
  22. }
  23. PlayerInfo::PlayerInfo(): canHumanPlay(false), canComputerPlay(false),
  24. aiTactic(EAiTactic::RANDOM), isFactionRandom(false), hasRandomHero(false), mainCustomHeroPortrait(-1), mainCustomHeroId(-1), hasMainTown(false),
  25. generateHeroAtMainTown(false), posOfMainTown(-1), team(TeamID::NO_TEAM)
  26. {
  27. allowedFactions = VLC->townh->getAllowedFactions();
  28. }
  29. FactionID PlayerInfo::defaultCastle() const
  30. {
  31. //if random allowed set it as default
  32. if(isFactionRandom)
  33. return FactionID::RANDOM;
  34. if(!allowedFactions.empty())
  35. return *allowedFactions.begin();
  36. // fall back to random
  37. return FactionID::RANDOM;
  38. }
  39. HeroTypeID PlayerInfo::defaultHero() const
  40. {
  41. // we will generate hero in front of main town
  42. if((generateHeroAtMainTown && hasMainTown) || hasRandomHero)
  43. {
  44. //random hero
  45. return HeroTypeID::RANDOM;
  46. }
  47. return HeroTypeID::NONE;
  48. }
  49. bool PlayerInfo::canAnyonePlay() const
  50. {
  51. return canHumanPlay || canComputerPlay;
  52. }
  53. bool PlayerInfo::hasCustomMainHero() const
  54. {
  55. return mainCustomHeroId.isValid();
  56. }
  57. EventCondition::EventCondition(EWinLoseType condition):
  58. object(nullptr),
  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. object(nullptr),
  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() : version(EMapFormat::VCMI), height(72), width(72),
  100. twoLevel(true), difficulty(1), levelLimit(0), howManyTeams(0), areAnyPlayers(false)
  101. {
  102. setupEvents();
  103. allowedHeroes = VLC->heroh->getDefaultAllowed();
  104. players.resize(PlayerColor::PLAYER_LIMIT_I);
  105. VLC->generaltexth->addSubContainer(*this);
  106. }
  107. CMapHeader::~CMapHeader()
  108. {
  109. VLC->generaltexth->removeSubContainer(*this);
  110. }
  111. ui8 CMapHeader::levels() const
  112. {
  113. return (twoLevel ? 2 : 1);
  114. }
  115. void CMapHeader::registerMapStrings()
  116. {
  117. VLC->generaltexth->removeSubContainer(*this);
  118. VLC->generaltexth->addSubContainer(*this);
  119. //get supported languages. Assuming that translation containing most strings is the base language
  120. std::set<std::string> mapLanguages, mapBaseLanguages;
  121. int maxStrings = 0;
  122. for(auto & translation : translations.Struct())
  123. {
  124. if(translation.first.empty() || !translation.second.isStruct() || translation.second.Struct().empty())
  125. continue;
  126. if(translation.second.Struct().size() > maxStrings)
  127. maxStrings = translation.second.Struct().size();
  128. mapLanguages.insert(translation.first);
  129. }
  130. if(maxStrings == 0 || mapLanguages.empty())
  131. {
  132. logGlobal->info("Map %s doesn't have any supported translation", name.toString());
  133. return;
  134. }
  135. //identifying base languages
  136. for(auto & translation : translations.Struct())
  137. {
  138. if(translation.second.isStruct() && translation.second.Struct().size() == maxStrings)
  139. mapBaseLanguages.insert(translation.first);
  140. }
  141. std::string baseLanguage, language;
  142. //english is preferrable as base language
  143. if(mapBaseLanguages.count(Languages::getLanguageOptions(Languages::ELanguages::ENGLISH).identifier))
  144. baseLanguage = Languages::getLanguageOptions(Languages::ELanguages::ENGLISH).identifier;
  145. else
  146. baseLanguage = *mapBaseLanguages.begin();
  147. if(mapBaseLanguages.count(CGeneralTextHandler::getPreferredLanguage()))
  148. {
  149. language = CGeneralTextHandler::getPreferredLanguage(); //preferred language is base language - use it
  150. baseLanguage = language;
  151. }
  152. else
  153. {
  154. if(mapLanguages.count(CGeneralTextHandler::getPreferredLanguage()))
  155. language = CGeneralTextHandler::getPreferredLanguage();
  156. else
  157. language = baseLanguage; //preferred language is not supported, use base language
  158. }
  159. assert(!language.empty());
  160. JsonNode data = translations[baseLanguage];
  161. if(language != baseLanguage)
  162. JsonUtils::mergeCopy(data, translations[language]);
  163. for(auto & s : data.Struct())
  164. registerString("map", TextIdentifier(s.first), s.second.String(), language);
  165. }
  166. std::string mapRegisterLocalizedString(const std::string & modContext, CMapHeader & mapHeader, const TextIdentifier & UID, const std::string & localized)
  167. {
  168. return mapRegisterLocalizedString(modContext, mapHeader, UID, localized, VLC->modh->getModLanguage(modContext));
  169. }
  170. std::string mapRegisterLocalizedString(const std::string & modContext, CMapHeader & mapHeader, const TextIdentifier & UID, const std::string & localized, const std::string & language)
  171. {
  172. mapHeader.registerString(modContext, UID, localized, language);
  173. mapHeader.translations.Struct()[language].Struct()[UID.get()].String() = localized;
  174. return UID.get();
  175. }
  176. VCMI_LIB_NAMESPACE_END