summaryrefslogtreecommitdiffstats
path: root/src/ChunkData.h
blob: e6aaa99b82b3e8e3a0f3f2f878032af152cc0f61 (plain) (blame)
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

#pragma once


#include <cstring>


#include "ChunkDef.h"




#if __cplusplus < 201103L
// auto_ptr style interface for memory management
#else
// unique_ptr style interface for memory management
#endif

class cChunkData
{
public:

	cChunkData();
	~cChunkData();
	
	#if __cplusplus < 201103L
		// auto_ptr style interface for memory management
		cChunkData(const cChunkData & a_Other);
		cChunkData & operator =(const cChunkData & a_Other);
	#else
		// unique_ptr style interface for memory management
		cChunkData(cChunkData && a_Other);
		cChunkData & operator =(cChunkData && a_ther);
	#endif

	BLOCKTYPE GetBlock(int a_X, int a_Y, int a_Z) const;
	void SetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_Block);

	NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const;
	bool SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble);
	
	NIBBLETYPE GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const;
	
	NIBBLETYPE GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const;
	
	cChunkData Copy(void) const;
	
	// Copys data from this object into the buffer in the a_Dest param
	// CopyBlocks also povides the optional parameters a_Idx and a_Length which specify an offset and length for
	// copying part of the BlockTypes array.
	void CopyBlocks    (BLOCKTYPE * a_Dest, size_t a_Idx = 0, size_t a_Length = cChunkDef::NumBlocks) const;
	void CopyMeta      (NIBBLETYPE * a_Dest) const;
	void CopyBlockLight(NIBBLETYPE * a_Dest) const;
	void CopySkyLight  (NIBBLETYPE * a_Dest) const;
	
	void SetBlocks    (const BLOCKTYPE * a_src);
	void SetMeta      (const NIBBLETYPE * a_src);
	void SetBlockLight(const NIBBLETYPE * a_src);
	void SetSkyLight  (const NIBBLETYPE * a_src);
	
private:

	static const size_t CHUNK_SECTION_HEIGHT = 16;
	static const size_t CHUNK_SECTION_COUNT = (256 / CHUNK_SECTION_HEIGHT);

	#if __cplusplus < 201103L
	// auto_ptr style interface for memory management
	mutable bool m_IsOwner;
	#endif
	
	struct sChunkSection {
		BLOCKTYPE  m_BlockTypes   [CHUNK_SECTION_HEIGHT * 16 * 16];
		NIBBLETYPE m_BlockMeta    [CHUNK_SECTION_HEIGHT * 16 * 16 / 2];
		NIBBLETYPE m_BlockLight   [CHUNK_SECTION_HEIGHT * 16 * 16 / 2];
		NIBBLETYPE m_BlockSkyLight[CHUNK_SECTION_HEIGHT * 16 * 16 / 2];
	};
	
	sChunkSection * m_Sections[CHUNK_SECTION_COUNT];
	
	/** Allocates a new section. Entry-point to custom allocators. */
	static sChunkSection * Allocate(void);
	
	/** Frees the specified section, previously allocated using Allocate().
	Note that a_Section may be NULL. */
	static void Free(sChunkSection * a_Section);
	
	/** Sets the data in the specified section to their default values. */
	void ZeroSection(sChunkSection * a_Section) const;
};