view Chest.cpp @ 848:ceef50611567

* AI fixed (temporary) * PID_ID, PID_TYPE replaces * >> 7 error in AudioPlayer
author zipi
date Sun, 31 Mar 2013 10:44:43 +0100
parents dfd683c4f538
children 0d96349d8c87
line wrap: on
line source

#include <stdio.h>
#include <assert.h>

#include "Chest.h"
#include "FrameTableInc.h"
#include "Allocator.h"
#include "LOD.h"
#include "MapInfo.h"
#include "Actor.h"
#include "Indoor.h"
#include "Outdoor.h"
#include "DecorationList.h"
#include "Party.h"
#include "AudioPlayer.h"
#include "Math.h"
#include "Texts.h"
#include "ObjectList.h"
#include "GUIWindow.h"
#include "Time.h"
#include "Overlays.h"

#include "mm7_data.h"
#include "MM7.h"



size_t uNumChests; // idb
struct ChestList *pChestList;
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( signed int uChestID )
{
  //char *v1; // edi@5
  unsigned int v2; // eax@8
  GUIWindow *v3; // eax@15
  int v5; // edx@16
  int v6; // eax@16
  ODMFace *v7; // eax@19
  int v8; // edx@19
  int v9; // edi@19
  signed int v10; // ebx@19
  int v11; // ecx@19
  int v12; // eax@19
  BLVFace *v13; // eax@20
  int v14; // ebx@21
  int v15; // edi@21
  int v16; // ecx@22
  __int64 v17; // qax@22
  double v18; // st7@23
  double v19; // st6@23
  double v20; // st7@23
  int v21; // ecx@26
  char v22[12]; // ST4C_12@28
  unsigned int *v23; // edi@28
  unsigned __int16 v24; // di@28
  signed int v25; // ecx@28
  char *v26; // edx@29
  unsigned __int16 v27; // ax@32
  //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
  int v32; // [sp+90h] [bp-34h]@16
  float v33; // [sp+94h] [bp-30h]@23
  //char *v34; // [sp+98h] [bp-2Ch]@5
  int v35; // [sp+9Ch] [bp-28h]@16
  Vec3_int_ pOut; // [sp+A0h] [bp-24h]@28
  int a4; // [sp+ACh] [bp-18h]@1
  int v38; // [sp+B0h] [bp-14h]@21
  int sRotX; // [sp+B4h] [bp-10h]@23
  float v40; // [sp+B8h] [bp-Ch]@23
  float v41; // [sp+BCh] [bp-8h]@23
  int sRotY; // [sp+C0h] [bp-4h]@8

  //a4 = uChestID;
  assert(uChestID < 20);
  if ((uChestID <0)&&(uChestID >=20) )
      return false;
  auto chest = &pChests[uChestID];

  ++pIcons_LOD->uTexturePacksCount;
  if (!pIcons_LOD->uNumPrevLoadedFiles)
    pIcons_LOD->uNumPrevLoadedFiles = pIcons_LOD->uNumLoadedFiles;


  //v1 = (char *)&pChests[uChestID].uFlags;
  //v34 = v1;
  if (!chest->Initialized())
    Chest::PlaceItems(uChestID);

  if ( !uActiveCharacter )
    return 0;
  *(float *)&sRotY = 0.0;
  v2 = pMapStats->GetMapInfo(pCurrentMapName);
  if ( !chest->Trapped() || !v2 )
    goto LABEL_12;
  if ( pPlayers[uActiveCharacter]->GetDisarmTrap() < 2 * pMapStats->pInfos[v2].LockX5 )
  {
    v29 = 811;
    v30 = 812;
    v31 = 813;
    v32 = 814;
    v5 = rand() % 4;
    v6 = PID_ID(EvtTargetObj);
    v35 = v5;
    if ( PID_TYPE(EvtTargetObj) == OBJECT_Decoration)
    {
      v16 = v6;
      v14 = pLevelDecorations[v6].vPosition.x;
      v38 = pLevelDecorations[v6].vPosition.y;
      v17 = pDecorationList->pDecorations[pLevelDecorations[v6].uDecorationDescID].uDecorationHeight;
      v15 = pLevelDecorations[v16].vPosition.z + (((signed int)v17 - HIDWORD(v17)) >> 1);
    }
    else
    {
      if ( PID_TYPE(EvtTargetObj) != OBJECT_BModel)
        goto LABEL_12;
      if ( uCurrentlyLoadedLevelType == LEVEL_Outdoor)
      {
        v7 = &pOutdoor->pBModels[EvtTargetObj >> 9].pFaces[(EvtTargetObj >> 3) & 0x3F];
        v8 = v7->pBoundingBox.y1;
        v9 = v7->pBoundingBox.z2;
        v10 = v7->pBoundingBox.x1 + v7->pBoundingBox.x2;
        v11 = v7->pBoundingBox.y2;
        v12 = v7->pBoundingBox.z1;
      }
      else
      {
        v13 = &pIndoor->pFaces[v6];
        v8 = v13->pBounding.y1;
        v9 = v13->pBounding.z2;
        v10 = v13->pBounding.x1 + v13->pBounding.x2;
        v11 = v13->pBounding.y2;
        v12 = v13->pBounding.z1;
      }
      v14 = v10 >> 1;
      v38 = (v8 + v11) >> 1;
      v15 = (v12 + v9) >> 1;
    }
    v18 = (double)pParty->vPosition.x - (double)v14;
    *(float *)&a4 = v18;
    v19 = (double)pParty->vPosition.y - (double)v38;
    v33 = v19;
    v41 = (double)pParty->sEyelevel + (double)pParty->vPosition.z - (double)v15;
    *(float *)&sRotY = v19 * v19;
    *(float *)&sRotX = v18 * v18;
    v20 = sqrt(v41 * v41 + *(float *)&sRotX + *(float *)&sRotY);
    v40 = v20;
    if ( v20 <= 1.0 )
    {
      *(float *)&sRotX = 0.0;
      *(float *)&sRotY = 0.0;
    }
    else
    {
      sRotY = (signed __int64)sqrt(*(float *)&sRotX + *(float *)&sRotY);
      sRotX = stru_5C6E00->Atan2((signed __int64)*(float *)&a4, (signed __int64)v33);
      sRotY = stru_5C6E00->Atan2(sRotY, (signed __int64)v41);
    }
    v21 = 256;
    if ( v40 < 256.0 )
      v21 = (signed __int64)v40 / 4;
    *(int *)&v22[8] = v15;
    *(_QWORD *)v22 = __PAIR__(v38, v14);
    Vec3_int_::Rotate(v21, sRotX, sRotY, *(Vec3_int_ *)v22, &pOut.x, &pOut.z, &pOut.y);
    v23 = (unsigned int *)(&v29 + v35);
    sub_42F7EB_DropItemAt(*v23, pOut.x, pOut.z, pOut.y, 0, 1, 0, 0x30u, 0);
    
    SpriteObject a1; // [sp+14h] [bp-B0h]@28
    //SpriteObject::SpriteObject(&a1);

    a1.stru_24.Reset();
    v24 = *(short *)v23;
    v25 = 0;
    a1.spell_skill = 0;
    a1.spell_level = 0;
    a1.spell_id = 0;
    a1.field_54 = 0;
    a1.uType = v24;
    if ( (signed int)pObjectList->uNumObjects <= 0 )
    {
LABEL_32:
      v27 = 0;
    }
    else
    {
      v26 = (char *)&pObjectList->pObjects->uObjectID;
      while ( v24 != *(short *)v26 )
      {
        ++v25;
        v26 += 56;
        if ( v25 >= (signed int)pObjectList->uNumObjects )
          goto LABEL_32;
      }
      v27 = v25;
    }
    a1.uObjectDescID = v27;
    a1.vPosition.y = pOut.z;
    a1.vPosition.x = pOut.x;
    a1.vPosition.z = pOut.y;
    a1.uSoundID = 0;
    a1.uAttributes = 48;
    a1.uSectorID = pIndoor->GetSector(pOut.x, pOut.z, pOut.y);
    a1.uSpriteFrameID = 0;
    a1.spell_caster_pid = 0;
    a1.spell_target_pid = 0;
    a1.uFacing = 0;
    a1.Create(0, 0, 0, 0);
    pAudioPlayer->PlaySound(SOUND_8, 0, 0, -1, 0, 0, 0, 0);
    a1._438E35();
    chest->SetInitialized(false);//*v34 &= 0xFEu;
    if ( uActiveCharacter && !qword_A750D8 && !dword_507CD8 )
    {
      qword_A750D8 = 256i64;
      word_A750E0 = 5;
      word_A750E2 = uActiveCharacter;
    }
    pIcons_LOD->_4114F2();
    dword_507CD8 = 0;
    return 0;
  }
  chest->SetInitialized(false);//*v1 &= 0xFEu;
  sRotY = 1;
LABEL_12:
  pAudioPlayer->StopChannels(-1, -1);
  pAudioPlayer->PlaySound(SOUND_OpenChest, 0, 0, -1, 0, 0, 0, 0);
  if ( *(float *)&sRotY != 0.0 )
  {
    if ( !dword_507CD8 )
      pPlayers[uActiveCharacter]->PlaySound(SPEECH_4, 0);
  }
  dword_507CD8 = 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,  UIMSG_Escape,  0, 0,  pGlobalTXT_LocalizationStrings[79],// Exit
                 (Texture *)(uExitCancelTextureId != -1 ? (int)&pIcons_LOD->pTextures[uExitCancelTextureId] : 0),   0);
  pChestWindow->CreateButton(7u, 8u, 460u, 343u, 1, 0, UIMSG_C, 0, 0, "", 0);
  pCurrentScreen = SCREEN_CHEST;
  pEventTimer->Pause();
  return 1;
}

//----- (0042092D) --------------------------------------------------------
void Chest::DrawChestUI(signed int uChestID)
    {

    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

    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) + ((signed int)(v13 - itemPixelWidth)/2);
            itemPixelPosY = chest_offs_y + 32 * (item_counter / chestHeghtCells) +
                ((signed int)(((itemPixelHeght - 14) & 0xFFFFFFE0) + 32- item_texture->uTextureHeight ) /2);
            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));
    }


//----- (0041FE71) --------------------------------------------------------
bool Chest::CanPlaceItemAt( signed int test_cell_position, int item_id, signed 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

    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;
    }
// 506128: using guessed type int areWeLoadingTexture;

//----- (0041FF64) --------------------------------------------------------
int Chest::CountChestItems(signed int uChestID)
{
  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 )
  {
    item_count = -1;
  }
  else
  {
    while ( pChests[uChestID].igChestItems[item_count].uItemID )
    {
      ++item_count;
      if ( item_count >= max_items )
          {
          item_count = -1;
          break;
          }
    }
  }
  return item_count;
}

//----- (0041FFA2) --------------------------------------------------------
int Chest::PutItemInChest(int position, ItemGen *put_item, signed int uChestID)
{
  int v3; // eax@1
  ItemGen *v4; // edi@1
  int v5; // esi@1
  int result; // eax@11
  unsigned int v7; // eax@12
  int v8; // edx@12
  int v9; // ecx@12
  signed int v10; // eax@12
  signed int v11; // edi@14
  unsigned int v12; // esi@14
  int v13; // edi@16
  void *v14; // edi@21
  int v15; // edi@21
  int i; // ecx@21
  ItemGen *Src; // [sp+Ch] [bp-18h]@1
  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
  signed int v22; // [sp+20h] [bp-4h]@3
  int v23; // [sp+20h] [bp-4h]@19

  v21 = 0;
  v3 = pChests[uChestID].uChestBitmapID;
  v4 = put_item;
  v5 = pChestWidthsByType[v3] * pChestHeightsByType[v3];
  Src = put_item;
  v19 = pChestWidthsByType[v3];
  if ( position == -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, pChestWindow->par1C) )
      {
        ++v22;
        if ( v22 >= v5 )
          goto LABEL_8;
      }
      v21 = v22;
    }
LABEL_8:
    if ( v22 == v5 )
    {
      if ( uActiveCharacter )
        pPlayers[uActiveCharacter]->PlaySound(SPEECH_NoRoom, 0);
      return 0;
    }
    v7 = pIcons_LOD->LoadTexture(pItemsTable->pItems[v4->uItemID].pIconName, TEXTURE_16BIT_PALETTE);
    HIWORD(v8) = 0;
    v9 = v7 != -1 ? (int)&pIcons_LOD->pTextures[v7] : 0;
    v10 = (v7 != -1 ? pIcons_LOD->pTextures[v7].uTextureWidth : 24);
    if ( v10 < 14 )
      v10 = 14;
    v11 = *(short *)(v9 + 26);
    v12 = ((v10 - 14) >> 5) + 1;
    if ( v11 < 14 )
      v11 = 14;
    v13 = ((v11 - 14) >> 5) + 1;
    if ( !areWeLoadingTexture )
    {
      ((Texture *)v9)->Release();
      pIcons_LOD->_40F9C5();
    }
    if ( v13 > 0 )
    {
      v23 = 0;
      v20 = v13;
      do
      {
        if ( (signed int)v12 > 0 )
        {
          v14 = &pChests[uChestID].pInventoryIndices[v21 + v23];
          LOWORD(v8) = -1 - v21;
          v8 <<= 16;
          LOWORD(v8) = -1 - v21;
          memset32(v14, v8, v12 >> 1);
          v15 = (int)((char *)v14 + 4 * (v12 >> 1));
          for ( i = v12 & 1; i; --i )
          {
            *(short *)v15 = v8;
            v15 += 2;
          }
        }
        v23 += v19;
        --v20;
      }
      while ( v20 );
    }
    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
  {
    result = 1;
  }
  return result;
}
// 506128: using guessed type int areWeLoadingTexture;

//----- (0042013E) --------------------------------------------------------
void Chest::PlaceItemAt( unsigned int put_cell_pos, unsigned int item_at_cell, signed 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

    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)
        {
        v6 = rand() % 21 + 10;
        pChests[ uChestID].igChestItems[item_at_cell].uNumCharges = v6;
        pChests[ uChestID].igChestItems[item_at_cell].uMaxCharges = 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;
    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;
    }
// 506128: using guessed type int areWeLoadingTexture;

//----- (00420284) --------------------------------------------------------
void Chest::PlaceItems(signed int uChestID )
    {
    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 & CHEST_OPENED)
                    pChests[uChestID].igChestItems[items_counter].SetIdentified();
                }
            }
        }
    pChests[uChestID].SetInitialized(true);
    }
// 420284: using guessed type char Dst[144];

//----- (00448A17) --------------------------------------------------------
void Chest::ToggleFlag(signed int uChestID, unsigned __int16 uFlag, unsigned int bToggle)
    {
    unsigned __int16 *pFlags; // eax@3

    if ( uChestID >= 0 && uChestID <= 19 )
        {
        pFlags = &pChests[uChestID].uFlags;
        if ( bToggle )
            *pFlags |= uFlag;
        else
            *pFlags &= ~uFlag;
        }
    }

//----- (00458B03) --------------------------------------------------------
void ChestList::ToFile()
{
  ChestList *v1; // esi@1
  FILE *v2; // eax@1
  FILE *v3; // edi@1

  v1 = this;
  v2 = fopen("data\\dchest.bin", "wb");
  v3 = v2;
  if ( !v2 )
    Abortf("Unable to save dchest.bin!");
  fwrite(v1, 4u, 1u, v2);
  fwrite(v1->pChests, 0x24u, v1->uNumChests, v3);
  fclose(v3);
}


//----- (00458B4F) --------------------------------------------------------
void ChestList::FromFile(void *pSerialized)
{
  uNumChests = *(int *)pSerialized;
  pChests = (ChestDesc *)pAllocator->AllocNamedChunk(pChests, 36 * uNumChests, "Chest Descrip");
  memcpy(pChests, (char *)pSerialized + 4, 36 * uNumChests);
}


//----- (00458B9C) --------------------------------------------------------
int ChestList::FromFileTxt(const char *Args)
{
  ChestList *v2; // ebx@1
  __int32 v3; // edi@1
  FILE *v4; // eax@1
  unsigned int v5; // esi@3
  const void *v6; // ST18_4@9
  void *v7; // eax@9
  FILE *v8; // ST0C_4@11
  char *i; // eax@11
  char v10; // al@14
  const char *v11; // ST14_4@14
  char v12; // al@14
  const char *v13; // ST10_4@14
  char Buf; // [sp+8h] [bp-2F0h]@3
  FrameTableTxtLine v16; // [sp+1FCh] [bp-FCh]@4
  FrameTableTxtLine v17; // [sp+278h] [bp-80h]@4
  FILE *File; // [sp+2F4h] [bp-4h]@1
  unsigned int Argsa; // [sp+300h] [bp+8h]@3

  v2 = this;
  pAllocator->FreeChunk(this->pChests);
  v3 = 0;
  v2->pChests = 0;
  v2->uNumChests = 0;
  v4 = fopen(Args, "r");
  File = v4;
  if ( !v4 )
    Abortf("ChestDescriptionList::load - Unable to open file: %s.");
  v5 = 0;
  Argsa = 0;
  if ( fgets(&Buf, 490, v4) )
  {
    do
    {
      *strchr(&Buf, 10) = 0;
      memcpy(&v17, txt_file_frametable_parser(&Buf, &v16), sizeof(v17));
	  if ( v17.uPropCount && *v17.pProperties[0] != 47 )
        ++Argsa;
    }
    while ( fgets(&Buf, 490, File) );
    v5 = Argsa;
    v3 = 0;
  }
  v6 = v2->pChests;
  v2->uNumChests = v5;
  v7 = pAllocator->AllocNamedChunk(v6, 36 * v5, "Chest Descrip");
  v2->pChests = (ChestDesc *)v7;
  if ( v7 == (void *)v3 )
    Abortf("ChestDescriptionList::load - Out of Memory!");
  memset(v7, v3, 36 * v2->uNumChests);
  v8 = File;
  v2->uNumChests = v3;
  fseek(v8, v3, v3);
  for ( i = fgets(&Buf, 490, File); i; i = fgets(&Buf, 490, File) )
  {
    *strchr(&Buf, 10) = 0;
    memcpy(&v17, txt_file_frametable_parser(&Buf, &v16), sizeof(v17));
	if ( v17.uPropCount && *v17.pProperties[0] != 47 )
    {
      strcpy(v2->pChests[v2->uNumChests].pName, v17.pProperties[0]);
      v10 = atoi(v17.pProperties[1]);
      v11 = v17.pProperties[2];
      v2->pChests[v2->uNumChests].uWidth = v10;
      v12 = atoi(v11);
      v13 = v17.pProperties[3];
      v2->pChests[v2->uNumChests].uHeight = v12;
      v2->pChests[v2->uNumChests++].uTextureID = atoi(v13);
    }
  }
  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
    int 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 = (int)v2;
    v2 = (void *)(5324 * (int)v2);
    //v3 = (ItemGen *)((char *)v2 + 36 * a1 + (int)((char *)pChests + 4));
	v3 = &pChests[v20].igChestItems[a1];
    //v17 = pChestWidthsByType[*(short *)((char *)v2 + (int)pChests)];
	v17 = pChestWidthsByType[pChests[v20].uChestBitmapID];
    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];
				v13 = &pChests[v20].pInventoryIndices[v19 + v11];
                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;