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 if (generate_wilderness_callback(y, x)) return;
533 #endif /* USE_SCRIPT */
536 /* Create the town */
539 /* Reset the buildings */
542 /* Initialize the town */
544 init_flags = INIT_CREATE_DUNGEON | INIT_ONLY_FEATURES;
546 init_flags = INIT_CREATE_DUNGEON;
548 process_dungeon_file("t_info_j.txt", 0, 0, MAX_HGT, MAX_WID);
550 if (!corner && !border) p_ptr->visit |= (1L << (p_ptr->town_num - 1));
554 int terrain = wilderness[y][x].terrain;
555 u32b seed = wilderness[y][x].seed;
557 generate_wilderness_area(terrain, seed, border, corner);
560 if (!corner && !wilderness[y][x].town)
563 * Place roads in the wilderness
564 * ToDo: make the road a bit more interresting
566 if (wilderness[y][x].road)
568 cave[MAX_HGT/2][MAX_WID/2].feat = FEAT_FLOOR;
570 if (wilderness[y-1][x].road)
573 for (y1 = 1; y1 < MAX_HGT/2; y1++)
576 cave[y1][x1].feat = FEAT_FLOOR;
580 if (wilderness[y+1][x].road)
583 for (y1 = MAX_HGT/2; y1 < MAX_HGT - 1; y1++)
586 cave[y1][x1].feat = FEAT_FLOOR;
590 if (wilderness[y][x+1].road)
593 for (x1 = MAX_WID/2; x1 < MAX_WID - 1; x1++)
596 cave[y1][x1].feat = FEAT_FLOOR;
600 if (wilderness[y][x-1].road)
603 for (x1 = 1; x1 < MAX_WID/2; x1++)
606 cave[y1][x1].feat = FEAT_FLOOR;
612 if (wilderness[y][x].entrance && !wilderness[y][x].town && (total_winner || !(d_info[wilderness[y][x].entrance].flags1 & DF1_WINNER)))
616 /* Hack -- Use the "simple" RNG */
619 /* Hack -- Induce consistant town layout */
620 Rand_value = wilderness[y][x].seed;
622 dy = rand_range(6, cur_hgt - 6);
623 dx = rand_range(6, cur_wid - 6);
625 cave[dy][dx].feat = FEAT_ENTRANCE;
626 cave[dy][dx].special = (byte)wilderness[y][x].entrance;
628 /* Use the complex RNG */
635 * Border of the wilderness area
637 static border_type border;
641 * Build the wilderness area outside of the town.
643 void wilderness_gen(void)
653 /* Determine number of panels */
654 max_panel_rows = (cur_hgt / SCREEN_HGT) * 2 - 2;
655 max_panel_cols = (cur_wid / SCREEN_WID) * 2 - 2;
657 /* Assume illegal panel */
658 panel_row = max_panel_rows;
659 panel_col = max_panel_cols;
661 /* Init the wilderness */
663 process_dungeon_file("w_info_j.txt", 0, 0, max_wild_y, max_wild_x);
665 x = p_ptr->wilderness_x;
666 y = p_ptr->wilderness_y;
668 /* Prepare allocation table */
669 get_mon_num_prep(get_monster_hook(), NULL);
672 generate_area(y - 1, x, TRUE, FALSE);
674 for (i = 1; i < MAX_WID - 1; i++)
676 border.north[i] = cave[MAX_HGT - 2][i].feat;
680 generate_area(y + 1, x, TRUE, FALSE);
682 for (i = 1; i < MAX_WID - 1; i++)
684 border.south[i] = cave[1][i].feat;
688 generate_area(y, x - 1, TRUE, FALSE);
690 for (i = 1; i < MAX_HGT - 1; i++)
692 border.west[i] = cave[i][MAX_WID - 2].feat;
696 generate_area(y, x + 1, TRUE, FALSE);
698 for (i = 1; i < MAX_HGT - 1; i++)
700 border.east[i] = cave[i][1].feat;
703 /* North west corner */
704 generate_area(y - 1, x - 1, FALSE, TRUE);
705 border.north_west = cave[MAX_HGT - 2][MAX_WID - 2].feat;
707 /* North east corner */
708 generate_area(y - 1, x + 1, FALSE, TRUE);
709 border.north_east = cave[MAX_HGT - 2][1].feat;
711 /* South west corner */
712 generate_area(y + 1, x - 1, FALSE, TRUE);
713 border.south_west = cave[1][MAX_WID - 2].feat;
715 /* South east corner */
716 generate_area(y + 1, x + 1, FALSE, TRUE);
717 border.south_east = cave[1][1].feat;
720 /* Create terrain of the current area */
721 generate_area(y, x, FALSE, FALSE);
724 /* Special boundary walls -- North */
725 for (i = 0; i < MAX_WID; i++)
727 cave[0][i].feat = FEAT_PERM_SOLID;
728 cave[0][i].mimic = border.north[i];
731 /* Special boundary walls -- South */
732 for (i = 0; i < MAX_WID; i++)
734 cave[MAX_HGT - 1][i].feat = FEAT_PERM_SOLID;
735 cave[MAX_HGT - 1][i].mimic = border.south[i];
738 /* Special boundary walls -- West */
739 for (i = 0; i < MAX_HGT; i++)
741 cave[i][0].feat = FEAT_PERM_SOLID;
742 cave[i][0].mimic = border.west[i];
745 /* Special boundary walls -- East */
746 for (i = 0; i < MAX_HGT; i++)
748 cave[i][MAX_WID - 1].feat = FEAT_PERM_SOLID;
749 cave[i][MAX_WID - 1].mimic = border.east[i];
752 /* North west corner */
753 cave[0][0].mimic = border.north_west;
755 /* North east corner */
756 cave[0][MAX_WID - 1].mimic = border.north_east;
758 /* South west corner */
759 cave[MAX_HGT - 1][0].mimic = border.south_west;
761 /* South east corner */
762 cave[MAX_HGT - 1][MAX_WID - 1].mimic = border.south_east;
766 if ((turn % (20L * TOWN_DAWN)) < ((20L * TOWN_DAWN) / 2))
771 /* Light up or darken the area */
772 for (y = 0; y < cur_hgt; y++)
774 for (x = 0; x < cur_wid; x++)
776 /* Get the cave grid */
782 c_ptr->info |= (CAVE_GLOW);
784 /* Hack -- Memorize lit grids if allowed */
785 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
789 /* Darken "boring" features */
790 if ((c_ptr->feat <= FEAT_INVIS) ||
791 ((c_ptr->feat >= FEAT_DEEP_WATER) &&
792 (c_ptr->feat <= FEAT_TREES) &&
793 (c_ptr->feat != FEAT_MUSEUM)) ||
794 (x == 0) || (x == cur_wid-1) ||
795 (y == 0) || (y == cur_hgt-1))
797 /* Forget the grid */
798 c_ptr->info &= ~(CAVE_GLOW | CAVE_MARK);
800 else if (c_ptr->feat == FEAT_ENTRANCE)
803 c_ptr->info |= (CAVE_GLOW);
805 /* Hack -- Memorize lit grids if allowed */
806 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
812 if (p_ptr->teleport_town)
814 for (y = 0; y < cur_hgt; y++)
816 for (x = 0; x < cur_wid; x++)
818 if (((cave[y][x].feat - FEAT_BLDG_HEAD) == 4) || ((p_ptr->town_num == 1) && ((cave[y][x].feat - FEAT_BLDG_HEAD) == 0)))
820 if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
826 p_ptr->teleport_town = FALSE;
829 else if (p_ptr->leaving_dungeon)
831 for (y = 0; y < cur_hgt; y++)
833 for (x = 0; x < cur_wid; x++)
835 if (cave[y][x].feat == FEAT_ENTRANCE)
837 if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
843 p_ptr->teleport_town = FALSE;
846 player_place(p_ptr->oldpy, p_ptr->oldpx);
847 p_ptr->leftbldg = FALSE;
848 /* p_ptr->leaving_dungeon = FALSE;*/
850 lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
852 /* Make some residents */
853 for (i = 0; i < lim; i++)
855 /* Make a resident */
856 (void)alloc_monster(generate_encounter ? 0 : 3, (bool)!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))));
859 if(generate_encounter) ambush_flag = TRUE;
860 generate_encounter = FALSE;
862 for (i = 0; i < 100; i++)
864 floor_type[i] = FEAT_FLOOR;
865 fill_type[i] = FEAT_WALL_EXTRA;
868 /* Set rewarded quests to finished */
869 for (i = 0; i < max_quests; i++)
871 if (quest[i].status == QUEST_STATUS_REWARDED)
872 quest[i].status = QUEST_STATUS_FINISHED;
878 * Build the wilderness area.
881 void wilderness_gen_small()
885 /* To prevent stupid things */
886 for (i = 0; i < MAX_WID; i++)
887 for (j = 0; j < MAX_HGT; j++)
889 cave[j][i].feat = FEAT_PERM_SOLID;
892 /* Init the wilderness */
893 process_dungeon_file("w_info_j.txt", 0, 0, max_wild_y, max_wild_x);
896 for (i = 0; i < max_wild_x; i++)
897 for (j = 0; j < max_wild_y; j++)
899 if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
901 cave[j][i].feat = FEAT_TOWN;
902 cave[j][i].special = wilderness[j][i].town;
904 else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
905 else if (wilderness[j][i].entrance && (total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
907 cave[j][i].feat = FEAT_ENTRANCE;
908 cave[j][i].special = (byte)wilderness[j][i].entrance;
910 else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
912 cave[j][i].info |= (CAVE_GLOW | CAVE_MARK);
915 cur_hgt = (max_wild_y / SCREEN_HGT + 1) * SCREEN_HGT;
916 cur_wid = (max_wild_x / SCREEN_WID + 1) * SCREEN_WID;
918 if (cur_hgt > MAX_HGT) cur_hgt = MAX_HGT;
919 if (cur_wid > MAX_WID) cur_wid = MAX_WID;
921 /* Determine number of panels */
922 max_panel_rows = (cur_hgt / SCREEN_HGT) * 2 - 2;
923 max_panel_cols = (cur_wid / SCREEN_WID) * 2 - 2;
925 /* Assume illegal panel */
926 panel_row = max_panel_rows;
927 panel_col = max_panel_cols;
929 /* Place the player */
930 px = p_ptr->wilderness_x;
931 py = p_ptr->wilderness_y;
937 typedef struct wilderness_grid wilderness_grid;
939 struct wilderness_grid
941 int terrain; /* Terrain type */
942 int town; /* Town number */
943 s16b level; /* Level of the wilderness */
944 byte road; /* Road */
945 char name[32]; /* Name of the town/wilderness */
949 static wilderness_grid w_letter[255];
953 * Parse a sub-file of the "extra info"
955 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
962 if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
966 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
979 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
981 int index = zz[0][0];
984 w_letter[index].terrain = atoi(zz[1]);
986 w_letter[index].terrain = 0;
989 w_letter[index].level = atoi(zz[2]);
991 w_letter[index].level = 0;
994 w_letter[index].town = atoi(zz[3]);
996 w_letter[index].town = 0;
999 w_letter[index].road = atoi(zz[4]);
1001 w_letter[index].road = 0;
1004 strcpy(w_letter[index].name, zz[5]);
1006 w_letter[index].name[0] = 0;
1011 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1017 /* Process "W:D:<layout> */
1018 /* Layout of the wilderness */
1021 /* Acquire the text */
1024 /* Length of the text */
1025 int len = strlen(s);
1027 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
1031 wilderness[*y][*x].terrain = w_letter[idx].terrain;
1033 wilderness[*y][*x].level = w_letter[idx].level;
1035 wilderness[*y][*x].town = w_letter[idx].town;
1037 wilderness[*y][*x].road = w_letter[idx].road;
1039 strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
1047 /* Process "W:P:<x>:<y> - starting position in the wilderness */
1050 if ((p_ptr->wilderness_x == 0) &&
1051 (p_ptr->wilderness_y == 0))
1053 if (tokenize(buf+4, 2, zz, 0) == 2)
1055 p_ptr->wilderness_y = atoi(zz[0]);
1056 p_ptr->wilderness_x = atoi(zz[1]);
1058 if ((p_ptr->wilderness_x < 1) ||
1059 (p_ptr->wilderness_x > max_wild_x) ||
1060 (p_ptr->wilderness_y < 1) ||
1061 (p_ptr->wilderness_y > max_wild_y))
1063 return (PARSE_ERROR_OUT_OF_BOUNDS);
1068 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1077 return (PARSE_ERROR_UNDEFINED_DIRECTIVE);
1080 for (i = 1; i < max_d_idx; i++)
1082 if (!d_info[i].maxdepth) continue;
1083 wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
1084 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
1085 wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
1094 * Generate the random seeds for the wilderness
1096 void seed_wilderness(void)
1101 if (!wilderness_init_callback())
1102 #endif /* USE_SCRIPT */
1104 /* Init wilderness seeds */
1105 for (x = 0; x < max_wild_x; x++)
1107 for (y = 0; y < max_wild_y; y++)
1109 wilderness[y][x].seed = rand_int(0x10000000);
1110 wilderness[y][x].entrance = 0;
1118 * Pointer to wilderness_type
1120 typedef wilderness_type *wilderness_type_ptr;
1123 * Initialize wilderness array
1125 errr init_wilderness(void)
1129 /* Allocate the wilderness (two-dimension array) */
1130 C_MAKE(wilderness, max_wild_y, wilderness_type_ptr);
1131 C_MAKE(wilderness[0], max_wild_x * max_wild_y, wilderness_type);
1133 /* Init the other pointers */
1134 for (i = 1; i < max_wild_y; i++)
1135 wilderness[i] = wilderness[0] + i * max_wild_x;
1137 generate_encounter = FALSE;
1143 bool change_wild_mode(void)
1147 if (lite_town || vanilla_town)
1150 msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
1152 msg_print("No global mal");
1156 if (!p_ptr->wild_mode)
1158 for (i = 1; i < m_max; i++)
1160 monster_type *m_ptr = &m_list[i];
1162 if (!m_ptr->r_idx) continue;
1163 if (m_ptr->csleep) continue;
1164 if (m_ptr->cdis > MAX_SIGHT) continue;
1165 if (!is_hostile(m_ptr)) continue;
1167 msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤¤Ï¹°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
1169 msg_print("You cannot enter global map, since there is some monsters nearby!");
1177 set_action(ACTION_NONE);
1179 p_ptr->wild_mode = !p_ptr->wild_mode;
1182 p_ptr->leaving = TRUE;