summaryrefslogtreecommitdiffstats
path: root/src/ChunkSender.cpp
blob: e27c9f541969debd5e566ad70a165b22d2c749ac (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

// ChunkSender.cpp

// Interfaces to the cChunkSender class representing the thread that waits for chunks becoming ready (loaded / generated) and sends them to clients





#include "Globals.h"
#include "ChunkSender.h"
#include "World.h"
#include "BlockEntities/BlockEntity.h"
#include "Protocol/ChunkDataSerializer.h"
#include "ClientHandle.h"
#include "Chunk.h"
#include "Entities/Player.h"





////////////////////////////////////////////////////////////////////////////////
// cChunkSender:

cChunkSender::cChunkSender(cWorld & a_World) :
	super("ChunkSender"),
	m_World(a_World)
{
}





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





bool cChunkSender::Start()
{
	m_ShouldTerminate = false;
	return super::Start();
}





void cChunkSender::Stop(void)
{
	m_ShouldTerminate = true;
	m_evtQueue.Set();
	Wait();
}





void cChunkSender::QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, eChunkPriority a_Priority, const std::weak_ptr<cClientHandle> & a_Client)
{
	ASSERT(!a_Client.expired());
	{
		cChunkCoords Chunk{ a_ChunkX, a_ChunkZ };
		cCSLock Lock(m_CS);

		auto Iterator = std::find_if(m_LoadQueue.begin(), m_LoadQueue.end(), [Chunk](const decltype(m_LoadQueue)::value_type & a_Entry) { return (a_Entry->m_Chunk == Chunk); });
		if (Iterator == m_LoadQueue.end())
		{
			auto ChunkStay = new cChunkQueue(a_Priority, Chunk, a_Client, *this);
			m_LoadQueue.emplace_back(ChunkStay);
			m_World.QueueTask([ChunkStay, this](cWorld & a_World) { ChunkStay->Enable(*m_World.GetChunkMap()); });
		}
		else
		{
			(*Iterator)->m_Priority = std::min(a_Priority, (*Iterator)->m_Priority);
			(*Iterator)->m_Clients.emplace_back(a_Client);
		}
	}
}





void cChunkSender::QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, eChunkPriority a_Priority, const std::vector<std::weak_ptr<cClientHandle>> & a_Clients)
{
	for (const auto Client : a_Clients)
	{
		QueueSendChunkTo(a_ChunkX, a_ChunkZ, a_Priority, Client);
	}
}





void cChunkSender::Execute(void)
{
	while (!m_ShouldTerminate)
	{
		m_evtQueue.Wait();

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

		std::sort(QueuedChunks.begin(), QueuedChunks.end(), [](const decltype(QueuedChunks)::value_type & a_Lhs, const decltype(QueuedChunks)::value_type & a_Rhs)
			{
				/* The Standard Priority Queue sorts from biggest to smallest
				return true here means you are smaller than the other object, and you get pushed down.

				The priorities go from HIGH (0) to LOW (3), so a smaller priority should mean further up the list
				therefore, return true (affirm we're "smaller", and get pushed down) only if our priority is bigger than theirs (they're more urgent)
				*/
				return a_Lhs->m_Priority < a_Rhs->m_Priority;
			}
		);

		for (const auto & Entry : QueuedChunks)
		{
			SendChunk(*Entry);
			m_World.QueueTask([Entry](cWorld & a_World) { Entry->Disable(); });
		}
	}  // while (!m_ShouldTerminate)
}





void cChunkSender::SendChunk(const cChunkQueue & a_Item)
{
	cChunkDataSerializer Data(a_Item.m_BlockTypes, a_Item.m_BlockMetas, a_Item.m_BlockLight, a_Item.m_BlockSkyLight, a_Item.m_BiomeMap, m_World.GetDimension());
	for (const auto Client : a_Item.m_Clients)
	{
		// Atomically acquired shared_ptr; thread safe
		auto ClientPointer = Client.lock();
		if (ClientPointer == nullptr)
		{
			continue;
		}

		// Send:
		ClientPointer->SendChunkData(m_World, a_Item.m_Chunk, Data);
	}
}





void cChunkSender::cChunkQueue::BiomeData(const cChunkDef::BiomeMap * a_BiomeMap)
{
	for (size_t i = 0; i < ARRAYCOUNT(m_BiomeMap); i++)
	{
		if ((*a_BiomeMap)[i] < 255)
		{
			// Normal MC biome, copy as-is:
			m_BiomeMap[i] = static_cast<unsigned char>((*a_BiomeMap)[i]);
		}
		else
		{
			// TODO: MCS-specific biome, need to map to some basic MC biome:
			ASSERT(!"Unimplemented MCS-specific biome");
		}
	}  // for i - m_BiomeMap[]
}





bool cChunkSender::cChunkQueue::OnAllChunksAvailable()
{
	VERIFY(m_ChunkSender.m_World.GetChunkData(m_Chunk.m_ChunkX, m_Chunk.m_ChunkZ, *this));

	{
		cCSLock Lock(m_ChunkSender.m_CS);
		m_ChunkSender.m_LoadQueue.erase(std::remove(m_ChunkSender.m_LoadQueue.begin(), m_ChunkSender.m_LoadQueue.end(), this), m_ChunkSender.m_LoadQueue.end());
		m_ChunkSender.m_SendChunks.push_back(this);
	}

	m_ChunkSender.m_evtQueue.Set();
	return false;
}