ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/snake.C
(Generate patch)

Comparing deliantra/server/random_maps/snake.C (file contents):
Revision 1.1 by elmex, Sun Aug 13 17:16:03 2006 UTC vs.
Revision 1.4 by root, Sun Dec 31 19:02:24 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines