-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbattle.cpp
391 lines (346 loc) · 10.8 KB
/
battle.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
/*
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 "battle.hpp"
#include "cell.hpp"
#include "events.hpp"
#include "graphics.hpp"
#include "hero.hpp"
#include "unit_animation.hpp"
//events_engine
using events_engine::input;
using events_engine::keys;
using events_engine::mouse;
using events_engine::MOUSE_BUTTON;
using events_engine::ATTACK_CURSOR;
using events_engine::ILLEGAL_CURSOR;
using events_engine::MOVE_CURSOR;
using events_engine::NORMAL_CURSOR;
using events_engine::WAIT_CURSOR;
// video_engine
using video_engine::screen;
using video_engine::RANDOM_EFFECT;
using video_engine::FACE_LEFT;
using video_engine::FACE_RIGHT;
// Constructor
Battle::Battle(Hero &player, Unit **enemies, int num_enemies, const char *terrain) : Map(20, 11) {
// Set the enemies
for (int i=0; i<num_enemies; i++)
creatures[ENEMY+1+i] = enemies[i];
for (int j=num_enemies; j<MAX_TEAM_UNITS; j++)
creatures[ENEMY+1+j] = NULL;
// Put the enemy creatures in the map.
for (int i=0; i<num_enemies; i++) {
creatures[ENEMY+1+i]->setFacingSide(FACE_LEFT);
map[map_width-3][i+1].setCreature(creatures[ENEMY+1+i]);
}
// No enemy hero
creatures[ENEMY] = NULL;
init(player, terrain);
}
// Constructor
Battle::Battle(Hero &player, Hero &enemy, const char *terrain) : Map(20, 11) {
setHero(enemy, FACE_LEFT);
init(player, terrain);
}
// Starts the battle.
void Battle::start(void) {
draw();
screen->transitionEffect(RANDOM_EFFECT);
Map::start();
}
// Things to do no matter what constructor was called.
void Battle::init(Hero &player_hero, const char *terrain) {
setHero(player_hero, FACE_RIGHT);
makeMapVisible();
// Put all units' progress to 0
for (int t=0; t<MAX_BATTLE_UNITS; t++)
turn_progress[t] = 0;
setTerrainToAllCells(terrain);
}
// Put a hero and his units in the map.
void Battle::setHero(Hero &hero, const int facing_side) {
int hero_pos_x;
int creatures_pos_x;
int side;
if (facing_side == FACE_RIGHT) {
side = PLAYER;
hero_pos_x = 1;
creatures_pos_x = 2;
} else {
side = ENEMY;
hero_pos_x = map_width-2;
creatures_pos_x = map_width-3;
}
// Set hero
creatures[side] = &hero;
hero.setFacingSide(facing_side);
map[hero_pos_x][4].setCreature(&hero);
// Set units
for (int i=0; i<MAX_TEAM_UNITS; i++) {
creatures[side+1+i] = hero.getCreature(i);
if (creatures[side+1+i]) {
creatures[side+1+i]->setFacingSide(facing_side);
map[creatures_pos_x][i+1].setCreature(creatures[side+1+i]);
}
}
}
// Controls the units not controled by the player.
void Battle::ai(void) {
Cell *temp = getAttackCell();
if (temp) { // Attack a unit
attack(*temp);
} else { // Move the unit
int x, y;
selected_unit->getPosition()->getCoordinates(x, y);
x=0;
while(!map[x][y].canMoveHere() && x<map_width-1)
x++;
if (x!=map_width-1)
move(map[x][y]);
else
selected_unit->getPosition()->unselect();
}
}
// Makes the selected creature attack the unit in the given cell.
void Battle::attack(Cell &cell) {
animation->startNewAnimation(ATTACK, *selected_unit, &cell);
}
// Function to execute when the mouse is over a cell.
void Battle::mouseOverCell(const int x, const int y) {
// Set the type of cursor needed
if (map[x][y].getCreature()) {
if (map[x][y].getCreature()->getMaster() != selected_unit->getMaster()) {
if (map[x][y].canAttackHere() || selected_unit->getProjectiles())
input->setCursorType(ATTACK_CURSOR);
else
input->setCursorType(ILLEGAL_CURSOR);
} else {
input->setCursorType(NORMAL_CURSOR);
}
} else if (map[x][y].canMoveHere()) {
input->setCursorType(MOVE_CURSOR);
} else {
input->setCursorType(NORMAL_CURSOR);
}
}
// Function to execute when the user left clicks on a cell.
void Battle::mouseLeftClick(const int x, const int y) {
if ( selected_unit->getPosition() != &map[x][y] ) {
if (map[x][y].getCreature()) {
if (map[x][y].getCreature()->getMaster() != selected_unit->getMaster() ) {
// Check close attack
bool can_attack = map[x][y].canAttackHere();
// Check distant attack
can_attack |= map[x][y].getCreature() && selected_unit->getProjectiles();
if (can_attack)
attack(map[x][y]);
}
} else if ( map[x][y].canMoveHere() ) {
move(map[x][y]);
}
}
}
// Starts the next turn.
void Battle::nextTurn(void) {
// Check if the battle has ended
// Check if the hero is dead
if (creatures[PLAYER] == NULL)
end_map = true;
// Calculate number of dead creatures
int counter = 0;
for (int i=0; i<MAX_TEAM_UNITS; i++) {
if (creatures[ENEMY+1+i] == NULL)
counter++;
}
// Check if there are no enemies
if (counter == MAX_TEAM_UNITS && !creatures[ENEMY])
end_map = true;
// If the battle hasn't ended continue
if (!end_map) {
bool done = false;
int turn;
// Search for the next creature
while (!done) {
// Look if it's someones turn
for (int i=0; i<MAX_BATTLE_UNITS && !done; i++) {
if (turn_progress[i] >= TURN) {
turn_progress[i] -= TURN;
if (creatures[i]) {
done = true;
turn = i;
}
}
}
// If it's no one's turn, increase all the turn_progress
for (int j=0; j<MAX_BATTLE_UNITS && !done; j++) {
if (creatures[j])
turn_progress[j] += creatures[j]->getAgility();
}
}
selected_unit = creatures[turn];
selected_unit->getPosition()->select();
}
// If the mouse button is pressed wait until it's released.
while (mouse[MOUSE_BUTTON]) input->readInput();
}
// Deletes a creature.
void Battle::deleteCreature(Unit &creature) {
if (&creature == creatures[PLAYER] || &creature == creatures[ENEMY]) {
Hero *hero = static_cast<Hero*>(&creature);
// Remove the hero's units from the battle
for (int j=0; j<MAX_TEAM_UNITS; j++) {
if (hero->getCreature(j))
hero->getCreature(j)->getPosition()->setCreature(NULL);
}
delete &creature;
if (&creature == creatures[PLAYER]) {
creatures[PLAYER] = NULL;
} else {
creatures[ENEMY] = NULL;
}
} else {
bool found = false;
for (int i=0; i<MAX_TEAM_UNITS && !found; i++) {
if (creatures[PLAYER+1+i] == &creature) {
/// @todo Do the same without recruitCreature(unit*, int)
static_cast<Hero*>(creatures[PLAYER])->recruitCreature(NULL, i);
creatures[PLAYER+1+i] = NULL;
found = true;
} else if (creatures[ENEMY+1+i] == &creature) {
if (creatures[ENEMY]) {
/// @todo Do the same without recruitCreature(unit*, int)
static_cast<Hero*>(creatures[ENEMY])->recruitCreature(NULL, i);
}
creatures[ENEMY+1+i] = NULL;
found = true;
}
}
delete &creature;
}
}
// This function is executed in the main loop. If
// it return_progress true, the loop ends, else it continues.
bool Battle::frame(void) {
if (Map::frame()) {
deleteCreature(*creatures[PLAYER]);
} else { // If the battle wasn't ended continue.
// Check if there's an animation in progress
if (animation->animationInProgress()) {
if (animation->frame()) {
if (animation->getType() == ATTACK) {
Unit *enemy_creature = animation->getFinalPosition()->getCreature();
selected_unit->damageCreature(*enemy_creature);
if ( enemy_creature->getNumber() == 0 )
animation->startNewAnimation(DIE, *enemy_creature);
else
nextTurn();
} else if (animation->getType() == DIE) {
animation->getUnit()->getPosition()->setCreature(NULL);
deleteCreature(*animation->getUnit());
nextTurn();
} else if (animation->getType() == MOVE) {
nextTurn();
}
}
} else {
// If there's no animation do normal stuff
if (selected_unit->getMaster() == creatures[ENEMY]) {
ai();
} else {
// This controls only work when a friendly creature is moving
if (keys[SDLK_SPACE]) {
keys[SDLK_SPACE] = false;
selected_unit->getPosition()->unselect();
nextTurn();
}
updateMouse();
}
}
// Draw the map
adjustVisibleMap();
draw();
}
return end_map;
}
// Creates and starts the default battle.
void createDefaultBattle(void) {
Hero *player;
Unit *creature[9];
Unit *temp;
// Create the hero and it's units.
player = new Hero("elf");
for (int j=0; j<9; j++) {
temp = new Unit("shyde", 5);
player->recruitCreature(temp);
}
// Create the enemy creatures.
/// @todo Free this units.
for (int i=0; i<5; i++) {
creature[i] = new Unit("skeleton", 3);
}
for (int t=5; t<9; t++) {
creature[t] = new Unit("bat", 10);
}
Battle war(*player, creature, 9, "flat_grass");
war.start();
if (war.win())
delete player;
// Make sure the cursors's state is normal
input->setCursorType(NORMAL_CURSOR);
}
// Creates and starts a battle.
bool createBattle(Hero &player, const char *enemy_type, const char *terrain_type) {
Cell *temp;
Unit *creature[9];
int num_enemies;
// Save players position in the world
temp = player.getPosition();
// Create the enemy creatures.
num_enemies = rand() % 5;
num_enemies++;
for (int i=0; i<9; i++) {
// Enemy creatures aren't deleted because in the future we will need them
creature[i] = new Unit(enemy_type, num_enemies);
}
Battle war(player, creature, 9, terrain_type);
war.start();
// Restore player's position
if (war.win())
temp->setCreature(&player);
else
temp->setCreature(NULL);
// Make sure the cursors's state is normal
input->setCursorType(NORMAL_CURSOR);
return war.win();
}
// Creates and starts a battle.
bool createBattle(Hero &player, Hero &enemy, const char *terrain_type) {
Cell *temp[2];
// Save heroes positions in the world
temp[0] = player.getPosition();
temp[1] = enemy.getPosition();
Battle war(player, enemy, terrain_type);
war.start();
// Restore winner's position
if (war.win()) {
temp[0]->setCreature(&player);
temp[1]->setCreature(NULL);
} else {
temp[0]->setCreature(NULL);
temp[1]->setCreature(&enemy);
}
// Make sure the cursors's state is normal
input->setCursorType(NORMAL_CURSOR);
return war.win();
}