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 (16 years, 1 month 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

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 *
8 * Deliantra is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (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 * 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 GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */
23
24 /* Specials in this file:
25 included maps */
26
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 void
46 nuke_map_region (maptile *map, int xstart, int ystart, int xsize, int ysize)
47 {
48 int i, j;
49 object *tmp;
50
51 for (i = xstart; i < xstart + xsize; i++)
52 for (j = ystart; j < ystart + ysize; j++)
53 {
54 for (tmp = GET_MAP_OB (map, i, j); tmp != NULL; tmp = tmp->above)
55 {
56 if (!QUERY_FLAG (tmp, FLAG_IS_FLOOR))
57 {
58 if (tmp->head)
59 tmp = tmp->head;
60 tmp->remove ();
61 tmp->destroy ();
62 tmp = GET_MAP_OB (map, i, j);
63 }
64 if (tmp == NULL)
65 break;
66 }
67 }
68 }
69
70 /* copy in_map into dest_map at point x,y */
71 void
72 include_map_in_map (maptile *dest_map, maptile *in_map, int x, int y)
73 {
74 /* First, splatter everything in the dest map at the location */
75 nuke_map_region (dest_map, x, y, in_map->width, in_map->height);
76
77 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 object *new_ob = tmp->deep_clone ();
87
88 if (QUERY_FLAG (tmp, FLAG_IS_LINKED))
89 add_button_link (new_ob, dest_map, tmp->path_attuned);
90
91 dest_map->insert (new_ob, x + i, y + j, 0, INS_NO_MERGE | INS_NO_WALK_ON);
92 }
93 }
94
95 int
96 find_spot_for_submap (maptile *map, char **layout, int *ix, int *iy, int xsize, int ysize)
97 {
98 int tries;
99 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 /* don't even try to place a submap into a map if the big map isn't
104 sufficiently large. */
105 if (2 * xsize > map->width || 2 * ysize > map->height)
106 return 0;
107
108 /* search a bit for a completely free spot. */
109 for (tries = 0; tries < 20; tries++)
110 {
111 /* pick a random location in the layout */
112 i = rndm (map->width - xsize - 2) + 1;
113 j = rndm (map->height - ysize - 2) + 1;
114 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
123 /* if we failed, relax the restrictions */
124
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 i = rndm (map->width - xsize - 2) + 1;
131 j = rndm (map->height - ysize - 2) + 1;
132 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 }
139
140 if (is_occupied)
141 return 0;
142
143 *ix = i;
144 *iy = j;
145 return 1;
146 }
147
148 void
149 place_fountain_with_specials (maptile *map)
150 {
151 int ix, iy, i = -1, tries = 0;
152 maptile *fountain_style = find_style ("/styles/misc", "fountains", -1);
153
154 if (!fountain_style)
155 {
156 LOG (llevError, "unable to load stylemap /styles/misc fountains\n");
157 return;
158 }
159
160 object *fountain = archetype::get ("fountain");
161 object *potion = fountain_style->pick_random_object ()->clone ();
162
163 while (i < 0 && tries < 10)
164 {
165 ix = rndm (map->width - 2) + 1;
166 iy = rndm (map->height - 2) + 1;
167 i = find_free_spot (fountain, map, ix, iy, 1, SIZEOFFREE1 + 1);
168 tries++;
169 }
170
171 if (i == -1)
172 { /* can't place fountain */
173 fountain->destroy ();
174 potion->destroy ();
175 return;
176 }
177
178 ix += freearr_x[i];
179 iy += freearr_y[i];
180 potion->face = fountain->face;
181 SET_FLAG (potion, FLAG_NO_PICK);
182 SET_FLAG (potion, FLAG_IDENTIFIED);
183 potion->name = potion->name_pl = "fountain";
184 potion->x = ix;
185 potion->y = iy;
186 potion->materialname = "adamantium";
187 fountain->x = ix;
188 fountain->y = iy;
189 insert_ob_in_map (fountain, map, NULL, 0);
190 insert_ob_in_map (potion, map, NULL, 0);
191 }
192
193 void
194 place_special_exit (maptile *map, int hole_type, random_map_params *RP)
195 {
196 int ix, iy, i = -1;
197 char buf[16384];
198 const char *style, *decor, *mon;
199 maptile *exit_style = find_style ("/styles/misc", "obscure_exits", -1);
200 int g_xsize, g_ysize;
201
202 if (!exit_style)
203 {
204 LOG (llevError, "unable to load stylemap /styles/misc obscure_exits\n");
205 return;
206 }
207
208 if (!exit_style)
209 return;
210
211 object *the_exit = exit_style->pick_random_object ()->clone ();
212
213 // put an upper bound here, just in case
214 for (int repeat = 8192; --repeat; )
215 {
216 ix = rndm (map->width - 2) + 1;
217 iy = rndm (map->height - 2) + 1;
218
219 i = find_free_spot (the_exit, map, ix, iy, 1, SIZEOFFREE1 + 1);
220 if (i >= 0)
221 {
222 ix += freearr_x[i];
223 iy += freearr_y[i];
224 break;
225 }
226 }
227
228 the_exit->x = ix;
229 the_exit->y = iy;
230
231 if (!hole_type)
232 hole_type = rndm (NR_OF_HOLE_TYPES) + 1;
233
234 switch (hole_type)
235 {
236 case GLORY_HOLE: /* treasures */
237 {
238 g_xsize = rndm (3) + 4 + RP->difficulty / 4;
239 g_ysize = rndm (3) + 4 + RP->difficulty / 4;
240 style = "onion";
241 decor = "wealth2";
242 mon = "none";
243 break;
244 }
245
246 case ORC_ZONE: /* hole with orcs in it. */
247 {
248 g_xsize = rndm (3) + 4 + RP->difficulty / 4;
249 g_ysize = rndm (3) + 4 + RP->difficulty / 4;
250 style = "onion";
251 decor = "wealth2";
252 mon = "orc";
253 break;
254 }
255
256 case MINING_ZONE: /* hole with orcs in it. */
257 {
258 g_xsize = rndm (9) + 4 + RP->difficulty / 4;
259 g_ysize = rndm (9) + 4 + RP->difficulty / 4;
260 style = "maze";
261 decor = "minerals2";
262 mon = "none";
263 break;
264 }
265
266 default: /* undefined */
267 LOG (llevError, "place_special_exit: undefined hole type %d\n", hole_type);
268 return;
269 break;
270 }
271
272 /* Need to be at least this size, otherwise the load
273 * code will generate new size values which are too large.
274 */
275 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
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 RMOPT_WALLS_ONLY, 0, 0, 1, RP->dungeon_level, RP->dungeon_level,
281 RP->difficulty, RP->difficulty, -1, 1, 0, 0, 0, 0, RP->difficulty_increase);
282 the_exit->slaying = "/!";
283 the_exit->msg = buf;
284
285 insert_ob_in_map (the_exit, map, NULL, 0);
286 }
287
288 void
289 place_specials_in_map (maptile *map, char **layout, random_map_params *RP)
290 {
291 maptile *special_map;
292 int ix, iy; /* map insertion locatons */
293 int special_type; /* type of special to make */
294
295 special_type = rndm (NUM_OF_SPECIAL_TYPES);
296
297 switch (special_type)
298 {
299 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
303 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
309 break;
310
311 case SPECIAL_FOUNTAIN:
312 /* Make a special fountain: an unpickable potion disguised as
313 a fountain, or rather, colocated with a fountain. */
314 place_fountain_with_specials (map);
315 break;
316
317 case SPECIAL_EXIT:
318 /* Make an exit to another random map, e.g. a gloryhole. */
319 place_special_exit (map, 0, RP);
320 break;
321 }
322 }
323