ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/snake.C
Revision: 1.5
Committed: Thu Jan 18 19:42:10 2007 UTC (17 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.4: +2 -2 lines
Log Message:
just experimenting

File Contents

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