]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - rogue/level.c
2602991806c8fef3f5423f09d766f2ae2b40e9a5
1 /* $NetBSD: level.c,v 1.9 2008/01/14 00:23:51 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.9 2008/01/14 00:23:51 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;}
63 const char *new_level_message
= 0;
64 short party_room
= NO_ROOM
;
67 const long level_points
[MAX_EXP_LEVEL
] = {
91 short random_rooms
[MAXROOMS
] = {3, 7, 5, 2, 0, 6, 1, 4, 8};
97 short must_1
, must_2
, must_3
;
101 if (cur_level
< LAST_DUNGEON
) {
104 if (cur_level
> max_level
) {
105 max_level
= cur_level
;
107 must_1
= get_rand(0, 5);
141 if (rand_percent(8)) {
144 big_room
= ((party_room
!= NO_ROOM
) && rand_percent(1));
146 make_room(BIG_ROOM
, 0, 0, 0);
148 for (i
= 0; i
< MAXROOMS
; i
++) {
149 make_room(i
, must_1
, must_2
, must_3
);
157 for (j
= 0; j
< MAXROOMS
; j
++) {
161 if (i
< (MAXROOMS
-1)) {
162 (void)connect_rooms(i
, i
+1);
164 if (i
< (MAXROOMS
-3)) {
165 (void)connect_rooms(i
, i
+3);
167 if (i
< (MAXROOMS
-2)) {
168 if (rooms
[i
+1].is_room
& R_NOTHING
) {
169 if (connect_rooms(i
, i
+2)) {
170 rooms
[i
+1].is_room
= R_CROSS
;
174 if (i
< (MAXROOMS
-6)) {
175 if (rooms
[i
+3].is_room
& R_NOTHING
) {
176 if (connect_rooms(i
, i
+6)) {
177 rooms
[i
+3].is_room
= R_CROSS
;
181 if (is_all_connected()) {
187 if (!has_amulet() && (cur_level
>= AMULET_LEVEL
)) {
193 make_room(rn
, r1
, r2
, r3
)
194 short rn
, r1
, r2
, r3
;
196 short left_col
, right_col
, top_row
, bottom_row
;
198 short row_offset
, col_offset
;
201 left_col
= right_col
= top_row
= bottom_row
= 0;
243 bottom_row
= DROWS
- 2;
249 bottom_row
= DROWS
- 2;
255 bottom_row
= DROWS
- 2;
258 top_row
= get_rand(MIN_ROW
, MIN_ROW
+5);
259 bottom_row
= get_rand(DROWS
-7, DROWS
-2);
260 left_col
= get_rand(0, 10);
261 right_col
= get_rand(DCOLS
-11, DCOLS
-1);
265 height
= get_rand(4, (bottom_row
- top_row
+ 1));
266 width
= get_rand(7, (right_col
- left_col
- 2));
268 row_offset
= get_rand(0, ((bottom_row
- top_row
) - height
+ 1));
269 col_offset
= get_rand(0, ((right_col
- left_col
) - width
+ 1));
271 top_row
+= row_offset
;
272 bottom_row
= top_row
+ height
- 1;
274 left_col
+= col_offset
;
275 right_col
= left_col
+ width
- 1;
277 if ((rn
!= r1
) && (rn
!= r2
) && (rn
!= r3
) && rand_percent(40)) {
281 rooms
[rn
].is_room
= R_ROOM
;
283 for (i
= top_row
; i
<= bottom_row
; i
++) {
284 for (j
= left_col
; j
<= right_col
; j
++) {
285 if ((i
== top_row
) || (i
== bottom_row
)) {
287 } else if ( ((i
!= top_row
) && (i
!= bottom_row
)) &&
288 ((j
== left_col
) || (j
== right_col
))) {
297 rooms
[rn
].top_row
= top_row
;
298 rooms
[rn
].bottom_row
= bottom_row
;
299 rooms
[rn
].left_col
= left_col
;
300 rooms
[rn
].right_col
= right_col
;
304 connect_rooms(room1
, room2
)
307 short row1
, col1
, row2
, col2
, dir
;
309 if ((!(rooms
[room1
].is_room
& (R_ROOM
| R_MAZE
))) ||
310 (!(rooms
[room2
].is_room
& (R_ROOM
| R_MAZE
)))) {
313 if (same_row(room1
, room2
) &&
314 (rooms
[room1
].left_col
> rooms
[room2
].right_col
)) {
315 put_door(&rooms
[room1
], LEFT
, &row1
, &col1
);
316 put_door(&rooms
[room2
], RIGHT
, &row2
, &col2
);
318 } else if (same_row(room1
, room2
) &&
319 (rooms
[room2
].left_col
> rooms
[room1
].right_col
)) {
320 put_door(&rooms
[room1
], RIGHT
, &row1
, &col1
);
321 put_door(&rooms
[room2
], LEFT
, &row2
, &col2
);
323 } else if (same_col(room1
, room2
) &&
324 (rooms
[room1
].top_row
> rooms
[room2
].bottom_row
)) {
325 put_door(&rooms
[room1
], UPWARD
, &row1
, &col1
);
326 put_door(&rooms
[room2
], DOWN
, &row2
, &col2
);
328 } else if (same_col(room1
, room2
) &&
329 (rooms
[room2
].top_row
> rooms
[room1
].bottom_row
)) {
330 put_door(&rooms
[room1
], DOWN
, &row1
, &col1
);
331 put_door(&rooms
[room2
], UPWARD
, &row2
, &col2
);
338 draw_simple_passage(row1
, col1
, row2
, col2
, dir
);
339 } while (rand_percent(4));
341 rooms
[room1
].doors
[dir
/2].oth_room
= room2
;
342 rooms
[room1
].doors
[dir
/2].oth_row
= row2
;
343 rooms
[room1
].doors
[dir
/2].oth_col
= col2
;
345 rooms
[room2
].doors
[(((dir
+4)%DIRS
)/2)].oth_room
= room1
;
346 rooms
[room2
].doors
[(((dir
+4)%DIRS
)/2)].oth_row
= row1
;
347 rooms
[room2
].doors
[(((dir
+4)%DIRS
)/2)].oth_col
= col1
;
356 for (i
= 0; i
< MAXROOMS
; i
++) {
357 rooms
[i
].is_room
= R_NOTHING
;
358 for (j
= 0; j
< 4; j
++) {
359 rooms
[i
].doors
[j
].oth_room
= NO_ROOM
;
363 for (i
= 0; i
< MAX_TRAPS
; i
++) {
364 traps
[i
].trap_type
= NO_TRAP
;
366 for (i
= 0; i
< DROWS
; i
++) {
367 for (j
= 0; j
< DCOLS
; j
++) {
368 dungeon
[i
][j
] = NOTHING
;
371 detect_monster
= see_invisible
= 0;
372 being_held
= bear_trap
= 0;
373 party_room
= NO_ROOM
;
374 rogue
.row
= rogue
.col
= -1;
379 put_door(rm
, dir
, row
, col
)
386 wall_width
= (rm
->is_room
& R_MAZE
) ? 0 : 1;
391 *row
= ((dir
== UPWARD
) ? rm
->top_row
: rm
->bottom_row
);
393 *col
= get_rand(rm
->left_col
+wall_width
,
394 rm
->right_col
-wall_width
);
395 } while (!(dungeon
[*row
][*col
] & (HORWALL
| TUNNEL
)));
399 *col
= (dir
== LEFT
) ? rm
->left_col
: rm
->right_col
;
401 *row
= get_rand(rm
->top_row
+wall_width
,
402 rm
->bottom_row
-wall_width
);
403 } while (!(dungeon
[*row
][*col
] & (VERTWALL
| TUNNEL
)));
406 if (rm
->is_room
& R_ROOM
) {
407 dungeon
[*row
][*col
] = DOOR
;
409 if ((cur_level
> 2) && rand_percent(HIDE_PERCENT
)) {
410 dungeon
[*row
][*col
] |= HIDDEN
;
412 rm
->doors
[dir
/2].door_row
= *row
;
413 rm
->doors
[dir
/2].door_col
= *col
;
417 draw_simple_passage(row1
, col1
, row2
, col2
, dir
)
418 short row1
, col1
, row2
, col2
, dir
;
422 if ((dir
== LEFT
) || (dir
== RIGHT
)) {
427 middle
= get_rand(col1
+1, col2
-1);
428 for (i
= col1
+1; i
!= middle
; i
++) {
429 dungeon
[row1
][i
] = TUNNEL
;
431 for (i
= row1
; i
!= row2
; i
+= (row1
> row2
) ? -1 : 1) {
432 dungeon
[i
][middle
] = TUNNEL
;
434 for (i
= middle
; i
!= col2
; i
++) {
435 dungeon
[row2
][i
] = TUNNEL
;
442 middle
= get_rand(row1
+1, row2
-1);
443 for (i
= row1
+1; i
!= middle
; i
++) {
444 dungeon
[i
][col1
] = TUNNEL
;
446 for (i
= col1
; i
!= col2
; i
+= (col1
> col2
) ? -1 : 1) {
447 dungeon
[middle
][i
] = TUNNEL
;
449 for (i
= middle
; i
!= row2
; i
++) {
450 dungeon
[i
][col2
] = TUNNEL
;
453 if (rand_percent(HIDE_PERCENT
)) {
454 hide_boxed_passage(row1
, col1
, row2
, col2
, 1);
459 same_row(room1
, room2
)
462 return((room1
/ 3) == (room2
/ 3));
466 same_col(room1
, room2
)
469 return((room1
% 3) == (room2
% 3));
480 start
= get_rand(0, (MAXROOMS
-1));
481 maze_percent
= (cur_level
* 5) / 4;
483 if (cur_level
> 15) {
484 maze_percent
+= cur_level
;
486 for (i
= 0; i
< MAXROOMS
; i
++) {
487 j
= ((start
+ i
) % MAXROOMS
);
488 if (rooms
[j
].is_room
& R_NOTHING
) {
489 if (rand_percent(maze_percent
)) {
490 rooms
[j
].is_room
= R_MAZE
;
491 make_maze(get_rand(rooms
[j
].top_row
+1, rooms
[j
].bottom_row
-1),
492 get_rand(rooms
[j
].left_col
+1, rooms
[j
].right_col
-1),
493 rooms
[j
].top_row
, rooms
[j
].bottom_row
,
494 rooms
[j
].left_col
, rooms
[j
].right_col
);
495 hide_boxed_passage(rooms
[j
].top_row
, rooms
[j
].left_col
,
496 rooms
[j
].bottom_row
, rooms
[j
].right_col
,
513 for (i
= 0; i
< MAXROOMS
; i
++) {
514 rn
= random_rooms
[i
];
515 if ((rooms
[rn
].is_room
& R_NOTHING
) ||
516 ((rooms
[rn
].is_room
& R_CROSS
) && coin_toss())) {
520 if (r_de
!= NO_ROOM
) {
526 fill_it(rn
, 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(rn
, offsets
, srow
, scol
)
590 const short *offsets
;
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(rn
, row
, col
, mask
)
631 for (i
= rooms
[rn
].top_row
; i
<= rooms
[rn
].bottom_row
; i
++) {
632 for (j
= rooms
[rn
].left_col
; j
<= rooms
[rn
].right_col
; j
++) {
633 if (dungeon
[i
][j
] & mask
) {
644 make_maze(r
, c
, tr
, br
, lc
, rc
)
645 short r
, c
, tr
, br
, lc
, rc
;
655 dungeon
[r
][c
] = TUNNEL
;
657 if (rand_percent(20)) {
658 for (i
= 0; i
< 10; i
++) {
664 swap(dirs
[t1
], dirs
[t2
]);
667 for (i
= 0; i
< 4; i
++) {
671 (dungeon
[r
-1][c
] != TUNNEL
) &&
672 (dungeon
[r
-1][c
-1] != TUNNEL
) &&
673 (dungeon
[r
-1][c
+1] != TUNNEL
) &&
674 (dungeon
[r
-2][c
] != TUNNEL
)) {
675 make_maze((r
-1), c
, tr
, br
, lc
, rc
);
680 (dungeon
[r
+1][c
] != TUNNEL
) &&
681 (dungeon
[r
+1][c
-1] != TUNNEL
) &&
682 (dungeon
[r
+1][c
+1] != TUNNEL
) &&
683 (dungeon
[r
+2][c
] != TUNNEL
)) {
684 make_maze((r
+1), c
, tr
, br
, lc
, rc
);
689 (dungeon
[r
][c
-1] != TUNNEL
) &&
690 (dungeon
[r
-1][c
-1] != TUNNEL
) &&
691 (dungeon
[r
+1][c
-1] != TUNNEL
) &&
692 (dungeon
[r
][c
-2] != TUNNEL
)) {
693 make_maze(r
, (c
-1), tr
, br
, lc
, rc
);
698 (dungeon
[r
][c
+1] != TUNNEL
) &&
699 (dungeon
[r
-1][c
+1] != TUNNEL
) &&
700 (dungeon
[r
+1][c
+1] != TUNNEL
) &&
701 (dungeon
[r
][c
+2] != TUNNEL
)) {
702 make_maze(r
, (c
+1), tr
, br
, lc
, rc
);
710 hide_boxed_passage(row1
, col1
, row2
, col2
, n
)
711 short row1
, col1
, row2
, col2
, n
;
714 short row
, col
, row_cut
, col_cut
;
727 if ((w
>= 5) || (h
>= 5)) {
728 row_cut
= ((h
>= 2) ? 1 : 0);
729 col_cut
= ((w
>= 2) ? 1 : 0);
731 for (i
= 0; i
< n
; i
++) {
732 for (j
= 0; j
< 10; j
++) {
733 row
= get_rand(row1
+ row_cut
, row2
- row_cut
);
734 col
= get_rand(col1
+ col_cut
, col2
- col_cut
);
735 if (dungeon
[row
][col
] == TUNNEL
) {
736 dungeon
[row
][col
] |= HIDDEN
;
747 short nr
; /* try not to put in this room */
749 short rn
= nr
, misses
;
752 for (misses
= 0; ((misses
< 2) && (rn
== nr
)); misses
++) {
753 gr_row_col(&row
, &col
, (FLOOR
| TUNNEL
| OBJECT
| STAIRS
));
754 rn
= get_room_number(row
, col
);
759 if (dungeon
[rogue
.row
][rogue
.col
] & TUNNEL
) {
764 if (cur_room
!= PASSAGE
) {
765 light_up_room(cur_room
);
767 light_passage(rogue
.row
, rogue
.col
);
769 rn
= get_room_number(rogue
.row
, rogue
.col
);
770 wake_room(rn
, 1, rogue
.row
, rogue
.col
);
771 if (new_level_message
) {
772 messagef(0, "%s", new_level_message
);
773 new_level_message
= NULL
;
775 mvaddch(rogue
.row
, rogue
.col
, rogue
.fchar
);
784 if (dungeon
[rogue
.row
][rogue
.col
] & STAIRS
) {
786 messagef(0, "you're floating in the air!");
791 messagef(0, "I see no way down");
799 if (!(dungeon
[rogue
.row
][rogue
.col
] & STAIRS
)) {
800 messagef(0, "I see no way up");
804 messagef(0, "your way is magically blocked");
808 new_level_message
= "you feel a wrenching sensation in your gut";
809 if (cur_level
== 1) {
819 add_exp(e
, promotion
)
826 rogue
.exp_points
+= e
;
828 if (rogue
.exp_points
>= level_points
[rogue
.exp
-1]) {
829 new_exp
= get_exp_level(rogue
.exp_points
);
830 if (rogue
.exp_points
> MAX_EXP
) {
831 rogue
.exp_points
= MAX_EXP
+ 1;
833 for (i
= rogue
.exp
+1; i
<= new_exp
; i
++) {
834 messagef(0, "welcome to level %d", i
);
837 rogue
.hp_current
+= hp
;
841 print_stats(STAT_HP
| STAT_EXP
);
844 print_stats(STAT_EXP
);
854 for (i
= 0; i
< (MAX_EXP_LEVEL
- 1); i
++) {
855 if (level_points
[i
] > e
) {
867 hp
= (wizard
? 10 : get_rand(3, 10));
875 float effective_average
;
877 if (rogue
.exp
== 1) {
878 real_average
= effective_average
= 0.00;
880 real_average
= (float)
881 ((rogue
.hp_max
- extra_hp
- INIT_HP
) + less_hp
) / (rogue
.exp
- 1);
882 effective_average
= (float)(rogue
.hp_max
- INIT_HP
) / (rogue
.exp
- 1);
885 messagef(0, "R-Hp: %.2f, E-Hp: %.2f (!: %d, V: %d)", real_average
,
886 effective_average
, extra_hp
, less_hp
);
895 for (i
= 0; i
< (3 * MAXROOMS
); i
++) {
897 x
= get_rand(0, (MAXROOMS
-1));
898 y
= get_rand(0, (MAXROOMS
-1));
900 swap(random_rooms
[x
], random_rooms
[y
]);