forked from flareteam/flare-game
/
Enemy.cpp
345 lines (292 loc) · 8.92 KB
/
Enemy.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
/*
Copyright © 2011-2012 Clint Bellanger
Copyright © 2012 Stefan Beller
This file is part of FLARE.
FLARE 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.
FLARE 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
FLARE. If not, see http://www.gnu.org/licenses/
*/
/*
* class Enemy
*/
#include "Animation.h"
#include "BehaviorStandard.h"
#include "CampaignManager.h"
#include "EnemyBehavior.h"
#include "Enemy.h"
#include "Hazard.h"
#include "LootManager.h"
#include "MapRenderer.h"
#include "PowerManager.h"
#include "SharedResources.h"
#include "UtilsMath.h"
#include <sstream>
using namespace std;
Enemy::Enemy(PowerManager *_powers, MapRenderer *_map) : Entity(_map) {
powers = _powers;
stats.cur_state = ENEMY_STANCE;
stats.turn_ticks = MAX_FRAMES_PER_SEC;
//stats.patrol_ticks = 0; //no longer needed due to A*
stats.cooldown = 0;
stats.last_seen.x = -1;
stats.last_seen.y = -1;
stats.in_combat = false;
stats.join_combat = false;
haz = NULL;
sfx_phys = false;
sfx_ment = false;
sfx_hit = false;
sfx_die = false;
sfx_critdie = false;
reward_xp = false;
instant_power = false;
eb = NULL;
}
Enemy::Enemy(const Enemy& e)
: Entity(e)
, type(e.type)
, haz(NULL) // do not copy hazard. This constructor is used during mapload, so no hazard should be active.
, eb(new BehaviorStandard(this))
, powers(e.powers)
, sfx_phys(e.sfx_phys)
, sfx_ment(e.sfx_ment)
, sfx_hit(e.sfx_hit)
, sfx_die(e.sfx_die)
, sfx_critdie(e.sfx_critdie)
, reward_xp(e.reward_xp)
, instant_power(e.instant_power)
{
assert(e.haz == NULL);
}
/**
* The current direction leads to a wall. Try the next best direction, if one is available.
*/
int Enemy::faceNextBest(int mapx, int mapy) {
int dx = abs(mapx - stats.pos.x);
int dy = abs(mapy - stats.pos.y);
switch (stats.direction) {
case 0:
if (dy > dx) return 7;
else return 1;
case 1:
if (mapy > stats.pos.y) return 0;
else return 2;
case 2:
if (dx > dy) return 1;
else return 3;
case 3:
if (mapx < stats.pos.x) return 2;
else return 4;
case 4:
if (dy > dx) return 3;
else return 5;
case 5:
if (mapy < stats.pos.y) return 4;
else return 6;
case 6:
if (dx > dy) return 5;
else return 7;
case 7:
if (mapx > stats.pos.x) return 6;
else return 0;
}
return 0;
}
/**
* Calculate distance between the enemy and the hero
*/
int Enemy::getDistance(Point dest) {
int dx = dest.x - stats.pos.x;
int dy = dest.y - stats.pos.y;
double step1 = (double)dx * (double)dx + (double)dy * (double)dy;
double step2 = sqrt(step1);
return int(step2);
}
void Enemy::newState(int state) {
stats.cur_state = state;
}
/**
* logic()
* Handle a single frame. This includes:
* - move the enemy based on AI % chances
* - calculate the next frame of animation
*/
void Enemy::logic() {
eb->logic();
return;
}
/**
* Whenever a hazard collides with an enemy, this function resolves the effect
* Called by HazardManager
*
* Returns false on miss
*/
bool Enemy::takeHit(const Hazard &h) {
if (stats.cur_state != ENEMY_DEAD && stats.cur_state != ENEMY_CRITDEAD)
{
if (!stats.in_combat) {
stats.join_combat = true;
stats.in_combat = true;
stats.last_seen.x = stats.hero_pos.x;
stats.last_seen.y = stats.hero_pos.y;
powers->activate(stats.power_index[BEACON], &stats, stats.pos); //emit beacon
}
// exit if it was a beacon (to prevent stats.targeted from being set)
if (powers->powers[h.power_index].beacon) return false;
// prepare the combat text
CombatText *combat_text = comb;
// if it's a miss, do nothing
int avoidance = stats.avoidance;
clampCeil(avoidance, MAX_AVOIDANCE);
if (percentChance(avoidance - h.accuracy - 25)) {
combat_text->addMessage(msg->get("miss"), stats.pos, COMBAT_MESSAGE_MISS, false);
return false;
}
// calculate base damage
int dmg = randBetween(h.dmg_min, h.dmg_max);
// apply elemental resistance
int vulnerable;
if (h.trait_elemental >= 0 && unsigned(h.trait_elemental) < stats.vulnerable.size()) {
unsigned i = h.trait_elemental;
if (MAX_RESIST < stats.vulnerable[i] && stats.vulnerable[i] < 100) vulnerable = MAX_RESIST;
else vulnerable = stats.vulnerable[i];
dmg = (dmg * vulnerable) / 100;
}
if (!h.trait_armor_penetration) { // armor penetration ignores all absorption
// substract absorption from armor
int absorption = randBetween(stats.absorb_min, stats.absorb_max);
if (absorption > 0) {
if ((dmg*100)/absorption > MAX_ABSORB)
absorption = (absorption * MAX_ABSORB) / 100;
if (absorption == 0) absorption = 1;
}
dmg = dmg - absorption;
if (dmg <= 0) {
dmg = 0;
if (h.trait_elemental < 0) {
if (MAX_ABSORB < 100) dmg = 1;
} else {
if (MAX_RESIST < 100) dmg = 1;
}
}
}
// check for crits
int true_crit_chance = h.crit_chance;
if (stats.effects.stun || stats.effects.speed < 100)
true_crit_chance += h.trait_crits_impaired;
bool crit = percentChance(true_crit_chance);
if (crit) {
dmg = dmg + h.dmg_max;
map->shaky_cam_ticks = MAX_FRAMES_PER_SEC/2;
// show crit damage
combat_text->addMessage(dmg, stats.pos, COMBAT_MESSAGE_CRIT, false);
}
else {
// show normal damage
combat_text->addMessage(dmg, stats.pos, COMBAT_MESSAGE_GIVEDMG, false);
}
// apply damage
stats.takeDamage(dmg);
// damage always breaks stun
if (dmg > 0) stats.effects.removeEffectType("stun");
// after effects
if (stats.hp > 0) {
if (h.mod_power > 0) powers->effect(&stats, h.mod_power);
powers->effect(&stats, h.power_index);
if (stats.effects.forced_move) {
float theta = powers->calcTheta(stats.hero_pos.x, stats.hero_pos.y, stats.pos.x, stats.pos.y);
stats.forced_speed.x = static_cast<int>(ceil(stats.effects.forced_speed * cos(theta)));
stats.forced_speed.y = static_cast<int>(ceil(stats.effects.forced_speed * sin(theta)));
}
}
if (h.hp_steal != 0) {
int heal_amt = (dmg * h.hp_steal) / 100;
if (heal_amt == 0 && dmg > 0) heal_amt = 1;
combat_text->addMessage(msg->get("+%d HP",heal_amt), h.src_stats->pos, COMBAT_MESSAGE_BUFF, true);
h.src_stats->hp += heal_amt;
clampCeil(h.src_stats->hp, h.src_stats->maxhp);
}
if (h.mp_steal != 0) {
int heal_amt = (dmg * h.mp_steal) / 100;
if (heal_amt == 0 && dmg > 0) heal_amt = 1;
combat_text->addMessage(msg->get("+%d MP",heal_amt), h.src_stats->pos, COMBAT_MESSAGE_BUFF, true);
h.src_stats->mp += heal_amt;
clampCeil(h.src_stats->mp, h.src_stats->maxmp);
}
// post effect power
if (h.post_power > 0 && dmg > 0) {
powers->activate(h.post_power, h.src_stats, stats.pos);
}
// interrupted to new state
if (dmg > 0) {
if (stats.hp <= 0 && crit) {
doRewards();
stats.effects.triggered_death = true;
stats.cur_state = ENEMY_CRITDEAD;
map->collider.unblock(stats.pos.x,stats.pos.y);
}
else if (stats.hp <= 0) {
doRewards();
stats.effects.triggered_death = true;
stats.cur_state = ENEMY_DEAD;
map->collider.unblock(stats.pos.x,stats.pos.y);
}
// don't go through a hit animation if stunned
else if (!stats.effects.stun) {
stats.cur_state = ENEMY_HIT;
// roll to see if the enemy's ON_HIT power is casted
if (percentChance(stats.power_chance[ON_HIT])) {
powers->activate(stats.power_index[ON_HIT], &stats, stats.pos);
}
}
}
return true;
}
return false;
}
/**
* Upon enemy death, handle rewards (currency, xp, loot)
*/
void Enemy::doRewards() {
reward_xp = true;
// some creatures create special loot if we're on a quest
if (stats.quest_loot_requires != "") {
// the loot manager will check quest_loot_id
// if set (not zero), the loot manager will 100% generate that loot.
if (!(map->camp->checkStatus(stats.quest_loot_requires) && !map->camp->checkStatus(stats.quest_loot_not))) {
stats.quest_loot_id = 0;
}
}
// some creatures drop special loot the first time they are defeated
// this must be done in conjunction with defeat status
if (stats.first_defeat_loot > 0) {
if (!map->camp->checkStatus(stats.defeat_status)) {
stats.quest_loot_id = stats.first_defeat_loot;
}
}
// defeating some creatures (e.g. bosses) affects the story
if (stats.defeat_status != "") {
map->camp->setStatus(stats.defeat_status);
}
LootManager::getInstance()->addEnemyLoot(this);
}
/**
* getRender()
* Map objects need to be drawn in Z order, so we allow a parent object (GameEngine)
* to collect all mobile sprites each frame.
*/
Renderable Enemy::getRender() {
Renderable r = activeAnimation->getCurrentFrame(stats.direction);
r.map_pos.x = stats.pos.x;
r.map_pos.y = stats.pos.y;
return r;
}
Enemy::~Enemy() {
delete haz;
delete eb;
}