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.3 by root, Sun Sep 3 00:18:42 2006 UTC vs.
Revision 1.15 by pippijn, Sat Jan 6 14:42:30 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines