changeset 2286:288e0b29a493

some cleanups in ItemsTable::GenerateItem
author Grumpy7
date Sat, 15 Mar 2014 19:53:14 +0100
parents 4fd03103be62
children 4e3236a4ea63
files Items.cpp
diffstat 1 files changed, 79 insertions(+), 155 deletions(-) [+]
line wrap: on
line diff
--- a/Items.cpp	Sat Mar 15 12:24:20 2014 +0100
+++ b/Items.cpp	Sat Mar 15 19:53:14 2014 +0100
@@ -978,13 +978,8 @@
 
 //----- (00456620) --------------------------------------------------------
 void ItemsTable::GenerateItem(int treasure_level, unsigned int uTreasureType, ItemGen *out_item)
-    {
-
-    ItemsTable *v5; // edi@1
-    int v6; // ebx@3
-    //int *v7; // ecx@33
-    //int v8; // eax@34
-    //int v9; // eax@39
+  {
+    int treasureLevelMinus1; // ebx@3
     int current_chance; // ebx@43
     int tmp_chance; // ecx@47
     int v17; // ebx@57
@@ -1000,21 +995,17 @@
     int j; // eax@121
     int val_list[800]; // [sp+Ch] [bp-C88h]@33
     int total_chance; // [sp+C8Ch] [bp-8h]@33
-    int v54; // [sp+C9Ch] [bp+8h]@3
-    //int v55; // [sp+CA0h] [bp+Ch]@34
     signed int v56; // [sp+CA0h] [bp+Ch]@55
     int v57; // [sp+CA0h] [bp+Ch]@62
 
-    v5 = this;
     if (!out_item)
         out_item = (ItemGen *)malloc(sizeof(ItemGen));
     memset(out_item, 0, sizeof(*out_item));
 
 
-    v6 = treasure_level - 1;
-    v54 = treasure_level - 1;
+    treasureLevelMinus1 = treasure_level - 1;
     if ( uTreasureType ) //generate known treasure type
-        {
+    {
         ITEM_EQUIP_TYPE   requested_equip;
         PLAYER_SKILL_TYPE requested_skill = PLAYER_SKILL_INVALID;
         switch (uTreasureType)
@@ -1082,82 +1073,61 @@
 
         current_chance = 0;
         if ( total_chance )
-            current_chance = rand() % total_chance;
-
-        out_item->uItemID = val_list[0];
-        if (!out_item->uItemID)
-            out_item->uItemID = 1;
-
-        if ( pItems[out_item->uItemID].uChanceByTreasureLvl[treasure_level - 1] < current_chance )
-            {
-            j=0;
-            tmp_chance=pItems[out_item->uItemID].uChanceByTreasureLvl[treasure_level - 1];
-            do
-                {
-                ++j;
-                out_item->uItemID = val_list[j];
-                tmp_chance += pItems[val_list[j]].uChanceByTreasureLvl[treasure_level - 1];
-                }
-                while ( tmp_chance < current_chance );
-            }
-
-        if (out_item->GetItemEquipType() == EQUIP_POTION && out_item->uItemID != ITEM_POTION_BOTTLE )
-            {// if it potion set potion spec
-            out_item->uEnchantmentType = 0;
-            for (int i=0; i<2; ++i)
-                out_item->uEnchantmentType += rand() % 4 + 1;
-            out_item->uEnchantmentType = out_item->uEnchantmentType * treasure_level; 
-            }
+        {
+          current_chance = rand() % total_chance + 1;
+          tmp_chance = 0;
+          j=0;
+          while(tmp_chance < current_chance)
+          {
+            out_item->uItemID = val_list[j];
+            tmp_chance += pItems[val_list[j]].uChanceByTreasureLvl[treasure_level - 1];
+            ++j;
+          }
         }
+        else
+        {
+          out_item->uItemID = 1;
+        }
+    }
     else
-        {
+    {
    //artifact
-        v56 = 0;
-        for(int i=0; i<29; ++i) 
+        if ( treasureLevelMinus1 == 5 )
+        {
+          v56 = 0;
+          for(int i=0; i<29; ++i) 
             v56 += pParty->pIsArtifactFound[i];
-
-        v17 = rand() % 29;
-
-        if ( v6 == 5 && (rand() % 100 < 5) && !pParty->pIsArtifactFound[v17] && v56 < 13 )
-            {
+          v17 = rand() % 29;
+          if ((rand() % 100 < 5) && !pParty->pIsArtifactFound[v17] && v56 < 13)
+          {
             pParty->pIsArtifactFound[v17] = 1;
             out_item->uAttributes = 0;
             out_item->uItemID = v17 + 500;
             SetSpecialBonus(out_item);
             return;
-            }
-
+          }
+        }
+        
         v57 = 0;
-        v18 = rand() % v5->uChanceByTreasureLvlSumm[treasure_level - 1];
-        out_item->uItemID = 0;
-        if ( v18 > 0 )
-            {
-            do
-                {      
-            v57 += pItems[out_item->uItemID + 1].uChanceByTreasureLvl[v6];
-            ++out_item->uItemID;
-                }
-            while ( v57 < v18 );
-            }
-
-        if ( !v18 )
-            out_item->uItemID = 1;
-        if ( !out_item->uItemID )
-            out_item->uItemID = 1;
-        if (out_item->GetItemEquipType() == EQUIP_POTION && out_item->uItemID != ITEM_POTION_BOTTLE )
-            {// if it potion set potion spec
-            out_item->uEnchantmentType = 0;
-            for (int i=0; i<2; ++i)
-                out_item->uEnchantmentType += rand() % 4 + 1;
-            out_item->uEnchantmentType = out_item->uEnchantmentType * treasure_level; 
-            }
-        out_item->uEnchantmentType = out_item->uEnchantmentType * treasure_level; 
+        v18 = rand() % this->uChanceByTreasureLvlSumm[treasure_level - 1] + 1;
+        while (v57 < v18)
+        {
+          ++out_item->uItemID;
+          v57 += pItems[out_item->uItemID].uChanceByTreasureLvl[treasureLevelMinus1];
         }
+    }
+    if (out_item->GetItemEquipType() == EQUIP_POTION && out_item->uItemID != ITEM_POTION_BOTTLE )
+    {// if it potion set potion spec
+      out_item->uEnchantmentType = 0;
+      for (int i=0; i<2; ++i)
+        out_item->uEnchantmentType += rand() % 4 + 1;
+      out_item->uEnchantmentType = out_item->uEnchantmentType * treasure_level; 
+    }
 
     if ( out_item->uItemID == ITEM_SPELLBOOK_LIGHT_DIVINE_INTERVENTION
         && !(unsigned __int16)_449B57_test_bit(pParty->_quest_bits, 239) )
         out_item->uItemID = ITEM_SPELLBOOK_LIGHT_SUN_BURST;
-    if ( pItemsTable->pItems[out_item->uItemID + 1].uItemID_Rep_St )
+    if ( pItemsTable->pItems[out_item->uItemID].uItemID_Rep_St )
         out_item->uAttributes = 0;
     else
         out_item->uAttributes = 1;
@@ -1173,9 +1143,9 @@
     case EQUIP_SINGLE_HANDED:
     case EQUIP_TWO_HANDED :   
     case EQUIP_BOW :    
-        if ( !uBonusChanceWpSpecial[v6] )
+        if ( !uBonusChanceWpSpecial[treasureLevelMinus1] )
             return;
-        if ((uint)(rand() % 100)>=uBonusChanceWpSpecial[v6])
+        if ((uint)(rand() % 100)>=uBonusChanceWpSpecial[treasureLevelMinus1])
             return;
         break;
     case      EQUIP_ARMOUR :        
@@ -1187,26 +1157,21 @@
     case      EQUIP_BOOTS  :        
     case      EQUIP_RING   : 
         
-        if ( !uBonusChanceStandart[v6] )
+        if ( !uBonusChanceStandart[treasureLevelMinus1] )
             return;
         special_chance = rand() % 100;
-        if ( special_chance < uBonusChanceStandart[v6])
+        if ( special_chance < uBonusChanceStandart[treasureLevelMinus1])
             {
-              v26 = rand() %pEnchantmentsSumm[out_item->GetItemEquipType()-3]; 
-            out_item->uEnchantmentType = 0;
-            v27=pEnchantments[out_item->uEnchantmentType].to_item[out_item->GetItemEquipType()-3];
-            if (v26>v27 )
-                {
-                do 
-                {
+              v26 = rand() %pEnchantmentsSumm[out_item->GetItemEquipType()-3] + 1; 
+              v27 = 0;
+              while(v27 < v26)
+              {
                 ++out_item->uEnchantmentType;
                 v27+=pEnchantments[out_item->uEnchantmentType].to_item[out_item->GetItemEquipType()-3];
-                } while (v26>v27);
-            }
-            ++out_item->uEnchantmentType;
+              }
 
-            v33 = rand() % (bonus_ranges[v6].maxR - bonus_ranges[v6].minR + 1);
-            out_item->m_enchantmentStrength = v33 + bonus_ranges[v6].minR;
+            v33 = rand() % (bonus_ranges[treasureLevelMinus1].maxR - bonus_ranges[treasureLevelMinus1].minR + 1);
+            out_item->m_enchantmentStrength = v33 + bonus_ranges[treasureLevelMinus1].minR;
             v32 = out_item->uEnchantmentType - 1;
             if ( v32 == 21 || v32 == 22 || v32 == 23 ) //Armsmaster skill, Dodge skill, Unarmed skill 
                 out_item->m_enchantmentStrength = out_item->m_enchantmentStrength/2;
@@ -1215,15 +1180,13 @@
             return;
             
             }
-        if ( !uBonusChanceSpecial[v6])
-            return;
-        v34 = uBonusChanceStandart[v6] + uBonusChanceSpecial[v6];
-        if ( special_chance>v34 )
-            return;
+        else if ( special_chance >= uBonusChanceStandart[treasureLevelMinus1] + uBonusChanceSpecial[treasureLevelMinus1] )
+          return;
         break;
     case EQUIP_WAND:
         out_item->uNumCharges = rand() % 6 + out_item->GetDamageMod() + 1;
         out_item->uMaxCharges = out_item->uNumCharges;
+        return;
     default:
         return;
         }
@@ -1233,71 +1196,32 @@
     int spc;
     memset(&val_list, 0, 3200);
     for (unsigned int i=0; i<pSpecialEnchantments_count;++i)
+    {
+      int tr_lv= pSpecialEnchantments[i].iTreasureLevel;
+      if ((treasure_level - 1 == 2) && ( tr_lv == 1 || tr_lv == 0 ) ||
+        (treasure_level - 1 == 3) && (tr_lv == 2 || tr_lv == 1 || tr_lv == 0) ||
+        (treasure_level - 1 == 4) && (tr_lv == 3 || tr_lv == 2 || tr_lv == 1) ||
+        (treasure_level - 1 == 5) && (tr_lv == 3)
+        )
+      {
+        spc=pSpecialEnchantments[i].to_item_apply[out_item->GetItemEquipType()];
+        spc_sum+=spc;
+        if(spc)
         {
-        int tr_lv= pSpecialEnchantments[i].iTreasureLevel;
-        switch ( treasure_level - 1 )
-            {
-        case 2:
-            if ((tr_lv==1)||(tr_lv==0))
-                {
-                spc=pSpecialEnchantments[i].to_item_apply[out_item->GetItemEquipType()];
-                spc_sum+=spc;
-                if(spc)
-                    {
-                    val_list[j++]=i;  
-                    }
-                }
-            break;
-        case 3:
-            if ((tr_lv==2)||(tr_lv==1)||(tr_lv==0))
-                {
-                spc=pSpecialEnchantments[i].to_item_apply[out_item->GetItemEquipType()];
-                spc_sum+=spc;
-                if(spc)
-                    {
-                    val_list[j++]=i;  
-                    }
-                }
-            break;
-        case 4:
-            if ((tr_lv==3)||(tr_lv==2)||(tr_lv==1))
-                {
-                spc=pSpecialEnchantments[i].to_item_apply[out_item->GetItemEquipType()];
-                spc_sum+=spc;
-                if(spc)
-                    {
-                    val_list[j++]=i;  
-                    }
-                }
-            break;
-        case 5:
-            if (tr_lv==3)
-                {
-                spc=pSpecialEnchantments[i].to_item_apply[out_item->GetItemEquipType()];
-                spc_sum+=spc;     
-                if(spc)
-                    {
-                    val_list[j++]=i;  
-                    }
-                }
-            break;
-            }
+          val_list[j++]=i;  
         }
+      }
+    }
 
     v46 = rand()%spc_sum+1;
     j=0;
-    out_item->uSpecEnchantmentType =val_list[j];
-    v45=pSpecialEnchantments[val_list[j]].to_item_apply[out_item->GetItemEquipType()];
-    if (v45<v46)
-        {
-        do 
-            {
-            ++j;
-            out_item->uSpecEnchantmentType=val_list[j];
-            v45+=pSpecialEnchantments[val_list[j]].to_item_apply[out_item->GetItemEquipType()];
-            } while (v45<v46);
-        }
-    ++out_item->uSpecEnchantmentType;
+    v45 = 0;
+    while (v45<v46)
+    {
+      ++j;
+      out_item->uSpecEnchantmentType=val_list[j];
+      v45+=pSpecialEnchantments[val_list[j]].to_item_apply[out_item->GetItemEquipType()];
+    }
 }
 
 //----- (004505CC) --------------------------------------------------------