Source:NetHack 3.3.0/teleport.c

From NetHackWiki
Revision as of 11:53, 4 March 2008 by Kernigh bot (talk | contribs) (NetHack 3.3.0/teleport.c moved to Source:NetHack 3.3.0/teleport.c: Robot: moved page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Below is the full text to teleport.c from the source code of NetHack 3.3.0. To link to a particular line, write [[NetHack 3.3.0/teleport.c#line123]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1.    /*	SCCS Id: @(#)teleport.c	3.3	1999/11/27	*/
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* NetHack may be freely redistributed.  See license for details. */
4.    
5.    #include "hack.h"
6.    
7.    STATIC_DCL boolean FDECL(tele_jump_ok, (int,int,int,int));
8.    STATIC_DCL boolean FDECL(teleok, (int,int,BOOLEAN_P));
9.    STATIC_DCL void NDECL(vault_tele);
10.   STATIC_DCL boolean FDECL(rloc_pos_ok, (int,int,struct monst *));
11.   STATIC_DCL void FDECL(mvault_tele, (struct monst *));
12.   
13.   /*
14.    * Is (x,y) a good position of mtmp?  If mtmp is NULL, then is (x,y) good
15.    * for an object?
16.    *
17.    * This function will only look at mtmp->mdat, so makemon, mplayer, etc can
18.    * call it to generate new monster positions with fake monster structures.
19.    */
20.   boolean
21.   goodpos(x, y, mtmp)
22.   int x,y;
23.   struct monst *mtmp;
24.   {
25.   	struct permonst *mdat = NULL;
26.   
27.   	if (!isok(x, y)) return FALSE;
28.   
29.   	/* in many cases, we're trying to create a new monster, which
30.   	 * can't go on top of the player or any existing monster.
31.   	 * however, occasionally we are relocating engravings or objects,
32.   	 * which could be co-located and thus get restricted a bit too much.
33.   	 * oh well.
34.   	 */
35.   	if (mtmp != &youmonst && x == u.ux && y == u.uy
36.   #ifdef STEED
37.   			&& (!u.usteed || mtmp != u.usteed)
38.   #endif
39.   			)
40.   		return FALSE;
41.   
42.   	if (mtmp) {
43.   	    struct monst *mtmp2 = m_at(x,y);
44.   
45.   	    if (mtmp2 && mtmp2 != mtmp)
46.   		return FALSE;
47.   
48.   	    mdat = mtmp->data;
49.   	    if (is_pool(x,y)) {
50.   		if (mtmp == &youmonst)
51.   			return !!(HLevitation || Flying || Wwalking ||
52.   					Swimming || Amphibious);
53.   		else	return (is_flyer(mdat) || is_swimmer(mdat) ||
54.   							is_clinger(mdat));
55.   	    } else if (mdat->mlet == S_EEL && rn2(13)) {
56.   		return FALSE;
57.   	    } else if (is_lava(x,y)) {
58.   		if (mtmp == &youmonst)
59.   		    return !!HLevitation;
60.   		else
61.   		    return (is_flyer(mdat) || likes_lava(mdat));
62.   	    }
63.   	    if (passes_walls(mdat) && may_passwall(x,y)) return TRUE;
64.   	}
65.   	if (!ACCESSIBLE(levl[x][y].typ)) return FALSE;
66.   	if (closed_door(x, y) && (!mdat || !amorphous(mdat)))
67.   		return FALSE;
68.   	if (sobj_at(BOULDER, x, y) && (!mdat || !throws_rocks(mdat)))
69.   		return FALSE;
70.   	return TRUE;
71.   }
72.   
73.   /*
74.    * "entity next to"
75.    *
76.    * Attempt to find a good place for the given monster type in the closest
77.    * position to (xx,yy).  Do so in successive square rings around (xx,yy).
78.    * If there is more than one valid positon in the ring, choose one randomly.
79.    * Return TRUE and the position chosen when successful, FALSE otherwise.
80.    */
81.   boolean
82.   enexto(cc, xx, yy, mdat)
83.   coord *cc;
84.   register xchar xx, yy;
85.   struct permonst *mdat;
86.   {
87.   #define MAX_GOOD 15
88.       coord good[MAX_GOOD], *good_ptr;
89.       int x, y, range, i;
90.       int xmin, xmax, ymin, ymax;
91.       struct monst fakemon;	/* dummy monster */
92.   
93.       if (!mdat) {
94.   #ifdef DEBUG
95.   	pline("enexto() called with mdat==0");
96.   #endif
97.   	/* default to player's original monster type */
98.   	mdat = &mons[u.umonster];
99.       }
100.      fakemon.data = mdat;	/* set up for goodpos */
101.      good_ptr = good;
102.      range = 1;
103.      /*
104.       * Walk around the border of the square with center (xx,yy) and
105.       * radius range.  Stop when we find at least one valid position.
106.       */
107.      do {
108.  	xmin = max(1, xx-range);
109.  	xmax = min(COLNO-1, xx+range);
110.  	ymin = max(0, yy-range);
111.  	ymax = min(ROWNO-1, yy+range);
112.  
113.  	for (x = xmin; x <= xmax; x++)
114.  	    if (goodpos(x, ymin, &fakemon)) {
115.  		good_ptr->x = x;
116.  		good_ptr->y = ymin ;
117.  		/* beware of accessing beyond segment boundaries.. */
118.  		if (good_ptr++ == &good[MAX_GOOD-1]) goto full;
119.  	    }
120.  	for (x = xmin; x <= xmax; x++)
121.  	    if (goodpos(x, ymax, &fakemon)) {
122.  		good_ptr->x = x;
123.  		good_ptr->y = ymax ;
124.  		/* beware of accessing beyond segment boundaries.. */
125.  		if (good_ptr++ == &good[MAX_GOOD-1]) goto full;
126.  	    }
127.  	for (y = ymin+1; y < ymax; y++)
128.  	    if (goodpos(xmin, y, &fakemon)) {
129.  		good_ptr->x = xmin;
130.  		good_ptr-> y = y ;
131.  		/* beware of accessing beyond segment boundaries.. */
132.  		if (good_ptr++ == &good[MAX_GOOD-1]) goto full;
133.  	    }
134.  	for (y = ymin+1; y < ymax; y++)
135.  	    if (goodpos(xmax, y, &fakemon)) {
136.  		good_ptr->x = xmax;
137.  		good_ptr->y = y ;
138.  		/* beware of accessing beyond segment boundaries.. */
139.  		if (good_ptr++ == &good[MAX_GOOD-1]) goto full;
140.  	    }
141.  	range++;
142.  
143.  	/* return if we've grown too big (nothing is valid) */
144.  	if (range > ROWNO && range > COLNO) return FALSE;
145.      } while (good_ptr == good);
146.  
147.  full:
148.      i = rn2((int)(good_ptr - good));
149.      cc->x = good[i].x;
150.      cc->y = good[i].y;
151.      return TRUE;
152.  }
153.  
154.  /*
155.   * Check for restricted areas present in some special levels.  (This might
156.   * need to be augmented to allow deliberate passage in wizard mode, but
157.   * only for explicitly chosen destinations.)
158.   */
159.  STATIC_OVL boolean
160.  tele_jump_ok(x1, y1, x2, y2)
161.  int x1, y1, x2, y2;
162.  {
163.  	if (dndest.nlx > 0) {
164.  	    /* if inside a restricted region, can't teleport outside */
165.  	    if (within_bounded_area(x1, y1, dndest.nlx, dndest.nly,
166.  						dndest.nhx, dndest.nhy) &&
167.  		!within_bounded_area(x2, y2, dndest.nlx, dndest.nly,
168.  						dndest.nhx, dndest.nhy))
169.  		return FALSE;
170.  	    /* and if outside, can't teleport inside */
171.  	    if (!within_bounded_area(x1, y1, dndest.nlx, dndest.nly,
172.  						dndest.nhx, dndest.nhy) &&
173.  		within_bounded_area(x2, y2, dndest.nlx, dndest.nly,
174.  						dndest.nhx, dndest.nhy))
175.  		return FALSE;
176.  	}
177.  	if (updest.nlx > 0) {		/* ditto */
178.  	    if (within_bounded_area(x1, y1, updest.nlx, updest.nly,
179.  						updest.nhx, updest.nhy) &&
180.  		!within_bounded_area(x2, y2, updest.nlx, updest.nly,
181.  						updest.nhx, updest.nhy))
182.  		return FALSE;
183.  	    if (!within_bounded_area(x1, y1, updest.nlx, updest.nly,
184.  						updest.nhx, updest.nhy) &&
185.  		within_bounded_area(x2, y2, updest.nlx, updest.nly,
186.  						updest.nhx, updest.nhy))
187.  		return FALSE;
188.  	}
189.  	return TRUE;
190.  }
191.  
192.  STATIC_OVL boolean
193.  teleok(x, y, trapok)
194.  register int x, y;
195.  boolean trapok;
196.  {
197.  	if (!trapok && t_at(x, y)) return FALSE;
198.  	if (!goodpos(x, y, &youmonst)) return FALSE;
199.  	if (!tele_jump_ok(u.ux, u.uy, x, y)) return FALSE;
200.  	return TRUE;
201.  }
202.  
203.  void
204.  teleds(nux, nuy)
205.  register int nux,nuy;
206.  {
207.  	if (Punished) unplacebc();
208.  	u.utrap = 0;
209.  	u.ustuck = 0;
210.  	u.ux0 = u.ux;
211.  	u.uy0 = u.uy;
212.  	u.ux = nux;
213.  	u.uy = nuy;
214.  	fill_pit(u.ux0, u.uy0); /* do this now so that cansee() is correct */
215.  
216.  	if (hides_under(youmonst.data))
217.  		u.uundetected = OBJ_AT(nux, nuy);
218.  	else if (youmonst.data->mlet == S_EEL)
219.  		u.uundetected = is_pool(u.ux, u.uy);
220.  	else {
221.  		u.uundetected = 0;
222.  		/* mimics stop being unnoticed */
223.  		if (youmonst.data->mlet == S_MIMIC)
224.  		    youmonst.m_ap_type = M_AP_NOTHING;
225.  	}
226.  
227.  	if (u.uswallow) {
228.  		u.uswldtim = u.uswallow = 0;
229.  		docrt();
230.  	}
231.  	if (Punished) placebc();
232.  	initrack(); /* teleports mess up tracking monsters without this */
233.  	update_player_regions();
234.  #ifdef STEED
235.  	/* Move your steed, too */
236.  	if (u.usteed) {
237.  		u.usteed->mx = nux;
238.  		u.usteed->my = nuy;
239.  	}
240.  #endif
241.  	/*
242.  	 *  Make sure the hero disappears from the old location.  This will
243.  	 *  not happen if she is teleported within sight of her previous
244.  	 *  location.  Force a full vision recalculation because the hero
245.  	 *  is now in a new location.
246.  	 */
247.  	newsym(u.ux0,u.uy0);
248.  	vision_full_recalc = 1;
249.  	nomul(0);
250.  	spoteffects();
251.  	invocation_message();
252.  }
253.  
254.  boolean
255.  safe_teleds()
256.  {
257.  	register int nux, nuy, tcnt = 0;
258.  
259.  	do {
260.  		nux = rnd(COLNO-1);
261.  		nuy = rn2(ROWNO);
262.  	} while (!teleok(nux, nuy, (boolean)(tcnt > 200)) && ++tcnt <= 400);
263.  
264.  	if (tcnt <= 400) {
265.  		teleds(nux, nuy);
266.  		return TRUE;
267.  	} else
268.  		return FALSE;
269.  }
270.  
271.  STATIC_OVL void
272.  vault_tele()
273.  {
274.  	register struct mkroom *croom = search_special(VAULT);
275.  	coord c;
276.  
277.  	if (croom && somexy(croom, &c) && teleok(c.x,c.y,FALSE)) {
278.  		teleds(c.x,c.y);
279.  		return;
280.  	}
281.  	tele();
282.  }
283.  
284.  boolean
285.  teleport_pet(mtmp, force_it)
286.  register struct monst *mtmp;
287.  boolean force_it;
288.  {
289.  	register struct obj *otmp;
290.  
291.  #ifdef STEED
292.  	if (mtmp == u.usteed)
293.  		return (FALSE);
294.  #endif
295.  
296.  	if (mtmp->mleashed) {
297.  	    otmp = get_mleash(mtmp);
298.  	    if (!otmp) {
299.  		impossible("%s is leashed, without a leash.", Monnam(mtmp));
300.  		goto release_it;
301.  	    }
302.  	    if (otmp->cursed && !force_it) {
303.  		yelp(mtmp);
304.  		return FALSE;
305.  	    } else {
306.  		Your("leash goes slack.");
307.   release_it:
308.  		m_unleash(mtmp);
309.  		return TRUE;
310.  	    }
311.  	}
312.  	return TRUE;
313.  }
314.  
315.  void
316.  tele()
317.  {
318.  	coord cc;
319.  
320.  	/* Disable teleportation in stronghold && Vlad's Tower */
321.  	if (level.flags.noteleport) {
322.  #ifdef WIZARD
323.  		if (!wizard) {
324.  #endif
325.  		    pline("A mysterious force prevents you from teleporting!");
326.  		    return;
327.  #ifdef WIZARD
328.  		}
329.  #endif
330.  	}
331.  
332.  	/* don't show trap if "Sorry..." */
333.  	if (!Blinded) make_blinded(0L,FALSE);
334.  
335.  	if ((u.uhave.amulet || On_W_tower_level(&u.uz)) && !rn2(3)) {
336.  	    You_feel("disoriented for a moment.");
337.  	    return;
338.  	}
339.  	if (Teleport_control
340.  #ifdef WIZARD
341.  			    || wizard
342.  #endif
343.  					) {
344.  	    if (unconscious()) {
345.  		pline("Being unconscious, you cannot control your teleport.");
346.  	    } else {
347.  #ifdef STEED
348.  		    char buf[BUFSZ];
349.  		    if (u.usteed) Sprintf(buf," and %s", mon_nam(u.usteed));
350.  #endif
351.  		    pline("To what position do you%s want to be teleported?",
352.  #ifdef STEED
353.  				u.usteed ? buf :
354.  #endif
355.  			   "");
356.  		    cc.x = u.ux;
357.  		    cc.y = u.uy;
358.  		    if (getpos(&cc, TRUE, "the desired position") < 0)
359.  			return;	/* abort */
360.  		    /* possible extensions: introduce a small error if
361.  		       magic power is low; allow transfer to solid rock */
362.  		    if (teleok(cc.x, cc.y, FALSE)) {
363.  			teleds(cc.x, cc.y);
364.  			return;
365.  		    }
366.  		    pline("Sorry...");
367.  		}
368.  	}
369.  
370.  	(void) safe_teleds();
371.  }
372.  
373.  int
374.  dotele()
375.  {
376.  	struct trap *trap;
377.  
378.  	trap = t_at(u.ux, u.uy);
379.  	if (trap && (!trap->tseen || trap->ttyp != TELEP_TRAP))
380.  		trap = 0;
381.  
382.  	if (trap) {
383.  		if (trap->once) {
384.  			pline("This is a vault teleport, usable once only.");
385.  			if (yn("Jump in?") == 'n')
386.  				trap = 0;
387.  			else {
388.  				deltrap(trap);
389.  				newsym(u.ux, u.uy);
390.  			}
391.  		}
392.  		if (trap)
393.  			You("%s onto the teleportation trap.",
394.  			    locomotion(youmonst.data, "jump"));
395.  	}
396.  	if (!trap) {
397.  	    boolean castit = FALSE;
398.  	    register int sp_no = 0, energy = 0;
399.  
400.  	    if (!Teleportation || (u.ulevel < (Role_if(PM_WIZARD) ? 8 : 12)
401.  					&& !can_teleport(youmonst.data))) {
402.  		/* Try to use teleport away spell. */
403.  		if (objects[SPE_TELEPORT_AWAY].oc_name_known && !Confusion)
404.  		    for (sp_no = 0; sp_no < MAXSPELL; sp_no++)
405.  			if (spl_book[sp_no].sp_id == SPE_TELEPORT_AWAY) {
406.  				castit = TRUE;
407.  				break;
408.  			}
409.  #ifdef WIZARD
410.  		if (!wizard) {
411.  #endif
412.  		    if (!castit) {
413.  			if (!Teleportation)
414.  			    You("don't know that spell.");
415.  			else You("are not able to teleport at will.");
416.  			return(0);
417.  		    }
418.  #ifdef WIZARD
419.  		}
420.  #endif
421.  	    }
422.  
423.  	    if (u.uhunger <= 100 || ACURR(A_STR) < 6) {
424.  #ifdef WIZARD
425.  		if (!wizard) {
426.  #endif
427.  			You("lack the strength %s.",
428.  			    castit ? "for a teleport spell" : "to teleport");
429.  			return 1;
430.  #ifdef WIZARD
431.  		}
432.  #endif
433.  	    }
434.  
435.  	    energy = objects[SPE_TELEPORT_AWAY].oc_level * 7 / 2 - 2;
436.  	    if (u.uen <= energy) {
437.  #ifdef WIZARD
438.  		if (wizard)
439.  			energy = u.uen;
440.  		else
441.  #endif
442.  		{
443.  			You("lack the energy %s.",
444.  			    castit ? "for a teleport spell" : "to teleport");
445.  			return 1;
446.  		}
447.  	    }
448.  
449.  	    if (check_capacity(
450.  			"Your concentration falters from carrying so much."))
451.  		return 1;
452.  
453.  	    if (castit) {
454.  		exercise(A_WIS, TRUE);
455.  		if (spelleffects(sp_no, TRUE))
456.  			return(1);
457.  		else
458.  #ifdef WIZARD
459.  		    if (!wizard)
460.  #endif
461.  			return(0);
462.  	    } else {
463.  		u.uen -= energy;
464.  		flags.botl = 1;
465.  	    }
466.  	}
467.  
468.  	if (next_to_u()) {
469.  		if (trap && trap->once) vault_tele();
470.  		else tele();
471.  		(void) next_to_u();
472.  	} else {
473.  		You(shudder_for_moment);
474.  		return(0);
475.  	}
476.  	if (!trap) morehungry(100);
477.  	return(1);
478.  }
479.  
480.  
481.  void
482.  level_tele()
483.  {
484.  	register int newlev;
485.  	d_level newlevel;
486.  	const char *escape_by_flying = 0;	/* when surviving dest of -N */
487.  
488.  	if ((u.uhave.amulet || In_endgame(&u.uz) || In_sokoban(&u.uz))
489.  #ifdef WIZARD
490.  						&& !wizard
491.  #endif
492.  							) {
493.  	    You_feel("very disoriented for a moment.");
494.  	    return;
495.  	}
496.  	if (Teleport_control
497.  #ifdef WIZARD
498.  	   || wizard
499.  #endif
500.  		) {
501.  	    char buf[BUFSZ], qbuf[BUFSZ];
502.  	    int trycnt = 0;
503.  
504.  	    Strcpy(qbuf, "To what level do you want to teleport?");
505.  	    do {
506.  		if (++trycnt == 2) Strcat(qbuf, " [type a number]");
507.  		getlin(qbuf, buf);
508.  		if (!strcmp(buf,"\033"))	/* cancelled */
509.  		    return;
510.  		else if (!strcmp(buf,"*"))
511.  		    goto random_levtport;
512.  		if ((newlev = lev_by_name(buf)) == 0) newlev = atoi(buf);
513.  	    } while (!newlev && !digit(buf[0]) &&
514.  		     (buf[0] != '-' || !digit(buf[1])) &&
515.  		     trycnt < 10);
516.  
517.  	    /* no dungeon escape via this route */
518.  	    if (newlev == 0) {
519.  		if (trycnt >= 10)
520.  		    goto random_levtport;
521.  		if (ynq("Go to Nowhere.  Are you sure?") != 'y') return;
522.  		You("scream in agony as your body begins to warp...");
523.  		display_nhwindow(WIN_MESSAGE, FALSE);
524.  		You("cease to exist.");
525.  		killer_format = NO_KILLER_PREFIX;
526.  		killer = "committed suicide";
527.  		done(DIED);
528.  		return;
529.  	    }
530.  
531.  	    /* if in Knox and the requested level > 0, stay put.
532.  	     * we let negative values requests fall into the "heaven" loop.
533.  	     */
534.  	    if (Is_knox(&u.uz) && newlev > 0) {
535.  		You(shudder_for_moment);
536.  		return;
537.  	    }
538.  	    /* if in Quest, the player sees "Home 1", etc., on the status
539.  	     * line, instead of the logical depth of the level.  controlled
540.  	     * level teleport request is likely to be relativized to the
541.  	     * status line, and consequently it should be incremented to
542.  	     * the value of the logical depth of the target level.
543.  	     *
544.  	     * we let negative values requests fall into the "heaven" loop.
545.  	     */
546.  	    if (In_quest(&u.uz) && newlev > 0)
547.  		newlev = newlev + dungeons[u.uz.dnum].depth_start - 1;
548.  	} else { /* involuntary level tele */
549.   random_levtport:
550.  	    newlev = random_teleport_level();
551.  	    if (newlev == depth(&u.uz)) {
552.  		You(shudder_for_moment);
553.  		return;
554.  	    }
555.  	}
556.  
557.  	if (!next_to_u()) {
558.  		You(shudder_for_moment);
559.  		return;
560.  	}
561.  #ifdef WIZARD
562.  	if (In_endgame(&u.uz)) {	/* must already be wizard */
563.  	    int llimit = dunlevs_in_dungeon(&u.uz);
564.  
565.  	    if (newlev >= 0 || newlev <= -llimit) {
566.  		You_cant("get there from here.");
567.  		return;
568.  	    }
569.  	    newlevel.dnum = u.uz.dnum;
570.  	    newlevel.dlevel = llimit + newlev;
571.  	    schedule_goto(&newlevel, FALSE, FALSE, 0, (char *)0, (char *)0);
572.  	    return;
573.  	}
574.  #endif
575.  
576.  	killer = 0;		/* still alive, so far... */
577.  
578.  	if (newlev < 0) {
579.  		if (newlev <= -10) {
580.  			You("arrive in heaven.");
581.  			verbalize("Thou art early, but we'll admit thee.");
582.  			killer_format = NO_KILLER_PREFIX;
583.  			killer = "went to heaven prematurely";
584.  		} else if (newlev == -9) {
585.  			You_feel("deliriously happy. ");
586.  			pline("(In fact, you're on Cloud 9!) ");
587.  			display_nhwindow(WIN_MESSAGE, FALSE);
588.  		} else
589.  			You("are now high above the clouds...");
590.  
591.  		if (killer) {
592.  		    ;		/* arrival in heaven is pending */
593.  		} else if (Levitation) {
594.  		    escape_by_flying = "float gently down to earth";
595.  		} else if (Flying) {
596.  		    escape_by_flying = "fly down to the ground";
597.  		} else {
598.  		    pline("Unfortunately, you don't know how to fly.");
599.  		    You("plummet a few thousand feet to your death.");
600.  		    killer_format = NO_KILLER_PREFIX;
601.  		    killer =
602.      self_pronoun("teleported out of the dungeon and fell to %s death","his");
603.  		}
604.  	}
605.  
606.  	if (killer) {	/* the chosen destination was not survivable */
607.  	    /* set specific death location; this also suppresses bones */
608.  	    u.uz.dnum = 0;	/* main dungeon */
609.  	    u.uz.dlevel = (newlev <= -10) ? -1 : 0;	/* heaven or surface */
610.  	    done(DIED);
611.  	    /* can only get here via life-saving (or declining to die in
612.  	       explore|debug mode); the hero has now left the dungeon... */
613.  	    escape_by_flying = "find yourself back on the surface";
614.  	}
615.  
616.  	/* calls done(ESCAPED) if newlevel==0 */
617.  	if (escape_by_flying) {
618.  	    You("%s.", escape_by_flying);
619.  	    newlevel.dnum = 0;		/* specify main dungeon */
620.  	    newlevel.dlevel = 0;	/* escape the dungeon */
621.  	    /* [dlevel used to be set to 1, but it doesn't make sense to
622.  		teleport out of the dungeon and float or fly down to the
623.  		surface but then actually arrive back inside the dungeon] */
624.  	} else if (u.uz.dnum == medusa_level.dnum &&
625.  	    newlev >= dungeons[u.uz.dnum].depth_start +
626.  						dunlevs_in_dungeon(&u.uz)) {
627.  	    find_hell(&newlevel);
628.  	} else {
629.  	    /* if invocation did not yet occur, teleporting into
630.  	     * the last level of Gehennom is forbidden.
631.  	     */
632.  #ifdef WIZARD
633.  		if (!wizard)
634.  #endif
635.  	    if (Inhell && !u.uevent.invoked &&
636.  			newlev >= (dungeons[u.uz.dnum].depth_start +
637.  					dunlevs_in_dungeon(&u.uz) - 1)) {
638.  		newlev = dungeons[u.uz.dnum].depth_start +
639.  					dunlevs_in_dungeon(&u.uz) - 2;
640.  		pline("Sorry...");
641.  	    }
642.  	    /* no teleporting out of quest dungeon */
643.  	    if (In_quest(&u.uz) && newlev < depth(&qstart_level))
644.  		newlev = depth(&qstart_level);
645.  	    /* the player thinks of levels purely in logical terms, so
646.  	     * we must translate newlev to a number relative to the
647.  	     * current dungeon.
648.  	     */
649.  	    get_level(&newlevel, newlev);
650.  	}
651.  	schedule_goto(&newlevel, FALSE, FALSE, 0, (char *)0, (char *)0);
652.  	/* in case player just read a scroll and is about to be asked to
653.  	   call it something, we can't defer until the end of the turn */
654.  	if (u.utotype && !flags.mon_moving) deferred_goto();
655.  }
656.  
657.  void
658.  domagicportal(ttmp)
659.  register struct trap *ttmp;
660.  {
661.  	struct d_level target_level;
662.  
663.  	if (!next_to_u()) {
664.  		You(shudder_for_moment);
665.  		return;
666.  	}
667.  
668.  	/* if landed from another portal, do nothing */
669.  	/* problem: level teleport landing escapes the check */
670.  	if (!on_level(&u.uz, &u.uz0)) return;
671.  
672.  	You("activated a magic portal!");
673.  
674.  	/* prevent the poor shnook, whose amulet was stolen while in
675.  	 * the endgame, from accidently triggering the portal to the
676.  	 * next level, and thus losing the game
677.  	 */
678.  	if (In_endgame(&u.uz) && !u.uhave.amulet) {
679.  	    You_feel("dizzy for a moment, but nothing happens...");
680.  	    return;
681.  	}
682.  
683.  	target_level = ttmp->dst;
684.  	schedule_goto(&target_level, FALSE, FALSE, 1,
685.  		      "You feel dizzy for a moment, but the sensation passes.",
686.  		      (char *)0);
687.  }
688.  
689.  void
690.  tele_trap(trap)
691.  struct trap *trap;
692.  {
693.  	if (In_endgame(&u.uz) || Antimagic) {
694.  		if (Antimagic)
695.  			shieldeff(u.ux, u.uy);
696.  		You_feel("a wrenching sensation.");
697.  	} else if (!next_to_u()) {
698.  		You(shudder_for_moment);
699.  	} else if (trap->once) {
700.  		deltrap(trap);
701.  		newsym(u.ux,u.uy);	/* get rid of trap symbol */
702.  		vault_tele();
703.  	} else
704.  		tele();
705.  }
706.  
707.  void
708.  level_tele_trap(trap)
709.  struct trap *trap;
710.  {
711.  	You("%s onto a level teleport trap!",
712.  		      Levitation ? (const char *)"float" :
713.  				  locomotion(youmonst.data, "step"));
714.  	if (Antimagic) {
715.  	    shieldeff(u.ux, u.uy);
716.  	}
717.  	if (Antimagic || In_endgame(&u.uz)) {
718.  	    You_feel("a wrenching sensation.");
719.  	    return;
720.  	}
721.  	if (!Blind)
722.  	    You("are momentarily blinded by a flash of light.");
723.  	else
724.  	    You("are momentarily disoriented.");
725.  	deltrap(trap);
726.  	newsym(u.ux,u.uy);	/* get rid of trap symbol */
727.  	level_tele();
728.  }
729.  
730.  /* check whether monster can arrive at location <x,y> via Tport (or fall) */
731.  STATIC_OVL boolean
732.  rloc_pos_ok(x, y, mtmp)
733.  register int x, y;		/* coordinates of candidate location */
734.  struct monst *mtmp;
735.  {
736.  	register int xx, yy;
737.  
738.  	if (!goodpos(x, y, mtmp)) return FALSE;
739.  	/*
740.  	 * Check for restricted areas present in some special levels.
741.  	 *
742.  	 * `xx' is current column; if 0, then `yy' will contain flag bits
743.  	 * rather than row:  bit #0 set => moving upwards; bit #1 set =>
744.  	 * inside the Wizard's tower.
745.  	 */
746.  	xx = mtmp->mx;
747.  	yy = mtmp->my;
748.  	if (!xx) {
749.  	    /* no current location (migrating monster arrival) */
750.  	    if (dndest.nlx && On_W_tower_level(&u.uz))
751.  		return ((yy & 2) != 0) ^	/* inside xor not within */
752.  		       !within_bounded_area(x, y, dndest.nlx, dndest.nly,
753.  						  dndest.nhx, dndest.nhy);
754.  	    if (updest.lx && (yy & 1) != 0)	/* moving up */
755.  		return (within_bounded_area(x, y, updest.lx, updest.ly,
756.  						  updest.hx, updest.hy) &&
757.  		       (!updest.nlx ||
758.  			!within_bounded_area(x, y, updest.nlx, updest.nly,
759.  						   updest.nhx, updest.nhy)));
760.  	    if (dndest.lx && (yy & 1) == 0)	/* moving down */
761.  		return (within_bounded_area(x, y, dndest.lx, dndest.ly,
762.  						  dndest.hx, dndest.hy) &&
763.  		       (!dndest.nlx ||
764.  			!within_bounded_area(x, y, dndest.nlx, dndest.nly,
765.  						   dndest.nhx, dndest.nhy)));
766.  	} else {
767.  	    /* current location is <xx,yy> */
768.  	    if (!tele_jump_ok(xx, yy, x, y)) return FALSE;
769.  	}
770.  	/* <x,y> is ok */
771.  	return TRUE;
772.  }
773.  
774.  /*
775.   * rloc_to()
776.   *
777.   * Pulls a monster from its current position and places a monster at
778.   * a new x and y.  If oldx is 0, then the monster was not in the levels.monsters
779.   * array.  However, if oldx is 0, oldy may still have a value because mtmp is a
780.   * migrating_mon.  Worm tails are always placed randomly around the head of
781.   * the worm.
782.   */
783.  void
784.  rloc_to(mtmp, x, y)
785.  struct monst *mtmp;
786.  register int x, y;
787.  {
788.  	register int oldx = mtmp->mx, oldy = mtmp->my;
789.  
790.  	if (x == mtmp->mx && y == mtmp->my)	/* that was easy */
791.  		return;
792.  
793.  	if (oldx) {				/* "pick up" monster */
794.  	    if (mtmp->wormno)
795.  		remove_worm(mtmp);
796.  	    else {
797.  		remove_monster(oldx, oldy);
798.  		newsym(oldx, oldy);		/* update old location */
799.  	    }
800.  	}
801.  
802.  	place_monster(mtmp, x, y);		/* put monster down */
803.  	update_monster_region(mtmp);
804.  
805.  	if (mtmp->wormno)			/* now put down tail */
806.  		place_worm_tail_randomly(mtmp, x, y);
807.  
808.  	if (u.ustuck == mtmp) {
809.  		if (u.uswallow) {
810.  			u.ux = x;
811.  			u.uy = y;
812.  			docrt();
813.  		} else	u.ustuck = 0;
814.  	}
815.  
816.  	newsym(x, y);				/* update new location */
817.  	set_apparxy(mtmp);			/* orient monster */
818.  }
819.  
820.  /* place a monster at a random location, typically due to teleport */
821.  void
822.  rloc(mtmp)
823.  struct monst *mtmp;	/* mx==0 implies migrating monster arrival */
824.  {
825.  	register int x, y, trycount;
826.  	xchar omx = mtmp->mx, omy = mtmp->my;
827.  
828.  	if (mtmp->iswiz && omx) {	/* Wizard, not just arriving */
829.  	    if (!In_W_tower(u.ux, u.uy, &u.uz))
830.  		x = xupstair,  y = yupstair;
831.  	    else if (!xdnladder)	/* bottom level of tower */
832.  		x = xupladder,  y = yupladder;
833.  	    else
834.  		x = xdnladder,  y = ydnladder;
835.  	    /* if the wiz teleports away to heal, try the up staircase,
836.  	       to block the player's escaping before he's healed
837.  	       (deliberately use `goodpos' rather than `rloc_pos_ok' here) */
838.  	    if (goodpos(x, y, mtmp))
839.  		goto found_xy;
840.  	}
841.  
842.  	trycount = 0;
843.  	do {
844.  	    x = rn1(COLNO-3,2);
845.  	    y = rn2(ROWNO);
846.  	    if ((trycount < 500) ? rloc_pos_ok(x, y, mtmp)
847.  				 : goodpos(x, y, mtmp))
848.  		goto found_xy;
849.  	} while (++trycount < 1000);
850.  
851.  	/* last ditch attempt to find a good place */
852.  	for (x = 2; x < COLNO - 1; x++)
853.  	    for (y = 0; y < ROWNO; y++)
854.  		if (goodpos(x, y, mtmp))
855.  		    goto found_xy;
856.  
857.  	/* level either full of monsters or somehow faulty */
858.  	impossible("rloc(): couldn't relocate monster");
859.  	return;
860.  
861.   found_xy:
862.  	rloc_to(mtmp, x, y);
863.  	/* shopkeepers will only teleport if you zap them with a wand of
864.  	   teleportation or if they've been transformed into a jumpy monster;
865.  	   the latter only happens if you've attacked them with polymorph */
866.  	if (mtmp->isshk && !inhishop(mtmp)) make_angry_shk(mtmp, omx, omy);
867.  }
868.  
869.  STATIC_OVL void
870.  mvault_tele(mtmp)
871.  struct monst *mtmp;
872.  {
873.  	register struct mkroom *croom = search_special(VAULT);
874.  	coord c;
875.  
876.  	if (croom && somexy(croom, &c) &&
877.  				goodpos(c.x, c.y, mtmp)) {
878.  		rloc_to(mtmp, c.x, c.y);
879.  		return;
880.  	}
881.  	rloc(mtmp);
882.  }
883.  
884.  boolean
885.  tele_restrict(mon)
886.  struct monst *mon;
887.  {
888.  	if (level.flags.noteleport) {
889.  		if (canseemon(mon))
890.  		    pline("A mysterious force prevents %s from teleporting!",
891.  			mon_nam(mon));
892.  		return TRUE;
893.  	}
894.  	return FALSE;
895.  }
896.  
897.  void
898.  mtele_trap(mtmp, trap, in_sight)
899.  struct monst *mtmp;
900.  struct trap *trap;
901.  int in_sight;
902.  {
903.  	char *monname;
904.  
905.  	if (tele_restrict(mtmp)) return;
906.  	if (teleport_pet(mtmp, FALSE)) {
907.  	    /* save name with pre-movement visibility */
908.  	    monname = Monnam(mtmp);
909.  
910.  	    /* Note: don't remove the trap if a vault.  Other-
911.  	     * wise the monster will be stuck there, since
912.  	     * the guard isn't going to come for it...
913.  	     */
914.  	    if (trap->once) mvault_tele(mtmp);
915.  	    else rloc(mtmp);
916.  
917.  	    if (in_sight) {
918.  		if (canseemon(mtmp))
919.  		    pline("%s seems disoriented.", monname);
920.  		else
921.  		    pline("%s suddenly disappears!", monname);
922.  		seetrap(trap);
923.  	    }
924.  	}
925.  }
926.  
927.  /* return 0 if still on level, 3 if not */
928.  int
929.  mlevel_tele_trap(mtmp, trap, force_it, in_sight)
930.  struct monst *mtmp;
931.  struct trap *trap;
932.  boolean force_it;
933.  int in_sight;
934.  {
935.  	int tt = trap->ttyp;
936.  	struct permonst *mptr = mtmp->data;
937.  
938.  	if (mtmp == u.ustuck)	/* probably a vortex */
939.  	    return 0;		/* temporary? kludge */
940.  	if (teleport_pet(mtmp, force_it)) {
941.  	    d_level tolevel;
942.  	    int migrate_typ = MIGR_RANDOM;
943.  
944.  	    if ((tt == HOLE || tt == TRAPDOOR)) {
945.  		if (Is_stronghold(&u.uz)) {
946.  		    assign_level(&tolevel, &valley_level);
947.  		} else if (Is_botlevel(&u.uz)) {
948.  		    if (in_sight && trap->tseen)
949.  			pline("%s avoids the %s.", Monnam(mtmp),
950.  			(tt == HOLE) ? "hole" : "trap");
951.  		    return 0;
952.  		} else {
953.  		    get_level(&tolevel, depth(&u.uz) + 1);
954.  		}
955.  	    } else if (tt == MAGIC_PORTAL) {
956.  		if (In_endgame(&u.uz) &&
957.  		    (mon_has_amulet(mtmp) || is_home_elemental(mptr))) {
958.  		    if (in_sight && mptr->mlet != S_ELEMENTAL) {
959.  			pline("%s seems to shimmer for a moment.",
960.  							Monnam(mtmp));
961.  			seetrap(trap);
962.  		    }
963.  		    return 0;
964.  		} else {
965.  		    assign_level(&tolevel, &trap->dst);
966.  		    migrate_typ = MIGR_PORTAL;
967.  		}
968.  	    } else { /* (tt == LEVEL_TELEP) */
969.  		int nlev;
970.  
971.  		if (mon_has_amulet(mtmp) || In_endgame(&u.uz)) {
972.  		    if (in_sight)
973.  			pline("%s seems very disoriented for a moment.",
974.  				Monnam(mtmp));
975.  		    return 0;
976.  		}
977.  		nlev = random_teleport_level();
978.  		if (nlev == depth(&u.uz)) {
979.  		    if (in_sight)
980.  			pline("%s shudders for a moment.", Monnam(mtmp));
981.  		    return 0;
982.  		}
983.  		get_level(&tolevel, nlev);
984.  	    }
985.  
986.  	    if (in_sight) {
987.  		pline("Suddenly, %s disappears out of sight.", mon_nam(mtmp));
988.  		seetrap(trap);
989.  	    }
990.  	    migrate_to_level(mtmp, ledger_no(&tolevel),
991.  			     migrate_typ, (coord *)0);
992.  	    return 3;	/* no longer on this level */
993.  	}
994.  	return 0;
995.  }
996.  
997.  
998.  void
999.  rloco(obj)
1000. register struct obj *obj;
1001. {
1002. 	register xchar tx, ty, otx, oty;
1003. 	boolean restricted_fall;
1004. 	int try_limit = 4000;
1005. 
1006. 	obj_extract_self(obj);
1007. 	otx = obj->ox;
1008. 	oty = obj->oy;
1009. 	restricted_fall = (otx == 0 && dndest.lx);
1010. 	do {
1011. 	    tx = rn1(COLNO-3,2);
1012. 	    ty = rn2(ROWNO);
1013. 	    if (!--try_limit) break;
1014. 	} while (!goodpos(tx, ty, (struct monst *)0) ||
1015. 		/* bug: this lacks provision for handling the Wizard's tower */
1016. 		 (restricted_fall &&
1017. 		  (!within_bounded_area(tx, ty, dndest.lx, dndest.ly,
1018. 						dndest.hx, dndest.hy) ||
1019. 		   (dndest.nlx &&
1020. 		    within_bounded_area(tx, ty, dndest.nlx, dndest.nly,
1021. 						dndest.nhx, dndest.nhy)))));
1022. 
1023. 	if (flooreffects(obj, tx, ty, "fall")) {
1024. 	    return;
1025. 	} else if (otx == 0 && oty == 0) {
1026. 	    ;	/* fell through a trapdoor; no update of old loc needed */
1027. 	} else {
1028. 	    if (costly_spot(otx, oty)
1029. 	      && (!costly_spot(tx, ty) ||
1030. 		  !index(in_rooms(tx, ty, 0), *in_rooms(otx, oty, 0)))) {
1031. 		if (costly_spot(u.ux, u.uy) &&
1032. 			    index(u.urooms, *in_rooms(otx, oty, 0)))
1033. 		    addtobill(obj, FALSE, FALSE, FALSE);
1034. 		else (void)stolen_value(obj, otx, oty, FALSE, FALSE);
1035. 	    }
1036. 	    newsym(otx, oty);	/* update old location */
1037. 	}
1038. 	place_object(obj, tx, ty);
1039. 	newsym(tx, ty);
1040. }
1041. 
1042. /* Returns an absolute depth */
1043. int
1044. random_teleport_level()
1045. {
1046. 	int nlev, max_depth, min_depth;
1047. 
1048. 	if (!rn2(5) || Is_knox(&u.uz))
1049. 		return (int)depth(&u.uz);
1050. 
1051. 	/* Get a random value relative to the current dungeon */
1052. 	/* Range is 1 to current+3, current not counting */
1053. 	nlev = rnd((int)depth(&u.uz) + 2);
1054. 	if (nlev >= (int)depth(&u.uz)) nlev++;
1055. 
1056. 	/* What I really want to do is as follows:
1057. 	 * -- If in a dungeon that goes down, the new level is to be restricted
1058. 	 *    to [top of parent, bottom of current dungeon]
1059. 	 * -- If in a dungeon that goes up, the new level is to be restricted
1060. 	 *    to [top of current dungeon, bottom of parent]
1061. 	 * -- If in a quest dungeon or similar dungeon entered by portals,
1062. 	 *    the new level is to be restricted to [top of current dungeon,
1063. 	 *    bottom of current dungeon]
1064. 	 * The current behavior is not as sophisticated as that ideal, but is
1065. 	 * still better what we used to do, which was like this for players
1066. 	 * but different for monsters for no obvious reason.  Currently, we
1067. 	 * must explicitly check for special dungeons.  We check for Knox
1068. 	 * above; endgame is handled in the caller due to its different
1069. 	 * message ("disoriented").
1070. 	 * --KAA
1071. 	 */
1072. 	min_depth = 1;
1073. 	max_depth = dunlevs_in_dungeon(&u.uz) +
1074. 			(dungeons[u.uz.dnum].depth_start - 1);
1075. 
1076. 	if (nlev > max_depth) {
1077. 	    nlev = max_depth;
1078. 	    /* teleport up if already on bottom */
1079. 	    if (Is_botlevel(&u.uz)) nlev -= rnd(3);
1080. 	}
1081. 	if (nlev < min_depth) {
1082. 	    nlev = min_depth;
1083. 	    if ((int)depth(&u.uz) == min_depth) {
1084. 		nlev += rnd(3);
1085. 		if (nlev > max_depth)
1086. 		    nlev = max_depth;
1087. 	    }
1088. 	}
1089. 	return nlev;
1090. }
1091. 
1092. /* you teleport a monster (via wand, spell, or poly'd q.mechanic attack);
1093.    return false iff the attempt fails */
1094. boolean
1095. u_teleport_mon(mtmp, give_feedback)
1096. struct monst *mtmp;
1097. boolean give_feedback;
1098. {
1099. 	coord cc;
1100. 
1101. 	if (mtmp->ispriest && *in_rooms(mtmp->mx, mtmp->my, TEMPLE)) {
1102. 	    if (give_feedback)
1103. 		pline("%s resists your magic!", Monnam(mtmp));
1104. 	    return FALSE;
1105. 	} else {
1106. 	    if (is_rider(mtmp->data) && rn2(13) &&
1107. 		    enexto(&cc, u.ux, u.uy, mtmp->data))
1108. 		rloc_to(mtmp, cc.x, cc.y);
1109. 	    else
1110. 		rloc(mtmp);
1111. 	    return TRUE;
1112. 	}
1113. }
1114. 
1115. /*teleport.c*/