summaryrefslogtreecommitdiff
path: root/src/mapnode.h
blob: 02abe4e520dea1be3400722a6db22c0760768b50 (plain)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
Minetest-c55
Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef MAPNODE_HEADER
#define MAPNODE_HEADER

#include <iostream>
#include "common_irrlicht.h"
#include "light.h"
#include "utility.h"
#include "exceptions.h"
#include "serialization.h"

// Size of node in rendering units
#define BS 10

#define MATERIALS_COUNT 256

/*
	Ignored node.

	Anything that stores MapNodes doesn't have to preserve parameters
	associated with this material.
	
	Doesn't create faces with anything and is considered being
	out-of-map in the game map.
*/
#define MATERIAL_IGNORE 255
#define MATERIAL_IGNORE_DEFAULT_PARAM 0

/*
	The common material through which the player can walk and which
	is transparent to light
*/
#define MATERIAL_AIR 254

/*
	Materials-todo:

	GRAVEL
	  - Dynamics of gravel: if there is a drop of more than two
	    blocks on any side, it will drop in there. Is this doable?
*/

enum Material
{
	MATERIAL_STONE=0,

	MATERIAL_GRASS,

	MATERIAL_WATER,

	MATERIAL_LIGHT,

	MATERIAL_TREE,
	
	MATERIAL_LEAVES,

	MATERIAL_GRASS_FOOTSTEPS,
	
	MATERIAL_MESE,

	MATERIAL_MUD,
	
	// This is set to the number of the actual values in this enum
	USEFUL_MATERIAL_COUNT
};

/*
	If true, the material allows light propagation and brightness is stored
	in param.
*/
inline bool light_propagates_material(u8 m)
{
	return (m == MATERIAL_AIR || m == MATERIAL_LIGHT || m == MATERIAL_WATER);
}

/*
	If true, the material allows lossless sunlight propagation.
*/
inline bool sunlight_propagates_material(u8 m)
{
	return (m == MATERIAL_AIR);
}

/*
	On a node-node surface, the material of the node with higher solidness
	is used for drawing.
	0: Invisible
	1: Transparent
	2: Opaque
*/
inline u8 material_solidness(u8 m)
{
	if(m == MATERIAL_AIR)
		return 0;
	if(m == MATERIAL_WATER)
		return 1;
	return 2;
}

// Objects collide with walkable materials
inline bool material_walkable(u8 m)
{
	return (m != MATERIAL_AIR && m != MATERIAL_WATER);
}

// A liquid resists fast movement
inline bool material_liquid(u8 m)
{
	return (m == MATERIAL_WATER);
}

// Pointable materials can be pointed to in the map
inline bool material_pointable(u8 m)
{
	return (m != MATERIAL_AIR && m != MATERIAL_WATER);
}

inline bool material_diggable(u8 m)
{
	return (m != MATERIAL_AIR && m != MATERIAL_WATER);
}

inline bool material_buildable_to(u8 m)
{
	return (m == MATERIAL_AIR || m == MATERIAL_WATER);
}

/*
	Nodes make a face if materials differ and solidness differs.
	Return value:
		0: No face
		1: Face uses m1's material
		2: Face uses m2's material
*/
inline u8 face_materials(u8 m1, u8 m2)
{
	if(m1 == MATERIAL_IGNORE || m2 == MATERIAL_IGNORE)
		return 0;
	
	bool materials_differ = (m1 != m2);
	bool solidness_differs = (material_solidness(m1) != material_solidness(m2));
	bool makes_face = materials_differ && solidness_differs;

	if(makes_face == false)
		return 0;

	if(material_solidness(m1) > material_solidness(m2))
		return 1;
	else
		return 2;
}

/*
	Returns true for materials that form the base ground that
	follows the main heightmap
*/
inline bool is_ground_material(u8 m)
{
	return(
		m == MATERIAL_STONE ||
		m == MATERIAL_GRASS ||
		m == MATERIAL_GRASS_FOOTSTEPS ||
		m == MATERIAL_MESE ||
		m == MATERIAL_MUD
	);
}

struct MapNode
{
	//TODO: block type to differ from material
	//      (e.g. grass edges or something)
	// block type
	u8 d;

	/*
		Misc parameter. Initialized to 0.
		- For light_propagates() blocks, this is light intensity,
		  stored logarithmically from 0 to LIGHT_MAX.
		  Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
	*/
	s8 param;

	u8 pressure;

	MapNode(const MapNode & n)
	{
		*this = n;
	}
	
	MapNode(u8 data=MATERIAL_AIR, u8 a_param=0)
	{
		d = data;
		param = a_param;
	}

	bool operator==(const MapNode &other)
	{
		return (d == other.d && param == other.param);
	}

	bool light_propagates()
	{
		return light_propagates_material(d);
	}
	
	bool sunlight_propagates()
	{
		return sunlight_propagates_material(d);
	}
	
	u8 solidness()
	{
		return material_solidness(d);
	}

	u8 light_source()
	{
		/*
			Note that a block that isn't light_propagates() can be a light source.
		*/
		if(d == MATERIAL_LIGHT)
			return LIGHT_MAX;
		
		return 0;
	}

	u8 getLight()
	{
		// Select the brightest of [light_source, transparent_light]
		u8 light = 0;
		if(light_propagates())
			light = param & 0x0f;
		if(light_source() > light)
			light = light_source();
		return light;
	}

	void setLight(u8 a_light)
	{
		// If not transparent, can't set light
		if(light_propagates() == false)
			return;
		param = a_light;
	}

	static u32 serializedLength(u8 version)
	{
		if(!ser_ver_supported(version))
			throw VersionMismatchException("ERROR: MapNode format not supported");
			
		if(version == 0)
			return 1;
		else
			return 2;
	}
	void serialize(u8 *dest, u8 version)
	{
		if(!ser_ver_supported(version))
			throw VersionMismatchException("ERROR: MapNode format not supported");
			
		if(version == 0)
		{
			dest[0] = d;
		}
		else
		{
			dest[0] = d;
			dest[1] = param;
		}
	}
	void deSerialize(u8 *source, u8 version)
	{
		if(!ser_ver_supported(version))
			throw VersionMismatchException("ERROR: MapNode format not supported");
			
		if(version == 0)
		{
			d = source[0];
		}
		else if(version == 1)
		{
			d = source[0];
			// This version doesn't support saved lighting
			if(light_propagates() || light_source() > 0)
				param = 0;
			else
				param = source[1];
		}
		else
		{
			d = source[0];
			param = source[1];
		}
	}
};

/*
	Returns integer position of the node in given
	floating point position.
*/
inline v3s16 floatToInt(v3f p)
{
	v3s16 p2(
		(p.X + (p.X>0 ? BS/2 : -BS/2))/BS,
		(p.Y + (p.Y>0 ? BS/2 : -BS/2))/BS,
		(p.Z + (p.Z>0 ? BS/2 : -BS/2))/BS);
	return p2;
}

inline v3f intToFloat(v3s16 p)
{
	v3f p2(
		p.X * BS,
		p.Y * BS,
		p.Z * BS
	);
	return p2;
}



#endif