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 static 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.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 && (p_ptr->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)
648 /* Assume illegal panel */
649 panel_row_min = cur_hgt;
650 panel_col_min = cur_wid;
652 /* Init the wilderness */
654 process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
656 x = p_ptr->wilderness_x;
657 y = p_ptr->wilderness_y;
659 /* Prepare allocation table */
660 get_mon_num_prep(get_monster_hook(), NULL);
663 generate_area(y - 1, x, TRUE, FALSE);
665 for (i = 1; i < MAX_WID - 1; i++)
667 border.north[i] = cave[MAX_HGT - 2][i].feat;
671 generate_area(y + 1, x, TRUE, FALSE);
673 for (i = 1; i < MAX_WID - 1; i++)
675 border.south[i] = cave[1][i].feat;
679 generate_area(y, x - 1, TRUE, FALSE);
681 for (i = 1; i < MAX_HGT - 1; i++)
683 border.west[i] = cave[i][MAX_WID - 2].feat;
687 generate_area(y, x + 1, TRUE, FALSE);
689 for (i = 1; i < MAX_HGT - 1; i++)
691 border.east[i] = cave[i][1].feat;
694 /* North west corner */
695 generate_area(y - 1, x - 1, FALSE, TRUE);
696 border.north_west = cave[MAX_HGT - 2][MAX_WID - 2].feat;
698 /* North east corner */
699 generate_area(y - 1, x + 1, FALSE, TRUE);
700 border.north_east = cave[MAX_HGT - 2][1].feat;
702 /* South west corner */
703 generate_area(y + 1, x - 1, FALSE, TRUE);
704 border.south_west = cave[1][MAX_WID - 2].feat;
706 /* South east corner */
707 generate_area(y + 1, x + 1, FALSE, TRUE);
708 border.south_east = cave[1][1].feat;
711 /* Create terrain of the current area */
712 generate_area(y, x, FALSE, FALSE);
715 /* Special boundary walls -- North */
716 for (i = 0; i < MAX_WID; i++)
718 cave[0][i].feat = FEAT_PERM_SOLID;
719 cave[0][i].mimic = border.north[i];
722 /* Special boundary walls -- South */
723 for (i = 0; i < MAX_WID; i++)
725 cave[MAX_HGT - 1][i].feat = FEAT_PERM_SOLID;
726 cave[MAX_HGT - 1][i].mimic = border.south[i];
729 /* Special boundary walls -- West */
730 for (i = 0; i < MAX_HGT; i++)
732 cave[i][0].feat = FEAT_PERM_SOLID;
733 cave[i][0].mimic = border.west[i];
736 /* Special boundary walls -- East */
737 for (i = 0; i < MAX_HGT; i++)
739 cave[i][MAX_WID - 1].feat = FEAT_PERM_SOLID;
740 cave[i][MAX_WID - 1].mimic = border.east[i];
743 /* North west corner */
744 cave[0][0].mimic = border.north_west;
746 /* North east corner */
747 cave[0][MAX_WID - 1].mimic = border.north_east;
749 /* South west corner */
750 cave[MAX_HGT - 1][0].mimic = border.south_west;
752 /* South east corner */
753 cave[MAX_HGT - 1][MAX_WID - 1].mimic = border.south_east;
755 /* Light up or darken the area */
756 for (y = 0; y < cur_hgt; y++)
758 for (x = 0; x < cur_wid; x++)
760 /* Get the cave grid */
766 c_ptr->info |= (CAVE_GLOW);
768 /* Hack -- Memorize lit grids if allowed */
769 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
773 /* Darken "boring" features */
774 if ((c_ptr->feat <= FEAT_INVIS) ||
775 ((c_ptr->feat >= FEAT_DEEP_WATER) &&
776 (c_ptr->feat <= FEAT_MOUNTAIN) &&
777 (c_ptr->feat != FEAT_MUSEUM)) ||
778 (x == 0) || (x == cur_wid-1) ||
779 (y == 0) || (y == cur_hgt-1))
781 /* Forget the grid */
782 c_ptr->info &= ~(CAVE_GLOW | CAVE_MARK);
784 else if (c_ptr->feat == FEAT_ENTRANCE)
787 c_ptr->info |= (CAVE_GLOW);
789 /* Hack -- Memorize lit grids if allowed */
790 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
796 if (p_ptr->teleport_town)
798 for (y = 0; y < cur_hgt; y++)
800 for (x = 0; x < cur_wid; x++)
802 if (((cave[y][x].feat - FEAT_BLDG_HEAD) == 4) || ((p_ptr->town_num == 1) && ((cave[y][x].feat - FEAT_BLDG_HEAD) == 0)))
804 if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
810 p_ptr->teleport_town = FALSE;
813 else if (p_ptr->leaving_dungeon)
815 for (y = 0; y < cur_hgt; y++)
817 for (x = 0; x < cur_wid; x++)
819 if (cave[y][x].feat == FEAT_ENTRANCE)
821 if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
827 p_ptr->teleport_town = FALSE;
830 player_place(p_ptr->oldpy, p_ptr->oldpx);
831 p_ptr->leftbldg = FALSE;
832 /* p_ptr->leaving_dungeon = FALSE;*/
834 lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
836 /* Make some residents */
837 for (i = 0; i < lim; i++)
841 if (!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))))
842 mode |= PM_ALLOW_SLEEP;
844 /* Make a resident */
845 (void)alloc_monster(generate_encounter ? 0 : 3, mode);
848 if(generate_encounter) ambush_flag = TRUE;
849 generate_encounter = FALSE;
851 for (i = 0; i < 100; i++)
853 floor_type[i] = FEAT_FLOOR;
854 fill_type[i] = FEAT_WALL_EXTRA;
857 /* Set rewarded quests to finished */
858 for (i = 0; i < max_quests; i++)
860 if (quest[i].status == QUEST_STATUS_REWARDED)
861 quest[i].status = QUEST_STATUS_FINISHED;
867 * Build the wilderness area.
870 void wilderness_gen_small()
874 /* To prevent stupid things */
875 for (i = 0; i < MAX_WID; i++)
876 for (j = 0; j < MAX_HGT; j++)
878 cave[j][i].feat = FEAT_PERM_SOLID;
881 /* Init the wilderness */
882 process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
885 for (i = 0; i < max_wild_x; i++)
886 for (j = 0; j < max_wild_y; j++)
888 if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
890 cave[j][i].feat = FEAT_TOWN;
891 cave[j][i].special = wilderness[j][i].town;
893 else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
894 else if (wilderness[j][i].entrance && (p_ptr->total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
896 cave[j][i].feat = FEAT_ENTRANCE;
897 cave[j][i].special = (byte)wilderness[j][i].entrance;
899 else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
901 cave[j][i].info |= (CAVE_GLOW | CAVE_MARK);
904 cur_hgt = (max_wild_y / SCREEN_HGT + 1) * SCREEN_HGT;
905 cur_wid = (max_wild_x / SCREEN_WID + 1) * SCREEN_WID;
907 if (cur_hgt > MAX_HGT) cur_hgt = MAX_HGT;
908 if (cur_wid > MAX_WID) cur_wid = MAX_WID;
910 /* Assume illegal panel */
911 panel_row_min = cur_hgt;
912 panel_col_min = cur_wid;
914 /* Place the player */
915 px = p_ptr->wilderness_x;
916 py = p_ptr->wilderness_y;
922 typedef struct wilderness_grid wilderness_grid;
924 struct wilderness_grid
926 int terrain; /* Terrain type */
927 int town; /* Town number */
928 s16b level; /* Level of the wilderness */
929 byte road; /* Road */
930 char name[32]; /* Name of the town/wilderness */
934 static wilderness_grid w_letter[255];
938 * Parse a sub-file of the "extra info"
940 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
947 if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
951 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
964 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
966 int index = zz[0][0];
969 w_letter[index].terrain = atoi(zz[1]);
971 w_letter[index].terrain = 0;
974 w_letter[index].level = atoi(zz[2]);
976 w_letter[index].level = 0;
979 w_letter[index].town = atoi(zz[3]);
981 w_letter[index].town = 0;
984 w_letter[index].road = atoi(zz[4]);
986 w_letter[index].road = 0;
989 strcpy(w_letter[index].name, zz[5]);
991 w_letter[index].name[0] = 0;
996 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1002 /* Process "W:D:<layout> */
1003 /* Layout of the wilderness */
1006 /* Acquire the text */
1009 /* Length of the text */
1010 int len = strlen(s);
1012 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
1016 wilderness[*y][*x].terrain = w_letter[idx].terrain;
1018 wilderness[*y][*x].level = w_letter[idx].level;
1020 wilderness[*y][*x].town = w_letter[idx].town;
1022 wilderness[*y][*x].road = w_letter[idx].road;
1024 strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
1032 /* Process "W:P:<x>:<y> - starting position in the wilderness */
1035 if ((p_ptr->wilderness_x == 0) &&
1036 (p_ptr->wilderness_y == 0))
1038 if (tokenize(buf+4, 2, zz, 0) == 2)
1040 p_ptr->wilderness_y = atoi(zz[0]);
1041 p_ptr->wilderness_x = atoi(zz[1]);
1043 if ((p_ptr->wilderness_x < 1) ||
1044 (p_ptr->wilderness_x > max_wild_x) ||
1045 (p_ptr->wilderness_y < 1) ||
1046 (p_ptr->wilderness_y > max_wild_y))
1048 return (PARSE_ERROR_OUT_OF_BOUNDS);
1053 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1062 return (PARSE_ERROR_UNDEFINED_DIRECTIVE);
1065 for (i = 1; i < max_d_idx; i++)
1067 if (!d_info[i].maxdepth) continue;
1068 wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
1069 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
1070 wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
1079 * Generate the random seeds for the wilderness
1081 void seed_wilderness(void)
1085 /* Init wilderness seeds */
1086 for (x = 0; x < max_wild_x; x++)
1088 for (y = 0; y < max_wild_y; y++)
1090 wilderness[y][x].seed = randint0(0x10000000);
1091 wilderness[y][x].entrance = 0;
1098 * Pointer to wilderness_type
1100 typedef wilderness_type *wilderness_type_ptr;
1103 * Initialize wilderness array
1105 errr init_wilderness(void)
1109 /* Allocate the wilderness (two-dimension array) */
1110 C_MAKE(wilderness, max_wild_y, wilderness_type_ptr);
1111 C_MAKE(wilderness[0], max_wild_x * max_wild_y, wilderness_type);
1113 /* Init the other pointers */
1114 for (i = 1; i < max_wild_y; i++)
1115 wilderness[i] = wilderness[0] + i * max_wild_x;
1117 generate_encounter = FALSE;
1123 bool change_wild_mode(void)
1126 bool have_pet = FALSE;
1128 if (lite_town || vanilla_town)
1131 msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
1133 msg_print("No global mal");
1137 if (!p_ptr->wild_mode)
1139 for (i = 1; i < m_max; i++)
1141 monster_type *m_ptr = &m_list[i];
1143 if (!m_ptr->r_idx) continue;
1144 if (is_pet(m_ptr) && i != p_ptr->riding) have_pet = TRUE;
1145 if (m_ptr->csleep) continue;
1146 if (m_ptr->cdis > MAX_SIGHT) continue;
1147 if (!is_hostile(m_ptr)) continue;
1149 msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤¤Ï¹°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
1151 msg_print("You cannot enter global map, since there is some monsters nearby!");
1160 if(!get_check_strict("¥Ú¥Ã¥È¤òÃÖ¤¤¤Æ¹°è¥Þ¥Ã¥×¤ËÆþ¤ê¤Þ¤¹¤«¡©", CHECK_OKAY_CANCEL))
1162 if(!get_check_strict("Do you leave your pets behind? ", CHECK_OKAY_CANCEL))
1173 set_action(ACTION_NONE);
1175 p_ptr->wild_mode = !p_ptr->wild_mode;
1178 p_ptr->leaving = TRUE;