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 = randint1(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 = randint0(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)randint0(table_size);
478 cave[MAX_HGT-2][1].feat = (byte)randint0(table_size);
479 cave[1][MAX_WID-2].feat = (byte)randint0(table_size);
480 cave[MAX_HGT-2][MAX_WID-2].feat = (byte)randint0(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 /* Assume illegal panel */
650 panel_row_min = cur_hgt;
651 panel_col_min = cur_wid;
653 /* Init the wilderness */
655 process_dungeon_file("w_info_j.txt", 0, 0, max_wild_y, max_wild_x);
657 x = p_ptr->wilderness_x;
658 y = p_ptr->wilderness_y;
660 /* Prepare allocation table */
661 get_mon_num_prep(get_monster_hook(), NULL);
664 generate_area(y - 1, x, TRUE, FALSE);
666 for (i = 1; i < MAX_WID - 1; i++)
668 border.north[i] = cave[MAX_HGT - 2][i].feat;
672 generate_area(y + 1, x, TRUE, FALSE);
674 for (i = 1; i < MAX_WID - 1; i++)
676 border.south[i] = cave[1][i].feat;
680 generate_area(y, x - 1, TRUE, FALSE);
682 for (i = 1; i < MAX_HGT - 1; i++)
684 border.west[i] = cave[i][MAX_WID - 2].feat;
688 generate_area(y, x + 1, TRUE, FALSE);
690 for (i = 1; i < MAX_HGT - 1; i++)
692 border.east[i] = cave[i][1].feat;
695 /* North west corner */
696 generate_area(y - 1, x - 1, FALSE, TRUE);
697 border.north_west = cave[MAX_HGT - 2][MAX_WID - 2].feat;
699 /* North east corner */
700 generate_area(y - 1, x + 1, FALSE, TRUE);
701 border.north_east = cave[MAX_HGT - 2][1].feat;
703 /* South west corner */
704 generate_area(y + 1, x - 1, FALSE, TRUE);
705 border.south_west = cave[1][MAX_WID - 2].feat;
707 /* South east corner */
708 generate_area(y + 1, x + 1, FALSE, TRUE);
709 border.south_east = cave[1][1].feat;
712 /* Create terrain of the current area */
713 generate_area(y, x, FALSE, FALSE);
716 /* Special boundary walls -- North */
717 for (i = 0; i < MAX_WID; i++)
719 cave[0][i].feat = FEAT_PERM_SOLID;
720 cave[0][i].mimic = border.north[i];
723 /* Special boundary walls -- South */
724 for (i = 0; i < MAX_WID; i++)
726 cave[MAX_HGT - 1][i].feat = FEAT_PERM_SOLID;
727 cave[MAX_HGT - 1][i].mimic = border.south[i];
730 /* Special boundary walls -- West */
731 for (i = 0; i < MAX_HGT; i++)
733 cave[i][0].feat = FEAT_PERM_SOLID;
734 cave[i][0].mimic = border.west[i];
737 /* Special boundary walls -- East */
738 for (i = 0; i < MAX_HGT; i++)
740 cave[i][MAX_WID - 1].feat = FEAT_PERM_SOLID;
741 cave[i][MAX_WID - 1].mimic = border.east[i];
744 /* North west corner */
745 cave[0][0].mimic = border.north_west;
747 /* North east corner */
748 cave[0][MAX_WID - 1].mimic = border.north_east;
750 /* South west corner */
751 cave[MAX_HGT - 1][0].mimic = border.south_west;
753 /* South east corner */
754 cave[MAX_HGT - 1][MAX_WID - 1].mimic = border.south_east;
758 if ((turn % (20L * TOWN_DAWN)) < ((20L * TOWN_DAWN) / 2))
763 /* Light up or darken the area */
764 for (y = 0; y < cur_hgt; y++)
766 for (x = 0; x < cur_wid; x++)
768 /* Get the cave grid */
774 c_ptr->info |= (CAVE_GLOW);
776 /* Hack -- Memorize lit grids if allowed */
777 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
781 /* Darken "boring" features */
782 if ((c_ptr->feat <= FEAT_INVIS) ||
783 ((c_ptr->feat >= FEAT_DEEP_WATER) &&
784 (c_ptr->feat <= FEAT_TREES) &&
785 (c_ptr->feat != FEAT_MUSEUM)) ||
786 (x == 0) || (x == cur_wid-1) ||
787 (y == 0) || (y == cur_hgt-1))
789 /* Forget the grid */
790 c_ptr->info &= ~(CAVE_GLOW | CAVE_MARK);
792 else if (c_ptr->feat == FEAT_ENTRANCE)
795 c_ptr->info |= (CAVE_GLOW);
797 /* Hack -- Memorize lit grids if allowed */
798 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
804 if (p_ptr->teleport_town)
806 for (y = 0; y < cur_hgt; y++)
808 for (x = 0; x < cur_wid; x++)
810 if (((cave[y][x].feat - FEAT_BLDG_HEAD) == 4) || ((p_ptr->town_num == 1) && ((cave[y][x].feat - FEAT_BLDG_HEAD) == 0)))
812 if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
818 p_ptr->teleport_town = FALSE;
821 else if (p_ptr->leaving_dungeon)
823 for (y = 0; y < cur_hgt; y++)
825 for (x = 0; x < cur_wid; x++)
827 if (cave[y][x].feat == FEAT_ENTRANCE)
829 if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
835 p_ptr->teleport_town = FALSE;
838 player_place(p_ptr->oldpy, p_ptr->oldpx);
839 p_ptr->leftbldg = FALSE;
840 /* p_ptr->leaving_dungeon = FALSE;*/
842 lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
844 /* Make some residents */
845 for (i = 0; i < lim; i++)
847 /* Make a resident */
848 (void)alloc_monster(generate_encounter ? 0 : 3, (bool)!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))));
851 if(generate_encounter) ambush_flag = TRUE;
852 generate_encounter = FALSE;
854 for (i = 0; i < 100; i++)
856 floor_type[i] = FEAT_FLOOR;
857 fill_type[i] = FEAT_WALL_EXTRA;
860 /* Set rewarded quests to finished */
861 for (i = 0; i < max_quests; i++)
863 if (quest[i].status == QUEST_STATUS_REWARDED)
864 quest[i].status = QUEST_STATUS_FINISHED;
870 * Build the wilderness area.
873 void wilderness_gen_small()
877 /* To prevent stupid things */
878 for (i = 0; i < MAX_WID; i++)
879 for (j = 0; j < MAX_HGT; j++)
881 cave[j][i].feat = FEAT_PERM_SOLID;
884 /* Init the wilderness */
885 process_dungeon_file("w_info_j.txt", 0, 0, max_wild_y, max_wild_x);
888 for (i = 0; i < max_wild_x; i++)
889 for (j = 0; j < max_wild_y; j++)
891 if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
893 cave[j][i].feat = FEAT_TOWN;
894 cave[j][i].special = wilderness[j][i].town;
896 else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
897 else if (wilderness[j][i].entrance && (total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
899 cave[j][i].feat = FEAT_ENTRANCE;
900 cave[j][i].special = (byte)wilderness[j][i].entrance;
902 else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
904 cave[j][i].info |= (CAVE_GLOW | CAVE_MARK);
907 cur_hgt = (max_wild_y / SCREEN_HGT + 1) * SCREEN_HGT;
908 cur_wid = (max_wild_x / SCREEN_WID + 1) * SCREEN_WID;
910 if (cur_hgt > MAX_HGT) cur_hgt = MAX_HGT;
911 if (cur_wid > MAX_WID) cur_wid = MAX_WID;
913 /* Assume illegal panel */
914 panel_row_min = cur_hgt;
915 panel_col_min = cur_wid;
917 /* Place the player */
918 px = p_ptr->wilderness_x;
919 py = p_ptr->wilderness_y;
925 typedef struct wilderness_grid wilderness_grid;
927 struct wilderness_grid
929 int terrain; /* Terrain type */
930 int town; /* Town number */
931 s16b level; /* Level of the wilderness */
932 byte road; /* Road */
933 char name[32]; /* Name of the town/wilderness */
937 static wilderness_grid w_letter[255];
941 * Parse a sub-file of the "extra info"
943 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
950 if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
954 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
967 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
969 int index = zz[0][0];
972 w_letter[index].terrain = atoi(zz[1]);
974 w_letter[index].terrain = 0;
977 w_letter[index].level = atoi(zz[2]);
979 w_letter[index].level = 0;
982 w_letter[index].town = atoi(zz[3]);
984 w_letter[index].town = 0;
987 w_letter[index].road = atoi(zz[4]);
989 w_letter[index].road = 0;
992 strcpy(w_letter[index].name, zz[5]);
994 w_letter[index].name[0] = 0;
999 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1005 /* Process "W:D:<layout> */
1006 /* Layout of the wilderness */
1009 /* Acquire the text */
1012 /* Length of the text */
1013 int len = strlen(s);
1015 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
1019 wilderness[*y][*x].terrain = w_letter[idx].terrain;
1021 wilderness[*y][*x].level = w_letter[idx].level;
1023 wilderness[*y][*x].town = w_letter[idx].town;
1025 wilderness[*y][*x].road = w_letter[idx].road;
1027 strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
1035 /* Process "W:P:<x>:<y> - starting position in the wilderness */
1038 if ((p_ptr->wilderness_x == 0) &&
1039 (p_ptr->wilderness_y == 0))
1041 if (tokenize(buf+4, 2, zz, 0) == 2)
1043 p_ptr->wilderness_y = atoi(zz[0]);
1044 p_ptr->wilderness_x = atoi(zz[1]);
1046 if ((p_ptr->wilderness_x < 1) ||
1047 (p_ptr->wilderness_x > max_wild_x) ||
1048 (p_ptr->wilderness_y < 1) ||
1049 (p_ptr->wilderness_y > max_wild_y))
1051 return (PARSE_ERROR_OUT_OF_BOUNDS);
1056 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1065 return (PARSE_ERROR_UNDEFINED_DIRECTIVE);
1068 for (i = 1; i < max_d_idx; i++)
1070 if (!d_info[i].maxdepth) continue;
1071 wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
1072 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
1073 wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
1082 * Generate the random seeds for the wilderness
1084 void seed_wilderness(void)
1088 /* Init wilderness seeds */
1089 for (x = 0; x < max_wild_x; x++)
1091 for (y = 0; y < max_wild_y; y++)
1093 wilderness[y][x].seed = randint0(0x10000000);
1094 wilderness[y][x].entrance = 0;
1101 * Pointer to wilderness_type
1103 typedef wilderness_type *wilderness_type_ptr;
1106 * Initialize wilderness array
1108 errr init_wilderness(void)
1112 /* Allocate the wilderness (two-dimension array) */
1113 C_MAKE(wilderness, max_wild_y, wilderness_type_ptr);
1114 C_MAKE(wilderness[0], max_wild_x * max_wild_y, wilderness_type);
1116 /* Init the other pointers */
1117 for (i = 1; i < max_wild_y; i++)
1118 wilderness[i] = wilderness[0] + i * max_wild_x;
1120 generate_encounter = FALSE;
1126 bool change_wild_mode(void)
1129 bool have_pet = FALSE;
1131 if (lite_town || vanilla_town)
1134 msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
1136 msg_print("No global mal");
1140 if (!p_ptr->wild_mode)
1142 for (i = 1; i < m_max; i++)
1144 monster_type *m_ptr = &m_list[i];
1146 if (!m_ptr->r_idx) continue;
1147 if (is_pet(m_ptr) && i != p_ptr->riding) have_pet = TRUE;
1148 if (m_ptr->csleep) continue;
1149 if (m_ptr->cdis > MAX_SIGHT) continue;
1150 if (!is_hostile(m_ptr)) continue;
1152 msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤¤Ï¹°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
1154 msg_print("You cannot enter global map, since there is some monsters nearby!");
1163 if(!get_check_strict("¥Ú¥Ã¥È¤òÃÖ¤¤¤Æ¹°è¥Þ¥Ã¥×¤ËÆþ¤ê¤Þ¤¹¤«¡©", 1))
1165 if(!get_check_strict("Do you leave your pets behind? ", 1))
1176 set_action(ACTION_NONE);
1178 p_ptr->wild_mode = !p_ptr->wild_mode;
1181 p_ptr->leaving = TRUE;