forked from mrkite/minutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapview.cpp
executable file
·737 lines (667 loc) · 17.3 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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*
Copyright (c) 2013, Sean Kasun
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "mapview.h"
#include "definitionmanager.h"
#include "blockidentifier.h"
#include "biomeidentifier.h"
#include <QPainter>
#include <QResizeEvent>
#include <QMessageBox>
#include <assert.h>
MapView::MapView(QWidget *parent) : QWidget(parent)
{
depth=255;
scale=1;
zoom=1.0;
connect(&cache, SIGNAL(chunkLoaded(int,int)),
this, SLOT(chunkUpdated(int,int)));
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;
}
}
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()));
this->blocks=dm->blockIdentifier();
this->biomes=dm->biomeIdentifier();
}
void MapView::setLocation(double x, double z)
{
this->x=x/scale;
this->z=z/scale;
redraw();
}
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;
}
cache.clear();
cache.setPath(path);
redraw();
}
void MapView::setDepth(int depth)
{
this->depth=depth;
redraw();
}
void MapView::setFlags(int flags)
{
this->flags=flags;
}
void MapView::chunkUpdated(int x, int z)
{
drawChunk(x,z);
update();
}
QString MapView::getWorldPath()
{
return cache.getPath();
}
void MapView::clearCache()
{
cache.clear();
//TODO: clear overlay items?
redraw();
}
static int lastX=-1,lastY=-1;
static bool dragging=false;
void MapView::mousePressEvent(QMouseEvent *event)
{
lastX=event->x();
lastY=event->y();
dragging=true;
}
void MapView::mouseMoveEvent(QMouseEvent *event)
{
if (!dragging)
{
int centerblockx=floor(this->x);
int centerblockz=floor(this->z);
int centerx=image.width()/2;
int centery=image.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);
return;
}
x+=(lastX-event->x())/zoom;
z+=(lastY-event->y())/zoom;
lastX=event->x();
lastY=event->y();
redraw();
}
void MapView::mouseReleaseEvent(QMouseEvent *)
{
dragging=false;
}
void MapView::mouseDoubleClickEvent(QMouseEvent*event)
{
int centerblockx=floor(this->x);
int centerblockz=floor(this->z);
int centerx=image.width()/2;
int centery=image.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;
foreach(const QSharedPointer<OverlayItem>& item, getItems(mx, my, mz))
{
properties.append(item->properties());
}
if (!properties.isEmpty())
{
emit showProperties(properties);
}
}
void MapView::wheelEvent(QWheelEvent *event)
{
if ((event->modifiers() & Qt::ShiftModifier) == Qt::ShiftModifier) // change depth
{
emit demandDepthChange(event->delta()/120);
}
else // change zoom
{
zoom+=floor(event->delta()/90.0);
if (zoom<1.0) zoom=1.0;
if (zoom>20.0) zoom=20.0;
redraw();
}
}
void MapView::keyPressEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_Up:
case Qt::Key_W:
z-=10.0/zoom;
redraw();
break;
case Qt::Key_Down:
case Qt::Key_S:
z+=10.0/zoom;
redraw();
break;
case Qt::Key_Left:
case Qt::Key_A:
x-=10.0/zoom;
redraw();
break;
case Qt::Key_Right:
case Qt::Key_D:
x+=10.0/zoom;
redraw();
break;
case Qt::Key_PageUp:
case Qt::Key_Q:
zoom++;
if (zoom>20.0) zoom=20.0;
redraw();
break;
case Qt::Key_PageDown:
case Qt::Key_E:
zoom--;
if (zoom<1.0) zoom=1.0;
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)
{
image=QImage(event->size(),QImage::Format_RGB32);
redraw();
}
void MapView::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.drawImage(QPoint(0,0),image);
p.end();
}
void MapView::redraw()
{
if (!this->isEnabled())
{
// blank
uchar *bits=image.bits();
int imgstride=image.bytesPerLine();
int imgoffset=0;
for (int y=0;y<image.height();y++,imgoffset+=imgstride)
memset(bits+imgoffset,0xee,imgstride);
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=image.width()/2;
int centery=image.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=image.width()/chunksize+3;
int blockstall=image.height()/chunksize+3;
for (int cz=startz;cz<startz+blockstall;cz++)
for (int cx=startx;cx<startx+blockswide;cx++)
drawChunk(cx,cz);
//add on the entity layer
QPainter canvas(&image);
double halfviewwidth = image.width()/2/zoom;
double halvviewheight = image.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++)
{
foreach(const QString& type, overlayItemTypes)
{
Chunk *chunk=cache.fetch(cx,cz);
if (chunk)
{
QPair<Chunk::EntityMap::iterator, Chunk::EntityMap::iterator> range = chunk->entities.equal_range(type);
for (Chunk::EntityMap::iterator it = range.first; it != range.second; ++it)
{
//don't show entities above our depth
int entityY = (*it)->midpoint().y;
if (entityY < depth)
{
int entityX = ((int)(*it)->midpoint().x) & 0x0f;
int entityZ = ((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);
}
}
}
}
}
}
//draw the generated structures
foreach(const QString& type, overlayItemTypes)
{
foreach(const QSharedPointer<OverlayItem>& item, overlayItems[type])
{
if (item->intersects(
OverlayItem::Point(x1-1, 0, z1-1),
OverlayItem::Point(x2+1, depth, z2+1)))
{
item->draw(x1, z1, zoom, canvas);
}
}
}
update();
}
void MapView::drawChunk(int x, int z)
{
if (!this->isEnabled())
return;
uchar *src=placeholder;
//fetch the chunk
Chunk *chunk=cache.fetch(x,z);
if (chunk && (chunk->renderedAt!=depth || chunk->renderedFlags!=flags))
{
renderChunk(chunk);
}
//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
int centerx=image.width()/2;
int centery=image.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;
int srcoffset=0;
uchar *bits=image.bits();
int imgstride=image.bytesPerLine();
int skipx=0,skipy=0;
int blockwidth=chunksize,blockheight=chunksize;
// now if we're off the screen we need to crop
if (centerx<0)
{
skipx=-centerx;
centerx=0;
}
if (centery<0)
{
skipy=-centery;
centery=0;
}
// or the other side, we need to trim
if (centerx+blockwidth>image.width())
blockwidth=image.width()-centerx;
if (centery+blockheight>image.height())
blockheight=image.height()-centery;
if (blockwidth<=0 || skipx>=blockwidth) return;
int imgoffset=centerx*4+centery*imgstride;
if (chunk)
src=chunk->image;
//blit (or scale blit)
for (int z=skipy;z<blockheight;z++,imgoffset+=imgstride)
{
srcoffset=floor(z/zoom)*16*4;
if (zoom==1.0)
memcpy(bits+imgoffset,src+srcoffset+skipx*4,(blockwidth-skipx)*4);
else
{
int xofs=0;
for (int x=skipx;x<blockwidth;x++,xofs+=4)
memcpy(bits+imgoffset+xofs,src+srcoffset+(int)floor(x/zoom)*4,4);
}
}
}
void MapView::renderChunk(Chunk *chunk)
{
int offset=0;
uchar *bits=chunk->image;
uchar *depthbits=chunk->depth;
for (int z=0;z<16;z++) //n->s
{
int lasty=-1;
for (int x=0;x<16;x++,offset++) //e->w
{
uchar r=0,g=0,b=0;
double alpha=0.0;
int top=depth;
if (top>chunk->highest)
top=chunk->highest;
int highest=0;
for (int y=top;y>=0;y--)
{
int sec=y>>4;
ChunkSection *section=chunk->sections[sec];
if (!section)
{
y=(sec<<4)-1; //skip whole section
continue;
}
// get data value
int data = section->getData(x,y,z);
// get BlockInfo from block value
BlockInfo &block=blocks->getBlock(section->getBlock(x,y,z),data);
if (block.alpha==0.0) continue;
// get light value from one block above
int light = 0;
ChunkSection *section1=NULL;
if (y<255)
section1 = chunk->sections[(y+1)>>4];
if (section1)
light = section1->getLight(x,y+1,z);
int light1 = light;
if (!(flags & flgLighting))
light = 13;
if ((alpha==0.0)&&(lasty!=-1))
{
if (lasty<y)
light+=2;
else if (lasty>y)
light-=2;
}
if (light<0) light=0;
if (light>15) light=15;
// define the color
quint32 color=block.colors[light];
quint32 colr = color >> 16;
quint32 colg = (color >> 8) & 0xff;
quint32 colb = color & 0xff;
if (flags & flgDepthShading)
{
// Use a table to define depth-relative shade:
static const quint32 shadeTable[] = {0, 12, 18, 22, 24, 26, 28, 29, 30, 31, 32};
size_t idx = std::min(static_cast<size_t>(depth - y), sizeof(shadeTable) / sizeof(*shadeTable) - 1);
quint32 shade = shadeTable[idx];
colr = colr - std::min(shade, colr);
colg = colg - std::min(shade, colg);
colb = colb - std::min(shade, colb);
}
if (flags & flgMobSpawn)
{
// get block info from 1 and 2 above and 1 below
quint16 blid1(0), blid2(0), blidB(0); // default to air
int data1(0), data2(0), dataB(0); // default variant
ChunkSection *section2=NULL;
ChunkSection *sectionB=NULL;
if (y<254)
section2 = chunk->sections[(y+2)>>4];
if (y>0)
sectionB = chunk->sections[(y-1)>>4];
if (section1)
{
blid1 = section1->getBlock(x,y+1,z);
data1 = section1->getData(x,y+1,z);
}
if (section2)
{
blid2 = section2->getBlock(x,y+2,z);
data2 = section2->getData(x,y+2,z);
}
if (sectionB)
{
blidB = sectionB->getBlock(x,y-1,z);
dataB = sectionB->getData(x,y-1,z);
}
BlockInfo &block2=blocks->getBlock(blid2,data2);
BlockInfo &block1=blocks->getBlock(blid1,data1);
BlockInfo &block0=block;
BlockInfo &blockB=blocks->getBlock(blidB,dataB);
int light0 = section->getLight(x,y,z);
// spawn check #1: on top of solid block
if ( block0.doesBlockHaveSolidTopSurface(data) &&
!block0.isBedrock() &&
(light1<8) &&
!block1.isBlockNormalCube() && block1.spawninside && !block1.isLiquid() &&
!block2.isBlockNormalCube() && block2.spawninside )
{
colr = (colr+256)/2;
colg = (colg+0)/2;
colb = (colb+192)/2;
}
// spawn check #2: current block is transparent, but mob can spawn through (e.g. snow)
if ( blockB.doesBlockHaveSolidTopSurface(dataB) &&
!blockB.isBedrock() &&
(light0<8) &&
!block0.isBlockNormalCube() && block0.spawninside && !block0.isLiquid() &&
!block1.isBlockNormalCube() && block1.spawninside )
{
colr = (colr+192)/2;
colg = (colg+0)/2;
colb = (colb+256)/2;
}
}
if (alpha==0.0)
{
alpha = block.alpha;
r = colr;
g = colg;
b = colb;
highest = y;
}
else
{
r =(quint8)(alpha*r + (1.0-alpha)*(colr));
g =(quint8)(alpha*g + (1.0-alpha)*(colg));
b =(quint8)(alpha*b + (1.0-alpha)*(colb));
alpha+=block.alpha*(1.0-alpha);
}
// finish deepth (Y) scanning when color is saturated enough
if (block.alpha==1.0 || alpha>0.9)
break;
}
*depthbits++=lasty=highest;
*bits++=b;
*bits++=g;
*bits++=r;
*bits++=0xff;
}
}
chunk->renderedAt=depth;
chunk->renderedFlags=flags;
}
void MapView::getToolTip(int x, int z)
{
int cx=floor(x/16.0);
int cz=floor(z/16.0);
Chunk *chunk=cache.fetch(cx,cz);
int offset=(x&0xf)+(z&0xf)*16;
int id=0,bd=0;
QString name="Unknown";
QString biome="Unknown Biome";
QMap<QString, int> entityIds;
if (chunk)
{
int top=depth;
if (top>chunk->highest)
top=chunk->highest;
int y = 0;
for (y=top;y>=0;y--)
{
int sec=y>>4;
ChunkSection *section=chunk->sections[sec];
if (!section)
{
y=(sec<<4)-1; //skip entire section
continue;
}
int yoffset=(y&0xf)<<8;
int data=section->data[(offset+yoffset)/2];
if (x&1) data>>=4;
BlockInfo &block=blocks->getBlock(section->blocks[offset+yoffset],data&0xf);
if (block.alpha==0.0) continue;
//found block
name=block.getName();
id=section->blocks[offset+yoffset];
bd=data&0xf;
break;
}
BiomeInfo &bi=biomes->getBiome(chunk->biomes[(x&0xf)+(z&0xf)*16]);
biome=bi.name;
foreach (const QSharedPointer<OverlayItem>& item, getItems(x, y, z))
{
entityIds[item->display()]++;
}
}
QString entityStr;
if (!entityIds.empty())
{
QStringList entities;
QMap<QString, int>::iterator it, itEnd = entityIds.end();
for (it = entityIds.begin(); it != itEnd; ++it)
{
if (it.value() > 1)
{
entities << it.key() + ":" + QString::number(it.value());
}
else
{
entities << it.key();
}
}
entityStr = entities.join(", ");
}
emit hoverTextChanged(tr("X:%1 Z:%2 - %3 - %4 (%5:%6) %7")
.arg(x)
.arg(z)
.arg(biome)
.arg(name)
.arg(id)
.arg(bd)
.arg(entityStr));
}
void MapView::addOverlayItem(QSharedPointer<OverlayItem> item)
{
overlayItems[item->type()].push_back(item);
}
void MapView::showOverlayItemTypes(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);
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);
Chunk *chunk=cache.fetch(cx,cz);
if (chunk)
{
double invzoom = 10.0/zoom;
foreach (const QString& type, overlayItemTypes)
{
//generated structures
foreach (const QSharedPointer<OverlayItem>& item, overlayItems[type])
{
double ymin = 0;
double ymax = depth;
//TODO: handle depth?
if (item->intersects(
OverlayItem::Point(x, ymin, z),
OverlayItem::Point(x, ymax, z)))
{
ret.append(item);
}
}
//entities
QPair<Chunk::EntityMap::iterator, Chunk::EntityMap::iterator> itemRange = chunk->entities.equal_range(type);
for(Chunk::EntityMap::iterator 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;
}