ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/special.C
Revision: 1.15
Committed: Mon Jan 15 15:54:19 2007 UTC (17 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.14: +2 -3 lines
Log Message:
experimental stability and correctness changes

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.15 i = find_free_spot (fountain, map, ix, iy, 1, SIZEOFFREE1 + 1);
174 root 1.4 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 root 1.4 void
200 root 1.12 place_special_exit (maptile *map, int hole_type, random_map_params *RP)
201 root 1.4 {
202     int ix, iy, i = -1;
203 root 1.14 char buf[16384], *style, *decor, *mon;
204 root 1.6 maptile *exit_style = find_style ("/styles/misc", "obscure_exits", -1);
205 root 1.4 int g_xsize, g_ysize;
206    
207 root 1.8 object *the_exit = object::create ();
208 root 1.4
209     if (!exit_style)
210     return;
211    
212 root 1.8 pick_random_object (exit_style)->copy_to (the_exit);
213 root 1.4
214     while (i < 0)
215     {
216 root 1.10 ix = RANDOM () % (map->width - 2) + 1;
217     iy = RANDOM () % (map->height - 2) + 1;
218 root 1.15 i = find_free_spot (the_exit, map, ix, iy, 1, SIZEOFFREE1 + 1);
219 elmex 1.1 }
220 root 1.4
221     ix += freearr_x[i];
222     iy += freearr_y[i];
223     the_exit->x = ix;
224     the_exit->y = iy;
225    
226     if (!hole_type)
227     hole_type = RANDOM () % NR_OF_HOLE_TYPES + 1;
228    
229     switch (hole_type)
230     {
231 root 1.12 case GLORY_HOLE: /* treasures */
232     {
233     g_xsize = RANDOM () % 3 + 4 + RP->difficulty / 4;
234     g_ysize = RANDOM () % 3 + 4 + RP->difficulty / 4;
235     style = "onion";
236     decor = "wealth2";
237     mon = "none";
238     break;
239     }
240 root 1.4
241 root 1.12 case ORC_ZONE: /* hole with orcs in it. */
242     {
243     g_xsize = RANDOM () % 3 + 4 + RP->difficulty / 4;
244     g_ysize = RANDOM () % 3 + 4 + RP->difficulty / 4;
245     style = "onion";
246     decor = "wealth2";
247     mon = "orc";
248     break;
249     }
250 root 1.4
251 root 1.12 case MINING_ZONE: /* hole with orcs in it. */
252     {
253     g_xsize = RANDOM () % 9 + 4 + RP->difficulty / 4;
254     g_ysize = RANDOM () % 9 + 4 + RP->difficulty / 4;
255     style = "maze";
256     decor = "minerals2";
257     mon = "none";
258     break;
259     }
260 root 1.4
261 root 1.12 default: /* undefined */
262     LOG (llevError, "place_special_exit: undefined hole type %d\n", hole_type);
263     return;
264     break;
265 elmex 1.1 }
266    
267 root 1.4 /* Need to be at least this size, otherwise the load
268     * code will generate new size values which are too large.
269     */
270     if (g_xsize < MIN_RANDOM_MAP_SIZE)
271     g_xsize = MIN_RANDOM_MAP_SIZE;
272     if (g_ysize < MIN_RANDOM_MAP_SIZE)
273     g_ysize = MIN_RANDOM_MAP_SIZE;
274    
275     write_parameters_to_string (buf, g_xsize, g_ysize, RP->wallstyle, RP->floorstyle, mon,
276     "none", style, decor, "none", RP->exitstyle, 0, 0, 0,
277 root 1.11 RMOPT_WALLS_ONLY, 0, 0, 1, RP->dungeon_level, RP->dungeon_level,
278 root 1.4 RP->difficulty, RP->difficulty, -1, 1, 0, 0, 0, 0, RP->difficulty_increase);
279     the_exit->slaying = "/!";
280     the_exit->msg = buf;
281 elmex 1.1
282 root 1.4 insert_ob_in_map (the_exit, map, NULL, 0);
283 elmex 1.1 }
284 root 1.4
285    
286     void
287 root 1.12 place_specials_in_map (maptile *map, char **layout, random_map_params *RP)
288 root 1.4 {
289 root 1.6 maptile *special_map;
290 root 1.4 int ix, iy; /* map insertion locatons */
291     int special_type; /* type of special to make */
292    
293    
294     special_type = RANDOM () % NUM_OF_SPECIAL_TYPES;
295     switch (special_type)
296     {
297    
298 root 1.12 /* includes a special map into the random map being made. */
299     case SPECIAL_SUBMAP:
300     {
301     special_map = find_style ("/styles/specialmaps", 0, RP->difficulty);
302     if (special_map == NULL)
303     return;
304    
305     if (find_spot_for_submap (map, layout, &ix, &iy, special_map->width, special_map->height))
306     include_map_in_map (map, special_map, ix, iy);
307     break;
308     }
309 root 1.4
310 root 1.12 /* Make a special fountain: an unpickable potion disguised as
311     a fountain, or rather, colocated with a fountain. */
312     case SPECIAL_FOUNTAIN:
313     {
314     place_fountain_with_specials (map);
315     break;
316     }
317 root 1.4
318 root 1.12 /* Make an exit to another random map, e.g. a gloryhole. */
319     case SPECIAL_EXIT:
320     {
321     place_special_exit (map, 0, RP);
322     break;
323     }
324 root 1.4 }
325 elmex 1.1
326     }