summaryrefslogtreecommitdiffstats
path: root/source/cChunkGenerator.cpp
blob: 78c90e2445e94b214562a671326c7a3431d5e394 (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
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
#include "MemoryLeak.h"
#include "cChunkGenerator.h"
#include "cChunkMap.h"
#include "cChunk.h"
#include "cWorld.h"

#include "cThread.h"
#include "cCriticalSection.h"
#include "cSemaphore.h"

#include "cMCLogger.h"

#include <list>

typedef std::pair<int, int> ChunkCoord;
typedef std::list< ChunkCoord > ChunkCoordList;





/// If the generation queue size exceeds this number, a warning will be output
#define QUEUE_WARNING_LIMIT 1000





struct cChunkGenerator::sChunkGeneratorState
{
	cCriticalSection m_CriticalSection;  // For protecting the variables in this struct

	ChunkCoordList GenerateQueue;
	ChunkCoord CurrentlyGeneratingCoords;
	cChunk* pCurrentlyGenerating;
	bool bCurrentlyGenerating;

	cSemaphore m_Semaphore;
	cThread * pThread;

	bool bStop;

	sChunkGeneratorState(void)
		: m_Semaphore(1, 0)
		, pThread( 0 )
		, bStop( false )
		, bCurrentlyGenerating( false )
		, pCurrentlyGenerating( false )
	{}
};





cChunkGenerator::cChunkGenerator( cChunkMap* a_pChunkMap )
	: m_pState( new sChunkGeneratorState )
	, m_pChunkMap( a_pChunkMap )
{
	m_pState->pThread = new cThread( GenerateThread, this, "cChunkGenerator::GenerateThread" );
	m_pState->pThread->Start( true );
}





cChunkGenerator::~cChunkGenerator()
{
	m_pState->bStop = true;

	m_pState->m_Semaphore.Signal(); // Signal so thread can continue and exit
	delete m_pState->pThread;

	delete m_pState;
}





void cChunkGenerator::GenerateChunk( int a_X, int a_Z )
{
	cCSLock Lock(&m_pState->m_CriticalSection);

	if (m_pState->bCurrentlyGenerating)
	{
		if ((m_pState->CurrentlyGeneratingCoords.first == a_X) && (m_pState->CurrentlyGeneratingCoords.second == a_Z))
		{
			return;	// Already generating this chunk, so ignore
		}
	}

	m_pState->GenerateQueue.remove( ChunkCoord(a_X, a_Z) );
	if (m_pState->GenerateQueue.size() >= QUEUE_WARNING_LIMIT)
	{
		LOGWARN("WARNING: Adding chunk (%i, %i) to generation queue; Queue is too big! (%i)", a_X, a_Z, m_pState->GenerateQueue.size() );
	}
	m_pState->GenerateQueue.push_back( ChunkCoord(a_X, a_Z) );

	Lock.Unlock();
	
	m_pState->m_Semaphore.Signal();
}





void cChunkGenerator::GenerateThread( void* a_Params )
{
	// Cache some values for easy access (they are all references/pointers)
	cChunkGenerator * self = (cChunkGenerator*)a_Params;
	sChunkGeneratorState * m_pState = self->m_pState;
	ChunkCoordList & GenerateQueue = m_pState->GenerateQueue;
	cChunkMap & ChunkMap = *self->m_pChunkMap;
	cCriticalSection * CriticalSection = &m_pState->m_CriticalSection;
	cSemaphore & Semaphore = m_pState->m_Semaphore;

	while (!m_pState->bStop)
	{
		cCSLock Lock(CriticalSection);
		if (GenerateQueue.size() == 0)
		{
			cCSUnlock Unlock(Lock);
			Semaphore.Wait();
		}
		if (m_pState->bStop) break;
		
		ChunkCoord coord = *GenerateQueue.begin();		// Get next coord from queue
		GenerateQueue.erase( GenerateQueue.begin() );	// Remove coordinate from queue
		m_pState->bCurrentlyGenerating = true;
		m_pState->CurrentlyGeneratingCoords = coord;
		Lock.Unlock();			// Unlock ASAP

		ChunkMap.GetWorld()->LockChunks();
		if( ChunkMap.GetChunk( coord.first, 0, coord.second ) ) // Make sure it has not been loaded in the meantime. Don't want to generate the same chunk twice
		{														// This is possible when forcing the server to generate a chunk in the main thread
			ChunkMap.GetWorld()->UnlockChunks();
			continue;
		}
		ChunkMap.GetWorld()->UnlockChunks();

		LOGINFO("cChunkGenerator generating chunk %i %i", coord.first, coord.second );
		cChunk* Chunk = new cChunk( coord.first, 0, coord.second, ChunkMap.GetWorld() );

		Lock.Lock();
		m_pState->pCurrentlyGenerating = Chunk;
		Lock.Unlock();

		Chunk->Initialize(); // Generate the chunk

		ChunkMap.GetWorld()->LockChunks();
		ChunkMap.AddChunk( Chunk );
		ChunkMap.GetWorld()->UnlockChunks();

		Lock.Lock();
		m_pState->bCurrentlyGenerating = false;
		m_pState->pCurrentlyGenerating = 0;
		Lock.Unlock();
	}  // while (!bStop)
}