File: CMapDefines.h

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 (280 lines) | stat: -rw-r--r-- 6,504 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * CMapDefines.h, 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
 *
 */

#pragma once

#include "../ResourceSet.h"
#include "../texts/MetaString.h"
#include "../VCMI_Lib.h"
#include "../TerrainHandler.h"

VCMI_LIB_NAMESPACE_BEGIN

class TerrainType;
class RiverType;
class RoadType;
class CGObjectInstance;
class CGTownInstance;
class JsonSerializeFormat;

/// The map event is an event which e.g. gives or takes resources of a specific
/// amount to/from players and can appear regularly or once a time.
class DLL_LINKAGE CMapEvent
{
public:
	CMapEvent();
	virtual ~CMapEvent() = default;

	bool occursToday(int currentDay) const;
	bool affectsPlayer(PlayerColor player, bool isHuman) const;

	std::string name;
	MetaString message;
	TResources resources;
	std::set<PlayerColor> players;
	bool humanAffected;
	bool computerAffected;
	ui32 firstOccurrence;
	ui32 nextOccurrence; /// specifies after how many days the event will occur the next time; 0 if event occurs only one time

	std::vector<ObjectInstanceID> deletedObjectsInstances;

	template <typename Handler>
	void serialize(Handler & h)
	{
		h & name;
		h & message;
		h & resources;
		if (h.version >= Handler::Version::EVENTS_PLAYER_SET)
		{
			h & players;
		}
		else
		{
			ui8 playersMask = 0;
			h & playersMask;
			for (int i = 0; i < 8; ++i)
				if ((playersMask & (1 << i)) != 0)
					players.insert(PlayerColor(i));
		}
		h & humanAffected;
		h & computerAffected;
		h & firstOccurrence;
		h & nextOccurrence;
		if(h.version >= Handler::Version::EVENT_OBJECTS_DELETION)
		{
			h & deletedObjectsInstances;
		}
	}
	
	virtual void serializeJson(JsonSerializeFormat & handler);
};

/// The castle event builds/adds buildings/creatures for a specific town.
class DLL_LINKAGE CCastleEvent: public CMapEvent
{
public:
	CCastleEvent() = default;

	std::set<BuildingID> buildings;
	std::vector<si32> creatures;

	template <typename Handler>
	void serialize(Handler & h)
	{
		h & static_cast<CMapEvent &>(*this);
		h & buildings;
		h & creatures;
	}
	
	void serializeJson(JsonSerializeFormat & handler) override;
};

/// The terrain tile describes the terrain type and the visual representation of the terrain.
/// Furthermore the struct defines whether the tile is visitable or/and blocked and which objects reside in it.
struct DLL_LINKAGE TerrainTile
{
	TerrainTile();

	/// Gets true if the terrain is not a rock. If from is water/land, same type is also required.
	inline bool entrableTerrain() const;
	inline bool entrableTerrain(const TerrainTile * from) const;
	inline bool entrableTerrain(bool allowLand, bool allowSea) const;
	/// Checks for blocking objects and terraint type (water / land).
	bool isClear(const TerrainTile * from = nullptr) const;
	/// Gets the ID of the top visitable object or -1 if there is none.
	Obj topVisitableId(bool excludeTop = false) const;
	CGObjectInstance * topVisitableObj(bool excludeTop = false) const;
	inline bool isWater() const;
	inline bool isLand() const;
	EDiggingStatus getDiggingStatus(bool excludeTop = true) const;
	inline bool hasFavorableWinds() const;

	inline bool visitable() const;
	inline bool blocked() const;

	inline const TerrainType * getTerrain() const;
	inline const RiverType * getRiver() const;
	inline const RoadType * getRoad() const;

	inline TerrainId getTerrainID() const;
	inline RiverId getRiverID() const;
	inline RoadId getRoadID() const;

	inline bool hasRiver() const;
	inline bool hasRoad() const;

	TerrainId terrainType;
	RiverId riverType;
	RoadId roadType;
	ui8 terView;
	ui8 riverDir;
	ui8 roadDir;
	/// first two bits - how to rotate terrain graphic (next two - river graphic, next two - road);
	///	7th bit - whether tile is coastal (allows disembarking if land or block movement if water); 8th bit - Favorable Winds effect
	ui8 extTileFlags;

	std::vector<CGObjectInstance *> visitableObjects;
	std::vector<CGObjectInstance *> blockingObjects;

	template <typename Handler>
	void serialize(Handler & h)
	{
		if (h.version >= Handler::Version::REMOVE_VLC_POINTERS)
		{
			h & terrainType;
		}
		else
		{
			bool isNull = false;
			h & isNull;
			if (!isNull)
				h & terrainType;
		}
		h & terView;
		if (h.version >= Handler::Version::REMOVE_VLC_POINTERS)
		{
			h & riverType;
		}
		else
		{
			bool isNull = false;
			h & isNull;
			if (!isNull)
				h & riverType;
		}
		h & riverDir;
		if (h.version >= Handler::Version::REMOVE_VLC_POINTERS)
		{
			h & roadType;
		}
		else
		{
			bool isNull = false;
			h & isNull;
			if (!isNull)
				h & roadType;
		}
		h & roadDir;
		h & extTileFlags;
		if (h.version < Handler::Version::REMOVE_VLC_POINTERS)
		{
			bool unused = false;
			h & unused;
			h & unused;
		}
		h & visitableObjects;
		h & blockingObjects;
	}
};

inline bool TerrainTile::hasFavorableWinds() const
{
	return extTileFlags & 128;
}

inline bool TerrainTile::isWater() const
{
	return getTerrain()->isWater();
}

inline bool TerrainTile::isLand() const
{
	return getTerrain()->isLand();
}

inline bool TerrainTile::visitable() const
{
	return !visitableObjects.empty();
}

inline bool TerrainTile::blocked() const
{
	return !blockingObjects.empty();
}

inline bool TerrainTile::hasRiver() const
{
	return getRiverID() != RiverId::NO_RIVER;
}

inline bool TerrainTile::hasRoad() const
{
	return getRoadID() != RoadId::NO_ROAD;
}

inline const TerrainType * TerrainTile::getTerrain() const
{
	return terrainType.toEntity(VLC);
}

inline const RiverType * TerrainTile::getRiver() const
{
	return riverType.toEntity(VLC);
}

inline const RoadType * TerrainTile::getRoad() const
{
	return roadType.toEntity(VLC);
}

inline TerrainId TerrainTile::getTerrainID() const
{
	return terrainType;
}

inline RiverId TerrainTile::getRiverID() const
{
	return riverType;
}

inline RoadId TerrainTile::getRoadID() const
{
	return roadType;
}

inline bool TerrainTile::entrableTerrain() const
{
	return entrableTerrain(true, true);
}

inline bool TerrainTile::entrableTerrain(const TerrainTile * from) const
{
	const TerrainType * terrainFrom = from->getTerrain();
	return entrableTerrain(terrainFrom->isLand(), terrainFrom->isWater());
}

inline bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
{
	const TerrainType * terrain = getTerrain();
	return terrain->isPassable() && ((allowSea && terrain->isWater()) || (allowLand && terrain->isLand()));
}

VCMI_LIB_NAMESPACE_END