/
traps.cc
1672 lines (1463 loc) · 44.5 KB
/
traps.cc
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
* @brief Traps related functions.
**/
#include "AppHdr.h"
#include "traps.h"
#include "trap-def.h"
#include <algorithm>
#include <cmath>
#include "areas.h"
#include "bloodspatter.h"
#include "branch.h"
#include "cloud.h"
#include "coordit.h"
#include "delay.h"
#include "describe.h"
#include "dungeon.h"
#include "english.h"
#include "god-passive.h" // passive_t::avoid_traps
#include "hints.h"
#include "item-prop.h"
#include "items.h"
#include "libutil.h"
#include "mapmark.h"
#include "mon-cast.h" // recall for zot traps
#include "mon-enum.h"
#include "mon-tentacle.h"
#include "mon-util.h"
#include "message.h"
#include "mon-place.h"
#include "nearby-danger.h"
#include "player-stats.h" // lose_stat for zot traps
#include "random.h"
#include "religion.h"
#include "shout.h"
#include "spl-transloc.h"
#include "spl-summoning.h"
#include "stash.h"
#include "state.h"
#include "stringutil.h"
#include "teleport.h"
#include "terrain.h"
#include "travel.h"
#include "xom.h"
bool trap_def::active() const
{
return type != TRAP_UNASSIGNED;
}
bool trap_def::type_has_ammo() const
{
switch (type)
{
#if TAG_MAJOR_VERSION == 34
case TRAP_NEEDLE:
#endif
case TRAP_ARROW: case TRAP_BOLT:
case TRAP_DART: case TRAP_SPEAR:
return true;
default:
break;
}
return false;
}
void trap_def::destroy(bool known)
{
if (!in_bounds(pos))
die("Trap position out of bounds!");
grd(pos) = DNGN_FLOOR;
if (known)
{
env.map_knowledge(pos).set_feature(DNGN_FLOOR);
StashTrack.update_stash(pos);
}
env.trap.erase(pos);
}
void trap_def::prepare_ammo(int charges)
{
skill_rnd = random2(256);
if (charges)
{
ammo_qty = charges;
return;
}
switch (type)
{
case TRAP_ARROW:
case TRAP_BOLT:
case TRAP_DART:
ammo_qty = 3 + random2avg(9, 3);
break;
case TRAP_SPEAR:
ammo_qty = 2 + random2avg(6, 3);
break;
case TRAP_GOLUBRIA:
// really, turns until it vanishes
ammo_qty = 30 + random2(20);
break;
case TRAP_TELEPORT:
ammo_qty = 1;
break;
default:
ammo_qty = 0;
break;
}
}
void trap_def::reveal()
{
grd(pos) = feature();
}
string trap_def::name(description_level_type desc) const
{
if (type >= NUM_TRAPS)
return "buggy";
string basename = full_trap_name(type);
if (desc == DESC_A)
{
string prefix = "a";
if (is_vowel(basename[0]))
prefix += 'n';
prefix += ' ';
return prefix + basename;
}
else if (desc == DESC_THE)
return string("the ") + basename;
else // everything else
return basename;
}
bool trap_def::is_bad_for_player() const
{
return type == TRAP_ALARM
|| type == TRAP_DISPERSAL
|| type == TRAP_ZOT
|| type == TRAP_NET;
}
bool trap_def::is_safe(actor* act) const
{
if (!act)
act = &you;
// TODO: For now, just assume they're safe; they don't damage outright,
// and the messages get old very quickly
if (type == TRAP_WEB) // && act->is_web_immune()
return true;
#if TAG_MAJOR_VERSION == 34
if (type == TRAP_SHADOW_DORMANT || type == TRAP_SHADOW)
return true;
#endif
if (!act->is_player())
return is_bad_for_player();
// No prompt (teleport traps are ineffective if wearing a -Tele item)
if ((type == TRAP_TELEPORT || type == TRAP_TELEPORT_PERMANENT)
&& you.no_tele(false))
{
return true;
}
if (type == TRAP_GOLUBRIA || type == TRAP_SHAFT)
return true;
#ifdef CLUA_BINDINGS
// Let players specify traps as safe via lua.
if (clua.callbooleanfn(false, "c_trap_is_safe", "s", trap_name(type).c_str()))
return true;
#endif
if (type == TRAP_DART)
return you.hp > 15;
else if (type == TRAP_ARROW)
return you.hp > 35;
else if (type == TRAP_BOLT)
return you.hp > 45;
else if (type == TRAP_SPEAR)
return you.hp > 40;
else if (type == TRAP_BLADE)
return you.hp > 95;
return false;
}
/**
* Get the item index of the first net on the square.
*
* @param where The location.
* @param trapped If true, the index of the stationary net (trapping a victim)
* is returned.
* @return The item index of the net.
*/
int get_trapping_net(const coord_def& where, bool trapped)
{
for (stack_iterator si(where); si; ++si)
{
if (si->is_type(OBJ_MISSILES, MI_THROWING_NET)
&& (!trapped || item_is_stationary_net(*si)))
{
return si->index();
}
}
return NON_ITEM;
}
/**
* Return a string describing the reason a given actor is ensnared. (Since nets
* & webs use the same status.
*
* @param actor The ensnared actor.
* @return Either 'held in a net' or 'caught in a web'.
*/
const char* held_status(actor *act)
{
act = act ? act : &you;
if (get_trapping_net(act->pos(), true) != NON_ITEM)
return "held in a net";
else
return "caught in a web";
}
// If there are more than one net on this square
// split off one of them for checking/setting values.
static void _maybe_split_nets(item_def &item, const coord_def& where)
{
if (item.quantity == 1)
{
set_net_stationary(item);
return;
}
item_def it;
it.base_type = item.base_type;
it.sub_type = item.sub_type;
it.net_durability = item.net_durability;
it.net_placed = item.net_placed;
it.flags = item.flags;
it.special = item.special;
it.quantity = --item.quantity;
item_colour(it);
item.quantity = 1;
set_net_stationary(item);
copy_item_to_grid(it, where);
}
static void _mark_net_trapping(const coord_def& where)
{
int net = get_trapping_net(where);
if (net == NON_ITEM)
{
net = get_trapping_net(where, false);
if (net != NON_ITEM)
_maybe_split_nets(mitm[net], where);
}
}
/**
* Attempt to trap a monster in a net.
*
* @param mon The monster being trapped.
* @return Whether the monster was successfully trapped.
*/
bool monster_caught_in_net(monster* mon)
{
if (mon->body_size(PSIZE_BODY) >= SIZE_GIANT)
{
if (you.see_cell(mon->pos()))
{
if (!mon->visible_to(&you))
mpr("The net bounces off something gigantic!");
else
simple_monster_message(*mon, " is too large for the net to hold!");
}
return false;
}
if (mons_class_is_stationary(mon->type))
{
if (you.see_cell(mon->pos()))
{
if (mon->visible_to(&you))
{
mprf("The net is caught on %s!",
mon->name(DESC_THE).c_str());
}
else
mpr("The net is caught on something unseen!");
}
return false;
}
if (mon->is_insubstantial())
{
if (you.can_see(*mon))
{
mprf("The net passes right through %s!",
mon->name(DESC_THE).c_str());
}
return false;
}
if (!mon->caught() && mon->add_ench(ENCH_HELD))
{
if (you.see_cell(mon->pos()))
{
if (!mon->visible_to(&you))
mpr("Something gets caught in the net!");
else
simple_monster_message(*mon, " is caught in the net!");
}
return true;
}
return false;
}
bool player_caught_in_net()
{
if (you.body_size(PSIZE_BODY) >= SIZE_GIANT)
return false;
if (!you.attribute[ATTR_HELD])
{
mpr("You become entangled in the net!");
stop_running();
// Set the attribute after the mpr, otherwise the screen updates
// and we get a glimpse of a web because there isn't a trapping net
// item yet
you.attribute[ATTR_HELD] = 1;
stop_delay(true); // even stair delays
return true;
}
return false;
}
void check_net_will_hold_monster(monster* mons)
{
ASSERT(mons); // XXX: should be monster &mons
if (mons->body_size(PSIZE_BODY) >= SIZE_GIANT)
{
int net = get_trapping_net(mons->pos());
if (net != NON_ITEM)
destroy_item(net);
if (you.see_cell(mons->pos()))
{
if (mons->visible_to(&you))
{
mprf("The net rips apart, and %s comes free!",
mons->name(DESC_THE).c_str());
}
else
mpr("All of a sudden the net rips apart!");
}
}
else if (mons->is_insubstantial())
{
const int net = get_trapping_net(mons->pos());
if (net != NON_ITEM)
free_stationary_net(net);
simple_monster_message(*mons,
" drifts right through the net!");
}
else
mons->add_ench(ENCH_HELD);
}
static bool _player_caught_in_web()
{
if (you.attribute[ATTR_HELD])
return false;
you.attribute[ATTR_HELD] = 1;
you.redraw_armour_class = true;
you.redraw_evasion = true;
you.redraw_quiver = true;
// No longer stop_running() and stop_delay().
return true;
}
vector<coord_def> find_golubria_on_level()
{
vector<coord_def> ret;
for (rectangle_iterator ri(coord_def(0, 0), coord_def(GXM-1, GYM-1)); ri; ++ri)
{
trap_def *trap = trap_at(*ri);
if (trap && trap->type == TRAP_GOLUBRIA)
ret.push_back(*ri);
}
return ret;
}
enum class passage_type
{
free,
blocked,
none,
};
static passage_type _find_other_passage_side(coord_def& to)
{
vector<coord_def> clear_passages;
bool has_blocks = false;
for (coord_def passage : find_golubria_on_level())
{
if (passage != to)
{
if (!actor_at(passage))
clear_passages.push_back(passage);
else
has_blocks = true;
}
}
const int choices = clear_passages.size();
if (choices < 1)
return has_blocks ? passage_type::blocked : passage_type::none;
to = clear_passages[random2(choices)];
return passage_type::free;
}
// Table of possible Zot trap effects as pairs with weights.
// 2/3 are "evil magic", 1/3 are "summons"
static const vector<pair<function<void ()>, int>> zot_effects = {
{ [] { lose_stat(STAT_RANDOM, 1 + random2avg(5, 2)); }, 4 },
{ [] { contaminate_player(7000 + random2avg(13000, 2), false); }, 4 },
{ [] { you.paralyse(nullptr, 2 + random2(4), "a Zot trap"); }, 1 },
{ [] { dec_mp(you.magic_points); canned_msg(MSG_MAGIC_DRAIN); }, 2 },
{ [] { you.petrify(nullptr); }, 1 },
{ [] { you.increase_duration(DUR_LOWERED_MR, random2(20), 20,
"You feel susceptible to magic."); }, 4 },
{ [] { mons_word_of_recall(nullptr, 2 + random2(3)); }, 3 },
{ [] {
mgen_data mg = mgen_data::hostile_at(RANDOM_DEMON_GREATER,
true, you.pos());
mg.set_summoned(nullptr, 0, SPELL_NO_SPELL, GOD_NO_GOD);
mg.set_non_actor_summoner("a Zot trap");
mg.extra_flags |= (MF_NO_REWARD | MF_HARD_RESET);
if (create_monster(mg))
mpr("You sense a hostile presence.");
}, 3 },
{ [] {
coord_def pt = find_gateway_location(&you);
if (pt != coord_def(0, 0))
create_malign_gateway(pt, BEH_HOSTILE, "a Zot trap", 150);
}, 1 },
{ [] {
mgen_data mg = mgen_data::hostile_at(MONS_TWISTER,
false, you.pos());
mg.set_summoned(nullptr, 2, SPELL_NO_SPELL, GOD_NO_GOD);
mg.set_non_actor_summoner("a Zot trap");
mg.extra_flags |= (MF_NO_REWARD | MF_HARD_RESET);
if (create_monster(mg))
mpr("A huge vortex of air appears!");
}, 1 },
};
// Zot traps only target the player. This rolls their effect.
static void _zot_trap()
{
mpr("The power of Zot is invoked against you!");
(*random_choose_weighted(zot_effects))();
}
void trap_def::trigger(actor& triggerer)
{
const bool you_trigger = triggerer.is_player();
// Out of sight, out of mind.
if (!you.see_cell(pos))
return;
// If set, the trap will be removed at the end of the
// triggering process.
bool trap_destroyed = false, know_trap_destroyed = false;
monster* m = triggerer.as_monster();
// Intelligent monsters native to a branch get a bonus avoiding traps
const bool trig_smart = m
&& mons_is_native_in_branch(*m)
&& mons_intel(*m) >= I_HUMAN;
// Smarter monsters and those native to the level will simply
// side-step known shafts. Unless they are already looking for
// an exit, of course.
if (type == TRAP_SHAFT
&& m
&& (!m->will_trigger_shaft()
|| trig_smart && !mons_is_fleeing(*m) && !m->pacified()))
{
return;
}
// Tentacles aren't real monsters, and shouldn't invoke magic traps.
if (m && mons_is_tentacle_or_tentacle_segment(m->type)
&& !is_mechanical())
{
return;
}
// Store the position now in case it gets cleared in between.
const coord_def p(pos);
if (type_has_ammo())
shoot_ammo(triggerer, trig_smart || you_trigger);
else switch (type)
{
case TRAP_GOLUBRIA:
{
coord_def to = p;
passage_type search_result = _find_other_passage_side(to);
if (search_result == passage_type::free)
{
if (you_trigger)
mpr("You enter the passage of Golubria.");
else
simple_monster_message(*m, " enters the passage of Golubria.");
// Should always be true.
bool moved = triggerer.move_to_pos(to);
ASSERT(moved);
place_cloud(CLOUD_TLOC_ENERGY, p, 1 + random2(3), &triggerer);
trap_destroyed = true;
know_trap_destroyed = you_trigger;
}
else if (you_trigger)
{
mprf("This passage %s!", search_result == passage_type::blocked ?
"seems to be blocked by something" : "doesn't lead anywhere");
}
break;
}
case TRAP_DISPERSAL:
dprf("Triggered dispersal.");
if (you_trigger)
mprf("You enter %s!", name(DESC_A).c_str());
else
mprf("%s enters %s!", triggerer.name(DESC_THE).c_str(),
name(DESC_A).c_str());
apply_visible_monsters([] (monster& mons) {
return !mons.no_tele() && monster_blink(&mons);
}, pos);
if (!you_trigger && you.see_cell_no_trans(pos))
you.blink();
// Don't chain disperse
triggerer.blink();
break;
case TRAP_TELEPORT:
case TRAP_TELEPORT_PERMANENT:
if (you_trigger)
mprf("You enter %s!", name(DESC_A).c_str());
if (ammo_qty > 0 && !--ammo_qty)
{
// can't use trap_destroyed, as we might recurse into a shaft
// or be banished by a Zot trap
env.map_knowledge(pos).set_feature(DNGN_FLOOR);
mprf("%s disappears.", name(DESC_THE).c_str());
destroy();
}
if (!triggerer.no_tele(true, you_trigger))
triggerer.teleport(true);
break;
case TRAP_ALARM:
// Alarms always mark the player, but not through glass
// The trap gets destroyed to prevent the player from abusing an alarm
// trap found in favourable terrain.
if (!you.see_cell_no_trans(pos))
break;
trap_destroyed = true;
if (you_trigger)
mprf("You set off the alarm!");
else
mprf("%s %s the alarm!", triggerer.name(DESC_THE).c_str(),
mons_intel(*m) >= I_HUMAN ? "pulls" : "sets off");
if (silenced(pos))
{
mprf("%s vibrates slightly, failing to make a sound.",
name(DESC_THE).c_str());
}
else
{
string msg = make_stringf("%s emits a blaring wail!",
name(DESC_THE).c_str());
noisy(40, pos, msg.c_str(), triggerer.mid);
}
you.sentinel_mark(true);
break;
case TRAP_BLADE:
if (you_trigger)
{
const int narrow_miss_rnd = random2(6) + 3;
if (one_chance_in(3))
mpr("You avoid triggering a blade trap.");
else if (random2limit(you.evasion(), 40) + narrow_miss_rnd > 8)
mpr("A huge blade swings just past you!");
else
{
mpr("A huge blade swings out and slices into you!");
const int damage = you.apply_ac(48 + random2avg(29, 2));
string n = name(DESC_A);
ouch(damage, KILLED_BY_TRAP, MID_NOBODY, n.c_str());
bleed_onto_floor(you.pos(), MONS_PLAYER, damage, true);
}
}
else if (m)
{
if (one_chance_in(5) || (trig_smart && coinflip()))
{
// Trap doesn't trigger.
simple_monster_message(*m, " fails to trigger a blade trap.");
}
else if (random2(m->evasion()) > 8
|| (trig_smart && random2(m->evasion()) > 8))
{
if (!simple_monster_message(*m,
" avoids a huge, swinging blade."))
{
mpr("A huge blade swings out!");
}
}
else
{
string msg = "A huge blade swings out";
if (m->visible_to(&you))
{
msg += " and slices into ";
msg += m->name(DESC_THE);
}
msg += "!";
mpr(msg);
int damage_taken = m->apply_ac(10 + random2avg(29, 2));
if (!m->is_summoned())
bleed_onto_floor(m->pos(), m->type, damage_taken, true);
m->hurt(nullptr, damage_taken);
if (m->alive())
print_wounds(*m);
}
}
break;
case TRAP_NET:
{
// Nets need LOF to hit the player, no netting through glass.
if (!you.see_cell_no_trans(pos))
break;
bool triggered = you_trigger;
if (m)
{
if (mons_intel(*m) < I_HUMAN)
{
// Not triggered, trap stays.
simple_monster_message(*m, " fails to trigger a net trap.");
}
else
{
// Triggered, net the player.
triggered = true;
if (!simple_monster_message(*m,
" drops a net on you."))
{
mpr("Something launches a net on you.");
}
}
}
if (triggered)
{
item_def item = generate_trap_item();
copy_item_to_grid(item, you.pos());
if (random2avg(2 * you.evasion(), 2) > 18 + env.absdepth0 / 2)
mpr("A net drops to the ground!");
else
{
mpr("A large net falls onto you!");
if (player_caught_in_net())
{
if (player_in_a_dangerous_place())
xom_is_stimulated(50);
// Mark the item as trapping; after this it's
// safe to update the view.
_mark_net_trapping(you.pos());
}
}
trap_destroyed = true;
}
}
break;
case TRAP_WEB:
if (triggerer.is_web_immune())
{
if (m)
{
if (m->is_insubstantial())
simple_monster_message(*m, " passes through a web.");
else if (mons_genus(m->type) == MONS_JELLY)
simple_monster_message(*m, " oozes through a web.");
// too spammy for spiders, and expected
}
break;
}
if (you_trigger)
{
if (one_chance_in(3))
mpr("You pick your way through the web.");
else
{
mpr("You are caught in the web!");
if (_player_caught_in_web())
{
check_monsters_sense(SENSE_WEB_VIBRATION, 9, you.pos());
if (player_in_a_dangerous_place())
xom_is_stimulated(50);
}
}
}
else if (m)
{
if (one_chance_in(3) || (trig_smart && coinflip()))
simple_monster_message(*m, " evades a web.");
else
{
if (m->visible_to(&you))
simple_monster_message(*m, " is caught in a web!");
else
mpr("A web moves frantically as something is caught in it!");
// If somehow already caught, make it worse.
m->add_ench(ENCH_HELD);
// Don't try to escape the web in the same turn
m->props[NEWLY_TRAPPED_KEY] = true;
// Alert monsters.
check_monsters_sense(SENSE_WEB_VIBRATION, 9, triggerer.position);
}
}
break;
case TRAP_ZOT:
if (you_trigger)
{
mpr("You enter the Zot trap.");
_zot_trap();
}
else if (m)
{
// Zot traps are out to get *the player*! Hostile monsters
// benefit and friendly monsters bring effects down on
// the player. Such is life.
// Give the player a chance to figure out what happened
if (player_can_hear(pos))
mprf(MSGCH_SOUND, "You hear a loud \"Zot\"!");
if (you.see_cell_no_trans(pos) && one_chance_in(5))
_zot_trap();
}
break;
case TRAP_SHAFT:
// Known shafts don't trigger as traps.
// Allies don't fall through shafts (no herding!)
if (trig_smart || (m && m->wont_attack()) || you_trigger)
break;
// A chance to escape.
if (one_chance_in(4))
break;
{
// keep this for messaging purposes
const bool triggerer_seen = you.can_see(triggerer);
// Fire away!
triggerer.do_shaft();
// Player-used shafts are destroyed
// after one use in down_stairs()
if (!you_trigger)
{
mprf("%s shaft crumbles and collapses.",
triggerer_seen ? "The" : "A");
know_trap_destroyed = true;
trap_destroyed = true;
}
}
break;
#if TAG_MAJOR_VERSION == 34
case TRAP_GAS:
mpr("The gas trap seems to be inoperative.");
trap_destroyed = true;
break;
#endif
case TRAP_PLATE:
dungeon_events.fire_position_event(DET_PRESSURE_PLATE, pos);
break;
#if TAG_MAJOR_VERSION == 34
case TRAP_SHADOW:
case TRAP_SHADOW_DORMANT:
#endif
default:
break;
}
if (you_trigger)
learned_something_new(HINT_SEEN_TRAP, p);
if (trap_destroyed)
destroy(know_trap_destroyed);
}
int trap_def::max_damage(const actor& act)
{
// Trap damage to monsters is a lot smaller, because they are fairly
// stupid and tend to have fewer hp than players -- this choice prevents
// traps from easily killing large monsters.
bool mon = act.is_monster();
switch (type)
{
case TRAP_DART: return 0;
case TRAP_ARROW: return mon ? 7 : 15;
case TRAP_SPEAR: return mon ? 10 : 26;
case TRAP_BOLT: return mon ? 18 : 40;
case TRAP_BLADE: return mon ? 38 : 76;
default: return 0;
}
return 0;
}
int trap_def::shot_damage(actor& act)
{
const int dam = max_damage(act);
if (!dam)
return 0;
return random2(dam) + 1;
}
int trap_def::to_hit_bonus()
{
switch (type)
{
// To-hit:
case TRAP_ARROW:
return 7;
case TRAP_SPEAR:
return 10;
case TRAP_BOLT:
return 15;
case TRAP_NET:
return 5;
case TRAP_DART:
return 8;
// Irrelevant:
default:
return 0;
}
}
void destroy_trap(const coord_def& pos)
{
if (trap_def* ptrap = trap_at(pos))
ptrap->destroy();
}
trap_def* trap_at(const coord_def& pos)
{
if (!feat_is_trap(grd(pos)))
return nullptr;
auto it = env.trap.find(pos);
ASSERT(it != env.trap.end());
ASSERT(it->second.pos == pos);
ASSERT(it->second.type != TRAP_UNASSIGNED);
return &it->second;
}
trap_type get_trap_type(const coord_def& pos)
{
if (trap_def* ptrap = trap_at(pos))
return ptrap->type;
return TRAP_UNASSIGNED;
}
/**
* End the ATTR_HELD state & redraw appropriate UI.
*
* Do NOT call without clearing up nets, webs, etc first!
*/
void stop_being_held()
{
you.attribute[ATTR_HELD] = 0;
you.redraw_quiver = true;
you.redraw_evasion = true;
}
/**
* Exit a web that's currently holding you.
*
* @param quiet Whether to squash messages.
*/
void leave_web(bool quiet)
{
const trap_def *trap = trap_at(you.pos());
if (!trap || trap->type != TRAP_WEB)
return;
if (trap->ammo_qty == 1) // temp web from e.g. jumpspider/spidersack
{
if (!quiet)
mpr("The web tears apart.");
destroy_trap(you.pos());
}
else if (!quiet)
mpr("You disentangle yourself.");
stop_being_held();
}
/**
* Let the player attempt to unstick themself from a web.
*/
static void _free_self_from_web()
{
// Check if there's actually a web trap in your tile.
trap_def *trap = trap_at(you.pos());
if (trap && trap->type == TRAP_WEB)
{
// if so, roll a chance to escape the web.
if (x_chance_in_y(3, 10))
{
mpr("You struggle to detach yourself from the web.");
// but you actually accomplished nothing!
return;
}
leave_web();
}
// whether or not there was a web trap there, you're free now.
stop_being_held();
}
void free_self_from_net()
{
const int net = get_trapping_net(you.pos());
if (net == NON_ITEM)
{
// If there's no net, it must be a web.
_free_self_from_web();
return;
}
int hold = mitm[net].net_durability;
dprf("net.net_durability: %d", hold);