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 $
39 * This source herein may be modified and/or distributed by anybody who
40 * so desires, with the following restrictions:
41 * 1.) No portion of this notice shall be removed.
42 * 2.) Credit shall not be taken for the creation of this source.
43 * 3.) This code is not to be traded, sold, or used for personal
50 #define SWAP(x,y) {t = (x); (x) = (y); (y) = t;}
52 static void add_mazes(void);
53 static boolean
connect_rooms(short, short);
54 static void draw_simple_passage(short, short, short, short, short);
55 static void fill_it(int, boolean
);
56 static void fill_out_level(void);
57 static short get_exp_level(long);
58 static void hide_boxed_passage(short, short, short, short, short);
59 static void make_maze(short, short, short, short, short, short);
60 static void make_room(short, short, short, short);
61 static boolean
mask_room(short, short *, short *, unsigned short);
62 static void mix_random_rooms(void);
63 static void put_door(room
*, short, short *, short *);
64 static void recursive_deadend(short, const short *, short, short);
65 static boolean
same_col(short, short);
66 static boolean
same_row(short, short);
71 const char *new_level_message
= NULL
;
72 short party_room
= NO_ROOM
;
76 const long level_points
[MAX_EXP_LEVEL
] = {
100 static short random_rooms
[MAXROOMS
] = {3, 7, 5, 2, 0, 6, 1, 4, 8};
106 short must_1
, must_2
, must_3
;
110 if (cur_level
< LAST_DUNGEON
) {
113 if (cur_level
> max_level
) {
114 max_level
= cur_level
;
116 must_1
= get_rand(0, 5);
150 if (rand_percent(8)) {
153 big_room
= ((party_room
!= NO_ROOM
) && rand_percent(1));
155 make_room(BIG_ROOM
, 0, 0, 0);
157 for (i
= 0; i
< MAXROOMS
; i
++) {
158 make_room(i
, must_1
, must_2
, must_3
);
166 for (j
= 0; j
< MAXROOMS
; j
++) {
170 if (i
< (MAXROOMS
-1)) {
171 connect_rooms(i
, i
+1);
173 if (i
< (MAXROOMS
-3)) {
174 connect_rooms(i
, i
+3);
176 if (i
< (MAXROOMS
-2)) {
177 if (rooms
[i
+1].is_room
& R_NOTHING
) {
178 if (connect_rooms(i
, i
+2)) {
179 rooms
[i
+1].is_room
= R_CROSS
;
183 if (i
< (MAXROOMS
-6)) {
184 if (rooms
[i
+3].is_room
& R_NOTHING
) {
185 if (connect_rooms(i
, i
+6)) {
186 rooms
[i
+3].is_room
= R_CROSS
;
190 if (is_all_connected()) {
196 if (!has_amulet() && (cur_level
>= AMULET_LEVEL
)) {
202 make_room(short rn
, short r1
, short r2
, short r3
)
204 short left_col
, right_col
, top_row
, bottom_row
;
206 short row_offset
, col_offset
;
209 left_col
= right_col
= top_row
= bottom_row
= 0;
252 bottom_row
= DROWS
- 2;
258 bottom_row
= DROWS
- 2;
264 bottom_row
= DROWS
- 2;
267 top_row
= get_rand(MIN_ROW
, MIN_ROW
+5);
268 bottom_row
= get_rand(DROWS
-7, DROWS
-2);
269 left_col
= get_rand(0, 10);
270 right_col
= get_rand(DCOLS
-11, DCOLS
-1);
274 height
= get_rand(4, (bottom_row
- top_row
+ 1));
275 width
= get_rand(7, (right_col
- left_col
- 2));
277 row_offset
= get_rand(0, ((bottom_row
- top_row
) - height
+ 1));
278 col_offset
= get_rand(0, ((right_col
- left_col
) - width
+ 1));
280 top_row
+= row_offset
;
281 bottom_row
= top_row
+ height
- 1;
283 left_col
+= col_offset
;
284 right_col
= left_col
+ width
- 1;
286 if ((rn
!= r1
) && (rn
!= r2
) && (rn
!= r3
) && rand_percent(40)) {
290 rooms
[rn
].is_room
= R_ROOM
;
292 for (i
= top_row
; i
<= bottom_row
; i
++) {
293 for (j
= left_col
; j
<= right_col
; j
++) {
294 if ((i
== top_row
) || (i
== bottom_row
)) {
296 } else if ( ((i
!= top_row
) && (i
!= bottom_row
)) &&
297 ((j
== left_col
) || (j
== right_col
))) {
306 rooms
[rn
].top_row
= top_row
;
307 rooms
[rn
].bottom_row
= bottom_row
;
308 rooms
[rn
].left_col
= left_col
;
309 rooms
[rn
].right_col
= right_col
;
313 connect_rooms(short room1
, short room2
)
315 short row1
, col1
, row2
, col2
, dir
;
317 if ((!(rooms
[room1
].is_room
& (R_ROOM
| R_MAZE
))) ||
318 (!(rooms
[room2
].is_room
& (R_ROOM
| R_MAZE
)))) {
321 if (same_row(room1
, room2
) &&
322 (rooms
[room1
].left_col
> rooms
[room2
].right_col
)) {
323 put_door(&rooms
[room1
], LEFT
, &row1
, &col1
);
324 put_door(&rooms
[room2
], RIGHT
, &row2
, &col2
);
326 } else if (same_row(room1
, room2
) &&
327 (rooms
[room2
].left_col
> rooms
[room1
].right_col
)) {
328 put_door(&rooms
[room1
], RIGHT
, &row1
, &col1
);
329 put_door(&rooms
[room2
], LEFT
, &row2
, &col2
);
331 } else if (same_col(room1
, room2
) &&
332 (rooms
[room1
].top_row
> rooms
[room2
].bottom_row
)) {
333 put_door(&rooms
[room1
], UPWARD
, &row1
, &col1
);
334 put_door(&rooms
[room2
], DOWN
, &row2
, &col2
);
336 } else if (same_col(room1
, room2
) &&
337 (rooms
[room2
].top_row
> rooms
[room1
].bottom_row
)) {
338 put_door(&rooms
[room1
], DOWN
, &row1
, &col1
);
339 put_door(&rooms
[room2
], UPWARD
, &row2
, &col2
);
346 draw_simple_passage(row1
, col1
, row2
, col2
, dir
);
347 } while (rand_percent(4));
349 rooms
[room1
].doors
[dir
/2].oth_room
= room2
;
350 rooms
[room1
].doors
[dir
/2].oth_row
= row2
;
351 rooms
[room1
].doors
[dir
/2].oth_col
= col2
;
353 rooms
[room2
].doors
[(((dir
+4)%DIRS
)/2)].oth_room
= room1
;
354 rooms
[room2
].doors
[(((dir
+4)%DIRS
)/2)].oth_row
= row1
;
355 rooms
[room2
].doors
[(((dir
+4)%DIRS
)/2)].oth_col
= col1
;
364 for (i
= 0; i
< MAXROOMS
; i
++) {
365 rooms
[i
].is_room
= R_NOTHING
;
366 for (j
= 0; j
< 4; j
++) {
367 rooms
[i
].doors
[j
].oth_room
= NO_ROOM
;
371 for (i
= 0; i
< MAX_TRAPS
; i
++) {
372 traps
[i
].trap_type
= NO_TRAP
;
374 for (i
= 0; i
< DROWS
; i
++) {
375 for (j
= 0; j
< DCOLS
; j
++) {
376 dungeon
[i
][j
] = NOTHING
;
379 detect_monster
= see_invisible
= 0;
380 bear_trap
= being_held
= 0;
381 party_room
= NO_ROOM
;
382 rogue
.row
= rogue
.col
= -1;
387 put_door(room
*rm
, short dir
, short *row
, short *col
)
391 wall_width
= (rm
->is_room
& R_MAZE
) ? 0 : 1;
396 *row
= ((dir
== UPWARD
) ? rm
->top_row
: rm
->bottom_row
);
398 *col
= get_rand(rm
->left_col
+wall_width
,
399 rm
->right_col
-wall_width
);
400 } while (!(dungeon
[*row
][*col
] & (HORWALL
| TUNNEL
)));
404 *col
= (dir
== LEFT
) ? rm
->left_col
: rm
->right_col
;
406 *row
= get_rand(rm
->top_row
+wall_width
,
407 rm
->bottom_row
-wall_width
);
408 } while (!(dungeon
[*row
][*col
] & (VERTWALL
| TUNNEL
)));
411 if (rm
->is_room
& R_ROOM
) {
412 dungeon
[*row
][*col
] = DOOR
;
414 if ((cur_level
> 2) && rand_percent(HIDE_PERCENT
)) {
415 dungeon
[*row
][*col
] |= HIDDEN
;
417 rm
->doors
[dir
/2].door_row
= *row
;
418 rm
->doors
[dir
/2].door_col
= *col
;
422 draw_simple_passage(short row1
, short col1
, short row2
, short col2
, short dir
)
426 if ((dir
== LEFT
) || (dir
== RIGHT
)) {
431 middle
= get_rand(col1
+1, col2
-1);
432 for (i
= col1
+1; i
!= middle
; i
++) {
433 dungeon
[row1
][i
] = TUNNEL
;
435 for (i
= row1
; i
!= row2
; i
+= (row1
> row2
) ? -1 : 1) {
436 dungeon
[i
][middle
] = TUNNEL
;
438 for (i
= middle
; i
!= col2
; i
++) {
439 dungeon
[row2
][i
] = TUNNEL
;
446 middle
= get_rand(row1
+1, row2
-1);
447 for (i
= row1
+1; i
!= middle
; i
++) {
448 dungeon
[i
][col1
] = TUNNEL
;
450 for (i
= col1
; i
!= col2
; i
+= (col1
> col2
) ? -1 : 1) {
451 dungeon
[middle
][i
] = TUNNEL
;
453 for (i
= middle
; i
!= row2
; i
++) {
454 dungeon
[i
][col2
] = TUNNEL
;
457 if (rand_percent(HIDE_PERCENT
)) {
458 hide_boxed_passage(row1
, col1
, row2
, col2
, 1);
463 same_row(short room1
, short room2
)
465 return((room1
/ 3) == (room2
/ 3));
469 same_col(short room1
, short room2
)
471 return((room1
% 3) == (room2
% 3));
482 start
= get_rand(0, (MAXROOMS
-1));
483 maze_percent
= (cur_level
* 5) / 4;
485 if (cur_level
> 15) {
486 maze_percent
+= cur_level
;
488 for (i
= 0; i
< MAXROOMS
; i
++) {
489 j
= ((start
+ i
) % MAXROOMS
);
490 if (rooms
[j
].is_room
& R_NOTHING
) {
491 if (rand_percent(maze_percent
)) {
492 rooms
[j
].is_room
= R_MAZE
;
493 make_maze(get_rand(rooms
[j
].top_row
+1, rooms
[j
].bottom_row
-1),
494 get_rand(rooms
[j
].left_col
+1, rooms
[j
].right_col
-1),
495 rooms
[j
].top_row
, rooms
[j
].bottom_row
,
496 rooms
[j
].left_col
, rooms
[j
].right_col
);
497 hide_boxed_passage(rooms
[j
].top_row
, rooms
[j
].left_col
,
498 rooms
[j
].bottom_row
, rooms
[j
].right_col
,
515 for (i
= 0; i
< MAXROOMS
; i
++) {
516 rn
= random_rooms
[i
];
517 if ((rooms
[rn
].is_room
& R_NOTHING
) ||
518 ((rooms
[rn
].is_room
& R_CROSS
) && coin_toss())) {
522 if (r_de
!= NO_ROOM
) {
528 fill_it(int rn
, boolean do_rec_de
)
530 short i
, tunnel_dir
, door_dir
, drow
, dcol
;
531 short target_room
, rooms_found
= 0;
533 static short offsets
[4] = {-1, 1, 3, -3};
534 boolean did_this
= 0;
536 for (i
= 0; i
< 10; i
++) {
537 srow
= get_rand(0, 3);
538 scol
= get_rand(0, 3);
540 offsets
[srow
] = offsets
[scol
];
543 for (i
= 0; i
< 4; i
++) {
545 target_room
= rn
+ offsets
[i
];
547 if (((target_room
< 0) || (target_room
>= MAXROOMS
)) ||
548 (!(same_row(rn
,target_room
) || same_col(rn
,target_room
))) ||
549 (!(rooms
[target_room
].is_room
& (R_ROOM
| R_MAZE
)))) {
552 if (same_row(rn
, target_room
)) {
553 tunnel_dir
= (rooms
[rn
].left_col
< rooms
[target_room
].left_col
) ?
556 tunnel_dir
= (rooms
[rn
].top_row
< rooms
[target_room
].top_row
) ?
559 door_dir
= ((tunnel_dir
+ 4) % DIRS
);
560 if (rooms
[target_room
].doors
[door_dir
/2].oth_room
!= NO_ROOM
) {
563 if (((!do_rec_de
) || did_this
) ||
564 (!mask_room(rn
, &srow
, &scol
, TUNNEL
))) {
565 srow
= (rooms
[rn
].top_row
+ rooms
[rn
].bottom_row
) / 2;
566 scol
= (rooms
[rn
].left_col
+ rooms
[rn
].right_col
) / 2;
568 put_door(&rooms
[target_room
], door_dir
, &drow
, &dcol
);
570 draw_simple_passage(srow
, scol
, drow
, dcol
, tunnel_dir
);
571 rooms
[rn
].is_room
= R_DEADEND
;
572 dungeon
[srow
][scol
] = TUNNEL
;
574 if ((i
< 3) && (!did_this
)) {
580 if ((rooms_found
< 2) && do_rec_de
) {
581 recursive_deadend(rn
, offsets
, srow
, scol
);
588 recursive_deadend(short rn
, const short *offsets
, short srow
, short scol
)
591 short drow
, dcol
, tunnel_dir
;
593 rooms
[rn
].is_room
= R_DEADEND
;
594 dungeon
[srow
][scol
] = TUNNEL
;
596 for (i
= 0; i
< 4; i
++) {
597 de
= rn
+ offsets
[i
];
598 if (((de
< 0) || (de
>= MAXROOMS
)) ||
599 (!(same_row(rn
, de
) || same_col(rn
, de
)))) {
602 if (!(rooms
[de
].is_room
& R_NOTHING
)) {
605 drow
= (rooms
[de
].top_row
+ rooms
[de
].bottom_row
) / 2;
606 dcol
= (rooms
[de
].left_col
+ rooms
[de
].right_col
) / 2;
607 if (same_row(rn
, de
)) {
608 tunnel_dir
= (rooms
[rn
].left_col
< rooms
[de
].left_col
) ?
611 tunnel_dir
= (rooms
[rn
].top_row
< rooms
[de
].top_row
) ?
614 draw_simple_passage(srow
, scol
, drow
, dcol
, tunnel_dir
);
616 recursive_deadend(de
, offsets
, drow
, dcol
);
621 mask_room(short rn
, short *row
, short *col
, unsigned short mask
)
625 for (i
= rooms
[rn
].top_row
; i
<= rooms
[rn
].bottom_row
; i
++) {
626 for (j
= rooms
[rn
].left_col
; j
<= rooms
[rn
].right_col
; j
++) {
627 if (dungeon
[i
][j
] & mask
) {
638 make_maze(short r
, short c
, short tr
, short br
, short lc
, short rc
)
648 dungeon
[r
][c
] = TUNNEL
;
650 if (rand_percent(20)) {
651 for (i
= 0; i
< 10; i
++) {
657 SWAP(dirs
[t1
], dirs
[t2
]);
660 for (i
= 0; i
< 4; i
++) {
664 (dungeon
[r
-1][c
] != TUNNEL
) &&
665 (dungeon
[r
-1][c
-1] != TUNNEL
) &&
666 (dungeon
[r
-1][c
+1] != TUNNEL
) &&
667 (dungeon
[r
-2][c
] != TUNNEL
)) {
668 make_maze((r
-1), c
, tr
, br
, lc
, rc
);
673 (dungeon
[r
+1][c
] != TUNNEL
) &&
674 (dungeon
[r
+1][c
-1] != TUNNEL
) &&
675 (dungeon
[r
+1][c
+1] != TUNNEL
) &&
676 (dungeon
[r
+2][c
] != TUNNEL
)) {
677 make_maze((r
+1), c
, tr
, br
, lc
, rc
);
682 (dungeon
[r
][c
-1] != TUNNEL
) &&
683 (dungeon
[r
-1][c
-1] != TUNNEL
) &&
684 (dungeon
[r
+1][c
-1] != TUNNEL
) &&
685 (dungeon
[r
][c
-2] != TUNNEL
)) {
686 make_maze(r
, (c
-1), tr
, br
, lc
, rc
);
691 (dungeon
[r
][c
+1] != TUNNEL
) &&
692 (dungeon
[r
-1][c
+1] != TUNNEL
) &&
693 (dungeon
[r
+1][c
+1] != TUNNEL
) &&
694 (dungeon
[r
][c
+2] != TUNNEL
)) {
695 make_maze(r
, (c
+1), tr
, br
, lc
, rc
);
703 hide_boxed_passage(short row1
, short col1
, short row2
, short col2
, short n
)
706 short row
, col
, row_cut
, col_cut
;
719 if ((w
>= 5) || (h
>= 5)) {
720 row_cut
= ((h
>= 2) ? 1 : 0);
721 col_cut
= ((w
>= 2) ? 1 : 0);
723 for (i
= 0; i
< n
; i
++) {
724 for (j
= 0; j
< 10; j
++) {
725 row
= get_rand(row1
+ row_cut
, row2
- row_cut
);
726 col
= get_rand(col1
+ col_cut
, col2
- col_cut
);
727 if (dungeon
[row
][col
] == TUNNEL
) {
728 dungeon
[row
][col
] |= HIDDEN
;
738 * 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
]);