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

Comparing deliantra/server/random_maps/room_gen_spiral.C (file contents):
Revision 1.1 by elmex, Sun Aug 13 17:16:03 2006 UTC vs.
Revision 1.6 by pippijn, Sat Jan 6 14:42:30 2007 UTC

1
1/* 2/*
2 CrossFire, A Multiplayer game for X-windows 3 CrossFire, A Multiplayer game for X-windows
3 4
5 Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
4 Copyright (C) 1994 Mark Wedel 6 Copyright (C) 1994 Mark Wedel
5 Copyright (C) 1992 Frank Tore Johansen 7 Copyright (C) 1992 Frank Tore Johansen
6 8
7 This program is free software; you can redistribute it and/or modify 9 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by 10 it under the terms of the GNU General Public License as published by
30 32
31*/ 33*/
32#include <global.h> 34#include <global.h>
33#include <random_map.h> 35#include <random_map.h>
34 36
35#define RANDOM_OPTIONS 0 /* Pick random options below */ 37#define RANDOM_OPTIONS 0 /* Pick random options below */
36#define REGULAR_SPIRAL 1 /* Regular spiral--distance increases constantly*/ 38#define REGULAR_SPIRAL 1 /* Regular spiral--distance increases constantly */
37#define FINE_SPIRAL 2 /* uses the min. separation: most coiling */ 39#define FINE_SPIRAL 2 /* uses the min. separation: most coiling */
38#define FIT_SPIRAL 4 /* scale to a rectangular region, not square */ 40#define FIT_SPIRAL 4 /* scale to a rectangular region, not square */
39#define MAX_SPIRAL_OPT 8 /* this should be 2x the last real option */ 41#define MAX_SPIRAL_OPT 8 /* this should be 2x the last real option */
40#include <math.h> 42#include <math.h>
41 43
42#ifndef MIN 44#ifndef MIN
43#define MIN(x,y) (((x)<(y))? (x):(y)) 45# define MIN(x,y) (((x)<(y))? (x):(y))
44#endif 46#endif
45#ifndef MAX 47#ifndef MAX
46#define MAX(x,y) (((x)<(y))? (y):(x)) 48# define MAX(x,y) (((x)<(y))? (y):(x))
47#endif 49#endif
48 50
49#define MINDIST 3 51#define MINDIST 3
50 52
51#define MAX_FINE .454545 53#define MAX_FINE .454545
52 54
53extern int surround_check(char **maze,int i, int j, int xsize, int ysize); 55extern int surround_check (char **maze, int i, int j, int xsize, int ysize);
54 56
57char **
55char **map_gen_spiral(int xsize, int ysize, int option) { 58map_gen_spiral (int xsize, int ysize, int option)
59{
56 int i,j; 60 int i, j;
57 float parm=0; 61 float parm = 0;
58 float x=0,y=0; 62 float x = 0, y = 0;
59 int ic,jc; 63 int ic, jc;
60 float SizeX,SizeY; 64 float SizeX, SizeY;
61 float xscale,yscale; 65 float xscale, yscale;
62 66
63 /* allocate that array, set it up */ 67 /* allocate that array, set it up */
64 char **maze = (char **)calloc(sizeof(char*),xsize); 68 char **maze = (char **) calloc (sizeof (char *), xsize);
69
65 for(i=0;i<xsize;i++) { 70 for (i = 0; i < xsize; i++)
71 {
66 maze[i] = (char *) calloc(sizeof(char),ysize); 72 maze[i] = (char *) calloc (sizeof (char), ysize);
67 } 73 }
68 74
69 /* slightly easier to fill and then cut */ 75 /* slightly easier to fill and then cut */
70 for(i=0;i<xsize;i++) for(j=0;j<ysize;j++) maze[i][j] = '#'; 76 for (i = 0; i < xsize; i++)
77 for (j = 0; j < ysize; j++)
78 maze[i][j] = '#';
71 79
72 ic = xsize/2; 80 ic = xsize / 2;
73 jc = ysize/2; 81 jc = ysize / 2;
74 SizeX = xsize/2 - 2; 82 SizeX = xsize / 2 - 2;
75 SizeY = ysize/2 -2; 83 SizeY = ysize / 2 - 2;
76 84
77 /* select random options if necessary */ 85 /* select random options if necessary */
78 if(option==0) { 86 if (option == 0)
87 {
79 option=RANDOM()%MAX_SPIRAL_OPT; 88 option = RANDOM () % MAX_SPIRAL_OPT;
80 } 89 }
81 90
82 /* the order in which these are evaluated matters*/ 91 /* the order in which these are evaluated matters */
83 92
84 /* the following two are mutually exclusive. 93 /* the following two are mutually exclusive.
85 pick one if they're both set. */ 94 pick one if they're both set. */
86 if((option & REGULAR_SPIRAL) && (option & FIT_SPIRAL)) 95 if ((option & REGULAR_SPIRAL) && (option & FIT_SPIRAL))
87 { 96 {
88 /* unset REGULAR_SPIRAL half the time */ 97 /* unset REGULAR_SPIRAL half the time */
89 if(RANDOM()%2 && (option & REGULAR_SPIRAL)) 98 if (RANDOM () % 2 && (option & REGULAR_SPIRAL))
90 option -= REGULAR_SPIRAL; 99 option -= REGULAR_SPIRAL;
91 else 100 else
92 option -= FIT_SPIRAL; 101 option -= FIT_SPIRAL;
93 } 102 }
94 103
95 xscale=yscale=MAX_FINE; /* fine spiral */ 104 xscale = yscale = MAX_FINE; /* fine spiral */
96 105
97 /* choose the spiral pitch */ 106 /* choose the spiral pitch */
98 if(! (option & FINE_SPIRAL) ) { 107 if (!(option & FINE_SPIRAL))
108 {
99 float pitch = (RANDOM() %5)/10. + 10./22.; 109 float pitch = (RANDOM () % 5) / 10. + 10. / 22.;
110
100 xscale=yscale=pitch; 111 xscale = yscale = pitch;
101 } 112 }
102 113
103 if((option & FIT_SPIRAL) && (xsize!=ysize) ) { 114 if ((option & FIT_SPIRAL) && (xsize != ysize))
115 {
116 if (xsize > ysize)
104 if(xsize > ysize) xscale *= (float)xsize/(float)ysize; 117 xscale *= (float) xsize / (float) ysize;
118 else
105 else yscale *= (float)ysize/(float)xsize; 119 yscale *= (float) ysize / (float) xsize;
106 } 120 }
107 121
108 if(option & REGULAR_SPIRAL) { 122 if (option & REGULAR_SPIRAL)
123 {
109 float scale = MIN(xscale,yscale); 124 float scale = MIN (xscale, yscale);
125
110 xscale=yscale=scale; 126 xscale = yscale = scale;
111 } 127 }
112 128
113 /* cut out the spiral */ 129 /* cut out the spiral */
114 while( (fabs(x) < SizeX) && (fabs(y) < SizeY) ) 130 while ((fabs (x) < SizeX) && (fabs (y) < SizeY))
115 { 131 {
116 x = parm * cos(parm)*xscale; 132 x = parm * cos (parm) * xscale;
117 y = parm * sin(parm)*yscale; 133 y = parm * sin (parm) * yscale;
118 maze[(int)(ic + x )][(int)(jc + y )]='\0'; 134 maze[(int) (ic + x)][(int) (jc + y)] = '\0';
119 parm+=0.01; 135 parm += 0.01;
120 }; 136 };
121 137
122maze[(int)(ic + x+0.5)][(int)(jc + y+0.5)]='<'; 138 maze[(int) (ic + x + 0.5)][(int) (jc + y + 0.5)] = '<';
123 139
124 140
125 /* cut out the center in a 2x2 and place the center and downexit */ 141 /* cut out the center in a 2x2 and place the center and downexit */
126 maze[ic][jc+1]='>'; 142 maze[ic][jc + 1] = '>';
127 maze[ic][jc]='C'; 143 maze[ic][jc] = 'C';
128 144
129 145
130 return maze; 146 return maze;
131} 147}
132 148
133/* the following function connects disjoint spirals which may 149/* the following function connects disjoint spirals which may
134 result from the symmetrization process. */ 150 result from the symmetrization process. */
151void
135void connect_spirals(int xsize,int ysize,int sym, char **layout) { 152connect_spirals (int xsize, int ysize, int sym, char **layout)
153{
136 154
137 int i,j,ic=xsize/2,jc=ysize/2; 155 int i, j, ic = xsize / 2, jc = ysize / 2;
138 156
139 if(sym==X_SYM) { 157 if (sym == SYMMETRY_X)
158 {
140 layout[ic][jc] = 0; 159 layout[ic][jc] = 0;
141 /* go left from map center */ 160 /* go left from map center */
142 for(i=ic-1,j=jc; i>0 && layout[i][j]=='#' ;i--) 161 for (i = ic - 1, j = jc; i > 0 && layout[i][j] == '#'; i--)
143 layout[i][j]=0; 162 layout[i][j] = 0;
144 /* go right */ 163 /* go right */
145 for(i=ic+1,j=jc; i<xsize-1 && layout[i][j]=='#' ;i++) 164 for (i = ic + 1, j = jc; i < xsize - 1 && layout[i][j] == '#'; i++)
146 layout[i][j]=0; 165 layout[i][j] = 0;
147 } 166 }
148 167
149 if(sym==Y_SYM) { 168 if (sym == SYMMETRY_Y)
169 {
150 170
151 layout[ic][jc] = 0; 171 layout[ic][jc] = 0;
152 /* go up */ 172 /* go up */
153 for(i=ic,j=jc-1; j>0 && layout[i][j]=='#' ;j--) 173 for (i = ic, j = jc - 1; j > 0 && layout[i][j] == '#'; j--)
154 layout[i][j]=0; 174 layout[i][j] = 0;
155 /* go down */ 175 /* go down */
156 for(i=ic,j=jc+1; j<ysize-1 && layout[i][j]=='#' ;j++) 176 for (i = ic, j = jc + 1; j < ysize - 1 && layout[i][j] == '#'; j++)
157 layout[i][j]=0; 177 layout[i][j] = 0;
158 } 178 }
159 179
160 if(sym==XY_SYM) { 180 if (sym == SYMMETRY_XY)
181 {
161 /* go left from map center */ 182 /* go left from map center */
162 layout[ic][jc/2]=0; 183 layout[ic][jc / 2] = 0;
163 layout[ic/2][jc]=0; 184 layout[ic / 2][jc] = 0;
164 layout[ic][jc/2+jc]=0; 185 layout[ic][jc / 2 + jc] = 0;
165 layout[ic/2+ic][jc]=0; 186 layout[ic / 2 + ic][jc] = 0;
166 for(i=ic-1,j=jc/2; i>0 && layout[i][j]=='#' ;i--) { 187 for (i = ic - 1, j = jc / 2; i > 0 && layout[i][j] == '#'; i--)
188 {
167 layout[i][j + jc]=0; 189 layout[i][j + jc] = 0;
168 layout[i][j]=0; 190 layout[i][j] = 0;
169 } 191 }
170 /* go right */ 192 /* go right */
171 for(i=ic+1,j=jc/2; i<xsize-1 && layout[i][j]=='#' ;i++) { 193 for (i = ic + 1, j = jc / 2; i < xsize - 1 && layout[i][j] == '#'; i++)
194 {
172 layout[i][j+jc]=0; 195 layout[i][j + jc] = 0;
173 layout[i][j]=0; 196 layout[i][j] = 0;
174 } 197 }
175 /* go up */ 198 /* go up */
176 for(i=ic/2,j=jc-1; j>0 && layout[i][j]=='#' ;j--) { 199 for (i = ic / 2, j = jc - 1; j > 0 && layout[i][j] == '#'; j--)
200 {
177 layout[i][j]=0; 201 layout[i][j] = 0;
178 layout[i+ic][j]=0; 202 layout[i + ic][j] = 0;
179 } 203 }
180 /* go down */ 204 /* go down */
181 for(i=ic/2,j=jc+1; j<ysize-1 && layout[i][j]=='#' ;j++) { 205 for (i = ic / 2, j = jc + 1; j < ysize - 1 && layout[i][j] == '#'; j++)
206 {
182 layout[i][j]=0; 207 layout[i][j] = 0;
183 layout[i+ic][j]=0; 208 layout[i + ic][j] = 0;
184 } 209 }
185 210
186 } 211 }
187 212
188 /* get rid of bad doors. */ 213 /* get rid of bad doors. */
189 for(i=0;i<xsize;i++) 214 for (i = 0; i < xsize; i++)
190 for(j=0;j<ysize;j++) { 215 for (j = 0; j < ysize; j++)
191 if(layout[i][j]=='D') { /* remove bad door. */ 216 {
217 if (layout[i][j] == 'D')
218 { /* remove bad door. */
192 int si = surround_check(layout,i,j,xsize,ysize); 219 int si = surround_check (layout, i, j, xsize, ysize);
220
193 if(si!=3 && si!=12) { 221 if (si != 3 && si != 12)
222 {
194 layout[i][j]=0; 223 layout[i][j] = 0;
195 /* back up and recheck any nearby doors */ 224 /* back up and recheck any nearby doors */
196 i=0;j=0; 225 i = 0;
226 j = 0;
227 }
197 } 228 }
198 } 229 }
199 } 230
200
201 231
202 232
203} 233}
204

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines