This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunkManager.cpp
416 lines (317 loc) · 12.3 KB
/
ChunkManager.cpp
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
#
#include "ChunkManager.h"
#include <iostream>
#include <limits>
#include "Filesystem.h"
#include "GL/glut.h"
#include "functions.h"
void ChunkManager::updateTnow(int _Tnow)
{
Tnow = _Tnow;
}
void ChunkManager::removeOld()
{
for (auto itr = chunks.cbegin(); itr != chunks.cend(); /**/)
if (Tnow - itr->second.second > CHUNK_TTL_MS)
itr = chunks.erase(itr);
else
itr = std::next(itr);
}
void ChunkManager::updateTTL(ChkCrd Xpos)
{
chunks[Xpos].second = Tnow;
}
ChunkManager::ChT& ChunkManager::emplaceChunk(ChkCrd Xpos)
{
return chunks.emplace(Xpos, std::make_pair(Chunk(Xpos), Tnow)).first->second;
}
void ChunkManager::loadChunk(ChkCrd Xpos)
{
if (chunks.find(Xpos) == chunks.end())
emplaceChunk(Xpos);
}
Chunk& ChunkManager::getChunk(ChkCrd Xpos)
{
if (chunks.find(Xpos) == chunks.end())
return emplaceChunk(Xpos).first;
return chunks.find(Xpos)->second.first;
}
std::vector<BlkCrd> possibleBlocksCollisions(EntCrd p1, EntCrd p2)
{
BlkCrd first = floor(std::min(p1, p2));
BlkCrd last = ceil(std::max(p1, p2)) - 1;
size_t count = last - first + 1;
std::vector<BlkCrd> temp(count);
BlkCrd p = first;
for (size_t i = 0; i < count; ++i)
temp[i] = first + i;
return temp;
}
size_t ChunkManager::count()
{
return chunks.size();
}
BlockN ChunkManager::getBlockAt(BlkCrd x, BlkCrd y)
{
BlkCrd blXpos = x % CHUNK_WIDTH;
if (blXpos < 0)
blXpos = (blXpos + CHUNK_WIDTH) % CHUNK_WIDTH;
BlkCrd chXpos = (x - blXpos) / CHUNK_WIDTH;
// std::cout << "Check chX blX blY: " << chXpos << ' ' << blXpos << ' ' << y << std::endl;
return getChunk(chXpos).getBlockAt(blXpos, y);
}
std::tuple<Pos, BlockN, BlockN, int, EntCrd> ChunkManager::checkCollision(const Movement& movement, const Entity& entity)
{
EntCrd dX = movement.second.X - movement.first.X;
EntCrd dY = movement.second.Y - movement.first.Y;
Pos finalPos_X = movement.second;
Pos finalPos_Y = movement.second;
BlockN finalBlock_X = BlockN::air;
BlockN finalBlock_Y = BlockN::air;
int finalSide_X = SIDE_NONE;
int finalSide_Y = SIDE_NONE;
EntCrd maxDistSq = getDistSq(dX, dY);
EntCrd maxDistSq_X = maxDistSq;
EntCrd maxDistSq_Y = maxDistSq;
#if ENV_COLLISIONS
// std::cout << "Expected dist: X: " << dX << " Y: " << dY << " Total: " << sqrt(maxDistSq) << std::endl;
//*
if (dY < 0) // moving down, collide bottom of Entity with tops of blocks
{
EntCrd begY = movement.first.Y + entity.boxBrel();
EntCrd endY = movement.second.Y + entity.boxBrel();
EntCrd begX = std::min(movement.first.X, movement.second.X) + entity.boxLrel();
EntCrd endX = std::max(movement.first.X, movement.second.X) + entity.boxRrel();
// std::cout << "MinY: " << begY << " MaxY: " << endY << std::endl;
// std::cout << "MinX: " << begX << " MaxX: " << endX << std::endl;
uint count = floor(entity.width) + 2; // perhaps use dynamically for each or check smallest block in the area? / smallestBlockWidth
// make a list of points (relative to entity position) which we want to check
std::vector<Pos> checkPts = interpolate(entity.posBLrel(), entity.posBRrel(), count);
// std::cout << "Check points:" << std::endl;
// for (Pos& x : checkPts)
// x.cout();
// make a list of blocks which we want to check
std::vector<BlkCrd> loopYs = possibleBlocksCollisions(begY, endY);
std::vector<BlkCrd> loopXs = possibleBlocksCollisions(begX, endX);
// std::cout << std::endl;
// for each block, check if any of the checked lines collides with the side
for (const BlkCrd& blkY : loopYs)
{
for (const BlkCrd& blkX : loopXs)
{
BlockN blk = getBlockAt(blkX, blkY);
if (blk != 0)
{
const Pos blkPos = { blkX, blkY };
// std::cout << "Checking block: X: " << blkX << " Y: " << blkY << std::endl;
// std::cout << "Top layer: " << blkY + blk.boxrel_X() << " bounded: (" << blkX + blk.boxLrel() << ", " << blkX + blk.boxRrel() << ") " << std::endl;
for (const Pos& chkPt : checkPts)
{
// std::cout << "Checking point: ";
// chkPt.cout();
std::pair<bool, Pos> collParams = findIntersect(movement.first + chkPt, movement.second + chkPt, blkPos + blk.posTLrel(), blkPos + blk.posTRrel());
// std::cout << "Collided? " << (collParams.first ? "yes" : "no") << std::endl;
if (collParams.first)
{
Pos newEntPos = collParams.second - chkPt; // pos + pt = line, then coll - pt = pos
EntCrd newDistSq = getDistSq(movement.first, newEntPos);
if (newDistSq < maxDistSq_Y)
{
maxDistSq_Y = newDistSq;
finalPos_Y = newEntPos;
finalSide_Y = SIDE_BOTTOM;
finalBlock_Y = blk;
}
}
}
// std::cout << std::endl;
}
}
}
}
//*/
//*
if (dY > 0) // moving up, collide top of Entity with bottoms of blocks
{
EntCrd begY = movement.first.Y + entity.boxrel_X();
EntCrd endY = movement.second.Y + entity.boxrel_X();
EntCrd begX = std::min(movement.first.X, movement.second.X) + entity.boxLrel();
EntCrd endX = std::max(movement.first.X, movement.second.X) + entity.boxRrel();
// std::cout << "MinY: " << begY << " MaxY: " << endY << std::endl;
// std::cout << "MinX: " << begX << " MaxX: " << endX << std::endl;
uint count = floor(entity.width) + 2; // perhaps use dynamically for each or check smallest block in the area? / smallestBlockWidth
// make a list of points (relative to entity position) which we want to check
std::vector<Pos> checkPts = interpolate(entity.posTLrel(), entity.posTRrel(), count);
// std::cout << "Check points:" << std::endl;
// for (Pos& x : checkPts)
// x.cout();
// make a list of blocks which we want to check
std::vector<BlkCrd> loopYs = possibleBlocksCollisions(begY, endY);
std::vector<BlkCrd> loopXs = possibleBlocksCollisions(begX, endX);
// std::cout << std::endl;
// for each block, check if any of the checked lines collides with the side
for (const BlkCrd& blkY : loopYs)
{
for (const BlkCrd& blkX : loopXs)
{
BlockN blk = getBlockAt(blkX, blkY);
if (blk != 0)
{
const Pos blkPos = { blkX, blkY };
for (const Pos& chkPt : checkPts)
{
// std::cout << "Checking point: ";
// chkPt.cout();
std::pair<bool, Pos> collParams = findIntersect(movement.first + chkPt, movement.second + chkPt, blkPos + blk.posBLrel(), blkPos + blk.posBRrel());
// std::cout << "Collided? " << (collParams.first ? "yes" : "no") << std::endl;
if (collParams.first)
{
Pos newEntPos = collParams.second - chkPt; // pos + pt = line, then coll - pt = pos
EntCrd newDistSq = getDistSq(movement.first, newEntPos);
if (newDistSq < maxDistSq_Y)
{
maxDistSq_Y = newDistSq;
finalPos_Y = newEntPos;
finalSide_Y = SIDE_TOP;
finalBlock_Y = blk;
}
}
}
// std::cout << std::endl;
}
}
}
}
//*/
//*
if (dX < 0) // moving down, collide bottom of Entity with tops of blocks
{
EntCrd begX = movement.first.X + entity.boxLrel();
EntCrd endX = movement.second.X + entity.boxLrel();
EntCrd begY = std::min(movement.first.Y, movement.second.Y) + entity.boxBrel();
EntCrd endY = std::max(movement.first.Y, movement.second.Y) + entity.boxrel_X();
// std::cout << "MinX: " << begX << " MaxX: " << endX << std::endl;
// std::cout << "MinY: " << begY << " MaxY: " << endY << std::endl;
uint count = floor(entity.height) + 2; // perhaps use dynamically for each or check smallest block in the area? / smallestBlockWidth
// make a list of points (relative to entity position) which we want to check
std::vector<Pos> checkPts = interpolate(entity.posTLrel(), entity.posBLrel(), count);
// std::cout << "Check points:" << std::endl;
// for (Pos& x : checkPts)
// x.cout();
// make a list of blocks which we want to check
std::vector<BlkCrd> loopYs = possibleBlocksCollisions(begY, endY);
std::vector<BlkCrd> loopXs = possibleBlocksCollisions(begX, endX);
// std::cout << std::endl;
// for each block, check if any of the checked lines collides with the side
for (const BlkCrd& blkY : loopYs)
{
for (const BlkCrd& blkX : loopXs)
{
BlockN blk = getBlockAt(blkX, blkY);
if (blk != 0)
{
const Pos blkPos = { blkX, blkY };
// std::cout << "Checking block: X: " << blkX << " Y: " << blkY << std::endl;
for (const Pos& chkPt : checkPts)
{
// std::cout << "Checking point: ";
// chkPt.cout();
std::pair<bool, Pos> collParams = findIntersect(movement.first + chkPt, movement.second + chkPt, blkPos + blk.posTRrel(), blkPos + blk.posBRrel());
// std::cout << "Collided? " << (collParams.first ? "yes" : "no") << std::endl;
if (collParams.first)
{
Pos newEntPos = collParams.second - chkPt; // pos + pt = line, then coll - pt = pos
EntCrd newDistSq = getDistSq(movement.first, newEntPos);
if (newDistSq < maxDistSq_X)
{
maxDistSq_X = newDistSq;
finalPos_X = newEntPos;
finalSide_X = SIDE_RIGHT;
finalBlock_X = blk;
}
}
}
// std::cout << std::endl;
}
}
}
}
//*/
//*
if (dX > 0) // moving down, collide bottom of Entity with tops of blocks
{
EntCrd begX = movement.first.X + entity.boxRrel();
EntCrd endX = movement.second.X + entity.boxRrel();
EntCrd begY = std::min(movement.first.Y, movement.second.Y) + entity.boxBrel();
EntCrd endY = std::max(movement.first.Y, movement.second.Y) + entity.boxrel_X();
// std::cout << "MinX: " << begX << " MaxX: " << endX << std::endl;
// std::cout << "MinY: " << begY << " MaxY: " << endY << std::endl;
uint count = floor(entity.height) + 2; // perhaps use dynamically for each or check smallest block in the area? / smallestBlockWidth
// make a list of points (relative to entity position) which we want to check
std::vector<Pos> checkPts = interpolate(entity.posTRrel(), entity.posBRrel(), count);
// std::cout << "Check points:" << std::endl;
// for (Pos& x : checkPts)
// x.cout();
// make a list of blocks which we want to check
std::vector<BlkCrd> loopYs = possibleBlocksCollisions(begY, endY);
std::vector<BlkCrd> loopXs = possibleBlocksCollisions(begX, endX);
// std::cout << std::endl;
// for each block, check if any of the checked lines collides with the side
for (const BlkCrd& blkY : loopYs)
{
for (const BlkCrd& blkX : loopXs)
{
BlockN blk = getBlockAt(blkX, blkY);
if (blk != 0)
{
const Pos blkPos = { blkX, blkY };
// std::cout << "Checking block: X: " << blkX << " Y: " << blkY << std::endl;
for (const Pos& chkPt : checkPts)
{
// std::cout << "Checking point: ";
// chkPt.cout();
std::pair<bool, Pos> collParams = findIntersect(movement.first + chkPt, movement.second + chkPt, blkPos + blk.posTLrel(), blkPos + blk.posBLrel());
// std::cout << "Collided? " << (collParams.first ? "yes" : "no") << std::endl;
if (collParams.first)
{
Pos newEntPos = collParams.second - chkPt; // pos + pt = line, then coll - pt = pos
EntCrd newDistSq = getDistSq(movement.first, newEntPos);
if (newDistSq < maxDistSq_X)
{
maxDistSq_X = newDistSq;
finalPos_X = newEntPos;
finalSide_X = SIDE_LEFT;
finalBlock_X = blk;
}
}
}
// std::cout << std::endl;
}
}
}
}
//*/
// std::cout << "maxDist_X: " << sqrt(maxDistSq_X) << std::endl;
// std::cout << "maxDist_Y: " << sqrt(maxDistSq_Y) << std::endl;
#endif
EntCrd ratio = 1;
if (maxDistSq_X == maxDistSq_Y)
{
ratio = maxDistSq ? sqrt(maxDistSq_X / maxDistSq) : ratio;
return std::make_tuple(finalPos_X, finalBlock_X, finalBlock_Y, finalSide_X | finalSide_Y, ratio);
}
else if (maxDistSq_X > maxDistSq_Y)
{
ratio = maxDistSq ? sqrt(maxDistSq_Y / maxDistSq) : ratio;
return std::make_tuple(finalPos_Y, BlockN::air, finalBlock_Y, finalSide_Y, ratio);
}
else if (maxDistSq_X < maxDistSq_Y)
{
ratio = maxDistSq ? sqrt(maxDistSq_X / maxDistSq) : ratio;
return std::make_tuple(finalPos_X, finalBlock_X, BlockN::air, finalSide_X, ratio);
}
else
{
throw std::logic_error("What the fuck happened, all ifs failed");
}
}