File: ObjectTemplate.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 (158 lines) | stat: -rw-r--r-- 3,597 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
/*
 * ObjectTemplate.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 "../GameConstants.h"
#include "../int3.h"

VCMI_LIB_NAMESPACE_BEGIN

class CBinaryReader;
class CLegacyConfigParser;
class JsonNode;
class int3;

class DLL_LINKAGE ObjectTemplate
{
	enum EBlockMapBits
	{
		VISIBLE = 1,
		VISITABLE = 2,
		BLOCKED = 4
	};

	/// tiles that are covered by this object, uses EBlockMapBits enum as flags
	std::vector<std::vector<ui8>> usedTiles;
	/// directions from which object can be entered, format same as for moveDir in CGHeroInstance(but 0 - 7)
	ui8 visitDir;
	/// list of terrains on which this object can be placed
	std::set<TerrainId> allowedTerrains;

	void afterLoadFixup();

public:
	/// H3 ID/subID of this object
	Obj id;
	si32 subid;
	/// print priority, objects with higher priority will be print first, below everything else
	si32 printPriority;
	/// animation file that should be used to display object
	std::string animationFile;

	/// map editor only animation file
	std::string editorAnimationFile;

	/// string ID, equals to def base name for h3m files (lower case, no extension) or specified in mod data
	std::string stringID;

	inline ui32 getWidth() const
	{
		return width;
	};

	inline ui32 getHeight() const
	{ 
		return height;
	};

	void setSize(ui32 width, ui32 height);

	inline bool isVisitable() const
	{
		return visitable;
	};

	// Checks object used tiles
	// Position is relative to bottom-right corner of the object, can not be negative
	bool isWithin(si32 X, si32 Y) const;
	bool isVisitableAt(si32 X, si32 Y) const;
	bool isVisibleAt(si32 X, si32 Y) const;
	bool isBlockedAt(si32 X, si32 Y) const;

	inline std::set<int3> getBlockedOffsets() const
	{
		return blockedOffsets;
	};

	inline int3 getBlockMapOffset() const
	{
		return blockMapOffset; 
	}; 

	// Checks if object is visitable from certain direction. X and Y must be between -1..+1
	bool isVisitableFrom(si8 X, si8 Y) const;
	inline int3 getVisitableOffset() const
	{
		//logGlobal->warn("Warning: getVisitableOffset called on non-visitable obj!");
		return visitableOffset;
	};

	inline bool isVisitableFromTop() const
	{
		return visitDir & 2;
	};

	// Checks if object can be placed on specific terrain
	bool canBePlacedAt(TerrainId terrain) const;

	ObjectTemplate();
	//custom copy constructor is required
	ObjectTemplate(const ObjectTemplate & other);

	ObjectTemplate& operator=(const ObjectTemplate & rhs);

	void readTxt(CLegacyConfigParser & parser);
	void readMsk();
	void readMap(CBinaryReader & reader);
	void readJson(const JsonNode & node, const bool withTerrain = true);
	void writeJson(JsonNode & node, const bool withTerrain = true) const;

	bool operator==(const ObjectTemplate& ot) const { return (id == ot.id && subid == ot.subid); }

private:
	ui32 width;
	ui32 height;
	bool visitable;

	std::set<int3> blockedOffsets;
	int3 blockMapOffset;
	int3 visitableOffset;

	void recalculate();

	void calculateWidth();
	void calculateHeight();
	void calculateVsitable();
	void calculateBlockedOffsets();
	void calculateBlockMapOffset();
	void calculateVisitableOffset();

public:
	template <typename Handler> void serialize(Handler &h, const int version)
	{
		h & usedTiles;
		h & allowedTerrains;
		h & animationFile;
		h & stringID;
		h & id;
		h & subid;
		h & printPriority;
		h & visitDir;
		h & editorAnimationFile;

		if (!h.saving)
		{
			recalculate();
		}
	}
};


VCMI_LIB_NAMESPACE_END