File: Terrain.h

package info (click to toggle)
vcmi 1.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 14,672 kB
  • sloc: cpp: 181,738; sh: 220; python: 178; ansic: 69; objc: 66; xml: 59; makefile: 34
file content (193 lines) | stat: -rw-r--r-- 4,688 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
/*
 * Terrain.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 "ConstTransitivePtr.h"
#include "GameConstants.h"
#include "JsonNode.h"

VCMI_LIB_NAMESPACE_BEGIN

class DLL_LINKAGE TerrainType
{
public:
	
	enum PassabilityType : ui8
	{
		LAND = 1,
		WATER = 2,
		SURFACE = 4,
		SUBTERRANEAN = 8,
		ROCK = 16
	};
	
	std::vector<std::string> battleFields;
	std::vector<TerrainId> prohibitTransitions;
	std::array<int, 3> minimapBlocked;
	std::array<int, 3> minimapUnblocked;
	std::string name;
	std::string musicFilename;
	std::string tilesFilename;
	std::string terrainText;
	std::string typeCode;
	std::string terrainViewPatterns;
	RiverId river;

	TerrainId id;
	TerrainId rockTerrain;
	int moveCost;
	int horseSoundId;
	ui8 passabilityType;
	bool transitionRequired;
	
	TerrainType(const std::string & name = "");
	
	bool operator==(const TerrainType & other);
	bool operator!=(const TerrainType & other);
	bool operator<(const TerrainType & other);
	
	bool isLand() const;
	bool isWater() const;
	bool isPassable() const;
	bool isSurface() const;
	bool isUnderground() const;
	bool isTransitionRequired() const;
		
	operator std::string() const;
	
	template <typename Handler> void serialize(Handler &h, const int version)
	{
		h & battleFields;
		h & prohibitTransitions;
		h & minimapBlocked;
		h & minimapUnblocked;
		h & name;
		h & musicFilename;
		h & tilesFilename;
		h & terrainText;
		h & typeCode;
		h & terrainViewPatterns;
		h & rockTerrain;
		h & river;

		h & id;
		h & moveCost;
		h & horseSoundId;
		h & passabilityType;
		h & transitionRequired;
	}
};

class DLL_LINKAGE RiverType
{
public:

	std::string fileName;
	std::string code;
	std::string deltaName;
	RiverId id;

	RiverType(const std::string & fileName = "", const std::string & code = "", RiverId id = River::NO_RIVER);

	template <typename Handler> void serialize(Handler& h, const int version)
	{
		h & fileName;
		h & code;
		h & deltaName;
		h & id;
	}
};

class DLL_LINKAGE RoadType
{
public:
	std::string fileName;
	std::string code;
	RoadId id;
	ui8 movementCost;

	RoadType(const std::string & fileName = "", const std::string& code = "", RoadId id = Road::NO_ROAD);

	template <typename Handler> void serialize(Handler& h, const int version)
	{
		h & fileName;
		h & code;
		h & id;
		h & movementCost;
	}
};

DLL_LINKAGE std::ostream & operator<<(std::ostream & os, const TerrainType & terrainType);

class DLL_LINKAGE TerrainTypeHandler //TODO: public IHandlerBase ?
{
public:

	TerrainTypeHandler();
	~TerrainTypeHandler() {};

	const std::vector<TerrainType> & terrains() const;
	const TerrainType * getInfoByName(const std::string & terrainName) const;
	const TerrainType * getInfoByCode(const std::string & terrainCode) const;
	const TerrainType * getInfoById(TerrainId id) const;

	const std::vector<RiverType> & rivers() const;
	const RiverType * getRiverByName(const std::string & riverName) const;
	const RiverType * getRiverByCode(const std::string & riverCode) const;
	const RiverType * getRiverById(RiverId id) const;

	const std::vector<RoadType> & roads() const;
	const RoadType * getRoadByName(const std::string & roadName) const;
	const RoadType * getRoadByCode(const std::string & roadCode) const;
	const RoadType * getRoadById(RoadId id) const;

	template <typename Handler> void serialize(Handler &h, const int version)
	{
		h & objects;
		h & riverTypes;
		h & roadTypes;

		if (!h.saving)
		{
			recreateTerrainMaps();
			recreateRiverMaps();
			recreateRoadMaps();
		}
	}

private:

	std::vector<TerrainType> objects;
	std::vector<RiverType> riverTypes;
	std::vector<RoadType> roadTypes;

	std::unordered_map<std::string, const TerrainType*> terrainInfoByName;
	std::unordered_map<std::string, const TerrainType*> terrainInfoByCode;
	std::unordered_map<TerrainId, const TerrainType*> terrainInfoById;

	std::unordered_map<std::string, const RiverType*> riverInfoByName;
	std::unordered_map<std::string, const RiverType*> riverInfoByCode;
	std::unordered_map<RiverId, const RiverType*> riverInfoById;

	std::unordered_map<std::string, const RoadType*> roadInfoByName;
	std::unordered_map<std::string, const RoadType*> roadInfoByCode;
	std::unordered_map<RoadId, const RoadType*> roadInfoById;

	void initTerrains(const std::vector<std::string> & allConfigs);
	void initRivers(const std::vector<std::string> & allConfigs);
	void initRoads(const std::vector<std::string> & allConfigs);
	void recreateTerrainMaps();
	void recreateRiverMaps();
	void recreateRoadMaps();

};

VCMI_LIB_NAMESPACE_END