Cuberite
A lightweight, fast and extensible game server for Minecraft
ChunkGeneratorThread.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "OSSupport/IsThread.h"
4 
5 
6 
7 
8 
9 // fwd:
10 class cIniFile;
11 class cChunkDesc;
12 class cChunkGenerator;
13 
14 
15 
16 
17 
25  public cIsThread
26 {
27  using Super = cIsThread;
28 
29 public:
30 
33  {
34  public:
35  // Force a virtual destructor
36  virtual ~cPluginInterface() {}
37 
40  virtual void CallHookChunkGenerating(cChunkDesc & a_ChunkDesc) = 0;
41 
44  virtual void CallHookChunkGenerated(cChunkDesc & a_ChunkDesc) = 0;
45  } ;
46 
47 
49  class cChunkSink
50  {
51  public:
52  // Force a virtual destructor
53  virtual ~cChunkSink() {}
54 
59  virtual void OnChunkGenerated(cChunkDesc & a_ChunkDesc) = 0;
60 
64  virtual bool IsChunkValid(cChunkCoords a_Coords) = 0;
65 
68  virtual bool HasChunkAnyClients(cChunkCoords a_Coords) = 0;
69 
72  virtual bool IsChunkQueued(cChunkCoords a_Coords) = 0;
73  } ;
74 
75 
76  cChunkGeneratorThread (void);
77  virtual ~cChunkGeneratorThread() override;
78 
80  bool Initialize(cPluginInterface & a_PluginInterface, cChunkSink & a_ChunkSink, cIniFile & a_IniFile);
81 
82  void Stop(void);
83 
89  void QueueGenerateChunk(cChunkCoords a_Coords, bool a_ForceRegeneration, cChunkCoordCallback * a_Callback = nullptr);
90 
92  void GenerateBiomes(cChunkCoords a_Coords, cChunkDef::BiomeMap & a_BiomeMap);
93 
94  void WaitForQueueEmpty();
95 
96  int GetQueueLength() const;
97 
98  int GetSeed() const;
99 
101  EMCSBiome GetBiomeAt(int a_BlockX, int a_BlockZ);
102 
103 
104 private:
105 
106  struct QueueItem
107  {
110 
113 
116 
117  QueueItem(cChunkCoords a_Coords, bool a_ForceRegeneration, cChunkCoordCallback * a_Callback):
118  m_Coords(a_Coords),
119  m_ForceRegeneration(a_ForceRegeneration),
120  m_Callback(a_Callback)
121  {
122  }
123  };
124 
125  using Queue = std::list<QueueItem>;
126 
127 
130 
133 
136 
139 
141  std::unique_ptr<cChunkGenerator> m_Generator;
142 
145 
148 
149 
150  // cIsThread override:
151  virtual void Execute(void) override;
152 
154  void DoGenerate(cChunkCoords a_Coords);
155 };
156 
157 
158 
159 
virtual ~cChunkGeneratorThread() override
QueueItem(cChunkCoords a_Coords, bool a_ForceRegeneration, cChunkCoordCallback *a_Callback)
Definition: Event.h:17
cChunkSink * m_ChunkSink
The destination where the generated chunks are sent.
EMCSBiome GetBiomeAt(int a_BlockX, int a_BlockZ)
Returns the biome at the specified coords.
The interface through which the plugins are called for their OnChunkGenerating / OnChunkGenerated hoo...
Queue m_Queue
Queue of the chunks to be generated.
cChunkCoordCallback * m_Callback
Callback to call after generating.
EMCSBiome
Biome IDs The first batch corresponds to the clientside biomes, used by MineCraft.
Definition: BiomeDef.h:21
cCriticalSection m_CS
CS protecting access to the queue.
void QueueGenerateChunk(cChunkCoords a_Coords, bool a_ForceRegeneration, cChunkCoordCallback *a_Callback=nullptr)
Queues the chunk for generation If a-ForceGenerate is set, the chunk is regenerated even if the data ...
cPluginInterface * m_PluginInterface
The plugin interface that may modify the generated chunks.
Takes requests for generating chunks and processes them in a separate thread one by one...
bool Initialize(cPluginInterface &a_PluginInterface, cChunkSink &a_ChunkSink, cIniFile &a_IniFile)
Read settings from the ini file and initialize in preperation for being started.
virtual void CallHookChunkGenerating(cChunkDesc &a_ChunkDesc)=0
Called when the chunk is about to be generated.
void DoGenerate(cChunkCoords a_Coords)
Generates the specified chunk and sets it into the chunksink.
bool m_ForceRegeneration
Force the regeneration of an already existing chunk.
virtual void Execute(void) override
This is the main thread entrypoint.
virtual void CallHookChunkGenerated(cChunkDesc &a_ChunkDesc)=0
Called after the chunk is generated, before it is handed to the chunk sink.
std::unique_ptr< cChunkGenerator > m_Generator
The actual chunk generator engine used.
cChunkCoords m_Coords
The chunk coords.
cEvent m_Event
Set when an item is added to the queue or the thread should terminate.
Interface class used as a callback for operations that involve chunk coords.
Definition: ChunkDef.h:610
void GenerateBiomes(cChunkCoords a_Coords, cChunkDef::BiomeMap &a_BiomeMap)
Generates the biomes for the specified chunk (directly, not in a separate thread).
std::list< QueueItem > Queue
The interface through which the generated chunks are handed to the cWorld or whoever created us...
cEvent m_evtRemoved
Set when an item is removed from the queue.
EMCSBiome BiomeMap[Width *Width]
The type used for any biomemap operations and storage inside Cuberite, using Cuberite biomes (need no...
Definition: ChunkDef.h:147
The interface that all chunk generators must implement to provide the generated chunks.
cIsThread(const AString &a_ThreadName)
Definition: IsThread.cpp:51