-
Notifications
You must be signed in to change notification settings - Fork 51
/
mapview.cpp
698 lines (602 loc) · 21 KB
/
mapview.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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/** Copyright (c) 2013, Sean Kasun */
#include <QPainter>
#include <QResizeEvent>
#include <QMessageBox>
#include <cmath>
#include <assert.h>
#include "mapview.h"
#include "chunkcache.h"
#include "chunkrenderer.h"
#include "identifier/definitionmanager.h"
#include "identifier/blockidentifier.h"
#include "identifier/biomeidentifier.h"
#include "clamp.h"
MapView::MapView(QWidget *parent)
: QWidget(parent)
, depth(255)
, scale(1) // overworld coordinate mapping
, zoomLevel(0) // 1:1
, cache(ChunkCache::Instance())
{
adjustZoom(0, false, false);
connect(&cache, &ChunkCache::chunkLoaded,
this, &MapView::chunkUpdated);
setMouseTracking(true);
setFocusPolicy(Qt::StrongFocus);
int offset = 0;
for (int y = 0; y < 16; y++)
for (int x = 0; x < 16; x++) {
uchar color = ((x & 8) ^ (y & 8)) == 0 ? 0x44 : 0x88;
placeholder[offset++] = color;
placeholder[offset++] = color;
placeholder[offset++] = color;
placeholder[offset++] = 0xff;
}
// calculate exponential function for cave shade
float cavesum = 0.0;
for (int i=0; i<CAVE_DEPTH; i++) {
caveshade[i] = 1/exp(i/(CAVE_DEPTH/2.0));
cavesum += caveshade[i];
}
for (int i=0; i<CAVE_DEPTH; i++) {
caveshade[i] = 1.5 * caveshade[i] / cavesum;
}
}
QSize MapView::minimumSizeHint() const {
return QSize(300, 300);
}
QSize MapView::sizeHint() const {
return QSize(400, 400);
}
void MapView::attach(DefinitionManager *dm) {
this->dm = dm;
connect(dm, SIGNAL(packsChanged()),
this, SLOT(redraw()));
}
void MapView::setLocation(double x, double z) {
setLocation(x, depth, z, false, true);
}
void MapView::setLocation(double x, int y, double z, bool ignoreScale, bool useHeight) {
this->x = ignoreScale ? x : x / scale;
this->z = ignoreScale ? z : z / scale;
if (useHeight == true && depth != y) {
emit demandDepthValue(y);
} else {
redraw();
}
}
MapView::BlockLocation *MapView::getLocation()
{
currentLocation.x = x;
currentLocation.y = depth;
currentLocation.z = z;
currentLocation.scale = scale;
return ¤tLocation;
}
void MapView::setDimension(QString path, int scale) {
if (scale > 0) {
this->x *= this->scale;
this->z *= this->scale; // undo current scale transform
this->scale = scale;
this->x /= scale; // and do the new scale transform
this->z /= scale;
} else {
this->scale = 1; // no scaling because no relation to overworld
this->x = 0; // and we jump to the center spawn automatically
this->z = 0;
}
clearOverlayItems();
cache.clear();
cache.setPath(path);
redraw();
}
void MapView::setDepth(int depth) {
this->depth = depth;
redraw();
}
void MapView::setFlags(int flags) {
this->flags = flags;
}
int MapView::getFlags() const {
return flags;
}
int MapView::getDepth() const {
return depth;
}
void MapView::chunkUpdated(int x, int z) {
drawChunk(x, z);
update();
}
QString MapView::getWorldPath() {
return cache.getPath();
}
void MapView::updateSearchResultPositions(const QVector<QSharedPointer<OverlayItem> > &searchResults)
{
currentSearchResults = searchResults;
}
void MapView::clearCache() {
cache.clear();
redraw();
}
void MapView::adjustZoom(double steps, bool allowZoomOut, bool cursorSource)
{
// save old zoom value for panning to cursor
double oldZoom = zoom;
// get current zoom level as an index, rounded to nearest int
int oldZoomIndex = (int)(floor(zoomLevel + 0.5));
zoomLevel += steps;
// use Fibonacci numbers to get natural zoom behaviour
const float zoomTable[] = {1, 2, 3, 5, 8, 13, 21, 34, 55, 89};
const int zoomMin = allowZoomOut ? -4 : 0;
const int zoomMax = (sizeof(zoomTable) / sizeof(float)) -1;
// snap the zoom level to bounds when the user scrolls too far
// do not force zoom-in when trying to zoom-out with allowZoomOut=false
if (zoomLevel < zoomMin && steps < 0) zoomLevel = std::min(zoomMin, oldZoomIndex);
else if (zoomLevel > zoomMax) zoomLevel = zoomMax;
int zoomIndex = (int)(floor(zoomLevel + 0.5 ));
// check whether zoomIndex has changed since last adjustZoom call
// don't return early if steps == 0, this is used for initialization
if (zoomIndex == oldZoomIndex && steps != 0) return;
// determine minimal zoomed value that is allowed
// with current window size and available physical memory
bool restrictZoom = true;
int maxchunks = cache.getMemoryMax();
int chunks = cache.getCacheMax();
do {
// apply new zoom
if (zoomIndex < 0)
zoom = 1 / pow(2.0, -zoomIndex);
else
zoom = zoomTable[zoomIndex];
// check
int ppc = ceil(16*zoom);
int cx = (imageChunks.width() +ppc-1) / ppc;
int cz = (imageChunks.height()+ppc-1) / ppc;
chunks = cx * cz;
if ((1.2 * chunks) <= maxchunks)
restrictZoom = false; // everything matches with a low margin of 20%
else {
// restrict zoom
zoomIndex++;
// since zoom index is forced, reset stored zoom level to be the same value
zoomLevel = static_cast<double>(zoomIndex);
}
} while (restrictZoom);
// we try to set higher margin than above (100%)!
cache.setCacheMaxSize(2.0 * chunks);
// pan to keep cursor pixel in same location
if (cursorSource && QSettings().value("zoomFollowsCursor", true).toBool()) {
int centerx = imageChunks.width() / 2;
int centery = imageChunks.height() / 2;
double distancex = static_cast<double>(lastMouseX - centerx);
double distancey = static_cast<double>(lastMouseY - centery);
double distanceScale = (1 / oldZoom) - (1 / zoom);
x += distancex * distanceScale;
z += distancey * distanceScale;
}
// no point in redrawing if the zoom level didn't change
if (steps != 0) redraw();
}
static bool dragging = false;
void MapView::mousePressEvent(QMouseEvent *event) {
lastMouseX = event->x();
lastMouseY = event->y();
dragging = true;
}
void MapView::mouseMoveEvent(QMouseEvent *event) {
if (dragging) {
x += (lastMouseX-event->x()) / zoom;
z += (lastMouseY-event->y()) / zoom;
redraw();
}
lastMouseX = event->x();
lastMouseY = event->y();
if (!dragging) {
int centerblockx = floor(this->x);
int centerblockz = floor(this->z);
int centerx = imageChunks.width() / 2;
int centery = imageChunks.height() / 2;
centerx -= (this->x - centerblockx) * zoom;
centery -= (this->z - centerblockz) * zoom;
int mx = floor(centerblockx - (centerx - event->x()) / zoom);
int mz = floor(centerblockz - (centery - event->y()) / zoom);
getToolTip(mx, mz);
}
}
void MapView::mouseReleaseEvent(QMouseEvent * /* event */) {
dragging = false;
}
void MapView::mouseDoubleClickEvent(QMouseEvent *event) {
int centerblockx = floor(this->x);
int centerblockz = floor(this->z);
int centerx = imageChunks.width() / 2;
int centery = imageChunks.height() / 2;
centerx -= (this->x - centerblockx) * zoom;
centery -= (this->z - centerblockz) * zoom;
int mx = floor(centerblockx - (centerx - event->x()) / zoom);
int mz = floor(centerblockz - (centery - event->y()) / zoom);
// get the y coordinate
int my = getY(mx, mz);
QList<QVariant> properties;
for (auto &item : getItems(mx, my, mz)) {
properties.append(item->properties());
}
if (!properties.isEmpty()) {
emit showProperties(properties);
}
}
void MapView::wheelEvent(QWheelEvent *event) {
Qt::KeyboardModifier modifier4DepthSlider = Qt::KeyboardModifier(QSettings().value("modifier4DepthSlider", Qt::ShiftModifier).toUInt());
Qt::KeyboardModifier modifier4ZoomOut = Qt::KeyboardModifier(QSettings().value("modifier4ZoomOut", Qt::ControlModifier).toUInt());
// ignore if vertical/horizontal scroll wheel was used
float angle = (event->angleDelta().x() + event->angleDelta().y()) / 120.0;
if ((event->modifiers() & modifier4DepthSlider) == modifier4DepthSlider) {
// change depth
emit demandDepthChange(angle);
} else if ((event->modifiers() & modifier4ZoomOut) == modifier4ZoomOut) {
// allow change zoom also to zoom OUT
adjustZoom(angle, true, true);
} else {
// normal change zoom
adjustZoom(angle, false, true);
}
}
void MapView::keyPressEvent(QKeyEvent *event) {
// default: 16 blocks / 1 chunk
float stepSize = 16.0;
bool allowZoomOut = false;
if ((event->modifiers() & Qt::ShiftModifier) == Qt::ShiftModifier) {
// 1 block for fine tuning
stepSize = 1.0;
} else if ((event->modifiers() & Qt::AltModifier) == Qt::AltModifier) {
// 8 chunks
stepSize = 128.0;
} else if ((event->modifiers() & Qt::ControlModifier) == Qt::ControlModifier) {
// 32 chunks / 1 Region
stepSize = 512.0;
allowZoomOut = true;
}
switch (event->key()) {
case Qt::Key_Up:
case Qt::Key_W:
z -= stepSize / zoom;
redraw();
break;
case Qt::Key_Down:
case Qt::Key_S:
z += stepSize / zoom;
redraw();
break;
case Qt::Key_Left:
case Qt::Key_A:
x -= stepSize / zoom;
redraw();
break;
case Qt::Key_Right:
case Qt::Key_D:
x += stepSize / zoom;
redraw();
break;
case Qt::Key_PageUp:
case Qt::Key_Q:
adjustZoom(1, allowZoomOut, false);
redraw();
break;
case Qt::Key_PageDown:
case Qt::Key_E:
adjustZoom(-1, allowZoomOut, false);
redraw();
break;
case Qt::Key_Home:
case Qt::Key_Plus:
case Qt::Key_BracketLeft:
emit demandDepthChange(+1);
break;
case Qt::Key_End:
case Qt::Key_Minus:
case Qt::Key_BracketRight:
emit demandDepthChange(-1);
break;
}
}
void MapView::resizeEvent(QResizeEvent *event) {
// adapt size of rendered images
imageChunks = QImage(event->size(), QImage::Format_RGB32);
imageOverlays = QImage(event->size(), QImage::Format_RGBA8888);
// restrict zoom and adapt ChunkCache
adjustZoom(0, true, false);
// redraw everything
redraw();
}
void MapView::paintEvent(QPaintEvent * /* event */) {
QPainter p(this);
p.drawImage(QPoint(0, 0), imageChunks);
p.drawImage(QPoint(0, 0), imageOverlays);
p.end();
}
void MapView::redraw() {
if (!this->isEnabled()) {
// blank
imageChunks.fill(palette().color(QPalette::Base));
imageOverlays.fill(Qt::transparent);
update();
return;
}
double chunksize = 16 * zoom;
// first find the center block position
int centerchunkx = floor(x / 16);
int centerchunkz = floor(z / 16);
// and the center of the screen
int centerx = imageChunks.width() / 2;
int centery = imageChunks.height() / 2;
// and align for panning
centerx -= (x - centerchunkx * 16) * zoom;
centery -= (z - centerchunkz * 16) * zoom;
// now calculate the topleft block on the screen
int startx = centerchunkx - floor(centerx / chunksize) - 1;
int startz = centerchunkz - floor(centery / chunksize) - 1;
// and the dimensions of the screen in blocks
int blockswide = imageChunks.width() / chunksize + 3;
int blockstall = imageChunks.height() / chunksize + 3;
for (int cz = startz; cz < startz + blockstall; cz++)
for (int cx = startx; cx < startx + blockswide; cx++)
drawChunk(cx, cz);
// clear the overlay layer
imageOverlays.fill(0);
// add on the entity layer
QPainter canvas(&imageOverlays);
double halfviewwidth = imageOverlays.width() / 2 / zoom;
double halvviewheight = imageOverlays.height() / 2 / zoom;
double x1 = x - halfviewwidth;
double z1 = z - halvviewheight;
double x2 = x + halfviewwidth;
double z2 = z + halvviewheight;
// draw the entities
for (int cz = startz; cz < startz + blockstall; cz++) {
for (int cx = startx; cx < startx + blockswide; cx++) {
QSharedPointer<Chunk> chunk(cache.fetch(cx, cz));
if (chunk) {
// Entities from Chunks
for (auto &type : overlayItemTypes) {
auto range = chunk->entities.equal_range(type);
for (auto it = range.first; it != range.second; ++it) {
// don't show entities above our depth
int entityY = (*it)->midpoint().y;
// everything below the current block,
// but also inside the current block
if (entityY < depth + 1) {
int entityX = static_cast<int>((*it)->midpoint().x) & 0x0f;
int entityZ = static_cast<int>((*it)->midpoint().z) & 0x0f;
int index = entityX + (entityZ << 4);
int highY = chunk->depth[index];
if ( (entityY+10 >= highY) ||
(entityY+10 >= depth) )
(*it)->draw(x1, z1, zoom, &canvas);
}
}
}
}
}
}
const OverlayItem::Cuboid viewingCuboid(OverlayItem::Point(x1 - 1, -4096, z1 - 1),
OverlayItem::Point(x2 + 1, depth, z2 + 1));
// draw the generated structures
for (auto &type : overlayItemTypes) {
drawOverlayItems(overlayItems[type], viewingCuboid, x1, z1, canvas);
}
drawOverlayItems(currentSearchResults, viewingCuboid, x1, z1, canvas);
emit coordinatesChanged(x, depth, z);
update();
}
template<typename ListT>
void MapView::drawOverlayItems(const ListT &list, const OverlayItem::Cuboid& cuboid, double x1, double z1, QPainter& canvas)
{
for (auto &item : list) {
if (item->intersects(cuboid)) {
item->draw(x1, z1, zoom, &canvas);
}
}
}
void MapView::drawChunk(int x, int z) {
if (!this->isEnabled())
return;
// fetch the chunk
QSharedPointer<Chunk> chunk(cache.fetch(x, z));
if (chunk && !chunk->loaded) return;
if (chunk && chunk->rendering) return;
if (chunk && (chunk->renderedAt != depth ||
chunk->renderedFlags != flags)) {
//renderChunk(chunk);
chunk->rendering = true;
ChunkRenderer *renderer = new ChunkRenderer(x, z, depth, flags);
connect(renderer, &ChunkRenderer::rendered,
[chunk](int, int) { chunk->rendering = false; });
connect(renderer, SIGNAL(rendered(int, int)),
this, SLOT(chunkUpdated(int, int)));
QThreadPool::globalInstance()->start(renderer);
return;
}
// this figures out where on the screen this chunk should be drawn
// first find the center chunk
int centerchunkx = floor(this->x / 16);
int centerchunkz = floor(this->z / 16);
// and the center chunk screen coordinates
double centerx = imageChunks.width() / 2;
double centery = imageChunks.height() / 2;
// which need to be shifted to account for panning inside that chunk
centerx -= (this->x - centerchunkx * 16) * zoom;
centery -= (this->z - centerchunkz * 16) * zoom;
// centerx,y now points to the top left corner of the center chunk
// so now calculate our x,y in relation
double chunksize = 16 * zoom;
centerx += (x - centerchunkx) * chunksize;
centery += (z - centerchunkz) * chunksize;
const uchar* srcImageData = chunk ? chunk->getImage() : placeholder;
QImage srcImage(srcImageData, 16, 16, QImage::Format_RGB32);
QRectF targetRect(centerx, centery, chunksize, chunksize);
QPainter canvas(&imageChunks);
if (this->zoom < 1.0)
canvas.setRenderHint(QPainter::SmoothPixmapTransform);
canvas.drawImage(targetRect, srcImage);
// Draw the ChunkLock overlay:
if (this->flags & flgChunkLock) {
if (chunk && chunk->getIsChunkLocked()) {
canvas.setPen(QColor::fromRgb(0xff0000));
canvas.setBrush(Qt::transparent);
double xAdj = (centerx > 0) ? 1 : 2; // Adjustments are needed for chunks crossing the X or Y axis in viewspace
double yAdj = (centery > 0) ? 1 : 2; // Otherwise their bottom / right edges are missing, most likely due to rounding.
canvas.drawRect(centerx, centery, chunksize - xAdj, chunksize - yAdj);
}
}
}
void MapView::getToolTip(int x, int z) {
int cx = floor(x / 16.0);
int cz = floor(z / 16.0);
QSharedPointer<Chunk> chunk(cache.fetch(cx, cz));
int offset = (x & 0xf) + (z & 0xf) * 16;
int y = 0;
QString blockname = "Unknown Block";
QString biomename = "Unknown Biome";
QString blockstate;
QMap<QString, int> entityIds;
if ((chunk) && (chunk->highest >= chunk->lowest)) {
int top = std::min(depth, chunk->highest);
for (y = top; y >= chunk->lowest; y--) {
const ChunkSection *section = chunk->getSectionByY(y);
if (!section) {
// skip entire section
int section_idx = (y >> 4);
y = (section_idx << 4) - 1;
continue;
}
// get information about block
const PaletteEntry & pdata = section->getPaletteEntry(offset, y);
blockname = pdata.name;
// For unknown legacy block IDs, add the legacy ID to the displayed name.
// TODO: Hoist "Unknown Block" literal into constant
if (blockname == "Unknown Block" && pdata.properties.contains(PaletteEntry::legacyBlockIdProperty)) {
uint fullLegacyBlockId = pdata.properties[PaletteEntry::legacyBlockIdProperty].toUInt();
// Legacy IDs with internal values > 4096 are virtual IDs with
// higher-order bits encoding the data.
uint baseLegacyBlockId = fullLegacyBlockId & 4095;
uint legacyData = fullLegacyBlockId >> 12;
QString displayName = QStringLiteral("Unknown [%1:%2]").arg(baseLegacyBlockId).arg(legacyData);
blockname = displayName;
}
// in case of fully transparent blocks (meaning air)
// -> we continue downwards
auto & block = BlockIdentifier::Instance().getBlockInfo(pdata.hid);
if (block.alpha == 0.0) continue;
if (flags & MapView::flgSeaGround && block.isLiquid()) continue;
// list all Block States
for (auto key : pdata.properties.keys()) {
// If needed, legacyBlockId is already encoded into the display name.
if (key == PaletteEntry::legacyBlockIdProperty) {
continue;
}
blockstate += key;
blockstate += ":";
blockstate += pdata.properties[key].toString();
blockstate += " ";
}
blockstate.chop(1);
break;
}
int biomeID = chunk->getBiomeID((x & 0xf), y, (z & 0xf));
const BiomeInfo &biome = (chunk->version >=2800) ?
BiomeIdentifier::Instance().getBiomeBySection(biomeID) :
BiomeIdentifier::Instance().getBiomeByChunk (biomeID);
biomename = biome.name;
// count Entity of each display type
for (auto &item : getItems(x, y, z)) {
entityIds[item->display()]++;
}
}
QString entityStr;
if (!entityIds.empty()) {
QStringList entities;
QMap<QString, int>::const_iterator it, itEnd = entityIds.cend();
for (it = entityIds.cbegin(); it != itEnd; ++it) {
if (it.value() > 1) {
entities << it.key() + ":" + QString::number(it.value());
} else {
entities << it.key();
}
}
entityStr = entities.join(", ");
}
QString hovertext = QString("X:%1 Y:%2 Z:%3 - %4 - %5")
.arg(x).arg(y).arg(z)
.arg(biomename)
.arg(blockname);
if (blockstate.length() > 0)
hovertext += " (" + blockstate + ")";
if (entityStr.length() > 0)
hovertext += " - " + entityStr;
#if defined(DEBUG) || defined(_DEBUG) || defined(QT_DEBUG)
hovertext += " [Cache:"
+ QString().number(this->cache.getCacheUsage()) + "/"
+ QString().number(this->cache.getCacheMax()) + "]";
hovertext += " Zoom:" + QString().number(zoomLevel);
#endif
if (chunk && chunk->getIsChunkLocked()) {
hovertext += QString(" (ChunkLocked, needs %1)").arg(chunk->getChunkLockItemName());
}
emit hoverTextChanged(hovertext);
}
void MapView::addOverlayItem(QSharedPointer<OverlayItem> item) {
// test if item is already in list
for (auto &it: overlayItems[item->type()]) {
OverlayItem::Point p1 = it ->midpoint();
OverlayItem::Point p2 = item->midpoint();
if ( (p1.x == p2.x) && (p1.y == p2.y) && (p1.z == p2.z) )
return; // skip if already present
}
// otherwise add item
overlayItems[item->type()].push_back(item);
}
void MapView::clearOverlayItems() {
overlayItems.clear();
}
void MapView::setVisibleOverlayItemTypes(const QSet<QString>& itemTypes) {
overlayItemTypes = itemTypes;
}
int MapView::getY(int x, int z) {
int cx = floor(x / 16.0);
int cz = floor(z / 16.0);
QSharedPointer<Chunk> chunk(cache.fetch(cx, cz));
return chunk ? chunk->depth[(x & 0xf) + (z & 0xf) * 16] : -1;
}
QList<QSharedPointer<OverlayItem>> MapView::getItems(int x, int y, int z) {
QList<QSharedPointer<OverlayItem>> ret;
int cx = floor(x / 16.0);
int cz = floor(z / 16.0);
QSharedPointer<Chunk> chunk(cache.fetch(cx, cz));
if (chunk) {
double invzoom = 10.0 / zoom;
for (auto &type : overlayItemTypes) {
// generated structures
for (auto &item : overlayItems[type]) {
double ymin = chunk->lowest;
double ymax = depth;
if (item->intersects(OverlayItem::Point(x, ymin, z),
OverlayItem::Point(x, ymax, z))) {
ret.append(item);
}
}
// entities
auto itemRange = chunk->entities.equal_range(type);
for (auto itItem = itemRange.first; itItem != itemRange.second;
++itItem) {
double ymin = y - 4;
double ymax = depth + 4;
if ((*itItem)->intersects(
OverlayItem::Point(x - invzoom/2, ymin, z - invzoom/2),
OverlayItem::Point(x + 1 + invzoom/2, ymax, z + 1 + invzoom/2))) {
ret.append(*itItem);
}
}
}
}
return ret;
}