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

Comparing deliantra/server/random_maps/random_map.C (file contents):
Revision 1.11 by root, Sat Dec 30 18:45:28 2006 UTC vs.
Revision 1.19 by root, Sat Jan 13 23:06:13 2007 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 CrossFire, A Multiplayer game for X-windows
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
29#include <random_map.h> 30#include <random_map.h>
30#include <rproto.h> 31#include <rproto.h>
31#include <sproto.h> 32#include <sproto.h>
32 33
33void 34void
34dump_layout (char **layout, random_map_params * RP) 35dump_layout (char **layout, random_map_params *RP)
35{ 36{
36 { 37 {
37 int i, j; 38 int i, j;
38 39
39 for (i = 0; i < RP->Xsize; i++) 40 for (i = 0; i < RP->Xsize; i++)
50 } 51 }
51 } 52 }
52 printf ("\n"); 53 printf ("\n");
53} 54}
54 55
55extern FILE *logfile; 56bool
56 57maptile::generate_random_map (random_map_params *RP)
57maptile *
58generate_random_map (const char *OutFileName, random_map_params * RP)
59{ 58{
60 char **layout, buf[HUGE_BUF]; 59 char **layout, buf[16384];
61 maptile *theMap;
62 int i; 60 int i;
63 61
64 /* pick a random seed, or use the one from the input file */ 62 /* pick a random seed, or use the one from the input file */
65 if (RP->random_seed == 0) 63 SRANDOM (RP->random_seed ? RP->random_seed + RP->dungeon_level : time (0));
66 RP->random_seed = time (0);
67
68 SRANDOM (RP->random_seed);
69 64
70 write_map_parameters_to_string (buf, RP); 65 write_map_parameters_to_string (buf, RP);
71 66
72 if (RP->difficulty == 0) 67 if (RP->difficulty == 0)
73 { 68 {
110#ifdef RMAP_DEBUG 105#ifdef RMAP_DEBUG
111 dump_layout (layout, RP); 106 dump_layout (layout, RP);
112#endif 107#endif
113 108
114 /* allocate the map and set the floor */ 109 /* allocate the map and set the floor */
115 theMap = make_map_floor (layout, RP->floorstyle, RP); 110 make_map_floor (layout, RP->floorstyle, RP);
116
117 /* set the name of the map. */
118 theMap->path = OutFileName;
119 111
120 /* set region */ 112 /* set region */
121 theMap->region = RP->region; 113 region = RP->region;
114
115 coroapi::cede ();
122 116
123 /* create walls unless the wallstyle is "none" */ 117 /* create walls unless the wallstyle is "none" */
124 if (strcmp (RP->wallstyle, "none")) 118 if (strcmp (RP->wallstyle, "none"))
125 { 119 {
126 make_map_walls (theMap, layout, RP->wallstyle, RP); 120 make_map_walls (this, layout, RP->wallstyle, RP);
127 121
128 /* place doors unless doorstyle or wallstyle is "none" */ 122 /* place doors unless doorstyle or wallstyle is "none" */
129 if (strcmp (RP->doorstyle, "none")) 123 if (strcmp (RP->doorstyle, "none"))
130 put_doors (theMap, layout, RP->doorstyle, RP); 124 put_doors (this, layout, RP->doorstyle, RP);
131 125
132 } 126 }
127
128 coroapi::cede ();
133 129
134 /* create exits unless the exitstyle is "none" */ 130 /* create exits unless the exitstyle is "none" */
135 if (strcmp (RP->exitstyle, "none")) 131 if (strcmp (RP->exitstyle, "none"))
136 place_exits (theMap, layout, RP->exitstyle, RP->orientation, RP); 132 place_exits (this, layout, RP->exitstyle, RP->orientation, RP);
137 133
134 coroapi::cede ();
135
138 place_specials_in_map (theMap, layout, RP); 136 place_specials_in_map (this, layout, RP);
137
138 coroapi::cede ();
139 139
140 /* create monsters unless the monsterstyle is "none" */ 140 /* create monsters unless the monsterstyle is "none" */
141 if (strcmp (RP->monsterstyle, "none")) 141 if (strcmp (RP->monsterstyle, "none"))
142 place_monsters (theMap, RP->monsterstyle, RP->difficulty, RP); 142 place_monsters (this, RP->monsterstyle, RP->difficulty, RP);
143
144 coroapi::cede ();
143 145
144 /* treasures needs to have a proper difficulty set for the map. */ 146 /* treasures needs to have a proper difficulty set for the map. */
145 theMap->difficulty = theMap->estimate_difficulty (); 147 difficulty = estimate_difficulty ();
148
149 coroapi::cede ();
146 150
147 /* create treasure unless the treasurestyle is "none" */ 151 /* create treasure unless the treasurestyle is "none" */
148 if (strcmp (RP->treasurestyle, "none")) 152 if (strcmp (RP->treasurestyle, "none"))
149 place_treasure (theMap, layout, RP->treasurestyle, RP->treasureoptions, RP); 153 place_treasure (this, layout, RP->treasurestyle, RP->treasureoptions, RP);
154
155 coroapi::cede ();
150 156
151 /* create decor unless the decorstyle is "none" */ 157 /* create decor unless the decorstyle is "none" */
152 if (strcmp (RP->decorstyle, "none")) 158 if (strcmp (RP->decorstyle, "none"))
153 put_decor (theMap, layout, RP->decorstyle, RP->decoroptions, RP); 159 put_decor (this, layout, RP->decorstyle, RP->decoroptions, RP);
160
161 coroapi::cede ();
154 162
155 /* generate treasures, etc. */ 163 /* generate treasures, etc. */
156 theMap->fix_auto_apply (); 164 fix_auto_apply ();
157 165
166 coroapi::cede ();
158 unblock_exits (theMap, layout, RP); 167 unblock_exits (this, layout, RP);
159 168
160 /* free the layout */ 169 /* free the layout */
161 for (i = 0; i < RP->Xsize; i++) 170 for (i = 0; i < RP->Xsize; i++)
162 free (layout[i]); 171 free (layout[i]);
172
163 free (layout); 173 free (layout);
164 174
165 theMap->msg = strdup (buf); 175 msg = strdup (buf);
176 in_memory = MAP_IN_MEMORY;
166 177
167 return theMap; 178 return 1;
168} 179}
169 180
170/* function selects the layout function and gives it whatever 181/* function selects the layout function and gives it whatever
171 arguments it needs. */ 182 arguments it needs. */
172char ** 183char **
173layoutgen (random_map_params * RP) 184layoutgen (random_map_params *RP)
174{ 185{
175 char **maze = 0; 186 char **maze = 0;
176 int oxsize = RP->Xsize, oysize = RP->Ysize; 187 int oxsize = RP->Xsize, oysize = RP->Ysize;
177 188
178 if (RP->symmetry == SYMMETRY_RANDOM) 189 if (RP->symmetry == SYMMETRY_RANDOM)
283} 294}
284 295
285/* takes a map and makes it symmetric: adjusts Xsize and 296/* takes a map and makes it symmetric: adjusts Xsize and
286Ysize to produce a symmetric map. */ 297Ysize to produce a symmetric map. */
287char ** 298char **
288symmetrize_layout (char **maze, int sym, random_map_params * RP) 299symmetrize_layout (char **maze, int sym, random_map_params *RP)
289{ 300{
290 int i, j; 301 int i, j;
291 char **sym_maze; 302 char **sym_maze;
292 int Xsize_orig, Ysize_orig; 303 int Xsize_orig, Ysize_orig;
293 304
351 onion layouts, making them possibly centered on any wall. 362 onion layouts, making them possibly centered on any wall.
352 It'll modify Xsize and Ysize if they're swapped. 363 It'll modify Xsize and Ysize if they're swapped.
353*/ 364*/
354 365
355char ** 366char **
356rotate_layout (char **maze, int rotation, random_map_params * RP) 367rotate_layout (char **maze, int rotation, random_map_params *RP)
357{ 368{
358 char **new_maze; 369 char **new_maze;
359 int i, j; 370 int i, j;
360 371
361 switch (rotation) 372 switch (rotation)
362 { 373 {
363 case 0: 374 case 0:
364 return maze; 375 return maze;
365 break; 376 break;
366 case 2: /* a reflection */ 377 case 2: /* a reflection */
367 { 378 {
368 char *newmaze = (char *) malloc (sizeof (char) * RP->Xsize * RP->Ysize); 379 char *newmaze = (char *) malloc (sizeof (char) * RP->Xsize * RP->Ysize);
369 380
370 for (i = 0; i < RP->Xsize; i++) 381 for (i = 0; i < RP->Xsize; i++)
371 { /* make a copy */ 382 { /* make a copy */
372 for (j = 0; j < RP->Ysize; j++) 383 for (j = 0; j < RP->Ysize; j++)
373 { 384 {
374 newmaze[i * RP->Ysize + j] = maze[i][j]; 385 newmaze[i * RP->Ysize + j] = maze[i][j];
375 } 386 }
376 } 387 }
377 for (i = 0; i < RP->Xsize; i++) 388 for (i = 0; i < RP->Xsize; i++)
378 { /* copy a reflection back */ 389 { /* copy a reflection back */
379 for (j = 0; j < RP->Ysize; j++) 390 for (j = 0; j < RP->Ysize; j++)
380 { 391 {
381 maze[i][j] = newmaze[(RP->Xsize - i - 1) * RP->Ysize + RP->Ysize - j - 1]; 392 maze[i][j] = newmaze[(RP->Xsize - i - 1) * RP->Ysize + RP->Ysize - j - 1];
382 } 393 }
383 } 394 }
392 new_maze = (char **) calloc (sizeof (char *), RP->Ysize); 403 new_maze = (char **) calloc (sizeof (char *), RP->Ysize);
393 for (i = 0; i < RP->Ysize; i++) 404 for (i = 0; i < RP->Ysize; i++)
394 { 405 {
395 new_maze[i] = (char *) calloc (sizeof (char), RP->Xsize); 406 new_maze[i] = (char *) calloc (sizeof (char), RP->Xsize);
396 } 407 }
397 if (rotation == 1) /* swap x and y */ 408 if (rotation == 1) /* swap x and y */
398 for (i = 0; i < RP->Xsize; i++) 409 for (i = 0; i < RP->Xsize; i++)
399 for (j = 0; j < RP->Ysize; j++) 410 for (j = 0; j < RP->Ysize; j++)
400 new_maze[j][i] = maze[i][j]; 411 new_maze[j][i] = maze[i][j];
401 412
402 if (rotation == 3) 413 if (rotation == 3)
403 { /* swap x and y */ 414 { /* swap x and y */
404 for (i = 0; i < RP->Xsize; i++) 415 for (i = 0; i < RP->Xsize; i++)
405 for (j = 0; j < RP->Ysize; j++) 416 for (j = 0; j < RP->Ysize; j++)
406 new_maze[j][i] = maze[RP->Xsize - i - 1][RP->Ysize - j - 1]; 417 new_maze[j][i] = maze[RP->Xsize - i - 1][RP->Ysize - j - 1];
407 } 418 }
408 419
422} 433}
423 434
424/* take a layout and make some rooms in it. 435/* take a layout and make some rooms in it.
425 --works best on onions.*/ 436 --works best on onions.*/
426void 437void
427roomify_layout (char **maze, random_map_params * RP) 438roomify_layout (char **maze, random_map_params *RP)
428{ 439{
429 int tries = RP->Xsize * RP->Ysize / 30; 440 int tries = RP->Xsize * RP->Ysize / 30;
430 int ti; 441 int ti;
431 442
432 for (ti = 0; ti < tries; ti++) 443 for (ti = 0; ti < tries; ti++)
459/* checks the layout to see if I can stick a horizontal(dir = 0) wall 470/* checks the layout to see if I can stick a horizontal(dir = 0) wall
460 (or vertical, dir == 1) 471 (or vertical, dir == 1)
461 here which ends up on other walls sensibly. */ 472 here which ends up on other walls sensibly. */
462 473
463int 474int
464can_make_wall (char **maze, int dx, int dy, int dir, random_map_params * RP) 475can_make_wall (char **maze, int dx, int dy, int dir, random_map_params *RP)
465{ 476{
466 int i1; 477 int i1;
467 int length = 0; 478 int length = 0;
468 479
469 /* dont make walls if we're on the edge. */ 480 /* dont make walls if we're on the edge. */
544make_wall (char **maze, int x, int y, int dir) 555make_wall (char **maze, int x, int y, int dir)
545{ 556{
546 maze[x][y] = 'D'; /* mark a door */ 557 maze[x][y] = 'D'; /* mark a door */
547 switch (dir) 558 switch (dir)
548 { 559 {
549 case 0: /* horizontal */ 560 case 0: /* horizontal */
550 { 561 {
551 int i1; 562 int i1;
552 563
553 for (i1 = x - 1; maze[i1][y] == 0; i1--) 564 for (i1 = x - 1; maze[i1][y] == 0; i1--)
554 maze[i1][y] = '#'; 565 maze[i1][y] = '#';
555 for (i1 = x + 1; maze[i1][y] == 0; i1++) 566 for (i1 = x + 1; maze[i1][y] == 0; i1++)
556 maze[i1][y] = '#'; 567 maze[i1][y] = '#';
557 break; 568 break;
558 } 569 }
559 case 1: /* vertical */ 570 case 1: /* vertical */
560 { 571 {
561 int i1; 572 int i1;
562 573
563 for (i1 = y - 1; maze[x][i1] == 0; i1--) 574 for (i1 = y - 1; maze[x][i1] == 0; i1--)
564 maze[x][i1] = '#'; 575 maze[x][i1] = '#';
565 for (i1 = y + 1; maze[x][i1] == 0; i1++) 576 for (i1 = y + 1; maze[x][i1] == 0; i1++)
566 maze[x][i1] = '#'; 577 maze[x][i1] = '#';
567 break; 578 break;
568 } 579 }
569 } 580 }
570 581
571 return 0; 582 return 0;
572} 583}
573 584
574/* puts doors at appropriate locations in a layout. */ 585/* puts doors at appropriate locations in a layout. */
575
576void 586void
577doorify_layout (char **maze, random_map_params * RP) 587doorify_layout (char **maze, random_map_params *RP)
578{ 588{
579 int ndoors = RP->Xsize * RP->Ysize / 60; /* reasonable number of doors. */ 589 int ndoors = RP->Xsize * RP->Ysize / 60; /* reasonable number of doors. */
580 char *doorlist_x; 590 char *doorlist_x;
581 char *doorlist_y; 591 char *doorlist_y;
582 int doorlocs = 0; /* # of available doorlocations */ 592 int doorlocs = 0; /* # of available doorlocations */
597 doorlist_x[doorlocs] = i; 607 doorlist_x[doorlocs] = i;
598 doorlist_y[doorlocs] = j; 608 doorlist_y[doorlocs] = j;
599 doorlocs++; 609 doorlocs++;
600 } 610 }
601 } 611 }
612
602 while (ndoors > 0 && doorlocs > 0) 613 while (ndoors > 0 && doorlocs > 0)
603 { 614 {
604 int di; 615 int di;
605 int sindex; 616 int sindex;
606 617
616 /* reduce the size of the list */ 627 /* reduce the size of the list */
617 doorlocs--; 628 doorlocs--;
618 doorlist_x[di] = doorlist_x[doorlocs]; 629 doorlist_x[di] = doorlist_x[doorlocs];
619 doorlist_y[di] = doorlist_y[doorlocs]; 630 doorlist_y[di] = doorlist_y[doorlocs];
620 } 631 }
632
621 free (doorlist_x); 633 free (doorlist_x);
622 free (doorlist_y); 634 free (doorlist_y);
623} 635}
624 636
625
626void 637void
627write_map_parameters_to_string (char *buf, random_map_params * RP) 638write_map_parameters_to_string (char *buf, random_map_params *RP)
628{ 639{
629 char small_buf[256]; 640 char small_buf[16384];
630 641
631 sprintf (buf, "xsize %d\nysize %d\n", RP->Xsize, RP->Ysize); 642 sprintf (buf, "xsize %d\nysize %d\n", RP->Xsize, RP->Ysize);
632 643
633 if (RP->wallstyle[0]) 644 if (RP->wallstyle[0])
634 { 645 {
676 { 687 {
677 sprintf (small_buf, "exitstyle %s\n", RP->exitstyle); 688 sprintf (small_buf, "exitstyle %s\n", RP->exitstyle);
678 strcat (buf, small_buf); 689 strcat (buf, small_buf);
679 } 690 }
680 691
681 if (RP->final_map[0]) 692 if (RP->final_map.length ())
682 { 693 {
683 sprintf (small_buf, "final_map %s\n", RP->final_map); 694 sprintf (small_buf, "final_map %s\n", &RP->final_map);
684 strcat (buf, small_buf); 695 strcat (buf, small_buf);
685 } 696 }
686 697
687 if (RP->exit_on_final_map[0]) 698 if (RP->exit_on_final_map[0])
688 { 699 {
689 sprintf (small_buf, "exit_on_final_map %s\n", RP->exit_on_final_map); 700 sprintf (small_buf, "exit_on_final_map %s\n", RP->exit_on_final_map);
690 strcat (buf, small_buf); 701 strcat (buf, small_buf);
691 } 702 }
692 703
693 if (RP->this_map[0]) 704 if (RP->this_map.length ())
694 { 705 {
695 sprintf (small_buf, "origin_map %s\n", RP->this_map); 706 sprintf (small_buf, "origin_map %s\n", &RP->this_map);
696 strcat (buf, small_buf); 707 strcat (buf, small_buf);
697 } 708 }
698 709
699 if (RP->expand2x) 710 if (RP->expand2x)
700 { 711 {
706 { 717 {
707 sprintf (small_buf, "layoutoptions1 %d\n", RP->layoutoptions1); 718 sprintf (small_buf, "layoutoptions1 %d\n", RP->layoutoptions1);
708 strcat (buf, small_buf); 719 strcat (buf, small_buf);
709 } 720 }
710 721
711
712 if (RP->layoutoptions2) 722 if (RP->layoutoptions2)
713 { 723 {
714 sprintf (small_buf, "layoutoptions2 %d\n", RP->layoutoptions2); 724 sprintf (small_buf, "layoutoptions2 %d\n", RP->layoutoptions2);
715 strcat (buf, small_buf); 725 strcat (buf, small_buf);
716 } 726 }
717 727
718
719 if (RP->layoutoptions3) 728 if (RP->layoutoptions3)
720 { 729 {
721 sprintf (small_buf, "layoutoptions3 %d\n", RP->layoutoptions3); 730 sprintf (small_buf, "layoutoptions3 %d\n", RP->layoutoptions3);
722 strcat (buf, small_buf); 731 strcat (buf, small_buf);
723 } 732 }
725 if (RP->symmetry) 734 if (RP->symmetry)
726 { 735 {
727 sprintf (small_buf, "symmetry %d\n", RP->symmetry); 736 sprintf (small_buf, "symmetry %d\n", RP->symmetry);
728 strcat (buf, small_buf); 737 strcat (buf, small_buf);
729 } 738 }
730
731 739
732 if (RP->difficulty && RP->difficulty_given) 740 if (RP->difficulty && RP->difficulty_given)
733 { 741 {
734 sprintf (small_buf, "difficulty %d\n", RP->difficulty); 742 sprintf (small_buf, "difficulty %d\n", RP->difficulty);
735 strcat (buf, small_buf); 743 strcat (buf, small_buf);
772 { 780 {
773 sprintf (small_buf, "origin_y %d\n", RP->origin_y); 781 sprintf (small_buf, "origin_y %d\n", RP->origin_y);
774 strcat (buf, small_buf); 782 strcat (buf, small_buf);
775 } 783 }
776 784
785 if (RP->treasureoptions)
786 {
787 sprintf (small_buf, "treasureoptions %d\n", RP->treasureoptions);
788 strcat (buf, small_buf);
789 }
790
777 if (RP->random_seed) 791 if (RP->random_seed)
778 { 792 {
779 /* Add one so that the next map is a bit different */
780 sprintf (small_buf, "random_seed %d\n", RP->random_seed + 1); 793 sprintf (small_buf, "random_seed %d\n", RP->random_seed);
781 strcat (buf, small_buf); 794 strcat (buf, small_buf);
782 }
783
784 if (RP->treasureoptions)
785 { 795 }
786 sprintf (small_buf, "treasureoptions %d\n", RP->treasureoptions); 796
797 if (RP->custom)
798 {
799 sprintf (small_buf, "custom %s\n", RP->custom);
787 strcat (buf, small_buf); 800 strcat (buf, small_buf);
788 } 801 }
789} 802}
790 803
791void 804void
813 int difficulty_given_n, 826 int difficulty_given_n,
814 int decoroptions_n, 827 int decoroptions_n,
815 int orientation_n, 828 int orientation_n,
816 int origin_x_n, int origin_y_n, int random_seed_n, int treasureoptions_n, float difficulty_increase) 829 int origin_x_n, int origin_y_n, int random_seed_n, int treasureoptions_n, float difficulty_increase)
817{ 830{
818
819 char small_buf[256]; 831 char small_buf[16384];
820 832
821 sprintf (buf, "xsize %d\nysize %d\n", xsize_n, ysize_n); 833 sprintf (buf, "xsize %d\nysize %d\n", xsize_n, ysize_n);
822 834
823 if (wallstyle_n && wallstyle_n[0]) 835 if (wallstyle_n && wallstyle_n[0])
824 { 836 {
889 if (layoutoptions1_n) 901 if (layoutoptions1_n)
890 { 902 {
891 sprintf (small_buf, "layoutoptions1 %d\n", layoutoptions1_n); 903 sprintf (small_buf, "layoutoptions1 %d\n", layoutoptions1_n);
892 strcat (buf, small_buf); 904 strcat (buf, small_buf);
893 } 905 }
894
895 906
896 if (layoutoptions2_n) 907 if (layoutoptions2_n)
897 { 908 {
898 sprintf (small_buf, "layoutoptions2 %d\n", layoutoptions2_n); 909 sprintf (small_buf, "layoutoptions2 %d\n", layoutoptions2_n);
899 strcat (buf, small_buf); 910 strcat (buf, small_buf);
968 if (treasureoptions_n) 979 if (treasureoptions_n)
969 { 980 {
970 sprintf (small_buf, "treasureoptions %d\n", treasureoptions_n); 981 sprintf (small_buf, "treasureoptions %d\n", treasureoptions_n);
971 strcat (buf, small_buf); 982 strcat (buf, small_buf);
972 } 983 }
973
974
975} 984}
976 985
977/* copy an object with an inventory... i.e., duplicate the inv too. */ 986/* copy an object with an inventory... i.e., duplicate the inv too. */
978void 987void
979copy_object_with_inv (object *src_ob, object *dest_ob) 988copy_object_with_inv (object *src_ob, object *dest_ob)
980{ 989{
981 object *walk, *tmp; 990 object *walk, *tmp;
982 991
983 src_ob->copy_to (dest_ob); 992 src_ob->copy_to (dest_ob);
984 993
985 for (walk = src_ob->inv; walk != NULL; walk = walk->below) 994 for (walk = src_ob->inv; walk; walk = walk->below)
986 { 995 {
987 tmp = object::create (); 996 tmp = object::create ();
997
988 walk->copy_to (tmp); 998 walk->copy_to (tmp);
989 insert_ob_in_ob (tmp, dest_ob); 999 insert_ob_in_ob (tmp, dest_ob);
990 } 1000 }
991} 1001}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines