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.2 by root, Sun Sep 10 16:06:37 2006 UTC vs.
Revision 1.9 by root, Tue Apr 15 03:00:24 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> 17void
16#include <global.h> 18make_snake_layout (Layout maze, int options)
17#include <time.h>
18
19
20
21
22char **
23make_snake_layout (int xsize, int ysize, int options)
24{ 19{
25 int i, j; 20 int i, j;
26 21
27 /* allocate that array, set it up */ 22 maze->clear ();
28 char **maze = (char **) calloc (sizeof (char *), xsize); 23 maze->border ();
29 24
30 for (i = 0; i < xsize; i++) 25 int xsize = maze->w;
31 { 26 int ysize = maze->h;
32 maze[i] = (char *) calloc (sizeof (char), ysize);
33 }
34
35 /* write the outer walls */
36 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 27
41 /* 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. */
42 if (xsize < 8 || ysize < 8) 29 if (xsize < 8 || ysize < 8)
43 return maze; 30 return;
44 31
45 /* decide snake orientation--vertical or horizontal , and 32 /* decide snake orientation--vertical or horizontal , and
46 make the walls and place the doors. */ 33 make the walls and place the doors. */
47 34
48 if (RANDOM () % 2) 35 if (rndm (2))
49 { /* vertical orientation */ 36 { /* vertical orientation */
50 int n_walls = RANDOM () % ((xsize - 5) / 3) + 1; 37 int n_walls = rndm (xsize - 5) / 3 + 1;
51 int spacing = xsize / (n_walls + 1); 38 int spacing = xsize / (n_walls + 1);
52 int orientation = 1; 39 int orientation = 1;
53 40
54 for (i = spacing; i < xsize - 3; i += spacing) 41 for (i = spacing; i < xsize - 3; i += spacing)
55 { 42 {
56 if (orientation) 43 if (orientation)
57 { 44 {
58 for (j = 1; j < ysize - 2; j++) 45 for (j = 1; j < ysize - 2; j++)
59 {
60 maze[i][j] = '#'; 46 maze[i][j] = '#';
61 } 47
62 maze[i][j] = 'D'; 48 maze[i][j] = 'D';
63 } 49 }
64 else 50 else
65 { 51 {
66 for (j = 2; j < ysize; j++) 52 for (j = 2; j < ysize; j++)
67 {
68 maze[i][j] = '#'; 53 maze[i][j] = '#';
69 } 54
70 maze[i][1] = 'D'; 55 maze[i][1] = 'D';
71 } 56 }
57
72 orientation ^= 1; /* toggle the value of orientation */ 58 orientation ^= 1; /* toggle the value of orientation */
73 } 59 }
74 } 60 }
75 else 61 else
76 { /* horizontal orientation */ 62 { /* horizontal orientation */
77 int n_walls = RANDOM () % ((ysize - 5) / 3) + 1; 63 int n_walls = rndm (ysize - 5) / 3 + 1;
78 int spacing = ysize / (n_walls + 1); 64 int spacing = ysize / (n_walls + 1);
79 int orientation = 1; 65 int orientation = 1;
80 66
81 for (i = spacing; i < ysize - 3; i += spacing) 67 for (i = spacing; i < ysize - 3; i += spacing)
82 { 68 {
83 if (orientation) 69 if (orientation)
84 { 70 {
85 for (j = 1; j < xsize - 2; j++) 71 for (j = 1; j < xsize - 2; j++)
86 {
87 maze[j][i] = '#'; 72 maze[j][i] = '#';
88 } 73
89 maze[j][i] = 'D'; 74 maze[j][i] = 'D';
90 } 75 }
91 else 76 else
92 { 77 {
93 for (j = 2; j < xsize; j++) 78 for (j = 2; j < xsize; j++)
94 {
95 maze[j][i] = '#'; 79 maze[j][i] = '#';
96 } 80
97 maze[1][i] = 'D'; 81 maze[1][i] = 'D';
98 } 82 }
83
99 orientation ^= 1; /* toggle the value of orientation */ 84 orientation ^= 1; /* toggle the value of orientation */
100 } 85 }
101 } 86 }
102 87
103 /* place the exit up/down */ 88 /* place the exit up/down */
104 if (RANDOM () % 2) 89 if (rndm (2))
105 { 90 {
106 maze[1][1] = '<'; 91 maze[1][1] = '<';
107 maze[xsize - 2][ysize - 2] = '>'; 92 maze[xsize - 2][ysize - 2] = '>';
108 } 93 }
109 else 94 else
110 { 95 {
111 maze[1][1] = '>'; 96 maze[1][1] = '>';
112 maze[xsize - 2][ysize - 2] = '<'; 97 maze[xsize - 2][ysize - 2] = '<';
113 } 98 }
99}
114 100
115
116 return maze;
117}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines