1 /* $NetBSD: level.c,v 1.10 2008/01/14 03:50:01 dholland Exp $ */
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)level.c 8.1 (Berkeley) 5/31/93";
40 __RCSID("$NetBSD: level.c,v 1.10 2008/01/14 03:50:01 dholland Exp $");
47 * This source herein may be modified and/or distributed by anybody who
48 * so desires, with the following restrictions:
49 * 1.) No portion of this notice shall be removed.
50 * 2.) Credit shall not be taken for the creation of this source.
51 * 3.) This code is not to be traded, sold, or used for personal
58 #define SWAP(x,y) (t = (x), (x) = (y), (y) = t)
60 static void add_mazes(void);
61 static int connect_rooms(short, short);
62 static void draw_simple_passage(short, short, short, short, short);
63 static void fill_it(int, boolean
);
64 static void fill_out_level(void);
65 static int get_exp_level(long);
66 static void hide_boxed_passage(short, short, short, short, short);
67 static void make_maze(short, short, short, short, short, short);
68 static void make_room(short, short, short, short);
69 static boolean
mask_room(short, short *, short *, unsigned short);
70 static void mix_random_rooms(void);
71 static void put_door(room
*, short, short *, short *);
72 static void recursive_deadend(short, const short *, short, short);
73 static int same_col(int, int);
74 static int same_row(int, int);
79 const char *new_level_message
= NULL
;
80 short party_room
= NO_ROOM
;
84 const long level_points
[MAX_EXP_LEVEL
] = {
108 static short random_rooms
[MAXROOMS
] = {3, 7, 5, 2, 0, 6, 1, 4, 8};
114 short must_1
, must_2
, must_3
;
118 if (cur_level
< LAST_DUNGEON
) {
121 if (cur_level
> max_level
) {
122 max_level
= cur_level
;
124 must_1
= get_rand(0, 5);
158 if (rand_percent(8)) {
161 big_room
= ((party_room
!= NO_ROOM
) && rand_percent(1));
163 make_room(BIG_ROOM
, 0, 0, 0);
165 for (i
= 0; i
< MAXROOMS
; i
++) {
166 make_room(i
, must_1
, must_2
, must_3
);
174 for (j
= 0; j
< MAXROOMS
; j
++) {
178 if (i
< (MAXROOMS
-1)) {
179 (void)connect_rooms(i
, i
+1);
181 if (i
< (MAXROOMS
-3)) {
182 (void)connect_rooms(i
, i
+3);
184 if (i
< (MAXROOMS
-2)) {
185 if (rooms
[i
+1].is_room
& R_NOTHING
) {
186 if (connect_rooms(i
, i
+2)) {
187 rooms
[i
+1].is_room
= R_CROSS
;
191 if (i
< (MAXROOMS
-6)) {
192 if (rooms
[i
+3].is_room
& R_NOTHING
) {
193 if (connect_rooms(i
, i
+6)) {
194 rooms
[i
+3].is_room
= R_CROSS
;
198 if (is_all_connected()) {
204 if (!has_amulet() && (cur_level
>= AMULET_LEVEL
)) {
210 make_room(short rn
, short r1
, short r2
, short r3
)
212 short left_col
, right_col
, top_row
, bottom_row
;
214 short row_offset
, col_offset
;
217 left_col
= right_col
= top_row
= bottom_row
= 0;
259 bottom_row
= DROWS
- 2;
265 bottom_row
= DROWS
- 2;
271 bottom_row
= DROWS
- 2;
274 top_row
= get_rand(MIN_ROW
, MIN_ROW
+5);
275 bottom_row
= get_rand(DROWS
-7, DROWS
-2);
276 left_col
= get_rand(0, 10);
277 right_col
= get_rand(DCOLS
-11, DCOLS
-1);
281 height
= get_rand(4, (bottom_row
- top_row
+ 1));
282 width
= get_rand(7, (right_col
- left_col
- 2));
284 row_offset
= get_rand(0, ((bottom_row
- top_row
) - height
+ 1));
285 col_offset
= get_rand(0, ((right_col
- left_col
) - width
+ 1));
287 top_row
+= row_offset
;
288 bottom_row
= top_row
+ height
- 1;
290 left_col
+= col_offset
;
291 right_col
= left_col
+ width
- 1;
293 if ((rn
!= r1
) && (rn
!= r2
) && (rn
!= r3
) && rand_percent(40)) {
297 rooms
[rn
].is_room
= R_ROOM
;
299 for (i
= top_row
; i
<= bottom_row
; i
++) {
300 for (j
= left_col
; j
<= right_col
; j
++) {
301 if ((i
== top_row
) || (i
== bottom_row
)) {
303 } else if ( ((i
!= top_row
) && (i
!= bottom_row
)) &&
304 ((j
== left_col
) || (j
== right_col
))) {
313 rooms
[rn
].top_row
= top_row
;
314 rooms
[rn
].bottom_row
= bottom_row
;
315 rooms
[rn
].left_col
= left_col
;
316 rooms
[rn
].right_col
= right_col
;
320 connect_rooms(short room1
, short room2
)
322 short row1
, col1
, row2
, col2
, dir
;
324 if ((!(rooms
[room1
].is_room
& (R_ROOM
| R_MAZE
))) ||
325 (!(rooms
[room2
].is_room
& (R_ROOM
| R_MAZE
)))) {
328 if (same_row(room1
, room2
) &&
329 (rooms
[room1
].left_col
> rooms
[room2
].right_col
)) {
330 put_door(&rooms
[room1
], LEFT
, &row1
, &col1
);
331 put_door(&rooms
[room2
], RIGHT
, &row2
, &col2
);
333 } else if (same_row(room1
, room2
) &&
334 (rooms
[room2
].left_col
> rooms
[room1
].right_col
)) {
335 put_door(&rooms
[room1
], RIGHT
, &row1
, &col1
);
336 put_door(&rooms
[room2
], LEFT
, &row2
, &col2
);
338 } else if (same_col(room1
, room2
) &&
339 (rooms
[room1
].top_row
> rooms
[room2
].bottom_row
)) {
340 put_door(&rooms
[room1
], UPWARD
, &row1
, &col1
);
341 put_door(&rooms
[room2
], DOWN
, &row2
, &col2
);
343 } else if (same_col(room1
, room2
) &&
344 (rooms
[room2
].top_row
> rooms
[room1
].bottom_row
)) {
345 put_door(&rooms
[room1
], DOWN
, &row1
, &col1
);
346 put_door(&rooms
[room2
], UPWARD
, &row2
, &col2
);
353 draw_simple_passage(row1
, col1
, row2
, col2
, dir
);
354 } while (rand_percent(4));
356 rooms
[room1
].doors
[dir
/2].oth_room
= room2
;
357 rooms
[room1
].doors
[dir
/2].oth_row
= row2
;
358 rooms
[room1
].doors
[dir
/2].oth_col
= col2
;
360 rooms
[room2
].doors
[(((dir
+4)%DIRS
)/2)].oth_room
= room1
;
361 rooms
[room2
].doors
[(((dir
+4)%DIRS
)/2)].oth_row
= row1
;
362 rooms
[room2
].doors
[(((dir
+4)%DIRS
)/2)].oth_col
= col1
;
371 for (i
= 0; i
< MAXROOMS
; i
++) {
372 rooms
[i
].is_room
= R_NOTHING
;
373 for (j
= 0; j
< 4; j
++) {
374 rooms
[i
].doors
[j
].oth_room
= NO_ROOM
;
378 for (i
= 0; i
< MAX_TRAPS
; i
++) {
379 traps
[i
].trap_type
= NO_TRAP
;
381 for (i
= 0; i
< DROWS
; i
++) {
382 for (j
= 0; j
< DCOLS
; j
++) {
383 dungeon
[i
][j
] = NOTHING
;
386 detect_monster
= see_invisible
= 0;
387 being_held
= bear_trap
= 0;
388 party_room
= NO_ROOM
;
389 rogue
.row
= rogue
.col
= -1;
394 put_door(room
*rm
, short dir
, short *row
, short *col
)
398 wall_width
= (rm
->is_room
& R_MAZE
) ? 0 : 1;
403 *row
= ((dir
== UPWARD
) ? rm
->top_row
: rm
->bottom_row
);
405 *col
= get_rand(rm
->left_col
+wall_width
,
406 rm
->right_col
-wall_width
);
407 } while (!(dungeon
[*row
][*col
] & (HORWALL
| TUNNEL
)));
411 *col
= (dir
== LEFT
) ? rm
->left_col
: rm
->right_col
;
413 *row
= get_rand(rm
->top_row
+wall_width
,
414 rm
->bottom_row
-wall_width
);
415 } while (!(dungeon
[*row
][*col
] & (VERTWALL
| TUNNEL
)));
418 if (rm
->is_room
& R_ROOM
) {
419 dungeon
[*row
][*col
] = DOOR
;
421 if ((cur_level
> 2) && rand_percent(HIDE_PERCENT
)) {
422 dungeon
[*row
][*col
] |= HIDDEN
;
424 rm
->doors
[dir
/2].door_row
= *row
;
425 rm
->doors
[dir
/2].door_col
= *col
;
429 draw_simple_passage(short row1
, short col1
, short row2
, short col2
, short dir
)
433 if ((dir
== LEFT
) || (dir
== RIGHT
)) {
438 middle
= get_rand(col1
+1, col2
-1);
439 for (i
= col1
+1; i
!= middle
; i
++) {
440 dungeon
[row1
][i
] = TUNNEL
;
442 for (i
= row1
; i
!= row2
; i
+= (row1
> row2
) ? -1 : 1) {
443 dungeon
[i
][middle
] = TUNNEL
;
445 for (i
= middle
; i
!= col2
; i
++) {
446 dungeon
[row2
][i
] = TUNNEL
;
453 middle
= get_rand(row1
+1, row2
-1);
454 for (i
= row1
+1; i
!= middle
; i
++) {
455 dungeon
[i
][col1
] = TUNNEL
;
457 for (i
= col1
; i
!= col2
; i
+= (col1
> col2
) ? -1 : 1) {
458 dungeon
[middle
][i
] = TUNNEL
;
460 for (i
= middle
; i
!= row2
; i
++) {
461 dungeon
[i
][col2
] = TUNNEL
;
464 if (rand_percent(HIDE_PERCENT
)) {
465 hide_boxed_passage(row1
, col1
, row2
, col2
, 1);
470 same_row(int room1
, int room2
)
472 return((room1
/ 3) == (room2
/ 3));
476 same_col(int room1
, int room2
)
478 return((room1
% 3) == (room2
% 3));
489 start
= get_rand(0, (MAXROOMS
-1));
490 maze_percent
= (cur_level
* 5) / 4;
492 if (cur_level
> 15) {
493 maze_percent
+= cur_level
;
495 for (i
= 0; i
< MAXROOMS
; i
++) {
496 j
= ((start
+ i
) % MAXROOMS
);
497 if (rooms
[j
].is_room
& R_NOTHING
) {
498 if (rand_percent(maze_percent
)) {
499 rooms
[j
].is_room
= R_MAZE
;
500 make_maze(get_rand(rooms
[j
].top_row
+1, rooms
[j
].bottom_row
-1),
501 get_rand(rooms
[j
].left_col
+1, rooms
[j
].right_col
-1),
502 rooms
[j
].top_row
, rooms
[j
].bottom_row
,
503 rooms
[j
].left_col
, rooms
[j
].right_col
);
504 hide_boxed_passage(rooms
[j
].top_row
, rooms
[j
].left_col
,
505 rooms
[j
].bottom_row
, rooms
[j
].right_col
,
522 for (i
= 0; i
< MAXROOMS
; i
++) {
523 rn
= random_rooms
[i
];
524 if ((rooms
[rn
].is_room
& R_NOTHING
) ||
525 ((rooms
[rn
].is_room
& R_CROSS
) && coin_toss())) {
529 if (r_de
!= NO_ROOM
) {
535 fill_it(int rn
, boolean do_rec_de
)
537 short i
, tunnel_dir
, door_dir
, drow
, dcol
;
538 short target_room
, rooms_found
= 0;
540 static short offsets
[4] = {-1, 1, 3, -3};
541 boolean did_this
= 0;
544 /* LSC: maybe-uninitialized, when compiling with -O3 */
546 #endif /* defined(__minix)*/
547 for (i
= 0; i
< 10; i
++) {
548 srow
= get_rand(0, 3);
549 scol
= get_rand(0, 3);
551 offsets
[srow
] = offsets
[scol
];
554 for (i
= 0; i
< 4; i
++) {
556 target_room
= rn
+ offsets
[i
];
558 if (((target_room
< 0) || (target_room
>= MAXROOMS
)) ||
559 (!(same_row(rn
,target_room
) || same_col(rn
,target_room
))) ||
560 (!(rooms
[target_room
].is_room
& (R_ROOM
| R_MAZE
)))) {
563 if (same_row(rn
, target_room
)) {
564 tunnel_dir
= (rooms
[rn
].left_col
< rooms
[target_room
].left_col
) ?
567 tunnel_dir
= (rooms
[rn
].top_row
< rooms
[target_room
].top_row
) ?
570 door_dir
= ((tunnel_dir
+ 4) % DIRS
);
571 if (rooms
[target_room
].doors
[door_dir
/2].oth_room
!= NO_ROOM
) {
574 if (((!do_rec_de
) || did_this
) ||
575 (!mask_room(rn
, &srow
, &scol
, TUNNEL
))) {
576 srow
= (rooms
[rn
].top_row
+ rooms
[rn
].bottom_row
) / 2;
577 scol
= (rooms
[rn
].left_col
+ rooms
[rn
].right_col
) / 2;
579 put_door(&rooms
[target_room
], door_dir
, &drow
, &dcol
);
581 draw_simple_passage(srow
, scol
, drow
, dcol
, tunnel_dir
);
582 rooms
[rn
].is_room
= R_DEADEND
;
583 dungeon
[srow
][scol
] = TUNNEL
;
585 if ((i
< 3) && (!did_this
)) {
591 if ((rooms_found
< 2) && do_rec_de
) {
592 recursive_deadend(rn
, offsets
, srow
, scol
);
599 recursive_deadend(short rn
, const short *offsets
, short srow
, short scol
)
602 short drow
, dcol
, tunnel_dir
;
604 rooms
[rn
].is_room
= R_DEADEND
;
605 dungeon
[srow
][scol
] = TUNNEL
;
607 for (i
= 0; i
< 4; i
++) {
608 de
= rn
+ offsets
[i
];
609 if (((de
< 0) || (de
>= MAXROOMS
)) ||
610 (!(same_row(rn
, de
) || same_col(rn
, de
)))) {
613 if (!(rooms
[de
].is_room
& R_NOTHING
)) {
616 drow
= (rooms
[de
].top_row
+ rooms
[de
].bottom_row
) / 2;
617 dcol
= (rooms
[de
].left_col
+ rooms
[de
].right_col
) / 2;
618 if (same_row(rn
, de
)) {
619 tunnel_dir
= (rooms
[rn
].left_col
< rooms
[de
].left_col
) ?
622 tunnel_dir
= (rooms
[rn
].top_row
< rooms
[de
].top_row
) ?
625 draw_simple_passage(srow
, scol
, drow
, dcol
, tunnel_dir
);
627 recursive_deadend(de
, offsets
, drow
, dcol
);
632 mask_room(short rn
, short *row
, short *col
, unsigned short mask
)
636 for (i
= rooms
[rn
].top_row
; i
<= rooms
[rn
].bottom_row
; i
++) {
637 for (j
= rooms
[rn
].left_col
; j
<= rooms
[rn
].right_col
; j
++) {
638 if (dungeon
[i
][j
] & mask
) {
649 make_maze(short r
, short c
, short tr
, short br
, short lc
, short rc
)
659 dungeon
[r
][c
] = TUNNEL
;
661 if (rand_percent(20)) {
662 for (i
= 0; i
< 10; i
++) {
668 SWAP(dirs
[t1
], dirs
[t2
]);
671 for (i
= 0; i
< 4; i
++) {
675 (dungeon
[r
-1][c
] != TUNNEL
) &&
676 (dungeon
[r
-1][c
-1] != TUNNEL
) &&
677 (dungeon
[r
-1][c
+1] != TUNNEL
) &&
678 (dungeon
[r
-2][c
] != TUNNEL
)) {
679 make_maze((r
-1), c
, tr
, br
, lc
, rc
);
684 (dungeon
[r
+1][c
] != TUNNEL
) &&
685 (dungeon
[r
+1][c
-1] != TUNNEL
) &&
686 (dungeon
[r
+1][c
+1] != TUNNEL
) &&
687 (dungeon
[r
+2][c
] != TUNNEL
)) {
688 make_maze((r
+1), c
, tr
, br
, lc
, rc
);
693 (dungeon
[r
][c
-1] != TUNNEL
) &&
694 (dungeon
[r
-1][c
-1] != TUNNEL
) &&
695 (dungeon
[r
+1][c
-1] != TUNNEL
) &&
696 (dungeon
[r
][c
-2] != TUNNEL
)) {
697 make_maze(r
, (c
-1), tr
, br
, lc
, rc
);
702 (dungeon
[r
][c
+1] != TUNNEL
) &&
703 (dungeon
[r
-1][c
+1] != TUNNEL
) &&
704 (dungeon
[r
+1][c
+1] != TUNNEL
) &&
705 (dungeon
[r
][c
+2] != TUNNEL
)) {
706 make_maze(r
, (c
+1), tr
, br
, lc
, rc
);
714 hide_boxed_passage(short row1
, short col1
, short row2
, short col2
, short n
)
717 short row
, col
, row_cut
, col_cut
;
730 if ((w
>= 5) || (h
>= 5)) {
731 row_cut
= ((h
>= 2) ? 1 : 0);
732 col_cut
= ((w
>= 2) ? 1 : 0);
734 for (i
= 0; i
< n
; i
++) {
735 for (j
= 0; j
< 10; j
++) {
736 row
= get_rand(row1
+ row_cut
, row2
- row_cut
);
737 col
= get_rand(col1
+ col_cut
, col2
- col_cut
);
738 if (dungeon
[row
][col
] == TUNNEL
) {
739 dungeon
[row
][col
] |= HIDDEN
;
749 * try not to put in room NR
754 short rn
= nr
, misses
;
757 for (misses
= 0; ((misses
< 2) && (rn
== nr
)); misses
++) {
758 gr_row_col(&row
, &col
, (FLOOR
| TUNNEL
| OBJECT
| STAIRS
));
759 rn
= get_room_number(row
, col
);
764 if (dungeon
[rogue
.row
][rogue
.col
] & TUNNEL
) {
769 if (cur_room
!= PASSAGE
) {
770 light_up_room(cur_room
);
772 light_passage(rogue
.row
, rogue
.col
);
774 rn
= get_room_number(rogue
.row
, rogue
.col
);
775 wake_room(rn
, 1, rogue
.row
, rogue
.col
);
776 if (new_level_message
) {
777 messagef(0, "%s", new_level_message
);
778 new_level_message
= NULL
;
780 mvaddch(rogue
.row
, rogue
.col
, rogue
.fchar
);
789 if (dungeon
[rogue
.row
][rogue
.col
] & STAIRS
) {
791 messagef(0, "you're floating in the air!");
796 messagef(0, "I see no way down");
804 if (!(dungeon
[rogue
.row
][rogue
.col
] & STAIRS
)) {
805 messagef(0, "I see no way up");
809 messagef(0, "your way is magically blocked");
813 new_level_message
= "you feel a wrenching sensation in your gut";
814 if (cur_level
== 1) {
824 add_exp(int e
, boolean promotion
)
829 rogue
.exp_points
+= e
;
831 if (rogue
.exp_points
>= level_points
[rogue
.exp
-1]) {
832 new_exp
= get_exp_level(rogue
.exp_points
);
833 if (rogue
.exp_points
> MAX_EXP
) {
834 rogue
.exp_points
= MAX_EXP
+ 1;
836 for (i
= rogue
.exp
+1; i
<= new_exp
; i
++) {
837 messagef(0, "welcome to level %d", i
);
840 rogue
.hp_current
+= hp
;
844 print_stats(STAT_HP
| STAT_EXP
);
847 print_stats(STAT_EXP
);
852 get_exp_level(long e
)
856 for (i
= 0; i
< (MAX_EXP_LEVEL
- 1); i
++) {
857 if (level_points
[i
] > e
) {
869 hp
= (wizard
? 10 : get_rand(3, 10));
874 show_average_hp(void)
877 float effective_average
;
879 if (rogue
.exp
== 1) {
880 real_average
= effective_average
= 0.00;
882 real_average
= (float)
883 ((rogue
.hp_max
- extra_hp
- INIT_HP
) + less_hp
) / (rogue
.exp
- 1);
884 effective_average
= (float)(rogue
.hp_max
- INIT_HP
) / (rogue
.exp
- 1);
887 messagef(0, "R-Hp: %.2f, E-Hp: %.2f (!: %d, V: %d)", real_average
,
888 effective_average
, extra_hp
, less_hp
);
892 mix_random_rooms(void)
897 for (i
= 0; i
< (3 * MAXROOMS
); i
++) {
899 x
= get_rand(0, (MAXROOMS
-1));
900 y
= get_rand(0, (MAXROOMS
-1));
902 SWAP(random_rooms
[x
], random_rooms
[y
]);