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);
54 * Helper for plasma generation.
56 static void perturb_point_mid(int x1, int x2, int x3, int x4,
57 int xmid, int ymid, int rough, int depth_max)
60 * Average the four corners & perturb it a bit.
61 * tmp is a random int +/- rough
63 int tmp2 = rough*2 + 1;
64 int tmp = randint1(tmp2) - (rough + 1);
66 int avg = ((x1 + x2 + x3 + x4) / 4) + tmp;
68 /* Division always rounds down, so we round up again */
69 if (((x1 + x2 + x3 + x4) % 4) > 1)
74 if (avg > depth_max) avg = depth_max;
76 /* Set the new value. */
77 cave[ymid][xmid].feat = avg;
81 static void perturb_point_end(int x1, int x2, int x3,
82 int xmid, int ymid, int rough, int depth_max)
85 * Average the three corners & perturb it a bit.
86 * tmp is a random int +/- rough
88 int tmp2 = rough * 2 + 1;
89 int tmp = randint0(tmp2) - rough;
91 int avg = ((x1 + x2 + x3) / 3) + tmp;
93 /* Division always rounds down, so we round up again */
94 if ((x1 + x2 + x3) % 3) avg++;
98 if (avg > depth_max) avg = depth_max;
100 /* Set the new value. */
101 cave[ymid][xmid].feat = avg;
106 * A generic function to generate the plasma fractal.
107 * Note that it uses ``cave_feat'' as temporary storage.
108 * The values in ``cave_feat'' after this function
109 * are NOT actual features; They are raw heights which
110 * need to be converted to features.
112 static void plasma_recursive(int x1, int y1, int x2, int y2,
113 int depth_max, int rough)
116 int xmid = (x2 - x1) / 2 + x1;
117 int ymid = (y2 - y1) / 2 + y1;
120 if (x1 + 1 == x2) return;
122 perturb_point_mid(cave[y1][x1].feat, cave[y2][x1].feat, cave[y1][x2].feat,
123 cave[y2][x2].feat, xmid, ymid, rough, depth_max);
125 perturb_point_end(cave[y1][x1].feat, cave[y1][x2].feat, cave[ymid][xmid].feat,
126 xmid, y1, rough, depth_max);
128 perturb_point_end(cave[y1][x2].feat, cave[y2][x2].feat, cave[ymid][xmid].feat,
129 x2, ymid, rough, depth_max);
131 perturb_point_end(cave[y2][x2].feat, cave[y2][x1].feat, cave[ymid][xmid].feat,
132 xmid, y2, rough, depth_max);
134 perturb_point_end(cave[y2][x1].feat, cave[y1][x1].feat, cave[ymid][xmid].feat,
135 x1, ymid, rough, depth_max);
138 /* Recurse the four quadrants */
139 plasma_recursive(x1, y1, xmid, ymid, depth_max, rough);
140 plasma_recursive(xmid, y1, x2, ymid, depth_max, rough);
141 plasma_recursive(x1, ymid, xmid, y2, depth_max, rough);
142 plasma_recursive(xmid, ymid, x2, y2, depth_max, rough);
147 * The default table in terrain level generation.
149 static int terrain_table[MAX_WILDERNESS][18] =
203 /* TERRAIN_DEEP_WATER */
229 /* TERRAIN_SHALLOW_WATER */
385 /* TERRAIN_SHALLOW_LAVA */
411 /* TERRAIN_DEEP_LAVA */
437 /* TERRAIN_MOUNTAIN */
467 static void generate_wilderness_area(int terrain, u32b seed, bool border, bool corner)
470 int table_size = sizeof(terrain_table[0]) / sizeof(int);
471 int roughness = 1; /* The roughness of the level. */
476 /* The outer wall is easy */
477 if (terrain == TERRAIN_EDGE)
479 /* Create level background */
480 for (y1 = 0; y1 < MAX_HGT; y1++)
482 for (x1 = 0; x1 < MAX_WID; x1++)
484 cave[y1][x1].feat = FEAT_PERM;
488 /* We are done already */
493 /* Hack -- Use the "simple" RNG */
496 /* Hack -- Induce consistant town layout */
501 /* Create level background */
502 for (y1 = 0; y1 < MAX_HGT; y1++)
504 for (x1 = 0; x1 < MAX_WID; x1++)
506 cave[y1][x1].feat = table_size / 2;
512 * Initialize the four corners
513 * ToDo: calculate the medium height of the adjacent
514 * terrains for every corner.
516 cave[1][1].feat = randint0(table_size);
517 cave[MAX_HGT-2][1].feat = randint0(table_size);
518 cave[1][MAX_WID-2].feat = randint0(table_size);
519 cave[MAX_HGT-2][MAX_WID-2].feat = randint0(table_size);
523 /* x1, y1, x2, y2, num_depths, roughness */
524 plasma_recursive(1, 1, MAX_WID-2, MAX_HGT-2, table_size-1, roughness);
527 /* Use the complex RNG */
530 for (y1 = 1; y1 < MAX_HGT-1; y1++)
532 for (x1 = 1; x1 < MAX_WID-1; x1++)
534 cave[y1][x1].feat = terrain_table[terrain][cave[y1][x1].feat];
542 * Load a town or generate a terrain level using "plasma" fractals.
544 * x and y are the coordinates of the area in the wilderness.
545 * Border and corner are optimization flags to speed up the
546 * generation of the fractal terrain.
547 * If border is set then only the border of the terrain should
548 * be generated (for initializing the border structure).
549 * If corner is set then only the corners of the area are needed.
551 static void generate_area(int y, int x, bool border, bool corner)
555 /* Number of the town (if any) */
556 p_ptr->town_num = wilderness[y][x].town;
558 /* Set the base level */
559 base_level = wilderness[y][x].level;
561 /* Set the dungeon level */
564 /* Set the monster generation level */
565 monster_level = base_level;
567 /* Set the object generation level */
568 object_level = base_level;
571 /* Create the town */
574 /* Reset the buildings */
577 /* Initialize the town */
579 init_flags = INIT_CREATE_DUNGEON | INIT_ONLY_FEATURES;
581 init_flags = INIT_CREATE_DUNGEON;
583 process_dungeon_file("t_info.txt", 0, 0, MAX_HGT, MAX_WID);
585 if (!corner && !border) p_ptr->visit |= (1L << (p_ptr->town_num - 1));
589 int terrain = wilderness[y][x].terrain;
590 u32b seed = wilderness[y][x].seed;
592 generate_wilderness_area(terrain, seed, border, corner);
595 if (!corner && !wilderness[y][x].town)
598 * Place roads in the wilderness
599 * ToDo: make the road a bit more interresting
601 if (wilderness[y][x].road)
603 cave[MAX_HGT/2][MAX_WID/2].feat = FEAT_FLOOR;
605 if (wilderness[y-1][x].road)
608 for (y1 = 1; y1 < MAX_HGT/2; y1++)
611 cave[y1][x1].feat = FEAT_FLOOR;
615 if (wilderness[y+1][x].road)
618 for (y1 = MAX_HGT/2; y1 < MAX_HGT - 1; y1++)
621 cave[y1][x1].feat = FEAT_FLOOR;
625 if (wilderness[y][x+1].road)
628 for (x1 = MAX_WID/2; x1 < MAX_WID - 1; x1++)
631 cave[y1][x1].feat = FEAT_FLOOR;
635 if (wilderness[y][x-1].road)
638 for (x1 = 1; x1 < MAX_WID/2; x1++)
641 cave[y1][x1].feat = FEAT_FLOOR;
647 if (wilderness[y][x].entrance && !wilderness[y][x].town && (p_ptr->total_winner || !(d_info[wilderness[y][x].entrance].flags1 & DF1_WINNER)))
651 /* Hack -- Use the "simple" RNG */
654 /* Hack -- Induce consistant town layout */
655 Rand_value = wilderness[y][x].seed;
657 dy = rand_range(6, cur_hgt - 6);
658 dx = rand_range(6, cur_wid - 6);
660 cave[dy][dx].feat = FEAT_ENTRANCE;
661 cave[dy][dx].special = wilderness[y][x].entrance;
663 /* Use the complex RNG */
670 * Border of the wilderness area
672 static border_type border;
676 * Build the wilderness area outside of the town.
678 void wilderness_gen(void)
688 /* Assume illegal panel */
689 panel_row_min = cur_hgt;
690 panel_col_min = cur_wid;
692 /* Init the wilderness */
694 process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
696 x = p_ptr->wilderness_x;
697 y = p_ptr->wilderness_y;
699 /* Prepare allocation table */
700 get_mon_num_prep(get_monster_hook(), NULL);
703 generate_area(y - 1, x, TRUE, FALSE);
705 for (i = 1; i < MAX_WID - 1; i++)
707 border.north[i] = cave[MAX_HGT - 2][i].feat;
711 generate_area(y + 1, x, TRUE, FALSE);
713 for (i = 1; i < MAX_WID - 1; i++)
715 border.south[i] = cave[1][i].feat;
719 generate_area(y, x - 1, TRUE, FALSE);
721 for (i = 1; i < MAX_HGT - 1; i++)
723 border.west[i] = cave[i][MAX_WID - 2].feat;
727 generate_area(y, x + 1, TRUE, FALSE);
729 for (i = 1; i < MAX_HGT - 1; i++)
731 border.east[i] = cave[i][1].feat;
734 /* North west corner */
735 generate_area(y - 1, x - 1, FALSE, TRUE);
736 border.north_west = cave[MAX_HGT - 2][MAX_WID - 2].feat;
738 /* North east corner */
739 generate_area(y - 1, x + 1, FALSE, TRUE);
740 border.north_east = cave[MAX_HGT - 2][1].feat;
742 /* South west corner */
743 generate_area(y + 1, x - 1, FALSE, TRUE);
744 border.south_west = cave[1][MAX_WID - 2].feat;
746 /* South east corner */
747 generate_area(y + 1, x + 1, FALSE, TRUE);
748 border.south_east = cave[1][1].feat;
751 /* Create terrain of the current area */
752 generate_area(y, x, FALSE, FALSE);
755 /* Special boundary walls -- North */
756 for (i = 0; i < MAX_WID; i++)
758 cave[0][i].feat = FEAT_PERM;
759 cave[0][i].mimic = border.north[i];
762 /* Special boundary walls -- South */
763 for (i = 0; i < MAX_WID; i++)
765 cave[MAX_HGT - 1][i].feat = FEAT_PERM;
766 cave[MAX_HGT - 1][i].mimic = border.south[i];
769 /* Special boundary walls -- West */
770 for (i = 0; i < MAX_HGT; i++)
772 cave[i][0].feat = FEAT_PERM;
773 cave[i][0].mimic = border.west[i];
776 /* Special boundary walls -- East */
777 for (i = 0; i < MAX_HGT; i++)
779 cave[i][MAX_WID - 1].feat = FEAT_PERM;
780 cave[i][MAX_WID - 1].mimic = border.east[i];
783 /* North west corner */
784 cave[0][0].mimic = border.north_west;
786 /* North east corner */
787 cave[0][MAX_WID - 1].mimic = border.north_east;
789 /* South west corner */
790 cave[MAX_HGT - 1][0].mimic = border.south_west;
792 /* South east corner */
793 cave[MAX_HGT - 1][MAX_WID - 1].mimic = border.south_east;
795 /* Light up or darken the area */
796 for (y = 0; y < cur_hgt; y++)
798 for (x = 0; x < cur_wid; x++)
800 /* Get the cave grid */
806 c_ptr->info |= (CAVE_GLOW);
808 /* Hack -- Memorize lit grids if allowed */
809 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
813 /* Feature code (applying "mimic" field) */
814 f_ptr = &f_info[get_feat_mimic(c_ptr)];
816 if (!is_mirror_grid(c_ptr) && !have_flag(f_ptr->flags, FF_QUEST_ENTER) &&
817 !have_flag(f_ptr->flags, FF_ENTRANCE))
820 c_ptr->info &= ~(CAVE_GLOW);
822 /* Darken "boring" features */
823 if (!have_flag(f_ptr->flags, FF_REMEMBER))
825 /* Forget the grid */
826 c_ptr->info &= ~(CAVE_MARK);
829 else if (have_flag(f_ptr->flags, FF_ENTRANCE))
832 c_ptr->info |= (CAVE_GLOW);
834 /* Hack -- Memorize lit grids if allowed */
835 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
841 if (p_ptr->teleport_town)
843 for (y = 0; y < cur_hgt; y++)
845 for (x = 0; x < cur_wid; x++)
847 /* Get the cave grid */
850 /* Seeing true feature code (ignore mimic) */
851 f_ptr = &f_info[c_ptr->feat];
853 if (have_flag(f_ptr->flags, FF_BLDG))
855 if ((f_ptr->power == 4) || ((p_ptr->town_num == 1) && (f_ptr->power == 0)))
857 if (c_ptr->m_idx) delete_monster_idx(c_ptr->m_idx);
864 p_ptr->teleport_town = FALSE;
867 else if (p_ptr->leaving_dungeon)
869 for (y = 0; y < cur_hgt; y++)
871 for (x = 0; x < cur_wid; x++)
873 /* Get the cave grid */
876 if (have_flag(f_flags_grid(c_ptr), FF_ENTRANCE))
878 if (c_ptr->m_idx) delete_monster_idx(c_ptr->m_idx);
884 p_ptr->teleport_town = FALSE;
887 player_place(p_ptr->oldpy, p_ptr->oldpx);
888 /* p_ptr->leaving_dungeon = FALSE;*/
890 lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
892 /* Make some residents */
893 for (i = 0; i < lim; i++)
897 if (!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))))
898 mode |= PM_ALLOW_SLEEP;
900 /* Make a resident */
901 (void)alloc_monster(generate_encounter ? 0 : 3, mode);
904 if(generate_encounter) ambush_flag = TRUE;
905 generate_encounter = FALSE;
907 /* Fill the arrays of floors and walls in the good proportions */
908 set_floor_and_wall(0);
910 /* Set rewarded quests to finished */
911 for (i = 0; i < max_quests; i++)
913 if (quest[i].status == QUEST_STATUS_REWARDED)
914 quest[i].status = QUEST_STATUS_FINISHED;
920 * Build the wilderness area.
923 void wilderness_gen_small()
927 /* To prevent stupid things */
928 for (i = 0; i < MAX_WID; i++)
929 for (j = 0; j < MAX_HGT; j++)
931 cave[j][i].feat = FEAT_PERM;
934 /* Init the wilderness */
935 process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
938 for (i = 0; i < max_wild_x; i++)
939 for (j = 0; j < max_wild_y; j++)
941 if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
943 cave[j][i].feat = FEAT_TOWN;
944 cave[j][i].special = wilderness[j][i].town;
946 else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
947 else if (wilderness[j][i].entrance && (p_ptr->total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
949 cave[j][i].feat = FEAT_ENTRANCE;
950 cave[j][i].special = (byte)wilderness[j][i].entrance;
952 else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
954 cave[j][i].info |= (CAVE_GLOW | CAVE_MARK);
957 cur_hgt = (s16b) max_wild_y;
958 cur_wid = (s16b) max_wild_x;
960 if (cur_hgt > MAX_HGT) cur_hgt = MAX_HGT;
961 if (cur_wid > MAX_WID) cur_wid = MAX_WID;
963 /* Assume illegal panel */
964 panel_row_min = cur_hgt;
965 panel_col_min = cur_wid;
967 /* Place the player */
968 px = p_ptr->wilderness_x;
969 py = p_ptr->wilderness_y;
975 typedef struct wilderness_grid wilderness_grid;
977 struct wilderness_grid
979 int terrain; /* Terrain type */
980 int town; /* Town number */
981 s16b level; /* Level of the wilderness */
982 byte road; /* Road */
983 char name[32]; /* Name of the town/wilderness */
987 static wilderness_grid w_letter[255];
991 * Parse a sub-file of the "extra info"
993 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
1003 if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
1007 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
1020 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
1022 int index = zz[0][0];
1025 w_letter[index].terrain = atoi(zz[1]);
1027 w_letter[index].terrain = 0;
1030 w_letter[index].level = atoi(zz[2]);
1032 w_letter[index].level = 0;
1035 w_letter[index].town = atoi(zz[3]);
1037 w_letter[index].town = 0;
1040 w_letter[index].road = atoi(zz[4]);
1042 w_letter[index].road = 0;
1045 strcpy(w_letter[index].name, zz[5]);
1047 w_letter[index].name[0] = 0;
1052 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1058 /* Process "W:D:<layout> */
1059 /* Layout of the wilderness */
1062 /* Acquire the text */
1065 /* Length of the text */
1066 int len = strlen(s);
1068 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
1072 wilderness[*y][*x].terrain = w_letter[idx].terrain;
1074 wilderness[*y][*x].level = w_letter[idx].level;
1076 wilderness[*y][*x].town = w_letter[idx].town;
1078 wilderness[*y][*x].road = w_letter[idx].road;
1080 strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
1088 /* Process "W:P:<x>:<y> - starting position in the wilderness */
1091 if ((p_ptr->wilderness_x == 0) &&
1092 (p_ptr->wilderness_y == 0))
1094 if (tokenize(buf+4, 2, zz, 0) == 2)
1096 p_ptr->wilderness_y = atoi(zz[0]);
1097 p_ptr->wilderness_x = atoi(zz[1]);
1099 if ((p_ptr->wilderness_x < 1) ||
1100 (p_ptr->wilderness_x > max_wild_x) ||
1101 (p_ptr->wilderness_y < 1) ||
1102 (p_ptr->wilderness_y > max_wild_y))
1104 return (PARSE_ERROR_OUT_OF_BOUNDS);
1109 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1118 return (PARSE_ERROR_UNDEFINED_DIRECTIVE);
1121 for (i = 1; i < max_d_idx; i++)
1123 if (!d_info[i].maxdepth) continue;
1124 wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
1125 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
1126 wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
1135 * Generate the random seeds for the wilderness
1137 void seed_wilderness(void)
1141 /* Init wilderness seeds */
1142 for (x = 0; x < max_wild_x; x++)
1144 for (y = 0; y < max_wild_y; y++)
1146 wilderness[y][x].seed = randint0(0x10000000);
1147 wilderness[y][x].entrance = 0;
1154 * Pointer to wilderness_type
1156 typedef wilderness_type *wilderness_type_ptr;
1159 * Initialize wilderness array
1161 errr init_wilderness(void)
1165 /* Allocate the wilderness (two-dimension array) */
1166 C_MAKE(wilderness, max_wild_y, wilderness_type_ptr);
1167 C_MAKE(wilderness[0], max_wild_x * max_wild_y, wilderness_type);
1169 /* Init the other pointers */
1170 for (i = 1; i < max_wild_y; i++)
1171 wilderness[i] = wilderness[0] + i * max_wild_x;
1173 generate_encounter = FALSE;
1179 bool change_wild_mode(void)
1182 bool have_pet = FALSE;
1184 /* It is in the middle of changing map */
1185 if (p_ptr->leaving) return FALSE;
1188 if (lite_town || vanilla_town)
1191 msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
1193 msg_print("No global map.");
1198 if (p_ptr->wild_mode)
1200 /* Save the location in the global map */
1201 p_ptr->wilderness_x = px;
1202 p_ptr->wilderness_y = py;
1204 /* Give first move to the player */
1205 p_ptr->energy_need = 0;
1207 /* Go back to the ordinary map */
1208 p_ptr->wild_mode = FALSE;
1211 p_ptr->leaving = TRUE;
1217 for (i = 1; i < m_max; i++)
1219 monster_type *m_ptr = &m_list[i];
1221 if (!m_ptr->r_idx) continue;
1222 if (is_pet(m_ptr) && i != p_ptr->riding) have_pet = TRUE;
1223 if (m_ptr->csleep) continue;
1224 if (m_ptr->cdis > MAX_SIGHT) continue;
1225 if (!is_hostile(m_ptr)) continue;
1227 msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤¤Ï¹°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
1229 msg_print("You cannot enter global map, since there is some monsters nearby!");
1238 cptr msg = "¥Ú¥Ã¥È¤òÃÖ¤¤¤Æ¹°è¥Þ¥Ã¥×¤ËÆþ¤ê¤Þ¤¹¤«¡©";
1240 cptr msg = "Do you leave your pets behind? ";
1243 if (!get_check_strict(msg, CHECK_OKAY_CANCEL))
1253 /* Remember the position */
1257 /* Cancel any special action */
1258 set_action(ACTION_NONE);
1260 /* Go into the global map */
1261 p_ptr->wild_mode = TRUE;
1264 p_ptr->leaving = TRUE;