4 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke,
7 * This software may be copied and distributed for educational, research,
8 * and not for profit purposes provided that this copyright and statement
9 * are included in all such copies. Other copyrights may also apply.
12 /* Purpose: Wilderness generation */
17 static void set_floor_and_wall_aux(s16b feat_type[100], feat_prob prob[DUNGEON_FEAT_PROB_NUM])
19 int lim[DUNGEON_FEAT_PROB_NUM], cur = 0, i;
21 lim[0] = prob[0].percent;
22 for (i = 1; i < DUNGEON_FEAT_PROB_NUM; i++) lim[i] = lim[i - 1] + prob[i].percent;
25 if (lim[DUNGEON_FEAT_PROB_NUM - 1] < 100) lim[DUNGEON_FEAT_PROB_NUM - 1] = 100;
27 for (i = 0; i < 100; i++)
29 while (i == lim[cur]) cur++;
30 feat_type[i] = prob[cur].feat;
35 * Fill the arrays of floors and walls in the good proportions
37 void set_floor_and_wall(byte type)
39 static byte cur_type = 255;
40 dungeon_info_type *d_ptr;
43 if (cur_type == type) return;
46 d_ptr = &d_info[type];
48 set_floor_and_wall_aux(floor_type, d_ptr->floor);
49 set_floor_and_wall_aux(fill_type, d_ptr->fill);
51 feat_wall_outer = d_ptr->outer_wall;
52 feat_wall_inner = d_ptr->inner_wall;
53 feat_wall_solid = d_ptr->outer_wall;
58 * Helper for plasma generation.
60 static void perturb_point_mid(int x1, int x2, int x3, int x4,
61 int xmid, int ymid, int rough, int depth_max)
64 * Average the four corners & perturb it a bit.
65 * tmp is a random int +/- rough
67 int tmp2 = rough*2 + 1;
68 int tmp = randint1(tmp2) - (rough + 1);
70 int avg = ((x1 + x2 + x3 + x4) / 4) + tmp;
72 /* Division always rounds down, so we round up again */
73 if (((x1 + x2 + x3 + x4) % 4) > 1)
78 if (avg > depth_max) avg = depth_max;
80 /* Set the new value. */
81 cave[ymid][xmid].feat = avg;
85 static void perturb_point_end(int x1, int x2, int x3,
86 int xmid, int ymid, int rough, int depth_max)
89 * Average the three corners & perturb it a bit.
90 * tmp is a random int +/- rough
92 int tmp2 = rough * 2 + 1;
93 int tmp = randint0(tmp2) - rough;
95 int avg = ((x1 + x2 + x3) / 3) + tmp;
97 /* Division always rounds down, so we round up again */
98 if ((x1 + x2 + x3) % 3) avg++;
101 if (avg < 0) avg = 0;
102 if (avg > depth_max) avg = depth_max;
104 /* Set the new value. */
105 cave[ymid][xmid].feat = avg;
110 * A generic function to generate the plasma fractal.
111 * Note that it uses ``cave_feat'' as temporary storage.
112 * The values in ``cave_feat'' after this function
113 * are NOT actual features; They are raw heights which
114 * need to be converted to features.
116 static void plasma_recursive(int x1, int y1, int x2, int y2,
117 int depth_max, int rough)
120 int xmid = (x2 - x1) / 2 + x1;
121 int ymid = (y2 - y1) / 2 + y1;
124 if (x1 + 1 == x2) return;
126 perturb_point_mid(cave[y1][x1].feat, cave[y2][x1].feat, cave[y1][x2].feat,
127 cave[y2][x2].feat, xmid, ymid, rough, depth_max);
129 perturb_point_end(cave[y1][x1].feat, cave[y1][x2].feat, cave[ymid][xmid].feat,
130 xmid, y1, rough, depth_max);
132 perturb_point_end(cave[y1][x2].feat, cave[y2][x2].feat, cave[ymid][xmid].feat,
133 x2, ymid, rough, depth_max);
135 perturb_point_end(cave[y2][x2].feat, cave[y2][x1].feat, cave[ymid][xmid].feat,
136 xmid, y2, rough, depth_max);
138 perturb_point_end(cave[y2][x1].feat, cave[y1][x1].feat, cave[ymid][xmid].feat,
139 x1, ymid, rough, depth_max);
142 /* Recurse the four quadrants */
143 plasma_recursive(x1, y1, xmid, ymid, depth_max, rough);
144 plasma_recursive(xmid, y1, x2, ymid, depth_max, rough);
145 plasma_recursive(x1, ymid, xmid, y2, depth_max, rough);
146 plasma_recursive(xmid, ymid, x2, y2, depth_max, rough);
151 * The default table in terrain level generation.
153 static int terrain_table[MAX_WILDERNESS][18] =
207 /* TERRAIN_DEEP_WATER */
233 /* TERRAIN_SHALLOW_WATER */
389 /* TERRAIN_SHALLOW_LAVA */
415 /* TERRAIN_DEEP_LAVA */
441 /* TERRAIN_MOUNTAIN */
471 static void generate_wilderness_area(int terrain, u32b seed, bool border, bool corner)
474 int table_size = sizeof(terrain_table[0]) / sizeof(int);
475 int roughness = 1; /* The roughness of the level. */
480 /* The outer wall is easy */
481 if (terrain == TERRAIN_EDGE)
483 /* Create level background */
484 for (y1 = 0; y1 < MAX_HGT; y1++)
486 for (x1 = 0; x1 < MAX_WID; x1++)
488 cave[y1][x1].feat = FEAT_PERM;
492 /* We are done already */
497 /* Hack -- Use the "simple" RNG */
500 /* Hack -- Induce consistant town layout */
505 /* Create level background */
506 for (y1 = 0; y1 < MAX_HGT; y1++)
508 for (x1 = 0; x1 < MAX_WID; x1++)
510 cave[y1][x1].feat = table_size / 2;
516 * Initialize the four corners
517 * ToDo: calculate the medium height of the adjacent
518 * terrains for every corner.
520 cave[1][1].feat = randint0(table_size);
521 cave[MAX_HGT-2][1].feat = randint0(table_size);
522 cave[1][MAX_WID-2].feat = randint0(table_size);
523 cave[MAX_HGT-2][MAX_WID-2].feat = randint0(table_size);
527 /* x1, y1, x2, y2, num_depths, roughness */
528 plasma_recursive(1, 1, MAX_WID-2, MAX_HGT-2, table_size-1, roughness);
531 /* Use the complex RNG */
534 for (y1 = 1; y1 < MAX_HGT-1; y1++)
536 for (x1 = 1; x1 < MAX_WID-1; x1++)
538 cave[y1][x1].feat = terrain_table[terrain][cave[y1][x1].feat];
546 * Load a town or generate a terrain level using "plasma" fractals.
548 * x and y are the coordinates of the area in the wilderness.
549 * Border and corner are optimization flags to speed up the
550 * generation of the fractal terrain.
551 * If border is set then only the border of the terrain should
552 * be generated (for initializing the border structure).
553 * If corner is set then only the corners of the area are needed.
555 static void generate_area(int y, int x, bool border, bool corner)
559 /* Number of the town (if any) */
560 p_ptr->town_num = wilderness[y][x].town;
562 /* Set the base level */
563 base_level = wilderness[y][x].level;
565 /* Set the dungeon level */
568 /* Set the monster generation level */
569 monster_level = base_level;
571 /* Set the object generation level */
572 object_level = base_level;
575 /* Create the town */
578 /* Reset the buildings */
581 /* Initialize the town */
583 init_flags = INIT_CREATE_DUNGEON | INIT_ONLY_FEATURES;
585 init_flags = INIT_CREATE_DUNGEON;
587 process_dungeon_file("t_info.txt", 0, 0, MAX_HGT, MAX_WID);
589 if (!corner && !border) p_ptr->visit |= (1L << (p_ptr->town_num - 1));
593 int terrain = wilderness[y][x].terrain;
594 u32b seed = wilderness[y][x].seed;
596 generate_wilderness_area(terrain, seed, border, corner);
599 if (!corner && !wilderness[y][x].town)
602 * Place roads in the wilderness
603 * ToDo: make the road a bit more interresting
605 if (wilderness[y][x].road)
607 cave[MAX_HGT/2][MAX_WID/2].feat = FEAT_FLOOR;
609 if (wilderness[y-1][x].road)
612 for (y1 = 1; y1 < MAX_HGT/2; y1++)
615 cave[y1][x1].feat = FEAT_FLOOR;
619 if (wilderness[y+1][x].road)
622 for (y1 = MAX_HGT/2; y1 < MAX_HGT - 1; y1++)
625 cave[y1][x1].feat = FEAT_FLOOR;
629 if (wilderness[y][x+1].road)
632 for (x1 = MAX_WID/2; x1 < MAX_WID - 1; x1++)
635 cave[y1][x1].feat = FEAT_FLOOR;
639 if (wilderness[y][x-1].road)
642 for (x1 = 1; x1 < MAX_WID/2; x1++)
645 cave[y1][x1].feat = FEAT_FLOOR;
651 if (wilderness[y][x].entrance && !wilderness[y][x].town && (p_ptr->total_winner || !(d_info[wilderness[y][x].entrance].flags1 & DF1_WINNER)))
655 /* Hack -- Use the "simple" RNG */
658 /* Hack -- Induce consistant town layout */
659 Rand_value = wilderness[y][x].seed;
661 dy = rand_range(6, cur_hgt - 6);
662 dx = rand_range(6, cur_wid - 6);
664 cave[dy][dx].feat = FEAT_ENTRANCE;
665 cave[dy][dx].special = wilderness[y][x].entrance;
667 /* Use the complex RNG */
674 * Border of the wilderness area
676 static border_type border;
680 * Build the wilderness area outside of the town.
682 void wilderness_gen(void)
692 /* Assume illegal panel */
693 panel_row_min = cur_hgt;
694 panel_col_min = cur_wid;
696 /* Init the wilderness */
698 process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
700 x = p_ptr->wilderness_x;
701 y = p_ptr->wilderness_y;
703 /* Prepare allocation table */
704 get_mon_num_prep(get_monster_hook(), NULL);
707 generate_area(y - 1, x, TRUE, FALSE);
709 for (i = 1; i < MAX_WID - 1; i++)
711 border.north[i] = cave[MAX_HGT - 2][i].feat;
715 generate_area(y + 1, x, TRUE, FALSE);
717 for (i = 1; i < MAX_WID - 1; i++)
719 border.south[i] = cave[1][i].feat;
723 generate_area(y, x - 1, TRUE, FALSE);
725 for (i = 1; i < MAX_HGT - 1; i++)
727 border.west[i] = cave[i][MAX_WID - 2].feat;
731 generate_area(y, x + 1, TRUE, FALSE);
733 for (i = 1; i < MAX_HGT - 1; i++)
735 border.east[i] = cave[i][1].feat;
738 /* North west corner */
739 generate_area(y - 1, x - 1, FALSE, TRUE);
740 border.north_west = cave[MAX_HGT - 2][MAX_WID - 2].feat;
742 /* North east corner */
743 generate_area(y - 1, x + 1, FALSE, TRUE);
744 border.north_east = cave[MAX_HGT - 2][1].feat;
746 /* South west corner */
747 generate_area(y + 1, x - 1, FALSE, TRUE);
748 border.south_west = cave[1][MAX_WID - 2].feat;
750 /* South east corner */
751 generate_area(y + 1, x + 1, FALSE, TRUE);
752 border.south_east = cave[1][1].feat;
755 /* Create terrain of the current area */
756 generate_area(y, x, FALSE, FALSE);
759 /* Special boundary walls -- North */
760 for (i = 0; i < MAX_WID; i++)
762 cave[0][i].feat = FEAT_PERM;
763 cave[0][i].mimic = border.north[i];
766 /* Special boundary walls -- South */
767 for (i = 0; i < MAX_WID; i++)
769 cave[MAX_HGT - 1][i].feat = FEAT_PERM;
770 cave[MAX_HGT - 1][i].mimic = border.south[i];
773 /* Special boundary walls -- West */
774 for (i = 0; i < MAX_HGT; i++)
776 cave[i][0].feat = FEAT_PERM;
777 cave[i][0].mimic = border.west[i];
780 /* Special boundary walls -- East */
781 for (i = 0; i < MAX_HGT; i++)
783 cave[i][MAX_WID - 1].feat = FEAT_PERM;
784 cave[i][MAX_WID - 1].mimic = border.east[i];
787 /* North west corner */
788 cave[0][0].mimic = border.north_west;
790 /* North east corner */
791 cave[0][MAX_WID - 1].mimic = border.north_east;
793 /* South west corner */
794 cave[MAX_HGT - 1][0].mimic = border.south_west;
796 /* South east corner */
797 cave[MAX_HGT - 1][MAX_WID - 1].mimic = border.south_east;
799 /* Light up or darken the area */
800 for (y = 0; y < cur_hgt; y++)
802 for (x = 0; x < cur_wid; x++)
804 /* Get the cave grid */
810 c_ptr->info |= (CAVE_GLOW);
812 /* Hack -- Memorize lit grids if allowed */
813 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
817 /* Feature code (applying "mimic" field) */
818 f_ptr = &f_info[get_feat_mimic(c_ptr)];
820 if (!is_mirror_grid(c_ptr) && !have_flag(f_ptr->flags, FF_QUEST_ENTER) &&
821 !have_flag(f_ptr->flags, FF_ENTRANCE))
824 c_ptr->info &= ~(CAVE_GLOW);
826 /* Darken "boring" features */
827 if (!have_flag(f_ptr->flags, FF_REMEMBER))
829 /* Forget the grid */
830 c_ptr->info &= ~(CAVE_MARK);
833 else if (have_flag(f_ptr->flags, FF_ENTRANCE))
836 c_ptr->info |= (CAVE_GLOW);
838 /* Hack -- Memorize lit grids if allowed */
839 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
845 if (p_ptr->teleport_town)
847 for (y = 0; y < cur_hgt; y++)
849 for (x = 0; x < cur_wid; x++)
851 /* Get the cave grid */
854 /* Seeing true feature code (ignore mimic) */
855 f_ptr = &f_info[c_ptr->feat];
857 if (have_flag(f_ptr->flags, FF_BLDG))
859 if ((f_ptr->power == 4) || ((p_ptr->town_num == 1) && (f_ptr->power == 0)))
861 if (c_ptr->m_idx) delete_monster_idx(c_ptr->m_idx);
868 p_ptr->teleport_town = FALSE;
871 else if (p_ptr->leaving_dungeon)
873 for (y = 0; y < cur_hgt; y++)
875 for (x = 0; x < cur_wid; x++)
877 /* Get the cave grid */
880 if (have_flag(f_flags_grid(c_ptr), FF_ENTRANCE))
882 if (c_ptr->m_idx) delete_monster_idx(c_ptr->m_idx);
888 p_ptr->teleport_town = FALSE;
891 player_place(p_ptr->oldpy, p_ptr->oldpx);
892 /* p_ptr->leaving_dungeon = FALSE;*/
894 lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
896 /* Make some residents */
897 for (i = 0; i < lim; i++)
901 if (!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))))
902 mode |= PM_ALLOW_SLEEP;
904 /* Make a resident */
905 (void)alloc_monster(generate_encounter ? 0 : 3, mode);
908 if(generate_encounter) ambush_flag = TRUE;
909 generate_encounter = FALSE;
911 /* Fill the arrays of floors and walls in the good proportions */
912 set_floor_and_wall(0);
914 /* Set rewarded quests to finished */
915 for (i = 0; i < max_quests; i++)
917 if (quest[i].status == QUEST_STATUS_REWARDED)
918 quest[i].status = QUEST_STATUS_FINISHED;
924 * Build the wilderness area.
927 void wilderness_gen_small()
931 /* To prevent stupid things */
932 for (i = 0; i < MAX_WID; i++)
933 for (j = 0; j < MAX_HGT; j++)
935 cave[j][i].feat = FEAT_PERM;
938 /* Init the wilderness */
939 process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
942 for (i = 0; i < max_wild_x; i++)
943 for (j = 0; j < max_wild_y; j++)
945 if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
947 cave[j][i].feat = FEAT_TOWN;
948 cave[j][i].special = wilderness[j][i].town;
950 else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
951 else if (wilderness[j][i].entrance && (p_ptr->total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
953 cave[j][i].feat = FEAT_ENTRANCE;
954 cave[j][i].special = (byte)wilderness[j][i].entrance;
956 else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
958 cave[j][i].info |= (CAVE_GLOW | CAVE_MARK);
961 cur_hgt = (s16b) max_wild_y;
962 cur_wid = (s16b) max_wild_x;
964 if (cur_hgt > MAX_HGT) cur_hgt = MAX_HGT;
965 if (cur_wid > MAX_WID) cur_wid = MAX_WID;
967 /* Assume illegal panel */
968 panel_row_min = cur_hgt;
969 panel_col_min = cur_wid;
971 /* Place the player */
972 px = p_ptr->wilderness_x;
973 py = p_ptr->wilderness_y;
979 typedef struct wilderness_grid wilderness_grid;
981 struct wilderness_grid
983 int terrain; /* Terrain type */
984 int town; /* Town number */
985 s16b level; /* Level of the wilderness */
986 byte road; /* Road */
987 char name[32]; /* Name of the town/wilderness */
991 static wilderness_grid w_letter[255];
995 * Parse a sub-file of the "extra info"
997 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
1007 if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
1011 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
1024 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
1026 int index = zz[0][0];
1029 w_letter[index].terrain = atoi(zz[1]);
1031 w_letter[index].terrain = 0;
1034 w_letter[index].level = atoi(zz[2]);
1036 w_letter[index].level = 0;
1039 w_letter[index].town = atoi(zz[3]);
1041 w_letter[index].town = 0;
1044 w_letter[index].road = atoi(zz[4]);
1046 w_letter[index].road = 0;
1049 strcpy(w_letter[index].name, zz[5]);
1051 w_letter[index].name[0] = 0;
1056 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1062 /* Process "W:D:<layout> */
1063 /* Layout of the wilderness */
1066 /* Acquire the text */
1069 /* Length of the text */
1070 int len = strlen(s);
1072 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
1076 wilderness[*y][*x].terrain = w_letter[idx].terrain;
1078 wilderness[*y][*x].level = w_letter[idx].level;
1080 wilderness[*y][*x].town = w_letter[idx].town;
1082 wilderness[*y][*x].road = w_letter[idx].road;
1084 strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
1092 /* Process "W:P:<x>:<y> - starting position in the wilderness */
1095 if ((p_ptr->wilderness_x == 0) &&
1096 (p_ptr->wilderness_y == 0))
1098 if (tokenize(buf+4, 2, zz, 0) == 2)
1100 p_ptr->wilderness_y = atoi(zz[0]);
1101 p_ptr->wilderness_x = atoi(zz[1]);
1103 if ((p_ptr->wilderness_x < 1) ||
1104 (p_ptr->wilderness_x > max_wild_x) ||
1105 (p_ptr->wilderness_y < 1) ||
1106 (p_ptr->wilderness_y > max_wild_y))
1108 return (PARSE_ERROR_OUT_OF_BOUNDS);
1113 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1122 return (PARSE_ERROR_UNDEFINED_DIRECTIVE);
1125 for (i = 1; i < max_d_idx; i++)
1127 if (!d_info[i].maxdepth) continue;
1128 wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
1129 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
1130 wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
1139 * Generate the random seeds for the wilderness
1141 void seed_wilderness(void)
1145 /* Init wilderness seeds */
1146 for (x = 0; x < max_wild_x; x++)
1148 for (y = 0; y < max_wild_y; y++)
1150 wilderness[y][x].seed = randint0(0x10000000);
1151 wilderness[y][x].entrance = 0;
1158 * Pointer to wilderness_type
1160 typedef wilderness_type *wilderness_type_ptr;
1163 * Initialize wilderness array
1165 errr init_wilderness(void)
1169 /* Allocate the wilderness (two-dimension array) */
1170 C_MAKE(wilderness, max_wild_y, wilderness_type_ptr);
1171 C_MAKE(wilderness[0], max_wild_x * max_wild_y, wilderness_type);
1173 /* Init the other pointers */
1174 for (i = 1; i < max_wild_y; i++)
1175 wilderness[i] = wilderness[0] + i * max_wild_x;
1177 generate_encounter = FALSE;
1183 bool change_wild_mode(void)
1186 bool have_pet = FALSE;
1188 /* It is in the middle of changing map */
1189 if (p_ptr->leaving) return FALSE;
1192 if (lite_town || vanilla_town)
1195 msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
1197 msg_print("No global map.");
1202 if (p_ptr->wild_mode)
1204 /* Save the location in the global map */
1205 p_ptr->wilderness_x = px;
1206 p_ptr->wilderness_y = py;
1208 /* Give first move to the player */
1209 p_ptr->energy_need = 0;
1211 /* Go back to the ordinary map */
1212 p_ptr->wild_mode = FALSE;
1215 p_ptr->leaving = TRUE;
1221 for (i = 1; i < m_max; i++)
1223 monster_type *m_ptr = &m_list[i];
1225 if (!m_ptr->r_idx) continue;
1226 if (is_pet(m_ptr) && i != p_ptr->riding) have_pet = TRUE;
1227 if (m_ptr->csleep) continue;
1228 if (m_ptr->cdis > MAX_SIGHT) continue;
1229 if (!is_hostile(m_ptr)) continue;
1231 msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤¤Ï¹°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
1233 msg_print("You cannot enter global map, since there is some monsters nearby!");
1242 cptr msg = "¥Ú¥Ã¥È¤òÃÖ¤¤¤Æ¹°è¥Þ¥Ã¥×¤ËÆþ¤ê¤Þ¤¹¤«¡©";
1244 cptr msg = "Do you leave your pets behind? ";
1247 if (!get_check_strict(msg, CHECK_OKAY_CANCEL))
1257 /* Remember the position */
1261 /* Cancel any special action */
1262 set_action(ACTION_NONE);
1264 /* Go into the global map */
1265 p_ptr->wild_mode = TRUE;
1268 p_ptr->leaving = TRUE;