1 /* NetHack 3.6 mkmaze.c $NHDT-Date: 1555022325 2019/04/11 22:38:45 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.68 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /*-Copyright (c) Pasi Kallinen, 2018. */
4 /* NetHack may be freely redistributed. See license for details. */
6 /* JNetHack Copyright */
7 /* (c) Issei Numata, Naoki Hamada, Shigehiro Miyashita, 1994-2000 */
8 /* For 3.4-, Copyright (c) SHIRAKATA Kentaro, 2002-2019 */
9 /* JNetHack may be freely redistributed. See license for details. */
13 #include "lev.h" /* save & restore info */
15 /* from sp_lev.c, for fixup_special() */
16 extern lev_region *lregions;
17 extern int num_lregions;
18 /* for preserving the insect legs when wallifying baalz level */
19 static lev_region bughack = { {COLNO, ROWNO, 0, 0}, {COLNO, ROWNO, 0, 0} };
21 STATIC_DCL int FDECL(iswall, (int, int));
22 STATIC_DCL int FDECL(iswall_or_stone, (int, int));
23 STATIC_DCL boolean FDECL(is_solid, (int, int));
24 STATIC_DCL int FDECL(extend_spine, (int[3][3], int, int, int));
25 STATIC_DCL boolean FDECL(okay, (int, int, int));
26 STATIC_DCL void FDECL(maze0xy, (coord *));
27 STATIC_DCL boolean FDECL(put_lregion_here, (XCHAR_P, XCHAR_P, XCHAR_P,
28 XCHAR_P, XCHAR_P, XCHAR_P,
29 XCHAR_P, BOOLEAN_P, d_level *));
30 STATIC_DCL void NDECL(baalz_fixup);
31 STATIC_DCL void NDECL(setup_waterlevel);
32 STATIC_DCL void NDECL(unsetup_waterlevel);
33 STATIC_DCL void FDECL(check_ransacked, (char *));
34 STATIC_DCL void FDECL(migr_booty_item, (int, const char *));
35 STATIC_DCL void FDECL(migrate_orc, (struct monst *, unsigned long));
36 STATIC_DCL void NDECL(stolen_booty);
38 /* adjust a coordinate one step in the specified direction */
39 #define mz_move(X, Y, dir) \
42 case 0: --(Y); break; \
43 case 1: (X)++; break; \
44 case 2: (Y)++; break; \
45 case 3: --(X); break; \
46 default: panic("mz_move: bad direction %d", dir); \
58 type = levl[x][y].typ;
59 return (IS_WALL(type) || IS_DOOR(type)
60 || type == SDOOR || type == IRONBARS);
67 /* out of bounds = stone */
71 return (levl[x][y].typ == STONE || iswall(x, y));
74 /* return TRUE if out of bounds, wall or rock */
79 return (boolean) (!isok(x, y) || IS_STWALL(levl[x][y].typ));
83 * Return 1 (not TRUE - we're doing bit vectors here) if we want to extend
84 * a wall spine in the (dx,dy) direction. Return 0 otherwise.
86 * To extend a wall spine in that direction, first there must be a wall there.
87 * Then, extend a spine unless the current position is surrounded by walls
88 * in the direction given by (dx,dy). E.g. if 'x' is our location, 'W'
89 * a wall, '.' a room, 'a' anything (we don't care), and our direction is
90 * (0,1) - South or down - then:
93 * W x W This would not extend a spine from x down
94 * W W W (a corridor of walls is formed).
97 * W x W This would extend a spine from x down.
101 extend_spine(locale, wall_there, dx, dy)
103 int wall_there, dx, dy;
110 if (wall_there) { /* wall in that direction */
112 if (locale[1][0] && locale[1][2] /* EW are wall/stone */
113 && locale[nx][0] && locale[nx][2]) { /* diag are wall/stone */
119 if (locale[0][1] && locale[2][1] /* NS are wall/stone */
120 && locale[0][ny] && locale[2][ny]) { /* diag are wall/stone */
133 /* Remove walls totally surrounded by stone */
135 wall_cleanup(x1, y1, x2, y2)
142 /* sanity check on incoming variables */
143 if (x1 < 0 || x2 >= COLNO || x1 > x2 || y1 < 0 || y2 >= ROWNO || y1 > y2)
144 panic("wall_cleanup: bad bounds (%d,%d) to (%d,%d)", x1, y1, x2, y2);
146 /* change walls surrounded by rock to rock. */
147 for (x = x1; x <= x2; x++)
148 for (y = y1; y <= y2; y++) {
149 if (within_bounded_area(x, y,
150 bughack.inarea.x1, bughack.inarea.y1,
151 bughack.inarea.x2, bughack.inarea.y2))
155 if (IS_WALL(type) && type != DBWALL) {
156 if (is_solid(x - 1, y - 1) && is_solid(x - 1, y)
157 && is_solid(x - 1, y + 1) && is_solid(x, y - 1)
158 && is_solid(x, y + 1) && is_solid(x + 1, y - 1)
159 && is_solid(x + 1, y) && is_solid(x + 1, y + 1))
165 /* Correct wall types so they extend and connect to each other */
167 fix_wall_spines(x1, y1, x2, y2)
173 int FDECL((*loc_f), (int, int));
175 int locale[3][3]; /* rock or wall status surrounding positions */
178 * Value 0 represents a free-standing wall. It could be anything,
179 * so even though this table says VWALL, we actually leave whatever
180 * typ was there alone.
182 static xchar spine_array[16] = { VWALL, HWALL, HWALL, HWALL,
183 VWALL, TRCORNER, TLCORNER, TDWALL,
184 VWALL, BRCORNER, BLCORNER, TUWALL,
185 VWALL, TLWALL, TRWALL, CROSSWALL };
187 /* sanity check on incoming variables */
188 if (x1 < 0 || x2 >= COLNO || x1 > x2 || y1 < 0 || y2 >= ROWNO || y1 > y2)
189 panic("wall_extends: bad bounds (%d,%d) to (%d,%d)", x1, y1, x2, y2);
191 /* set the correct wall type. */
192 for (x = x1; x <= x2; x++)
193 for (y = y1; y <= y2; y++) {
196 if (!(IS_WALL(type) && type != DBWALL))
199 /* set the locations TRUE if rock or wall or out of bounds */
200 loc_f = within_bounded_area(x, y, /* for baalz insect */
201 bughack.inarea.x1, bughack.inarea.y1,
202 bughack.inarea.x2, bughack.inarea.y2)
205 locale[0][0] = (*loc_f)(x - 1, y - 1);
206 locale[1][0] = (*loc_f)(x, y - 1);
207 locale[2][0] = (*loc_f)(x + 1, y - 1);
209 locale[0][1] = (*loc_f)(x - 1, y);
210 locale[2][1] = (*loc_f)(x + 1, y);
212 locale[0][2] = (*loc_f)(x - 1, y + 1);
213 locale[1][2] = (*loc_f)(x, y + 1);
214 locale[2][2] = (*loc_f)(x + 1, y + 1);
216 /* determine if wall should extend to each direction NSEW */
217 bits = (extend_spine(locale, iswall(x, y - 1), 0, -1) << 3)
218 | (extend_spine(locale, iswall(x, y + 1), 0, 1) << 2)
219 | (extend_spine(locale, iswall(x + 1, y), 1, 0) << 1)
220 | extend_spine(locale, iswall(x - 1, y), -1, 0);
222 /* don't change typ if wall is free-standing */
224 lev->typ = spine_array[bits];
229 wallification(x1, y1, x2, y2)
232 wall_cleanup(x1, y1, x2, y2);
233 fix_wall_spines(x1, y1, x2, y2);
243 if (x < 3 || y < 3 || x > x_maze_max || y > y_maze_max
244 || levl[x][y].typ != STONE)
249 /* find random starting point for maze generation */
254 cc->x = 3 + 2 * rn2((x_maze_max >> 1) - 1);
255 cc->y = 3 + 2 * rn2((y_maze_max >> 1) - 1);
262 * pos is inside restricted region (lx,ly,hx,hy) OR
263 * NOT (pos is corridor and a maze level OR pos is a room OR pos is air)
266 bad_location(x, y, lx, ly, hx, hy)
268 xchar lx, ly, hx, hy;
270 return (boolean) (occupied(x, y)
271 || within_bounded_area(x, y, lx, ly, hx, hy)
272 || !((levl[x][y].typ == CORR && level.flags.is_maze_lev)
273 || levl[x][y].typ == ROOM
274 || levl[x][y].typ == AIR));
277 /* pick a location in area (lx, ly, hx, hy) but not in (nlx, nly, nhx, nhy)
278 and place something (based on rtype) in that region */
280 place_lregion(lx, ly, hx, hy, nlx, nly, nhx, nhy, rtype, lev)
281 xchar lx, ly, hx, hy;
282 xchar nlx, nly, nhx, nhy;
290 if (!lx) { /* default to whole level */
292 * if there are rooms and this a branch, let place_branch choose
293 * the branch location (to avoid putting branches in corridors).
295 if (rtype == LR_BRANCH && nroom) {
296 place_branch(Is_branchlev(&u.uz), 0, 0);
300 lx = 1; /* column 0 is not used */
302 ly = 0; /* 3.6.0 and earlier erroneously had 1 here */
306 /* first a probabilistic approach */
308 oneshot = (lx == hx && ly == hy);
309 for (trycnt = 0; trycnt < 200; trycnt++) {
310 x = rn1((hx - lx) + 1, lx);
311 y = rn1((hy - ly) + 1, ly);
312 if (put_lregion_here(x, y, nlx, nly, nhx, nhy, rtype, oneshot, lev))
316 /* then a deterministic one */
318 for (x = lx; x <= hx; x++)
319 for (y = ly; y <= hy; y++)
320 if (put_lregion_here(x, y, nlx, nly, nhx, nhy, rtype, TRUE, lev))
323 impossible("Couldn't place lregion type %d!", rtype);
327 put_lregion_here(x, y, nlx, nly, nhx, nhy, rtype, oneshot, lev)
329 xchar nlx, nly, nhx, nhy;
336 if (bad_location(x, y, nlx, nly, nhx, nhy)) {
338 return FALSE; /* caller should try again */
340 /* Must make do with the only location possible;
341 avoid failure due to a misplaced trap.
342 It might still fail if there's a dungeon feature here. */
343 struct trap *t = t_at(x, y);
345 if (t && t->ttyp != MAGIC_PORTAL && t->ttyp != VIBRATING_SQUARE)
347 if (bad_location(x, y, nlx, nly, nhx, nhy))
355 /* "something" means the player in this case */
356 if ((mtmp = m_at(x, y)) != 0) {
357 /* move the monster if no choice, or just try again */
359 if (!rloc(mtmp, TRUE))
367 mkportal(x, y, lev->dnum, lev->dlevel);
371 mkstairs(x, y, (char) rtype, (struct mkroom *) 0);
374 place_branch(Is_branchlev(&u.uz), x, y);
380 /* fix up Baalzebub's lair, which depicts a level-sized beetle;
381 its legs are walls within solid rock--regular wallification
382 classifies them as superfluous and gets rid of them */
387 int x, y, lastx, lasty;
390 * baalz level's nondiggable region surrounds the "insect" and rooms.
391 * The outermost perimeter of that region is subject to wall cleanup
392 * (hence 'x + 1' and 'y + 1' for starting don't-clean column and row,
393 * 'lastx - 1' and 'lasty - 1' for ending don't-clean column and row)
394 * and the interior is protected against that (in wall_cleanup()).
396 * Assumes level.flags.corrmaze is True, otherwise the bug legs will
397 * have already been "cleaned" away by general wallification.
400 /* find low and high x for to-be-wallified portion of level */
402 for (lastx = x = 0; x < COLNO; ++x)
403 if ((levl[x][y].wall_info & W_NONDIGGABLE) != 0) {
405 bughack.inarea.x1 = x + 1;
408 bughack.inarea.x2 = ((lastx > bughack.inarea.x1) ? lastx : x) - 1;
409 /* find low and high y for to-be-wallified portion of level */
410 x = bughack.inarea.x1;
411 for (lasty = y = 0; y < ROWNO; ++y)
412 if ((levl[x][y].wall_info & W_NONDIGGABLE) != 0) {
414 bughack.inarea.y1 = y + 1;
417 bughack.inarea.y2 = ((lasty > bughack.inarea.y1) ? lasty : y) - 1;
418 /* two pools mark where special post-wallify fix-ups are needed */
419 for (x = bughack.inarea.x1; x <= bughack.inarea.x2; ++x)
420 for (y = bughack.inarea.y1; y <= bughack.inarea.y2; ++y)
421 if (levl[x][y].typ == POOL) {
422 levl[x][y].typ = HWALL;
423 if (bughack.delarea.x1 == COLNO)
424 bughack.delarea.x1 = x, bughack.delarea.y1 = y;
426 bughack.delarea.x2 = x, bughack.delarea.y2 = y;
427 } else if (levl[x][y].typ == IRONBARS) {
428 /* novelty effect; allowing digging in front of 'eyes' */
429 levl[x - 1][y].wall_info &= ~W_NONDIGGABLE;
431 levl[x - 2][y].wall_info &= ~W_NONDIGGABLE;
434 wallification(max(bughack.inarea.x1 - 2, 1),
435 max(bughack.inarea.y1 - 2, 0),
436 min(bughack.inarea.x2 + 2, COLNO - 1),
437 min(bughack.inarea.y2 + 2, ROWNO - 1));
439 /* bughack hack for rear-most legs on baalz level; first joint on
440 both top and bottom gets a bogus extra connection to room area,
441 producing unwanted rectangles; change back to separated legs */
442 x = bughack.delarea.x1, y = bughack.delarea.y1;
443 if (isok(x, y) && levl[x][y].typ == TLWALL
444 && isok(x, y + 1) && levl[x][y + 1].typ == TUWALL) {
445 levl[x][y].typ = BRCORNER;
446 levl[x][y + 1].typ = HWALL;
447 if ((mtmp = m_at(x, y)) != 0) /* something at temporary pool... */
448 (void) rloc(mtmp, FALSE);
450 x = bughack.delarea.x2, y = bughack.delarea.y2;
451 if (isok(x, y) && levl[x][y].typ == TLWALL
452 && isok(x, y - 1) && levl[x][y - 1].typ == TDWALL) {
453 levl[x][y].typ = TRCORNER;
454 levl[x][y - 1].typ = HWALL;
455 if ((mtmp = m_at(x, y)) != 0) /* something at temporary pool... */
456 (void) rloc(mtmp, FALSE);
459 /* reset bughack region; set low end to <COLNO,ROWNO> so that
460 within_bounded_region() in fix_wall_spines() will fail
461 most quickly--on its first test--when loading other levels */
462 bughack.inarea.x1 = bughack.delarea.x1 = COLNO;
463 bughack.inarea.y1 = bughack.delarea.y1 = ROWNO;
464 bughack.inarea.x2 = bughack.delarea.x2 = 0;
465 bughack.inarea.y2 = bughack.delarea.y2 = 0;
468 static boolean was_waterlevel; /* ugh... this shouldn't be needed */
470 /* this is special stuff that the level compiler cannot (yet) handle */
474 lev_region *r = lregions;
477 struct mkroom *croom;
478 boolean added_branch = FALSE;
480 if (was_waterlevel) {
481 was_waterlevel = FALSE;
483 unsetup_waterlevel();
485 if (Is_waterlevel(&u.uz) || Is_airlevel(&u.uz)) {
486 level.flags.hero_memory = 0;
487 was_waterlevel = TRUE;
488 /* water level is an odd beast - it has to be set up
489 before calling place_lregions etc. */
492 for (x = 0; x < num_lregions; x++, r++) {
499 if (*r->rname.str >= '0' && *r->rname.str <= '9') {
500 /* "chutes and ladders" */
502 lev.dlevel = atoi(r->rname.str);
504 s_level *sp = find_level(r->rname.str);
512 place_lregion(r->inarea.x1, r->inarea.y1, r->inarea.x2,
513 r->inarea.y2, r->delarea.x1, r->delarea.y1,
514 r->delarea.x2, r->delarea.y2, r->rtype, &lev);
520 /* save the region outlines for goto_level() */
521 if (r->rtype == LR_TELE || r->rtype == LR_UPTELE) {
522 updest.lx = r->inarea.x1;
523 updest.ly = r->inarea.y1;
524 updest.hx = r->inarea.x2;
525 updest.hy = r->inarea.y2;
526 updest.nlx = r->delarea.x1;
527 updest.nly = r->delarea.y1;
528 updest.nhx = r->delarea.x2;
529 updest.nhy = r->delarea.y2;
531 if (r->rtype == LR_TELE || r->rtype == LR_DOWNTELE) {
532 dndest.lx = r->inarea.x1;
533 dndest.ly = r->inarea.y1;
534 dndest.hx = r->inarea.x2;
535 dndest.hy = r->inarea.y2;
536 dndest.nlx = r->delarea.x1;
537 dndest.nly = r->delarea.y1;
538 dndest.nhx = r->delarea.x2;
539 dndest.nhy = r->delarea.y2;
541 /* place_lregion gets called from goto_level() */
546 free((genericptr_t) r->rname.str), r->rname.str = 0;
549 /* place dungeon branch if not placed above */
550 if (!added_branch && Is_branchlev(&u.uz)) {
551 place_lregion(0, 0, 0, 0, 0, 0, 0, 0, LR_BRANCH, (d_level *) 0);
554 /* Still need to add some stuff to level file */
555 if (Is_medusa_level(&u.uz)) {
559 croom = &rooms[0]; /* only one room on the medusa level */
560 for (tryct = rnd(4); tryct; tryct--) {
563 if (goodpos(x, y, (struct monst *) 0, 0)) {
564 otmp = mk_tt_object(STATUE, x, y);
565 while (otmp && (poly_when_stoned(&mons[otmp->corpsenm])
566 || pm_resistance(&mons[otmp->corpsenm],
568 /* set_corpsenm() handles weight too */
569 set_corpsenm(otmp, rndmonnum());
575 otmp = mk_tt_object(STATUE, somex(croom), somey(croom));
576 else /* Medusa statues don't contain books */
578 mkcorpstat(STATUE, (struct monst *) 0, (struct permonst *) 0,
579 somex(croom), somey(croom), CORPSTAT_NONE);
581 while (pm_resistance(&mons[otmp->corpsenm], MR_STONE)
582 || poly_when_stoned(&mons[otmp->corpsenm])) {
583 /* set_corpsenm() handles weight too */
584 set_corpsenm(otmp, rndmonnum());
587 } else if (Is_wiz1_level(&u.uz)) {
588 croom = search_special(MORGUE);
590 create_secret_door(croom, W_SOUTH | W_EAST | W_WEST);
591 } else if (Is_knox(&u.uz)) {
592 /* using an unfilled morgue for rm id */
593 croom = search_special(MORGUE);
594 /* avoid inappropriate morgue-related messages */
595 level.flags.graveyard = level.flags.has_morgue = 0;
596 croom->rtype = OROOM; /* perhaps it should be set to VAULT? */
597 /* stock the main vault */
598 for (x = croom->lx; x <= croom->hx; x++)
599 for (y = croom->ly; y <= croom->hy; y++) {
600 (void) mkgold((long) rn1(300, 600), x, y);
601 if (!rn2(3) && !is_pool(x, y))
602 (void) maketrap(x, y, rn2(3) ? LANDMINE : SPIKED_PIT);
604 } else if (Role_if(PM_PRIEST) && In_quest(&u.uz)) {
605 /* less chance for undead corpses (lured from lower morgues) */
606 level.flags.graveyard = 1;
607 } else if (Is_stronghold(&u.uz)) {
608 level.flags.graveyard = 1;
609 } else if (Is_sanctum(&u.uz)) {
610 croom = search_special(TEMPLE);
612 create_secret_door(croom, W_ANY);
613 } else if (on_level(&u.uz, &orcus_level)) {
614 struct monst *mtmp, *mtmp2;
616 /* it's a ghost town, get rid of shopkeepers */
617 for (mtmp = fmon; mtmp; mtmp = mtmp2) {
622 } else if (on_level(&u.uz, &baalzebub_level)) {
623 /* custom wallify the "beetle" potion of the level */
625 } else if (u.uz.dnum == mines_dnum && ransacked) {
630 free((genericptr_t) lregions), lregions = 0;
638 /* this kludge only works as long as orctown is minetn-1 */
639 ransacked = (u.uz.dnum == mines_dnum && !strcmp(s, "minetn-1"));
643 static const char *orcfruit[] = { "paddle cactus", "dwarven root" };
646 migrate_orc(mtmp, mflags)
648 unsigned long mflags;
650 int nlev, max_depth, cur_depth;
653 cur_depth = (int) depth(&u.uz);
654 max_depth = dunlevs_in_dungeon(&u.uz)
655 + (dungeons[u.uz.dnum].depth_start - 1);
656 if (mflags == ORC_LEADER) {
657 /* Note that the orc leader will take possession of any
658 * remaining stuff not already delivered to other
659 * orcs between here and the bottom of the mines.
662 /* once in a blue moon, he won't be at the very bottom */
665 mtmp->mspare1 = MIGR_LEFTOVERS;
667 nlev = rn2((max_depth - cur_depth) + 1) + cur_depth;
668 if (nlev == cur_depth)
670 if (nlev > max_depth)
674 get_level(&dest, nlev);
675 migrate_to_level(mtmp, ledger_no(&dest), MIGR_RANDOM, (coord *) 0);
679 shiny_orc_stuff(mtmp)
682 int gemprob, goldprob, otyp;
684 boolean is_captain = (mtmp->data == &mons[PM_ORC_CAPTAIN]);
687 goldprob = is_captain ? 600 : 300;
688 gemprob = goldprob / 4;
689 if (rn2(1000) < goldprob) {
690 if ((otmp = mksobj(GOLD_PIECE, FALSE, FALSE)) != 0) {
691 otmp->quan = 1L + rnd(goldprob);
692 otmp->owt = weight(otmp);
693 add_to_minv(mtmp, otmp);
696 if (rn2(1000) < gemprob) {
697 if ((otmp = mkobj(GEM_CLASS, FALSE)) != 0) {
698 if (otmp->otyp == ROCK)
701 add_to_minv(mtmp, otmp);
704 if (is_captain || !rn2(8)) {
705 otyp = shiny_obj(RING_CLASS);
706 if (otyp != STRANGE_OBJECT && (otmp = mksobj(otyp, FALSE, FALSE)) != 0)
707 add_to_minv(mtmp, otmp);
711 migr_booty_item(otyp, gang)
717 otmp = mksobj_migr_to_species(otyp, (unsigned long) M2_ORC, FALSE, FALSE);
719 new_oname(otmp, strlen(gang) + 1); /* removes old name if present */
720 Strcpy(ONAME(otmp), gang);
721 if (otyp >= TRIPE_RATION && otyp <= TIN) {
722 if (otyp == SLIME_MOLD)
723 otmp->spe = fruitadd((char *) orcfruit[rn2(SIZE(orcfruit))],
725 otmp->quan += (long) rn2(3);
726 otmp->owt = weight(otmp);
732 stolen_booty(VOID_ARGS)
734 char *gang, gang_name[BUFSZ];
739 * --------------------------------------------------------
742 * A tragic accident has occurred in Frontier Town...
743 * It has been overrun by orcs.
745 * The booty that the orcs took from the town is now
746 * in the possession of the orcs that did this and
747 * have long since fled the level.
748 * --------------------------------------------------------
751 gang = rndorcname(gang_name);
752 /* create the stuff that the gang took */
754 for (i = 0; i < cnt; ++i)
755 migr_booty_item(rn2(4) ? TALLOW_CANDLE : WAX_CANDLE, gang);
757 for (i = 0; i < cnt; ++i)
758 migr_booty_item(SKELETON_KEY, gang);
759 otyp = rn2((GAUNTLETS_OF_DEXTERITY - LEATHER_GLOVES) + 1) + LEATHER_GLOVES;
760 migr_booty_item(otyp, gang);
762 for (i = 0; i < cnt; ++i) {
763 /* Food items - but no lembas! (or some other weird things) */
764 otyp = rn2((TIN - TRIPE_RATION) + 1) + TRIPE_RATION;
765 if (otyp != LEMBAS_WAFER && otyp != GLOB_OF_GRAY_OOZE
766 && otyp != GLOB_OF_BROWN_PUDDING && otyp != GLOB_OF_GREEN_SLIME
767 && otyp != GLOB_OF_BLACK_PUDDING && otyp != MEAT_STICK
768 && otyp != MEATBALL && otyp != MEAT_STICK && otyp != MEAT_RING
769 && otyp != HUGE_CHUNK_OF_MEAT && otyp != CORPSE)
770 migr_booty_item(otyp, gang);
772 migr_booty_item(rn2(2) ? LONG_SWORD : SILVER_SABER, gang);
773 /* create the leader of the orc gang */
774 mtmp = makemon(&mons[PM_ORC_CAPTAIN], 0, 0, MM_NONAME);
776 mtmp = christen_monst(mtmp, upstart(gang));
778 shiny_orc_stuff(mtmp);
779 migrate_orc(mtmp, ORC_LEADER);
781 /* Make most of the orcs on the level be part of the invading gang */
782 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
783 if (DEADMONSTER(mtmp))
786 if (is_orc(mtmp->data) && !has_mname(mtmp) && rn2(10)) {
788 * We'll consider the orc captain from the level
789 * .des file to be the captain of a rival orc horde
790 * who is there to see what has transpired, and to
791 * contemplate future action.
793 * Don't christen the orc captain as a subordinate
794 * member of the main orc horde.
796 if (mtmp->data != &mons[PM_ORC_CAPTAIN])
797 mtmp = christen_orc(mtmp, upstart(gang), "");
800 /* Lastly, ensure there's several more orcs from the gang along the way.
801 * The mechanics are such that they aren't actually identified as
802 * members of the invading gang until they get their spoils assigned
803 * to the inventory; handled during that assignment.
806 for (i = 0; i < cnt; ++i) {
809 mtyp = rn2((PM_ORC_SHAMAN - PM_ORC) + 1) + PM_ORC;
810 mtmp = makemon(&mons[mtyp], 0, 0, MM_NONAME);
812 shiny_orc_stuff(mtmp);
813 migrate_orc(mtmp, 0UL);
825 return (x >= 2 && y >= 2
826 && x < x_maze_max && y < y_maze_max && isok(x, y));
830 maze_remove_deadends(typ)
834 int x, y, dir, idx, idx2, dx, dy, dx2, dy2;
836 dirok[0] = 0; /* lint suppression */
837 for (x = 2; x < x_maze_max; x++)
838 for (y = 2; y < y_maze_max; y++)
839 if (ACCESSIBLE(levl[x][y].typ) && (x % 2) && (y % 2)) {
841 for (dir = 0; dir < 4; dir++) {
842 /* note: mz_move() is a macro which modifies
843 one of its first two parameters */
846 mz_move(dx, dy, dir);
847 if (!maze_inbounds(dx, dy)) {
851 mz_move(dx2, dy2, dir);
852 mz_move(dx2, dy2, dir);
853 if (!maze_inbounds(dx2, dy2)) {
857 if (!ACCESSIBLE(levl[dx][dy].typ)
858 && ACCESSIBLE(levl[dx2][dy2].typ)) {
863 if (idx2 >= 3 && idx > 0) {
866 dir = dirok[rn2(idx)];
867 mz_move(dx, dy, dir);
868 levl[dx][dy].typ = typ;
873 /* Create a maze with specified corridor width and wall thickness
874 * TODO: rewrite walkfrom so it works on temp space, not levl
877 create_maze(corrwid, wallthick)
883 int tmp_xmax = x_maze_max;
884 int tmp_ymax = y_maze_max;
891 else if (wallthick > 5)
896 else if (corrwid > 5)
899 scale = corrwid + wallthick;
900 rdx = (x_maze_max / scale);
901 rdy = (y_maze_max / scale);
903 if (level.flags.corrmaze)
904 for (x = 2; x < (rdx * 2); x++)
905 for (y = 2; y < (rdy * 2); y++)
906 levl[x][y].typ = STONE;
908 for (x = 2; x <= (rdx * 2); x++)
909 for (y = 2; y <= (rdy * 2); y++)
910 levl[x][y].typ = ((x % 2) && (y % 2)) ? STONE : HWALL;
912 /* set upper bounds for maze0xy and walkfrom */
913 x_maze_max = (rdx * 2);
914 y_maze_max = (rdy * 2);
918 walkfrom((int) mm.x, (int) mm.y, 0);
921 maze_remove_deadends((level.flags.corrmaze) ? CORR : ROOM);
924 x_maze_max = tmp_xmax;
925 y_maze_max = tmp_ymax;
927 /* scale maze up if needed */
929 char tmpmap[COLNO][ROWNO];
932 /* back up the existing smaller maze */
933 for (x = 1; x < x_maze_max; x++)
934 for (y = 1; y < y_maze_max; y++) {
935 tmpmap[x][y] = levl[x][y].typ;
940 while (rx < x_maze_max) {
941 int mx = (x % 2) ? corrwid
942 : ((x == 2 || x == (rdx * 2)) ? 1
945 while (ry < y_maze_max) {
947 int my = (y % 2) ? corrwid
948 : ((y == 2 || y == (rdy * 2)) ? 1
950 for (dx = 0; dx < mx; dx++)
951 for (dy = 0; dy < my; dy++) {
952 if (rx+dx >= x_maze_max
953 || ry+dy >= y_maze_max)
955 levl[rx + dx][ry + dy].typ = tmpmap[x][y];
974 s_level *sp = Is_special(&u.uz);
978 if (sp && sp->rndlevs)
979 Sprintf(protofile, "%s-%d", s, rnd((int) sp->rndlevs));
981 Strcpy(protofile, s);
982 } else if (*(dungeons[u.uz.dnum].proto)) {
983 if (dunlevs_in_dungeon(&u.uz) > 1) {
984 if (sp && sp->rndlevs)
985 Sprintf(protofile, "%s%d-%d", dungeons[u.uz.dnum].proto,
986 dunlev(&u.uz), rnd((int) sp->rndlevs));
988 Sprintf(protofile, "%s%d", dungeons[u.uz.dnum].proto,
990 } else if (sp && sp->rndlevs) {
991 Sprintf(protofile, "%s-%d", dungeons[u.uz.dnum].proto,
992 rnd((int) sp->rndlevs));
994 Strcpy(protofile, dungeons[u.uz.dnum].proto);
997 Strcpy(protofile, "");
999 /* SPLEVTYPE format is "level-choice,level-choice"... */
1000 if (wizard && *protofile && sp && sp->rndlevs) {
1001 char *ep = getenv("SPLEVTYPE"); /* not nh_getenv */
1003 /* rindex always succeeds due to code in prior block */
1004 int len = (int) ((rindex(protofile, '-') - protofile) + 1);
1007 if (!strncmp(ep, protofile, len)) {
1008 int pick = atoi(ep + len);
1009 /* use choice only if valid */
1010 if (pick > 0 && pick <= (int) sp->rndlevs)
1011 Sprintf(protofile + len, "%d", pick);
1014 ep = index(ep, ',');
1023 check_ransacked(protofile);
1024 Strcat(protofile, LEV_EXT);
1025 if (load_special(protofile)) {
1026 /* some levels can end up with monsters
1027 on dead mon list, including light source monsters */
1029 return; /* no mazification right now */
1031 impossible("Couldn't load \"%s\" - making a maze.", protofile);
1034 level.flags.is_maze_lev = TRUE;
1035 level.flags.corrmaze = !rn2(3);
1037 if (!Invocation_lev(&u.uz) && rn2(2)) {
1038 int corrscale = rnd(4);
1039 create_maze(corrscale,rnd(4)-corrscale);
1044 if (!level.flags.corrmaze)
1045 wallification(2, 2, x_maze_max, y_maze_max);
1048 mkstairs(mm.x, mm.y, 1, (struct mkroom *) 0); /* up */
1049 if (!Invocation_lev(&u.uz)) {
1051 mkstairs(mm.x, mm.y, 0, (struct mkroom *) 0); /* down */
1052 } else { /* choose "vibrating square" location */
1053 #define x_maze_min 2
1054 #define y_maze_min 2
1056 * Pick a position where the stairs down to Moloch's Sanctum
1057 * level will ultimately be created. At that time, an area
1058 * will be altered: walls removed, moat and traps generated,
1059 * boulders destroyed. The position picked here must ensure
1060 * that that invocation area won't extend off the map.
1062 * We actually allow up to 2 squares around the usual edge of
1063 * the area to get truncated; see mkinvokearea(mklev.c).
1065 #define INVPOS_X_MARGIN (6 - 2)
1066 #define INVPOS_Y_MARGIN (5 - 2)
1067 #define INVPOS_DISTANCE 11
1068 int x_range = x_maze_max - x_maze_min - 2 * INVPOS_X_MARGIN - 1,
1069 y_range = y_maze_max - y_maze_min - 2 * INVPOS_Y_MARGIN - 1;
1071 if (x_range <= INVPOS_X_MARGIN || y_range <= INVPOS_Y_MARGIN
1072 || (x_range * y_range) <= (INVPOS_DISTANCE * INVPOS_DISTANCE)) {
1073 debugpline2("inv_pos: maze is too small! (%d x %d)",
1074 x_maze_max, y_maze_max);
1076 inv_pos.x = inv_pos.y = 0; /*{occupied() => invocation_pos()}*/
1078 x = rn1(x_range, x_maze_min + INVPOS_X_MARGIN + 1);
1079 y = rn1(y_range, y_maze_min + INVPOS_Y_MARGIN + 1);
1080 /* we don't want it to be too near the stairs, nor
1081 to be on a spot that's already in use (wall|trap) */
1082 } while (x == xupstair || y == yupstair /*(direct line)*/
1083 || abs(x - xupstair) == abs(y - yupstair)
1084 || distmin(x, y, xupstair, yupstair) <= INVPOS_DISTANCE
1085 || !SPACE_POS(levl[x][y].typ) || occupied(x, y));
1088 maketrap(inv_pos.x, inv_pos.y, VIBRATING_SQUARE);
1089 #undef INVPOS_X_MARGIN
1090 #undef INVPOS_Y_MARGIN
1091 #undef INVPOS_DISTANCE
1096 /* place branch stair or portal */
1097 place_branch(Is_branchlev(&u.uz), 0, 0);
1099 for (x = rn1(8, 11); x; x--) {
1101 (void) mkobj_at(rn2(2) ? GEM_CLASS : 0, mm.x, mm.y, TRUE);
1103 for (x = rn1(10, 2); x; x--) {
1105 (void) mksobj_at(BOULDER, mm.x, mm.y, TRUE, FALSE);
1107 for (x = rn2(3); x; x--) {
1109 (void) makemon(&mons[PM_MINOTAUR], mm.x, mm.y, NO_MM_FLAGS);
1111 for (x = rn1(5, 7); x; x--) {
1113 (void) makemon((struct permonst *) 0, mm.x, mm.y, NO_MM_FLAGS);
1115 for (x = rn1(6, 7); x; x--) {
1117 (void) mkgold(0L, mm.x, mm.y);
1119 for (x = rn1(6, 7); x; x--)
1120 mktrap(0, 1, (struct mkroom *) 0, (coord *) 0);
1124 /* Make the mazewalk iterative by faking a stack. This is needed to
1125 * ensure the mazewalk is successful in the limited stack space of
1126 * the program. This iterative version uses the minimum amount of stack
1127 * that is totally safe.
1134 #define CELLS (ROWNO * COLNO) / 4 /* a maze cell is 4 squares */
1135 char mazex[CELLS + 1], mazey[CELLS + 1]; /* char's are OK */
1140 if (level.flags.corrmaze)
1147 mazex[pos] = (char) x;
1148 mazey[pos] = (char) y;
1150 x = (int) mazex[pos];
1151 y = (int) mazey[pos];
1152 if (!IS_DOOR(levl[x][y].typ)) {
1153 /* might still be on edge of MAP, so don't overwrite */
1154 levl[x][y].typ = typ;
1155 levl[x][y].flags = 0;
1158 for (a = 0; a < 4; a++)
1166 levl[x][y].typ = typ;
1170 panic("Overflow in walkfrom");
1171 mazex[pos] = (char) x;
1172 mazey[pos] = (char) y;
1187 if (level.flags.corrmaze)
1193 if (!IS_DOOR(levl[x][y].typ)) {
1194 /* might still be on edge of MAP, so don't overwrite */
1195 levl[x][y].typ = typ;
1196 levl[x][y].flags = 0;
1201 for (a = 0; a < 4; a++)
1208 levl[x][y].typ = typ;
1210 walkfrom(x, y, typ);
1215 /* find random point in generated corridors,
1216 so we don't create items in moats, bunkers, or walls */
1224 cc->x = 1 + rn2(x_maze_max);
1225 cc->y = 1 + rn2(y_maze_max);
1228 && levl[cc->x][cc->y].typ
1229 != (level.flags.corrmaze ? CORR : ROOM));
1234 for (x = 1; x < x_maze_max; x++)
1235 for (y = 1; y < y_maze_max; y++) {
1238 if (levl[cc->x][cc->y].typ
1239 == (level.flags.corrmaze ? CORR : ROOM))
1242 panic("mazexy: can't find a place!");
1247 /* put a non-diggable boundary around the initial portion of a level map.
1248 * assumes that no level will initially put things beyond the isok() range.
1250 * we can't bound unconditionally on the last line with something in it,
1251 * because that something might be a niche which was already reachable,
1252 * so the boundary would be breached
1254 * we can't bound unconditionally on one beyond the last line, because
1255 * that provides a window of abuse for wallified special levels
1263 boolean found, nonwall;
1264 int xmin, xmax, ymin, ymax;
1266 if (Is_earthlevel(&u.uz))
1267 return; /* everything diggable here */
1269 found = nonwall = FALSE;
1270 for (xmin = 0; !found && xmin <= COLNO; xmin++) {
1271 lev = &levl[xmin][0];
1272 for (y = 0; y <= ROWNO - 1; y++, lev++) {
1281 xmin -= (nonwall || !level.flags.is_maze_lev) ? 2 : 1;
1285 found = nonwall = FALSE;
1286 for (xmax = COLNO - 1; !found && xmax >= 0; xmax--) {
1287 lev = &levl[xmax][0];
1288 for (y = 0; y <= ROWNO - 1; y++, lev++) {
1297 xmax += (nonwall || !level.flags.is_maze_lev) ? 2 : 1;
1301 found = nonwall = FALSE;
1302 for (ymin = 0; !found && ymin <= ROWNO; ymin++) {
1303 lev = &levl[xmin][ymin];
1304 for (x = xmin; x <= xmax; x++, lev += ROWNO) {
1313 ymin -= (nonwall || !level.flags.is_maze_lev) ? 2 : 1;
1315 found = nonwall = FALSE;
1316 for (ymax = ROWNO - 1; !found && ymax >= 0; ymax--) {
1317 lev = &levl[xmin][ymax];
1318 for (x = xmin; x <= xmax; x++, lev += ROWNO) {
1327 ymax += (nonwall || !level.flags.is_maze_lev) ? 2 : 1;
1329 for (x = 0; x < COLNO; x++)
1330 for (y = 0; y < ROWNO; y++)
1331 if (y <= ymin || y >= ymax || x <= xmin || x >= xmax) {
1334 lev->wall_info |= W_NONDIGGABLE;
1336 levl[x][y].wall_info |= W_NONDIGGABLE;
1342 mkportal(x, y, todnum, todlevel)
1343 xchar x, y, todnum, todlevel;
1345 /* a portal "trap" must be matched by a
1346 portal in the destination dungeon/dlevel */
1347 struct trap *ttmp = maketrap(x, y, MAGIC_PORTAL);
1350 impossible("portal on top of portal??");
1353 debugpline4("mkportal: at <%d,%d>, to %s, level %d", x, y,
1354 dungeons[todnum].dname, todlevel);
1355 ttmp->dst.dnum = todnum;
1356 ttmp->dst.dlevel = todlevel;
1364 boolean snd = FALSE, loud = FALSE;
1366 for (n = rn2(3) + 2; n; n--) {
1367 xchar x = rn1(COLNO - 4, 3);
1368 xchar y = rn1(ROWNO - 4, 3);
1370 if (levl[x][y].typ == LAVAPOOL) {
1371 NhRegion *r = create_gas_cloud(x, y, 4 + rn2(5), rn1(10, 5));
1373 clear_heros_fault(r);
1375 if (distu(x, y) < 15)
1380 Norep("You hear a %swhoosh!", loud ? "loud " : "");
1384 * Special waterlevel stuff in endgame (TH).
1386 * Some of these functions would probably logically belong to some
1387 * other source files, but they are all so nicely encapsulated here.
1390 static struct bubble *bbubbles, *ebubbles;
1392 static struct trap *wportal;
1393 static int xmin, ymin, xmax, ymax; /* level boundaries */
1394 /* bubble movement boundaries */
1395 #define bxmin (xmin + 1)
1396 #define bymin (ymin + 1)
1397 #define bxmax (xmax - 1)
1398 #define bymax (ymax - 1)
1400 STATIC_DCL void NDECL(set_wportal);
1401 STATIC_DCL void FDECL(mk_bubble, (int, int, int));
1402 STATIC_DCL void FDECL(mv_bubble, (struct bubble *, int, int, BOOLEAN_P));
1411 static const struct rm water_pos = { cmap_to_glyph(S_water), WATER, 0, 0,
1413 static const struct rm air_pos = { cmap_to_glyph(S_cloud), AIR, 0, 0, 0,
1416 /* set up the portal the first time bubbles are moved */
1422 if (Is_waterlevel(&u.uz)) {
1423 /* keep attached ball&chain separate from bubble objects */
1428 * Pick up everything inside of a bubble then fill all bubble
1431 for (b = up ? bbubbles : ebubbles; b; b = up ? b->next : b->prev) {
1433 panic("movebubbles: cons != null");
1434 for (i = 0, x = b->x; i < (int) b->bm[0]; i++, x++)
1435 for (j = 0, y = b->y; j < (int) b->bm[1]; j++, y++)
1436 if (b->bm[j + 2] & (1 << i)) {
1438 impossible("movebubbles: bad pos (%d,%d)", x, y);
1442 /* pick up objects, monsters, hero, and traps */
1444 struct obj *olist = (struct obj *) 0, *otmp;
1445 struct container *cons =
1446 (struct container *) alloc(
1447 sizeof(struct container));
1449 while ((otmp = level.objects[x][y]) != 0) {
1450 remove_object(otmp);
1451 otmp->ox = otmp->oy = 0;
1452 otmp->nexthere = olist;
1458 cons->what = CONS_OBJ;
1459 cons->list = (genericptr_t) olist;
1460 cons->next = b->cons;
1464 struct monst *mon = m_at(x, y);
1465 struct container *cons =
1466 (struct container *) alloc(
1467 sizeof(struct container));
1471 cons->what = CONS_MON;
1472 cons->list = (genericptr_t) mon;
1474 cons->next = b->cons;
1480 remove_monster(x, y);
1482 newsym(x, y); /* clean up old position */
1483 mon->mx = mon->my = 0;
1485 if (!u.uswallow && x == u.ux && y == u.uy) {
1486 struct container *cons =
1487 (struct container *) alloc(
1488 sizeof(struct container));
1492 cons->what = CONS_HERO;
1493 cons->list = (genericptr_t) 0;
1495 cons->next = b->cons;
1498 if ((btrap = t_at(x, y)) != 0) {
1499 struct container *cons =
1500 (struct container *) alloc(
1501 sizeof(struct container));
1505 cons->what = CONS_TRAP;
1506 cons->list = (genericptr_t) btrap;
1508 cons->next = b->cons;
1512 levl[x][y] = water_pos;
1516 } else if (Is_airlevel(&u.uz)) {
1517 for (x = 0; x < COLNO; x++)
1518 for (y = 0; y < ROWNO; y++) {
1519 levl[x][y] = air_pos;
1520 unblock_point(x, y);
1525 * Every second time traverse down. This is because otherwise
1526 * all the junk that changes owners when bubbles overlap
1527 * would eventually end up in the last bubble in the chain.
1530 for (b = up ? bbubbles : ebubbles; b; b = up ? b->next : b->prev) {
1531 int rx = rn2(3), ry = rn2(3);
1533 mv_bubble(b, b->dx + 1 - (!b->dx ? rx : (rx ? 1 : 0)),
1534 b->dy + 1 - (!b->dy ? ry : (ry ? 1 : 0)), FALSE);
1537 /* put attached ball&chain back */
1538 if (Is_waterlevel(&u.uz) && Punished)
1540 vision_full_recalc = 1;
1543 /* when moving in water, possibly (1 in 3) alter the intended destination */
1548 boolean eff = FALSE;
1550 if (Swimming && rn2(4))
1551 return; /* natural swimmers have advantage */
1553 if (u.dx && !rn2(!u.dy ? 3 : 6)) { /* 1/3 chance or half that */
1554 /* cancel delta x and choose an arbitrary delta y value */
1557 dy = rn2(3) - 1; /* -1, 0, 1 */
1559 } while (dy && (!isok(x, y) || !is_pool(x, y)));
1563 } else if (u.dy && !rn2(!u.dx ? 3 : 5)) { /* 1/3 or 1/5*(5/6) */
1564 /* cancel delta y and choose an arbitrary delta x value */
1567 dx = rn2(3) - 1; /* -1 .. 1 */
1569 } while (dx && (!isok(x, y) || !is_pool(x, y)));
1576 pline("Water turbulence affects your movements.");
1578 pline("
\90\85\82Ì
\97¬
\82ê
\82ª
\82Â
\82È
\82½
\82Ì
\93®
\82«
\82É
\89e
\8b¿
\82ð
\97^
\82¦
\82½
\81D");
1582 save_waterlevel(fd, mode)
1587 if (!Is_waterlevel(&u.uz) && !Is_airlevel(&u.uz))
1590 if (perform_bwrite(mode)) {
1592 for (b = bbubbles; b; b = b->next)
1594 bwrite(fd, (genericptr_t) &n, sizeof(int));
1595 bwrite(fd, (genericptr_t) &xmin, sizeof(int));
1596 bwrite(fd, (genericptr_t) &ymin, sizeof(int));
1597 bwrite(fd, (genericptr_t) &xmax, sizeof(int));
1598 bwrite(fd, (genericptr_t) &ymax, sizeof(int));
1599 for (b = bbubbles; b; b = b->next)
1600 bwrite(fd, (genericptr_t) b, sizeof(struct bubble));
1602 if (release_data(mode))
1603 unsetup_waterlevel();
1607 restore_waterlevel(fd)
1610 struct bubble *b = (struct bubble *) 0, *btmp;
1613 if (!Is_waterlevel(&u.uz) && !Is_airlevel(&u.uz))
1617 mread(fd, (genericptr_t) &n, sizeof(int));
1618 mread(fd, (genericptr_t) &xmin, sizeof(int));
1619 mread(fd, (genericptr_t) &ymin, sizeof(int));
1620 mread(fd, (genericptr_t) &xmax, sizeof(int));
1621 mread(fd, (genericptr_t) &ymax, sizeof(int));
1622 for (i = 0; i < n; i++) {
1624 b = (struct bubble *) alloc(sizeof(struct bubble));
1625 mread(fd, (genericptr_t) b, sizeof(struct bubble));
1631 b->prev = (struct bubble *) 0;
1633 mv_bubble(b, 0, 0, TRUE);
1636 b->next = (struct bubble *) 0;
1637 was_waterlevel = TRUE;
1641 waterbody_name(x, y)
1648 return "drink"; /* should never happen */
1651 if (ltyp == DRAWBRIDGE_UP)
1652 ltyp = db_under_typ(lev->drawbridgemask);
1654 if (ltyp == LAVAPOOL)
1656 return hliquid("lava");
1658 return hliquid("
\97n
\8aâ");
1659 else if (ltyp == ICE)
1664 else if (ltyp == POOL)
1666 return "pool of water";
1668 return "
\90\85\82½
\82Ü
\82è";
1669 else if (ltyp == WATER || Is_waterlevel(&u.uz))
1670 ; /* fall through to default return value */
1671 else if (Is_juiblex_level(&u.uz))
1676 else if (ltyp == MOAT && !Is_medusa_level(&u.uz))
1683 return hliquid("water");
1685 return hliquid("
\90\85\92\86");
1691 /* there better be only one magic portal on water level... */
1692 for (wportal = ftrap; wportal; wportal = wportal->ntrap)
1693 if (wportal->ttyp == MAGIC_PORTAL)
1695 impossible("set_wportal(): no portal!");
1703 int water_glyph = cmap_to_glyph(S_water),
1704 air_glyph = cmap_to_glyph(S_air);
1706 /* ouch, hardcoded... */
1713 /* set hero's memory to water */
1715 for (x = xmin; x <= xmax; x++)
1716 for (y = ymin; y <= ymax; y++)
1717 levl[x][y].glyph = Is_waterlevel(&u.uz) ? water_glyph : air_glyph;
1721 if (Is_waterlevel(&u.uz)) {
1722 xskip = 10 + rn2(10);
1729 for (x = bxmin; x <= bxmax; x += xskip)
1730 for (y = bymin; y <= bymax; y += yskip)
1731 mk_bubble(x, y, rn2(7));
1735 unsetup_waterlevel()
1737 struct bubble *b, *bb;
1741 for (b = bbubbles; b; b = bb) {
1743 free((genericptr_t) b);
1745 bbubbles = ebubbles = (struct bubble *) 0;
1753 * These bit masks make visually pleasing bubbles on a normal aspect
1754 * 25x80 terminal, which naturally results in them being mathematically
1755 * anything but symmetric. For this reason they cannot be computed
1756 * in situ, either. The first two elements tell the dimensions of
1757 * the bubble's bounding box.
1759 static uchar bm2[] = { 2, 1, 0x3 },
1760 bm3[] = { 3, 2, 0x7, 0x7 },
1761 bm4[] = { 4, 3, 0x6, 0xf, 0x6 },
1762 bm5[] = { 5, 3, 0xe, 0x1f, 0xe },
1763 bm6[] = { 6, 4, 0x1e, 0x3f, 0x3f, 0x1e },
1764 bm7[] = { 7, 4, 0x3e, 0x7f, 0x7f, 0x3e },
1765 bm8[] = { 8, 4, 0x7e, 0xff, 0xff, 0x7e },
1766 *bmask[] = { bm2, bm3, bm4, bm5, bm6, bm7, bm8 };
1769 if (x >= bxmax || y >= bymax)
1771 if (n >= SIZE(bmask)) {
1772 impossible("n too large (mk_bubble)");
1773 n = SIZE(bmask) - 1;
1775 if (bmask[n][1] > MAX_BMASK) {
1776 panic("bmask size is larger than MAX_BMASK");
1778 b = (struct bubble *) alloc(sizeof(struct bubble));
1779 if ((x + (int) bmask[n][0] - 1) > bxmax)
1780 x = bxmax - bmask[n][0] + 1;
1781 if ((y + (int) bmask[n][1] - 1) > bymax)
1782 y = bymax - bmask[n][1] + 1;
1787 /* y dimension is the length of bitmap data - see bmask above */
1788 (void) memcpy((genericptr_t) b->bm, (genericptr_t) bmask[n],
1789 (bmask[n][1] + 2) * sizeof(b->bm[0]));
1797 b->prev = (struct bubble *) 0;
1798 b->next = (struct bubble *) 0;
1800 mv_bubble(b, 0, 0, TRUE);
1804 * The player, the portal and all other objects and monsters
1805 * float along with their associated bubbles. Bubbles may overlap
1806 * freely, and the contents may get associated with other bubbles in
1807 * the process. Bubbles are "sticky", meaning that if the player is
1808 * in the immediate neighborhood of one, he/she may get sucked inside.
1809 * This property also makes leaving a bubble slightly difficult.
1812 mv_bubble(b, dx, dy, ini)
1817 int x, y, i, j, colli = 0;
1818 struct container *cons, *ctemp;
1820 /* clouds move slowly */
1821 if (!Is_airlevel(&u.uz) || !rn2(6)) {
1823 if (dx < -1 || dx > 1 || dy < -1 || dy > 1) {
1824 /* pline("mv_bubble: dx = %d, dy = %d", dx, dy); */
1830 * collision with level borders?
1831 * 1 = horizontal border, 2 = vertical, 3 = corner
1837 if ((int) (b->x + b->bm[0] - 1) >= bxmax)
1839 if ((int) (b->y + b->bm[1] - 1) >= bymax)
1843 pline("bubble xmin: x = %d, xmin = %d", b->x, bxmin);
1847 pline("bubble ymin: y = %d, ymin = %d", b->y, bymin);
1850 if ((int) (b->x + b->bm[0] - 1) > bxmax) {
1851 pline("bubble xmax: x = %d, xmax = %d", b->x + b->bm[0] - 1,
1853 b->x = bxmax - b->bm[0] + 1;
1855 if ((int) (b->y + b->bm[1] - 1) > bymax) {
1856 pline("bubble ymax: y = %d, ymax = %d", b->y + b->bm[1] - 1,
1858 b->y = bymax - b->bm[1] + 1;
1861 /* bounce if we're trying to move off the border */
1862 if (b->x == bxmin && dx < 0)
1864 if (b->x + b->bm[0] - 1 == bxmax && dx > 0)
1866 if (b->y == bymin && dy < 0)
1868 if (b->y + b->bm[1] - 1 == bymax && dy > 0)
1875 /* draw the bubbles */
1876 for (i = 0, x = b->x; i < (int) b->bm[0]; i++, x++)
1877 for (j = 0, y = b->y; j < (int) b->bm[1]; j++, y++)
1878 if (b->bm[j + 2] & (1 << i)) {
1879 if (Is_waterlevel(&u.uz)) {
1880 levl[x][y].typ = AIR;
1882 unblock_point(x, y);
1883 } else if (Is_airlevel(&u.uz)) {
1884 levl[x][y].typ = CLOUD;
1890 if (Is_waterlevel(&u.uz)) {
1891 /* replace contents of bubble */
1892 for (cons = b->cons; cons; cons = ctemp) {
1897 switch (cons->what) {
1899 struct obj *olist, *otmp;
1901 for (olist = (struct obj *) cons->list; olist; olist = otmp) {
1902 otmp = olist->nexthere;
1903 place_object(olist, cons->x, cons->y);
1909 struct monst *mon = (struct monst *) cons->list;
1911 (void) mnearto(mon, cons->x, cons->y, TRUE);
1916 int ux0 = u.ux, uy0 = u.uy;
1918 /* change u.ux0 and u.uy0? */
1921 newsym(ux0, uy0); /* clean up old position */
1923 if (MON_AT(cons->x, cons->y)) {
1924 mnexto(m_at(cons->x, cons->y));
1930 struct trap *btrap = (struct trap *) cons->list;
1931 btrap->tx = cons->x;
1932 btrap->ty = cons->y;
1937 impossible("mv_bubble: unknown bubble contents");
1940 free((genericptr_t) cons);
1951 b->dy = -b->dy; /* fall through */
1956 /* sometimes alter direction for fun anyway
1957 (higher probability for stationary bubbles) */
1958 if (!ini && ((b->dx || b->dy) ? !rn2(20) : !rn2(5))) {