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.7 by root, Fri Apr 11 21:09:53 2008 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;
9a char value of 0 represents a blank space: a '#' is 8a char value of 0 represents a blank space: a '#' is
10a wall. 9a wall.
11 10
12*/ 11*/
13 12
13#include <global.h>
14#include "random_map.h"
15#include "rproto.h"
14 16
15#include <stdio.h> 17Maze
16#include <global.h> 18make_snake_layout (int xsize, int ysize, int options)
17#include <time.h> 19{
20 int i, j;
18 21
19 22 Maze maze (xsize, ysize);
20
21
22char **make_snake_layout(int xsize, int ysize,int options) {
23 int i,j;
24
25 /* allocate that array, set it up */
26 char **maze = (char **)calloc(sizeof(char*),xsize);
27 for(i=0;i<xsize;i++) {
28 maze[i] = (char *) calloc(sizeof(char),ysize);
29 }
30 23
31 /* write the outer walls */ 24 /* write the outer walls */
32 for(i=0;i<xsize;i++) 25 for (i = 0; i < xsize; i++) maze[i][0] = maze[i][ysize - 1] = '#';
33 maze[i][0] = maze[i][ysize-1] = '#'; 26 for (j = 0; j < ysize; j++) maze[0][j] = maze[xsize - 1][j] = '#';
34 for(j=0;j<ysize;j++)
35 maze[0][j] = maze[xsize-1][j] = '#';
36 27
37 /* Bail out if the size is too small to make a snake. */ 28 /* Bail out if the size is too small to make a snake. */
38 if(xsize < 8 || ysize < 8) return maze; 29 if (xsize < 8 || ysize < 8)
30 return maze;
39 31
40 /* decide snake orientation--vertical or horizontal , and 32 /* decide snake orientation--vertical or horizontal , and
41 make the walls and place the doors. */ 33 make the walls and place the doors. */
42 34
43 if(RANDOM()%2) { /* vertical orientation */ 35 if (rndm (2))
36 { /* vertical orientation */
44 int n_walls = RANDOM() % ((xsize - 5)/3) +1; 37 int n_walls = rndm (xsize - 5) / 3 + 1;
45 int spacing = xsize / (n_walls+1); 38 int spacing = xsize / (n_walls + 1);
46 int orientation=1; 39 int orientation = 1;
40
47 for(i=spacing;i<xsize-3;i+=spacing) { 41 for (i = spacing; i < xsize - 3; i += spacing)
42 {
48 if(orientation) { 43 if (orientation)
44 {
49 for(j=1;j<ysize-2;j++) { 45 for (j = 1; j < ysize - 2; j++)
50 maze[i][j] = '#'; 46 maze[i][j] = '#';
47
48 maze[i][j] = 'D';
49 }
50 else
51 {
52 for (j = 2; j < ysize; j++)
53 maze[i][j] = '#';
54
55 maze[i][1] = 'D';
56 }
57
58 orientation ^= 1; /* toggle the value of orientation */
51 } 59 }
52 maze[i][j] = 'D';
53 } 60 }
61 else
62 { /* horizontal orientation */
63 int n_walls = rndm (ysize - 5) / 3 + 1;
64 int spacing = ysize / (n_walls + 1);
65 int orientation = 1;
66
67 for (i = spacing; i < ysize - 3; i += spacing)
54 else { 68 {
55 for(j=2;j<ysize;j++) { 69 if (orientation)
70 {
71 for (j = 1; j < xsize - 2; j++)
56 maze[i][j] = '#'; 72 maze[j][i] = '#';
73
74 maze[j][i] = 'D';
75 }
76 else
77 {
78 for (j = 2; j < xsize; j++)
79 maze[j][i] = '#';
80
81 maze[1][i] = 'D';
82 }
83
84 orientation ^= 1; /* toggle the value of orientation */
57 } 85 }
58 maze[i][1] = 'D';
59 }
60 orientation ^= 1; /* toggle the value of orientation */
61 } 86 }
62 } 87
63 else { /* horizontal orientation */ 88 /* place the exit up/down */
64 int n_walls = RANDOM() % ((ysize - 5)/3) +1; 89 if (rndm (2))
65 int spacing = ysize / (n_walls+1); 90 {
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'; 91 maze[1][1] = '<';
79 } 92 maze[xsize - 2][ysize - 2] = '>';
80 orientation ^= 1; /* toggle the value of orientation */
81 } 93 }
82 }
83
84 /* place the exit up/down */
85 if(RANDOM() %2)
86 { maze[1][1] = '<'; maze[xsize-2][ysize-2]='>'; }
87 else 94 else
88 { maze[1][1] = '>'; maze[xsize-2][ysize-2]='<'; }
89 95 {
90 96 maze[1][1] = '>';
97 maze[xsize - 2][ysize - 2] = '<';
98 }
99
91 return maze; 100 return maze;
92} 101}
93 102
94
95

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines