summaryrefslogtreecommitdiffstats
path: root/src/LightingThread.cpp
blob: 6f51e173ca6255c694a86cd13f3e237a4f7e586d (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473

// LightingThread.cpp

// Implements the cLightingThread class representing the thread that processes requests for lighting

#include "Globals.h"
#include "LightingThread.h"
#include "ChunkMap.h"
#include "ChunkStay.h"
#include "World.h"






/** Chunk data callback that takes the chunk data and puts them into cLightingThread's m_BlockTypes[] / m_HeightMap[]: */
class cReader :
	public cChunkDataCallback
{
	virtual void ChunkData(const cChunkData & a_ChunkBuffer) override
	{
		a_ChunkBuffer.CopyBlockTypes(m_BlockTypes.data());
	}

	virtual void HeightMap(const cChunkDef::HeightMap * a_Heightmap) override
	{
		std::copy(std::begin(*a_Heightmap), std::end(*a_Heightmap), std::begin(m_HeightMap));
	}

public:

	cLightingThread::BlockDataArray & m_BlockTypes;  // 3x3 chunks of block types, organized as a single XZY blob of data (instead of 3x3 XZY blobs)
	cLightingThread::HeightDataArray & m_HeightMap;  // 3x3 chunks of height map,  organized as a single XZY blob of data (instead of 3x3 XZY blobs)

	cReader(cLightingThread::BlockDataArray & a_BlockTypes, cLightingThread::HeightDataArray & a_HeightMap) :
		m_BlockTypes(a_BlockTypes),
		m_HeightMap(a_HeightMap)
	{
	}
} ;





////////////////////////////////////////////////////////////////////////////////
// cLightingThread:

cLightingThread::cLightingThread(void) :
	super("cLightingThread"),
	m_World(nullptr)
{
}





cLightingThread::~cLightingThread()
{
	Stop();
}





bool cLightingThread::Start(cWorld * a_World)
{
	ASSERT(m_World == nullptr);  // Not started yet
	m_World = a_World;

	return super::Start();
}





void cLightingThread::Stop(void)
{
	m_ShouldTerminate = true;
	m_evtItemDataLoaded.Set();
	Wait();
}





void cLightingThread::QueueChunk(int a_ChunkX, int a_ChunkZ, std::unique_ptr<cChunkCoordCallback> a_CallbackAfter)
{
	ASSERT(m_World != nullptr);  // Did you call Start() properly?

	// If the chunk is already lit, skip it:
	if (m_World->IsChunkValid(a_ChunkX, a_ChunkZ) && m_World->IsChunkLighted(a_ChunkX, a_ChunkZ))
	{
		if (a_CallbackAfter != nullptr)
		{
			a_CallbackAfter->Call(a_ChunkX, a_ChunkZ, true);
		}
		return;
	}


	auto ChunkStay = new cLightingChunkStay(*this, a_ChunkX, a_ChunkZ, std::move(a_CallbackAfter));
	{
		// The ChunkStay will enqueue itself using the QueueChunkStay() once it is fully loaded
		// In the meantime, put it into the PendingQueue so that it can be removed when stopping the thread
		cCSLock Lock(m_CS);
		m_PendingQueue.emplace_back(ChunkStay);
	}
	ChunkStay->Enable(*m_World->GetChunkMap());
}





size_t cLightingThread::GetQueueLength(void)
{
	cCSLock Lock(m_CS);
	return m_PendingQueue.size();
}





void cLightingThread::Execute(void)
{
	while (!m_ShouldTerminate)
	{
		m_evtItemDataLoaded.Wait();

		decltype(m_Queue) QueuedChunks;
		{
			cCSLock Lock(m_CS);
			std::swap(m_Queue, QueuedChunks);
		}

		for (const auto & Entry : QueuedChunks)
		{
			Entry->m_BlockTypes = cpp14::make_unique<BlockDataArray>();
			Entry->m_HeightMap = cpp14::make_unique<HeightDataArray>();
		}

		m_World->QueueTask(
			[&QueuedChunks](cWorld & a_World)
			{
				for (const auto & Entry : QueuedChunks)
				{
					cReader Reader(*Entry->m_BlockTypes, *Entry->m_HeightMap);
					VERIFY(a_World.GetChunkData(Entry->m_ChunkX, Entry->m_ChunkZ, Reader));
				}
			}
		).wait();

		for (const auto & Entry : QueuedChunks)
		{
			LightChunk(*Entry);
		}
	}  // while (!m_ShouldTerminate)
}





void cLightingThread::LightChunk(cLightingChunkStay & a_Item)
{
	std::unique_ptr<BlockDataArray>
		BlockLight{ cpp14::make_unique<decltype(BlockLight)::element_type>() },
		SkyLight{ cpp14::make_unique<decltype(BlockLight)::element_type>() }
	;

	CalcLight(a_Item, PrepareBlockLight(a_Item, *BlockLight), *BlockLight);

	/*
	// DEBUG: Save chunk data with highlighted seeds for visual inspection:
	cFile f4;
	if (
		f4.Open(Printf("Chunk_%d_%d_seeds.grab", a_Item.m_ChunkX, a_Item.m_ChunkZ), cFile::fmWrite)
	)
	{
		for (int z = 0; z < cChunkDef::Width * 3; z++)
		{
			for (int y = cChunkDef::Height / 2; y >= 0; y--)
			{
				unsigned char Seeds     [cChunkDef::Width * 3];
				memcpy(Seeds, m_BlockTypes + y * cLightingChunkStay::BlocksPerYLayer + z * cChunkDef::Width * 3, cChunkDef::Width * 3);
				for (int x = 0; x < cChunkDef::Width * 3; x++)
				{
					if (m_IsSeed1[y * cLightingChunkStay::BlocksPerYLayer + z * cChunkDef::Width * 3 + x])
					{
						Seeds[x] = E_BLOCK_DIAMOND_BLOCK;
					}
				}
				f4.Write(Seeds, cChunkDef::Width * 3);
			}
		}
		f4.Close();
	}
	//*/

	CalcLight(a_Item, PrepareSkyLight(a_Item, *SkyLight), *SkyLight);

	/*
	// DEBUG: Save XY slices of the chunk data and lighting for visual inspection:
	cFile f1, f2, f3;
	if (
		f1.Open(Printf("Chunk_%d_%d_data.grab",  a_Item.m_ChunkX, a_Item.m_ChunkZ), cFile::fmWrite) &&
		f2.Open(Printf("Chunk_%d_%d_sky.grab",   a_Item.m_ChunkX, a_Item.m_ChunkZ), cFile::fmWrite) &&
		f3.Open(Printf("Chunk_%d_%d_glow.grab",  a_Item.m_ChunkX, a_Item.m_ChunkZ), cFile::fmWrite)
	)
	{
		for (int z = 0; z < cChunkDef::Width * 3; z++)
		{
			for (int y = cChunkDef::Height / 2; y >= 0; y--)
			{
				f1.Write(m_BlockTypes + y * cLightingChunkStay::BlocksPerYLayer + z * cChunkDef::Width * 3, cChunkDef::Width * 3);
				unsigned char SkyLight  [cChunkDef::Width * 3];
				unsigned char BlockLight[cChunkDef::Width * 3];
				for (int x = 0; x < cChunkDef::Width * 3; x++)
				{
					SkyLight[x]   = m_SkyLight  [y * cLightingChunkStay::BlocksPerYLayer + z * cChunkDef::Width * 3 + x] << 4;
					BlockLight[x] = m_BlockLight[y * cLightingChunkStay::BlocksPerYLayer + z * cChunkDef::Width * 3 + x] << 4;
				}
				f2.Write(SkyLight,   cChunkDef::Width * 3);
				f3.Write(BlockLight, cChunkDef::Width * 3);
			}
		}
		f1.Close();
		f2.Close();
		f3.Close();
	}
	//*/

	std::shared_ptr<BlockNibbles>
		CompressedBlockLight{ std::make_shared<decltype(CompressedBlockLight)::element_type>() },
		CompressedSkyLight{ std::make_shared<decltype(CompressedSkyLight)::element_type>() }
	;
	CompressLight(*BlockLight, *CompressedBlockLight);
	CompressLight(*SkyLight, *CompressedSkyLight);

	m_World->QueueTask(
		[&a_Item, CompressedBlockLight, CompressedSkyLight](cWorld & a_World)
		{
			a_World.ChunkLighted(
				a_Item.m_ChunkX,
				a_Item.m_ChunkZ,
				reinterpret_cast<const cChunkDef::BlockNibbles &>(*CompressedBlockLight->data()),
				reinterpret_cast<const cChunkDef::BlockNibbles &>(*CompressedSkyLight->data())
			);

			if (a_Item.m_CallbackAfter != nullptr)
			{
				a_Item.m_CallbackAfter->Call(a_Item.m_ChunkX, a_Item.m_ChunkZ, true);
			}

			a_Item.Disable();
		}
	);
}





std::vector<size_t> cLightingThread::PrepareSkyLight(const cLightingChunkStay & a_Item, BlockDataArray & a_SkyLight)
{
	std::vector<size_t> IndicesToProcess;
	IndicesToProcess.reserve(a_Item.m_HeightMap->size() * 40);

	for (size_t Index = 0; Index != a_Item.m_HeightMap->size(); ++Index)
	{
		for (int Y = cChunkDef::Height - 1; Y >= (*a_Item.m_HeightMap)[Index]; --Y)
		{
			auto HeightAdjustedIndex = Index + Y * cChunkDef::Width * cChunkDef::Width;
			a_SkyLight[HeightAdjustedIndex] = 15;

			IndicesToProcess.emplace_back(HeightAdjustedIndex);
		}
	}

	return IndicesToProcess;
}





std::vector<size_t> cLightingThread::PrepareBlockLight(const cLightingChunkStay & a_Item, BlockDataArray & a_BlockLight)
{
	std::vector<size_t> IndicesToProcess;
	IndicesToProcess.reserve(cChunkDef::Width * cChunkDef::Width);

	for (size_t Index = 0; Index != a_Item.m_BlockTypes->size(); ++Index)
	{
		auto Light = cBlockInfo::GetLightValue((*a_Item.m_BlockTypes)[Index]);
		a_BlockLight[Index] = Light;

		if (Light > 1)
		{
			IndicesToProcess.emplace_back(Index);
		}
	}

	return IndicesToProcess;
}





void cLightingThread::CalcLight(const cLightingChunkStay & a_Item, std::vector<size_t> a_IndicesToProcess, BlockDataArray & a_Light)
{
	while (!a_IndicesToProcess.empty())
	{
		auto Back = a_IndicesToProcess.back();
		a_IndicesToProcess.pop_back();

		DiscoverLightAdjacents(a_IndicesToProcess, a_Item, a_Light, Back);
	}
}





void cLightingThread::DiscoverLightAdjacents(std::vector<size_t> & a_IndicesToProcess, const cLightingChunkStay & a_Item, BlockDataArray & a_Light, size_t a_OriginatorIndex)
{
	auto Position = cChunkDef::IndexToCoordinate(a_OriginatorIndex);

	if (Position.x % cChunkDef::Width != 15)
	{
		// TODO: update neighbouring chunk
		UpdateLightAdjacent(a_IndicesToProcess, a_Item, a_Light, a_OriginatorIndex, a_OriginatorIndex + 1);
	}

	if (Position.x % cChunkDef::Width != 0)
	{
		// TODO: update neighbouring chunk
		UpdateLightAdjacent(a_IndicesToProcess, a_Item, a_Light, a_OriginatorIndex, a_OriginatorIndex - 1);
	}

	if (Position.z % cChunkDef::Width != 15)
	{
		// TODO: update neighbouring chunk
		UpdateLightAdjacent(a_IndicesToProcess, a_Item, a_Light, a_OriginatorIndex, a_OriginatorIndex + cChunkDef::Width);
	}

	if (Position.z % cChunkDef::Width != 0)
	{
		// TODO: update neighbouring chunk
		UpdateLightAdjacent(a_IndicesToProcess, a_Item, a_Light, a_OriginatorIndex, a_OriginatorIndex - cChunkDef::Width);
	}

	if (Position.y != cChunkDef::Height - 1)
	{
		UpdateLightAdjacent(a_IndicesToProcess, a_Item, a_Light, a_OriginatorIndex, a_OriginatorIndex + cChunkDef::Width * cChunkDef::Width);
	}

	if (Position.y != 0)
	{
		UpdateLightAdjacent(a_IndicesToProcess, a_Item, a_Light, a_OriginatorIndex, a_OriginatorIndex - cChunkDef::Width * cChunkDef::Width);
	}
}





void cLightingThread::UpdateLightAdjacent(std::vector<size_t> & a_IndicesToProcess, const cLightingChunkStay & a_Item, BlockDataArray & a_Light, size_t a_OriginatorIndex, size_t a_DestinationIndex)
{
	// Assertions for index validity occur within PropagateLightToAdjacent
	if (PropagateLightToAdjacent(a_Item, a_Light, a_OriginatorIndex, a_DestinationIndex))
	{
		a_IndicesToProcess.emplace_back(a_DestinationIndex);
	}
}





void cLightingThread::CompressLight(const BlockDataArray & a_LightArray, BlockNibbles & a_ChunkLight)
{
	for (size_t Index = 0; Index != a_ChunkLight.size(); ++Index)
	{
		auto AdjustedIndex = Index * 2;
		a_ChunkLight[Index] = a_LightArray[AdjustedIndex] | static_cast<NIBBLETYPE>(a_LightArray[AdjustedIndex + 1] << 4);
	}
}





////////////////////////////////////////////////////////////////////////////////
// cLightingThread::cLightingChunkStay:

cLightingThread::cLightingChunkStay::cLightingChunkStay(cLightingThread & a_LightingThread, int a_ChunkX, int a_ChunkZ, std::unique_ptr<cChunkCoordCallback> a_CallbackAfter) :
	m_CallbackAfter(std::move(a_CallbackAfter)),
	m_ChunkX(a_ChunkX),
	m_ChunkZ(a_ChunkZ),
	m_MaxHeight(0),  // Required by cReader to be initialised to zero
	m_LightingThread(a_LightingThread)
{
	Add(a_ChunkX, a_ChunkZ);
}





bool cLightingThread::cLightingChunkStay::OnAllChunksAvailable(void)
{
	if (m_LightingThread.m_ShouldTerminate)
	{
		return false;
	}

	ASSERT(m_LightingThread.m_World->IsChunkValid(m_ChunkX, m_ChunkZ));

	// If the chunk is already lit, skip it:
	if (m_LightingThread.m_World->IsChunkLighted(m_ChunkX, m_ChunkZ))
	{
		if (m_CallbackAfter != nullptr)
		{
			m_CallbackAfter->Call(m_ChunkX, m_ChunkZ, true);
		}


		{
			cCSLock Lock(m_LightingThread.m_CS);
			m_LightingThread.m_PendingQueue.erase(
				std::remove(m_LightingThread.m_PendingQueue.begin(), m_LightingThread.m_PendingQueue.end(), this),
				m_LightingThread.m_PendingQueue.end()
			);
		}

		return true;
	}

	{
		cCSLock Lock(m_LightingThread.m_CS);
		m_LightingThread.m_PendingQueue.erase(
			std::remove(m_LightingThread.m_PendingQueue.begin(), m_LightingThread.m_PendingQueue.end(), this),
			m_LightingThread.m_PendingQueue.end()
		);
		m_LightingThread.m_Queue.emplace_back(this);
	}

	m_LightingThread.m_evtItemDataLoaded.Set();

	// Keep the ChunkStay alive:
	return false;
}





void cLightingThread::cLightingChunkStay::OnDisabled(void)
{
	delete this;
}