2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)level.c 8.1 (Berkeley) 5/31/93
33 * $FreeBSD: src/games/rogue/level.c,v 1.3 1999/11/30 03:49:23 billf Exp $
34 * $DragonFly: src/games/rogue/level.c,v 1.4 2006/09/02 19:31:07 pavalos Exp $
40 * This source herein may be modified and/or distributed by anybody who
41 * so desires, with the following restrictions:
42 * 1.) No portion of this notice shall be removed.
43 * 2.) Credit shall not be taken for the creation of this source.
44 * 3.) This code is not to be traded, sold, or used for personal
51 #define swap(x,y) {t = x; x = y; y = t;}
53 static void add_mazes(void);
54 static boolean connect_rooms(short, short);
55 static void draw_simple_passage(short, short, short, short, short);
56 static void fill_it(int, boolean);
57 static void fill_out_level(void);
58 static short get_exp_level(long);
59 static void hide_boxed_passage(short, short, short, short, short);
60 static void make_maze(short, short, short, short, short, short);
61 static void make_room(short, short, short, short);
62 static boolean mask_room(short, short *, short *, unsigned short);
63 static void mix_random_rooms(void);
64 static void put_door(room *, short, short *, short *);
65 static void recursive_deadend(short, const short *, short, short);
66 static boolean same_col(short, short);
67 static boolean same_row(short, short);
72 const char *new_level_message = NULL;
73 short party_room = NO_ROOM;
76 const long level_points[MAX_EXP_LEVEL] = {
100 short random_rooms[MAXROOMS] = {3, 7, 5, 2, 0, 6, 1, 4, 8};
102 extern boolean being_held, wizard, detect_monster;
103 extern boolean see_invisible;
104 extern short bear_trap, levitate, extra_hp, less_hp;
110 short must_1, must_2 = 0, must_3 = 0;
113 if (cur_level < LAST_DUNGEON) {
116 if (cur_level > max_level) {
117 max_level = cur_level;
119 must_1 = get_rand(0, 5);
153 if (rand_percent(8)) {
156 big_room = ((party_room != NO_ROOM) && rand_percent(1));
158 make_room(BIG_ROOM, 0, 0, 0);
160 for (i = 0; i < MAXROOMS; i++) {
161 make_room(i, must_1, must_2, must_3);
169 for (j = 0; j < MAXROOMS; j++) {
173 if (i < (MAXROOMS-1)) {
174 connect_rooms(i, i+1);
176 if (i < (MAXROOMS-3)) {
177 connect_rooms(i, i+3);
179 if (i < (MAXROOMS-2)) {
180 if (rooms[i+1].is_room & R_NOTHING) {
181 if (connect_rooms(i, i+2)) {
182 rooms[i+1].is_room = R_CROSS;
186 if (i < (MAXROOMS-6)) {
187 if (rooms[i+3].is_room & R_NOTHING) {
188 if (connect_rooms(i, i+6)) {
189 rooms[i+3].is_room = R_CROSS;
193 if (is_all_connected()) {
199 if (!has_amulet() && (cur_level >= AMULET_LEVEL)) {
205 make_room(short rn, short r1, short r2, short r3)
207 short left_col, right_col, top_row, bottom_row;
209 short row_offset, col_offset;
212 left_col = right_col = top_row = bottom_row = 0;
255 bottom_row = DROWS - 2;
261 bottom_row = DROWS - 2;
267 bottom_row = DROWS - 2;
270 top_row = get_rand(MIN_ROW, MIN_ROW+5);
271 bottom_row = get_rand(DROWS-7, DROWS-2);
272 left_col = get_rand(0, 10);
273 right_col = get_rand(DCOLS-11, DCOLS-1);
277 height = get_rand(4, (bottom_row - top_row + 1));
278 width = get_rand(7, (right_col - left_col - 2));
280 row_offset = get_rand(0, ((bottom_row - top_row) - height + 1));
281 col_offset = get_rand(0, ((right_col - left_col) - width + 1));
283 top_row += row_offset;
284 bottom_row = top_row + height - 1;
286 left_col += col_offset;
287 right_col = left_col + width - 1;
289 if ((rn != r1) && (rn != r2) && (rn != r3) && rand_percent(40)) {
293 rooms[rn].is_room = R_ROOM;
295 for (i = top_row; i <= bottom_row; i++) {
296 for (j = left_col; j <= right_col; j++) {
297 if ((i == top_row) || (i == bottom_row)) {
299 } else if ( ((i != top_row) && (i != bottom_row)) &&
300 ((j == left_col) || (j == right_col))) {
309 rooms[rn].top_row = top_row;
310 rooms[rn].bottom_row = bottom_row;
311 rooms[rn].left_col = left_col;
312 rooms[rn].right_col = right_col;
316 connect_rooms(short room1, short room2)
318 short row1, col1, row2, col2, dir;
320 if ((!(rooms[room1].is_room & (R_ROOM | R_MAZE))) ||
321 (!(rooms[room2].is_room & (R_ROOM | R_MAZE)))) {
324 if (same_row(room1, room2) &&
325 (rooms[room1].left_col > rooms[room2].right_col)) {
326 put_door(&rooms[room1], LEFT, &row1, &col1);
327 put_door(&rooms[room2], RIGHT, &row2, &col2);
329 } else if (same_row(room1, room2) &&
330 (rooms[room2].left_col > rooms[room1].right_col)) {
331 put_door(&rooms[room1], RIGHT, &row1, &col1);
332 put_door(&rooms[room2], LEFT, &row2, &col2);
334 } else if (same_col(room1, room2) &&
335 (rooms[room1].top_row > rooms[room2].bottom_row)) {
336 put_door(&rooms[room1], UPWARD, &row1, &col1);
337 put_door(&rooms[room2], DOWN, &row2, &col2);
339 } else if (same_col(room1, room2) &&
340 (rooms[room2].top_row > rooms[room1].bottom_row)) {
341 put_door(&rooms[room1], DOWN, &row1, &col1);
342 put_door(&rooms[room2], UPWARD, &row2, &col2);
349 draw_simple_passage(row1, col1, row2, col2, dir);
350 } while (rand_percent(4));
352 rooms[room1].doors[dir/2].oth_room = room2;
353 rooms[room1].doors[dir/2].oth_row = row2;
354 rooms[room1].doors[dir/2].oth_col = col2;
356 rooms[room2].doors[(((dir+4)%DIRS)/2)].oth_room = room1;
357 rooms[room2].doors[(((dir+4)%DIRS)/2)].oth_row = row1;
358 rooms[room2].doors[(((dir+4)%DIRS)/2)].oth_col = col1;
367 for (i = 0; i < MAXROOMS; i++) {
368 rooms[i].is_room = R_NOTHING;
369 for (j = 0; j < 4; j++) {
370 rooms[i].doors[j].oth_room = NO_ROOM;
374 for (i = 0; i < MAX_TRAPS; i++) {
375 traps[i].trap_type = NO_TRAP;
377 for (i = 0; i < DROWS; i++) {
378 for (j = 0; j < DCOLS; j++) {
379 dungeon[i][j] = NOTHING;
382 detect_monster = see_invisible = 0;
383 bear_trap = being_held = 0;
384 party_room = NO_ROOM;
385 rogue.row = rogue.col = -1;
390 put_door(room *rm, short dir, short *row, short *col)
394 wall_width = (rm->is_room & R_MAZE) ? 0 : 1;
399 *row = ((dir == UPWARD) ? rm->top_row : rm->bottom_row);
401 *col = get_rand(rm->left_col+wall_width,
402 rm->right_col-wall_width);
403 } while (!(dungeon[*row][*col] & (HORWALL | TUNNEL)));
407 *col = (dir == LEFT) ? rm->left_col : rm->right_col;
409 *row = get_rand(rm->top_row+wall_width,
410 rm->bottom_row-wall_width);
411 } while (!(dungeon[*row][*col] & (VERTWALL | TUNNEL)));
414 if (rm->is_room & R_ROOM) {
415 dungeon[*row][*col] = DOOR;
417 if ((cur_level > 2) && rand_percent(HIDE_PERCENT)) {
418 dungeon[*row][*col] |= HIDDEN;
420 rm->doors[dir/2].door_row = *row;
421 rm->doors[dir/2].door_col = *col;
425 draw_simple_passage(short row1, short col1, short row2, short col2, short dir)
429 if ((dir == LEFT) || (dir == RIGHT)) {
434 middle = get_rand(col1+1, col2-1);
435 for (i = col1+1; i != middle; i++) {
436 dungeon[row1][i] = TUNNEL;
438 for (i = row1; i != row2; i += (row1 > row2) ? -1 : 1) {
439 dungeon[i][middle] = TUNNEL;
441 for (i = middle; i != col2; i++) {
442 dungeon[row2][i] = TUNNEL;
449 middle = get_rand(row1+1, row2-1);
450 for (i = row1+1; i != middle; i++) {
451 dungeon[i][col1] = TUNNEL;
453 for (i = col1; i != col2; i += (col1 > col2) ? -1 : 1) {
454 dungeon[middle][i] = TUNNEL;
456 for (i = middle; i != row2; i++) {
457 dungeon[i][col2] = TUNNEL;
460 if (rand_percent(HIDE_PERCENT)) {
461 hide_boxed_passage(row1, col1, row2, col2, 1);
466 same_row(short room1, short room2)
468 return((room1 / 3) == (room2 / 3));
472 same_col(short room1, short room2)
474 return((room1 % 3) == (room2 % 3));
485 start = get_rand(0, (MAXROOMS-1));
486 maze_percent = (cur_level * 5) / 4;
488 if (cur_level > 15) {
489 maze_percent += cur_level;
491 for (i = 0; i < MAXROOMS; i++) {
492 j = ((start + i) % MAXROOMS);
493 if (rooms[j].is_room & R_NOTHING) {
494 if (rand_percent(maze_percent)) {
495 rooms[j].is_room = R_MAZE;
496 make_maze(get_rand(rooms[j].top_row+1, rooms[j].bottom_row-1),
497 get_rand(rooms[j].left_col+1, rooms[j].right_col-1),
498 rooms[j].top_row, rooms[j].bottom_row,
499 rooms[j].left_col, rooms[j].right_col);
500 hide_boxed_passage(rooms[j].top_row, rooms[j].left_col,
501 rooms[j].bottom_row, rooms[j].right_col,
518 for (i = 0; i < MAXROOMS; i++) {
519 rn = random_rooms[i];
520 if ((rooms[rn].is_room & R_NOTHING) ||
521 ((rooms[rn].is_room & R_CROSS) && coin_toss())) {
525 if (r_de != NO_ROOM) {
531 fill_it(int rn, boolean do_rec_de)
533 short i, tunnel_dir, door_dir, drow, dcol;
534 short target_room, rooms_found = 0;
536 static short offsets[4] = {-1, 1, 3, -3};
537 boolean did_this = 0;
539 for (i = 0; i < 10; i++) {
540 srow = get_rand(0, 3);
541 scol = get_rand(0, 3);
543 offsets[srow] = offsets[scol];
546 for (i = 0; i < 4; i++) {
548 target_room = rn + offsets[i];
550 if (((target_room < 0) || (target_room >= MAXROOMS)) ||
551 (!(same_row(rn,target_room) || same_col(rn,target_room))) ||
552 (!(rooms[target_room].is_room & (R_ROOM | R_MAZE)))) {
555 if (same_row(rn, target_room)) {
556 tunnel_dir = (rooms[rn].left_col < rooms[target_room].left_col) ?
559 tunnel_dir = (rooms[rn].top_row < rooms[target_room].top_row) ?
562 door_dir = ((tunnel_dir + 4) % DIRS);
563 if (rooms[target_room].doors[door_dir/2].oth_room != NO_ROOM) {
566 if (((!do_rec_de) || did_this) ||
567 (!mask_room(rn, &srow, &scol, TUNNEL))) {
568 srow = (rooms[rn].top_row + rooms[rn].bottom_row) / 2;
569 scol = (rooms[rn].left_col + rooms[rn].right_col) / 2;
571 put_door(&rooms[target_room], door_dir, &drow, &dcol);
573 draw_simple_passage(srow, scol, drow, dcol, tunnel_dir);
574 rooms[rn].is_room = R_DEADEND;
575 dungeon[srow][scol] = TUNNEL;
577 if ((i < 3) && (!did_this)) {
583 if ((rooms_found < 2) && do_rec_de) {
584 recursive_deadend(rn, offsets, srow, scol);
591 recursive_deadend(short rn, const short *offsets, short srow, short scol)
594 short drow, dcol, tunnel_dir;
596 rooms[rn].is_room = R_DEADEND;
597 dungeon[srow][scol] = TUNNEL;
599 for (i = 0; i < 4; i++) {
600 de = rn + offsets[i];
601 if (((de < 0) || (de >= MAXROOMS)) ||
602 (!(same_row(rn, de) || same_col(rn, de)))) {
605 if (!(rooms[de].is_room & R_NOTHING)) {
608 drow = (rooms[de].top_row + rooms[de].bottom_row) / 2;
609 dcol = (rooms[de].left_col + rooms[de].right_col) / 2;
610 if (same_row(rn, de)) {
611 tunnel_dir = (rooms[rn].left_col < rooms[de].left_col) ?
614 tunnel_dir = (rooms[rn].top_row < rooms[de].top_row) ?
617 draw_simple_passage(srow, scol, drow, dcol, tunnel_dir);
619 recursive_deadend(de, offsets, drow, dcol);
624 mask_room(short rn, short *row, short *col, unsigned short mask)
628 for (i = rooms[rn].top_row; i <= rooms[rn].bottom_row; i++) {
629 for (j = rooms[rn].left_col; j <= rooms[rn].right_col; j++) {
630 if (dungeon[i][j] & mask) {
641 make_maze(short r, short c, short tr, short br, short lc, short rc)
651 dungeon[r][c] = TUNNEL;
653 if (rand_percent(20)) {
654 for (i = 0; i < 10; i++) {
660 swap(dirs[t1], dirs[t2]);
663 for (i = 0; i < 4; i++) {
667 (dungeon[r-1][c] != TUNNEL) &&
668 (dungeon[r-1][c-1] != TUNNEL) &&
669 (dungeon[r-1][c+1] != TUNNEL) &&
670 (dungeon[r-2][c] != TUNNEL)) {
671 make_maze((r-1), c, tr, br, lc, rc);
676 (dungeon[r+1][c] != TUNNEL) &&
677 (dungeon[r+1][c-1] != TUNNEL) &&
678 (dungeon[r+1][c+1] != TUNNEL) &&
679 (dungeon[r+2][c] != TUNNEL)) {
680 make_maze((r+1), c, tr, br, lc, rc);
685 (dungeon[r][c-1] != TUNNEL) &&
686 (dungeon[r-1][c-1] != TUNNEL) &&
687 (dungeon[r+1][c-1] != TUNNEL) &&
688 (dungeon[r][c-2] != TUNNEL)) {
689 make_maze(r, (c-1), tr, br, lc, rc);
694 (dungeon[r][c+1] != TUNNEL) &&
695 (dungeon[r-1][c+1] != TUNNEL) &&
696 (dungeon[r+1][c+1] != TUNNEL) &&
697 (dungeon[r][c+2] != TUNNEL)) {
698 make_maze(r, (c+1), tr, br, lc, rc);
706 hide_boxed_passage(short row1, short col1, short row2, short col2, short n)
709 short row, col, row_cut, col_cut;
722 if ((w >= 5) || (h >= 5)) {
723 row_cut = ((h >= 2) ? 1 : 0);
724 col_cut = ((w >= 2) ? 1 : 0);
726 for (i = 0; i < n; i++) {
727 for (j = 0; j < 10; j++) {
728 row = get_rand(row1 + row_cut, row2 - row_cut);
729 col = get_rand(col1 + col_cut, col2 - col_cut);
730 if (dungeon[row][col] == TUNNEL) {
731 dungeon[row][col] |= HIDDEN;
741 put_player(short nr) /* try not to put in this room */
743 short rn = nr, misses;
746 for (misses = 0; ((misses < 2) && (rn == nr)); misses++) {
747 gr_row_col(&row, &col, (FLOOR | TUNNEL | OBJECT | STAIRS));
748 rn = get_room_number(row, col);
753 if (dungeon[rogue.row][rogue.col] & TUNNEL) {
758 if (cur_room != PASSAGE) {
759 light_up_room(cur_room);
761 light_passage(rogue.row, rogue.col);
763 rn = get_room_number(rogue.row, rogue.col);
764 wake_room(rn, 1, rogue.row, rogue.col);
765 if (new_level_message) {
766 message(new_level_message, 0);
767 new_level_message = NULL;
769 mvaddch(rogue.row, rogue.col, rogue.fchar);
778 if (dungeon[rogue.row][rogue.col] & STAIRS) {
780 message("you're floating in the air!", 0);
785 message("I see no way down", 0);
793 if (!(dungeon[rogue.row][rogue.col] & STAIRS)) {
794 message("I see no way up", 0);
798 message("your way is magically blocked", 0);
802 new_level_message = "you feel a wrenching sensation in your gut";
803 if (cur_level == 1) {
813 add_exp(int e, boolean promotion)
819 rogue.exp_points += e;
821 if (rogue.exp_points >= level_points[rogue.exp-1]) {
822 new_exp = get_exp_level(rogue.exp_points);
823 if (rogue.exp_points > MAX_EXP) {
824 rogue.exp_points = MAX_EXP + 1;
826 for (i = rogue.exp+1; i <= new_exp; i++) {
827 sprintf(mbuf, "welcome to level %d", i);
831 rogue.hp_current += hp;
835 print_stats(STAT_HP | STAT_EXP);
838 print_stats(STAT_EXP);
843 get_exp_level(long e)
847 for (i = 0; i < (MAX_EXP_LEVEL - 1); i++) {
848 if (level_points[i] > e) {
860 hp = (wizard ? 10 : get_rand(3, 10));
865 show_average_hp(void)
869 float effective_average;
871 if (rogue.exp == 1) {
872 real_average = effective_average = 0.00;
874 real_average = (float)
875 ((rogue.hp_max - extra_hp - INIT_HP) + less_hp) / (rogue.exp - 1);
876 effective_average = (float)(rogue.hp_max - INIT_HP) / (rogue.exp - 1);
879 sprintf(mbuf, "R-Hp: %.2f, E-Hp: %.2f (!: %d, V: %d)", real_average,
880 effective_average, extra_hp, less_hp);
885 mix_random_rooms(void)
890 for (i = 0; i < (3 * MAXROOMS); i++) {
892 x = get_rand(0, (MAXROOMS-1));
893 y = get_rand(0, (MAXROOMS-1));
895 swap(random_rooms[x], random_rooms[y]);