Repository navigation
Expand file tree
/
Copy pathMap.cpp
More file actions
704 lines (619 loc) · 16.6 KB
/
Copy pathMap.cpp
File metadata and controls
704 lines (619 loc) · 16.6 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
/*
* 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 "../Resource/TerrainObjectSet.h"
#include "../Resource/TerrainObject.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/GameTime.h"
#include "../Savegame/Craft.h"
#include "../Savegame/Ufo.h"
#include "../Savegame/Tile.h"
#include "../Savegame/BattleUnit.h"
#include "../Savegame/BattleSoldier.h"
#include "../Ruleset/RuleTerrain.h"
#include "../Ruleset/RuleCraft.h"
#include "../Ruleset/RuleUfo.h"
#include "../Interface/Text.h"
#include "../Interface/Cursor.h"
#define SCROLL_AMOUNT 8
#define SCROLL_BORDER 5
#define DEFAULT_ANIM_SPEED 100
#define DEFAULT_WALK_SPEED 50
#define DEFAULT_BULLET_SPEED 20
/*
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+
*/
/**
* 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)
{
_scrollTimer = new Timer(50);
_scrollTimer->onTimer((SurfaceHandler)&Map::scroll);
_animTimer = new Timer(DEFAULT_ANIM_SPEED);
_animTimer->onTimer((SurfaceHandler)&Map::animate);
_animTimer->start();
_walkingTimer = new Timer(DEFAULT_WALK_SPEED);
_walkingTimer->onTimer((SurfaceHandler)&Map::moveUnit);
_walkingTimer->start();
_bulletTimer = new Timer(DEFAULT_BULLET_SPEED);
_bulletTimer->onTimer((SurfaceHandler)&Map::moveBullet);
_bulletTimer->start();
_animFrame = 0;
_ScrollX = 0;
_ScrollY = 0;
_RMBDragging = false;
}
/**
* Deletes the map.
*/
Map::~Map()
{
delete _scrollTimer;
delete _animTimer;
delete _walkingTimer;
delete _bulletTimer;
}
/**
* 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.
*/
void Map::setSavedGame(SavedBattleGame *save, Game *game)
{
_save = save;
_game = game;
}
/**
* 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);
_walkingTimer->think(0, this);
_bulletTimer->think(0, this);
}
/**
* Draws the whole map, part by part.
*/
void Map::draw()
{
clear();
drawTerrain();
}
/**
* Gets one single surface of the compiled list of surfaces.
* @param tobID the TerrainObjectID index of the object
* @param frame the animation frame (0-7)
* @return pointer to a surface
*/
Surface *Map::getSurface(TerrainObject *tob, int frame)
{
// UFO doors are only animated when walked through
if (tob->isUFODoor())
{
frame = 0;
}
return tob->getSprite(frame);
}
/**
* Draw the terrain.
*/
void Map::drawTerrain()
{
int frameNumber = 0;
TerrainObject *object = 0;
Surface *frame;
Tile *tile;
int beginX = 0, endX = _save->getWidth() - 1;
int beginY = 0, endY = _save->getLength() - 1;
int beginZ = 0, endZ = _viewHeight;
UnitSprite *unitSprite = new UnitSprite(32, 40, 0, 0);
unitSprite->setResourcePack(_res);
unitSprite->setPalette(this->getPalette());
Position mapPosition, screenPosition;
for (int itZ = beginZ; itZ <= endZ; itZ++)
{
// Draw remaining sprites
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.x;
screenPosition.y = _MapOffsetY + screenPosition.y;
// only render cells that are inside the viewport
if (screenPosition.x > -_SpriteWidth && screenPosition.x < getWidth() + _SpriteWidth &&
screenPosition.y > -_SpriteHeight && screenPosition.y < getHeight() + _SpriteHeight )
{
BattleUnit *unit = _save->selectUnit(mapPosition);
// Draw floor
tile = _save->getTile(mapPosition);
if (tile)
{
for (int part=0;part<1;part++)
{
object = tile->getTerrainObject(part);
if (object)
{
frame = getSurface(object, _animFrame);
frame->setX(screenPosition.x);
frame->setY(screenPosition.y - object->getYOffset());
frame->blit(this);
}
}
}
// 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 and objects
if (tile)
{
for (int part = 1; part < 4; part++)
{
object = tile->getTerrainObject(part);
if (object)
{
frame = getSurface(object, _animFrame);
frame->setX(screenPosition.x);
frame->setY(screenPosition.y - object->getYOffset());
frame->blit(this);
}
}
}
// Draw items
// Draw soldier
if (unit)
{
Position offset;
calculateWalkingOffset(unit->getDirection(), unit->getWalkingPhase(), mapPosition, &offset);
unitSprite->setBattleUnit(unit);
unitSprite->setX(screenPosition.x + offset.x);
unitSprite->setY(screenPosition.y + offset.y);
unitSprite->draw();
unitSprite->blit(this);
if (unit == (BattleUnit*)_save->getSelectedSoldier() && !_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
}
}
}
}
delete unitSprite;
}
/**
* Blits the map onto another surface.
* @param surface Pointer to another surface.
*/
void Map::blit(Surface *surface)
{
Surface::blit(surface);
}
/**
* Ignores any mouse clicks that are outside the map.
* @param action Pointer to an action.
* @param state State that the action handlers belong to.
*/
void Map::mousePress(Action *action, State *state)
{
}
/**
* Ignores any mouse clicks that are outside the map.
* @param action Pointer to an action.
* @param state State that the action handlers belong to.
*/
void Map::mouseRelease(Action *action, State *state)
{
}
/**
* Ignores any mouse clicks that are outside the map
* @param action Pointer to an action.
* @param state State that the action handlers belong to.
*/
/*void Map::mouseClick(Action *action, State *state)
{
int test=0;
}*/
/**
* 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)
{
}
/**
* 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))
{
_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 && posX > 0)
{
_ScrollX = SCROLL_AMOUNT;
}
else if (posX > (getWidth() - SCROLL_BORDER) * action->getXScale())
{
_ScrollX = -SCROLL_AMOUNT;
}
else if (posX)
{
_ScrollX = 0;
}
if (posY < SCROLL_BORDER && posY > 0)
{
_ScrollY = SCROLL_AMOUNT;
}
else if (posY > (getHeight() - SCROLL_BORDER) * action->getYScale())
{
_ScrollY = -SCROLL_AMOUNT;
}
else if (posY)
{
_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)
{
if (!mx && !my) return; // cursor is offscreen
// add half a tileheight to the mouseposition per layer we are above the floor
my += -_SpriteHeight + (_viewHeight + 1) * (_SpriteHeight / 2);
// calculate the actual x/y pixelposition on a diamond shaped map
// taking the view offset into account
_selectorX = mx - _MapOffsetX - 2 * my + 2 * _MapOffsetY;
_selectorY = my - _MapOffsetY + _selectorX / 4;
// to get the row&col itself, divide by the size of a tile
_selectorY /= (_SpriteWidth/4);
_selectorX /= _SpriteWidth;
minMaxInt(&_selectorX, 0, _save->getWidth() - 1);
minMaxInt(&_selectorY, 0, _save->getLength() - 1);
}
/**
* Handle scrolling.
*/
void Map::scroll()
{
_MapOffsetX += _ScrollX;
_MapOffsetY += _ScrollY;
// don't scroll outside the map
minMaxInt(&_MapOffsetX, -(_save->getWidth() - 1) * _SpriteWidth, 0);
minMaxInt(&_MapOffsetY, -(_save->getLength() - 1) * (_SpriteWidth/4), _save->getLength() * (_SpriteWidth/4));
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;
draw();
}
/**
* Go one level up.
*/
void Map::up()
{
_viewHeight++;
minMaxInt(&_viewHeight, 0, _save->getHeight()-1);
draw();
}
/**
* Go one level down.
*/
void Map::down()
{
_viewHeight--;
minMaxInt(&_viewHeight, 0, _save->getHeight()-1);
draw();
}
/**
* Center map on a certain position.
* @param pos
*/
void Map::centerOnPosition(const Position &mapPos)
{
Position screenPos;
convertMapToScreen(mapPos, &screenPos);
_MapOffsetX = -(screenPos.x - (getWidth() / 2));
_MapOffsetY = -(screenPos.y - (getHeight() / 2));
_viewHeight = mapPos.z;
}
/**
* Convert map coordinates X,Y,Z to screen positions X, Y.
* @param mapPos
* @param pointer 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 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 dir direction the soldier is heading.
* @param phase walking phase.
* @param position current position of the soldier
* @param offset pointer to the offset to return the calculation.
*/
void Map::calculateWalkingOffset(const int dir, const int phase, const Position &position, Position *offset)
{
int offsetX[8] = { 1, 2, 1, 0, -1, -2, -1, 0 };
int offsetY[8] = { 1, 0, -1, -2, -1, 0, 1, 2 };
Tile *fromTile = _save->getTile(position);
int fromLevel = 0;
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];
}
}
// Terrain level is the height of a soldier when it stands on that tile
if (fromTile->getTerrainObject(0))
fromLevel = fromTile->getTerrainObject(0)->getTerrainLevel();
if (fromTile->getTerrainObject(3))
fromLevel += fromTile->getTerrainObject(3)->getTerrainLevel();
// If we are walking in between tiles, interpolate it's terrain level.
if (phase)
{
Position vector;
Pathfinding::directionToVector(dir, &vector);
if (phase >= 4)
{
vector *= Position(-1, -1, -1);
}
Tile *toTile = _save->getTile(position + vector);
int toLevel = 0;
if (toTile->getTerrainObject(0))
toLevel = toTile->getTerrainObject(0)->getTerrainLevel();
if (toTile->getTerrainObject(3))
toLevel += toTile->getTerrainObject(3)->getTerrainLevel();
if (phase < 4)
{
offset->y += ((fromLevel * (8 - phase)) / 8) + ((toLevel * (phase)) / 8);
}
else
{
offset->y += ((toLevel * (8 - phase)) / 8) + ((fromLevel * (phase)) / 8);
}
}
else
{
offset->y += fromLevel;
}
}
/**
* Animate walking unit.
*/
void Map::moveUnit()
{
if (_save->getSelectedSoldier()->getStatus() == STATUS_WALKING)
{
_save->getSelectedSoldier()->keepWalking();
}
if (_save->getSelectedSoldier()->getStatus() == STATUS_STANDING)
{
// check if the soldier has floor, else fall down
if (_save->getTile(_save->getSelectedSoldier()->getPosition())->hasNoFloor() &&
_save->getSelectedSoldier()->getPosition().z > 0)
{
_save->getSelectedSoldier()->setPosition(_save->getSelectedSoldier()->getPosition() + Position(0, 0, -1));
}
int dir = _save->getPathfinding()->dequeuePath();
if (dir != -1)
{
_save->getSelectedSoldier()->startWalking(dir);
_hideCursor = true; // hide cursor while walking
_game->getCursor()->setVisible(false);
}else if (_hideCursor)
{
_hideCursor = false; // show cursor again
_game->getCursor()->setVisible(true);
}
}
draw();
}
/**
* Animate flying bullet.
*/
void Map::moveBullet()
{
}