/
attack.c
1185 lines (948 loc) · 27.2 KB
/
attack.c
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
/*
* File: attack.c
* Purpose: Attacking (both throwing and melee) code
*
* Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
*
* This work is free software; you can redistribute it and/or modify it
* under the terms of either:
*
* a) the GNU General Public License as published by the Free Software
* Foundation, version 2, or
*
* b) the "Angband licence":
* This software may be copied and distributed for educational, research,
* and not for profit purposes provided that this copyright and statement
* are included in all such copies. Other copyrights may also apply.
*/
#include "angband.h"
#include "cave.h"
#include "cmds.h"
#include "game-cmd.h"
#include "monster/monster.h"
#include "object/object.h"
#include "object/tvalsval.h"
#include "spells.h"
#include "target.h"
/* Returns percent chance of an object breaking after throwing or shooting. */
int breakage_chance(const object_type *o_ptr)
{
/* Artifacts never break */
if (artifact_p(o_ptr)) return 0;
switch (o_ptr->tval)
{
case TV_FLASK:
case TV_POTION:
case TV_BOTTLE:
case TV_FOOD:
case TV_JUNK:
return 100;
case TV_LIGHT:
case TV_SCROLL:
case TV_SKELETON:
return 50;
case TV_ARROW:
return 35;
case TV_WAND:
case TV_SHOT:
case TV_BOLT:
case TV_SPIKE:
return 25;
default:
return 10;
}
}
/*
* Determine if the player "hits" a monster.
*
* Note -- Always miss 5%, always hit 5%, otherwise random.
*/
bool test_hit(int chance, int ac, int vis)
{
int k;
/* Percentile dice */
k = randint0(100);
/* Hack -- Instant miss or hit */
if (k < 10) return (k < 5);
/* Penalize invisible targets */
if (!vis) chance = chance / 2;
/* Power competes against armor */
if ((chance > 0) && (randint0(chance) >= (ac * 3 / 4))) return (TRUE);
/* Assume miss */
return (FALSE);
}
/*
* Critical hits (from objects thrown by player)
* Factor in item weight, total plusses, and player level.
*/
static int critical_shot(int weight, int plus, int dam, u32b *msg_type)
{
int i, k;
/* Extract "shot" power */
i = (weight + ((p_ptr->state.to_h + plus) * 4) + (p_ptr->lev * 2));
/* Critical hit */
if (randint1(5000) <= i)
{
k = weight + randint1(500);
if (k < 500)
{
*msg_type = MSG_HIT_GOOD;
dam = 2 * dam + 5;
}
else if (k < 1000)
{
*msg_type = MSG_HIT_GREAT;
dam = 2 * dam + 10;
}
else
{
*msg_type = MSG_HIT_SUPERB;
dam = 3 * dam + 15;
}
}
else
*msg_type = MSG_SHOOT_HIT;
return (dam);
}
/*
* Critical hits (by player)
*
* Factor in weapon weight, total plusses, player level.
*/
static int critical_norm(int weight, int plus, int dam, u32b *msg_type)
{
int i, k;
/* Extract "blow" power */
i = (weight + ((p_ptr->state.to_h + plus) * 5) + (p_ptr->lev * 3));
/* Chance */
if (randint1(5000) <= i)
{
k = weight + randint1(650);
if (k < 400)
{
*msg_type = MSG_HIT_GOOD;
dam = 2 * dam + 5;
}
else if (k < 700)
{
*msg_type = MSG_HIT_GREAT;
dam = 2 * dam + 10;
}
else if (k < 900)
{
*msg_type = MSG_HIT_SUPERB;
dam = 3 * dam + 15;
}
else if (k < 1300)
{
*msg_type = MSG_HIT_HI_GREAT;
dam = 3 * dam + 20;
}
else
{
*msg_type = MSG_HIT_HI_SUPERB;
dam = ((7 * dam) / 2) + 25;
}
}
else
*msg_type = MSG_HIT;
return dam;
}
/**
* Extract the multiplier from a given object hitting a given monster.
*
* If there is a slay or brand in effect, change the verb for hitting
* to something interesting ('burn', 'smite', etc.). Also, note which
* flags had an effect in o_ptr->known_flags[].
*
* \param o_ptr is the object being used to attack
* \param m_ptr is the monster being attacked
* \param best_s_ptr is the best applicable slay_table entry, or NULL if no
* slay already known
*
*/
void improve_attack_modifier(object_type *o_ptr, const monster_type *m_ptr, const slay_t **best_s_ptr)
{
const slay_t *s_ptr;
monster_race *r_ptr = &r_info[m_ptr->r_idx];
monster_lore *l_ptr = &l_list[m_ptr->r_idx];
bitflag f[OF_SIZE], known_f[OF_SIZE];
object_flags(o_ptr, f);
object_flags_known(o_ptr, known_f);
for (s_ptr = slay_table; s_ptr->slay_flag; s_ptr++)
{
if (!of_has(f, s_ptr->slay_flag)) continue;
/* Learn about monster resistance/vulnerability IF:
* 1) The slay flag on the object is known OR
* 2) The monster does not possess the appropriate resistance flag OR
* 3) The monster does possess the appropriate vulnerability flag
*/
if (of_has(known_f, s_ptr->slay_flag) ||
(s_ptr->monster_flag && rf_has(r_ptr->flags, s_ptr->monster_flag)) ||
(s_ptr->resist_flag && !rf_has(r_ptr->flags, s_ptr->resist_flag)))
{
if (m_ptr->ml && s_ptr->monster_flag)
{
rf_on(l_ptr->flags, s_ptr->monster_flag);
}
if (m_ptr->ml && s_ptr->resist_flag)
{
rf_on(l_ptr->flags, s_ptr->resist_flag);
}
}
/* If the monster doesn't match or the slay flag does */
if ((s_ptr->brand && !rf_has(r_ptr->flags, s_ptr->resist_flag)) ||
rf_has(r_ptr->flags, s_ptr->monster_flag))
{
/* notice any brand or slay that would affect the monster */
object_notice_slay(o_ptr, s_ptr->slay_flag);
/* compare multipliers to determine best attack, would be more complex for O-style brands */
if ((*best_s_ptr == NULL) || ((*best_s_ptr)->mult < s_ptr->mult))
*best_s_ptr = s_ptr;
}
}
}
/*
* Attack the monster at the given location
*
* If no "weapon" is available, then "punch" the monster one time.
*
* We get blows until energy drops below that required for another blow, or
* until the target monster dies. We use a wrapper to work out the number of
* blows. We don't allow @ to spend more than 100 energy in one go, to avoid
* slower monsters getting double moves.
*/
bool py_attack_real(int y, int x)
{
int bonus, chance;
monster_type *m_ptr = &mon_list[cave_m_idx[y][x]];
monster_race *r_ptr = &r_info[m_ptr->r_idx];
monster_lore *l_ptr = &l_list[m_ptr->r_idx];
object_type *o_ptr;
char m_name[80];
bool fear = FALSE;
bool do_quake = FALSE;
bool dead = FALSE;
u32b msg_type = 0;
bool success = FALSE;
/* Default to punching for one damage */
const char *hit_verb = "punch";
int dmg = 1;
msg_type = MSG_HIT;
/* Extract monster name (or "it") */
monster_desc(m_name, sizeof(m_name), m_ptr, 0);
/* Auto-Recall if possible and visible */
if (m_ptr->ml) monster_race_track(m_ptr->r_idx);
/* Track a new monster */
if (m_ptr->ml) health_track(cave_m_idx[y][x]);
/* Handle player fear (only for invisible monsters) */
if (p_ptr->state.afraid)
{
message_format(MSG_AFRAID, 0,
"You are too afraid to attack %s!",
m_name);
return (FALSE);
}
/* Disturb the monster */
wake_monster(m_ptr);
/* Get the weapon */
o_ptr = &p_ptr->inventory[INVEN_WIELD];
/* Calculate the "attack quality" */
bonus = p_ptr->state.to_h + o_ptr->to_h;
chance = (p_ptr->state.skills[SKILL_TO_HIT_MELEE] + (bonus * BTH_PLUS_ADJ));
/* See if the player hit */
success = test_hit(chance, r_ptr->ac, m_ptr->ml);
/* If a miss, skip this hit */
if (!success)
{
message_format(MSG_MISS, m_ptr->r_idx, "You miss %s.", m_name);
return (FALSE);
}
/* Handle normal weapon */
if (o_ptr->k_idx)
{
int i;
const slay_t *best_s_ptr = NULL;
hit_verb = "hit";
/* Get the best attack from all slays or
* brands on all non-launcher equipment */
for (i = INVEN_LEFT; i < INVEN_TOTAL; i++)
improve_attack_modifier(&p_ptr->inventory[i], m_ptr,
&best_s_ptr);
improve_attack_modifier(o_ptr, m_ptr, &best_s_ptr);
if (best_s_ptr != NULL)
hit_verb = best_s_ptr->melee_verb;
dmg = damroll(o_ptr->dd, o_ptr->ds);
dmg *= (best_s_ptr == NULL) ? 1 : best_s_ptr->mult;
if (p_ptr->state.impact && (dmg > 50))
do_quake = TRUE;
dmg += o_ptr->to_d;
dmg = critical_norm(o_ptr->weight, o_ptr->to_h, dmg, &msg_type);
/* Learn by use for the weapon */
object_notice_attack_plusses(o_ptr);
if (do_quake)
wieldeds_notice_flag(OF_IMPACT);
}
/* Learn by use for other equipped items */
wieldeds_notice_on_attack();
/* Apply the player damage bonuses */
dmg += p_ptr->state.to_d;
/* No negative damage */
if (dmg <= 0) dmg = 0;
/* Tell the player what happened */
if (dmg <= 0)
message_format(MSG_MISS, m_ptr->r_idx,
"You fail to harm %s.", m_name);
else if (msg_type == MSG_HIT)
message_format(MSG_HIT, m_ptr->r_idx, "You %s %s.",
hit_verb, m_name);
else if (msg_type == MSG_HIT_GOOD)
message_format(MSG_HIT_GOOD, m_ptr->r_idx, "You %s %s. %s",
hit_verb, m_name, "It was a good hit!");
else if (msg_type == MSG_HIT_GREAT)
message_format(MSG_HIT_GREAT, m_ptr->r_idx, "You %s %s. %s",
hit_verb, m_name, "It was a great hit!");
else if (msg_type == MSG_HIT_SUPERB)
message_format(MSG_HIT_SUPERB, m_ptr->r_idx, "You %s %s. %s",
hit_verb, m_name, "It was a superb hit!");
else if (msg_type == MSG_HIT_HI_GREAT)
message_format(MSG_HIT_HI_GREAT, m_ptr->r_idx, "You %s %s. %s",
hit_verb, m_name, "It was a *GREAT* hit!");
else if (msg_type == MSG_HIT_HI_SUPERB)
message_format(MSG_HIT_HI_SUPERB, m_ptr->r_idx, "You %s %s. %s",
hit_verb, m_name, "It was a *SUPERB* hit!");
/* Complex message */
if (p_ptr->wizard)
msg_format("You do %d (out of %d) damage.", dmg, m_ptr->hp);
/* Confusion attack */
if (p_ptr->confusing)
{
/* Cancel glowing hands */
p_ptr->confusing = FALSE;
/* Message */
msg_print("Your hands stop glowing.");
/* Update the lore */
if (m_ptr->ml)
rf_on(l_ptr->flags, RF_NO_CONF);
/* Confuse the monster */
if (rf_has(r_ptr->flags, RF_NO_CONF))
msg_format("%^s is unaffected.", m_name);
else if (randint0(100) < r_ptr->level)
msg_format("%^s is unaffected.", m_name);
else
{
msg_format("%^s appears confused.", m_name);
m_ptr->confused += 10 + randint0(p_ptr->lev) / 5;
}
}
/* Damage, check for fear and death */
dead = mon_take_hit(cave_m_idx[y][x], dmg, &fear, NULL);
/* Hack -- delay fear messages */
if (fear && m_ptr->ml)
message_format(MSG_FLEE, m_ptr->r_idx, "%^s flees in terror!",
m_name);
/* Mega-Hack -- apply earthquake brand */
if (do_quake) earthquake(p_ptr->py, p_ptr->px, 10);
return (dead);
}
void py_attack(int y, int x)
{
int blow_energy;
bool stop = FALSE;
int blows = 0;
/* disturb the player */
disturb(0,0);
/* calculate energy per blow */
blow_energy = 10000 / p_ptr->state.num_blow;
/*
* set energy use to zero, overriding whatever was set before we
* got here
*/
p_ptr->energy_use = 0;
/*
* take blows until energy runs out or monster dies -
* note that we limit energy use to 100, even if an extra blow would
* be possible, to avoid monster double moves
*/
while ((p_ptr->energy >= (blow_energy * (blows + 1))) && !stop)
{
stop = py_attack_real(y, x);
p_ptr->energy_use += blow_energy;
if ((p_ptr->energy_use + blow_energy) > 100) stop = TRUE;
blows++;
}
}
/*
* Fire an object from the pack or floor.
*
* You may only fire items that "match" your missile launcher.
*
* See "calc_bonuses()" for more calculations and such.
*
* Note that "firing" a missile is MUCH better than "throwing" it.
*
* Note: "unseen" monsters are very hard to hit.
*
* Objects are more likely to break if they "attempt" to hit a monster.
*
* Rangers (with Bows) and Anyone (with "Extra Shots") get extra shots.
* The "extra shot" code works by decreasing the amount of energy
* required to make each shot, spreading the shots out over time.
*
* Note that when firing missiles, the launcher multiplier is applied
* after all the bonuses are added in, making multipliers very useful.
*
* Note that Bows of "Extra Might" get extra range and an extra bonus
* for the damage multiplier.
*/
void do_cmd_fire(cmd_code code, cmd_arg args[])
{
int dir, item;
int i, j, y, x;
s16b ty, tx;
int tdam, tdis, thits;
int bonus, chance;
object_type *o_ptr;
object_type *j_ptr;
object_type *i_ptr;
object_type object_type_body;
bool hit_body = FALSE;
byte missile_attr;
char missile_char;
char o_name[80];
u32b msg_type = 0;
int path_n;
u16b path_g[256];
int msec = op_ptr->delay_factor * op_ptr->delay_factor;
/* Get the "bow" */
j_ptr = &p_ptr->inventory[INVEN_BOW];
/* Require a usable launcher */
if (!j_ptr->tval || !p_ptr->state.ammo_tval)
{
msg_print("You have nothing to fire with.");
return;
}
/* Get item to fire and direction to fire in. */
item = args[0].item;
dir = args[1].direction;
/* Check the item being fired is usable by the player. */
if (!item_is_available(item, NULL, (USE_EQUIP | USE_INVEN | USE_FLOOR)))
{
msg_format("That item is not within your reach.");
return;
}
/* Get the object for the ammo */
o_ptr = object_from_item_idx(item);
/* Check the ammo can be used with the launcher */
if (o_ptr->tval != p_ptr->state.ammo_tval)
{
msg_format("That ammo cannot be fired by your current weapon.");
return;
}
/* Base range XXX XXX */
tdis = 6 + 2 * p_ptr->state.ammo_mult;
/* Start at the player */
x = p_ptr->px;
y = p_ptr->py;
/* Predict the "target" location */
ty = y + 99 * ddy[dir];
tx = x + 99 * ddx[dir];
/* Check for target validity */
if ((dir == 5) && target_okay())
{
target_get(&tx, &ty);
if (distance(y, x, ty, tx) > tdis)
{
if (!get_check("Target out of range. Fire anyway? "))
return;
}
}
/* Sound */
sound(MSG_SHOOT);
object_notice_on_firing(o_ptr);
/* Describe the object */
object_desc(o_name, sizeof(o_name), o_ptr, ODESC_FULL | ODESC_SINGULAR);
/* Find the color and symbol for the object for throwing */
missile_attr = object_attr(o_ptr);
missile_char = object_char(o_ptr);
/* Use the proper number of shots */
thits = p_ptr->state.num_fire;
/* Actually "fire" the object */
bonus = (p_ptr->state.to_h + o_ptr->to_h + j_ptr->to_h);
chance = p_ptr->state.skills[SKILL_TO_HIT_BOW] +
(bonus * BTH_PLUS_ADJ);
/* Take a (partial) turn */
p_ptr->energy_use = (100 / thits);
/* Calculate the path */
path_n = project_path(path_g, tdis, y, x, ty, tx, 0);
/* Hack -- Handle stuff */
handle_stuff();
/* Project along the path */
for (i = 0; i < path_n; ++i)
{
int ny = GRID_Y(path_g[i]);
int nx = GRID_X(path_g[i]);
/* Hack -- Stop before hitting walls */
if (!cave_floor_bold(ny, nx)) break;
/* Advance */
x = nx;
y = ny;
/* Only do visuals if the player can "see" the missile */
if (player_can_see_bold(y, x))
{
/* Visual effects */
print_rel(missile_char, missile_attr, y, x);
move_cursor_relative(y, x);
Term_fresh();
if (p_ptr->redraw) redraw_stuff();
Term_xtra(TERM_XTRA_DELAY, msec);
light_spot(y, x);
Term_fresh();
if (p_ptr->redraw) redraw_stuff();
}
/* Delay anyway for consistency */
else
{
/* Pause anyway, for consistancy */
Term_xtra(TERM_XTRA_DELAY, msec);
}
/* Handle monster */
if (cave_m_idx[y][x] > 0)
{
monster_type *m_ptr = &mon_list[cave_m_idx[y][x]];
monster_race *r_ptr = &r_info[m_ptr->r_idx];
int chance2 = chance - distance(p_ptr->py, p_ptr->px, y, x);
int visible = m_ptr->ml;
int multiplier = 1;
const char *hit_verb = "hits";
const slay_t *best_s_ptr = NULL;
/* Note the collision */
hit_body = TRUE;
/* Did we hit it (penalize distance travelled) */
if (test_hit(chance2, r_ptr->ac, m_ptr->ml))
{
bool fear = FALSE;
/* Assume a default death */
cptr note_dies = " dies.";
improve_attack_modifier(o_ptr, m_ptr, &best_s_ptr);
improve_attack_modifier(j_ptr, m_ptr, &best_s_ptr);
if (best_s_ptr != NULL)
hit_verb = best_s_ptr->range_verb;
/* Some monsters get "destroyed" */
if (monster_is_unusual(r_ptr))
{
/* Special note at death */
note_dies = " is destroyed.";
}
/* Calculate multiplier */
multiplier = p_ptr->state.ammo_mult;
if (best_s_ptr != NULL) multiplier += best_s_ptr->mult;
/* Apply damage: multiplier, slays, criticals, bonuses */
tdam = damroll(o_ptr->dd, o_ptr->ds);
tdam += o_ptr->to_d + j_ptr->to_d;
tdam *= multiplier;
tdam = critical_shot(o_ptr->weight, o_ptr->to_h, tdam, &msg_type);
object_notice_attack_plusses(o_ptr);
object_notice_attack_plusses(&p_ptr->inventory[INVEN_BOW]);
/* No negative damage; change verb if no damage done */
if (tdam <= 0)
{
tdam = 0;
hit_verb = "fail to harm";
}
/* Handle unseen monster */
if (!visible)
{
/* Invisible monster */
message_format(MSG_SHOOT_HIT, 0, "The %s finds a mark.", o_name);
}
/* Handle visible monster */
else
{
char m_name[80];
/* Get "the monster" or "it" */
monster_desc(m_name, sizeof(m_name), m_ptr, 0);
/* Tell the player what happened */
if (msg_type == MSG_SHOOT_HIT)
message_format(MSG_SHOOT_HIT, 0, "The %s %s %s.", o_name, hit_verb, m_name);
else
{
if (msg_type == MSG_HIT_GOOD)
{
message_format(MSG_HIT_GOOD, 0, "The %s %s %s. %s", o_name, hit_verb, m_name,
"It was a good hit!");
}
else if (msg_type == MSG_HIT_GREAT)
{
message_format(MSG_HIT_GREAT, 0, "The %s %s %s. %s", o_name, hit_verb, m_name,
"It was a great hit!");
}
else if (msg_type == MSG_HIT_SUPERB)
{
message_format(MSG_HIT_SUPERB, 0, "The %s %s %s. %s", o_name, hit_verb, m_name,
"It was a superb hit!");
}
}
/* Hack -- Track this monster race */
if (m_ptr->ml) monster_race_track(m_ptr->r_idx);
/* Hack -- Track this monster */
if (m_ptr->ml) health_track(cave_m_idx[y][x]);
}
/* Complex message */
if (p_ptr->wizard)
{
msg_format("You do %d (out of %d) damage.",
tdam, m_ptr->hp);
}
/* Hit the monster, check for death */
if (mon_take_hit(cave_m_idx[y][x], tdam, &fear, note_dies))
{
/* Dead monster */
}
/* No death */
else
{
/* Message */
message_pain(cave_m_idx[y][x], tdam);
/* Take note */
if (fear && m_ptr->ml)
{
char m_name[80];
/* Get the monster name (or "it") */
monster_desc(m_name, sizeof(m_name), m_ptr, 0);
/* Message */
message_format(MSG_FLEE, m_ptr->r_idx,
"%^s flees in terror!", m_name);
}
}
}
/* Stop looking */
break;
}
}
/* Get local object */
i_ptr = &object_type_body;
/* Obtain a local object */
object_copy(i_ptr, o_ptr);
/* Single object */
i_ptr->number = 1;
if (item >= 0)
{
inven_item_increase(item, -1);
inven_item_describe(item);
inven_item_optimize(item);
}
/* Reduce and describe floor item */
else
{
floor_item_increase(0 - item, -1);
floor_item_optimize(0 - item);
}
/* Chance of breakage (during attacks) */
j = (hit_body ? breakage_chance(i_ptr) : 0);
/* Drop (or break) near that location */
drop_near(i_ptr, j, y, x, TRUE);
}
void textui_cmd_fire_at_nearest(void)
{
/* the direction '5' means 'use the target' */
int i, dir = 5, item = -1;
/* Require a usable launcher */
if (!p_ptr->inventory[INVEN_BOW].tval || !p_ptr->state.ammo_tval)
{
msg_print("You have nothing to fire with.");
return;
}
/* Find first eligible ammo in the quiver */
for (i = QUIVER_START; i < QUIVER_END; i++)
{
if (p_ptr->inventory[i].tval != p_ptr->state.ammo_tval) continue;
item = i;
break;
}
/* Require usable ammo */
if (item < 0)
{
msg_print("You have no ammunition in the quiver to fire");
return;
}
/* Require foe */
if (!target_set_closest(TARGET_KILL | TARGET_QUIET))
return;
/* Check for confusion */
if (p_ptr->timed[TMD_CONFUSED])
{
msg_print("You are confused.");
dir = ddd[randint0(8)];
}
/* Fire! */
cmd_insert(CMD_FIRE);
cmd_set_arg_item(cmd_get_top(), 0, item);
cmd_set_arg_target(cmd_get_top(), 1, dir);
}
/*
* Throw an object from the pack or floor.
*
* Note: "unseen" monsters are very hard to hit.
*
* Should throwing a weapon do full damage? Should it allow the magic
* to hit bonus of the weapon to have an effect? Should it ever cause
* the item to be destroyed? Should it do any damage at all?
*/
void do_cmd_throw(cmd_code code, cmd_arg args[])
{
int dir, item;
int i, j, y, x;
s16b ty, tx;
int chance, tdam, tdis;
int weight;
object_type *o_ptr;
object_type *i_ptr;
object_type object_type_body;
bool hit_body = FALSE;
byte missile_attr;
char missile_char;
char o_name[80];
u32b msg_type = 0;
int path_n;
u16b path_g[256];
int msec = op_ptr->delay_factor * op_ptr->delay_factor;
/* Get item to throw and direction in which to throw it. */
item = args[0].item;
dir = args[1].direction;
/* Make sure the player isn't throwing wielded items */
if (item >= INVEN_WIELD && item < QUIVER_START)
{
msg_print("You have cannot throw wielded items.");
return;
}
/* Check the item being thrown is usable by the player. */
if (!item_is_available(item, NULL, (USE_EQUIP | USE_INVEN | USE_FLOOR)))
{
msg_format("That item is not within your reach.");
return;
}
/* Get the object */
o_ptr = object_from_item_idx(item);
object_notice_on_firing(o_ptr);
/* Get local object */
i_ptr = &object_type_body;
/* Obtain a local object */
object_copy(i_ptr, o_ptr);
/* Distribute the charges of rods/wands/staves between the stacks */
distribute_charges(o_ptr, i_ptr, 1);
/* Single object */
i_ptr->number = 1;
/* Reduce and describe inventory */
if (item >= 0)
{
inven_item_increase(item, -1);
inven_item_describe(item);
inven_item_optimize(item);
}
/* Reduce and describe floor item */
else
{
floor_item_increase(0 - item, -1);
floor_item_optimize(0 - item);
}
/* Description */
object_desc(o_name, sizeof(o_name), i_ptr, ODESC_FULL);
/* Find the color and symbol for the object for throwing */
missile_attr = object_attr(i_ptr);
missile_char = object_char(i_ptr);
/* Enforce a minimum "weight" of one pound */
weight = ((i_ptr->weight > 10) ? i_ptr->weight : 10);
/* Hack -- Distance -- Reward strength, penalize weight */
tdis = (adj_str_blow[p_ptr->state.stat_ind[A_STR]] + 20) * 10 / weight;
/* Max distance of 10 */
if (tdis > 10) tdis = 10;
/* Hack -- Base damage from thrown object */
tdam = damroll(i_ptr->dd, i_ptr->ds);
if (!tdam) tdam = 1;
tdam += i_ptr->to_d;
/* Chance of hitting */
chance = (p_ptr->state.skills[SKILL_TO_HIT_THROW] + (p_ptr->state.to_h * BTH_PLUS_ADJ));
/* Take a turn */
p_ptr->energy_use = 100;
/* Start at the player */
y = p_ptr->py;
x = p_ptr->px;
/* Predict the "target" location */
ty = p_ptr->py + 99 * ddy[dir];
tx = p_ptr->px + 99 * ddx[dir];
/* Check for "target request" */
if ((dir == 5) && target_okay())
{
target_get(&tx, &ty);
}
/* Calculate the path */
path_n = project_path(path_g, tdis, p_ptr->py, p_ptr->px, ty, tx, 0);
/* Hack -- Handle stuff */
handle_stuff();
/* Project along the path */
for (i = 0; i < path_n; ++i)
{
int ny = GRID_Y(path_g[i]);
int nx = GRID_X(path_g[i]);
/* Hack -- Stop before hitting walls */
if (!cave_floor_bold(ny, nx)) break;
/* Advance */
x = nx;
y = ny;
/* Only do visuals if the player can "see" the missile */
if (player_can_see_bold(y, x))
{
/* Visual effects */