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.17 by root, Mon Jan 15 15:54:19 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];
333 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.
334 353
335 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
336 sure a key is placed on both sides of the door. 355 sure a key is placed on both sides of the door.
337*/ 356*/
338
339int 357int
340keyplace (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)
341{ 359{
342 int i, j; 360 int i, j;
343 int kx, ky; 361 int kx = 0, ky = 0;
344 object *the_keymaster; /* the monster that gets the key. */ 362 object *the_keymaster; /* the monster that gets the key. */
345 object *the_key; 363 object *the_key;
346 364
347 /* get a key and set its keycode */ 365 /* get a key and set its keycode */
348 the_key = get_archetype ("key2"); 366 the_key = get_archetype ("key2");
350 368
351 if (door_flag == PASS_DOORS) 369 if (door_flag == PASS_DOORS)
352 { 370 {
353 int tries = 0; 371 int tries = 0;
354 372
355 the_keymaster = NULL; 373 the_keymaster = 0;
356 while (tries < 15 && the_keymaster == NULL) 374 while (tries < 15 && !the_keymaster)
357 { 375 {
358 i = (RANDOM () % (RP->Xsize - 2)) + 1; 376 i = (RANDOM () % (RP->Xsize - 2)) + 1;
359 j = (RANDOM () % (RP->Ysize - 2)) + 1; 377 j = (RANDOM () % (RP->Ysize - 2)) + 1;
360 tries++; 378 tries++;
361 the_keymaster = find_closest_monster (map, i, j, RP); 379 the_keymaster = find_closest_monster (map, i, j, RP);
362 } 380 }
381
363 /* 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. */
364 if (the_keymaster == NULL) 383 if (!the_keymaster)
365 { 384 {
366 int freeindex; 385 int freeindex;
367 386
368 freeindex = -1; 387 freeindex = -1;
369 for (tries = 0; tries < 15 && freeindex == -1; tries++) 388 for (tries = 0; tries < 15 && freeindex == -1; tries++)
371 kx = (RANDOM () % (RP->Xsize - 2)) + 1; 390 kx = (RANDOM () % (RP->Xsize - 2)) + 1;
372 ky = (RANDOM () % (RP->Ysize - 2)) + 1; 391 ky = (RANDOM () % (RP->Ysize - 2)) + 1;
373 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);
374 } 393 }
375 394
395 // can freeindex ever be < 0?
376 if (freeindex != -1) 396 if (freeindex >= 0)
377 { 397 {
378 kx += freearr_x[freeindex]; 398 kx += freearr_x [freeindex];
379 ky += freearr_y[freeindex]; 399 ky += freearr_y [freeindex];
380 } 400 }
381 } 401 }
382 } 402 }
383 else 403 else
384 { /* NO_PASS_DOORS --we have to work harder. */ 404 { /* NO_PASS_DOORS --we have to work harder. */
388 { 408 {
389 if (wall_blocked (map, x, y)) 409 if (wall_blocked (map, x, y))
390 return 0; 410 return 0;
391 411
392 the_keymaster = find_monster_in_room (map, x, y, RP); 412 the_keymaster = find_monster_in_room (map, x, y, RP);
393 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. */
394 find_spot_in_room (map, x, y, &kx, &ky, RP); 414 find_spot_in_room (map, x, y, &kx, &ky, RP);
395 } 415 }
396 else 416 else
397 { 417 {
398 int sum = 0; /* count how many keys we actually place */ 418 int sum = 0; /* count how many keys we actually place */
400 /* 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. */
401 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);
402 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);
403 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);
404 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
405 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. */
406 { /* diagnoally this time. */ 427 { /* diagonally this time. */
407 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);
408 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);
409 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);
410 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);
411 } 432 }
433
412 return 1; 434 return 1;
413 } 435 }
414 } 436 }
415 437
416 if (the_keymaster == NULL) 438 if (!the_keymaster)
417 { 439 {
418 the_key->x = kx; 440 the_key->x = kx;
419 the_key->y = ky; 441 the_key->y = ky;
420 insert_ob_in_map (the_key, map, NULL, 0); 442 insert_ob_in_map (the_key, map, NULL, 0);
421 return 1; 443 return 1;
509 free (layout2); 531 free (layout2);
510 532
511 return theMonsterToFind; 533 return theMonsterToFind;
512} 534}
513 535
514
515
516
517/* 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 */
518int *room_free_spots_x; 537int *room_free_spots_x;
519int *room_free_spots_y; 538int *room_free_spots_y;
520int number_of_free_spots_in_room; 539int number_of_free_spots_in_room;
521 540
522/* the workhorse routine, which finds the free spots in a room: 541/* the workhorse routine, which finds the free spots in a room:
523a 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
524that datastructure. */ 543that datastructure. */
525
526void 544void
527find_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)
528{ 546{
529 int i, j; 547 int i, j;
530 548
541 /* check off this point */ 559 /* check off this point */
542 layout[x][y] = 1; 560 layout[x][y] = 1;
543 room_free_spots_x[number_of_free_spots_in_room] = x; 561 room_free_spots_x[number_of_free_spots_in_room] = x;
544 room_free_spots_y[number_of_free_spots_in_room] = y; 562 room_free_spots_y[number_of_free_spots_in_room] = y;
545 number_of_free_spots_in_room++; 563 number_of_free_spots_in_room++;
564
546 /* 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 */
547 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++)
548 {
549 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);
550 }
551 568
552} 569}
553 570
554/* 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. */
555void 572void
566 /* allocate and copy the layout, converting C to 0. */ 583 /* allocate and copy the layout, converting C to 0. */
567 for (i = 0; i < RP->Xsize; i++) 584 for (i = 0; i < RP->Xsize; i++)
568 { 585 {
569 layout2[i] = (char *) calloc (sizeof (char), RP->Ysize); 586 layout2[i] = (char *) calloc (sizeof (char), RP->Ysize);
570 for (j = 0; j < RP->Ysize; j++) 587 for (j = 0; j < RP->Ysize; j++)
571 {
572 if (wall_blocked (map, i, j)) 588 if (wall_blocked (map, i, j))
573 layout2[i][j] = '#'; 589 layout2[i][j] = '#';
574 }
575 } 590 }
576 591
577 /* setup num_free_spots and room_free_spots */ 592 /* setup num_free_spots and room_free_spots */
578 find_spot_in_room_recursive (layout2, x, y, RP); 593 find_spot_in_room_recursive (layout2, x, y, RP);
579 594
584 *ky = room_free_spots_y[i]; 599 *ky = room_free_spots_y[i];
585 } 600 }
586 601
587 /* deallocate the temp. layout */ 602 /* deallocate the temp. layout */
588 for (i = 0; i < RP->Xsize; i++) 603 for (i = 0; i < RP->Xsize; i++)
589 {
590 free (layout2[i]); 604 free (layout2[i]);
591 } 605
592 free (layout2); 606 free (layout2);
593 free (room_free_spots_x); 607 free (room_free_spots_x);
594 free (room_free_spots_y); 608 free (room_free_spots_y);
595} 609}
596 610
597 611
598/* 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
599 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
600 it'll return 0 if no FREE spots are found.*/ 614 it'll return 0 if no FREE spots are found.*/
601
602void 615void
603find_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)
604{ 617{
605 int x, y; 618 int x, y;
606 int i; 619 int i;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines