summaryrefslogtreecommitdiffstats
path: root/source/cChunkGenerator.h
blob: cb30636c3db956b9035f765f3089dd819a352113 (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

// cChunkGenerator.h

// Interfaces to the cChunkGenerator class representing the thread that generates chunks

// The object takes requests for generating chunks and processes them in a separate thread one by one.
// The requests are not added to the queue if there is already a request with the same coords
// Before generating, the thread checks if the chunk hasn't been already generated.
// It is theoretically possible to have multiple generator threads by having multiple instances of this object (if the cChunkPtr is thread-safe)
// If the generator queue is overloaded, the generator skips chunks with no clients in them





#pragma once

#include "cIsThread.h"
#include "cChunk.h"





class cWorld;
class cWorldGenerator;





class cChunkGenerator :
	cIsThread
{
	typedef cIsThread super;
	
public:

	cChunkGenerator (void);
	~cChunkGenerator();

	bool Start(cWorld * a_World, const AString & a_WorldGeneratorName);
	void Stop(void);

	void GenerateChunk(int a_ChunkX, int a_ChunkY, int a_ChunkZ);  // Queues the chunk for generation; removes duplicate requests
	
	void WaitForQueueEmpty(void);
	
	int GetQueueLength(void);

private:

	cWorld * m_World;
	cWorldGenerator * m_pWorldGenerator;

	cCriticalSection m_CS;
	cChunkCoordsList m_Queue;
	cEvent           m_Event;  // Set when an item is added to the queue or the thread should terminate
	cEvent           m_evtRemoved;  // Set when an item is removed from the queue

	// cIsThread override:
	virtual void Execute(void) override;

	void DoGenerate(int a_ChunkX, int a_ChunkY, int a_ChunkZ);
};