ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/treasure.C
(Generate patch)

Comparing deliantra/server/random_maps/treasure.C (file contents):
Revision 1.13 by root, Sun Dec 31 20:46:17 2006 UTC vs.
Revision 1.19 by root, Wed Jan 17 12:36:31 2007 UTC

1
2/* 1/*
2<<<<<<< treasure.C
3 CrossFire, A Multiplayer game for X-windows 3 * CrossFire, A Multiplayer game
4 4 *
5 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
5 Copyright (C) 2001 Mark Wedel & Crossfire Development Team 6 * Copyright (C) 2001 Mark Wedel & Crossfire Development Team
6 Copyright (C) 1992 Frank Tore Johansen 7 * Copyright (C) 1992 Frank Tore Johansen
7 8 *
8 This program is free software; you can redistribute it and/or modify 9 * This program 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 * it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or 11 * the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version. 12 * (at your option) any later version.
12 13 *
13 This program is distributed in the hope that it will be useful, 14 * This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details. 17 * GNU General Public License for more details.
17 18 *
18 You should have received a copy of the GNU General Public License 19 * You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software 20 * along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 22 *
22 The authors can be reached via e-mail at <crossfire@schmorp.de> 23 * The authors can be reached via e-mail at <crossfire@schmorp.de>
23*/ 24 */
25=======
26 * CrossFire, A Multiplayer game for X-windows
27 *
28 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
29 * Copyright (C) 2001 Mark Wedel & Crossfire Development Team
30 * Copyright (C) 1992 Frank Tore Johansen
31 *
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
36 *
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
41 *
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45 *
46 * The authors can be reached via e-mail at <crossfire@schmorp.de>
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
54int 76int
55wall_blocked (maptile *m, int x, int y) 77wall_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
66map, (required) 87map, (required)
67layout, (required) 88layout, (required)
68treasure style (may be empty or NULL, or "none" to cause no treasure.) 89treasure style (may be empty or NULL, or "none" to cause no treasure.)
69treasureoptions (may be 0 for random choices or positive) 90treasureoptions (may be 0 for random choices or positive)
70*/ 91*/
71
72void 92void
73place_treasure (maptile *map, char **layout, char *treasure_style, int treasureoptions, random_map_params *RP) 93place_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];
211 if (i == -1) 231 if (i == -1)
212 { 232 {
213 the_chest->destroy (); 233 the_chest->destroy ();
214 return NULL; 234 return NULL;
215 } 235 }
236
216 xl = x + freearr_x[i]; 237 xl = x + freearr_x[i];
217 yl = y + freearr_y[i]; 238 yl = y + freearr_y[i];
218 239
219 /* if the placement is blocked, return a fail. */ 240 /* if the placement is blocked, return a fail. */
220 if (wall_blocked (map, xl, yl)) 241 if (wall_blocked (map, xl, yl))
331 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.
332 353
333 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
334 sure a key is placed on both sides of the door. 355 sure a key is placed on both sides of the door.
335*/ 356*/
336
337int 357int
338keyplace (maptile *map, int x, int y, char *keycode, int door_flag, int n_keys, random_map_params *RP) 358keyplace (maptile *map, int x, int y, char *keycode, int door_flag, int n_keys, random_map_params *RP)
339{ 359{
340 int i, j; 360 int i, j;
341 int kx, ky; 361 int kx = 0, ky = 0;
342 object *the_keymaster; /* the monster that gets the key. */ 362 object *the_keymaster; /* the monster that gets the key. */
343 object *the_key; 363 object *the_key;
344 364
345 /* get a key and set its keycode */ 365 /* get a key and set its keycode */
346 the_key = get_archetype ("key2"); 366 the_key = get_archetype ("key2");
348 368
349 if (door_flag == PASS_DOORS) 369 if (door_flag == PASS_DOORS)
350 { 370 {
351 int tries = 0; 371 int tries = 0;
352 372
353 the_keymaster = NULL; 373 the_keymaster = 0;
354 while (tries < 15 && the_keymaster == NULL) 374 while (tries < 15 && !the_keymaster)
355 { 375 {
356 i = (RANDOM () % (RP->Xsize - 2)) + 1; 376 i = (RANDOM () % (RP->Xsize - 2)) + 1;
357 j = (RANDOM () % (RP->Ysize - 2)) + 1; 377 j = (RANDOM () % (RP->Ysize - 2)) + 1;
358 tries++; 378 tries++;
359 the_keymaster = find_closest_monster (map, i, j, RP); 379 the_keymaster = find_closest_monster (map, i, j, RP);
360 } 380 }
381
361 /* 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. */
362 if (the_keymaster == NULL) 383 if (!the_keymaster)
363 { 384 {
364 int freeindex; 385 int freeindex;
365 386
366 freeindex = -1; 387 freeindex = -1;
367 for (tries = 0; tries < 15 && freeindex == -1; tries++) 388 for (tries = 0; tries < 15 && freeindex == -1; tries++)
368 { 389 {
369 kx = (RANDOM () % (RP->Xsize - 2)) + 1; 390 kx = (RANDOM () % (RP->Xsize - 2)) + 1;
370 ky = (RANDOM () % (RP->Ysize - 2)) + 1; 391 ky = (RANDOM () % (RP->Ysize - 2)) + 1;
371 freeindex = find_first_free_spot (the_key, map, kx, ky); 392 freeindex = find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1);
372 } 393 }
394
395 // can freeindex ever be < 0?
373 if (freeindex != -1) 396 if (freeindex >= 0)
374 { 397 {
375 kx += freearr_x[freeindex]; 398 kx += freearr_x [freeindex];
376 ky += freearr_y[freeindex]; 399 ky += freearr_y [freeindex];
377 } 400 }
378 } 401 }
379 } 402 }
380 else 403 else
381 { /* NO_PASS_DOORS --we have to work harder. */ 404 { /* NO_PASS_DOORS --we have to work harder. */
383 NO_PASS_DOORS is set. */ 406 NO_PASS_DOORS is set. */
384 if (n_keys == 1) 407 if (n_keys == 1)
385 { 408 {
386 if (wall_blocked (map, x, y)) 409 if (wall_blocked (map, x, y))
387 return 0; 410 return 0;
411
388 the_keymaster = find_monster_in_room (map, x, y, RP); 412 the_keymaster = find_monster_in_room (map, x, y, RP);
389 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. */
390 find_spot_in_room (map, x, y, &kx, &ky, RP); 414 find_spot_in_room (map, x, y, &kx, &ky, RP);
391 } 415 }
392 else 416 else
393 { 417 {
394 int sum = 0; /* count how many keys we actually place */ 418 int sum = 0; /* count how many keys we actually place */
396 /* 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. */
397 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);
398 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);
399 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);
400 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
401 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. */
402 { /* diagnoally this time. */ 427 { /* diagonally this time. */
403 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);
404 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);
405 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);
406 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);
407 } 432 }
433
408 return 1; 434 return 1;
409 } 435 }
410 } 436 }
411 437
412 if (the_keymaster == NULL) 438 if (!the_keymaster)
413 { 439 {
414 the_key->x = kx; 440 the_key->x = kx;
415 the_key->y = ky; 441 the_key->y = ky;
416 insert_ob_in_map (the_key, map, NULL, 0); 442 insert_ob_in_map (the_key, map, NULL, 0);
417 return 1; 443 return 1;
505 free (layout2); 531 free (layout2);
506 532
507 return theMonsterToFind; 533 return theMonsterToFind;
508} 534}
509 535
510
511
512
513/* 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 */
514int *room_free_spots_x; 537int *room_free_spots_x;
515int *room_free_spots_y; 538int *room_free_spots_y;
516int number_of_free_spots_in_room; 539int number_of_free_spots_in_room;
517 540
518/* the workhorse routine, which finds the free spots in a room: 541/* the workhorse routine, which finds the free spots in a room:
519a datastructure of free points is set up, and a position chosen from 542a datastructure of free points is set up, and a position chosen from
520that datastructure. */ 543that datastructure. */
521
522void 544void
523find_spot_in_room_recursive (char **layout, int x, int y, random_map_params *RP) 545find_spot_in_room_recursive (char **layout, int x, int y, random_map_params *RP)
524{ 546{
525 int i, j; 547 int i, j;
526 548
537 /* check off this point */ 559 /* check off this point */
538 layout[x][y] = 1; 560 layout[x][y] = 1;
539 room_free_spots_x[number_of_free_spots_in_room] = x; 561 room_free_spots_x[number_of_free_spots_in_room] = x;
540 room_free_spots_y[number_of_free_spots_in_room] = y; 562 room_free_spots_y[number_of_free_spots_in_room] = y;
541 number_of_free_spots_in_room++; 563 number_of_free_spots_in_room++;
564
542 /* 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 */
543 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++)
544 {
545 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);
546 }
547 568
548} 569}
549 570
550/* 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. */
551void 572void
562 /* allocate and copy the layout, converting C to 0. */ 583 /* allocate and copy the layout, converting C to 0. */
563 for (i = 0; i < RP->Xsize; i++) 584 for (i = 0; i < RP->Xsize; i++)
564 { 585 {
565 layout2[i] = (char *) calloc (sizeof (char), RP->Ysize); 586 layout2[i] = (char *) calloc (sizeof (char), RP->Ysize);
566 for (j = 0; j < RP->Ysize; j++) 587 for (j = 0; j < RP->Ysize; j++)
567 {
568 if (wall_blocked (map, i, j)) 588 if (wall_blocked (map, i, j))
569 layout2[i][j] = '#'; 589 layout2[i][j] = '#';
570 }
571 } 590 }
572 591
573 /* setup num_free_spots and room_free_spots */ 592 /* setup num_free_spots and room_free_spots */
574 find_spot_in_room_recursive (layout2, x, y, RP); 593 find_spot_in_room_recursive (layout2, x, y, RP);
575 594
580 *ky = room_free_spots_y[i]; 599 *ky = room_free_spots_y[i];
581 } 600 }
582 601
583 /* deallocate the temp. layout */ 602 /* deallocate the temp. layout */
584 for (i = 0; i < RP->Xsize; i++) 603 for (i = 0; i < RP->Xsize; i++)
585 {
586 free (layout2[i]); 604 free (layout2[i]);
587 } 605
588 free (layout2); 606 free (layout2);
589 free (room_free_spots_x); 607 free (room_free_spots_x);
590 free (room_free_spots_y); 608 free (room_free_spots_y);
591} 609}
592 610
593 611
594/* 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
595 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
596 it'll return 0 if no FREE spots are found.*/ 614 it'll return 0 if no FREE spots are found.*/
597
598void 615void
599find_enclosed_spot (maptile *map, int *cx, int *cy, random_map_params *RP) 616find_enclosed_spot (maptile *map, int *cx, int *cy, random_map_params *RP)
600{ 617{
601 int x, y; 618 int x, y;
602 int i; 619 int i;
653 *cy = ly; 670 *cy = ly;
654 return; 671 return;
655 } 672 }
656 } 673 }
657 /* give up and return the closest free spot. */ 674 /* give up and return the closest free spot. */
658 i = find_first_free_spot (&archetype::find ("chest")->clone, map, x, y); 675 i = find_free_spot (&archetype::find ("chest")->clone, map, x, y, 1, SIZEOFFREE1 + 1);
659 if (i != -1 && i <= SIZEOFFREE1) 676
677 if (i != -1)
660 { 678 {
661 *cx = x + freearr_x[i]; 679 *cx = x + freearr_x[i];
662 *cy = y + freearr_y[i]; 680 *cy = y + freearr_y[i];
663 return; 681 }
682 else
664 } 683 {
665 /* indicate failure */ 684 /* indicate failure */
685 *cx = -1;
666 *cx = *cy = -1; 686 *cy = -1;
687 }
667} 688}
668 689
669 690
670void 691void
671remove_monsters (int x, int y, maptile *map) 692remove_monsters (int x, int y, maptile *map)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines