1 | /* |
1 | /* |
|
|
2 | <<<<<<< treasure.C |
|
|
3 | * CrossFire, A Multiplayer game |
|
|
4 | * |
|
|
5 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
|
|
6 | * Copyright (C) 2001 Mark Wedel & Crossfire Development Team |
|
|
7 | * Copyright (C) 1992 Frank Tore Johansen |
|
|
8 | * |
|
|
9 | * This program is free software; you can redistribute it and/or modify |
|
|
10 | * it under the terms of the GNU General Public License as published by |
|
|
11 | * the Free Software Foundation; either version 2 of the License, or |
|
|
12 | * (at your option) any later version. |
|
|
13 | * |
|
|
14 | * This program is distributed in the hope that it will be useful, |
|
|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
17 | * GNU General Public License for more details. |
|
|
18 | * |
|
|
19 | * You should have received a copy of the GNU General Public License |
|
|
20 | * along with this program; if not, write to the Free Software |
|
|
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
|
22 | * |
|
|
23 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
|
|
24 | */ |
|
|
25 | ======= |
2 | * CrossFire, A Multiplayer game for X-windows |
26 | * CrossFire, A Multiplayer game for X-windows |
3 | * |
27 | * |
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
28 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
5 | * Copyright (C) 2001 Mark Wedel & Crossfire Development Team |
29 | * Copyright (C) 2001 Mark Wedel & Crossfire Development Team |
6 | * Copyright (C) 1992 Frank Tore Johansen |
30 | * Copyright (C) 1992 Frank Tore Johansen |
… | |
… | |
19 | * along with this program; if not, write to the Free Software |
43 | * along with this program; if not, write to the Free Software |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
44 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 | * |
45 | * |
22 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
46 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
23 | */ |
47 | */ |
|
|
48 | >>>>>>> 1.18 |
24 | |
49 | |
25 | /* placing treasure in maps, where appropriate. */ |
50 | /* placing treasure in maps, where appropriate. */ |
26 | |
|
|
27 | |
|
|
28 | |
51 | |
29 | #include <global.h> |
52 | #include <global.h> |
30 | #include <random_map.h> |
53 | #include <random_map.h> |
31 | #include <rproto.h> |
54 | #include <rproto.h> |
32 | |
55 | |
… | |
… | |
48 | |
71 | |
49 | /* returns true if square x,y has P_NO_PASS set, which is true for walls |
72 | /* returns true if square x,y has P_NO_PASS set, which is true for walls |
50 | * and doors but not monsters. |
73 | * and doors but not monsters. |
51 | * This function is not map tile aware. |
74 | * This function is not map tile aware. |
52 | */ |
75 | */ |
53 | |
|
|
54 | int |
76 | int |
55 | wall_blocked (maptile *m, int x, int y) |
77 | wall_blocked (maptile *m, int x, int y) |
56 | { |
78 | { |
57 | int r; |
|
|
58 | |
|
|
59 | if (OUT_OF_REAL_MAP (m, x, y)) |
79 | if (OUT_OF_REAL_MAP (m, x, y)) |
60 | return 1; |
80 | return 1; |
|
|
81 | |
61 | r = GET_MAP_MOVE_BLOCK (m, x, y) & ~MOVE_BLOCK_DEFAULT; |
82 | int r = GET_MAP_MOVE_BLOCK (m, x, y) & ~MOVE_BLOCK_DEFAULT; |
62 | return r; |
83 | return r; |
63 | } |
84 | } |
64 | |
85 | |
65 | /* place treasures in the map, given the |
86 | /* place treasures in the map, given the |
66 | map, (required) |
87 | map, (required) |
67 | layout, (required) |
88 | layout, (required) |
68 | treasure style (may be empty or NULL, or "none" to cause no treasure.) |
89 | treasure style (may be empty or NULL, or "none" to cause no treasure.) |
69 | treasureoptions (may be 0 for random choices or positive) |
90 | treasureoptions (may be 0 for random choices or positive) |
70 | */ |
91 | */ |
71 | |
|
|
72 | void |
92 | void |
73 | place_treasure (maptile *map, char **layout, char *treasure_style, int treasureoptions, random_map_params *RP) |
93 | place_treasure (maptile *map, char **layout, char *treasure_style, int treasureoptions, random_map_params *RP) |
74 | { |
94 | { |
75 | char styledirname[1024]; |
95 | char styledirname[1024]; |
76 | char stylefilepath[1024]; |
96 | char stylefilepath[1024]; |
… | |
… | |
332 | it will place 2-4 keys regardless of what nkeys is provided nkeys > 1. |
352 | it will place 2-4 keys regardless of what nkeys is provided nkeys > 1. |
333 | |
353 | |
334 | The idea is that you call keyplace on x,y where a door is, and it'll make |
354 | The idea is that you call keyplace on x,y where a door is, and it'll make |
335 | sure a key is placed on both sides of the door. |
355 | sure a key is placed on both sides of the door. |
336 | */ |
356 | */ |
337 | |
|
|
338 | int |
357 | int |
339 | keyplace (maptile *map, int x, int y, char *keycode, int door_flag, int n_keys, random_map_params *RP) |
358 | keyplace (maptile *map, int x, int y, char *keycode, int door_flag, int n_keys, random_map_params *RP) |
340 | { |
359 | { |
341 | int i, j; |
360 | int i, j; |
342 | int kx, ky; |
361 | int kx = 0, ky = 0; |
343 | object *the_keymaster; /* the monster that gets the key. */ |
362 | object *the_keymaster; /* the monster that gets the key. */ |
344 | object *the_key; |
363 | object *the_key; |
345 | |
364 | |
346 | /* get a key and set its keycode */ |
365 | /* get a key and set its keycode */ |
347 | the_key = get_archetype ("key2"); |
366 | the_key = get_archetype ("key2"); |
… | |
… | |
349 | |
368 | |
350 | if (door_flag == PASS_DOORS) |
369 | if (door_flag == PASS_DOORS) |
351 | { |
370 | { |
352 | int tries = 0; |
371 | int tries = 0; |
353 | |
372 | |
354 | the_keymaster = NULL; |
373 | the_keymaster = 0; |
355 | while (tries < 15 && the_keymaster == NULL) |
374 | while (tries < 15 && !the_keymaster) |
356 | { |
375 | { |
357 | i = (RANDOM () % (RP->Xsize - 2)) + 1; |
376 | i = (RANDOM () % (RP->Xsize - 2)) + 1; |
358 | j = (RANDOM () % (RP->Ysize - 2)) + 1; |
377 | j = (RANDOM () % (RP->Ysize - 2)) + 1; |
359 | tries++; |
378 | tries++; |
360 | the_keymaster = find_closest_monster (map, i, j, RP); |
379 | the_keymaster = find_closest_monster (map, i, j, RP); |
361 | } |
380 | } |
|
|
381 | |
362 | /* if we don't find a good keymaster, drop the key on the ground. */ |
382 | /* if we don't find a good keymaster, drop the key on the ground. */ |
363 | if (the_keymaster == NULL) |
383 | if (!the_keymaster) |
364 | { |
384 | { |
365 | int freeindex; |
385 | int freeindex; |
366 | |
386 | |
367 | freeindex = -1; |
387 | freeindex = -1; |
368 | for (tries = 0; tries < 15 && freeindex == -1; tries++) |
388 | for (tries = 0; tries < 15 && freeindex == -1; tries++) |
… | |
… | |
370 | kx = (RANDOM () % (RP->Xsize - 2)) + 1; |
390 | kx = (RANDOM () % (RP->Xsize - 2)) + 1; |
371 | ky = (RANDOM () % (RP->Ysize - 2)) + 1; |
391 | ky = (RANDOM () % (RP->Ysize - 2)) + 1; |
372 | freeindex = find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1); |
392 | freeindex = find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1); |
373 | } |
393 | } |
374 | |
394 | |
|
|
395 | // can freeindex ever be < 0? |
375 | if (freeindex != -1) |
396 | if (freeindex >= 0) |
376 | { |
397 | { |
377 | kx += freearr_x[freeindex]; |
398 | kx += freearr_x [freeindex]; |
378 | ky += freearr_y[freeindex]; |
399 | ky += freearr_y [freeindex]; |
379 | } |
400 | } |
380 | } |
401 | } |
381 | } |
402 | } |
382 | else |
403 | else |
383 | { /* NO_PASS_DOORS --we have to work harder. */ |
404 | { /* NO_PASS_DOORS --we have to work harder. */ |
… | |
… | |
387 | { |
408 | { |
388 | if (wall_blocked (map, x, y)) |
409 | if (wall_blocked (map, x, y)) |
389 | return 0; |
410 | return 0; |
390 | |
411 | |
391 | the_keymaster = find_monster_in_room (map, x, y, RP); |
412 | the_keymaster = find_monster_in_room (map, x, y, RP); |
392 | if (the_keymaster == NULL) /* if fail, find a spot to drop the key. */ |
413 | if (!the_keymaster) /* if fail, find a spot to drop the key. */ |
393 | find_spot_in_room (map, x, y, &kx, &ky, RP); |
414 | find_spot_in_room (map, x, y, &kx, &ky, RP); |
394 | } |
415 | } |
395 | else |
416 | else |
396 | { |
417 | { |
397 | int sum = 0; /* count how many keys we actually place */ |
418 | int sum = 0; /* count how many keys we actually place */ |
… | |
… | |
399 | /* I'm lazy, so just try to place in all 4 directions. */ |
420 | /* 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); |
421 | 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); |
422 | 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); |
423 | 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); |
424 | sum += keyplace (map, x, y - 1, keycode, NO_PASS_DOORS, 1, RP); |
|
|
425 | |
404 | if (sum < 2) /* we might have made a disconnected map-place more keys. */ |
426 | if (sum < 2) /* we might have made a disconnected map-place more keys. */ |
405 | { /* diagnoally this time. */ |
427 | { /* diagonally this time. */ |
406 | keyplace (map, x + 1, y + 1, keycode, NO_PASS_DOORS, 1, RP); |
428 | 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); |
429 | 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); |
430 | 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); |
431 | keyplace (map, x - 1, y - 1, keycode, NO_PASS_DOORS, 1, RP); |
410 | } |
432 | } |
|
|
433 | |
411 | return 1; |
434 | return 1; |
412 | } |
435 | } |
413 | } |
436 | } |
414 | |
437 | |
415 | if (the_keymaster == NULL) |
438 | if (!the_keymaster) |
416 | { |
439 | { |
417 | the_key->x = kx; |
440 | the_key->x = kx; |
418 | the_key->y = ky; |
441 | the_key->y = ky; |
419 | insert_ob_in_map (the_key, map, NULL, 0); |
442 | insert_ob_in_map (the_key, map, NULL, 0); |
420 | return 1; |
443 | return 1; |
… | |
… | |
508 | free (layout2); |
531 | free (layout2); |
509 | |
532 | |
510 | return theMonsterToFind; |
533 | return theMonsterToFind; |
511 | } |
534 | } |
512 | |
535 | |
513 | |
|
|
514 | |
|
|
515 | |
|
|
516 | /* a datastructure needed by find_spot_in_room and find_spot_in_room_recursive */ |
536 | /* a datastructure needed by find_spot_in_room and find_spot_in_room_recursive */ |
517 | int *room_free_spots_x; |
537 | int *room_free_spots_x; |
518 | int *room_free_spots_y; |
538 | int *room_free_spots_y; |
519 | int number_of_free_spots_in_room; |
539 | int number_of_free_spots_in_room; |
520 | |
540 | |
521 | /* the workhorse routine, which finds the free spots in a room: |
541 | /* 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 |
542 | a datastructure of free points is set up, and a position chosen from |
523 | that datastructure. */ |
543 | that datastructure. */ |
524 | |
|
|
525 | void |
544 | void |
526 | find_spot_in_room_recursive (char **layout, int x, int y, random_map_params *RP) |
545 | find_spot_in_room_recursive (char **layout, int x, int y, random_map_params *RP) |
527 | { |
546 | { |
528 | int i, j; |
547 | int i, j; |
529 | |
548 | |
… | |
… | |
540 | /* check off this point */ |
559 | /* check off this point */ |
541 | layout[x][y] = 1; |
560 | layout[x][y] = 1; |
542 | room_free_spots_x[number_of_free_spots_in_room] = x; |
561 | room_free_spots_x[number_of_free_spots_in_room] = x; |
543 | room_free_spots_y[number_of_free_spots_in_room] = y; |
562 | room_free_spots_y[number_of_free_spots_in_room] = y; |
544 | number_of_free_spots_in_room++; |
563 | number_of_free_spots_in_room++; |
|
|
564 | |
545 | /* now search all the 8 squares around recursively for free spots,in random order */ |
565 | /* 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++) |
566 | for (i = RANDOM () % 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); |
567 | find_spot_in_room_recursive (layout, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); |
549 | } |
|
|
550 | |
568 | |
551 | } |
569 | } |
552 | |
570 | |
553 | /* find a random non-blocked spot in this room to drop a key. */ |
571 | /* find a random non-blocked spot in this room to drop a key. */ |
554 | void |
572 | void |
… | |
… | |
565 | /* allocate and copy the layout, converting C to 0. */ |
583 | /* allocate and copy the layout, converting C to 0. */ |
566 | for (i = 0; i < RP->Xsize; i++) |
584 | for (i = 0; i < RP->Xsize; i++) |
567 | { |
585 | { |
568 | layout2[i] = (char *) calloc (sizeof (char), RP->Ysize); |
586 | layout2[i] = (char *) calloc (sizeof (char), RP->Ysize); |
569 | for (j = 0; j < RP->Ysize; j++) |
587 | for (j = 0; j < RP->Ysize; j++) |
570 | { |
|
|
571 | if (wall_blocked (map, i, j)) |
588 | if (wall_blocked (map, i, j)) |
572 | layout2[i][j] = '#'; |
589 | layout2[i][j] = '#'; |
573 | } |
|
|
574 | } |
590 | } |
575 | |
591 | |
576 | /* setup num_free_spots and room_free_spots */ |
592 | /* setup num_free_spots and room_free_spots */ |
577 | find_spot_in_room_recursive (layout2, x, y, RP); |
593 | find_spot_in_room_recursive (layout2, x, y, RP); |
578 | |
594 | |
… | |
… | |
583 | *ky = room_free_spots_y[i]; |
599 | *ky = room_free_spots_y[i]; |
584 | } |
600 | } |
585 | |
601 | |
586 | /* deallocate the temp. layout */ |
602 | /* deallocate the temp. layout */ |
587 | for (i = 0; i < RP->Xsize; i++) |
603 | for (i = 0; i < RP->Xsize; i++) |
588 | { |
|
|
589 | free (layout2[i]); |
604 | free (layout2[i]); |
590 | } |
605 | |
591 | free (layout2); |
606 | free (layout2); |
592 | free (room_free_spots_x); |
607 | free (room_free_spots_x); |
593 | free (room_free_spots_y); |
608 | free (room_free_spots_y); |
594 | } |
609 | } |
595 | |
610 | |
596 | |
611 | |
597 | /* searches the map for a spot with walls around it. The more |
612 | /* 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 |
613 | 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.*/ |
614 | it'll return 0 if no FREE spots are found.*/ |
600 | |
|
|
601 | void |
615 | void |
602 | find_enclosed_spot (maptile *map, int *cx, int *cy, random_map_params *RP) |
616 | find_enclosed_spot (maptile *map, int *cx, int *cy, random_map_params *RP) |
603 | { |
617 | { |
604 | int x, y; |
618 | int x, y; |
605 | int i; |
619 | int i; |