File: CMapHeader.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (207 lines) | stat: -rw-r--r-- 6,310 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * CMapHeader.cpp, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */
#include "StdInc.h"
#include "CMapHeader.h"

#include "MapFormat.h"

#include "../VCMI_Lib.h"
#include "../entities/faction/CTownHandler.h"
#include "../entities/hero/CHeroHandler.h"
#include "../json/JsonUtils.h"
#include "../modding/CModHandler.h"
#include "../texts/CGeneralTextHandler.h"
#include "../texts/Languages.h"

VCMI_LIB_NAMESPACE_BEGIN

SHeroName::SHeroName() : heroId(-1)
{
}

PlayerInfo::PlayerInfo(): canHumanPlay(false), canComputerPlay(false),
	aiTactic(EAiTactic::RANDOM), isFactionRandom(false), hasRandomHero(false), mainCustomHeroPortrait(-1), mainCustomHeroId(-1), hasMainTown(false),
	generateHeroAtMainTown(false), posOfMainTown(-1), team(TeamID::NO_TEAM)
{
	allowedFactions = VLC->townh->getAllowedFactions();
}

FactionID PlayerInfo::defaultCastle() const
{
	//if random allowed set it as default
	if(isFactionRandom)
		return FactionID::RANDOM;

	if(!allowedFactions.empty())
		return *allowedFactions.begin();

	// fall back to random
	return FactionID::RANDOM;
}

HeroTypeID PlayerInfo::defaultHero() const
{
	// we will generate hero in front of main town
	if((generateHeroAtMainTown && hasMainTown) || hasRandomHero)
	{
		//random hero
		return HeroTypeID::RANDOM;
	}

	return HeroTypeID::NONE;
}

bool PlayerInfo::canAnyonePlay() const
{
	return canHumanPlay || canComputerPlay;
}

bool PlayerInfo::hasCustomMainHero() const
{
	return mainCustomHeroId.isValid();
}

EventCondition::EventCondition(EWinLoseType condition):
	value(-1),
	position(-1, -1, -1),
	condition(condition)
{
}

EventCondition::EventCondition(EWinLoseType condition, si32 value, TargetTypeID objectType, const int3 & position):
	value(value),
	objectType(objectType),
	position(position),
	condition(condition)
{}


void CMapHeader::setupEvents()
{
	EventCondition victoryCondition(EventCondition::STANDARD_WIN);
	EventCondition defeatCondition(EventCondition::DAYS_WITHOUT_TOWN);
	defeatCondition.value = 7;

	//Victory condition - defeat all
	TriggeredEvent standardVictory;
	standardVictory.effect.type = EventEffect::VICTORY;
	standardVictory.effect.toOtherMessage.appendTextID("core.genrltxt.5");
	standardVictory.identifier = "standardVictory";
	standardVictory.description.clear(); // TODO: display in quest window
	standardVictory.onFulfill.appendTextID("core.genrltxt.659");
	standardVictory.trigger = EventExpression(victoryCondition);

	//Loss condition - 7 days without town
	TriggeredEvent standardDefeat;
	standardDefeat.effect.type = EventEffect::DEFEAT;
	standardDefeat.effect.toOtherMessage.appendTextID("core.genrltxt.8");
	standardDefeat.identifier = "standardDefeat";
	standardDefeat.description.clear(); // TODO: display in quest window
	standardDefeat.onFulfill.appendTextID("core.genrltxt.7");
	standardDefeat.trigger = EventExpression(defeatCondition);

	triggeredEvents.push_back(standardVictory);
	triggeredEvents.push_back(standardDefeat);

	victoryIconIndex = 11;
	victoryMessage.appendTextID("core.vcdesc.0");

	defeatIconIndex = 3;
	defeatMessage.appendTextID("core.lcdesc.0");
}

CMapHeader::CMapHeader() : version(EMapFormat::VCMI), height(72), width(72),
	twoLevel(true), difficulty(EMapDifficulty::NORMAL), levelLimit(0), howManyTeams(0), areAnyPlayers(false)
{
	setupEvents();
	allowedHeroes = VLC->heroh->getDefaultAllowed();
	players.resize(PlayerColor::PLAYER_LIMIT_I);
}

CMapHeader::~CMapHeader() = default;

ui8 CMapHeader::levels() const
{
	return (twoLevel ? 2 : 1);
}

void CMapHeader::registerMapStrings()
{
	//get supported languages. Assuming that translation containing most strings is the base language
	std::set<std::string, std::less<>> mapLanguages;
	std::set<std::string, std::less<>> mapBaseLanguages;
	int maxStrings = 0;
	for(auto & translation : translations.Struct())
	{
		if(translation.first.empty() || !translation.second.isStruct() || translation.second.Struct().empty())
			continue;
		
		if(translation.second.Struct().size() > maxStrings)
			maxStrings = translation.second.Struct().size();
		mapLanguages.insert(translation.first);
	}
	
	if(maxStrings == 0 || mapLanguages.empty())
	{
		logGlobal->trace("Map %s doesn't have any supported translation", name.toString());
		return;
	}
	
	//identifying base languages
	for(auto & translation : translations.Struct())
	{
		if(translation.second.isStruct() && translation.second.Struct().size() == maxStrings)
			mapBaseLanguages.insert(translation.first);
	}
	
	std::string baseLanguage;
	std::string language;
	//english is preferable as base language
	if(mapBaseLanguages.count(Languages::getLanguageOptions(Languages::ELanguages::ENGLISH).identifier))
		baseLanguage = Languages::getLanguageOptions(Languages::ELanguages::ENGLISH).identifier;
	else
		baseLanguage = *mapBaseLanguages.begin();

	if(mapBaseLanguages.count(CGeneralTextHandler::getPreferredLanguage()))
	{
		language = CGeneralTextHandler::getPreferredLanguage(); //preferred language is base language - use it
		baseLanguage = language;
	}
	else
	{
		if(mapLanguages.count(CGeneralTextHandler::getPreferredLanguage()))
			language = CGeneralTextHandler::getPreferredLanguage();
		else
			language = baseLanguage; //preferred language is not supported, use base language
	}
	
	assert(!language.empty());
	
	JsonNode data = translations[baseLanguage];
	if(language != baseLanguage)
		JsonUtils::mergeCopy(data, translations[language]);
	
	for(auto & s : data.Struct())
		texts.registerString("map", TextIdentifier(s.first), s.second.String());
}

std::string mapRegisterLocalizedString(const std::string & modContext, CMapHeader & mapHeader, const TextIdentifier & UID, const std::string & localized)
{
	return mapRegisterLocalizedString(modContext, mapHeader, UID, localized, VLC->modh->getModLanguage(modContext));
}

std::string mapRegisterLocalizedString(const std::string & modContext, CMapHeader & mapHeader, const TextIdentifier & UID, const std::string & localized, const std::string & language)
{
	mapHeader.texts.registerString(modContext, UID, localized);
	mapHeader.translations.Struct()[language].Struct()[UID.get()].String() = localized;
	return UID.get();
}

VCMI_LIB_NAMESPACE_END