view Chest.cpp @ 433:0eaed3e0d75f

Walk speed x12 (debugging purposes)
author Nomad
date Sun, 24 Feb 2013 01:08:02 +0200
parents 94546824edad
children 0dbf780e247f
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"




size_t uNumChests; // idb
struct ChestList *pChestList;
Chest pChests[20];





//----- (0042041E) --------------------------------------------------------
bool Chest::Open(unsigned 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
  //LayingItem 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);
  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::_420284(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 = dword_5B5920 >> 3;
    v35 = v5;
    if ( (dword_5B5920 & 7) == 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 ( (dword_5B5920 & 7) != OBJECT_BModel)
        goto LABEL_12;
      if ( uCurrentlyLoadedLevelType == LEVEL_Outdoor)
      {
        v7 = &pOutdoor->pBModels[dword_5B5920 >> 9].pFaces[(dword_5B5920 >> 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);
    
    LayingItem a1; // [sp+14h] [bp-B0h]@28
    //LayingItem::LayingItem(&a1);

    a1.stru_24.Reset();
    v24 = *(short *)v23;
    v25 = 0;
    a1.field_50 = 0;
    a1.field_4C = 0;
    a1.field_48 = 0;
    a1.field_54 = 0;
    a1.uItemType = 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.field_58_pid = 0;
    a1.field_5C = 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;
  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);
  pChestWindow->CreateButton(7u, 8u, 460u, 343u, 1, 0, 0xCu, 0, 0, "", 0);
  pCurrentScreen = SCREEN_CHEST;
  pEventTimer->Pause();
  return 1;
}

//----- (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

  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;
    }
    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

  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;
    }
    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)
{
  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 )
  {
LABEL_5:
    result = -1;
  }
  else
  {
    pItem = pChests[v1].mm7__vector_pItems;
    while ( pItem->uItemID )
    {
      ++result;
      ++pItem;
      if ( result >= v3 )
        goto LABEL_5;
    }
  }
  return result;
}

//----- (0041FFA2) --------------------------------------------------------
int Chest::_41FFA2(int a1, ItemGen *a2, unsigned 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 v18; // [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 = a2;
  v5 = pChestWidthsByType[v3] * pChestHeightsByType[v3];
  Src = a2;
  v19 = pChestWidthsByType[v3];
  if ( a1 == -1 )
  {
    v18 = _41FF64(uChestID);
    if ( v18 == -1 )
      return 0;
    v22 = 0;
    if ( v5 > 0 )
    {
      while ( !Chest::CanPlaceItemAt(v22, v4->uItemID, (unsigned int)pChestWindow->ptr_1C) )
      {
        ++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[0].pInventoryIndices[v21 + v23 + 2662 * uChestID];
          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[0].pInventoryIndices[v21 + 2662 * uChestID] = v18 + 1;
    memcpy(&pChests[uChestID].mm7__vector_pItems[v18], Src, 0x24u);
    result = v21 + 1;
  }
  else
  {
    result = 1;
  }
  return result;
}
// 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

  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 )
        {
          *(short *)v15 = v3;
          v15 += 2;
        }
      }
      v13 += v19;
      --v20;
    }
    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
    {
      do
        v5 = (unsigned __int8)rand();
      while ( v5 >= uChestArea );
      while ( Dst[v5] )
      {
        ++v5;
        if ( v5 == uChestArea )
          v5 = 0;
      }
      Dst[v5] = v1++;
    }
    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) --------------------------------------------------------
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, texture_frame_table_txt_parser(&Buf, &v16), sizeof(v17));
      if ( v17.field_0 && *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, texture_frame_table_txt_parser(&Buf, &v16), sizeof(v17));
    if ( v17.field_0 && *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;
}