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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines