|
|
1 | |
1 | /* |
2 | /* |
2 | * static char *rcsid_map_c = |
3 | * static char *rcsid_map_c = |
3 | * "$Id: square_spiral.C,v 1.1 2006/08/13 17:16:03 elmex Exp $"; |
4 | * "$Id: square_spiral.C,v 1.2 2006/09/10 16:06:37 root Exp $"; |
4 | */ |
5 | */ |
5 | |
6 | |
6 | /* |
7 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
8 | CrossFire, A Multiplayer game for X-windows |
8 | |
9 | |
… | |
… | |
48 | #include <random_map.h> |
49 | #include <random_map.h> |
49 | #include <sproto.h> |
50 | #include <sproto.h> |
50 | #include <rproto.h> |
51 | #include <rproto.h> |
51 | |
52 | |
52 | |
53 | |
53 | char **map_gen_onion(int xsize, int ysize, int option, int layers); |
54 | char **map_gen_onion (int xsize, int ysize, int option, int layers); |
54 | |
55 | |
55 | |
56 | |
56 | /* These are some helper functions which help with |
57 | /* These are some helper functions which help with |
57 | manipulating a centered onion and turning it into |
58 | manipulating a centered onion and turning it into |
58 | a square spiral */ |
59 | a square spiral */ |
… | |
… | |
60 | /* this starts from within a centered onion layer (or between two layers), |
61 | /* this starts from within a centered onion layer (or between two layers), |
61 | and looks up until it finds a wall, and then looks right until it |
62 | and looks up until it finds a wall, and then looks right until it |
62 | finds a vertical wall, i.e., the corner. It sets cx and cy to that. |
63 | finds a vertical wall, i.e., the corner. It sets cx and cy to that. |
63 | it also starts from cx and cy. */ |
64 | it also starts from cx and cy. */ |
64 | |
65 | |
|
|
66 | void |
65 | void find_top_left_corner(char **maze,int *cx, int *cy) { |
67 | find_top_left_corner (char **maze, int *cx, int *cy) |
|
|
68 | { |
66 | |
69 | |
67 | (*cy)--; |
70 | (*cy)--; |
68 | /* find the top wall. */ |
71 | /* find the top wall. */ |
69 | while(maze[*cx][*cy]==0) (*cy)--; |
72 | while (maze[*cx][*cy] == 0) |
|
|
73 | (*cy)--; |
70 | /* proceed right until a corner is detected */ |
74 | /* proceed right until a corner is detected */ |
71 | while(maze[*cx][*cy+1]==0) (*cx)++; |
75 | while (maze[*cx][*cy + 1] == 0) |
72 | |
76 | (*cx)++; |
|
|
77 | |
73 | /* cx and cy should now be the top-right corner of the onion layer */ |
78 | /* cx and cy should now be the top-right corner of the onion layer */ |
74 | } |
79 | } |
75 | |
80 | |
76 | |
81 | |
|
|
82 | char ** |
77 | char **make_square_spiral_layout(int xsize, int ysize,int options) { |
83 | make_square_spiral_layout (int xsize, int ysize, int options) |
|
|
84 | { |
78 | int i,j; |
85 | int i, j; |
79 | int cx,cy; |
86 | int cx, cy; |
80 | int tx,ty; |
87 | int tx, ty; |
81 | |
88 | |
82 | /* generate and allocate a doorless, centered onion */ |
89 | /* generate and allocate a doorless, centered onion */ |
83 | char **maze = map_gen_onion(xsize,ysize,OPT_CENTERED | OPT_NO_DOORS,0); |
90 | char **maze = map_gen_onion (xsize, ysize, OPT_CENTERED | OPT_NO_DOORS, 0); |
84 | |
91 | |
85 | /* find the layout center. */ |
92 | /* find the layout center. */ |
86 | cx = 0; cy = 0; |
93 | cx = 0; |
|
|
94 | cy = 0; |
87 | for(i=0;i<xsize;i++) |
95 | for (i = 0; i < xsize; i++) |
88 | for(j=0;j<ysize;j++) { |
96 | for (j = 0; j < ysize; j++) |
|
|
97 | { |
89 | if(maze[i][j]=='C' ) { |
98 | if (maze[i][j] == 'C') |
|
|
99 | { |
90 | cx = i; cy=j; |
100 | cx = i; |
|
|
101 | cy = j; |
|
|
102 | } |
91 | } |
103 | } |
|
|
104 | tx = cx; |
|
|
105 | ty = cy; |
|
|
106 | while (1) |
|
|
107 | { |
|
|
108 | find_top_left_corner (maze, &tx, &ty); |
|
|
109 | |
|
|
110 | if (ty < 2 || tx < 2 || tx > xsize - 2 || ty > ysize - 2) |
|
|
111 | break; |
|
|
112 | make_wall (maze, tx, ty - 1, 1); /* make a vertical wall with a door */ |
|
|
113 | |
|
|
114 | maze[tx][ty - 1] = '#'; /* convert the door that make_wall puts here to a wall */ |
|
|
115 | maze[tx - 1][ty] = 'D'; /* make a doorway out of this layer */ |
|
|
116 | |
|
|
117 | /* walk left until we find the top-left corner */ |
|
|
118 | while ((tx > 2) && maze[tx - 1][ty]) |
|
|
119 | tx--; |
|
|
120 | |
|
|
121 | make_wall (maze, tx - 1, ty, 0); /* make a horizontal wall with a door */ |
|
|
122 | |
|
|
123 | /* walk down until we find the bottom-left corner */ |
|
|
124 | while (((ty + 1) < ysize) && maze[tx][ty + 1]) |
|
|
125 | ty++; |
|
|
126 | |
|
|
127 | make_wall (maze, tx, ty + 1, 1); /* make a vertical wall with a door */ |
|
|
128 | |
|
|
129 | /* walk rightuntil we find the bottom-right corner */ |
|
|
130 | while (((tx + 1) < xsize) && maze[tx + 1][ty]) |
|
|
131 | tx++; |
|
|
132 | |
|
|
133 | make_wall (maze, tx + 1, ty, 0); /* make a horizontal wall with a door */ |
|
|
134 | tx++; /* set up for next layer. */ |
92 | } |
135 | } |
93 | tx = cx; ty = cy; |
|
|
94 | while(1) { |
|
|
95 | find_top_left_corner(maze,&tx,&ty); |
|
|
96 | |
136 | |
97 | if(ty < 2 || tx < 2 || tx > xsize -2 || ty > ysize-2 ) break; |
|
|
98 | make_wall(maze,tx,ty-1,1); /* make a vertical wall with a door */ |
|
|
99 | |
|
|
100 | maze[tx][ty-1]='#'; /* convert the door that make_wall puts here to a wall */ |
|
|
101 | maze[tx-1][ty]='D';/* make a doorway out of this layer */ |
|
|
102 | |
|
|
103 | /* walk left until we find the top-left corner */ |
|
|
104 | while((tx>2) && maze[tx-1][ty]) tx--; |
|
|
105 | |
|
|
106 | make_wall(maze,tx-1,ty,0); /* make a horizontal wall with a door */ |
|
|
107 | |
|
|
108 | /* walk down until we find the bottom-left corner */ |
|
|
109 | while(((ty+1) < ysize) && maze[tx][ty+1] ) ty++; |
|
|
110 | |
|
|
111 | make_wall(maze,tx,ty+1,1); /* make a vertical wall with a door */ |
|
|
112 | |
|
|
113 | /* walk rightuntil we find the bottom-right corner */ |
|
|
114 | while(((tx + 1) < xsize) && maze[tx+1][ty]) tx++; |
|
|
115 | |
|
|
116 | make_wall(maze,tx+1,ty,0); /* make a horizontal wall with a door */ |
|
|
117 | tx++; /* set up for next layer. */ |
|
|
118 | } |
|
|
119 | |
|
|
120 | /* place the exits. */ |
137 | /* place the exits. */ |
121 | |
138 | |
122 | if(RANDOM() %2) { |
139 | if (RANDOM () % 2) |
|
|
140 | { |
123 | maze[cx][cy]='>'; |
141 | maze[cx][cy] = '>'; |
124 | maze[xsize-2][1]='<'; |
142 | maze[xsize - 2][1] = '<'; |
125 | } |
143 | } |
126 | else { |
144 | else |
|
|
145 | { |
127 | maze[cx][cy]='<'; |
146 | maze[cx][cy] = '<'; |
128 | maze[xsize-2][1]='>'; |
147 | maze[xsize - 2][1] = '>'; |
129 | } |
|
|
130 | |
148 | } |
|
|
149 | |
131 | return maze; |
150 | return maze; |
132 | } |
151 | } |
133 | |
|
|
134 | |
|
|
135 | |
|
|