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 */
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;
513 * Initialize the four corners
514 * ToDo: calculate the medium height of the adjacent
515 * terrains for every corner.
517 cave[1][1].feat = randint0(table_size);
518 cave[MAX_HGT-2][1].feat = randint0(table_size);
519 cave[1][MAX_WID-2].feat = randint0(table_size);
520 cave[MAX_HGT-2][MAX_WID-2].feat = randint0(table_size);
524 /* x1, y1, x2, y2, num_depths, roughness */
525 plasma_recursive(1, 1, MAX_WID-2, MAX_HGT-2, table_size-1, roughness);
528 /* Use the complex RNG */
531 for (y1 = 1; y1 < MAX_HGT-1; y1++)
533 for (x1 = 1; x1 < MAX_WID-1; x1++)
535 cave[y1][x1].feat = terrain_table[terrain][cave[y1][x1].feat];
543 * Load a town or generate a terrain level using "plasma" fractals.
545 * x and y are the coordinates of the area in the wilderness.
546 * Border and corner are optimization flags to speed up the
547 * generation of the fractal terrain.
548 * If border is set then only the border of the terrain should
549 * be generated (for initializing the border structure).
550 * If corner is set then only the corners of the area are needed.
552 static void generate_area(int y, int x, bool border, bool corner)
556 /* Number of the town (if any) */
557 p_ptr->town_num = wilderness[y][x].town;
559 /* Set the base level */
560 base_level = wilderness[y][x].level;
562 /* Set the dungeon level */
565 /* Set the monster generation level */
566 monster_level = base_level;
568 /* Set the object generation level */
569 object_level = base_level;
572 /* Create the town */
575 /* Reset the buildings */
578 /* Initialize the town */
580 init_flags = INIT_CREATE_DUNGEON | INIT_ONLY_FEATURES;
582 init_flags = INIT_CREATE_DUNGEON;
584 process_dungeon_file("t_info.txt", 0, 0, MAX_HGT, MAX_WID);
586 if (!corner && !border) p_ptr->visit |= (1L << (p_ptr->town_num - 1));
590 int terrain = wilderness[y][x].terrain;
591 u32b seed = wilderness[y][x].seed;
593 generate_wilderness_area(terrain, seed, border, corner);
596 if (!corner && !wilderness[y][x].town)
599 * Place roads in the wilderness
600 * ToDo: make the road a bit more interresting
602 if (wilderness[y][x].road)
604 cave[MAX_HGT/2][MAX_WID/2].feat = FEAT_FLOOR;
606 if (wilderness[y-1][x].road)
609 for (y1 = 1; y1 < MAX_HGT/2; y1++)
612 cave[y1][x1].feat = FEAT_FLOOR;
616 if (wilderness[y+1][x].road)
619 for (y1 = MAX_HGT/2; y1 < MAX_HGT - 1; y1++)
622 cave[y1][x1].feat = FEAT_FLOOR;
626 if (wilderness[y][x+1].road)
629 for (x1 = MAX_WID/2; x1 < MAX_WID - 1; x1++)
632 cave[y1][x1].feat = FEAT_FLOOR;
636 if (wilderness[y][x-1].road)
639 for (x1 = 1; x1 < MAX_WID/2; x1++)
642 cave[y1][x1].feat = FEAT_FLOOR;
648 if (wilderness[y][x].entrance && !wilderness[y][x].town && (p_ptr->total_winner || !(d_info[wilderness[y][x].entrance].flags1 & DF1_WINNER)))
652 /* Hack -- Use the "simple" RNG */
655 /* Hack -- Induce consistant town layout */
656 Rand_value = wilderness[y][x].seed;
658 dy = rand_range(6, cur_hgt - 6);
659 dx = rand_range(6, cur_wid - 6);
661 cave[dy][dx].feat = FEAT_ENTRANCE;
662 cave[dy][dx].special = wilderness[y][x].entrance;
664 /* Use the complex RNG */
671 * Border of the wilderness area
673 static border_type border;
677 * Build the wilderness area outside of the town.
679 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;
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;
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;
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;
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 /* Feature code (applying "mimic" field) */
815 f_ptr = &f_info[get_feat_mimic(c_ptr)];
817 if (!is_mirror_grid(c_ptr) && !have_flag(f_ptr->flags, FF_QUEST_ENTER) &&
818 !have_flag(f_ptr->flags, FF_ENTRANCE))
821 c_ptr->info &= ~(CAVE_GLOW);
823 /* Darken "boring" features */
824 if (!have_flag(f_ptr->flags, FF_REMEMBER))
826 /* Forget the grid */
827 c_ptr->info &= ~(CAVE_MARK);
830 else if (have_flag(f_ptr->flags, FF_ENTRANCE))
833 c_ptr->info |= (CAVE_GLOW);
835 /* Hack -- Memorize lit grids if allowed */
836 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
842 if (p_ptr->teleport_town)
844 for (y = 0; y < cur_hgt; y++)
846 for (x = 0; x < cur_wid; x++)
848 /* Get the cave grid */
851 /* Seeing true feature code (ignore mimic) */
852 f_ptr = &f_info[c_ptr->feat];
854 if (have_flag(f_ptr->flags, FF_BLDG))
856 if ((f_ptr->power == 4) || ((p_ptr->town_num == 1) && (f_ptr->power == 0)))
858 if (c_ptr->m_idx) delete_monster_idx(c_ptr->m_idx);
865 p_ptr->teleport_town = FALSE;
868 else if (p_ptr->leaving_dungeon)
870 for (y = 0; y < cur_hgt; y++)
872 for (x = 0; x < cur_wid; x++)
874 /* Get the cave grid */
877 if (have_flag(f_flags_grid(c_ptr), FF_ENTRANCE))
879 if (c_ptr->m_idx) delete_monster_idx(c_ptr->m_idx);
885 p_ptr->teleport_town = FALSE;
888 player_place(p_ptr->oldpy, p_ptr->oldpx);
889 /* p_ptr->leaving_dungeon = FALSE;*/
891 lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
893 /* Make some residents */
894 for (i = 0; i < lim; i++)
898 if (!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))))
899 mode |= PM_ALLOW_SLEEP;
901 /* Make a resident */
902 (void)alloc_monster(generate_encounter ? 0 : 3, mode);
905 if(generate_encounter) ambush_flag = TRUE;
906 generate_encounter = FALSE;
908 /* Fill the arrays of floors and walls in the good proportions */
909 set_floor_and_wall(0);
911 /* Set rewarded quests to finished */
912 for (i = 0; i < max_quests; i++)
914 if (quest[i].status == QUEST_STATUS_REWARDED)
915 quest[i].status = QUEST_STATUS_FINISHED;
921 * Build the wilderness area.
924 void wilderness_gen_small()
928 /* To prevent stupid things */
929 for (i = 0; i < MAX_WID; i++)
930 for (j = 0; j < MAX_HGT; j++)
932 cave[j][i].feat = FEAT_PERM;
935 /* Init the wilderness */
936 process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
939 for (i = 0; i < max_wild_x; i++)
940 for (j = 0; j < max_wild_y; j++)
942 if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
944 cave[j][i].feat = FEAT_TOWN;
945 cave[j][i].special = wilderness[j][i].town;
947 else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
948 else if (wilderness[j][i].entrance && (p_ptr->total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
950 cave[j][i].feat = FEAT_ENTRANCE;
951 cave[j][i].special = (byte)wilderness[j][i].entrance;
953 else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
955 cave[j][i].info |= (CAVE_GLOW | CAVE_MARK);
958 cur_hgt = (s16b) max_wild_y;
959 cur_wid = (s16b) max_wild_x;
961 if (cur_hgt > MAX_HGT) cur_hgt = MAX_HGT;
962 if (cur_wid > MAX_WID) cur_wid = MAX_WID;
964 /* Assume illegal panel */
965 panel_row_min = cur_hgt;
966 panel_col_min = cur_wid;
968 /* Place the player */
969 px = p_ptr->wilderness_x;
970 py = p_ptr->wilderness_y;
976 typedef struct wilderness_grid wilderness_grid;
978 struct wilderness_grid
980 int terrain; /* Terrain type */
981 int town; /* Town number */
982 s16b level; /* Level of the wilderness */
983 byte road; /* Road */
984 char name[32]; /* Name of the town/wilderness */
988 static wilderness_grid w_letter[255];
992 * Parse a sub-file of the "extra info"
994 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
1004 if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
1008 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
1021 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
1023 int index = zz[0][0];
1026 w_letter[index].terrain = atoi(zz[1]);
1028 w_letter[index].terrain = 0;
1031 w_letter[index].level = atoi(zz[2]);
1033 w_letter[index].level = 0;
1036 w_letter[index].town = atoi(zz[3]);
1038 w_letter[index].town = 0;
1041 w_letter[index].road = atoi(zz[4]);
1043 w_letter[index].road = 0;
1046 strcpy(w_letter[index].name, zz[5]);
1048 w_letter[index].name[0] = 0;
1053 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1059 /* Process "W:D:<layout> */
1060 /* Layout of the wilderness */
1063 /* Acquire the text */
1066 /* Length of the text */
1067 int len = strlen(s);
1069 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
1073 wilderness[*y][*x].terrain = w_letter[idx].terrain;
1075 wilderness[*y][*x].level = w_letter[idx].level;
1077 wilderness[*y][*x].town = w_letter[idx].town;
1079 wilderness[*y][*x].road = w_letter[idx].road;
1081 strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
1089 /* Process "W:P:<x>:<y> - starting position in the wilderness */
1092 if ((p_ptr->wilderness_x == 0) &&
1093 (p_ptr->wilderness_y == 0))
1095 if (tokenize(buf+4, 2, zz, 0) == 2)
1097 p_ptr->wilderness_y = atoi(zz[0]);
1098 p_ptr->wilderness_x = atoi(zz[1]);
1100 if ((p_ptr->wilderness_x < 1) ||
1101 (p_ptr->wilderness_x > max_wild_x) ||
1102 (p_ptr->wilderness_y < 1) ||
1103 (p_ptr->wilderness_y > max_wild_y))
1105 return (PARSE_ERROR_OUT_OF_BOUNDS);
1110 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1119 return (PARSE_ERROR_UNDEFINED_DIRECTIVE);
1122 for (i = 1; i < max_d_idx; i++)
1124 if (!d_info[i].maxdepth) continue;
1125 wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
1126 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
1127 wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
1136 * Generate the random seeds for the wilderness
1138 void seed_wilderness(void)
1142 /* Init wilderness seeds */
1143 for (x = 0; x < max_wild_x; x++)
1145 for (y = 0; y < max_wild_y; y++)
1147 wilderness[y][x].seed = randint0(0x10000000);
1148 wilderness[y][x].entrance = 0;
1155 * Pointer to wilderness_type
1157 typedef wilderness_type *wilderness_type_ptr;
1160 * Initialize wilderness array
1162 errr init_wilderness(void)
1166 /* Allocate the wilderness (two-dimension array) */
1167 C_MAKE(wilderness, max_wild_y, wilderness_type_ptr);
1168 C_MAKE(wilderness[0], max_wild_x * max_wild_y, wilderness_type);
1170 /* Init the other pointers */
1171 for (i = 1; i < max_wild_y; i++)
1172 wilderness[i] = wilderness[0] + i * max_wild_x;
1174 generate_encounter = FALSE;
1180 bool change_wild_mode(void)
1183 bool have_pet = FALSE;
1185 /* It is in the middle of changing map */
1186 if (p_ptr->leaving) return FALSE;
1189 if (lite_town || vanilla_town)
1192 msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
1194 msg_print("No global map.");
1199 if (p_ptr->wild_mode)
1201 /* Save the location in the global map */
1202 p_ptr->wilderness_x = px;
1203 p_ptr->wilderness_y = py;
1205 /* Give first move to the player */
1206 p_ptr->energy_need = 0;
1208 /* Go back to the ordinary map */
1209 p_ptr->wild_mode = FALSE;
1212 p_ptr->leaving = TRUE;
1218 for (i = 1; i < m_max; i++)
1220 monster_type *m_ptr = &m_list[i];
1222 if (!m_ptr->r_idx) continue;
1223 if (is_pet(m_ptr) && i != p_ptr->riding) have_pet = TRUE;
1224 if (m_ptr->csleep) continue;
1225 if (m_ptr->cdis > MAX_SIGHT) continue;
1226 if (!is_hostile(m_ptr)) continue;
1228 msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤¤Ï¹°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
1230 msg_print("You cannot enter global map, since there is some monsters nearby!");
1239 cptr msg = "¥Ú¥Ã¥È¤òÃÖ¤¤¤Æ¹°è¥Þ¥Ã¥×¤ËÆþ¤ê¤Þ¤¹¤«¡©";
1241 cptr msg = "Do you leave your pets behind? ";
1244 if (!get_check_strict(msg, CHECK_OKAY_CANCEL))
1254 /* Remember the position */
1258 /* Cancel any special action */
1259 set_action(ACTION_NONE);
1261 /* Go into the global map */
1262 p_ptr->wild_mode = TRUE;
1265 p_ptr->leaving = TRUE;