Repository navigation
Expand file tree
/
Copy pathMap.cpp
More file actions
940 lines (849 loc) · 23.2 KB
/
Copy pathMap.cpp
File metadata and controls
940 lines (849 loc) · 23.2 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
/*
* Copyright 2010 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenXcom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#define _USE_MATH_DEFINES
#include <cmath>
#include <fstream>
#include "Map.h"
#include "UnitSprite.h"
#include "Position.h"
#include "Pathfinding.h"
#include "TerrainModifier.h"
#include "../Resource/ResourcePack.h"
#include "../Engine/Action.h"
#include "../Engine/SurfaceSet.h"
#include "../Engine/Timer.h"
#include "../Engine/Font.h"
#include "../Engine/Language.h"
#include "../Engine/Palette.h"
#include "../Engine/Game.h"
#include "../Savegame/SavedBattleGame.h"
#include "../Savegame/Tile.h"
#include "../Savegame/BattleUnit.h"
#include "../Ruleset/MapDataSet.h"
#include "../Ruleset/MapData.h"
#include "../Interface/Text.h"
#include "../Interface/Cursor.h"
#define SCROLL_AMOUNT 20
#define SCROLL_BORDER 5
#define SCROLL_DIAGONAL_EDGE 60
#define DEFAULT_ANIM_SPEED 100
#define RMB_SCROLL false
/*
1) Map origin is left corner.
2) X axis goes downright. (width of the map)
3) Y axis goes upright. (length of the map
4) Z axis goes up (height of the map)
y+
/\
0,0/ \
\ /
\/
x+
*/
namespace OpenXcom
{
/**
* Sets up a map with the specified size and position.
* @param width Width in pixels.
* @param height Height in pixels.
* @param x X position in pixels.
* @param y Y position in pixels.
*/
Map::Map(int width, int height, int x, int y) : InteractiveSurface(width, height, x, y), _mapOffsetX(-250), _mapOffsetY(250), _viewHeight(0), _hideCursor(false), _animFrame(0), _scrollX(0), _scrollY(0), _RMBDragging(false)
{
_scrollTimer = new Timer(50);
_scrollTimer->onTimer((SurfaceHandler)&Map::scroll);
_animTimer = new Timer(DEFAULT_ANIM_SPEED);
_animTimer->onTimer((SurfaceHandler)&Map::animate);
_animTimer->start();
}
/**
* Deletes the map.
*/
Map::~Map()
{
delete _scrollTimer;
delete _animTimer;
for (int i = 0; i < _tileCount; i++)
{
delete _tileFloorCache[i];
delete _tileWallsCache[i];
}
delete[] _tileFloorCache;
delete[] _tileWallsCache;
for (std::vector<Surface*>::iterator i = _unitCache.begin(); i != _unitCache.end(); i++)
{
delete *i;
}
delete _arrow;
}
/**
* Changes the pack for the map to get resources for rendering.
* @param res Pointer to the resource pack.
*/
void Map::setResourcePack(ResourcePack *res)
{
_res = res;
_spriteWidth = res->getSurfaceSet("BLANKS.PCK")->getFrame(0)->getWidth();
_spriteHeight = res->getSurfaceSet("BLANKS.PCK")->getFrame(0)->getHeight();
}
/**
* Changes the saved game content for the map to render.
* @param save Pointer to the saved game.
* @param game Pointer to the Game.
*/
void Map::setSavedGame(SavedBattleGame *save, Game *game)
{
_save = save;
_game = game;
_tileCount = _save->getHeight() * _save->getLength() * _save->getWidth();
_tileFloorCache = new Surface*[_tileCount];
_tileWallsCache = new Surface*[_tileCount];
_unitCache.clear();
for (int i = 0; i < _tileCount; i++)
{
_tileFloorCache[i] = 0;
_tileWallsCache[i] = 0;
}
for (std::vector<BattleUnit*>::iterator i = _save->getUnits()->begin(); i != _save->getUnits()->end(); i++)
{
_unitCache.push_back(0);
}
for (std::vector<MapDataSet*>::const_iterator i = _save->getMapDataSets()->begin(); i != _save->getMapDataSets()->end(); i++)
{
(*i)->getSurfaceset()->setPalette(this->getPalette());
}
}
/**
* initializes stuff
*/
void Map::init()
{
// load the tiny arrow into a surface
int f = Palette::blockOffset(1)+1; // yellow
int b = 15; // black
int pixels[81] = { 0, 0, b, b, b, b, b, 0, 0,
0, 0, b, f, f, f, b, 0, 0,
0, 0, b, f, f, f, b, 0, 0,
b, b, b, f, f, f, b, b, b,
b, f, f, f, f, f, f, f, b,
0, b, f, f, f, f, f, b, 0,
0, 0, b, f, f, f, b, 0, 0,
0, 0, 0, b, f, b, 0, 0, 0,
0, 0, 0, 0, b, 0, 0, 0, 0 };
_arrow = new Surface(9, 9);
_arrow->setPalette(this->getPalette());
_arrow->lock();
for (int y = 0; y < 9;y++)
for (int x = 0; x < 9; x++)
_arrow->setPixel(x, y, pixels[x+(y*9)]);
_arrow->unlock();
}
/**
* Keeps the animation timers running.
*/
void Map::think()
{
_scrollTimer->think(0, this);
_animTimer->think(0, this);
}
/**
* Draws the whole map, part by part.
*/
void Map::draw()
{
clear();
drawTerrain();
}
/**
* Draw the terrain.
*/
void Map::drawTerrain()
{
int frameNumber = 0;
Surface *frame;
Tile *tile;
int beginX = 0, endX = _save->getWidth() - 1;
int beginY = 0, endY = _save->getLength() - 1;
int beginZ = 0, endZ = _viewHeight;
Position mapPosition, screenPosition;
int index;
int cacheCount = 0;
for (int itZ = beginZ; itZ <= endZ; itZ++)
{
for (int itX = beginX; itX <= endX; itX++)
{
for (int itY = endY; itY >= beginY; itY--)
{
mapPosition = Position(itX, itY, itZ);
convertMapToScreen(mapPosition, &screenPosition);
screenPosition.x += _mapOffsetX;
screenPosition.y += _mapOffsetY;
// only render cells that are inside the viewport
if (screenPosition.x > -_spriteWidth && screenPosition.x < getWidth() + _spriteWidth &&
screenPosition.y > -_spriteHeight && screenPosition.y < getHeight() + _spriteHeight )
{
index = _save->getTileIndex(mapPosition); // index used for tile cache
// to minize framedrops, only recalculate x tiles per draw.
if (cacheCount < 10)
{
if (cacheTileSprites(index))
cacheCount++;
}
tile = _save->getTile(mapPosition);
// Draw floor
if (tile)
{
frame = _tileFloorCache[index];
if (frame)
{
frame->setX(screenPosition.x);
frame->setY(screenPosition.y);
frame->blit(this);
}
}
BattleUnit *unit = tile->getUnit();
// Draw cursor back
if (_selectorX == itY && _selectorY == itX && !_hideCursor)
{
if (_viewHeight == itZ)
{
if (unit)
frameNumber = 1; // yellow box
else
frameNumber = 0; // red box
}
else if (_viewHeight > itZ)
{
frameNumber = 2; // blue box
}
frame = _res->getSurfaceSet("CURSOR.PCK")->getFrame(frameNumber);
frame->setX(screenPosition.x);
frame->setY(screenPosition.y);
frame->blit(this);
}
// Draw walls
if (tile)
{
frame = _tileWallsCache[index];
if (frame)
{
frame->setX(screenPosition.x);
frame->setY(screenPosition.y);
frame->blit(this);
}
}
// Draw items
// Draw soldier
if (unit)
{
frame = _unitCache.at(unit->getId());
if (frame)
{
Position offset;
calculateWalkingOffset(unit, &offset);
frame->setX(screenPosition.x + offset.x);
frame->setY(screenPosition.y + offset.y);
frame->blit(this);
if (unit == (BattleUnit*)_save->getSelectedUnit() && !_hideCursor)
{
drawArrow(screenPosition + offset);
}
}
}
// if we can see through the floor, draw the soldier below it if it is on stairs
if (itZ > 0 && tile->hasNoFloor())
{
unit = _save->selectUnit(mapPosition + Position(0, 0, -1));
tile = _save->getTile(mapPosition + Position(0, 0, -1));
if (unit && tile->getTerrainLevel() < 0)
{
frame = _unitCache.at(unit->getId());
if (frame)
{
Position offset;
calculateWalkingOffset(unit, &offset);
offset.y += 24;
frame->setX(screenPosition.x + offset.x);
frame->setY(screenPosition.y + offset.y);
frame->blit(this);
if (unit == (BattleUnit*)_save->getSelectedUnit() && !_hideCursor)
{
drawArrow(screenPosition + offset);
}
}
}
}
// Draw cursor front
if (_selectorX == itY && _selectorY == itX && !_hideCursor)
{
if (_viewHeight == itZ)
{
if (unit)
frameNumber = 4; // yellow box
else
frameNumber = 3; // red box
}
else if (_viewHeight > itZ)
{
frameNumber = 5; // blue box
}
frame = _res->getSurfaceSet("CURSOR.PCK")->getFrame(frameNumber);
frame->setX(screenPosition.x);
frame->setY(screenPosition.y);
frame->blit(this);
}
// Draw smoke/fire
if (tile->getFire() && tile->isDiscovered())
{
frameNumber = 0; // see http://www.ufopaedia.org/images/c/cb/Smoke.gif
frame = _res->getSurfaceSet("SMOKE.PCK")->getFrame(frameNumber + (_animFrame / 2));
frame->setX(screenPosition.x);
frame->setY(screenPosition.y);
frame->blit(this);
}
}
}
}
}
}
/**
* Handles map mouse shortcuts.
* @param action Pointer to an action.
* @param state State that the action handlers belong to.
*/
void Map::mouseClick(Action *action, State *state)
{
InteractiveSurface::mouseClick(action, state);
if (action->getDetails()->button.button == SDL_BUTTON_WHEELUP)
{
up();
}
else if (action->getDetails()->button.button == SDL_BUTTON_WHEELDOWN)
{
down();
}
}
/**
* Handles map keyboard shortcuts.
* @param action Pointer to an action.
* @param state State that the action handlers belong to.
*/
void Map::keyboardPress(Action *action, State *state)
{
Position pos;
getSelectorPosition(&pos);
InteractiveSurface::keyboardPress(action, state);
// "d" - destroys all objects on a tile (for testing purposes)
if (action->getDetails()->key.keysym.sym == SDLK_d)
{
_save->getTerrainModifier()->destroyTile(_save->getTile(pos));
}
// "f" - puts a tile on fire (for testing purposes)
if (action->getDetails()->key.keysym.sym == SDLK_f)
{
_save->getTile(pos)->setFire(1);
_save->getTerrainModifier()->calculateLighting();
}
}
/**
* Handles mouse over events.
* @param action Pointer to an action.
* @param state State that the action handlers belong to.
*/
void Map::mouseOver(Action *action, State *state)
{
int posX = action->getDetails()->motion.x;
int posY = action->getDetails()->motion.y;
// handle RMB dragging
if ((action->getDetails()->motion.state & SDL_BUTTON(SDL_BUTTON_RIGHT)) && RMB_SCROLL)
{
_RMBDragging = true;
_scrollX = (int)(-(double)(_RMBClickX - posX) * (action->getXScale() * 2));
_scrollY = (int)(-(double)(_RMBClickY - posY) * (action->getYScale() * 2));
_RMBClickX = posX;
_RMBClickY = posY;
}
else
{
// handle scrolling with mouse at edge of screen
if (posX < (SCROLL_BORDER * action->getXScale()) && posX > 0)
{
_scrollX = SCROLL_AMOUNT;
// if close to top or bottom, also scroll diagonally
if (posY < (SCROLL_DIAGONAL_EDGE * action->getYScale()) && posY > 0)
{
_scrollY = SCROLL_AMOUNT;
}
else if (posY > (getHeight() - SCROLL_DIAGONAL_EDGE) * action->getYScale())
{
_scrollY = -SCROLL_AMOUNT;
}
}
else if (posX > (getWidth() - SCROLL_BORDER) * action->getXScale())
{
_scrollX = -SCROLL_AMOUNT;
// if close to top or bottom, also scroll diagonally
if (posY < (SCROLL_DIAGONAL_EDGE * action->getYScale()) && posY > 0)
{
_scrollY = SCROLL_AMOUNT;
}
else if (posY > (getHeight() - SCROLL_DIAGONAL_EDGE) * action->getYScale())
{
_scrollY = -SCROLL_AMOUNT;
}
}
else if (posX)
{
_scrollX = 0;
}
if (posY < (SCROLL_BORDER * action->getYScale()) && posY > 0)
{
_scrollY = SCROLL_AMOUNT;
// if close to left or right edge, also scroll diagonally
if (posX < (SCROLL_DIAGONAL_EDGE * action->getXScale()) && posX > 0)
{
_scrollX = SCROLL_AMOUNT;
}
else if (posX > (getWidth() - SCROLL_DIAGONAL_EDGE) * action->getXScale())
{
_scrollX = -SCROLL_AMOUNT;
}
}
else if (posY > (getHeight() - SCROLL_BORDER) * action->getYScale())
{
_scrollY = -SCROLL_AMOUNT;
// if close to left or right edge, also scroll diagonally
if (posX < (SCROLL_DIAGONAL_EDGE * action->getXScale()) && posX > 0)
{
_scrollX = SCROLL_AMOUNT;
}
else if (posX > (getWidth() - SCROLL_DIAGONAL_EDGE) * action->getXScale())
{
_scrollX = -SCROLL_AMOUNT;
}
}
else if (posY && _scrollX == 0)
{
_scrollY = 0;
}
}
if ((_scrollX || _scrollY) && !_scrollTimer->isRunning())
{
_scrollTimer->start();
}
else if ((!_scrollX && !_scrollY) && _scrollTimer->isRunning())
{
_scrollTimer->stop();
}
setSelectorPosition((int)((double)posX / action->getXScale()), (int)((double)posY / action->getYScale()));
}
/**
* Sets the value to min if it is below min, sets value to max if above max.
* @param value pointer to the value
* @param minimum value
* @param maximum value
*/
void Map::minMaxInt(int *value, const int minValue, const int maxValue)
{
if (*value < minValue)
{
*value = minValue;
}
else if (*value > maxValue)
{
*value = maxValue;
}
}
/**
* Sets the selector to a certain tile on the map.
* @param mx mouse x position
* @param my mouse y position
*/
void Map::setSelectorPosition(int mx, int my)
{
int oldX = _selectorX, oldY = _selectorY;
if (!mx && !my) return; // cursor is offscreen
convertScreenToMap(mx, my, &_selectorX, &_selectorY);
minMaxInt(&_selectorX, 0, _save->getWidth() - 1);
minMaxInt(&_selectorY, 0, _save->getLength() - 1);
if (oldX != _selectorX || oldY != _selectorY)
{
draw();
}
}
void Map::convertScreenToMap(int screenX, int screenY, int *mapX, int *mapY)
{
// add half a tileheight to the mouseposition per layer we are above the floor
screenY += -_spriteHeight + (_viewHeight + 1) * (_spriteHeight / 2);
// calculate the actual x/y pixelposition on a diamond shaped map
// taking the view offset into account
*mapX = screenX - _mapOffsetX - 2 * screenY + 2 * _mapOffsetY;
*mapY = screenY - _mapOffsetY + *mapX / 4;
// to get the row&col itself, divide by the size of a tile
*mapY /= (_spriteWidth / 4);
*mapX /= _spriteWidth;
}
/**
* Handle scrolling.
*/
void Map::scroll()
{
_mapOffsetX += _scrollX;
_mapOffsetY += _scrollY;
convertScreenToMap((getWidth() / 2), (BUTTONS_AREA / 2), &_centerX, &_centerY);
// if center goes out of map bounds, hold the scrolling (may need further tweaking)
if (_centerX > _save->getWidth() - 1 || _centerY > _save->getLength() - 1 || _centerX < 0 || _centerY < 0)
{
_mapOffsetX -= _scrollX;
_mapOffsetY -= _scrollY;
}
if (_RMBDragging)
{
_RMBDragging = false;
_scrollX = 0;
_scrollY = 0;
}
draw();
}
/**
* Handle animating tiles/units/bullets. 8 Frames per animation.
*/
void Map::animate()
{
_animFrame = _animFrame == 7 ? 0 : _animFrame+1;
for (int i = 0; i < _tileCount; i++)
{
_save->getTiles()[i]->animate();
}
cacheTileSprites();
draw();
}
/**
* Go one level up.
*/
void Map::up()
{
if (_viewHeight < _save->getHeight() - 1)
{
_viewHeight++;
_mapOffsetY += _spriteHeight / 2;
draw();
}
}
/**
* Go one level down.
*/
void Map::down()
{
if (_viewHeight > 0)
{
_viewHeight--;
_mapOffsetY -= _spriteHeight / 2;
draw();
}
}
/**
* Set viewheight.
* @param viewheight
*/
void Map::setViewHeight(int viewheight)
{
_viewHeight = viewheight;
minMaxInt(&_viewHeight, 0, _save->getHeight()-1);
draw();
}
/**
* Center map on a certain position.
* @param mapPos Position to center on.
*/
void Map::centerOnPosition(const Position &mapPos)
{
Position screenPos;
convertMapToScreen(mapPos, &screenPos);
_mapOffsetX = -(screenPos.x - (getWidth() / 2));
_mapOffsetY = -(screenPos.y - (BUTTONS_AREA / 2));
convertScreenToMap((getWidth() / 2), (BUTTONS_AREA / 2), &_centerX, &_centerY);
_viewHeight = mapPos.z;
draw();
}
/**
* Convert map coordinates X,Y,Z to screen positions X, Y.
* @param mapPos X,Y,Z coordinates on the map.
* @param screenPos to screen position.
*/
void Map::convertMapToScreen(const Position &mapPos, Position *screenPos)
{
screenPos->z = 0; // not used
screenPos->x = mapPos.x * (_spriteWidth / 2) + mapPos.y * (_spriteWidth / 2);
screenPos->y = mapPos.x * (_spriteWidth / 4) - mapPos.y * (_spriteWidth / 4) - mapPos.z * ((_spriteHeight + _spriteWidth / 4) / 2);
}
/**
* Draws the small arrow above the selected soldier.
* @param screenPos
*/
void Map::drawArrow(const Position &screenPos)
{
_arrow->setX(screenPos.x + (_spriteWidth / 2) - (_arrow->getWidth() / 2));
_arrow->setY(screenPos.y - _arrow->getHeight() + _animFrame);
_arrow->blit(this);
}
/**
* Draws the small arrow above the selected soldier.
* @param pos pointer to a position
*/
void Map::getSelectorPosition(Position *pos)
{
// don't know why X and Y are inverted here...
pos->x = _selectorY;
pos->y = _selectorX;
pos->z = _viewHeight;
}
/**
* Calculate the offset of a soldier, when it is walking in the middle of 2 tiles.
* @param unit pointer to BattleUnit
* @param offset pointer to the offset to return the calculation.
*/
void Map::calculateWalkingOffset(BattleUnit *unit, Position *offset)
{
int offsetX[8] = { 1, 2, 1, 0, -1, -2, -1, 0 };
int offsetY[8] = { 1, 0, -1, -2, -1, 0, 1, 2 };
int phase = unit->getWalkingPhase();
int dir = unit->getDirection();
if (phase)
{
if (phase < 4)
{
offset->x = phase * 2 * offsetX[dir];
offset->y = - phase * offsetY[dir];
}
else
{
offset->x = (phase - 8) * 2 * offsetX[dir];
offset->y = - (phase - 8) * offsetY[dir];
}
}
// If we are walking in between tiles, interpolate it's terrain level.
if (phase)
{
if (phase < 4)
{
int fromLevel = _save->getTile(unit->getPosition())->getTerrainLevel();
int toLevel = _save->getTile(unit->getDestination())->getTerrainLevel();
if (unit->getPosition().z > unit->getDestination().z)
{
// going down a level, so toLevel 0 becomes +24, -8 becomes 16
toLevel += 24*(unit->getPosition().z - unit->getDestination().z);
}else if (unit->getPosition().z < unit->getDestination().z)
{
// going up a level, so toLevel 0 becomes -24, -8 becomes -16
toLevel = -24*(unit->getDestination().z - unit->getPosition().z) + abs(toLevel);
}
offset->y += ((fromLevel * (8 - phase)) / 8) + ((toLevel * (phase)) / 8);
}
else
{
// from phase 4 onwards the unit behind the scenes already is on the destination tile
// we have to get it's last position to calculate the correct offset
int fromLevel = _save->getTile(unit->getLastPosition())->getTerrainLevel();
int toLevel = _save->getTile(unit->getDestination())->getTerrainLevel();
if (unit->getLastPosition().z > unit->getDestination().z)
{
// going down a level, so fromLevel 0 becomes -24, -8 becomes -32
fromLevel -= 24*(unit->getLastPosition().z - unit->getDestination().z);
}else if (unit->getLastPosition().z < unit->getDestination().z)
{
// going up a level, so fromLevel 0 becomes +24, -8 becomes 16
fromLevel = -24*(unit->getDestination().z - unit->getLastPosition().z) + abs(fromLevel);
}
offset->y += ((fromLevel * (8 - phase)) / 8) + ((toLevel * (phase)) / 8);
}
}
else
{
offset->y += _save->getTile(unit->getPosition())->getTerrainLevel();
}
}
/**
* This removes the selection caret and soldier selection arrow.
* @param flag
*/
void Map::hideCursor(bool flag)
{
_hideCursor = flag;
}
/**
* Check if cursor is hidden.
* @return flag
*/
bool Map::isCursorHidden()
{
return _hideCursor;
}
void Map::cacheTileSprites()
{
for (int i = 0; i < _tileCount; i++)
{
cacheTileSprites(i);
}
}
/**
* Caches tile's sprites.
*/
bool Map::cacheTileSprites(int i)
{
MapData *object = 0;
Surface *frame = 0;
Tile *tile = _save->getTiles()[i];
bool door = false;
if(tile && !tile->isCached())
{
/* draw a floor object on the cache (if any) */
object = tile->getMapData(O_FLOOR);
if (object)
{
if (_tileFloorCache[i] == 0)
{
_tileFloorCache[i] = new Surface(_spriteWidth, _spriteHeight);
_tileFloorCache[i]->setPalette(this->getPalette());
}
else
{
_tileFloorCache[i]->clear();
}
// Draw floor
frame = tile->getSprite(O_FLOOR);
frame->setX(0);
frame->setY(-object->getYOffset());
frame->blit(_tileFloorCache[i]);
_tileFloorCache[i]->setShade(tile->isDiscovered()?tile->getShade():16);
}
else if (_tileFloorCache[i] != 0)
{
_tileFloorCache[i]->clear();
}
/* draw terrain objects on the cache (if any) */
if (tile->getMapData(O_WESTWALL) != 0 || tile->getMapData(O_NORTHWALL) != 0 || tile->getMapData(O_OBJECT) != 0)
{
if (_tileWallsCache[i] == 0)
{
_tileWallsCache[i] = new Surface(_spriteWidth, _spriteHeight);
_tileWallsCache[i]->setPalette(this->getPalette());
}
else
{
_tileWallsCache[i]->clear();
}
// Draw west wall
object = tile->getMapData(O_WESTWALL);
if (object)
{
frame = tile->getSprite(O_WESTWALL);
frame->setX(0);
frame->setY(-object->getYOffset());
frame->blit(_tileWallsCache[i]);
door = object->isDoor() || object->isUFODoor();
}
// Draw north wall
object = tile->getMapData(O_NORTHWALL);
if (object)
{
frame = tile->getSprite(O_NORTHWALL);
frame->setX(0);
frame->setY(-object->getYOffset());
// if there is a westwall, cut off some of the north wall (otherwise it will overlap)
if (tile->getMapData(O_WESTWALL))
{
frame->setX(frame->getWidth() / 2);
frame->getCrop()->x = frame->getWidth() / 2;
frame->getCrop()->w = frame->getWidth() / 2;
frame->getCrop()->h = frame->getHeight();
}else
{
frame->getCrop()->w = 0;
frame->getCrop()->h = 0;
}
frame->blit(_tileWallsCache[i]);
door = object->isDoor() || object->isUFODoor();
}
// Draw object
object = tile->getMapData(O_OBJECT);
if (object)
{
frame = tile->getSprite(O_OBJECT);
frame->setX(0);
frame->setY(-object->getYOffset());
frame->blit(_tileWallsCache[i]);
}
if (door && tile->getShade() > 8 && tile->isDiscovered()) // don't shade doors too dark, so you can still see them
{
_tileWallsCache[i]->setShade(8);
}
else
{
_tileWallsCache[i]->setShade(tile->isDiscovered()?tile->getShade():16);
}
}else if (_tileWallsCache[i] != 0)
{
_tileWallsCache[i]->clear();
}
// if lighting changed for a tile, then it also does for a unit standing on it
if (tile->getUnit() != 0 && tile->getUnit()->getFaction() != FACTION_PLAYER)
{
tile->getUnit()->setCached(false);
}
tile->setCached(true);
return true;
}
else
{
return false;
}
}
void Map::cacheUnits()
{
UnitSprite *unitSprite = new UnitSprite(_spriteWidth, _spriteHeight, 0, 0);
unitSprite->setResourcePack(_res);
unitSprite->setPalette(this->getPalette());
for (std::vector<BattleUnit*>::iterator i = _save->getUnits()->begin(); i != _save->getUnits()->end(); i++)
{
if (!(*i)->isCached())
{
if (_unitCache.at((*i)->getId()) == 0)
{
_unitCache.at((*i)->getId()) = new Surface(_spriteWidth, _spriteHeight);
_unitCache.at((*i)->getId())->setPalette(this->getPalette());
}
else
{
_unitCache.at((*i)->getId())->clear();
}
unitSprite->setBattleUnit((*i));
unitSprite->draw();
unitSprite->blit(_unitCache.at((*i)->getId()));
// non player units get shaded according to the tile's shade
if ((*i)->getFaction() != FACTION_PLAYER)
{
_unitCache.at((*i)->getId())->setShade(_save->getTile((*i)->getPosition())->getShade());
}
}
}
delete unitSprite;
}
}