Changeset 827

Show
Ignore:
Timestamp:
03/31/08 06:10:39 (5 months ago)
Author:
takkaria
Message:

Significantly clean up the logic of object generation, and in the process fix a possible-bug reported by ChodTheWacko?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/obj-make.c

    r825 r827  
    235235 * an unenchanted item. 
    236236 */ 
    237 static int make_ego_item(object_type *o_ptr, int level, bool only_good) 
     237static int make_ego_item(object_type *o_ptr, int level, bool force_uncursed) 
    238238{ 
    239239        int i, j; 
     
    281281                e_ptr = &e_info[e_idx]; 
    282282 
    283                 /* If we force good/great, don't create cursed */ 
    284                 if (only_good && (e_ptr->flags3 & TR3_LIGHT_CURSE)) continue; 
     283                /* Avoid cursed items if specified */ 
     284                if (force_uncursed && (e_ptr->flags3 & TR3_LIGHT_CURSE)) continue; 
    285285 
    286286                /* Test if this is a legal ego-item type for this object */ 
     
    332332 
    333333 
     334/** 
     335 * Copy artifact data to a normal object, and set various slightly hacky 
     336 * globals. 
     337 */ 
     338static void copy_artifact_data(object_type *o_ptr, artifact_type *a_ptr) 
     339{ 
     340        /* Hack -- Mark the artifact as "created" */ 
     341        a_ptr->cur_num = 1; 
     342 
     343        /* Extract the other fields */ 
     344        o_ptr->pval = a_ptr->pval; 
     345        o_ptr->ac = a_ptr->ac; 
     346        o_ptr->dd = a_ptr->dd; 
     347        o_ptr->ds = a_ptr->ds; 
     348        o_ptr->to_a = a_ptr->to_a; 
     349        o_ptr->to_h = a_ptr->to_h; 
     350        o_ptr->to_d = a_ptr->to_d; 
     351        o_ptr->weight = a_ptr->weight; 
     352 
     353        /* Hack -- extract the "cursed" flag */ 
     354        if (a_ptr->flags3 & TR3_LIGHT_CURSE) 
     355                o_ptr->flags3 |= TR3_LIGHT_CURSE; 
     356 
     357        /* Mega-Hack -- increase the rating */ 
     358        rating += 10; 
     359 
     360        /* Mega-Hack -- increase the rating again */ 
     361        if (a_ptr->cost > 50000L) rating += 10; 
     362 
     363        /* Set the good item flag */ 
     364        good_item_flag = TRUE; 
     365 
     366        /* Cheat -- peek at the item */ 
     367        if (cheat_peek) object_mention(o_ptr); 
     368} 
     369 
     370 
    334371/* 
    335372 * Mega-Hack -- Attempt to create one of the "Special Objects". 
     
    401438                o_ptr->name1 = i; 
    402439 
     440                /* Copy across all the data from the artifact struct */ 
     441                copy_artifact_data(o_ptr, a_ptr); 
     442 
    403443                /* Success */ 
    404                 return (TRUE)
     444                return TRUE
    405445        } 
    406446 
    407447        /* Failure */ 
    408         return (FALSE)
     448        return FALSE
    409449} 
    410450 
     
    462502                o_ptr->name1 = i; 
    463503 
    464                 /* Success */ 
    465                 return (TRUE); 
    466         } 
    467  
    468         /* Failure */ 
    469         return (FALSE); 
     504                /* Copy across all the data from the artifact struct */ 
     505                copy_artifact_data(o_ptr, a_ptr); 
     506 
     507                return TRUE; 
     508        } 
     509 
     510        return FALSE; 
    470511} 
    471512 
     
    9871028 
    9881029 
    989 /* 
    990  * Complete the "creation" of an object by applying "magic" to the item 
    991  * 
    992  * This includes not only rolling for random bonuses, but also putting the 
    993  * finishing touches on ego-items and artifacts, giving charges to wands and 
    994  * staffs, giving fuel to lites, and placing traps on chests. 
    995  * 
    996  * In particular, note that "Instant Artifacts", if "created" by an external 
    997  * routine, must pass through this function to complete the actual creation. 
    998  * 
    999  * The base "chance" of the item being "good" increases with the "level" 
    1000  * parameter, which is usually derived from the dungeon level, being equal 
    1001  * to the level plus 10, up to a maximum of 75.  If "good" is true, then 
    1002  * the object is guaranteed to be "good".  If an object is "good", then 
    1003  * the chance that the object will be "great" (ego-item or artifact), also 
    1004  * increases with the "level", being equal to half the level, plus 5, up to 
    1005  * a maximum of 20.  If "great" is true, then the object is guaranteed to be 
    1006  * "great".  At dungeon level 65 and below, 15/100 objects are "great". 
    1007  * 
    1008  * If the object is not "good", there is a chance it will be "cursed", and 
    1009  * if it is "cursed", there is a chance it will be "broken".  These chances 
    1010  * are related to the "good" / "great" chances above. 
    1011  * 
    1012  * Otherwise "normal" rings and amulets will be "good" half the time and 
    1013  * "cursed" half the time, unless the ring/amulet is always good or cursed. 
    1014  * 
    1015  * If "okay" is true, and the object is going to be "great", then there is 
    1016  * a chance that an artifact will be created.  This is true even if both the 
    1017  * "good" and "great" arguments are false.  Objects which are forced "great" 
    1018  * get three extra "attempts" to become an artifact. 
    1019  */ 
    1020 void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great) 
    1021 
    1022         int i, rolls, f1, f2, power; 
    1023  
    1024  
    1025         /* Maximum "level" for various things */ 
     1030/** 
     1031 * Complete object creation by applying magic to it. 
     1032 * 
     1033 * Magic includes rolling for random bonuses, applying flags to ego-items, 
     1034 * charging charged items, fuelling lights, and trapping chests. 
     1035 * 
     1036 * The `good` argument forces the item to be at least `good`, and the `great` 
     1037 * argument does likewise.  Setting `allow_artifacts` to TRUE allows artifacts 
     1038 * to be created here. 
     1039 * 
     1040 * If `good` or `great` are not set, then the `lev` argument controls the 
     1041 * quality of item.  See the function itself for the specifics of the 
     1042 * calculations involved. 
     1043 */ 
     1044void apply_magic(object_type *o_ptr, int lev, bool allow_artifacts, bool good, bool great) 
     1045
     1046        int power = 0; 
     1047 
     1048        /* Chance of being `good` and `great` */ 
     1049        int good_chance = MIN(2*lev + 10, 100); 
     1050        int great_chance = MIN(good_chance / 3, 20); 
     1051 
     1052 
     1053        /* Limit depth */ 
    10261054        if (lev > MAX_DEPTH - 1) lev = MAX_DEPTH - 1; 
    10271055 
    1028  
    1029         /* Base chance of being "good" */ 
    1030         f1 = 2*lev + 10; 
    1031  
    1032         /* Maximal chance of being "good" */ 
    1033         if (f1 > 100) f1 = 100; 
    1034  
    1035         /* Base chance of being "great" */ 
    1036         f2 = f1 / 3; 
    1037  
    1038         /* Maximal chance of being "great" */ 
    1039         if (f2 > 20) f2 = 20; 
    1040  
    1041  
    1042         /* Assume normal */ 
    1043         power = 0; 
    1044  
    10451056        /* Roll for "good" */ 
    1046         if (good || (rand_int(100) < f1)) 
     1057        if (good || (rand_int(100) < good_chance)) 
    10471058        { 
    10481059                /* Assume "good" */ 
     
    10501061 
    10511062                /* Roll for "great" */ 
    1052                 if (great || (rand_int(100) < f2)) power = 2; 
     1063                if (great || (rand_int(100) < great_chance)) power = 2; 
    10531064        } 
    10541065 
    10551066        /* Roll for "cursed" */ 
    1056         else if (rand_int(100) < f1
     1067        else if (rand_int(100) < good_chance
    10571068        { 
    10581069                /* Assume "cursed" */ 
     
    10601071 
    10611072                /* Roll for "broken" */ 
    1062                 if (rand_int(100) < f2) power = -2; 
    1063         } 
    1064  
    1065         /* Assume no rolls */ 
    1066         rolls = 0; 
    1067  
    1068         /* Get one roll if excellent */ 
    1069         if (power >= 2) rolls = 1; 
    1070  
    1071         /* Get four rolls if forced great */ 
    1072         if (great) rolls = 4; 
    1073  
    1074         /* Get no rolls if not allowed */ 
    1075         if (!okay || o_ptr->name1) rolls = 0; 
    1076  
    1077         /* Roll for artifacts if allowed */ 
    1078         for (i = 0; i < rolls; i++) 
    1079         { 
    1080                 /* Roll for an artifact */ 
    1081                 if (make_artifact(o_ptr)) break; 
    1082         } 
    1083  
    1084  
    1085         /* Hack -- analyze artifacts */ 
    1086         if (o_ptr->name1) 
    1087         { 
    1088                 artifact_type *a_ptr = &a_info[o_ptr->name1]; 
    1089  
    1090                 /* Hack -- Mark the artifact as "created" */ 
    1091                 a_ptr->cur_num = 1; 
    1092  
    1093                 /* Extract the other fields */ 
    1094                 o_ptr->pval = a_ptr->pval; 
    1095                 o_ptr->ac = a_ptr->ac; 
    1096                 o_ptr->dd = a_ptr->dd; 
    1097                 o_ptr->ds = a_ptr->ds; 
    1098                 o_ptr->to_a = a_ptr->to_a; 
    1099                 o_ptr->to_h = a_ptr->to_h; 
    1100                 o_ptr->to_d = a_ptr->to_d; 
    1101                 o_ptr->weight = a_ptr->weight; 
    1102  
    1103                 /* Hack -- extract the "cursed" flag */ 
    1104                 if (a_ptr->flags3 & TR3_LIGHT_CURSE) 
    1105                         o_ptr->flags3 |= TR3_LIGHT_CURSE; 
    1106  
    1107                 /* Mega-Hack -- increase the rating */ 
    1108                 rating += 10; 
    1109  
    1110                 /* Mega-Hack -- increase the rating again */ 
    1111                 if (a_ptr->cost > 50000L) rating += 10; 
    1112  
    1113                 /* Set the good item flag */ 
    1114                 good_item_flag = TRUE; 
    1115  
    1116                 /* Cheat -- peek at the item */ 
    1117                 if (cheat_peek) object_mention(o_ptr); 
    1118  
    1119                 /* Done */ 
    1120                 return; 
     1073                if (rand_int(100) < great_chance) power = -2; 
     1074        } 
     1075 
     1076 
     1077        /* Roll for artifact creation */ 
     1078        if (allow_artifacts && !o_ptr->name1) 
     1079        { 
     1080                int i; 
     1081                int rolls = 0; 
     1082 
     1083                /* Get one roll if excellent */ 
     1084                if (power >= 2) rolls = 1; 
     1085 
     1086                /* Get four rolls if forced great */ 
     1087                if (great) rolls = 4; 
     1088 
     1089                /* Roll for artifacts if allowed */ 
     1090                for (i = 0; i < rolls; i++) 
     1091                { 
     1092                        if (make_artifact(o_ptr)) 
     1093                                return; 
     1094                } 
    11211095        } 
    11221096 
     
    11341108                case TV_BOLT: 
    11351109                { 
    1136                         if ((power > 1) || (power < -1)
     1110                        if (power == 2 || power == -2
    11371111                        { 
    11381112                                int ego_power; 
    11391113 
    1140                                 ego_power = make_ego_item(o_ptr, lev, (bool)(good || great)); 
     1114                                ego_power = make_ego_item(o_ptr, lev, (bool)(power > 0)); 
    11411115 
    11421116                                if (ego_power) power = ego_power; 
     
    11581132                case TV_BOOTS: 
    11591133                { 
    1160                         if ((power > 1) || (power < -1)
     1134                        if (power == 2 || power == -2
    11611135                        { 
    11621136                                int ego_power; 
    11631137 
    1164                                 ego_power = make_ego_item(o_ptr, lev, (bool)(good || great)); 
     1138                                ego_power = make_ego_item(o_ptr, lev, (bool)(power > 0)); 
    11651139 
    11661140                                if (ego_power) power = ego_power; 
     
    11821156                case TV_LITE: 
    11831157                { 
    1184                         if ((power > 1) || (power < -1)) 
    1185                         { 
    1186                                 make_ego_item(o_ptr, lev, (bool)(good || great)); 
    1187                         } 
     1158                        if (power == 2 || power == -2) 
     1159                                make_ego_item(o_ptr, lev, (bool)(power > 0)); 
    11881160 
    11891161                        /* Fuel it */ 
     
    13931365bool make_object(object_type *j_ptr, int lev, bool good, bool great) 
    13941366{ 
    1395         int prob, base; 
     1367        int k_idx, i; 
     1368        int tries = 1; 
     1369 
     1370        int base; 
    13961371        object_kind *k_ptr; 
    13971372 
    1398  
    1399         /* Chance of "special object" */ 
    1400         prob = (good ? 10 : 1000); 
     1373        /* Try to make a special artifact */ 
     1374        if (one_in_(good ? 10 : 1000)) 
     1375               return make_artifact_special(j_ptr, lev); 
    14011376 
    14021377        /* Base level for the object */ 
    14031378        base = (good ? (lev + 10) : lev); 
    14041379 
    1405  
    1406         /* Generate a special artifact, or a normal object */ 
    1407         if ((rand_int(prob) != 0) || !make_artifact_special(j_ptr, lev)) 
    1408         { 
    1409                int k_idx, i; 
    1410                int tries = 1; 
    1411  
     1380        /* Try multiple times to generate good/great items */ 
     1381        if (great)     tries = 5; 
     1382        else if (good) tries = 3; 
     1383 
     1384        for (i = 0; i < tries; i++) 
     1385        { 
     1386                /* Pick a random object */ 
    14121387#if 0 
    1413                 /* Good objects */ 
    1414                 if (good) 
    1415                 { 
    1416                         /* Activate restriction */ 
    1417                         get_obj_num_hook = kind_is_good; 
    1418  
    1419                         /* Prepare allocation table */ 
    1420                         get_obj_num_prep(); 
    1421                 } 
     1388                k_idx = get_obj_num(base, kind_is_good); 
     1389#else 
     1390                k_idx = get_obj_num(base); 
    14221391#endif 
    14231392 
    1424                 if (great) 
    1425                     tries = 5; 
    1426                 else if (good) 
    1427                     tries = 3; 
    1428  
    1429                 for (i = 0; i < tries; i++) 
    1430                 { 
    1431                     /* Pick a random object */ 
    1432                     k_idx = get_obj_num(base); 
    1433                      
    1434                     /* Keep if it's good, or try again */ 
    1435                     if (kind_is_good(k_idx)) break; 
    1436                 } 
    1437  
    1438 #if 0 
    1439                 /* Good objects */ 
    1440                 if (good) 
    1441                 { 
    1442                         /* Clear restriction */ 
    1443                         get_obj_num_hook = NULL; 
    1444  
    1445                         /* Prepare allocation table */ 
    1446                         get_obj_num_prep(); 
    1447                 } 
    1448 #endif 
    1449  
    1450                 /* Handle failure */ 
    1451                 if (!k_idx) return (FALSE); 
    1452  
    1453                 /* Prepare the object */ 
    1454                 object_prep(j_ptr, k_idx); 
    1455         } 
     1393                /* Keep if it's good, or try again */ 
     1394                if (kind_is_good(k_idx)) break; 
     1395        } 
     1396 
     1397        if (!k_idx) return FALSE; 
     1398 
     1399        /* Prepare the object */ 
     1400        object_prep(j_ptr, k_idx); 
    14561401 
    14571402        /* Apply magic (allow artifacts) */ 
     
    14601405 
    14611406        /* Generate multiple items */ 
    1462         /* Imported from Steamband and Sangband */ 
    1463         /* XXX Will probably not work so well for stacks of potions (yet) */ 
    14641407        k_ptr = &k_info[j_ptr->k_idx]; 
    14651408 
     
    14811424        } 
    14821425 
    1483         /* Success */ 
    1484         return (TRUE); 
     1426        return TRUE; 
    14851427} 
    14861428