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.46 by root, Tue Jan 3 11:25:34 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines