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 /* x1, y1, x2, y2, num_depths, roughness */
529 plasma_recursive(1, 1, MAX_WID-2, MAX_HGT-2, table_size-1, roughness);
532 /* Use the complex RNG */
535 for (y1 = 1; y1 < MAX_HGT-1; y1++)
537 for (x1 = 1; x1 < MAX_WID-1; x1++)
539 cave[y1][x1].feat = terrain_table[terrain][cave[y1][x1].feat];
547 * Load a town or generate a terrain level using "plasma" fractals.
549 * x and y are the coordinates of the area in the wilderness.
550 * Border and corner are optimization flags to speed up the
551 * generation of the fractal terrain.
552 * If border is set then only the border of the terrain should
553 * be generated (for initializing the border structure).
554 * If corner is set then only the corners of the area are needed.
556 static void generate_area(int y, int x, bool border, bool corner)
560 /* Number of the town (if any) */
561 p_ptr->town_num = wilderness[y][x].town;
563 /* Set the base level */
564 base_level = wilderness[y][x].level;
566 /* Set the dungeon level */
569 /* Set the monster generation level */
570 monster_level = base_level;
572 /* Set the object generation level */
573 object_level = base_level;
576 /* Create the town */
579 /* Reset the buildings */
582 /* Initialize the town */
584 init_flags = INIT_CREATE_DUNGEON | INIT_ONLY_FEATURES;
586 init_flags = INIT_CREATE_DUNGEON;
588 process_dungeon_file("t_info.txt", 0, 0, MAX_HGT, MAX_WID);
590 if (!corner && !border) p_ptr->visit |= (1L << (p_ptr->town_num - 1));
594 int terrain = wilderness[y][x].terrain;
595 u32b seed = wilderness[y][x].seed;
597 generate_wilderness_area(terrain, seed, border, corner);
600 if (!corner && !wilderness[y][x].town)
603 * Place roads in the wilderness
604 * ToDo: make the road a bit more interresting
606 if (wilderness[y][x].road)
608 cave[MAX_HGT/2][MAX_WID/2].feat = FEAT_FLOOR;
610 if (wilderness[y-1][x].road)
613 for (y1 = 1; y1 < MAX_HGT/2; y1++)
616 cave[y1][x1].feat = FEAT_FLOOR;
620 if (wilderness[y+1][x].road)
623 for (y1 = MAX_HGT/2; y1 < MAX_HGT - 1; y1++)
626 cave[y1][x1].feat = FEAT_FLOOR;
630 if (wilderness[y][x+1].road)
633 for (x1 = MAX_WID/2; x1 < MAX_WID - 1; x1++)
636 cave[y1][x1].feat = FEAT_FLOOR;
640 if (wilderness[y][x-1].road)
643 for (x1 = 1; x1 < MAX_WID/2; x1++)
646 cave[y1][x1].feat = FEAT_FLOOR;
652 if (wilderness[y][x].entrance && !wilderness[y][x].town && (p_ptr->total_winner || !(d_info[wilderness[y][x].entrance].flags1 & DF1_WINNER)))
656 /* Hack -- Use the "simple" RNG */
659 /* Hack -- Induce consistant town layout */
660 Rand_value = wilderness[y][x].seed;
662 dy = rand_range(6, cur_hgt - 6);
663 dx = rand_range(6, cur_wid - 6);
665 cave[dy][dx].feat = FEAT_ENTRANCE;
666 cave[dy][dx].special = (byte)wilderness[y][x].entrance;
668 /* Use the complex RNG */
675 * Border of the wilderness area
677 static border_type border;
681 * Build the wilderness area outside of the town.
683 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_SOLID;
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_SOLID;
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_SOLID;
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_SOLID;
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 byte feat = c_ptr->mimic ? c_ptr->mimic : f_info[c_ptr->feat].mimic;
820 if (!is_mirror_grid(c_ptr) && (feat != FEAT_QUEST_ENTER) && (feat != FEAT_ENTRANCE))
823 c_ptr->info &= ~(CAVE_GLOW);
825 /* Darken "boring" features */
826 if ((feat <= FEAT_INVIS) ||
827 ((feat >= FEAT_DEEP_WATER) &&
828 (feat <= FEAT_MOUNTAIN) &&
829 (feat != FEAT_MUSEUM)))
831 /* Forget the grid */
832 c_ptr->info &= ~(CAVE_MARK);
835 else if (feat == FEAT_ENTRANCE)
838 c_ptr->info |= (CAVE_GLOW);
840 /* Hack -- Memorize lit grids if allowed */
841 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
847 if (p_ptr->teleport_town)
849 for (y = 0; y < cur_hgt; y++)
851 for (x = 0; x < cur_wid; x++)
853 if (((cave[y][x].feat - FEAT_BLDG_HEAD) == 4) || ((p_ptr->town_num == 1) && ((cave[y][x].feat - FEAT_BLDG_HEAD) == 0)))
855 if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
861 p_ptr->teleport_town = FALSE;
864 else if (p_ptr->leaving_dungeon)
866 for (y = 0; y < cur_hgt; y++)
868 for (x = 0; x < cur_wid; x++)
870 if (cave[y][x].feat == FEAT_ENTRANCE)
872 if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
878 p_ptr->teleport_town = FALSE;
881 player_place(p_ptr->oldpy, p_ptr->oldpx);
882 p_ptr->leftbldg = FALSE;
883 /* p_ptr->leaving_dungeon = FALSE;*/
885 lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
887 /* Make some residents */
888 for (i = 0; i < lim; i++)
892 if (!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))))
893 mode |= PM_ALLOW_SLEEP;
895 /* Make a resident */
896 (void)alloc_monster(generate_encounter ? 0 : 3, mode);
899 if(generate_encounter) ambush_flag = TRUE;
900 generate_encounter = FALSE;
902 /* Fill the arrays of floors and walls in the good proportions */
903 set_floor_and_wall(0);
905 /* Set rewarded quests to finished */
906 for (i = 0; i < max_quests; i++)
908 if (quest[i].status == QUEST_STATUS_REWARDED)
909 quest[i].status = QUEST_STATUS_FINISHED;
915 * Build the wilderness area.
918 void wilderness_gen_small()
922 /* To prevent stupid things */
923 for (i = 0; i < MAX_WID; i++)
924 for (j = 0; j < MAX_HGT; j++)
926 cave[j][i].feat = FEAT_PERM_SOLID;
929 /* Init the wilderness */
930 process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
933 for (i = 0; i < max_wild_x; i++)
934 for (j = 0; j < max_wild_y; j++)
936 if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
938 cave[j][i].feat = FEAT_TOWN;
939 cave[j][i].special = wilderness[j][i].town;
941 else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
942 else if (wilderness[j][i].entrance && (p_ptr->total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
944 cave[j][i].feat = FEAT_ENTRANCE;
945 cave[j][i].special = (byte)wilderness[j][i].entrance;
947 else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
949 cave[j][i].info |= (CAVE_GLOW | CAVE_MARK);
952 cur_hgt = (s16b) max_wild_y;
953 cur_wid = (s16b) max_wild_x;
955 if (cur_hgt > MAX_HGT) cur_hgt = MAX_HGT;
956 if (cur_wid > MAX_WID) cur_wid = MAX_WID;
958 /* Assume illegal panel */
959 panel_row_min = cur_hgt;
960 panel_col_min = cur_wid;
962 /* Place the player */
963 px = p_ptr->wilderness_x;
964 py = p_ptr->wilderness_y;
970 typedef struct wilderness_grid wilderness_grid;
972 struct wilderness_grid
974 int terrain; /* Terrain type */
975 int town; /* Town number */
976 s16b level; /* Level of the wilderness */
977 byte road; /* Road */
978 char name[32]; /* Name of the town/wilderness */
982 static wilderness_grid w_letter[255];
986 * Parse a sub-file of the "extra info"
988 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
998 if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
1002 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
1015 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
1017 int index = zz[0][0];
1020 w_letter[index].terrain = atoi(zz[1]);
1022 w_letter[index].terrain = 0;
1025 w_letter[index].level = atoi(zz[2]);
1027 w_letter[index].level = 0;
1030 w_letter[index].town = atoi(zz[3]);
1032 w_letter[index].town = 0;
1035 w_letter[index].road = atoi(zz[4]);
1037 w_letter[index].road = 0;
1040 strcpy(w_letter[index].name, zz[5]);
1042 w_letter[index].name[0] = 0;
1047 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1053 /* Process "W:D:<layout> */
1054 /* Layout of the wilderness */
1057 /* Acquire the text */
1060 /* Length of the text */
1061 int len = strlen(s);
1063 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
1067 wilderness[*y][*x].terrain = w_letter[idx].terrain;
1069 wilderness[*y][*x].level = w_letter[idx].level;
1071 wilderness[*y][*x].town = w_letter[idx].town;
1073 wilderness[*y][*x].road = w_letter[idx].road;
1075 strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
1083 /* Process "W:P:<x>:<y> - starting position in the wilderness */
1086 if ((p_ptr->wilderness_x == 0) &&
1087 (p_ptr->wilderness_y == 0))
1089 if (tokenize(buf+4, 2, zz, 0) == 2)
1091 p_ptr->wilderness_y = atoi(zz[0]);
1092 p_ptr->wilderness_x = atoi(zz[1]);
1094 if ((p_ptr->wilderness_x < 1) ||
1095 (p_ptr->wilderness_x > max_wild_x) ||
1096 (p_ptr->wilderness_y < 1) ||
1097 (p_ptr->wilderness_y > max_wild_y))
1099 return (PARSE_ERROR_OUT_OF_BOUNDS);
1104 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1113 return (PARSE_ERROR_UNDEFINED_DIRECTIVE);
1116 for (i = 1; i < max_d_idx; i++)
1118 if (!d_info[i].maxdepth) continue;
1119 wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
1120 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
1121 wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
1130 * Generate the random seeds for the wilderness
1132 void seed_wilderness(void)
1136 /* Init wilderness seeds */
1137 for (x = 0; x < max_wild_x; x++)
1139 for (y = 0; y < max_wild_y; y++)
1141 wilderness[y][x].seed = randint0(0x10000000);
1142 wilderness[y][x].entrance = 0;
1149 * Pointer to wilderness_type
1151 typedef wilderness_type *wilderness_type_ptr;
1154 * Initialize wilderness array
1156 errr init_wilderness(void)
1160 /* Allocate the wilderness (two-dimension array) */
1161 C_MAKE(wilderness, max_wild_y, wilderness_type_ptr);
1162 C_MAKE(wilderness[0], max_wild_x * max_wild_y, wilderness_type);
1164 /* Init the other pointers */
1165 for (i = 1; i < max_wild_y; i++)
1166 wilderness[i] = wilderness[0] + i * max_wild_x;
1168 generate_encounter = FALSE;
1174 bool change_wild_mode(void)
1177 bool have_pet = FALSE;
1179 if (lite_town || vanilla_town)
1182 msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
1184 msg_print("No global mal");
1188 if (!p_ptr->wild_mode)
1190 for (i = 1; i < m_max; i++)
1192 monster_type *m_ptr = &m_list[i];
1194 if (!m_ptr->r_idx) continue;
1195 if (is_pet(m_ptr) && i != p_ptr->riding) have_pet = TRUE;
1196 if (m_ptr->csleep) continue;
1197 if (m_ptr->cdis > MAX_SIGHT) continue;
1198 if (!is_hostile(m_ptr)) continue;
1200 msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤¤Ï¹°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
1202 msg_print("You cannot enter global map, since there is some monsters nearby!");
1211 if(!get_check_strict("¥Ú¥Ã¥È¤òÃÖ¤¤¤Æ¹°è¥Þ¥Ã¥×¤ËÆþ¤ê¤Þ¤¹¤«¡©", CHECK_OKAY_CANCEL))
1213 if(!get_check_strict("Do you leave your pets behind? ", CHECK_OKAY_CANCEL))
1224 set_action(ACTION_NONE);
1226 p_ptr->wild_mode = !p_ptr->wild_mode;
1229 p_ptr->leaving = TRUE;