ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/special.C
Revision: 1.14
Committed: Thu Jan 11 00:41:08 2007 UTC (17 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.13: +1 -1 lines
Log Message:
make random map paths more beautiful, in the common case

File Contents

# User Rev Content
1 root 1.12
2 elmex 1.1 /*
3     CrossFire, A Multiplayer game for X-windows
4    
5 pippijn 1.13 Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
6 elmex 1.1 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
7     Copyright (C) 1992 Frank Tore Johansen
8    
9     This program is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13    
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     GNU General Public License for more details.
18    
19     You should have received a copy of the GNU General Public License
20     along with this program; if not, write to the Free Software
21     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22    
23 root 1.5 The authors can be reached via e-mail at <crossfire@schmorp.de>
24 elmex 1.1 */
25    
26     /* Specials in this file:
27 root 1.2 included maps */
28 elmex 1.1
29     #include <global.h>
30     #include <random_map.h>
31     #include <rproto.h>
32    
33     #define NUM_OF_SPECIAL_TYPES 4
34     #define NO_SPECIAL 0
35     #define SPECIAL_SUBMAP 1
36     #define SPECIAL_FOUNTAIN 2
37     #define SPECIAL_EXIT 3
38    
39     #define GLORY_HOLE 1
40     #define ORC_ZONE 2
41     #define MINING_ZONE 3
42     #define NR_OF_HOLE_TYPES 3
43    
44     /* clear map completely of all objects: a rectangular area of xsize, ysize
45     is cleared with the top left corner at xstart, ystart */
46    
47 root 1.4 void
48 root 1.6 nuke_map_region (maptile *map, int xstart, int ystart, int xsize, int ysize)
49 root 1.4 {
50     int i, j;
51 elmex 1.1 object *tmp;
52 root 1.4
53     for (i = xstart; i < xstart + xsize; i++)
54     for (j = ystart; j < ystart + ysize; j++)
55     {
56 root 1.9 for (tmp = GET_MAP_OB (map, i, j); tmp != NULL; tmp = tmp->above)
57 root 1.4 {
58     if (!QUERY_FLAG (tmp, FLAG_IS_FLOOR))
59     {
60     if (tmp->head)
61     tmp = tmp->head;
62 root 1.7 tmp->remove ();
63 root 1.8 tmp->destroy ();
64 root 1.9 tmp = GET_MAP_OB (map, i, j);
65 root 1.4 }
66     if (tmp == NULL)
67     break;
68     }
69 elmex 1.1 }
70     }
71    
72    
73    
74 root 1.4 /* copy in_map into dest_map at point x,y */
75 elmex 1.1
76    
77 root 1.4 void
78 root 1.6 include_map_in_map (maptile *dest_map, maptile *in_map, int x, int y)
79 root 1.4 {
80     int i, j;
81 elmex 1.1 object *tmp;
82     object *new_ob;
83 root 1.4
84 elmex 1.1 /* First, splatter everything in the dest map at the location */
85 root 1.10 nuke_map_region (dest_map, x, y, in_map->width, in_map->height);
86 root 1.4
87 root 1.10 for (i = 0; i < in_map->width; i++)
88     for (j = 0; j < in_map->height; j++)
89 root 1.4 {
90 root 1.9 for (tmp = GET_MAP_OB (in_map, i, j); tmp != NULL; tmp = tmp->above)
91 root 1.4 {
92     /* don't copy things with multiple squares: must be dealt with
93     specially. */
94     if (tmp->head != NULL)
95     continue;
96     new_ob = arch_to_object (tmp->arch);
97     copy_object_with_inv (tmp, new_ob);
98     if (QUERY_FLAG (tmp, FLAG_IS_LINKED))
99     add_button_link (new_ob, dest_map, tmp->path_attuned);
100     new_ob->x = i + x;
101     new_ob->y = j + y;
102     insert_multisquare_ob_in_map (new_ob, dest_map);
103     }
104 elmex 1.1 }
105     }
106    
107 root 1.4 int
108 root 1.6 find_spot_for_submap (maptile *map, char **layout, int *ix, int *iy, int xsize, int ysize)
109 root 1.4 {
110 elmex 1.1 int tries;
111 root 1.4 int i = 0, j = 0; /* initialization may not be needed but prevents compiler warnings */
112     int is_occupied = 0;
113     int l, m;
114    
115 elmex 1.1 /* don't even try to place a submap into a map if the big map isn't
116     sufficiently large. */
117 root 1.10 if (2 * xsize > map->width || 2 * ysize > map->height)
118 root 1.4 return 0;
119    
120 elmex 1.1 /* search a bit for a completely free spot. */
121 root 1.4 for (tries = 0; tries < 20; tries++)
122     {
123     /* pick a random location in the layout */
124 root 1.10 i = RANDOM () % (map->width - xsize - 2) + 1;
125     j = RANDOM () % (map->height - ysize - 2) + 1;
126 root 1.4 is_occupied = 0;
127     for (l = i; l < i + xsize; l++)
128     for (m = j; m < j + ysize; m++)
129     is_occupied |= layout[l][m];
130     if (!is_occupied)
131     break;
132     }
133    
134 elmex 1.1
135     /* if we failed, relax the restrictions */
136 root 1.4
137     if (is_occupied)
138     { /* failure, try a relaxed placer. */
139     /* pick a random location in the layout */
140     for (tries = 0; tries < 10; tries++)
141     {
142 root 1.10 i = RANDOM () % (map->width - xsize - 2) + 1;
143     j = RANDOM () % (map->height - ysize - 2) + 1;
144 root 1.4 is_occupied = 0;
145     for (l = i; l < i + xsize; l++)
146     for (m = j; m < j + ysize; m++)
147     if (layout[l][m] == 'C' || layout[l][m] == '>' || layout[l][m] == '<')
148     is_occupied |= 1;
149     }
150 elmex 1.1 }
151 root 1.4 if (is_occupied)
152     return 0;
153     *ix = i;
154     *iy = j;
155 elmex 1.1 return 1;
156 root 1.4 }
157 elmex 1.1
158    
159 root 1.4 void
160 root 1.6 place_fountain_with_specials (maptile *map)
161 root 1.4 {
162     int ix, iy, i = -1, tries = 0;
163 root 1.6 maptile *fountain_style = find_style ("/styles/misc", "fountains", -1);
164 root 1.4 object *fountain = get_archetype ("fountain");
165 root 1.8 object *potion = object::create ();
166    
167     pick_random_object (fountain_style)->copy_to (potion);
168 root 1.4
169     while (i < 0 && tries < 10)
170     {
171 root 1.10 ix = RANDOM () % (map->width - 2) + 1;
172     iy = RANDOM () % (map->height - 2) + 1;
173 root 1.4 i = find_first_free_spot (fountain, map, ix, iy);
174     tries++;
175 root 1.8 }
176    
177 root 1.4 if (i == -1)
178     { /* can't place fountain */
179 root 1.8 fountain->destroy ();
180     potion->destroy ();
181 root 1.4 return;
182     }
183 root 1.8
184 elmex 1.1 ix += freearr_x[i];
185     iy += freearr_y[i];
186 root 1.4 potion->face = fountain->face;
187     SET_FLAG (potion, FLAG_NO_PICK);
188     SET_FLAG (potion, FLAG_IDENTIFIED);
189 root 1.3 potion->name = potion->name_pl = "fountain";
190 elmex 1.1 potion->x = ix;
191     potion->y = iy;
192 root 1.4 potion->material = M_ADAMANT;
193 elmex 1.1 fountain->x = ix;
194     fountain->y = iy;
195 root 1.4 insert_ob_in_map (fountain, map, NULL, 0);
196     insert_ob_in_map (potion, map, NULL, 0);
197 elmex 1.1
198     }
199    
200 root 1.4 void
201 root 1.12 place_special_exit (maptile *map, int hole_type, random_map_params *RP)
202 root 1.4 {
203     int ix, iy, i = -1;
204 root 1.14 char buf[16384], *style, *decor, *mon;
205 root 1.6 maptile *exit_style = find_style ("/styles/misc", "obscure_exits", -1);
206 root 1.4 int g_xsize, g_ysize;
207    
208 root 1.8 object *the_exit = object::create ();
209 root 1.4
210     if (!exit_style)
211     return;
212    
213 root 1.8 pick_random_object (exit_style)->copy_to (the_exit);
214 root 1.4
215     while (i < 0)
216     {
217 root 1.10 ix = RANDOM () % (map->width - 2) + 1;
218     iy = RANDOM () % (map->height - 2) + 1;
219 root 1.4 i = find_first_free_spot (the_exit, map, ix, iy);
220 elmex 1.1 }
221 root 1.4
222     ix += freearr_x[i];
223     iy += freearr_y[i];
224     the_exit->x = ix;
225     the_exit->y = iy;
226    
227     if (!hole_type)
228     hole_type = RANDOM () % NR_OF_HOLE_TYPES + 1;
229    
230     switch (hole_type)
231     {
232 root 1.12 case GLORY_HOLE: /* treasures */
233     {
234     g_xsize = RANDOM () % 3 + 4 + RP->difficulty / 4;
235     g_ysize = RANDOM () % 3 + 4 + RP->difficulty / 4;
236     style = "onion";
237     decor = "wealth2";
238     mon = "none";
239     break;
240     }
241 root 1.4
242 root 1.12 case ORC_ZONE: /* hole with orcs in it. */
243     {
244     g_xsize = RANDOM () % 3 + 4 + RP->difficulty / 4;
245     g_ysize = RANDOM () % 3 + 4 + RP->difficulty / 4;
246     style = "onion";
247     decor = "wealth2";
248     mon = "orc";
249     break;
250     }
251 root 1.4
252 root 1.12 case MINING_ZONE: /* hole with orcs in it. */
253     {
254     g_xsize = RANDOM () % 9 + 4 + RP->difficulty / 4;
255     g_ysize = RANDOM () % 9 + 4 + RP->difficulty / 4;
256     style = "maze";
257     decor = "minerals2";
258     mon = "none";
259     break;
260     }
261 root 1.4
262 root 1.12 default: /* undefined */
263     LOG (llevError, "place_special_exit: undefined hole type %d\n", hole_type);
264     return;
265     break;
266 elmex 1.1 }
267    
268 root 1.4 /* Need to be at least this size, otherwise the load
269     * code will generate new size values which are too large.
270     */
271     if (g_xsize < MIN_RANDOM_MAP_SIZE)
272     g_xsize = MIN_RANDOM_MAP_SIZE;
273     if (g_ysize < MIN_RANDOM_MAP_SIZE)
274     g_ysize = MIN_RANDOM_MAP_SIZE;
275    
276     write_parameters_to_string (buf, g_xsize, g_ysize, RP->wallstyle, RP->floorstyle, mon,
277     "none", style, decor, "none", RP->exitstyle, 0, 0, 0,
278 root 1.11 RMOPT_WALLS_ONLY, 0, 0, 1, RP->dungeon_level, RP->dungeon_level,
279 root 1.4 RP->difficulty, RP->difficulty, -1, 1, 0, 0, 0, 0, RP->difficulty_increase);
280     the_exit->slaying = "/!";
281     the_exit->msg = buf;
282 elmex 1.1
283 root 1.4 insert_ob_in_map (the_exit, map, NULL, 0);
284 elmex 1.1 }
285 root 1.4
286    
287     void
288 root 1.12 place_specials_in_map (maptile *map, char **layout, random_map_params *RP)
289 root 1.4 {
290 root 1.6 maptile *special_map;
291 root 1.4 int ix, iy; /* map insertion locatons */
292     int special_type; /* type of special to make */
293    
294    
295     special_type = RANDOM () % NUM_OF_SPECIAL_TYPES;
296     switch (special_type)
297     {
298    
299 root 1.12 /* includes a special map into the random map being made. */
300     case SPECIAL_SUBMAP:
301     {
302     special_map = find_style ("/styles/specialmaps", 0, RP->difficulty);
303     if (special_map == NULL)
304     return;
305    
306     if (find_spot_for_submap (map, layout, &ix, &iy, special_map->width, special_map->height))
307     include_map_in_map (map, special_map, ix, iy);
308     break;
309     }
310 root 1.4
311 root 1.12 /* Make a special fountain: an unpickable potion disguised as
312     a fountain, or rather, colocated with a fountain. */
313     case SPECIAL_FOUNTAIN:
314     {
315     place_fountain_with_specials (map);
316     break;
317     }
318 root 1.4
319 root 1.12 /* Make an exit to another random map, e.g. a gloryhole. */
320     case SPECIAL_EXIT:
321     {
322     place_special_exit (map, 0, RP);
323     break;
324     }
325 root 1.4 }
326 elmex 1.1
327     }