forked from Whales/Cataclysm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.cpp
2747 lines (2470 loc) · 64.9 KB
/
entity.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "entity.h"
#include "rng.h"
#include "game.h"
#include "geometry.h"
#include "map.h"
#include <sstream>
Stats::Stats()
{
strength = 0;
dexterity = 0;
intelligence = 0;
perception = 0;
}
Stats::~Stats()
{
}
void Stats::reset()
{
strength = strength_max;
dexterity = dexterity_max;
intelligence = intelligence_max;
perception = perception_max;
}
Stats& Stats::operator= (const Stats& rhs)
{
strength = rhs.strength;
dexterity = rhs.dexterity;
intelligence = rhs.intelligence;
perception = rhs.perception;
return *this;
}
Stats& Stats::operator+=(const Stats& rhs)
{
strength += rhs.strength;
dexterity += rhs.dexterity;
intelligence += rhs.intelligence;
perception += rhs.perception;
return *this;
}
Stats& Stats::operator-=(const Stats& rhs)
{
strength -= rhs.strength;
dexterity -= rhs.dexterity;
intelligence -= rhs.intelligence;
perception -= rhs.perception;
return *this;
}
Entity_plan::Entity_plan()
{
target_point = Tripoint(-1, -1, -1);
target_entity = NULL;
attention = 0;
goal_type = AIGOAL_NULL;
}
Entity_plan::~Entity_plan()
{
}
void Entity_plan::set_target(AI_goal goal, Tripoint target, int att)
{
if (att == -1) { // att defaults to -1
att = 15;
}
target_point = target;
target_entity = NULL; // TODO: Don't do this?
attention = att;
goal_type = goal;
}
void Entity_plan::set_target(AI_goal goal, Entity* target, int att)
{
if (!target) {
target_point = Tripoint(-1, -1, -1);
attention = 0;
target_entity = NULL;
return;
}
if (att == -1) { // att defaults to -1
att = 15;
}
target_entity = target;
target_point = target->pos;
attention = att;
goal_type = goal;
}
void Entity_plan::generate_path_to_target(Entity_AI AI, Tripoint origin)
{
if (!is_active() || target_point.x < 0 || target_point.y < 0) {
return;
}
Generic_map map = GAME.map->get_movement_map(AI, origin, target_point);
Pathfinder pf(map);
path = pf.get_path(PATH_A_STAR, origin, target_point);
if (path.empty()) {
attention = 0;
target_point = Tripoint(-1, -1, -1);
target_entity = NULL;
}
}
void Entity_plan::update()
{
if (attention > 0) {
attention--;
}
if (attention <= 0) {
target_entity = NULL;
}
}
bool Entity_plan::is_active()
{
if (attention <= 0) {
return false;
}
if (target_entity) {
return true;
}
if (target_point.x < 0 || target_point.y < 0) {
return false;
}
return true;
}
Tripoint Entity_plan::get_target()
{
if (target_point.x >= 0 && target_point.y >= 0) {
return target_point;
}
if (target_entity) {
return target_entity->pos;
}
return Tripoint(-1, -1, -1);
}
Tripoint Entity_plan::next_step()
{
if (path.empty()) {
return Tripoint(-1, -1, -1);
}
return path[0];
}
void Entity_plan::erase_step()
{
path.erase_step(0);
}
void Entity_plan::clear()
{
target_point = Tripoint(-1, -1, -1);
target_entity = NULL;
attention = 0;
goal_type = AIGOAL_NULL;
path.clear();
}
void Entity_plan::shift(int shiftx, int shifty)
{
if (target_point.x > -1) {
target_point.x -= shiftx * SUBMAP_SIZE;
target_point.y -= shifty * SUBMAP_SIZE;
}
if (target_point.x < 0 || target_point.y < 0) {
clear();
}
path.shift(shiftx, shifty);
}
Entity::Entity()
{
uid = -1;
pos.x = 15;
pos.y = 15;
pos.z = 0;
action_points = 0;
dead = false;
killed_by_player = false;
hunger = 0;
thirst = 0;
fatigue = 0;
stomach_food = 0;
stomach_water = 0;
pain = 0;
painkill = 0;
special_timer = 0;
experience = 0;
morale = 0;
// Initialize trait vector
for (int i = 0; i < TRAIT_MAX; i++) {
traits.push_back(false);
}
}
Entity::~Entity()
{
}
std::string Entity::get_data_name()
{
return "Nothing";
}
std::string Entity::get_name()
{
return "Nothing";
}
std::string Entity::get_name_to_player()
{
return "Nothing";
}
std::string Entity::get_possessive()
{
return "Nothing's";
}
std::string Entity::conjugate(const std::string &verb)
{
// Dumbest conjugation ever, but it should work for most cases!
// TODO: Special-case stuff?
if (verb[ verb.length() - 1 ] == 's') {
return verb + "es"; // "miss" => "misses"
}
return verb + "s"; // "hit" => "hits"
}
glyph Entity::get_glyph()
{
return glyph();
}
void Entity::die()
{
for (int i = 0; i < inventory.size(); i++) {
GAME.map->add_item( inventory[i], pos.x, pos.y, pos.z );
}
for (int i = 0; i < items_worn.size(); i++) {
GAME.map->add_item( items_worn[i], pos.x, pos.y, pos.z );
}
if (weapon.is_real()) {
GAME.map->add_item( weapon, pos.x, pos.y, pos.z );
}
// Tell any monsters that're targeting us to QUIT IT
for (std::list<Entity*>::iterator it = GAME.entities.instances.begin();
it != GAME.entities.instances.end();
it++) {
if ( (*it)->plan.target_entity == this ) {
(*it)->plan.clear();
}
}
}
void Entity::start_turn()
{
stats.reset(); // Reset all to their max values
if (GAME.minute_timer(30)) {
adjust_morale(); // Adjust morale before doing other stuff.
}
// Move food/water from stomach to body
if (GAME.minute_timer(1)) {
if (stomach_food > 0) {
int move = (stomach_food + 1) / 2;
hunger -= move;
stomach_food -= move;
int min = get_hunger_minimum();
if (hunger < min) {
hunger = min;
}
}
if (stomach_water > 0) {
int move = (stomach_water + 1) / 2;
thirst -= move;
stomach_water -= move;
int min = get_thirst_minimum();
if (thirst < min) {
thirst = min;
}
}
}
// Increment hunger, thirst, and fatigue when appropriate...
int hunger_interval = 6, thirst_interval = 6, fatigue_interval = 6;
if (has_trait(TRAIT_LIGHT_EATER)) {
hunger_interval += 2;
}
if (has_trait(TRAIT_CAMEL)) {
thirst_interval += 2;
}
if (GAME.minute_timer(hunger_interval)) {
hunger++;
}
if (GAME.minute_timer(thirst_interval)) {
thirst++;
}
if (GAME.minute_timer(fatigue_interval)) {
fatigue++;
}
// Adjust stats as needed
stats += get_stats_mod();
process_status_effects();
}
void Entity::adjust_morale()
{
if (morale < 0) {
morale += 1 + abs(morale) / 10;
} else if (morale > 0) {
morale -= 1 + abs(morale) / 10;
}
}
void Entity::process_status_effects()
{
for (int i = 0; i < effects.size(); i++) {
int level = effects[i].level;
stats += effects[i].stats_mod();
switch (effects[i].type) {
case STATUS_SLEEP_AID:
if (GAME.minute_timer(1)) {
fatigue++;
}
break;
case STATUS_PAINKILL_MILD: {
int pkill = (level > 2 ? 20 : 10 * level);
if (GAME.turn_timer(24 - level * 4) && painkill < pkill) {
painkill++;
}
} break;
case STATUS_PAINKILL_MED: {
int pkill = (level > 4 ? 90 : 30 + 15 * level);
if (GAME.turn_timer(17 - level * 2) && painkill < pkill) {
painkill++;
}
} break;
case STATUS_PAINKILL_LONG: {
int pkill = (level > 3 ? 60 : 30 + 10 * level);
if (GAME.minute_timer(2) && painkill < pkill) {
painkill++;
}
} break;
case STATUS_PAINKILL_HEAVY: {
int pkill = (level > 6 ? 200 : 80 + 20 * level);
if (GAME.turn_timer(11 - level) && painkill < pkill) {
painkill++;
}
} break;
} // switch (effects[i].type)
if (effects[i].decrement()) { // Returns true if duration <= 0
effects.erase(effects.begin() + i);
i--;
}
}
}
void Entity::gain_action_points()
{
action_points += get_speed();
}
nc_color Entity::get_speed_color()
{
int speed = get_speed();
if (speed <= 70) {
return c_red;
}
if (speed <= 85) {
return c_ltred;
}
if (speed < 100) {
return c_yellow;
}
if (speed == 100) {
return c_white;
}
if (speed < 115) {
return c_ltgreen;
}
return c_green;
}
int Entity::get_speed()
{
int ret = 100;
ret -= get_hunger_speed_penalty();
ret -= get_thirst_speed_penalty();
ret -= get_fatigue_speed_penalty();
ret -= get_pain_speed_penalty();
for (int i = 0; i < effects.size(); i++) {
ret += effects[i].speed_mod();
}
if (has_trait(TRAIT_QUICK)) {
ret = ret + (ret + 19) / 20; // "+ 19" means we always round up.
}
return ret;
}
Stats Entity::get_stats_mod()
{
Stats ret;
ret -= get_hunger_stats_penalty();
ret -= get_thirst_stats_penalty();
ret -= get_fatigue_stats_penalty();
for (int i = 0; i < effects.size(); i++) {
ret += effects[i].stats_mod();
}
return ret;
}
std::map<std::string,int> Entity::get_speed_modifiers()
{
std::map<std::string,int> ret;
int tmp;
tmp = get_hunger_speed_penalty();
if (tmp > 0) {
ret["hunger"] = 0 - tmp;
}
tmp = get_thirst_speed_penalty();
if (tmp > 0) {
ret["thirst"] = 0 - tmp;
}
tmp = get_fatigue_speed_penalty();
if (tmp > 0) {
ret["fatigue"] = 0 - tmp;
}
tmp = get_pain_speed_penalty();
if (tmp > 0) {
ret["pain"] = 0 - tmp;
}
for (int i = 0; i < effects.size(); i++) {
tmp = effects[i].speed_mod();
std::string name = effects[i].get_name();
if (ret.count(name) == 0) {
ret[name] = tmp;
} else {
ret[name] += tmp;
}
}
if (has_trait(TRAIT_QUICK)) {
int amount = get_speed() / 21;
ret["Quick"] = amount;
}
return ret;
}
int Entity::get_movement_cost()
{
return 100;
}
int Entity::get_hunger_speed_penalty()
{
if (hunger >= 960) {
return 30;
}
if (hunger >= 480) {
return 20;
}
if (hunger >= 240) {
return 12;
}
if (hunger >= 120) {
return 7;
}
if (hunger >= 60) {
return 5;
}
return 0;
}
Stats Entity::get_hunger_stats_penalty()
{
Stats ret;
if (hunger >= 960) {
ret.strength = -3;
ret.dexterity = -3;
ret.intelligence = -3;
ret.perception = -2;
} else if (hunger >= 480) {
ret.strength = -3;
ret.dexterity = -2;
ret.intelligence = -3;
ret.perception = -1;
} else if (hunger >= 240) {
ret.strength = -2;
ret.dexterity = -1;
ret.intelligence = -2;
ret.perception = -1;
} else if (hunger >= 120) {
ret.strength = -1;
ret.dexterity = -1;
ret.intelligence = -1;
} else if (hunger >= 60) {
ret.intelligence = -1;
}
return ret;
}
int Entity::get_thirst_speed_penalty()
{
if (thirst >= 360) {
return 35;
}
if (thirst >= 240) {
return 25;
}
if (thirst >= 120) {
return 18;
}
if (thirst >= 90) {
return 10;
}
if (thirst >= 60) {
return 5;
}
return 0;
}
Stats Entity::get_thirst_stats_penalty()
{
Stats ret;
if (thirst >= 360) {
ret.strength = -3;
ret.dexterity = -3;
ret.intelligence = -3;
ret.perception = -3;
} else if (thirst >= 240) {
ret.strength = -3;
ret.dexterity = -2;
ret.intelligence = -3;
ret.perception = -2;
} else if (thirst >= 120) {
ret.strength = -2;
ret.dexterity = -1;
ret.intelligence = -2;
ret.perception = -1;
} else if (thirst >= 90) {
ret.strength = -1;
ret.intelligence = -1;
} else if (thirst >= 60) {
ret.intelligence = -1;
}
return ret;
}
int Entity::get_fatigue_speed_penalty()
{
// Note that we're "tired" starting at 160; the first four hours have no penalty
if (fatigue >= 360) {
return 35;
}
if (fatigue >= 320) {
return 25;
}
if (fatigue >= 280) {
return 15;
}
if (fatigue >= 240) {
return 10;
}
if (fatigue >= 200) {
return 5;
}
return 0;
}
Stats Entity::get_fatigue_stats_penalty()
{
Stats ret;
if (fatigue >= 360) {
ret.strength = -3;
ret.dexterity = -2;
ret.intelligence = -3;
ret.perception = -3;
} else if (fatigue >= 320) {
ret.strength = -3;
ret.dexterity = -1;
ret.intelligence = -3;
ret.perception = -2;
} else if (fatigue >= 280) {
ret.strength = -2;
ret.dexterity = -1;
ret.intelligence = -3;
ret.perception = -2;
} else if (fatigue >= 240) {
ret.strength = -1;
ret.intelligence = -2;
ret.perception = -1;
} else if (fatigue >= 200) {
ret.intelligence = -1;
ret.perception = -1;
} else if (fatigue >= 160) {
ret.intelligence = -1;
}
return ret;
}
int Entity::get_net_pain()
{
int ret = pain;
ret -= painkill;
return (ret < 0 ? 0 : ret);
}
int Entity::get_pain_speed_penalty()
{
int p = get_net_pain();
p -= 16; // First 19 pain points don't incur a speed penalty
if (p <= 0) {
return 0;
}
int ret = p / 4;
return (ret > 50 ? 50 : ret);
}
// TODO: Overload for monsters?
int Entity::get_smell()
{
if (has_trait(TRAIT_SMELLY)) {
return 18;
}
return 10;
}
void Entity::gain_xp(int amount)
{
if (amount < 0) {
debugmsg("%s gained %d XP (negative)!",
get_name_to_player().c_str(), amount);
return;
}
experience += amount;
}
bool Entity::improve_skill(Skill_type type)
{
if (type == SKILL_NULL || type == SKILL_MAX) {
debugmsg("%s tried to improve %s.",
get_name_to_player().c_str(), skill_type_name(type).c_str());
return false;
}
if (skills.maxed_out(type)) {
return false;
}
int xp_needed = skills.improve_cost(type);
if (experience < xp_needed) {
return false;
}
experience -= xp_needed;
skills.increase_level(type);
return true;
}
void Entity::gain_morale(int amount)
{
morale += amount;
}
int Entity::get_morale()
{
return morale;
}
// TODO: This function. Once we have morale.
int Entity::personal_mission_cap()
{
return 3;
}
void Entity::assign_personal_missions(bool message)
{
int cap = personal_mission_cap();
int personal_missions = 0;
for (int i = 0; i < missions.size(); i++) {
if (missions[i].personal) {
personal_missions++;
}
}
std::stringstream ss_msg;
ss_msg << "<c=yellow>";
int new_missions = 0;
while (personal_missions < cap) {
Mission_template* m_temp = MISSIONS.random_instance();
if (!m_temp) {
debugmsg("Fetched a NULL Mission_template at random!");
debugmsg("%d templates exist.", MISSIONS.size());
return;
}
Mission miss;
miss.set_from_template(m_temp);
miss.personal = true;
missions.push_back(miss);
personal_missions++;
if (new_missions > 0) {
ss_msg << "; ";
}
new_missions++;
ss_msg << miss.get_description();
}
ss_msg << "<c=/>";
if (message && new_missions > 0) {
GAME.add_msg( (new_missions >= 2 ? "New missions:" : "New mission:") );
GAME.add_msg( ss_msg.str() );
}
}
bool Entity::check_mission(Mission_type type, std::string target, int count)
{
target = no_caps( trim( target ) );
bool found_match = false;
for (int i = 0; !found_match && i < missions.size(); i++) {
//debugmsg("target '%s', checking '%s'", target.c_str(), missions[i].target_name.c_str());
if (missions[i].type == type && missions[i].target_name == target) {
//debugmsg("Match! Count %d - %d = %d.", missions[i].target_count, count, missions[i].target_count - count);
found_match = true;
missions[i].target_count -= count;
if (missions[i].target_count <= 0) {
complete_mission(i);
}
}
}
// Clear out any now-completed missions.
clean_up_missions();
return true;
}
void Entity::complete_mission(int index)
{
if (index < 0 || index >= missions.size()) {
debugmsg("Entity::complete_mission(%d) called - we have %d missions.",
index, missions.size());
return;
}
if (missions[index].status != MISSION_STATUS_ACTIVE) {
debugmsg("Entity::complete_mission() called on non-active mission!");
return;
}
missions[index].status = MISSION_STATUS_COMPLETE;
GAME.add_msg("<c=yellow>Mission complete!<c=/>");
gain_experience(missions[index].xp);
}
void Entity::clean_up_missions()
{
for (int i = 0; i < missions.size(); i++) {
if (missions[i].status != MISSION_STATUS_ACTIVE) {
missions.erase( missions.begin() + i );
i--;
}
}
assign_personal_missions();
}
void Entity::gain_experience(int amount)
{
if (amount <= 0) {
return;
}
// Display a message when we unlock improvements
std::vector<bool> could_improve;
for (int i = 0; i < SKILL_MAX; i++) {
Skill_type sk = Skill_type(i);
int cost = skills.improve_cost(sk);
if (skills.maxed_out(sk) && has_trait(TRAIT_AUTODIDACT)) {
cost *= 2;
}
bool maxed = (skills.maxed_out(sk) && !has_trait(TRAIT_AUTODIDACT));
if (experience >= cost && !maxed) {
could_improve.push_back(true);
} else {
could_improve.push_back(false);
}
}
experience += amount;
GAME.add_msg("You gain %d experience!", amount);
std::vector<Skill_type> unlocked_skills;
for (int i = 0; i < SKILL_MAX; i++) {
Skill_type sk = Skill_type(i);
int cost = skills.improve_cost(sk);
if (skills.maxed_out(sk) && has_trait(TRAIT_AUTODIDACT)) {
cost *= 2;
}
bool maxed = (skills.maxed_out(sk) && !has_trait(TRAIT_AUTODIDACT));
if (i > 0 && !could_improve[i] && experience >= cost && !maxed) {
unlocked_skills.push_back(sk);
}
}
if (unlocked_skills.size() > 3) {
GAME.add_msg("You can improve %d new skills!", unlocked_skills.size());
} else if (!unlocked_skills.empty()) {
std::stringstream mes;
mes << "You can now improve ";
for (int i = 0; i < unlocked_skills.size(); i++) {
mes << skill_type_user_name( unlocked_skills[i] );
if (i == unlocked_skills.size() - 1) {
mes << "!";
} else if (i == unlocked_skills.size() - 2) {
mes << " and ";
} else {
mes << ", ";
}
}
GAME.add_msg(mes.str());
}
}
void Entity::take_turn()
{
}
bool Entity::is_enemy(Entity* ent)
{
if (!ent) {
return false;
}
// Not us, and not in our genus.
return (ent->uid != uid && ent->get_genus_uid() != get_genus_uid());
}
bool Entity::try_goal(AI_goal goal)
{
return false;
}
bool Entity::pick_attack_victim()
{
return false;
}
bool Entity::pick_flee_target()
{
return false;
}
bool Entity::has_target()
{
return (plan.target_entity);
}
bool Entity::is_fleeing()
{
return (plan.is_active() && plan.goal_type == AIGOAL_FLEE);
}
std::vector<Ranged_attack> Entity::get_ranged_attacks()
{
return std::vector<Ranged_attack>();
}
Ranged_attack Entity::pick_ranged_attack(Entity* target)
{
std::vector<Ranged_attack> ra = get_ranged_attacks();
if (ra.empty()) {
return Ranged_attack();
}
int total_chance = 0;
std::vector<Ranged_attack> used;
for (int i = 0; i < ra.size(); i++) {
if (target && rl_dist(pos, target->pos) > ra[i].range) {
ra.erase(ra.begin() + i);
i--;
} else {
total_chance += ra[i].weight;
}
}
if (ra.empty()) { // No attacks in range!
return Ranged_attack();
}
int index = rng(1, total_chance);
for (int i = 0; i < ra.size(); i++) {
index -= ra[i].weight;
if (index <= 0) {
return ra[i];
}
}
return ra.back();
}
Entity_AI Entity::get_AI()
{
return Entity_AI();
}
bool Entity::has_sense(Sense_type sense)
{
return false;
}
bool Entity::has_trait(Trait_id trait)
{
if (trait >= traits.size()) {
debugmsg("Entity '%s' has traits.size() %d (should be %d)!",
get_name().c_str(), traits.size(), TRAIT_MAX);
return false;
}
return traits[trait];
}
int Entity::get_genus_uid()
{
return -2;
}
int Entity::get_hunger_minimum()
{
return -80;
}
int Entity::get_thirst_minimum()
{
return -40;
}
int Entity::get_stomach_maximum()
{
return 60;
}
int Entity::sight_range(int light_level)
{
int ret = light_level;
if (has_trait(TRAIT_NIGHT_VISION)) {
if (ret < 3) {
ret = ret * 2;
} else if (ret == 3) {
ret = 4;
}
}
if (has_trait(TRAIT_MYOPIC) && ret > 6 &&
!is_wearing_item_flag(ITEM_FLAG_GLASSES)) {
ret = 6;
}
return ret;
}
bool Entity::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;
}