diff Chest.cpp @ 576:1bd8758f50b8

Слияние
author Ritor1
date Mon, 04 Mar 2013 09:22:38 +0600
parents ec3546510fa1
children 96f48cfdd657 653724e9c234
line wrap: on
line diff
--- a/Chest.cpp	Mon Mar 04 09:22:29 2013 +0600
+++ b/Chest.cpp	Mon Mar 04 09:22:38 2013 +0600
@@ -29,12 +29,15 @@
 Chest pChests[20];
 
 
-
+const int pChestPixelOffsetX[8]  = {42, 18, 18, 42, 42, 42, 18, 42};
+const int pChestPixelOffsetY[8]  = {34, 30, 30, 34, 34, 34, 30, 34};
+const int pChestWidthsByType[8]  = {9, 9, 9, 9, 9, 9, 9, 9};
+const int pChestHeightsByType[8] = {9, 9, 9, 9, 9, 9, 9, 9};
 
 
 //----- (0042041E) --------------------------------------------------------
-bool Chest::Open(unsigned int uChestID)
-{
+bool Chest::Open( signed int uChestID )
+    {
   //char *v1; // edi@5
   unsigned int v2; // eax@8
   GUIWindow *v3; // eax@15
@@ -61,7 +64,7 @@
   signed int v25; // ecx@28
   char *v26; // edx@29
   unsigned __int16 v27; // ax@32
-  //LayingItem a1; // [sp+14h] [bp-B0h]@28
+  //SpriteObject a1; // [sp+14h] [bp-B0h]@28
   int v29; // [sp+84h] [bp-40h]@16
   int v30; // [sp+88h] [bp-3Ch]@16
   int v31; // [sp+8Ch] [bp-38h]@16
@@ -79,7 +82,9 @@
 
   //a4 = uChestID;
   assert(uChestID < 20);
-  auto chest = pChests + uChestID;
+  if ((uChestID <0)&&(uChestID >=20) )
+      return false;
+  auto chest = &pChests[uChestID];
 
   ++pIcons_LOD->uTexturePacksCount;
   if (!pIcons_LOD->uNumPrevLoadedFiles)
@@ -89,7 +94,7 @@
   //v1 = (char *)&pChests[uChestID].uFlags;
   //v34 = v1;
   if (!chest->Initialized())
-    Chest::_420284(uChestID);
+    Chest::PlaceItems(uChestID);
 
   if ( !uActiveCharacter )
     return 0;
@@ -169,8 +174,8 @@
     v23 = (unsigned int *)(&v29 + v35);
     sub_42F7EB_DropItemAt(*v23, pOut.x, pOut.z, pOut.y, 0, 1, 0, 0x30u, 0);
     
-    LayingItem a1; // [sp+14h] [bp-B0h]@28
-    //LayingItem::LayingItem(&a1);
+    SpriteObject a1; // [sp+14h] [bp-B0h]@28
+    //SpriteObject::SpriteObject(&a1);
 
     a1.stru_24.Reset();
     v24 = *(short *)v23;
@@ -179,7 +184,7 @@
     a1.field_4C = 0;
     a1.field_48 = 0;
     a1.field_54 = 0;
-    a1.uItemType = v24;
+    a1.uType = v24;
     if ( (signed int)pObjectList->uNumObjects <= 0 )
     {
 LABEL_32:
@@ -233,22 +238,11 @@
       pPlayers[uActiveCharacter]->PlaySound(SPEECH_4, 0);
   }
   dword_507CD8 = 0;
-  v3 = GUIWindow::Create(0, 0, 640u, 480u, WINDOW_Chest, a4, 0);
-  pGUIWindow_CurrentMenu = v3;
-  pChestWindow = v3;
-  pBtn_ExitCancel = v3->CreateButton(
-                 0x1D7u,
-                 0x1BDu,
-                 0xA9u,
-                 0x23u,
-                 1,
-                 0,
-                 0x71u,
-                 0,
-                 0,
-                 pGlobalTXT_LocalizationStrings[79],// Exit
-                 (Texture *)(uTextureID_506438 != -1 ? (int)&pIcons_LOD->pTextures[uTextureID_506438] : 0),
-                 0);
+
+  pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640u, 480u, WINDOW_Chest, uChestID, 0);
+  pChestWindow = pGUIWindow_CurrentMenu;
+  pBtn_ExitCancel = pChestWindow->CreateButton(0x1D7u, 0x1BDu, 0xA9u, 0x23u, 1,  0,  0x71u,  0, 0,  pGlobalTXT_LocalizationStrings[79],// Exit
+                 (Texture *)(uExitCancelTextureId != -1 ? (int)&pIcons_LOD->pTextures[uExitCancelTextureId] : 0),   0);
   pChestWindow->CreateButton(7u, 8u, 460u, 343u, 1, 0, 0xCu, 0, 0, "", 0);
   pCurrentScreen = SCREEN_CHEST;
   pEventTimer->Pause();
@@ -257,188 +251,165 @@
 
 //----- (0042092D) --------------------------------------------------------
 void Chest::DrawChestUI(unsigned int uChestID)
-{
-  Render *v1; // edi@1
-  unsigned int v2; // ebx@1
-  unsigned int v3; // ebx@1
-  int v4; // eax@1
-  unsigned int v5; // eax@1
-  int v6; // ecx@3
-  unsigned int v7; // eax@4
-  Texture *v8; // esi@4
-  signed int v9; // ecx@4
-  signed int v10; // edx@4
-  signed int v11; // eax@4
-  int v12; // eax@6
-  int v13; // eax@6
-  unsigned int v14; // ST34_4@8
-  int v15; // edi@8
-  int *v16; // [sp+Ch] [bp-28h]@1
-  int v17; // [sp+10h] [bp-24h]@4
-  int v18; // [sp+14h] [bp-20h]@1
-  signed int v19; // [sp+18h] [bp-1Ch]@1
-  int v20; // [sp+1Ch] [bp-18h]@1
-  signed int v21; // [sp+20h] [bp-14h]@1
-  char *v22; // [sp+2Ch] [bp-8h]@2
-  signed int v23; // [sp+30h] [bp-4h]@1
+    {
+
+    int chestBitmapId; // eax@1
+    unsigned int v5; // eax@1
+    int chest_item_index; // ecx@3
+    unsigned int item_texture_id; // eax@4
+    Texture *item_texture; // esi@4
+    signed int itemPixelWidth; // ecx@4
+    signed int itemPixelHeght; // edx@4
+    signed int v11; // eax@4
+    int v12; // eax@6
+    int v13; // eax@6
+    unsigned int itemPixelPosX; // ST34_4@8
+    int itemPixelPosY; // edi@8
+    int *v16; // [sp+Ch] [bp-28h]@1
+    int v17; // [sp+10h] [bp-24h]@4
+    int chest_offs_y; // [sp+14h] [bp-20h]@1
+    signed int chestHeghtCells; // [sp+18h] [bp-1Ch]@1
+    int chest_offs_x; // [sp+1Ch] [bp-18h]@1
+    signed int chestWidthCells; // [sp+20h] [bp-14h]@1
+    signed int item_counter; // [sp+30h] [bp-4h]@1
 
-  v1 = pRenderer;
-  v2 = uChestID;
-  v16 = pRenderer->pActiveZBuffer;
-  pRenderer->ClearZBuffer(0, 479);
-  v3 = v2;
-  v4 = pChests[v3].uChestBitmapID;
-  v20 = pChestSmthn1ByType[v4];
-  v18 = pChestSmthn2ByType[v4];
-  v21 = pChestWidthsByType[v4];
-  v19 = pChestHeightsByType[v4];
-  sprintf(pTmpBuf, "chest%02d", pChestList->pChests[pChests[v3].uChestBitmapID].uTextureID);
-  v5 = pIcons_LOD->LoadTexture(pTmpBuf, TEXTURE_16BIT_PALETTE);
-  pRenderer->DrawTextureIndexed(8u, 8u, (Texture *)(v5 != -1 ? (int)&pIcons_LOD->pTextures[v5] : 0));
-  v23 = 0;
-  if ( v21 * v19 > 0 )
-  {
-    v22 = (char *)pChests[v3].pInventoryIndices;
-    do
-    {
-      v6 = *(short *)v22;
-      if ( v6 > 0 )
-      {
-        v17 = v18 + 32 * v23 / v19;
-        v7 = pIcons_LOD->LoadTexture(
-               pItemsTable->pItems[*(int *)((char *)&pOtherOverlayList->pOverlays[49].field_4 + 36 * v6 + v3 * 5324)].pIconName,
-               TEXTURE_16BIT_PALETTE);
-        v8 = (Texture *)(v7 != -1 ? &pIcons_LOD->pTextures[v7] : 0);
-        v9 = (v7 != -1 ? pIcons_LOD->pTextures[v7].uTextureWidth : 24);
-        v10 = (v7 != -1 ? pIcons_LOD->pTextures[v7].uTextureHeight : 26);
-        v11 = (v7 != -1 ? pIcons_LOD->pTextures[v7].uTextureWidth : 24);
-        if ( v9 < 14 )
-          v11 = 14;
-        v12 = v11 - 14;
-        LOBYTE(v12) = v12 & 0xE0;
-        v13 = v12 + 32;
-        if ( (signed int)v8->uTextureHeight < 14 )
-          v10 = 14;
-        v14 = v20 + 32 * v23 % v21 + ((v13 - v9) >> 1);
-        v15 = v17 + ((((v10 - 14) & 0xFFFFFFE0) - v8->uTextureHeight + 32) >> 1);
-        pRenderer->DrawTextureTransparent(
-          v14,
-          v17 + ((((v10 - 14) & 0xFFFFFFE0) - v8->uTextureHeight + 32) >> 1),
-          v8);
-        sub_40F92A(&v16[v14 + pSRZBufferLineOffsets[v15]], v8, v23 + 1);
-        v1 = pRenderer;
-      }
-      ++v23;
-      v22 += 2;
+    v16 = pRenderer->pActiveZBuffer;
+    pRenderer->ClearZBuffer(0, 479);
+    chestBitmapId = pChests[uChestID].uChestBitmapID;
+    chest_offs_x = pChestPixelOffsetX[chestBitmapId];
+    chest_offs_y = pChestPixelOffsetY[chestBitmapId];
+    chestWidthCells = pChestWidthsByType[chestBitmapId];
+    chestHeghtCells = pChestHeightsByType[chestBitmapId];
+    sprintf(pTmpBuf, "chest%02d", pChestList->pChests[chestBitmapId].uTextureID);
+    v5 = pIcons_LOD->LoadTexture(pTmpBuf, TEXTURE_16BIT_PALETTE);
+    pRenderer->DrawTextureIndexed(8u, 8u, (Texture *)(v5 != -1 ? (int)&pIcons_LOD->pTextures[v5] : 0));
+
+    for (item_counter = 0; item_counter< chestWidthCells * chestHeghtCells; ++item_counter)
+        {
+        chest_item_index = pChests[uChestID].pInventoryIndices[item_counter];
+        if ( chest_item_index > 0 )
+            {
+            item_texture_id = pIcons_LOD->LoadTexture(
+                //pItemsTable->pItems[*(int *)((char *)&pOtherOverlayList->pOverlays[49].field_4 + 36 * v6 + v3 * 5324)].pIconName,
+                pItemsTable->pItems[pChests[uChestID].igChestItems[chest_item_index-1].uItemID].pIconName, TEXTURE_16BIT_PALETTE);
+            item_texture = (Texture *)(item_texture_id != -1 ? &pIcons_LOD->pTextures[item_texture_id] : 0);
+            itemPixelWidth = (item_texture_id != -1 ? pIcons_LOD->pTextures[item_texture_id].uTextureWidth : 24);
+            itemPixelHeght = (item_texture_id != -1 ? pIcons_LOD->pTextures[item_texture_id].uTextureHeight : 26);
+            if ( itemPixelWidth < 14 )
+                itemPixelWidth = 14;
+            v12 = itemPixelWidth - 14;
+            v12 = v12 & 0xFFFFFFE0;
+            v13 = v12 + 32;
+            if ( itemPixelHeght < 14 )
+                itemPixelHeght = 14;
+            itemPixelPosX = chest_offs_x + 32 * (item_counter % chestWidthCells) + ((v13 - itemPixelWidth) >> 1);
+            itemPixelPosY = chest_offs_y + 32 * (item_counter / chestHeghtCells) +
+                ((((itemPixelHeght - 14) & 0xFFFFFFE0) - item_texture->uTextureHeight + 32) >> 1);
+            pRenderer->DrawTextureTransparent(  itemPixelPosX,   itemPixelPosY,  item_texture);
+            sub_40F92A(&v16[itemPixelPosX + pSRZBufferLineOffsets[itemPixelPosY]], item_texture, item_counter + 1);
+            }
+        }
+    pRenderer->DrawTextureIndexed(pBtn_ExitCancel->uX, pBtn_ExitCancel->uY,
+        (Texture *)(uExitCancelTextureId != -1 ? (int)&pIcons_LOD->pTextures[uExitCancelTextureId] : 0));
     }
-    while ( v23 < v21 * v19 );
-  }
-  pRenderer->DrawTextureIndexed(pBtn_ExitCancel->uX, pBtn_ExitCancel->uY,
-    (Texture *)(uTextureID_506438 != -1 ? (int)&pIcons_LOD->pTextures[uTextureID_506438] : 0));
-}
 
 
 //----- (0041FE71) --------------------------------------------------------
-int Chest::CanPlaceItemAt(signed int a1, int a2, unsigned int uChestID)
-{
-  int v3; // eax@1
-  unsigned int v4; // eax@1
-  Texture *v5; // ecx@1
-  signed int v6; // eax@1
-  signed int v7; // edi@3
-  signed int v8; // eax@3
-  int v9; // edi@3
-  int v10; // ebx@5
-  int v11; // esi@9
-  int v12; // edx@10
-  int v13; // ecx@11
-  char *v14; // eax@12
-  int v16; // [sp+Ch] [bp-Ch]@1
-  signed int v17; // [sp+10h] [bp-8h]@1
-  signed int v18; // [sp+14h] [bp-4h]@1
+bool Chest::CanPlaceItemAt( signed int test_cell_position, int item_id, unsigned int uChestID )
+    {
+    int v3; // eax@1
+    unsigned int item_texture_id; // eax@1
+    Texture *item_texture; // ecx@1
+    signed int v6; // eax@1
+    signed int v7; // edi@3
+    signed int v8; // eax@3
+    int texture_cell_width; // edi@3
+    int texture_cell_height; // ebx@5
+    int _row; // esi@9
+    int _cell_rows; // edx@10
+    int _column; // ecx@11
+    char *v14; // eax@12
+    int chest_cell_heght; // [sp+Ch] [bp-Ch]@1
+    signed int v17; // [sp+10h] [bp-8h]@1
+    signed int chest_cell_width; // [sp+14h] [bp-4h]@1
 
-  v17 = a1;
-  v3 = pChests[uChestID].uChestBitmapID;
-  v16 = pChestHeightsByType[v3];
-  v18 = pChestWidthsByType[v3];
-  v4 = pIcons_LOD->LoadTexture(pItemsTable->pItems[a2].pIconName, TEXTURE_16BIT_PALETTE);
-  v5 = (Texture *)(v4 != -1 ? (int)&pIcons_LOD->pTextures[v4] : 0);
-  v6 = (v4 != -1 ? pIcons_LOD->pTextures[v4].uTextureWidth : 24);
-  if ( v6 < 14 )
-    v6 = 14;
-  v7 = v6 - 14;
-  v8 = v5->uTextureHeight;
-  v9 = (v7 >> 5) + 1;
-  if ( v8 < 14 )
-    v8 = 14;
-  v10 = ((v8 - 14) >> 5) + 1;
-  if ( !areWeLoadingTexture )
-  {
-    v5->Release();
-    pIcons_LOD->_40F9C5();
-  }
-  if ( v9 + v17 % v18 <= v18 && v10 + v17 / v18 <= v16 )
-  {
-    v11 = 0;
-    if ( v10 <= 0 )
-      return 1;
-    v12 = 0;
-    while ( 1 )
-    {
-      v13 = 0;
-      if ( v9 > 0 )
-        break;
-LABEL_15:
-      v12 += v18;
-      ++v11;
-      if ( v11 >= v10 )
-        return 1;
+    chest_cell_heght = pChestHeightsByType[pChests[uChestID].uChestBitmapID];
+    chest_cell_width = pChestWidthsByType[pChests[uChestID].uChestBitmapID];
+    item_texture_id = pIcons_LOD->LoadTexture(pItemsTable->pItems[item_id].pIconName, TEXTURE_16BIT_PALETTE);
+    item_texture = (Texture *)(item_texture_id != -1 ? (int)&pIcons_LOD->pTextures[item_texture_id] : 0);
+    v6 = (item_texture_id != -1 ? pIcons_LOD->pTextures[item_texture_id].uTextureWidth : 24);
+    if ( v6 < 14 )
+        v6 = 14;
+    texture_cell_width = (v6 - 14 >> 5) + 1;
+    v8 = item_texture->uTextureHeight;
+    if ( v8 < 14 )
+        v8 = 14;
+    texture_cell_height = ((v8 - 14) >> 5) + 1;
+    if ( !areWeLoadingTexture )
+        {
+        item_texture->Release();
+        pIcons_LOD->_40F9C5();
+        }
+    if ( (texture_cell_width + test_cell_position % chest_cell_width <= chest_cell_width) && 
+        (texture_cell_height + test_cell_position / chest_cell_width <= chest_cell_heght) )
+        { //we not put over borders
+        _row = 0;
+        if ( texture_cell_height <= 0 )
+            return true;
+        _cell_rows = 0;
+        while ( 1 )
+            {
+            _column = 0;
+            if ( texture_cell_width > 0 )
+                {
+                while ( pChests[uChestID].pInventoryIndices[test_cell_position + _cell_rows+_column]==0)
+                    {
+                    ++_column;
+                    if ( _column >= texture_cell_width )
+                        break;
+                    }
+                if (pChests[uChestID].pInventoryIndices[test_cell_position + _cell_rows+_column]!=0)
+                    return false;
+                }
+            _cell_rows += chest_cell_width;
+            ++_row;
+            if ( _row >= texture_cell_height )
+                return true;
+            }
+
+        }
+    return false;
     }
-    v14 = (char *)&pChests[0].pInventoryIndices[v17 + v12 + 2662 * uChestID];
-    while ( !*(short *)v14 )
-    {
-      ++v13;
-      v14 += 2;
-      if ( v13 >= v9 )
-        goto LABEL_15;
-    }
-  }
-  return 0;
-}
 // 506128: using guessed type int areWeLoadingTexture;
 
 //----- (0041FF64) --------------------------------------------------------
-int Chest::_41FF64(unsigned int uChestID)
+int Chest::CountChestItems(unsigned int uChestID)
 {
-  unsigned int v1; // ecx@1
-  signed int result; // eax@1
-  int v3; // edx@1
-  ItemGen *pItem; // ecx@2
-
-  v1 = uChestID;
-  result = 0;
-  v3 = pChestWidthsByType[pChests[v1].uChestBitmapID] * pChestHeightsByType[pChests[v1].uChestBitmapID];
-  if ( v3 <= 0 )
+  signed int item_count; // eax@1
+  int max_items; // edx@1
+  item_count = 0;
+  max_items = pChestWidthsByType[pChests[uChestID].uChestBitmapID] * pChestHeightsByType[pChests[uChestID].uChestBitmapID];
+  if ( max_items <= 0 )
   {
-LABEL_5:
-    result = -1;
+    item_count = -1;
   }
   else
   {
-    pItem = pChests[v1].mm7__vector_pItems;
-    while ( pItem->uItemID )
+    while ( pChests[uChestID].igChestItems[item_count].uItemID )
     {
-      ++result;
-      ++pItem;
-      if ( result >= v3 )
-        goto LABEL_5;
+      ++item_count;
+      if ( item_count >= max_items )
+          {
+          item_count = -1;
+          break;
+          }
     }
   }
-  return result;
+  return item_count;
 }
 
 //----- (0041FFA2) --------------------------------------------------------
-int Chest::_41FFA2(int a1, ItemGen *a2, unsigned int uChestID)
+int Chest::PutItemInChest(int position, ItemGen *put_item, unsigned int uChestID)
 {
   int v3; // eax@1
   ItemGen *v4; // edi@1
@@ -455,7 +426,7 @@
   int v15; // edi@21
   int i; // ecx@21
   ItemGen *Src; // [sp+Ch] [bp-18h]@1
-  signed int v18; // [sp+10h] [bp-14h]@2
+  signed int item_in_chest_count; // [sp+10h] [bp-14h]@2
   int v19; // [sp+14h] [bp-10h]@1
   int v20; // [sp+18h] [bp-Ch]@19
   signed int v21; // [sp+1Ch] [bp-8h]@1
@@ -464,19 +435,19 @@
 
   v21 = 0;
   v3 = pChests[uChestID].uChestBitmapID;
-  v4 = a2;
+  v4 = put_item;
   v5 = pChestWidthsByType[v3] * pChestHeightsByType[v3];
-  Src = a2;
+  Src = put_item;
   v19 = pChestWidthsByType[v3];
-  if ( a1 == -1 )
+  if ( position == -1 )
   {
-    v18 = _41FF64(uChestID);
-    if ( v18 == -1 )
+    item_in_chest_count = CountChestItems(uChestID);
+    if ( item_in_chest_count == -1 )
       return 0;
     v22 = 0;
     if ( v5 > 0 )
     {
-      while ( !Chest::CanPlaceItemAt(v22, v4->uItemID, (unsigned int)pChestWindow->ptr_1C) )
+      while ( !Chest::CanPlaceItemAt(v22, v4->uItemID, pChestWindow->par1C) )
       {
         ++v22;
         if ( v22 >= v5 )
@@ -515,7 +486,7 @@
       {
         if ( (signed int)v12 > 0 )
         {
-          v14 = &pChests[0].pInventoryIndices[v21 + v23 + 2662 * uChestID];
+          v14 = &pChests[uChestID].pInventoryIndices[v21 + v23];
           LOWORD(v8) = -1 - v21;
           v8 <<= 16;
           LOWORD(v8) = -1 - v21;
@@ -532,8 +503,8 @@
       }
       while ( v20 );
     }
-    pChests[0].pInventoryIndices[v21 + 2662 * uChestID] = v18 + 1;
-    memcpy(&pChests[uChestID].mm7__vector_pItems[v18], Src, 0x24u);
+    pChests[uChestID].pInventoryIndices[v21] = item_in_chest_count + 1;
+    memcpy(&pChests[uChestID].igChestItems[item_in_chest_count], put_item, sizeof(ItemGen));
     result = v21 + 1;
   }
   else
@@ -545,159 +516,110 @@
 // 506128: using guessed type int areWeLoadingTexture;
 
 //----- (0042013E) --------------------------------------------------------
-unsigned int Chest::PlaceItemAt(unsigned int a1, unsigned int uItemIdx, unsigned int uChestID)
-{
-  int v3; // esi@1
-  unsigned int v4; // ebx@1
-  int uItemID; // edi@1
-  int v6; // edx@4
-  unsigned int v7; // eax@5
-  Texture *v8; // ecx@5
-  signed int v9; // eax@5
-  signed int v10; // edi@7
-  unsigned int v11; // ebx@7
-  int v12; // edi@9
-  int v13; // edx@12
-  void *v14; // edi@14
-  int v15; // edi@14
-  int i; // ecx@14
-  unsigned int result; // eax@18
-  __int16 v18; // [sp+Ch] [bp-10h]@1
-  int v19; // [sp+10h] [bp-Ch]@11
-  int v20; // [sp+14h] [bp-8h]@12
-  unsigned int v21; // [sp+18h] [bp-4h]@1
+void Chest::PlaceItemAt( unsigned int put_cell_pos, unsigned int item_at_cell, unsigned int uChestID )
+    {
+
+    int uItemID; // edi@1
+    int v6; // edx@4
+    unsigned int v7; // eax@5
+    Texture *v8; // ecx@5
+    signed int v9; // eax@5
+    signed int v10; // edi@7
+    unsigned int texture_cell_width; // ebx@7
+    int textute_cell_height; // edi@9
+    int chest_cell_row_pos; // edx@12
+    int chest_cell_width; // [sp+10h] [bp-Ch]@11
 
-  v3 = 5324 * uChestID;
-  v21 = a1;
-  v4 = 5324 * uChestID + 36 * uItemIdx;
-  v18 = uItemIdx;
-  uItemID = *(unsigned int *)((char *)&pChests[0].mm7__vector_pItems[0].uItemID + v4);
-  pItemsTable->SetSpecialBonus((ItemGen *)((char *)pChests[0].mm7__vector_pItems + v4));
-  if ( uItemID >= 135 && uItemID <= 159 && !*(int *)((char *)&pChests[0].mm7__vector_pItems[0].uNumCharges + v4) )
-  {
-    v6 = rand() % 21 + 10;
-    *(int *)((char *)&pChests[0].mm7__vector_pItems[0].uNumCharges + v4) = v6;
-    *(&pChests[0].mm7__vector_pItems[0].uMaxCharges + v4) = v6;
-  }
-  v7 = pIcons_LOD->LoadTexture(pItemsTable->pItems[uItemID].pIconName, TEXTURE_16BIT_PALETTE);
-  v8 = (Texture *)(v7 != -1 ? (int)&pIcons_LOD->pTextures[v7] : 0);
-  v9 = (v7 != -1 ? pIcons_LOD->pTextures[v7].uTextureWidth : 24);
-  if ( v9 < 14 )
-    v9 = 14;
-  v10 = v8->uTextureHeight;
-  v11 = ((v9 - 14) >> 5) + 1;
-  if ( v10 < 14 )
-    v10 = 14;
-  v12 = ((v10 - 14) >> 5) + 1;
-  if ( !areWeLoadingTexture )
-  {
-    v8->Release();
-    pIcons_LOD->_40F9C5();
-  }
-  v19 = pChestWidthsByType[*(unsigned __int16 *)((char *)&pChests[0].uChestBitmapID + v3)];
-  if ( v12 > 0 )
-  {
-    v13 = 0;
-    v20 = v12;
-    do
-    {
-      if ( (signed int)v11 > 0 )
-      {
-        v14 = &pChests[0].pInventoryIndices[v21 + v13 + 2662 * uChestID];
-        LOWORD(v3) = -1 - v21;
-        v3 <<= 16;
-        LOWORD(v3) = -1 - v21;
-        memset32(v14, v3, v11 >> 1);
-        v15 = (int)((char *)v14 + 4 * (v11 >> 1));
-        for ( i = v11 & 1; i; --i )
+    uItemID = pChests[ uChestID].igChestItems[item_at_cell].uItemID;
+    pItemsTable->SetSpecialBonus(&pChests[ uChestID].igChestItems[item_at_cell]);
+    if ( uItemID >= 135 && uItemID <= 159 && !pChests[ uChestID].igChestItems[item_at_cell].uNumCharges)
         {
-          *(short *)v15 = v3;
-          v15 += 2;
+        v6 = rand() % 21 + 10;
+        pChests[ uChestID].igChestItems[item_at_cell].uNumCharges = v6;
+        pChests[ uChestID].igChestItems[item_at_cell].uMaxCharges = v6;
         }
-      }
-      v13 += v19;
-      --v20;
+    v7 = pIcons_LOD->LoadTexture(pItemsTable->pItems[uItemID].pIconName, TEXTURE_16BIT_PALETTE);
+    v8 = (Texture *)(v7 != -1 ? (int)&pIcons_LOD->pTextures[v7] : 0);
+    v9 = (v7 != -1 ? pIcons_LOD->pTextures[v7].uTextureWidth : 24);
+    if ( v9 < 14 )
+        v9 = 14;
+    v10 = v8->uTextureHeight;
+    texture_cell_width = ((v9 - 14) >> 5) + 1;
+    if ( v10 < 14 )
+        v10 = 14;
+    textute_cell_height = ((v10 - 14) >> 5) + 1;
+    if ( !areWeLoadingTexture )
+        {
+        v8->Release();
+        pIcons_LOD->_40F9C5();
+        }
+    chest_cell_width = pChestWidthsByType[pChests[ uChestID].uChestBitmapID];
+    chest_cell_row_pos = 0;
+    for(int i=0; i<textute_cell_height; ++i) 
+        { 
+        for (int j=0; j<texture_cell_width; ++j)
+            {
+            pChests[uChestID].pInventoryIndices[put_cell_pos + chest_cell_row_pos+j]=(signed __int16)-(put_cell_pos+1);
+            } 
+        chest_cell_row_pos += chest_cell_width;
+        }
+    pChests[uChestID].pInventoryIndices[put_cell_pos] = item_at_cell + 1;
     }
-    while ( v20 );
-  }
-  result = v21 + 2662 * uChestID;
-  pChests[0].pInventoryIndices[result] = v18 + 1;
-  return result;
-}
 // 506128: using guessed type int areWeLoadingTexture;
 
 //----- (00420284) --------------------------------------------------------
-char *Chest::_420284(unsigned int uChestID)
-{
-  int v1; // ebx@1
-  unsigned int v2; // esi@1
-  unsigned int v3; // esi@1
-  int uChestArea; // edi@1
-  int v5; // eax@2
-  int v6; // ebx@11
-  char *result; // eax@18
-  char Dst[144]; // [sp+Ch] [bp-A0h]@1
-  int v9; // [sp+9Ch] [bp-10h]@10
-  unsigned int v10; // [sp+A0h] [bp-Ch]@1
-  unsigned int v11; // [sp+A4h] [bp-8h]@8
-  ItemGen *v12; // [sp+A8h] [bp-4h]@9
-
-  v1 = 0;
-  v2 = uChestID;
-  v10 = uChestID;
-  pRenderer->ClearZBuffer(0, 479);
-  v3 = v2;
-  uChestArea = pChestWidthsByType[pChests[v3].uChestBitmapID] * pChestHeightsByType[pChests[v3].uChestBitmapID];
-  memset(Dst, 0, 0x90u);
-  if ( uChestArea > 0 )
-  {
-    do
+void Chest::PlaceItems( unsigned int uChestID )
     {
-      do
-        v5 = (unsigned __int8)rand();
-      while ( v5 >= uChestArea );
-      while ( Dst[v5] )
-      {
-        ++v5;
-        if ( v5 == uChestArea )
-          v5 = 0;
-      }
-      Dst[v5] = v1++;
+    int uChestArea; // edi@1
+    int random_chest_pos; // eax@2
+    int test_position; // ebx@11
+    char chest_cells_map[144]; // [sp+Ch] [bp-A0h]@1
+    int chest_item_id; // [sp+9Ch] [bp-10h]@10
+    unsigned int items_counter; // [sp+A4h] [bp-8h]@8
+
+    pRenderer->ClearZBuffer(0, 479);
+    uChestArea = pChestWidthsByType[pChests[uChestID].uChestBitmapID] * pChestHeightsByType[pChests[uChestID].uChestBitmapID];
+    memset(chest_cells_map, 0, 144);
+    //fill cell map at random positions
+    for (items_counter = 0; items_counter<uChestArea; ++items_counter)
+        {
+        //get random position in chest
+        do
+        random_chest_pos = (unsigned __int8)rand();
+        while ( random_chest_pos >= uChestArea );
+        //if this pos occupied move to next
+        while ( chest_cells_map[random_chest_pos] )
+            {
+            ++random_chest_pos;
+            if ( random_chest_pos == uChestArea )
+                random_chest_pos = 0;
+            }
+        chest_cells_map[random_chest_pos] = items_counter;
+        }
+    items_counter = 0;
+
+    for (items_counter = 0; items_counter<uChestArea; ++items_counter)
+        {
+        chest_item_id = pChests[uChestID].igChestItems[items_counter].uItemID;
+        if ( chest_item_id )
+            {
+            test_position = 0;
+            while ( !Chest::CanPlaceItemAt((unsigned __int8)chest_cells_map[test_position], chest_item_id, uChestID) )
+                {
+                ++test_position;
+                if ( test_position >= uChestArea )
+                    break;
+                }
+            if(test_position<uChestArea)
+                {
+                Chest::PlaceItemAt((unsigned __int8)chest_cells_map[test_position], items_counter, uChestID);
+                if ( pChests[uChestID].uFlags & 4 )
+                    pChests[uChestID].igChestItems[items_counter].SetIdentified();
+                }
+            }
+        }
+    pChests[uChestID].SetInitialized(true);
     }
-    while ( v1 < uChestArea );
-  }
-  v11 = 0;
-  if ( uChestArea > 0 )
-  {
-	v12 = pChests[v3].mm7__vector_pItems;//&pChests[v3].mm7__vector_pItems[0].uAttributes;
-    do
-    {
-	  v9 = v12->uItemID;
-      if ( v9 )
-      {
-        v6 = 0;
-        while ( !Chest::CanPlaceItemAt((unsigned __int8)Dst[v6], v9, v10) )
-        {
-          ++v6;
-          if ( v6 >= uChestArea )
-            break;
-        }
-		if(v6<uChestArea)
-		{
-			Chest::PlaceItemAt((unsigned __int8)Dst[v6], v11, v10);
-			if ( pChests[v3].uFlags & 4 )
-				v12->SetIdentified();
-		}
-      }
-      ++v11;
-      ++v12;
-    }
-    while ( (signed int)v11 < uChestArea );
-  }
-  result = (char *)&pChests[v3].uFlags;
-  pChests[v3].uFlags = pChests[v3].uFlags & 0xFFFB | 2;
-  return result;
-}
 // 420284: using guessed type char Dst[144];
 
 //----- (00458B03) --------------------------------------------------------
@@ -801,3 +723,83 @@
   fclose(File);
   return 1;
 }
+
+//----- (00420B13) --------------------------------------------------------
+void __fastcall sub_420B13(int a1, int a2)
+    {
+    void *v2; // eax@1
+    ItemGen *v3; // ebx@1
+    unsigned int v4; // eax@1
+    Texture *v5; // ecx@1
+    signed int v6; // eax@1
+    signed int v7; // edi@3
+    signed int v8; // eax@3
+    int v9; // edi@3
+    int v10; // eax@5
+    int v11; // esi@8
+    unsigned int v12; // ecx@10
+    void *v13; // edi@10
+    unsigned __int8 v14; // cf@10
+    int v15; // edi@10
+    int i; // ecx@10
+    int v17; // [sp+Ch] [bp-14h]@1
+    int v18; // [sp+10h] [bp-10h]@3
+    int v19; // [sp+14h] [bp-Ch]@1
+    void *v20; // [sp+18h] [bp-8h]@1
+    int v21; // [sp+1Ch] [bp-4h]@5
+    int v22; // [sp+1Ch] [bp-4h]@8
+
+    v19 = a2;
+    v2 = pChestWindow->ptr_1C;
+    v20 = v2;
+    v2 = (void *)(5324 * (int)v2);
+    v3 = (ItemGen *)((char *)v2 + 36 * a1 + (int)((char *)pChests + 4));
+    v17 = pChestWidthsByType[*(short *)((char *)v2 + (int)pChests)];
+    v4 = pIcons_LOD->LoadTexture(pItemsTable->pItems[v3->uItemID].pIconName, TEXTURE_16BIT_PALETTE);
+    v5 = (Texture *)(v4 != -1 ? (int)&pIcons_LOD->pTextures[v4] : 0);
+    v6 = (v4 != -1 ? pIcons_LOD->pTextures[v4].uTextureWidth : 24);
+    if ( v6 < 14 )
+        v6 = 14;
+    v7 = v6 - 14;
+    v8 = v5->uTextureHeight;
+    v9 = (v7 >> 5) + 1;
+    v18 = v9;
+    if ( v8 < 14 )
+        v8 = 14;
+    v10 = ((v8 - 14) >> 5) + 1;
+    v21 = v10;
+    if ( !areWeLoadingTexture )
+        {
+        v5->Release();
+        pIcons_LOD->_40F9C5();
+        v10 = v21;
+        }
+    if ( v10 > 0 )
+        {
+        v11 = 0;
+        v22 = v10;
+        do
+            {
+            if ( v9 > 0 )
+                {
+                v12 = v9;
+                v13 = &pChests[0].pInventoryIndices[v19 + v11 + 2662 * (int)v20];
+                v14 = v12 & 1;
+                v12 >>= 1;
+                memset(v13, 0, 4 * v12);
+                v15 = (int)((char *)v13 + 4 * v12);
+                for ( i = v14; i; --i )
+                    {
+                    *(short *)v15 = 0;
+                    v15 += 2;
+                    }
+                v9 = v18;
+                }
+            v11 += v17;
+            --v22;
+            }
+            while ( v22 );
+        }
+    v3->Reset();
+    }
+// 506128: using guessed type int areWeLoadingTexture;