ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/special.C
Revision: 1.31
Committed: Sun May 4 08:25:33 2008 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.30: +2 -4 lines
Log Message:
rewrite objetc_create_clone to deep_clone and fix randfom map generator bug

File Contents

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