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.14 by pippijn, Sat Jan 6 14:42:30 2007 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) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
6 Copyright (C) 2001 Mark Wedel & Crossfire Development Team 6 * Copyright (C) 2001 Mark Wedel & Crossfire Development Team
7 Copyright (C) 1992 Frank Tore Johansen 7 * Copyright (C) 1992 Frank Tore Johansen
8 8 *
9 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
10 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
11 the Free Software Foundation; either version 2 of the License, or 11 * the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version. 12 * (at your option) any later version.
13 13 *
14 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,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details. 17 * GNU General Public License for more details.
18 18 *
19 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
20 along with this program; if not, write to the Free Software 20 * along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 22 *
23 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>
24*/ 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
25 49
26/* placing treasure in maps, where appropriate. */ 50/* placing treasure in maps, where appropriate. */
27
28
29 51
30#include <global.h> 52#include <global.h>
31#include <random_map.h> 53#include <random_map.h>
32#include <rproto.h> 54#include <rproto.h>
33 55
49 71
50/* 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
51 * and doors but not monsters. 73 * and doors but not monsters.
52 * This function is not map tile aware. 74 * This function is not map tile aware.
53 */ 75 */
54
55int 76int
56wall_blocked (maptile *m, int x, int y) 77wall_blocked (maptile *m, int x, int y)
57{ 78{
58 int r;
59
60 if (OUT_OF_REAL_MAP (m, x, y)) 79 if (OUT_OF_REAL_MAP (m, x, y))
61 return 1; 80 return 1;
81
62 r = GET_MAP_MOVE_BLOCK (m, x, y) & ~MOVE_BLOCK_DEFAULT; 82 int r = GET_MAP_MOVE_BLOCK (m, x, y) & ~MOVE_BLOCK_DEFAULT;
63 return r; 83 return r;
64} 84}
65 85
66/* place treasures in the map, given the 86/* place treasures in the map, given the
67map, (required) 87map, (required)
68layout, (required) 88layout, (required)
69treasure 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.)
70treasureoptions (may be 0 for random choices or positive) 90treasureoptions (may be 0 for random choices or positive)
71*/ 91*/
72
73void 92void
74place_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)
75{ 94{
76 char styledirname[1024]; 95 char styledirname[1024];
77 char stylefilepath[1024]; 96 char stylefilepath[1024];
212 if (i == -1) 231 if (i == -1)
213 { 232 {
214 the_chest->destroy (); 233 the_chest->destroy ();
215 return NULL; 234 return NULL;
216 } 235 }
236
217 xl = x + freearr_x[i]; 237 xl = x + freearr_x[i];
218 yl = y + freearr_y[i]; 238 yl = y + freearr_y[i];
219 239
220 /* if the placement is blocked, return a fail. */ 240 /* if the placement is blocked, return a fail. */
221 if (wall_blocked (map, xl, yl)) 241 if (wall_blocked (map, xl, yl))
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
338int 357int
339keyplace (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)
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++)
369 { 389 {
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_first_free_spot (the_key, map, kx, ky); 392 freeindex = find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1);
373 } 393 }
394
395 // can freeindex ever be < 0?
374 if (freeindex != -1) 396 if (freeindex >= 0)
375 { 397 {
376 kx += freearr_x[freeindex]; 398 kx += freearr_x [freeindex];
377 ky += freearr_y[freeindex]; 399 ky += freearr_y [freeindex];
378 } 400 }
379 } 401 }
380 } 402 }
381 else 403 else
382 { /* NO_PASS_DOORS --we have to work harder. */ 404 { /* NO_PASS_DOORS --we have to work harder. */
384 NO_PASS_DOORS is set. */ 406 NO_PASS_DOORS is set. */
385 if (n_keys == 1) 407 if (n_keys == 1)
386 { 408 {
387 if (wall_blocked (map, x, y)) 409 if (wall_blocked (map, x, y))
388 return 0; 410 return 0;
411
389 the_keymaster = find_monster_in_room (map, x, y, RP); 412 the_keymaster = find_monster_in_room (map, x, y, RP);
390 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. */
391 find_spot_in_room (map, x, y, &kx, &ky, RP); 414 find_spot_in_room (map, x, y, &kx, &ky, RP);
392 } 415 }
393 else 416 else
394 { 417 {
395 int sum = 0; /* count how many keys we actually place */ 418 int sum = 0; /* count how many keys we actually place */
397 /* 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. */
398 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);
399 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);
400 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);
401 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
402 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. */
403 { /* diagnoally this time. */ 427 { /* diagonally this time. */
404 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);
405 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);
406 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);
407 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);
408 } 432 }
433
409 return 1; 434 return 1;
410 } 435 }
411 } 436 }
412 437
413 if (the_keymaster == NULL) 438 if (!the_keymaster)
414 { 439 {
415 the_key->x = kx; 440 the_key->x = kx;
416 the_key->y = ky; 441 the_key->y = ky;
417 insert_ob_in_map (the_key, map, NULL, 0); 442 insert_ob_in_map (the_key, map, NULL, 0);
418 return 1; 443 return 1;
506 free (layout2); 531 free (layout2);
507 532
508 return theMonsterToFind; 533 return theMonsterToFind;
509} 534}
510 535
511
512
513
514/* 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 */
515int *room_free_spots_x; 537int *room_free_spots_x;
516int *room_free_spots_y; 538int *room_free_spots_y;
517int number_of_free_spots_in_room; 539int number_of_free_spots_in_room;
518 540
519/* the workhorse routine, which finds the free spots in a room: 541/* the workhorse routine, which finds the free spots in a room:
520a 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
521that datastructure. */ 543that datastructure. */
522
523void 544void
524find_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)
525{ 546{
526 int i, j; 547 int i, j;
527 548
538 /* check off this point */ 559 /* check off this point */
539 layout[x][y] = 1; 560 layout[x][y] = 1;
540 room_free_spots_x[number_of_free_spots_in_room] = x; 561 room_free_spots_x[number_of_free_spots_in_room] = x;
541 room_free_spots_y[number_of_free_spots_in_room] = y; 562 room_free_spots_y[number_of_free_spots_in_room] = y;
542 number_of_free_spots_in_room++; 563 number_of_free_spots_in_room++;
564
543 /* 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 */
544 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++)
545 {
546 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);
547 }
548 568
549} 569}
550 570
551/* 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. */
552void 572void
563 /* allocate and copy the layout, converting C to 0. */ 583 /* allocate and copy the layout, converting C to 0. */
564 for (i = 0; i < RP->Xsize; i++) 584 for (i = 0; i < RP->Xsize; i++)
565 { 585 {
566 layout2[i] = (char *) calloc (sizeof (char), RP->Ysize); 586 layout2[i] = (char *) calloc (sizeof (char), RP->Ysize);
567 for (j = 0; j < RP->Ysize; j++) 587 for (j = 0; j < RP->Ysize; j++)
568 {
569 if (wall_blocked (map, i, j)) 588 if (wall_blocked (map, i, j))
570 layout2[i][j] = '#'; 589 layout2[i][j] = '#';
571 }
572 } 590 }
573 591
574 /* setup num_free_spots and room_free_spots */ 592 /* setup num_free_spots and room_free_spots */
575 find_spot_in_room_recursive (layout2, x, y, RP); 593 find_spot_in_room_recursive (layout2, x, y, RP);
576 594
581 *ky = room_free_spots_y[i]; 599 *ky = room_free_spots_y[i];
582 } 600 }
583 601
584 /* deallocate the temp. layout */ 602 /* deallocate the temp. layout */
585 for (i = 0; i < RP->Xsize; i++) 603 for (i = 0; i < RP->Xsize; i++)
586 {
587 free (layout2[i]); 604 free (layout2[i]);
588 } 605
589 free (layout2); 606 free (layout2);
590 free (room_free_spots_x); 607 free (room_free_spots_x);
591 free (room_free_spots_y); 608 free (room_free_spots_y);
592} 609}
593 610
594 611
595/* 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
596 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
597 it'll return 0 if no FREE spots are found.*/ 614 it'll return 0 if no FREE spots are found.*/
598
599void 615void
600find_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)
601{ 617{
602 int x, y; 618 int x, y;
603 int i; 619 int i;
654 *cy = ly; 670 *cy = ly;
655 return; 671 return;
656 } 672 }
657 } 673 }
658 /* give up and return the closest free spot. */ 674 /* give up and return the closest free spot. */
659 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);
660 if (i != -1 && i <= SIZEOFFREE1) 676
677 if (i != -1)
661 { 678 {
662 *cx = x + freearr_x[i]; 679 *cx = x + freearr_x[i];
663 *cy = y + freearr_y[i]; 680 *cy = y + freearr_y[i];
664 return; 681 }
682 else
665 } 683 {
666 /* indicate failure */ 684 /* indicate failure */
685 *cx = -1;
667 *cx = *cy = -1; 686 *cy = -1;
687 }
668} 688}
669 689
670 690
671void 691void
672remove_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