ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/special.C
Revision: 1.50
Committed: Sat Jun 26 22:10:18 2010 UTC (14 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.49: +6 -5 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 Frank Tore Johansen
7 *
8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * the terms of the Affero GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * 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 Affero GNU General Public License
19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 *
22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */
24
25 /* Specials in this file:
26 included maps */
27
28 #include <global.h>
29 #include <random_map.h>
30 #include <rproto.h>
31
32 #define NUM_OF_SPECIAL_TYPES 4
33 #define NO_SPECIAL 0
34 #define SPECIAL_SUBMAP 1
35 #define SPECIAL_FOUNTAIN 2
36 #define SPECIAL_EXIT 3
37
38 #define GLORY_HOLE 1
39 #define ORC_ZONE 2
40 #define MINING_ZONE 3
41 #define NR_OF_HOLE_TYPES 3
42
43 /* clear map completely of all !floor objects:
44 * a rectangular area of xsize, ysize
45 * is cleared with the top left corner at xstart, ystart
46 */
47 static void
48 nuke_map_region (maptile *map, char **layout, int xstart, int ystart, int xsize, int ysize)
49 {
50 for (int i = xstart; i < xstart + xsize; i++)
51 for (int j = ystart; j < ystart + ysize; j++)
52 {
53 layout[i][j] = 'S';
54
55 for (object *tmp = map->at (i, j).bot; tmp; )
56 if (tmp->flag [FLAG_IS_FLOOR])
57 tmp = tmp->above;
58 else
59 {
60 object *head = tmp->head_ ();
61 tmp = tmp->above;
62 head->destroy ();
63 }
64 }
65 }
66
67 /* copy in_map into dest_map at point x,y */
68 static void
69 include_map_in_map (maptile *dest_map, maptile *in_map, int x, int y)
70 {
71 for (int i = 0; i < in_map->width; i++)
72 for (int j = 0; j < in_map->height; j++)
73 for (object *tmp = GET_MAP_OB (in_map, i, j); tmp; tmp = tmp->above)
74 {
75 /* don't copy tails: must be dealt with specially. */
76 if (!tmp->is_head ())
77 continue;
78
79 object *new_ob = tmp->deep_clone ();
80
81 if (tmp->flag [FLAG_IS_LINKED])
82 new_ob->add_link (dest_map, tmp->find_link ()->id);
83
84 dest_map->insert (new_ob, x + i, y + j, 0, INS_NO_MERGE | INS_NO_WALK_ON);
85 }
86 }
87
88 static int
89 find_spot_for_submap (maptile *map, char **layout, int *ix, int *iy, int xsize, int ysize)
90 {
91 int blocked, i, j;
92
93 /* don't even try to place a submap into a map if the big map isn't
94 sufficiently large. */
95 if (2 * xsize > map->width || 2 * ysize > map->height)
96 return 0;
97
98 /* search a bit for a completely free spot. */
99 for (int tries = 0; tries < 20; tries++)
100 {
101 blocked = 0;
102
103 /* pick a random location in the layout */
104 i = rmg_rndm (1, map->width - xsize - 2);
105 j = rmg_rndm (1, map->height - ysize - 2);
106
107 for (int l = i; l < i + xsize; l++)
108 for (int m = j; m < j + ysize; m++)
109 {
110 blocked = 1;
111 break;
112 }
113
114 if (!blocked)
115 break;
116 }
117
118 /* if we failed, relax the restrictions */
119 if (blocked)
120 { /* failure, try a relaxed placer. */
121 /* pick a random location in the layout */
122 for (int tries = 0; tries < 10; tries++)
123 {
124 i = rmg_rndm (1, map->width - xsize - 2);
125 j = rmg_rndm (1, map->height - ysize - 2);
126
127 blocked = 0;
128
129 for (int l = i; l < i + xsize; l++)
130 for (int m = j; m < j + ysize; m++)
131 if (layout[l][m] != '#' && layout[l][m] != 'D' && layout[l][m] != 0)
132 {
133 blocked = 1;
134 break;
135 }
136 }
137 }
138
139 if (blocked)
140 return 0;
141
142 *ix = i;
143 *iy = j;
144
145 return 1;
146 }
147
148 static 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 (shstr_fountain);
161 object *potion = fountain_style->pick_random_object (rmg_rndm)->clone ();
162
163 while (i < 0 && tries < 10)
164 {
165 ix = rmg_rndm (map->width - 2) + 1;
166 iy = rmg_rndm (map->height - 2) + 1;
167 i = rmg_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 potion->set_flag (FLAG_NO_PICK);
182 potion->set_flag (FLAG_IDENTIFIED);
183 potion->name = potion->name_pl = shstr_fountain;
184 potion->x = ix;
185 potion->y = iy;
186 potion->material = name_to_material (shstr_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 static void
194 place_special_exit (maptile *map, int hole_type, random_map_params *RP)
195 {
196 maptile *exit_style = find_style ("/styles/misc", "obscure_exits", RP->difficulty);
197 if (!exit_style)
198 {
199 LOG (llevError, "unable to load stylemap /styles/misc obscure_exits\n");
200 return;
201 }
202
203 if (!exit_style)
204 return;
205
206 object *the_exit = exit_style->pick_random_object (rmg_rndm)->clone ();
207
208 // put an upper bound here, just in case
209 for (int repeat = 8192; --repeat; )
210 {
211 int ix = rmg_rndm (1, map->width - 2);
212 int iy = rmg_rndm (1, map->height - 2);
213
214 int i = rmg_find_free_spot (the_exit, map, ix, iy, 1, SIZEOFFREE1 + 1);
215 if (i >= 0)
216 {
217 the_exit->x = ix + freearr_x[i];
218 the_exit->y = iy + freearr_y[i];
219 break;
220 }
221 }
222
223 if (!hole_type)
224 hole_type = rmg_rndm (NR_OF_HOLE_TYPES) + 1;
225
226 const char *style, *decor, *mon;
227 int g_xsize, g_ysize;
228
229 switch (hole_type)
230 {
231 case GLORY_HOLE: /* treasures */
232 {
233 g_xsize = rmg_rndm (3) + 4 + RP->difficulty / 4;
234 g_ysize = rmg_rndm (3) + 4 + RP->difficulty / 4;
235 style = "onion";
236 decor = "special_wealth";
237 mon = "none";
238 break;
239 }
240
241 case ORC_ZONE: /* hole with orcs in it. */
242 {
243 g_xsize = rmg_rndm (3) + 4 + RP->difficulty / 4;
244 g_ysize = rmg_rndm (3) + 4 + RP->difficulty / 4;
245 style = "onion";
246 decor = "special_wealth";
247 mon = "orc";
248 break;
249 }
250
251 case MINING_ZONE: /* hole with orcs in it. */
252 {
253 g_xsize = rmg_rndm (9) + 4 + RP->difficulty / 4;
254 g_ysize = rmg_rndm (9) + 4 + RP->difficulty / 4;
255 style = "maze";
256 decor = "minerals2";
257 mon = "none";
258 break;
259 }
260
261 default: /* undefined */
262 LOG (llevError, "place_special_exit: undefined hole type %d\n", hole_type);
263 return;
264 }
265
266 /* Need to be at least this size, otherwise the load
267 * code will generate new size values which are too large.
268 */
269 max_it (g_xsize, MIN_RANDOM_MAP_SIZE);
270 max_it (g_ysize, MIN_RANDOM_MAP_SIZE);
271
272 dynbuf_text buf;
273
274 {
275 random_map_params &rp = *new random_map_params; // for zero_intiialised to work...
276
277 //TODO: deep copy
278 rp.hv = (HV *)SvREFCNT_inc_NN ((SV *)RP->hv);
279
280 rp.xsize = g_xsize;
281 rp.ysize = g_ysize;
282
283 assign (rp.monsterstyle , mon);
284 assign (rp.treasurestyle, "none");
285 assign (rp.layoutstyle , style);
286 assign (rp.decorstyle , decor);
287
288 rp.layoutoptions1 = RMOPT_WALLS_ONLY;
289 rp.symmetry = SYMMETRY_NONE;
290 rp.dungeon_depth = RP->dungeon_level;
291 rp.dungeon_level = RP->dungeon_level;
292 rp.difficulty = RP->difficulty;
293 rp.difficulty_given = RP->difficulty;
294 rp.difficulty_increase = RP->difficulty_increase;
295 rp.decoroptions = -1;
296 rp.orientation = 1;
297 rp.random_seed = RP->random_seed + 0xdeadbeef;
298 rp.region = RP->region;
299
300 write_map_parameters_to_string (buf, &rp);
301 delete &rp;
302 }
303
304 the_exit->slaying = shstr_random_map_exit;
305 the_exit->msg = buf;
306
307 insert_ob_in_map (the_exit, map, NULL, 0);
308 }
309
310 void
311 place_specials_in_map (maptile *map, char **layout, random_map_params *RP)
312 {
313 switch (rmg_rndm (NUM_OF_SPECIAL_TYPES))
314 {
315 case SPECIAL_SUBMAP:
316 {
317 /* includes a special map into the random map being made. */
318 maptile *special_map = find_style ("/styles/specialmaps", 0, RP->difficulty, true);
319
320 if (!special_map)
321 break;
322
323 int ix, iy; /* map insertion locatons */
324
325 if (find_spot_for_submap (map, layout, &ix, &iy, special_map->width, special_map->height))
326 {
327 /* First, splatter everything in the dest map at the location */
328 nuke_map_region (map, layout, ix, iy, special_map->width, special_map->height);
329 include_map_in_map (map, special_map, ix, iy);
330 }
331 }
332
333 break;
334
335 case SPECIAL_FOUNTAIN:
336 /* Make a special fountain: an unpickable potion disguised as
337 a fountain, or rather, colocated with a fountain. */
338 place_fountain_with_specials (map);
339 break;
340
341 case SPECIAL_EXIT:
342 /* Make an exit to another random map, e.g. a gloryhole. */
343 place_special_exit (map, 0, RP);
344 break;
345 }
346 }
347