1 | /* |
1 | /* |
2 | CrossFire, A Multiplayer game for X-windows |
2 | * CrossFire, A Multiplayer game |
3 | |
3 | * |
|
|
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | Copyright (C) 2001 Mark Wedel & Crossfire Development Team |
5 | * Copyright (C) 2001 Mark Wedel & Crossfire Development Team |
5 | Copyright (C) 1992 Frank Tore Johansen |
6 | * Copyright (C) 1992 Frank Tore Johansen |
6 | |
7 | * |
7 | This program is free software; you can redistribute it and/or modify |
8 | * This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by |
9 | * it under the terms of the GNU General Public License as published by |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | * the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. |
11 | * (at your option) any later version. |
11 | |
12 | * |
12 | This program is distributed in the hope that it will be useful, |
13 | * This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
16 | * GNU General Public License for more details. |
16 | |
17 | * |
17 | You should have received a copy of the GNU General Public License |
18 | * You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software |
19 | * along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | |
21 | * |
21 | The authors can be reached via e-mail at <crossfire@schmorp.de> |
22 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
22 | */ |
23 | */ |
23 | |
24 | |
24 | /* placing treasure in maps, where appropriate. */ |
25 | /* placing treasure in maps, where appropriate. */ |
25 | |
|
|
26 | |
|
|
27 | |
26 | |
28 | #include <global.h> |
27 | #include <global.h> |
29 | #include <random_map.h> |
28 | #include <random_map.h> |
30 | #include <rproto.h> |
29 | #include <rproto.h> |
31 | |
30 | |
… | |
… | |
47 | |
46 | |
48 | /* returns true if square x,y has P_NO_PASS set, which is true for walls |
47 | /* returns true if square x,y has P_NO_PASS set, which is true for walls |
49 | * and doors but not monsters. |
48 | * and doors but not monsters. |
50 | * This function is not map tile aware. |
49 | * This function is not map tile aware. |
51 | */ |
50 | */ |
52 | |
|
|
53 | int |
51 | int |
54 | wall_blocked (maptile *m, int x, int y) |
52 | wall_blocked (maptile *m, int x, int y) |
55 | { |
53 | { |
56 | int r; |
|
|
57 | |
|
|
58 | if (OUT_OF_REAL_MAP (m, x, y)) |
54 | if (OUT_OF_REAL_MAP (m, x, y)) |
59 | return 1; |
55 | return 1; |
|
|
56 | |
60 | r = GET_MAP_MOVE_BLOCK (m, x, y) & ~MOVE_BLOCK_DEFAULT; |
57 | int r = GET_MAP_MOVE_BLOCK (m, x, y) & ~MOVE_BLOCK_DEFAULT; |
61 | return r; |
58 | return r; |
62 | } |
59 | } |
63 | |
60 | |
64 | /* place treasures in the map, given the |
61 | /* place treasures in the map, given the |
65 | map, (required) |
62 | map, (required) |
66 | layout, (required) |
63 | layout, (required) |
67 | treasure style (may be empty or NULL, or "none" to cause no treasure.) |
64 | treasure style (may be empty or NULL, or "none" to cause no treasure.) |
68 | treasureoptions (may be 0 for random choices or positive) |
65 | treasureoptions (may be 0 for random choices or positive) |
69 | */ |
66 | */ |
70 | |
|
|
71 | void |
67 | void |
72 | place_treasure (maptile *map, char **layout, char *treasure_style, int treasureoptions, RMParms * RP) |
68 | place_treasure (maptile *map, char **layout, char *treasure_style, int treasureoptions, random_map_params *RP) |
73 | { |
69 | { |
74 | char styledirname[256]; |
70 | char styledirname[1024]; |
75 | char stylefilepath[256]; |
71 | char stylefilepath[1024]; |
76 | maptile *style_map = 0; |
72 | maptile *style_map = 0; |
77 | int num_treasures; |
73 | int num_treasures; |
78 | |
74 | |
79 | /* bail out if treasure isn't wanted. */ |
75 | /* bail out if treasure isn't wanted. */ |
80 | if (treasure_style) |
76 | if (treasure_style) |
81 | if (!strcmp (treasure_style, "none")) |
77 | if (!strcmp (treasure_style, "none")) |
82 | return; |
78 | return; |
|
|
79 | |
83 | if (treasureoptions <= 0) |
80 | if (treasureoptions <= 0) |
84 | treasureoptions = RANDOM () % (2 * LAST_OPTION); |
81 | treasureoptions = RANDOM () % (2 * LAST_OPTION); |
85 | |
82 | |
86 | /* filter out the mutually exclusive options */ |
83 | /* filter out the mutually exclusive options */ |
87 | if ((treasureoptions & RICH) && (treasureoptions & SPARSE)) |
84 | if ((treasureoptions & RICH) && (treasureoptions & SPARSE)) |
88 | { |
85 | { |
89 | if (RANDOM () % 2) |
86 | if (rndm (2)) |
90 | treasureoptions -= 1; |
87 | treasureoptions -= 1; |
91 | else |
88 | else |
92 | treasureoptions -= 2; |
89 | treasureoptions -= 2; |
93 | } |
90 | } |
94 | |
91 | |
… | |
… | |
113 | { |
110 | { |
114 | |
111 | |
115 | /* map_layout_style global, and is previously set */ |
112 | /* map_layout_style global, and is previously set */ |
116 | switch (RP->map_layout_style) |
113 | switch (RP->map_layout_style) |
117 | { |
114 | { |
118 | case ONION_LAYOUT: |
115 | case LAYOUT_ONION: |
119 | case SPIRAL_LAYOUT: |
116 | case LAYOUT_SPIRAL: |
120 | case SQUARE_SPIRAL_LAYOUT: |
117 | case LAYOUT_SQUARE_SPIRAL: |
121 | { |
118 | { |
122 | int i, j; |
119 | int i, j; |
123 | |
120 | |
124 | /* search the onion for C's or '>', and put treasure there. */ |
121 | /* search the onion for C's or '>', and put treasure there. */ |
125 | for (i = 0; i < RP->Xsize; i++) |
122 | for (i = 0; i < RP->Xsize; i++) |
126 | { |
123 | { |
127 | for (j = 0; j < RP->Ysize; j++) |
124 | for (j = 0; j < RP->Ysize; j++) |
128 | { |
125 | { |
129 | if (layout[i][j] == 'C' || layout[i][j] == '>') |
126 | if (layout[i][j] == 'C' || layout[i][j] == '>') |
130 | { |
127 | { |
131 | int tdiv = RP->symmetry_used; |
128 | int tdiv = RP->symmetry_used; |
132 | object **doorlist; |
129 | object **doorlist; |
133 | object *chest; |
130 | object *chest; |
134 | |
131 | |
135 | if (tdiv == 3) |
132 | if (tdiv == 3) |
136 | tdiv = 2; /* this symmetry uses a divisor of 2 */ |
133 | tdiv = 2; /* this symmetry uses a divisor of 2 */ |
137 | /* don't put a chest on an exit. */ |
134 | /* don't put a chest on an exit. */ |
138 | chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures / tdiv, RP); |
135 | chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures / tdiv, RP); |
139 | if (!chest) |
136 | if (!chest) |
140 | continue; /* if no chest was placed NEXT */ |
137 | continue; /* if no chest was placed NEXT */ |
141 | if (treasureoptions & (DOORED | HIDDEN)) |
138 | if (treasureoptions & (DOORED | HIDDEN)) |
142 | { |
139 | { |
143 | doorlist = find_doors_in_room (map, i, j, RP); |
140 | doorlist = find_doors_in_room (map, i, j, RP); |
144 | lock_and_hide_doors (doorlist, map, treasureoptions, RP); |
141 | lock_and_hide_doors (doorlist, map, treasureoptions, RP); |
145 | free (doorlist); |
142 | free (doorlist); |
146 | } |
143 | } |
147 | } |
144 | } |
148 | } |
145 | } |
149 | } |
146 | } |
150 | break; |
147 | break; |
151 | } |
148 | } |
152 | default: |
149 | default: |
153 | { |
150 | { |
154 | int i, j, tries; |
151 | int i, j, tries; |
155 | object *chest; |
152 | object *chest; |
156 | object **doorlist; |
153 | object **doorlist; |
157 | |
154 | |
158 | i = j = -1; |
155 | i = j = -1; |
159 | tries = 0; |
156 | tries = 0; |
160 | while (i == -1 && tries < 100) |
157 | while (i == -1 && tries < 100) |
161 | { |
158 | { |
162 | i = RANDOM () % (RP->Xsize - 2) + 1; |
159 | i = RANDOM () % (RP->Xsize - 2) + 1; |
163 | j = RANDOM () % (RP->Ysize - 2) + 1; |
160 | j = RANDOM () % (RP->Ysize - 2) + 1; |
164 | find_enclosed_spot (map, &i, &j, RP); |
161 | find_enclosed_spot (map, &i, &j, RP); |
165 | if (wall_blocked (map, i, j)) |
162 | if (wall_blocked (map, i, j)) |
166 | i = -1; |
163 | i = -1; |
167 | tries++; |
164 | tries++; |
168 | } |
165 | } |
169 | chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures, RP); |
166 | chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures, RP); |
170 | if (!chest) |
167 | if (!chest) |
171 | return; |
168 | return; |
172 | i = chest->x; |
169 | i = chest->x; |
173 | j = chest->y; |
170 | j = chest->y; |
174 | if (treasureoptions & (DOORED | HIDDEN)) |
171 | if (treasureoptions & (DOORED | HIDDEN)) |
175 | { |
172 | { |
176 | doorlist = surround_by_doors (map, layout, i, j, treasureoptions); |
173 | doorlist = surround_by_doors (map, layout, i, j, treasureoptions); |
177 | lock_and_hide_doors (doorlist, map, treasureoptions, RP); |
174 | lock_and_hide_doors (doorlist, map, treasureoptions, RP); |
178 | free (doorlist); |
175 | free (doorlist); |
179 | } |
176 | } |
180 | } |
177 | } |
181 | } |
178 | } |
182 | } |
179 | } |
183 | else |
180 | else |
184 | { /* DIFFUSE treasure layout */ |
181 | { /* DIFFUSE treasure layout */ |
185 | int ti, i, j; |
182 | int ti, i, j; |
… | |
… | |
191 | place_chest (treasureoptions, i, j, map, style_map, 1, RP); |
188 | place_chest (treasureoptions, i, j, map, style_map, 1, RP); |
192 | } |
189 | } |
193 | } |
190 | } |
194 | } |
191 | } |
195 | |
192 | |
196 | |
|
|
197 | |
|
|
198 | /* put a chest into the map, near x and y, with the treasure style |
193 | /* put a chest into the map, near x and y, with the treasure style |
199 | determined (may be null, or may be a treasure list from lib/treasures, |
194 | determined (may be null, or may be a treasure list from lib/treasures, |
200 | if the global variable "treasurestyle" is set to that treasure list's name */ |
195 | if the global variable "treasurestyle" is set to that treasure list's name */ |
201 | |
196 | |
202 | object * |
197 | object * |
203 | place_chest (int treasureoptions, int x, int y, maptile *map, maptile *style_map, int n_treasures, RMParms * RP) |
198 | place_chest (int treasureoptions, int x, int y, maptile *map, maptile *style_map, int n_treasures, random_map_params *RP) |
204 | { |
199 | { |
205 | object *the_chest; |
200 | object *the_chest; |
206 | int i, xl, yl; |
201 | int i, xl, yl; |
207 | |
202 | |
208 | the_chest = get_archetype ("chest"); /* was "chest_2" */ |
203 | the_chest = get_archetype ("chest"); /* was "chest_2" */ |
… | |
… | |
212 | if (i == -1) |
207 | if (i == -1) |
213 | { |
208 | { |
214 | the_chest->destroy (); |
209 | the_chest->destroy (); |
215 | return NULL; |
210 | return NULL; |
216 | } |
211 | } |
|
|
212 | |
217 | xl = x + freearr_x[i]; |
213 | xl = x + freearr_x[i]; |
218 | yl = y + freearr_y[i]; |
214 | yl = y + freearr_y[i]; |
219 | |
215 | |
220 | /* if the placement is blocked, return a fail. */ |
216 | /* if the placement is blocked, return a fail. */ |
221 | if (wall_blocked (map, xl, yl)) |
217 | if (wall_blocked (map, xl, yl)) |
222 | return 0; |
218 | return 0; |
223 | |
|
|
224 | |
219 | |
225 | /* put the treasures in the chest. */ |
220 | /* put the treasures in the chest. */ |
226 | /* if(style_map) { */ |
221 | /* if(style_map) { */ |
227 | #if 0 /* don't use treasure style maps for now! */ |
222 | #if 0 /* don't use treasure style maps for now! */ |
228 | int ti; |
223 | int ti; |
… | |
… | |
241 | { /* use the style map */ |
236 | { /* use the style map */ |
242 | the_chest->randomitems = tlist; |
237 | the_chest->randomitems = tlist; |
243 | the_chest->stats.hp = n_treasures; |
238 | the_chest->stats.hp = n_treasures; |
244 | } |
239 | } |
245 | #endif |
240 | #endif |
246 | else |
|
|
247 | { /* neither style_map no treasure list given */ |
241 | { /* neither style_map no treasure list given */ |
248 | treasurelist *tlist = find_treasurelist ("chest"); |
242 | treasurelist *tlist = find_treasurelist ("chest"); |
249 | |
243 | |
250 | the_chest->randomitems = tlist; |
244 | the_chest->randomitems = tlist; |
251 | the_chest->stats.hp = n_treasures; |
245 | the_chest->stats.hp = n_treasures; |
… | |
… | |
279 | the lockcode. It's not worth bothering to lock the chest if |
273 | the lockcode. It's not worth bothering to lock the chest if |
280 | there's only 1 treasure.... */ |
274 | there's only 1 treasure.... */ |
281 | |
275 | |
282 | if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) |
276 | if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) |
283 | { |
277 | { |
284 | char keybuf[256]; |
278 | char keybuf[1024]; |
285 | |
279 | |
286 | sprintf (keybuf, "%d", (int) RANDOM ()); |
280 | sprintf (keybuf, "%d", (int) RANDOM ()); |
287 | the_chest->slaying = keybuf; |
281 | the_chest->slaying = keybuf; |
288 | keyplace (map, x, y, keybuf, PASS_DOORS, 1, RP); |
282 | keyplace (map, x, y, keybuf, PASS_DOORS, 1, RP); |
289 | } |
283 | } |
… | |
… | |
297 | |
291 | |
298 | |
292 | |
299 | /* finds the closest monster and returns him, regardless of doors |
293 | /* finds the closest monster and returns him, regardless of doors |
300 | or walls */ |
294 | or walls */ |
301 | object * |
295 | object * |
302 | find_closest_monster (maptile *map, int x, int y, RMParms * RP) |
296 | find_closest_monster (maptile *map, int x, int y, random_map_params *RP) |
303 | { |
297 | { |
304 | int i; |
298 | int i; |
305 | |
299 | |
306 | for (i = 0; i < SIZEOFFREE; i++) |
300 | for (i = 0; i < SIZEOFFREE; i++) |
307 | { |
301 | { |
… | |
… | |
312 | /* boundscheck */ |
306 | /* boundscheck */ |
313 | if (lx >= 0 && ly >= 0 && lx < RP->Xsize && ly < RP->Ysize) |
307 | if (lx >= 0 && ly >= 0 && lx < RP->Xsize && ly < RP->Ysize) |
314 | /* don't bother searching this square unless the map says life exists. */ |
308 | /* don't bother searching this square unless the map says life exists. */ |
315 | if (GET_MAP_FLAGS (map, lx, ly) & P_IS_ALIVE) |
309 | if (GET_MAP_FLAGS (map, lx, ly) & P_IS_ALIVE) |
316 | { |
310 | { |
317 | object *the_monster = get_map_ob (map, lx, ly); |
311 | object *the_monster = GET_MAP_OB (map, lx, ly); |
318 | |
312 | |
319 | for (; the_monster != NULL && (!QUERY_FLAG (the_monster, FLAG_MONSTER)); the_monster = the_monster->above); |
313 | for (; the_monster != NULL && (!QUERY_FLAG (the_monster, FLAG_MONSTER)); the_monster = the_monster->above); |
320 | if (the_monster && QUERY_FLAG (the_monster, FLAG_MONSTER)) |
314 | if (the_monster && QUERY_FLAG (the_monster, FLAG_MONSTER)) |
321 | return the_monster; |
315 | return the_monster; |
322 | } |
316 | } |
… | |
… | |
334 | it will place 2-4 keys regardless of what nkeys is provided nkeys > 1. |
328 | it will place 2-4 keys regardless of what nkeys is provided nkeys > 1. |
335 | |
329 | |
336 | The idea is that you call keyplace on x,y where a door is, and it'll make |
330 | The idea is that you call keyplace on x,y where a door is, and it'll make |
337 | sure a key is placed on both sides of the door. |
331 | sure a key is placed on both sides of the door. |
338 | */ |
332 | */ |
339 | |
|
|
340 | int |
333 | int |
341 | keyplace (maptile *map, int x, int y, char *keycode, int door_flag, int n_keys, RMParms * RP) |
334 | keyplace (maptile *map, int x, int y, char *keycode, int door_flag, int n_keys, random_map_params *RP) |
342 | { |
335 | { |
343 | int i, j; |
336 | int i, j; |
344 | int kx, ky; |
337 | int kx = 0, ky = 0; |
345 | object *the_keymaster; /* the monster that gets the key. */ |
338 | object *the_keymaster; /* the monster that gets the key. */ |
346 | object *the_key; |
339 | object *the_key; |
347 | |
340 | |
348 | /* get a key and set its keycode */ |
341 | /* get a key and set its keycode */ |
349 | the_key = get_archetype ("key2"); |
342 | the_key = get_archetype ("key2"); |
… | |
… | |
351 | |
344 | |
352 | if (door_flag == PASS_DOORS) |
345 | if (door_flag == PASS_DOORS) |
353 | { |
346 | { |
354 | int tries = 0; |
347 | int tries = 0; |
355 | |
348 | |
356 | the_keymaster = NULL; |
349 | the_keymaster = 0; |
357 | while (tries < 15 && the_keymaster == NULL) |
350 | while (tries < 15 && !the_keymaster) |
358 | { |
351 | { |
359 | i = (RANDOM () % (RP->Xsize - 2)) + 1; |
352 | i = (RANDOM () % (RP->Xsize - 2)) + 1; |
360 | j = (RANDOM () % (RP->Ysize - 2)) + 1; |
353 | j = (RANDOM () % (RP->Ysize - 2)) + 1; |
361 | tries++; |
354 | tries++; |
362 | the_keymaster = find_closest_monster (map, i, j, RP); |
355 | the_keymaster = find_closest_monster (map, i, j, RP); |
363 | } |
356 | } |
|
|
357 | |
364 | /* if we don't find a good keymaster, drop the key on the ground. */ |
358 | /* if we don't find a good keymaster, drop the key on the ground. */ |
365 | if (the_keymaster == NULL) |
359 | if (!the_keymaster) |
366 | { |
360 | { |
367 | int freeindex; |
361 | int freeindex; |
368 | |
362 | |
369 | freeindex = -1; |
363 | freeindex = -1; |
370 | for (tries = 0; tries < 15 && freeindex == -1; tries++) |
364 | for (tries = 0; tries < 15 && freeindex == -1; tries++) |
371 | { |
365 | { |
372 | kx = (RANDOM () % (RP->Xsize - 2)) + 1; |
366 | kx = (RANDOM () % (RP->Xsize - 2)) + 1; |
373 | ky = (RANDOM () % (RP->Ysize - 2)) + 1; |
367 | ky = (RANDOM () % (RP->Ysize - 2)) + 1; |
374 | freeindex = find_first_free_spot (the_key, map, kx, ky); |
368 | freeindex = find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1); |
375 | } |
369 | } |
|
|
370 | |
|
|
371 | // can freeindex ever be < 0? |
376 | if (freeindex != -1) |
372 | if (freeindex >= 0) |
377 | { |
373 | { |
378 | kx += freearr_x[freeindex]; |
374 | kx += freearr_x [freeindex]; |
379 | ky += freearr_y[freeindex]; |
375 | ky += freearr_y [freeindex]; |
380 | } |
376 | } |
381 | } |
377 | } |
382 | } |
378 | } |
383 | else |
379 | else |
384 | { /* NO_PASS_DOORS --we have to work harder. */ |
380 | { /* NO_PASS_DOORS --we have to work harder. */ |
… | |
… | |
386 | NO_PASS_DOORS is set. */ |
382 | NO_PASS_DOORS is set. */ |
387 | if (n_keys == 1) |
383 | if (n_keys == 1) |
388 | { |
384 | { |
389 | if (wall_blocked (map, x, y)) |
385 | if (wall_blocked (map, x, y)) |
390 | return 0; |
386 | return 0; |
|
|
387 | |
391 | the_keymaster = find_monster_in_room (map, x, y, RP); |
388 | the_keymaster = find_monster_in_room (map, x, y, RP); |
392 | if (the_keymaster == NULL) /* if fail, find a spot to drop the key. */ |
389 | if (!the_keymaster) /* if fail, find a spot to drop the key. */ |
393 | find_spot_in_room (map, x, y, &kx, &ky, RP); |
390 | find_spot_in_room (map, x, y, &kx, &ky, RP); |
394 | } |
391 | } |
395 | else |
392 | else |
396 | { |
393 | { |
397 | int sum = 0; /* count how many keys we actually place */ |
394 | int sum = 0; /* count how many keys we actually place */ |
… | |
… | |
399 | /* I'm lazy, so just try to place in all 4 directions. */ |
396 | /* I'm lazy, so just try to place in all 4 directions. */ |
400 | sum += keyplace (map, x + 1, y, keycode, NO_PASS_DOORS, 1, RP); |
397 | sum += keyplace (map, x + 1, y, keycode, NO_PASS_DOORS, 1, RP); |
401 | sum += keyplace (map, x, y + 1, keycode, NO_PASS_DOORS, 1, RP); |
398 | sum += keyplace (map, x, y + 1, keycode, NO_PASS_DOORS, 1, RP); |
402 | sum += keyplace (map, x - 1, y, keycode, NO_PASS_DOORS, 1, RP); |
399 | sum += keyplace (map, x - 1, y, keycode, NO_PASS_DOORS, 1, RP); |
403 | sum += keyplace (map, x, y - 1, keycode, NO_PASS_DOORS, 1, RP); |
400 | sum += keyplace (map, x, y - 1, keycode, NO_PASS_DOORS, 1, RP); |
|
|
401 | |
404 | if (sum < 2) /* we might have made a disconnected map-place more keys. */ |
402 | if (sum < 2) /* we might have made a disconnected map-place more keys. */ |
405 | { /* diagnoally this time. */ |
403 | { /* diagonally this time. */ |
406 | keyplace (map, x + 1, y + 1, keycode, NO_PASS_DOORS, 1, RP); |
404 | keyplace (map, x + 1, y + 1, keycode, NO_PASS_DOORS, 1, RP); |
407 | keyplace (map, x + 1, y - 1, keycode, NO_PASS_DOORS, 1, RP); |
405 | keyplace (map, x + 1, y - 1, keycode, NO_PASS_DOORS, 1, RP); |
408 | keyplace (map, x - 1, y + 1, keycode, NO_PASS_DOORS, 1, RP); |
406 | keyplace (map, x - 1, y + 1, keycode, NO_PASS_DOORS, 1, RP); |
409 | keyplace (map, x - 1, y - 1, keycode, NO_PASS_DOORS, 1, RP); |
407 | keyplace (map, x - 1, y - 1, keycode, NO_PASS_DOORS, 1, RP); |
410 | } |
408 | } |
|
|
409 | |
411 | return 1; |
410 | return 1; |
412 | } |
411 | } |
413 | } |
412 | } |
414 | |
413 | |
415 | if (the_keymaster == NULL) |
414 | if (!the_keymaster) |
416 | { |
415 | { |
417 | the_key->x = kx; |
416 | the_key->x = kx; |
418 | the_key->y = ky; |
417 | the_key->y = ky; |
419 | insert_ob_in_map (the_key, map, NULL, 0); |
418 | insert_ob_in_map (the_key, map, NULL, 0); |
420 | return 1; |
419 | return 1; |
… | |
… | |
432 | |
431 | |
433 | /* a recursive routine which will return a monster, eventually,if there is one. |
432 | /* a recursive routine which will return a monster, eventually,if there is one. |
434 | it does a check-off on the layout, converting 0's to 1's */ |
433 | it does a check-off on the layout, converting 0's to 1's */ |
435 | |
434 | |
436 | object * |
435 | object * |
437 | find_monster_in_room_recursive (char **layout, maptile *map, int x, int y, RMParms * RP) |
436 | find_monster_in_room_recursive (char **layout, maptile *map, int x, int y, random_map_params *RP) |
438 | { |
437 | { |
439 | int i, j; |
438 | int i, j; |
440 | |
439 | |
441 | /* if we've found a monster already, leave */ |
440 | /* if we've found a monster already, leave */ |
442 | if (theMonsterToFind != NULL) |
441 | if (theMonsterToFind != NULL) |
… | |
… | |
453 | /* check the current square for a monster. If there is one, |
452 | /* check the current square for a monster. If there is one, |
454 | set theMonsterToFind and return it. */ |
453 | set theMonsterToFind and return it. */ |
455 | layout[x][y] = 1; |
454 | layout[x][y] = 1; |
456 | if (GET_MAP_FLAGS (map, x, y) & P_IS_ALIVE) |
455 | if (GET_MAP_FLAGS (map, x, y) & P_IS_ALIVE) |
457 | { |
456 | { |
458 | object *the_monster = get_map_ob (map, x, y); |
457 | object *the_monster = GET_MAP_OB (map, x, y); |
459 | |
458 | |
460 | /* check off this point */ |
459 | /* check off this point */ |
461 | for (; the_monster != NULL && (!QUERY_FLAG (the_monster, FLAG_ALIVE)); the_monster = the_monster->above); |
460 | for (; the_monster != NULL && (!QUERY_FLAG (the_monster, FLAG_ALIVE)); the_monster = the_monster->above); |
462 | if (the_monster && QUERY_FLAG (the_monster, FLAG_ALIVE)) |
461 | if (the_monster && QUERY_FLAG (the_monster, FLAG_ALIVE)) |
463 | { |
462 | { |
… | |
… | |
465 | return theMonsterToFind; |
464 | return theMonsterToFind; |
466 | } |
465 | } |
467 | } |
466 | } |
468 | |
467 | |
469 | /* now search all the 8 squares around recursively for a monster,in random order */ |
468 | /* now search all the 8 squares around recursively for a monster,in random order */ |
470 | for (i = RANDOM () % 8, j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) |
469 | for (i = rndm (8), j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) |
471 | { |
470 | { |
472 | theMonsterToFind = find_monster_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); |
471 | theMonsterToFind = find_monster_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); |
473 | if (theMonsterToFind != NULL) |
472 | if (theMonsterToFind != NULL) |
474 | return theMonsterToFind; |
473 | return theMonsterToFind; |
475 | } |
474 | } |
… | |
… | |
479 | |
478 | |
480 | /* sets up some data structures: the _recursive form does the |
479 | /* sets up some data structures: the _recursive form does the |
481 | real work. */ |
480 | real work. */ |
482 | |
481 | |
483 | object * |
482 | object * |
484 | find_monster_in_room (maptile *map, int x, int y, RMParms * RP) |
483 | find_monster_in_room (maptile *map, int x, int y, random_map_params *RP) |
485 | { |
484 | { |
486 | char **layout2; |
485 | char **layout2; |
487 | int i, j; |
486 | int i, j; |
488 | |
487 | |
489 | theMonsterToFind = 0; |
488 | theMonsterToFind = 0; |
… | |
… | |
508 | free (layout2); |
507 | free (layout2); |
509 | |
508 | |
510 | return theMonsterToFind; |
509 | return theMonsterToFind; |
511 | } |
510 | } |
512 | |
511 | |
513 | |
|
|
514 | |
|
|
515 | |
|
|
516 | /* a datastructure needed by find_spot_in_room and find_spot_in_room_recursive */ |
512 | /* a datastructure needed by find_spot_in_room and find_spot_in_room_recursive */ |
517 | int *room_free_spots_x; |
513 | int *room_free_spots_x; |
518 | int *room_free_spots_y; |
514 | int *room_free_spots_y; |
519 | int number_of_free_spots_in_room; |
515 | int number_of_free_spots_in_room; |
520 | |
516 | |
521 | /* the workhorse routine, which finds the free spots in a room: |
517 | /* the workhorse routine, which finds the free spots in a room: |
522 | a datastructure of free points is set up, and a position chosen from |
518 | a datastructure of free points is set up, and a position chosen from |
523 | that datastructure. */ |
519 | that datastructure. */ |
524 | |
|
|
525 | void |
520 | void |
526 | find_spot_in_room_recursive (char **layout, int x, int y, RMParms * RP) |
521 | find_spot_in_room_recursive (char **layout, int x, int y, random_map_params *RP) |
527 | { |
522 | { |
528 | int i, j; |
523 | int i, j; |
529 | |
524 | |
530 | /* bounds check x and y */ |
525 | /* bounds check x and y */ |
531 | if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) |
526 | if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) |
… | |
… | |
540 | /* check off this point */ |
535 | /* check off this point */ |
541 | layout[x][y] = 1; |
536 | layout[x][y] = 1; |
542 | room_free_spots_x[number_of_free_spots_in_room] = x; |
537 | room_free_spots_x[number_of_free_spots_in_room] = x; |
543 | room_free_spots_y[number_of_free_spots_in_room] = y; |
538 | room_free_spots_y[number_of_free_spots_in_room] = y; |
544 | number_of_free_spots_in_room++; |
539 | number_of_free_spots_in_room++; |
|
|
540 | |
545 | /* now search all the 8 squares around recursively for free spots,in random order */ |
541 | /* now search all the 8 squares around recursively for free spots,in random order */ |
546 | for (i = RANDOM () % 8, j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) |
542 | for (i = rndm (8), j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) |
547 | { |
|
|
548 | find_spot_in_room_recursive (layout, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); |
543 | find_spot_in_room_recursive (layout, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); |
549 | } |
|
|
550 | |
544 | |
551 | } |
545 | } |
552 | |
546 | |
553 | /* find a random non-blocked spot in this room to drop a key. */ |
547 | /* find a random non-blocked spot in this room to drop a key. */ |
554 | void |
548 | void |
555 | find_spot_in_room (maptile *map, int x, int y, int *kx, int *ky, RMParms * RP) |
549 | find_spot_in_room (maptile *map, int x, int y, int *kx, int *ky, random_map_params *RP) |
556 | { |
550 | { |
557 | char **layout2; |
551 | char **layout2; |
558 | int i, j; |
552 | int i, j; |
559 | |
553 | |
560 | number_of_free_spots_in_room = 0; |
554 | number_of_free_spots_in_room = 0; |
… | |
… | |
565 | /* allocate and copy the layout, converting C to 0. */ |
559 | /* allocate and copy the layout, converting C to 0. */ |
566 | for (i = 0; i < RP->Xsize; i++) |
560 | for (i = 0; i < RP->Xsize; i++) |
567 | { |
561 | { |
568 | layout2[i] = (char *) calloc (sizeof (char), RP->Ysize); |
562 | layout2[i] = (char *) calloc (sizeof (char), RP->Ysize); |
569 | for (j = 0; j < RP->Ysize; j++) |
563 | for (j = 0; j < RP->Ysize; j++) |
570 | { |
|
|
571 | if (wall_blocked (map, i, j)) |
564 | if (wall_blocked (map, i, j)) |
572 | layout2[i][j] = '#'; |
565 | layout2[i][j] = '#'; |
573 | } |
|
|
574 | } |
566 | } |
575 | |
567 | |
576 | /* setup num_free_spots and room_free_spots */ |
568 | /* setup num_free_spots and room_free_spots */ |
577 | find_spot_in_room_recursive (layout2, x, y, RP); |
569 | find_spot_in_room_recursive (layout2, x, y, RP); |
578 | |
570 | |
… | |
… | |
583 | *ky = room_free_spots_y[i]; |
575 | *ky = room_free_spots_y[i]; |
584 | } |
576 | } |
585 | |
577 | |
586 | /* deallocate the temp. layout */ |
578 | /* deallocate the temp. layout */ |
587 | for (i = 0; i < RP->Xsize; i++) |
579 | for (i = 0; i < RP->Xsize; i++) |
588 | { |
|
|
589 | free (layout2[i]); |
580 | free (layout2[i]); |
590 | } |
581 | |
591 | free (layout2); |
582 | free (layout2); |
592 | free (room_free_spots_x); |
583 | free (room_free_spots_x); |
593 | free (room_free_spots_y); |
584 | free (room_free_spots_y); |
594 | } |
585 | } |
595 | |
586 | |
596 | |
587 | |
597 | /* searches the map for a spot with walls around it. The more |
588 | /* searches the map for a spot with walls around it. The more |
598 | walls the better, but it'll settle for 1 wall, or even 0, but |
589 | walls the better, but it'll settle for 1 wall, or even 0, but |
599 | it'll return 0 if no FREE spots are found.*/ |
590 | it'll return 0 if no FREE spots are found.*/ |
600 | |
|
|
601 | void |
591 | void |
602 | find_enclosed_spot (maptile *map, int *cx, int *cy, RMParms * RP) |
592 | find_enclosed_spot (maptile *map, int *cx, int *cy, random_map_params *RP) |
603 | { |
593 | { |
604 | int x, y; |
594 | int x, y; |
605 | int i; |
595 | int i; |
606 | |
596 | |
607 | x = *cx; |
597 | x = *cx; |
… | |
… | |
656 | *cy = ly; |
646 | *cy = ly; |
657 | return; |
647 | return; |
658 | } |
648 | } |
659 | } |
649 | } |
660 | /* give up and return the closest free spot. */ |
650 | /* give up and return the closest free spot. */ |
661 | i = find_first_free_spot (&archetype::find ("chest")->clone, map, x, y); |
651 | i = find_free_spot (&archetype::find ("chest")->clone, map, x, y, 1, SIZEOFFREE1 + 1); |
662 | if (i != -1 && i <= SIZEOFFREE1) |
652 | |
|
|
653 | if (i != -1) |
663 | { |
654 | { |
664 | *cx = x + freearr_x[i]; |
655 | *cx = x + freearr_x[i]; |
665 | *cy = y + freearr_y[i]; |
656 | *cy = y + freearr_y[i]; |
666 | return; |
657 | } |
|
|
658 | else |
667 | } |
659 | { |
668 | /* indicate failure */ |
660 | /* indicate failure */ |
|
|
661 | *cx = -1; |
669 | *cx = *cy = -1; |
662 | *cy = -1; |
|
|
663 | } |
670 | } |
664 | } |
671 | |
|
|
672 | |
665 | |
673 | void |
666 | void |
674 | remove_monsters (int x, int y, maptile *map) |
667 | remove_monsters (int x, int y, maptile *map) |
675 | { |
668 | { |
676 | object *tmp; |
669 | object *tmp; |
677 | |
670 | |
678 | for (tmp = get_map_ob (map, x, y); tmp != NULL; tmp = tmp->above) |
671 | for (tmp = GET_MAP_OB (map, x, y); tmp; tmp = tmp->above) |
679 | if (QUERY_FLAG (tmp, FLAG_ALIVE)) |
672 | if (QUERY_FLAG (tmp, FLAG_ALIVE)) |
680 | { |
673 | { |
681 | if (tmp->head) |
674 | if (tmp->head) |
682 | tmp = tmp->head; |
675 | tmp = tmp->head; |
683 | tmp->remove (); |
676 | tmp->remove (); |
684 | tmp->destroy (); |
677 | tmp->destroy (); |
685 | tmp = get_map_ob (map, x, y); |
678 | tmp = GET_MAP_OB (map, x, y); |
686 | if (tmp == NULL) |
679 | if (tmp == NULL) |
687 | break; |
680 | break; |
688 | }; |
681 | }; |
689 | } |
682 | } |
690 | |
683 | |
691 | |
|
|
692 | /* surrounds the point x,y by doors, so as to enclose something, like |
684 | /* surrounds the point x,y by doors, so as to enclose something, like |
693 | a chest. It only goes as far as the 8 squares surrounding, and |
685 | a chest. It only goes as far as the 8 squares surrounding, and |
694 | it'll remove any monsters it finds.*/ |
686 | it'll remove any monsters it finds.*/ |
695 | |
|
|
696 | object ** |
687 | object ** |
697 | surround_by_doors (maptile *map, char **layout, int x, int y, int opts) |
688 | surround_by_doors (maptile *map, char **layout, int x, int y, int opts) |
698 | { |
689 | { |
699 | int i; |
690 | int i; |
700 | char *doors[2]; |
691 | char *doors[2]; |
… | |
… | |
717 | /* place doors in all the 8 adjacent unblocked squares. */ |
708 | /* place doors in all the 8 adjacent unblocked squares. */ |
718 | for (i = 1; i < 9; i++) |
709 | for (i = 1; i < 9; i++) |
719 | { |
710 | { |
720 | int x1 = x + freearr_x[i], y1 = y + freearr_y[i]; |
711 | int x1 = x + freearr_x[i], y1 = y + freearr_y[i]; |
721 | |
712 | |
722 | if (!wall_blocked (map, x1, y1) || layout[x1][y1] == '>') |
713 | if (!wall_blocked (map, x1, y1) && layout[x1][y1] == '>') |
723 | { /* place a door */ |
714 | { /* place a door */ |
|
|
715 | remove_monsters (x1, y1, map); |
|
|
716 | |
724 | object *new_door = get_archetype ((freearr_x[i] == 0) ? doors[1] : doors[0]); |
717 | object *new_door = get_archetype (freearr_x[i] == 0 ? doors[1] : doors[0]); |
725 | |
718 | map->insert (new_door, x1, y1); |
726 | new_door->x = x + freearr_x[i]; |
|
|
727 | new_door->y = y + freearr_y[i]; |
|
|
728 | remove_monsters (new_door->x, new_door->y, map); |
|
|
729 | insert_ob_in_map (new_door, map, NULL, 0); |
|
|
730 | doorlist[ndoors_made] = new_door; |
719 | doorlist[ndoors_made] = new_door; |
731 | ndoors_made++; |
720 | ndoors_made++; |
732 | } |
721 | } |
733 | } |
722 | } |
|
|
723 | |
734 | return doorlist; |
724 | return doorlist; |
735 | } |
725 | } |
736 | |
726 | |
737 | |
727 | |
738 | /* returns the first door in this square, or NULL if there isn't a door. */ |
728 | /* returns the first door in this square, or NULL if there isn't a door. */ |
739 | object * |
729 | object * |
740 | door_in_square (maptile *map, int x, int y) |
730 | door_in_square (maptile *map, int x, int y) |
741 | { |
731 | { |
742 | object *tmp; |
732 | object *tmp; |
743 | |
733 | |
744 | for (tmp = get_map_ob (map, x, y); tmp != NULL; tmp = tmp->above) |
734 | for (tmp = GET_MAP_OB (map, x, y); tmp != NULL; tmp = tmp->above) |
745 | if (tmp->type == DOOR || tmp->type == LOCKED_DOOR) |
735 | if (tmp->type == DOOR || tmp->type == LOCKED_DOOR) |
746 | return tmp; |
736 | return tmp; |
747 | return NULL; |
737 | return NULL; |
748 | } |
738 | } |
749 | |
739 | |
750 | |
740 | |
751 | /* the workhorse routine, which finds the doors in a room */ |
741 | /* the workhorse routine, which finds the doors in a room */ |
752 | void |
742 | void |
753 | find_doors_in_room_recursive (char **layout, maptile *map, int x, int y, object **doorlist, int *ndoors, RMParms * RP) |
743 | find_doors_in_room_recursive (char **layout, maptile *map, int x, int y, object **doorlist, int *ndoors, random_map_params *RP) |
754 | { |
744 | { |
755 | int i, j; |
745 | int i, j; |
756 | object *door; |
746 | object *door; |
757 | |
747 | |
758 | /* bounds check x and y */ |
748 | /* bounds check x and y */ |
… | |
… | |
766 | /* check off this point */ |
756 | /* check off this point */ |
767 | if (layout[x][y] == '#') |
757 | if (layout[x][y] == '#') |
768 | { /* there could be a door here */ |
758 | { /* there could be a door here */ |
769 | layout[x][y] = 1; |
759 | layout[x][y] = 1; |
770 | door = door_in_square (map, x, y); |
760 | door = door_in_square (map, x, y); |
771 | if (door != NULL) |
761 | if (door) |
772 | { |
762 | { |
773 | doorlist[*ndoors] = door; |
763 | doorlist[*ndoors] = door; |
774 | if (*ndoors > 254) /* eek! out of memory */ |
764 | if (*ndoors > 1022) /* eek! out of memory */ |
775 | { |
765 | { |
776 | LOG (llevError, "find_doors_in_room_recursive:Too many doors for memory allocated!\n"); |
766 | LOG (llevError, "find_doors_in_room_recursive:Too many doors for memory allocated!\n"); |
777 | return; |
767 | return; |
778 | } |
768 | } |
|
|
769 | |
779 | *ndoors = *ndoors + 1; |
770 | *ndoors = *ndoors + 1; |
780 | } |
771 | } |
781 | } |
772 | } |
782 | else |
773 | else |
783 | { |
774 | { |
784 | layout[x][y] = 1; |
775 | layout[x][y] = 1; |
785 | /* now search all the 8 squares around recursively for free spots,in random order */ |
776 | /* now search all the 8 squares around recursively for free spots,in random order */ |
786 | for (i = RANDOM () % 8, j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) |
777 | for (i = rndm (8), j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) |
787 | { |
|
|
788 | find_doors_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], doorlist, ndoors, RP); |
778 | find_doors_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], doorlist, ndoors, RP); |
789 | } |
|
|
790 | } |
779 | } |
791 | } |
780 | } |
792 | |
781 | |
793 | /* find a random non-blocked spot in this room to drop a key. */ |
782 | /* find a random non-blocked spot in this room to drop a key. */ |
794 | object ** |
783 | object ** |
795 | find_doors_in_room (maptile *map, int x, int y, RMParms * RP) |
784 | find_doors_in_room (maptile *map, int x, int y, random_map_params *RP) |
796 | { |
785 | { |
797 | char **layout2; |
786 | char **layout2; |
798 | object **doorlist; |
787 | object **doorlist; |
799 | int i, j; |
788 | int i, j; |
800 | int ndoors = 0; |
789 | int ndoors = 0; |
801 | |
790 | |
802 | doorlist = (object **) calloc (sizeof (int), 256); |
791 | doorlist = (object **) calloc (sizeof (int), 1024); |
803 | |
|
|
804 | |
792 | |
805 | layout2 = (char **) calloc (sizeof (char *), RP->Xsize); |
793 | layout2 = (char **) calloc (sizeof (char *), RP->Xsize); |
806 | /* allocate and copy the layout, converting C to 0. */ |
794 | /* allocate and copy the layout, converting C to 0. */ |
807 | for (i = 0; i < RP->Xsize; i++) |
795 | for (i = 0; i < RP->Xsize; i++) |
808 | { |
796 | { |
… | |
… | |
817 | /* setup num_free_spots and room_free_spots */ |
805 | /* setup num_free_spots and room_free_spots */ |
818 | find_doors_in_room_recursive (layout2, map, x, y, doorlist, &ndoors, RP); |
806 | find_doors_in_room_recursive (layout2, map, x, y, doorlist, &ndoors, RP); |
819 | |
807 | |
820 | /* deallocate the temp. layout */ |
808 | /* deallocate the temp. layout */ |
821 | for (i = 0; i < RP->Xsize; i++) |
809 | for (i = 0; i < RP->Xsize; i++) |
822 | { |
|
|
823 | free (layout2[i]); |
810 | free (layout2[i]); |
824 | } |
811 | |
825 | free (layout2); |
812 | free (layout2); |
826 | return doorlist; |
813 | return doorlist; |
827 | } |
814 | } |
828 | |
815 | |
829 | |
816 | |
830 | |
817 | |
831 | /* locks and/or hides all the doors in doorlist, or does nothing if |
818 | /* locks and/or hides all the doors in doorlist, or does nothing if |
832 | opts doesn't say to lock/hide doors. */ |
819 | opts doesn't say to lock/hide doors. */ |
833 | |
820 | |
834 | void |
821 | void |
835 | lock_and_hide_doors (object **doorlist, maptile *map, int opts, RMParms * RP) |
822 | lock_and_hide_doors (object **doorlist, maptile *map, int opts, random_map_params *RP) |
836 | { |
823 | { |
837 | object *door; |
824 | object *door; |
838 | int i; |
825 | int i; |
839 | |
826 | |
840 | /* lock the doors and hide the keys. */ |
827 | /* lock the doors and hide the keys. */ |
… | |
… | |
842 | if (opts & DOORED) |
829 | if (opts & DOORED) |
843 | { |
830 | { |
844 | for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) |
831 | for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) |
845 | { |
832 | { |
846 | object *new_door = get_archetype ("locked_door1"); |
833 | object *new_door = get_archetype ("locked_door1"); |
847 | char keybuf[256]; |
834 | char keybuf[1024]; |
848 | |
835 | |
849 | door = doorlist[i]; |
836 | door = doorlist[i]; |
850 | new_door->face = door->face; |
837 | new_door->face = door->face; |
851 | new_door->x = door->x; |
838 | new_door->x = door->x; |
852 | new_door->y = door->y; |
839 | new_door->y = door->y; |