ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/special.C
Revision: 1.34
Committed: Mon Sep 29 09:04:51 2008 UTC (15 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.33: +3 -3 lines
Log Message:
Replace all destroy calls by destroy (true) (after checking), add
temporary debugging code for lost objects, fix some minor issues in random_maps.

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 root 1.32 /* clear map completely of all !floor objects:
43     * a rectangular area of xsize, ysize
44     * is cleared with the top left corner at xstart, ystart
45     */
46     static void
47     nuke_map_region (maptile *map, char **layout, int xstart, int ystart, int xsize, int ysize)
48 root 1.4 {
49 root 1.32 for (int i = xstart; i < xstart + xsize; i++)
50     for (int j = ystart; j < ystart + ysize; j++)
51     {
52     layout[i][j] = 'S';
53 root 1.4
54 root 1.32 for (object *tmp = map->at (i, j).bot; tmp; )
55     if (tmp->flag [FLAG_IS_FLOOR])
56     tmp = tmp->above;
57     else
58     {
59     object *head = tmp->head_ ();
60     tmp = tmp->above;
61 root 1.34 head->destroy (true);
62 root 1.32 }
63 elmex 1.1 }
64     }
65    
66 root 1.4 /* copy in_map into dest_map at point x,y */
67 root 1.32 static void
68 root 1.6 include_map_in_map (maptile *dest_map, maptile *in_map, int x, int y)
69 root 1.4 {
70 root 1.30 for (int i = 0; i < in_map->width; i++)
71     for (int j = 0; j < in_map->height; j++)
72     for (object *tmp = GET_MAP_OB (in_map, i, j); tmp; tmp = tmp->above)
73     {
74 root 1.32 /* don't copy tails: must be dealt with specially. */
75 root 1.30 if (!tmp->is_head ())
76     continue;
77    
78 root 1.31 object *new_ob = tmp->deep_clone ();
79 root 1.30
80     if (QUERY_FLAG (tmp, FLAG_IS_LINKED))
81     add_button_link (new_ob, dest_map, tmp->path_attuned);
82    
83 root 1.31 dest_map->insert (new_ob, x + i, y + j, 0, INS_NO_MERGE | INS_NO_WALK_ON);
84 root 1.30 }
85 elmex 1.1 }
86    
87 root 1.32 static int
88 root 1.6 find_spot_for_submap (maptile *map, char **layout, int *ix, int *iy, int xsize, int ysize)
89 root 1.4 {
90 root 1.32 int blocked, i, j;
91 root 1.4
92 elmex 1.1 /* don't even try to place a submap into a map if the big map isn't
93     sufficiently large. */
94 root 1.10 if (2 * xsize > map->width || 2 * ysize > map->height)
95 root 1.4 return 0;
96    
97 elmex 1.1 /* search a bit for a completely free spot. */
98 root 1.32 for (int tries = 0; tries < 20; tries++)
99 root 1.4 {
100 root 1.32 blocked = 0;
101    
102 root 1.4 /* pick a random location in the layout */
103 root 1.33 i = rmg_rndm (1, map->width - xsize - 2);
104     j = rmg_rndm (1, map->height - ysize - 2);
105 root 1.32
106     for (int l = i; l < i + xsize; l++)
107     for (int m = j; m < j + ysize; m++)
108     {
109     blocked = 1;
110     break;
111     }
112    
113     if (!blocked)
114 root 1.4 break;
115     }
116    
117 elmex 1.1 /* if we failed, relax the restrictions */
118 root 1.32 if (blocked)
119 root 1.4 { /* failure, try a relaxed placer. */
120     /* pick a random location in the layout */
121 root 1.32 for (int tries = 0; tries < 10; tries++)
122 root 1.4 {
123 root 1.33 i = rmg_rndm (1, map->width - xsize - 2);
124     j = rmg_rndm (1, map->height - ysize - 2);
125 root 1.32
126     blocked = 0;
127    
128     for (int l = i; l < i + xsize; l++)
129     for (int m = j; m < j + ysize; m++)
130     if (layout[l][m] != '#' && layout[l][m] != 'D' && layout[l][m] != 0)
131     {
132     blocked = 1;
133     break;
134     }
135 root 1.4 }
136 elmex 1.1 }
137 root 1.27
138 root 1.32 if (blocked)
139 root 1.4 return 0;
140 root 1.27
141 root 1.4 *ix = i;
142     *iy = j;
143 root 1.32
144 elmex 1.1 return 1;
145 root 1.4 }
146 elmex 1.1
147 root 1.32 static void
148 root 1.6 place_fountain_with_specials (maptile *map)
149 root 1.4 {
150     int ix, iy, i = -1, tries = 0;
151 root 1.6 maptile *fountain_style = find_style ("/styles/misc", "fountains", -1);
152 root 1.18
153     if (!fountain_style)
154 root 1.19 {
155 pippijn 1.20 LOG (llevError, "unable to load stylemap /styles/misc fountains\n");
156 root 1.19 return;
157     }
158 root 1.18
159 root 1.32 object *fountain = archetype::get (shstr_fountain);
160     object *potion = fountain_style->pick_random_object (rmg_rndm)->clone ();
161 root 1.4
162     while (i < 0 && tries < 10)
163     {
164 root 1.32 ix = rmg_rndm (map->width - 2) + 1;
165     iy = rmg_rndm (map->height - 2) + 1;
166 root 1.15 i = find_free_spot (fountain, map, ix, iy, 1, SIZEOFFREE1 + 1);
167 root 1.4 tries++;
168 root 1.8 }
169    
170 root 1.4 if (i == -1)
171     { /* can't place fountain */
172 root 1.34 fountain->destroy (true);
173     potion->destroy (true);
174 root 1.4 return;
175     }
176 root 1.8
177 elmex 1.1 ix += freearr_x[i];
178     iy += freearr_y[i];
179 root 1.4 potion->face = fountain->face;
180     SET_FLAG (potion, FLAG_NO_PICK);
181     SET_FLAG (potion, FLAG_IDENTIFIED);
182 root 1.3 potion->name = potion->name_pl = "fountain";
183 elmex 1.1 potion->x = ix;
184     potion->y = iy;
185 root 1.21 potion->materialname = "adamantium";
186 elmex 1.1 fountain->x = ix;
187     fountain->y = iy;
188 root 1.4 insert_ob_in_map (fountain, map, NULL, 0);
189     insert_ob_in_map (potion, map, NULL, 0);
190 elmex 1.1 }
191    
192 root 1.32 static void
193 root 1.12 place_special_exit (maptile *map, int hole_type, random_map_params *RP)
194 root 1.4 {
195     int ix, iy, i = -1;
196 root 1.23 char buf[16384];
197     const char *style, *decor, *mon;
198 root 1.6 maptile *exit_style = find_style ("/styles/misc", "obscure_exits", -1);
199 root 1.4 int g_xsize, g_ysize;
200    
201 root 1.18 if (!exit_style)
202 root 1.19 {
203 root 1.28 LOG (llevError, "unable to load stylemap /styles/misc obscure_exits\n");
204 root 1.19 return;
205     }
206 root 1.18
207 root 1.4 if (!exit_style)
208     return;
209    
210 root 1.32 object *the_exit = exit_style->pick_random_object (rmg_rndm)->clone ();
211 root 1.4
212 root 1.25 // put an upper bound here, just in case
213     for (int repeat = 8192; --repeat; )
214 root 1.4 {
215 root 1.33 ix = rmg_rndm (1, map->width - 2);
216     iy = rmg_rndm (1, map->height - 2);
217 root 1.25
218 root 1.15 i = find_free_spot (the_exit, map, ix, iy, 1, SIZEOFFREE1 + 1);
219 root 1.25 if (i >= 0)
220     {
221     ix += freearr_x[i];
222     iy += freearr_y[i];
223     break;
224     }
225 elmex 1.1 }
226 root 1.4
227     the_exit->x = ix;
228     the_exit->y = iy;
229    
230     if (!hole_type)
231 root 1.32 hole_type = rmg_rndm (NR_OF_HOLE_TYPES) + 1;
232 root 1.4
233     switch (hole_type)
234     {
235 root 1.12 case GLORY_HOLE: /* treasures */
236     {
237 root 1.32 g_xsize = rmg_rndm (3) + 4 + RP->difficulty / 4;
238     g_ysize = rmg_rndm (3) + 4 + RP->difficulty / 4;
239 root 1.12 style = "onion";
240     decor = "wealth2";
241     mon = "none";
242     break;
243     }
244 root 1.4
245 root 1.12 case ORC_ZONE: /* hole with orcs in it. */
246     {
247 root 1.32 g_xsize = rmg_rndm (3) + 4 + RP->difficulty / 4;
248     g_ysize = rmg_rndm (3) + 4 + RP->difficulty / 4;
249 root 1.12 style = "onion";
250     decor = "wealth2";
251     mon = "orc";
252     break;
253     }
254 root 1.4
255 root 1.12 case MINING_ZONE: /* hole with orcs in it. */
256     {
257 root 1.32 g_xsize = rmg_rndm (9) + 4 + RP->difficulty / 4;
258     g_ysize = rmg_rndm (9) + 4 + RP->difficulty / 4;
259 root 1.12 style = "maze";
260     decor = "minerals2";
261     mon = "none";
262     break;
263     }
264 root 1.4
265 root 1.12 default: /* undefined */
266     LOG (llevError, "place_special_exit: undefined hole type %d\n", hole_type);
267     return;
268     break;
269 elmex 1.1 }
270    
271 root 1.4 /* Need to be at least this size, otherwise the load
272     * code will generate new size values which are too large.
273     */
274 root 1.27 if (g_xsize < MIN_RANDOM_MAP_SIZE) g_xsize = MIN_RANDOM_MAP_SIZE;
275     if (g_ysize < MIN_RANDOM_MAP_SIZE) g_ysize = MIN_RANDOM_MAP_SIZE;
276 root 1.4
277     write_parameters_to_string (buf, g_xsize, g_ysize, RP->wallstyle, RP->floorstyle, mon,
278     "none", style, decor, "none", RP->exitstyle, 0, 0, 0,
279 root 1.11 RMOPT_WALLS_ONLY, 0, 0, 1, RP->dungeon_level, RP->dungeon_level,
280 root 1.4 RP->difficulty, RP->difficulty, -1, 1, 0, 0, 0, 0, RP->difficulty_increase);
281     the_exit->slaying = "/!";
282     the_exit->msg = buf;
283 elmex 1.1
284 root 1.4 insert_ob_in_map (the_exit, map, NULL, 0);
285 elmex 1.1 }
286 root 1.4
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.32 switch (rmg_rndm (NUM_OF_SPECIAL_TYPES))
291 root 1.4 {
292 root 1.19 case SPECIAL_SUBMAP:
293 root 1.32 {
294     /* includes a special map into the random map being made. */
295     maptile *special_map = find_style ("/styles/specialmaps", 0, RP->difficulty);
296    
297     if (!special_map)
298     break;
299 root 1.4
300 root 1.32 int ix, iy; /* map insertion locatons */
301 root 1.19
302 root 1.32 if (find_spot_for_submap (map, layout, &ix, &iy, special_map->width, special_map->height))
303     {
304     /* First, splatter everything in the dest map at the location */
305     nuke_map_region (map, layout, ix, iy, special_map->width, special_map->height);
306     include_map_in_map (map, special_map, ix, iy);
307     }
308     }
309 root 1.12
310 root 1.19 break;
311 root 1.4
312 root 1.19 case SPECIAL_FOUNTAIN:
313 root 1.12 /* Make a special fountain: an unpickable potion disguised as
314     a fountain, or rather, colocated with a fountain. */
315 root 1.19 place_fountain_with_specials (map);
316     break;
317 root 1.4
318 root 1.19 case SPECIAL_EXIT:
319 root 1.12 /* Make an exit to another random map, e.g. a gloryhole. */
320 root 1.19 place_special_exit (map, 0, RP);
321     break;
322 root 1.4 }
323 root 1.27 }
324 elmex 1.1