]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - rogue/level.c
1 /* $NetBSD: level.c,v 1.4 1997/10/12 11:45:16 lukem 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
42 static char sccsid
[] = "@(#)level.c 8.1 (Berkeley) 5/31/93";
44 __RCSID("$NetBSD: level.c,v 1.4 1997/10/12 11:45:16 lukem Exp $");
51 * This source herein may be modified and/or distributed by anybody who
52 * so desires, with the following restrictions:
53 * 1.) No portion of this notice shall be removed.
54 * 2.) Credit shall not be taken for the creation of this source.
55 * 3.) This code is not to be traded, sold, or used for personal
62 #define swap(x,y) {t = x; x = y; y = t;}
67 char *new_level_message
= 0;
68 short party_room
= NO_ROOM
;
71 long level_points
[MAX_EXP_LEVEL
] = {
95 short random_rooms
[MAXROOMS
] = {3, 7, 5, 2, 0, 6, 1, 4, 8};
101 short must_1
, must_2
, must_3
;
105 if (cur_level
< LAST_DUNGEON
) {
108 if (cur_level
> max_level
) {
109 max_level
= cur_level
;
111 must_1
= get_rand(0, 5);
145 if (rand_percent(8)) {
148 big_room
= ((party_room
!= NO_ROOM
) && rand_percent(1));
150 make_room(BIG_ROOM
, 0, 0, 0);
152 for (i
= 0; i
< MAXROOMS
; i
++) {
153 make_room(i
, must_1
, must_2
, must_3
);
161 for (j
= 0; j
< MAXROOMS
; j
++) {
165 if (i
< (MAXROOMS
-1)) {
166 (void) connect_rooms(i
, i
+1);
168 if (i
< (MAXROOMS
-3)) {
169 (void) connect_rooms(i
, i
+3);
171 if (i
< (MAXROOMS
-2)) {
172 if (rooms
[i
+1].is_room
& R_NOTHING
) {
173 if (connect_rooms(i
, i
+2)) {
174 rooms
[i
+1].is_room
= R_CROSS
;
178 if (i
< (MAXROOMS
-6)) {
179 if (rooms
[i
+3].is_room
& R_NOTHING
) {
180 if (connect_rooms(i
, i
+6)) {
181 rooms
[i
+3].is_room
= R_CROSS
;
185 if (is_all_connected()) {
191 if (!has_amulet() && (cur_level
>= AMULET_LEVEL
)) {
197 make_room(rn
, r1
, r2
, r3
)
198 short rn
, r1
, r2
, r3
;
200 short left_col
, right_col
, top_row
, bottom_row
;
202 short row_offset
, col_offset
;
205 left_col
= right_col
= top_row
= bottom_row
= 0;
247 bottom_row
= DROWS
- 2;
253 bottom_row
= DROWS
- 2;
259 bottom_row
= DROWS
- 2;
262 top_row
= get_rand(MIN_ROW
, MIN_ROW
+5);
263 bottom_row
= get_rand(DROWS
-7, DROWS
-2);
264 left_col
= get_rand(0, 10);;
265 right_col
= get_rand(DCOLS
-11, DCOLS
-1);
269 height
= get_rand(4, (bottom_row
- top_row
+ 1));
270 width
= get_rand(7, (right_col
- left_col
- 2));
272 row_offset
= get_rand(0, ((bottom_row
- top_row
) - height
+ 1));
273 col_offset
= get_rand(0, ((right_col
- left_col
) - width
+ 1));
275 top_row
+= row_offset
;
276 bottom_row
= top_row
+ height
- 1;
278 left_col
+= col_offset
;
279 right_col
= left_col
+ width
- 1;
281 if ((rn
!= r1
) && (rn
!= r2
) && (rn
!= r3
) && rand_percent(40)) {
285 rooms
[rn
].is_room
= R_ROOM
;
287 for (i
= top_row
; i
<= bottom_row
; i
++) {
288 for (j
= left_col
; j
<= right_col
; j
++) {
289 if ((i
== top_row
) || (i
== bottom_row
)) {
291 } else if ( ((i
!= top_row
) && (i
!= bottom_row
)) &&
292 ((j
== left_col
) || (j
== right_col
))) {
301 rooms
[rn
].top_row
= top_row
;
302 rooms
[rn
].bottom_row
= bottom_row
;
303 rooms
[rn
].left_col
= left_col
;
304 rooms
[rn
].right_col
= right_col
;
308 connect_rooms(room1
, room2
)
311 short row1
, col1
, row2
, col2
, dir
;
313 if ((!(rooms
[room1
].is_room
& (R_ROOM
| R_MAZE
))) ||
314 (!(rooms
[room2
].is_room
& (R_ROOM
| R_MAZE
)))) {
317 if (same_row(room1
, room2
) &&
318 (rooms
[room1
].left_col
> rooms
[room2
].right_col
)) {
319 put_door(&rooms
[room1
], LEFT
, &row1
, &col1
);
320 put_door(&rooms
[room2
], RIGHT
, &row2
, &col2
);
322 } else if (same_row(room1
, room2
) &&
323 (rooms
[room2
].left_col
> rooms
[room1
].right_col
)) {
324 put_door(&rooms
[room1
], RIGHT
, &row1
, &col1
);
325 put_door(&rooms
[room2
], LEFT
, &row2
, &col2
);
327 } else if (same_col(room1
, room2
) &&
328 (rooms
[room1
].top_row
> rooms
[room2
].bottom_row
)) {
329 put_door(&rooms
[room1
], UPWARD
, &row1
, &col1
);
330 put_door(&rooms
[room2
], DOWN
, &row2
, &col2
);
332 } else if (same_col(room1
, room2
) &&
333 (rooms
[room2
].top_row
> rooms
[room1
].bottom_row
)) {
334 put_door(&rooms
[room1
], DOWN
, &row1
, &col1
);
335 put_door(&rooms
[room2
], UPWARD
, &row2
, &col2
);
342 draw_simple_passage(row1
, col1
, row2
, col2
, dir
);
343 } while (rand_percent(4));
345 rooms
[room1
].doors
[dir
/2].oth_room
= room2
;
346 rooms
[room1
].doors
[dir
/2].oth_row
= row2
;
347 rooms
[room1
].doors
[dir
/2].oth_col
= col2
;
349 rooms
[room2
].doors
[(((dir
+4)%DIRS
)/2)].oth_room
= room1
;
350 rooms
[room2
].doors
[(((dir
+4)%DIRS
)/2)].oth_row
= row1
;
351 rooms
[room2
].doors
[(((dir
+4)%DIRS
)/2)].oth_col
= col1
;
360 for (i
= 0; i
< MAXROOMS
; i
++) {
361 rooms
[i
].is_room
= R_NOTHING
;
362 for (j
= 0; j
< 4; j
++) {
363 rooms
[i
].doors
[j
].oth_room
= NO_ROOM
;
367 for (i
= 0; i
< MAX_TRAPS
; i
++) {
368 traps
[i
].trap_type
= NO_TRAP
;
370 for (i
= 0; i
< DROWS
; i
++) {
371 for (j
= 0; j
< DCOLS
; j
++) {
372 dungeon
[i
][j
] = NOTHING
;
375 detect_monster
= see_invisible
= 0;
376 being_held
= bear_trap
= 0;
377 party_room
= NO_ROOM
;
378 rogue
.row
= rogue
.col
= -1;
383 put_door(rm
, dir
, row
, col
)
390 wall_width
= (rm
->is_room
& R_MAZE
) ? 0 : 1;
395 *row
= ((dir
== UPWARD
) ? rm
->top_row
: rm
->bottom_row
);
397 *col
= get_rand(rm
->left_col
+wall_width
,
398 rm
->right_col
-wall_width
);
399 } while (!(dungeon
[*row
][*col
] & (HORWALL
| TUNNEL
)));
403 *col
= (dir
== LEFT
) ? rm
->left_col
: rm
->right_col
;
405 *row
= get_rand(rm
->top_row
+wall_width
,
406 rm
->bottom_row
-wall_width
);
407 } while (!(dungeon
[*row
][*col
] & (VERTWALL
| TUNNEL
)));
410 if (rm
->is_room
& R_ROOM
) {
411 dungeon
[*row
][*col
] = DOOR
;
413 if ((cur_level
> 2) && rand_percent(HIDE_PERCENT
)) {
414 dungeon
[*row
][*col
] |= HIDDEN
;
416 rm
->doors
[dir
/2].door_row
= *row
;
417 rm
->doors
[dir
/2].door_col
= *col
;
421 draw_simple_passage(row1
, col1
, row2
, col2
, dir
)
422 short row1
, col1
, row2
, col2
, 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(room1
, room2
)
466 return((room1
/ 3) == (room2
/ 3));
470 same_col(room1
, room2
)
473 return((room1
% 3) == (room2
% 3));
484 start
= get_rand(0, (MAXROOMS
-1));
485 maze_percent
= (cur_level
* 5) / 4;
487 if (cur_level
> 15) {
488 maze_percent
+= cur_level
;
490 for (i
= 0; i
< MAXROOMS
; i
++) {
491 j
= ((start
+ i
) % MAXROOMS
);
492 if (rooms
[j
].is_room
& R_NOTHING
) {
493 if (rand_percent(maze_percent
)) {
494 rooms
[j
].is_room
= R_MAZE
;
495 make_maze(get_rand(rooms
[j
].top_row
+1, rooms
[j
].bottom_row
-1),
496 get_rand(rooms
[j
].left_col
+1, rooms
[j
].right_col
-1),
497 rooms
[j
].top_row
, rooms
[j
].bottom_row
,
498 rooms
[j
].left_col
, rooms
[j
].right_col
);
499 hide_boxed_passage(rooms
[j
].top_row
, rooms
[j
].left_col
,
500 rooms
[j
].bottom_row
, rooms
[j
].right_col
,
517 for (i
= 0; i
< MAXROOMS
; i
++) {
518 rn
= random_rooms
[i
];
519 if ((rooms
[rn
].is_room
& R_NOTHING
) ||
520 ((rooms
[rn
].is_room
& R_CROSS
) && coin_toss())) {
524 if (r_de
!= NO_ROOM
) {
530 fill_it(rn
, do_rec_de
)
534 short i
, tunnel_dir
, door_dir
, drow
, dcol
;
535 short target_room
, rooms_found
= 0;
537 static short offsets
[4] = {-1, 1, 3, -3};
538 boolean did_this
= 0;
540 for (i
= 0; i
< 10; i
++) {
541 srow
= get_rand(0, 3);
542 scol
= get_rand(0, 3);
544 offsets
[srow
] = offsets
[scol
];
547 for (i
= 0; i
< 4; i
++) {
549 target_room
= rn
+ offsets
[i
];
551 if (((target_room
< 0) || (target_room
>= MAXROOMS
)) ||
552 (!(same_row(rn
,target_room
) || same_col(rn
,target_room
))) ||
553 (!(rooms
[target_room
].is_room
& (R_ROOM
| R_MAZE
)))) {
556 if (same_row(rn
, target_room
)) {
557 tunnel_dir
= (rooms
[rn
].left_col
< rooms
[target_room
].left_col
) ?
560 tunnel_dir
= (rooms
[rn
].top_row
< rooms
[target_room
].top_row
) ?
563 door_dir
= ((tunnel_dir
+ 4) % DIRS
);
564 if (rooms
[target_room
].doors
[door_dir
/2].oth_room
!= NO_ROOM
) {
567 if (((!do_rec_de
) || did_this
) ||
568 (!mask_room(rn
, &srow
, &scol
, TUNNEL
))) {
569 srow
= (rooms
[rn
].top_row
+ rooms
[rn
].bottom_row
) / 2;
570 scol
= (rooms
[rn
].left_col
+ rooms
[rn
].right_col
) / 2;
572 put_door(&rooms
[target_room
], door_dir
, &drow
, &dcol
);
574 draw_simple_passage(srow
, scol
, drow
, dcol
, tunnel_dir
);
575 rooms
[rn
].is_room
= R_DEADEND
;
576 dungeon
[srow
][scol
] = TUNNEL
;
578 if ((i
< 3) && (!did_this
)) {
584 if ((rooms_found
< 2) && do_rec_de
) {
585 recursive_deadend(rn
, offsets
, srow
, scol
);
592 recursive_deadend(rn
, offsets
, srow
, scol
)
598 short drow
, dcol
, tunnel_dir
;
600 rooms
[rn
].is_room
= R_DEADEND
;
601 dungeon
[srow
][scol
] = TUNNEL
;
603 for (i
= 0; i
< 4; i
++) {
604 de
= rn
+ offsets
[i
];
605 if (((de
< 0) || (de
>= MAXROOMS
)) ||
606 (!(same_row(rn
, de
) || same_col(rn
, de
)))) {
609 if (!(rooms
[de
].is_room
& R_NOTHING
)) {
612 drow
= (rooms
[de
].top_row
+ rooms
[de
].bottom_row
) / 2;
613 dcol
= (rooms
[de
].left_col
+ rooms
[de
].right_col
) / 2;
614 if (same_row(rn
, de
)) {
615 tunnel_dir
= (rooms
[rn
].left_col
< rooms
[de
].left_col
) ?
618 tunnel_dir
= (rooms
[rn
].top_row
< rooms
[de
].top_row
) ?
621 draw_simple_passage(srow
, scol
, drow
, dcol
, tunnel_dir
);
623 recursive_deadend(de
, offsets
, drow
, dcol
);
628 mask_room(rn
, row
, col
, mask
)
635 for (i
= rooms
[rn
].top_row
; i
<= rooms
[rn
].bottom_row
; i
++) {
636 for (j
= rooms
[rn
].left_col
; j
<= rooms
[rn
].right_col
; j
++) {
637 if (dungeon
[i
][j
] & mask
) {
648 make_maze(r
, c
, tr
, br
, lc
, rc
)
649 short r
, c
, tr
, br
, lc
, 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(row1
, col1
, row2
, col2
, n
)
715 short row1
, col1
, row2
, col2
, n
;
718 short row
, col
, row_cut
, col_cut
;
731 if ((w
>= 5) || (h
>= 5)) {
732 row_cut
= ((h
>= 2) ? 1 : 0);
733 col_cut
= ((w
>= 2) ? 1 : 0);
735 for (i
= 0; i
< n
; i
++) {
736 for (j
= 0; j
< 10; j
++) {
737 row
= get_rand(row1
+ row_cut
, row2
- row_cut
);
738 col
= get_rand(col1
+ col_cut
, col2
- col_cut
);
739 if (dungeon
[row
][col
] == TUNNEL
) {
740 dungeon
[row
][col
] |= HIDDEN
;
751 short nr
; /* try not to put in this room */
753 short rn
= nr
, misses
;
756 for (misses
= 0; ((misses
< 2) && (rn
== nr
)); misses
++) {
757 gr_row_col(&row
, &col
, (FLOOR
| TUNNEL
| OBJECT
| STAIRS
));
758 rn
= get_room_number(row
, col
);
763 if (dungeon
[rogue
.row
][rogue
.col
] & TUNNEL
) {
768 if (cur_room
!= PASSAGE
) {
769 light_up_room(cur_room
);
771 light_passage(rogue
.row
, rogue
.col
);
773 rn
= get_room_number(rogue
.row
, rogue
.col
);
774 wake_room(rn
, 1, rogue
.row
, rogue
.col
);
775 if (new_level_message
) {
776 message(new_level_message
, 0);
777 new_level_message
= 0;
779 mvaddch(rogue
.row
, rogue
.col
, rogue
.fchar
);
788 if (dungeon
[rogue
.row
][rogue
.col
] & STAIRS
) {
790 message("you're floating in the air!", 0);
795 message("I see no way down", 0);
803 if (!(dungeon
[rogue
.row
][rogue
.col
] & STAIRS
)) {
804 message("I see no way up", 0);
808 message("your way is magically blocked", 0);
812 new_level_message
= "you feel a wrenching sensation in your gut";
813 if (cur_level
== 1) {
823 add_exp(e
, promotion
)
831 rogue
.exp_points
+= e
;
833 if (rogue
.exp_points
>= level_points
[rogue
.exp
-1]) {
834 new_exp
= get_exp_level(rogue
.exp_points
);
835 if (rogue
.exp_points
> MAX_EXP
) {
836 rogue
.exp_points
= MAX_EXP
+ 1;
838 for (i
= rogue
.exp
+1; i
<= new_exp
; i
++) {
839 sprintf(mbuf
, "welcome to level %d", i
);
843 rogue
.hp_current
+= hp
;
847 print_stats(STAT_HP
| STAT_EXP
);
850 print_stats(STAT_EXP
);
860 for (i
= 0; i
< (MAX_EXP_LEVEL
- 1); i
++) {
861 if (level_points
[i
] > e
) {
873 hp
= (wizard
? 10 : get_rand(3, 10));
882 float effective_average
;
884 if (rogue
.exp
== 1) {
885 real_average
= effective_average
= 0.00;
887 real_average
= (float)
888 ((rogue
.hp_max
- extra_hp
- INIT_HP
) + less_hp
) / (rogue
.exp
- 1);
889 effective_average
= (float) (rogue
.hp_max
- INIT_HP
) / (rogue
.exp
- 1);
892 sprintf(mbuf
, "R-Hp: %.2f, E-Hp: %.2f (!: %d, V: %d)", real_average
,
893 effective_average
, extra_hp
, less_hp
);
903 for (i
= 0; i
< (3 * MAXROOMS
); i
++) {
905 x
= get_rand(0, (MAXROOMS
-1));
906 y
= get_rand(0, (MAXROOMS
-1));
908 swap(random_rooms
[x
], random_rooms
[y
]);