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 #include "ChunkDef.h"
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  size_t 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 
EMCSBiome
Biome IDs The first batch corresponds to the clientside biomes, used by MineCraft.
Definition: BiomeDef.h:18
Wraps the chunk coords into a single structure.
Definition: ChunkDef.h:57
EMCSBiome BiomeMap[Width *Width]
The type used for any biomemap operations and storage inside Cuberite, using Cuberite biomes (need no...
Definition: ChunkDef.h:137
Interface class used as a callback for operations that involve chunk coords.
Definition: ChunkDef.h:467
Takes requests for generating chunks and processes them in a separate thread one by one.
cEvent m_evtRemoved
Set when an item is removed from the queue.
Queue m_Queue
Queue of the chunks to be generated.
cEvent m_Event
Set when an item is added to the queue or the thread should terminate.
virtual void Execute(void) override
This function, overloaded by the descendants, is called in the new thread.
bool Initialize(cPluginInterface &a_PluginInterface, cChunkSink &a_ChunkSink, cIniFile &a_IniFile)
Read settings from the ini file and initialize in preperation for being started.
std::list< QueueItem > Queue
cChunkSink * m_ChunkSink
The destination where the generated chunks are sent.
cCriticalSection m_CS
CS protecting access to the queue.
EMCSBiome GetBiomeAt(int a_BlockX, int a_BlockZ)
Returns the biome at the specified coords.
void DoGenerate(cChunkCoords a_Coords)
Generates the specified chunk and sets it into the chunksink.
void GenerateBiomes(cChunkCoords a_Coords, cChunkDef::BiomeMap &a_BiomeMap)
Generates the biomes for the specified chunk (directly, not in a separate thread).
std::unique_ptr< cChunkGenerator > m_Generator
The actual chunk generator engine used.
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.
virtual ~cChunkGeneratorThread() override
The interface through which the plugins are called for their OnChunkGenerating / OnChunkGenerated hoo...
virtual void CallHookChunkGenerated(cChunkDesc &a_ChunkDesc)=0
Called after the chunk is generated, before it is handed to the chunk sink.
virtual void CallHookChunkGenerating(cChunkDesc &a_ChunkDesc)=0
Called when the chunk is about to be generated.
The interface through which the generated chunks are handed to the cWorld or whoever created us.
virtual bool HasChunkAnyClients(cChunkCoords a_Coords)=0
Called when the generator is overloaded to skip chunks that are no longer needed.
virtual bool IsChunkValid(cChunkCoords a_Coords)=0
Called just before the chunk generation is started, to verify that it hasn't been generated in the me...
virtual void OnChunkGenerated(cChunkDesc &a_ChunkDesc)=0
Called after the chunk has been generated The interface may store the chunk, send it over network,...
virtual bool IsChunkQueued(cChunkCoords a_Coords)=0
Called to check whether the specified chunk is in the queued state.
QueueItem(cChunkCoords a_Coords, bool a_ForceRegeneration, cChunkCoordCallback *a_Callback)
cChunkCoordCallback * m_Callback
Callback to call after generating.
bool m_ForceRegeneration
Force the regeneration of an already existing chunk.
cChunkCoords m_Coords
The chunk coords.
The interface that all chunk generators must implement to provide the generated chunks.
Definition: Event.h:18
cIsThread(AString &&a_ThreadName)
Definition: IsThread.cpp:16