Repository navigation
Expand file tree
/
Copy pathmapHandler.cpp
More file actions
1659 lines (1432 loc) · 50.4 KB
/
Copy pathmapHandler.cpp
File metadata and controls
1659 lines (1432 loc) · 50.4 KB
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* mapHandler.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "mapHandler.h"
#include "CBitmapHandler.h"
#include "gui/CAnimation.h"
#include "gui/SDL_Extensions.h"
#include "CGameInfo.h"
#include "../lib/mapObjects/CGHeroInstance.h"
#include "../lib/mapObjects/CObjectClassesHandler.h"
#include "../lib/CGameState.h"
#include "../lib/CHeroHandler.h"
#include "../lib/CTownHandler.h"
#include "Graphics.h"
#include "../lib/mapping/CMap.h"
#include "CDefHandler.h"
#include "../lib/CConfigHandler.h"
#include "../lib/CGeneralTextHandler.h"
#include "../lib/GameConstants.h"
#include "../lib/CStopWatch.h"
#include "CMT.h"
#include "../lib/CRandomGenerator.h"
#define ADVOPT (conf.go()->ac)
std::string nameFromType (int typ)
{
switch(ETerrainType(typ))
{
case ETerrainType::DIRT:
return std::string("DIRTTL.DEF");
case ETerrainType::SAND:
return std::string("SANDTL.DEF");
case ETerrainType::GRASS:
return std::string("GRASTL.DEF");
case ETerrainType::SNOW:
return std::string("SNOWTL.DEF");
case ETerrainType::SWAMP:
return std::string("SWMPTL.DEF");
case ETerrainType::ROUGH:
return std::string("ROUGTL.DEF");
case ETerrainType::SUBTERRANEAN:
return std::string("SUBBTL.DEF");
case ETerrainType::LAVA:
return std::string("LAVATL.DEF");
case ETerrainType::WATER:
return std::string("WATRTL.DEF");
case ETerrainType::ROCK:
return std::string("ROCKTL.DEF");
case ETerrainType::BORDER:
//TODO use me
break;
default:
//TODO do something here
break;
}
return std::string();
}
static bool objectBlitOrderSorter(const TerrainTileObject & a, const TerrainTileObject & b)
{
return CMapHandler::compareObjectBlitOrder(a.obj, b.obj);
}
struct NeighborTilesInfo
{
bool d7, //789
d8, //456
d9, //123
d4,
d5,
d6,
d1,
d2,
d3;
NeighborTilesInfo(const int3 & pos, const int3 & sizes, const std::vector< std::vector< std::vector<ui8> > > & visibilityMap)
{
auto getTile = [&](int dx, int dy)->bool
{
if ( dx + pos.x < 0 || dx + pos.x >= sizes.x
|| dy + pos.y < 0 || dy + pos.y >= sizes.y)
return false;
return visibilityMap[dx+pos.x][dy+pos.y][pos.z];
};
d7 = getTile(-1, -1); //789
d8 = getTile( 0, -1); //456
d9 = getTile(+1, -1); //123
d4 = getTile(-1, 0);
d5 = visibilityMap[pos.x][pos.y][pos.z];
d6 = getTile(+1, 0);
d1 = getTile(-1, +1);
d2 = getTile( 0, +1);
d3 = getTile(+1, +1);
}
bool areAllHidden() const
{
return !(d1 || d2 || d3 || d4 || d5 || d6 || d7 || d8 || d8 );
}
int getBitmapID() const
{
//NOTE: some images have unused in VCMI pair (same blockmap but a bit different look)
// 0-1, 2-3, 4-5, 11-13, 12-14
static const int visBitmaps[256] = {
-1, 34, 4, 4, 22, 23, 4, 4, 36, 36, 38, 38, 47, 47, 38, 38, //16
3, 25, 12, 12, 3, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //32
35, 39, 48, 48, 41, 43, 48, 48, 36, 36, 38, 38, 47, 47, 38, 38, //48
26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //64
0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //80
13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //96
0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //112
13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //128
15, 17, 30, 30, 16, 19, 30, 30, 46, 46, 40, 40, 32, 32, 40, 40, //144
2, 25, 12, 12, 2, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //160
18, 42, 31, 31, 20, 21, 31, 31, 46, 46, 40, 40, 32, 32, 40, 40, //176
26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //192
0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //208
13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //224
0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //240
13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10 //256
};
return visBitmaps[d1 + d2 * 2 + d3 * 4 + d4 * 8 + d6 * 16 + d7 * 32 + d8 * 64 + d9 * 128]; // >=0 -> partial hide, <0 - full hide
}
};
void CMapHandler::prepareFOWDefs()
{
graphics->FoWfullHide = CDefHandler::giveDef("TSHRC.DEF");
graphics->FoWpartialHide = CDefHandler::giveDef("TSHRE.DEF");
//adding necessary rotations
static const int missRot [] = {22, 15, 2, 13, 12, 16, 28, 17, 20, 19, 7, 24, 26, 25, 30, 32, 27};
Cimage nw;
for(auto & elem : missRot)
{
nw = graphics->FoWpartialHide->ourImages[elem];
nw.bitmap = CSDL_Ext::verticalFlip(nw.bitmap);
graphics->FoWpartialHide->ourImages.push_back(nw);
}
//necessaary rotations added
//alpha - transformation
for(auto & elem : graphics->FoWpartialHide->ourImages)
{
CSDL_Ext::alphaTransform(elem.bitmap);
}
//initialization of type of full-hide image
hideBitmap.resize(sizes.x);
for (auto & elem : hideBitmap)
{
elem.resize(sizes.y);
}
for (auto & elem : hideBitmap)
{
for (int j = 0; j < sizes.y; ++j)
{
elem[j].resize(sizes.z);
for(int k = 0; k < sizes.z; ++k)
{
elem[j][k] = CRandomGenerator::getDefault().nextInt(graphics->FoWfullHide->ourImages.size() - 1);
}
}
}
}
EMapAnimRedrawStatus CMapHandler::drawTerrainRectNew(SDL_Surface * targetSurface, const MapDrawingInfo * info, bool redrawOnlyAnim /* = false */)
{
assert(info);
bool hasActiveFade = updateObjectsFade();
resolveBlitter(info)->blit(targetSurface, info);
return hasActiveFade ? EMapAnimRedrawStatus::REDRAW_REQUESTED : EMapAnimRedrawStatus::OK;
}
void CMapHandler::roadsRiverTerrainInit()
{
//initializing road's and river's DefHandlers
roadDefs.push_back(CDefHandler::giveDefEss("dirtrd.def"));
roadDefs.push_back(CDefHandler::giveDefEss("gravrd.def"));
roadDefs.push_back(CDefHandler::giveDefEss("cobbrd.def"));
staticRiverDefs.push_back(CDefHandler::giveDefEss("clrrvr.def"));
staticRiverDefs.push_back(CDefHandler::giveDefEss("icyrvr.def"));
staticRiverDefs.push_back(CDefHandler::giveDefEss("mudrvr.def"));
staticRiverDefs.push_back(CDefHandler::giveDefEss("lavrvr.def"));
for(auto & elem : staticRiverDefs)
{
for(size_t h=0; h < elem->ourImages.size(); ++h)
{
CSDL_Ext::alphaTransform(elem->ourImages[h].bitmap);
}
}
for(auto & elem : roadDefs)
{
for(size_t h=0; h < elem->ourImages.size(); ++h)
{
CSDL_Ext::alphaTransform(elem->ourImages[h].bitmap);
}
}
// Create enough room for the whole map and its frame
ttiles.resize(sizes.x, frameW, frameW);
for (int i=0-frameW;i<ttiles.size()-frameW;i++)
{
ttiles[i].resize(sizes.y, frameH, frameH);
}
for (int i=0-frameW;i<ttiles.size()-frameW;i++)
{
for (int j=0-frameH;j<(int)sizes.y+frameH;j++)
ttiles[i][j].resize(sizes.z, 0, 0);
}
}
void CMapHandler::borderAndTerrainBitmapInit()
{
CDefHandler * bord = CDefHandler::giveDef("EDG.DEF");
bord->notFreeImgs = true;
terrainGraphics.resize(10);
for (int i = 0; i < 10 ; i++)
{
CDefHandler *hlp = CDefHandler::giveDef(nameFromType(i));
terrainGraphics[i].resize(hlp->ourImages.size());
hlp->notFreeImgs = true;
for(size_t j=0; j < hlp->ourImages.size(); ++j)
terrainGraphics[i][j] = hlp->ourImages[j].bitmap;
delete hlp;
}
for (int i=0-frameW; i<sizes.x+frameW; i++) //by width
{
for (int j=0-frameH; j<sizes.y+frameH;j++) //by height
{
for(int k=0; k<sizes.z; ++k) //by levles
{
if(i < 0 || i > (sizes.x-1) || j < 0 || j > (sizes.y-1))
{
int terBitmapNum = -1;
auto & rand = CRandomGenerator::getDefault();
if(i==-1 && j==-1)
terBitmapNum = 16;
else if(i==-1 && j==(sizes.y))
terBitmapNum = 19;
else if(i==(sizes.x) && j==-1)
terBitmapNum = 17;
else if(i==(sizes.x) && j==(sizes.y))
terBitmapNum = 18;
else if(j == -1 && i > -1 && i < sizes.x)
terBitmapNum = rand.nextInt(22, 23);
else if(i == -1 && j > -1 && j < sizes.y)
terBitmapNum = rand.nextInt(33, 34);
else if(j == sizes.y && i >-1 && i < sizes.x)
terBitmapNum = rand.nextInt(29, 30);
else if(i == sizes.x && j > -1 && j < sizes.y)
terBitmapNum = rand.nextInt(25, 26);
else
terBitmapNum = rand.nextInt(15);
if(terBitmapNum != -1)
{
ttiles[i][j][k].terbitmap = bord->ourImages[terBitmapNum].bitmap;
continue;
}
}
}
}
}
delete bord;
}
static void processDef (const ObjectTemplate & objTempl)
{
if(objTempl.id == Obj::EVENT)
{
graphics->advmapobjGraphics[objTempl.animationFile] = nullptr;
return;
}
CDefEssential * ourDef = graphics->getDef(objTempl);
if(!ourDef) //if object has already set handler (eg. heroes) it should not be overwritten
{
if(objTempl.animationFile.size())
{
graphics->advmapobjGraphics[objTempl.animationFile] = CDefHandler::giveDefEss(objTempl.animationFile);
}
else
{
logGlobal->warnStream() << "No def name for " << objTempl.id << " " << objTempl.subid;
return;
}
ourDef = graphics->getDef(objTempl);
}
//alpha transformation
for(auto & elem : ourDef->ourImages)
{
CSDL_Ext::alphaTransform(elem.bitmap);
}
}
void CMapHandler::initObjectRects()
{
//initializing objects / rects
for(auto & elem : map->objects)
{
const CGObjectInstance *obj = elem;
if( !obj
|| (obj->ID==Obj::HERO && static_cast<const CGHeroInstance*>(obj)->inTownGarrison) //garrisoned hero
|| (obj->ID==Obj::BOAT && static_cast<const CGBoat*>(obj)->hero)) //boat with hero (hero graphics is used)
{
continue;
}
if (!graphics->getDef(obj)) //try to load it
processDef(obj->appearance);
if (!graphics->getDef(obj)) // stil no graphics? exit
continue;
const SDL_Surface *bitmap = graphics->getDef(obj)->ourImages[0].bitmap;
for(int fx=0; fx < obj->getWidth(); ++fx)
{
for(int fy=0; fy < obj->getHeight(); ++fy)
{
int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
SDL_Rect cr;
cr.w = 32;
cr.h = 32;
cr.x = bitmap->w - fx * 32 - 32;
cr.y = bitmap->h - fy * 32 - 32;
TerrainTileObject toAdd(obj,cr);
if( map->isInTheMap(currTile) && // within map
cr.x + cr.w > 0 && // image has data on this tile
cr.y + cr.h > 0 &&
obj->coveringAt(currTile.x, currTile.y) // object is visible here
)
{
ttiles[currTile.x][currTile.y][currTile.z].objects.push_back(toAdd);
}
}
}
}
for(int ix=0; ix<ttiles.size()-frameW; ++ix)
{
for(int iy=0; iy<ttiles[0].size()-frameH; ++iy)
{
for(int iz=0; iz<ttiles[0][0].size(); ++iz)
{
stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), objectBlitOrderSorter);
}
}
}
}
void CMapHandler::init()
{
CStopWatch th;
th.getDiff();
graphics->advmapobjGraphics["AB01_.DEF"] = graphics->boatAnims[0];
graphics->advmapobjGraphics["AB02_.DEF"] = graphics->boatAnims[1];
graphics->advmapobjGraphics["AB03_.DEF"] = graphics->boatAnims[2];
// Size of visible terrain.
int mapW = conf.go()->ac.advmapW;
int mapH = conf.go()->ac.advmapH;
//sizes of terrain
sizes.x = map->width;
sizes.y = map->height;
sizes.z = map->twoLevel ? 2 : 1;
// Total number of visible tiles. Subtract the center tile, then
// compute the number of tiles on each side, and reassemble.
int t1, t2;
t1 = (mapW-32)/2;
t2 = mapW - 32 - t1;
tilesW = 1 + (t1+31)/32 + (t2+31)/32;
t1 = (mapH-32)/2;
t2 = mapH - 32 - t1;
tilesH = 1 + (t1+31)/32 + (t2+31)/32;
// Size of the frame around the map. In extremes positions, the
// frame must not be on the center of the map, but right on the
// edge of the center tile.
frameW = (mapW+31) /32 / 2;
frameH = (mapH+31) /32 / 2;
offsetX = (mapW - (2*frameW+1)*32)/2;
offsetY = (mapH - (2*frameH+1)*32)/2;
prepareFOWDefs();
roadsRiverTerrainInit(); //road's and river's DefHandlers; and simple values initialization
borderAndTerrainBitmapInit();
logGlobal->infoStream()<<"\tPreparing FoW, roads, rivers,borders: "<<th.getDiff();
initObjectRects();
logGlobal->infoStream()<<"\tMaking object rects: "<<th.getDiff();
}
CMapHandler::CMapBlitter *CMapHandler::resolveBlitter(const MapDrawingInfo * info) const
{
if (info->scaled)
return worldViewBlitter;
if (info->puzzleMode)
return puzzleViewBlitter;
return normalBlitter;
}
void CMapHandler::CMapNormalBlitter::drawElement(EMapCacheType cacheType, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect, bool alphaBlit, ui8 rotationInfo) const
{
if (rotationInfo != 0)
{
if (!sourceRect)
{
Rect sourceRect2(0, 0, sourceSurf->w, sourceSurf->h);
if (alphaBlit)
CSDL_Ext::getBlitterWithRotationAndAlpha(targetSurf)(sourceSurf, sourceRect2, targetSurf, *destRect, rotationInfo);
else
CSDL_Ext::getBlitterWithRotation(targetSurf)(sourceSurf, sourceRect2, targetSurf, *destRect, rotationInfo);
}
else
{
if (alphaBlit)
CSDL_Ext::getBlitterWithRotationAndAlpha(targetSurf)(sourceSurf, *sourceRect, targetSurf, *destRect, rotationInfo);
else
CSDL_Ext::getBlitterWithRotation(targetSurf)(sourceSurf, *sourceRect, targetSurf, *destRect, rotationInfo);
}
}
else
{
if (alphaBlit)
CSDL_Ext::blit8bppAlphaTo24bpp(sourceSurf, sourceRect, targetSurf, destRect);
else
CSDL_Ext::blitSurface(sourceSurf, sourceRect, targetSurf, destRect);
}
}
void CMapHandler::CMapNormalBlitter::init(const MapDrawingInfo * drawingInfo)
{
info = drawingInfo;
// Width and height of the portion of the map to process. Units in tiles.
tileCount.x = parent->tilesW;
tileCount.y = parent->tilesH;
topTile = info->topTile;
initPos.x = parent->offsetX + info->drawBounds->x;
initPos.y = parent->offsetY + info->drawBounds->y;
realTileRect = Rect(initPos.x, initPos.y, tileSize, tileSize);
// If moving, we need to add an extra column/line
if (info->movement.x != 0)
{
tileCount.x++;
initPos.x += info->movement.x;
if (info->movement.x > 0)
{
// Moving right. We still need to draw the old tile on the
// left, so adjust our referential
topTile.x--;
initPos.x -= tileSize;
}
}
if (info->movement.y != 0)
{
tileCount.y++;
initPos.y += info->movement.y;
if (info->movement.y > 0)
{
// Moving down. We still need to draw the tile on the top,
// so adjust our referential.
topTile.y--;
initPos.y -= tileSize;
}
}
// Reduce sizes if we go out of the full map.
if (topTile.x < -parent->frameW)
topTile.x = -parent->frameW;
if (topTile.y < -parent->frameH)
topTile.y = -parent->frameH;
if (topTile.x + tileCount.x > parent->sizes.x + parent->frameW)
tileCount.x = parent->sizes.x + parent->frameW - topTile.x;
if (topTile.y + tileCount.y > parent->sizes.y + parent->frameH)
tileCount.y = parent->sizes.y + parent->frameH - topTile.y;
}
SDL_Rect CMapHandler::CMapNormalBlitter::clip(SDL_Surface * targetSurf) const
{
SDL_Rect prevClip;
SDL_GetClipRect(targetSurf, &prevClip);
SDL_SetClipRect(targetSurf, info->drawBounds);
return prevClip;
}
CMapHandler::CMapNormalBlitter::CMapNormalBlitter(CMapHandler * parent)
: CMapBlitter(parent)
{
tileSize = 32;
halfTileSizeCeil = 16;
defaultTileRect = Rect(0, 0, tileSize, tileSize);
}
SDL_Surface * CMapHandler::CMapWorldViewBlitter::objectToIcon(Obj id, si32 subId, PlayerColor owner) const
{
int ownerIndex = 0;
if(owner < PlayerColor::PLAYER_LIMIT)
{
ownerIndex = owner.getNum() * 19;
}
else if (owner == PlayerColor::NEUTRAL)
{
ownerIndex = PlayerColor::PLAYER_LIMIT.getNum() * 19;
}
switch(id)
{
case Obj::MONOLITH_ONE_WAY_ENTRANCE:
case Obj::MONOLITH_ONE_WAY_EXIT:
case Obj::MONOLITH_TWO_WAY:
return info->iconsDef->ourImages[(int)EWorldViewIcon::TELEPORT].bitmap;
case Obj::SUBTERRANEAN_GATE:
return info->iconsDef->ourImages[(int)EWorldViewIcon::GATE].bitmap;
case Obj::ARTIFACT:
return info->iconsDef->ourImages[(int)EWorldViewIcon::ARTIFACT].bitmap;
case Obj::TOWN:
return info->iconsDef->ourImages[(int)EWorldViewIcon::TOWN + ownerIndex].bitmap;
case Obj::HERO:
return info->iconsDef->ourImages[(int)EWorldViewIcon::HERO + ownerIndex].bitmap;
case Obj::MINE:
return info->iconsDef->ourImages[(int)EWorldViewIcon::MINE_WOOD + subId + ownerIndex].bitmap;
case Obj::RESOURCE:
return info->iconsDef->ourImages[(int)EWorldViewIcon::RES_WOOD + subId + ownerIndex].bitmap;
}
return nullptr;
}
void CMapHandler::CMapWorldViewBlitter::calculateWorldViewCameraPos()
{
bool outsideLeft = topTile.x < 0;
bool outsideTop = topTile.y < 0;
bool outsideRight = std::max(0, topTile.x) + tileCount.x > parent->sizes.x;
bool outsideBottom = std::max(0, topTile.y) + tileCount.y > parent->sizes.y;
if (tileCount.x > parent->sizes.x)
topTile.x = parent->sizes.x / 2 - tileCount.x / 2; // center viewport if the whole map can fit into the screen at once
else if (outsideLeft)
{
if (outsideRight)
topTile.x = parent->sizes.x / 2 - tileCount.x / 2;
else
topTile.x = 0;
}
else if (outsideRight)
topTile.x = parent->sizes.x - tileCount.x;
if (tileCount.y > parent->sizes.y)
topTile.y = parent->sizes.y / 2 - tileCount.y / 2;
else if (outsideTop)
{
if (outsideBottom)
topTile.y = parent->sizes.y / 2 - tileCount.y / 2;
else
topTile.y = 0;
}
else if (outsideBottom)
topTile.y = parent->sizes.y - tileCount.y;
}
void CMapHandler::CMapWorldViewBlitter::drawScaledRotatedElement(EMapCacheType type, SDL_Surface * baseSurf, SDL_Surface * targetSurf, ui8 rotation,
float scale, SDL_Rect * dstRect, SDL_Rect * srcRect /*= nullptr*/) const
{
auto key = parent->cache.genKey((intptr_t)baseSurf, rotation);
auto scaledSurf = parent->cache.requestWorldViewCache(type, key);
if (scaledSurf) // blitting from cache
{
if (srcRect)
{
dstRect->w = srcRect->w;
dstRect->h = srcRect->h;
}
CSDL_Ext::blitSurface(scaledSurf, srcRect, targetSurf, dstRect);
}
else // creating new
{
auto baseSurfRotated = CSDL_Ext::newSurface(baseSurf->w, baseSurf->h);
if (!baseSurfRotated)
return;
Rect baseRect(0, 0, baseSurf->w, baseSurf->h);
CSDL_Ext::getBlitterWithRotationAndAlpha(targetSurf)(baseSurf, baseRect, baseSurfRotated, baseRect, rotation);
SDL_Surface * scaledSurf2 = CSDL_Ext::scaleSurfaceFast(baseSurfRotated, baseSurf->w * scale, baseSurf->h * scale);
CSDL_Ext::blitSurface(scaledSurf2, srcRect, targetSurf, dstRect);
parent->cache.cacheWorldViewEntry(type, key, scaledSurf2);
}
}
void CMapHandler::CMapWorldViewBlitter::drawElement(EMapCacheType cacheType, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect, bool alphaBlit, ui8 rotationInfo) const
{
if (rotationInfo != 0)
{
drawScaledRotatedElement(cacheType, sourceSurf, targetSurf, rotationInfo, info->scale, destRect, sourceRect);
}
else
{
auto scaledSurf = parent->cache.requestWorldViewCacheOrCreate(cacheType, (intptr_t) sourceSurf, sourceSurf, info->scale);
if (alphaBlit)
CSDL_Ext::blit8bppAlphaTo24bpp(scaledSurf, sourceRect, targetSurf, destRect);
else
CSDL_Ext::blitSurface(scaledSurf, sourceRect, targetSurf, destRect);
}
}
void CMapHandler::CMapWorldViewBlitter::drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
{
auto drawIcon = [this,targetSurf](Obj id, si32 subId, PlayerColor owner)
{
SDL_Surface * wvIcon = this->objectToIcon(id, subId, owner);
if (nullptr != wvIcon)
{
// centering icon on the object
Rect destRect(realPos.x + tileSize / 2 - wvIcon->w / 2, realPos.y + tileSize / 2 - wvIcon->h / 2, wvIcon->w, wvIcon->h);
CSDL_Ext::blitSurface(wvIcon, nullptr, targetSurf, &destRect);
}
};
auto & objects = tile.objects;
for(auto & object : objects)
{
const CGObjectInstance * obj = object.obj;
const bool sameLevel = obj->pos.z == pos.z;
const bool isVisible = (*info->visibilityMap)[pos.x][pos.y][pos.z];
const bool isVisitable = obj->visitableAt(pos.x, pos.y);
if(sameLevel && isVisible && isVisitable)
drawIcon(obj->ID, obj->subID, obj->tempOwner);
}
}
void CMapHandler::CMapWorldViewBlitter::drawOverlayEx(SDL_Surface * targetSurf)
{
if(nullptr == info->additionalIcons)
return;
const int3 bottomRight = pos + tileCount;
for(const ObjectPosInfo & iconInfo : *(info->additionalIcons))
{
if( iconInfo.pos.z != pos.z)
continue;
if((iconInfo.pos.x < topTile.x) || (iconInfo.pos.y < topTile.y))
continue;
if((iconInfo.pos.x > bottomRight.x) || (iconInfo.pos.y > bottomRight.y))
continue;
realPos.x = initPos.x + (iconInfo.pos.x - topTile.x) * tileSize;
realPos.y = initPos.x + (iconInfo.pos.y - topTile.y) * tileSize;
SDL_Surface * wvIcon = this->objectToIcon(iconInfo.id, iconInfo.subId, iconInfo.owner);
if (nullptr != wvIcon)
{
// centering icon on the object
Rect destRect(realPos.x + tileSize / 2 - wvIcon->w / 2, realPos.y + tileSize / 2 - wvIcon->h / 2, wvIcon->w, wvIcon->h);
CSDL_Ext::blitSurface(wvIcon, nullptr, targetSurf, &destRect);
}
}
}
void CMapHandler::CMapWorldViewBlitter::drawHeroFlag(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const
{
if (moving)
return;
CMapBlitter::drawHeroFlag(targetSurf, sourceSurf, sourceRect, destRect, false);
}
void CMapHandler::CMapWorldViewBlitter::drawObject(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, bool moving) const
{
if (moving)
return;
Rect scaledSourceRect(sourceRect->x * info->scale, sourceRect->y * info->scale, sourceRect->w, sourceRect->h);
CMapBlitter::drawObject(targetSurf, sourceSurf, &scaledSourceRect, false);
}
void CMapHandler::CMapBlitter::drawTileTerrain(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile2 & tile) const
{
Rect destRect(realTileRect);
if(tile.terbitmap) //if custom terrain graphic - use it
drawElement(EMapCacheType::TERRAIN_CUSTOM, tile.terbitmap, nullptr, targetSurf, &destRect);
else //use default terrain graphic
drawElement(EMapCacheType::TERRAIN, parent->terrainGraphics[tinfo.terType][tinfo.terView],
nullptr, targetSurf, &destRect, false, tinfo.extTileFlags % 4);
}
void CMapHandler::CMapWorldViewBlitter::init(const MapDrawingInfo * drawingInfo)
{
info = drawingInfo;
parent->cache.updateWorldViewScale(info->scale);
topTile = info->topTile;
tileSize = (int) floorf(32.0f * info->scale);
halfTileSizeCeil = (int)ceilf(tileSize / 2.0f);
tileCount.x = (int) ceilf((float)info->drawBounds->w / tileSize);
tileCount.y = (int) ceilf((float)info->drawBounds->h / tileSize);
initPos.x = info->drawBounds->x;
initPos.y = info->drawBounds->y;
realTileRect = Rect(initPos.x, initPos.y, tileSize, tileSize);
defaultTileRect = Rect(0, 0, tileSize, tileSize);
calculateWorldViewCameraPos();
}
SDL_Rect CMapHandler::CMapWorldViewBlitter::clip(SDL_Surface * targetSurf) const
{
SDL_Rect prevClip;
SDL_FillRect(targetSurf, info->drawBounds, SDL_MapRGB(targetSurf->format, 0, 0, 0));
// makes the clip area smaller if the map is smaller than the screen frame
// (actually, it could be made 1 tile bigger so that overlay icons on edge tiles could be drawn partly outside)
Rect clipRect(std::max<int>(info->drawBounds->x, info->drawBounds->x - topTile.x * tileSize),
std::max<int>(info->drawBounds->y, info->drawBounds->y - topTile.y * tileSize),
std::min<int>(info->drawBounds->w, parent->sizes.x * tileSize),
std::min<int>(info->drawBounds->h, parent->sizes.y * tileSize));
SDL_GetClipRect(targetSurf, &prevClip);
SDL_SetClipRect(targetSurf, &clipRect); //preventing blitting outside of that rect
return prevClip;
}
CMapHandler::CMapWorldViewBlitter::CMapWorldViewBlitter(CMapHandler * parent)
: CMapBlitter(parent)
{
}
void CMapHandler::CMapPuzzleViewBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
{
CMapBlitter::drawObjects(targetSurf, tile);
// grail X mark
if(pos.x == info->grailPos.x && pos.y == info->grailPos.y)
{
Rect destRect(realTileRect);
CSDL_Ext::blit8bppAlphaTo24bpp(graphics->heroMoveArrows->ourImages[0].bitmap, nullptr, targetSurf, &destRect);
}
}
void CMapHandler::CMapPuzzleViewBlitter::postProcessing(SDL_Surface * targetSurf) const
{
CSDL_Ext::applyEffect(targetSurf, info->drawBounds, static_cast<int>(!ADVOPT.puzzleSepia));
}
bool CMapHandler::CMapPuzzleViewBlitter::canDrawObject(const CGObjectInstance * obj) const
{
if (!CMapBlitter::canDrawObject(obj))
return false;
//don't print flaggable objects in puzzle mode
if (obj->isVisitable())
return false;
if(std::find(unblittableObjects.begin(), unblittableObjects.end(), obj->ID) != unblittableObjects.end())
return false;
return true;
}
CMapHandler::CMapPuzzleViewBlitter::CMapPuzzleViewBlitter(CMapHandler * parent)
: CMapNormalBlitter(parent)
{
unblittableObjects.push_back(Obj::HOLE);
}
void CMapHandler::CMapBlitter::drawFrame(SDL_Surface * targetSurf) const
{
Rect destRect(realTileRect);
drawElement(EMapCacheType::FRAME, parent->ttiles[pos.x][pos.y][topTile.z].terbitmap, nullptr, targetSurf, &destRect);
}
void CMapHandler::CMapBlitter::drawOverlayEx(SDL_Surface * targetSurf)
{
//nothing to do here
}
void CMapHandler::CMapBlitter::drawHeroFlag(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const
{
drawElement(EMapCacheType::HERO_FLAGS, sourceSurf, sourceRect, targetSurf, destRect, false);
}
void CMapHandler::CMapBlitter::drawObject(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, bool moving) const
{
Rect dstRect(realTileRect);
drawElement(EMapCacheType::OBJECTS, sourceSurf, sourceRect, targetSurf, &dstRect, true);
}
void CMapHandler::CMapBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
{
auto & objects = tile.objects;
for(auto & object : objects)
{
if (object.fadeAnimKey >= 0)
{
auto fadeIter = parent->fadeAnims.find(object.fadeAnimKey);
if (fadeIter != parent->fadeAnims.end())
{
// this object is currently fading, so skip normal drawing
Rect r2(realTileRect);
CFadeAnimation * fade = (*fadeIter).second.second;
fade->draw(targetSurf, nullptr, &r2);
continue;
}
logGlobal->errorStream() << "Fading map object with missing fade anim : " << object.fadeAnimKey;
continue;
}
const CGObjectInstance * obj = object.obj;
if (!obj)
{
logGlobal->errorStream() << "Stray map object that isn't fading";
continue;
}
if (!graphics->getDef(obj))
processDef(obj->appearance);
if (!graphics->getDef(obj) && !obj->appearance.animationFile.empty())
logGlobal->errorStream() << "Failed to load image " << obj->appearance.animationFile;
if (!canDrawObject(obj))
continue;
auto objData = findObjectBitmap(obj, info->anim);
if (objData.objBitmap)
{
Rect srcRect(object.rect.x, object.rect.y, tileSize, tileSize);
drawObject(targetSurf, objData.objBitmap, &srcRect, objData.isMoving);
if (objData.flagBitmap)
{
if (objData.isMoving)
{
srcRect.y += FRAMES_PER_MOVE_ANIM_GROUP * 2 - tileSize;
Rect dstRect(realPos.x, realPos.y - tileSize / 2, tileSize, tileSize);
drawHeroFlag(targetSurf, objData.flagBitmap, &srcRect, &dstRect, true);
}
else if (obj->pos.x == pos.x && obj->pos.y == pos.y)
{
Rect dstRect(realPos.x - 2 * tileSize, realPos.y - tileSize, 3 * tileSize, 2 * tileSize);
drawHeroFlag(targetSurf, objData.flagBitmap, nullptr, &dstRect, false);
}
}
}
}
}
void CMapHandler::CMapBlitter::drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const
{
if (tinfoUpper && tinfoUpper->roadType != ERoadType::NO_ROAD)
{
Rect source(0, tileSize / 2, tileSize, tileSize / 2);
Rect dest(realPos.x, realPos.y, tileSize, tileSize / 2);
drawElement(EMapCacheType::ROADS, parent->roadDefs[tinfoUpper->roadType - 1]->ourImages[tinfoUpper->roadDir].bitmap,
&source, targetSurf, &dest, true, (tinfoUpper->extTileFlags >> 4) % 4);
}
if(tinfo.roadType != ERoadType::NO_ROAD) //print road from this tile
{
Rect source(0, 0, tileSize, halfTileSizeCeil);
Rect dest(realPos.x, realPos.y + tileSize / 2, tileSize, tileSize / 2);
drawElement(EMapCacheType::ROADS, parent->roadDefs[tinfo.roadType - 1]->ourImages[tinfo.roadDir].bitmap,
&source, targetSurf, &dest, true, (tinfo.extTileFlags >> 4) % 4);
}
}
void CMapHandler::CMapBlitter::drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const
{
Rect destRect(realTileRect);
drawElement(EMapCacheType::RIVERS, parent->staticRiverDefs[tinfo.riverType-1]->ourImages[tinfo.riverDir].bitmap,
nullptr, targetSurf, &destRect, true, (tinfo.extTileFlags >> 2) % 4);
}
void CMapHandler::CMapBlitter::drawFow(SDL_Surface * targetSurf) const
{
Rect destRect(realTileRect);
std::pair<SDL_Surface *, bool> hide = getVisBitmap();
drawElement(EMapCacheType::FOW, hide.first, nullptr, targetSurf, &destRect, hide.second);
}
void CMapHandler::CMapBlitter::blit(SDL_Surface * targetSurf, const MapDrawingInfo * info)
{
init(info);
auto prevClip = clip(targetSurf);
pos = int3(0, 0, topTile.z);
for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
{
if (pos.x < 0 || pos.x >= parent->sizes.x)
continue;
for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
{
if (pos.y < 0 || pos.y >= parent->sizes.y)
continue;
const bool isVisible = canDrawCurrentTile();
realTileRect.x = realPos.x;
realTileRect.y = realPos.y;
const TerrainTile2 & tile = parent->ttiles[pos.x][pos.y][pos.z];
const TerrainTile & tinfo = parent->map->getTile(pos);
const TerrainTile * tinfoUpper = pos.y > 0 ? &parent->map->getTile(int3(pos.x, pos.y - 1, pos.z)) : nullptr;
if(isVisible || info->showAllTerrain)
{
drawTileTerrain(targetSurf, tinfo, tile);
if (tinfo.riverType)
drawRiver(targetSurf, tinfo);
drawRoad(targetSurf, tinfo, tinfoUpper);
}
if(isVisible)
drawObjects(targetSurf, tile);
}
}
for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
{
for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
{
realTileRect.x = realPos.x;
realTileRect.y = realPos.y;
if (pos.x < 0 || pos.x >= parent->sizes.x ||
pos.y < 0 || pos.y >= parent->sizes.y)
{
drawFrame(targetSurf);
}
else
{
const TerrainTile2 & tile = parent->ttiles[pos.x][pos.y][pos.z];
if (!(*info->visibilityMap)[pos.x][pos.y][topTile.z] && !info->showAllTerrain)
drawFow(targetSurf);
// overlay needs to be drawn over fow, because of artifacts-aura-like spells
drawTileOverlay(targetSurf, tile);
// drawDebugVisitables()
if (settings["session"]["showBlock"].Bool())
{
if(parent->map->getTile(int3(pos.x, pos.y, pos.z)).blocked) //temporary hiding blocked positions
{
static SDL_Surface * block = nullptr;
if (!block)
block = BitmapHandler::loadBitmap("blocked");
CSDL_Ext::blitSurface(block, nullptr, targetSurf, &realTileRect);
}
}
if (settings["session"]["showVisit"].Bool())
{