-
Notifications
You must be signed in to change notification settings - Fork 89
/
npcmove.cpp
1983 lines (1802 loc) · 57.8 KB
/
npcmove.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 <sstream>
#include "npc.h"
#include "rng.h"
#include "game.h"
#include "line.h"
#define TARGET_PLAYER -2
// A list of items used for escape, in order from least to most valuable
#ifndef NUM_ESCAPE_ITEMS
#define NUM_ESCAPE_ITEMS 11
itype_id ESCAPE_ITEMS[NUM_ESCAPE_ITEMS] = {
itm_cola, itm_caffeine, itm_energy_drink, itm_canister_goo, itm_smokebomb,
itm_smokebomb_act, itm_adderall, itm_coke, itm_meth, itm_teleporter,
itm_pheromone
};
#endif
// A list of alternate attack items (e.g. grenades), from least to most valuable
#ifndef NUM_ALT_ATTACK_ITEMS
#define NUM_ALT_ATTACK_ITEMS 16
itype_id ALT_ATTACK_ITEMS[NUM_ALT_ATTACK_ITEMS] = {
itm_knife_combat, itm_spear_wood, itm_molotov, itm_pipebomb, itm_grenade,
itm_gasbomb, itm_bot_manhack, itm_tazer, itm_dynamite, itm_mininuke,
itm_molotov_lit, itm_pipebomb_act, itm_grenade_act, itm_gasbomb_act,
itm_dynamite_act, itm_mininuke_act
};
#endif
std::string npc_action_name(npc_action action);
bool thrown_item(item *used);
// Used in npc::drop_items()
struct ratio_index
{
double ratio;
int index;
ratio_index(double R, int I) : ratio (R), index (I) {};
};
// class npc functions!
void npc::move(game *g)
{
npc_action action = npc_undecided;
int danger = 0, total_danger = 0, target = -1;
choose_monster_target(g, target, danger, total_danger);
if (g->debugmon)
debugmsg("NPC %s: target = %d, danger = %d, range = %d",
name.c_str(), target, danger, confident_range(-1));
if (is_enemy()) {
int pl_danger = player_danger( &(g->u) );
if (pl_danger > danger || target == -1) {
target = TARGET_PLAYER;
danger = pl_danger;
if (g->debugmon)
debugmsg("NPC %s: Set target to PLAYER, danger = %d", name.c_str(), danger);
}
}
// TODO: Place player-aiding actions here, with a weight
//if (!bravery_check(danger) || !bravery_check(total_danger) ||
if (target == TARGET_PLAYER && attitude == NPCATT_FLEE)
action = method_of_fleeing(g, target);
else if (danger > 0 || (target == TARGET_PLAYER && attitude == NPCATT_KILL))
action = method_of_attack(g, target, danger);
else { // No present danger
action = address_needs(g, danger);
if (g->debugmon)
debugmsg("address_needs %s", npc_action_name(action).c_str());
if (action == npc_undecided)
action = address_player(g);
if (g->debugmon)
debugmsg("address_player %s", npc_action_name(action).c_str());
if (action == npc_undecided) {
if (mission == NPC_MISSION_SHELTER || has_disease(DI_INFECTION))
action = npc_pause;
else if (has_new_items)
action = scan_new_items(g, target);
else if (!fetching_item)
find_item(g);
if (g->debugmon)
debugmsg("find_item %s", npc_action_name(action).c_str());
if (fetching_item) // Set to true if find_item() found something
action = npc_pickup;
else if (is_following()) // No items, so follow the player?
action = npc_follow_player;
else // Do our long-term action
action = long_term_goal_action(g);
if (g->debugmon)
debugmsg("long_term_goal_action %s", npc_action_name(action).c_str());
}
}
/* Sometimes we'll be following the player at this point, but close enough that
* "following" means standing still. If that's the case, if there are any
* monsters around, we should attack them after all!
*/
if (action == npc_follow_player && danger > 0 &&
rl_dist(posx, posy, g->u.posx, g->u.posy) <= follow_distance())
action = method_of_attack(g, target, danger);
if (g->debugmon)
debugmsg("%s chose action %s.", name.c_str(), npc_action_name(action).c_str());
execute_action(g, action, target);
}
void npc::execute_action(game *g, npc_action action, int target)
{
int oldmoves = moves;
int tarx = posx, tary = posy;
int linet, light = g->light_level();
if (target == -2) {
tarx = g->u.posx;
tary = g->u.posy;
} else if (target >= 0) {
tarx = g->z[target].posx;
tary = g->z[target].posy;
}
/*
debugmsg("%s ran execute_action() with target = %d! Action %s",
name.c_str(), target, npc_action_name(action).c_str());
*/
std::vector<point> line;
if (tarx != posx || tary != posy) {
int linet, dist = sight_range(g->light_level());
if (g->m.sees(posx, posy, tarx, tary, dist, linet))
line = line_to(posx, posy, tarx, tary, linet);
else
line = line_to(posx, posy, tarx, tary, 0);
}
switch (action) {
case npc_pause:
move_pause();
break;
case npc_reload: {
moves -= weapon.reload_time(*this);
int ammo_index = weapon.pick_reload_ammo(*this, false);
if (!weapon.reload(*this, ammo_index))
debugmsg("NPC reload failed.");
recoil = 6;
if (g->u_see(posx, posy, linet))
g->add_msg("%s reloads %s %s.", name.c_str(), (male ? "his" : "her"),
weapon.tname().c_str());
} break;
case npc_sleep:
/* TODO: Open a dialogue with the player, allowing us to ask if it's alright if
* we get some sleep, how long watch shifts should be, etc.
*/
//add_disease(DI_LYING_DOWN, 300, g);
if (is_friend() && g->u_see(posx, posy, linet))
say(g, "I'm going to sleep.");
break;
case npc_pickup:
pick_up_item(g);
break;
case npc_escape_item:
use_escape_item(g, choose_escape_item(), target);
break;
case npc_wield_melee:
wield_best_melee(g);
break;
case npc_wield_loaded_gun:
{
int index = -1, max = 0;
for (int i = 0; i < inv.size(); i++) {
if (inv[i].is_gun() && inv[i].charges > max) {
max = inv[i].charges;
index = i;
}
}
if (index == -1) {
debugmsg("NPC tried to wield a loaded gun, but has none!");
move_pause();
} else
wield(g, index);
} break;
case npc_wield_empty_gun:
{
bool ammo_found = false;
int index = -1;
for (int i = 0; i < inv.size(); i++) {
bool am = (inv[i].is_gun() &&
has_ammo( (dynamic_cast<it_gun*>(inv[i].type))->ammo ).size() > 0);
if (inv[i].is_gun() && (!ammo_found || am)) {
index = i;
ammo_found = (ammo_found || am);
}
}
if (index == -1) {
debugmsg("NPC tried to wield a gun, but has none!");
move_pause();
} else
wield(g, index);
} break;
case npc_heal:
heal_self(g);
break;
case npc_use_painkiller:
use_painkiller(g);
break;
case npc_eat:
pick_and_eat(g);
break;
case npc_drop_items:
/*
drop_items(g, weight_carried() - weight_capacity() / 4,
volume_carried() - volume_capacity());
*/
move_pause();
break;
case npc_flee:
// TODO: More intelligent fleeing
move_away_from(g, tarx, tary);
break;
case npc_melee:
update_path(g, tarx, tary);
if (path.size() > 1)
move_to_next(g);
else if (path.size() == 1) {
if (target >= 0)
melee_monster(g, target);
else if (target == TARGET_PLAYER)
melee_player(g, g->u);
} else
look_for_player(g, g->u);
break;
case npc_shoot:
g->fire(*this, tarx, tary, line, false);
break;
case npc_shoot_burst:
g->fire(*this, tarx, tary, line, true);
break;
case npc_alt_attack:
alt_attack(g, target);
break;
case npc_look_for_player:
if (saw_player_recently() && g->m.sees(posx, posy, plx, ply, light, linet)) {
// (plx, ply) is the point where we last saw the player
update_path(g, plx, ply);
move_to_next(g);
} else
look_for_player(g, g->u);
break;
case npc_heal_player:
update_path(g, g->u.posx, g->u.posy);
if (path.size() == 1) // We're adjacent to u, and thus can heal u
heal_player(g, g->u);
else if (path.size() > 0)
move_to_next(g);
else
move_pause();
break;
case npc_follow_player:
update_path(g, g->u.posx, g->u.posy);
if (path.size() <= follow_distance()) // We're close enough to u.
move_pause();
else if (path.size() > 0)
move_to_next(g);
else
move_pause();
break;
case npc_talk_to_player:
talk_to_u(g);
moves = 0;
break;
case npc_mug_player:
update_path(g, g->u.posx, g->u.posy);
if (path.size() == 1) // We're adjacent to u, and thus can mug u
mug_player(g, g->u);
else if (path.size() > 0)
move_to_next(g);
else
move_pause();
break;
case npc_goto_destination:
go_to_destination(g);
break;
case npc_avoid_friendly_fire:
avoid_friendly_fire(g, target);
break;
default:
debugmsg("Unknown NPC action (%d)", action);
}
if (oldmoves == moves) {
debugmsg("NPC didn't use its moves. Action %d. Turning on debug mode.", action);
g->debugmon = true;
}
}
void npc::choose_monster_target(game *g, int &enemy, int &danger,
int &total_danger)
{
int linet = 0;
bool defend_u = g->sees_u(posx, posy, linet) && is_defending();
int highest_priority = 0;
total_danger = 0;
for (int i = 0; i < g->z.size(); i++) {
monster *mon = &(g->z[i]);
if (g->pl_sees(this, mon, linet)) {
int distance = (100 * rl_dist(posx, posy, mon->posx, mon->posy)) /
mon->speed;
double hp_percent = (mon->type->hp - mon->hp) / mon->type->hp;
int priority = mon->type->difficulty * (1 + hp_percent) - distance;
int monster_danger = (mon->type->difficulty * mon->hp) / mon->type->hp;
if (!mon->is_fleeing(*this))
monster_danger++;
if (mon->friendly != 0) {
priority = -999;
monster_danger *= -1;
}/* else if (mon->speed < current_speed(g)) {
priority -= 10;
monster_danger -= 10;
} else
priority *= 1 + (.1 * distance);
*/
total_danger += int(monster_danger / (distance == 0 ? 1 : distance));
bool okay_by_rules = true;
if (is_following()) {
switch (combat_rules.engagement) {
case ENGAGE_NONE:
okay_by_rules = false;
break;
case ENGAGE_CLOSE:
okay_by_rules = (distance <= 6);
break;
case ENGAGE_WEAK:
okay_by_rules = (mon->hp <= average_damage_dealt());
break;
case ENGAGE_HIT:
okay_by_rules = (mon->has_effect(ME_HIT_BY_PLAYER));
break;
}
}
if (okay_by_rules && monster_danger > danger) {
danger = monster_danger;
if (enemy == -1) {
highest_priority = priority;
enemy = i;
}
}
if (okay_by_rules && priority > highest_priority) {
highest_priority = priority;
enemy = i;
} else if (okay_by_rules && defend_u) {
priority = mon->type->difficulty * (1 + hp_percent);
distance = (100 * rl_dist(g->u.posx, g->u.posy, mon->posx, mon->posy)) /
mon->speed;
priority -= distance;
if (mon->speed < current_speed(g))
priority -= 10;
priority *= (personality.bravery + personality.altruism + op_of_u.value) /
15;
if (priority > highest_priority) {
highest_priority = priority;
enemy = i;
}
}
}
}
}
npc_action npc::method_of_fleeing(game *g, int enemy)
{
int speed = (enemy == TARGET_PLAYER ? g->u.current_speed(g) :
g->z[enemy].speed);
point enemy_loc = (enemy == TARGET_PLAYER ? point(g->u.posx, g->u.posy) :
point(g->z[enemy].posx, g->z[enemy].posy));
int distance = rl_dist(posx, posy, enemy_loc.x, enemy_loc.y);
if (choose_escape_item() >= 0) // We have an escape item!
return npc_escape_item;
if (speed > 0 && (100 * distance) / speed <= 4 && speed > current_speed(g))
return method_of_attack(g, enemy, -1); // Can't outrun, so attack
return npc_flee;
}
npc_action npc::method_of_attack(game *g, int target, int danger)
{
int tarx = posx, tary = posy;
bool can_use_gun = (!is_following() || combat_rules.use_guns),
can_use_grenades = (!is_following() || combat_rules.use_grenades);
if (target == TARGET_PLAYER) {
tarx = g->u.posx;
tary = g->u.posy;
} else if (target >= 0) {
tarx = g->z[target].posx;
tary = g->z[target].posy;
} else { // This function shouldn't be called...
debugmsg("Ran npc::method_of_attack without a target!");
return npc_pause;
}
int dist = rl_dist(posx, posy, tarx, tary), target_HP;
if (target == TARGET_PLAYER)
target_HP = g->u.hp_percentage() * g->u.hp_max[hp_torso];
else
target_HP = g->z[target].hp;
if (can_use_gun) {
if (need_to_reload() && can_reload())
return npc_reload;
if (emergency(danger_assessment(g)) && alt_attack_available(g))
return npc_alt_attack;
if (weapon.is_gun() && weapon.charges > 0) {
it_gun* gun = dynamic_cast<it_gun*>(weapon.type);
if (dist > confident_range()) {
if (can_reload() && enough_time_to_reload(g, target, weapon))
return npc_reload;
else
return npc_melee;
}
if (!wont_hit_friend(g, tarx, tary))
return npc_avoid_friendly_fire;
else if (dist <= confident_range() / 3 && weapon.charges >= gun->burst &&
gun->burst > 1 &&
(target_HP >= weapon.curammo->damage * 3 || emergency(danger * 2)))
return npc_shoot_burst;
else
return npc_shoot;
}
}
// Check if there's something better to wield
bool has_empty_gun = false, has_better_melee = false;
std::vector<int> empty_guns;
for (int i = 0; i < inv.size(); i++) {
if (can_use_gun && inv[i].is_gun() && inv[i].charges > 0)
return npc_wield_loaded_gun;
else if (can_use_gun && inv[i].is_gun() &&
enough_time_to_reload(g, target, inv[i])) {
has_empty_gun = true;
empty_guns.push_back(i);
} else if (inv[i].melee_value(sklevel) > weapon.melee_value(sklevel) * 1.1)
has_better_melee = true;
}
bool has_ammo_for_empty_gun = false;
for (int i = 0; i < empty_guns.size(); i++) {
for (int j = 0; j < inv.size(); j++) {
if (inv[j].is_ammo() &&
inv[j].ammo_type() == inv[ empty_guns[i] ].ammo_type())
has_ammo_for_empty_gun = true;
}
}
if (has_empty_gun && has_ammo_for_empty_gun)
return npc_wield_empty_gun;
else if (has_better_melee)
return npc_wield_melee;
return npc_melee;
}
npc_action npc::address_needs(game *g, int danger)
{
if (has_healing_item()) {
for (int i = 0; i < num_hp_parts; i++) {
hp_part part = hp_part(i);
if ((part == hp_head && hp_cur[i] <= 35) ||
(part == hp_torso && hp_cur[i] <= 25) ||
hp_cur[i] <= 15)
return npc_heal;
}
}
if (has_painkiller() && !took_painkiller() && pain - pkill >= 15)
return npc_use_painkiller;
if (can_reload())
return npc_reload;
if ((danger <= NPC_DANGER_VERY_LOW && (hunger > 40 || thirst > 40)) ||
thirst > 80 || hunger > 160)
return npc_eat;
/*
if (weight_carried() > weight_capacity() / 4 ||
volume_carried() > volume_capacity())
return npc_drop_items;
*/
/*
if (danger <= 0 && fatigue > 191)
return npc_sleep;
*/
// TODO: Mutation & trait related needs
// e.g. finding glasses; getting out of sunlight if we're an albino; etc.
return npc_undecided;
}
npc_action npc::address_player(game *g)
{
int linet;
if ((attitude == NPCATT_TALK || attitude == NPCATT_TRADE) &&
g->sees_u(posx, posy, linet)) {
if (rl_dist(posx, posy, g->u.posx, g->u.posy) <= 6)
return npc_talk_to_player; // Close enough to talk to you
else {
if (one_in(10))
say(g, "<lets_talk>");
return npc_follow_player;
}
}
if (attitude == NPCATT_MUG && g->sees_u(posx, posy, linet)) {
if (one_in(3))
say(g, "Don't move a <swear> muscle...");
return npc_mug_player;
}
if (attitude == NPCATT_WAIT_FOR_LEAVE) {
patience--;
if (patience <= 0) {
patience = 0;
attitude = NPCATT_KILL;
return method_of_attack(g, TARGET_PLAYER, player_danger( &(g->u) ));
}
return npc_undecided;
}
if (attitude == NPCATT_FLEE)
return npc_flee;
if (attitude == NPCATT_LEAD) {
if (rl_dist(posx, posy, g->u.posx, g->u.posy) >= 12 ||
!g->sees_u(posx, posy, linet)) {
int intense = disease_intensity(DI_CATCH_UP);
if (intense < 10) {
say(g, "<keep_up>");
add_disease(DI_CATCH_UP, 5, g, 1, 15);
return npc_pause;
} else if (intense == 10) {
say(g, "<im_leaving_you>");
add_disease(DI_CATCH_UP, 5, g, 1, 15);
return npc_pause;
} else
return npc_goto_destination;
} else
return npc_goto_destination;
}
return npc_undecided;
}
npc_action npc::long_term_goal_action(game *g)
{
if (g->debugmon)
debugmsg("long_term_goal_action()");
path.clear();
if (mission == NPC_MISSION_SHOPKEEP || mission == NPC_MISSION_SHELTER)
return npc_pause; // Shopkeeps just stay put.
// TODO: Follow / look for player
if (!has_destination())
set_destination(g);
return npc_goto_destination;
return npc_undecided;
}
bool npc::alt_attack_available(game *g)
{
for (int i = 0; i < NUM_ALT_ATTACK_ITEMS; i++) {
if ((!is_following() || combat_rules.use_grenades ||
!g->itypes[ALT_ATTACK_ITEMS[i]]->item_flags & mfb(IF_GRENADE)) &&
has_amount(ALT_ATTACK_ITEMS[i], 1))
return true;
}
return false;
}
int npc::choose_escape_item()
{
int best = -1, ret = -1;
for (int i = 0; i < inv.size(); i++) {
for (int j = 0; j < NUM_ESCAPE_ITEMS; j++) {
it_comest* food = NULL;
if (inv[i].is_food())
food = dynamic_cast<it_comest*>(inv[i].type);
if (inv[i].type->id == ESCAPE_ITEMS[j] &&
(food == NULL || stim < food->stim || // Avoid guzzling down
(food->stim >= 10 && stim < food->stim * 2)) && // Adderall etc.
(j > best || (j == best && inv[i].charges < inv[ret].charges))) {
ret = i;
best = j;
j = NUM_ESCAPE_ITEMS;
}
}
}
return ret;
}
void npc::use_escape_item(game *g, int index, int target)
{
if (index < 0 || index >= inv.size()) {
debugmsg("%s tried to use item %d (%d in inv)", name.c_str(), index,
inv.size());
move_pause();
return;
}
/* There is a static list of items that NPCs consider to be "escape items," so
* we can just use a switch here to decide what to do based on type. See
* ESCAPE_ITEMS, defined in npc.h
*/
item* used = &(inv[index]);
if (used->is_food() || used->is_food_container()) {
eat(g, index);
return;
}
if (used->is_tool()) {
it_tool* tool = dynamic_cast<it_tool*>(used->type);
iuse use;
(use.*tool->use)(g, this, used, false);
used->charges -= tool->charges_per_use;
if (used->invlet == 0)
inv.remove_item(index);
return;
}
debugmsg("NPC tried to use %s (%d) but it has no use?", used->tname().c_str(),
index);
move_pause();
}
// Index defaults to -1, i.e., wielded weapon
int npc::confident_range(int index)
{
if (index == -1 && (!weapon.is_gun() || weapon.charges <= 0))
return 1;
double deviation = 0;
int max = 0;
if (index == -1) {
it_gun* firing = dynamic_cast<it_gun*>(weapon.type);
// We want at least 50% confidence that missed_by will be < .5.
// missed_by = .00325 * deviation * range <= .5; deviation * range <= 156
// (range <= 156 / deviation) is okay, so confident range is (156 / deviation)
// Here we're using median values for deviation, for a around-50% estimate.
// See game::fire (ranged.cpp) for where these computations come from
if (sklevel[firing->skill_used] < 5)
deviation += 3.5 * (5 - sklevel[firing->skill_used]);
else
deviation -= 2.5 * (sklevel[firing->skill_used] - 5);
if (sklevel[sk_gun] < 3)
deviation += 1.5 * (3 - sklevel[sk_gun]);
else
deviation -= .5 * (sklevel[sk_gun] - 3);
if (per_cur < 8)
deviation += 2 * (9 - per_cur);
else
deviation -= (per_cur > 16 ? 8 : per_cur - 8);
if (dex_cur < 6)
deviation += 4 * (6 - dex_cur);
else if (dex_cur < 8)
deviation += 8 - dex_cur;
else if (dex_cur > 8)
deviation -= .5 * (dex_cur - 8);
deviation += .5 * encumb(bp_torso) + 2 * encumb(bp_eyes);
if (weapon.curammo == NULL) // This shouldn't happen, but it does sometimes
debugmsg("%s has NULL curammo!", name.c_str()); // TODO: investigate this bug
else {
deviation += .5 * weapon.curammo->accuracy;
max = weapon.range();
}
deviation += .5 * firing->accuracy;
deviation += 3 * recoil;
} else { // We aren't firing a gun, we're throwing something!
item *thrown = &(inv[index]);
max = throw_range(index); // The max distance we can throw
int deviation = 0;
if (sklevel[sk_throw] < 8)
deviation += rng(0, 8 - sklevel[sk_throw]);
else
deviation -= sklevel[sk_throw] - 6;
deviation += throw_dex_mod();
if (per_cur < 6)
deviation += rng(0, 8 - per_cur);
else if (per_cur > 8)
deviation -= per_cur - 8;
deviation += rng(0, encumb(bp_hands) * 2 + encumb(bp_eyes) + 1);
if (thrown->volume() > 5)
deviation += rng(0, 1 + (thrown->volume() - 5) / 4);
if (thrown->volume() == 0)
deviation += rng(0, 3);
deviation += rng(0, 1 + abs(str_cur - thrown->weight()));
}
// Using 180 for now for extra-confident NPCs.
int ret = (max > int(180 / deviation) ? max : int(180 / deviation));
if (ret > weapon.curammo->range)
return weapon.curammo->range;
return ret;
}
// Index defaults to -1, i.e., wielded weapon
bool npc::wont_hit_friend(game *g, int tarx, int tary, int index)
{
int linet = 0, dist = sight_range(g->light_level());
int confident = confident_range(index);
if (rl_dist(posx, posy, tarx, tary) == 1)
return true; // If we're *really* sure that our aim is dead-on
std::vector<point> traj;
if (g->m.sees(posx, posy, tarx, tary, dist, linet))
traj = line_to(posx, posy, tarx, tary, linet);
else
traj = line_to(posx, posy, tarx, tary, 0);
for (int i = 0; i < traj.size(); i++) {
int dist = rl_dist(posx, posy, traj[i].x, traj[i].y);
int deviation = 1 + int(dist / confident);
for (int x = traj[i].x - deviation; x <= traj[i].x + deviation; x++) {
for (int y = traj[i].y - deviation; y <= traj[i].y + deviation; y++) {
// Hit the player?
if (is_friend() && g->u.posx == x && g->u.posy == y)
return false;
// Hit a friendly monster?
/*
for (int n = 0; n < g->z.size(); n++) {
if (g->z[n].friendly != 0 && g->z[n].posx == x && g->z[n].posy == y)
return false;
}
*/
// Hit an NPC that's on our team?
/*
for (int n = 0; n < g->active_npc.size(); n++) {
npc* guy = &(g->active_npc[n]);
if (guy != this && (is_friend() == guy->is_friend()) &&
guy->posx == x && guy->posy == y)
return false;
}
*/
}
}
}
return true;
}
bool npc::can_reload()
{
if (!weapon.is_gun())
return false;
it_gun* gun = dynamic_cast<it_gun*> (weapon.type);
return (weapon.charges < gun->clip && has_ammo(gun->ammo).size() > 0);
}
bool npc::need_to_reload()
{
if (!weapon.is_gun())
return false;
it_gun* gun = dynamic_cast<it_gun*> (weapon.type);
return (weapon.charges < gun->clip * .1);
}
bool npc::enough_time_to_reload(game *g, int target, item &gun)
{
int rltime = gun.reload_time(*this);
double turns_til_reloaded = rltime / current_speed(g);
int dist, speed, linet;
if (target == TARGET_PLAYER) {
if (g->sees_u(posx, posy, linet) && g->u.weapon.is_gun() && rltime > 200)
return false; // Don't take longer than 2 turns if player has a gun
dist = rl_dist(posx, posy, g->u.posx, g->u.posy);
speed = speed_estimate(g->u.current_speed(g));
} else if (target >= 0) {
dist = rl_dist(posx, posy, g->z[target].posx, g->z[target].posy);
speed = speed_estimate(g->z[target].speed);
} else
return true; // No target, plenty of time to reload
double turns_til_reached = (dist * 100) / speed;
return (turns_til_reloaded < turns_til_reached);
}
void npc::update_path(game *g, int x, int y)
{
if (path.empty()) {
path = g->m.route(posx, posy, x, y);
return;
}
point last = path[path.size() - 1];
if (last.x == x && last.y == y)
return; // Our path already leads to that point, no need to recalculate
path = g->m.route(posx, posy, x, y);
if (!path.empty() && path[0].x == posx && path[0].y == posy)
path.erase(path.begin());
}
bool npc::can_move_to(game *g, int x, int y)
{
if ((g->m.move_cost(x, y) > 0 || g->m.has_flag(bashable, x, y)) &&
rl_dist(posx, posy, x, y) <= 1)
return true;
return false;
}
void npc::move_to(game *g, int x, int y)
{
if (has_disease(DI_DOWNED)) {
moves -= 100;
return;
}
if (recoil > 0) { // Start by dropping recoil a little
if (int(str_cur / 2) + sklevel[sk_gun] >= recoil)
recoil = 0;
else {
recoil -= int(str_cur / 2) + sklevel[sk_gun];
recoil = int(recoil / 2);
}
}
if (has_disease(DI_STUNNED)) {
x = rng(posx - 1, posx + 1);
y = rng(posy - 1, posy + 1);
}
if (rl_dist(posx, posy, x, y) > 1) {
/*
debugmsg("Tried to move_to more than one space! (%d, %d) to (%d, %d)",
posx, posy, x, y);
debugmsg("Route is size %d.", path.size());
*/
int linet;
std::vector<point> newpath;
if (g->m.sees(posx, posy, x, y, -1, linet))
newpath = line_to(posx, posy, x, y, linet);
x = newpath[0].x;
y = newpath[0].y;
}
if (x == posx && y == posy) // We're just pausing!
moves -= 100;
else if (g->mon_at(x, y) != -1) { // Shouldn't happen, but it might.
monster *m = &(g->z[g->mon_at(x, y)]);
//debugmsg("Bumped into a monster, %d, a %s",g->mon_at(x, y),m->name().c_str());
melee_monster(g, g->mon_at(x, y));
} else if (g->u.posx == x && g->u.posy == y) {
say(g, "<let_me_pass>");
moves -= 100;
} else if (g->npc_at(x, y) != -1)
// TODO: Determine if it's an enemy NPC (hit them), or a friendly in the way
moves -= 100;
else if (g->m.move_cost(x, y) > 0) {
posx = x;
posy = y;
moves -= run_cost(g->m.move_cost(x, y) * 50);
} else if (g->m.open_door(x, y, (g->m.ter(posx, posy) == t_floor)))
moves -= 100;
else if (g->m.has_flag(bashable, x, y)) {
moves -= 110;
std::string bashsound;
int smashskill = int(str_cur / 2 + weapon.type->melee_dam);
g->m.bash(x, y, smashskill, bashsound);
g->sound(x, y, 18, bashsound);
} else
moves -= 100;
}
void npc::move_to_next(game *g)
{
if (path.empty()) {
debugmsg("npc::move_to_next() called with an empty path!");
move_pause();
return;
}
while (posx == path[0].x && posy == path[0].y)
path.erase(path.begin());
move_to(g, path[0].x, path[0].y);
if (posx == path[0].x && posy == path[0].y) // Move was successful
path.erase(path.begin());
}
// TODO: Rewrite this. It doesn't work well and is ugly.
void npc::avoid_friendly_fire(game *g, int target)
{
int tarx, tary;
if (target == TARGET_PLAYER) {
tarx = g->u.posx;
tary = g->u.posy;
} else if (target >= 0) {
tarx = g->z[target].posx;
tary = g->z[target].posy;
if (!one_in(3))
say(g, "<move> so I can shoot that %s!", g->z[target].name().c_str());
} else {
debugmsg("npc::avoid_friendly_fire() called with no target!");
move_pause();
return;
}
int xdir = (tarx > posx ? 1 : -1), ydir = (tary > posy ? 1 : -1);
direction dir_to_target = direction_from(posx, posy, tarx, tary);
std::vector<point> valid_moves;
/* Ugh, big ugly switch. This fills valid_moves with a list of moves from most
* desirable to least; the only two moves excluded are those along the line of
* sight.
* TODO: Use some math instead of a big ugly switch.
*/
switch (dir_to_target) {
case NORTH:
valid_moves.push_back(point(posx + xdir, posy));
valid_moves.push_back(point(posx - xdir, posy));
valid_moves.push_back(point(posx + xdir, posy + 1));
valid_moves.push_back(point(posx - xdir, posy + 1));
valid_moves.push_back(point(posx + xdir, posy - 1));
valid_moves.push_back(point(posx - xdir, posy - 1));
break;
case NORTHEAST:
valid_moves.push_back(point(posx + 1, posy + 1));
valid_moves.push_back(point(posx - 1, posy - 1));
valid_moves.push_back(point(posx - 1, posy ));
valid_moves.push_back(point(posx , posy + 1));
valid_moves.push_back(point(posx + 1, posy ));
valid_moves.push_back(point(posx , posy - 1));
break;
case EAST:
valid_moves.push_back(point(posx, posy - 1));
valid_moves.push_back(point(posx, posy + 1));
valid_moves.push_back(point(posx - 1, posy - 1));
valid_moves.push_back(point(posx - 1, posy + 1));
valid_moves.push_back(point(posx + 1, posy - 1));
valid_moves.push_back(point(posx + 1, posy + 1));
break;
case SOUTHEAST:
valid_moves.push_back(point(posx + 1, posy - 1));
valid_moves.push_back(point(posx - 1, posy + 1));
valid_moves.push_back(point(posx + 1, posy ));
valid_moves.push_back(point(posx , posy + 1));
valid_moves.push_back(point(posx - 1, posy ));
valid_moves.push_back(point(posx , posy - 1));
break;
case SOUTH:
valid_moves.push_back(point(posx + xdir, posy));
valid_moves.push_back(point(posx - xdir, posy));
valid_moves.push_back(point(posx + xdir, posy - 1));
valid_moves.push_back(point(posx - xdir, posy - 1));
valid_moves.push_back(point(posx + xdir, posy + 1));
valid_moves.push_back(point(posx - xdir, posy + 1));
break;
case SOUTHWEST: