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 */
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. */
481 /* The outer wall is easy */
482 if (terrain == TERRAIN_EDGE)
484 /* Create level background */
485 for (y1 = 0; y1 < MAX_HGT; y1++)
487 for (x1 = 0; x1 < MAX_WID; x1++)
489 cave[y1][x1].feat = FEAT_PERM_SOLID;
493 /* We are done already */
498 /* Hack -- Use the "simple" RNG */
501 /* Hack -- Induce consistant town layout */
506 /* Create level background */
507 for (y1 = 0; y1 < MAX_HGT; y1++)
509 for (x1 = 0; x1 < MAX_WID; x1++)
511 cave[y1][x1].feat = table_size / 2;
517 * Initialize the four corners
518 * ToDo: calculate the medium height of the adjacent
519 * terrains for every corner.
521 cave[1][1].feat = (byte)randint0(table_size);
522 cave[MAX_HGT-2][1].feat = (byte)randint0(table_size);
523 cave[1][MAX_WID-2].feat = (byte)randint0(table_size);
524 cave[MAX_HGT-2][MAX_WID-2].feat = (byte)randint0(table_size);
528 /* Hack -- preserve four corners */
529 s16b north_west = cave[1][1].feat;
530 s16b south_west = cave[MAX_HGT - 2][1].feat;
531 s16b north_east = cave[1][MAX_WID - 2].feat;
532 s16b south_east = cave[MAX_HGT - 2][MAX_WID - 2].feat;
534 /* x1, y1, x2, y2, num_depths, roughness */
535 plasma_recursive(1, 1, MAX_WID-2, MAX_HGT-2, table_size-1, roughness);
537 /* Hack -- copyback four corners */
538 cave[1][1].feat = north_west;
539 cave[MAX_HGT - 2][1].feat = south_west;
540 cave[1][MAX_WID - 2].feat = north_east;
541 cave[MAX_HGT - 2][MAX_WID - 2].feat = south_east;
543 for (y1 = 1; y1 < MAX_HGT - 1; y1++)
545 for (x1 = 1; x1 < MAX_WID - 1; x1++)
547 cave[y1][x1].feat = terrain_table[terrain][cave[y1][x1].feat];
551 else /* Hack -- only four corners */
553 cave[1][1].feat = terrain_table[terrain][cave[1][1].feat];
554 cave[MAX_HGT - 2][1].feat = terrain_table[terrain][cave[MAX_HGT - 2][1].feat];
555 cave[1][MAX_WID - 2].feat = terrain_table[terrain][cave[1][MAX_WID - 2].feat];
556 cave[MAX_HGT - 2][MAX_WID - 2].feat = terrain_table[terrain][cave[MAX_HGT - 2][MAX_WID - 2].feat];
559 /* Use the complex RNG */
566 * Load a town or generate a terrain level using "plasma" fractals.
568 * x and y are the coordinates of the area in the wilderness.
569 * Border and corner are optimization flags to speed up the
570 * generation of the fractal terrain.
571 * If border is set then only the border of the terrain should
572 * be generated (for initializing the border structure).
573 * If corner is set then only the corners of the area are needed.
575 static void generate_area(int y, int x, bool border, bool corner)
579 /* Number of the town (if any) */
580 p_ptr->town_num = wilderness[y][x].town;
582 /* Set the base level */
583 base_level = wilderness[y][x].level;
585 /* Set the dungeon level */
588 /* Set the monster generation level */
589 monster_level = base_level;
591 /* Set the object generation level */
592 object_level = base_level;
595 /* Create the town */
598 /* Reset the buildings */
601 /* Initialize the town */
603 init_flags = INIT_CREATE_DUNGEON | INIT_ONLY_FEATURES;
605 init_flags = INIT_CREATE_DUNGEON;
607 process_dungeon_file("t_info.txt", 0, 0, MAX_HGT, MAX_WID);
609 if (!corner && !border) p_ptr->visit |= (1L << (p_ptr->town_num - 1));
613 int terrain = wilderness[y][x].terrain;
614 u32b seed = wilderness[y][x].seed;
616 generate_wilderness_area(terrain, seed, border, corner);
619 if (!corner && !wilderness[y][x].town)
622 * Place roads in the wilderness
623 * ToDo: make the road a bit more interresting
625 if (wilderness[y][x].road)
627 cave[MAX_HGT/2][MAX_WID/2].feat = FEAT_FLOOR;
629 if (wilderness[y-1][x].road)
632 for (y1 = 1; y1 < MAX_HGT/2; y1++)
635 cave[y1][x1].feat = FEAT_FLOOR;
639 if (wilderness[y+1][x].road)
642 for (y1 = MAX_HGT/2; y1 < MAX_HGT - 1; y1++)
645 cave[y1][x1].feat = FEAT_FLOOR;
649 if (wilderness[y][x+1].road)
652 for (x1 = MAX_WID/2; x1 < MAX_WID - 1; x1++)
655 cave[y1][x1].feat = FEAT_FLOOR;
659 if (wilderness[y][x-1].road)
662 for (x1 = 1; x1 < MAX_WID/2; x1++)
665 cave[y1][x1].feat = FEAT_FLOOR;
671 if (wilderness[y][x].entrance && !wilderness[y][x].town && (p_ptr->total_winner || !(d_info[wilderness[y][x].entrance].flags1 & DF1_WINNER)))
675 /* Hack -- Use the "simple" RNG */
678 /* Hack -- Induce consistant town layout */
679 Rand_value = wilderness[y][x].seed;
681 dy = rand_range(6, cur_hgt - 6);
682 dx = rand_range(6, cur_wid - 6);
684 cave[dy][dx].feat = FEAT_ENTRANCE;
685 cave[dy][dx].special = (byte)wilderness[y][x].entrance;
687 /* Use the complex RNG */
694 * Border of the wilderness area
696 static border_type border;
700 * Build the wilderness area outside of the town.
702 void wilderness_gen(void)
711 /* Assume illegal panel */
712 panel_row_min = cur_hgt;
713 panel_col_min = cur_wid;
715 /* Init the wilderness */
717 process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
719 x = p_ptr->wilderness_x;
720 y = p_ptr->wilderness_y;
722 /* Prepare allocation table */
723 get_mon_num_prep(get_monster_hook(), NULL);
726 generate_area(y - 1, x, TRUE, FALSE);
728 for (i = 1; i < MAX_WID - 1; i++)
730 border.north[i] = cave[MAX_HGT - 2][i].feat;
734 generate_area(y + 1, x, TRUE, FALSE);
736 for (i = 1; i < MAX_WID - 1; i++)
738 border.south[i] = cave[1][i].feat;
742 generate_area(y, x - 1, TRUE, FALSE);
744 for (i = 1; i < MAX_HGT - 1; i++)
746 border.west[i] = cave[i][MAX_WID - 2].feat;
750 generate_area(y, x + 1, TRUE, FALSE);
752 for (i = 1; i < MAX_HGT - 1; i++)
754 border.east[i] = cave[i][1].feat;
757 /* North west corner */
758 generate_area(y - 1, x - 1, FALSE, TRUE);
759 border.north_west = cave[MAX_HGT - 2][MAX_WID - 2].feat;
761 /* North east corner */
762 generate_area(y - 1, x + 1, FALSE, TRUE);
763 border.north_east = cave[MAX_HGT - 2][1].feat;
765 /* South west corner */
766 generate_area(y + 1, x - 1, FALSE, TRUE);
767 border.south_west = cave[1][MAX_WID - 2].feat;
769 /* South east corner */
770 generate_area(y + 1, x + 1, FALSE, TRUE);
771 border.south_east = cave[1][1].feat;
774 /* Create terrain of the current area */
775 generate_area(y, x, FALSE, FALSE);
778 /* Special boundary walls -- North */
779 for (i = 0; i < MAX_WID; i++)
781 cave[0][i].feat = FEAT_PERM_SOLID;
782 cave[0][i].mimic = border.north[i];
785 /* Special boundary walls -- South */
786 for (i = 0; i < MAX_WID; i++)
788 cave[MAX_HGT - 1][i].feat = FEAT_PERM_SOLID;
789 cave[MAX_HGT - 1][i].mimic = border.south[i];
792 /* Special boundary walls -- West */
793 for (i = 0; i < MAX_HGT; i++)
795 cave[i][0].feat = FEAT_PERM_SOLID;
796 cave[i][0].mimic = border.west[i];
799 /* Special boundary walls -- East */
800 for (i = 0; i < MAX_HGT; i++)
802 cave[i][MAX_WID - 1].feat = FEAT_PERM_SOLID;
803 cave[i][MAX_WID - 1].mimic = border.east[i];
806 /* North west corner */
807 cave[0][0].mimic = border.north_west;
809 /* North east corner */
810 cave[0][MAX_WID - 1].mimic = border.north_east;
812 /* South west corner */
813 cave[MAX_HGT - 1][0].mimic = border.south_west;
815 /* South east corner */
816 cave[MAX_HGT - 1][MAX_WID - 1].mimic = border.south_east;
818 /* Light up or darken the area */
819 for (y = 0; y < cur_hgt; y++)
821 for (x = 0; x < cur_wid; x++)
823 /* Get the cave grid */
829 c_ptr->info |= (CAVE_GLOW);
831 /* Hack -- Memorize lit grids if allowed */
832 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
836 /* Feature code (applying "mimic" field) */
837 byte feat = f_info[c_ptr->mimic ? c_ptr->mimic : c_ptr->feat].mimic;
839 if (!is_mirror_grid(c_ptr) && (feat != FEAT_QUEST_ENTER) && (feat != FEAT_ENTRANCE))
842 c_ptr->info &= ~(CAVE_GLOW);
844 /* Darken "boring" features */
845 if ((feat <= FEAT_INVIS) ||
846 ((feat >= FEAT_DEEP_WATER) &&
847 (feat <= FEAT_MOUNTAIN) &&
848 (feat != FEAT_MUSEUM)))
850 /* Forget the grid */
851 c_ptr->info &= ~(CAVE_MARK);
854 else if (feat == FEAT_ENTRANCE)
857 c_ptr->info |= (CAVE_GLOW);
859 /* Hack -- Memorize lit grids if allowed */
860 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
866 if (p_ptr->teleport_town)
868 for (y = 0; y < cur_hgt; y++)
870 for (x = 0; x < cur_wid; x++)
872 if (((cave[y][x].feat - FEAT_BLDG_HEAD) == 4) || ((p_ptr->town_num == 1) && ((cave[y][x].feat - FEAT_BLDG_HEAD) == 0)))
874 if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
880 p_ptr->teleport_town = FALSE;
883 else if (p_ptr->leaving_dungeon)
885 for (y = 0; y < cur_hgt; y++)
887 for (x = 0; x < cur_wid; x++)
889 if (cave[y][x].feat == FEAT_ENTRANCE)
891 if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
897 p_ptr->teleport_town = FALSE;
900 player_place(p_ptr->oldpy, p_ptr->oldpx);
901 p_ptr->leftbldg = FALSE;
902 /* p_ptr->leaving_dungeon = FALSE;*/
904 lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
906 /* Make some residents */
907 for (i = 0; i < lim; i++)
911 if (!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))))
912 mode |= PM_ALLOW_SLEEP;
914 /* Make a resident */
915 (void)alloc_monster(generate_encounter ? 0 : 3, mode);
918 if(generate_encounter) ambush_flag = TRUE;
919 generate_encounter = FALSE;
921 /* Fill the arrays of floors and walls in the good proportions */
922 set_floor_and_wall(0);
924 /* Set rewarded quests to finished */
925 for (i = 0; i < max_quests; i++)
927 if (quest[i].status == QUEST_STATUS_REWARDED)
928 quest[i].status = QUEST_STATUS_FINISHED;
934 * Build the wilderness area.
937 void wilderness_gen_small()
941 /* To prevent stupid things */
942 for (i = 0; i < MAX_WID; i++)
943 for (j = 0; j < MAX_HGT; j++)
945 cave[j][i].feat = FEAT_PERM_SOLID;
948 /* Init the wilderness */
949 process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
952 for (i = 0; i < max_wild_x; i++)
953 for (j = 0; j < max_wild_y; j++)
955 if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
957 cave[j][i].feat = FEAT_TOWN;
958 cave[j][i].special = wilderness[j][i].town;
960 else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
961 else if (wilderness[j][i].entrance && (p_ptr->total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
963 cave[j][i].feat = FEAT_ENTRANCE;
964 cave[j][i].special = (byte)wilderness[j][i].entrance;
966 else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
968 cave[j][i].info |= (CAVE_GLOW | CAVE_MARK);
971 cur_hgt = (s16b) max_wild_y;
972 cur_wid = (s16b) max_wild_x;
974 if (cur_hgt > MAX_HGT) cur_hgt = MAX_HGT;
975 if (cur_wid > MAX_WID) cur_wid = MAX_WID;
977 /* Assume illegal panel */
978 panel_row_min = cur_hgt;
979 panel_col_min = cur_wid;
981 /* Place the player */
982 px = p_ptr->wilderness_x;
983 py = p_ptr->wilderness_y;
989 typedef struct wilderness_grid wilderness_grid;
991 struct wilderness_grid
993 int terrain; /* Terrain type */
994 int town; /* Town number */
995 s16b level; /* Level of the wilderness */
996 byte road; /* Road */
997 char name[32]; /* Name of the town/wilderness */
1001 static wilderness_grid w_letter[255];
1005 * Parse a sub-file of the "extra info"
1007 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
1017 if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
1021 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
1034 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
1036 int index = zz[0][0];
1039 w_letter[index].terrain = atoi(zz[1]);
1041 w_letter[index].terrain = 0;
1044 w_letter[index].level = atoi(zz[2]);
1046 w_letter[index].level = 0;
1049 w_letter[index].town = atoi(zz[3]);
1051 w_letter[index].town = 0;
1054 w_letter[index].road = atoi(zz[4]);
1056 w_letter[index].road = 0;
1059 strcpy(w_letter[index].name, zz[5]);
1061 w_letter[index].name[0] = 0;
1066 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1072 /* Process "W:D:<layout> */
1073 /* Layout of the wilderness */
1076 /* Acquire the text */
1079 /* Length of the text */
1080 int len = strlen(s);
1082 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
1086 wilderness[*y][*x].terrain = w_letter[idx].terrain;
1088 wilderness[*y][*x].level = w_letter[idx].level;
1090 wilderness[*y][*x].town = w_letter[idx].town;
1092 wilderness[*y][*x].road = w_letter[idx].road;
1094 strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
1102 /* Process "W:P:<x>:<y> - starting position in the wilderness */
1105 if ((p_ptr->wilderness_x == 0) &&
1106 (p_ptr->wilderness_y == 0))
1108 if (tokenize(buf+4, 2, zz, 0) == 2)
1110 p_ptr->wilderness_y = atoi(zz[0]);
1111 p_ptr->wilderness_x = atoi(zz[1]);
1113 if ((p_ptr->wilderness_x < 1) ||
1114 (p_ptr->wilderness_x > max_wild_x) ||
1115 (p_ptr->wilderness_y < 1) ||
1116 (p_ptr->wilderness_y > max_wild_y))
1118 return (PARSE_ERROR_OUT_OF_BOUNDS);
1123 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1132 return (PARSE_ERROR_UNDEFINED_DIRECTIVE);
1135 for (i = 1; i < max_d_idx; i++)
1137 if (!d_info[i].maxdepth) continue;
1138 wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
1139 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
1140 wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
1149 * Generate the random seeds for the wilderness
1151 void seed_wilderness(void)
1155 /* Init wilderness seeds */
1156 for (x = 0; x < max_wild_x; x++)
1158 for (y = 0; y < max_wild_y; y++)
1160 wilderness[y][x].seed = randint0(0x10000000);
1161 wilderness[y][x].entrance = 0;
1168 * Pointer to wilderness_type
1170 typedef wilderness_type *wilderness_type_ptr;
1173 * Initialize wilderness array
1175 errr init_wilderness(void)
1179 /* Allocate the wilderness (two-dimension array) */
1180 C_MAKE(wilderness, max_wild_y, wilderness_type_ptr);
1181 C_MAKE(wilderness[0], max_wild_x * max_wild_y, wilderness_type);
1183 /* Init the other pointers */
1184 for (i = 1; i < max_wild_y; i++)
1185 wilderness[i] = wilderness[0] + i * max_wild_x;
1187 generate_encounter = FALSE;
1193 bool change_wild_mode(void)
1196 bool have_pet = FALSE;
1198 if (lite_town || vanilla_town)
1201 msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
1203 msg_print("No global map.");
1207 if (!p_ptr->wild_mode)
1209 for (i = 1; i < m_max; i++)
1211 monster_type *m_ptr = &m_list[i];
1213 if (!m_ptr->r_idx) continue;
1214 if (is_pet(m_ptr) && i != p_ptr->riding) have_pet = TRUE;
1215 if (m_ptr->csleep) continue;
1216 if (m_ptr->cdis > MAX_SIGHT) continue;
1217 if (!is_hostile(m_ptr)) continue;
1219 msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤¤Ï¹°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
1221 msg_print("You cannot enter global map, since there is some monsters nearby!");
1230 if(!get_check_strict("¥Ú¥Ã¥È¤òÃÖ¤¤¤Æ¹°è¥Þ¥Ã¥×¤ËÆþ¤ê¤Þ¤¹¤«¡©", CHECK_OKAY_CANCEL))
1232 if(!get_check_strict("Do you leave your pets behind? ", CHECK_OKAY_CANCEL))
1243 set_action(ACTION_NONE);
1245 p_ptr->wild_mode = !p_ptr->wild_mode;
1248 p_ptr->leaving = TRUE;