3 /* Purpose: Wilderness generation */
6 * Copyright (c) 1989, 1999 James E. Wilson, Robert A. Koeneke,
9 * This software may be copied and distributed for educational, research, and
10 * not for profit purposes provided that this copyright and statement are
11 * included in all such copies.
18 * Helper for plasma generation.
20 static void perturb_point_mid(int x1, int x2, int x3, int x4,
21 int xmid, int ymid, int rough, int depth_max)
24 * Average the four corners & perturb it a bit.
25 * tmp is a random int +/- rough
27 int tmp2 = rough*2 + 1;
28 int tmp = randint(tmp2) - (rough + 1);
30 int avg = ((x1 + x2 + x3 + x4) / 4) + tmp;
32 /* Division always rounds down, so we round up again */
33 if (((x1 + x2 + x3 + x4) % 4) > 1)
38 if (avg > depth_max) avg = depth_max;
40 /* Set the new value. */
41 cave[ymid][xmid].feat = avg;
45 static void perturb_point_end(int x1, int x2, int x3,
46 int xmid, int ymid, int rough, int depth_max)
49 * Average the three corners & perturb it a bit.
50 * tmp is a random int +/- rough
52 int tmp2 = rough * 2 + 1;
53 int tmp = rand_int(tmp2) - rough;
55 int avg = ((x1 + x2 + x3) / 3) + tmp;
57 /* Division always rounds down, so we round up again */
58 if ((x1 + x2 + x3) % 3) avg++;
62 if (avg > depth_max) avg = depth_max;
64 /* Set the new value. */
65 cave[ymid][xmid].feat = avg;
70 * A generic function to generate the plasma fractal.
71 * Note that it uses ``cave_feat'' as temporary storage.
72 * The values in ``cave_feat'' after this function
73 * are NOT actual features; They are raw heights which
74 * need to be converted to features.
76 static void plasma_recursive(int x1, int y1, int x2, int y2,
77 int depth_max, int rough)
80 int xmid = (x2 - x1) / 2 + x1;
81 int ymid = (y2 - y1) / 2 + y1;
84 if (x1 + 1 == x2) return;
86 perturb_point_mid(cave[y1][x1].feat, cave[y2][x1].feat, cave[y1][x2].feat,
87 cave[y2][x2].feat, xmid, ymid, rough, depth_max);
89 perturb_point_end(cave[y1][x1].feat, cave[y1][x2].feat, cave[ymid][xmid].feat,
90 xmid, y1, rough, depth_max);
92 perturb_point_end(cave[y1][x2].feat, cave[y2][x2].feat, cave[ymid][xmid].feat,
93 x2, ymid, rough, depth_max);
95 perturb_point_end(cave[y2][x2].feat, cave[y2][x1].feat, cave[ymid][xmid].feat,
96 xmid, y2, rough, depth_max);
98 perturb_point_end(cave[y2][x1].feat, cave[y1][x1].feat, cave[ymid][xmid].feat,
99 x1, ymid, rough, depth_max);
102 /* Recurse the four quadrants */
103 plasma_recursive(x1, y1, xmid, ymid, depth_max, rough);
104 plasma_recursive(xmid, y1, x2, ymid, depth_max, rough);
105 plasma_recursive(x1, ymid, xmid, y2, depth_max, rough);
106 plasma_recursive(xmid, ymid, x2, y2, depth_max, rough);
111 * The default table in terrain level generation.
113 static int terrain_table[MAX_WILDERNESS][18] =
167 /* TERRAIN_DEEP_WATER */
193 /* TERRAIN_SHALLOW_WATER */
349 /* TERRAIN_SHALLOW_LAVA */
375 /* TERRAIN_DEEP_LAVA */
401 /* TERRAIN_MOUNTAIN */
431 void generate_wilderness_area(int terrain, u32b seed, bool border, bool corner)
434 int table_size = sizeof(terrain_table[0]) / sizeof(int);
435 int roughness = 1; /* The roughness of the level. */
437 /* The outer wall is easy */
438 if (terrain == TERRAIN_EDGE)
440 /* Create level background */
441 for (y1 = 0; y1 < MAX_HGT; y1++)
443 for (x1 = 0; x1 < MAX_WID; x1++)
445 cave[y1][x1].feat = FEAT_PERM_SOLID;
449 /* We are done already */
454 /* Hack -- Use the "simple" RNG */
457 /* Hack -- Induce consistant town layout */
462 /* Create level background */
463 for (y1 = 0; y1 < MAX_HGT; y1++)
465 for (x1 = 0; x1 < MAX_WID; x1++)
467 cave[y1][x1].feat = table_size / 2;
473 * Initialize the four corners
474 * ToDo: calculate the medium height of the adjacent
475 * terrains for every corner.
477 cave[1][1].feat = (byte)rand_int(table_size);
478 cave[MAX_HGT-2][1].feat = (byte)rand_int(table_size);
479 cave[1][MAX_WID-2].feat = (byte)rand_int(table_size);
480 cave[MAX_HGT-2][MAX_WID-2].feat = (byte)rand_int(table_size);
484 /* x1, y1, x2, y2, num_depths, roughness */
485 plasma_recursive(1, 1, MAX_WID-2, MAX_HGT-2, table_size-1, roughness);
488 /* Use the complex RNG */
491 for (y1 = 1; y1 < MAX_HGT-1; y1++)
493 for (x1 = 1; x1 < MAX_WID-1; x1++)
495 cave[y1][x1].feat = terrain_table[terrain][cave[y1][x1].feat];
503 * Load a town or generate a terrain level using "plasma" fractals.
505 * x and y are the coordinates of the area in the wilderness.
506 * Border and corner are optimization flags to speed up the
507 * generation of the fractal terrain.
508 * If border is set then only the border of the terrain should
509 * be generated (for initializing the border structure).
510 * If corner is set then only the corners of the area are needed.
512 static void generate_area(int y, int x, bool border, bool corner)
516 /* Number of the town (if any) */
517 p_ptr->town_num = wilderness[y][x].town;
519 /* Set the base level */
520 base_level = wilderness[y][x].level;
522 /* Set the dungeon level */
525 /* Set the monster generation level */
526 monster_level = base_level;
528 /* Set the object generation level */
529 object_level = base_level;
532 /* Create the town */
535 /* Reset the buildings */
538 /* Initialize the town */
540 init_flags = INIT_CREATE_DUNGEON | INIT_ONLY_FEATURES;
542 init_flags = INIT_CREATE_DUNGEON;
544 process_dungeon_file("t_info_j.txt", 0, 0, MAX_HGT, MAX_WID);
546 if (!corner && !border) p_ptr->visit |= (1L << (p_ptr->town_num - 1));
550 int terrain = wilderness[y][x].terrain;
551 u32b seed = wilderness[y][x].seed;
553 generate_wilderness_area(terrain, seed, border, corner);
556 if (!corner && !wilderness[y][x].town)
559 * Place roads in the wilderness
560 * ToDo: make the road a bit more interresting
562 if (wilderness[y][x].road)
564 cave[MAX_HGT/2][MAX_WID/2].feat = FEAT_FLOOR;
566 if (wilderness[y-1][x].road)
569 for (y1 = 1; y1 < MAX_HGT/2; y1++)
572 cave[y1][x1].feat = FEAT_FLOOR;
576 if (wilderness[y+1][x].road)
579 for (y1 = MAX_HGT/2; y1 < MAX_HGT - 1; y1++)
582 cave[y1][x1].feat = FEAT_FLOOR;
586 if (wilderness[y][x+1].road)
589 for (x1 = MAX_WID/2; x1 < MAX_WID - 1; x1++)
592 cave[y1][x1].feat = FEAT_FLOOR;
596 if (wilderness[y][x-1].road)
599 for (x1 = 1; x1 < MAX_WID/2; x1++)
602 cave[y1][x1].feat = FEAT_FLOOR;
608 if (wilderness[y][x].entrance && !wilderness[y][x].town && (total_winner || !(d_info[wilderness[y][x].entrance].flags1 & DF1_WINNER)))
612 /* Hack -- Use the "simple" RNG */
615 /* Hack -- Induce consistant town layout */
616 Rand_value = wilderness[y][x].seed;
618 dy = rand_range(6, cur_hgt - 6);
619 dx = rand_range(6, cur_wid - 6);
621 cave[dy][dx].feat = FEAT_ENTRANCE;
622 cave[dy][dx].special = (byte)wilderness[y][x].entrance;
624 /* Use the complex RNG */
631 * Border of the wilderness area
633 static border_type border;
637 * Build the wilderness area outside of the town.
639 void wilderness_gen(void)
649 /* Determine number of panels */
650 max_panel_rows = (cur_hgt / SCREEN_HGT) * 2 - 2;
651 max_panel_cols = (cur_wid / SCREEN_WID) * 2 - 2;
653 /* Assume illegal panel */
654 panel_row = max_panel_rows;
655 panel_col = max_panel_cols;
657 /* Init the wilderness */
659 process_dungeon_file("w_info_j.txt", 0, 0, max_wild_y, max_wild_x);
661 x = p_ptr->wilderness_x;
662 y = p_ptr->wilderness_y;
664 /* Prepare allocation table */
665 get_mon_num_prep(get_monster_hook(), NULL);
668 generate_area(y - 1, x, TRUE, FALSE);
670 for (i = 1; i < MAX_WID - 1; i++)
672 border.north[i] = cave[MAX_HGT - 2][i].feat;
676 generate_area(y + 1, x, TRUE, FALSE);
678 for (i = 1; i < MAX_WID - 1; i++)
680 border.south[i] = cave[1][i].feat;
684 generate_area(y, x - 1, TRUE, FALSE);
686 for (i = 1; i < MAX_HGT - 1; i++)
688 border.west[i] = cave[i][MAX_WID - 2].feat;
692 generate_area(y, x + 1, TRUE, FALSE);
694 for (i = 1; i < MAX_HGT - 1; i++)
696 border.east[i] = cave[i][1].feat;
699 /* North west corner */
700 generate_area(y - 1, x - 1, FALSE, TRUE);
701 border.north_west = cave[MAX_HGT - 2][MAX_WID - 2].feat;
703 /* North east corner */
704 generate_area(y - 1, x + 1, FALSE, TRUE);
705 border.north_east = cave[MAX_HGT - 2][1].feat;
707 /* South west corner */
708 generate_area(y + 1, x - 1, FALSE, TRUE);
709 border.south_west = cave[1][MAX_WID - 2].feat;
711 /* South east corner */
712 generate_area(y + 1, x + 1, FALSE, TRUE);
713 border.south_east = cave[1][1].feat;
716 /* Create terrain of the current area */
717 generate_area(y, x, FALSE, FALSE);
720 /* Special boundary walls -- North */
721 for (i = 0; i < MAX_WID; i++)
723 cave[0][i].feat = FEAT_PERM_SOLID;
724 cave[0][i].mimic = border.north[i];
727 /* Special boundary walls -- South */
728 for (i = 0; i < MAX_WID; i++)
730 cave[MAX_HGT - 1][i].feat = FEAT_PERM_SOLID;
731 cave[MAX_HGT - 1][i].mimic = border.south[i];
734 /* Special boundary walls -- West */
735 for (i = 0; i < MAX_HGT; i++)
737 cave[i][0].feat = FEAT_PERM_SOLID;
738 cave[i][0].mimic = border.west[i];
741 /* Special boundary walls -- East */
742 for (i = 0; i < MAX_HGT; i++)
744 cave[i][MAX_WID - 1].feat = FEAT_PERM_SOLID;
745 cave[i][MAX_WID - 1].mimic = border.east[i];
748 /* North west corner */
749 cave[0][0].mimic = border.north_west;
751 /* North east corner */
752 cave[0][MAX_WID - 1].mimic = border.north_east;
754 /* South west corner */
755 cave[MAX_HGT - 1][0].mimic = border.south_west;
757 /* South east corner */
758 cave[MAX_HGT - 1][MAX_WID - 1].mimic = border.south_east;
762 if ((turn % (20L * TOWN_DAWN)) < ((20L * TOWN_DAWN) / 2))
767 /* Light up or darken the area */
768 for (y = 0; y < cur_hgt; y++)
770 for (x = 0; x < cur_wid; x++)
772 /* Get the cave grid */
778 c_ptr->info |= (CAVE_GLOW);
780 /* Hack -- Memorize lit grids if allowed */
781 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
785 /* Darken "boring" features */
786 if ((c_ptr->feat <= FEAT_INVIS) ||
787 ((c_ptr->feat >= FEAT_DEEP_WATER) &&
788 (c_ptr->feat <= FEAT_TREES) &&
789 (c_ptr->feat != FEAT_MUSEUM)) ||
790 (x == 0) || (x == cur_wid-1) ||
791 (y == 0) || (y == cur_hgt-1))
793 /* Forget the grid */
794 c_ptr->info &= ~(CAVE_GLOW | CAVE_MARK);
796 else if (c_ptr->feat == FEAT_ENTRANCE)
799 c_ptr->info |= (CAVE_GLOW);
801 /* Hack -- Memorize lit grids if allowed */
802 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
808 if (p_ptr->teleport_town)
810 for (y = 0; y < cur_hgt; y++)
812 for (x = 0; x < cur_wid; x++)
814 if (((cave[y][x].feat - FEAT_BLDG_HEAD) == 4) || ((p_ptr->town_num == 1) && ((cave[y][x].feat - FEAT_BLDG_HEAD) == 0)))
816 if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
822 p_ptr->teleport_town = FALSE;
825 else if (p_ptr->leaving_dungeon)
827 for (y = 0; y < cur_hgt; y++)
829 for (x = 0; x < cur_wid; x++)
831 if (cave[y][x].feat == FEAT_ENTRANCE)
833 if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
839 p_ptr->teleport_town = FALSE;
842 player_place(p_ptr->oldpy, p_ptr->oldpx);
843 p_ptr->leftbldg = FALSE;
844 /* p_ptr->leaving_dungeon = FALSE;*/
846 lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
848 /* Make some residents */
849 for (i = 0; i < lim; i++)
851 /* Make a resident */
852 (void)alloc_monster(generate_encounter ? 0 : 3, (bool)!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))));
855 if(generate_encounter) ambush_flag = TRUE;
856 generate_encounter = FALSE;
858 for (i = 0; i < 100; i++)
860 floor_type[i] = FEAT_FLOOR;
861 fill_type[i] = FEAT_WALL_EXTRA;
864 /* Set rewarded quests to finished */
865 for (i = 0; i < max_quests; i++)
867 if (quest[i].status == QUEST_STATUS_REWARDED)
868 quest[i].status = QUEST_STATUS_FINISHED;
874 * Build the wilderness area.
877 void wilderness_gen_small()
881 /* To prevent stupid things */
882 for (i = 0; i < MAX_WID; i++)
883 for (j = 0; j < MAX_HGT; j++)
885 cave[j][i].feat = FEAT_PERM_SOLID;
888 /* Init the wilderness */
889 process_dungeon_file("w_info_j.txt", 0, 0, max_wild_y, max_wild_x);
892 for (i = 0; i < max_wild_x; i++)
893 for (j = 0; j < max_wild_y; j++)
895 if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
897 cave[j][i].feat = FEAT_TOWN;
898 cave[j][i].special = wilderness[j][i].town;
900 else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
901 else if (wilderness[j][i].entrance && (total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
903 cave[j][i].feat = FEAT_ENTRANCE;
904 cave[j][i].special = (byte)wilderness[j][i].entrance;
906 else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
908 cave[j][i].info |= (CAVE_GLOW | CAVE_MARK);
911 cur_hgt = (max_wild_y / SCREEN_HGT + 1) * SCREEN_HGT;
912 cur_wid = (max_wild_x / SCREEN_WID + 1) * SCREEN_WID;
914 if (cur_hgt > MAX_HGT) cur_hgt = MAX_HGT;
915 if (cur_wid > MAX_WID) cur_wid = MAX_WID;
917 /* Determine number of panels */
918 max_panel_rows = (cur_hgt / SCREEN_HGT) * 2 - 2;
919 max_panel_cols = (cur_wid / SCREEN_WID) * 2 - 2;
921 /* Assume illegal panel */
922 panel_row = max_panel_rows;
923 panel_col = max_panel_cols;
925 /* Place the player */
926 px = p_ptr->wilderness_x;
927 py = p_ptr->wilderness_y;
933 typedef struct wilderness_grid wilderness_grid;
935 struct wilderness_grid
937 int terrain; /* Terrain type */
938 int town; /* Town number */
939 s16b level; /* Level of the wilderness */
940 byte road; /* Road */
941 char name[32]; /* Name of the town/wilderness */
945 static wilderness_grid w_letter[255];
949 * Parse a sub-file of the "extra info"
951 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
958 if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
962 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
975 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
977 int index = zz[0][0];
980 w_letter[index].terrain = atoi(zz[1]);
982 w_letter[index].terrain = 0;
985 w_letter[index].level = atoi(zz[2]);
987 w_letter[index].level = 0;
990 w_letter[index].town = atoi(zz[3]);
992 w_letter[index].town = 0;
995 w_letter[index].road = atoi(zz[4]);
997 w_letter[index].road = 0;
1000 strcpy(w_letter[index].name, zz[5]);
1002 w_letter[index].name[0] = 0;
1007 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1013 /* Process "W:D:<layout> */
1014 /* Layout of the wilderness */
1017 /* Acquire the text */
1020 /* Length of the text */
1021 int len = strlen(s);
1023 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
1027 wilderness[*y][*x].terrain = w_letter[idx].terrain;
1029 wilderness[*y][*x].level = w_letter[idx].level;
1031 wilderness[*y][*x].town = w_letter[idx].town;
1033 wilderness[*y][*x].road = w_letter[idx].road;
1035 strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
1043 /* Process "W:P:<x>:<y> - starting position in the wilderness */
1046 if ((p_ptr->wilderness_x == 0) &&
1047 (p_ptr->wilderness_y == 0))
1049 if (tokenize(buf+4, 2, zz, 0) == 2)
1051 p_ptr->wilderness_y = atoi(zz[0]);
1052 p_ptr->wilderness_x = atoi(zz[1]);
1054 if ((p_ptr->wilderness_x < 1) ||
1055 (p_ptr->wilderness_x > max_wild_x) ||
1056 (p_ptr->wilderness_y < 1) ||
1057 (p_ptr->wilderness_y > max_wild_y))
1059 return (PARSE_ERROR_OUT_OF_BOUNDS);
1064 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1073 return (PARSE_ERROR_UNDEFINED_DIRECTIVE);
1076 for (i = 1; i < max_d_idx; i++)
1078 if (!d_info[i].maxdepth) continue;
1079 wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
1080 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
1081 wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
1090 * Generate the random seeds for the wilderness
1092 void seed_wilderness(void)
1096 /* Init wilderness seeds */
1097 for (x = 0; x < max_wild_x; x++)
1099 for (y = 0; y < max_wild_y; y++)
1101 wilderness[y][x].seed = rand_int(0x10000000);
1102 wilderness[y][x].entrance = 0;
1109 * Pointer to wilderness_type
1111 typedef wilderness_type *wilderness_type_ptr;
1114 * Initialize wilderness array
1116 errr init_wilderness(void)
1120 /* Allocate the wilderness (two-dimension array) */
1121 C_MAKE(wilderness, max_wild_y, wilderness_type_ptr);
1122 C_MAKE(wilderness[0], max_wild_x * max_wild_y, wilderness_type);
1124 /* Init the other pointers */
1125 for (i = 1; i < max_wild_y; i++)
1126 wilderness[i] = wilderness[0] + i * max_wild_x;
1128 generate_encounter = FALSE;
1134 bool change_wild_mode(void)
1137 bool have_pet = FALSE;
1139 if (lite_town || vanilla_town)
1142 msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
1144 msg_print("No global mal");
1148 if (!p_ptr->wild_mode)
1150 for (i = 1; i < m_max; i++)
1152 monster_type *m_ptr = &m_list[i];
1154 if (!m_ptr->r_idx) continue;
1155 if (is_pet(m_ptr) && i != p_ptr->riding) have_pet = TRUE;
1156 if (m_ptr->csleep) continue;
1157 if (m_ptr->cdis > MAX_SIGHT) continue;
1158 if (!is_hostile(m_ptr)) continue;
1160 msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤¤Ï¹°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
1162 msg_print("You cannot enter global map, since there is some monsters nearby!");
1171 if(!get_check_strict("¥Ú¥Ã¥È¤òÃÖ¤¤¤Æ¹°è¥Þ¥Ã¥×¤ËÆþ¤ê¤Þ¤¹¤«¡©", 1))
1173 if(!get_check_strict("Do you leave your pets behind? ", 1))
1184 set_action(ACTION_NONE);
1186 p_ptr->wild_mode = !p_ptr->wild_mode;
1189 p_ptr->leaving = TRUE;