ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/snake.C
Revision: 1.2
Committed: Sun Sep 10 16:06:37 2006 UTC (17 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.1: +78 -56 lines
Log Message:
indent

File Contents

# User Rev Content
1 elmex 1.1
2    
3     /* peterm@langmuir.eecs.berkeley.edu: this function generates a random
4     snake-type layout.
5    
6     input: xsize, ysize;
7     output: a char** array with # and . for closed and open respectively.
8    
9     a char value of 0 represents a blank space: a '#' is
10     a wall.
11    
12     */
13    
14    
15     #include <stdio.h>
16     #include <global.h>
17     #include <time.h>
18    
19    
20    
21    
22 root 1.2 char **
23     make_snake_layout (int xsize, int ysize, int options)
24     {
25     int i, j;
26 elmex 1.1
27     /* allocate that array, set it up */
28 root 1.2 char **maze = (char **) calloc (sizeof (char *), xsize);
29    
30     for (i = 0; i < xsize; i++)
31     {
32     maze[i] = (char *) calloc (sizeof (char), ysize);
33     }
34 elmex 1.1
35     /* write the outer walls */
36 root 1.2 for (i = 0; i < xsize; i++)
37     maze[i][0] = maze[i][ysize - 1] = '#';
38     for (j = 0; j < ysize; j++)
39     maze[0][j] = maze[xsize - 1][j] = '#';
40 elmex 1.1
41     /* Bail out if the size is too small to make a snake. */
42 root 1.2 if (xsize < 8 || ysize < 8)
43     return maze;
44 elmex 1.1
45     /* decide snake orientation--vertical or horizontal , and
46     make the walls and place the doors. */
47    
48 root 1.2 if (RANDOM () % 2)
49     { /* vertical orientation */
50     int n_walls = RANDOM () % ((xsize - 5) / 3) + 1;
51     int spacing = xsize / (n_walls + 1);
52     int orientation = 1;
53    
54     for (i = spacing; i < xsize - 3; i += spacing)
55     {
56     if (orientation)
57     {
58     for (j = 1; j < ysize - 2; j++)
59     {
60     maze[i][j] = '#';
61     }
62     maze[i][j] = 'D';
63     }
64     else
65     {
66     for (j = 2; j < ysize; j++)
67     {
68     maze[i][j] = '#';
69     }
70     maze[i][1] = 'D';
71     }
72     orientation ^= 1; /* toggle the value of orientation */
73 elmex 1.1 }
74     }
75 root 1.2 else
76     { /* horizontal orientation */
77     int n_walls = RANDOM () % ((ysize - 5) / 3) + 1;
78     int spacing = ysize / (n_walls + 1);
79     int orientation = 1;
80    
81     for (i = spacing; i < ysize - 3; i += spacing)
82     {
83     if (orientation)
84     {
85     for (j = 1; j < xsize - 2; j++)
86     {
87     maze[j][i] = '#';
88     }
89     maze[j][i] = 'D';
90     }
91     else
92     {
93     for (j = 2; j < xsize; j++)
94     {
95     maze[j][i] = '#';
96     }
97     maze[1][i] = 'D';
98     }
99     orientation ^= 1; /* toggle the value of orientation */
100 elmex 1.1 }
101     }
102 root 1.2
103 elmex 1.1 /* place the exit up/down */
104 root 1.2 if (RANDOM () % 2)
105     {
106     maze[1][1] = '<';
107     maze[xsize - 2][ysize - 2] = '>';
108     }
109 elmex 1.1 else
110 root 1.2 {
111     maze[1][1] = '>';
112     maze[xsize - 2][ysize - 2] = '<';
113     }
114    
115    
116 elmex 1.1 return maze;
117     }