forked from Whales/Cataclysm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonster.cpp
369 lines (333 loc) · 7.93 KB
/
monster.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
#include "monster.h"
#include "rng.h"
#include "game.h"
#include "player.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;
}
void Monster::set_type(std::string name)
{
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";
}
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();
}
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;
}
// TODO: Support different senses
// TODO: Don't hard-code for player; instead select a target from Game.entities
// Iterate through our AI's goals until we find one we can try for
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 (tmp->uid != uid && tmp->get_genus_uid() != get_genus_uid() &&
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();
if (!plan.is_active()) {
wander();
return;
}
plan.update();
if (special_timer > 0) {
special_timer--;
}
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();
}
}
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, 10, 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();
}
void Monster::take_damage(Damage_type type, 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"?
*/
if (reason.find("you") != std::string::npos) {
killed_by_player = true;
}
}
}
void Monster::heal_damage(int damage, HP_part part)
{
current_hp += damage;
if (current_hp > max_hp) {
current_hp = max_hp;
}
}
// 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;
}