OSDN Git Service

呪いは外れないだけではなく、いろいろイヤな効果がある。
[hengband/hengband.git] / src / object2.c
index e7c7c86..d006204 100644 (file)
@@ -169,10 +169,6 @@ void delete_object_idx(int o_idx)
                lite_spot(y, x);
        }
 
-#ifdef USE_SCRIPT
-       object_delete_callback(j_ptr);
-#endif /* USE_SCRIPT */
-
        /* Wipe the object */
        object_wipe(j_ptr);
 
@@ -209,10 +205,6 @@ void delete_object(int y, int x)
                /* Acquire next object */
                next_o_idx = o_ptr->next_o_idx;
 
-#ifdef USE_SCRIPT
-               object_delete_callback(o_ptr);
-#endif /* USE_SCRIPT */
-
                /* Wipe the object */
                object_wipe(o_ptr);
 
@@ -306,10 +298,6 @@ static void compact_objects_aux(int i1, int i2)
        /* Structure copy */
        o_list[i2] = o_list[i1];
 
-#ifdef USE_SCRIPT
-       object_delete_callback(o_ptr);
-#endif /* USE_SCRIPT */
-
        /* Wipe the hole */
        object_wipe(o_ptr);
 }
@@ -339,7 +327,7 @@ void compact_objects(int size)
        {
                /* Message */
 #ifdef JP
-msg_print("¥¢¥¤¥Æ¥à¾ðÊó¤ò°µ½Ì¤·¤Æ¤¤¤Þ¤¹...");
+               msg_print("¥¢¥¤¥Æ¥à¾ðÊó¤ò°µ½Ì¤·¤Æ¤¤¤Þ¤¹...");
 #else
                msg_print("Compacting objects...");
 #endif
@@ -386,7 +374,7 @@ msg_print("
                                x = m_ptr->fx;
 
                                /* Monsters protect their objects */
-                               if (rand_int(100) < 90) continue;
+                               if (randint0(100) < 90) continue;
                        }
 
                        /* Dungeon */
@@ -408,7 +396,7 @@ msg_print("
                            (cnt < 1000)) chance = 100;
 
                        /* Apply the saving throw */
-                       if (rand_int(100) < chance) continue;
+                       if (randint0(100) < chance) continue;
 
                        /* Delete the object */
                        delete_object_idx(i);
@@ -498,10 +486,6 @@ void wipe_o_list(void)
                        c_ptr->o_idx = 0;
                }
 
-#ifdef USE_SCRIPT
-               object_delete_callback(o_ptr);
-#endif /* USE_SCRIPT */
-
                /* Wipe the object */
                object_wipe(o_ptr);
        }
@@ -563,7 +547,7 @@ s16b o_pop(void)
 
        /* Warn the player (except during dungeon creation) */
 #ifdef JP
-if (character_dungeon) msg_print("¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë¡ª");
+       if (character_dungeon) msg_print("¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë¡ª");
 #else
        if (character_dungeon) msg_print("Too many objects!");
 #endif
@@ -636,10 +620,10 @@ s16b get_obj_num(int level)
        if ((level > 0) && !(d_info[dungeon_type].flags1 & DF1_BEGINNER))
        {
                /* Occasional "boost" */
-               if (rand_int(GREAT_OBJ) == 0)
+               if (one_in_(GREAT_OBJ))
                {
                        /* What a bizarre calculation */
-                       level = 1 + (level * MAX_DEPTH / randint(MAX_DEPTH));
+                       level = 1 + (level * MAX_DEPTH / randint1(MAX_DEPTH));
                }
        }
 
@@ -676,7 +660,7 @@ s16b get_obj_num(int level)
 
 
        /* Pick an object */
-       value = rand_int(total);
+       value = randint0(total);
 
        /* Find the object */
        for (i = 0; i < alloc_kind_size; i++)
@@ -690,7 +674,7 @@ s16b get_obj_num(int level)
 
 
        /* Power boost */
-       p = rand_int(100);
+       p = randint0(100);
 
        /* Try for a "better" object once (50%) or twice (10%) */
        if (p < 60)
@@ -699,7 +683,7 @@ s16b get_obj_num(int level)
                j = i;
 
                /* Pick a object */
-               value = rand_int(total);
+               value = randint0(total);
 
                /* Find the object */
                for (i = 0; i < alloc_kind_size; i++)
@@ -722,7 +706,7 @@ s16b get_obj_num(int level)
                j = i;
 
                /* Pick a object */
-               value = rand_int(total);
+               value = randint0(total);
 
                /* Find the object */
                for (i = 0; i < alloc_kind_size; i++)
@@ -790,7 +774,7 @@ void object_aware(object_type *o_ptr)
        o_ptr->aware = TRUE;
 #endif /* SCRIPT_OBJ_KIND */
 
-       if(mihanmei && !(k_info[o_ptr->k_idx].flags3 & TR3_INSTA_ART) && record_ident &&
+       if(mihanmei && !(k_info[o_ptr->k_idx].gen_flags & TRG_INSTA_ART) && record_ident &&
           !death && ((o_ptr->tval >= TV_AMULET && o_ptr->tval <= TV_POTION) || (o_ptr->tval == TV_FOOD)))
        {
                object_type forge;
@@ -944,7 +928,7 @@ s32b flag_cost(object_type * o_ptr, int plusses)
        count = 0;
        if (f1 & TR1_CHAOTIC) {total += 5000;count++;}
        if (f1 & TR1_VAMPIRIC) {total += 6500;count++;}
-       if (f1 & TR1_FORCE_WEPON) {tmp_cost += 2500;count++;}
+       if (f1 & TR1_FORCE_WEAPON) {tmp_cost += 2500;count++;}
        if (f1 & TR1_SLAY_ANIMAL) {tmp_cost += 1800;count++;}
        if (f1 & TR1_SLAY_EVIL) {tmp_cost += 2300;count++;}
        if (f1 & TR1_SLAY_UNDEAD) {tmp_cost += 1800;count++;}
@@ -1004,19 +988,18 @@ s32b flag_cost(object_type * o_ptr, int plusses)
        if (f3 & TR3_SH_FIRE) total += 5000;
        if (f3 & TR3_SH_ELEC) total += 5000;
        if (f3 & TR3_SH_COLD) total += 5000;
-       if (f3 & TR3_QUESTITEM) total += 0;
        if (f3 & TR3_NO_TELE) total -= 10000;
        if (f3 & TR3_NO_MAGIC) total += 2500;
        if (f3 & TR3_TY_CURSE) total -= 15000;
        if (f3 & TR3_HIDE_TYPE) total += 0;
        if (f3 & TR3_SHOW_MODS) total += 0;
-       if (f3 & TR3_INSTA_ART) total += 0;
        if (f3 & TR3_FEATHER) total += 1250;
        if (f3 & TR3_LITE) total += 1250;
        if (f3 & TR3_SEE_INVIS) total += 2000;
        if (f3 & TR3_TELEPATHY) total += 20000;
        if (f3 & TR3_SLOW_DIGEST) total += 750;
        if (f3 & TR3_REGEN) total += 2500;
+       if (f3 & TR3_WARNING) total += 2000;
        if (f3 & TR3_XTRA_MIGHT) total += 2250;
        if (f3 & TR3_XTRA_SHOTS) total += 10000;
        if (f3 & TR3_IGNORE_ACID) total += 100;
@@ -1027,16 +1010,16 @@ s32b flag_cost(object_type * o_ptr, int plusses)
        if (f3 & TR3_DRAIN_EXP) total -= 12500;
        if (f3 & TR3_TELEPORT)
        {
-               if (o_ptr->ident & IDENT_CURSED)
+               if (cursed_p(o_ptr))
                        total -= 7500;
                else
                        total += 250;
        }
        if (f3 & TR3_AGGRAVATE) total -= 10000;
        if (f3 & TR3_BLESSED) total += 750;
-       if (f3 & TR3_CURSED) total -= 5000;
-       if (f3 & TR3_HEAVY_CURSE) total -= 12500;
-       if (f3 & TR3_PERMA_CURSE) total -= 15000;
+       if (o_ptr->curse_flags & TRC_CURSED) total -= 5000;
+       if (o_ptr->curse_flags & TRC_HEAVY_CURSE) total -= 12500;
+       if (o_ptr->curse_flags & TRC_PERMA_CURSE) total -= 15000;
 
        /* Also, give some extra for activatable powers... */
        if (o_ptr->art_name && (o_ptr->art_flags3 & TR3_ACTIVATE))
@@ -1056,7 +1039,7 @@ s32b flag_cost(object_type * o_ptr, int plusses)
                else if (type == ACT_BA_COLD_2) total += 1250;
                else if (type == ACT_BA_ELEC_2) total += 1500;
                else if (type == ACT_DRAIN_2) total += 750;
-               else if (type == ACT_VAMPIRE_1) total = 1000;
+               else if (type == ACT_VAMPIRE_1) total += 1000;
                else if (type == ACT_BO_MISS_2) total += 1000;
                else if (type == ACT_BA_FIRE_2) total += 1750;
                else if (type == ACT_BA_COLD_3) total += 2500;
@@ -1073,6 +1056,7 @@ s32b flag_cost(object_type * o_ptr, int plusses)
                else if (type == ACT_QUAKE) total += 600;
                else if (type == ACT_TERROR) total += 2500;
                else if (type == ACT_TELE_AWAY) total += 2000;
+               else if (type == ACT_BANISH_EVIL) total += 2000;
                else if (type == ACT_GENOCIDE) total += 10000;
                else if (type == ACT_MASS_GENO) total += 10000;
                else if (type == ACT_CHARM_ANIMAL) total += 7500;
@@ -1087,6 +1071,7 @@ s32b flag_cost(object_type * o_ptr, int plusses)
                else if (type == ACT_SUMMON_UNDEAD) total += 20000;
                else if (type == ACT_CURE_LW) total += 500;
                else if (type == ACT_CURE_MW) total += 750;
+               else if (type == ACT_CURE_POISON) total += 1000;
                else if (type == ACT_REST_LIFE) total += 7500;
                else if (type == ACT_REST_ALL) total += 15000;
                else if (type == ACT_CURE_700) total += 10000;
@@ -1326,11 +1311,9 @@ s32b object_value_real(object_type *o_ptr)
                        /* Factor in the bonuses */
                        value += ((o_ptr->to_h + o_ptr->to_d + o_ptr->to_a) * 100L);
 
-                       /* Hack -- Factor in extra damage dice */
-                       if ((o_ptr->dd > k_ptr->dd) && (o_ptr->ds == k_ptr->ds))
-                       {
-                               value += (o_ptr->dd - k_ptr->dd) * o_ptr->ds * 100L;
-                       }
+                       /* Hack -- Factor in extra damage dice and sides */
+                       value += (o_ptr->dd - k_ptr->dd) * o_ptr->ds * 250L;
+                       value += (o_ptr->ds - k_ptr->ds) * o_ptr->dd * 250L;
 
                        /* Done */
                        break;
@@ -1347,11 +1330,9 @@ s32b object_value_real(object_type *o_ptr)
                        /* Factor in the bonuses */
                        value += ((o_ptr->to_h + o_ptr->to_d) * 5L);
 
-                       /* Hack -- Factor in extra damage dice */
-                       if ((o_ptr->dd > k_ptr->dd) && (o_ptr->ds == k_ptr->ds))
-                       {
-                               value += (o_ptr->dd - k_ptr->dd) * o_ptr->ds * 5L;
-                       }
+                       /* Hack -- Factor in extra damage dice and sides */
+                       value += (o_ptr->dd - k_ptr->dd) * o_ptr->ds * 5L;
+                       value += (o_ptr->ds - k_ptr->ds) * o_ptr->dd * 5L;
 
                        /* Done */
                        break;
@@ -1511,11 +1492,12 @@ void reduce_charges(object_type *o_ptr, int amt)
  *
  * Chests, and activatable items, never stack (for various reasons).
  */
-bool object_similar(object_type *o_ptr, object_type *j_ptr)
-{
-       int total = o_ptr->number + j_ptr->number;
-
 
+/*
+ *  Determine if an item can partly absorb a second item.
+ */
+bool object_similar_part(object_type *o_ptr, object_type *j_ptr)
+{
        /* Require identical object types */
        if (o_ptr->k_idx != j_ptr->k_idx) return (0);
 
@@ -1693,7 +1675,7 @@ bool object_similar(object_type *o_ptr, object_type *j_ptr)
                return (0);
 
        /* Hack -- Require identical "cursed" status */
-       if ((o_ptr->ident & (IDENT_CURSED)) != (j_ptr->ident & (IDENT_CURSED))) return (0);
+       if (o_ptr->curse_flags != j_ptr->curse_flags) return (0);
 
        /* Hack -- Require identical "broken" status */
        if ((o_ptr->ident & (IDENT_BROKEN)) != (j_ptr->ident & (IDENT_BROKEN))) return (0);
@@ -1711,6 +1693,20 @@ bool object_similar(object_type *o_ptr, object_type *j_ptr)
        if (!stack_force_costs && (o_ptr->discount != j_ptr->discount)) return (0);
 
 
+       /* They match, so they must be similar */
+       return (TRUE);
+}
+
+/*
+ *  Determine if an item can absorb a second item.
+ */
+bool object_similar(object_type *o_ptr, object_type *j_ptr)
+{
+       int total = o_ptr->number + j_ptr->number;
+
+       if (!object_similar_part(o_ptr, j_ptr))
+               return FALSE;
+
        /* Maximal "stacking" limit */
        if (total >= MAX_STACK_SIZE) return (0);
 
@@ -1720,6 +1716,7 @@ bool object_similar(object_type *o_ptr, object_type *j_ptr)
 }
 
 
+
 /*
  * Allow one item to "absorb" another, assuming they are similar
  */
@@ -1808,7 +1805,7 @@ s16b lookup_kind(int tval, int sval)
 #if 0
        /* Oops */
 #ifdef JP
-msg_format("¥¢¥¤¥Æ¥à¤¬¤Ê¤¤ (%d,%d)", tval, sval);
+       msg_format("¥¢¥¤¥Æ¥à¤¬¤Ê¤¤ (%d,%d)", tval, sval);
 #else
        msg_format("No object (%d,%d)", tval, sval);
 #endif
@@ -1837,10 +1834,6 @@ void object_copy(object_type *o_ptr, object_type *j_ptr)
 {
        /* Copy the structure */
        COPY(o_ptr, j_ptr, object_type);
-
-#ifdef USE_SCRIPT
-       j_ptr->python = object_copy_callback(o_ptr, j_ptr);
-#endif /* USE_SCRIPT */
 }
 
 
@@ -1884,7 +1877,12 @@ void object_prep(object_type *o_ptr, int k_idx)
        if (get_object_cost(o_ptr) <= 0) o_ptr->ident |= (IDENT_BROKEN);
 
        /* Hack -- cursed items are always "cursed" */
-       if (k_ptr->flags3 & (TR3_CURSED)) o_ptr->ident |= (IDENT_CURSED);
+       if (k_ptr->gen_flags & (TRG_CURSED)) o_ptr->curse_flags |= (TRC_CURSED);
+       if (k_ptr->gen_flags & (TRG_HEAVY_CURSE)) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
+       if (k_ptr->gen_flags & (TRG_PERMA_CURSE)) o_ptr->curse_flags |= (TRC_PERMA_CURSE);
+       if (k_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
+       if (k_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
+       if (k_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
 }
 
 
@@ -1943,7 +1941,7 @@ s16b m_bonus(int max, int level)
        extra = ((max * level) % MAX_DEPTH);
 
        /* Hack -- simulate floating point computations */
-       if (rand_int(MAX_DEPTH) < extra) bonus++;
+       if (randint0(MAX_DEPTH) < extra) bonus++;
 
 
        /* The "stand" is equal to one quarter of the max */
@@ -1953,7 +1951,7 @@ s16b m_bonus(int max, int level)
        extra = (max % 4);
 
        /* Hack -- simulate floating point computations */
-       if (rand_int(4) < extra) stand++;
+       if (randint0(4) < extra) stand++;
 
 
        /* Choose an "interesting" value */
@@ -1985,7 +1983,7 @@ static void object_mention(object_type *o_ptr)
        {
                /* Silly message */
 #ifdef JP
-msg_format("ÅÁÀâ¤Î¥¢¥¤¥Æ¥à (%s)", o_name);
+               msg_format("ÅÁÀâ¤Î¥¢¥¤¥Æ¥à (%s)", o_name);
 #else
                msg_format("Artifact (%s)", o_name);
 #endif
@@ -1996,7 +1994,7 @@ msg_format("
        else if (o_ptr->art_name)
        {
 #ifdef JP
-msg_print("¥é¥ó¥À¥à¡¦¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È");
+               msg_print("¥é¥ó¥À¥à¡¦¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È");
 #else
                msg_print("Random artifact");
 #endif
@@ -2008,7 +2006,7 @@ msg_print("
        {
                /* Silly message */
 #ifdef JP
-msg_format("̾¤Î¤¢¤ë¥¢¥¤¥Æ¥à (%s)", o_name);
+               msg_format("̾¤Î¤¢¤ë¥¢¥¤¥Æ¥à (%s)", o_name);
 #else
                msg_format("Ego-item (%s)", o_name);
 #endif
@@ -2020,7 +2018,7 @@ msg_format("̾
        {
                /* Silly message */
 #ifdef JP
-msg_format("¥¢¥¤¥Æ¥à (%s)", o_name);
+               msg_format("¥¢¥¤¥Æ¥à (%s)", o_name);
 #else
                msg_format("Object (%s)", o_name);
 #endif
@@ -2046,6 +2044,9 @@ static bool make_artifact_special(object_type *o_ptr)
        /* No artifacts in the town */
        if (!dun_level) return (FALSE);
 
+       /* Themed object */
+       if (get_obj_num_hook) return (FALSE);
+
        /* Check the artifact list (just the "specials") */
        for (i = 0; i < max_a_idx; i++)
        {
@@ -2057,8 +2058,8 @@ static bool make_artifact_special(object_type *o_ptr)
                /* Cannot make an artifact twice */
                if (a_ptr->cur_num) continue;
 
-               if (a_ptr->flags3 & TR3_QUESTITEM) continue;
-               if (!(a_ptr->flags3 & TR3_INSTA_ART)) continue;
+               if (a_ptr->gen_flags & TRG_QUESTITEM) continue;
+               if (!(a_ptr->gen_flags & TRG_INSTA_ART)) continue;
 
                /* XXX XXX Enforce minimum "depth" (loosely) */
                if (a_ptr->level > dun_level)
@@ -2067,11 +2068,11 @@ static bool make_artifact_special(object_type *o_ptr)
                        int d = (a_ptr->level - dun_level) * 2;
 
                        /* Roll for out-of-depth creation */
-                       if (rand_int(d) != 0) continue;
+                       if (!one_in_(d)) continue;
                }
 
                /* Artifact "rarity roll" */
-               if (rand_int(a_ptr->rarity) != 0) continue;
+               if (!one_in_(a_ptr->rarity)) continue;
 
                /* Find the base object */
                k_idx = lookup_kind(a_ptr->tval, a_ptr->sval);
@@ -2083,7 +2084,7 @@ static bool make_artifact_special(object_type *o_ptr)
                        int d = (k_info[k_idx].level - object_level) * 5;
 
                        /* Roll for out-of-depth creation */
-                       if (rand_int(d) != 0) continue;
+                       if (!one_in_(d)) continue;
                }
 
                /* Assign the template */
@@ -2093,7 +2094,7 @@ static bool make_artifact_special(object_type *o_ptr)
                o_ptr->name1 = i;
 
                /* Hack: Some artifacts get random extra powers */
-               random_artifact_resistance(o_ptr);
+               random_artifact_resistance(o_ptr, a_ptr);
 
                /* Success */
                return (TRUE);
@@ -2133,9 +2134,9 @@ static bool make_artifact(object_type *o_ptr)
                /* Cannot make an artifact twice */
                if (a_ptr->cur_num) continue;
 
-               if (a_ptr->flags3 & TR3_QUESTITEM) continue;
+               if (a_ptr->gen_flags & TRG_QUESTITEM) continue;
 
-               if (a_ptr->flags3 & TR3_INSTA_ART) continue;
+               if (a_ptr->gen_flags & TRG_INSTA_ART) continue;
 
                /* Must have the correct fields */
                if (a_ptr->tval != o_ptr->tval) continue;
@@ -2148,17 +2149,17 @@ static bool make_artifact(object_type *o_ptr)
                        int d = (a_ptr->level - dun_level) * 2;
 
                        /* Roll for out-of-depth creation */
-                       if (rand_int(d) != 0) continue;
+                       if (!one_in_(d)) continue;
                }
 
                /* We must make the "rarity roll" */
-               if (rand_int(a_ptr->rarity) != 0) continue;
+               if (!one_in_(a_ptr->rarity)) continue;
 
                /* Hack -- mark the item as an artifact */
                o_ptr->name1 = i;
 
                /* Hack: Some artifacts get random extra powers */
-               random_artifact_resistance(o_ptr);
+               random_artifact_resistance(o_ptr, a_ptr);
 
                /* Success */
                return (TRUE);
@@ -2170,6 +2171,48 @@ static bool make_artifact(object_type *o_ptr)
 
 
 /*
+ *  Choose random ego type
+ */
+static byte get_random_ego(byte slot, bool good, int level)
+{
+       int i, value;
+       ego_item_type *e_ptr;
+
+       long total = 0L;
+       
+       for (i = 1; i < max_e_idx; i++)
+       {
+               e_ptr = &e_info[i];
+               
+               if (e_ptr->slot == slot
+                   /* && level >= e_ptr->level */
+                   && ((good && e_ptr->rating) || (!good && !e_ptr->rating)) )
+               {
+                       if (e_ptr->rarity)
+                               total += (255 / e_ptr->rarity);
+               }
+       }
+
+       value = randint1(total);
+
+       for (i = 1; i < max_e_idx; i++)
+       {
+               e_ptr = &e_info[i];
+               
+               if (e_ptr->slot == slot
+                   /* && level >= e_ptr->level */
+                   && ((good && e_ptr->rating) || (!good && !e_ptr->rating)) )
+               {
+                       if (e_ptr->rarity)
+                               value -= (255 / e_ptr->rarity);
+                       if (value <= 0L) break;
+               }
+       }
+       return (byte)i;
+}
+
+
+/*
  * Apply magic to an item known to be a "weapon"
  *
  * Hack -- note special base damage dice boosting
@@ -2178,8 +2221,8 @@ static bool make_artifact(object_type *o_ptr)
  */
 static void a_m_aux_1(object_type *o_ptr, int level, int power)
 {
-       int tohit1 = randint(5) + m_bonus(5, level);
-       int todam1 = randint(5) + m_bonus(5, level);
+       int tohit1 = randint1(5) + m_bonus(5, level);
+       int todam1 = randint1(5) + m_bonus(5, level);
 
        int tohit2 = m_bonus(10, level);
        int todam2 = m_bonus(10, level);
@@ -2187,11 +2230,9 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
        if ((o_ptr->tval == TV_BOLT) || (o_ptr->tval == TV_ARROW) || (o_ptr->tval == TV_SHOT))
        {
                tohit2 = (tohit2+1)/2;
-               tohit2 = (todam2+1)/2;
+               todam2 = (todam2+1)/2;
        }
 
-       artifact_bias = 0;
-
        /* Good */
        if (power > 0)
        {
@@ -2224,7 +2265,7 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                }
 
                /* Cursed (if "bad") */
-               if (o_ptr->to_h + o_ptr->to_d < 0) o_ptr->ident |= (IDENT_CURSED);
+               if (o_ptr->to_h + o_ptr->to_d < 0) o_ptr->curse_flags |= TRC_CURSED;
        }
 
        if ((o_ptr->tval == TV_SWORD) && (o_ptr->sval == SV_DIAMOND_EDGE)) return;
@@ -2237,7 +2278,7 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                        /* Very good */
                        if (power > 1)
                        {
-                               if (randint(30) == 1)
+                               if (one_in_(30))
                                        create_artifact(o_ptr, FALSE);
                                else
                                        /* Special Ego-item */
@@ -2248,7 +2289,7 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                        else if (power < -1)
                        {
                                /* Hack -- Horrible digging bonus */
-                               o_ptr->pval = 0 - (5 + randint(5));
+                               o_ptr->pval = 0 - (5 + randint1(5));
                        }
 
                        /* Bad */
@@ -2269,253 +2310,96 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                        /* Very Good */
                        if (power > 1)
                        {
-                               /* Roll for an ego-item */
-                               switch (randint((o_ptr->tval == TV_POLEARM) ? 42 : 44))
+                               if (one_in_(40))
                                {
-                                       case 1:
-                                       {
-                                               o_ptr->name2 = EGO_HA;
-
-                                               if ((randint(4) == 1) && (level > 40))
-                                               {
-                                                       o_ptr->art_flags1 |= TR1_BLOWS;
-                                               }
-                                               break;
-                                       }
-
-                                       case 2:
-                                       {
-                                               o_ptr->name2 = EGO_DF;
-                                               if (randint(3) == 1)
-                                                       o_ptr->art_flags2 |= TR2_RES_POIS;
-                                               random_resistance(o_ptr, FALSE, randint(22)+16);
-                                               break;
-                                       }
-
-                                       case 3:
-                                       {
-                                               o_ptr->name2 = EGO_BRAND_ACID;
-                                               break;
-                                       }
-
-                                       case 4:
-                                       {
-                                               o_ptr->name2 = EGO_BRAND_ELEC;
-                                               break;
-                                       }
-
-                                       case 5:
-                                       {
-                                               o_ptr->name2 = EGO_BRAND_FIRE;
-                                               break;
-                                       }
-
-                                       case 6:
-                                       {
-                                               o_ptr->name2 = EGO_BRAND_COLD;
-                                               break;
-                                       }
-
-                                       case 7: case 8:
-                                       {
-                                               o_ptr->name2 = EGO_SLAY_ANIMAL;
-                                               if (rand_int(100) < 20)
-                                               {
-                                                       o_ptr->name2 = EGO_KILL_ANIMAL;
-                                               }
-                                               break;
-                                       }
-
-                                       case 9: case 10:
-                                       {
-                                               o_ptr->name2 = EGO_SLAY_DRAGON;
-                                               random_resistance(o_ptr, FALSE, randint(12) + 4);
-                                               if (rand_int(100) < 20)
-                                               {
-                                                       if (randint(3) == 1) o_ptr->art_flags2 |= TR2_RES_POIS;
-                                                       random_resistance(o_ptr, FALSE, randint(14) + 4);
-                                                       o_ptr->name2 = EGO_KILL_DRAGON;
-                                               }
-                                               break;
-                                       }
-
-                                       case 11: case 12:
-                                       {
-                                               o_ptr->name2 = EGO_SLAY_EVIL;
-                                               if (rand_int(100) < 20)
-                                               {
-                                                       o_ptr->name2 = EGO_KILL_EVIL;
-                                               }
-                                               break;
-                                       }
-
-                                       case 13: case 14:
-                                       {
-                                               o_ptr->name2 = EGO_SLAY_UNDEAD;
-                                               if (rand_int(100) < 20)
-                                               {
-                                                       o_ptr->name2 = EGO_KILL_UNDEAD;
-                                               }
-                                               break;
-                                       }
-
-                                       case 15: case 16: case 17:
-                                       {
-                                               o_ptr->name2 = EGO_SLAY_ORC;
-                                               if (rand_int(100) < 20)
-                                               {
-                                                       o_ptr->name2 = EGO_KILL_ORC;
-                                               }
-                                               break;
-                                       }
-
-                                       case 18: case 19: case 20:
-                                       {
-                                               o_ptr->name2 = EGO_SLAY_TROLL;
-                                               if (rand_int(100) < 20)
-                                               {
-                                                       o_ptr->name2 = EGO_KILL_TROLL;
-                                               }
-                                               break;
-                                       }
-
-                                       case 21: case 22: case 23:
-                                       {
-                                               o_ptr->name2 = EGO_SLAY_GIANT;
-                                               if (rand_int(100) < 20)
-                                               {
-                                                       o_ptr->name2 = EGO_KILL_GIANT;
-                                               }
-                                               break;
-                                       }
-
-                                       case 24: case 25:
-                                       {
-                                               o_ptr->name2 = EGO_SLAY_DEMON;
-                                               if (rand_int(100) < 20)
-                                               {
-                                                       o_ptr->name2 = EGO_KILL_DEMON;
-                                               }
-                                               break;
-                                       }
-
-                                       case 26:
-                                       {
-                                               o_ptr->name2 = EGO_PRISM;
-                                               break;
-                                       }
-
-                                       case 27:
-                                       {
-                                               o_ptr->name2 = EGO_WEST;
-                                               if (randint(3) == 1) o_ptr->art_flags2 |= TR2_RES_FEAR;
-                                               break;
-                                       }
-
-                                       case 28:
-                                       {
-                                               o_ptr->name2 = EGO_BLESS_BLADE;
-                                               break;
-                                       }
-
-                                       case 29: case 30:
-                                       {
-                                               o_ptr->name2 = EGO_ATTACKS;
-                                               break;
-                                       }
+                                       create_artifact(o_ptr, FALSE);
+                                       break;
+                               }
+                               while (1)
+                               {
+                                       /* Roll for an ego-item */
+                                       o_ptr->name2 = get_random_ego(INVEN_RARM, TRUE, level);
+                                       if (o_ptr->name2 == EGO_SHARPNESS && o_ptr->tval != TV_SWORD)
+                                               continue;
+                                       if (o_ptr->name2 == EGO_EARTHQUAKES && o_ptr->tval != TV_HAFTED)
+                                               continue;
+                                       break;
+                               }
 
-                                       case 31: case 32:
-                                       {
-                                               o_ptr->name2 = EGO_VAMPIRIC;
-                                               break;
-                                       }
-                                       case 33:
-                                       {
-                                               o_ptr->name2 = EGO_BRAND_POIS;
-                                               break;
-                                       }
-                                       case 34:
-                                       {
-                                               o_ptr->name2 = EGO_CHAOTIC;
-                                               random_resistance(o_ptr, FALSE, (randint(34) + 4));
-                                               break;
-                                       }
-                                       case 35:
-                                       {
-                                               create_artifact(o_ptr, FALSE);
-                                               break;
-                                       }
-                                       case 36: case 37:
+                               switch (o_ptr->name2)
+                               {
+                               case EGO_HA:
+                                       if (one_in_(4) && (level > 40))
+                                               o_ptr->art_flags1 |= TR1_BLOWS;
+                                       break;
+                               case EGO_DF:
+                                       if (one_in_(3))
+                                               o_ptr->art_flags2 |= TR2_RES_POIS;
+                                       break;
+                               case EGO_KILL_DRAGON:
+                                       if (one_in_(3))
+                                               o_ptr->art_flags2 |= TR2_RES_POIS;
+                                       break;
+                               case EGO_WEST:
+                                       if (one_in_(3))
+                                               o_ptr->art_flags2 |= TR2_RES_FEAR;
+                                       break;
+                               case EGO_SLAYING_WEAPON:
+                                       if (one_in_(3)) /* double damage */
+                                               o_ptr->dd *= 2;
+                                       else
                                        {
-                                               o_ptr->name2 = EGO_SLAYING_WEAPON;
-                                               if (randint(3) == 1) /* double damage */
-                                                       o_ptr->dd *= 2;
-                                               else
-                                               {
-                                                       do
-                                                       {
-                                                               o_ptr->dd++;
-                                                       }
-                                                       while (randint(o_ptr->dd) == 1);
-
-                                                       do
-                                                       {
-                                                               o_ptr->ds++;
-                                                       }
-                                                       while (randint(o_ptr->ds) == 1);
-                                               }
-
-                                               if (randint(5) == 1)
+                                               do
                                                {
-                                                       o_ptr->art_flags1 |= TR1_BRAND_POIS;
+                                                       o_ptr->dd++;
                                                }
-                                               if (o_ptr->tval == TV_SWORD && (randint(3) == 1))
+                                               while (one_in_(o_ptr->dd));
+                                               
+                                               do
                                                {
-                                                       o_ptr->art_flags1 |= TR1_VORPAL;
+                                                       o_ptr->ds++;
                                                }
-                                               break;
-                                       }
-                                       case 38: case 39:
-                                       {
-                                               o_ptr->name2 = EGO_TRUMP;
-                                               random_resistance(o_ptr, FALSE, (randint(22) + 16));
-                                               if (randint(5) == 1) o_ptr->art_flags1 |= TR1_SLAY_DEMON;
-                                               break;
-                                       }
-                                       case 40:
-                                       {
-                                               o_ptr->name2 = EGO_PATTERN;
-                                               if (randint(3) == 1) o_ptr->art_flags2 |= TR2_HOLD_LIFE;
-                                               if (randint(3) == 1) o_ptr->art_flags1 |= TR1_DEX;
-                                               if (randint(5) == 1) o_ptr->art_flags2 |= TR2_RES_FEAR;
-                                               random_resistance(o_ptr, FALSE, (randint(22) + 16));
-                                               break;
+                                               while (one_in_(o_ptr->ds));
                                        }
-                                       case 41: case 42:
+                                       
+                                       if (one_in_(5))
                                        {
-                                               o_ptr->name2 = EGO_FORCE_WEPON;
-                                               break;
+                                               o_ptr->art_flags1 |= TR1_BRAND_POIS;
                                        }
-                                       default: /* 2 slots for TV_SWORD and TV_HAFTED */
+                                       if (o_ptr->tval == TV_SWORD && one_in_(3))
                                        {
-                                               if (o_ptr->tval == TV_SWORD)
-                                               {
-                                                       o_ptr->name2 = EGO_SHARPNESS;
-                                                       o_ptr->pval = m_bonus(5, level) + 1;
-                                               }
-                                               else /* Hafted */
-                                               {
-                                                       o_ptr->name2 = EGO_EARTHQUAKES;
-                                                       if ((randint(3) == 1) && (level > 60)) o_ptr->art_flags1 |= TR1_BLOWS;
-                                                       else o_ptr->pval = m_bonus(3, level);
-                                               }
+                                               o_ptr->art_flags1 |= TR1_VORPAL;
                                        }
+                                       break;
+                               case EGO_TRUMP:
+                                       if (one_in_(5))
+                                               o_ptr->art_flags1 |= TR1_SLAY_DEMON;
+                                       if (one_in_(7))
+                                               one_ability(o_ptr);
+                                       break;
+                               case EGO_PATTERN:
+                                       if (one_in_(3))
+                                               o_ptr->art_flags2 |= TR2_HOLD_LIFE;
+                                       if (one_in_(3))
+                                               o_ptr->art_flags1 |= TR1_DEX;
+                                       if (one_in_(5))
+                                               o_ptr->art_flags2 |= TR2_RES_FEAR;
+                                       break;
+                               case EGO_SHARPNESS:
+                                       o_ptr->pval = m_bonus(5, level) + 1;
+                                       break;
+                               case EGO_EARTHQUAKES:
+                                       if (one_in_(3) && (level > 60))
+                                               o_ptr->art_flags1 |= TR1_BLOWS;
+                                       else
+                                               o_ptr->pval = m_bonus(3, level);
+                                       break;
                                }
 
                                if (!o_ptr->art_name)
                                {
                                        /* Hack -- Super-charge the damage dice */
-                                       while (rand_int(10L * o_ptr->dd * o_ptr->ds) == 0) o_ptr->dd++;
+                                       while (one_in_(10L * o_ptr->dd * o_ptr->ds)) o_ptr->dd++;
 
                                        /* Hack -- Lower the damage dice */
                                        if (o_ptr->dd > 9) o_ptr->dd = 9;
@@ -2526,10 +2410,14 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                        else if (power < -1)
                        {
                                /* Roll for ego-item */
-                               if (rand_int(MAX_DEPTH) < level)
+                               if (randint0(MAX_DEPTH) < level)
                                {
-                                       o_ptr->name2 = EGO_MORGUL;
-                                       if (randint(6) == 1) o_ptr->art_flags3 |= TR3_TY_CURSE;
+                                       o_ptr->name2 = get_random_ego(INVEN_RARM, FALSE, level);
+                                       switch (o_ptr->name2)
+                                       {
+                                       case EGO_MORGUL:
+                                               if (one_in_(6)) o_ptr->art_flags3 |= TR3_TY_CURSE;
+                                       }
                                }
                        }
 
@@ -2542,40 +2430,12 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                        /* Very good */
                        if (power > 1)
                        {
-                               /* Roll for ego-item */
-                               switch (randint(21))
+                               if (one_in_(20))
                                {
-                                       case 1: case 11:
-                                       {
-                                               o_ptr->name2 = EGO_EXTRA_MIGHT;
-                                               random_resistance(o_ptr, FALSE, (randint(34) + 4));
-                                               break;
-                                       }
-
-                                       case 2: case 12:
-                                       {
-                                               o_ptr->name2 = EGO_EXTRA_SHOTS;
-                                               break;
-                                       }
-
-                                       case 3: case 4: case 5: case 6:
-                                       case 13: case 14: case 15: case 16:
-                                       {
-                                               o_ptr->name2 = EGO_VELOCITY;
-                                               break;
-                                       }
-
-                                       case 7: case 8: case 9: case 10:
-                                       case 17: case 18: case 19: case 20:
-                                       {
-                                               o_ptr->name2 = EGO_ACCURACY;
-                                               break;
-                                       }
-                                       default:
-                                       {
-                                               create_artifact(o_ptr, FALSE);
-                                       }
+                                       create_artifact(o_ptr, FALSE);
+                                       break;
                                }
+                               o_ptr->name2 = get_random_ego(INVEN_BOW, TRUE, level);
                        }
 
                        break;
@@ -2589,61 +2449,17 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                        /* Very good */
                        if (power > 1)
                        {
-                               /* Roll for ego-item */
-                               switch (randint(12))
-                               {
-                                       case 1: case 2: case 3:
-                                       {
-                                               o_ptr->name2 = EGO_WOUNDING;
-                                               break;
-                                       }
+                               o_ptr->name2 = get_random_ego(INVEN_AMMO, TRUE, level);
 
-                                       case 4:
-                                       {
-                                               o_ptr->name2 = EGO_FLAME;
-                                               break;
-                                       }
-
-                                       case 5:
-                                       {
-                                               o_ptr->name2 = EGO_FROST;
-                                               break;
-                                       }
-
-                                       case 6: case 7:
-                                       {
-                                               o_ptr->name2 = EGO_HURT_ANIMAL;
-                                               break;
-                                       }
-
-                                       case 8: case 9:
-                                       {
-                                               o_ptr->name2 = EGO_HURT_EVIL;
-                                               break;
-                                       }
-
-                                       case 10:
-                                       {
-                                               o_ptr->name2 = EGO_HURT_DRAGON;
-                                               break;
-                                       }
-
-                                       case 11:
-                                       {
-                                               o_ptr->name2 = EGO_LIGHTNING_BOLT;
-                                               break;
-                                       }
-
-                                       case 12:
-                                       {
-                                               o_ptr->name2 = EGO_SLAYING_BOLT;
-                                               o_ptr->dd++;
-                                               break;
-                                       }
+                               switch (o_ptr->name2)
+                               {
+                               case EGO_SLAYING_BOLT:
+                                       o_ptr->dd++;
+                                       break;
                                }
 
                                /* Hack -- super-charge the damage dice */
-                               while (rand_int(10L * o_ptr->dd * o_ptr->ds) == 0) o_ptr->dd++;
+                               while (one_in_(10L * o_ptr->dd * o_ptr->ds)) o_ptr->dd++;
 
                                /* Hack -- restrict the damage dice */
                                if (o_ptr->dd > 9) o_ptr->dd = 9;
@@ -2653,9 +2469,9 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                        else if (power < -1)
                        {
                                /* Roll for ego-item */
-                               if (rand_int(MAX_DEPTH) < level)
+                               if (randint0(MAX_DEPTH) < level)
                                {
-                                       o_ptr->name2 = EGO_BACKBITING;
+                                       o_ptr->name2 = get_random_ego(INVEN_AMMO, FALSE, level);
                                }
                        }
 
@@ -2669,14 +2485,12 @@ static void dragon_resist(object_type * o_ptr)
 {
        do
        {
-               artifact_bias = 0;
-
-               if (randint(4) == 1)
-                       random_resistance(o_ptr, FALSE, (randint(14) + 4));
+               if (one_in_(4))
+                       one_dragon_ele_resistance(o_ptr);
                else
-                       random_resistance(o_ptr, FALSE, (randint(22) + 16));
+                       one_high_resistance(o_ptr);
        }
-       while (randint(2) == 1);
+       while (one_in_(2));
 }
 
 
@@ -2688,12 +2502,10 @@ static void dragon_resist(object_type * o_ptr)
  */
 static void a_m_aux_2(object_type *o_ptr, int level, int power)
 {
-       int toac1 = randint(5) + m_bonus(5, level);
+       int toac1 = randint1(5) + m_bonus(5, level);
 
        int toac2 = m_bonus(10, level);
 
-       artifact_bias = 0;
-
        /* Good */
        if (power > 0)
        {
@@ -2722,7 +2534,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                }
 
                /* Cursed (if "bad") */
-               if (o_ptr->to_a < 0) o_ptr->ident |= (IDENT_CURSED);
+               if (o_ptr->to_a < 0) o_ptr->curse_flags |= TRC_CURSED;
        }
 
 
@@ -2733,7 +2545,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                {
                        /* Rating boost */
                        rating += 30;
-                       if(rand_int(50) == 1)
+                       if(one_in_(50))
                                create_artifact(o_ptr, FALSE);
 
                        /* Mention the item */
@@ -2751,7 +2563,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                /* Hack -- Try for "Robes of the Magi" */
                                if ((o_ptr->tval == TV_SOFT_ARMOR) &&
                                    (o_ptr->sval == SV_ROBE) &&
-                                   (rand_int(100) < 15))
+                                   (randint0(100) < 15))
                                {
                                        if (one_in_(5))
                                        {
@@ -2762,61 +2574,50 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                                o_ptr->to_a = 0;
                                        }
                                        else
+                                       {
                                                o_ptr->name2 = EGO_PERMANENCE;
+                                       }
                                        break;
                                }
 
-                               /* Roll for ego-item */
-                               switch (randint(24))
+                               if (one_in_(20))
                                {
-                                       case 1: case 2: case 3:
-                                       {
-                                               o_ptr->name2 = EGO_RESIST_ACID;
-                                               break;
-                                       }
+                                       create_artifact(o_ptr, FALSE);
+                                       break;
+                               }
 
-                                       case 5: case 6: case 7:
-                                       {
-                                               o_ptr->name2 = EGO_RESIST_ELEC;
-                                               break;
-                                       }
+                               while (1)
+                               {
+                                       bool okay_flag = TRUE;
 
-                                       case 9: case 10: case 11: case 12:
-                                       {
-                                               o_ptr->name2 = EGO_RESIST_FIRE;
-                                               break;
-                                       }
+                                       o_ptr->name2 = get_random_ego(INVEN_BODY, TRUE, level);
 
-                                       case 13: case 14: case 15: case 16:
+                                       switch (o_ptr->name2)
                                        {
-                                               o_ptr->name2 = EGO_RESIST_COLD;
-                                               break;
-                                       }
-
-                                       case 4: case 8:
-                                       {
-                                               o_ptr->name2 = EGO_DWARVEN;
-                                               if (randint(4) == 1) o_ptr->art_flags1 |= TR1_CON;
+                                       case EGO_RESISTANCE:
+                                               if (one_in_(4))
+                                                       o_ptr->art_flags2 |= TR2_RES_POIS;
                                                break;
-                                       }
-
-                                       case 17: case 18:
-                                       {
-                                               o_ptr->name2 = EGO_RESISTANCE;
-                                               if (randint(4) == 1) o_ptr->art_flags2 |= TR2_RES_POIS;
-                                               random_resistance(o_ptr, FALSE, (randint(22) + 16));
+                                       case EGO_ELVENKIND:
                                                break;
+                                       case EGO_DWARVEN:
+                                               if (o_ptr->tval != TV_HARD_ARMOR)
+                                               {
+                                                       okay_flag = FALSE;
+                                                       break;
+                                               }
+                                               else
+                                               {
+                                                       o_ptr->weight = (2 * k_info[o_ptr->k_idx].weight / 3);
+                                                       o_ptr->ac = k_info[o_ptr->k_idx].ac + 5;
+                                                       if (one_in_(4))
+                                                               o_ptr->art_flags1 |= TR1_CON;
+                                                       break;
+                                               }
                                        }
 
-                                       case 20: case 21:
-                                       {
-                                               o_ptr->name2 = EGO_ELVENKIND;
+                                       if (okay_flag)
                                                break;
-                                       }
-                                       default:
-                                       {
-                                               create_artifact(o_ptr, FALSE);
-                                       }
                                }
                        }
 
@@ -2834,65 +2635,29 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                /* Mention the item */
                                if (cheat_peek) object_mention(o_ptr);
                                dragon_resist(o_ptr);
-                               if (randint(3) != 1) break;
+                               if (!one_in_(3)) break;
                        }
+
+                       /* Very good */
+                       if (power > 1)
                        {
-                               /* Very good */
-                               if (power > 1)
+                               if (one_in_(20))
                                {
-                                       /* Roll for ego-item */
-                                       switch (randint(23))
-                                       {
-                                               case 1: case 11:
-                                               {
-                                                       o_ptr->name2 = EGO_ENDURE_ACID;
-                                                       break;
-                                               }
-
-                                               case 2: case 3:
-                                               case 12: case 13: case 14:
-                                               {
-                                                       o_ptr->name2 = EGO_ENDURE_ELEC;
-                                                       break;
-                                               }
-
-                                               case 5: case 6:
-                                               case 15: case 16:
-                                               {
-                                                       o_ptr->name2 = EGO_ENDURE_FIRE;
-                                                       break;
-                                               }
-
-                                               case 7: case 8:
-                                               case 17: case 18: case 19:
-                                               {
-                                                       o_ptr->name2 = EGO_ENDURE_COLD;
-                                                       break;
-                                               }
-
-                                               case 10: case 20:
-                                               {
-                                                       random_resistance(o_ptr, FALSE, (randint(34) + 4));
-                                                       if (randint(4) == 1) o_ptr->art_flags2 |= TR2_RES_POIS;
-                                                       o_ptr->name2 = EGO_ENDURANCE;
-                                                       break;
-                                               }
-                                               case 21: case 22:
-                                               {
-                                                       if (o_ptr->sval != SV_SHIELD_OF_DEFLECTION) o_ptr->name2 = EGO_REFLECTION;
-                                                       break;
-                                               }
-                                               case 4: case 9:
-                                               {
-                                                       o_ptr->name2 = EGO_NIGHT_DAY;
-                                                       break;
-                                               }
-
-                                               default:
-                                               {
-                                                       create_artifact(o_ptr, FALSE);
-                                               }
-                                       }
+                                       create_artifact(o_ptr, FALSE);
+                                       break;
+                               }
+                               o_ptr->name2 = get_random_ego(INVEN_LARM, TRUE, level);
+                               
+                               switch (o_ptr->name2)
+                               {
+                               case EGO_ENDURANCE:
+                                       if (!one_in_(3)) one_high_resistance(o_ptr);
+                                       if (one_in_(4)) o_ptr->art_flags2 |= TR2_RES_POIS;
+                                       break;
+                               case EGO_REFLECTION:
+                                       if (o_ptr->sval == SV_SHIELD_OF_DEFLECTION)
+                                               o_ptr->name2 = 0;
+                                       break;
                                }
                        }
                        break;
@@ -2908,77 +2673,22 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                /* Mention the item */
                                if (cheat_peek) object_mention(o_ptr);
                                dragon_resist(o_ptr);
-                               if (randint(3) != 1) break;
+                               if (!one_in_(3)) break;
                        }
+                       if (power > 1)
                        {
-                               /* Very good */
-                               if (power > 1)
-                               {
-                                       if (randint(20) == 1)
-                                               create_artifact(o_ptr, FALSE);
-                                       else
-                                       {
-                                               /* Roll for ego-item */
-                                               switch (randint(28))
-                                               {
-                                                       case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10:
-                                                       {
-                                                               o_ptr->name2 = EGO_FREE_ACTION;
-                                                               break;
-                                                       }
-
-                                                       case 11: case 12: case 13: case 14: case 15: case 16:
-                                                       {
-                                                               o_ptr->name2 = EGO_SLAYING;
-                                                               break;
-                                                       }
-
-                                                       case 17: case 18: case 19: case 20: case 21:
-                                                       {
-                                                               o_ptr->name2 = EGO_MAGIC_MASTERY;
-                                                               break;
-                                                       }
-
-                                                       case 22: case 23: case 24: case 25:
-                                                       {
-                                                               o_ptr->name2 = EGO_AGILITY;
-                                                               break;
-                                                       }
-
-                                                       case 26: case 27:
-                                                       {
-                                                               o_ptr->name2 = EGO_POWER;
-                                                               random_resistance(o_ptr, FALSE, (randint(22) + 16));
-                                                               break;
-                                                       }
-
-                                                       case 28:
-                                                       {
-                                                               o_ptr->name2 = EGO_2HAND;
-                                                               break;
-                                                       }
-                                               }
-                                       }
-                               }
-
-                               /* Very cursed */
-                               else if (power < -1)
+                               if (one_in_(20))
                                {
-                                       /* Roll for ego-item */
-                                       switch (randint(2))
-                                       {
-                                               case 1:
-                                               {
-                                                       o_ptr->name2 = EGO_CLUMSINESS;
-                                                       break;
-                                               }
-                                               default:
-                                               {
-                                                       o_ptr->name2 = EGO_WEAKNESS;
-                                                       break;
-                                               }
-                                       }
+                                       create_artifact(o_ptr, FALSE);
+                                       break;
                                }
+                               o_ptr->name2 = get_random_ego(INVEN_HANDS, TRUE, level);
+                       }
+                       
+                       /* Very cursed */
+                       else if (power < -1)
+                       {
+                               o_ptr->name2 = get_random_ego(INVEN_HANDS, FALSE, level);
                        }
 
                        break;
@@ -2994,168 +2704,76 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                /* Mention the item */
                                if (cheat_peek) object_mention(o_ptr);
                                dragon_resist(o_ptr);
-                               if (randint(3) != 1) break;
-                       }
-                       {
-                               /* Very good */
-                               if (power > 1)
-                               {
-                                       if (randint(20) == 1)
-                                               create_artifact(o_ptr, FALSE);
-                                       else
-                                       {
-                                               /* Roll for ego-item */
-                                               switch (randint(24))
-                                               {
-                                                       case 1:
-                                                       {
-                                                               o_ptr->name2 = EGO_SPEED;
-                                                               break;
-                                                       }
-
-                                                       case 2: case 3: case 4: case 5:
-                                                       {
-                                                               o_ptr->name2 = EGO_MOTION;
-                                                               break;
-                                                       }
-
-                                                       case 6: case 7: case 8: case 9:
-                                                       case 10: case 11:
-                                                       {
-                                                               o_ptr->name2 = EGO_QUIET;
-                                                               break;
-                                                       }
-
-                                                       case 12: case 13: case 14: case 15:
-                                                       {
-                                                               o_ptr->name2 = EGO_JUMP;
-                                                               break;
-                                                       }
-
-                                                       default:
-                                                       {
-                                                               o_ptr->name2 = EGO_SLOW_DESCENT;
-
-                                                               if (randint(2) == 1)
-                                                               {
-                                                                       random_resistance(o_ptr, FALSE, (randint(22) + 16));
-                                                               }
-                                                               break;
-                                                       }
-                                               }
-                                       }
-                               }
-
-                               /* Very cursed */
-                               else if (power < -1)
-                               {
-                                       /* Roll for ego-item */
-                                       switch (randint(3))
-                                       {
-                                               case 1:
-                                               {
-                                                       o_ptr->name2 = EGO_NOISE;
-                                                       break;
-                                               }
-                                               case 2:
-                                               {
-                                                       o_ptr->name2 = EGO_SLOWNESS;
-                                                       break;
-                                               }
-                                               case 3:
-                                               {
-                                                       o_ptr->name2 = EGO_ANNOYANCE;
-                                                       break;
-                                               }
-                                       }
-                               }
+                               if (!one_in_(3)) break;
                        }
-
-                       break;
-               }
-
-               case TV_CROWN:
-               {
                        /* Very good */
                        if (power > 1)
                        {
-                               if (randint(20) == 1)
+                               if (one_in_(20))
+                               {
                                        create_artifact(o_ptr, FALSE);
-                               else
+                                       break;
+                               }
+                               o_ptr->name2 = get_random_ego(INVEN_FEET, TRUE, level);
+
+                               switch (o_ptr->name2)
                                {
-                                       /* Roll for ego-item */
-                                       switch (randint(8))
+                               case EGO_SLOW_DESCENT:
+                                       if (one_in_(2))
                                        {
-                                               case 1:
-                                               {
-                                                       o_ptr->name2 = EGO_MAGI;
-                                                       random_resistance(o_ptr, FALSE, (randint(22) + 16));
-                                                       break;
-                                               }
-                                               case 2:
-                                               {
-                                                       o_ptr->name2 = EGO_MIGHT;
-                                                       random_resistance(o_ptr, FALSE, (randint(22) + 16));
-                                                       break;
-                                               }
-                                               case 3:
-                                               {
-                                                       o_ptr->name2 = EGO_TELEPATHY;
-                                                       break;
-                                               }
-                                               case 4:
-                                               {
-                                                       o_ptr->name2 = EGO_REGENERATION;
-                                                       break;
-                                               }
-                                               case 5: case 6:
-                                               {
-                                                       o_ptr->name2 = EGO_LORDLINESS;
-                                                       random_resistance(o_ptr, FALSE, (randint(22) + 16));
-                                                       break;
-                                               }
-                                               default:
-                                               {
-                                                       o_ptr->name2 = EGO_SEEING;
-                                                       if (randint(3) == 1) o_ptr->art_flags3 |= TR3_TELEPATHY;
-                                                       break;
-                                               }
+                                               one_high_resistance(o_ptr);
                                        }
+                                       break;
                                }
                        }
-
                        /* Very cursed */
                        else if (power < -1)
                        {
-                               /* Roll for ego-item */
-                               switch (randint(7))
+                               o_ptr->name2 = get_random_ego(INVEN_FEET, FALSE, level);
+                       }
+
+                       break;
+               }
+
+               case TV_CROWN:
+               {
+                       /* Very good */
+                       if (power > 1)
+                       {
+                               if (one_in_(20))
                                {
-                                       case 1: case 2:
-                                       {
-                                               o_ptr->name2 = EGO_STUPIDITY;
-                                               break;
-                                       }
-                                       case 3: case 4:
-                                       {
-                                               o_ptr->name2 = EGO_NAIVETY;
-                                               break;
-                                       }
-                                       case 5:
-                                       {
-                                               o_ptr->name2 = EGO_UGLINESS;
-                                               break;
-                                       }
-                                       case 6:
+                                       create_artifact(o_ptr, FALSE);
+                                       break;
+                               }
+                               while (1)
+                               {
+                                       bool ok_flag = TRUE;
+                                       o_ptr->name2 = get_random_ego(INVEN_HEAD, TRUE, level);
+
+                                       switch (o_ptr->name2)
                                        {
-                                               o_ptr->name2 = EGO_SICKLINESS;
+                                       case EGO_MAGI:
+                                       case EGO_MIGHT:
+                                       case EGO_TELEPATHY:
+                                       case EGO_REGENERATION:
+                                       case EGO_LORDLINESS:
                                                break;
-                                       }
-                                       case 7:
-                                       {
-                                               o_ptr->name2 = EGO_TELEPORTATION;
+                                       case EGO_SEEING:
+                                               if (one_in_(3)) o_ptr->art_flags3 |= TR3_TELEPATHY;
                                                break;
+                                       default:/* not existing crown (wisdom,lite, etc...) */
+                                               ok_flag = FALSE;
                                        }
+                                       if (ok_flag)
+                                               break; /* while (1) */
                                }
+                               break;
+                       }
+
+                       /* Very cursed */
+                       else if (power < -1)
+                       {
+                               o_ptr->name2 = get_random_ego(INVEN_HEAD, FALSE, level);
                        }
 
                        break;
@@ -3171,175 +2789,75 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                /* Mention the item */
                                if (cheat_peek) object_mention(o_ptr);
                                dragon_resist(o_ptr);
-                               if (randint(3) != 1) break;
+                               if (!one_in_(3)) break;
                        }
+
+                       /* Very good */
+                       if (power > 1)
                        {
-                               /* Very good */
-                               if (power > 1)
+                               if (one_in_(20))
                                {
-                                       if (randint(20) == 1)
-                                               create_artifact(o_ptr, FALSE);
-                                       else
-                                       {
-                                               /* Roll for ego-item */
-                                               switch (randint(24))
-                                               {
-                                                       case 1: case 2: case 3:
-                                                       {
-                                                               o_ptr->name2 = EGO_INTELLIGENCE;
-                                                               break;
-                                                       }
-                                                       case 4: case 5: case 6:
-                                                       {
-                                                               o_ptr->name2 = EGO_WISDOM;
-                                                               break;
-                                                       }
-                                                       case 7: case 8: case 9:
-                                                       {
-                                                               o_ptr->name2 = EGO_BEAUTY;
-                                                               break;
-                                                       }
-                                                       case 10: case 11: case 12:
-                                                       {
-                                                               o_ptr->name2 = EGO_SEEING;
-                                                               if (randint(7) == 1) o_ptr->art_flags3 |= TR3_TELEPATHY;
-                                                               break;
-                                                       }
-                                                       case 13: case 14: case 15:
-                                                       {
-                                                               o_ptr->name2 = EGO_LITE;
-                                                               break;
-                                                       }
-                                                       case 16: case 17: case 18:
-                                                       {
-                                                               o_ptr->name2 = EGO_DARK;
-                                                               break;
-                                                       }
-                                                       default:
-                                                       {
-                                                               o_ptr->name2 = EGO_INFRAVISION;
-                                                               break;
-                                                       }
-                                               }
-                                       }
+                                       create_artifact(o_ptr, FALSE);
+                                       break;
                                }
-
-                               /* Very cursed */
-                               else if (power < -1)
+                               while (1)
                                {
-                                       /* Roll for ego-item */
-                                       switch (randint(7))
+                                       bool ok_flag = TRUE;
+                                       o_ptr->name2 = get_random_ego(INVEN_HEAD, TRUE, level);
+
+                                       switch (o_ptr->name2)
                                        {
-                                               case 1: case 2:
-                                               {
-                                                       o_ptr->name2 = EGO_STUPIDITY;
-                                                       break;
-                                               }
-                                               case 3: case 4:
-                                               {
-                                                       o_ptr->name2 = EGO_NAIVETY;
-                                                       break;
-                                               }
-                                               case 5:
-                                               {
-                                                       o_ptr->name2 = EGO_UGLINESS;
-                                                       break;
-                                               }
-                                               case 6:
-                                               {
-                                                       o_ptr->name2 = EGO_SICKLINESS;
-                                                       break;
-                                               }
-                                               case 7:
-                                               {
-                                                       o_ptr->name2 = EGO_TELEPORTATION;
-                                                       break;
-                                               }
+                                       case EGO_INTELLIGENCE:
+                                       case EGO_WISDOM:
+                                       case EGO_BEAUTY:
+                                       case EGO_LITE:
+                                       case EGO_INFRAVISION:
+                                               break;
+                                       case EGO_SEEING:
+                                               if (one_in_(7)) o_ptr->art_flags3 |= TR3_TELEPATHY;
+                                               break;
+                                       default:/* not existing helm (Magi, Might, etc...)*/
+                                               ok_flag = FALSE;
                                        }
+                                       if (ok_flag)
+                                               break; /* while (1) */
                                }
+                               break;
+                       }
+                       /* Very cursed */
+                       else if (power < -1)
+                       {
+                               o_ptr->name2 = get_random_ego(INVEN_HEAD, FALSE, level);
                        }
                        break;
                }
 
                case TV_CLOAK:
                {
-                       if (o_ptr->sval == SV_ELVEN_CLOAK)
-                               o_ptr->pval = randint(4); /* No cursed elven cloaks...? */
-
                        /* Very good */
                        if (power > 1)
                        {
-                               if (randint(20) == 1)
+                               if (one_in_(20))
+                               {
                                        create_artifact(o_ptr, FALSE);
-                               else
+                                       break;
+                               }
+                               o_ptr->name2 = get_random_ego(INVEN_OUTER, TRUE, level);
+
+                               switch (o_ptr->name2)
                                {
-                                       /* Roll for ego-item */
-                                       switch (randint(20))
-                                       {
-                                               case 1: case 2: case 3: case 4:
-                                               case 5: case 6: case 7: case 8:
-                                               {
-                                                       o_ptr->name2 = EGO_PROTECTION;
-                                                       break;
-                                               }
-                                               case 9: case 10: case 11: case 12:
-                                               case 13: case 14: case 15:
-                                               {
-                                                       o_ptr->name2 = EGO_STEALTH;
-                                                       break;
-                                               }
-                                               case 16:
-                                               {
-                                                       o_ptr->name2 = EGO_BAT;
-                                                       o_ptr->to_d -= 6;
-                                                       o_ptr->to_h -= 6;
-                                                       break;
-                                               }
-                                               case 17:
-                                               {
-                                                       o_ptr->name2 = EGO_AMAN;
-                                                       break;
-                                               }
-                                               case 18:
-                                               {
-                                                       o_ptr->name2 = EGO_AURA_ELEC;
-                                                       break;
-                                               }
-                                               case 19:
-                                               {
-                                                       o_ptr->name2 = EGO_AURA_COLD;
-                                                       break;
-                                               }
-                                               default:
-                                               {
-                                                       o_ptr->name2 = EGO_AURA_FIRE;
-                                               }
-                                       }
+                               case EGO_BAT:
+                                       o_ptr->to_d -= 6;
+                                       o_ptr->to_h -= 6;
+                                       break;
                                }
+
                        }
 
                        /* Very cursed */
                        else if (power < -1)
                        {
-                               /* Choose some damage */
-                               switch (randint(3))
-                               {
-                                       case 1:
-                                       {
-                                               o_ptr->name2 = EGO_IRRITATION;
-                                               break;
-                                       }
-                                       case 2:
-                                       {
-                                               o_ptr->name2 = EGO_VULNERABILITY;
-                                               break;
-                                       }
-                                       case 3:
-                                       {
-                                               o_ptr->name2 = EGO_ENVELOPING;
-                                               break;
-                                       }
-                               }
+                               o_ptr->name2 = get_random_ego(INVEN_OUTER, FALSE, level);
                        }
 
                        break;
@@ -3358,9 +2876,6 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
  */
 static void a_m_aux_3(object_type *o_ptr, int level, int power)
 {
-
-       artifact_bias = 0;
-
        /* Apply magic (good or bad) according to type */
        switch (o_ptr->tval)
        {
@@ -3383,7 +2898,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse pval */
                                                o_ptr->pval = 0 - (o_ptr->pval);
@@ -3412,7 +2927,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse pval */
                                                o_ptr->pval = 0 - (o_ptr->pval);
@@ -3425,10 +2940,10 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                case SV_RING_SPEED:
                                {
                                        /* Base speed (1 to 10) */
-                                       o_ptr->pval = randint(5) + m_bonus(5, level);
+                                       o_ptr->pval = randint1(5) + m_bonus(5, level);
 
                                        /* Super-charge the ring */
-                                       while (rand_int(100) < 50) o_ptr->pval++;
+                                       while (randint0(100) < 50) o_ptr->pval++;
 
                                        /* Cursed Ring */
                                        if (power < 0)
@@ -3437,7 +2952,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse pval */
                                                o_ptr->pval = 0 - (o_ptr->pval);
@@ -3458,12 +2973,12 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                {
                                        do
                                        {
-                                               random_resistance(o_ptr, FALSE, randint(20) + 18);
+                                               one_lordly_high_resistance(o_ptr);
                                        }
-                                       while (randint(4) == 1);
+                                       while (one_in_(4));
 
                                        /* Bonus to armor class */
-                                       o_ptr->to_a = 10 + randint(5) + m_bonus(10, level);
+                                       o_ptr->to_a = 10 + randint1(5) + m_bonus(10, level);
                                        rating += 15;
                                }
                                break;
@@ -3481,7 +2996,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse pval */
                                                o_ptr->pval = 0 - (o_ptr->pval);
@@ -3497,7 +3012,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                case SV_RING_ELEC:
                                {
                                        /* Bonus to armor class */
-                                       o_ptr->to_a = 5 + randint(5) + m_bonus(10, level);
+                                       o_ptr->to_a = 5 + randint1(5) + m_bonus(10, level);
                                        break;
                                }
 
@@ -3509,7 +3024,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        o_ptr->ident |= (IDENT_BROKEN);
 
                                        /* Cursed */
-                                       o_ptr->ident |= (IDENT_CURSED);
+                                       o_ptr->curse_flags |= TRC_CURSED;
 
                                        /* Penalize */
                                        o_ptr->pval = 0 - (1 + m_bonus(5, level));
@@ -3525,7 +3040,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        o_ptr->ident |= (IDENT_BROKEN);
 
                                        /* Cursed */
-                                       o_ptr->ident |= (IDENT_CURSED);
+                                       o_ptr->curse_flags |= TRC_CURSED;
 
                                        /* Penalize */
                                        o_ptr->to_a = 0 - (5 + m_bonus(10, level));
@@ -3539,7 +3054,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                case SV_RING_DAMAGE:
                                {
                                        /* Bonus to damage */
-                                       o_ptr->to_d = 1 + randint(5) + m_bonus(16, level);
+                                       o_ptr->to_d = 1 + randint1(5) + m_bonus(16, level);
 
                                        /* Cursed */
                                        if (power < 0)
@@ -3548,7 +3063,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse bonus */
                                                o_ptr->to_d = 0 - o_ptr->to_d;
@@ -3561,7 +3076,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                case SV_RING_ACCURACY:
                                {
                                        /* Bonus to hit */
-                                       o_ptr->to_h = 1 + randint(5) + m_bonus(16, level);
+                                       o_ptr->to_h = 1 + randint1(5) + m_bonus(16, level);
 
                                        /* Cursed */
                                        if (power < 0)
@@ -3570,7 +3085,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse tohit */
                                                o_ptr->to_h = 0 - o_ptr->to_h;
@@ -3583,7 +3098,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                case SV_RING_PROTECTION:
                                {
                                        /* Bonus to armor class */
-                                       o_ptr->to_a = 5 + randint(8) + m_bonus(10, level);
+                                       o_ptr->to_a = 5 + randint1(8) + m_bonus(10, level);
 
                                        /* Cursed */
                                        if (power < 0)
@@ -3592,7 +3107,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse toac */
                                                o_ptr->to_a = 0 - o_ptr->to_a;
@@ -3605,8 +3120,8 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                case SV_RING_SLAYING:
                                {
                                        /* Bonus to damage and to hit */
-                                       o_ptr->to_d = randint(5) + m_bonus(12, level);
-                                       o_ptr->to_h = randint(5) + m_bonus(12, level);
+                                       o_ptr->to_d = randint1(5) + m_bonus(12, level);
+                                       o_ptr->to_h = randint1(5) + m_bonus(12, level);
 
                                        /* Cursed */
                                        if (power < 0)
@@ -3615,7 +3130,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse bonuses */
                                                o_ptr->to_h = 0 - o_ptr->to_h;
@@ -3637,7 +3152,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse bonuses */
                                                o_ptr->pval = 0 - o_ptr->pval;
@@ -3651,13 +3166,13 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        o_ptr->ident |= (IDENT_BROKEN);
 
                                        /* Cursed */
-                                       o_ptr->ident |= (IDENT_CURSED);
+                                       o_ptr->curse_flags |= TRC_CURSED;
 
                                        if (power > 0) power = 0 - power;
                                        break;
                                }
                        }
-                       if (randint(400) == 1 && (power > 0) && !(o_ptr->ident & IDENT_CURSED) && (level > 79))
+                       if (one_in_(400) && (power > 0) && !cursed_p(o_ptr) && (level > 79))
                        {
                                o_ptr->pval = MIN(o_ptr->pval,4);
                                /* Randart amulet */
@@ -3669,7 +3184,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                {
                                        int tmp = m_bonus(10, level);
                                        object_kind *k_ptr = &k_info[o_ptr->k_idx];
-                                       switch(randint(28))
+                                       switch(randint1(28))
                                        {
                                        case 1: case 2:
                                                o_ptr->name2 = EGO_RING_THROW;
@@ -3753,7 +3268,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                        else
                                                        {
                                                                o_ptr->name2 = EGO_RING_BERSERKER;
-                                                               o_ptr->to_h -= 2+randint(4);
+                                                               o_ptr->to_h -= 2+randint1(4);
                                                        }
                                                        break;
                                                case SV_RING_PROTECTION:
@@ -3781,8 +3296,8 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                        break;
                                                case SV_RING_LORDLY:
                                                        if (!one_in_(20)) break;
-                                                       random_resistance(o_ptr, FALSE, randint(20) + 18);
-                                                       random_resistance(o_ptr, FALSE, randint(20) + 18);
+                                                       one_lordly_high_resistance(o_ptr);
+                                                       one_lordly_high_resistance(o_ptr);
                                                        o_ptr->name2 = EGO_RING_TRUE;
                                                        break;
                                                case SV_RING_SUSTAIN:
@@ -3808,13 +3323,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        }
                                }
                                /* Uncurse it */
-                               o_ptr->ident &= ~(IDENT_CURSED);
-
-                               if (o_ptr->art_flags3 & TR3_CURSED)
-                                       o_ptr->art_flags3 &= ~(TR3_CURSED);
-
-                               if (o_ptr->art_flags3 & TR3_HEAVY_CURSE)
-                                       o_ptr->art_flags3 &= ~(TR3_HEAVY_CURSE);
+                               o_ptr->curse_flags = 0L;
                        }
                        else if ((power == -2) && one_in_(2))
                        {
@@ -3827,7 +3336,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                while(!o_ptr->name2)
                                {
                                        object_kind *k_ptr = &k_info[o_ptr->k_idx];
-                                       switch(randint(5))
+                                       switch(randint1(5))
                                        {
                                        case 1:
                                                if (k_ptr->flags3 & TR3_DRAIN_EXP) break;
@@ -3853,7 +3362,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                o_ptr->ident |= (IDENT_BROKEN);
 
                                /* Cursed */
-                               o_ptr->ident |= (IDENT_CURSED);
+                               o_ptr->curse_flags |= (TRC_CURSED | TRC_HEAVY_CURSE);
                        }
                        break;
                }
@@ -3877,7 +3386,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= (TRC_CURSED);
 
                                                /* Reverse bonuses */
                                                o_ptr->pval = 0 - o_ptr->pval;
@@ -3899,7 +3408,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= (TRC_CURSED);
 
                                                /* Reverse bonuses */
                                                o_ptr->pval = 0 - o_ptr->pval;
@@ -3912,22 +3421,22 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                {
                                        if (power < 0)
                                        {
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= (TRC_CURSED);
                                        }
                                        break;
                                }
 
                                case SV_AMULET_RESISTANCE:
                                {
-                                       if (randint(3) == 1) random_resistance(o_ptr, FALSE, (randint(34) + 4));
-                                       if (randint(5) == 1) o_ptr->art_flags2 |= TR2_RES_POIS;
+                                       if (one_in_(5)) one_high_resistance(o_ptr);
+                                       if (one_in_(5)) o_ptr->art_flags2 |= TR2_RES_POIS;
                                }
                                break;
 
                                /* Amulet of searching */
                                case SV_AMULET_SEARCHING:
                                {
-                                       o_ptr->pval = randint(2) + m_bonus(4, level);
+                                       o_ptr->pval = randint1(2) + m_bonus(4, level);
 
                                        /* Cursed */
                                        if (power < 0)
@@ -3936,7 +3445,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= (TRC_CURSED);
 
                                                /* Reverse bonuses */
                                                o_ptr->pval = 0 - (o_ptr->pval);
@@ -3948,8 +3457,8 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                /* Amulet of the Magi -- never cursed */
                                case SV_AMULET_THE_MAGI:
                                {
-                                       o_ptr->pval = randint(5) + m_bonus(5, level);
-                                       o_ptr->to_a = randint(5) + m_bonus(5, level);
+                                       o_ptr->pval = randint1(5) + m_bonus(5, level);
+                                       o_ptr->to_a = randint1(5) + m_bonus(5, level);
 
                                        /* Boost the rating */
                                        rating += 15;
@@ -3967,11 +3476,11 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        o_ptr->ident |= (IDENT_BROKEN);
 
                                        /* Cursed */
-                                       o_ptr->ident |= (IDENT_CURSED);
+                                       o_ptr->curse_flags |= (TRC_CURSED);
 
                                        /* Penalize */
-                                       o_ptr->pval = 0 - (randint(5) + m_bonus(5, level));
-                                       o_ptr->to_a = 0 - (randint(5) + m_bonus(5, level));
+                                       o_ptr->pval = 0 - (randint1(5) + m_bonus(5, level));
+                                       o_ptr->to_a = 0 - (randint1(5) + m_bonus(5, level));
                                        if (power > 0) power = 0 - power;
 
                                        break;
@@ -3988,7 +3497,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= (TRC_CURSED);
 
                                                /* Reverse bonuses */
                                                o_ptr->pval = 0 - o_ptr->pval;
@@ -3997,7 +3506,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        break;
                                }
                        }
-                       if (randint(150) == 1 && (power > 0) && !(o_ptr->ident & IDENT_CURSED) && (level > 79))
+                       if (one_in_(150) && (power > 0) && !cursed_p(o_ptr) && (level > 79))
                        {
                                o_ptr->pval = MIN(o_ptr->pval,4);
                                /* Randart amulet */
@@ -4008,7 +3517,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                while(!o_ptr->name2)
                                {
                                        object_kind *k_ptr = &k_info[o_ptr->k_idx];
-                                       switch(randint(21))
+                                       switch(randint1(21))
                                        {
                                        case 1: case 2:
                                                if (k_ptr->flags3 & TR3_SLOW_DIGEST) break;
@@ -4099,13 +3608,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        }
                                }
                                /* Uncurse it */
-                               o_ptr->ident &= ~(IDENT_CURSED);
-
-                               if (o_ptr->art_flags3 & TR3_CURSED)
-                                       o_ptr->art_flags3 &= ~(TR3_CURSED);
-
-                               if (o_ptr->art_flags3 & TR3_HEAVY_CURSE)
-                                       o_ptr->art_flags3 &= ~(TR3_HEAVY_CURSE);
+                               o_ptr->curse_flags = 0L;
                        }
                        else if ((power == -2) && one_in_(2))
                        {
@@ -4118,7 +3621,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                while(!o_ptr->name2)
                                {
                                        object_kind *k_ptr = &k_info[o_ptr->k_idx];
-                                       switch(randint(5))
+                                       switch(randint1(5))
                                        {
                                        case 1:
                                                if (k_ptr->flags3 & TR3_DRAIN_EXP) break;
@@ -4144,7 +3647,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                o_ptr->ident |= (IDENT_BROKEN);
 
                                /* Cursed */
-                               o_ptr->ident |= (IDENT_CURSED);
+                               o_ptr->curse_flags |= (TRC_CURSED | TRC_HEAVY_CURSE);
                        }
                        break;
                }
@@ -4194,7 +3697,7 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
                                o_ptr->ident |= (IDENT_BROKEN);
 
                                /* Cursed */
-                               o_ptr->ident |= (IDENT_CURSED);
+                               o_ptr->curse_flags |= (TRC_CURSED);
                        }
 #endif
                        break;
@@ -4210,14 +3713,14 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
                        /* Hack -- Torches -- random fuel */
                        if (o_ptr->sval == SV_LITE_TORCH)
                        {
-                               if (o_ptr->pval > 0) o_ptr->xtra4 = randint(o_ptr->pval);
+                               if (o_ptr->pval > 0) o_ptr->xtra4 = randint1(o_ptr->pval);
                                o_ptr->pval = 0;
                        }
 
                        /* Hack -- Lanterns -- random fuel */
                        if (o_ptr->sval == SV_LITE_LANTERN)
                        {
-                               if (o_ptr->pval > 0) o_ptr->xtra4 = randint(o_ptr->pval);
+                               if (o_ptr->pval > 0) o_ptr->xtra4 = randint1(o_ptr->pval);
                                o_ptr->pval = 0;
                        }
 
@@ -4225,34 +3728,33 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
                        {
                                while (!o_ptr->name2)
                                {
-                                       switch(randint(13))
+                                       while (1)
                                        {
-                                       case 1: case 2: case 3: case 4:
-                                               o_ptr->name2 = EGO_LITE_SHINE;
-                                               break;
-                                       case 5:
-                                               o_ptr->name2 = EGO_LITE_ILLUMINATION;
-                                               break;
-                                       case 6:
-                                               o_ptr->name2 = EGO_LITE_AURA_FIRE;
-                                               break;
-                                       case 7: case 8:
-                                               o_ptr->name2 = EGO_LITE_INFRA;
-                                               break;
-                                       case 9: case 10: case 11: case 12:
-                                               if (o_ptr->sval == SV_LITE_FEANOR) break;
-                                               o_ptr->name2 = EGO_LITE_LONG;
-                                               break;
-                                       case 13:
-                                               o_ptr->name2 = EGO_LITE_EYE;
-                                               break;
+                                               bool okay_flag = TRUE;
+
+                                               o_ptr->name2 = get_random_ego(INVEN_LITE, TRUE, level);
+
+                                               switch (o_ptr->name2)
+                                               {
+                                               case EGO_LITE_LONG:
+                                                       if (o_ptr->sval == SV_LITE_FEANOR)
+                                                               okay_flag = FALSE;
+                                               }
+                                               if (okay_flag)
+                                                       break;
                                        }
                                }
                        }
                        else if (power == -2)
                        {
-                               o_ptr->name2 = EGO_LITE_DARKNESS;
-                               o_ptr->xtra4 = 0;
+                               o_ptr->name2 = get_random_ego(INVEN_LITE, FALSE, level);
+
+                               switch (o_ptr->name2)
+                               {
+                               case EGO_LITE_DARKNESS:
+                                       o_ptr->xtra4 = 0;
+                                       break;
+                               }
                        }
 
                        break;
@@ -4264,7 +3766,7 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
                        /* The wand or staff gets a number of initial charges equal
                         * to between 1/2 (+1) and the full object kind's pval. -LM-
                         */
-                       o_ptr->pval = k_ptr->pval / 2 + randint((k_ptr->pval + 1) / 2);
+                       o_ptr->pval = k_ptr->pval / 2 + randint1((k_ptr->pval + 1) / 2);
                        break;
                }
 
@@ -4293,7 +3795,7 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
                        /* Pick a random non-unique monster race */
                        while (1)
                        {
-                               i = randint(max_r_idx - 1);
+                               i = randint1(max_r_idx - 1);
 
                                if (!item_monster_okay(i)) continue;
                                if (i == MON_TSUCHINOKO) continue;
@@ -4306,7 +3808,7 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
                                if (!r_ptr->rarity) continue;
 
                                /* Prefer less out-of-depth monsters */
-                               if (rand_int(check)) continue;
+                               if (randint0(check)) continue;
 
                                break;
                        }
@@ -4314,18 +3816,18 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
                        o_ptr->pval = i;
 
                        /* Some figurines are cursed */
-                       if (one_in_(6)) o_ptr->ident |= IDENT_CURSED;
+                       if (one_in_(6)) o_ptr->curse_flags |= TRC_CURSED;
 
                        if (cheat_peek)
                        {
 #ifdef JP
-msg_format("%s¤Î¿Í·Á, ¿¼¤µ +%d%s",
+                               msg_format("%s¤Î¿Í·Á, ¿¼¤µ +%d%s",
 #else
                                msg_format("Figurine of %s, depth +%d%s",
 #endif
 
                                                          r_name + r_ptr->name, check - 1,
-                                                         !(o_ptr->ident & IDENT_CURSED) ? "" : " {cursed}");
+                                                         !cursed_p(o_ptr) ? "" : " {cursed}");
                        }
 
                        break;
@@ -4368,7 +3870,7 @@ msg_format("%s
                                if (!(r_ptr->flags9 & match)) continue;
 
                                /* Prefer less out-of-depth monsters */
-                               if (rand_int(check)) continue;
+                               if (randint0(check)) continue;
 
                                break;
                        }
@@ -4378,7 +3880,7 @@ msg_format("%s
                        if (cheat_peek)
                        {
 #ifdef JP
-msg_format("%s¤Î»àÂÎ,¿¼¤µ +%d",
+                               msg_format("%s¤Î»àÂÎ,¿¼¤µ +%d",
 #else
                                msg_format("Corpse of %s, depth +%d",
 #endif
@@ -4400,7 +3902,7 @@ msg_format("%s
                        /* Pick a random monster race */
                        while (1)
                        {
-                               i = randint(max_r_idx - 1);
+                               i = randint1(max_r_idx - 1);
 
                                r_ptr = &r_info[i];
 
@@ -4415,7 +3917,7 @@ msg_format("%s
                        if (cheat_peek)
                        {
 #ifdef JP
-msg_format("%s¤ÎÁü,", r_name + r_ptr->name);
+                               msg_format("%s¤ÎÁü,", r_name + r_ptr->name);
 #else
                                msg_format("Statue of %s", r_name + r_ptr->name);
 #endif
@@ -4435,13 +3937,13 @@ msg_format("%s
                        if (obj_level <= 0) break;
 
                        /* Hack -- pick a "difficulty" */
-                       o_ptr->pval = randint(obj_level);
+                       o_ptr->pval = randint1(obj_level);
                        if (o_ptr->sval == SV_CHEST_KANDUME) o_ptr->pval = 6;
 
                        o_ptr->xtra3 = dun_level + 5;
 
                        /* Never exceed "difficulty" of 55 to 59 */
-                       if (o_ptr->pval > 55) o_ptr->pval = 55 + (byte)rand_int(5);
+                       if (o_ptr->pval > 55) o_ptr->pval = 55 + (byte)randint0(5);
 
                        break;
                }
@@ -4485,7 +3987,7 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
 
        int i, rolls, f1, f2, power;
 
-       if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN) lev += rand_int(p_ptr->lev/2+10);
+       if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN) lev += randint0(p_ptr->lev/2+10);
 
        /* Maximum "level" for various things */
        if (lev > MAX_DEPTH - 1) lev = MAX_DEPTH - 1;
@@ -4598,7 +4100,12 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
                if (!a_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
 
                /* Hack -- extract the "cursed" flag */
-               if (a_ptr->flags3 & TR3_CURSED) o_ptr->ident |= (IDENT_CURSED);
+               if (a_ptr->gen_flags & TRG_CURSED) o_ptr->curse_flags |= (TRC_CURSED);
+               if (a_ptr->gen_flags & TRG_HEAVY_CURSE) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
+               if (a_ptr->gen_flags & TRG_PERMA_CURSE) o_ptr->curse_flags |= (TRC_PERMA_CURSE);
+               if (a_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
+               if (a_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
+               if (a_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
 
                /* Mega-Hack -- increase the rating */
                rating += 10;
@@ -4656,13 +4163,17 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
                case TV_GLOVES:
                case TV_BOOTS:
                {
+                       /* Elven Cloak and Black Clothes ... */
+                       if (((o_ptr->tval == TV_CLOAK) && (o_ptr->sval == SV_ELVEN_CLOAK)) ||
+                           ((o_ptr->tval == TV_SOFT_ARMOR) && (o_ptr->sval == SV_KUROSHOUZOKU)))
+                               o_ptr->pval = randint1(4);
+
 #if 1
                        if (power ||
                             ((o_ptr->tval == TV_HELM) && (o_ptr->sval == SV_DRAGON_HELM)) ||
                             ((o_ptr->tval == TV_SHIELD) && (o_ptr->sval == SV_DRAGON_SHIELD)) ||
                             ((o_ptr->tval == TV_GLOVES) && (o_ptr->sval == SV_SET_OF_DRAGON_GLOVES)) ||
-                            ((o_ptr->tval == TV_BOOTS) && (o_ptr->sval == SV_PAIR_OF_DRAGON_GREAVE)) ||
-                            ((o_ptr->tval == TV_CLOAK) && (o_ptr->sval == SV_ELVEN_CLOAK)))
+                            ((o_ptr->tval == TV_BOOTS) && (o_ptr->sval == SV_PAIR_OF_DRAGON_GREAVE)))
                                a_m_aux_2(o_ptr, lev, power);
 #else
                        if (power) a_m_aux_2(o_ptr, lev, power);
@@ -4673,7 +4184,7 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
                case TV_RING:
                case TV_AMULET:
                {
-                       if (!power && (rand_int(100) < 50)) power = -1;
+                       if (!power && (randint0(100) < 50)) power = -1;
                        a_m_aux_3(o_ptr, lev, power);
                        break;
                }
@@ -4700,93 +4211,35 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
        {
                ego_item_type *e_ptr = &e_info[o_ptr->name2];
 
-               /* Hack -- extra powers */
-               switch (o_ptr->name2)
-               {
-                       /* Weapon (Holy Avenger) */
-                       case EGO_HA:
-                       {
-                               o_ptr->xtra1 = EGO_XTRA_SUSTAIN;
-                               break;
-                       }
-
-                       /* Weapon (Defender) */
-                       case EGO_DF:
-                       {
-                               o_ptr->xtra1 = EGO_XTRA_SUSTAIN;
-                               break;
-                       }
-
-                       /* Weapon (Blessed) */
-                       case EGO_BLESS_BLADE:
-                       {
-                               o_ptr->xtra1 = EGO_XTRA_ABILITY;
-                               break;
-                       }
-
-                       /* Trump weapon */
-                       case EGO_TRUMP:
-                       {
-                               if (randint(7) == 1) o_ptr->xtra1 = EGO_XTRA_ABILITY;
-                               break;
-                       }
-
-                       /* Robe of Permanance */
-                       case EGO_PERMANENCE:
-                       {
-                               o_ptr->xtra1 = EGO_XTRA_POWER;
-                               break;
-                       }
-
-                       /* Armor of Elvenkind */
-                       case EGO_ELVENKIND:
-                       {
-                               o_ptr->xtra1 = EGO_XTRA_POWER;
-                               break;
-                       }
-
-                       /* Crown of the Magi */
-                       case EGO_MAGI:
-                       {
-                               o_ptr->xtra1 = EGO_XTRA_ABILITY;
-                               break;
-                       }
-
-                       /* Cloak of Aman */
-                       case EGO_AMAN:
-                       {
-                               o_ptr->xtra1 = EGO_XTRA_POWER;
-                               break;
-                       }
-
-                       case EGO_DWARVEN:
-                       {
-                               o_ptr->weight = (2 * k_info[o_ptr->k_idx].weight / 3);
-                               o_ptr->ac = k_info[o_ptr->k_idx].ac + 5;
-                               break;
-                       }
-               }
-
-               /* Randomize the "xtra" power */
-               if (o_ptr->xtra1 && !o_ptr->art_name)
-                       o_ptr->xtra2 = randint(256);
-
                /* Hack -- acquire "broken" flag */
                if (!e_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
 
                /* Hack -- acquire "cursed" flag */
-               if (e_ptr->flags3 & (TR3_CURSED)) o_ptr->ident |= (IDENT_CURSED);
+               if (e_ptr->gen_flags & TRG_CURSED) o_ptr->curse_flags |= (TRC_CURSED);
+               if (e_ptr->gen_flags & TRG_HEAVY_CURSE) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
+               if (e_ptr->gen_flags & TRG_PERMA_CURSE) o_ptr->curse_flags |= (TRC_PERMA_CURSE);
+               if (e_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
+               if (e_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
+               if (e_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
+
+               if (e_ptr->gen_flags & (TRG_ONE_SUSTAIN)) one_sustain(o_ptr);
+               if (e_ptr->gen_flags & (TRG_XTRA_POWER)) one_ability(o_ptr);
+               if (e_ptr->gen_flags & (TRG_XTRA_H_RES)) one_high_resistance(o_ptr);
+               if (e_ptr->gen_flags & (TRG_XTRA_E_RES)) one_ele_resistance(o_ptr);
+               if (e_ptr->gen_flags & (TRG_XTRA_D_RES)) one_dragon_ele_resistance(o_ptr);
+               if (e_ptr->gen_flags & (TRG_XTRA_L_RES)) one_lordly_high_resistance(o_ptr);
+               if (e_ptr->gen_flags & (TRG_XTRA_RES)) one_resistance(o_ptr);
 
                /* Hack -- apply extra penalties if needed */
                if (cursed_p(o_ptr) || broken_p(o_ptr))
                {
                        /* Hack -- obtain bonuses */
-                       if (e_ptr->max_to_h) o_ptr->to_h -= randint(e_ptr->max_to_h);
-                       if (e_ptr->max_to_d) o_ptr->to_d -= randint(e_ptr->max_to_d);
-                       if (e_ptr->max_to_a) o_ptr->to_a -= randint(e_ptr->max_to_a);
+                       if (e_ptr->max_to_h) o_ptr->to_h -= randint1(e_ptr->max_to_h);
+                       if (e_ptr->max_to_d) o_ptr->to_d -= randint1(e_ptr->max_to_d);
+                       if (e_ptr->max_to_a) o_ptr->to_a -= randint1(e_ptr->max_to_a);
 
                        /* Hack -- obtain pval */
-                       if (e_ptr->max_pval) o_ptr->pval -= randint(e_ptr->max_pval);
+                       if (e_ptr->max_pval) o_ptr->pval -= randint1(e_ptr->max_pval);
                }
 
                /* Hack -- apply extra bonuses if needed */
@@ -4796,20 +4249,20 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
                        if (e_ptr->max_to_h)
                        {
                                if (e_ptr->max_to_h > 127)
-                                       o_ptr->to_h -= randint(256-e_ptr->max_to_h);
-                               else o_ptr->to_h += randint(e_ptr->max_to_h);
+                                       o_ptr->to_h -= randint1(256-e_ptr->max_to_h);
+                               else o_ptr->to_h += randint1(e_ptr->max_to_h);
                        }
                        if (e_ptr->max_to_d)
                        {
                                if (e_ptr->max_to_d > 127)
-                                       o_ptr->to_d -= randint(256-e_ptr->max_to_d);
-                               else o_ptr->to_d += randint(e_ptr->max_to_d);
+                                       o_ptr->to_d -= randint1(256-e_ptr->max_to_d);
+                               else o_ptr->to_d += randint1(e_ptr->max_to_d);
                        }
                        if (e_ptr->max_to_a)
                        {
                                if (e_ptr->max_to_a > 127)
-                                       o_ptr->to_a -= randint(256-e_ptr->max_to_a);
-                               else o_ptr->to_a += randint(e_ptr->max_to_a);
+                                       o_ptr->to_a -= randint1(256-e_ptr->max_to_a);
+                               else o_ptr->to_a += randint1(e_ptr->max_to_a);
                        }
 
                        /* Hack -- obtain pval */
@@ -4822,24 +4275,24 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
                                }
                                else if (o_ptr->name2 == EGO_ATTACKS)
                                {
-                                       o_ptr->pval = randint(e_ptr->max_pval*lev/100+1);
+                                       o_ptr->pval = randint1(e_ptr->max_pval*lev/100+1);
                                        if (o_ptr->pval > 3) o_ptr->pval = 3;
                                        if ((o_ptr->tval == TV_SWORD) && (o_ptr->sval == SV_HAYABUSA))
-                                               o_ptr->pval += randint(2);
+                                               o_ptr->pval += randint1(2);
                                }
                                else if (o_ptr->name2 == EGO_BAT)
                                {
-                                       o_ptr->pval = randint(e_ptr->max_pval);
-                                       if (o_ptr->sval == SV_ELVEN_CLOAK) o_ptr->pval += randint(2);
+                                       o_ptr->pval = randint1(e_ptr->max_pval);
+                                       if (o_ptr->sval == SV_ELVEN_CLOAK) o_ptr->pval += randint1(2);
                                }
                                else
                                {
-                                       o_ptr->pval += randint(e_ptr->max_pval);
+                                       o_ptr->pval += randint1(e_ptr->max_pval);
                                }
                        }
                        if ((o_ptr->name2 == EGO_SPEED) && (lev < 50))
                        {
-                               o_ptr->pval = randint(o_ptr->pval);
+                               o_ptr->pval = randint1(o_ptr->pval);
                        }
                        if ((o_ptr->tval == TV_SWORD) && (o_ptr->sval == SV_HAYABUSA) && (o_ptr->pval > 2) && (o_ptr->name2 != EGO_ATTACKS))
                                o_ptr->pval = 2;
@@ -4864,7 +4317,12 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
                if (!get_object_cost(o_ptr)) o_ptr->ident |= (IDENT_BROKEN);
 
                /* Hack -- acquire "cursed" flag */
-               if (k_ptr->flags3 & (TR3_CURSED)) o_ptr->ident |= (IDENT_CURSED);
+               if (k_ptr->gen_flags & (TRG_CURSED)) o_ptr->curse_flags |= (TRC_CURSED);
+               if (k_ptr->gen_flags & (TRG_HEAVY_CURSE)) o_ptr->curse_flags |= TRC_HEAVY_CURSE;
+               if (k_ptr->gen_flags & (TRG_PERMA_CURSE)) o_ptr->curse_flags |= TRC_PERMA_CURSE;
+               if (k_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
+               if (k_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
+               if (k_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
        }
 }
 
@@ -4974,7 +4432,7 @@ bool make_object(object_type *j_ptr, bool good, bool great)
 
 
        /* Generate a special object, or a normal object */
-       if ((rand_int(prob) != 0) || !make_artifact_special(j_ptr))
+       if (!one_in_(prob) || !make_artifact_special(j_ptr))
        {
                int k_idx;
 
@@ -5106,10 +4564,6 @@ void place_object(int y, int x, bool good, bool great)
                /* Place the object */
                c_ptr->o_idx = o_idx;
 
-#ifdef USE_SCRIPT
-               o_ptr->python = object_create_callback(o_ptr);
-#endif /* USE_SCRIPT */
-
                /* Notice */
                note_spot(y, x);
 
@@ -5140,12 +4594,12 @@ bool make_gold(object_type *j_ptr)
 
 
        /* Hack -- Pick a Treasure variety */
-       i = ((randint(object_level + 2) + 2) / 2) - 1;
+       i = ((randint1(object_level + 2) + 2) / 2) - 1;
 
        /* Apply "extra" magic */
-       if (rand_int(GREAT_OBJ) == 0)
+       if (one_in_(GREAT_OBJ))
        {
-               i += randint(object_level + 1);
+               i += randint1(object_level + 1);
        }
 
        /* Hack -- Creeping Coins only generate "themselves" */
@@ -5161,7 +4615,7 @@ bool make_gold(object_type *j_ptr)
        base = k_info[OBJ_GOLD_LIST+i].cost;
 
        /* Determine how much the treasure is "worth" */
-       j_ptr->pval = (base + (8L * randint(base)) + randint(8));
+       j_ptr->pval = (base + (8L * randint1(base)) + randint1(8));
 
        /* Success */
        return (TRUE);
@@ -5227,10 +4681,6 @@ void place_gold(int y, int x)
                /* Place the object */
                c_ptr->o_idx = o_idx;
 
-#ifdef USE_SCRIPT
-               o_ptr->python = object_create_callback(o_ptr);
-#endif /* USE_SCRIPT */
-
                /* Notice */
                note_spot(y, x);
 
@@ -5287,7 +4737,7 @@ s16b drop_near(object_type *j_ptr, int chance, int y, int x)
 
 
        /* Handle normal "breakage" */
-       if (!(j_ptr->art_name || artifact_p(j_ptr)) && (rand_int(100) < chance))
+       if (!(j_ptr->art_name || artifact_p(j_ptr)) && (randint0(100) < chance))
        {
                /* Message */
 #ifdef JP
@@ -5300,7 +4750,7 @@ s16b drop_near(object_type *j_ptr, int chance, int y, int x)
 
                /* Debug */
 #ifdef JP
-if (wizard) msg_print("(ÇË»)");
+               if (wizard) msg_print("(ÇË»)");
 #else
                if (wizard) msg_print("(breakage)");
 #endif
@@ -5397,7 +4847,7 @@ if (wizard) msg_print("(
                        if (s > bs) bn = 0;
 
                        /* Apply the randomizer to equivalent values */
-                       if ((++bn >= 2) && (rand_int(bn) != 0)) continue;
+                       if ((++bn >= 2) && !one_in_(bn)) continue;
 
                        /* Keep score */
                        bs = s;
@@ -5426,7 +4876,7 @@ if (wizard) msg_print("(
 
                /* Debug */
 #ifdef JP
-if (wizard) msg_print("(¾²¥¹¥Ú¡¼¥¹¤¬¤Ê¤¤)");
+               if (wizard) msg_print("(¾²¥¹¥Ú¡¼¥¹¤¬¤Ê¤¤)");
 #else
                if (wizard) msg_print("(no floor space)");
 #endif
@@ -5450,8 +4900,8 @@ if (wizard) msg_print("(
                /* Random locations */
                else
                {
-                       ty = rand_int(cur_hgt);
-                       tx = rand_int(cur_wid);
+                       ty = randint0(cur_hgt);
+                       tx = randint0(cur_wid);
                }
 
                /* Grid */
@@ -5521,7 +4971,7 @@ if (wizard) msg_print("(
 
                /* Debug */
 #ifdef JP
-if (wizard) msg_print("(¥¢¥¤¥Æ¥à¤¬Â¿²á¤®¤ë)");
+               if (wizard) msg_print("(¥¢¥¤¥Æ¥à¤¬Â¿²á¤®¤ë)");
 #else
                if (wizard) msg_print("(too many objects)");
 #endif
@@ -5577,7 +5027,7 @@ if (wizard) msg_print("(
        if (chance && (by == py) && (bx == px))
        {
 #ifdef JP
-msg_print("²¿¤«¤¬Â­²¼¤Ëž¤¬¤Ã¤Æ¤­¤¿¡£");
+               msg_print("²¿¤«¤¬Â­²¼¤Ëž¤¬¤Ã¤Æ¤­¤¿¡£");
 #else
                msg_print("You feel something roll beneath your feet.");
 #endif
@@ -5617,17 +5067,13 @@ void acquirement(int y1, int x1, int num, bool great, bool known)
                        object_known(i_ptr);
                }
 
-#ifdef USE_SCRIPT
-               i_ptr->python = object_create_callback(i_ptr);
-#endif /* USE_SCRIPT */
-
                /* Drop the object */
                (void)drop_near(i_ptr, -1, y1, x1);
        }
 }
 
 
-#define MAX_TRAPS              17
+#define MAX_TRAPS              18
 
 static int trap_num[MAX_TRAPS] =
 {
@@ -5648,6 +5094,7 @@ static int trap_num[MAX_TRAPS] =
        FEAT_TRAP_POISON,
        FEAT_TRAP_SLEEP,
        FEAT_TRAP_TRAPS,
+       FEAT_TRAP_ALARM,
 };
 
 
@@ -5673,7 +5120,7 @@ void pick_trap(int y, int x)
        while (1)
        {
                /* Hack -- pick a trap */
-               feat = trap_num[rand_int(MAX_TRAPS)];
+               feat = trap_num[randint0(MAX_TRAPS)];
 
                /* Accept non-trapdoors */
                if (feat != FEAT_TRAP_TRAPDOOR) break;
@@ -5848,10 +5295,6 @@ void inven_item_optimize(int item)
                /* One less item */
                inven_cnt--;
 
-#ifdef USE_SCRIPT
-               object_delete_callback(&inventory[item]);
-#endif /* USE_SCRIPT */
-
                /* Slide everything down */
                for (i = item; i < INVEN_PACK; i++)
                {
@@ -5872,10 +5315,6 @@ void inven_item_optimize(int item)
                /* One less item */
                equip_cnt--;
 
-#ifdef USE_SCRIPT
-               object_delete_callback(&inventory[item]);
-#endif /* USE_SCRIPT */
-
                /* Erase the empty slot */
                object_wipe(&inventory[item]);
 
@@ -6176,11 +5615,6 @@ s16b inven_carry(object_type *o_ptr)
                        object_copy(&inventory[k+1], &inventory[k]);
                }
 
-#ifdef USE_SCRIPT
-               /* Not a real deletion */
-               /* object_delete_callback(&inventory[i]); */
-#endif /* USE_SCRIPT */
-
                /* Wipe the empty slot */
                object_wipe(&inventory[i]);
        }
@@ -6273,7 +5707,7 @@ s16b inven_takeoff(int item, int amt)
        if (item == INVEN_RARM)
        {
 #ifdef JP
-act = "¤òÁõÈ÷¤«¤é¤Ï¤º¤·¤¿";
+               act = "¤òÁõÈ÷¤«¤é¤Ï¤º¤·¤¿";
 #else
                act = "You were wielding";
 #endif
@@ -6284,7 +5718,7 @@ act = "
        else if (item == INVEN_BOW)
        {
 #ifdef JP
-act = "¤òÁõÈ÷¤«¤é¤Ï¤º¤·¤¿";
+               act = "¤òÁõÈ÷¤«¤é¤Ï¤º¤·¤¿";
 #else
                act = "You were holding";
 #endif
@@ -6295,7 +5729,7 @@ act = "
        else if (item == INVEN_LITE)
        {
 #ifdef JP
-act = "¤ò¸÷¸»¤«¤é¤Ï¤º¤·¤¿";
+               act = "¤ò¸÷¸»¤«¤é¤Ï¤º¤·¤¿";
 #else
                act = "You were holding";
 #endif
@@ -6306,7 +5740,7 @@ act = "
        else
        {
 #ifdef JP
-act = "¤òÁõÈ÷¤«¤é¤Ï¤º¤·¤¿";
+               act = "¤òÁõÈ÷¤«¤é¤Ï¤º¤·¤¿";
 #else
                act = "You were wearing";
 #endif
@@ -6386,7 +5820,7 @@ void inven_drop(int item, int amt)
 
        /* Message */
 #ifdef JP
-msg_format("%s(%c)¤òÍî¤È¤·¤¿¡£", o_name, index_to_label(item));
+       msg_format("%s(%c)¤òÍî¤È¤·¤¿¡£", o_name, index_to_label(item));
 #else
        msg_format("You drop %s (%c).", o_name, index_to_label(item));
 #endif
@@ -6433,35 +5867,46 @@ void combine_pack(void)
                        /* Skip empty items */
                        if (!j_ptr->k_idx) continue;
 
-                       /* Can we drop "o_ptr" onto "j_ptr"? */
-                       if (object_similar(j_ptr, o_ptr))
+                       /* Can we (partialy) drop "o_ptr" onto "j_ptr"? */
+                       if (object_similar_part(j_ptr, o_ptr)
+                           && j_ptr->number < MAX_STACK_SIZE-1)
                        {
-                               /* Take note */
-                               flag = TRUE;
+                               if (o_ptr->number + j_ptr->number < MAX_STACK_SIZE)
+                               {
+                                       /* Take note */
+                                       flag = TRUE;
 
-                               /* Add together the item counts */
-                               object_absorb(j_ptr, o_ptr);
+                                       /* Add together the item counts */
+                                       object_absorb(j_ptr, o_ptr);
 
-                               /* One object is gone */
-                               inven_cnt--;
+                                       /* One object is gone */
+                                       inven_cnt--;
 
-                               /* Slide everything down */
-                               for (k = i; k < INVEN_PACK; k++)
+                                       /* Slide everything down */
+                                       for (k = i; k < INVEN_PACK; k++)
+                                       {
+                                               /* Structure copy */
+                                               inventory[k] = inventory[k+1];
+                                       }
+                                       
+                                       /* Erase the "final" slot */
+                                       object_wipe(&inventory[k]);
+                               }
+                               else
                                {
-                                       /* Structure copy */
-                                       inventory[k] = inventory[k+1];
+                                       int remain = j_ptr->number + o_ptr->number - 99;
+                                       
+                                       o_ptr->number -= remain;
+                                       
+                                       /* Add together the item counts */
+                                       object_absorb(j_ptr, o_ptr);
+
+                                       o_ptr->number = remain;
+                                       
                                }
-
-#ifdef USE_SCRIPT
-                               object_delete_callback(&inventory[k]);
-#endif /* USE_SCRIPT */
-
-                               /* Erase the "final" slot */
-                               object_wipe(&inventory[k]);
-
                                /* Window stuff */
                                p_ptr->window |= (PW_INVEN);
-
+                               
                                /* Done */
                                break;
                        }
@@ -6470,7 +5915,7 @@ void combine_pack(void)
 
        /* Message */
 #ifdef JP
-if (flag) msg_print("¥¶¥Ã¥¯¤ÎÃæ¤Î¥¢¥¤¥Æ¥à¤ò¤Þ¤È¤áľ¤·¤¿¡£");
+       if (flag) msg_print("¥¶¥Ã¥¯¤ÎÃæ¤Î¥¢¥¤¥Æ¥à¤ò¤Þ¤È¤áľ¤·¤¿¡£");
 #else
        if (flag) msg_print("You combine some items in your pack.");
 #endif
@@ -6592,7 +6037,7 @@ void reorder_pack(void)
 
        /* Message */
 #ifdef JP
-if (flag) msg_print("¥¶¥Ã¥¯¤ÎÃæ¤Î¥¢¥¤¥Æ¥à¤òÊ¤Ùľ¤·¤¿¡£");
+       if (flag) msg_print("¥¶¥Ã¥¯¤ÎÃæ¤Î¥¢¥¤¥Æ¥à¤òÊ¤Ùľ¤·¤¿¡£");
 #else
        if (flag) msg_print("You reorder some items in your pack.");
 #endif
@@ -6670,38 +6115,76 @@ void display_koff(int k_idx)
        }
 }
 
+/* Choose one of items that have warning flag */
+object_type *choose_warning_item(void)
+{
+       int i;
+       int choices[INVEN_TOTAL-INVEN_RARM];
+       int number = 0;
+
+       /* Paranoia -- Player has no warning-item */
+       if (!p_ptr->warning) return (NULL);
+
+       /* Search Inventry */
+       for (i = INVEN_RARM; i < INVEN_TOTAL; i++)
+       {
+               u32b f1, f2, f3;
+               object_type *o_ptr = &inventory[i];
+
+               object_flags(o_ptr, &f1, &f2, &f3);
+               if (f3 & (TR3_WARNING))
+               {
+                       choices[number] = i;
+                       number++;
+               }
+       }
+
+       /* Choice one of them */
+       return (&inventory[choices[randint0(number)]]);
+}
+
 /* Examine the grid (xx,yy) and warn the player if there are any danger */
-bool process_frakir(int xx, int yy){
-  int mx,my;
-  cave_type *c_ptr;
+bool process_frakir(int xx, int yy)
+{
+       int mx,my;
+       cave_type *c_ptr;
+       char o_name[MAX_NLEN];
 
 #define FRAKIR_AWARE_RANGE 12
-  int dam_max=0;
-  static int old_damage = 0;
-  
-  for (mx=xx-FRAKIR_AWARE_RANGE;mx<xx+FRAKIR_AWARE_RANGE+1;mx++){
-    for (my=yy-FRAKIR_AWARE_RANGE;my<yy+FRAKIR_AWARE_RANGE+1;my++){
-      if (!in_bounds(my,mx) || (distance(my,mx,yy,xx)>FRAKIR_AWARE_RANGE))continue;
-      
-      c_ptr = &cave[my][mx];
-      if (c_ptr->m_idx > 0){
-       
-       int dam_max0=0;
-       int m_idx = c_ptr->m_idx;
-       monster_type *m_ptr = &m_list[m_idx];
-       monster_race *r_ptr = &r_info[m_ptr->r_idx];
+       int dam_max = 0;
+       static int old_damage = 0;
+
+       for (mx = xx-FRAKIR_AWARE_RANGE; mx < xx+FRAKIR_AWARE_RANGE+1; mx++)
+       {
+               for (my = yy-FRAKIR_AWARE_RANGE; my < yy+FRAKIR_AWARE_RANGE+1; my++)
+               {
+                       int dam_max0=0;
+                       monster_type *m_ptr;
+                       monster_race *r_ptr;
+                       u32b f4, f5, f6;
+                       int rlev;
+
+                       if (!in_bounds(my,mx) || (distance(my,mx,yy,xx)>FRAKIR_AWARE_RANGE)) continue;
+
+                       c_ptr = &cave[my][mx];
 
-       u32b  f4 = r_ptr->flags4;
-       u32b  f5 = r_ptr->flags5;
-       u32b  f6 = r_ptr->flags6;
+                       if (!c_ptr->m_idx) continue;
 
-       int   rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
+                       m_ptr = &m_list[c_ptr->m_idx];
+                       r_ptr = &r_info[m_ptr->r_idx];
 
-       if (m_ptr->csleep) continue;
-       if (is_pet(m_ptr)) continue;
+                       f4 = r_ptr->flags4;
+                       f5 = r_ptr->flags5;
+                       f6 = r_ptr->flags6;
 
-       /* Monster spells (only powerful ones)*/
-       if(projectable(my,mx,yy,xx)){
+                       rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
+
+                       if (m_ptr->csleep) continue;
+                       if (is_pet(m_ptr)) continue;
+
+                       /* Monster spells (only powerful ones)*/
+                       if(projectable(my,mx,yy,xx))
+                       {
 
 #define DAMCALC(f,val,max,im,vln,res,resx,resy,op,opx,opy,dmax) \
           if (f){ int dam = (val)>(max)? (max):(val); \
@@ -6712,169 +6195,173 @@ bool process_frakir(int xx, int yy){
            if (dam>dmax) dmax = dam; \
            }
 
-           DAMCALC(f4 & (RF4_BR_FIRE), m_ptr->hp / 3, 1600, 
-                   p_ptr->immune_fire, p_ptr->muta3 & MUT3_VULN_ELEM,
-                   p_ptr->resist_fire, 1, 3,
-                   p_ptr->oppose_fire, 1, 3, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_FIRE), m_ptr->hp / 3, 1600, 
+                                       p_ptr->immune_fire, p_ptr->muta3 & MUT3_VULN_ELEM,
+                                       p_ptr->resist_fire, 1, 3,
+                                       p_ptr->oppose_fire, 1, 3, dam_max0);
+
+                               DAMCALC(f4 & (RF4_BR_COLD), m_ptr->hp / 3, 1600, 
+                                       p_ptr->immune_cold, p_ptr->muta3 & MUT3_VULN_ELEM,
+                                       p_ptr->resist_cold, 1, 3,
+                                       p_ptr->oppose_cold, 1, 3, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_COLD), m_ptr->hp / 3, 1600, 
-                   p_ptr->immune_cold, p_ptr->muta3 & MUT3_VULN_ELEM,
-                   p_ptr->resist_cold, 1, 3,
-                   p_ptr->oppose_cold, 1, 3, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_ELEC), m_ptr->hp / 3, 1600, 
+                                       p_ptr->immune_elec, p_ptr->muta3 & MUT3_VULN_ELEM,
+                                       p_ptr->resist_elec, 1, 3,
+                                       p_ptr->oppose_elec, 1, 3, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_ELEC), m_ptr->hp / 3, 1600, 
-                   p_ptr->immune_elec, p_ptr->muta3 & MUT3_VULN_ELEM,
-                   p_ptr->resist_elec, 1, 3,
-                   p_ptr->oppose_elec, 1, 3, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_ACID), m_ptr->hp / 3, 1600, 
+                                       p_ptr->immune_acid, p_ptr->muta3 & MUT3_VULN_ELEM,
+                                       p_ptr->resist_acid, 1, 3,
+                                       p_ptr->oppose_acid, 1, 3, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_ACID), m_ptr->hp / 3, 1600, 
-                   p_ptr->immune_acid, p_ptr->muta3 & MUT3_VULN_ELEM,
-                   p_ptr->resist_acid, 1, 3,
-                   p_ptr->oppose_acid, 1, 3, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_POIS), m_ptr->hp / 3, 800,
+                                       FALSE , FALSE,
+                                       p_ptr->resist_pois, 1, 3,
+                                       p_ptr->oppose_pois, 1, 3, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_POIS), m_ptr->hp / 3, 800,
-                   FALSE , FALSE,
-                   p_ptr->resist_pois, 1, 3,
-                   p_ptr->oppose_pois, 1, 3, dam_max0);
 
+                               DAMCALC(f4 & (RF4_BR_NETH), m_ptr->hp / 6, 550, FALSE , FALSE,
+                                       p_ptr->resist_neth, 6, 9, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_NETH), m_ptr->hp / 6, 550, FALSE , FALSE,
-                   p_ptr->resist_neth, 6, 9, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_LITE), m_ptr->hp / 6, 400, FALSE , FALSE,
+                                       p_ptr->resist_lite, 4, 9, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_LITE), m_ptr->hp / 6, 400, FALSE , FALSE,
-                   p_ptr->resist_lite, 4, 9, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_DARK), m_ptr->hp / 6, 400, FALSE , FALSE,
+                                       p_ptr->resist_dark, 4, 9, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_DARK), m_ptr->hp / 6, 400, FALSE , FALSE,
-                   p_ptr->resist_dark, 4, 9, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_CONF), m_ptr->hp / 6, 450, FALSE , FALSE,
+                                       p_ptr->resist_conf, 5, 9, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_CONF), m_ptr->hp / 6, 450, FALSE , FALSE,
-                   p_ptr->resist_conf, 5, 9, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_SOUN), m_ptr->hp / 6, 450, FALSE , FALSE,
+                                       p_ptr->resist_sound, 5, 9, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_SOUN), m_ptr->hp / 6, 450, FALSE , FALSE,
-                   p_ptr->resist_sound, 5, 9, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_CHAO), m_ptr->hp / 6, 600, FALSE , FALSE,
+                                       p_ptr->resist_chaos, 6, 9, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_CHAO), m_ptr->hp / 6, 600, FALSE , FALSE,
-                   p_ptr->resist_chaos, 6, 9, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_DISE), m_ptr->hp / 6, 500, FALSE , FALSE,
+                                       p_ptr->resist_disen, 6, 9, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_DISE), m_ptr->hp / 6, 500, FALSE , FALSE,
-                   p_ptr->resist_disen, 6, 9, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_NEXU), m_ptr->hp / 3, 250, FALSE , FALSE,
+                                       p_ptr->resist_nexus, 6, 9, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_NEXU), m_ptr->hp / 3, 250, FALSE , FALSE,
-                   p_ptr->resist_nexus, 6, 9, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_TIME), m_ptr->hp / 3, 150, FALSE , FALSE,
+                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_TIME), m_ptr->hp / 3, 150, FALSE , FALSE,
-                   FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_INER), m_ptr->hp / 6, 200, FALSE , FALSE,
+                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_INER), m_ptr->hp / 6, 200, FALSE , FALSE,
-                   FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_GRAV), m_ptr->hp / 3, 200, FALSE , FALSE,
+                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_GRAV), m_ptr->hp / 3, 200, FALSE , FALSE,
-                   FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_SHAR), m_ptr->hp / 6, 500, FALSE , FALSE,
+                                       p_ptr->resist_shard, 6, 9, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_SHAR), m_ptr->hp / 6, 500, FALSE , FALSE,
-                   p_ptr->resist_shard, 6, 9, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_PLAS), m_ptr->hp / 6, 150, FALSE , FALSE,
+                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_PLAS), m_ptr->hp / 6, 150, FALSE , FALSE,
-                   FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_WALL), m_ptr->hp / 6, 200, FALSE , FALSE,
+                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_WALL), m_ptr->hp / 6, 200, FALSE , FALSE,
-                   FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_MANA), m_ptr->hp / 3, 250, FALSE , FALSE,
+                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_MANA), m_ptr->hp / 3, 250, FALSE , FALSE,
-                   FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_NUKE), m_ptr->hp / 3, 800, FALSE , FALSE,
+                                       p_ptr->resist_pois, 2, 5, 
+                                       p_ptr->oppose_pois, 2, 5, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_NUKE), m_ptr->hp / 3, 800, FALSE , FALSE,
-                   p_ptr->resist_pois, 2, 5, 
-                   p_ptr->oppose_pois, 2, 5, dam_max0);
+                               DAMCALC(f4 & (RF4_BR_DISI), m_ptr->hp / 3, 300, FALSE , FALSE,
+                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_BR_DISI), m_ptr->hp / 3, 300, FALSE , FALSE,
-                   FALSE, 1, 1, FALSE, 1, 1, dam_max0);
 
+                               DAMCALC(f4 & (RF4_ROCKET), m_ptr->hp / 4, 800, FALSE , FALSE,
+                                       p_ptr->resist_shard, 1, 2, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_ROCKET), m_ptr->hp / 4, 800, FALSE , FALSE,
-                   p_ptr->resist_shard, 1, 2, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f5 & (RF5_BA_MANA), rlev*4 + 150, 9999, FALSE , FALSE,
+                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f5 & (RF5_BA_MANA), rlev*4 + 150, 9999, FALSE , FALSE,
-                   FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f5 & (RF5_BA_DARK), rlev*4 + 150, 9999, FALSE , FALSE,
+                                       p_ptr->resist_dark, 4, 9, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f5 & (RF5_BA_DARK), rlev*4 + 150, 9999, FALSE , FALSE,
-                   p_ptr->resist_dark, 4, 9, FALSE, 1, 1, dam_max0);
+                               DAMCALC(f5 & (RF5_BA_LITE), rlev*4 + 150, 9999, FALSE , FALSE,
+                                       p_ptr->resist_lite, 4, 9, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f5 & (RF5_BA_LITE), rlev*4 + 150, 9999, FALSE , FALSE,
-                   p_ptr->resist_lite, 4, 9, FALSE, 1, 1, dam_max0);
 
+                               DAMCALC(f6 & (RF6_HAND_DOOM), p_ptr->chp*6/10, 9999, FALSE , FALSE,
+                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
 
-           DAMCALC(f4 & (RF4_ROCKET),m_ptr->hp / 4, 800, FALSE , FALSE,
-                   p_ptr->resist_shard, 1, 2, FALSE, 1, 1, dam_max0);
+                       }
 
-           DAMCALC(f6 & (RF6_HAND_DOOM), p_ptr->chp*6/10, 9999, FALSE , FALSE,
-                   FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+                       /* Monster melee attacks */
+                       if(mx <= xx+1 && mx >= xx-1 && my <=yy+1 && my >= yy-1)
+                       {
+                               int m;
+                               int dam_melee=0;
+                               for (m = 0; m < 4; m++)
+                               {
+                                       int d1, d2;
 
-       }
+                                       /* Skip non-attacks */
+                                       if (!r_ptr->blow[m].method) continue;
 
-       /* Monster melee attacks */
-       if( mx <= xx+1 && mx >= xx-1 && my <=yy+1 && my >= yy-1 ){
-         int m;
-         int dam_melee=0;
-         for (m = 0; m < 4; m++)
-           {
-             int d1, d2;
-             
-             /* Skip non-attacks */
-             if (!r_ptr->blow[m].method) continue;
-             
-             /* Extract the attack info */
-             d1 = r_ptr->blow[m].d_dice;
-             d2 = r_ptr->blow[m].d_side;
-             
-             dam_melee += d1*d2;
-           }
-        if(dam_melee>dam_max0)dam_max0=dam_melee;
+                                       /* Extract the attack info */
+                                       d1 = r_ptr->blow[m].d_dice;
+                                       d2 = r_ptr->blow[m].d_side;
+
+                                       dam_melee += d1*d2;
+                               }
+                               if(dam_melee>dam_max0)dam_max0=dam_melee;
+                       }
+
+                       /* Contribution from this monster */
+                       dam_max+=dam_max0;
+               }
        }
 
-       /* Contribution from this monster */
-       dam_max+=dam_max0;
-      }
-      
-      
-    }
-  }
+       /* Prevent excessive warning */
+       if(dam_max > old_damage)
+       {
+               old_damage=dam_max * 3 / 2;
+
+               if (dam_max>(p_ptr->chp)/2)
+               {
+                       object_type *o_ptr = choose_warning_item();
 
-  /* Prevent excessive warning */
-  if(dam_max>old_damage){
-    old_damage=dam_max * 3 / 2;
-    
-    if (dam_max>(p_ptr->chp)/2){
+                       object_desc(o_name, o_ptr, FALSE, 0);
 #ifdef JP
-      msg_print("»ØÎؤ¬±Ô¤¯¿Ì¤¨¤¿¡ª");
+                       msg_format("%s¤¬±Ô¤¯¿Ì¤¨¤¿¡ª", o_name);
 #else
-      msg_print("The Ring pulsates sharply!");
+                       msg_format("%s pulsates sharply!", o_name);
 #endif
-      disturb(0,0);
+                       disturb(0,0);
 #ifdef JP
-      return(get_check("ËÜÅö¤Ë¤³¤Î¤Þ¤Þ¿Ê¤à¤«¡©"));
+                       return (get_check("ËÜÅö¤Ë¤³¤Î¤Þ¤Þ¿Ê¤à¤«¡©"));
 #else
-      return(get_check("Realy want to go ahead? "));
+                       return (get_check("Realy want to go ahead? "));
 #endif
-    }
-  }
-  else old_damage = old_damage/2;
+               }
+       }
+       else old_damage = old_damage/2;
+
+       c_ptr = &cave[yy][xx];
+       if (((is_trap(c_ptr->feat) && !easy_disarm) || (c_ptr->info & CAVE_TRAP)) && !one_in_(13))
+       {
+               object_type *o_ptr = choose_warning_item();
 
-  c_ptr = &cave[yy][xx];
-  if (((is_trap(c_ptr->feat) && !easy_disarm) || (c_ptr->info & CAVE_TRAP)) && randint(13)!=1){
+               object_desc(o_name, o_ptr, FALSE, 0);
 #ifdef JP
-    msg_print("»ØÎؤ¬¿Ì¤¨¤¿¡ª");
+               msg_format("%s¤¬¿Ì¤¨¤¿¡ª", o_name);
 #else
-    msg_print("The Ring pulsates!");
+               msg_format("%s pulsates!", o_name);
 #endif
-    disturb(0,0);
+               disturb(0,0);
 #ifdef JP
-      return(get_check("ËÜÅö¤Ë¤³¤Î¤Þ¤Þ¿Ê¤à¤«¡©"));
+               return (get_check("ËÜÅö¤Ë¤³¤Î¤Þ¤Þ¿Ê¤à¤«¡©"));
 #else
-      return(get_check("Realy want to go ahead? "));
+               return (get_check("Realy want to go ahead? "));
 #endif
-  }
-  return(TRUE);
+       }
+       return(TRUE);
 }
 
 
@@ -7186,8 +6673,8 @@ static void drain_essence(void)
 
        /* Get an item */
 #ifdef JP
-q = "¤É¤Î¥¢¥¤¥Æ¥à¤«¤éÃê½Ð¤·¤Þ¤¹¤«¡©";
-s = "Ãê½Ð¤Ç¤­¤ë¥¢¥¤¥Æ¥à¤¬¤¢¤ê¤Þ¤»¤ó¡£";
+       q = "¤É¤Î¥¢¥¤¥Æ¥à¤«¤éÃê½Ð¤·¤Þ¤¹¤«¡©";
+       s = "Ãê½Ð¤Ç¤­¤ë¥¢¥¤¥Æ¥à¤¬¤¢¤ê¤Þ¤»¤ó¡£";
 #else
        q = "Extract from which item? ";
        s = "You have nothing you can extract from.";
@@ -7230,7 +6717,7 @@ s = "
        old_dd = o_ptr->dd;
        old_pval = o_ptr->pval;
        old_name2 = o_ptr->name2;
-       if (old_f3 & (TR3_CURSED | TR3_HEAVY_CURSE | TR3_PERMA_CURSE)) dec--;
+       if (o_ptr->curse_flags & (TRC_CURSED | TRC_HEAVY_CURSE | TRC_PERMA_CURSE)) dec--;
        if (old_f3 & (TR3_AGGRAVATE)) dec--;
        if (old_f3 & (TR3_NO_TELE)) dec--;
        if (old_f3 & (TR3_DRAIN_EXP)) dec--;
@@ -7303,7 +6790,7 @@ s = "
                }
        }
 
-       if ((old_f1 & TR1_FORCE_WEPON) && !(new_f1 & TR1_FORCE_WEPON))
+       if ((old_f1 & TR1_FORCE_WEAPON) && !(new_f1 & TR1_FORCE_WEAPON))
        {
                drain_value[ESSENCE_INT-1] += 5;
                drain_value[ESSENCE_WIS-1] += 5;
@@ -7328,9 +6815,9 @@ s = "
        {
                drain_value[ESSENCE_DEX-1] += 10;
        }
-       if (old_name2 == EGO_2HAND)
+       if (old_name2 == EGO_2WEAPON)
        {
-               drain_value[ESSENCE_DEX] += 20;
+               drain_value[ESSENCE_DEX-1] += 20;
        }
        if ((o_ptr->tval >= TV_SHOT) && (o_ptr->tval <= TV_SWORD) && (o_ptr->tval != TV_BOW))
        {
@@ -7440,6 +6927,7 @@ static int choose_essence()
                                menu_line+= 4;
                                break;
                        case '\r':
+                       case '\n':
                        case 'x':
                        case 'X':
                                mode = menu_line;
@@ -7543,7 +7031,7 @@ static void add_essence(int mode)
 
        /* Build a prompt */
 #ifdef JP
-(void) strnfmt(out_val, 78, "('*'¤Ç°ìÍ÷, ESC¤ÇÃæÃÇ) ¤É¤ÎǽÎϤòÉղä·¤Þ¤¹¤«¡©");
+       (void) strnfmt(out_val, 78, "('*'¤Ç°ìÍ÷, ESC¤ÇÃæÃÇ) ¤É¤ÎǽÎϤòÉղä·¤Þ¤¹¤«¡©");
 #else
        (void)strnfmt(out_val, 78, "(*=List, ESC=exit) Add which ability? ");
 #endif
@@ -7603,6 +7091,7 @@ static void add_essence(int mode)
                                case 'x':
                                case 'X':
                                case '\r':
+                               case '\n':
                                {
                                        i = menu_line - 1;
                                        ask = FALSE;
@@ -7820,8 +7309,8 @@ static void add_essence(int mode)
 
        /* Get an item */
 #ifdef JP
-q = "¤É¤Î¥¢¥¤¥Æ¥à¤ò²þÎɤ·¤Þ¤¹¤«¡©";
-s = "²þÎɤǤ­¤ë¥¢¥¤¥Æ¥à¤¬¤¢¤ê¤Þ¤»¤ó¡£";
+       q = "¤É¤Î¥¢¥¤¥Æ¥à¤ò²þÎɤ·¤Þ¤¹¤«¡©";
+       s = "²þÎɤǤ­¤ë¥¢¥¤¥Æ¥à¤¬¤¢¤ê¤Þ¤»¤ó¡£";
 #else
        q = "Improve which item? ";
        s = "You have nothing to improve.";
@@ -7967,8 +7456,8 @@ s = "
 #endif
                                return;
                        }
-                       get_to_h = ((val+1)/2+rand_int(val/2+1));
-                       get_to_d = ((val+1)/2+rand_int(val/2+1));
+                       get_to_h = ((val+1)/2+randint0(val/2+1));
+                       get_to_d = ((val+1)/2+randint0(val/2+1));
                        o_ptr->xtra4 = (get_to_h<<8)+get_to_d;
                        o_ptr->to_h += get_to_h;
                        o_ptr->to_d += get_to_d;
@@ -8083,7 +7572,7 @@ s = "
 #endif
 
        /* Combine the pack */
-       p_ptr->notice |= (PN_COMBINE);
+       p_ptr->notice |= (PN_COMBINE | PN_REORDER);
 
        /* Window stuff */
        p_ptr->window |= (PW_INVEN);
@@ -8132,8 +7621,8 @@ void erase_essence(void)
 
        /* Get an item */
 #ifdef JP
-q = "¤É¤Î¥¢¥¤¥Æ¥à¤Î¥¨¥Ã¥»¥ó¥¹¤ò¾Ãµî¤·¤Þ¤¹¤«¡©";
-s = "¥¨¥Ã¥»¥ó¥¹¤òÉղä·¤¿¥¢¥¤¥Æ¥à¤¬¤¢¤ê¤Þ¤»¤ó¡£";
+       q = "¤É¤Î¥¢¥¤¥Æ¥à¤Î¥¨¥Ã¥»¥ó¥¹¤ò¾Ãµî¤·¤Þ¤¹¤«¡©";
+       s = "¥¨¥Ã¥»¥ó¥¹¤òÉղä·¤¿¥¢¥¤¥Æ¥à¤¬¤¢¤ê¤Þ¤»¤ó¡£";
 #else
        q = "Remove from which item? ";
        s = "You have nothing to remove essence.";
@@ -8178,7 +7667,7 @@ s = "
 #endif
 
        /* Combine the pack */
-       p_ptr->notice |= (PN_COMBINE);
+       p_ptr->notice |= (PN_COMBINE | PN_REORDER);
 
        /* Window stuff */
        p_ptr->window |= (PW_INVEN);
@@ -8196,7 +7685,7 @@ void do_cmd_kaji(bool only_browse)
                if (p_ptr->confused)
                {
 #ifdef JP
-msg_print("º®Í𤷤Ƥ¤¤Æºî¶È¤Ç¤­¤Ê¤¤¡ª");
+                       msg_print("º®Í𤷤Ƥ¤¤Æºî¶È¤Ç¤­¤Ê¤¤¡ª");
 #else
                        msg_print("You are too confused!");
 #endif
@@ -8206,7 +7695,7 @@ msg_print("
                if (p_ptr->blind)
                {
 #ifdef JP
-msg_print("Ìܤ¬¸«¤¨¤Ê¤¯¤Æºî¶È¤Ç¤­¤Ê¤¤¡ª");
+                       msg_print("Ìܤ¬¸«¤¨¤Ê¤¯¤Æºî¶È¤Ç¤­¤Ê¤¤¡ª");
 #else
                        msg_print("You are blind!");
 #endif
@@ -8216,7 +7705,7 @@ msg_print("
                if (p_ptr->image)
                {
 #ifdef JP
-msg_print("¤¦¤Þ¤¯¸«¤¨¤Ê¤¯¤Æºî¶È¤Ç¤­¤Ê¤¤¡ª");
+                       msg_print("¤¦¤Þ¤¯¸«¤¨¤Ê¤¯¤Æºî¶È¤Ç¤­¤Ê¤¤¡ª");
 #else
                        msg_print("You are hullcinating!");
 #endif
@@ -8271,6 +7760,7 @@ msg_print("
                                menu_line+= 4;
                                break;
                        case '\r':
+                       case '\n':
                        case 'x':
                        case 'X':
                                mode = menu_line;