This repository has been archived by the owner on Nov 28, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFight.cpp
2216 lines (2021 loc) · 69.6 KB
/
Fight.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
/***************************************************************
This file is for all the ranged weapon attack resolution code
****************************************************************/
#ifndef _WIN32
#include <unistd.h>
#endif
#include "Global.h"
#include "Util.h"
#include "Hero.h"
#include "Interface.h"
static shSpecialEffect
beamSpecialEffect (shAttack *atk, shDirection dir)
{
switch (atk->mType) {
case shAttack::kHeatRay:
case shAttack::kBreatheFire:
return kHeatEffect;
case shAttack::kBlast:
return kExplosionEffect;
case shAttack::kFlash:
return kRadiationEffect;
case shAttack::kFreezeRay:
return kColdEffect;
case shAttack::kPoisonRay:
return kPoisonEffect;
case shAttack::kDisintegrationRay:
return kDisintegrationEffect;
case shAttack::kBreatheTraffic:
return kBinaryEffect;
case shAttack::kBreatheBugs:
return kBugsEffect;
case shAttack::kBreatheViruses:
return kVirusesEffect;
case shAttack::kLaser:
case shAttack::kBolt:
switch (dir) {
case kNorthWest: case kSouthEast: return kLaserBeamBDiagEffect;
case kSouthWest: case kNorthEast: return kLaserBeamFDiagEffect;
case kEast: case kWest: return kLaserBeamHorizEffect;
case kNorth: case kSouth: return kLaserBeamVertEffect;
case kOrigin: default: return kLaserBeamEffect;
}
case shAttack::kRail:
switch (dir) {
case kNorthWest: case kSouthEast: return kRailBDiagEffect;
case kSouthWest: case kNorthEast: return kRailFDiagEffect;
case kEast: case kWest: return kRailHorizEffect;
case kNorth: case kSouth: return kRailVertEffect;
case kOrigin: default: return kRailEffect;
}
case shAttack::kBreatheTime:
case shAttack::kGammaRay:
case shAttack::kGaussRay:
case shAttack::kTransporterRay:
case shAttack::kStasisRay:
case shAttack::kHealingRay:
case shAttack::kRestorationRay:
default:
return kNone;
}
}
static const char *
beamHitsMesg (shAttack *atk)
{
switch (atk->mType) {
case shAttack::kBlast: return NULL; /*"The blast hits";*/
case shAttack::kBreatheFire: return "The fire engulfs";
case shAttack::kBreatheBugs: return "The cloud of bugs envelopes";
case shAttack::kBreatheViruses: return "The cloud of viruses envelopes";
case shAttack::kBreatheTime: return "The time warp envelopes";
case shAttack::kBreatheTraffic: return "The packet storm envelopes";
case shAttack::kDisintegrationRay:
return "The disintegration ray hits";
case shAttack::kFlash: return NULL;
case shAttack::kFreezeRay: return "The freeze ray hits";
case shAttack::kGammaRay: return NULL;
case shAttack::kGaussRay: return NULL;
case shAttack::kHeatRay: return "The heat ray hits";
case shAttack::kLaser: return "The laser beam hits";
case shAttack::kMentalBlast: return "The mental blast hits";
case shAttack::kPoisonRay: return "The poison ray hits";
case shAttack::kStasisRay: return "The stasis ray hits";
case shAttack::kTransporterRay: return NULL;
case shAttack::kHealingRay: return NULL;
case shAttack::kRestorationRay: return NULL;
}
return "it hits you";
}
static const char *
monHitsYouMesg (shAttack *atk)
{
switch (atk->mType) {
case shAttack::kNoAttack: return " doesn't attack";
case shAttack::kAttach: return " attaches an object to you";
case shAttack::kAnalProbe: return " probes you";
case shAttack::kBite: return " bites you";
case shAttack::kBlast: return " blasts you";
case shAttack::kBullet: return " shoots you";
case shAttack::kBolt: return " blasts you";
case shAttack::kClaw: return " claws you";
case shAttack::kClub: return " clubs you";
case shAttack::kChoke: return " chokes you";
case shAttack::kCook: return " cooks you";
case shAttack::kCrush: return " crushes you";
case shAttack::kCut: return " cuts you";
case shAttack::kDisintegrationRay:
return " zaps you with a disintegration ray";
case shAttack::kExtractBrain:
return NULL;
case shAttack::kFaceHug: return " attacks your face";
case shAttack::kFlash: return " blasts you with a bright light";
case shAttack::kFreezeRay: return " zaps you with a freeze ray";
case shAttack::kGammaRay: return " zaps you with a gamma ray";
case shAttack::kGaussRay: return " zaps you with a gauss ray";
case shAttack::kHeadButt: return " head butts you";
case shAttack::kHealingRay: return " zaps you with a healing ray";
case shAttack::kHeatRay: return " zaps you with a heat ray";
case shAttack::kKick: return " kicks you";
case shAttack::kLaser: return " zaps you with a laser beam";
case shAttack::kLegalThreat: return " raises an objection";
case shAttack::kMentalBlast: return " blasts your mind";
case shAttack::kPoisonRay: return "zaps you with a poison ray";
case shAttack::kPunch: return " punches you";
case shAttack::kQuill: return " sticks you with a quill";
case shAttack::kRail: return " rails you";
case shAttack::kRestorationRay: return " zaps you with a restoration ray";
case shAttack::kShot: return " shoots you";
case shAttack::kSlash: return " slashes you";
case shAttack::kSlime: return " slimes you";
case shAttack::kSmash: return " smacks you";
case shAttack::kStab: return " stabs you";
case shAttack::kStasisRay: return " zaps you with a stasis ray";
case shAttack::kSting: return " stings you";
case shAttack::kSword: return " slices you";
case shAttack::kTailSlap: return "'s tail whips you";
case shAttack::kTouch: return " touches you";
case shAttack::kTransporterRay: return " zaps you with a transporter ray";
case shAttack::kZap: return " zaps you";
}
return "hits you";
}
static const char *
monHitsMonMesg (shAttack *atk)
{
switch (atk->mType) {
case shAttack::kNoAttack: return " doesn't attack";
case shAttack::kAnalProbe: return " probes";
case shAttack::kAttach: return " attaches an object to";
case shAttack::kBite: return " bites";
case shAttack::kBlast: return " blasts";
case shAttack::kBullet: return " shoots";
case shAttack::kBolt: return " blasts";
case shAttack::kClaw: return " claws";
case shAttack::kClub: return " clubs";
case shAttack::kChoke: return " chokes";
case shAttack::kCook: return " cooks";
case shAttack::kCrush: return " crushes";
case shAttack::kCut: return " cuts";
case shAttack::kDisintegrationRay: return " zaps a disintegration ray at";
case shAttack::kExtractBrain: return " extracts the brain of";
case shAttack::kFaceHug: return " attacks the face of";
case shAttack::kFlash: return " flashes a bright light at";
case shAttack::kFreezeRay: return " zaps a freeze ray at";
case shAttack::kGammaRay: return " zaps a gamma ray at";
case shAttack::kGaussRay: return " zaps a gauss ray at";
case shAttack::kHeadButt: return " head butts";
case shAttack::kHealingRay: return " zaps a healing ray at";
case shAttack::kHeatRay: return " zaps a heat ray at";
case shAttack::kKick: return " kicks";
case shAttack::kLaser: return " zaps a laser beam at";
case shAttack::kMentalBlast: return " blasts the mind of";
case shAttack::kPoisonRay: return " zaps a poison ray at";
case shAttack::kPunch: return " punches";
case shAttack::kQuill: return " quills";
case shAttack::kRail: return " rails";
case shAttack::kRestorationRay: return " zaps a restoration ray at";
case shAttack::kShot: return " shoots";
case shAttack::kSlash: return " slashes";
case shAttack::kSlime: return " slimes";
case shAttack::kSmash: return " smacks";
case shAttack::kStab: return " stabs";
case shAttack::kStasisRay: return " zaps a stasis ray at";
case shAttack::kSting: return " stings";
case shAttack::kSword: return " slices";
case shAttack::kTailSlap: return "'s tail whips";
case shAttack::kTouch: return " touches";
case shAttack::kTransporterRay: return " zaps a transporter ray at";
case shAttack::kZap: return " zaps";
}
return " hits";
}
static shDirection
reflectBackDir (shDirection dir) {
dir = uTurn (dir);
switch (RNG (4)) {
case 0: return leftTurn (dir);
case 1: return rightTurn (dir);
default: return dir;
}
}
/* returns: the bonus to AC afforded by the supplied cover percentage */
static int
CoverACBonus (int cover)
{
if (cover > 0) {
/* expanded interpretation of SRD allows for more ranges of cover */
if (cover <= 15) { return 1; }
else if (cover <= 25) { return 2; }
else if (cover <= 40) { return 3; }
else if (cover <= 50) { return 4; }
else if (cover <= 60) { return 5; }
else if (cover <= 70) { return 6; }
else if (cover <= 75) { return 7; }
else if (cover <= 80) { return 8; }
else if (cover <= 85) { return 9; }
else if (cover <= 90) { return 10; }
else { return cover - 80; }
} else {
return 0;
}
}
/* returns: 1 if the attack misses due to visibility problem
0 otherwise
*/
static int
VisibilityMiss (int vis)
{
if (vis < 100) {
int misschance;
if (vis >= 75) { misschance = 10; }
else if (vis >= 50) { misschance = 20; }
else if (vis >= 25) { misschance = 30; }
else if (vis >= 10) { misschance = 40; }
else { misschance = 50; }
if (RNG (100) < misschance) {
I->diag ("visibility miss! (chance was %d%%)", misschance);
return 1;
}
}
return 0;
}
/* roll to save against the attack
returns: 0 if save failed, true if made
*/
int
shCreature::reflexSave (shAttack *attack, int DC)
{
int result = RNG (1, 20) + mReflexSaveBonus + ABILITY_MODIFIER (getAgi ());
if (isAsleep () || isSessile ()) {
return 0;
}
if (Hero.isLucky ()) {
if (isHero () || isPet ()) {
result += RNG (1, 7);
} else {
result -= RNG (1, 7);
}
}
return result >= DC;
}
/* roll to hit the target
returns: -x for a miss (returns the amount missed by)
1 for a hit
2+ for a critical (returns the appropriate damage multiplier)
*/
int
shCreature::attackRoll (shAttack *attack, shObject *weapon,
int attackmod, int AC, shCreature *target)
{
int result = RNG (1, 20);
int dmul = 1;
int threatrange = weapon ? weapon->getThreatRange (target) : 20;
int critmult = weapon ? weapon->getCriticalMultiplier () : 2;
const char *thetarget = target->the ();
if (1 == result) { /* rolling a 1 always results in a miss */
I->diag ("attacking %s: rolled a 1 against AC %d.", thetarget, AC);
return -99;
}
if (20 == result ||
result + attackmod >= AC)
{ /* hit */
if ((!target->isImmuneToCriticalHits ()) &&
(result >= threatrange))
{ /* critical threat */
int threat = RNG (1, 20);
if ((1 != threat) &&
((20 == threat) || (threat + attackmod >= AC)))
{ /* critical hit */
dmul = critmult;
I->diag ("attacking %s: rolled %d, then %d+%d=%d against "
"AC %d: critical hit, dmg mult %d!",
thetarget, result, threat, attackmod,
threat + attackmod, AC, dmul);
} else {
I->diag ("attacking %s: rolled %d, then %d+%d=%d against AC %d",
thetarget, result, threat, attackmod,
threat + attackmod, AC);
}
} else {
I->diag ("attacking %s: rolled a %d+%d=%d against AC %d",
thetarget, result, attackmod, result + attackmod, AC);
}
return dmul;
}
I->diag ("attacking %s: rolled a %d+%d=%d against AC %d",
thetarget, result, attackmod, result + attackmod, AC);
return result + attackmod - AC;
}
/* determine if a ranged attack hits.
returns: -x for a miss (returns amount missed by)
1 for a hit
2+ for a critical hit (returns damage multiplier)
modifies: adds calculated damage bonuses to *dbonus
*/
int
shCreature::rangedAttackHits (shAttack *attack,
shObject *weapon,
int attackmod,
shCreature *target,
int *dbonus)
{
int AC;
int flatfooted = 0; /* set to 1 if the target is unable to react to the
attack due to unawareness or other disability */
int cover;
int vis;
int range = distance (target, this);
int wrange = attack->mRange;
int maxrange;
*dbonus += mDamageModifier;
if (weapon) {
*dbonus += weapon->mEnhancement;
attackmod += ((shWeaponIlk *) weapon->mIlk) -> mToHitBonus;
}
if (!attack->isMeleeAttack () &&
target->hasShield () &&
target->countEnergy ())
{ /* almost always hits the targets shield and the target gets no
benefit from concealment */
attackmod += 100;
} else if (target->mConcealment && RNG (100) < target->mConcealment) {
/* should telepathy counter concealment? */
I->debug ("missed due to concealment");
return -20;
}
vis = canSee (target, &cover);
if (attack->isMissileAttack ()) {
maxrange = 10 * wrange; /* for thrown weapons, it's actually 2d4
squares. see throwObject() for details */
if (weapon->isThrownWeapon ()) {
attackmod += getWeaponSkillModifier (weapon->mIlk);
attackmod += weapon->mEnhancement;
attackmod -= 2 * weapon->mDamage;
} else {
/* improvised missile weapon */
attackmod += ABILITY_MODIFIER (getDex ());
//FIX: assess stiff penalties for large or impractical missiles
}
} else {
maxrange = 10 * wrange;
if (!weapon) {
/* psionic or innate attack - caller should have already
added relevant skill bonus to attackmod */
} else if (weapon->isA (kRayGun)) {
attackmod += mBAB + 4; /* ray guns are normally pretty reliable */
attackmod += mToHitModifier;
maxrange = 99999; /* range checking handled by beam code */
if (weapon->isOptimized ()) {
attackmod += 2;
}
} else {
attackmod += getWeaponSkillModifier (weapon->mIlk);
attackmod += weapon->mEnhancement;
attackmod -= 2 * weapon->mDamage;
if (weapon->isOptimized ()) {
attackmod += 2;
}
}
}
if (0 == target->canSee (this)) {
attackmod += 2;
flatfooted = 1;
}
if (target->isStunned ()) {
flatfooted = 1;
attackmod += 2;
}
if (target->isAsleep () || target->isParalyzed () ||
target->isSessile ())
{
flatfooted = 1;
attackmod += 4;
}
if (range > maxrange) {
return -20;
}
if (shAttack::kShot == attack->mType) {
attackmod += 2 * (range / wrange);
*dbonus -= 4 * (range / wrange);
} else {
attackmod -= 2 * (range / wrange);
}
if (attack->isTouchAttack ()) {
AC = target->getTouchAC (flatfooted, this);
} else {
AC = target->getAC (flatfooted, this);
}
if (cover >= 100) {
/* normally it would be impossible to see the target if there is
100% cover, unless the cover is transparent (e.g. a window) */
return -20;
} else {
AC += CoverACBonus (cover);
}
if (VisibilityMiss(vis)) {
return -20;
}
/* roll to hit */
return attackRoll (attack, weapon, attackmod, AC, target);
}
/* works out the ranged attack against the target
returns: 1 if the target is eliminated (e.g. it dies, teleports away, etc.)
0 if the target is hit but not eliminated
-1 if the attack was a complete miss
-2 if the attack was reflected
*/
int
shCreature::resolveRangedAttack (shAttack *attack,
shObject *weapon,
int attackmod,
shCreature *target)
{
const char *n_attacker;
const char *an_attacker;
const char *n_target;
const char *n_weapon;
int dbonus = 0;
int dmul = -1;
int cantsee = 1;
if (isHero ()) {
n_attacker = "you";
cantsee = 0;
} else if (Hero.canSee (this)) {
n_attacker = the ();
cantsee = 0;
} else {
n_attacker = "something";
}
if (target->isHero ()) {
n_target = "you";
cantsee = 0;
} else if (Hero.canSee (target)) {
n_target = target->the ();
cantsee = 0;
} else {
n_target = "something";
}
an_attacker = an ();
if (weapon) {
n_weapon = weapon->theQuick ();
} else if (&OpticBlastAttack == attack) {
if (isHero ())
n_weapon = "your laser beam";
else
n_weapon = "the laser blast";
} else {
n_weapon = "it";
}
/* work out hit/miss */
dmul = rangedAttackHits (attack, weapon, attackmod, target, &dbonus);
if (dmul <= 0) {
//FIXME: determine if the attack hit armor or just plain missed
dmul = -1;
goto youmiss;
}
/* successful hit */
if (target->mHidden) {
target->revealSelf ();
}
if (attack->isMissileAttack () && weapon) {
//I->p ("%s is hit!", n_target);
exerciseWeaponSkill (weapon, 1);
weapon->impact (target,
vectorDirection (mX, mY, target->mX, target->mY),
this);
return 0; /* FIX */
}
if (isHero () &&!target->isHero ()) {
if (dmul > 1) {
I->p ("You hit %s!", n_target);
} else {
I->p ("You hit %s.", n_target);
}
if (weapon) {
/* FIX: doesn't exercise unarmed combat.
(anyways, exercise has been phased out for now...) */
exerciseWeaponSkill (weapon, 1);
}
if (target->sufferDamage (attack, this, dbonus, dmul)) {
target->pDeathMessage (n_target, kSlain);
if (!target->isHero ()) {
Hero.earnXP (target->mCLevel);
}
target->die (kSlain);
return 1;
} else {
target->newEnemy (this);
target->interrupt ();
return 0;
}
} else if (target->isHero ()) {
if (dmul > 1) {
I->p ("You are hit!");
} else {
I->p ("You are hit.");
}
if (target->sufferDamage (attack, this, dbonus, dmul)) {
target->die (kSlain, this);
return 1;
}
target->interrupt ();
return 0;
} else {
if (target->sufferDamage (attack, this, dbonus, dmul)) {
if (!cantsee)
target->pDeathMessage (n_target, kSlain);
target->die (kSlain);
return 1;
} else if (dmul > 1) {
if (!cantsee) {
I->p ("%s is hit!", n_target);
}
} else {
if (!cantsee) {
I->p ("%s is hit.", n_target);
}
}
if (isPet ()) {
/* monsters will tolerate friendly-fire */
target->newEnemy (this);
}
target->interrupt ();
return 0;
}
youmiss:
if (attack->isMissileAttack ()) {
if (target->isHero ()) {
I->p ("%s misses you!", n_weapon);
} else {
I->p ("%s misses %s.", n_weapon, n_target);
}
} else if (&OpticBlastAttack == attack) {
//if (isHero () && target->mHidden <= 0) {
// I->p ("Your laser beam misses %s.", n_target);
//}
} else {
if (kNone == beamSpecialEffect (attack, kOrigin)) {
if (isHero () && target->mHidden <= 0 && canSee (target)) {
I->p ("You miss %s.", n_target);
} /* else if (target->isHero ()) {
I->p ("%s misses.", n_attacker);
} else {
I->p ("%s misses %s", n_attacker, n_target);
} */
}
}
target->interrupt ();
target->newEnemy (this);
return -1;
}
/* work out the result of firing the weapon in the given direction
returns: ms elapsed, -2 if attacker dies
*/
int
shCreature::shootWeapon (shObject *weapon, shDirection dir)
{
int numrounds = 0;
int x, y;
int firsttarget = 1;
shAttack *attack = NULL;
int setemptyraygun = 0;
assert (weapon);
Hero.interrupt ();
if (weapon->isA (kWeapon)) {
attack = & ((shWeaponIlk *) weapon->mIlk) -> mAttack;
numrounds = expendAmmo (weapon);
} else if (weapon->isA (kRayGun)) {
attack = & ((shRayGunIlk *) weapon->mIlk) -> mAttack;
if (weapon->mCharges) {
numrounds = 1;
--weapon->mCharges;
if (!weapon->mCharges && weapon->isChargeKnown ()) {
++setemptyraygun;
}
} else {
numrounds = 0;
}
}
if (0 == numrounds) {
if (isHero ()) {
if (weapon->isA (kRayGun)) {
weapon->mIlk = findAnIlk (&RayGunIlks, "empty ray gun");
weapon->setChargeKnown ();
weapon->setIlkKnown ();
weapon->setAppearanceKnown ();
I->p ("Nothing happens.");
} else {
I->p ("You're out of ammo!");
}
}
return 200; /* this wastes some time */
}
if (!isHero ()) {
int knownappearance = weapon->isAppearanceKnown ();
weapon->setAppearanceKnown ();
if (Hero.canSee (this)) {
I->p ("%s shoots %s!", the (), weapon->her (this));
} else {
const char *a_weap = weapon->anVague ();
if (!Hero.isBlind () && Level->isInLOS (mX, mY)) {
/* muzzle flash gives away position */
I->p ("Someone shoots %s!", a_weap);
Level->feelSq (mX, mY);
} else {
I->p ("You hear someone shooting %s!", a_weap);
}
if (!knownappearance) {
weapon->resetAppearanceKnown ();
}
}
} else if (!isBlind ()) {
/* some weapons identify themselves when shot */
if (weapon->isA ("heat ray gun") ||
weapon->isA ("freeze ray gun") ||
weapon->isA ("disintegration ray gun") ||
weapon->isA ("stasis ray gun") ||
weapon->isA ("poison ray gun"))
{
weapon->setIlkKnown ();
}
}
if (weapon->isBuggy () && !RNG (5)) {
if (weapon->isA (kRayGun) && RNG (3)) {
int died;
if (isHero ()) {
I->p ("Your ray gun explodes!");
} else if (Hero.canSee (this)) {
I->p ("%s's ray gun explodes!", the ());
} else {
I->p ("You hear an explosion");
}
/* this is tricky, hope I got it right:
1. delete the weapon first, so no risk of double-deletion due to
it somehow getting destroyed by a secondary explosion effect
2. kludgily borrow and modify the weapon's own attack structure
to save typing in new exploding ray gun attacks
3. remember to return -2 if the attacker is killed in the
explosion, because higher level code will handle deletion
*/
removeObjectFromInventory (weapon);
delete weapon;
attack->mEffect = shAttack::kBurst;
died = mLevel->areaEffect(attack, NULL, mX, mY, kNoDirection,
this, 0);
attack->mEffect = shAttack::kBeam;
return died ? -2 : 1000;
}
if (isHero ()) {
weapon->setBugginessKnown ();
I->p ("Your weapon misfires!");
} else if (Hero.canSee (this)) {
I->p ("%s's weapon misfires!", the ());
weapon->setBugginessKnown ();
} else {
I->p ("You hear a weapon misfire.");
}
return 1000;
}
switch (attack->mEffect) {
case shAttack::kSingle:
if (kOrigin == dir) {
if (isHero ()) {
I->p ("You shoot yourself.");
}
if (sufferDamage (attack, this, 0, 1)) {
die (kSuicide);
return -2;
}
return attack->mAttackTime;
}
else if (kUp == dir) {
if (isHero ()) {
I->p ("You shoot at the ceiling.");
}
return attack->mAttackTime;
} else if (kDown == dir) {
if (isHero ()) {
I->p ("You shoot at the floor.");
}
return attack->mAttackTime;
}
if (-1 == numrounds) { /* pea shooter */
numrounds = 1;
}
while (numrounds--) {
int timeout = 100;
x = mX;
y = mY;
firsttarget = 1;
while (Level->moveForward (dir, &x, &y)
&& --timeout)
{
shSpecialEffect eff = beamSpecialEffect (attack, dir);
if (eff && !Hero.isBlind ()) {
Level->setSpecialEffect (x, y, eff);
Level->drawSq (x, y);
}
if (Level->isOccupied (x, y)) {
shCreature *c = Level->getCreature (x, y);
int tohitmod = 0;
if (!isHero () && !c->isHero ())
tohitmod -= 8; /* avoid friendly fire */
else if (!firsttarget)
tohitmod -= 4;
firsttarget = 0;
if (c->reflectAttack (attack, &dir)) {
Level->setSpecialEffect
(x, y, beamSpecialEffect (attack, kOrigin));
I->pauseXY (Hero.mX, Hero.mY, 10);
Level->setSpecialEffect (x, y, kNone);
continue;
}
int r = resolveRangedAttack (attack, weapon, tohitmod,
Level->getCreature (x, y));
if (r >= 0 && shAttack::kRail != attack->mType) {
Level->setSpecialEffect (x, y, kNone);
break;
}
}
if (Level->isOcclusive (x, y)) {
shFeature *f = Level->getFeature (x, y);
if (f) {
switch (f->mType) {
case shFeature::kDoorClosed:
case shFeature::kDoorBerserkClosed:
if (f->isMagneticallySealed ()) {
if (kForce == attack->mDamage[0].mEnergy) {
I->p ("The %s bounces off the door!",
attack->noun ());
dir = reflectBackDir (dir);
Level->setSpecialEffect (x, y,
beamSpecialEffect (attack, kOrigin));
I->pauseXY (Hero.mX, Hero.mY, 10);
Level->setSpecialEffect (x, y, kNone);
continue;
} else {
I->p ("Your shot hits a force field!");
}
break;
}
if (isHero () && f->isLockedDoor ()) {
shootLock (weapon, attack, f, 0);
}
break;
default:
/* TODO: shoot other types of features */
break;
}
}
Level->setSpecialEffect (x, y, kNone);
break;
}
if (eff) {
if (timeout > 85 || firsttarget ||
distance (x, y, Hero.mX, Hero.mY) < 40 ||
!RNG (5))
{ /* trying not to pause too much */
I->pauseXY (Hero.mX, Hero.mY, 5);
}
Level->setSpecialEffect (x, y, kNone);
}
}
}
break;
case shAttack::kBeam:
{
if (Hero.canSee (this)) {
weapon->setIlkKnown ();
}
Level->areaEffect (attack, weapon, mX, mY, dir, this, 0);
break;
}
default:
I->p ("Unkown weapon type!!");
break;
}
if (setemptyraygun) {
weapon->mIlk = findAnIlk (&RayGunIlks, "empty ray gun");
}
return attack->mAttackTime;
}
void
shCreature::projectile (shObject *obj, int x, int y, shDirection dir,
shAttack *attack, int range)
{
int firsttarget = 1;
while (range--) {
shFeature *f;
if (!mLevel->moveForward (dir, &x, &y)) {
mLevel->moveForward (uTurn (dir), &x, &y);
obj->impact (x, y, dir, this);
return;
}
if (mLevel->isOccupied (x, y)) {
shCreature *c = mLevel->getCreature (x, y);
int tohitmod = 0;
if (!isHero () && !c->isHero ())
tohitmod -= 8; /* avoid friendly fire */
else if (!firsttarget)
tohitmod -= 4;
int r = resolveRangedAttack (attack, obj, tohitmod, c);
firsttarget = 0;
if (r >= 0) {
/* a hit - resolveRangedAttack will have called obj->impact */
return;
} else if (Hero.canSee (c)) {
/* a miss - assuming we were aiming at this creature, the
object shouldn't land too far away */
range = mini (range, RNG (1, 3) - 1);
if (-1 == range) { /* land in the square in front */
mLevel->moveForward (uTurn (dir), &x, &y);
obj->impact (x, y, dir, this);
}
}
}
f = mLevel->getFeature (x, y);
if (f) {
switch (f->mType) {
case shFeature::kDoorHiddenVert:
case shFeature::kDoorHiddenHoriz:
case shFeature::kDoorBerserkClosed:
case shFeature::kDoorClosed:
case shFeature::kComputerTerminal:
case shFeature::kPortal:
/* the thrown object will hit these solid features */
obj->impact (f, dir, this);
return;
case shFeature::kStairsUp:
case shFeature::kStairsDown:
case shFeature::kRadTrap:
case shFeature::kDoorOpen:
case shFeature::kDoorBerserkOpen:
case shFeature::kMaxFeatureType:
/* these features it will fly right past */
break;
}
}
if (mLevel->isObstacle (x, y)) {
obj->impact (x, y, dir, this);
return;
}
}
/* maximum range */
obj->impact (x, y, dir, this);
}
/* work out the result of throwing the object in the given direction
returns: ms elapsed
*/
int
shCreature::throwObject (shObject *obj, shDirection dir)
{
shAttack *attack;
int maxrange;
if (isHero ()) {
Hero.usedUpItem (obj, obj->mCount, "thow");
obj->resetUnpaid ();
}
if (obj->isThrownWeapon ()) {
attack = & ((shWeaponIlk *) obj->mIlk) -> mAttack;
} else {
attack = &ImprovisedMissileAttack;
}
if (!isHero () && Hero.canSee (this)) {
I->p ("%s throws %s!", the (), obj->anQuick ());
}
if (kUp == dir) {
if (isHero ()) {
I->p ("It bounces off the ceiling and lands on your head!");
}
obj->impact (this, kDown, this);
return attack->mAttackTime;
} else if (kDown == dir) {
obj->impact (mX, mY, kDown, this);
return attack->mAttackTime;
}
/* 5 range increments @ 5 ft per increment */
maxrange = 4 + ABILITY_MODIFIER (getStr ()) + NDX (2, 4);
projectile (obj, mX, mY, dir, attack, maxrange);
return attack->mAttackTime;
}
shAttack KickedWallDamage =
shAttack (NULL, shAttack::kSmash, shAttack::kOther, 0, kConcussive, 1, 2);
int
shHero::kick (shDirection dir)