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

Comparing deliantra/server/random_maps/wall.C (file contents):
Revision 1.2 by root, Tue Aug 29 08:01:36 2006 UTC vs.
Revision 1.13 by root, Sun Dec 31 19:02:24 2006 UTC

1/*
2 * static char *rcsid_wall_c =
3 * "$Id: wall.C,v 1.2 2006/08/29 08:01:36 root Exp $";
4 */
5 1
6/* 2/*
7 CrossFire, A Multiplayer game for X-windows 3 CrossFire, A Multiplayer game for X-windows
8 4
9 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
21 17
22 You should have received a copy of the GNU General Public License 18 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software 19 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 21
26 The authors can be reached via e-mail at crossfire-devel@real-time.com 22 The authors can be reached via e-mail at <crossfire@schmorp.de>
27*/ 23*/
28 24
29#include <global.h> 25#include <global.h>
30#include <random_map.h> 26#include <random_map.h>
31#include <rproto.h> 27#include <rproto.h>
34 30
35 31
36/* given a layout and a coordinate, tell me which squares up/down/right/left 32/* given a layout and a coordinate, tell me which squares up/down/right/left
37 are occupied. */ 33 are occupied. */
38 34
35int
39int surround_flag(char **layout,int i,int j,RMParms *RP){ 36surround_flag (char **layout, int i, int j, random_map_params *RP)
37{
40 /* 1 = wall to left, 38 /* 1 = wall to left,
41 2 = wall to right, 39 2 = wall to right,
42 4 = wall above 40 4 = wall above
43 8 = wall below */ 41 8 = wall below */
42 int surround_index = 0;
43
44 if ((i > 0) && layout[i - 1][j] != 0)
44 int surround_index = 0; 45 surround_index |= 1;
45 if((i > 0) && layout[i-1][j]!=0) surround_index |=1;
46 if((i < RP->Xsize-1) && layout[i+1][j]!=0) surround_index |=2; 46 if ((i < RP->Xsize - 1) && layout[i + 1][j] != 0)
47 if((j > 0) && layout[i][j-1]!=0) surround_index |=4; 47 surround_index |= 2;
48 if ((j > 0) && layout[i][j - 1] != 0)
49 surround_index |= 4;
48 if((j < RP->Ysize-1) && layout[i][j+1]!=0) surround_index |=8; 50 if ((j < RP->Ysize - 1) && layout[i][j + 1] != 0)
51 surround_index |= 8;
49 return surround_index; 52 return surround_index;
50} 53}
51 54
52 55
53/* like surround_flag, but only walls count. 56/* like surround_flag, but only walls count.
54 */ 57 */
55 58
59int
56int surround_flag2(char **layout,int i,int j,RMParms *RP){ 60surround_flag2 (char **layout, int i, int j, random_map_params *RP)
61{
57 /* 1 = wall to left, 62 /* 1 = wall to left,
58 2 = wall to right, 63 2 = wall to right,
59 4 = wall above 64 4 = wall above
60 8 = wall below */ 65 8 = wall below */
66 int surround_index = 0;
67
68 if ((i > 0) && layout[i - 1][j] == '#')
61 int surround_index = 0; 69 surround_index |= 1;
62 if((i > 0) && layout[i-1][j]=='#') surround_index |=1;
63 if((i < RP->Xsize-1) && layout[i+1][j]=='#') surround_index |=2; 70 if ((i < RP->Xsize - 1) && layout[i + 1][j] == '#')
64 if((j > 0) && layout[i][j-1]=='#') surround_index |=4; 71 surround_index |= 2;
72 if ((j > 0) && layout[i][j - 1] == '#')
73 surround_index |= 4;
65 if((j < RP->Ysize-1) && layout[i][j+1]=='#') surround_index |=8; 74 if ((j < RP->Ysize - 1) && layout[i][j + 1] == '#')
75 surround_index |= 8;
66 return surround_index; 76 return surround_index;
67} 77}
68 78
69 79
70/* like surround_flag, except it checks a map, not a layout. 80/* like surround_flag, except it checks a map, not a layout.
71 * Since this is part of the random map code, presumption 81 * Since this is part of the random map code, presumption
72 * is that this is not a tiled map. 82 * is that this is not a tiled map.
73 * What is considered blocking and not is somewhat hard coded. 83 * What is considered blocking and not is somewhat hard coded.
74 */ 84 */
85int
75int surround_flag3(mapstruct *map,sint16 i,sint16 j,RMParms *RP){ 86surround_flag3 (maptile *map, sint16 i, sint16 j, random_map_params *RP)
87{
76 /* 88 /*
77 * 1 = blocked to left, 89 * 1 = blocked to left,
78 * 2 = blocked to right, 90 * 2 = blocked to right,
79 * 4 = blocked above 91 * 4 = blocked above
80 * 8 = blocked below 92 * 8 = blocked below
81 */ 93 */
82 94
83 int surround_index = 0; 95 int surround_index = 0;
84 96
85 if((i > 0) && (GET_MAP_MOVE_BLOCK(map, i-1,j) & ~MOVE_BLOCK_DEFAULT)) 97 if ((i > 0) && (GET_MAP_MOVE_BLOCK (map, i - 1, j) & ~MOVE_BLOCK_DEFAULT))
86 surround_index |=1; 98 surround_index |= 1;
87 if((i < RP->Xsize-1) && (GET_MAP_MOVE_BLOCK(map, i+1,j) & ~MOVE_BLOCK_DEFAULT)) 99 if ((i < RP->Xsize - 1) && (GET_MAP_MOVE_BLOCK (map, i + 1, j) & ~MOVE_BLOCK_DEFAULT))
88 surround_index |=2; 100 surround_index |= 2;
89 if((j > 0) && (GET_MAP_MOVE_BLOCK(map, i,j-1) & ~MOVE_BLOCK_DEFAULT)) 101 if ((j > 0) && (GET_MAP_MOVE_BLOCK (map, i, j - 1) & ~MOVE_BLOCK_DEFAULT))
90 surround_index |=4; 102 surround_index |= 4;
91 if((j < RP->Ysize-1) && (GET_MAP_MOVE_BLOCK(map,i,j+1) & ~MOVE_BLOCK_DEFAULT)) 103 if ((j < RP->Ysize - 1) && (GET_MAP_MOVE_BLOCK (map, i, j + 1) & ~MOVE_BLOCK_DEFAULT))
92 surround_index |=8; 104 surround_index |= 8;
93 105
94 return surround_index; 106 return surround_index;
95} 107}
96 108
97/* like surround_flag2, except it checks a map, not a layout. */ 109/* like surround_flag2, except it checks a map, not a layout. */
98 110
99int surround_flag4(mapstruct *map,int i,int j,RMParms *RP){ 111int
112surround_flag4 (maptile *map, int i, int j, random_map_params *RP)
113{
100 /* 1 = blocked to left, 114 /* 1 = blocked to left,
101 2 = blocked to right, 115 2 = blocked to right,
102 4 = blocked above 116 4 = blocked above
103 8 = blocked below */ 117 8 = blocked below */
118 int surround_index = 0;
119
120 if ((i > 0) && wall_blocked (map, i - 1, j))
104 int surround_index = 0; 121 surround_index |= 1;
105
106 if((i > 0) && wall_blocked(map,i-1,j)) surround_index |=1;
107 if((i < RP->Xsize-1) && wall_blocked(map,i+1,j)) surround_index |=2; 122 if ((i < RP->Xsize - 1) && wall_blocked (map, i + 1, j))
123 surround_index |= 2;
108 if((j > 0) && wall_blocked(map,i,j-1)) surround_index |=4; 124 if ((j > 0) && wall_blocked (map, i, j - 1))
125 surround_index |= 4;
109 if((j < RP->Ysize-1) && wall_blocked(map,i,j+1)) surround_index |=8; 126 if ((j < RP->Ysize - 1) && wall_blocked (map, i, j + 1))
127 surround_index |= 8;
110 128
111 return surround_index; 129 return surround_index;
112} 130}
113 131
114/* takes a map and a layout, and puts walls in the map (picked from 132/* takes a map and a layout, and puts walls in the map (picked from
115 w_style) at '#' marks. */ 133 w_style) at '#' marks. */
116 134
135void
117void make_map_walls(mapstruct *map,char **layout, char *w_style,RMParms *RP) { 136make_map_walls (maptile *map, char **layout, char *w_style, random_map_params *RP)
137{
118 char styledirname[256]; 138 char styledirname[256];
119 char stylefilepath[256]; 139 char stylefilepath[256];
120 mapstruct *style_map=0; 140 maptile *style_map = 0;
121 object *the_wall; 141 object *the_wall;
122 142
123 /* get the style map */ 143 /* get the style map */
124 if(!strcmp(w_style,"none")) return; 144 if (!strcmp (w_style, "none"))
145 return;
125 sprintf(styledirname,"%s","/styles/wallstyles"); 146 sprintf (styledirname, "%s", "/styles/wallstyles");
126 sprintf(stylefilepath,"%s/%s",styledirname,w_style); 147 sprintf (stylefilepath, "%s/%s", styledirname, w_style);
127 style_map = find_style(styledirname,w_style,-1); 148 style_map = find_style (styledirname, w_style, -1);
128 if(style_map == 0) return; 149 if (style_map == 0)
150 return;
129 151
130 /* fill up the map with the given floor style */ 152 /* fill up the map with the given floor style */
131 if((the_wall=pick_random_object(style_map))!=NULL) { 153 if ((the_wall = pick_random_object (style_map)) != NULL)
154 {
132 int i,j; 155 int i, j;
133 char *cp; 156 char *cp;
134 int joinedwalls=0; 157 int joinedwalls = 0;
135 object *thiswall; 158 object *thiswall;
136 159
137 sprintf(RP->wall_name,"%s",the_wall->arch->name); 160 sprintf (RP->wall_name, "%s", &the_wall->arch->name);
138 if ((cp=strchr(RP->wall_name,'_'))!=NULL) { 161 if ((cp = strchr (RP->wall_name, '_')) != NULL)
162 {
139 *cp=0; 163 *cp = 0;
140 joinedwalls=1; 164 joinedwalls = 1;
141 } 165 }
142 166
143 for(i=0;i<RP->Xsize;i++) 167 for (i = 0; i < RP->Xsize; i++)
144 for(j=0;j<RP->Ysize;j++) { 168 for (j = 0; j < RP->Ysize; j++)
169 {
145 if(layout[i][j]=='#') { 170 if (layout[i][j] == '#')
171 {
146 if(joinedwalls) 172 if (joinedwalls)
147 thiswall=pick_joined_wall(the_wall,layout,i,j,RP); 173 thiswall = pick_joined_wall (the_wall, layout, i, j, RP);
148 else 174 else
149 thiswall=arch_to_object(the_wall->arch); 175 thiswall = arch_to_object (the_wall->arch);
150 thiswall->x = i; 176 thiswall->x = i;
151 thiswall->y = j; 177 thiswall->y = j;
152 thiswall->move_block = MOVE_ALL; 178 thiswall->move_block = MOVE_ALL;
153 insert_ob_in_map(thiswall,map,thiswall,INS_NO_MERGE | INS_NO_WALK_ON); 179 insert_ob_in_map (thiswall, map, thiswall, INS_NO_MERGE | INS_NO_WALK_ON);
154 } 180 }
155 } 181 }
156 } 182 }
157} 183}
158 184
159 185
160/* picks the right wall type for this square, to make it look nice, 186/* picks the right wall type for this square, to make it look nice,
161 and have everything nicely joined. It uses the layout. */ 187 and have everything nicely joined. It uses the layout. */
162 188
189object *
163object *pick_joined_wall(object *the_wall,char **layout,int i,int j,RMParms *RP) { 190pick_joined_wall (object *the_wall, char **layout, int i, int j, random_map_params *RP)
191{
164 /* 1 = wall to left, 192 /* 1 = wall to left,
165 2 = wall to right, 193 2 = wall to right,
166 4 = wall above 194 4 = wall above
167 8 = wall below */ 195 8 = wall below */
168 int surround_index=0; 196 int surround_index = 0;
169 int l; 197 int l;
170 char wall_name[64]; 198 char wall_name[64];
171 archetype *wall_arch=0; 199 archetype *wall_arch = 0;
172 200
173 strcpy(wall_name,the_wall->arch->name); 201 strcpy (wall_name, the_wall->arch->name);
174 202
175 /* conventionally, walls are named like this: 203 /* conventionally, walls are named like this:
176 wallname_wallcode, where wallcode indicates 204 wallname_wallcode, where wallcode indicates
177 a joinedness, and wallname is the wall. 205 a joinedness, and wallname is the wall.
178 this code depends on the convention for 206 this code depends on the convention for
179 finding the right wall. */ 207 finding the right wall. */
180 208
181 /* extract the wall name, which is the text up to the leading _ */ 209 /* extract the wall name, which is the text up to the leading _ */
182 for(l=0;l<64;l++) { 210 for (l = 0; l < 64; l++)
211 {
183 if(wall_name[l]=='_') { 212 if (wall_name[l] == '_')
213 {
184 wall_name[l] = 0; 214 wall_name[l] = 0;
185 break;
186 }
187 }
188
189 surround_index = surround_flag2(layout,i,j,RP);
190
191 switch(surround_index) {
192 case 0:
193 strcat(wall_name,"_0");
194 break; 215 break;
195 case 1: 216 }
217 }
218
219 surround_index = surround_flag2 (layout, i, j, RP);
220
221 switch (surround_index)
222 {
223 case 0:
196 strcat(wall_name,"_1_3"); 224 strcat (wall_name, "_0");
197 break; 225 break;
198 case 2: 226 case 1:
199 strcat(wall_name,"_1_4"); 227 strcat (wall_name, "_1_3");
200 break; 228 break;
201 case 3: 229 case 2:
202 strcat(wall_name,"_2_1_2"); 230 strcat (wall_name, "_1_4");
203 break; 231 break;
204 case 4: 232 case 3:
205 strcat(wall_name,"_1_2"); 233 strcat (wall_name, "_2_1_2");
206 break; 234 break;
207 case 5: 235 case 4:
208 strcat(wall_name,"_2_2_4"); 236 strcat (wall_name, "_1_2");
209 break; 237 break;
210 case 6: 238 case 5:
211 strcat(wall_name,"_2_2_1"); 239 strcat (wall_name, "_2_2_4");
212 break; 240 break;
213 case 7: 241 case 6:
214 strcat(wall_name,"_3_1"); 242 strcat (wall_name, "_2_2_1");
215 break; 243 break;
216 case 8: 244 case 7:
217 strcat(wall_name,"_1_1"); 245 strcat (wall_name, "_3_1");
218 break; 246 break;
219 case 9: 247 case 8:
220 strcat(wall_name,"_2_2_3"); 248 strcat (wall_name, "_1_1");
221 break; 249 break;
222 case 10: 250 case 9:
223 strcat(wall_name,"_2_2_2"); 251 strcat (wall_name, "_2_2_3");
224 break; 252 break;
225 case 11: 253 case 10:
226 strcat(wall_name,"_3_3"); 254 strcat (wall_name, "_2_2_2");
227 break; 255 break;
228 case 12: 256 case 11:
229 strcat(wall_name,"_2_1_1"); 257 strcat (wall_name, "_3_3");
230 break; 258 break;
231 case 13: 259 case 12:
232 strcat(wall_name,"_3_4"); 260 strcat (wall_name, "_2_1_1");
233 break; 261 break;
234 case 14: 262 case 13:
235 strcat(wall_name,"_3_2"); 263 strcat (wall_name, "_3_4");
236 break; 264 break;
237 case 15: 265 case 14:
238 strcat(wall_name,"_4"); 266 strcat (wall_name, "_3_2");
239 break; 267 break;
268 case 15:
269 strcat (wall_name, "_4");
270 break;
240 } 271 }
241 wall_arch = find_archetype(wall_name); 272 wall_arch = archetype::find (wall_name);
242 if(wall_arch) return arch_to_object(wall_arch);
243 else {
244 nroferrors--;
245 return arch_to_object(the_wall->arch);
246 }
247 273
248 274 return wall_arch ? arch_to_object (wall_arch) : arch_to_object (the_wall->arch);
249} 275}
250 276
251 277
252/* this takes a map, and changes an existing wall to match what's blocked 278/* this takes a map, and changes an existing wall to match what's blocked
253 * around it, counting only doors and walls as blocked. If insert_flag is 279 * around it, counting only doors and walls as blocked. If insert_flag is
254 * 1, it will go ahead and insert the wall into the map. If not, it 280 * 1, it will go ahead and insert the wall into the map. If not, it
255 * will only return the wall which would belong there, and doesn't 281 * will only return the wall which would belong there, and doesn't
256 * remove anything. It depends on the 282 * remove anything. It depends on the
257 * global, previously-set variable, "wall_name" 283 * global, previously-set variable, "wall_name"
258 */ 284 */
259 285
286object *
260object * retrofit_joined_wall(mapstruct *the_map,int i,int j,int insert_flag,RMParms *RP) { 287retrofit_joined_wall (maptile *the_map, int i, int j, int insert_flag, random_map_params *RP)
288{
261 /* 1 = wall to left, 289 /* 1 = wall to left,
262 * 2 = wall to right, 290 * 2 = wall to right,
263 * 4 = wall above 291 * 4 = wall above
264 * 8 = wall below 292 * 8 = wall below
265 */ 293 */
266 int surround_index=0; 294 int surround_index = 0;
267 int l; 295 int l;
268 object *the_wall=0; 296 object *the_wall = 0;
269 object *new_wall=0; 297 object *new_wall = 0;
270 archetype * wall_arch=0; 298 archetype *wall_arch = 0;
271 299
272 /* first find the wall */ 300 /* first find the wall */
273 for(the_wall = get_map_ob(the_map,i,j);the_wall!=NULL;the_wall=the_wall->above) 301 for (the_wall = GET_MAP_OB (the_map, i, j); the_wall != NULL; the_wall = the_wall->above)
274 if ((the_wall->move_type & MOVE_WALK) && the_wall->type!=EXIT && the_wall->type!=TELEPORTER) 302 if ((the_wall->move_type & MOVE_WALK) && the_wall->type != EXIT && the_wall->type != TELEPORTER)
275 break; 303 break;
276 304
277 305
278 /* if what we found is a door, don't remove it, set the_wall to NULL to 306 /* if what we found is a door, don't remove it, set the_wall to NULL to
279 * signal that later. 307 * signal that later.
280 */ 308 */
281 if(the_wall && (the_wall->type==DOOR || the_wall->type==LOCKED_DOOR) ) { 309 if (the_wall && (the_wall->type == DOOR || the_wall->type == LOCKED_DOOR))
310 {
282 the_wall=NULL; 311 the_wall = NULL;
283 /* if we're not supposed to insert a new wall where there wasn't one, 312 /* if we're not supposed to insert a new wall where there wasn't one,
284 * we've gotta leave. 313 * we've gotta leave.
285 */ 314 */
286 if(insert_flag==0) return 0; 315 if (insert_flag == 0)
316 return 0;
287 } 317 }
288 else if(the_wall==NULL) return NULL; 318 else if (the_wall == NULL)
319 return NULL;
289 320
290 /* canonicalize the wall name */ 321 /* canonicalize the wall name */
291 for(l=0;l<64;l++) { 322 for (l = 0; l < 64; l++)
323 {
292 if(RP->wall_name[l]=='_') { 324 if (RP->wall_name[l] == '_')
325 {
293 RP->wall_name[l] = 0; 326 RP->wall_name[l] = 0;
294 break; 327 break;
295 } 328 }
296 } 329 }
297 330
298 surround_index = surround_flag4(the_map,i,j,RP); 331 surround_index = surround_flag4 (the_map, i, j, RP);
299 /* This would be a lot cleaner to just us a lookup table, 332 /* This would be a lot cleaner to just us a lookup table,
300 * eg, wall_suffix[surround_index] 333 * eg, wall_suffix[surround_index]
301 */ 334 */
302 switch(surround_index) { 335 switch (surround_index)
336 {
303 case 0: 337 case 0:
304 strcat(RP->wall_name,"_0"); 338 strcat (RP->wall_name, "_0");
305 break; 339 break;
306 case 1: 340 case 1:
307 strcat(RP->wall_name,"_1_3"); 341 strcat (RP->wall_name, "_1_3");
308 break; 342 break;
309 case 2: 343 case 2:
310 strcat(RP->wall_name,"_1_4"); 344 strcat (RP->wall_name, "_1_4");
311 break; 345 break;
312 case 3: 346 case 3:
313 strcat(RP->wall_name,"_2_1_2"); 347 strcat (RP->wall_name, "_2_1_2");
314 break; 348 break;
315 case 4: 349 case 4:
316 strcat(RP->wall_name,"_1_2"); 350 strcat (RP->wall_name, "_1_2");
317 break; 351 break;
318 case 5: 352 case 5:
319 strcat(RP->wall_name,"_2_2_4"); 353 strcat (RP->wall_name, "_2_2_4");
320 break; 354 break;
321 case 6: 355 case 6:
322 strcat(RP->wall_name,"_2_2_1"); 356 strcat (RP->wall_name, "_2_2_1");
323 break; 357 break;
324 case 7: 358 case 7:
325 strcat(RP->wall_name,"_3_1"); 359 strcat (RP->wall_name, "_3_1");
326 break; 360 break;
327 case 8: 361 case 8:
328 strcat(RP->wall_name,"_1_1"); 362 strcat (RP->wall_name, "_1_1");
329 break; 363 break;
330 case 9: 364 case 9:
331 strcat(RP->wall_name,"_2_2_3"); 365 strcat (RP->wall_name, "_2_2_3");
332 break; 366 break;
333 case 10: 367 case 10:
334 strcat(RP->wall_name,"_2_2_2"); 368 strcat (RP->wall_name, "_2_2_2");
335 break; 369 break;
336 case 11: 370 case 11:
337 strcat(RP->wall_name,"_3_3"); 371 strcat (RP->wall_name, "_3_3");
338 break; 372 break;
339 case 12: 373 case 12:
340 strcat(RP->wall_name,"_2_1_1"); 374 strcat (RP->wall_name, "_2_1_1");
341 break; 375 break;
342 case 13: 376 case 13:
343 strcat(RP->wall_name,"_3_4"); 377 strcat (RP->wall_name, "_3_4");
344 break; 378 break;
345 case 14: 379 case 14:
346 strcat(RP->wall_name,"_3_2"); 380 strcat (RP->wall_name, "_3_2");
347 break; 381 break;
348 case 15: 382 case 15:
349 strcat(RP->wall_name,"_4"); 383 strcat (RP->wall_name, "_4");
350 break; 384 break;
351 } 385 }
386
352 wall_arch = find_archetype(RP->wall_name); 387 wall_arch = archetype::find (RP->wall_name);
353 if(wall_arch!=NULL) { 388
389 if (!wall_arch)
390 {
354 new_wall=arch_to_object(wall_arch); 391 new_wall = arch_to_object (wall_arch);
355 new_wall->x = i; 392 new_wall->x = i;
356 new_wall->y = j; 393 new_wall->y = j;
394
357 if(the_wall && the_wall->map) { 395 if (the_wall && the_wall->map)
358 remove_ob(the_wall); 396 {
359 free_object(the_wall); 397 the_wall->remove ();
398 the_wall->destroy ();
360 } 399 }
400
361 the_wall->move_block = MOVE_ALL; 401 the_wall->move_block = MOVE_ALL;
362 insert_ob_in_map(new_wall,the_map,new_wall,INS_NO_MERGE | INS_NO_WALK_ON); 402 insert_ob_in_map (new_wall, the_map, new_wall, INS_NO_MERGE | INS_NO_WALK_ON);
363 } 403 }
364 else 404
365 nroferrors--; /* it's OK not to find an arch. */
366 return new_wall; 405 return new_wall;
367} 406}
368
369

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines