view Items.cpp @ 336:ea55d6414029

Event_proceccor preliminary
author Gloval
date Thu, 21 Feb 2013 00:33:12 +0400
parents 2a09e1d6fc8f
children 0f35f908547f
line wrap: on
line source

#include <stdlib.h>
#include <assert.h>

#include "Items.h"
#include "MapInfo.h"
#include "FrameTableInc.h"
#include "Allocator.h"
#include "LOD.h"
#include "Monsters.h"
#include "Party.h"
#include "FactionTable.h"
#include "StorylineTextTable.h"
#include "texts.h"
#include "mm7_data.h"





ItemGen *ptr_50C9A4;

struct ItemsTable *pItemsTable; // 005D29E0





//----- (00439DF3) --------------------------------------------------------
int ItemGen::_439DF3_get_additional_damage(int *a2, int *a3)
{
  int *v3; // edi@1
  ItemGen *v4; // ebx@1
  signed int v5; // esi@1
  unsigned int v7; // ecx@5
  signed int v8; // ebx@7
  int v9; // edx@22
  int v10; // eax@24
  int v11; // edx@25
  int v12; // ebx@28
  int v13; // ebx@29
  int v14; // ebx@30
  int v15; // ebx@31
  int v16; // ebx@32
  signed int v17; // [sp-4h] [bp-10h]@24

  auto a1 = this;
  v3 = a2;
  v4 = a1;
  v5 = 0;
  *a2 = 0;
  if ( !a1->uItemID )
    return 0;
  UpdateTempBonus(pParty->uTimePlayed);
  if ( v4->uItemID == 501 )
  {
    *v3 = 1;
    v10 = rand();
    v17 = 10;
    return v10 % v17 + 6;
  }
  if ( v4->uItemID == 507 )
  {
    *v3 = 0;
    v11 = rand() % 16;
    return v11 + 3;
  }
  v7 = 3;
  if ( v4->uItemID == 510 )
  {
    *v3 = 2;
    v9 = rand() % 4;
    return v9 + 9;
  }
  if ( v4->uItemID == 517 )
  {
    v5 = 8;
    *v3 = v5;
    return v5;
  }
  v8 = v4->uAdditionalValue;
  if ( v8 > 46 )
  {
    v5 = 0;
    *v3 = v5;
    return v5;
  }
  if ( v8 == 46 )
  {
    *v3 = 0;
    return rand() % 11 + 10;
  }
  if ( v8 > 11 )
  {
    v12 = v8 - 12;
    if ( !v12 )
    {
      *v3 = 0;
      return GetDiceResult(v7, 6u);
    }
    v13 = v12 - 1;
    if ( !v13 )
    {
      *v3 = 8;
      return 5;
    }
    v14 = v13 - 1;
    if ( v14 )
    {
      v15 = v14 - 1;
      if ( v15 )
      {
        v16 = v15 - 1;
        if ( !v16 || v16 == 25 )
        {
          *v3 = 10;
          *a3 = 1;
          return v5;
        }
		v5 = 0;
		*v3 = v5;
		return v5;
      }
      *v3 = 2;
      return 12;
    }
    v5 = 8;
    *v3 = v5;
    return v5;
  }
  if ( v8 == 11 )
  {
    *v3 = 0;
    v7 = 2;
    return GetDiceResult(v7, 6u);
  }
  if ( v8 == 4 )
  {
    *v3 = 2;
    v11 = rand() % 2;
    return v11 + 3;
  }
  if ( v8 == 5 )
  {
    *v3 = 2;
    v10 = rand();
    v17 = 3;
    return v10 % v17 + 6;
  }
  if ( v8 == 6 )
  {
    *v3 = 2;
    v9 = rand() % 4;
    return v9 + 9;
  }
  if ( v8 == 7 )
  {
    *v3 = 1;
    return rand() % 4 + 2;
  }
  if ( v8 == 8 )
  {
    *v3 = 1;
    return rand() % 7 + 4;
  }
  if ( v8 == 9 )
  {
    *v3 = 1;
    v10 = rand();
    v17 = 10;
    return v10 % v17 + 6;
  }
  if ( v8 != 10 )
  {
        v5 = 0;
    *v3 = v5;
    return v5;
  }
  *v3 = 0;
  v7 = 1;
  return GetDiceResult(v7, 6u);
}


//----- (00402F07) --------------------------------------------------------
void ItemGen::Reset()
{
  this->field_1A = 0;
  this->uAttributes = 0;
  this->uNumCharges = 0;
  this->uAdditionalValue = 0;
  this->_bonus_strength = 0;
  this->_bonus_type = 0;
  this->uItemID = 0;
  this->uBodyAnchor = 0;
  this->uExpireTime = 0i64;
}

//----- (00458260) --------------------------------------------------------
void ItemGen::UpdateTempBonus(__int64 uTimePlayed)
{
  unsigned int v2; // eax@1

  v2 = this->uAttributes;
  if ( v2 & ITEM_TEMP_BONUS )
  {
    if ( uTimePlayed > (signed __int64)this->uExpireTime )
    {
      this->_bonus_type = 0;
      LOBYTE(v2) = v2 & 0xF7;
      this->uAdditionalValue = 0;
      this->uAttributes = v2;
    }
  }
}

//----- (0045814E) --------------------------------------------------------
char *ItemsTable::Release()
{
  ItemsTable *v1; // edi@1
  char *result; // eax@23

  v1 = this;
  if ( pMonstersTXT_Raw )
    pAllocator->FreeChunk(pMonstersTXT_Raw);
  if ( pMonsterPlacementTXT_Raw )
    pAllocator->FreeChunk(pMonsterPlacementTXT_Raw);
  if ( pSkillDescTXT_Raw )
    pAllocator->FreeChunk(pSkillDescTXT_Raw);
  if ( v1->pSpcItemsTXT_Raw )
    pAllocator->FreeChunk(v1->pSpcItemsTXT_Raw);
  if ( v1->pStdItemsTXT_Raw )
    pAllocator->FreeChunk(v1->pStdItemsTXT_Raw);
  if ( v1->pRndItemsTXT_Raw )
    pAllocator->FreeChunk(v1->pRndItemsTXT_Raw);
  if ( v1->pItemsTXT_Raw )
    pAllocator->FreeChunk(v1->pItemsTXT_Raw);
  if ( pHostileTXT_Raw )
    pAllocator->FreeChunk(pHostileTXT_Raw);
  if ( pHistoryTXT_Raw )
    pAllocator->FreeChunk(pHistoryTXT_Raw);
  if ( pPotionsTXT_Raw )
    pAllocator->FreeChunk(pPotionsTXT_Raw);
  if ( pPotionNotesTXT_Raw )
    pAllocator->FreeChunk(pPotionNotesTXT_Raw);
  v1->pSpcItemsTXT_Raw = 0;
  pSkillDescTXT_Raw = 0;
  v1->pStdItemsTXT_Raw = 0;
  v1->pRndItemsTXT_Raw = pSkillDescTXT_Raw;
  result = pSkillDescTXT_Raw;
  v1->pItemsTXT_Raw = pSkillDescTXT_Raw;
  return result;
}


//----- (00456D84) --------------------------------------------------------
void ItemsTable::Initialize()
	{
	int i,j;
	char* test_string;
	unsigned char c;
	bool break_loop;
	unsigned int temp_str_len;
	char* tmp_pos;
	int decode_step;
	int item_counter;

	pMapStats = new MapStats;
	pMapStats->Initialize();

	pMonsterStats = new MonsterStats;
	pMonsterStats->Initialize();
	pMonsterStats->InitializePlacements();

	pSpellStats = new SpellStats;
	pSpellStats->Initialize();

	LoadPotions();
	LoadPotionNotes();

	pFactionTable = new FactionTable;
	pFactionTable->Initialize();

	pStorylineText = new StorylineText;
	pStorylineText->Initialize();

	pStdItemsTXT_Raw = NULL;
	pStdItemsTXT_Raw = (char *)pEvents_LOD->LoadRaw("stditems.txt", 0);
	strtok(pStdItemsTXT_Raw, "\r");
	strtok(NULL, "\r");
	strtok(NULL, "\r");
	strtok(NULL, "\r");
	//Standard Bonuses by Group	
	for (i=0;i<24;++i)
		{
		test_string = strtok(NULL, "\r") + 1;
		break_loop = false;
		decode_step=0;
		do 
			{
			c = *(unsigned char*)test_string;
			temp_str_len = 0;
			while((c!='\t')&&(c>0))
				{
				++temp_str_len;
				c=test_string[temp_str_len];
				}		
			tmp_pos=test_string+temp_str_len;
			if (*tmp_pos == 0)
				break_loop = true;
			*tmp_pos = 0;
			if (temp_str_len)
				{
				switch (decode_step)
					{
				case 0: 
					pEnchantments[i].pBonusStat=RemoveQuotes(test_string);
					break;
				case 1:
					pEnchantments[i].pOfName= RemoveQuotes(test_string);
					break;
				default:
					pEnchantments[i].to_item[decode_step-2]=atoi(test_string);
					}
				}
			else
				{ 
				if (!decode_step)
					break_loop = true;
				}
			++decode_step;
			test_string=tmp_pos+1;
			} while ((decode_step<11)&&!break_loop);
		}

	memset(&pEnchantmentsSumm, 0, 36);
	for(i=0;i<9;++i)
		{
		for (j=0;j<24;++j)
			pEnchantmentsSumm[i]+=pEnchantments[j].to_item[i];
		}

	//Bonus range for Standard by Level
	strtok(NULL, "\r");
	strtok(NULL, "\r");
	strtok(NULL, "\r");
	strtok(NULL, "\r");
	strtok(NULL, "\r");
	for(i=0;i<6;++i) //counted from 1
		{
		test_string = strtok(NULL, "\r") + 1;
		break_loop = false;
		decode_step=0;
		do 
			{
			c = *(unsigned char*)test_string;
			temp_str_len = 0;
			while((c!='\t')&&(c>0))
				{
				++temp_str_len;
				c=test_string[temp_str_len];
				}		
			tmp_pos=test_string+temp_str_len;
			if (*tmp_pos == 0)
				break_loop = true;
			*tmp_pos = 0;
			if  (decode_step==2)
				bonus_ranges[i].minR = atoi(test_string);
			else if (decode_step==3)
				bonus_ranges[i].maxR =atoi(test_string);
			++decode_step;
			test_string=tmp_pos+1;
			} while ((decode_step<4)&&!break_loop);
		}


	pSpcItemsTXT_Raw = 0;
	pSpcItemsTXT_Raw = (char *)pEvents_LOD->LoadRaw("spcitems.txt", 0);
	strtok(pSpcItemsTXT_Raw, "\r");
	strtok(NULL, "\r");
	strtok(NULL, "\r");
	strtok(NULL, "\r");
	for (i=0;i<72;++i)
		{
		test_string = strtok(NULL, "\r") + 1;
		break_loop = false;
		decode_step=0;
		do 
			{
			c = *(unsigned char*)test_string;
			temp_str_len = 0;
			while((c!='\t')&&(c>0))
				{
				++temp_str_len;
				c=test_string[temp_str_len];
				}		
			tmp_pos=test_string+temp_str_len;
			if (*tmp_pos == 0)
				break_loop = true;
			*tmp_pos = 0;
			if (temp_str_len)
				{
				switch (decode_step)
					{
				case 0: 
					pSpecialEnchantments[i].pBonusStatement=RemoveQuotes(test_string);
					break;
				case 1:
					pSpecialEnchantments[i].pNameAdd= RemoveQuotes(test_string);
					break;
				case 14:
					int res;
					res=atoi(test_string);
					if(!res)
						{
						++test_string; 
						while (*test_string==' ')//fix X 2 case
							++test_string; 
						res=atoi(test_string);
						}				
					pSpecialEnchantments[i].iValue=res;
					break;
				case 15:
					pSpecialEnchantments[i].iTreasureLevel=  tolower(*test_string) - 97;;
					break;
				default:
					pSpecialEnchantments[i].to_item_apply[decode_step-2]=atoi(test_string);
					}
				}
			else
				{ 
				if (!decode_step)
					break_loop = true;
				}
			++decode_step;
			test_string=tmp_pos+1;
			} while ((decode_step<16)&&!break_loop);
		}

	pSpecialEnchantments_count = 71;
	memset(&pSpecialEnchantmentsSumm, 0, 96);
	for(i=0;i<12;++i)
		{
		for (j=0;j<pSpecialEnchantments_count;++j)
			pSpecialEnchantmentsSumm[i]+=pSpecialEnchantments[j].to_item_apply[i];
		}



	Initialize2DA();

	pItemsTXT_Raw = NULL;
	pItemsTXT_Raw = (char*) pEvents_LOD->LoadRaw("items.txt", 0);
	strtok(pItemsTXT_Raw, "\r");
	strtok(NULL, "\r");
	uAllItemsCount = 0;
	item_counter = 0;
	while (true)
		{
		test_string = strtok(NULL, "\r") + 1;
		break_loop = false;
		decode_step=0;
		do 
			{
			c = *(unsigned char*)test_string;
			temp_str_len = 0;
			while((c!='\t')&&(c>0))
				{
				++temp_str_len;
				c=test_string[temp_str_len];
				}		
			tmp_pos=test_string+temp_str_len;
			if (*tmp_pos == 0)
				break_loop = true;
			*tmp_pos = 0;
			if (temp_str_len)
				{
				switch (decode_step)
					{
				case 0: //Item #
					item_counter=atoi(test_string);
					uAllItemsCount=item_counter;
					break;
				case 1: //Pic File
					pItems[item_counter].pIconName = RemoveQuotes(test_string);
					break;
				case 2: //Name
					pItems[item_counter].pName = RemoveQuotes(test_string);
					break;
				case 3: //Value
					pItems[item_counter].uValue=atoi(test_string);
					break;
				case 4: //Equip Stat
					{
					if ( !_strcmpi(test_string, "weapon") )
						{
						pItems[item_counter].uEquipType = 0;
						break;
						}
					if ( !_strcmpi(test_string, "weapon2") )
						{
						pItems[item_counter].uEquipType = 1;
						break;
						}
					if ( !_strcmpi(test_string, "weapon1or2") )
						{
						pItems[item_counter].uEquipType = 0;
						break;
						}
					if ( !(_strcmpi(test_string, "missile")&&_strcmpi(test_string, "bow")))
						{
						pItems[item_counter].uEquipType = 2;
						break;
						}
					if ( !_strcmpi(test_string, "armor") )
						{
						pItems[item_counter].uEquipType = 3;
						break;
						}
					if ( !_strcmpi(test_string, "shield") )
						{
						pItems[item_counter].uEquipType = 4;
						break;
						}
					if ( !_strcmpi(test_string, "helm") )
						{
						pItems[item_counter].uEquipType = 5;
						break;
						}
					if ( !_strcmpi(test_string, "belt") )
						{
						pItems[item_counter].uEquipType = 6;
						break;
						}
					if ( !_strcmpi(test_string, "cloak") )
						{
						pItems[item_counter].uEquipType = 7;
						break;
						}
					if ( !_strcmpi(test_string, "gauntlets") )
						{
						pItems[item_counter].uEquipType = 8;
						break;
						}
					if ( !_strcmpi(test_string, "boots") )
						{
						pItems[item_counter].uEquipType = 9;
						break;
						}
					if ( !_strcmpi(test_string, "ring") )
						{
						pItems[item_counter].uEquipType = 10;
						break;
						}
					if ( !_strcmpi(test_string, "amulet") )
						{
						pItems[item_counter].uEquipType = 11;
						break;
						}
					if ( !_strcmpi(test_string, "weaponw") )
						{
						pItems[item_counter].uEquipType = 12;
						break;
						}
					if ( !(_strcmpi(test_string, "herb")&&_strcmpi(test_string, "reagent")))
						{
						pItems[item_counter].uEquipType = 13;
						break;
						}
					if ( !_strcmpi(test_string, "bottle") )
						{
						pItems[item_counter].uEquipType = 14;
						break;
						}
					if ( !_strcmpi(test_string, "sscroll") )
						{
						pItems[item_counter].uEquipType = 15;
						break;
						}
					if ( !_strcmpi(test_string, "book") )
						{
						pItems[item_counter].uEquipType = 16;
						break;
						}
					if ( !_strcmpi(test_string, "mscroll") )
						{
						pItems[item_counter].uEquipType = 17;
						break;
						}
					if ( !_strcmpi(test_string, "gold") )
						{
						pItems[item_counter].uEquipType = 18;
						break;
						}
					if ( !_strcmpi(test_string, "gem") )
						{
						pItems[item_counter].uEquipType = 19;
						break;
						}
					pItems[item_counter].uEquipType = 20;
					break;
					}
				case 5: //Skill Group
					{
					if ( !_strcmpi(test_string, "staff") )
						{
						pItems[item_counter].uSkillType = 0;
						break;
						}
					if ( !_strcmpi(test_string, "sword") )
						{
						pItems[item_counter].uSkillType = 1;
						break;
						}
					if ( !_strcmpi(test_string, "dagger") )
						{
						pItems[item_counter].uSkillType = 2;
						break;
						}
					if ( !_strcmpi(test_string, "axe") )
						{
						pItems[item_counter].uSkillType = 3;
						break;
						}
					if ( !_strcmpi(test_string, "spear") )
						{
						pItems[item_counter].uSkillType = 4;
						break;
						}
					if ( !_strcmpi(test_string, "bow") )
						{
						pItems[item_counter].uSkillType = 5;
						break;
						}
					if ( !_strcmpi(test_string, "mace") )
						{
						pItems[item_counter].uSkillType = 6;
						break;
						}
					if ( !_strcmpi(test_string, "blaster") )
						{
						pItems[item_counter].uSkillType = 7;
						break;
						}
					if ( !_strcmpi(test_string, "shield") )
						{
						pItems[item_counter].uSkillType = 8;
						break;
						}
					if ( !_strcmpi(test_string, "leather") )
						{
						pItems[item_counter].uSkillType = 9;
						break;
						}
					if ( !_strcmpi(test_string, "chain") )
						{
						pItems[item_counter].uSkillType = 10;
						break;
						}
					if ( !_strcmpi(test_string, "plate") )
						{
						pItems[item_counter].uSkillType = 11;
						break;
						}
					if ( !_strcmpi(test_string, "club") )
						{
						pItems[item_counter].uSkillType = 37;
						break;
						}
					pItems[item_counter].uSkillType = 38;
					break;
					}
				case 6: //Mod1
					{
					int ii;
					char* test_char;
					int tst_len;
					tst_len=strlen(test_string);
					pItems[item_counter].uDamageDice=0;
					pItems[item_counter].uDamageRoll=0;
					test_char=test_string;
					for (ii=0; ii<tst_len; ++ii)
						{
						if (tolower(*test_char)=='d')
							{
							*test_char=0;
							pItems[item_counter].uDamageDice=atoi(test_string);
							pItems[item_counter].uDamageRoll=atoi(test_char+1);
							*test_char='d';
							break;
							}
						++test_char;
						}
					test_char=test_string;
					if ((ii==tst_len)&&(tolower(*test_char)=='s'))
						{
						pItems[item_counter].uDamageDice=atoi(test_char+1);
						pItems[item_counter].uDamageRoll=1;
						}
					break;
					}
				case 7: //Mod2
					pItems[item_counter].uDamageMod=atoi(test_string);
					break;
				case 8: //material
					{
					if ( !_strcmpi(test_string, "artifact") )
						{
						pItems[item_counter].uMaterial = 1;
						break;
						}
					if ( !_strcmpi(test_string, "relic") )
						{
						pItems[item_counter].uMaterial = 2;
						break;
						}
					if ( !_strcmpi(test_string, "special") )
						{
						pItems[item_counter].uMaterial = 3;
						break;
						}
					pItems[item_counter].uMaterial = 0;
					break;}
				case 9:  //ID/Rep/St
					pItems[item_counter].uItemID_Rep_St=atoi(test_string);
					break;
				case 10: //Not identified name
					pItems[item_counter].pUnidentifiedName = RemoveQuotes(test_string);
					break;
				case 11: //Sprite Index
					pItems[item_counter].uSpriteID=atoi(test_string);
					break;
				case 12: //VarA
					{
					pItems[item_counter]._additional_value=0;
					pItems[item_counter]._bonus_type=0;
					if (pItems[item_counter].uMaterial==3)
						{
						for(int ii=0; ii<24; ++ii)
							{
							if (stricmp(test_string,pEnchantments[ii].pBonusStat))
								{
								pItems[item_counter]._bonus_type=ii+1;
								break;
								}
							}
						if (!pItems[item_counter]._bonus_type)
							{
							for(int ii=0; ii<72; ++ii)
								{
								if (stricmp(test_string,pSpecialEnchantments[ii].pBonusStatement))
									{
									pItems[item_counter]._additional_value=ii+1;
									}
								}
							}
						}

					break;
					}
				case 13: //VarB
					if ((pItems[item_counter].uMaterial==3)&&(pItems[item_counter]._bonus_type))
						{
						char b_s=atoi(test_string);
						if (b_s)
							pItems[item_counter]._bonus_strength=b_s;
						else
							pItems[item_counter]._bonus_strength=1;
						}
					else
						pItems[item_counter]._bonus_strength=0;
					break;
				case 14: //Equip X
					pItems[item_counter].uEquipX=atoi(test_string);
					break;
				case 15: //Equip Y
					pItems[item_counter].uEquipY=atoi(test_string);
					break;
				case 16: //Notes
					pItems[item_counter].pDescription = RemoveQuotes(test_string);
					break;

					}
				}
			else
				{ 
				if (!decode_step)
					break_loop = true;
				}
			++decode_step;
			test_string=tmp_pos+1;
			} while ((decode_step<17)&&!break_loop);
			++item_counter;
			if (item_counter>799)
				break;
		}


	pRndItemsTXT_Raw = NULL;
	uAllItemsCount = item_counter;
	pRndItemsTXT_Raw = (char *)pEvents_LOD->LoadRaw("rnditems.txt", 0);
	strtok(pRndItemsTXT_Raw, "\r");
	strtok(NULL, "\r");
	strtok(NULL, "\r");
	strtok(NULL, "\r");
	item_counter = 0;
	while (true)
		{
		test_string = strtok(NULL, "\r") + 1;
		break_loop = false;
		decode_step=0;
		do 
			{
			c = *(unsigned char*)test_string;
			temp_str_len = 0;
			while((c!='\t')&&(c>0))
				{
				++temp_str_len;
				c=test_string[temp_str_len];
				}		
			tmp_pos=test_string+temp_str_len;
			if (*tmp_pos == 0)
				break_loop = true;
			*tmp_pos = 0;
			if (temp_str_len)
				{
				switch (decode_step)
					{
				case 0: 
					item_counter=atoi(test_string);
					break;
				case 2:
					pItems[item_counter].uChanceByTreasureLvl1=atoi(test_string);
					break;
				case 3:
					pItems[item_counter].uChanceByTreasureLvl2=atoi(test_string);
					break;
				case 4:
					pItems[item_counter].uChanceByTreasureLvl3=atoi(test_string);
					break;
				case 5:
					pItems[item_counter].uChanceByTreasureLvl4=atoi(test_string);
					break;
				case 6:
					pItems[item_counter].uChanceByTreasureLvl5=atoi(test_string);
					break;
				case 7:
					pItems[item_counter].uChanceByTreasureLvl6=atoi(test_string);
					break;
					}
				}
			else
				{ 
				if (!decode_step)
					break_loop = true;
				}
			++decode_step;
			test_string=tmp_pos+1;
			} while ((decode_step<8)&&!break_loop);
			++item_counter;
			if (item_counter>618)
				break;
		}

	if ( pRndItemsTXT_Raw )
		{
		pAllocator->FreeChunk(pRndItemsTXT_Raw);
		pRndItemsTXT_Raw = NULL;
		}
	//ChanceByTreasureLvl Summ - anti cheating?
	memset(&uChanceByTreasureLvlSumm, 0, 24);
	for(i=0;i<6;++i)
		{
		for (j=0;j<800;++j)
			uChanceByTreasureLvlSumm[i]+=pItems[j].uChanceByTreasureLvl[i];
		}

	strtok(NULL, "\r");
	strtok(NULL, "\r");
	strtok(NULL, "\r");
	strtok(NULL, "\r");
	strtok(NULL, "\r");
	for (i=0;i<3;++i)
		{
		test_string = strtok(NULL, "\r") + 1;
		break_loop = false;
		decode_step=0;
		do 
			{
			c = *(unsigned char*)test_string;
			temp_str_len = 0;
			while((c!='\t')&&(c>0))
				{
				++temp_str_len;
				c=test_string[temp_str_len];
				}		
			tmp_pos=test_string+temp_str_len;
			if (*tmp_pos == 0)
				break_loop = true;
			*tmp_pos = 0;
			if (temp_str_len)
				{
				switch (decode_step)
					{
				case 2: 
					switch (i)
						{
					case 0:
						uBonusChanceStandart[0]=atoi(test_string);
						break;
					case 1:
						uBonusChanceSpecial[0]=atoi(test_string);
						break;
					case 2:
						uBonusChanceWpSpecial[0]=atoi(test_string);
						break;
						}
					break;
				case 3:
					switch (i)
						{
					case 0:
						uBonusChanceStandart[1]=atoi(test_string);
						break;
					case 1:
						uBonusChanceSpecial[1]=atoi(test_string);
						break;
					case 2:
						uBonusChanceWpSpecial[1]=atoi(test_string);
						break;
						}
					break;
				case 4: 
					switch (i)
						{
					case 0:
						uBonusChanceStandart[2]=atoi(test_string);
						break;
					case 1:
						uBonusChanceSpecial[2]=atoi(test_string);
						break;
					case 2:
						uBonusChanceWpSpecial[2]=atoi(test_string);
						break;
						}
					break;
				case 5:
					switch (i)
						{
					case 0:
						uBonusChanceStandart[3]=atoi(test_string);
						break;
					case 1:
						uBonusChanceSpecial[3]=atoi(test_string);
						break;
					case 2:
						uBonusChanceWpSpecial[3]=atoi(test_string);
						break;
						}
					break;
				case 6: 
					switch (i)
						{
					case 0:
						uBonusChanceStandart[4]=atoi(test_string);
						break;
					case 1:
						uBonusChanceSpecial[4]=atoi(test_string);
						break;
					case 2:
						uBonusChanceWpSpecial[4]=atoi(test_string);
						break;
						}
					break;
				case 7:
					switch (i)
						{
					case 0:
						uBonusChanceStandart[5]=atoi(test_string);
						break;
					case 1:
						uBonusChanceSpecial[5]=atoi(test_string);
						break;
					case 2:
						uBonusChanceWpSpecial[5]=atoi(test_string);
						break;
						}
					break;
					}
				}
			else
				{ 
				if (!decode_step)
					break_loop = true;
				}
			++decode_step;
			test_string=tmp_pos+1;
			} while ((decode_step<8)&&!break_loop);
		}

	pSkillDescTXT_Raw = NULL;
	pSkillDescTXT_Raw = (char *)pEvents_LOD->LoadRaw("skilldes.txt", 0);
	strtok(pSkillDescTXT_Raw, "\r");
	for (i=0; i<37; ++i)
		{
		test_string = strtok(NULL, "\r") + 1;
		break_loop = false;
		decode_step=0;
		do 
			{
			c = *(unsigned char*)test_string;
			temp_str_len = 0;
			while((c!='\t')&&(c>0))
				{
				++temp_str_len;
				c=test_string[temp_str_len];
				}		
			tmp_pos=test_string+temp_str_len;
			if (*tmp_pos == 0)
				break_loop = true;
			*tmp_pos = 0;
			if (temp_str_len)
				{
				switch (decode_step)
					{
				case 1:
					pSkillDesc[i] = RemoveQuotes(test_string);
					break;
				case 2:
					pNormalSkillDesc[i] = RemoveQuotes(test_string);
					break;
				case 3:
					pExpertSkillDesc[i] = RemoveQuotes(test_string);
					break;
				case 4:
					pMasterSkillDesc[i] = RemoveQuotes(test_string);
					break;
				case 5:
					pGrandSkillDesc[i] = RemoveQuotes(test_string);
					break;	  
					}
				}
			else
				{ 
				if (!decode_step)
					break_loop = true;
				}
			++decode_step;
			test_string=tmp_pos+1;
			} while ((decode_step<6)&&!break_loop);
		}

	pStatsTXT_Raw = 0;
	pStatsTXT_Raw = (char *)pEvents_LOD->LoadRaw("stats.txt", 0);
	strtok(pStatsTXT_Raw, "\r");
	for (i=0; i<26; ++i)
		{
		test_string = strtok(NULL, "\r") + 1;
		break_loop = false;
		decode_step=0;
		do 
			{
			c = *(unsigned char*)test_string;
			temp_str_len = 0;
			while((c!='\t')&&(c>0))
				{
				++temp_str_len;
				c=test_string[temp_str_len];
				}		
			tmp_pos=test_string+temp_str_len;
			if (*tmp_pos == 0)
				break_loop = true;
			*tmp_pos = 0;
			if (temp_str_len)
				{
				switch (i)
					{
				case 0:
				case 1:
				case 2:
				case 3:
				case 4:
				case 5:
				case 6:
					pAttributeDescriptions[i] = RemoveQuotes(test_string);
					break;
				case 7:
					pHealthPointsAttributeDescription = RemoveQuotes(test_string);
					break;
				case 8:
					pArmourClassAttributeDescription = RemoveQuotes(test_string);
					break;
				case 9:
					pSpellPointsAttributeDescription = RemoveQuotes(test_string);
					break;
				case 10:
					pPlayerConditionAttributeDescription = RemoveQuotes(test_string);
					break;
				case 11:
					pFastSpellAttributeDescription = RemoveQuotes(test_string);
					break;
				case 12:
					pPlayerAgeAttributeDescription = RemoveQuotes(test_string);
					break;
				case 13:
					pPlayerLevelAttributeDescription = RemoveQuotes(test_string);
					break;
				case 14:
					pPlayerExperienceAttributeDescription = RemoveQuotes(test_string);
					break;
				case 15:
					pAttackBonusAttributeDescription = RemoveQuotes(test_string);
					break;
				case 16:
					pAttackDamageAttributeDescription = RemoveQuotes(test_string);
					break;
				case 17:
					pMissleBonusAttributeDescription = RemoveQuotes(test_string);
					break;
				case 18:
					pMissleDamageAttributeDescription = RemoveQuotes(test_string);
					break;
				case 19:
					pFireResistanceAttributeDescription = RemoveQuotes(test_string);
					break;
				case 20:
					pAirResistanceAttributeDescription = RemoveQuotes(test_string);
					break;
				case 21:
					pWaterResistanceAttributeDescription = RemoveQuotes(test_string);
					break;
				case 22:
					pEarthResistanceAttributeDescription = RemoveQuotes(test_string);
					break;
				case 23:
					pMindResistanceAttributeDescription = RemoveQuotes(test_string);
					break;
				case 24:
					pBodyResistanceAttributeDescription = RemoveQuotes(test_string);
					break;
				case 25:
					pSkillPointsAttributeDescription = RemoveQuotes(test_string);
					break;  
					}
				}
			else
				{ 
				if (!decode_step)
					break_loop = true;
				}
			++decode_step;
			test_string=tmp_pos+1;
			} while ((decode_step<2)&&!break_loop);
		}


	pClassTXT_Raw = 0;
	pClassTXT_Raw = (char *)pEvents_LOD->LoadRaw("class.txt", 0);
	strtok(pClassTXT_Raw, "\r");
	for (i=0; i<36; ++i)
		{
		test_string = strtok(NULL, "\r") + 1;
		break_loop = false;
		decode_step=0;
		do 
			{
			c = *(unsigned char*)test_string;
			temp_str_len = 0;
			while((c!='\t')&&(c>0))
				{
				++temp_str_len;
				c=test_string[temp_str_len];
				}		
			tmp_pos=test_string+temp_str_len;
			if (*tmp_pos == 0)
				break_loop = true;
			*tmp_pos = 0;
			if (temp_str_len)
				{
				//if(i) 
					pClassDescriptions[i]=RemoveQuotes(test_string);
				}
			else
				{ 
				if (!decode_step)
					break_loop = true;
				}
			++decode_step;
			test_string=tmp_pos+1;
			} while ((decode_step<2)&&!break_loop);
		}

	}

//----- (00456D17) --------------------------------------------------------
void ItemsTable::SetSpecialBonus(ItemGen *pItem)
{
  ItemDesc *v2; // eax@1

  v2 = &this->pItems[pItem->uItemID];
  if ( v2->uMaterial == 3 )
  {
    pItem->_bonus_type = v2->_bonus_type;
    pItem->uAdditionalValue = v2->_additional_value;
    pItem->_bonus_strength = v2->_bonus_strength;
  }
}

//----- (00456D43) --------------------------------------------------------
bool ItemsTable::_456D43_is_material_equals_3(ItemGen *pItem)
{
  return this->pItems[pItem->uItemID].uMaterial == 3;
}

//----- (00456D5E) --------------------------------------------------------
bool ItemsTable::_456D5E_is_some_material(ItemGen *pItem)
{
  unsigned __int8 v2; // al@1

  v2 = this->pItems[pItem->uItemID].uMaterial;
  return v2 == 3 || v2 == 1 || v2 == 2;
}


//----- (00453B3C) --------------------------------------------------------
void ItemsTable::LoadPotions()
	{

	CHAR Text[90]; // [sp+Ch] [bp-6Ch]@26
	char* test_string;
	unsigned int uRow;
	unsigned int uColumn;
	unsigned __int8 potion_value;

	if ( pPotionsTXT_Raw )
		pAllocator->FreeChunk(pPotionsTXT_Raw);
	pPotionsTXT_Raw = NULL;
	pPotionsTXT_Raw = (char *)pEvents_LOD->LoadRaw("potion.txt", 0);
	test_string = strtok(pPotionsTXT_Raw,"\t\r\n");
	while ( 1 )
		{
		if ( !test_string )
			{
			MessageBoxA(0, "Error Pre-Parsing Potion Table", "Load Error", MB_ICONHAND|MB_CANCELTRYCONTINUE);
			return;
			}
		if ( !strcmp(test_string, "222") )
			break;

		test_string = strtok(NULL, "\t\r\n");
		}
	while ( 1 )
		{
		test_string = strtok(NULL, "\t\r\n");

		if ( !test_string )
			{
			MessageBoxA(0, "Error Pre-Parsing Potion Table", "Load Error", MB_ICONHAND|MB_CANCELTRYCONTINUE);
			return;
			}
		if ( !strcmp(test_string, "222") )
			break;
		}

	for (uRow = 0;uRow < 50; ++uRow)
		{
		int skip_count;
		for (skip_count = 0;skip_count < 6; ++skip_count)
			{
			if ( !strtok(NULL, "\r\t\n") )
				break;
			}
		if ( skip_count != 6 )
			break;
		for (uColumn = 0; uColumn < 50; ++uColumn)
			{
			test_string = strtok(NULL, "\r\t\n");   
			if ( !test_string )
				break;
			potion_value = atoi(test_string);
			unsigned char c=*test_string;
			if ( !potion_value )
				{
				if ( tolower(c) == 'e' )
					potion_value = atoi(test_string + 1);
				else
					potion_value = 0;
				}
			this->potion_data[uRow][uColumn]=potion_value;
			}
		if ( uColumn != 50 )
			break;
		strtok(NULL, "\r\t\n");
		}

	if ( uRow != 50 )
		{
		wsprintfA(Text, "Error Parsing Potion Table at Row: %d Column: %d", uRow, uColumn);
		MessageBoxA(0, Text, "Parsing Error", MB_ICONHAND|MB_CANCELTRYCONTINUE);
		}
	if ( pPotionsTXT_Raw )
		{
		pAllocator->FreeChunk(pPotionsTXT_Raw);
		pPotionsTXT_Raw = 0;
		}
	}

//----- (00453CE5) --------------------------------------------------------
void ItemsTable::LoadPotionNotes()
	{

	CHAR Text[90]; 
	char* test_string;
	unsigned int uRow;
	unsigned int uColumn;
	unsigned __int8 potion_note;

	if ( pPotionNotesTXT_Raw )
		pAllocator->FreeChunk(pPotionNotesTXT_Raw);
	pPotionNotesTXT_Raw = NULL;
	pPotionNotesTXT_Raw = (char *)pEvents_LOD->LoadRaw("potnotes.txt", 0);
	test_string = strtok(pPotionNotesTXT_Raw ,"\t\r\n");
	while ( 1 )
		{
		if ( !test_string )
			{
			MessageBoxA(0, "Error Pre-Parsing Potion Table", "Load Error", MB_ICONHAND|MB_CANCELTRYCONTINUE);
			return;
			}	
		if ( !strcmp(test_string, "222") )
			break;
		test_string = strtok(NULL, "\t\r\n");
		}
	while ( 1 )
		{
		test_string = strtok(NULL, "\t\r\n");

		if ( !test_string )
			{
			MessageBoxA(0, "Error Pre-Parsing Potion Table", "Load Error", MB_ICONHAND|MB_CANCELTRYCONTINUE);
			return;
			}
		if ( !strcmp(test_string, "222") )
			break;
		}

	for (uRow = 0;uRow < 50; ++uRow)
		{
		int skip_count;
		for (skip_count = 0;skip_count < 6; ++skip_count)
			{
			if ( !strtok(NULL, "\r\t\n") )
				break;
			}
		if ( skip_count != 6 )
			break;
		for (uColumn = 0; uColumn < 50; ++uColumn)
			{
			test_string = strtok(NULL, "\r\t\n");   
			if ( !test_string )
				break;
			potion_note = atoi(test_string);
			unsigned char c=*test_string;
			if ( !potion_note )
				{
				if ( tolower(c) == 'e' )
					potion_note = atoi(test_string + 1);
				else
					potion_note = 0;
				}
			this->potion_note[uRow][uColumn]=potion_note;
			}
		if ( uColumn != 50 )
			break;
		strtok(NULL, "\r\t\n");
		}
	if ( uRow != 50 )
		{
		wsprintfA(Text, "Error Parsing Potion Table at Row: %d Column: %d", uRow, uColumn);
		MessageBoxA(0, Text, "Parsing Error", MB_ICONHAND|MB_CANCELTRYCONTINUE);
		}
	}


//----- (00456442) --------------------------------------------------------
unsigned int ItemGen::GetValue()
{
  ItemGen *v1; // esi@1
  unsigned int uBaseValue; // edi@1
  int v3; // eax@4
  int v4; // esi@5
  unsigned int result; // eax@7

  v1 = this;
  uBaseValue = pItemsTable->pItems[this->uItemID].uValue;
  if ( this->uAttributes & ITEM_TEMP_BONUS || pItemsTable->_456D5E_is_some_material(this) )
    return uBaseValue;
  if ( v1->_bonus_type )
  {
    v3 = 100 * v1->_bonus_strength;
    return uBaseValue + v3;
  }
  v4 = v1->uAdditionalValue;
  if ( !v4 )
  {
    result = uBaseValue;
  }
  else
  {
	v3 = pItemsTable->pSpecialEnchantments[v4].iTreasureLevel;
    if ( (unsigned int)v3 > 0xA )
      return uBaseValue + v3;
    result = uBaseValue * v3;
  }
  return result;
}

//----- (00456499) --------------------------------------------------------
const char *ItemGen::GetDisplayName()
{
  const char *result; // eax@2

  if ( this->uAttributes & ITEM_IDENTIFIED )
    result = GetIdentifiedName();
  else
    result = pItemsTable->pItems[uItemID].pUnidentifiedName;
  return result;
}

//----- (004564B3) --------------------------------------------------------
const char *ItemGen::GetIdentifiedName()
{
  ItemGen *v1; // esi@1
  unsigned int v2; // eax@1
  unsigned __int8 v3; // cl@1
  char *v4; // edi@4
  char v5; // al@6
  const char *v6; // esi@8
  int v7; // eax@15
  const char *v9; // [sp-Ch] [bp-14h]@14
  const char *v10; // [sp-8h] [bp-10h]@4
  char *v11; // [sp-4h] [bp-Ch]@4

  v1 = this;
  v2 = this->uItemID;
  v3 = pItemsTable->pItems[v2].uEquipType;
  if ( v3 >= EQUIP_REAGENT && (v3 <= EQUIP_POTION || v3 == EQUIP_GOLD) )
  {
    v11 = pItemsTable->pItems[v2].pName;
    v4 = item__getname_buffer;
    v10 = "%s";
    sprintf(v4, v10, v11);
    return v4;
  }
  v4 = item__getname_buffer;
  sprintf(item__getname_buffer, "%s", pItemsTable->pItems[v2].pName);
  if ( v1->uItemID == 601 )
  {
    v5 = v1->field_1A;
    if ( (unsigned __int8)v5 >= 1u )
    {
      if ( (unsigned __int8)v5 <= 4u )
      {
        v6 = pPlayers[(unsigned __int8)v5]->pName;
        strlen(pPlayers[(unsigned __int8)v5]->pName);
        v11 = (char *)v6;
        if ( v6[strlen(v6) - 1] == 115 )
          v10 = pGlobalTXT_LocalizationStrings[655];
        else
          v10 = pGlobalTXT_LocalizationStrings[654];
		sprintf(v4, v10, v11);
		return v4;
      }
    }
  }
  if ( !pItemsTable->_456D5E_is_some_material(v1) )
  {
    if ( v1->_bonus_type )
    {
      strcat(item__getname_buffer, " ");
      v9 = (const char *)*((unsigned int *)&pItemsTable->pItems[799].uEquipType + 5 * v1->_bonus_type);
    }
    else
    {
      v7 = v1->uAdditionalValue;
      if ( !v7 )
        return v4;
      if ( v7 == 16
        || v7 == 39
        || v7 == 40
        || v7 == 45
        || v7 == 56
        || v7 == 57
        || v7 == 58
        || v7 == 60
        || v7 == 61
        || v7 == 59
        || v7 == 63
        || v7 == 64
        || v7 == 67
        || v7 == 68 )
      {
        sprintf(
          item__getname_buffer,
          "%s %s",
		  pItemsTable->pSpecialEnchantments[v7].pBonusStatement,
          pItemsTable->pItems[v1->uItemID].pName);
        return v4;
      }
      strcat(item__getname_buffer, " ");
	  v9 = pItemsTable->pSpecialEnchantments[v1->uAdditionalValue].pBonusStatement;
    }
    strcat(item__getname_buffer, v9);
  }
  return v4;
}


//----- (00456620) --------------------------------------------------------
void ItemsTable::GenerateItem(int treasure_level, int a3, ItemGen *out_item)
{
  
ItemGen *v4; // esi@1
  ItemsTable *v5; // edi@1
  int v6; // ebx@3
  int *v7; // ecx@33
  //int v8; // eax@34
  //int v9; // eax@39
  int v10; // ebx@43
  int v11; // ecx@47
  unsigned int *v12; // edx@48
  unsigned int v13; // eax@49
  signed int v14; // ebx@52
  int v15; // eax@53
  signed int v16; // eax@55
  int v17; // ebx@57
  int v18; // edx@62
  signed int v19; // ebx@70
  unsigned __int8 v20; // al@81
  int v21; // eax@84
  int v22; // ebx@85
  int v23; // eax@86
  int v24; // ebx@86
  int v25; // edx@86
  int v26; // edx@89
  unsigned int v27; // eax@89
  int i; // ebx@89
  unsigned int v29; // ecx@90
  int v30; // ebx@91
  int v31; // eax@91
  int v32; // ecx@91
  int v33; // eax@91
  int v34; // eax@97
  unsigned __int8 v35; // sf@97
  unsigned __int8 v36; // of@97
  int v37; // ebx@98
  int v38; // edx@99
  signed int v39; // ebx@101
  int v40; // ecx@102
  char v41; // zf@107
  char v42; // al@108
  char v43; // al@111
  int *v44; // edx@118
  int v45; // eax@120
  int v46; // edx@120
  int *j; // eax@121
  unsigned int v48; // ecx@123
  int v49; // eax@123
  int v50; // eax@123
  int Dst[800]; // [sp+Ch] [bp-C88h]@33
  int v52; // [sp+C8Ch] [bp-8h]@33
  int v53; // [sp+C90h] [bp-4h]@1
  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
  int *v58; // [sp+CA0h] [bp+Ch]@102
  int v59; // [sp+CA0h] [bp+Ch]@123
  //signed int a2a; // [sp+CA4h] [bp+10h]@33
  int a2b; // [sp+CA4h] [bp+10h]@101
  int a2c; // [sp+CA4h] [bp+10h]@120

  v53 = -1;
  v5 = this;
  if (!out_item)
    out_item = (ItemGen *)pAllocator->AllocNamedChunk(out_item, sizeof(*out_item), "newItemGen");
  memset(out_item, 0, sizeof(*out_item));

  v4 = out_item;
  v6 = treasure_level - 1;
  //v54 = treasure_level - 1;
  if ( a3 )
  {
    ITEM_EQUIP_TYPE   requested_equip;
    PLAYER_SKILL_TYPE requested_skill = PLAYER_SKILL_INVALID;
    switch (a3)
     {
      case 20: requested_equip = EQUIP_ONE_OR_TWO_HANDS; break;
      case 21: requested_equip = EQUIP_ARMOUR; break;
      case 22: requested_skill = (PLAYER_SKILL_TYPE)38; __debugbreak();/*check this skill*/ break;
      case 23: requested_skill = PLAYER_SKILL_SWORD; break;
      case 24: requested_skill = PLAYER_SKILL_DAGGER; break;
      case 25: requested_skill = PLAYER_SKILL_AXE; break;
      case 26: requested_skill = PLAYER_SKILL_SPEAR; break;
      case 27: requested_skill = PLAYER_SKILL_BOW; break;
      case 28: requested_skill = PLAYER_SKILL_MACE; break;
      case 29: requested_skill = (PLAYER_SKILL_TYPE)37; __debugbreak();/*check this skill*/break;
      case 30: requested_skill = PLAYER_SKILL_STAFF; break;
      case 31: requested_skill = PLAYER_SKILL_LEATHER; break;
      case 32: requested_skill = PLAYER_SKILL_CHAIN; break;
      case 33: requested_skill = PLAYER_SKILL_PLATE; break;
      case 34: requested_equip = EQUIP_SHIELD; break;
      case 35: requested_equip = EQUIP_HELMET; break;
      case 36: requested_equip = EQUIP_BELT; break;
      case 37: requested_equip = EQUIP_CLOAK; break;
      case 38: requested_equip = EQUIP_GAUNTLETS; break;
      case 39: requested_equip = EQUIP_BOOTS; break;
      case 40: requested_equip = EQUIP_RING; break;
      case 41: requested_equip = EQUIP_AMULET; break;
      case 42: requested_equip = EQUIP_WAND; break;
      case 43: requested_equip = EQUIP_F; break;
      case 44: requested_equip = EQUIP_POTION; break;
      case 45: requested_equip = EQUIP_REAGENT; break;
      case 46: requested_equip = EQUIP_GEM; break;
      default:
        __debugbreak(); // check this condition
        requested_equip = (ITEM_EQUIP_TYPE)(a3 - 1);
      break;
    }
    memset(Dst, 0, sizeof(Dst));
    v52 = 0;
    v7 = Dst;
    //a2a = 1;
    if (requested_skill == PLAYER_SKILL_INVALID)
    {
      for (uint i = 1; i < 500; ++i)
      {
        if (pItems[i].uEquipType == requested_equip)
        {
          *v7++ = i;
          v52 += pItems[i].uChanceByTreasureLvl[v6];
        }
      }
    }
    else
    {
      for (uint i = 1; i < 500; ++i)
      {
        if (pItems[i].uSkillType == requested_skill)
        {
          *v7++ = i;
          v52 += pItems[i].uChanceByTreasureLvl[v6];
        }
      }
    }

    v10 = 0;
    if ( v52 )
      v10 = rand() % v52;

    v4->uItemID = *Dst;
    if (!v4->uItemID)
      v4->uItemID = 1;

    v11 = pItems[v4->uItemID].uChanceByTreasureLvl[v6];
    if ( v11 < v10 )
    {
      v12 = (uint *)Dst;
      do
      {
        ++v12;
        v13 = *v12;
        v4->uItemID = *v12;
        v11 += pItems[v13].uChanceByTreasureLvl[v6];
      }
      while ( v11 < v10 );
    }
    if (pItems[v4->uItemID].uEquipType == EQUIP_POTION && v4->uItemID != ITEM_POTION_BOTTLE )
    {
      v4->_bonus_type = 0;
      v14 = 2;
      do
      {
        v4->_bonus_type += rand() % 4 + 1;
        v15 = v4->_bonus_type;
        --v14;
      }
      while ( v14 );
      v4->_bonus_type = v15 * treasure_level;
    }
  }
  else
  {
    v16 = 0;
    v56 = 0;
    do
      v56 += pParty->field_7BA[v16++];
    while ( v16 < 29 );
    v17 = rand() % 29;
    if ( v6 == 5 && rand() % 100 < 5 && !pParty->field_7BA[v17] && v56 < 13 )
    {
      pParty->field_7BA[v17] = 1;
      v4->uAttributes = 0;
      v4->uItemID = v17 + 500;
      SetSpecialBonus(v4);
      return;
    }
    v57 = 0;
    v18 = rand() % v5->uChanceByTreasureLvlSumm[v54];// v5->field_11684[v54];
    v4->uItemID = 0;
    if ( v18 > 0 )
    {
      do
        v57 += pItems[v4->uItemID++ + 1].uChanceByTreasureLvl[v6];
      while ( v57 < v18 );
    }
    if ( !v18 )
      v4->uItemID = 1;
    if ( !v4->uItemID )
      v4->uItemID = 1;
    if (pItems[v4->uItemID].uEquipType == EQUIP_POTION && v4->uItemID != ITEM_POTION_BOTTLE)
    {
      v4->_bonus_type = 0;
      v19 = 2;
      do
      {
        v4->_bonus_type += rand() % 4 + 1;
        v15 = v4->_bonus_type;
        --v19;
      }
      while ( v19 );
      v4->_bonus_type = v15 * treasure_level;
    }
  }
  if ( v4->uItemID == ITEM_SPELLBOOK_LIGHT_DIVINE_INTERVENTION
    && !(unsigned __int16)_449B57_test_bit(pParty->_award_bits, 239) )
    v4->uItemID = ITEM_SPELLBOOK_LIGHT_SUN_BURST;
  if ( pItemsTable->pItems[v4->uItemID + 1].uItemID_Rep_St )
    v4->uAttributes = 0;
  else
    v4->uAttributes = 1;
  if ( pItems[v4->uItemID].uEquipType != EQUIP_POTION )
  {
    v4->uAdditionalValue = 0;
    v4->_bonus_type = 0;
  }
  v20 = pItems[v4->uItemID].uEquipType;
  if ( v20 <= EQUIP_BOW )
  {
    v37 = v5->uBonusChanceWpSpecial[v54];
    if ( !*(unsigned int *)v37 )
      return;
    v38 = rand() % 100;
    v36 = __OFSUB__(v38, *(unsigned int *)v37);
    v35 = v38 - *(unsigned int *)v37 < 0;
  }
  else
  {
    if ( v20 > EQUIP_AMULET )
    {
      if ( v20 == EQUIP_WAND )
      {
        v21 = rand() % 6 + pItemsTable->pItems[v4->uItemID].uDamageMod + 1;
        v4->uNumCharges = v21;
        v4->uMaxCharges = v21;
      }
      return;
    }
    v22 = (int)&uBonusChanceStandart[v6];
    if ( !*(unsigned int *)v22 )
      return;
    v23 = rand();
    v24 = *(unsigned int *)v22;
    v25 = v23 % 100;
    if ( !uBonusChanceSpecial[v6] && !(v23 % 100 < v24))
        return;
    if ( v25 < v24 || v23 % 100 < v24)
    {
      v26 = rand() % 10;//v5->field_116D8[v5->pItems[v4->uItemID].uEquipType];
      v27 = v4->uItemID;
      v4->_bonus_type = 0;
      for ( i = pEnchantments[0].to_item[pItems[v27].uEquipType + 1];
            i < v26;
            i += pEnchantments[v4->_bonus_type].to_item[pItems[v29].uEquipType + 1] )
      {
        v29 = v4->uItemID;
        ++v4->_bonus_type;
      }
      ++v4->_bonus_type;
      v30 = 10;// v5->field_116D8[2 * v54 + 12];
      v31 = rand();
      v32 = v4->_bonus_type - 1;
      v33 = v31 % 10;//(v5->field_116D8[2 * v54 + 13] - v30 + 1) + v30;
      v4->_bonus_strength = v33;
      if ( v32 == 21 || v32 == 22 || v32 == 23 )
        v4->_bonus_strength = v33 >> 1;
      if ( v4->_bonus_strength <= 0 )
        v4->_bonus_strength = 1;
      return;
    }
    v34 = v24 + 10;//*(unsigned int *)&v5->field_1169C[4 * v54 + 24];
    v36 = __OFSUB__(v25, v34);
    v35 = v25 - v34 < 0;
  }
  if ( !(v35 ^ v36) )
    return;
  memset(&Dst, 0, 0xC80u);
  v39 = 0;
  a2b = 0;
  if ( pSpecialEnchantments_count > 0 )
  {
    v52 = -16 - (uint)this;
    v58 = (int *)&Dst;
    v40 = (int)&pSpecialEnchantments[1];
    do
    {
      switch ( v6 )
      {
        case 2:
          if ( !*(unsigned char *)v40 )
          {
            v39 += *((unsigned char *)&uAllItemsCount + v40 + v52 + pItems[v4->uItemID].uEquipType);
            if ( *((unsigned char *)&uAllItemsCount + v40 + v52 + pItems[v4->uItemID].uEquipType) )
            {
              v44 = v58;
              ++v58;
              *v44 = a2b;
            }
            ++a2b;
			v40 += 28;
			continue;
          }
          v41 = *(unsigned char *)v40 == 1;
          break;
        case 3:
          v43 = *(unsigned char *)v40;
          if ( !*(unsigned char *)v40 || v43 == 1 )
          {
            v39 += *((unsigned char *)&uAllItemsCount + v40 + v52 + pItems[v4->uItemID].uEquipType);
            if ( *((unsigned char *)&uAllItemsCount + v40 + v52 + pItems[v4->uItemID].uEquipType) )
            {
              v44 = v58;
              ++v58;
              *v44 = a2b;
            }
            ++a2b;
			v40 += 28;
			continue;
          }
          v41 = v43 == 2;
          break;
        case 4:
          v42 = *(unsigned char *)v40;
          if ( *(unsigned char *)v40 == 1 || v42 == 2 )
          {
            v39 += *((unsigned char *)&uAllItemsCount + v40 + v52 + pItems[v4->uItemID].uEquipType);
            if ( *((unsigned char *)&uAllItemsCount + v40 + v52 + pItems[v4->uItemID].uEquipType) )
            {
              v44 = v58;
              ++v58;
              *v44 = a2b;
            }
            ++a2b;
			v40 += 28;
			continue;
          }
          v41 = v42 == 3;
          break;
        default:
          if ( v6 != 5 )
		  {
            ++a2b;
			v40 += 28;
			continue;
		  }
          v41 = *(unsigned char *)v40 == 3;
          break;
      }
      if ( v41 )
      {
		v39 += *((unsigned char *)&uAllItemsCount + v40 + v52 + pItems[v4->uItemID].uEquipType);
		if ( *((unsigned char *)&uAllItemsCount + v40 + v52 + pItems[v4->uItemID].uEquipType) )
		{
			v44 = v58;
			++v58;
			*v44 = a2b;
		}
	  }
LABEL_119:
      ++a2b;
      v40 += 28;
    }
    while ( a2b < pSpecialEnchantments_count );
  }
  v45 = rand();
  v4->uAdditionalValue = *(uint *)Dst;
  v46 = v45 % v39 + 1;
  a2c = *((unsigned char *)&uAllItemsCount + 28 * (*(uint *)Dst + 1389) + pItems[v4->uItemID].uEquipType);
  if ( a2c < v46 )
  {
    for ( j = (int *)&Dst; ; j = (int *)v59 )
    {
      v48 = v4->uItemID;
      v49 = (int)(j + 1);
      v59 = v49;
      v50 = *(unsigned int *)v49;
      v4->uAdditionalValue = v50;
      a2c += *((unsigned char *)&uAllItemsCount + 28 * (v50 + 1389) + pItems[v48].uEquipType);
      if ( a2c >= v46 )
        break;
    }
  }
  ++v4->uAdditionalValue;
}

//----- (004505CC) --------------------------------------------------------
bool ItemGen::GenerateArtifact()
{
  ItemGen *v1; // edi@1
  signed int uNumArtifactsNotFound; // esi@1
  int v3; // eax@1
  bool result; // eax@6
  int v5[32]; // [sp+8h] [bp-A0h]@3
  char Dst[32]; // [sp+88h] [bp-20h]@1

  auto a2 = this;
  v1 = a2;
  memset(Dst, 0, 0x20u);
  uNumArtifactsNotFound = 0;
  v3 = 500;
  do
  {
    if ( !pParty->field_3C.pIsArtifactFound[v3] )
      v5[uNumArtifactsNotFound++] = v3;
    ++v3;
  }
  while ( v3 < 529 );
  v1->Reset();
  if ( uNumArtifactsNotFound )
  {
    v1->uItemID = v5[rand() % uNumArtifactsNotFound];
    pItemsTable->SetSpecialBonus(v1);
    result = 1;
  }
  else
  {
    result = 0;
  }
  return result;
}
// 4505CC: using guessed type int var_A0[32];