ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/square_spiral.C
Revision: 1.12
Committed: Fri Apr 11 21:09:53 2008 UTC (16 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.11: +6 -17 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.11 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 pippijn 1.7 *
4 root 1.12 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.10 * Copyright (©) 2001,2007 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992,2007 Frank Tore Johansen
7 pippijn 1.7 *
8 root 1.11 * Deliantra is free software: you can redistribute it and/or modify
9 pippijn 1.7 * it under the terms of the GNU General Public License as published by
10 root 1.10 * the Free Software Foundation, either version 3 of the License, or
11 pippijn 1.7 * (at your option) any later version.
12     *
13     * This program is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 root 1.10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 pippijn 1.7 * GNU General Public License for more details.
17     *
18     * You should have received a copy of the GNU General Public License
19 root 1.10 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20     *
21 root 1.11 * The authors can be reached via e-mail to <support@deliantra.net>
22 pippijn 1.7 */
23 elmex 1.1
24     /* peterm@langmuir.eecs.berkeley.edu: this function generates a random
25     snake-type layout.
26    
27     input: xsize, ysize;
28     output: a char** array with # and . for closed and open respectively.
29    
30     a char value of 0 represents a blank space: a '#' is
31     a wall.
32    
33     */
34    
35     #include <global.h>
36    
37 root 1.12 #include "random_map.h"
38     #include "rproto.h"
39 elmex 1.1
40     /* These are some helper functions which help with
41     manipulating a centered onion and turning it into
42     a square spiral */
43    
44     /* this starts from within a centered onion layer (or between two layers),
45     and looks up until it finds a wall, and then looks right until it
46     finds a vertical wall, i.e., the corner. It sets cx and cy to that.
47     it also starts from cx and cy. */
48    
49 root 1.2 void
50     find_top_left_corner (char **maze, int *cx, int *cy)
51     {
52 root 1.9 --(*cy);
53 elmex 1.1
54     /* find the top wall. */
55 root 1.2 while (maze[*cx][*cy] == 0)
56 root 1.9 --(*cy);
57    
58 elmex 1.1 /* proceed right until a corner is detected */
59 root 1.2 while (maze[*cx][*cy + 1] == 0)
60 root 1.9 ++(*cx);
61 root 1.2
62 elmex 1.1 /* cx and cy should now be the top-right corner of the onion layer */
63     }
64    
65 root 1.12 Maze
66 root 1.2 make_square_spiral_layout (int xsize, int ysize, int options)
67     {
68     int i, j;
69     int cx, cy;
70     int tx, ty;
71 elmex 1.1
72     /* generate and allocate a doorless, centered onion */
73 root 1.12 Maze maze = map_gen_onion (xsize, ysize, RMOPT_CENTERED | RMOPT_NO_DOORS, 0);
74 elmex 1.1
75     /* find the layout center. */
76 root 1.2 cx = 0;
77     cy = 0;
78     for (i = 0; i < xsize; i++)
79     for (j = 0; j < ysize; j++)
80 root 1.9 if (maze[i][j] == 'C')
81     {
82     cx = i;
83     cy = j;
84     }
85    
86 root 1.2 tx = cx;
87     ty = cy;
88 root 1.9 for (;;)
89 root 1.2 {
90     find_top_left_corner (maze, &tx, &ty);
91    
92     if (ty < 2 || tx < 2 || tx > xsize - 2 || ty > ysize - 2)
93     break;
94 root 1.9
95 root 1.2 make_wall (maze, tx, ty - 1, 1); /* make a vertical wall with a door */
96    
97     maze[tx][ty - 1] = '#'; /* convert the door that make_wall puts here to a wall */
98     maze[tx - 1][ty] = 'D'; /* make a doorway out of this layer */
99 elmex 1.1
100 root 1.2 /* walk left until we find the top-left corner */
101 root 1.12 while ((tx > 2) && maze[tx - 1, ty])
102 root 1.2 tx--;
103 elmex 1.1
104 root 1.2 make_wall (maze, tx - 1, ty, 0); /* make a horizontal wall with a door */
105 elmex 1.1
106 root 1.2 /* walk down until we find the bottom-left corner */
107     while (((ty + 1) < ysize) && maze[tx][ty + 1])
108     ty++;
109 elmex 1.1
110 root 1.2 make_wall (maze, tx, ty + 1, 1); /* make a vertical wall with a door */
111 elmex 1.1
112 root 1.2 /* walk rightuntil we find the bottom-right corner */
113     while (((tx + 1) < xsize) && maze[tx + 1][ty])
114     tx++;
115 elmex 1.1
116 root 1.2 make_wall (maze, tx + 1, ty, 0); /* make a horizontal wall with a door */
117     tx++; /* set up for next layer. */
118     }
119 elmex 1.1
120     /* place the exits. */
121    
122 root 1.8 if (rndm (2))
123 root 1.2 {
124     maze[cx][cy] = '>';
125     maze[xsize - 2][1] = '<';
126     }
127     else
128     {
129     maze[cx][cy] = '<';
130     maze[xsize - 2][1] = '>';
131     }
132    
133 elmex 1.1 return maze;
134     }
135 root 1.9