forked from NetHack/NetHack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
teleport.c
2167 lines (1994 loc) · 80.8 KB
/
teleport.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
/* NetHack 3.7 teleport.c $NHDT-Date: 1685863331 2023/06/04 07:22:11 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.206 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Robert Patrick Rankin, 2011. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
#define NEW_ENEXTO
static boolean goodpos_onscary(coordxy, coordxy, struct permonst *);
static boolean tele_jump_ok(coordxy, coordxy, coordxy, coordxy);
static boolean teleok(coordxy, coordxy, boolean);
static void vault_tele(void);
static boolean rloc_pos_ok(coordxy, coordxy, struct monst *);
static void rloc_to_core(struct monst *, coordxy, coordxy, unsigned);
static void mvault_tele(struct monst *);
static boolean m_blocks_teleporting(struct monst *);
/* does monster block others from teleporting? */
static boolean
m_blocks_teleporting(struct monst *mtmp)
{
if (is_dlord(mtmp->data) || is_dprince(mtmp->data))
return TRUE;
return FALSE;
}
/* teleporting is prevented on this level for this monster? */
boolean
noteleport_level(struct monst* mon)
{
/* demon court in Gehennom prevent others from teleporting */
if (In_hell(&u.uz) && !(is_dlord(mon->data) || is_dprince(mon->data)))
if (get_iter_mons(m_blocks_teleporting))
return TRUE;
/* natural no-teleport level */
if (gl.level.flags.noteleport)
return TRUE;
return FALSE;
}
/* this is an approximation of onscary() that doesn't use any 'struct monst'
fields aside from 'monst->data'; used primarily for new monster creation
and monster teleport destination, not for ordinary monster movement */
static boolean
goodpos_onscary(
coordxy x, coordxy y,
struct permonst *mptr)
{
/* onscary() checks Angels and lawful minions; this oversimplifies */
if (mptr->mlet == S_HUMAN || mptr->mlet == S_ANGEL
|| is_rider(mptr) || unique_corpstat(mptr))
return FALSE;
/* onscary() checks for vampshifted vampire bats/fog clouds/wolves too */
if (IS_ALTAR(levl[x][y].typ) && mptr->mlet == S_VAMPIRE)
return TRUE;
/* scare monster scroll doesn't have any of the below restrictions,
being its own source of power */
if (sobj_at(SCR_SCARE_MONSTER, x, y))
return TRUE;
/* engraved Elbereth doesn't work in Gehennom or the end-game */
if (Inhell || In_endgame(&u.uz))
return FALSE;
/* creatures who don't (or can't) fear a written Elbereth and weren't
caught by the minions check */
if (mptr == &mons[PM_MINOTAUR] || !haseyes(mptr))
return FALSE;
return sengr_at("Elbereth", x, y, TRUE) ? TRUE : FALSE;
}
/*
* Is (x,y) a good position of mtmp? If mtmp is NULL, then is (x,y) good
* for an object?
*
* This function will only look at mtmp->mdat, so makemon, mplayer, etc can
* call it to generate new monster positions with fake monster structures.
*/
boolean
goodpos(
coordxy x, coordxy y,
struct monst *mtmp,
mmflags_nht gpflags)
{
struct permonst *mdat = (struct permonst *) 0;
boolean ignorewater = ((gpflags & MM_IGNOREWATER) != 0),
ignorelava = ((gpflags & MM_IGNORELAVA) != 0),
checkscary = ((gpflags & GP_CHECKSCARY) != 0),
allow_u = ((gpflags & GP_ALLOW_U) != 0);
if (!isok(x, y))
return FALSE;
/* in many cases, we're trying to create a new monster, which
* can't go on top of the player or any existing monster.
* however, occasionally we are relocating engravings or objects,
* which could be co-located and thus get restricted a bit too much.
* oh well.
*/
if (!allow_u) {
if (u_at(x, y) && mtmp != &gy.youmonst
&& (mtmp != u.ustuck || !u.uswallow)
&& (!u.usteed || mtmp != u.usteed))
return FALSE;
}
if (mtmp) {
struct monst *mtmp2 = m_at(x, y);
/* Be careful with long worms. A monster may be placed back in
* its own location. Normally, if m_at() returns the same monster
* that we're trying to place, the monster is being placed in its
* own location. However, that is not correct for worm segments,
* because all the segments of the worm return the same m_at().
* Actually we overdo the check a little bit--a worm can't be placed
* in its own location, period. If we just checked for mtmp->mx
* != x || mtmp->my != y, we'd miss the case where we're called
* to place the worm segment and the worm's head is at x,y.
*/
if (mtmp2 && (mtmp2 != mtmp || mtmp->wormno))
return FALSE;
mdat = mtmp->data;
if (is_pool(x, y) && !ignorewater) {
/* [what about Breathless?] */
if (mtmp == &gy.youmonst)
return (Swimming || Amphibious
|| (!Is_waterlevel(&u.uz)
&& !is_waterwall(x, y)
/* water on the Plane of Water has no surface
so there's no way to be on or above that */
&& (Levitation || Flying || Wwalking)));
else
return (is_swimmer(mdat)
|| (!Is_waterlevel(&u.uz)
&& !is_waterwall(x, y)
&& m_in_air(mtmp)));
} else if (mdat->mlet == S_EEL && rn2(13) && !ignorewater) {
return FALSE;
} else if (is_lava(x, y) && !ignorelava) {
/* 3.6.3: floating eye can levitate over lava but it avoids
that due the effect of the heat causing it to dry out */
if (mdat == &mons[PM_FLOATING_EYE])
return FALSE;
else if (mtmp == &gy.youmonst)
return (Levitation || Flying
|| (Fire_resistance && Wwalking && uarmf
&& uarmf->oerodeproof)
|| (Upolyd && likes_lava(gy.youmonst.data)));
else
return (m_in_air(mtmp) || likes_lava(mdat));
}
if (passes_walls(mdat) && may_passwall(x, y))
return TRUE;
if (amorphous(mdat) && closed_door(x, y))
return TRUE;
/* avoid onscary() if caller has specified that restriction */
if (checkscary && (mtmp->m_id ? onscary(x, y, mtmp)
: goodpos_onscary(x, y, mdat)))
return FALSE;
}
if (!accessible(x, y)) {
if (!(is_pool(x, y) && ignorewater)
&& !(is_lava(x, y) && ignorelava))
return FALSE;
}
/* skip boulder locations for most creatures */
if (sobj_at(BOULDER, x, y) && (!mdat || !throws_rocks(mdat)))
return FALSE;
return TRUE;
}
/*
* "entity next to"
*
* Attempt to find a good place for the given monster type in the closest
* position to (xx,yy). Do so in successive square rings around (xx,yy).
* If there is more than one valid position in the ring, choose one randomly.
* Return TRUE and the position chosen when successful, FALSE otherwise.
*/
boolean
enexto(
coord *cc,
coordxy xx, coordxy yy,
struct permonst *mdat)
{
return (enexto_core(cc, xx, yy, mdat, GP_CHECKSCARY)
|| enexto_core(cc, xx, yy, mdat, NO_MM_FLAGS));
}
#ifdef NEW_ENEXTO
boolean
enexto_core(
coord *cc, /* output; <cc.x,cc.y> as close as feasible to <xx,yy> */
coordxy xx, coordxy yy, /* input coordinates */
struct permonst *mdat, /* type of monster; affects whether water or
* lava or boulder spots will be considered */
mmflags_nht entflags) /* flags for goodpos() */
{
coord candy[ROWNO * (COLNO - 1)]; /* enough room for every location */
int i, nearcandyct, allcandyct;
struct monst fakemon; /* dummy monster */
boolean allow_xx_yy = (boolean) ((entflags & GP_ALLOW_XY) != 0);
/* note: GP_ALLOW_XY isn't used by goodpos(); old enext_core() used to
mask it off to hide it from goodpos but that isn't required and we
want to keep it in case the debugpline4() gets called */
if (!mdat) {
debugpline0("enexto() called with null mdat");
/* default to player's original monster type */
mdat = &mons[u.umonster];
}
fakemon = cg.zeromonst;
set_mon_data(&fakemon, mdat); /* set up for goodpos() */
/* gather candidate coordinates within 3 steps, those 1 step away in
random order first, then those 2 steps away in random order, then 3;
this will usually find a good spot without scanning the whole map */
nearcandyct = collect_coords(candy, xx, yy, 3, CC_NO_FLAGS,
(boolean (*)(coordxy, coordxy)) 0);
for (i = 0; i < nearcandyct; ++i) {
*cc = candy[i];
if (goodpos(cc->x, cc->y, &fakemon, entflags))
return TRUE;
}
/* didn't find a spot; gather coordinates for the whole map except
for <xx,yy> itself, ordered in expanding distance from <xx,yy>
(subsets of equal distance grouped together with order randomized) */
allcandyct = collect_coords(candy, xx, yy, 0, CC_NO_FLAGS,
(boolean (*)(coordxy, coordxy)) 0);
/* skip first 'nearcandyct' spots, they have already been rejected;
they will occur in different random order but same overall total */
for (i = nearcandyct; i < allcandyct; ++i) {
*cc = candy[i];
if (goodpos(cc->x, cc->y, &fakemon, entflags))
return TRUE;
}
/* still didn't find a spot; maybe try <xx,yy> itself */
cc->x = xx, cc->y = yy; /* final value for 'cc' in case we return False */
if (allow_xx_yy && goodpos(cc->x, cc->y, &fakemon, entflags))
return TRUE;
/* failed to find any acceptable spot */
debugpline4("enexto(\"%s\",%d,%d,0x%08lx) failed",
mdat->pmnames[NEUTRAL], (int) xx, (int) yy,
(unsigned long) entflags);
return FALSE;
}
#else /* !NEW_ENEXTO */
boolean
enexto_core(
coord *cc,
coordxy xx, coordxy yy,
struct permonst *mdat,
mmflags_nht entflags)
{
#define MAX_GOOD 15
coord good[MAX_GOOD], *good_ptr;
coordxy x, y, range, i;
coordxy xmin, xmax, ymin, ymax, rangemax;
struct monst fakemon; /* dummy monster */
boolean allow_xx_yy = (boolean) ((entflags & GP_ALLOW_XY) != 0);
entflags &= ~GP_ALLOW_XY;
if (!mdat) {
debugpline0("enexto() called with null mdat");
/* default to player's original monster type */
mdat = &mons[u.umonster];
}
fakemon = cg.zeromonst;
set_mon_data(&fakemon, mdat); /* set up for goodpos */
/* used to use 'if (range > ROWNO && range > COLNO) return FALSE' below,
so effectively 'max(ROWNO, COLNO)' which performs useless iterations
(possibly many iterations if <xx,yy> is in the center of the map) */
xmax = max(xx - 1, (COLNO - 1) - xx);
ymax = max(yy - 0, (ROWNO - 1) - yy);
rangemax = max(xmax, ymax);
/* setup: no suitable spots yet, first iteration checks adjacent spots */
good_ptr = good;
range = 1;
/*
* Walk around the border of the square with center (xx,yy) and
* radius range. Stop when we find at least one valid position.
*/
do {
xmin = max(1, xx - range);
xmax = min(COLNO - 1, xx + range);
ymin = max(0, yy - range);
ymax = min(ROWNO - 1, yy + range);
for (x = xmin; x <= xmax; x++) {
if (goodpos(x, ymin, &fakemon, entflags)) {
good_ptr->x = x;
good_ptr->y = ymin;
/* beware of accessing beyond segment boundaries.. */
if (good_ptr++ == &good[MAX_GOOD - 1])
goto full;
}
if (goodpos(x, ymax, &fakemon, entflags)) {
good_ptr->x = x;
good_ptr->y = ymax;
if (good_ptr++ == &good[MAX_GOOD - 1])
goto full;
}
}
/* 3.6.3: this used to use 'ymin+1' which left top row unchecked */
for (y = ymin; y < ymax; y++) {
if (goodpos(xmin, y, &fakemon, entflags)) {
good_ptr->x = xmin;
good_ptr->y = y;
if (good_ptr++ == &good[MAX_GOOD - 1])
goto full;
}
if (goodpos(xmax, y, &fakemon, entflags)) {
good_ptr->x = xmax;
good_ptr->y = y;
if (good_ptr++ == &good[MAX_GOOD - 1])
goto full;
}
}
} while (++range <= rangemax && good_ptr == good);
/* return False if we exhausted 'range' without finding anything */
if (good_ptr == good) {
/* 3.6.3: earlier versions didn't have the option to try <xx,yy>,
and left 'cc' uninitialized when returning False */
cc->x = xx, cc->y = yy;
/* if every spot other than <xx,yy> has failed, try <xx,yy> itself */
if (allow_xx_yy && goodpos(xx, yy, &fakemon, entflags)) {
return TRUE; /* 'cc' is set */
} else {
debugpline3("enexto(\"%s\",%d,%d) failed",
mdat->pmnames[NEUTRAL], (int) xx, (int) yy);
return FALSE;
}
}
full:
/* we've got between 1 and SIZE(good) candidates; choose one */
i = (coordxy) rn2((int) (good_ptr - good));
cc->x = good[i].x;
cc->y = good[i].y;
return TRUE;
#undef MAX_GOOD
}
#endif /* ?NEW_ENEXTO */
/*
* Check for restricted areas present in some special levels. (This might
* need to be augmented to allow deliberate passage in wizard mode, but
* only for explicitly chosen destinations.)
*/
static boolean
tele_jump_ok(coordxy x1, coordxy y1, coordxy x2, coordxy y2)
{
if (!isok(x2, y2))
return FALSE;
if (gd.dndest.nlx > 0) {
/* if inside a restricted region, can't teleport outside */
if (within_bounded_area(x1, y1, gd.dndest.nlx, gd.dndest.nly,
gd.dndest.nhx, gd.dndest.nhy)
&& !within_bounded_area(x2, y2, gd.dndest.nlx, gd.dndest.nly,
gd.dndest.nhx, gd.dndest.nhy))
return FALSE;
/* and if outside, can't teleport inside */
if (!within_bounded_area(x1, y1, gd.dndest.nlx, gd.dndest.nly,
gd.dndest.nhx, gd.dndest.nhy)
&& within_bounded_area(x2, y2, gd.dndest.nlx, gd.dndest.nly,
gd.dndest.nhx, gd.dndest.nhy))
return FALSE;
}
if (gu.updest.nlx > 0) { /* ditto */
if (within_bounded_area(x1, y1, gu.updest.nlx, gu.updest.nly,
gu.updest.nhx, gu.updest.nhy)
&& !within_bounded_area(x2, y2, gu.updest.nlx, gu.updest.nly,
gu.updest.nhx, gu.updest.nhy))
return FALSE;
if (!within_bounded_area(x1, y1, gu.updest.nlx, gu.updest.nly,
gu.updest.nhx, gu.updest.nhy)
&& within_bounded_area(x2, y2, gu.updest.nlx, gu.updest.nly,
gu.updest.nhx, gu.updest.nhy))
return FALSE;
}
return TRUE;
}
static boolean
teleok(coordxy x, coordxy y, boolean trapok)
{
if (!trapok) {
/* allow teleportation onto vibrating square, it's not a real trap;
also allow pits and holes if levitating or flying */
struct trap *trap = t_at(x, y);
if (!trap)
trapok = TRUE;
else if (trap->ttyp == VIBRATING_SQUARE)
trapok = TRUE;
else if ((is_pit(trap->ttyp) || is_hole(trap->ttyp))
&& (Levitation || Flying))
trapok = TRUE;
if (!trapok)
return FALSE;
}
if (!goodpos(x, y, &gy.youmonst, 0))
return FALSE;
if (!tele_jump_ok(u.ux, u.uy, x, y))
return FALSE;
if (!in_out_region(x, y))
return FALSE;
return TRUE;
}
void
teleds(coordxy nux, coordxy nuy, int teleds_flags)
{
unsigned was_swallowed;
boolean ball_active, ball_still_in_range = FALSE,
allow_drag = (teleds_flags & TELEDS_ALLOW_DRAG) != 0,
is_teleport = (teleds_flags & TELEDS_TELEPORT) != 0;
struct monst *vault_guard = vault_occupied(u.urooms) ? findgd() : 0;
if (u.utraptype == TT_BURIEDBALL) {
/* unearth it */
buried_ball_to_punishment();
}
ball_active = (Punished && uball->where != OBJ_FREE);
if (!ball_active
|| near_capacity() > SLT_ENCUMBER
|| distmin(u.ux, u.uy, nux, nuy) > 1)
allow_drag = FALSE;
/* If they have to move the ball, then drag if allow_drag is true;
* otherwise they are teleporting, so unplacebc().
* If they don't have to move the ball, then always "drag" whether or
* not allow_drag is true, because we are calling that function, not
* to drag, but to move the chain. *However* there are some dumb
* special cases:
* 0 0
* _X move east -----> X_
* @ @
* These are permissible if teleporting, but not if dragging. As a
* result, drag_ball() needs to know about allow_drag and might end
* up dragging the ball anyway. Also, drag_ball() might find that
* dragging the ball is completely impossible (ball in range but there's
* rock in the way), in which case it teleports the ball on its own.
*/
if (ball_active) {
if (!carried(uball) && distmin(nux, nuy, uball->ox, uball->oy) <= 2)
ball_still_in_range = TRUE; /* don't have to move the ball */
else if (!allow_drag)
unplacebc(); /* have to move the ball */
}
reset_utrap(FALSE);
was_swallowed = u.uswallow; /* set_ustuck(Null) clears uswallow */
set_ustuck((struct monst *) 0);
u.ux0 = u.ux;
u.uy0 = u.uy;
if (!hideunder(&gy.youmonst) && gy.youmonst.data->mlet == S_MIMIC) {
/* mimics stop being unnoticed */
gy.youmonst.m_ap_type = M_AP_NOTHING;
}
if (was_swallowed) {
if (Punished) { /* ball&chain are off map while swallowed */
ball_active = TRUE; /* to put chain and non-carried ball on map */
ball_still_in_range = allow_drag = FALSE; /* (redundant) */
}
docrt();
}
if (ball_active && (ball_still_in_range || allow_drag)) {
int bc_control;
coordxy ballx, bally, chainx, chainy;
boolean cause_delay;
if (drag_ball(nux, nuy, &bc_control, &ballx, &bally, &chainx,
&chainy, &cause_delay, allow_drag))
move_bc(0, bc_control, ballx, bally, chainx, chainy);
else {
/* dragging fails if hero is encumbered beyond 'burdened' */
/* uball might've been cleared via drag_ball -> spoteffects ->
dotrap -> magic trap unpunishment */
ball_active = (Punished && uball->where != OBJ_FREE);
if (ball_active)
unplacebc(); /* to match placebc() below */
}
}
/* must set u.ux, u.uy after drag_ball(), which may need to know
the old position if allow_drag is true... */
u_on_newpos(nux, nuy); /* set u.<x,y>, usteed-><mx,my>; cliparound() */
fill_pit(u.ux0, u.uy0);
if (ball_active && uchain && uchain->where == OBJ_FREE)
placebc(); /* put back the ball&chain if they were taken off map */
initrack(); /* teleports mess up tracking monsters without this */
update_player_regions();
/*
* Make sure the hero disappears from the old location. This will
* not happen if she is teleported within sight of her previous
* location. Force a full vision recalculation because the hero
* is now in a new location.
*/
newsym(u.ux0, u.uy0);
see_monsters();
gv.vision_full_recalc = 1;
nomul(0);
vision_recalc(0); /* vision before effects */
/* this used to take place sooner, but if a --More-- prompt was issued
then the old map display was shown instead of the new one */
if (is_teleport && Verbose(2, teleds))
You("materialize in %s location!",
(nux == u.ux0 && nuy == u.uy0) ? "the same" : "a different");
/* if terrain type changes, levitation or flying might become blocked
or unblocked; might issue message, so do this after map+vision has
been updated for new location instead of right after u_on_newpos() */
if (levl[u.ux][u.uy].typ != levl[u.ux0][u.uy0].typ)
switch_terrain();
/* sequencing issue: we want guard's alarm, if any, to occur before
room entry message, if any, so need to check for vault exit prior
to spoteffects; but spoteffects() sets up new value for u.urooms
and vault code depends upon that value, so we need to fake it */
if (vault_guard) {
char save_urooms[5]; /* [sizeof u.urooms] */
Strcpy(save_urooms, u.urooms);
Strcpy(u.urooms, in_rooms(u.ux, u.uy, VAULT));
/* if hero has left vault, make guard notice */
if (!vault_occupied(u.urooms))
uleftvault(vault_guard);
Strcpy(u.urooms, save_urooms); /* reset prior to spoteffects() */
}
/* possible shop entry message comes after guard's shrill whistle */
spoteffects(TRUE);
invocation_message();
return;
}
/* make a list of coordinates in expanding distance from <cx,cy>;
return value is number of coordinates inserted into ccc[] */
int
collect_coords(
coord *ccc, /* pointer to array of at least size ROWNO*(COLNO-1) */
coordxy cx, coordxy cy, /* center point, not necessarly <u.ux,u.uy> */
int maxradius, /* how far from center to go collecting spots;
* 0 means collect entire map */
unsigned cc_flags, /* incl_center: put <cx,cy> in output list
* (provided that it passes filter, if any);
* unshuffled: keep output in collection order;
* ring_pairs: shuffle pairs of rings together
* instead of keeping each ring distinct;
* skip_mons: reject occupied spots;
* skip_inaccs: reject !ZAP_POS() spots */
boolean (*filter)(coordxy, coordxy)) /* if Null, no filtering */
{
coordxy x, y, lox, hix, loy, hiy;
int radius, rowrange, colrange, k, n = 0;
coord cc, *passcc = NULL;
boolean newpass, passend,
/* is <cx,cy> a candidate? */
include_cxcy = (cc_flags & CC_INCL_CENTER) != 0,
/* flag is negative; turn local variable into positive */
scramble = (cc_flags & CC_UNSHUFFLED) == 0,
/* if scrambling, shuffle rings 1+2, 3+4, &c together */
ring_pairs = (scramble && (cc_flags & CC_RING_PAIRS) != 0),
/* exclude locations containing monsters from output */
skip_mons = (cc_flags & CC_SKIP_MONS) != 0,
/* exclude !ZAP_POS() locations from output; allows pools+lava */
skip_inaccessible = (cc_flags & CC_SKIP_INACCS) != 0;
int result = 0;
rowrange = (cy < ROWNO / 2) ? (ROWNO - 1 - cy) : cy;
colrange = (cx < COLNO / 2) ? (COLNO - 1 - cx) : cx;
k = max(rowrange, colrange);
/* if no radius limit has been specified, cover the whole map */
if (!maxradius)
maxradius = k;
else
maxradius = min(maxradius, k);
/*
* We operate on an expanding radius around the center, optionally
* starting with the center spot itself, and shuffle the edges of
* each expanding square or "ring". (So all 1's are shuffled at
* end of the pass for radius==1, then all 2's at end of radius==2,
* and so on. Shuffling of each ring doesn't encroach on any of
* the others except when ring_pairs mode is specified.)
*
* Diagram of first three rings (four if 'include_cxcy' is specified)
* rings unshuffled output sample shuffled output (varies)
* 3333333 25 26 . . . . 31 33 29 . . . . 44
* 3222223 32 9 10 11 12 13 33 35 22 16 14 24 13 40
* 3211123 34 14 1 2 3 15 . 38 20 2 8 3 15 .
* 3210123 . 16 4 0 5 17 . . 11 1 0 6 9 .
* 3211123 . 18 6 7 8 19 39 . 19 5 7 4 12 25
* 3222223 40 20 21 22 23 24 41 43 10 21 17 23 18 27
* 3333333 42 . . . . 47 48 37 . . . . 30 36
* . == entry not shown to reduce clutter when viewing inner portion.
*
* The digits under 'rings' are ring number, which is also distmin
* from center, indicating the order in which sets of spots are
* evaluated. Output gets collected in expanding rings. For the
* two output grids, the number shown represents the position in the
* returned list of coordinates. When shuffling while ring_pairs is
* specified, the 1's and 2's will be mixed together, the 3's and
* (unshown) 4's will be mixed together, and so forth.
*
* If caller processes the output list in order, the closest viable
* spot will be chosen. If a compeletely random spot is preferred,
* the list can be requested to be unscrambled and then the caller
* can shuffle it, overriding the collection rings. A filter function
* could be used to skip everything after the first acceptable spot.
* 'ring_pairs' mode allows for choosing a very close spot that isn't
* immediately adjacent to the center point, useful for emergency
* teleport to not always end up at the closest possible spot.
*
* TODO:
* Redo filter interface to have caller pass in a context cookie
* that can be passed through to filter so that it could have access
* to more info than just <x,y>. Also add a way to stop collecting
* when an optimal value is found, without checking and skipping the
* rest of the map.
*/
for (radius = include_cxcy ? 0 : 1; radius <= maxradius; ++radius) {
if (!ring_pairs) {
newpass = passend = TRUE;
} else {
/* 0 (if include_cxcy) and maxradius override odd/even */
newpass = ((radius % 2) != 0 || radius == 0); /* odd */
passend = ((radius % 2) == 0 || radius == maxradius); /* even */
}
if (newpass || !passcc) { /* !passcc is redundant but used to fend
* off analyzers thinking use of passcc
* below might occur while still Null */
passcc = ccc; /* start of output entries for current radius (or
* first half of radius pair depending on flags) */
n = 0; /* number of entries for passcc; used for shuffling */
}
lox = cx - radius, hix = cx + radius;
loy = cy - radius, hiy = cy + radius;
for (y = max(loy, 0); y <= hiy; ++y) {
if (y > ROWNO - 1)
break; /* done with collection for current radius */
for (x = max(lox, 1); x <= hix; ++x) { /* column 0 is not used */
if (x > COLNO - 1)
break; /* advance to next 'y' */
if (x != lox && x != hix && y != loy && y != hiy)
continue; /* not any edge of ring/square */
if ((skip_mons && m_at(x, y))
/* note: !ACCESSIBLE() would reject water and lava;
!ZAP_POS() accepts them; caller needs to handle such */
|| (skip_inaccessible && !ZAP_POS(levl[x][y].typ)))
continue; /* quick filters */
if (filter && !(*filter)(x, y))
continue;
cc.x = x, cc.y = y;
*ccc++ = cc;
++n;
++result;
}
}
if (scramble && passend) {
/* shuffle entries gathered for current radius (or pair) */
while (n > 1) {
k = rn2(n); /* 0..n-1 */
if (k) { /* swap [k] with [0] when k is 1..n-1 */
cc = passcc[0];
passcc[0] = passcc[k];
passcc[k] = cc;
}
++passcc; /* passcc[0] has reached its final place */
--n; /* and become exempt from further shuffling */
}
}
} /* radius loop */
debugpline4("collect_coords(,%d,%d,%d,,)=%d", cx, cy, maxradius, result);
return result;
}
/* [try to] teleport hero to a safe spot */
boolean
safe_teleds(int teleds_flags)
{
coordxy nux, nuy;
unsigned cc_flags;
coord candy[ROWNO * (COLNO - 1)], backupspot;
int tcnt, candycount;
/*
* This used to try random locations up to 400 times, with first 200
* tries disallowing trap locations and remaining 200 accepting such.
* On levels without many accessible locations (either due to being
* mostly stone or high monster population) it could fail to find a
* spot.
*
* Now it tries completely randomly only 40 times, all disallowing
* traps, then resorts to checking the entire map, near hero's spot
* first then expanding out from there. If no non-trap spot is found,
* first trap spot is used.
*/
for (tcnt = 0; tcnt < 40; ++tcnt) {
nux = rnd(COLNO - 1);
nuy = rn2(ROWNO);
if (teleok(nux, nuy, FALSE)) {
teleds(nux, nuy, teleds_flags);
return TRUE;
}
}
/* get a shuffled list of candidate locations, starting with spots
1 or 2 steps from hero, then 3 or 4 steps, then 5 or 6, on up */
cc_flags = CC_RING_PAIRS | CC_SKIP_MONS;
if (!Passes_walls)
cc_flags |= CC_SKIP_INACCS;
candycount = collect_coords(candy, u.ux, u.uy, 0, cc_flags,
(boolean (*)(coordxy, coordxy)) 0);
backupspot.x = backupspot.y = 0;
/* skip trap locations via teleok(,,FALSE) but remember first
encountered trap spot that is acceptable to teleok(,,TRUE) */
for (tcnt = 0; tcnt < candycount; ++tcnt) {
nux = candy[tcnt].x, nuy = candy[tcnt].y;
if (teleok(nux, nuy, FALSE)) {
teleds(nux, nuy, teleds_flags);
return TRUE;
}
if (!backupspot.x && t_at(nux, nuy) && teleok(nux, nuy, TRUE))
backupspot.x = nux, backupspot.y = nuy;
}
/* no non-trap spot found; if we skipped a viable trap spot, use it */
if (backupspot.x) {
teleds(backupspot.x, backupspot.y, teleds_flags);
return TRUE;
}
return FALSE;
}
static void
vault_tele(void)
{
struct mkroom *croom = search_special(VAULT);
coord c;
if (croom && somexyspace(croom, &c) && teleok(c.x, c.y, FALSE)) {
teleds(c.x, c.y, TELEDS_TELEPORT);
return;
}
tele();
}
boolean
teleport_pet(register struct monst* mtmp, boolean force_it)
{
struct obj *otmp;
if (mtmp == u.usteed)
return FALSE;
if (mtmp->mleashed) {
otmp = get_mleash(mtmp);
if (!otmp) {
impossible("%s is leashed, without a leash.", Monnam(mtmp));
goto release_it;
}
if (otmp->cursed && !force_it) {
yelp(mtmp);
return FALSE;
} else {
Your("leash goes slack.");
release_it:
m_unleash(mtmp, FALSE);
return TRUE;
}
}
return TRUE;
}
/* teleport the hero via some method other than scroll of teleport */
void
tele(void)
{
scrolltele((struct obj *) 0);
}
/* teleport the hero; usually discover scroll of teleportation if via scroll */
void
scrolltele(struct obj* scroll)
{
coord cc;
/* Disable teleportation in stronghold && Vlad's Tower */
if (noteleport_level(&gy.youmonst) && !wizard) {
pline("A mysterious force prevents you from teleporting!");
if (scroll)
learnscroll(scroll); /* this is obviously a teleport scroll */
return;
}
/* don't show trap if "Sorry..." */
if (!Blinded)
make_blinded(0L, FALSE);
if ((u.uhave.amulet || On_W_tower_level(&u.uz)) && !rn2(3)) {
You_feel("disoriented for a moment.");
/* don't discover the scroll [at least not yet for wizard override];
disorientation doesn't reveal that this is a teleport attempt */
if (!wizard || y_n("Override?") != 'y')
return;
}
if (((Teleport_control || (scroll && scroll->blessed)) && !Stunned)
|| wizard) {
if (unconscious()) {
pline("Being unconscious, you cannot control your teleport.");
} else {
char whobuf[BUFSZ];
Strcpy(whobuf, "you");
if (u.usteed)
Sprintf(eos(whobuf), " and %s", mon_nam(u.usteed));
pline("Where do %s want to be teleported?", whobuf);
if (scroll)
learnscroll(scroll);
cc.x = u.ux;
cc.y = u.uy;
if (isok(iflags.travelcc.x, iflags.travelcc.y)) {
/* The player showed some interest in traveling here;
* pre-suggest this coordinate. */
cc = iflags.travelcc;
}
if (getpos(&cc, TRUE, "the desired position") < 0)
return; /* abort */
/* possible extensions: introduce a small error if
magic power is low; allow transfer to solid rock */
if (teleok(cc.x, cc.y, FALSE)) {
/* for scroll, discover it regardless of destination */
teleds(cc.x, cc.y, TELEDS_TELEPORT);
if (u_at(iflags.travelcc.x, iflags.travelcc.y))
iflags.travelcc.x = iflags.travelcc.y = 0;
return;
}
pline("Sorry...");
}
}
/* we used to suppress discovery if hero teleported to a nearby
spot which was already within view, but now there is always a
"materialize" message regardless of how far you teleported so
discovery of scroll type is unconditional */
if (scroll)
learnscroll(scroll);
(void) safe_teleds(TELEDS_TELEPORT);
}
/* the #teleport command; 'm ^T' == choose among several teleport modes */
int
dotelecmd(void)
{
long save_HTele, save_ETele;
int res, added, hidden;
boolean ignore_restrictions = FALSE;
/* also defined in spell.c */
#define NOOP_SPELL 0
#define HIDE_SPELL 1
#define ADD_SPELL 2
#define UNHIDESPELL 3
#define REMOVESPELL 4
/* normal mode; ignore 'm' prefix if it was given */
if (!wizard)
return dotele(FALSE) ? ECMD_TIME : ECMD_OK;
added = hidden = NOOP_SPELL;
save_HTele = HTeleportation, save_ETele = ETeleportation;
if (!iflags.menu_requested) {
ignore_restrictions = TRUE;
} else {
static const struct tporttypes {
char menulet;
const char *menudesc;
} tports[] = {
/*
* Potential combinations:
* 1) attempt ^T without intrinsic, not know spell;
* 2) via intrinsic, not know spell, obey restrictions;
* 3) via intrinsic, not know spell, ignore restrictions;
* 4) via intrinsic, know spell, obey restrictions;
* 5) via intrinsic, know spell, ignore restrictions;
* 6) via spell, not have intrinsic, obey restrictions;
* 7) via spell, not have intrinsic, ignore restrictions;
* 8) force, obey other restrictions;
* 9) force, ignore restrictions.
* We only support the 1st (t), 2nd (n), 6th (s), and 9th (w).
*
* This ignores the fact that there is an experience level
* (or poly-form) requirement which might make normal ^T fail.
*/
{ 'n', "normal ^T on demand; no spell, obey restrictions" },
{ 's', "via spellcast; no intrinsic teleport" },
{ 't', "try ^T without having it; no spell" },
{ 'w', "debug mode; ignore restrictions" }, /* trad wizard mode */
};
menu_item *picks = (menu_item *) 0;
anything any;
winid win;
int i, tmode, clr = 0;
win = create_nhwindow(NHW_MENU);
start_menu(win, MENU_BEHAVE_STANDARD);
any = cg.zeroany;
for (i = 0; i < SIZE(tports); ++i) {
any.a_int = (int) tports[i].menulet;
add_menu(win, &nul_glyphinfo, &any, (char) any.a_int, 0,
ATR_NONE, clr, tports[i].menudesc,
(tports[i].menulet == 'w') ? MENU_ITEMFLAGS_SELECTED
: MENU_ITEMFLAGS_NONE);
}
end_menu(win, "Which way do you want to teleport?");
i = select_menu(win, PICK_ONE, &picks);
destroy_nhwindow(win);
if (i > 0) {
tmode = picks[0].item.a_int;
/* if we got 2, use the one which wasn't preselected */
if (i > 1 && tmode == 'w')
tmode = picks[1].item.a_int;
free((genericptr_t) picks);
} else if (i == 0) {
/* preselected one was explicitly chosen and got toggled off */
tmode = 'w';
} else { /* ESC */
return ECMD_OK;
}
switch (tmode) {
case 'n':
HTeleportation |= I_SPECIAL; /* confer intrinsic teleportation */
hidden = tport_spell(HIDE_SPELL); /* hide teleport-away */
break;
case 's':
HTeleportation = ETeleportation = 0L; /* suppress intrinsic */
added = tport_spell(ADD_SPELL); /* add teleport-away */
break;
case 't':
HTeleportation = ETeleportation = 0L; /* suppress intrinsic */
hidden = tport_spell(HIDE_SPELL); /* hide teleport-away */
break;
case 'w':
ignore_restrictions = TRUE;
break;
}
}
/* if dotele() can be fatal, final disclosure might lie about
intrinsic teleportation; we should be able to live with that
since the menu finagling is only applicable in wizard mode */
res = dotele(ignore_restrictions);
HTeleportation = save_HTele;
ETeleportation = save_ETele;
if (added != NOOP_SPELL || hidden != NOOP_SPELL)
/* can't both be non-NOOP so addition will yield the non-NOOP one */
(void) tport_spell(added + hidden - NOOP_SPELL);
return res ? ECMD_TIME : ECMD_OK;
#undef NOOP_SPELL
#undef HIDE_SPELL
#undef ADD_SPELL
#undef UNHIDESPELL
#undef REMOVESPELL
}
int
dotele(
boolean break_the_rules) /* True: wizard mode ^T */
{
struct trap *trap;
const char *cantdoit;
boolean trap_once = FALSE;
trap = t_at(u.ux, u.uy);
if (trap && !trap->tseen)
trap = 0;
if (trap) {
if (trap->ttyp == LEVEL_TELEP && trap->tseen) {
if (y_n("There is a level teleporter here. Trigger it?") == 'y') {
level_tele_trap(trap, FORCETRAP);
/* deliberate jumping will always take time even if it doesn't
* work */
return 1;
} else
trap = 0; /* continue with normal horizontal teleport */
} else if (trap->ttyp == TELEP_TRAP) {