CMapHeader.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "../CHeroHandler.h"
  17. #include "../Languages.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. SHeroName::SHeroName() : heroId(-1)
  20. {
  21. }
  22. PlayerInfo::PlayerInfo(): canHumanPlay(false), canComputerPlay(false),
  23. aiTactic(EAiTactic::RANDOM), isFactionRandom(false), hasRandomHero(false), mainCustomHeroPortrait(-1), mainCustomHeroId(-1), hasMainTown(false),
  24. generateHeroAtMainTown(false), posOfMainTown(-1), team(TeamID::NO_TEAM)
  25. {
  26. allowedFactions = VLC->townh->getAllowedFactions();
  27. }
  28. si8 PlayerInfo::defaultCastle() const
  29. {
  30. //if random allowed set it as default
  31. if(isFactionRandom)
  32. return -1;
  33. if(!allowedFactions.empty())
  34. return *allowedFactions.begin();
  35. // fall back to random
  36. return -1;
  37. }
  38. si8 PlayerInfo::defaultHero() const
  39. {
  40. // we will generate hero in front of main town
  41. if((generateHeroAtMainTown && hasMainTown) || hasRandomHero)
  42. {
  43. //random hero
  44. return -1;
  45. }
  46. return -2;
  47. }
  48. bool PlayerInfo::canAnyonePlay() const
  49. {
  50. return canHumanPlay || canComputerPlay;
  51. }
  52. bool PlayerInfo::hasCustomMainHero() const
  53. {
  54. return !mainCustomHeroNameTextId.empty() && mainCustomHeroPortrait != -1;
  55. }
  56. EventCondition::EventCondition(EWinLoseType condition):
  57. object(nullptr),
  58. metaType(EMetaclass::INVALID),
  59. value(-1),
  60. objectType(-1),
  61. objectSubtype(-1),
  62. position(-1, -1, -1),
  63. condition(condition)
  64. {
  65. }
  66. EventCondition::EventCondition(EWinLoseType condition, si32 value, si32 objectType, const int3 & position):
  67. object(nullptr),
  68. metaType(EMetaclass::INVALID),
  69. value(value),
  70. objectType(objectType),
  71. objectSubtype(-1),
  72. position(position),
  73. condition(condition)
  74. {}
  75. void CMapHeader::setupEvents()
  76. {
  77. EventCondition victoryCondition(EventCondition::STANDARD_WIN);
  78. EventCondition defeatCondition(EventCondition::DAYS_WITHOUT_TOWN);
  79. defeatCondition.value = 7;
  80. //Victory condition - defeat all
  81. TriggeredEvent standardVictory;
  82. standardVictory.effect.type = EventEffect::VICTORY;
  83. standardVictory.effect.toOtherMessage.appendTextID("core.genrltxt.5");
  84. standardVictory.identifier = "standardVictory";
  85. standardVictory.description.clear(); // TODO: display in quest window
  86. standardVictory.onFulfill.appendTextID("core.genrltxt.659");
  87. standardVictory.trigger = EventExpression(victoryCondition);
  88. //Loss condition - 7 days without town
  89. TriggeredEvent standardDefeat;
  90. standardDefeat.effect.type = EventEffect::DEFEAT;
  91. standardDefeat.effect.toOtherMessage.appendTextID("core.genrltxt.8");
  92. standardDefeat.identifier = "standardDefeat";
  93. standardDefeat.description.clear(); // TODO: display in quest window
  94. standardDefeat.onFulfill.appendTextID("core.genrltxt.7");
  95. standardDefeat.trigger = EventExpression(defeatCondition);
  96. triggeredEvents.push_back(standardVictory);
  97. triggeredEvents.push_back(standardDefeat);
  98. victoryIconIndex = 11;
  99. victoryMessage.appendTextID("core.vcdesc.0");
  100. defeatIconIndex = 3;
  101. defeatMessage.appendTextID("core.lcdesc.0");
  102. }
  103. CMapHeader::CMapHeader() : version(EMapFormat::VCMI), height(72), width(72),
  104. twoLevel(true), difficulty(1), levelLimit(0), howManyTeams(0), areAnyPlayers(false)
  105. {
  106. setupEvents();
  107. allowedHeroes = VLC->heroh->getDefaultAllowed();
  108. players.resize(PlayerColor::PLAYER_LIMIT_I);
  109. VLC->generaltexth->addSubContainer(*this);
  110. }
  111. CMapHeader::~CMapHeader()
  112. {
  113. VLC->generaltexth->removeSubContainer(*this);
  114. }
  115. ui8 CMapHeader::levels() const
  116. {
  117. return (twoLevel ? 2 : 1);
  118. }
  119. void CMapHeader::registerMapStrings()
  120. {
  121. auto language = CGeneralTextHandler::getPreferredLanguage();
  122. JsonNode data;
  123. if(translations[language].isNull())
  124. {
  125. //english is preferrable
  126. language = Languages::getLanguageOptions(Languages::ELanguages::ENGLISH).identifier;
  127. std::list<Languages::Options> languages{Languages::getLanguageList().begin(), Languages::getLanguageList().end()};
  128. while(translations[language].isNull() && !languages.empty())
  129. {
  130. language = languages.front().identifier;
  131. languages.pop_front();
  132. }
  133. if(!translations[language].isNull())
  134. {
  135. logGlobal->info("Map %s doesn't have any translation", name.toString());
  136. return;
  137. }
  138. }
  139. for(auto & s : translations[language].Struct())
  140. registerString("map", TextIdentifier(s.first), s.second.String(), language);
  141. }
  142. VCMI_LIB_NAMESPACE_END