3 /* Purpose: Wilderness generation */
6 * Copyright (c) 1989, 1999 James E. Wilson, Robert A. Koeneke,
9 * This software may be copied and distributed for educational, research, and
10 * not for profit purposes provided that this copyright and statement are
11 * included in all such copies.
18 * Fill the arrays of floors and walls in the good proportions
20 void set_floor_and_wall(byte type)
22 static byte cur_type = 255;
26 if (cur_type == type) return;
30 for (i = 0; i < 100; i++)
34 lim1 = d_info[type].floor_percent1;
35 lim2 = lim1 + d_info[type].floor_percent2;
36 lim3 = lim2 + d_info[type].floor_percent3;
39 floor_type[i] = d_info[type].floor1;
41 floor_type[i] = d_info[type].floor2;
43 floor_type[i] = d_info[type].floor3;
45 lim1 = d_info[type].fill_percent1;
46 lim2 = lim1 + d_info[type].fill_percent2;
47 lim3 = lim2 + d_info[type].fill_percent3;
49 fill_type[i] = d_info[type].fill_type1;
51 fill_type[i] = d_info[type].fill_type2;
53 fill_type[i] = d_info[type].fill_type3;
59 * Helper for plasma generation.
61 static void perturb_point_mid(int x1, int x2, int x3, int x4,
62 int xmid, int ymid, int rough, int depth_max)
65 * Average the four corners & perturb it a bit.
66 * tmp is a random int +/- rough
68 int tmp2 = rough*2 + 1;
69 int tmp = randint1(tmp2) - (rough + 1);
71 int avg = ((x1 + x2 + x3 + x4) / 4) + tmp;
73 /* Division always rounds down, so we round up again */
74 if (((x1 + x2 + x3 + x4) % 4) > 1)
79 if (avg > depth_max) avg = depth_max;
81 /* Set the new value. */
82 cave[ymid][xmid].feat = avg;
86 static void perturb_point_end(int x1, int x2, int x3,
87 int xmid, int ymid, int rough, int depth_max)
90 * Average the three corners & perturb it a bit.
91 * tmp is a random int +/- rough
93 int tmp2 = rough * 2 + 1;
94 int tmp = randint0(tmp2) - rough;
96 int avg = ((x1 + x2 + x3) / 3) + tmp;
98 /* Division always rounds down, so we round up again */
99 if ((x1 + x2 + x3) % 3) avg++;
102 if (avg < 0) avg = 0;
103 if (avg > depth_max) avg = depth_max;
105 /* Set the new value. */
106 cave[ymid][xmid].feat = avg;
111 * A generic function to generate the plasma fractal.
112 * Note that it uses ``cave_feat'' as temporary storage.
113 * The values in ``cave_feat'' after this function
114 * are NOT actual features; They are raw heights which
115 * need to be converted to features.
117 static void plasma_recursive(int x1, int y1, int x2, int y2,
118 int depth_max, int rough)
121 int xmid = (x2 - x1) / 2 + x1;
122 int ymid = (y2 - y1) / 2 + y1;
125 if (x1 + 1 == x2) return;
127 perturb_point_mid(cave[y1][x1].feat, cave[y2][x1].feat, cave[y1][x2].feat,
128 cave[y2][x2].feat, xmid, ymid, rough, depth_max);
130 perturb_point_end(cave[y1][x1].feat, cave[y1][x2].feat, cave[ymid][xmid].feat,
131 xmid, y1, rough, depth_max);
133 perturb_point_end(cave[y1][x2].feat, cave[y2][x2].feat, cave[ymid][xmid].feat,
134 x2, ymid, rough, depth_max);
136 perturb_point_end(cave[y2][x2].feat, cave[y2][x1].feat, cave[ymid][xmid].feat,
137 xmid, y2, rough, depth_max);
139 perturb_point_end(cave[y2][x1].feat, cave[y1][x1].feat, cave[ymid][xmid].feat,
140 x1, ymid, rough, depth_max);
143 /* Recurse the four quadrants */
144 plasma_recursive(x1, y1, xmid, ymid, depth_max, rough);
145 plasma_recursive(xmid, y1, x2, ymid, depth_max, rough);
146 plasma_recursive(x1, ymid, xmid, y2, depth_max, rough);
147 plasma_recursive(xmid, ymid, x2, y2, depth_max, rough);
152 * The default table in terrain level generation.
154 static int terrain_table[MAX_WILDERNESS][18] =
208 /* TERRAIN_DEEP_WATER */
234 /* TERRAIN_SHALLOW_WATER */
390 /* TERRAIN_SHALLOW_LAVA */
416 /* TERRAIN_DEEP_LAVA */
442 /* TERRAIN_MOUNTAIN */
472 static void generate_wilderness_area(int terrain, u32b seed, bool border, bool corner)
475 int table_size = sizeof(terrain_table[0]) / sizeof(int);
476 int roughness = 1; /* The roughness of the level. */
478 /* The outer wall is easy */
479 if (terrain == TERRAIN_EDGE)
481 /* Create level background */
482 for (y1 = 0; y1 < MAX_HGT; y1++)
484 for (x1 = 0; x1 < MAX_WID; x1++)
486 cave[y1][x1].feat = FEAT_PERM_SOLID;
490 /* We are done already */
495 /* Hack -- Use the "simple" RNG */
498 /* Hack -- Induce consistant town layout */
503 /* Create level background */
504 for (y1 = 0; y1 < MAX_HGT; y1++)
506 for (x1 = 0; x1 < MAX_WID; x1++)
508 cave[y1][x1].feat = table_size / 2;
514 * Initialize the four corners
515 * ToDo: calculate the medium height of the adjacent
516 * terrains for every corner.
518 cave[1][1].feat = (byte)randint0(table_size);
519 cave[MAX_HGT-2][1].feat = (byte)randint0(table_size);
520 cave[1][MAX_WID-2].feat = (byte)randint0(table_size);
521 cave[MAX_HGT-2][MAX_WID-2].feat = (byte)randint0(table_size);
525 /* x1, y1, x2, y2, num_depths, roughness */
526 plasma_recursive(1, 1, MAX_WID-2, MAX_HGT-2, table_size-1, roughness);
529 /* Use the complex RNG */
532 for (y1 = 1; y1 < MAX_HGT-1; y1++)
534 for (x1 = 1; x1 < MAX_WID-1; x1++)
536 cave[y1][x1].feat = terrain_table[terrain][cave[y1][x1].feat];
544 * Load a town or generate a terrain level using "plasma" fractals.
546 * x and y are the coordinates of the area in the wilderness.
547 * Border and corner are optimization flags to speed up the
548 * generation of the fractal terrain.
549 * If border is set then only the border of the terrain should
550 * be generated (for initializing the border structure).
551 * If corner is set then only the corners of the area are needed.
553 static void generate_area(int y, int x, bool border, bool corner)
557 /* Number of the town (if any) */
558 p_ptr->town_num = wilderness[y][x].town;
560 /* Set the base level */
561 base_level = wilderness[y][x].level;
563 /* Set the dungeon level */
566 /* Set the monster generation level */
567 monster_level = base_level;
569 /* Set the object generation level */
570 object_level = base_level;
573 /* Create the town */
576 /* Reset the buildings */
579 /* Initialize the town */
581 init_flags = INIT_CREATE_DUNGEON | INIT_ONLY_FEATURES;
583 init_flags = INIT_CREATE_DUNGEON;
585 process_dungeon_file("t_info.txt", 0, 0, MAX_HGT, MAX_WID);
587 if (!corner && !border) p_ptr->visit |= (1L << (p_ptr->town_num - 1));
591 int terrain = wilderness[y][x].terrain;
592 u32b seed = wilderness[y][x].seed;
594 generate_wilderness_area(terrain, seed, border, corner);
597 if (!corner && !wilderness[y][x].town)
600 * Place roads in the wilderness
601 * ToDo: make the road a bit more interresting
603 if (wilderness[y][x].road)
605 cave[MAX_HGT/2][MAX_WID/2].feat = FEAT_FLOOR;
607 if (wilderness[y-1][x].road)
610 for (y1 = 1; y1 < MAX_HGT/2; y1++)
613 cave[y1][x1].feat = FEAT_FLOOR;
617 if (wilderness[y+1][x].road)
620 for (y1 = MAX_HGT/2; y1 < MAX_HGT - 1; y1++)
623 cave[y1][x1].feat = FEAT_FLOOR;
627 if (wilderness[y][x+1].road)
630 for (x1 = MAX_WID/2; x1 < MAX_WID - 1; x1++)
633 cave[y1][x1].feat = FEAT_FLOOR;
637 if (wilderness[y][x-1].road)
640 for (x1 = 1; x1 < MAX_WID/2; x1++)
643 cave[y1][x1].feat = FEAT_FLOOR;
649 if (wilderness[y][x].entrance && !wilderness[y][x].town && (p_ptr->total_winner || !(d_info[wilderness[y][x].entrance].flags1 & DF1_WINNER)))
653 /* Hack -- Use the "simple" RNG */
656 /* Hack -- Induce consistant town layout */
657 Rand_value = wilderness[y][x].seed;
659 dy = rand_range(6, cur_hgt - 6);
660 dx = rand_range(6, cur_wid - 6);
662 cave[dy][dx].feat = FEAT_ENTRANCE;
663 cave[dy][dx].special = (byte)wilderness[y][x].entrance;
665 /* Use the complex RNG */
672 * Border of the wilderness area
674 static border_type border;
678 * Build the wilderness area outside of the town.
680 void wilderness_gen(void)
689 /* Assume illegal panel */
690 panel_row_min = cur_hgt;
691 panel_col_min = cur_wid;
693 /* Init the wilderness */
695 process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
697 x = p_ptr->wilderness_x;
698 y = p_ptr->wilderness_y;
700 /* Prepare allocation table */
701 get_mon_num_prep(get_monster_hook(), NULL);
704 generate_area(y - 1, x, TRUE, FALSE);
706 for (i = 1; i < MAX_WID - 1; i++)
708 border.north[i] = cave[MAX_HGT - 2][i].feat;
712 generate_area(y + 1, x, TRUE, FALSE);
714 for (i = 1; i < MAX_WID - 1; i++)
716 border.south[i] = cave[1][i].feat;
720 generate_area(y, x - 1, TRUE, FALSE);
722 for (i = 1; i < MAX_HGT - 1; i++)
724 border.west[i] = cave[i][MAX_WID - 2].feat;
728 generate_area(y, x + 1, TRUE, FALSE);
730 for (i = 1; i < MAX_HGT - 1; i++)
732 border.east[i] = cave[i][1].feat;
735 /* North west corner */
736 generate_area(y - 1, x - 1, FALSE, TRUE);
737 border.north_west = cave[MAX_HGT - 2][MAX_WID - 2].feat;
739 /* North east corner */
740 generate_area(y - 1, x + 1, FALSE, TRUE);
741 border.north_east = cave[MAX_HGT - 2][1].feat;
743 /* South west corner */
744 generate_area(y + 1, x - 1, FALSE, TRUE);
745 border.south_west = cave[1][MAX_WID - 2].feat;
747 /* South east corner */
748 generate_area(y + 1, x + 1, FALSE, TRUE);
749 border.south_east = cave[1][1].feat;
752 /* Create terrain of the current area */
753 generate_area(y, x, FALSE, FALSE);
756 /* Special boundary walls -- North */
757 for (i = 0; i < MAX_WID; i++)
759 cave[0][i].feat = FEAT_PERM_SOLID;
760 cave[0][i].mimic = border.north[i];
763 /* Special boundary walls -- South */
764 for (i = 0; i < MAX_WID; i++)
766 cave[MAX_HGT - 1][i].feat = FEAT_PERM_SOLID;
767 cave[MAX_HGT - 1][i].mimic = border.south[i];
770 /* Special boundary walls -- West */
771 for (i = 0; i < MAX_HGT; i++)
773 cave[i][0].feat = FEAT_PERM_SOLID;
774 cave[i][0].mimic = border.west[i];
777 /* Special boundary walls -- East */
778 for (i = 0; i < MAX_HGT; i++)
780 cave[i][MAX_WID - 1].feat = FEAT_PERM_SOLID;
781 cave[i][MAX_WID - 1].mimic = border.east[i];
784 /* North west corner */
785 cave[0][0].mimic = border.north_west;
787 /* North east corner */
788 cave[0][MAX_WID - 1].mimic = border.north_east;
790 /* South west corner */
791 cave[MAX_HGT - 1][0].mimic = border.south_west;
793 /* South east corner */
794 cave[MAX_HGT - 1][MAX_WID - 1].mimic = border.south_east;
796 /* Light up or darken the area */
797 for (y = 0; y < cur_hgt; y++)
799 for (x = 0; x < cur_wid; x++)
801 /* Get the cave grid */
807 c_ptr->info |= (CAVE_GLOW);
809 /* Hack -- Memorize lit grids if allowed */
810 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
814 /* Darken "boring" features */
815 if ((c_ptr->feat <= FEAT_INVIS) ||
816 ((c_ptr->feat >= FEAT_DEEP_WATER) &&
817 (c_ptr->feat <= FEAT_MOUNTAIN) &&
818 (c_ptr->feat != FEAT_MUSEUM)) ||
819 (x == 0) || (x == cur_wid-1) ||
820 (y == 0) || (y == cur_hgt-1))
822 /* Forget the grid */
823 c_ptr->info &= ~(CAVE_GLOW | CAVE_MARK);
825 else if (c_ptr->feat == FEAT_ENTRANCE)
828 c_ptr->info |= (CAVE_GLOW);
830 /* Hack -- Memorize lit grids if allowed */
831 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
837 if (p_ptr->teleport_town)
839 for (y = 0; y < cur_hgt; y++)
841 for (x = 0; x < cur_wid; x++)
843 if (((cave[y][x].feat - FEAT_BLDG_HEAD) == 4) || ((p_ptr->town_num == 1) && ((cave[y][x].feat - FEAT_BLDG_HEAD) == 0)))
845 if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
851 p_ptr->teleport_town = FALSE;
854 else if (p_ptr->leaving_dungeon)
856 for (y = 0; y < cur_hgt; y++)
858 for (x = 0; x < cur_wid; x++)
860 if (cave[y][x].feat == FEAT_ENTRANCE)
862 if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
868 p_ptr->teleport_town = FALSE;
871 player_place(p_ptr->oldpy, p_ptr->oldpx);
872 p_ptr->leftbldg = FALSE;
873 /* p_ptr->leaving_dungeon = FALSE;*/
875 lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
877 /* Make some residents */
878 for (i = 0; i < lim; i++)
882 if (!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))))
883 mode |= PM_ALLOW_SLEEP;
885 /* Make a resident */
886 (void)alloc_monster(generate_encounter ? 0 : 3, mode);
889 if(generate_encounter) ambush_flag = TRUE;
890 generate_encounter = FALSE;
892 /* Fill the arrays of floors and walls in the good proportions */
893 set_floor_and_wall(0);
895 /* Set rewarded quests to finished */
896 for (i = 0; i < max_quests; i++)
898 if (quest[i].status == QUEST_STATUS_REWARDED)
899 quest[i].status = QUEST_STATUS_FINISHED;
905 * Build the wilderness area.
908 void wilderness_gen_small()
912 /* To prevent stupid things */
913 for (i = 0; i < MAX_WID; i++)
914 for (j = 0; j < MAX_HGT; j++)
916 cave[j][i].feat = FEAT_PERM_SOLID;
919 /* Init the wilderness */
920 process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
923 for (i = 0; i < max_wild_x; i++)
924 for (j = 0; j < max_wild_y; j++)
926 if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
928 cave[j][i].feat = FEAT_TOWN;
929 cave[j][i].special = wilderness[j][i].town;
931 else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
932 else if (wilderness[j][i].entrance && (p_ptr->total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
934 cave[j][i].feat = FEAT_ENTRANCE;
935 cave[j][i].special = (byte)wilderness[j][i].entrance;
937 else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
939 cave[j][i].info |= (CAVE_GLOW | CAVE_MARK);
942 cur_hgt = (s16b) max_wild_y;
943 cur_wid = (s16b) max_wild_x;
945 if (cur_hgt > MAX_HGT) cur_hgt = MAX_HGT;
946 if (cur_wid > MAX_WID) cur_wid = MAX_WID;
948 /* Assume illegal panel */
949 panel_row_min = cur_hgt;
950 panel_col_min = cur_wid;
952 /* Place the player */
953 px = p_ptr->wilderness_x;
954 py = p_ptr->wilderness_y;
960 typedef struct wilderness_grid wilderness_grid;
962 struct wilderness_grid
964 int terrain; /* Terrain type */
965 int town; /* Town number */
966 s16b level; /* Level of the wilderness */
967 byte road; /* Road */
968 char name[32]; /* Name of the town/wilderness */
972 static wilderness_grid w_letter[255];
976 * Parse a sub-file of the "extra info"
978 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
985 if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
989 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
1002 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
1004 int index = zz[0][0];
1007 w_letter[index].terrain = atoi(zz[1]);
1009 w_letter[index].terrain = 0;
1012 w_letter[index].level = atoi(zz[2]);
1014 w_letter[index].level = 0;
1017 w_letter[index].town = atoi(zz[3]);
1019 w_letter[index].town = 0;
1022 w_letter[index].road = atoi(zz[4]);
1024 w_letter[index].road = 0;
1027 strcpy(w_letter[index].name, zz[5]);
1029 w_letter[index].name[0] = 0;
1034 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1040 /* Process "W:D:<layout> */
1041 /* Layout of the wilderness */
1044 /* Acquire the text */
1047 /* Length of the text */
1048 int len = strlen(s);
1050 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
1054 wilderness[*y][*x].terrain = w_letter[idx].terrain;
1056 wilderness[*y][*x].level = w_letter[idx].level;
1058 wilderness[*y][*x].town = w_letter[idx].town;
1060 wilderness[*y][*x].road = w_letter[idx].road;
1062 strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
1070 /* Process "W:P:<x>:<y> - starting position in the wilderness */
1073 if ((p_ptr->wilderness_x == 0) &&
1074 (p_ptr->wilderness_y == 0))
1076 if (tokenize(buf+4, 2, zz, 0) == 2)
1078 p_ptr->wilderness_y = atoi(zz[0]);
1079 p_ptr->wilderness_x = atoi(zz[1]);
1081 if ((p_ptr->wilderness_x < 1) ||
1082 (p_ptr->wilderness_x > max_wild_x) ||
1083 (p_ptr->wilderness_y < 1) ||
1084 (p_ptr->wilderness_y > max_wild_y))
1086 return (PARSE_ERROR_OUT_OF_BOUNDS);
1091 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1100 return (PARSE_ERROR_UNDEFINED_DIRECTIVE);
1103 for (i = 1; i < max_d_idx; i++)
1105 if (!d_info[i].maxdepth) continue;
1106 wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
1107 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
1108 wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
1117 * Generate the random seeds for the wilderness
1119 void seed_wilderness(void)
1123 /* Init wilderness seeds */
1124 for (x = 0; x < max_wild_x; x++)
1126 for (y = 0; y < max_wild_y; y++)
1128 wilderness[y][x].seed = randint0(0x10000000);
1129 wilderness[y][x].entrance = 0;
1136 * Pointer to wilderness_type
1138 typedef wilderness_type *wilderness_type_ptr;
1141 * Initialize wilderness array
1143 errr init_wilderness(void)
1147 /* Allocate the wilderness (two-dimension array) */
1148 C_MAKE(wilderness, max_wild_y, wilderness_type_ptr);
1149 C_MAKE(wilderness[0], max_wild_x * max_wild_y, wilderness_type);
1151 /* Init the other pointers */
1152 for (i = 1; i < max_wild_y; i++)
1153 wilderness[i] = wilderness[0] + i * max_wild_x;
1155 generate_encounter = FALSE;
1161 bool change_wild_mode(void)
1164 bool have_pet = FALSE;
1166 if (lite_town || vanilla_town)
1169 msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
1171 msg_print("No global mal");
1175 if (!p_ptr->wild_mode)
1177 for (i = 1; i < m_max; i++)
1179 monster_type *m_ptr = &m_list[i];
1181 if (!m_ptr->r_idx) continue;
1182 if (is_pet(m_ptr) && i != p_ptr->riding) have_pet = TRUE;
1183 if (m_ptr->csleep) continue;
1184 if (m_ptr->cdis > MAX_SIGHT) continue;
1185 if (!is_hostile(m_ptr)) continue;
1187 msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤¤Ï¹°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
1189 msg_print("You cannot enter global map, since there is some monsters nearby!");
1198 if(!get_check_strict("¥Ú¥Ã¥È¤òÃÖ¤¤¤Æ¹°è¥Þ¥Ã¥×¤ËÆþ¤ê¤Þ¤¹¤«¡©", CHECK_OKAY_CANCEL))
1200 if(!get_check_strict("Do you leave your pets behind? ", CHECK_OKAY_CANCEL))
1211 set_action(ACTION_NONE);
1213 p_ptr->wild_mode = !p_ptr->wild_mode;
1216 p_ptr->leaving = TRUE;