OSDN Git Service

呪文の巻物の効果がスナイパーにも一部付加されていた不具合を修正。
[hengband/hengband.git] / src / init1.c
index c5812a0..bfcdb7d 100644 (file)
@@ -1,24 +1,18 @@
 /* File: init1.c */
 
+/*
+ * Copyright (c) 1997 Ben Harrison
+ *
+ * This software may be copied and distributed for educational, research,
+ * and not for profit purposes provided that this copyright and statement
+ * are included in all such copies.  Other copyrights may also apply.
+ */
+
 /* Purpose: Initialization (part 1) -BEN- */
 
 #include "angband.h"
 
 
-#ifdef JP
-#undef strchr
-char* _strchr(char* ptr, char ch)
-{
-       for ( ; *ptr != '\0'; ++ptr)
-       {
-               if (*ptr == ch) return ptr;
-               if (iskanji(*ptr)) ++ptr;
-       }
-
-       return NULL;
-}
-#define strchr _strchr
-#endif
 /*
  * This file is used to initialize various variables and arrays for the
  * Angband game.  Note the use of "fd_read()" and "fd_write()" to bypass
@@ -134,6 +128,130 @@ static cptr r_info_blow_effect[] =
 
 
 /*
+ * Feature info flags
+ */
+static cptr f_info_flags[] =
+{
+       "LOS",
+       "PROJECT",
+       "MOVE",
+       "PLACE",
+       "DROP",
+       "SECRET",
+       "NOTICE",
+       "REMEMBER",
+       "OPEN",
+       "CLOSE",
+       "BASH",
+       "SPIKE",
+       "DISARM",
+       "STORE",
+       "TUNNEL",
+       "MAY_HAVE_GOLD",
+       "HAS_GOLD",
+       "HAS_ITEM",
+       "DOOR",
+       "TRAP",
+       "STAIRS",
+       "GLYPH",
+       "LESS",
+       "MORE",
+       "AVOID_RUN",
+       "FLOOR",
+       "WALL",
+       "PERMANENT",
+       "XXX00",
+       "XXX01",
+       "XXX02",
+       "HIT_TRAP",
+
+       "BRIDGE",
+       "RIVER",
+       "LAKE",
+       "BRIDGED",
+       "COVERED",
+       "GLOW",
+       "ENSECRET",
+       "WATER",
+       "LAVA",
+       "SHALLOW",
+       "DEEP",
+       "FILLED",
+       "HURT_ROCK",
+       "HURT_FIRE",
+       "HURT_COLD",
+       "HURT_ACID",
+       "ICE",
+       "ACID",
+       "OIL",
+       "XXX04",
+       "CAN_CLIMB",
+       "CAN_FLY",
+       "CAN_SWIM",
+       "CAN_PASS",
+       "CAN_OOZE",
+       "CAN_DIG",
+       "HIDE_ITEM",
+       "HIDE_SNEAK",
+       "HIDE_SWIM",
+       "HIDE_DIG",
+       "KILL_HUGE",
+       "KILL_MOVE",
+
+       "PICK_TRAP",
+       "PICK_DOOR",
+       "ALLOC",
+       "CHEST",
+       "DROP_1D2",
+       "DROP_2D2",
+       "DROP_GOOD",
+       "DROP_GREAT",
+       "HURT_POIS",
+       "HURT_ELEC",
+       "HURT_WATER",
+       "HURT_BWATER",
+       "USE_FEAT",
+       "GET_FEAT",
+       "GROUND",
+       "OUTSIDE",
+       "EASY_HIDE",
+       "EASY_CLIMB",
+       "MUST_CLIMB",
+       "TREE",
+       "NEED_TREE",
+       "BLOOD",
+       "DUST",
+       "SLIME",
+       "PLANT",
+       "XXX2",
+       "INSTANT",
+       "EXPLODE",
+       "TIMED",
+       "ERUPT",
+       "STRIKE",
+       "SPREAD",
+
+       "SPECIAL",
+       "HURT_DISI",
+       "QUEST_ENTER",
+       "QUEST_EXIT",
+       "QUEST",
+       "SHAFT",
+       "MOUNTAIN",
+       "BLDG",
+       "MINOR_GLYPH",
+       "PATTERN",
+       "TOWN",
+       "ENTRANCE",
+       "MIRROR",
+       "UNPERM",
+       "TELEPORTABLE",
+       "CONVERT",
+       "GLASS",
+};
+
+
+/*
  * Monster race flags
  */
 static cptr r_info_flags1[] =
@@ -143,14 +261,14 @@ static cptr r_info_flags1[] =
        "MALE",
        "FEMALE",
        "CHAR_CLEAR",
-       "CHAR_MULTI",
+       "SHAPECHANGER",
        "ATTR_CLEAR",
        "ATTR_MULTI",
        "FORCE_DEPTH",
        "FORCE_MAXHP",
        "FORCE_SLEEP",
        "FORCE_EXTRA",
-       "XXX1",
+       "ATTR_SEMIRAND",
        "FRIENDS",
        "ESCORT",
        "ESCORTS",
@@ -187,7 +305,7 @@ static cptr r_info_flags2[] =
        "WEIRD_MIND",
        "MULTIPLY",
        "REGENERATE",
-       "SHAPECHANGER",
+       "CHAR_MULTI",
        "ATTR_ANY",
        "POWERFUL",
        "ELDRITCH_HORROR",
@@ -201,13 +319,13 @@ static cptr r_info_flags2[] =
        "KILL_BODY",
        "TAKE_ITEM",
        "KILL_ITEM",
-       "BRAIN_1",
-       "BRAIN_2",
-       "BRAIN_3",
-       "BRAIN_4",
-       "BRAIN_5",
-       "BRAIN_6",
-       "BRAIN_7",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "HUMAN",
        "QUANTUM"
 };
 
@@ -232,18 +350,18 @@ static cptr r_info_flags3[] =
        "HURT_ROCK",
        "HURT_FIRE",
        "HURT_COLD",
-       "IM_ACID",
-       "IM_ELEC",
-       "IM_FIRE",
-       "IM_COLD",
-       "IM_POIS",
-       "RES_TELE",
-       "RES_NETH",
-       "RES_WATE",
-       "RES_PLAS",
-       "RES_NEXU",
-       "RES_DISE",
-       "RES_ALL",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
        "NO_FEAR",
        "NO_STUN",
        "NO_CONF",
@@ -377,7 +495,7 @@ static cptr r_info_flags7[] =
        "CAN_SWIM",
        "CAN_FLY",
        "FRIENDLY",
-       "UNIQUE_7",
+       "NAZGUL",
        "UNIQUE2",
        "RIDING",
        "KAGE",
@@ -388,11 +506,11 @@ static cptr r_info_flags7[] =
        "GUARDIAN",
        "CHAMELEON",
        "KILL_EXP",
-       "XXX7X15",
-       "XXX7X16",
-       "XXX7X17",
-       "XXX7X18",
-       "XXX7X19",
+       "TANUKI",
+       "HAS_DARK_1",
+       "SELF_DARK_1",
+       "HAS_DARK_2",
+       "SELF_DARK_2",
        "XXX7X20",
        "XXX7X21",
        "XXX7X22",
@@ -489,9 +607,49 @@ static cptr r_info_flags9[] =
 
 
 /*
+ * Monster race flags - Resistances
+ */
+static cptr r_info_flagsr[] =
+{
+       "IM_ACID",
+       "IM_ELEC",
+       "IM_FIRE",
+       "IM_COLD",
+       "IM_POIS",
+       "RES_LITE",
+       "RES_DARK",
+       "RES_NETH",
+       "RES_WATE",
+       "RES_PLAS",
+       "RES_SHAR",
+       "RES_SOUN",
+       "RES_CHAO",
+       "RES_NEXU",
+       "RES_DISE",
+       "RES_WALL",
+       "RES_INER",
+       "RES_TIME",
+       "RES_GRAV",
+       "RES_ALL",
+       "RES_TELE",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+};
+
+
+/*
  * Object flags
  */
-static cptr k_info_flags1[] =
+static cptr k_info_flags[] =
 {
        "STR",
        "INT",
@@ -524,14 +682,8 @@ static cptr k_info_flags1[] =
        "BRAND_ACID",
        "BRAND_ELEC",
        "BRAND_FIRE",
-       "BRAND_COLD"
-};
+       "BRAND_COLD",
 
-/*
- * Object flags
- */
-static cptr k_info_flags2[] =
-{
        "SUST_STR",
        "SUST_INT",
        "SUST_WIS",
@@ -539,7 +691,7 @@ static cptr k_info_flags2[] =
        "SUST_CON",
        "SUST_CHR",
        "RIDING",
-       "XXX2",
+       "EASY_SPELL",
        "IM_ACID",
        "IM_ELEC",
        "IM_FIRE",
@@ -563,17 +715,11 @@ static cptr k_info_flags2[] =
        "RES_NETHER",
        "RES_NEXUS",
        "RES_CHAOS",
-       "RES_DISEN"
-};
+       "RES_DISEN",
 
-/*
- * Object flags
- */
-static cptr k_info_flags3[] =
-{
        "SH_FIRE",
        "SH_ELEC",
-       "XXX1",
+       "SLAY_HUMAN",
        "SH_COLD",
        "NO_TELE",
        "NO_MAGIC",
@@ -582,8 +728,8 @@ static cptr k_info_flags3[] =
        "WARNING",
        "HIDE_TYPE",
        "SHOW_MODS",
-       "XXX2",
-       "FEATHER",
+       "XXX1",
+       "LEVITATION",
        "LITE",
        "SEE_INVIS",
        "TELEPATHY",
@@ -600,9 +746,32 @@ static cptr k_info_flags3[] =
        "TELEPORT",
        "AGGRAVATE",
        "BLESSED",
-       "CURSED",
-       "HEAVY_CURSE",
-       "PERMA_CURSE"
+       "XXX3",
+       "XXX4",
+       "XXX5",
+
+       "KILL_ANIMAL",
+       "KILL_EVIL",
+       "KILL_UNDEAD",
+       "KILL_DEMON",
+       "KILL_ORC",
+       "KILL_TROLL",
+       "KILL_GIANT",
+       "KILL_HUMAN",
+       "ESP_ANIMAL",
+       "ESP_UNDEAD",
+       "ESP_DEMON",
+       "ESP_ORC",
+       "ESP_TROLL",
+       "ESP_GIANT",
+       "ESP_DRAGON",
+       "ESP_HUMAN",
+       "ESP_EVIL",
+       "ESP_GOOD",
+       "ESP_NONLIVING",
+       "ESP_UNIQUE",
+       "FULL_NAME",
+       "FIXED_FLAVOR",
 };
 
 
@@ -618,12 +787,12 @@ static cptr k_info_gen_flags[] =
        "XTRA_L_RES",
        "XTRA_D_RES",
        "XTRA_RES",
-       "XXX",
-       "XXX",
-       "XXX",
-       "XXX",
-       "XXX",
-       "XXX",
+       "CURSED",
+       "HEAVY_CURSE",
+       "PERMA_CURSE",
+       "RANDOM_CURSE0",
+       "RANDOM_CURSE1",
+       "RANDOM_CURSE2",
        "XXX",
        "XXX",
        "XXX",
@@ -648,38 +817,38 @@ static cptr k_info_gen_flags[] =
  */
 static cptr d_info_flags1[] =
 {
-        "WINNER",
-        "MAZE",
-        "SMALLEST",
-        "BEGINNER",
-        "BIG",
-        "NO_DOORS",
-        "WATER_RIVER",
-        "LAVA_RIVER",
-        "WATER_RIVERS",
-        "LAVA_RIVERS",
-        "CAVE",
-        "CAVERN",
-        "NO_UP",
-        "HOT",
-        "COLD",
-        "NO_DOWN",
-        "FORGET",
-        "LAKE_WATER",
-        "LAKE_LAVA",
-        "LAKE_RUBBLE",
-        "LAKE_TREE",
-        "NO_VAULT",
-        "ARENA",
-        "DESTROY",
-        "XXX1",
-        "NO_CAVE",
-        "NO_MAGIC",
-        "NO_MELEE",
-        "CHAMELEON",
-        "DARKNESS",
-        "XXX1",
-        "XXX1"
+       "WINNER",
+       "MAZE",
+       "SMALLEST",
+       "BEGINNER",
+       "BIG",
+       "NO_DOORS",
+       "WATER_RIVER",
+       "LAVA_RIVER",
+       "CURTAIN",
+       "GLASS_DOOR",
+       "CAVE",
+       "CAVERN",
+       "XXX",
+       "XXX",
+       "XXX",
+       "XXX",
+       "FORGET",
+       "LAKE_WATER",
+       "LAKE_LAVA",
+       "LAKE_RUBBLE",
+       "LAKE_TREE",
+       "NO_VAULT",
+       "ARENA",
+       "DESTROY",
+       "GLASS_ROOM",
+       "NO_CAVE",
+       "NO_MAGIC",
+       "NO_MELEE",
+       "CHAMELEON",
+       "DARKNESS",
+       "XXX",
+       "XXX"
 };
 
 
@@ -689,7 +858,7 @@ static cptr d_info_flags1[] =
  * Returns FALSE when there isn't enough space available to store
  * the text.
  */
-static bool add_text(u32b *offset, header *head, cptr buf)
+static bool add_text(u32b *offset, header *head, cptr buf, bool normal_text)
 {
        /* Hack -- Verify space */
        if (head->text_size + strlen(buf) + 8 > FAKE_TEXT_SIZE)
@@ -699,7 +868,34 @@ static bool add_text(u32b *offset, header *head, cptr buf)
        if (*offset == 0)
        {
                /* Advance and save the text index */
-               *offset = ++head->text_size;    
+               *offset = ++head->text_size;
+       }
+
+       /* Additional text */
+       else if (normal_text)
+       {
+               /*
+                * If neither the end of the last line nor
+                * the beginning of current line is not a space,
+                * fill up a space as a correct separator of two words.
+                */
+               if (head->text_size > 0 &&
+#ifdef JP
+                   (*(head->text_ptr + head->text_size - 1) != ' ') &&
+                   ((head->text_size == 1) || !iskanji(*(head->text_ptr + head->text_size - 2))) && 
+                   (buf[0] != ' ') && !iskanji(buf[0])
+#else
+                   (*(head->text_ptr + head->text_size - 1) != ' ') &&
+                   (buf[0] != ' ')
+#endif
+                   )
+               {
+                       /* Append a space */
+                       *(head->text_ptr + head->text_size) = ' ';
+
+                       /* Advance the index */
+                       head->text_size++;
+               }
        }
 
        /* Append chars to the text */
@@ -744,10 +940,52 @@ static bool add_name(u32b *offset, header *head, cptr buf)
 
 
 /*
+ * Add a tag to the tag-storage and return an offset to it.
+ *
+ * Returns FALSE when there isn't enough space available to store
+ * the name.
+ */
+static bool add_tag(s16b *offset, header *head, cptr buf)
+{
+       u32b i;
+
+       /* Search for an existing (fake) tag */
+       for (i = 1; i < head->tag_size; i += strlen(&head->tag_ptr[i]) + 1)
+       {
+               /* Found it */
+               if (streq(&head->tag_ptr[i], buf)) break;
+       }
+
+       /* There was no existing tag */
+       if (i >= head->tag_size)
+       {
+               /* Hack -- Verify space */
+               if (head->tag_size + strlen(buf) + 8 > FAKE_TAG_SIZE)
+                       return FALSE;
+
+               /* Append chars to the tags */
+               strcpy(head->tag_ptr + head->tag_size, buf);
+
+               /* Point the new tag */
+               i = head->tag_size;
+
+               /* Advance the index */
+               head->tag_size += strlen(buf) + 1;
+       }
+
+       /* Return offset of the tag */
+       *offset = (s16b)i;
+
+       /* Success */
+       return TRUE;
+}
+
+
+/*
  * Convert a "color letter" into an "actual" color
  * The colors are: dwsorgbuDWvyRGBU, as shown below
  */
-static int color_char_to_attr(char c)
+byte color_char_to_attr(char c)
 {
        switch (c)
        {
@@ -770,7 +1008,7 @@ static int color_char_to_attr(char c)
                case 'U': return (TERM_L_UMBER);
        }
 
-       return (-1);
+       return (255);
 }
 
 
@@ -782,7 +1020,7 @@ static int color_char_to_attr(char c)
  * Initialize an "*_info" array, by parsing an ascii "template" file
  */
 errr init_info_txt(FILE *fp, char *buf, header *head,
-                   parse_info_txt_func parse_info_txt_line)
+                  parse_info_txt_func parse_info_txt_line)
 {
        errr err;
 
@@ -796,6 +1034,7 @@ errr init_info_txt(FILE *fp, char *buf, header *head,
        /* Prepare the "fake" stuff */
        head->name_size = 0;
        head->text_size = 0;
+       head->tag_size = 0;
 
        /* Parse */
        while (0 == my_fgets(fp, buf, 1024))
@@ -817,6 +1056,17 @@ errr init_info_txt(FILE *fp, char *buf, header *head,
                        continue;
                }
 
+               /* Mega Hack -- Calculate Check Sum */
+               if (buf[0] != 'N' && buf[0] != 'D')
+               {
+                       int i;
+                       for (i = 0; buf[i]; i++)
+                       {
+                               head->v_extra += (byte)buf[i];
+                               head->v_extra ^= (1 << (i % 8));
+                       }
+               }
+
                /* Parse the line */
                if ((err = (*parse_info_txt_line)(buf, head)) != 0)
                        return (err);
@@ -847,7 +1097,7 @@ errr parse_v_info(char *buf, header *head)
        if (buf[0] == 'N')
        {
                /* Find the colon before the name */
-               s = strchr(buf+2, ':');
+               s = my_strchr(buf+2, ':');
 
                /* Verify that colon */
                if (!s) return (1);
@@ -887,7 +1137,7 @@ errr parse_v_info(char *buf, header *head)
                s = buf+2;
 
                /* Store the text */
-               if (!add_text(&v_ptr->text, head, s)) return (7);
+               if (!add_text(&v_ptr->text, head, s, FALSE)) return (7);
        }
 
        /* Process 'X' for "Extra info" (one line only) */
@@ -952,13 +1202,18 @@ errr parse_s_info(char *buf, header *head)
        else if (buf[0] == 'W')
        {
                int tval, sval, start, max;
-               const s16b exp_conv_table[] = { 0, 4000, 6000, 7000, 8000 };
+               const s16b exp_conv_table[] =
+               {
+                       WEAPON_EXP_UNSKILLED, WEAPON_EXP_BEGINNER, WEAPON_EXP_SKILLED,
+                       WEAPON_EXP_EXPERT, WEAPON_EXP_MASTER
+               };
 
                /* Scan for the values */
                if (4 != sscanf(buf+2, "%d:%d:%d:%d",
                                &tval, &sval, &start, &max)) return (1);
 
-               if (start < 0 || start > 4 || max < 0 || max > 4) return (8);
+               if (start < EXP_LEVEL_UNSKILLED || start > EXP_LEVEL_MASTER
+                       || max < EXP_LEVEL_UNSKILLED || max > EXP_LEVEL_MASTER) return (8);
 
                /* Save the values */
                s_ptr->w_start[tval][sval] = exp_conv_table[start];
@@ -974,7 +1229,8 @@ errr parse_s_info(char *buf, header *head)
                if (3 != sscanf(buf+2, "%d:%d:%d",
                                &num, &start, &max)) return (1);
 
-               if (start < 0 || start > 8000 || max < 0 || max > 8000) return (8);
+               if (start < WEAPON_EXP_UNSKILLED || start > WEAPON_EXP_MASTER
+                       || max < WEAPON_EXP_UNSKILLED || max > WEAPON_EXP_MASTER) return (8);
 
                /* Save the values */
                s_ptr->s_start[num] = start;
@@ -1035,7 +1291,7 @@ errr parse_m_info(char *buf, header *head)
                int xtra, type, first, weight;
 
                /* Find the colon before the name */
-               s = strchr(buf+2, ':');
+               s = my_strchr(buf+2, ':');
 
                /* Verify that colon */
                if (!s) return (1);
@@ -1055,7 +1311,7 @@ errr parse_m_info(char *buf, header *head)
                stat = s;
 
                /* Find the colon before the name */
-               s = strchr(s, ':');
+               s = my_strchr(s, ':');
 
                /* Verify that colon */
                if (!s) return (1);
@@ -1074,7 +1330,7 @@ errr parse_m_info(char *buf, header *head)
 
                /* Scan for the values */
                if (4 != sscanf(s, "%x:%d:%d:%d",
-                               &xtra, &type, &first, &weight)) return (1);
+                               (uint *)&xtra, &type, &first, &weight)) return (1);
 
                m_ptr->spell_xtra = xtra;
                m_ptr->spell_type = type;
@@ -1119,13 +1375,92 @@ errr parse_m_info(char *buf, header *head)
 
 
 /*
+ * Grab one flag from a textual string
+ */
+static errr grab_one_flag(u32b *flags, cptr names[], cptr what)
+{
+       int i;
+
+       /* Check flags */
+       for (i = 0; i < 32; i++)
+       {
+               if (streq(what, names[i]))
+               {
+                       *flags |= (1L << i);
+                       return 0;
+               }
+       }
+
+       return -1;
+}
+
+
+/*
+ * Grab one flag in an feature_type from a textual string
+ */
+static errr grab_one_feat_flag(feature_type *f_ptr, cptr what)
+{
+       int i;
+
+       /* Check flags */
+       for (i = 0; i < FF_FLAG_MAX; i++)
+       {
+               if (streq(what, f_info_flags[i]))
+               {
+                       add_flag(f_ptr->flags, i);
+                       return 0;
+               }
+       }
+
+       /* Oops */
+#ifdef JP
+       msg_format("̤ÃΤÎÃÏ·Á¥Õ¥é¥° '%s'¡£", what);
+#else
+       msg_format("Unknown feature flag '%s'.", what);
+#endif
+
+       /* Error */
+       return PARSE_ERROR_GENERIC;
+}
+
+
+/*
+ * Grab an action in an feature_type from a textual string
+ */
+static errr grab_one_feat_action(feature_type *f_ptr, cptr what, int count)
+{
+       int i;
+
+       /* Check flags */
+       for (i = 0; i < FF_FLAG_MAX; i++)
+       {
+               if (streq(what, f_info_flags[i]))
+               {
+                       f_ptr->state[count].action = i;
+                       return 0;
+               }
+       }
+
+       /* Oops */
+#ifdef JP
+       msg_format("̤ÃΤÎÃÏ·Á¥¢¥¯¥·¥ç¥ó '%s'¡£", what);
+#else
+       msg_format("Unknown feature action '%s'.", what);
+#endif
+
+       /* Error */
+       return PARSE_ERROR_GENERIC;
+}
+
+
+/*
  * Initialize the "f_info" array, by parsing an ascii "template" file
  */
 errr parse_f_info(char *buf, header *head)
 {
        int i;
 
-       char *s;
+       char *s, *t;
 
        /* Current entry */
        static feature_type *f_ptr = NULL;
@@ -1135,18 +1470,13 @@ errr parse_f_info(char *buf, header *head)
        if (buf[0] == 'N')
        {
                /* Find the colon before the name */
-               s = strchr(buf+2, ':');
+               s = my_strchr(buf+2, ':');
 
-                       /* Verify that colon */
-               if (!s) return (1);
-
-               /* Nuke the colon, advance to the name */
-               *s++ = '\0';
-
-#ifdef JP
-               /* Paranoia -- require a name */
-               if (!*s) return (1);
-#endif
+               if (s)
+               {
+                       /* Nuke the colon, advance to the name */
+                       *s++ = '\0';
+               }
 
                /* Get the index */
                i = atoi(buf+2);
@@ -1163,25 +1493,43 @@ errr parse_f_info(char *buf, header *head)
                /* Point at the "info" */
                f_ptr = &f_info[i];
 
-#ifdef JP
-               /* Store the name */
-               if (!add_name(&f_ptr->name, head, s)) return (7);
-#endif
+               /* Tag name is given */
+               if (s)
+               {
+                       /* Store the tag */
+                       if (!add_tag(&f_ptr->tag, head, s)) return (7);
+               }
+
                /* Default "mimic" */
                f_ptr->mimic = i;
+
+               /* Default "destroyed state" -- if not specified */
+               f_ptr->destroyed = i;
+
+               /* Default "states" */
+               for (i = 0; i < MAX_FEAT_STATES; i++) f_ptr->state[i].action = FF_FLAG_MAX;
        }
 
        /* There better be a current f_ptr */
        else if (!f_ptr) return (3);
 
 #ifdef JP
-       /* ±Ñ¸ì̾¤òÆɤà¥ë¡¼¥Á¥ó¤òÄɲà*/
-       /* 'E' ¤«¤é»Ï¤Þ¤ë¹Ô¤Ï±Ñ¸ì̾¤È¤·¤Æ¤¤¤ë */
+       else if (buf[0] == 'J')
+       {
+               /* Store the name */
+               if (!add_name(&f_ptr->name, head, buf+2)) return (7);
+       }
+
        else if (buf[0] == 'E')
        {
-               /* nothing to do */
+               /* Ignore english name */
        }
 #else
+       else if (buf[0] == 'J')
+       {
+               /* Ignore Japanese name */
+       }
+
        else if (buf[0] == 'E')
        {
                /* Acquire the Text */
@@ -1192,56 +1540,193 @@ errr parse_f_info(char *buf, header *head)
        }
 #endif
 
-#if 0
-
-       /* Process 'D' for "Description" */
-       else if (buf[0] == 'D')
-       {
-               /* Acquire the text */
-               s = buf+2;
-
-               /* Store the text */
-               if (!add_text(&f_ptr->text, head, s)) return (7);
-       }
-
-#endif
-
 
        /* Process 'M' for "Mimic" (one line only) */
        else if (buf[0] == 'M')
        {
-               int mimic;
+               s16b offset;
 
-               /* Scan for the values */
-               if (1 != sscanf(buf+2, "%d",
-                               &mimic)) return (1);
-
-               /* Save the values */
-               f_ptr->mimic = mimic;
+               if (!add_tag(&offset, head, buf + 2)) return PARSE_ERROR_OUT_OF_MEMORY;
 
+               /* Record a fake tag index */
+               f_ptr->mimic = -offset;
        }
 
 
        /* Process 'G' for "Graphics" (one line only) */
        else if (buf[0] == 'G')
        {
-               int tmp;
+               int j;
+               byte s_attr;
+               char char_tmp[F_LIT_MAX];
 
                /* Paranoia */
+               if (buf[1] != ':') return (1);
                if (!buf[2]) return (1);
-               if (!buf[3]) return (1);
+               if (buf[3] != ':') return (1);
                if (!buf[4]) return (1);
 
+               /* Extract the char */
+               char_tmp[F_LIT_STANDARD] = buf[2];
+
                /* Extract the color */
-               tmp = color_char_to_attr(buf[4]);
+               s_attr = color_char_to_attr(buf[4]);
 
                /* Paranoia */
-               if (tmp < 0) return (1);
+               if (s_attr > 127) return (1);
 
-               /* Save the values */
-               f_ptr->d_attr = tmp;
-               f_ptr->d_char = buf[2];
+               /* Save the standard values */
+               f_ptr->d_attr[F_LIT_STANDARD] = s_attr;
+               f_ptr->d_char[F_LIT_STANDARD] = char_tmp[F_LIT_STANDARD];
+
+               /* Is this feature supports lighting? */
+               if (buf[5] == ':')
+               {
+                       /* G:c:a:LIT (default) */
+                       apply_default_feat_lighting(f_ptr->d_attr, f_ptr->d_char);
+
+                       /* G:c:a:lc:la:dc:da */
+                       if (!streq(buf + 6, "LIT"))
+                       {
+                               char attr_lite_tmp[F_LIT_MAX - F_LIT_NS_BEGIN];
 
+                               if ((F_LIT_MAX - F_LIT_NS_BEGIN) * 2 != sscanf(buf + 6, "%c:%c:%c:%c",
+                                       &char_tmp[F_LIT_LITE], &attr_lite_tmp[F_LIT_LITE - F_LIT_NS_BEGIN],
+                                       &char_tmp[F_LIT_DARK], &attr_lite_tmp[F_LIT_DARK - F_LIT_NS_BEGIN])) return 1;
+                               if (buf[F_LIT_MAX * 4 + 1]) return 1;
+
+                               for (j = F_LIT_NS_BEGIN; j < F_LIT_MAX; j++)
+                               {
+                                       switch (attr_lite_tmp[j - F_LIT_NS_BEGIN])
+                                       {
+                                       case '*':
+                                               /* Use default lighting */
+                                               break;
+                                       case '-':
+                                               /* No lighting support */
+                                               f_ptr->d_attr[j] = f_ptr->d_attr[F_LIT_STANDARD];
+                                               break;
+                                       default:
+                                               /* Extract the color */
+                                               f_ptr->d_attr[j] = color_char_to_attr(attr_lite_tmp[j - F_LIT_NS_BEGIN]);
+                                               if (f_ptr->d_attr[j] > 127) return 1;
+                                               break;
+                                       }
+                                       f_ptr->d_char[j] = char_tmp[j];
+                               }
+                       }
+               }
+               else if (!buf[5])
+               {
+                       for (j = F_LIT_NS_BEGIN; j < F_LIT_MAX; j++)
+                       {
+                               f_ptr->d_attr[j] = s_attr;
+                               f_ptr->d_char[j] = char_tmp[F_LIT_STANDARD];
+                       }
+               }
+               else return 1;
+       }
+
+       /* Hack -- Process 'F' for flags */
+       else if (buf[0] == 'F')
+       {
+               /* Parse every entry textually */
+               for (s = buf + 2; *s; )
+               {
+                       /* Find the end of this entry */
+                       for (t = s; *t && (*t != ' ') && (*t != '|'); ++t) /* loop */;
+
+                       /* Nuke and skip any dividers */
+                       if (*t)
+                       {
+                               *t++ = '\0';
+                               while (*t == ' ' || *t == '|') t++;
+                       }
+
+                       /* XXX XXX XXX Hack -- Read feature subtype */
+                       if (1 == sscanf(s, "SUBTYPE_%d", &i))
+                       {
+                               /* Extract a "subtype" */
+                               f_ptr->subtype =  i;
+
+                               /* Start at next entry */
+                               s = t;
+
+                               /* Continue */
+                               continue;
+                       }
+
+                       /* XXX XXX XXX Hack -- Read feature power */
+                       if (1 == sscanf(s, "POWER_%d", &i))
+                       {
+                               /* Extract a "power" */
+                               f_ptr->power =  i;
+
+                               /* Start at next entry */
+                               s = t;
+
+                               /* Continue */
+                               continue;
+                       }
+
+                       /* Parse this entry */
+                       if (0 != grab_one_feat_flag(f_ptr, s)) return (PARSE_ERROR_INVALID_FLAG);
+
+                       /* Start the next entry */
+                       s = t;
+               }
+       }
+
+       /* Process 'W' for "More Info" (one line only) */
+       else if (buf[0] == 'W')
+       {
+               int priority;
+
+               /* Scan for the value */
+               if (1 != sscanf(buf+2, "%d", &priority)) return (PARSE_ERROR_GENERIC);
+
+               /* Save the value */
+               f_ptr->priority = priority;
+       }
+
+       /* Process 'K' for "States" (up to four lines + default (which cannot be last)) */
+       else if (buf[0] == 'K')
+       {
+               s16b offset;
+
+               /* Find the next empty state slot (if any) */
+               for (i = 0; i < MAX_FEAT_STATES; i++) if (f_ptr->state[i].action == FF_FLAG_MAX) break;
+
+               /* Oops, no more slots */
+               if (i == MAX_FEAT_STATES) return PARSE_ERROR_GENERIC;
+
+               /* Analyze the first field */
+               for (s = t = buf+2; *t && (*t != ':'); t++) /* loop */;
+
+               /* Terminate the field (if necessary) */
+               if (*t == ':') *t++ = '\0';
+
+               /* Is this default entry? */
+               if (streq(s, "DESTROYED"))
+               {
+                       if (!add_tag(&offset, head, t)) return PARSE_ERROR_OUT_OF_MEMORY;
+
+                       /* Record a fake tag index */
+                       f_ptr->destroyed = -offset;
+               }
+               else
+               {
+                       /* Reset */
+                       f_ptr->state[i].action = 0;
+
+                       /* Parse this entry */
+                       if (0 != grab_one_feat_action(f_ptr, s, i)) return PARSE_ERROR_INVALID_FLAG;
+
+                       if (!add_tag(&offset, head, t)) return PARSE_ERROR_OUT_OF_MEMORY;
+
+                       /* Record a fake tag index */
+                       f_ptr->state[i].result = -offset;
+               }
        }
 
        /* Oops */
@@ -1253,52 +1738,99 @@ errr parse_f_info(char *buf, header *head)
 
 
 /*
- * Grab one flag in an object_kind from a textual string
+ * Convert a fake tag to a real feat index
  */
-static errr grab_one_kind_flag(object_kind *k_ptr, cptr what)
+s16b f_tag_to_index(cptr str)
 {
-       int i;
+       u16b i;
 
-       /* Check flags1 */
-       for (i = 0; i < 32; i++)
+       /* Search for real index corresponding to this fake tag */
+       for (i = 0; i < f_head.info_num; i++)
        {
-               if (streq(what, k_info_flags1[i]))
+               if (streq(f_tag + f_info[i].tag, str))
                {
-                       k_ptr->flags1 |= (1L << i);
-                       return (0);
+                       /* Return the index */
+                       return (s16b)i;
                }
        }
 
-       /* Check flags2 */
-       for (i = 0; i < 32; i++)
+       /* Not found */
+       return -1;
+}
+
+
+/*
+ * Search for real index corresponding to this fake tag
+ */
+static void search_real_feat(s16b *feat)
+{
+       int i;
+
+       /* Don't convert non-fake tag */
+       if (*feat >= 0) return;
+
+       /* Search for real index corresponding to this fake tag */
+       for (i = 0; i < f_head.info_num; i++)
        {
-               if (streq(what, k_info_flags2[i]))
+               if ((-(*feat)) == f_info[i].tag)
                {
-                       k_ptr->flags2 |= (1L << i);
-                       return (0);
+                       /* Record real index */
+                       *feat = (s16b)i;
+                       return;
                }
        }
 
-       /* Check flags3 */
-       for (i = 0; i < 32; i++)
+       /* Undefined tag */
+#ifdef JP
+       msg_format("̤ÄêµÁ¤Î¥¿¥° '%s'¡£", f_tag + (-(*feat)));
+#else
+       msg_format("%s is undefined.", f_tag + (-(*feat)));
+#endif
+}
+
+
+/*
+ * Retouch fake tags of f_info
+ */
+void retouch_f_info(header *head)
+{
+       int i;
+
+       /* Convert fake tags to real feat indices */
+       for (i = 0; i < head->info_num; i++)
        {
-               if (streq(what, k_info_flags3[i]))
-               {
-                       k_ptr->flags3 |= (1L << i);
-                       return (0);
-               }
+               feature_type *f_ptr = &f_info[i];
+               int j;
+
+               search_real_feat(&f_ptr->mimic);
+
+               search_real_feat(&f_ptr->destroyed);
+
+               for (j = 0; j < MAX_FEAT_STATES; j++) search_real_feat(&f_ptr->state[j].result);
        }
+}
 
-       /* Check gen_flags */
-       for (i = 0; i < 32; i++)
+
+/*
+ * Grab one flag in an object_kind from a textual string
+ */
+static errr grab_one_kind_flag(object_kind *k_ptr, cptr what)
+{
+       int i;
+
+       /* Check flags */
+       for (i = 0; i < TR_FLAG_MAX; i++)
        {
-               if (streq(what, k_info_gen_flags[i]))
+               if (streq(what, k_info_flags[i]))
                {
-                       k_ptr->gen_flags |= (1L << i);
+                       add_flag(k_ptr->flags, i);
                        return (0);
                }
        }
 
+       if (grab_one_flag(&k_ptr->gen_flags, k_info_gen_flags, what) == 0)
+               return 0;
+
        /* Oops */
 #ifdef JP
        msg_format("̤ÃΤΥ¢¥¤¥Æ¥à¡¦¥Õ¥é¥° '%s'¡£", what);
@@ -1328,19 +1860,19 @@ errr parse_k_info(char *buf, header *head)
        /* Process 'N' for "New/Number/Name" */
        if (buf[0] == 'N')
        {
+#ifdef JP
+               char *flavor;
+#endif
+
                /* Find the colon before the name */
-               s = strchr(buf+2, ':');
+               s = my_strchr(buf+2, ':');
 
-                       /* Verify that colon */
+               /* Verify that colon */
                if (!s) return (1);
 
                /* Nuke the colon, advance to the name */
                *s++ = '\0';
 
-#ifdef JP
-               /* Paranoia -- require a name */
-               if (!*s) return (1);
-#endif
                /* Get the index */
                i = atoi(buf+2);
 
@@ -1357,6 +1889,22 @@ errr parse_k_info(char *buf, header *head)
                k_ptr = &k_info[i];
 
 #ifdef JP
+               /* Paranoia -- require a name */
+               if (!*s) return (1);
+
+               /* Find the colon before the flavor */
+               flavor = my_strchr(s, ':');
+
+               /* Verify that colon */
+               if (flavor)
+               {
+                       /* Nuke the colon, advance to the flavor */
+                       *flavor++ = '\0';
+
+                       /* Store the flavor */
+                       if (!add_name(&k_ptr->flavor_name, head, flavor)) return (7);
+               }
+
                /* Store the name */
                if (!add_name(&k_ptr->name, head, s)) return (7);
 #endif
@@ -1376,37 +1924,58 @@ errr parse_k_info(char *buf, header *head)
 #else
        else if (buf[0] == 'E')
        {
-               /* Acquire the Text */
+               char *flavor;
+
+               /* Acquire the name */
                s = buf+2;
 
+               /* Find the colon before the flavor */
+               flavor = my_strchr(s, ':');
+
+               /* Verify that colon */
+               if (flavor)
+               {
+                       /* Nuke the colon, advance to the flavor */
+                       *flavor++ = '\0';
+
+                       /* Store the flavor */
+                       if (!add_name(&k_ptr->flavor_name, head, flavor)) return (7);
+               }
+
                /* Store the name */
                if (!add_name(&k_ptr->name, head, s)) return (7);
        }
 #endif
-#if 0
 
        /* Process 'D' for "Description" */
        else if (buf[0] == 'D')
        {
+#ifdef JP
+               if (buf[2] == '$')
+                       return (0);
                /* Acquire the text */
                s = buf+2;
+#else
+               if (buf[2] != '$')
+                       return (0);
+               /* Acquire the text */
+               s = buf+3;
+#endif
 
                /* Store the text */
-               if (!add_text(&k_ptr->text, head, s)) return (7);
+               if (!add_text(&k_ptr->text, head, s, TRUE)) return (7);
        }
 
-#endif
-
-
        /* Process 'G' for "Graphics" (one line only) */
        else if (buf[0] == 'G')
        {
                char sym;
-               int tmp;
+               byte tmp;
 
                /* Paranoia */
+               if (buf[1] != ':') return (1);
                if (!buf[2]) return (1);
-               if (!buf[3]) return (1);
+               if (buf[3] != ':') return (1);
                if (!buf[4]) return (1);
 
                /* Extract the char */
@@ -1416,7 +1985,7 @@ errr parse_k_info(char *buf, header *head)
                tmp = color_char_to_attr(buf[4]);
 
                /* Paranoia */
-               if (tmp < 0) return (1);
+               if (tmp > 127) return (1);
 
                /* Save the values */
                k_ptr->d_attr = tmp;
@@ -1470,10 +2039,10 @@ errr parse_k_info(char *buf, header *head)
                        k_ptr->locale[i] = atoi(s+1);
 
                                /* Find the slash */
-                       t = strchr(s+1, '/');
+                       t = my_strchr(s+1, '/');
 
                                /* Find the next colon */
-                       s = strchr(s+1, ':');
+                       s = my_strchr(s+1, ':');
 
                                /* If the slash is "nearby", use it */
                        if (t && (!s || t < s))
@@ -1539,48 +2108,21 @@ errr parse_k_info(char *buf, header *head)
  * Grab one flag in an artifact_type from a textual string
  */
 static errr grab_one_artifact_flag(artifact_type *a_ptr, cptr what)
-{
-       int i;
-
-       /* Check flags1 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, k_info_flags1[i]))
-               {
-                       a_ptr->flags1 |= (1L << i);
-                       return (0);
-               }
-       }
-
-       /* Check flags2 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, k_info_flags2[i]))
-               {
-                       a_ptr->flags2 |= (1L << i);
-                       return (0);
-               }
-       }
+{
+       int i;
 
-       /* Check flags3 */
-       for (i = 0; i < 32; i++)
+       /* Check flags */
+       for (i = 0; i < TR_FLAG_MAX; i++)
        {
-               if (streq(what, k_info_flags3[i]))
+               if (streq(what, k_info_flags[i]))
                {
-                       a_ptr->flags3 |= (1L << i);
+                       add_flag(a_ptr->flags, i);
                        return (0);
                }
        }
 
-       /* Check gen_flags */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, k_info_gen_flags[i]))
-               {
-                       a_ptr->gen_flags |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&a_ptr->gen_flags, k_info_gen_flags, what) == 0)
+               return 0;
 
        /* Oops */
 #ifdef JP
@@ -1614,7 +2156,7 @@ errr parse_a_info(char *buf, header *head)
        if (buf[0] == 'N')
        {
                /* Find the colon before the name */
-               s = strchr(buf+2, ':');
+               s = my_strchr(buf+2, ':');
 
                        /* Verify that colon */
                if (!s) return (1);
@@ -1641,10 +2183,10 @@ errr parse_a_info(char *buf, header *head)
                a_ptr = &a_info[i];
 
                /* Ignore everything */
-               a_ptr->flags3 |= (TR3_IGNORE_ACID);
-               a_ptr->flags3 |= (TR3_IGNORE_ELEC);
-               a_ptr->flags3 |= (TR3_IGNORE_FIRE);
-               a_ptr->flags3 |= (TR3_IGNORE_COLD);
+               add_flag(a_ptr->flags, TR_IGNORE_ACID);
+               add_flag(a_ptr->flags, TR_IGNORE_ELEC);
+               add_flag(a_ptr->flags, TR_IGNORE_FIRE);
+               add_flag(a_ptr->flags, TR_IGNORE_COLD);
 #ifdef JP
                /* Store the name */
                if (!add_name(&a_ptr->name, head, s)) return (7);
@@ -1689,7 +2231,7 @@ errr parse_a_info(char *buf, header *head)
 #endif
 
                /* Store the text */
-               if (!add_text(&a_ptr->text, head, s)) return (7);
+               if (!add_text(&a_ptr->text, head, s, TRUE)) return (7);
        }
 
 
@@ -1783,47 +2325,20 @@ static bool grab_one_ego_item_flag(ego_item_type *e_ptr, cptr what)
 {
        int i;
 
-       /* Check flags1 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, k_info_flags1[i]))
-               {
-                       e_ptr->flags1 |= (1L << i);
-                       return (0);
-               }
-       }
-
-       /* Check flags2 */
-       for (i = 0; i < 32; i++)
+       /* Check flags */
+       for (i = 0; i < TR_FLAG_MAX; i++)
        {
-               if (streq(what, k_info_flags2[i]))
+               if (streq(what, k_info_flags[i]))
                {
-                       e_ptr->flags2 |= (1L << i);
+                       add_flag(e_ptr->flags, i);
                        return (0);
                }
        }
 
-       /* Check flags3 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, k_info_flags3[i]))
-               {
-                       e_ptr->flags3 |= (1L << i);
-                       return (0);
-               }
-       }
-       
-       /* Check gen_flags */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, k_info_gen_flags[i]))
-               {
-                       e_ptr->gen_flags |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&e_ptr->gen_flags, k_info_gen_flags, what) == 0)
+               return 0;
 
-/* Oops */
+       /* Oops */
 #ifdef JP
        msg_format("̤ÃΤÎ̾¤Î¤¢¤ë¥¢¥¤¥Æ¥à¡¦¥Õ¥é¥° '%s'¡£", what);
 #else
@@ -1862,7 +2377,7 @@ errr parse_e_info(char *buf, header *head)
        if (buf[0] == 'N')
        {
                /* Find the colon before the name */
-               s = strchr(buf+2, ':');
+               s = my_strchr(buf+2, ':');
 
                        /* Verify that colon */
                if (!s) return (1);
@@ -1923,7 +2438,7 @@ errr parse_e_info(char *buf, header *head)
                s = buf+2;
 
                /* Store the text */
-               if (!add_text(&e_ptr->text, head, s)) return (7);
+               if (!add_text(&e_ptr->text, head, s, TRUE)) return (7);
        }
 
 #endif
@@ -2011,67 +2526,26 @@ errr parse_e_info(char *buf, header *head)
  */
 static errr grab_one_basic_flag(monster_race *r_ptr, cptr what)
 {
-       int i;
+       if (grab_one_flag(&r_ptr->flags1, r_info_flags1, what) == 0)
+               return 0;
 
-       /* Scan flags1 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, r_info_flags1[i]))
-               {
-                       r_ptr->flags1 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&r_ptr->flags2, r_info_flags2, what) == 0)
+               return 0;
 
-       /* Scan flags2 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, r_info_flags2[i]))
-               {
-                       r_ptr->flags2 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&r_ptr->flags3, r_info_flags3, what) == 0)
+               return 0;
 
-       /* Scan flags3 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, r_info_flags3[i]))
-               {
-                       r_ptr->flags3 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&r_ptr->flags7, r_info_flags7, what) == 0)
+               return 0;
 
-       /* Scan flags7 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, r_info_flags7[i]))
-               {
-                       r_ptr->flags7 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&r_ptr->flags8, r_info_flags8, what) == 0)
+               return 0;
 
-       /* Scan flags8 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, r_info_flags8[i]))
-               {
-                       r_ptr->flags8 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&r_ptr->flags9, r_info_flags9, what) == 0)
+               return 0;
 
-       /* Scan flags9 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, r_info_flags9[i]))
-               {
-                       r_ptr->flags9 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&r_ptr->flagsr, r_info_flagsr, what) == 0)
+               return 0;
 
        /* Oops */
 #ifdef JP
@@ -2091,37 +2565,14 @@ static errr grab_one_basic_flag(monster_race *r_ptr, cptr what)
  */
 static errr grab_one_spell_flag(monster_race *r_ptr, cptr what)
 {
-       int i;
-
-       /* Scan flags4 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, r_info_flags4[i]))
-               {
-                       r_ptr->flags4 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&r_ptr->flags4, r_info_flags4, what) == 0)
+               return 0;
 
-       /* Scan flags5 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, r_info_flags5[i]))
-               {
-                       r_ptr->flags5 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&r_ptr->flags5, r_info_flags5, what) == 0)
+               return 0;
 
-       /* Scan flags6 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, r_info_flags6[i]))
-               {
-                       r_ptr->flags6 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&r_ptr->flags6, r_info_flags6, what) == 0)
+               return 0;
 
        /* Oops */
 #ifdef JP
@@ -2155,7 +2606,7 @@ errr parse_r_info(char *buf, header *head)
        if (buf[0] == 'N')
        {
                /* Find the colon before the name */
-               s = strchr(buf+2, ':');
+               s = my_strchr(buf+2, ':');
 
                        /* Verify that colon */
                if (!s) return (1);
@@ -2227,18 +2678,19 @@ errr parse_r_info(char *buf, header *head)
 #endif
 
                /* Store the text */
-               if (!add_text(&r_ptr->text, head, s)) return (7);
+               if (!add_text(&r_ptr->text, head, s, TRUE)) return (7);
        }
 
        /* Process 'G' for "Graphics" (one line only) */
        else if (buf[0] == 'G')
        {
                char sym;
-               int tmp;
+               byte tmp;
 
                /* Paranoia */
+               if (buf[1] != ':') return (1);
                if (!buf[2]) return (1);
-               if (!buf[3]) return (1);
+               if (buf[3] != ':') return (1);
                if (!buf[4]) return (1);
 
                /* Extract the char */
@@ -2248,7 +2700,7 @@ errr parse_r_info(char *buf, header *head)
                tmp = color_char_to_attr(buf[4]);
 
                /* Paranoia */
-               if (tmp < 0) return (1);
+               if (tmp > 127) return (1);
 
                /* Save the values */
                r_ptr->d_char = sym;
@@ -2266,8 +2718,8 @@ errr parse_r_info(char *buf, header *head)
 
                /* Save the values */
                r_ptr->speed = spd;
-               r_ptr->hdice = hp1;
-               r_ptr->hside = hp2;
+               r_ptr->hdice = MAX(hp1, 1);
+               r_ptr->hside = MAX(hp2, 1);
                r_ptr->aaf = aaf;
                r_ptr->ac = ac;
                r_ptr->sleep = slp;
@@ -2396,7 +2848,7 @@ errr parse_r_info(char *buf, header *head)
                        if (1 == sscanf(s, "1_IN_%d", &i))
                        {
                                /* Extract a "frequency" */
-                               r_ptr->freq_spell = r_ptr->freq_inate = 100 / i;
+                               r_ptr->freq_spell = 100 / i;
 
                                        /* Start at next entry */
                                s = t;
@@ -2427,23 +2879,14 @@ errr parse_r_info(char *buf, header *head)
  */
 static errr grab_one_dungeon_flag(dungeon_info_type *d_ptr, cptr what)
 {
-       int i;
-
-       /* Scan flags1 */
-       for (i = 0; i < 32; i++)
-       {
-                if (streq(what, d_info_flags1[i]))
-               {
-                        d_ptr->flags1 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&d_ptr->flags1, d_info_flags1, what) == 0)
+               return 0;
 
        /* Oops */
 #ifdef JP
-        msg_format("̤ÃΤΥÀ¥ó¥¸¥ç¥ó¡¦¥Õ¥é¥° '%s'¡£", what);
+       msg_format("̤ÃΤΥÀ¥ó¥¸¥ç¥ó¡¦¥Õ¥é¥° '%s'¡£", what);
 #else
-        msg_format("Unknown dungeon type flag '%s'.", what);
+       msg_format("Unknown dungeon type flag '%s'.", what);
 #endif
 
        /* Failure */
@@ -2455,67 +2898,26 @@ static errr grab_one_dungeon_flag(dungeon_info_type *d_ptr, cptr what)
  */
 static errr grab_one_basic_monster_flag(dungeon_info_type *d_ptr, cptr what)
 {
-       int i;
+       if (grab_one_flag(&d_ptr->mflags1, r_info_flags1, what) == 0)
+               return 0;
 
-       /* Scan flags1 */
-       for (i = 0; i < 32; i++)
-       {
-                if (streq(what, r_info_flags1[i]))
-               {
-                        d_ptr->mflags1 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&d_ptr->mflags2, r_info_flags2, what) == 0)
+               return 0;
 
-       /* Scan flags2 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, r_info_flags2[i]))
-               {
-                        d_ptr->mflags2 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&d_ptr->mflags3, r_info_flags3, what) == 0)
+               return 0;
 
-       /* Scan flags3 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, r_info_flags3[i]))
-               {
-                        d_ptr->mflags3 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&d_ptr->mflags7, r_info_flags7, what) == 0)
+               return 0;
 
-       /* Scan flags7 */
-       for (i = 0; i < 32; i++)
-       {
-                if (streq(what, r_info_flags7[i]))
-               {
-                        d_ptr->mflags7 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&d_ptr->mflags8, r_info_flags8, what) == 0)
+               return 0;
 
-       /* Scan flags8 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, r_info_flags8[i]))
-               {
-                        d_ptr->mflags8 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&d_ptr->mflags9, r_info_flags9, what) == 0)
+               return 0;
 
-       /* Scan flags9 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, r_info_flags9[i]))
-               {
-                        d_ptr->mflags9 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&d_ptr->mflagsr, r_info_flagsr, what) == 0)
+               return 0;
 
        /* Oops */
 #ifdef JP
@@ -2533,37 +2935,14 @@ static errr grab_one_basic_monster_flag(dungeon_info_type *d_ptr, cptr what)
  */
 static errr grab_one_spell_monster_flag(dungeon_info_type *d_ptr, cptr what)
 {
-       int i;
-
-       /* Scan flags4 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, r_info_flags4[i]))
-               {
-                        d_ptr->mflags4 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&d_ptr->mflags4, r_info_flags4, what) == 0)
+               return 0;
 
-       /* Scan flags5 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, r_info_flags5[i]))
-               {
-                        d_ptr->mflags5 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&d_ptr->mflags5, r_info_flags5, what) == 0)
+               return 0;
 
-       /* Scan flags6 */
-       for (i = 0; i < 32; i++)
-       {
-               if (streq(what, r_info_flags6[i]))
-               {
-                        d_ptr->mflags6 |= (1L << i);
-                       return (0);
-               }
-       }
+       if (grab_one_flag(&d_ptr->mflags6, r_info_flags6, what) == 0)
+               return 0;
 
        /* Oops */
 #ifdef JP
@@ -2593,7 +2972,7 @@ errr parse_d_info(char *buf, header *head)
        if (buf[0] == 'N')
        {
                /* Find the colon before the name */
-               s = strchr(buf+2, ':');
+               s = my_strchr(buf+2, ':');
 
                /* Verify that colon */
                if (!s) return (1);
@@ -2653,7 +3032,7 @@ errr parse_d_info(char *buf, header *head)
 #endif
 
                /* Store the text */
-               if (!add_text(&d_ptr->text, head, s)) return (7);
+               if (!add_text(&d_ptr->text, head, s, TRUE)) return (7);
        }
 
        /* Process 'W' for "More Info" (one line only) */
@@ -2698,45 +3077,50 @@ errr parse_d_info(char *buf, header *head)
        /* Process 'L' for "fLoor type" (one line only) */
        else if (buf[0] == 'L')
        {
-               int f1, f2, f3;
-               int p1, p2, p3;
-               int tunnel;
+               char *zz[16];
 
                /* Scan for the values */
-               if (7 != sscanf(buf+2, "%d:%d:%d:%d:%d:%d:%d",
-                                &f1, &p1, &f2, &p2, &f3, &p3, &tunnel)) return (1);
+               if (tokenize(buf+2, DUNGEON_FEAT_PROB_NUM * 2 + 1, zz, 0) != (DUNGEON_FEAT_PROB_NUM * 2 + 1)) return (1);
 
                /* Save the values */
-               d_ptr->floor1 = f1;
-               d_ptr->floor_percent1 = p1;
-               d_ptr->floor2 = f2;
-               d_ptr->floor_percent2 = p2;
-               d_ptr->floor3 = f3;
-               d_ptr->floor_percent3 = p3;
-               d_ptr->tunnel_percent = tunnel;
+               for (i = 0; i < DUNGEON_FEAT_PROB_NUM; i++)
+               {
+                       d_ptr->floor[i].feat = f_tag_to_index(zz[i * 2]);
+                       if (d_ptr->floor[i].feat < 0) return PARSE_ERROR_UNDEFINED_TERRAIN_TAG;
+
+                       d_ptr->floor[i].percent = atoi(zz[i * 2 + 1]);
+               }
+               d_ptr->tunnel_percent = atoi(zz[DUNGEON_FEAT_PROB_NUM * 2]);
        }
 
        /* Process 'A' for "wAll type" (one line only) */
        else if (buf[0] == 'A')
        {
-               int w1, w2, w3, outer, inner, stream1, stream2;
-               int p1, p2, p3;
+               char *zz[16];
 
                /* Scan for the values */
-               if (10 != sscanf(buf+2, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
-                                &w1, &p1, &w2, &p2, &w3, &p3, &outer, &inner, &stream1, &stream2)) return (1);
+               if (tokenize(buf+2, DUNGEON_FEAT_PROB_NUM * 2 + 4, zz, 0) != (DUNGEON_FEAT_PROB_NUM * 2 + 4)) return (1);
 
                /* Save the values */
-               d_ptr->fill_type1 = w1;
-               d_ptr->fill_percent1 = p1;
-               d_ptr->fill_type2 = w2;
-               d_ptr->fill_percent2 = p2;
-               d_ptr->fill_type3 = w3;
-               d_ptr->fill_percent3 = p3;
-               d_ptr->outer_wall = outer;
-               d_ptr->inner_wall = inner;
-               d_ptr->stream1 = stream1;
-               d_ptr->stream2 = stream2;
+               for (i = 0; i < DUNGEON_FEAT_PROB_NUM; i++)
+               {
+                       d_ptr->fill[i].feat = f_tag_to_index(zz[i * 2]);
+                       if (d_ptr->fill[i].feat < 0) return PARSE_ERROR_UNDEFINED_TERRAIN_TAG;
+
+                       d_ptr->fill[i].percent = atoi(zz[i * 2 + 1]);
+               }
+
+               d_ptr->outer_wall = f_tag_to_index(zz[DUNGEON_FEAT_PROB_NUM * 2]);
+               if (d_ptr->outer_wall < 0) return PARSE_ERROR_UNDEFINED_TERRAIN_TAG;
+
+               d_ptr->inner_wall = f_tag_to_index(zz[DUNGEON_FEAT_PROB_NUM * 2 + 1]);
+               if (d_ptr->inner_wall < 0) return PARSE_ERROR_UNDEFINED_TERRAIN_TAG;
+
+               d_ptr->stream1 = f_tag_to_index(zz[DUNGEON_FEAT_PROB_NUM * 2 + 2]);
+               if (d_ptr->stream1 < 0) return PARSE_ERROR_UNDEFINED_TERRAIN_TAG;
+
+               d_ptr->stream2 = f_tag_to_index(zz[DUNGEON_FEAT_PROB_NUM * 2 + 3]);
+               if (d_ptr->stream2 < 0) return PARSE_ERROR_UNDEFINED_TERRAIN_TAG;
        }
 
        /* Process 'F' for "Dungeon Flags" (multiple lines) */
@@ -2747,17 +3131,17 @@ errr parse_d_info(char *buf, header *head)
                /* Parse every entry */
                for (s = buf + 2; *s; )
                {
-                               /* Find the end of this entry */
+                       /* Find the end of this entry */
                        for (t = s; *t && (*t != ' ') && (*t != '|'); ++t) /* loop */;
 
-                               /* Nuke and skip any dividers */
+                       /* Nuke and skip any dividers */
                        if (*t)
                        {
                                *t++ = '\0';
                                while (*t == ' ' || *t == '|') t++;
                        }
 
-                                /* XXX XXX XXX Hack -- Read Final Artifact */
+                       /* XXX XXX XXX Hack -- Read Final Artifact */
                        if (1 == sscanf(s, "FINAL_ARTIFACT_%d", &artif))
                        {
                                /* Extract a "Final Artifact" */
@@ -2770,7 +3154,7 @@ errr parse_d_info(char *buf, header *head)
                                continue;
                        }
 
-                                /* XXX XXX XXX Hack -- Read Final Object */
+                       /* XXX XXX XXX Hack -- Read Final Object */
                        if (1 == sscanf(s, "FINAL_OBJECT_%d", &artif))
                        {
                                /* Extract a "Final Artifact" */
@@ -2783,7 +3167,7 @@ errr parse_d_info(char *buf, header *head)
                                continue;
                        }
 
-                                /* XXX XXX XXX Hack -- Read Artifact Guardian */
+                       /* XXX XXX XXX Hack -- Read Artifact Guardian */
                        if (1 == sscanf(s, "FINAL_GUARDIAN_%d", &monst))
                        {
                                /* Extract a "Artifact Guardian" */
@@ -2796,7 +3180,7 @@ errr parse_d_info(char *buf, header *head)
                                continue;
                        }
 
-                                /* XXX XXX XXX Hack -- Read Special Percentage */
+                       /* XXX XXX XXX Hack -- Read Special Percentage */
                        if (1 == sscanf(s, "MONSTER_DIV_%d", &monst))
                        {
                                /* Extract a "Special %" */
@@ -2809,10 +3193,10 @@ errr parse_d_info(char *buf, header *head)
                                continue;
                        }
 
-                               /* Parse this entry */
+                       /* Parse this entry */
                        if (0 != grab_one_dungeon_flag(d_ptr, s)) return (5);
 
-                               /* Start the next entry */
+                       /* Start the next entry */
                        s = t;
                }
        }
@@ -2820,29 +3204,27 @@ errr parse_d_info(char *buf, header *head)
        /* Process 'M' for "Basic Flags" (multiple lines) */
        else if (buf[0] == 'M')
        {
-               byte r_char_number = 0, r_char;
-
                /* Parse every entry */
                for (s = buf + 2; *s; )
                {
-                               /* Find the end of this entry */
+                       /* Find the end of this entry */
                        for (t = s; *t && (*t != ' ') && (*t != '|'); ++t) /* loop */;
 
-                               /* Nuke and skip any dividers */
+                       /* Nuke and skip any dividers */
                        if (*t)
                        {
                                *t++ = '\0';
                                while (*t == ' ' || *t == '|') t++;
                        }
 
-                                /* XXX XXX XXX Hack -- Read monster symbols */
-                       if (1 == sscanf(s, "R_CHAR_%c", &r_char))
+                       /* Hack -- Read monster symbols */
+                       if (!strncmp(s, "R_CHAR_", 7))
                        {
-                               /* Limited to 5 races */
-                               if(r_char_number >= 5) continue;
+                               /* Skip "R_CHAR_" */
+                               s += 7;
 
-                               /* Extract a "frequency" */
-                               d_ptr->r_char[r_char_number++] = r_char;
+                               /* Read a string */
+                               strncpy(d_ptr->r_char, s, sizeof(d_ptr->r_char));
 
                                /* Start at next entry */
                                s = t;
@@ -2851,10 +3233,10 @@ errr parse_d_info(char *buf, header *head)
                                continue;
                        }
 
-                               /* Parse this entry */
+                       /* Parse this entry */
                        if (0 != grab_one_basic_monster_flag(d_ptr, s)) return (5);
 
-                               /* Start the next entry */
+                       /* Start the next entry */
                        s = t;
                }
        }
@@ -2911,13 +3293,13 @@ static int i = 0;
 
 
 /* Random dungeon grid effects */
-#define RANDOM_NONE         0x00
-#define RANDOM_FEATURE      0x01
-#define RANDOM_MONSTER      0x02
-#define RANDOM_OBJECT       0x04
-#define RANDOM_EGO          0x08
-#define RANDOM_ARTIFACT     0x10
-#define RANDOM_TRAP         0x20
+#define RANDOM_NONE         0x00000000
+#define RANDOM_FEATURE      0x00000001
+#define RANDOM_MONSTER      0x00000002
+#define RANDOM_OBJECT       0x00000004
+#define RANDOM_EGO          0x00000008
+#define RANDOM_ARTIFACT     0x00000010
+#define RANDOM_TRAP         0x00000020
 
 
 typedef struct dungeon_grid dungeon_grid;
@@ -2957,15 +3339,15 @@ static errr parse_line_feature(char *buf)
                int index = zz[0][0];
 
                /* Reset the info for the letter */
-               letter[index].feature = 0;
+               letter[index].feature = feat_none;
                letter[index].monster = 0;
                letter[index].object = 0;
                letter[index].ego = 0;
                letter[index].artifact = 0;
-               letter[index].trap = 0;
+               letter[index].trap = feat_none;
                letter[index].cave_info = 0;
                letter[index].special = 0;
-               letter[index].random = 0;
+               letter[index].random = RANDOM_NONE;
 
                switch (num)
                {
@@ -2975,19 +3357,14 @@ static errr parse_line_feature(char *buf)
                                /* Fall through */
                        /* Trap */
                        case 8:
-                               if (zz[7][0] == '*')
+                               if ((zz[7][0] == '*') && !zz[7][1])
                                {
                                        letter[index].random |= RANDOM_TRAP;
-
-                                       if (zz[7][1])
-                                       {
-                                               zz[7]++;
-                                               letter[index].trap = atoi(zz[7]);
-                                       }
                                }
                                else
                                {
-                                       letter[index].trap = atoi(zz[7]);
+                                       letter[index].trap = f_tag_to_index(zz[7]);
+                                       if (letter[index].trap < 0) return PARSE_ERROR_UNDEFINED_TERRAIN_TAG;
                                }
                                /* Fall through */
                        /* Artifact */
@@ -2995,12 +3372,7 @@ static errr parse_line_feature(char *buf)
                                if (zz[6][0] == '*')
                                {
                                        letter[index].random |= RANDOM_ARTIFACT;
-
-                                       if (zz[6][1])
-                                       {
-                                               zz[6]++;
-                                               letter[index].artifact = atoi(zz[6]);
-                                       }
+                                       if (zz[6][1]) letter[index].artifact = atoi(zz[6] + 1);
                                }
                                else
                                {
@@ -3012,12 +3384,7 @@ static errr parse_line_feature(char *buf)
                                if (zz[5][0] == '*')
                                {
                                        letter[index].random |= RANDOM_EGO;
-
-                                       if (zz[5][1])
-                                       {
-                                               zz[5]++;
-                                               letter[index].ego = atoi(zz[5]);
-                                       }
+                                       if (zz[5][1]) letter[index].ego = atoi(zz[5] + 1);
                                }
                                else
                                {
@@ -3029,12 +3396,7 @@ static errr parse_line_feature(char *buf)
                                if (zz[4][0] == '*')
                                {
                                        letter[index].random |= RANDOM_OBJECT;
-
-                                       if (zz[4][1])
-                                       {
-                                               zz[4]++;
-                                               letter[index].object = atoi(zz[4]);
-                                       }
+                                       if (zz[4][1]) letter[index].object = atoi(zz[4] + 1);
                                }
                                else
                                {
@@ -3046,15 +3408,12 @@ static errr parse_line_feature(char *buf)
                                if (zz[3][0] == '*')
                                {
                                        letter[index].random |= RANDOM_MONSTER;
-                                       if (zz[3][1])
-                                       {
-                                               zz[3]++;
-                                               letter[index].monster = atoi(zz[3]);
-                                       }
+                                       if (zz[3][1]) letter[index].monster = atoi(zz[3] + 1);
                                }
                                else if (zz[3][0] == 'c')
                                {
-                                       letter[index].monster = - atoi(zz[3]+1);
+                                       if (!zz[3][1]) return PARSE_ERROR_GENERIC;
+                                       letter[index].monster = - atoi(zz[3] + 1);
                                }
                                else
                                {
@@ -3067,18 +3426,14 @@ static errr parse_line_feature(char *buf)
                                /* Fall through */
                        /* Feature */
                        case 2:
-                               if (zz[1][0] == '*')
+                               if ((zz[1][0] == '*') && !zz[1][1])
                                {
                                        letter[index].random |= RANDOM_FEATURE;
-                                       if (zz[1][1])
-                                       {
-                                               zz[1]++;
-                                               letter[index].feature = atoi(zz[1]);
-                                       }
                                }
                                else
                                {
-                                       letter[index].feature = atoi(zz[1]);
+                                       letter[index].feature = f_tag_to_index(zz[1]);
+                                       if (letter[index].feature < 0) return PARSE_ERROR_UNDEFINED_TERRAIN_TAG;
                                }
                                break;
                }
@@ -3091,7 +3446,7 @@ static errr parse_line_feature(char *buf)
 
 
 /*
- * Process "F:<letter>:<terrain>:<cave_info>:<monster>:<object>:<ego>:<artifact>:<trap>:<special>" -- info for dungeon grid
+ * Process "B:<Index>:<Command>:..." -- Building definition
  */
 static errr parse_line_building(char *buf)
 {
@@ -3113,7 +3468,7 @@ static errr parse_line_building(char *buf)
        index = atoi(s);
 
        /* Find the colon after the building number */
-       s = strchr(s, ':');
+       s = my_strchr(s, ':');
 
        /* Verify that colon */
        if (!s) return (1);
@@ -3244,6 +3599,39 @@ static errr parse_line_building(char *buf)
 
 
 /*
+ * Place the object j_ptr to a grid
+ */
+static void drop_here(object_type *j_ptr, int y, int x)
+{
+       cave_type *c_ptr = &cave[y][x];
+       object_type *o_ptr;
+
+       /* Get new object */
+       s16b o_idx = o_pop();
+
+       /* Access new object */
+       o_ptr = &o_list[o_idx];
+
+       /* Structure copy */
+       object_copy(o_ptr, j_ptr);
+
+
+       /* Locate */
+       o_ptr->iy = y;
+       o_ptr->ix = x;
+
+       /* No monster */
+       o_ptr->held_m_idx = 0;
+
+       /* Build a stack */
+       o_ptr->next_o_idx = c_ptr->o_idx;
+
+       /* Place the object */
+       c_ptr->o_idx = o_idx;
+}
+
+
+/*
  * Parse a sub-file of the "extra info"
  */
 static errr process_dungeon_file_aux(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
@@ -3305,7 +3693,7 @@ static errr process_dungeon_file_aux(char *buf, int ymin, int xmin, int ymax, in
                        int artifact_index = letter[idx].artifact;
 
                        /* Lay down a floor */
-                       c_ptr->feat = letter[idx].feature;
+                       c_ptr->feat = conv_dungeon_feat(letter[idx].feature);
 
                        /* Only the features */
                        if (init_flags & INIT_ONLY_FEATURES) continue;
@@ -3318,7 +3706,7 @@ static errr process_dungeon_file_aux(char *buf, int ymin, int xmin, int ymax, in
                        {
                                monster_level = base_level + monster_index;
 
-                               place_monster(*y, *x, TRUE, TRUE);
+                               place_monster(*y, *x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
 
                                monster_level = base_level;
                        }
@@ -3343,7 +3731,8 @@ static errr process_dungeon_file_aux(char *buf, int ymin, int xmin, int ymax, in
                                }
 
                                /* Make alive again */
-                               if (r_info[monster_index].flags7 & RF7_UNIQUE_7)
+                               /* Hack -- Non-unique Nazguls are semi-unique */
+                               else if (r_info[monster_index].flags7 & RF7_NAZGUL)
                                {
                                        if (r_info[monster_index].cur_num == r_info[monster_index].max_num)
                                        {
@@ -3352,7 +3741,7 @@ static errr process_dungeon_file_aux(char *buf, int ymin, int xmin, int ymax, in
                                }
 
                                /* Place it */
-                               place_monster_aux(*y, *x, monster_index, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE);
+                               place_monster_aux(0, *y, *x, monster_index, (PM_ALLOW_SLEEP | PM_NO_KAGE));
                                if (clone)
                                {
                                        /* clone */
@@ -3375,7 +3764,7 @@ static errr process_dungeon_file_aux(char *buf, int ymin, int xmin, int ymax, in
                                 */
                                if (randint0(100) < 75)
                                {
-                                       place_object(*y, *x, FALSE, FALSE);
+                                       place_object(*y, *x, 0L);
                                }
                                else
                                {
@@ -3390,11 +3779,11 @@ static errr process_dungeon_file_aux(char *buf, int ymin, int xmin, int ymax, in
 
                                /* Create an out of deep object */
                                if (randint0(100) < 75)
-                                       place_object(*y, *x, FALSE, FALSE);
+                                       place_object(*y, *x, 0L);
                                else if (randint0(100) < 80)
-                                       place_object(*y, *x, TRUE, FALSE);
+                                       place_object(*y, *x, AM_GOOD);
                                else
-                                       place_object(*y, *x, TRUE, TRUE);
+                                       place_object(*y, *x, AM_GOOD | AM_GREAT);
 
                                object_level = base_level;
                        }
@@ -3403,6 +3792,12 @@ static errr process_dungeon_file_aux(char *buf, int ymin, int xmin, int ymax, in
                        {
                                place_trap(*y, *x);
                        }
+                       /* Hidden trap (or door) */
+                       else if (letter[idx].trap)
+                       {
+                               c_ptr->mimic = c_ptr->feat;
+                               c_ptr->feat = conv_dungeon_feat(letter[idx].trap);
+                       }
                        else if (object_index)
                        {
                                /* Get local object */
@@ -3419,9 +3814,9 @@ static errr process_dungeon_file_aux(char *buf, int ymin, int xmin, int ymax, in
                                }
 
                                /* Apply magic (no messages, no artifacts) */
-                               apply_magic(o_ptr, base_level, FALSE, TRUE, FALSE, FALSE);
+                               apply_magic(o_ptr, base_level, AM_NO_FIXED_ART | AM_GOOD);
 
-                               (void)drop_near(o_ptr, -1, *y, *x);
+                               drop_here(o_ptr, *y, *x);
                        }
 
                        /* Artifact */
@@ -3429,21 +3824,20 @@ static errr process_dungeon_file_aux(char *buf, int ymin, int xmin, int ymax, in
                        {
                                if (a_info[artifact_index].cur_num)
                                {
-                                       int k_idx = 198;
+                                       int k_idx = lookup_kind(TV_SCROLL, SV_SCROLL_ACQUIREMENT);
                                        object_type forge;
                                        object_type *q_ptr = &forge;
 
                                        object_prep(q_ptr, k_idx);
 
                                        /* Drop it in the dungeon */
-                                       (void)drop_near(q_ptr, -1, *y, *x);
+                                       drop_here(q_ptr, *y, *x);
                                }
                                else
                                {
                                        /* Create the artifact */
-                                       create_named_art(artifact_index, *y, *x);
-
-                                       a_info[artifact_index].cur_num = 1;
+                                       if (create_named_art(artifact_index, *y, *x))
+                                               a_info[artifact_index].cur_num = 1;
                                }
                        }
 
@@ -3557,17 +3951,9 @@ static errr process_dungeon_file_aux(char *buf, int ymin, int xmin, int ymax, in
                                if (*x % SCREEN_WID) panels_x++;
                                cur_wid = panels_x * SCREEN_WID;
 
-                               /* Choose a panel row */
-                               max_panel_rows = (cur_hgt / SCREEN_HGT) * 2 - 2;
-                               if (max_panel_rows < 0) max_panel_rows = 0;
-
-                               /* Choose a panel col */
-                               max_panel_cols = (cur_wid / SCREEN_WID) * 2 - 2;
-                               if (max_panel_cols < 0) max_panel_cols = 0;
-
                                /* Assume illegal panel */
-                               panel_row = max_panel_rows;
-                               panel_col = max_panel_cols;
+                               panel_row_min = cur_hgt;
+                               panel_col_min = cur_wid;
 
                                /* Place player in a quest level */
                                if (p_ptr->inside_quest)
@@ -3654,10 +4040,10 @@ static errr process_dungeon_file_aux(char *buf, int ymin, int xmin, int ymax, in
                                max_e_idx = atoi(zz[1]);
                        }
 
-                        /* Maximum d_idx */
-                        else if (zz[0][0] == 'D')
+                       /* Maximum d_idx */
+                       else if (zz[0][0] == 'D')
                        {
-                                max_d_idx = atoi(zz[1]); 
+                               max_d_idx = atoi(zz[1]); 
                        }
 
                        /* Maximum o_idx */
@@ -3778,16 +4164,15 @@ static cptr process_dungeon_file_expr(char **sp, char *fp)
                /* Function: EQU */
                else if (streq(t, "EQU"))
                {
-                       v = "1";
+                       v = "0";
                        if (*s && (f != b2))
                        {
                                t = process_dungeon_file_expr(&s, &f);
                        }
                        while (*s && (f != b2))
                        {
-                               p = t;
-                               t = process_dungeon_file_expr(&s, &f);
-                               if (*t && !streq(p, t)) v = "0";
+                               p = process_dungeon_file_expr(&s, &f);
+                               if (streq(t, p)) v = "1";
                        }
                }
 
@@ -3803,7 +4188,7 @@ static cptr process_dungeon_file_expr(char **sp, char *fp)
                        {
                                p = t;
                                t = process_dungeon_file_expr(&s, &f);
-                               if (*t && (strcmp(p, t) > 0)) v = "0";
+                               if (*t && atoi(p) > atoi(t)) v = "0";
                        }
                }
 
@@ -3819,7 +4204,9 @@ static cptr process_dungeon_file_expr(char **sp, char *fp)
                        {
                                p = t;
                                t = process_dungeon_file_expr(&s, &f);
-                               if (*t && (strcmp(p, t) < 0)) v = "0";
+
+                               /* Compare two numbers instead of string */
+                               if (*t && atoi(p) < atoi(t)) v = "0";
                        }
                }
 
@@ -3843,7 +4230,15 @@ static cptr process_dungeon_file_expr(char **sp, char *fp)
        else
        {
                /* Accept all printables except spaces and brackets */
-               while (isprint(*s) && !strchr(" []", *s)) ++s;
+#ifdef JP
+               while (iskanji(*s) || (isprint(*s) && !my_strchr(" []", *s)))
+               {
+                       if (iskanji(*s)) s++;
+                       s++;
+               }
+#else
+               while (isprint(*s) && !my_strchr(" []", *s)) ++s;
+#endif
 
                /* Extract final and Terminate */
                if ((f = *s) != '\0') *s++ = '\0';
@@ -3875,7 +4270,7 @@ static cptr process_dungeon_file_expr(char **sp, char *fp)
                        else if (streq(b+1, "RACE"))
                        {
 #ifdef JP
-                                v = rp_ptr->E_title;
+                               v = rp_ptr->E_title;
 #else
                                v = rp_ptr->title;
 #endif
@@ -3885,7 +4280,7 @@ static cptr process_dungeon_file_expr(char **sp, char *fp)
                        else if (streq(b+1, "CLASS"))
                        {
 #ifdef JP
-                                v = cp_ptr->E_title;
+                               v = cp_ptr->E_title;
 #else
                                v = cp_ptr->title;
 #endif
@@ -3897,7 +4292,7 @@ static cptr process_dungeon_file_expr(char **sp, char *fp)
 #ifdef JP
                                v = E_realm_names[p_ptr->realm1];
 #else
-                                v = realm_names[p_ptr->realm1];
+                               v = realm_names[p_ptr->realm1];
 #endif
                        }
 
@@ -3905,7 +4300,7 @@ static cptr process_dungeon_file_expr(char **sp, char *fp)
                        else if (streq(b+1, "REALM2"))
                        {
 #ifdef JP
-                                v = E_realm_names[p_ptr->realm2];
+                               v = E_realm_names[p_ptr->realm2];
 #else
                                v = realm_names[p_ptr->realm2];
 #endif
@@ -3914,7 +4309,22 @@ static cptr process_dungeon_file_expr(char **sp, char *fp)
                        /* Player name */
                        else if (streq(b+1, "PLAYER"))
                        {
-                               v = player_base;
+                               static char tmp_player_name[32];
+                               char *pn, *tpn;
+                               for (pn = player_name, tpn = tmp_player_name; *pn; pn++, tpn++)
+                               {
+#ifdef JP
+                                       if (iskanji(*pn))
+                                       {
+                                               *(tpn++) = *(pn++);
+                                               *tpn = *pn;
+                                               continue;
+                                       }
+#endif
+                                       *tpn = my_strchr(" []", *pn) ? '_' : *pn;
+                               }
+                               *tpn = '\0';
+                               v = tmp_player_name;
                        }
 
                        /* Town */
@@ -4014,7 +4424,7 @@ errr process_dungeon_file(cptr name, int ymin, int xmin, int ymax, int xmax)
 
 
        /* Build the filename */
-       path_build(buf, 1024, ANGBAND_DIR_EDIT, name);
+       path_build(buf, sizeof(buf), ANGBAND_DIR_EDIT, name);
 
        /* Open the file */
        fp = my_fopen(buf, "r");
@@ -4024,7 +4434,7 @@ errr process_dungeon_file(cptr name, int ymin, int xmin, int ymax, int xmax)
 
 
        /* Process the file */
-       while (0 == my_fgets(fp, buf, 1024))
+       while (0 == my_fgets(fp, buf, sizeof(buf)))
        {
                /* Count lines */
                num++;
@@ -4063,18 +4473,6 @@ errr process_dungeon_file(cptr name, int ymin, int xmin, int ymax, int xmax)
                /* Apply conditionals */
                if (bypass) continue;
 
-
-               /* Process "%:<file>" */
-               if (buf[0] == '%')
-               {
-                       /* Process that file if allowed */
-                       (void)process_dungeon_file(buf + 2, ymin, xmin, ymax, xmax);
-
-                       /* Continue */
-                       continue;
-               }
-
-
                /* Process the line */
                err = process_dungeon_file_aux(buf, ymin, xmin, ymax, xmax, &y, &x);
 
@@ -4117,10 +4515,10 @@ void write_r_info_txt(void)
        int i, j, z, fc, bc;
        int dlen;
 
-       cptr flags[288];
+       cptr flags[32 * 10];
 
-       u32b f_ptr[9];
-       cptr *n_ptr[9];
+       u32b f_ptr[10];
+       cptr *n_ptr[10];
 
        monster_race *r_ptr;
 
@@ -4186,6 +4584,7 @@ void write_r_info_txt(void)
                f_ptr[6] = r_ptr->flags7; n_ptr[6] = r_info_flags7;
                f_ptr[7] = r_ptr->flags8; n_ptr[7] = r_info_flags8;
                f_ptr[8] = r_ptr->flags9; n_ptr[8] = r_info_flags9;
+               f_ptr[9] = r_ptr->flagsr; n_ptr[9] = r_info_flagsr;
 
                /* Write New/Number/Name */
                fprintf(fff, "N:%d:%s\n", z + 1, r_name + r_ptr->name);
@@ -4215,14 +4614,14 @@ void write_r_info_txt(void)
                }
 
                /* Extract the flags */
-               for (fc = 0, j = 0; j < 96; j++)
+               for (fc = 0, j = 0; j < 32 * 3; j++)
                {
                        /* Check this flag */
                        if (f_ptr[j / 32] & (1L << (j % 32))) flags[fc++] = n_ptr[j / 32][j % 32];
                }
 
                /* Extract the extra flags */
-               for (j = 192; j < 288; j++)
+               for (j = 32 * 6; j < 32 * 10; j++)
                {
                        /* Check this flag */
                        if (f_ptr[j / 32] & (1L << (j % 32))) flags[fc++] = n_ptr[j / 32][j % 32];