-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.cpp
601 lines (547 loc) · 19.8 KB
/
map.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
/*
This file is part of Heroes of Wesnoth.
Copyright (C) 2007, 2008, 2009 Jon Ander Peñalba <jonan88@gmail.com>
Heroes of Wesnoth is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 as
published by the Free Software Foundation.
Heroes of Wesnoth 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 Heroes of Wesnoth. If not, see <http://www.gnu.org/licenses/>
*/
#include "map.hpp"
#include <fstream>
#include <string>
#include <vector>
#include "tinyxml/tinyxml.h"
#include "cell.hpp"
#include "events.hpp"
#include "graphics.hpp"
#include "unit.hpp"
#include "unit_animation.hpp"
#include "xml_manager.hpp"
// events_engine
using events_engine::input;
using events_engine::keys;
using events_engine::mouse;
using events_engine::MOUSE_X;
using events_engine::MOUSE_Y;
using events_engine::MOUSE_BUTTON;
using events_engine::ATTACK_CURSOR;
using events_engine::MOVE_CURSOR;
using events_engine::NORMAL_CURSOR;
// std
using std::string;
// video_engine
using video_engine::screen;
// Starts the map.
void Map::start(void) {
// Block movement to the cells in the border of the map
for (int x=0; x<map_width; x++) {
map[x][0].setPassable(false);
map[x][map_height-1].setPassable(false);
}
for (int y=0; y<map_height; y++) {
map[0][y].setPassable(false);
map[map_width-1][y].setPassable(false);
}
// Set first turn.
nextTurn();
loop();
}
// Constructor
Map::Map(const int width, const int height) {
map_width = width;
map_height = height;
// Create the array of cells
map = new Cell*[map_width];
for (int i=0; i<map_width; i++)
map[i] = new Cell[map_height];
// Set the coordinates
for (int x=0; x<map_width; x++)
for (int y=0; y<map_height; y++)
map[x][y].setCoordinates(x, y);
// Connect the cells
connectCells();
init();
}
// Constructor
Map::Map(const char *map_file) {
loadMapFile(map_file);
init();
}
// Destructor
Map::~Map(void) {
delete animation;
deleteCells();
}
// Sets the terrain type to all the cells in the map.
void Map::setTerrainToAllCells(const char *id) {
for (int i=0; i<map_width; i++)
for (int j=0; j<map_height; j++)
map[i][j].setTerrain(id);
}
// Loads the terrain, creatures and items of the map from a file.
void Map::loadMapFile(const char *file_name) {
// Load the files
// Create a string with the fisical location of the map
// "maps/" + file_name
string map_dir("maps/");
map_dir += file_name;
std::ifstream map_file(map_dir.c_str());
// The cells and units haven't been created yet so there's no
// cell with the mouse over it and there's no unit selected.
mouse_over_cell = NULL;
selected_unit = NULL;
// Detect map's size
char temp[4];
map_file.getline(temp, 4);
map_width = atoi(temp);
map_file.getline(temp, 4);
map_height = atoi(temp);
// Create a new map
map = new Cell*[map_width];
for (int i=0; i<map_width; i++)
map[i] = new Cell[map_height];
// Set the coordinates
for (int x=0; x<map_width; x++)
for (int y=0; y<map_height; y++)
map[x][y].setCoordinates(x, y);
connectCells();
char map_temp[3], creatures_temp[3], item_temp[3];
for (int j=0; j<map_height; j++) {
for (int i=0; i<map_width; i++) {
// Get info from file
map_file.get(map_temp, 3);
map_file.get(creatures_temp, 3);
map_file.get(item_temp, 3);
// Set the info in the cell
map[i][j].setTerrain(map_temp);
if ( strcmp(creatures_temp, "--") ) {
Unit *creature = new Unit(creatures_temp, 0);
map[i][j].setCreature(creature);
}
if ( strcmp(item_temp, "--") )
map[i][j].setItem(item_temp);
}
map_file.getline(map_temp, 3);
}
map_file.close();
}
// Makes the hole map visible
void Map::makeMapVisible(void) {
for (int x=0; x<map_width; x++)
for (int y=0; y<map_height; y++)
map[x][y].calculateView(1);
}
// Adjusts the visible map to the window size.
void Map::adjustVisibleMap(void) {
// Calculate the size of the map's window (might have changed)
int new_width, new_height;
bool changed = false; // To check if the window size changed
screen->getScreenSize(new_width, new_height);
if (new_width != window_width) {
changed = true;
window_width = new_width;
window_horizontal_cells = (window_width/108)*2+2; // +2 cells that don't fit compleatly
if (window_horizontal_cells > map_width) window_horizontal_cells = map_width;
}
if (new_height != window_height) {
changed = true;
window_height = new_height;
window_vertical_cells = (window_height/72)+2; // +2 cells that don't fit compleatly
if (window_vertical_cells > map_height) window_vertical_cells = map_height;
}
// If the window size has change, center the view
if (changed && selected_unit) centerView(*selected_unit);
}
// Returns a cell where the creature can attack.
Cell* Map::getAttackCell(void) {
/// @todo Optimize this function.
Cell *temp = NULL;
int x, y;
x=0;
y=0;
while (!temp && x<map_width) {
if (map[x][y].canAttackHere() &&
map[x][y].getCreature()->getMaster() != selected_unit->getMaster()) {
temp = &map[x][y];
} else {
y++;
if (y == map_height) {
y=0;
x++;
}
}
}
return temp;
}
// Updates the map according to the mouse state
void Map::updateMouse(void) {
int i = first_cell_coor.x;
int j = first_cell_coor.y;
if (mouse_over_cell)
mouse_over_cell->removeMouse();
// Find out which cell is the mouse over
SDL_Rect cellPosition;
cellPosition.x = first_cell_pos.x;
while (mouse[MOUSE_X] > cellPosition.x){
cellPosition.x += 54;
i++;
}
i--;
if ( (i%2)==1 )
cellPosition.y = first_cell_pos.y+36;
else
cellPosition.y = first_cell_pos.y;
while (mouse[MOUSE_Y] > cellPosition.y){
cellPosition.y += 72;
j++;
}
j--;
if (i>0 && i<map_width-1 && j>0 && j<map_height-1) {
// map[i][j] is a valid cell and the mouse is over it
map[i][j].putMouse();
mouse_over_cell = &map[i][j];
mouseOverCell(i,j);
if (mouse[MOUSE_BUTTON] == SDL_BUTTON_LEFT) mouseLeftClick(i,j);
if (mouse[MOUSE_BUTTON] == SDL_BUTTON_RIGHT) mouseRightClick(i,j);
} else {
input->setCursorType(NORMAL_CURSOR);
}
// move visible map
if ( (mouse[MOUSE_X] == 0 || keys[SDLK_LEFT]) &&
first_cell_coor.x != 0 )
first_cell_coor.x--;
else if ( (mouse[MOUSE_X] == window_width-1 || keys[SDLK_RIGHT]) &&
first_cell_coor.x != map_width-window_horizontal_cells )
first_cell_coor.x++;
if ( (mouse[MOUSE_Y] == 0 || keys[SDLK_UP]) &&
first_cell_coor.y != 0 )
first_cell_coor.y--;
else if ( (mouse[MOUSE_Y] == window_height-1 || keys[SDLK_DOWN]) &&
first_cell_coor.y != map_height-window_vertical_cells )
first_cell_coor.y++;
}
// Function to execute when the mouse is over a cell.
void Map::mouseOverCell(const int x, const int y) {
if (map[x][y].canAttackHere() && selected_unit->getPosition() != &map[x][y])
input->setCursorType(ATTACK_CURSOR);
else if (map[x][y].canMoveHere())
input->setCursorType(MOVE_CURSOR);
else
input->setCursorType(NORMAL_CURSOR);
}
// Function to execute when the user right clicks on a cell.
void Map::mouseRightClick(const int x, const int y) {
centerView(map[x][y]);
mouse[MOUSE_BUTTON] = 0;
}
// Moves the selected creature to a cell.
void Map::move(Cell &cell) {
animation->startNewAnimation(MOVE, *selected_unit, &cell);
}
// Connects all the cells in the map.
void Map::connectCells(void) {
for (int coor1=0; coor1<map_width; coor1++) {
for (int coor2=0; coor2<map_height; coor2++) {
if ( (coor1%2)==1 ) { // coor1 is an odd number
if (coor1 == map_width-1) { // The last colum of the map
if (coor2 == 0) {
// map[coor1][coor2].connectCell(N, NULL);
map[coor1][coor2].connectCell(S, &map[coor1][coor2+1]);
map[coor1][coor2].connectCell(SW, &map[coor1-1][coor2+1]);
} else if (coor2 == map_height-1) {
map[coor1][coor2].connectCell(N, &map[coor1][coor2-1]);
// map[coor1][coor2].connectCell(S, NULL);
// map[coor1][coor2].connectCell(SW, NULL);
} else {
map[coor1][coor2].connectCell(N, &map[coor1][coor2-1]);
map[coor1][coor2].connectCell(S, &map[coor1][coor2+1]);
map[coor1][coor2].connectCell(SW, &map[coor1-1][coor2+1]);
}
// map[coor1][coor2].connectCell(NE, NULL);
// map[coor1][coor2].connectCell(SE, NULL);
map[coor1][coor2].connectCell(NW, &map[coor1-1][coor2]);
} else if (coor2 == 0) { // The first row of the map
// map[coor1][coor2].connectCell(N, NULL);
map[coor1][coor2].connectCell(S, &map[coor1][coor2+1]);
map[coor1][coor2].connectCell(NE, &map[coor1+1][coor2]);
map[coor1][coor2].connectCell(SE, &map[coor1+1][coor2+1]);
map[coor1][coor2].connectCell(NW, &map[coor1-1][coor2]);
map[coor1][coor2].connectCell(SW, &map[coor1-1][coor2+1]);
} else if (coor2 == map_height-1) { // Last row of the map
map[coor1][coor2].connectCell(N, &map[coor1][coor2-1]);
// map[coor1][coor2].connectCell(S, NULL);
map[coor1][coor2].connectCell(NE, &map[coor1+1][coor2]);
// map[coor1][coor2].connectCell(SE, NULL);
map[coor1][coor2].connectCell(NW, &map[coor1-1][coor2]);
// map[coor1][coor2].connectCell(SW, NULL);
} else {
map[coor1][coor2].connectCell(N, &map[coor1][coor2-1]);
map[coor1][coor2].connectCell(S, &map[coor1][coor2+1]);
map[coor1][coor2].connectCell(NE, &map[coor1+1][coor2]);
map[coor1][coor2].connectCell(SE, &map[coor1+1][coor2+1]);
map[coor1][coor2].connectCell(NW, &map[coor1-1][coor2]);
map[coor1][coor2].connectCell(SW, &map[coor1-1][coor2+1]);
}
} else { // coor1 is an even number
if (coor1 == 0) { // The first colum of the map
if (coor2 == 0) {
// map[coor1][coor2].connectCell(N, NULL);
map[coor1][coor2].connectCell(S, &map[coor1][coor2+1]);
// map[coor1][coor2].connectCell(NE, NULL);
} else if (coor2 == map_height-1) {
map[coor1][coor2].connectCell(N, &map[coor1][coor2-1]);
// map[coor1][coor2].connectCell(S, NULL);
map[coor1][coor2].connectCell(NE, &map[coor1+1][coor2-1]);
} else {
map[coor1][coor2].connectCell(N, &map[coor1][coor2-1]);
map[coor1][coor2].connectCell(S, &map[coor1][coor2+1]);
map[coor1][coor2].connectCell(NE, &map[coor1+1][coor2-1]);
}
// map[coor1][coor2].connectCell(NW, NULL);
// map[coor1][coor2].connectCell(SW, NULL);
map[coor1][coor2].connectCell(SE, &map[coor1+1][coor2]);
} else if (coor1 == map_width-1) { // The last colum of the map
if (coor2 == 0) {
// map[coor1][coor2].connectCell(N, NULL);
map[coor1][coor2].connectCell(S, &map[coor1][coor2+1]);
// map[coor1][coor2].connectCell(NW, NULL);
} else if (coor2 == map_height-1) {
map[coor1][coor2].connectCell(N, &map[coor1][coor2-1]);
// map[coor1][coor2].connectCell(S, NULL);
map[coor1][coor2].connectCell(NW,&map[coor1-1][coor2-1]);
} else {
map[coor1][coor2].connectCell(N, &map[coor1][coor2-1]);
map[coor1][coor2].connectCell(S, &map[coor1][coor2+1]);
map[coor1][coor2].connectCell(NW, &map[coor1-1][coor2-1]);
}
// map[coor1][coor2].connectCell(NE, NULL);
// map[coor1][coor2].connectCell(SE, NULL);
map[coor1][coor2].connectCell(SW, &map[coor1-1][coor2]);
} else if (coor2 == 0) { // The first row of the map
// map[coor1][coor2].connectCell(N, NULL);
map[coor1][coor2].connectCell(S, &map[coor1][coor2+1]);
// map[coor1][coor2].connectCell(NE, NULL);
map[coor1][coor2].connectCell(SE, &map[coor1+1][coor2]);
// map[coor1][coor2].connectCell(NW, NULL);
map[coor1][coor2].connectCell(SW, &map[coor1-1][coor2]);
} else if (coor2 == map_height-1) { // Last row of the map
map[coor1][coor2].connectCell(N, &map[coor1][coor2-1]);
// map[coor1][coor2].connectCell(S, NULL);
map[coor1][coor2].connectCell(NE, &map[coor1+1][coor2-1]);
map[coor1][coor2].connectCell(SE, &map[coor1+1][coor2]);
map[coor1][coor2].connectCell(NW, &map[coor1-1][coor2-1]);
map[coor1][coor2].connectCell(SW, &map[coor1-1][coor2]);
} else {
map[coor1][coor2].connectCell(N, &map[coor1][coor2-1]);
map[coor1][coor2].connectCell(S, &map[coor1][coor2+1]);
map[coor1][coor2].connectCell(NE, &map[coor1+1][coor2-1]);
map[coor1][coor2].connectCell(SE, &map[coor1+1][coor2]);
map[coor1][coor2].connectCell(NW, &map[coor1-1][coor2-1]);
map[coor1][coor2].connectCell(SW, &map[coor1-1][coor2]);
}
}
}
}
}
// Softens the map to make it look nicer.
void Map::smoothMap(void) {
XmlManager *xml = XmlManager::getInstance();
TiXmlElement *root = xml->getRootElement(SMOOTH_RULES_XML_FILE);
TiXmlElement *weight;
std::string current_cell_type;
std::string temp_cell_type;
Cell *connected_cell;
bool need_smooth[6];
for (int x=0; x<map_width; x++) {
for (int y=0; y<map_height; y++) {
current_cell_type = map[x][y].getBaseTerrain();
weight = root->FirstChildElement("weight");
weight = weight->FirstChildElement();
while ( current_cell_type != weight->Attribute("name") ) {
for (int i=N; i<=NW; i++) {
need_smooth[i] = false;
connected_cell = map[x][y].getConnectedCell(i);
if (connected_cell) {
temp_cell_type = connected_cell->getBaseTerrain();
need_smooth[i] = ( temp_cell_type == weight->Attribute("name") );
}
}
addSmoothImages(&map[x][y], need_smooth, weight->Attribute("name"));
weight = weight->NextSiblingElement();
}
}
}
}
// Centers the map view in a given cell
void Map::centerView(Cell& position) {
int x, y;
position.getCoordinates(x, y);
x -= window_horizontal_cells/2;
y -= window_vertical_cells/2;
if (x<0)
x=0;
if (y<0)
y=0;
if (map_width-x < window_horizontal_cells)
x -= window_horizontal_cells-(map_width-x);
if (map_height-y < window_vertical_cells)
y -= window_vertical_cells-(map_height-y);
first_cell_coor.x = x;
first_cell_coor.y = y;
}
// Centers the map view in a given creature
void Map::centerView(Unit& creature) {
centerView(*creature.getPosition());
}
// Draws the map in the screen.
void Map::draw(void) {
SDL_Rect position;
screen->erase();
// Draws the visible cells.
for (int i=DRAW_TERRAIN; i<=DRAW_UNIT; i++) {
// Position of the firts cell (top-left)
position.x = first_cell_pos.x;
if ( (first_cell_coor.x%2)==1 )
position.y = first_cell_pos.y+36;
else
position.y = first_cell_pos.y;
position.w = 72;
position.h = 72;
// Draw
for (int x=first_cell_coor.x; position.x<window_width && x<map_width; x++) {
for (int y=first_cell_coor.y; position.y<window_height && y<map_height; y++) {
map[x][y].draw(position, i);
position.y+=72;
}
if ( (x%2)==1 ) // x is an odd number
position.y=first_cell_pos.y;
else
position.y=first_cell_pos.y+36;
position.x+=54;
}
}
}
// This function is executed in the main loop. If
// it returns true, the loop ends, else it continues.
bool Map::frame(void) {
if (keys[SDLK_ESCAPE]) {
keys[SDLK_ESCAPE] = false;
end_map = true;
}
return end_map;
}
// Delete all the cells of the map
void Map::deleteCells(void) {
for (int i=0; i<map_width; i++)
delete [] map[i];
delete [] map;
}
// Delete all the creatures in the map
void Map::deleteCreatures(void) {
for (int x=0; x<map_width; x++)
for (int y=0; y<map_height; y++) {
delete map[x][y].getCreature();
map[x][y].setCreature(NULL);
}
}
// Initialize all the variables.
void Map::init(void) {
selected_unit = NULL;
mouse_over_cell = NULL;
end_map = false;
animation = new UnitAnimation();
first_cell_coor.x = 0;
first_cell_coor.y = 0;
// Adjust the first cell's coordinates
first_cell_pos.x = -53;
first_cell_pos.y = -72;
// Calculate the size of the map's window
window_width = window_height = 0;
adjustVisibleMap();
}
// Adds the corresponding smooth images to the cell.
void Map::addSmoothImages(Cell *cell, bool *need_smooth, const char *terrain) {
int num_smooth_needed = 0;
for (int t=N; t<=NW; t++)
if (need_smooth[t]) num_smooth_needed++;
if (num_smooth_needed) {
XmlManager *xml = XmlManager::getInstance();
// Create a vector with the names of the images that will be added
TiXmlElement *root_rules = xml->getRootElement(SMOOTH_RULES_XML_FILE);
std::vector<const char*> images(1, terrain);
TiXmlNode *special = NULL;
TiXmlNode *special_to = NULL;
TiXmlNode *special_case = NULL;
TiXmlNode *special_img = NULL;
bool found = false;
const char *cell_terrain_name;
while ( (special = root_rules->IterateChildren("special",special)) ) {
while ( (special_to = special->IterateChildren("to",special_to)) ) {
cell_terrain_name = xml->getName(cell->getTerrainId(),TERRAIN_XML_FILE);
if ( !strcmp(cell_terrain_name,special_to->ToElement()->GetText()) )
found = true;
}
if (found) {
found = false;
while ( (special_case = special->IterateChildren("case",special_case)) ) {
if ( !strcmp(terrain,special_case->ToElement()->Attribute("name")) ) {
images.clear();
while ( (special_img = special_case->IterateChildren("image",special_img)) )
images.push_back(special_img->ToElement()->GetText());
}
}
}
}
// Add the images
TiXmlElement *root_image = xml->getRootElement(SMOOTH_IMAGES_XML_FILE);
int num_images = images.size();
std::vector<TiXmlElement*> elements(num_images);
TiXmlNode *element = NULL;
while ( (element = root_image->IterateChildren(element)) ) {
for (int i=0; i<num_images; i++) {
if ( !strcmp(images[i],element->ToElement()->Attribute("name")) )
elements[i] = element->ToElement();
}
}
const char* cardinal_dir[] = {"n","ne","se","s","sw","nw"};
int max_matches, num_matches;
TiXmlElement *max_matches_element;
TiXmlNode *temp_element;
// Temporal copies of some variables
int temp_num_smooth_needed;
bool temp_need_smooth[6];
for (int j=0; j<num_images; j++) {
temp_num_smooth_needed = num_smooth_needed;
for (int t=N; t<=NW; t++) temp_need_smooth[t] = need_smooth[t];
while (temp_num_smooth_needed) {
// Look for the best image to add
max_matches = 0;
temp_element = NULL;
while ( (temp_element = elements[j]->IterateChildren(temp_element)) ) {
num_matches = 0;
for (int t=N; t<=NW; t++) {
if (temp_element->ToElement()->Attribute(cardinal_dir[t])) {
if (temp_need_smooth[t]) num_matches++;
else num_matches = -6;
}
}
if (num_matches > max_matches) {
max_matches = num_matches;
max_matches_element = temp_element->ToElement();
}
}
cell->addImage(*screen->getImage(max_matches_element->GetText()));
for (int t=N; t<=NW; t++) {
if (max_matches_element->Attribute(cardinal_dir[t]))
temp_need_smooth[t] = false;
}
temp_num_smooth_needed -= max_matches;
}
}
}
}