forked from Whales/Cataclysm2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonster.cpp
More file actions
516 lines (458 loc) · 10.7 KB
/
monster.cpp
File metadata and controls
516 lines (458 loc) · 10.7 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
#include "monster.h"
#include "rng.h"
#include "game.h"
#include "player.h"
#include "monster_ability.h"
#include <sstream>
Monster::Monster()
{
dead = false;
killed_by_player = false;
current_hp = 0;
max_hp = 0;
type = NULL;
action_points = 0;
special_timer = 0;
summons_used = 0;
parent_uid = -1;
}
Monster::Monster(std::string name)
{
dead = false;
killed_by_player = false;
current_hp = 0;
max_hp = 0;
type = NULL;
action_points = 0;
special_timer = 0;
summons_used = 0;
parent_uid = -1;
set_type(name);
}
Monster::Monster(Monster_type* newtype)
{
dead = false;
killed_by_player = false;
current_hp = 0;
max_hp = 0;
type = NULL;
action_points = 0;
special_timer = 0;
summons_used = 0;
parent_uid = -1;
set_type(newtype);
}
void Monster::set_type(std::string name)
{
Monster_type* newtype = MONSTER_TYPES.lookup_name(name);
if (!newtype) {
debugmsg("Monster::set_type(%s) - no such monster", name.c_str());
return;
}
set_type(MONSTER_TYPES.lookup_name(name));
}
void Monster::set_type(Monster_type* newtype)
{
dead = false;
killed_by_player = false;
type = newtype;
if (type) {
current_hp = type->hp_dice.roll();
max_hp = current_hp;
} else {
debugmsg("Monster::set_type(NULL)!");
}
action_points = 0;
}
Monster::~Monster()
{
}
glyph Monster::get_glyph()
{
if (type) {
return type->sym;
}
return glyph();
}
std::string Monster::get_data_name()
{
if (type) {
return type->get_data_name();
}
return "Typeless Monster";
}
std::string Monster::get_name()
{
if (type) {
return type->get_name();
}
return "Typeless Monster";
}
// TODO: Include possesive if it's friendly (e.g. "your dog")
std::string Monster::get_name_to_player()
{
return get_name_definite();
}
std::string Monster::get_possessive()
{
std::stringstream ret;
ret << get_name_definite() << "'s";
return ret.str();
}
std::string Monster::get_name_indefinite()
{
// TODO: Support a/an
// TODO: For unique monsters - redirect to definite=
std::stringstream ret;
ret << "a " << get_name();
return ret.str();
}
std::string Monster::get_name_definite()
{
std::stringstream ret;
ret << "the " << get_name();
return ret.str();
}
void Monster::die()
{
Entity::die();
if (parent_uid >= 0) { // Reduce the parent's summons_used
Entity* parent = GAME.entities.lookup_uid(parent_uid);
if (parent && parent->summons_used > 0) {
parent->summons_used--;
}
}
// Drop a corpse.
if (type) {
Item corpse( ITEM_TYPES.lookup_name("corpse") );
corpse.set_corpse(type);
GAME.map->add_item( corpse, pos.x, pos.y, pos.z );
}
}
bool Monster::has_sense(Sense_type sense)
{
if (!sense) {
return false;
}
return type->has_sense(sense);
}
Entity_AI Monster::get_AI()
{
if (!type) {
return Entity_AI();
}
return type->AI;
}
int Monster::get_genus_uid()
{
if (!type || !type->genus) {
return -1;
}
return type->genus->uid;
}
int Monster::get_speed()
{
if (!type) {
return 0;
}
return type->speed;
}
void Monster::make_plans()
{
// TODO: Don't hard-code this, add a "don't cancel plan" function or something
if (plan.is_active() && plan.goal_type == AIGOAL_FLEE) {
return;
}
Entity_AI AI = get_AI();
bool found_goal = false;
for (int i = 0; !found_goal && i < AI.goals.size(); i++) {
found_goal = try_goal(AI.goals[i]);
}
if (!found_goal) {
// TODO: Straight line / pinball wandering
} else {
plan.generate_path_to_target(AI, pos);
}
}
bool Monster::try_goal(AI_goal goal)
{
switch (goal) {
case AIGOAL_NULL: // Shouldn't happen
return false;
case AIGOAL_ATTACK_ENEMIES:
case AIGOAL_ATTACK_NEUTRALS:
return pick_attack_victim();
case AIGOAL_FLEE:
return pick_flee_target();
case AIGOAL_EAT_CORPSES: // TODO: This.
return false;
case AIGOAL_COLLECT_ITEMS: // TODO: This.
return false;
default:
debugmsg("Monster::try_goal doesn't know how to handle %s!",
AI_goal_name(goal).c_str());
return false;
}
return false;
}
bool Monster::pick_attack_victim()
{
// TODO: Include an estimate of the target's strength relative to our own
// TODO: Differentiate between "attacking aggresors" and "preying upon randos"
int closest = 0;
std::vector<Entity*> best;
Entity_pool *pool = &(GAME.entities);
for (std::list<Entity*>::iterator it = pool->instances.begin();
it != pool->instances.end();
it++) {
Entity* tmp = (*it);
// Not us, not in our genus, and we can sense them
if (is_enemy(tmp) && can_sense(tmp)) {
int dist = rl_dist(pos, tmp->pos);
if (closest == 0 || dist < closest) {
closest = dist;
best.clear();
best.push_back(tmp);
} else if (dist == closest) {
best.push_back(tmp);
}
}
}
if (best.empty()) {
return false;
}
int index = rng(0, best.size() - 1);
Entity_AI AI = get_AI();
plan.set_target( AIGOAL_ATTACK_ENEMIES, best[index], AI.attention_span );
return true;
}
bool Monster::pick_flee_target()
{
// TODO: This could be better.
Tripoint flee_target;
int map_max = MAP_SIZE * SUBMAP_SIZE - 1;
int z_level = (pos.z >= 0 ? 0 : pos.z);
switch (rng(1, 4)) {
case 1:
flee_target = Tripoint(0 , 0 , z_level);
break;
case 2:
flee_target = Tripoint(0 , map_max, z_level);
break;
case 3:
flee_target = Tripoint(map_max, 0 , z_level);
break;
case 4:
flee_target = Tripoint(map_max, map_max, z_level);
break;
}
Entity_AI AI = get_AI();
plan.set_target( AIGOAL_FLEE, flee_target, AI.attention_span * 3 );
return true;
}
std::vector<Ranged_attack> Monster::get_ranged_attacks()
{
if (!type) {
return std::vector<Ranged_attack>();
}
return type->ranged_attacks;
}
void Monster::take_turn()
{
if (action_points <= 0 || dead) {
return;
}
// TODO: Move make_plans() outside of this function?
make_plans();
/*
*/
// Make some noise!
if (type) {
Sound snd = type->get_sound( has_target() );
if (snd.valid()) {
snd.description = get_name_indefinite() + " " + snd.description;
if (snd.volume >= 15) {
snd.description += "!";
} else {
snd.description += ".";
}
GAME.make_sound(snd, pos);
}
}
if (!plan.is_active()) {
wander();
return;
}
plan.update();
if (special_timer > 0) {
special_timer--;
} else {
use_ability();
}
if (plan.target_entity && can_attack(plan.target_entity)) {
attack(plan.target_entity);
} else if (special_timer <= 0 && plan.target_entity &&
can_attack_ranged(plan.target_entity)) {
attack_ranged(plan.target_entity, pick_ranged_attack(plan.target_entity));
} else if (can_move_to(GAME.map, plan.next_step())) {
if (rl_dist(pos, plan.next_step()) > 1) {
debugmsg("Monster %s jumping %d steps", get_name().c_str(),
rl_dist(pos, plan.next_step()));
}
move_to(GAME.map, plan.next_step());
plan.erase_step();
} else if (can_smash(GAME.map, plan.next_step())) {
smash(GAME.map, plan.next_step());
} else {
/*
Most likely, an entity is in our way
TODO: Check if we want to attack that enemy
Tripoint next = plan.next_step();
plan.generate_path_to_target(get_AI(), pos);
*/
pause();
}
}
void Monster::use_ability()
{
if (special_timer > 0 || !type) {
return;
}
Monster_ability* abil = type->get_ability();
if (!abil || current_hp < abil->HP_cost) {
return;
}
if (abil->effect(this)) {
action_points -= abil->AP_cost;
current_hp -= abil->HP_cost;
special_timer += abil->frequency;
}
}
/*
bool Monster::can_sense(Entity* entity)
{
if (!entity) {
return false;
}
// Do it in order of lowest resource cost to highest!
if (has_sense(SENSE_OMNISCIENT)) {
return true;
}
// TODO: require that the target is warm-blooded
if (has_sense(SENSE_INFRARED)) {
return true;
}
// TODO: Use a range other than 15
if (has_sense(SENSE_SIGHT) &&
GAME.map->senses(pos, entity->pos, 15, SENSE_SIGHT)) {
return true;
}
// TODO: Use a range other than 10
if (has_sense(SENSE_SMELL) &&
GAME.map->senses(pos, entity->pos, entity->get_smell(), SENSE_SMELL)) {
return true;
}
// TODO: Other senses (e.g. echolocation)
return false;
}
*/
Attack Monster::base_attack()
{
if (!type || type->attacks.empty()) {
return Attack();
}
int index = rng(1, type->total_attack_weight);
for (int i = 0; i < type->attacks.size(); i++) {
index -= type->attacks[i].weight;
if (index <= 0) {
return type->attacks[i];
}
}
return type->attacks.back();
}
int Monster::hit_roll(int bonus)
{
int max = (type ? type->accuracy : 0);
if (max <= 0) {
return 0;
}
return rng(0, max) + bonus;
}
int Monster::dodge_roll()
{
int max = (type ? type->dodge : 0);
if (max <= 0) {
return 0;
}
return rng(0, max);
}
void Monster::take_damage(Damage_type damtype, int damage, std::string reason,
Body_part part)
{
damage -= rng(0, get_armor(damtype));
take_damage_no_armor(damtype, damage, reason, part);
}
void Monster::take_damage_no_armor(Damage_type damtype, int damage,
std::string reason, Body_part part)
{
current_hp -= damage;
if (current_hp <= 0) {
dead = true;
/* TODO: This is inefficient. Maybe make a wrapper struct for damage reasons,
* which has a flag "is_the_player"?
* (Then again, this inefficiency doesn't actually matter).
*/
if (reason.find("you") != std::string::npos) {
killed_by_player = true;
}
}
}
void Monster::absorb_damage(Damage_type damtype, int& damage, Body_part part)
{
// Entity::absorb_damage() runs it through any clothing we're wearing.
Entity::absorb_damage(damtype, damage, part);
// Monsters also get innate armor!
damage -= get_armor(damtype);
}
void Monster::heal_damage(int damage, HP_part part)
{
current_hp += damage;
if (current_hp > max_hp) {
current_hp = max_hp;
}
}
int Monster::get_armor(Damage_type damtype, Body_part part)
{
if (!type) {
return 0;
}
return type->armor[damtype];
}
// TODO: Rewrite this function.
void Monster::wander()
{
std::vector<Tripoint> open_points;
for (int x = pos.x - 1; x <= pos.x + 1; x++) {
for (int y = pos.y - 1; y <= pos.y + 1; y++) {
if (can_move_to(GAME.map, x, y, pos.z)) {
open_points.push_back( Tripoint(x, y, pos.z) );
}
}
}
if (open_points.empty()) {
pause();
} else {
move_to(GAME.map, open_points[ rng(0, open_points.size() - 1) ]);
}
}
void Monster::pause()
{
action_points = 0;
}
/*
void Monster::shift(int shiftx, int shifty)
{
Entity::shift(shiftx, shifty);
plan.shift(shiftx, shifty);
}
*/