view mm7_4.cpp @ 1340:22cb507446a4

turnengine refactoring continue
author Gloval
date Sat, 06 Jul 2013 00:29:29 +0400
parents 8bfdc44cebfd
children b99aeb077d4f
line wrap: on
line source

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <assert.h>

#include "Texture.h"
#include "mm7_data.h"
#include "VideoPlayer.h"
#include "Sprites.h"
#include "BSPModel.h"
#include "OutdoorCamera.h"
#include "Mouse.h"
#include "stru6.h"
#include "stru11.h"
#include "stru12.h"

#include "LightmapBuilder.h"
#include "MM7.h"
#include "MapInfo.h"
#include "Game.h"
#include "GUIWindow.h"
#include "GUIFont.h"
#include "Party.h"
#include "AudioPlayer.h"
#include "Outdoor.h"
#include "Outdoor_stuff.h"
#include "IndoorCamera.h"
#include "LOD.h"
#include "Actor.h"
#include "Allocator.h"
#include "Events.h"
#include "Viewport.h"
#include "FrameTableInc.h"
#include "Math.h"
#include "SpriteObject.h"
#include "ObjectList.h"
#include "DecorationList.h"
#include "Time.h"
#include "IconFrameTable.h"
#include "PlayerFrameTable.h"
#include "Awards.h"
#include "TurnEngine.h"
#include "Events2D.h"
#include "stru159.h"
#include "texts.h"
#include "Log.h"
#include "UI\UIHouses.h"
#include "Lights.h"

//----- (0046CC4B) --------------------------------------------------------
void __cdecl check_event_triggers()
{
  int v0; // eax@1
  LevelDecoration *v1; // esi@2
  signed int v2; // edi@2
  int v3; // ebx@2
  int v4; // eax@3
  int v5; // ebx@3
  unsigned int v6; // ecx@3
  unsigned int v7; // edx@6
  unsigned int v8; // edx@8
  Actor *v9; // edi@13
  int v10; // ebx@14
  int v11; // eax@14
  int v12; // ebx@14
  unsigned int v13; // ecx@14
  int v14; // edx@15
  unsigned int v15; // edx@17
  unsigned int v16; // edx@19
  char *v17; // edi@25
  int v18; // ebx@26
  int v19; // eax@26
  int v20; // ebx@26
  unsigned int v21; // ecx@26
  int v22; // edx@27
  unsigned int v23; // edx@29
  unsigned int v24; // edx@31
  int v25; // [sp+0h] [bp-24h]@3
  int v26; // [sp+0h] [bp-24h]@14
  int v27; // [sp+0h] [bp-24h]@26
  int v28; // [sp+4h] [bp-20h]@3
  int v29; // [sp+4h] [bp-20h]@14
  int v30; // [sp+4h] [bp-20h]@26
  signed int v31; // [sp+8h] [bp-1Ch]@2
  int v32; // [sp+Ch] [bp-18h]@2
  int v33; // [sp+10h] [bp-14h]@2
  int i; // [sp+14h] [bp-10h]@1
  int v35; // [sp+18h] [bp-Ch]@2
  int v36; // [sp+1Ch] [bp-8h]@3
  signed int v37; // [sp+1Ch] [bp-8h]@12
  signed int v38; // [sp+20h] [bp-4h]@24

  v0 = 0;
  for ( i = 0; i < _6836C8_num_decorations_6807E8; ++i )
  {
    v1 = &pLevelDecorations[_6807E8_level_decorations_ids[v0]];
    v2 = v1->field_18;
    v3 = v1->vPosition.y;
    v33 = v1->vPosition.x;
    v32 = v1->vPosition.y;
    v35 = v1->vPosition.z;
    v31 = v1->field_18;
    if ( v1->field_2 & 1 )
    {
      v36 = abs(v1->vPosition.x - pParty->vPosition.x);
      v25 = abs(v3 - pParty->vPosition.y);
      v28 = abs(v35 - pParty->vPosition.z);
      v4 = v36;
      v5 = v25;
      v6 = v28;
      if ( v36 < v25 )
      {
        v4 = v25;
        v5 = v36;
      }
      if ( v4 < v28 )
      {
        v7 = v4;
        v4 = v28;
        v6 = v7;
      }
      if ( v5 < (signed int)v6 )
      {
        v8 = v6;
        v6 = v5;
        v5 = v8;
      }
      if ( (signed int)(((unsigned int)(11 * v5) >> 5) + (v6 >> 2) + v4) < v2 )
        EventProcessor(v1->field_16_event_id, PID(OBJECT_Decoration,i), 1);
    }
    if ( v1->field_2 & 2 )
    {
      v37 = 0;
      if ( (signed int)uNumActors > 0 )
      {
        v9 = pActors.data();//[0].vPosition.y;
        do
        {
		  v10 = abs(v33 - v9->vPosition.x);
          v29 = abs(v32 - v9->vPosition.y);
          v26 = abs(v35 - v9->vPosition.z);
          v11 = v10;
          v12 = v29;
          v13 = v26;
          if ( v11 < v29 )
          {
            v14 = v11;
            v11 = v29;
            v12 = v14;
          }
          if ( v11 < v26 )
          {
            v15 = v11;
            v11 = v26;
            v13 = v15;
          }
          if ( v12 < (signed int)v13 )
          {
            v16 = v13;
            v13 = v12;
            v12 = v16;
          }
          if ( (signed int)(((unsigned int)(11 * v12) >> 5) + (v13 >> 2) + v11) < v31 )
            EventProcessor(v1->field_16_event_id, 0, 1);
          ++v37;
          ++v9;
        }
        while ( v37 < (signed int)uNumActors );
      }
    }
    if ( v1->field_2 & 4 )
    {
      v38 = 0;
      if ( (signed int)uNumSpriteObjects > 0 )
      {
        v17 = (char *)&pSpriteObjects[0].vPosition.y;
        do
        {
          v18 = abs(v33 - *((int *)v17 - 1));
          v30 = abs(v32 - *(int *)v17);
          v27 = abs(v35 - *((int *)v17 + 1));
          v19 = v18;
          v20 = v30;
          v21 = v27;
          if ( v19 < v30 )
          {
            v22 = v19;
            v19 = v30;
            v20 = v22;
          }
          if ( v19 < v27 )
          {
            v23 = v19;
            v19 = v27;
            v21 = v23;
          }
          if ( v20 < (signed int)v21 )
          {
            v24 = v21;
            v21 = v20;
            v20 = v24;
          }
          if ( (signed int)(((unsigned int)(11 * v20) >> 5) + (v21 >> 2) + v19) < v31 )
            EventProcessor(v1->field_16_event_id, 0, 1);
          ++v38;
          v17 += 112;
        }
        while ( v38 < (signed int)uNumSpriteObjects );
      }
    }
    v0 = i + 1;
  }
}
// 6836C8: using guessed type int 6836C8_num_decorations_6807E8;

//----- (0046D8E3) --------------------------------------------------------
int __fastcall sub_46D8E3(int a1, signed int a2, int a3, int a4)
{
  int v4; // ebx@1
  unsigned int v5; // ecx@1
  BSPModel *v6; // edi@3
  ODMFace *v7; // esi@10
  unsigned __int8 v8; // al@10
  signed __int16 *v9; // eax@18
  int v10; // edx@19
  int v11; // ecx@21
  int v12; // ecx@23
  signed int v13; // eax@25
  int v14; // edx@27
  int v15; // edx@29
  int v16; // ST18_4@29
  signed int v17; // edx@29
  signed __int64 v18; // qtt@29
  int v19; // eax@35
  signed int v20; // ecx@37
  signed int v22; // ebx@42
  unsigned int v23; // esi@43
  int v24; // edx@44
  int v25; // eax@44
  int v26; // [sp+Ch] [bp-38h]@7
  int v27; // [sp+10h] [bp-34h]@21
  int v28; // [sp+18h] [bp-2Ch]@21
  int v29; // [sp+1Ch] [bp-28h]@2
  unsigned int v30; // [sp+20h] [bp-24h]@8
  signed int v31; // [sp+24h] [bp-20h]@1
  signed int v32; // [sp+28h] [bp-1Ch]@1
  int v33; // [sp+2Ch] [bp-18h]@7
  bool v34; // [sp+30h] [bp-14h]@21
  bool v35; // [sp+34h] [bp-10h]@23
  signed int v36; // [sp+38h] [bp-Ch]@17
  signed int v37; // [sp+38h] [bp-Ch]@21
  signed int v38; // [sp+38h] [bp-Ch]@42
  signed int v39; // [sp+3Ch] [bp-8h]@1
  signed int v40; // [sp+40h] [bp-4h]@1

  dword_720ED0[0] = -1;
  dword_720E80[0] = -1;
  v4 = a1;
  v5 = 0;
  v40 = a2;
  v31 = v4;
  v39 = 1;
  dword_720F20[0] = 10000;
  v32 = 0;
  if ( (signed int)pOutdoor->uNumBModels > 0 )
  {
    v29 = 0;
    do
    {
      v6 = &pOutdoor->pBModels[v29];
      if ( v4 <= pOutdoor->pBModels[v29].sMaxX )
      {
        if ( v4 >= v6->sMinX )
        {
          if ( v40 <= v6->sMaxY )
          {
            if ( v40 >= v6->sMinY )
            {
              v33 = v5;
              v26 = v6->uNumFaces;
              if ( (signed int)v6->uNumFaces > (signed int)v5 )
              {
                v30 = v5;
                while ( 1 )
                {
                  v7 = (ODMFace *)((char *)v6->pFaces + v30);
                  v8 = v7->uPolygonType;
                  if ( (v8 == 5 || v8 == 6)
                    && !(BYTE3(v7->uAttributes) & 0x20)
                    && v4 <= v7->pBoundingBox.x2
                    && v4 >= v7->pBoundingBox.x1
                    && v40 <= v7->pBoundingBox.y2
                    && v40 >= v7->pBoundingBox.y1 )
                  {
                    v36 = v5;
                    if ( v7->uNumVertices )
                    {
                      v9 = v7->pXInterceptDisplacements;
                      do
                      {
                        v10 = 2 * v36;
                        word_720DB0_xs[2 * v36] = *v9 + LOWORD(v6->pVertices.pVertices[*(v9 - 60)].x);
                        word_720CE0_ys[2 * v36] = v9[20] + LOWORD(v6->pVertices.pVertices[*(v9 - 60)].y);
                        word_720DB0_xs[2 * v36++ + 1] = *v9 + LOWORD(v6->pVertices.pVertices[*(v9 - 59)].x);
                        word_720CE0_ys[v10 + 1] = v9[20] + LOWORD(v6->pVertices.pVertices[*(v9 - 59)].y);
                        ++v9;
                      }
                      while ( v36 < v7->uNumVertices );
                      v4 = v31;
                    }
                    v27 = 2 * v7->uNumVertices;
                    word_720DB0_xs[2 * v7->uNumVertices] = word_720DB0_xs[0];
                    word_720CE0_ys[v27] = word_720CE0_ys[0];
                    v11 = 0;
                    v34 = word_720CE0_ys[0] >= v40;
                    v37 = 0;
                    v28 = 0;
                    if ( v27 > 0 )
                    {
                      do
                      {
                        if ( v37 >= 2 )
                          break;
                        v12 = v11;
                        v4 = v31;
                        v35 = word_720CE0_ys[v12 + 1] >= v40;
                        if ( v34 != v35 )
                        {
                          v13 = word_720DB0_xs[v12 + 1] >= v31 ? 0 : 2;
                          v14 = v13 | (word_720DB0_xs[v12] < v31);
                          if ( v14 != 3 )
                          {
                            if ( !v14
                              || (v15 = word_720CE0_ys[v12],
                                  v16 = word_720CE0_ys[v12 + 1] - v15,
                                  v17 = v40 - v15,
                                  LODWORD(v18) = v17 << 16,
                                  HIDWORD(v18) = v17 >> 16,
                                  (signed int)(((unsigned __int64)(((signed int)word_720DB0_xs[v12 + 1]
                                                                  - (signed int)word_720DB0_xs[v12])
                                                                 * v18
                                                                 / v16) >> 16)
                                             + word_720DB0_xs[v12]) >= v31) )
                              ++v37;
                          }
                        }
                        v11 = v28 + 1;
                        v34 = v35;
                        ++v28;
                      }
                      while ( v28 < v27 );
                      if ( v37 == 1 )
                      {
                        if ( v39 >= 20 )
                          break;
                        if ( v7->uPolygonType == 5 )
                          v19 = v6->pVertices.pVertices[v7->pVertexIDs[0]].z;
                        else
                          v19 = ((unsigned __int64)(v7->zCalc1 * (signed __int64)v4) >> 16)
                              + ((unsigned __int64)(v7->zCalc2 * (signed __int64)v40) >> 16)
                              + HIWORD(v7->zCalc3);
                        v20 = v39++;
                        dword_720F20[v20] = v19;
                        dword_720ED0[v20] = v32;
                        dword_720E80[v20] = v33;
                      }
                    }
                  }
                  ++v33;
                  v30 += 308;
                  if ( v33 >= v26 )
                    break;
                  v5 = 0;
                }
              }
            }
          }
        }
      }
      ++v32;
      ++v29;
      v5 = 0;
    }
    while ( v32 < (signed int)pOutdoor->uNumBModels );
    if ( !v39 )
    {
      *(int *)a4 = 0;
      return dword_720F20[0];
    }
  }
  v22 = 0;
  v38 = v5;
  if ( v39 <= (signed int)v5 )
    goto LABEL_54;
  v23 = 0;
  do
  {
    v24 = dword_720F20[v5 / 4];
    v25 = *(int *)((char *)dword_720F20.data() + v23);
    if ( v24 == v25 )
      goto LABEL_50;
    if ( v25 > a3 + 15 )
    {
      if ( v24 >= v25 )
        goto LABEL_51;
LABEL_50:
      v22 = v38;
      v23 = v5;
      goto LABEL_51;
    }
    if ( v24 > v25 && v24 <= a3 + 15 )
      goto LABEL_50;
LABEL_51:
    ++v38;
    v5 += 4;
  }
  while ( v38 < v39 );
  if ( v22 )
  {
    *(int *)a4 = dword_720E80[v22] | (dword_720ED0[v22] << 6);
    return dword_720F20[v22];
  }
LABEL_54:
  *(int *)a4 = 0;
  return dword_720F20[v22];
}

//----- (0046DEF2) --------------------------------------------------------
unsigned int __fastcall sub_46DEF2(signed int a2, unsigned int uLayingItemID)
{
  unsigned int result; // eax@1

  result = uLayingItemID;
  if ( pObjectList->pObjects[pSpriteObjects[uLayingItemID].uObjectDescID].uFlags & 0x10 )
    result = _46BFFA_check_object_intercept(uLayingItemID, a2);
  return result;
}

//----- (0046E0B2) --------------------------------------------------------
void __cdecl _46E0B2_collide_against_decorations()
{
  BLVSector *v0; // ebp@1
  LevelDecoration *v1; // edi@2
  DecorationDesc *v2; // esi@3
  int v3; // edx@4
  int v4; // eax@4
  int v5; // ecx@6
  int v6; // ebx@8
  int v7; // esi@8
  int v8; // ebx@10
  int v9; // esi@11
  int v10; // edi@12
  int v11; // eax@12
  int v12; // esi@14
  unsigned int v13; // eax@17
  signed int i; // [sp+4h] [bp-14h]@1
  int v15; // [sp+8h] [bp-10h]@10
  int v16; // [sp+Ch] [bp-Ch]@10
  int v17; // [sp+10h] [bp-8h]@10
  int v18; // [sp+14h] [bp-4h]@8

  v0 = &pIndoor->pSectors[stru_721530.uSectorID];
  for ( i = 0; i < v0->uNumDecorations; ++i )
  {
    v1 = &pLevelDecorations[v0->pDecorationIDs[i]];
    if ( !(v1->field_2 & 0x20) )
    {
      v2 = &pDecorationList->pDecorations[v1->uDecorationDescID];
      if (!v2->CanMoveThrough())
      {
        v3 = v2->uRadius;
        v4 = v1->vPosition.x;
        if ( stru_721530.sMaxX <= v4 + v3 )
        {
          if ( stru_721530.sMinX >= v4 - v3 )
          {
            v5 = v1->vPosition.y;
            if ( stru_721530.sMaxY <= v5 + v3 )
            {
              if ( stru_721530.sMinY >= v5 - v3 )
              {
                v6 = v2->uDecorationHeight;
                v7 = v1->vPosition.z;
                v18 = v6;
                if ( stru_721530.sMaxZ <= v7 + v6 )
                {
                  if ( stru_721530.sMinZ >= v7 )
                  {
                    v16 = v4 - stru_721530.normal.x;
                    v15 = v5 - stru_721530.normal.y;
                    v8 = stru_721530.prolly_normal_d + v3;
                    v17 = ((v4 - stru_721530.normal.x) * stru_721530.field_58.y
                         - (v5 - stru_721530.normal.y) * stru_721530.field_58.x) >> 16;
                    if ( abs(v17) <= stru_721530.prolly_normal_d + v3 )
                    {
                      v9 = (v16 * stru_721530.field_58.x + v15 * stru_721530.field_58.y) >> 16;
                      if ( v9 > 0 )
                      {
                        v10 = v1->vPosition.z;
                        v11 = stru_721530.normal.z
                            + ((unsigned __int64)(stru_721530.field_58.z * (signed __int64)v9) >> 16);
                        if ( v11 >= v10 )
                        {
                          if ( v11 <= v18 + v10 )
                          {
                            v12 = v9 - integer_sqrt(v8 * v8 - v17 * v17);
                            if ( v12 < 0 )
                              v12 = 0;
                            if ( v12 < stru_721530.field_7C )
                            {
                              stru_721530.field_7C = v12;
                              v13 = 8 * v0->pDecorationIDs[i];
                              LOBYTE(v13) = v13 | 5;
                              stru_721530.uFaceID = v13;
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

//----- (00486F92) --------------------------------------------------------
void __cdecl sr_sub_486F92_MessWithEdgesAndSpans()
{
  Span *v0; // ebx@1
  int v1; // eax@2
  Edge *v2; // ecx@3
  Edge *v3; // edx@3
  Edge *v4; // esi@6
  Edge *v5; // eax@7
  Edge *v6; // ecx@8
  Surf *v7; // esi@11
  double v8; // st7@13
  Surf *v9; // edi@13
  double v10; // st6@13
  double v11; // st7@14
  signed __int64 v12; // qax@14
  stru148 *v13; // eax@15
  Span *v14; // ecx@17
  double v15; // st7@28
  signed __int64 v16; // qax@28
  stru148 *v17; // eax@29
  Span *v18; // ecx@31
  Edge *i; // eax@40
  Edge *v20; // ecx@43
  Edge *v21; // esi@44
  double v22; // st7@45
  Edge *v23; // edx@48
  unsigned int v24; // [sp+10h] [bp-10h]@2
  float v25; // [sp+14h] [bp-Ch]@3
  Edge *v26; // [sp+18h] [bp-8h]@8
  unsigned int v27; // [sp+1Ch] [bp-4h]@1

  v0 = pSpans;
  stru_80C9D8.pSurf = &stru_80C980;
  stru_80C9D8.field_0 = (double)(signed int)pViewport->uViewportTL_X;
  stru_80C9A4.pSurf = &stru_80C980;
  stru_80C980.pPrev = &stru_80C980;
  stru_80C980.pNext = &stru_80C980;
  stru_80C9A4.field_0 = (double)(signed int)pViewport->uViewportBR_X;
  stru_80C980.field_8 = 0.0;
  stru_80C980.field_4 = 0.0;
  stru_80C9D8.pNext = &stru_80C9A4;
  stru_80C9D8.pPrev = 0;
  stru_80C9D8.field_8 = 1;
  stru_80C9A4.pNext = 0;
  stru_80C9A4.pPrev = &stru_80C9D8;
  stru_80C9A4.field_8 = 0;
  LODWORD(stru_80C980.field_0) = 0xC97423F0u;
  v27 = pViewport->uViewportTL_Y;
  if ( (signed int)pViewport->uViewportTL_Y > (signed int)pViewport->uViewportBR_Y )
  {
LABEL_51:
    v0->field_8 = -1;
  }
  else
  {
    v1 = 52 * pViewport->uViewportTL_Y;
    v24 = 52 * pViewport->uViewportTL_Y;
    while ( 1 )
    {
      v2 = *(Edge **)((char *)&pNewEdges->pNext + v1);
      v3 = &stru_80C9D8;
      v25 = (double)(signed int)v27;
      if ( v2 != &defaultEdge )
      {
        do
        {
          while ( 1 )
          {
            v4 = v3->pNext;
            if ( v2->field_0 <= (double)v4->field_0 )
              break;
            v3 = v3->pNext;
          }
          v5 = v2->pNext;
          v2->pNext = v4;
          v2->pPrev = v3;
          v3->pNext->pPrev = v2;
          v3->pNext = v2;
          v3 = v2;
          v2 = v5;
        }
        while ( v5 != &defaultEdge );
      }
      v6 = stru_80C9D8.pNext;
      stru_80C980.field_20 = 0;
      stru_80C980.field_22 = 1;
      v26 = stru_80C9D8.pNext;
      if ( stru_80C9D8.pNext )
      {
        while ( 1 )
        {
          v7 = v6->pSurf;
          if ( v6->field_8 )
          {
            ++v7->field_22;
            if ( v7->field_22 == 1 )
            {
              v8 = v6->field_0 + 2.0;
              v9 = stru_80C980.pNext;
              v10 = (v8 - v7->field_C) * v7->field_4 + (v25 - v7->field_10) * v7->field_8 + v7->field_0;
              if ( v10 <= (v8 - stru_80C980.pNext->field_C) * stru_80C980.pNext->field_4
                        + (v25 - stru_80C980.pNext->field_10) * stru_80C980.pNext->field_8
                        + stru_80C980.pNext->field_0 )
              {
                do
                  v9 = v9->pNext;
                while ( v10 <= (v8 - v9->field_C) * v9->field_4 + (v25 - v9->field_10) * v9->field_8 + v9->field_0 );
                v7->pNext = v9;
                v7->pPrev = v9->pPrev;
                v9->pPrev->pNext = v7;
                v9->pPrev = v7;
              }
              else
              {
                v11 = v6->field_0 + 0.5;
                v12 = (signed __int64)(v11 - (double)stru_80C980.pNext->field_20);
                v0->field_C = v12;
                if ( (signed __int16)v12 > 0 )
                {
                  v0->field_A = v27;
                  v0->field_8 = v9->field_20;
                  v13 = v9->pParent;
                  v0->pParent = v13;
                  if ( v13 )
                  {
                    if ( v13->prolly_head )
                    {
                      v14 = v13->prolly_tail;
                      if ( !v14 )
                        return;
                      v14->pNext = v0;
                    }
                    else
                    {
                      v13->prolly_head = v0;
                    }
                    v13->prolly_tail = v0;
                  }
                  if ( (signed int)pOutdoorCamera->uNumSpans >= 12499 )
                    return;
                  ++v0;
                  ++pOutdoorCamera->uNumSpans;
                }
                v6 = v26;
                v7->field_20 = (signed __int64)v11;
                v7->pNext = v9;
                v9->pPrev = v7;
                stru_80C980.pNext = v7;
                v7->pPrev = &stru_80C980;
              }
            }
          }
          else
          {
            --v7->field_22;
            if ( !v7->field_22 )
            {
              if ( stru_80C980.pNext == v7 )
              {
                v15 = v6->field_0 + 0.5;
                v16 = (signed __int64)(v15 - (double)v7->field_20);
                v0->field_C = v16;
                if ( (signed __int16)v16 > 0 )
                {
                  v0->field_A = v27;
                  v0->field_8 = v7->field_20;
                  v17 = v7->pParent;
                  v0->pParent = v17;
                  if ( v17 )
                  {
                    if ( v17->prolly_head )
                    {
                      v18 = v17->prolly_tail;
                      if ( !v18 )
                        return;
                      v18->pNext = v0;
                    }
                    else
                    {
                      v17->prolly_head = v0;
                    }
                    v17->prolly_tail = v0;
                  }
                  if ( (signed int)pOutdoorCamera->uNumSpans >= 12499 )
                    return;
                  ++v0;
                  ++pOutdoorCamera->uNumSpans;
                }
                v7->pNext->field_20 = (signed __int64)v15;
                v6 = v26;
              }
              v7->pNext->pPrev = v7->pPrev;
              v7->pPrev->pNext = v7->pNext;
            }
          }
          v26 = v6->pNext;
          if ( !v26 )
            break;
          v6 = v6->pNext;
        }
      }
      for ( i = ptr_80CA10[v27]; i; i = i->ptr_18 )
      {
        i->pPrev->pNext = i->pNext;
        i->pNext->pPrev = i->pPrev;
      }
      v20 = stru_80C9D8.pNext;
      if ( stru_80C9D8.pNext != &stru_80C9A4 )
        break;
LABEL_50:
      ++v27;
      v1 = v24 + 52;
      v24 += 52;
      if ( (signed int)v27 > (signed int)pViewport->uViewportBR_Y )
        goto LABEL_51;
    }
    while ( 1 )
    {
      v21 = v20->pNext;
      if ( !v21 )
        break;
      v22 = v20->field_4 + v20->field_0;
      v20->field_0 = v22;
      if ( v22 < stru_80C9D8.field_0 )
        v20->field_0 = stru_80C9D8.field_0 + 0.0000001;
      while ( 1 )
      {
        v23 = v20->pPrev;
        if ( v20->field_0 >= (double)v23->field_0 )
          break;
        v23->pNext = v20->pNext;
        v20->pNext->pPrev = v23;
        v23->pPrev->pNext = v20;
        v20->pPrev = v23->pPrev;
        v20->pNext = v23;
        v23->pPrev = v20;
      }
      v20 = v21;
      if ( v21 == &stru_80C9A4 )
        goto LABEL_50;
    }
  }
}
// 4EC3EC: using guessed type Edge defaultEdge;

//----- (00487DA9) --------------------------------------------------------
void __cdecl sub_487DA9()
{
  char *v0; // eax@1

  v0 = &array_77EC08[0].field_108;
  do
  {
    *v0 = 0;
    v0 += 268;
  }
  while ( (signed int)v0 < (signed int)&pVerticesSR_801A10[4] );
}

//----- (00487DBE) --------------------------------------------------------
void Software_ResetNewEdges()
{
  int v0; // ecx@1
  Edge **v1; // eax@1
  signed int v2; // eax@3

  v0 = 0;
  v1 = ptr_80CA10.data();
  do
  {
    ++v0;
    *((int *)&pNewEdges[v0] - 9) = (int)&defaultEdge;
    *v1 = 0;
    ++v1;
  }
  while ( (signed int)v1 < (signed int)&unk_80D190 );
  v2 = 2000;
  do
  {
    pSurfs->field_22 = 0;
    --v2;
  }
  while ( v2 );
}
// 4EC3EC: using guessed type Edge defaultEdge;

//----- (0048958E) --------------------------------------------------------
stru12_MemoryBlock::stru12_MemoryBlock(int a2)
{
  stru12_MemoryBlock *v2; // esi@1
  void *v3; // eax@1
  void *v4; // ecx@1
  stru12_MemoryBlock *result; // eax@1

  v2 = this;
  v3 = operator new(8 * a2 + 16);
  v2->pBlockBase = v3;
  v4 = (char *)v3 + PID_TYPE(-(signed int)v3);
  result = v2;
  v2->pAlignedBlock = v4;
}

//----- (004895B7) --------------------------------------------------------
stru12::stru12(stru11 *pStru11)
{
  stru12 *v2; // esi@1

  v2 = this;
  this->field_44 = 0x41000000u;
  this->field_4C = 0x46000000u;
  //this->vdestructor_ptr = stru12::_4898E6;
  this->field_48 = 0;
  this->field_50 = 0;
  this->field_54 = 0x33D6BF95u;
  this->field_58 = 0;
  
  for (int i = 0; i < 12; ++i)
    v2->pMemBlocks[i] = new stru12_MemoryBlock(640);
}

//----- (00489810) --------------------------------------------------------
stru12::~stru12()
{
  for (int i = 0; i < 12; ++i)
    delete pMemBlocks[i];
}

//----- (004898BF) --------------------------------------------------------
stru12_MemoryBlock::~stru12_MemoryBlock()
{
  stru12_MemoryBlock *v2; // esi@1
  void *v3; // ST00_4@1

  v2 = this;
  v3 = this->pBlockBase;
  this->pAlignedBlock = 0;
  free(v3);
  v2->pBlockBase = 0;
  /*if ( a2 & 1 )
    free(v2);*/
}

//----- (004898E6) --------------------------------------------------------
void stru12::_4898E6()
{
  void *v1; // eax@1
  void *v2; // edx@1
  char *v3; // edi@2
  double v4; // st7@2
  char *v5; // ebx@2
  double v6; // st6@3
  double v7; // st5@3
  double v8; // st4@3
  int v9; // esi@3
  double v10; // st3@3
  float v11; // ST34_4@5
  double v12; // st6@10
  double v13; // ST0C_8@10
  char *v14; // [sp+14h] [bp-44h]@2
  char *v15; // [sp+18h] [bp-40h]@2
  char *v16; // [sp+1Ch] [bp-3Ch]@2
  char *v17; // [sp+20h] [bp-38h]@2
  char *v18; // [sp+24h] [bp-34h]@2
  float v19; // [sp+30h] [bp-28h]@3
  float v20; // [sp+38h] [bp-20h]@3
  char *v21; // [sp+3Ch] [bp-1Ch]@2
  int v22; // [sp+40h] [bp-18h]@1
  char *v23; // [sp+44h] [bp-14h]@2
  char *v24; // [sp+48h] [bp-10h]@2
  int v25; // [sp+4Ch] [bp-Ch]@2
  float v26; // [sp+50h] [bp-8h]@3
  float v27; // [sp+54h] [bp-4h]@3

  __debugbreak();
  v22 = 0;
  v1 = this->pMemBlocks[1]->pAlignedBlock;
  v2 = this->pMemBlocks[6]->pAlignedBlock;
  if ( this->field_8C > 0 )
  {
    v24 = (char *)((char *)pMemBlocks[0]->pAlignedBlock - v1);
    v23 = (char *)((char *)pMemBlocks[2]->pAlignedBlock - v1);
    v18 = (char *)((char *)pMemBlocks[7]->pAlignedBlock - v2);
    v17 = (char *)((char *)pMemBlocks[8]->pAlignedBlock - v2);
    v25 = (int)v1 - (int)v2;
    v16 = (char *)((char *)pMemBlocks[9]->pAlignedBlock - v2);
    v3 = (char *)((char *)pMemBlocks[4]->pAlignedBlock - v2);
    v15 = (char *)((char *)pMemBlocks[10]->pAlignedBlock - v2);
    v4 = 1.0;
    v5 = (char *)((char *)pMemBlocks[5]->pAlignedBlock - v2);
    v21 = (char *)((char *)pMemBlocks[3]->pAlignedBlock - v2);
    v14 = (char *)((char *)pMemBlocks[11]->pAlignedBlock - v2);
    do
    {
      v26 = *(float *)&v24[(int)((char *)v2 + v25)] - (double)pIndoorCamera->pos.x;
      v27 = *(float *)((char *)v2 + v25) - (double)pIndoorCamera->pos.y;
      v6 = *(float *)&v23[(int)((char *)v2 + v25)] - (double)pIndoorCamera->pos.z;
      v7 = pIndoorCamera->fRotationYCosine;
      v8 = pIndoorCamera->fRotationYSine;
      v20 = pIndoorCamera->fRotationXCosine;
      v19 = pIndoorCamera->fRotationXSine;
      v9 = (int)((char *)v2 + (int)v21);
      v10 = v27 * pIndoorCamera->fRotationYSine + pIndoorCamera->fRotationYCosine * v26;
      if ( pIndoorCamera->sRotationX )
      {
        v11 = v10;
        *(float *)v9 = v11 * pIndoorCamera->fRotationXCosine + pIndoorCamera->fRotationXSine * v6;
        *(float *)((char *)v2 + (int)v3) = v7 * v27 - v8 * v26;
        *(float *)((char *)v2 + (int)v5) = v20 * v6 - v11 * v19;
      }
      else
      {
        *(float *)v9 = v10;
        *(float *)((char *)v2 + (int)v3) = v7 * v27 - v8 * v26;
        *(float *)((char *)v2 + (int)v5) = v6;
      }
      if ( *(float *)v9 >= 8.0 )
      {
        if ( (double)pOutdoorCamera->shading_dist_mist >= *(float *)v9 )
        {
          *(int *)v2 = 0;
          v12 = v4 / (*(float *)v9 + 0.0000001) * (double)pOutdoorCamera->int_fov_rad;
          *(float *)((char *)v2 + (int)v18) = (double)pViewport->uScreenCenterX
                                               - v12 * *(float *)((char *)v2 + (int)v3);
          *(float *)((char *)v2 + (int)v17) = (double)pViewport->uScreenCenterY
                                               - v12 * *(float *)((char *)v2 + (int)v5);
          *(float *)((char *)v2 + (int)v16) = v4
                                               - v4 / (*(float *)v9 * 1000.0 / (double)pOutdoorCamera->shading_dist_mist);
          *(float *)((char *)v2 + (int)v15) = v4 / (*(float *)v9 + 0.0000001);
          v13 = *(float *)v9 + 6.7553994e15;
          v4 = 1.0;
          *(int *)((char *)v2 + (int)v14) = LODWORD(v13);
        }
        else
        {
          *(int *)v2 = 2;
        }
      }
      else
      {
        *(int *)v2 = 1;
      }
      ++v22;
      v2 = (char *)v2 + 4;
    }
    while ( v22 < this->field_8C );
  }
  this->field_8C = 0;
}

//----- (0048A959) --------------------------------------------------------
unsigned int ReplaceHSV(unsigned int uColor, float h_replace, float s_replace, float v_replace)
{
  float r = ((uColor & 0x00FF0000) >> 16) / 255.0f,
        g = ((uColor & 0x0000FF00) >> 8) / 255.0f,
        b = (uColor & 0x000000FF) / 255.0f;

  float h, s, v;
  RGB2HSV(&h, &s, r, g, b, &v);

  if ( h_replace != -1.0 )
    h = h_replace;
  if ( s_replace != -1.0 )
    s = s_replace;
  if ( v_replace != -1.0 )
    v = v_replace;
  HSV2RGB(&r, &g, &b, h, s, v);

  return (((uint)floorf(r * 255.0f + 0.5f) & 0xFF) << 16) |
         (((uint)floorf(g * 255.0f + 0.5f) & 0xFF) << 8) |
         ((uint)floorf(b * 255.0f + 0.5f) & 0xFF);
}

//----- (0048B561) --------------------------------------------------------
int fixpoint_from_float(float val)
{
  //  float X.Yf -> int XXXX YYYY
  int left = floorf((val - 0.5f) + 0.5f);
  int right = floorf((val - left) * 65536.0f);
  return (left << 16) | right;
}

//----- (00491E3A) --------------------------------------------------------
void __cdecl sub_491E3A()
{
  Player *v0; // ebx@1
  signed int v1; // esi@3
  char *v2; // eax@4
  unsigned int v3; // eax@7
  unsigned int v4; // edx@8
  char *v5; // ecx@9
  int v6; // edi@17
  Texture *v7; // ebx@18
  struct IDirect3DTexture2 **v8; // eax@19
  struct IDirect3DTexture2 *v9; // eax@20
  struct IDirectDrawSurface **v10; // eax@22
  struct IDirectDrawSurface *v11; // eax@23
  int v12; // eax@26

  v0 = pParty->pPlayers;
  do
  {
    if (SoundSetAction[24][0])
    {
      v1 = 0;
      if ( (signed int)pSoundList->sNumSounds <= 0 )
      {
LABEL_7:
        v3 = 0;
      }
      else
      {
        v2 = (char *)&pSoundList->pSounds->uSoundID;
        while ( *(int *)v2 != 2 * (SoundSetAction[24][0] + 50 * v0->uVoiceID) + 4998 )
        {
          ++v1;
          v2 += 120;
          if ( v1 >= (signed int)pSoundList->sNumSounds )
            goto LABEL_7;
        }
        v3 = v1;
      }
      pSoundList->UnloadSound(v3, 1);
      v4 = 0;
      if ( (signed int)pSoundList->sNumSounds <= 0 )
      {
LABEL_12:
        v4 = 0;
      }
      else
      {
        v5 = (char *)&pSoundList->pSounds->uSoundID;
        while ( *(int *)v5 != 2 * (SoundSetAction[24][0] + 50 * v0->uVoiceID) + 4999 )
        {
          ++v4;
          v5 += 120;
          if ( (signed int)v4 >= (signed int)pSoundList->sNumSounds )
            goto LABEL_12;
        }
      }
      pSoundList->UnloadSound(v4, 1);
    }
    ++v0;
  }
  while ( (signed int)v0 < (signed int)pParty->pHirelings );
  v6 = pIcons_LOD->uNumLoadedFiles - 1;
  if ( v6 >= pIcons_LOD->pFacesLock )
  {
    v7 = &pIcons_LOD->pTextures[v6];
    do
    {
      v7->Release();
      v8 = pIcons_LOD->pHardwareTextures;
      if ( v8 )
      {
        v9 = v8[v6];
        if ( v9 )
        {
          v9->Release();
          pIcons_LOD->pHardwareTextures[v6] = 0;
        }
      }
      v10 = pIcons_LOD->pHardwareSurfaces;
      if ( v10 )
      {
        v11 = v10[v6];
        if ( v11 )
        {
          v11->Release();
          pIcons_LOD->pHardwareSurfaces[v6] = 0;
        }
      }
      --v6;
      --v7;
    }
    while ( v6 >= pIcons_LOD->pFacesLock );
  }
  v12 = pIcons_LOD->pFacesLock;
  pIcons_LOD->pFacesLock = 0;
  pIcons_LOD->uNumLoadedFiles = v12;
}
// 4ED498: using guessed type char byte_4ED498;

//----- (00493938) --------------------------------------------------------
int __cdecl _493938_regenerate()
{
  int v0; // edi@1
  signed __int64 v1; // qax@1
  int v2; // ecx@1
  int result; // eax@1
  int v4; // eax@2
  int v5; // edi@5
  int *v6; // ecx@5
  char v7; // sf@5
  int *v8; // ecx@10
  int v9; // edi@15
  signed int v10; // eax@15
  __int16 *v11; // edx@16
  int v12; // eax@20
  int v13; // ebx@20
  unsigned int *v14; // esi@21
  unsigned int v15; // ecx@21
  unsigned int v16; // eax@21
  int v17; // edx@21
  int v18; // eax@21
  signed int v19; // eax@21
  signed int v20; // ebx@25
  Player *v21; // esi@25
  ITEM_EQUIP_TYPE v22; // edi@30
  //int v23; // edx@31
  signed int v24; // ecx@32
  signed int v25; // eax@33
  int v26; // eax@35
  int v27; // eax@36
  int v28; // eax@37
  int v29; // eax@40
  int v30; // eax@41
  signed int v31; // ecx@53
  char *v32; // eax@53
  char *v33; // edi@82
  int v34; // ecx@88
  int v35; // eax@88
  char *v36; // edi@99
  int v37; // edi@104
  int v38; // edi@106
  int v39; // edi@111
  int v40; // ecx@113
  char v41[400]; // [sp+4h] [bp-22Ch]@20
  SpriteObject a1; // [sp+194h] [bp-9Ch]@15
  Vec3_int_ a3; // [sp+204h] [bp-2Ch]@15
  int v44; // [sp+210h] [bp-20h]@22
  int v45; // [sp+214h] [bp-1Ch]@25
  int v46; // [sp+218h] [bp-18h]@25
  int v47; // [sp+21Ch] [bp-14h]@25
  int v48; // [sp+220h] [bp-10h]@25
  int v49; // [sp+224h] [bp-Ch]@24
  int v50; // [sp+228h] [bp-8h]@25
  int v51; // [sp+22Ch] [bp-4h]@2

  v0 = (signed int)(signed __int64)((double)(signed __int64)pParty->uTimePlayed * 0.234375) / 60;
  v1 = (signed __int64)((double)pParty->uLastRegenerationTime * 0.234375);
  v2 = (signed int)v1 / 60;
  result = (signed int)v1 / 60 + 5;
  if ( v0 >= result )
  {
    v51 = 0;
    v4 = (v0 - v2) / 5;
    if (pParty->FlyActive())
    {
      if ( pParty->bFlying )
      {
        if ( !(pParty->pPartyBuffs[PARTY_BUFF_FLY].uFlags & 1) )
        { // colliding with something in the air - fall down
          v5 = v4 * pParty->pPartyBuffs[PARTY_BUFF_FLY].uPower;
          //__debugbreak();
		  v6 = (int *)&pParty->pPlayers[pParty->pPartyBuffs[PARTY_BUFF_FLY].uCaster].pConditions[0];//&stru_AA1058[4].pSounds[6972 * pParty->pPartyBuffs[PARTY_BUFF_FLY].uCaster + 2000];
          v7 = *v6 < v5;
          *v6 -= v5;
          if ( v7 )
          {
            *v6 = 0;
            pParty->uFlags &= 0xFFFFFFBFu;
            pParty->bFlying = 0;
            v51 = 1;
          }
        }
      }
    }

    if (pParty->WaterWalkActive())
    {
      if (pParty->uFlags & PARTY_FLAGS_1_STANDING_ON_WATER )
      {
        if ( !(pParty->pPartyBuffs[PARTY_BUFF_WATER_WALK].uFlags & 1) )
        { // taking on water
          //__debugbreak();
          v8 = (int *)&pParty->pPlayers[pParty->pPartyBuffs[PARTY_BUFF_WATER_WALK].uCaster].pConditions[0];//&stru_AA1058[4].pSounds[6972 * pParty->pPartyBuffs[PARTY_BUFF_WATER_WALK].uCaster + 2000];
          v7 = *v8 < v4;
          *v8 -= v4;
          if ( v7 )
          {
            *v8 = 0;
            pParty->uFlags &= ~PARTY_FLAGS_1_STANDING_ON_WATER;
            v51 = 1;
          }
        }
      }
    }

    if (pParty->ImmolationActive())
    {
      //SpriteObject::SpriteObject(&a1);
      v9 = 0;
      a3.z = 0;
      a3.y = 0;
      a3.x = 0;
      a1.stru_24.Reset();
      a1.spell_level = pParty->pPartyBuffs[PARTY_BUFF_IMMOLATION].uPower;
      a1.spell_skill = pParty->ImmolationSkillLevel();
      v10 = 0;
      a1.uType = 1070;
      a1.spell_id = SPELL_FIRE_IMMOLATION;
      if ( (signed int)pObjectList->uNumObjects <= 0 )
      {
LABEL_19:
        LOWORD(v10) = 0;
      }
      else
      {
        v11 = &pObjectList->pObjects->uObjectID;
        while ( stru_4E3ACC[8].uType != *v11 )
        {
          ++v10;
          v11 += 28;
          if ( v10 >= (signed int)pObjectList->uNumObjects )
            goto LABEL_19;
        }
      }
      a1.uObjectDescID = v10;
      a1.field_60_distance_related_prolly_lod = 0;
      v12 = 8 * pParty->pPartyBuffs[PARTY_BUFF_IMMOLATION].uCaster;
      LOBYTE(v12) = v12 | OBJECT_Player;
      a1.uAttributes = 0;
      a1.uSectorID = 0;
      a1.uSpriteFrameID = 0;
      a1.spell_caster_pid = v12;
      a1.uFacing = 0;
      a1.uSoundID = 0;
      v13 = sub_46A89E((int)v41, 100, 307);
      if ( v13 > 0 )
      {
        do
        {
          v14 = (unsigned int *)&v41[4 * v9];
          v15 = *v14;
          v16 = *v14;
          a1.vPosition.x = pActors[v16].vPosition.x;
          v17 = pActors[v16].vPosition.y;
          a1.vPosition.z = pActors[v16].vPosition.z;
          v18 = 8 * v15;
          LOBYTE(v18) = PID(OBJECT_Actor,v15);
          a1.vPosition.y = v17;
          a1.spell_target_pid = v18;
          v19 = a1.Create(0, 0, 0, 0);
          DamageMonsterFromParty(PID(OBJECT_Item,v19), *v14, &a3);
          ++v9;
        }
        while ( v9 < v13 );
      }
    }
    v44 = 0;
    if (PartyHasDragon())
      v44 = 1;
    v49 = 0;
LABEL_25:
    v20 = 0;
    v21 = &pParty->pPlayers[v49];
    v50 = 0;
    v47 = 0;
    v45 = 0;
    v48 = 0;
    v46 = 0;
    if ( v44 && v21->classType == PLAYER_CLASS_WARLOCK )
      v50 = 1;
    if ( v21->classType == PLAYER_CLASS_LICH )
      v48 = 1;
    v22 = (ITEM_EQUIP_TYPE)0;
    while ( 1 )
    {
      if ( v21->HasItemEquipped(v22) )
      {
        //v23 = v21->pEquipment.pIndices;
        auto _idx = v21->pEquipment.pIndices[v22];
        v24 = v21->pInventoryItems[_idx].uItemID;
        if ( v24 > 134 )
        {
          if ( v24 == 529 )
            goto LABEL_43;
          if ( v24 == 535 )
            goto LABEL_44;
          if ( v24 == 515 )
          {
            v47 = 1;
          }
          else
          {
            if ( v24 == 532 )
              goto LABEL_50;
          }
        }
        else
        {
          v25 = v21->pInventoryItems[_idx].uSpecEnchantmentType;
          if ( v25 <= 50 )
          {
            if ( v25 != 50 )
            {
              v26 = v25 - 37;
              if ( v26 )
              {
                v27 = v26 - 1;
                if ( !v27 )
                  goto LABEL_50;
                v28 = v27 - 6;
                if ( v28 )
                {
                  if ( v28 != 3 )
                    goto LABEL_51;
LABEL_50:
                  v50 = 1;
                  goto LABEL_51;
                }
              }
            }
LABEL_44:
            v20 = 1;
            goto LABEL_51;
          }
          v29 = v25 - 54;
          if ( !v29 )
            goto LABEL_44;
          v30 = v29 - 1;
          if ( !v30 )
            goto LABEL_50;
          if ( v30 == 11 )
          {
LABEL_43:
            v20 = 1;
            v50 = 1;
            goto LABEL_51;
          }
        }
      }
LABEL_51:
      v22 = (ITEM_EQUIP_TYPE)((int)v22 + 1);
      if ( (signed int)v22 >= 16 )
      {
        if ( v48 )
        {
          v31 = 0;
          v32 = (char *)v21->pInventoryItems;
          while ( *(int *)v32 != 601 || (unsigned __int8)v32[26] != v49 + 1 )
          {
            ++v31;
            v32 += 36;
            if ( v31 >= 138 )
              goto LABEL_59;
          }
          v48 = 0;
          v45 = 1;
        }
LABEL_59:
        if ( v21->pConditions[17] )
          v46 = 1;
        if ( v20 && !v21->pConditions[14] && !v21->pConditions[16] )
        {
          ++v21->sHealth;
          if ( v21->sHealth > v21->GetMaxHealth() )
            v21->sHealth = v21->GetMaxHealth();
          if ( v21->pConditions[13] && v21->sHealth > 0 )
          {
            LODWORD(v21->pConditions[13]) = 0;
            HIDWORD(v21->pConditions[13]) = 0;
          }
          v51 = 1;
        }
        if ( SHIDWORD(v21->pPlayerBuffs[12].uExpireTime) >= 0
          && (SHIDWORD(v21->pPlayerBuffs[12].uExpireTime) > 0 || LODWORD(v21->pPlayerBuffs[12].uExpireTime))
          && !v21->pConditions[14]
          && !v21->pConditions[16] )
        {
          v21->sHealth += 5 * v21->pPlayerBuffs[12].uPower;
          if ( v21->sHealth > v21->GetMaxHealth() )
            v21->sHealth = v21->GetMaxHealth();
          if ( v21->pConditions[13] && v21->sHealth > 0 )
          {
            LODWORD(v21->pConditions[13]) = 0;
            HIDWORD(v21->pConditions[13]) = 0;
          }
          v51 = 1;
        }
        if ( v50 )
        {
          v33 = (char *)&v21->sMana;
          ++*(int *)v33;
          if ( v21->sMana > v21->GetMaxMana() )
            *(int *)v33 = v21->GetMaxMana();
          v51 = 1;
        }
        if ( v47 && !v21->pConditions[14] && !v21->pConditions[16] )
        {
          v34 = LODWORD(v21->pConditions[13]);
          --v21->sHealth;
          v35 = v21->sHealth;
          if ( !(HIDWORD(v21->pConditions[13]) | v34) && v35 < 0 )
            v21->pConditions[13] = pParty->uTimePlayed;
          if ( v35 < 1 )
          {
            if ( v21->sHealth + v21->uEndurance + v21->GetItemsBonus(CHARACTER_ATTRIBUTE_ENDURANCE, 0) >= 1
              || (signed __int64)v21->pPlayerBuffs[11].uExpireTime > 0 )
            {
              v21->pConditions[13] = pParty->uTimePlayed;
            }
            else
            {
              if ( !v21->pConditions[14] )
                v21->pConditions[14] = pParty->uTimePlayed;
            }
          }
          v51 = 1;
        }
        if ( v45 )
        {
          v36 = (char *)&v21->sMana;
          ++*(int *)v36;
          if ( v21->sMana > v21->GetMaxMana() )
            *(int *)v36 = v21->GetMaxMana();
        }
        if ( v48 && !v21->pConditions[14] && !v21->pConditions[16] )
        {
          v37 = v21->sHealth;
          if ( v37 > v21->GetMaxHealth() / 2 )
            v21->sHealth = v37 - 2;
          v38 = v21->sMana;
          if ( v38 > v21->GetMaxMana() / 2 )
            v21->sMana = v38 - 2;
        }
        if ( v46 && !v21->pConditions[14] && !v21->pConditions[16] )
        {
          v39 = v21->sHealth;
          if ( v39 > v21->GetMaxHealth() / 2 )
            v21->sHealth = v39 - 1;
          v40 = v21->sMana;
          if ( v40 > 0 )
            v21->sMana = v40 - 1;
        }
        ++v49;
        if ( v49 >= 4 )
        {
          result = HIDWORD(pParty->uTimePlayed);
          pParty->uLastRegenerationTime = pParty->uTimePlayed;
          if ( !viewparams->bRedrawGameUI )
          {
            result = v51;
            viewparams->bRedrawGameUI = v51;
          }
          return result;
        }
        goto LABEL_25;
      }
    }
  }
  return result;
}

//----- (00493F79) --------------------------------------------------------
void sub_493F79(stru351_summoned_item *_this, __int64 a2)
{
  signed __int64 v2; // ST2C_8@1
  signed __int64 v3; // qax@1
  //signed __int64 v4; // ST1C_8@1
  unsigned __int64 v5; // qax@1
  unsigned int v6; // ebx@1

  v2 = (signed __int64)((double)a2 * 0.234375);
  v3 = v2 / 60 / 60;
  //v4 = v3;
  v5 = (unsigned int)v3 / 0x18;
  v6 = (unsigned int)(v5 / 7) >> 2;
  _this->field_0_expire_second = v2 % 60;
  _this->field_4_expire_minute = v2 / 60 % 60;
  _this->field_8_expire_hour = v3 % 24;
  _this->field_10_expire_week = v5 / 7 & 3;
  _this->field_C_expire_day = (unsigned int)v5 % 0x1C;
  _this->field_14_exprie_month = v6 % 0xC;
  _this->field_18_expire_year = v6 / 0xC + game_starting_year;
}

//----- (00494035) --------------------------------------------------------
void _494035_timed_effects__water_walking_damage__etc()
{
  signed __int64 v0; // qax@1
  signed __int64 v1; // ST30_8@1
  signed __int64 v2; // ST38_8@1
  unsigned __int64 v3; // qax@1
  unsigned int v4; // edi@1
  //signed int v5; // eax@4
  //char *v6; // ecx@5
  //Player *v7; // esi@8
  //char *v8; // ecx@12
  //Player *pPlayer; // esi@15
  //void *v10; // esi@25
  unsigned int v11; // ecx@27
  signed int v12; // edi@29
  Player *v13; // ecx@30
  Player *v14; // esi@35
  double v15; // st7@35
  Player **v16; // esi@43
  Player *v17; // edi@44
  double v18; // st7@44
  //float v19; // ST28_4@48
  //double v20; // ST38_8@48
  Player *v21; // esi@51
  signed int v22; // edi@53
  int v23; // eax@59
  int v24; // ecx@60
  int v25; // eax@63
  int v26; // ecx@64
  int v27; // eax@67
  int v28; // ecx@68
  int v29; // eax@71
  int v30; // ecx@72
  int v31; // eax@75
  int v32; // ecx@76
  int v33; // eax@79
  int v34; // ecx@80
  int v35; // eax@83
  int v36; // ecx@84
  int v37; // eax@87
  int v38; // ecx@88
  int v39; // eax@91
  int v40; // ecx@92
  int v41; // eax@95
  int v42; // ecx@96
  bool v43; // ebx@102
  //SpellBuff *v44; // edi@104
  //signed int v45; // ebp@104
  bool v46; // edi@111
  //SpellBuff *v47; // esi@113
  //Player **v48; // esi@119
  //signed int v49; // edi@121
  //char *v50; // esi@122
  signed int v51; // edx@128
  signed int v52; // ecx@130
  int v53; // eax@131
  Player *v54; // eax@141
  //unsigned int v55; // [sp-8h] [bp-38h]@18
  unsigned int v56; // [sp-8h] [bp-38h]@55
  //int v57; // [sp-4h] [bp-34h]@18
  int v58; // [sp-4h] [bp-34h]@33
  int v59; // [sp-4h] [bp-34h]@55
  //unsigned int v60; // [sp+10h] [bp-20h]@1
  unsigned int v61; // [sp+14h] [bp-1Ch]@1
  Player **v62; // [sp+14h] [bp-1Ch]@50
  //unsigned int a2; // [sp+18h] [bp-18h]@1
  signed int a2a; // [sp+18h] [bp-18h]@47
  signed int v65; // [sp+1Ch] [bp-14h]@47

  //a2 = pParty->uCurrentHour;
  v61 = pParty->uDaysPlayed;
  //auto prev_time = pEventTimer->uTimeElapsed;
  pParty->uTimePlayed += pEventTimer->uTimeElapsed;
  v0 = (signed __int64)(pParty->uTimePlayed * 0.234375) / 60;
  v1 = v0;
  v0 /= 60i64;
  v2 = v0;
  v3 = (unsigned int)v0 / 24;
  v4 = (unsigned int)(v3 / 7) >> 2;
  pParty->uCurrentTimeSecond = (signed __int64)((double)(signed __int64)pParty->uTimePlayed * 0.234375) % 60;
  pParty->uCurrentMinute = v1 % 60;
  pParty->uCurrentMonthWeek = v3 / 7 & 3;
  pParty->uCurrentHour = v2 % 24;
  pParty->uDaysPlayed = (unsigned int)v3 % 28;
  pParty->uCurrentMonth = v4 % 12;
  pParty->uCurrentYear = v4 / 0xC + game_starting_year;
  if ( pParty->uCurrentHour >= 3 && (pParty->uCurrentHour < 3 || pParty->uDaysPlayed > v61) ) // new day dawns
  {
    pParty->pHirelings[0].bHasUsedTheAbility = false;
    pParty->pHirelings[1].bHasUsedTheAbility = false;

    for (uint i = 0; i < pNPCStats->uNumNewNPCs; ++i)
      pNPCStats->pNewNPCData[i].bHasUsedTheAbility = false;

    ++pParty->days_played_without_rest;
    if (pParty->days_played_without_rest > 1)
    {
      for (uint i = 0; i < 4; ++i)
        pParty->pPlayers[i].SetCondition(Player::Condition_Weak, 0);

      if (pParty->uNumFoodRations)
        Party::TakeFood(1);
      else
        for (uint i = 0; i < 4; ++i)
          pParty->pPlayers[i].sHealth = pParty->pPlayers[i].sHealth / (pParty->days_played_without_rest + 1) + 1;

      if (pParty->days_played_without_rest > 3)
        for (uint i = 0; i < 4; ++i)
        {
          auto player = pParty->pPlayers + i;

          player->Zero();

          if (!player->Pertified() && !player->Eradicated() && !player->Dead())
          {
            if (rand() % 100 < 5 * pParty->days_played_without_rest)
              player->SetCondition(Player::Condition_Dead, 0);
            if (rand() % 100 < 10 * pParty->days_played_without_rest)
              player->SetCondition(Player::Condition_Insane, 0);
          }
        }
    }
    if (uCurrentlyLoadedLevelType == LEVEL_Outdoor)
      pOutdoor->SetFog();

    for (uint i = 0; i < 4; ++i)
      pParty->pPlayers[i].uNumDivineInterventionCastsThisDay = 0;
  }

  v11 = LODWORD(pParty->uTimePlayed);
  if ( pParty->uFlags & 4 && pParty->field_6FC < (signed __int64)pParty->uTimePlayed )
  {
    v12 = 1;
    pParty->field_6FC = LODWORD(pParty->uTimePlayed) + 128;
    viewparams->bRedrawGameUI = 1;
    while ( 1 )
    {
      if ( pPlayers[v12]->WearsItem(ITEM_RELIC_HARECS_LEATHER, EQUIP_ARMOUR)
        || pPlayers[v12]->HasEnchantedItemEquipped(71)
        || pPlayers[v12]->pPlayerBuffs[23].uExpireTime > 0 )
      {
        v58 = 0;
      }
      else
      {
        v58 = 0;
        if ( !sub_43EE77_ProbablyIfUnderwaterSuitIsEquipped(v12) )
        {
          v14 = pPlayers[v12];
          v15 = (double)pPlayers[v12]->GetMaxHealth() * 0.1;
          v14->ReceiveDamage((signed __int64)v15, DMGT_FIRE);
          if ( pParty->uFlags & 4 )
          {
            strcpy(GameUI_Footer_TimedString.data(), pGlobalTXT_LocalizationStrings[660]);
            GameUI_Footer_TimeLeft = 128;
          }
          goto LABEL_39;
        }
        v13 = pPlayers[v12];
      }
      v13->PlayEmotion(CHARACTER_EXPRESSION_37, v58);
LABEL_39:
      ++v12;
      if ( v12 > 4 )
      {
        v11 = LODWORD(pParty->uTimePlayed);
        break;
      }
    }
  }
  if ( pParty->uFlags & 0x200 && pParty->field_6FC < (signed __int64)__PAIR__(HIDWORD(pParty->uTimePlayed), v11) )
  {
    viewparams->bRedrawGameUI = 1;
    pParty->field_6FC = v11 + 128;
    v16 = &pPlayers[1];
    do
    {
      v17 = *v16;
      v18 = (double)(*v16)->GetMaxHealth() * 0.1;
      v17->ReceiveDamage((signed __int64)v18, DMGT_FIRE);
      if ( pParty->uFlags & 0x200 )
      {
        strcpy(GameUI_Footer_TimedString.data(), pGlobalTXT_LocalizationStrings[661]);
        GameUI_Footer_TimeLeft = 128;
      }
      ++v16;
    }
    while ( (signed int)v16 <= (signed int)&pPlayers[4] );
  }
  _493938_regenerate();
  v65 = 4;
  a2a = pEventTimer->uTimeElapsed;
  if ( pParty->uFlags2 & PARTY_FLAGS_2_RUNNING )
  {
    //v19 = (double)(signed int)prev_time * 0.5;
    //v20 = v19 + 6.7553994e15;
    a2a *= 0.5f;
    if (a2a < 1)
      a2a = 1;
  }
  v62 = &pPlayers[1];
  do
  {
    v21 = *v62;
    if ( (*v62)->uTimeToRecovery )
      v21->Recover(a2a);
    v22 = v21->sHealth;
    if ( v21->GetItemsBonus(CHARACTER_ATTRIBUTE_ENDURANCE, 0) + v22 + v21->uEndurance >= 1
      || (signed __int64)v21->pPlayerBuffs[11].uExpireTime > 0 )
    {
      if ( v22 >= 1 )
        goto LABEL_59;
      v59 = 0;
      v56 = 13;
    }
    else
    {
      v59 = 0;
      v56 = 14;
    }
    v21->SetCondition(v56, v59);
LABEL_59:
    v23 = (int)&v21->field_E0;
    if ( v21->field_E0 )
    {
      v24 = *(int *)v23 - pEventTimer->uTimeElapsed;
      if ( v24 > 0 )
      {
        *(int *)v23 = v24;
      }
      else
      {
        *(int *)v23 = 0;
        viewparams->bRedrawGameUI = 1;
      }
    }
    v25 = (int)&v21->field_E4;
    if ( v21->field_E4 )
    {
      v26 = *(int *)v25 - pEventTimer->uTimeElapsed;
      if ( v26 > 0 )
      {
        *(int *)v25 = v26;
      }
      else
      {
        *(int *)v25 = 0;
        viewparams->bRedrawGameUI = 1;
      }
    }
    v27 = (int)&v21->field_E8;
    if ( v21->field_E8 )
    {
      v28 = *(int *)v27 - pEventTimer->uTimeElapsed;
      if ( v28 > 0 )
      {
        *(int *)v27 = v28;
      }
      else
      {
        *(int *)v27 = 0;
        viewparams->bRedrawGameUI = 1;
      }
    }
    v29 = (int)&v21->field_EC;
    if ( v21->field_EC )
    {
      v30 = *(int *)v29 - pEventTimer->uTimeElapsed;
      if ( v30 > 0 )
      {
        *(int *)v29 = v30;
      }
      else
      {
        *(int *)v29 = 0;
        viewparams->bRedrawGameUI = 1;
      }
    }
    v31 = (int)&v21->field_F0;
    if ( v21->field_F0 )
    {
      v32 = *(int *)v31 - pEventTimer->uTimeElapsed;
      if ( v32 > 0 )
      {
        *(int *)v31 = v32;
      }
      else
      {
        *(int *)v31 = 0;
        viewparams->bRedrawGameUI = 1;
      }
    }
    v33 = (int)&v21->field_F4;
    if ( v21->field_F4 )
    {
      v34 = *(int *)v33 - pEventTimer->uTimeElapsed;
      if ( v34 > 0 )
      {
        *(int *)v33 = v34;
      }
      else
      {
        *(int *)v33 = 0;
        viewparams->bRedrawGameUI = 1;
      }
    }
    v35 = (int)&v21->field_F8;
    if ( v21->field_F8 )
    {
      v36 = *(int *)v35 - pEventTimer->uTimeElapsed;
      if ( v36 > 0 )
      {
        *(int *)v35 = v36;
      }
      else
      {
        *(int *)v35 = 0;
        viewparams->bRedrawGameUI = 1;
      }
    }
    v37 = (int)&v21->field_FC;
    if ( v21->field_FC )
    {
      v38 = *(int *)v37 - pEventTimer->uTimeElapsed;
      if ( v38 > 0 )
      {
        *(int *)v37 = v38;
      }
      else
      {
        *(int *)v37 = 0;
        viewparams->bRedrawGameUI = 1;
      }
    }
    v39 = (int)&v21->field_100;
    if ( v21->field_100 )
    {
      v40 = *(int *)v39 - pEventTimer->uTimeElapsed;
      if ( v40 > 0 )
      {
        *(int *)v39 = v40;
      }
      else
      {
        *(int *)v39 = 0;
        viewparams->bRedrawGameUI = 1;
      }
    }
    v41 = (int)&v21->field_104;
    if ( v21->field_104 )
    {
      v42 = *(int *)v41 - pEventTimer->uTimeElapsed;
      if ( v42 > 0 )
      {
        *(int *)v41 = v42;
      }
      else
      {
        *(int *)v41 = 0;
        viewparams->bRedrawGameUI = 1;
      }
    }
    if ( v21->pConditions[2] | v21->pConditions[12] | v21->pConditions[13] | v21->pConditions[14] | v21->pConditions[15] | v21->pConditions[16] )
      --v65;
    v43 = (signed __int64)v21->pPlayerBuffs[7].uExpireTime > 0;

    for (uint k = 0; k < 24; ++k)
      v21->pPlayerBuffs[k].IsBuffExpiredToTime(pParty->uTimePlayed);

    if ( v43 && (signed __int64)v21->pPlayerBuffs[7].uExpireTime <= 0 )
      v21->SetCondition(1u, 0);
    ++v62;
  }
  while ( (signed int)v62 <= (signed int)&pPlayers[4] );
  v46 = (signed __int64)pParty->pPartyBuffs[PARTY_BUFF_HASTE].uExpireTime > 0;

  for (uint i = 0; i < 20; ++i)
  {
    if (pParty->pPartyBuffs[i].IsBuffExpiredToTime(pParty->uTimePlayed) == 1)
      viewparams->bRedrawGameUI = 1;
  }

  if ( v46 && (signed __int64)pParty->pPartyBuffs[PARTY_BUFF_HASTE].uExpireTime <= 0 )
  {
    for (uint i = 0; i < 4; ++i)
      pParty->pPlayers[i].SetCondition(1, 0);
  }

  for (uint i = 0; i < 2; ++i)
  {
    auto pBuf = &pParty->pPartyBuffs[dword_4EE07C[i]];
    if (pBuf->uExpireTime == 0)
      continue;

    if ( !(pBuf->uFlags & 1) )
    {
      if (!pPlayers[pBuf->uCaster]->CanAct())
      {
        pBuf->Reset();
        if (dword_4EE07C[i] == 7 )
          pParty->bFlying = false;
      }
    }
  }

  v51 = v65;
  if ( v65 )
    goto LABEL_135;
  if ( pCurrentScreen != SCREEN_REST )
  {
    v52 = (signed int)&pPlayers[1];
    while ( 1 )
    {
      v53 = *(int *)v52;
      if ( *(_QWORD *)(*(int *)v52 + 16) )
        break;
      v52 += 4;
      if ( v52 > (signed int)&pPlayers[4] )
        goto LABEL_135;
    }
    *(int *)(v53 + 16) = 0;
    *(int *)(v53 + 20) = 0;
    v51 = 1;
LABEL_135:
    if ( pCurrentScreen != SCREEN_REST
      && (!v51 || dword_5C35C0) )
      uGameState = GAME_STATE_PARTY_DIED;
  }
  if ( uActiveCharacter )
  {
    if ( pCurrentScreen != SCREEN_REST )
    {
      v54 = pPlayers[uActiveCharacter];
      if ( v54->pConditions[2]
        || v54->pConditions[12]
        || v54->pConditions[13]
        || v54->pConditions[14]
        || v54->pConditions[15]
        || v54->pConditions[16] )
      {
        viewparams->bRedrawGameUI = 1;
        uActiveCharacter = pParty->GetNextActiveCharacter();
      }
    }
  }
}

//----- (00494820) --------------------------------------------------------
unsigned int __fastcall _494820_training_time(unsigned int a1)
{
  signed int v1; // eax@1

  v1 = 5;
  if ( a1 % 24 >= 5 )
    v1 = 29;
  return v1 - a1 % 24;
}

//----- (00494836) --------------------------------------------------------
int stru339_spell_sound::_494836(int uSoundID, int a6)
{
  int v3; // esi@1
  int result; // eax@1
  stru339_spell_sound *v5; // ebx@1
  int *v6; // edi@2
  unsigned int v7; // eax@3
  int v8; // [sp+Ch] [bp-8h]@3
  int v9; // [sp+10h] [bp-4h]@2
  int a2a; // [sp+1Ch] [bp+8h]@1

  v3 = 0;
  result = word_4EE088_sound_ids[uSoundID];
  v5 = this;
  a2a = word_4EE088_sound_ids[uSoundID];
  if ( result )
  {
    v9 = 0;
    v6 = this->pSoundsOffsets;
    do
    {
      v7 = a2a++;
      result = pSoundList->LoadSound(v7, (char *)v5 + v3, 44744 - v3, &v8, a6);
      if ( !result )
        break;
      a6 += 4;
      result = v8 + 256;
      *v6 = v3;
      v3 += result;
      ++v9;
      *(v6 - 2) = result;
      ++v6;
    }
    while ( v9 < 2 );
  }
  return result;
}
// 4EE088: using guessed type __int16 word_4EE088_sound_ids[];

//----- (00494AED) --------------------------------------------------------
unsigned int PlayerFrameTable::GetFrameIdByExpression(CHARACTER_EXPRESSION_ID expression)
{
  unsigned int _uNumFrames; // edx@1
  unsigned int result; // eax@1
  PlayerFrame *v4; // ecx@2

  _uNumFrames = this->uNumFrames;
  result = 0;
  if ( (signed int)this->uNumFrames <= 0 )
  {
LABEL_5:
    result = 0;
  }
  else
  {
    v4 = this->pFrames;
    while ( v4->expression != expression )
    {
      ++result;
      ++v4;
      if ( (signed int)result >= (signed int)_uNumFrames )
        goto LABEL_5;
    }
  }
  return result;
}

//----- (00494B10) --------------------------------------------------------
PlayerFrame *PlayerFrameTable::GetFrameBy_x(unsigned int uFramesetID, unsigned int uFrameID)
{
  unsigned int v3; // esi@1
  PlayerFrame *v4; // edi@1
  PlayerFrame *v5; // ecx@1
  __int16 v6; // dx@2
  int v7; // edx@3
  char *i; // eax@3
  int v9; // ecx@5
  PlayerFrame *result; // eax@6

  v3 = uFramesetID;
  v4 = this->pFrames;
  v5 = &v4[uFramesetID];
  if ( v5->uFlags & 1 && (v6 = v5->uAnimLength) != 0 )
  {
    v7 = ((signed int)uFrameID >> 3) % (unsigned __int16)v6;
    for ( i = (char *)&v5->uAnimTime; ; i += 10 )
    {
      v9 = *(short *)i;
      if ( v7 <= v9 )
        break;
      v7 -= v9;
      ++v3;
    }
    result = &v4[v3];
  }
  else
  {
    result = &v4[uFramesetID];
  }
  return result;
}

//----- (00494B5E) --------------------------------------------------------
PlayerFrame *PlayerFrameTable::GetFrameBy_y(int *pFramesetID, int *pAnimTime, int a4)
{
  PlayerFrameTable *v4; // edi@1
  int v5; // esi@1
  int v6; // eax@2

  v4 = this;
  v5 = a4 + *pAnimTime;
  if ( v5 < 8 * this->pFrames[*pFramesetID].uAnimTime )
  {
    *pAnimTime = v5;
  }
  else
  {
    v6 = rand() % 4 + 21;
    *pFramesetID = v6;
    *pAnimTime = 8 * v5 % v4->pFrames[v6].uAnimTime;
  }
  return &v4->pFrames[*pFramesetID];
}

//----- (00494BC3) --------------------------------------------------------
void PlayerFrameTable::ToFile()
{
  PlayerFrameTable *v1; // esi@1
  FILE *v2; // eax@1
  FILE *v3; // edi@1

  auto Str = this;

  v1 = Str;
  v2 = fopen("data\\dpft.bin", "wb");
  v3 = v2;
  if ( !v2 )
    Abortf("Unable to save dpft.bin!");
  fwrite(v1, 4u, 1u, v2);
  fwrite(v1->pFrames, 0xAu, v1->uNumFrames, v3);
  fclose(v3);
}

//----- (00494C0F) --------------------------------------------------------
void PlayerFrameTable::FromFile(void *pSerialized)
{
  uNumFrames = *(int *)pSerialized;
  pFrames = (PlayerFrame *)pAllocator->AllocNamedChunk(pFrames, 10 * uNumFrames, "P Frames");
  memcpy(pFrames, (char *)pSerialized + 4, 10 * uNumFrames);
}

//----- (00494C5A) --------------------------------------------------------
int PlayerFrameTable::FromFileTxt(const char *Args)
{
  PlayerFrameTable *v2; // ebx@1
  FILE *v3; // eax@1
  int v4; // esi@3
  void *v5; // eax@10
  FILE *v6; // ST0C_4@12
  char *i; // eax@12
  __int16 v8; // ax@15
  const char *v9; // ST10_4@15
  unsigned __int16 v10; // ax@15
  const char *v11; // ST0C_4@15
  int j; // esi@15
  int v13; // eax@17
  int v14; // edx@22
  int v15; // ecx@23
  int v16; // eax@24
  signed int k; // eax@27
  PlayerFrame *v18; // edx@28
  int v19; // esi@28
  int l; // ecx@29
  char Buf; // [sp+Ch] [bp-2F8h]@3
  FrameTableTxtLine v23; // [sp+200h] [bp-104h]@4
  FrameTableTxtLine v24; // [sp+27Ch] [bp-88h]@4
  int v25; // [sp+2F8h] [bp-Ch]@3
  int v26; // [sp+2FCh] [bp-8h]@3
  FILE *File; // [sp+300h] [bp-4h]@1
  int Argsa; // [sp+30Ch] [bp+8h]@28

  v2 = this;
  //TileTable::dtor((TileTable *)this);
  v3 = fopen(Args, "r");
  File = v3;
  if ( !v3 )
    Abortf("PlayerFrameTable::load - Unable to open file: %s.", Args);
  v4 = 0;
  v25 = 0;
  v26 = 1;
  if ( fgets(&Buf, 490, v3) )
  {
    do
    {
      *strchr(&Buf, 10) = 0;
      memcpy(&v24, txt_file_frametable_parser(&Buf, &v23), sizeof(v24));
      if ( v24.uPropCount && *v24.pProperties[0] != 47 )
      {
        if ( v24.uPropCount < 3 )
          Abortf("PlayerFrameTable::load, too few arguments, %s line %i.", Args, v26);
        ++v25;
      }
      ++v26;
    }
    while ( fgets(&Buf, 490, File) );
    v4 = v25;
  }
  v2->uNumFrames = v4;
  v5 = pAllocator->AllocNamedChunk(v2->pFrames, 10 * v4, "P Frames");
  v2->pFrames = (PlayerFrame *)v5;
  if ( !v5 )
    Abortf("PlayerFrameTable::load - Out of Memory!");
  v6 = File;
  v2->uNumFrames = 0;
  fseek(v6, 0, 0);
  for ( i = fgets(&Buf, 490, File); i; i = fgets(&Buf, 490, File) )
  {
    *strchr(&Buf, 10) = 0;
    memcpy(&v24, txt_file_frametable_parser(&Buf, &v23), sizeof(v24));
    if ( v24.uPropCount && *v24.pProperties[0] != 47 )
    {
      v8 = atoi(v24.pProperties[0]);
      v9 = v24.pProperties[1];
      v2->pFrames[v2->uNumFrames].expression = (CHARACTER_EXPRESSION_ID)v8;
      v10 = atoi(v9);
      v11 = v24.pProperties[2];
      v2->pFrames[v2->uNumFrames].uTextureID = v10;
      v2->pFrames[v2->uNumFrames].uAnimTime = atoi(v11);
      v2->pFrames[v2->uNumFrames].uAnimLength = 0;
      v2->pFrames[v2->uNumFrames].uFlags = 0;
      for ( j = 3; j < v24.uPropCount; ++j )
      {
        if ( !_stricmp(v24.pProperties[j], "New") )
        {
          v13 = (int)&v2->pFrames[v2->uNumFrames].uFlags;
          *(char *)v13 |= 4u;
        }
      }
      ++v2->uNumFrames;
    }
  }
  fclose(File);
  v14 = 0;
  if ( (signed int)(v2->uNumFrames - 1) > 0 )
  {
    v15 = 0;
    do
    {
      v16 = (int)&v2->pFrames[v15];
      if ( !(*(char *)(v16 + 18) & 4) )
        *(char *)(v16 + 8) |= 1u;
      ++v14;
      ++v15;
    }
    while ( v14 < (signed int)(v2->uNumFrames - 1) );
  }
  for ( k = 0; k < (signed int)v2->uNumFrames; *(short *)(Argsa + 6) = v19 )
  {
    v18 = v2->pFrames;
    Argsa = (int)&v18[k];
    v19 = *(short *)(Argsa + 4);
    if ( *(char *)(Argsa + 8) & 1 )
    {
      ++k;
      for ( l = (int)&v18[k]; *(char *)(l + 8) & 1; l += 10 )
      {
        v19 += *(short *)(l + 4);
        ++k;
      }
      LOWORD(v19) = v18[k].uAnimTime + v19;
    }
    ++k;
  }
  return 1;
}

//----- (00495366) --------------------------------------------------------
char *__fastcall sub_495366(unsigned __int8 a1, unsigned __int8 a2)
{
  int v2; // edi@1
  int v3; // edx@2
  int v4; // esi@3
  int v5; // ebx@5
  signed int v7; // [sp+Ch] [bp-14h]@1
  signed int v8; // [sp+10h] [bp-10h]@1
  int **v9; // [sp+14h] [bp-Ch]@4
  signed int v10; // [sp+18h] [bp-8h]@3
  unsigned __int8 v11; // [sp+1Ch] [bp-4h]@1

  v2 = a1;
  v11 = a2;
  v8 = 0;
  v7 = 0;
  if ( dword_AE336C == a1 )
  {
    v3 = dword_AE3370;
  }
  else
  {
    v4 = a2;
    dword_AE336C = a1;
    v10 = 0;
    if ( (signed int)pNPCStats->uNumNPCNames[v4] <= 0 )
      goto LABEL_17;
    v9 = (int **)((char *)pNPCStats->pNPCNames + v4 * 4);
    do
    {
      v5 = tolower(*(char *)*v9);
      if ( v5 == tolower(v2) )
      {
        if ( v8 )
          v7 = v10;
        else
          v8 = v10;
      }
      ++v10;
      v9 += 2;
    }
    while ( v10 < (signed int)pNPCStats->uNumNPCNames[v4] );
    if ( v8 && v8 != v7 )
      v3 = v8 + rand() % (v7 - v8);
    else
LABEL_17:
      v3 = rand() % (signed int)pNPCStats->uNumNPCNames[v4];
  }
  dword_AE3370 = v3;
  return pNPCStats->pNPCNames[0][v11 + 2 * v3];
}
// AE336C: using guessed type int dword_AE336C;
// AE3370: using guessed type int dword_AE3370;

//----- (00495430) --------------------------------------------------------
char * GetReputationString( signed int a1 )
    {
  if (a1 >= 25)
    return pGlobalTXT_LocalizationStrings[379]; // Hated
  else if (a1 >= 6)
    return pGlobalTXT_LocalizationStrings[392]; // Unfriendly
  else if (a1 >= -5)
    return pGlobalTXT_LocalizationStrings[399]; // Neutral;
  else if (a1 >= -24)
    return pGlobalTXT_LocalizationStrings[402]; // Friendly
  else
    return pGlobalTXT_LocalizationStrings[434]; // Respected;
}

//----- (00495461) --------------------------------------------------------
char *BuilDialogueString(char *lpsz, unsigned __int8 uPlayerID, ItemGen *a3, char *a4, int a5, __int64 *a6)
{
  unsigned __int8 v6; // bl@1
  Player *pPlayer; // ebx@3
  //NPCData *v9; // eax@4
  //size_t v10; // eax@6
  char *v11; // esi@7
  //int v12; // edx@7
  //ItemGen *v13; // edi@7
  //char v14; // cl@8
  //char *v15; // ecx@10
  //int v16; // edx@10
  int v17; // eax@10
  signed __int64 v18; // qax@18
  unsigned int v19; // edi@32
  unsigned __int8 *v20; // ebx@32
  int v21; // ecx@34
  __int16 v22; // ax@34
  int v23; // edx@39
  int v24; // eax@39
  int v25; // eax@45
  float *v26; // esi@68
  float v27; // ST18_4@68
  signed int v28; // eax@68
  int v29; // eax@68
  float v30; // ST18_4@72
  signed int v31; // eax@72
  float v32; // ST18_4@74
  signed int v33; // eax@74
  double v34; // st7@75
  float v35; // ST18_4@76
  signed int v36; // eax@76
  float *v37; // esi@78
  float v38; // ST18_4@78
  int v39; // eax@78
  float v40; // ST18_4@82
  int v41; // eax@82
  float v42; // ST18_4@83
  int v43; // eax@83
  float v44; // ST18_4@85
  int v45; // eax@85
  const char *v46; // eax@86
  unsigned int v47; // eax@87
  float v48; // ST18_4@97
  __int64 v49; // ST14_8@107
  int v50; // eax@107
  int v51; // eax@108
  Player *v52; // eax@109
  int v53; // [sp-4h] [bp-13Ch]@107
  int v54; // [sp+0h] [bp-138h]@107
  __int16 v55[56]; // [sp+10h] [bp-128h]@34
  stru351_summoned_item v56; // [sp+80h] [bp-B8h]@107
  stru351_summoned_item v57; // [sp+9Ch] [bp-9Ch]@115
  char a1[100]; // [sp+B8h] [bp-80h]@3
  unsigned int v59; // [sp+11Ch] [bp-1Ch]@3
  //size_t v60; // [sp+120h] [bp-18h]@3
  //Player *v61; // [sp+124h] [bp-14h]@3
  //char *Str; // [sp+128h] [bp-10h]@1
  int v63; // [sp+12Ch] [bp-Ch]@32
  //int v64; // [sp+130h] [bp-8h]@6
  //NPCData *v65; // [sp+134h] [bp-4h]@6
  //int packed;



  v6 = uPlayerID;
  //Str = lpsz;
  if ( IsBadStringPtrA(lpsz, 1u) )
    return "Invalid String Passed";

  a1[0] = 0;
  pPlayer = &pParty->pPlayers[v6];
  v59 = TargetColor(255, 255, 155);
  //v61 = pPlayer;
  memset(pTmpBuf2.data(), 0, sizeof(pTmpBuf2));

  NPCData *npc = nullptr;
  if ( dword_5C35D4 )
  {
    //__debugbreak(); // fix  
    npc = HouseNPCData[(unsigned int)((char *)pDialogueNPCCount + -(dword_591080 != 0))]; //- 1
  }
  else
    npc = GetNPCData(sDialogue_SpeakingActorNPC_ID);
  //v65 = v9;
  //v10 = 0;
  //v64 = 0;

  //int i = 0;
  //if ( (signed int)v60 > 0 )
  v11 = a4;

  auto len = strlen(lpsz);
  for (int i = 0, dst = 0; i < len; ++i)
  {
    //v12 = v64;
    //v13 = a3;
    //while ( 1 )
    //{
    char c = lpsz[i];
    if (c != '%')
      pTmpBuf2[dst++] = c;
    else
    {
        //v15 = &lpsz[v12 + 1];
        //v16 = (unsigned __int8)lpsz[v12 + 2];
        //packed = 10 * (int)lpsz[v12 + 1] + lpsz[v12 + 2];
        //v17 = packed - 528;// 10 * '0' + '0'
      v17 = 10 * (int)(lpsz[i + 1] - '0') + lpsz[i + 2] - '0';

      switch ( v17 )
      {
        case 1:
          v11 = npc->pName;
        goto _continue_strcat;
          case 2:
            v11 = pPlayer->pName;
            goto _continue_strcat;
          case 3:
          case 4:
            v11 = a1;
            goto _continue_strcat;
          case 5:
            v18 = (signed __int64)((double)(signed __int64)pParty->uTimePlayed * 0.234375) / 60 / 60 % 24;
            v11 = pGlobalTXT_LocalizationStrings[397];// "evening"
            if ( SHIDWORD(v18) <= 0 && SHIDWORD(v18) >= 0 && (unsigned int)v18 >= 5 && SHIDWORD(v18) <= 0 )
            {
              if ( SHIDWORD(v18) >= 0 && (unsigned int)v18 >= 0xB )
              {
                if ( v18 < 20 )
                  v11 = pGlobalTXT_LocalizationStrings[396];// "day"
              }
              else
              {
                v11 = pGlobalTXT_LocalizationStrings[395];// "morning"
              }
            }
            goto _continue_strcat;
          case 6:
            if ( pPlayer->uSex )
              v11 = pGlobalTXT_LocalizationStrings[387];// "lady"
            else
              v11 = pGlobalTXT_LocalizationStrings[385];// "sir"
            goto _continue_strcat;
          case 7:
            if ( pPlayer->uSex )
              v11 = pGlobalTXT_LocalizationStrings[389];// "Lady"
            else
              v11 = pGlobalTXT_LocalizationStrings[386];// "Sir"
            goto _continue_strcat;
          case 8:
            v19 = 0;
            v63 = 0;
            v20 = (unsigned __int8 *)pPlayer->_guilds_member_bits;
            do
            {
              if ( (unsigned __int16)_449B57_test_bit(v20, word_4EE150[v19]) )
              {
                v21 = v63;
                v22 = word_4EE150[v19];
                ++v63;
                v55[v21] = v22;
              }
              ++v19;
            }
            while ( v19 < 28 );
            if ( v63 )
            {
              v23 = rand() % v63;
              v24 = dword_A74CDC;
              if ( dword_A74CDC == -1 )
              {
                v24 = v23;
                dword_A74CDC = v23;
              }
              v11 = (char *)pAwards[v55[v24]].pText;//(char *)dword_723E80_award_related[2 * v55[v24]];
            }
            else
            {
              v11 = (char *)pNPCTopics[55].pText;
            }
            //pPlayer = v61;
            //v13 = a3;
            goto _continue_strcat;
          case 9:
            if ( npc->uSex )
              v11 = pGlobalTXT_LocalizationStrings[384];// "her"
            else
              v11 = pGlobalTXT_LocalizationStrings[383];// "his"
            goto _continue_strcat;
          case 10:
            if ( pPlayer->uSex )
              v11 = pGlobalTXT_LocalizationStrings[389];// "Lady"
            else
              v11 = pGlobalTXT_LocalizationStrings[388];// "Lord"
            goto _continue_strcat;
          case 11:
            v25 = GetPartyReputation();
            goto LABEL_46;
          case 12:
            v25 = npc->rep;
LABEL_46:
            if ( v25 >= 25 )
            {
              v11 = pGlobalTXT_LocalizationStrings[379];
            }
            else
            {
              if ( v25 < 6 )
              {
                if ( v25 >= -5 )
                {
                  v11 = pGlobalTXT_LocalizationStrings[399];
                }
                else
                {
                  if ( v25 < -24 )
                    v11 = pGlobalTXT_LocalizationStrings[434];
                  else
                    v11 = pGlobalTXT_LocalizationStrings[402];
                }
              }
              else
              {
                v11 = pGlobalTXT_LocalizationStrings[392];
              }
            }
            goto _continue_strcat;
          case 13:
            v11 = sub_495366(pPlayer->pName[0], pPlayer->uSex);
            goto _continue_strcat;
          case 14:
            {
              if ( npc->uSex )
                v11 = pGlobalTXT_LocalizationStrings[391];// "sister"
              else
                v11 = pGlobalTXT_LocalizationStrings[390];// "brother"
              goto _continue_strcat;
            }

          case 15:
          {
            v11 = pGlobalTXT_LocalizationStrings[393];// "daughter"
_continue_strcat:
            strcat(pTmpBuf2.data(), v11);
            dst = strlen(pTmpBuf2.data());
            //v64 += 2;
            i += 2;
          }
          break;

          case 16:
          {
            if ( npc->uSex )
              v11 = pGlobalTXT_LocalizationStrings[391];// "sister"
            else
              v11 = pGlobalTXT_LocalizationStrings[390];// "brother"
            goto _continue_strcat;
          }
          case 17:
          {
            auto pay_percentage = pNPCStats->pProfessions[npc->uProfession - 1].uHirePrice / 100;
            if (!pay_percentage)
              pay_percentage = 1;

            sprintf(a1, "%lu", pay_percentage);
            v11 = a1;
            goto _continue_strcat;
          }

          case 18:
          case 19:
          case 20:
          case 21:
          case 22:
          case 26:
_continue_strncpy:
            strncpy(a1, lpsz + i + 1, 2);
          v51 = atoi(a1);
          sprintf(a1, "%lu", v51);
          v11 = a1;
          goto _continue_strcat;

          case 23:
          {
            v47 = pMapStats->GetMapInfo(pCurrentMapName.data());
            if ( v47 )
              v11 = pMapStats->pInfos[v47].pName;
            else
              v11 = pGlobalTXT_LocalizationStrings[394];// "Unknown"
            goto _continue_strcat;
          }
          case 24:
          {
            v46 = a3->GetDisplayName();
            sprintf(a1, format_4E2D80, v59, v46);
            v11 = a1;
            goto _continue_strcat;
          }


          case 25:
          {
            v37 = &p2DEvents[(signed int)a4 - 1].fPriceMultiplier;
            v38 = *v37;
            v39 = a3->GetValue();
            v29 = pPlayer->GetBaseBuyingPrice(v39, v38);
          switch ( a5 )
          {
            case 3:
              v44 = *v37;
              v45 = a3->GetValue();
              v29 = pPlayer->GetBaseSellingPrice(v45, v44);
              break;
            case 4:
              v29 = pPlayer->GetBaseIdentifyPrice(*v37);
              break;
            case 5:
              v42 = *v37;
              v43 = a3->GetValue();
              v29 = pPlayer->GetBaseRepairPrice(v43, v42);
              break;
            case 6:
              v40 = *v37;
              v41 = a3->GetValue();
              v29 = pPlayer->GetBaseSellingPrice(v41, v40) / 2;
              break;
            }
            goto _continue_sprintf;
          }

          case 27:
          {
                    v26 = &p2DEvents[(signed int)a4 - 1].fPriceMultiplier;
        v27 = *v26;
        v28 = a3->GetValue();
        v29 = pPlayer->GetBuyingPrice(v28, v27);
        if ( a5 == 3 )
        {
          v35 = *v26;
          v36 = a3->GetValue();
          v29 = pPlayer->GetPriceSell(v36, v35);
          if (a3->Broken())
            v29 = 1;
          goto _continue_sprintf;
        }
        if ( a5 != 4 )
        {
          if ( a5 == 5 )
          {
            v32 = *v26;
            v33 = a3->GetValue();
            v29 = pPlayer->GetPriceRepair(v33, v32);
          }
          else
          {
            if ( a5 == 6 )
            {
              v30 = *v26;
              v31 = a3->GetValue();
              v29 = pPlayer->GetPriceSell(v31, v30) / 2;
              if (a3->Broken())
                v29 = 1;
              if (!v29)
                v29 = 1;
              goto _continue_sprintf;
            }
          }
_continue_sprintf:
          sprintf(a1, "%lu", v29);
          v11 = a1;
          goto _continue_strcat;
        }
        v34 = *v26;

      v48 = v34;
      v29 = pPlayer->GetPriceIdentification(v48);
      goto _continue_sprintf;
          }

          case 28:
          {
            //v11 = (char *)p2DEvents_minus1__10[13 * (signed int)a4];
            v11 = (char *)p2DEvents[(signed int)a4 - 1].pProprieterTitle;
            goto _continue_strcat;
          }

          case 29:
          {
            //v34 = p2DEvents_minus1__20[13 * (signed int)a4];
            v34 = p2DEvents[(signed int)a4 - 1].fPriceMultiplier;
            v48 = v34;
            v29 = pPlayer->GetPriceIdentification(v48);
            goto _continue_sprintf;
          }

          case 30:
          {
                if ( !a6 )
      goto _continue_strcat;
    sub_493F79(&v57, *a6);
    v54 = v57.field_18_expire_year;
    v53 = v57.field_C_expire_day + 1;
    v50 = v57.field_14_exprie_month;
    sprintf(a1, pGlobalTXT_LocalizationStrings[378], aMonthNames[v50], v53, v54);
    v11 = a1;
    goto _continue_strcat;
          }

          case 31:
          case 32:
          case 33:
          case 34:
            v52 = &pParty->pPlayers[v17 - 31];
            v11 = v52->pName;
            goto _continue_strcat;

          default:
            if ( v17 <= 50 || v17 > 70 )
              goto _continue_strncpy;
            if ( v17 - 51 >= 20 )
              goto _continue_strcat;
            v49 = pParty->field_3C._s_times[v17-51];
            
            sub_493F79(&v56, v49);
            v54 = v56.field_18_expire_year;
            v53 = v56.field_C_expire_day + 1;
            v50 = v56.field_14_exprie_month;
            sprintf(a1, pGlobalTXT_LocalizationStrings[378], aMonthNames[v50], v53, v54);
            v11 = a1;
            goto _continue_strcat;
        }
      }
  }
  return pTmpBuf2.data();
}

//----- (0049B04D) --------------------------------------------------------
void stru154::GetFacePlaneAndClassify(ODMFace *a2, BSPVertexBuffer *a3)
{
  //stru154 *v3; // edi@1
  //signed int v4; // eax@1
  //signed int result; // eax@9
  //signed int v6; // [sp-8h] [bp-18h]@8
  Vec3_float_ v; // [sp+4h] [bp-Ch]@1
  float v7;

  v.x = 0.0;
  v.y = 0.0;
  v.z = 0.0;
  GetFacePlane(a2, a3, &v, &v7);

  if (fabsf(a2->pFacePlane.vNormal.z) < 1e-6f)
    polygonType = POLYGON_VerticalWall;
  else if (fabsf(a2->pFacePlane.vNormal.x) < 1e-6f &&
           fabsf(a2->pFacePlane.vNormal.y) < 1e-6f)
    polygonType = POLYGON_Floor;
  else
    polygonType = POLYGON_InBetweenFloorAndWall;

  face_plane.vNormal.x = v.x;
  face_plane.vNormal.y = v.y;
  face_plane.vNormal.z = v.z;
  face_plane.dist = v7;
}

//----- (0049B0C9) --------------------------------------------------------
void stru154::ClassifyPolygon(Vec3_float_ *pNormal, float dist)
{
  if (fabsf(pNormal->z) < 1e-6f)
    polygonType = POLYGON_VerticalWall;
  else if (fabsf(pNormal->x) < 1e-6f &&
           fabsf(pNormal->y) < 1e-6f)
    polygonType = POLYGON_Floor;
  else
    polygonType = POLYGON_InBetweenFloorAndWall;

  face_plane.vNormal.x = pNormal->x;
  face_plane.dist = dist;
  face_plane.vNormal.y = pNormal->y;
  face_plane.vNormal.z = pNormal->z;
}

//----- (0049B13D) --------------------------------------------------------
void stru154::GetFacePlane(ODMFace *pFace, BSPVertexBuffer *pVertices, Vec3_float_ *pOutNormal, float *pOutDist)
{
  ODMFace *v5; // ebx@1
  //int v6; // eax@1
  //unsigned __int16 *v7; // ebx@2
  //Vec3_int_ *v8; // eax@3
  Vec3_int_ *v9; // ecx@3
  //double v10; // st7@3
  //int v11; // ecx@3
  Vec3_int_ *v12; // ecx@3
  //double v13; // st7@3
  //double v14; // st6@3
  //double v15; // st5@3
  //int v16; // ecx@3
  Vec3_int_ *v17; // eax@3
  //double v18; // st5@3
  Vec3_float_ *v19; // eax@3
  //int result; // eax@8
  //float v21; // ecx@10
  //double v22; // st7@10
  //double v23; // st6@10
  Vec3_float_ v2; // [sp+4h] [bp-64h]@3
  //float v25; // [sp+18h] [bp-50h]@3
  float v26; // [sp+1Ch] [bp-4Ch]@3
  float v27; // [sp+20h] [bp-48h]@3
  float v28; // [sp+24h] [bp-44h]@3
  //float v29; // [sp+2Ch] [bp-3Ch]@3
  //float v30; // [sp+30h] [bp-38h]@3
  //float v31; // [sp+34h] [bp-34h]@3
  //float v32; // [sp+38h] [bp-30h]@3
  //float v33; // [sp+3Ch] [bp-2Ch]@3
  Vec3_float_ v1; // [sp+40h] [bp-28h]@1
  //float v35; // [sp+4Ch] [bp-1Ch]@3
  //float v36; // [sp+50h] [bp-18h]@3
  //float v37; // [sp+54h] [bp-14h]@3
  Vec3_float_ v38; // [sp+58h] [bp-10h]@3
  //int v39; // [sp+64h] [bp-4h]@1

  //v39 = 0;
  v1.x = 0.0;
  v5 = pFace;
  //v6 = pFace->uNumVertices;
  v1.y = 0.0;
  v1.z = 0.0;

  if (pFace->uNumVertices >= 2)
  {
    int i = 0;
    while ( i < pFace->uNumVertices - 2 )
    {
      v9 = &pVertices->pVertices[pFace->pVertexIDs[i]];
      v12 = &pVertices->pVertices[pFace->pVertexIDs[i + 1]];
      v17 = &pVertices->pVertices[pFace->pVertexIDs[i + 2]];
	  i++;
      v1.x = v12->x - v9->x;
      v26 = v17->x - v12->x;
      v1.y = v12->y - v9->y;
      v27 = v17->y - v12->y;
      v1.z = v12->z - v9->z;
      v28 = v17->z - v12->z;
      v19 = Vec3_float_::Cross(&v1, &v2, v26, v27, v28);
      v38.x = v19->x;
      v38.y = v19->y;
      v38.z = v19->z;
      if ( v38.x != 0.0  || v38.y != 0.0 || v38.z != 0.0)
      {
        v38.Normalize();

        pOutNormal->x = v38.x;
        pOutNormal->y = v38.y;
        pOutNormal->z = v38.z;

        *pOutDist = -(v9->x * v38.x + v9->y * v38.y + v9->z * v38.z);
        return;
      }
    }
  }

  pOutNormal->x = (double)(v5->pFacePlane.vNormal.x & 0xFFFF) / 65535.0f + (double)(v5->pFacePlane.vNormal.x >> 16);
  pOutNormal->y = (double)(v5->pFacePlane.vNormal.y & 0xFFFF) / 65535.0f + (double)(v5->pFacePlane.vNormal.y >> 16);
  pOutNormal->z = (double)(v5->pFacePlane.vNormal.z & 0xFFFF) / 65535.0f + (double)(v5->pFacePlane.vNormal.z >> 16);
  *pOutDist = (double)(v5->pFacePlane.dist & 0xFFFF) / 65535.0f + (double)(v5->pFacePlane.dist >> 16);
}

//----- (0049D700) --------------------------------------------------------
unsigned int __fastcall GetMaxMipLevels(unsigned int uDim)
{
  unsigned int v1; // eax@1
  int v2; // ecx@1
  unsigned int v3; // eax@1

  v1 = uDim;
  v2 = 0;
  v3 = v1 - 1;
  while ( v3 & 1 )
  {
    v3 >>= 1;
    ++v2;
  }
  return v3 == 0 ? v2 : 0;
}

//----- (004A19D8) --------------------------------------------------------
unsigned int BlendColors(unsigned int a1, unsigned int a2)
{
  /*signed __int64 v2; // ST10_8@1
  double v3; // st7@1
  float v4; // ST24_4@1
  double v5; // ST10_8@1
  int v6; // ST1C_4@1
  float v7; // ST24_4@1
  double v8; // ST10_8@1
  unsigned __int8 v9; // ST20_1@1
  float v10; // ST24_4@1
  double v11; // ST10_8@1
  float v12; // ST24_4@1
  double v13; // ST08_8@1*/

  uint alpha = (uint)floorf(0.5f + (a1 >> 24) / 255.0f *
                                   (a2 >> 24) / 255.0f * 255.0f),
       red = (uint)floorf(0.5f + ((a1 >> 16) & 0xFF) / 255.0f *
                                 ((a2 >> 16) & 0xFF) / 255.0f * 255.0f),
       green = (uint)floorf(0.5f + ((a1 >> 8) & 0xFF) / 255.0f *
                                   ((a2 >> 8) & 0xFF) / 255.0f * 255.0f),
       blue = (uint)floorf(0.5f + ((a1 >> 0) & 0xFF) / 255.0f *
                                   ((a2 >> 0) & 0xFF) / 255.0f * 255.0f);
  return (alpha << 24) | (red << 16) | (green << 8) | blue;
  /*v2 = a1 >> 24;
  v3 = (double)v2 / 255.0f;
  HIDWORD(v2) = 0;
  LODWORD(v2) = a2 >> 24;
  v4 = v3 * (double)v2 / 255.0f * 255.0;
  v5 = v4 + 6.7553994e15;
  v6 = LODWORD(v5);
  v7 = (double)((a1 >> 16) & 0xFFi64) / 255.0f * (double)((a2 >> 16) & 0xFF) * 0.0039215689 * 255.0;
  v8 = v7 + 6.7553994e15;
  v9 = LOBYTE(v8);
  v10 = (double)((unsigned __int16)a1 >> 8) / 255.0f * (double)((unsigned __int16)a2 >> 8) / 255.0f * 255.0;
  v11 = v10 + 6.7553994e15;
  v12 = (double)(a1 & 0xFFi64) / 255.0f * (double)(unsigned __int8)a2 / 255.0f * 255.0;
  v13 = v12 + 6.7553994e15;
  return LOBYTE(v13) | ((LOBYTE(v11) | (((v6 << 8) | v9) << 8)) << 8);*/
}

//----- (004A46E6) --------------------------------------------------------
int __fastcall sr_4A46E6_draw_particle_segment(unsigned int x, signed int y, signed int _z, int a4, unsigned int lightColor)
{
  int v5; // eax@1
  int z; // eax@1
  unsigned int v7; // eax@9
  unsigned int v8; // ecx@9
  int v9; // eax@9
  unsigned int v10; // eax@10
  int *v11; // esi@10
  int *v12; // edi@10
  int v13; // ecx@10
  int v14; // edx@10
  unsigned int v15; // eax@22
  int *v16; // esi@22
  int *v17; // edi@22
  int v18; // ecx@22
  int v19; // edx@22
  unsigned __int16 *pTarget; // [sp+Ch] [bp-8h]@9
  int *pTargetZ; // [sp+10h] [bp-4h]@9
  unsigned int v22; // [sp+1Ch] [bp+8h]@9
  signed int v23; // [sp+20h] [bp+Ch]@1

  v5 = a4;
  v23 = _z >> 16;
  z = x + v5;
  if ( z >= (signed int)pViewport->uViewportTL_X
    && (signed int)x <= (signed int)pViewport->uViewportBR_X
    && y >= (signed int)pViewport->uViewportTL_Y
    && y <= (signed int)pViewport->uViewportBR_Y )
  {
    if ( (signed int)x < (signed int)pViewport->uViewportTL_X )
      x = pViewport->uViewportTL_X;
    if ( z > (signed int)pViewport->uViewportBR_X )
      z = pViewport->uViewportBR_X;
    pTarget = &pRenderer->pTargetSurface[x + y * pRenderer->uTargetSurfacePitch];
    v22 = z - x;
    pTargetZ = &pRenderer->pActiveZBuffer[x + 640 * y];
    v7 = lightColor >> 3;
    v8 = lightColor & 0xF0;
    v9 = v7 & 0x1E0000;
    if ( pRenderer->uTargetGBits == 5 )
    {
      v10 = (v8 | (((unsigned __int16)(lightColor & 0xF000) | (unsigned int)v9) >> 3)) >> 4;
      v11 = (int *)pTarget;
      v12 = pTargetZ;
      v13 = v22;
      v14 = (v10 << 16) | v10;
      z = (unsigned __int8)pTarget & 2;
      if ( (unsigned __int8)pTarget & 2 )
      {
        z = (unsigned int)*pTargetZ >> 16;
        if ( z > v23 )
        {
          z = v14 + ((*pTarget & 0x7BDEu) >> 1);
          *pTarget = z;
        }
        v13 = v22 - 1;
        v11 = (int *)(pTarget + 1);
        v12 = pTargetZ + 1;
      }
      while ( v13 != 1 )
      {
        if ( v13 < 1 )
          return z;
        z = (unsigned int)*v12 >> 16;
        if ( z <= v23 )
        {
          v13 -= 2;
          ++v11;
          v12 += 2;
        }
        else
        {
          v12 += 2;
          z = v14 + ((*v11 & 0x7BDE7BDEu) >> 1);
          v13 -= 2;
          *v11 = z;
          ++v11;
        }
      }
      z = (unsigned int)*v12 >> 16;
      if ( z > v23 )
      {
        z = v14 + ((*(short *)v11 & 0x7BDEu) >> 1);
        *(short *)v11 = z;
      }
    }
    else
    {
      v15 = (v8 | (((unsigned __int16)(lightColor & 0xF800) | (unsigned int)v9) >> 2)) >> 4;
      v16 = (int *)pTarget;
      v17 = pTargetZ;
      v18 = v22;
      v19 = (v15 << 16) | v15;
      z = (unsigned __int8)pTarget & 2;
      if ( (unsigned __int8)pTarget & 2 )
      {
        z = (unsigned int)*pTargetZ >> 16;
        if ( z > v23 )
        {
          z = v19 + ((*pTarget & 0xF7DEu) >> 1);
          *pTarget = z;
        }
        v18 = v22 - 1;
        v16 = (int *)(pTarget + 1);
        v17 = pTargetZ + 1;
      }
      while ( v18 != 1 )
      {
        if ( v18 < 1 )
          return z;
        z = (unsigned int)*v17 >> 16;
        if ( z <= v23 )
        {
          v18 -= 2;
          ++v16;
          v17 += 2;
        }
        else
        {
          v17 += 2;
          z = v19 + ((*v16 & 0xF7DEF7DEu) >> 1);
          v18 -= 2;
          *v16 = z;
          ++v16;
        }
      }
      z = (unsigned int)*v17 >> 16;
      if ( z > v23 )
      {
        z = v19 + ((*(short *)v16 & 0xF7DEu) >> 1);
        *(short *)v16 = z;
      }
    }
  }
  return z;
}

//----- (004A57E9) --------------------------------------------------------
void __cdecl Present_ColorKey()
{
  HRESULT v0; // eax@3
  HRESULT v1; // eax@3
  HRESULT v2; // eax@3
  HRESULT v3; // eax@3
  HRESULT v4; // eax@3
  RECT a2; // [sp+4h] [bp-14h]@3
  //CheckHRESULT_stru0 this; // [sp+14h] [bp-4h]@3

  if ( !pRenderer->uNumSceneBegins )
  {
    if ( pRenderer->field_40110 )
    {
      a2.bottom = pViewport->uViewportTL_Y;
      a2.left = 0;
      a2.top = 0;
      a2.right = 640;
      ErrD3D(pRenderer->pBackBuffer4->BltFast(0, 0, pRenderer->pColorKeySurface4, &a2, 16u));
      a2.right = 640;
      a2.left = 0;
      a2.top = pViewport->uViewportBR_Y + 1;
      a2.bottom = 480;
      ErrD3D(pRenderer->pBackBuffer4->BltFast(
             0,
             pViewport->uViewportBR_Y + 1,
             pRenderer->pColorKeySurface4,
             &a2,
             16u));
      a2.right = pViewport->uViewportTL_X;
      a2.bottom = pViewport->uViewportBR_Y + 1;
      a2.left = 0;
      a2.top = pViewport->uViewportTL_Y;
      ErrD3D(pRenderer->pBackBuffer4->BltFast(
             0,
             pViewport->uViewportTL_Y,
             pRenderer->pColorKeySurface4,
             &a2,
             16u));
      a2.left = pViewport->uViewportBR_X;
      a2.top = pViewport->uViewportTL_Y;
      a2.right = 640;
      a2.bottom = pViewport->uViewportBR_Y + 1;
      ErrD3D(pRenderer->pBackBuffer4->BltFast(
             pViewport->uViewportBR_X,
             pViewport->uViewportTL_Y,
             pRenderer->pColorKeySurface4,
             &a2,
             16u));
      a2.right = pViewport->uViewportBR_X;
      a2.bottom = pViewport->uViewportBR_Y + 1;
      a2.left = pViewport->uViewportTL_X;
      a2.top = pViewport->uViewportTL_Y;
      ErrD3D(pRenderer->pBackBuffer4->BltFast(
             pViewport->uViewportTL_X,
             pViewport->uViewportTL_Y,
             pRenderer->pColorKeySurface4,
             &a2,
             17u));
    }
  }
}

//----- (004A597D) --------------------------------------------------------
void Present_NoColorKey()
{
  //unsigned __int16 *v0; // eax@4
  unsigned __int16 *v1; // esi@4
  void *v2; // edi@4
  //signed int v4; // ebx@4
  //signed int v5; // ebx@6
  //void *v6; // edi@7
  //const void *v7; // esi@7
  signed int v8; // ebx@8
  int v9; // eax@10
  unsigned int v10; // esi@10
  unsigned __int32 v11; // edi@10
  //int v12; // ecx@10
  unsigned int v13; // ebx@10
  int v14; // eax@11
  int v15; // eax@13
  int v16; // eax@14
  int v17; // eax@16
  HRESULT v18; // eax@22
  DDSURFACEDESC2 Dst; // [sp+Ch] [bp-98h]@3
  int v20; // [sp+88h] [bp-1Ch]@10
  int v21; // [sp+8Ch] [bp-18h]@10
  __int32 v22; // [sp+90h] [bp-14h]@10
  //unsigned __int32 v23; // [sp+94h] [bp-10h]@10
  unsigned int v24; // [sp+98h] [bp-Ch]@4
  //unsigned int _this; // [sp+9Ch] [bp-8h]@10
  //LPVOID v26; // [sp+A0h] [bp-4h]@4

  if ( !pRenderer->uNumSceneBegins )
  {
    if ( pRenderer->field_40110 )
    {
      memset(&Dst, 0, 0x7Cu);
      Dst.dwSize = 124;
      if ( pRenderer->LockSurface_DDraw4(pRenderer->pBackBuffer4, &Dst, DDLOCK_WAIT) )
      {
        //v26 = Dst.lpSurface;
        pRenderer->pCurrentlyLockedSurfaceDataPtr = (unsigned __int16 *)Dst.lpSurface;
        v24 = pRenderer->uTargetGMask | pRenderer->uTargetBMask |
              ((pRenderer->uTargetGMask | pRenderer->uTargetBMask) << 16);
        pRenderer->pCurrentlyLockedSoftSurface = pRenderer->pTargetSurface;
        pRenderer->uCurrentlyLockedSurfacePitch = Dst.lPitch;
        v1 = pRenderer->pTargetSurface;
        v2 = Dst.lpSurface;


        /*for (uint y = 0; y < 480; ++y)
        {
          auto pDst = (unsigned short *)((char *)Dst.lpSurface + y * Dst.lPitch);
          for (uint x = 0; x < 640; ++x)
            pDst[x] = pRenderer->uTargetRMask | pRenderer->uTargetBMask;
        }*/
        
        auto pSrc = pRenderer->pTargetSurface;
        auto pDst = (__int16 *)Dst.lpSurface;

        for (uint y = 0; y < 8; ++y)
          memcpy(pDst + y * Dst.lPitch / 2,

		  pSrc + y * 640, 640 * sizeof(__int16));

        for (uint y = 8; y < 352; ++y)
        {
          memcpy(pDst + y * Dst.lPitch / 2,
                 pSrc + y * 640, 8 * sizeof(__int16));
          memcpy(pDst + 8 + game_viewport_width/*462*/ + y * Dst.lPitch / 2,
                 pSrc + 8 + game_viewport_width/*462*/ + y * 640, 174/*172*/ * sizeof(__int16));
        }

        for (uint y = 352; y < 480; ++y)
          memcpy(pDst + y * Dst.lPitch / 2,
                 pSrc + y * 640, 640 * sizeof(__int16));


        auto pSrc_x1y1 = pSrc + 640 * pViewport->uViewportTL_Y + pViewport->uViewportTL_X;
        //_this = (unsigned int)&pSrc[2 * (((signed int)pViewport->uViewportX >> 1) + 320 * pViewport->uViewportY)];
        auto pDst_x1y1 = pDst + Dst.lPitch * pViewport->uViewportTL_Y + pViewport->uViewportTL_X;
        //v23 = (unsigned __int32)((char *)v26 + 4 * (((signed int)pViewport->uViewportX >> 1) + (Dst.lPitch >> 2) * pViewport->uViewportY));
        v9 = ((signed int)pViewport->uViewportTL_X >> 1) - ((signed int)pViewport->uViewportBR_X >> 1);
        //v20 = ((signed int)pViewport->uViewportZ >> 1) - ((signed int)pViewport->uViewportX >> 1);
        v22 = 4 * ((Dst.lPitch / 4) + v9);
        v21 = 4 * v9 + 1280;

        //auto uNumLines = pViewport->uViewportW - pViewport->uViewportY + 1;
        //v26 = (LPVOID)(pViewport->uViewportW - pViewport->uViewportY + 1);
        v10 = (int)pSrc_x1y1;
        v11 = (int)pDst_x1y1;
        auto uHalfWidth = v20 = (pViewport->uViewportBR_X - pViewport->uViewportTL_X) / 2;
        v13 = v24;

        for (uint y = pViewport->uViewportTL_Y; y < pViewport->uViewportBR_Y + 1; ++y)
        {
          //memcpy(pDst + pViewport->uViewportX + y * Dst.lPitch / 2,
          //       pSrc + pViewport->uViewportX + y * 640, (pViewport->uViewportZ - pViewport->uViewportX) * sizeof(__int16));
          for (uint x = pViewport->uViewportTL_X; x < pViewport->uViewportBR_X; ++x)
          {
            if (pSrc[y * 640 + x] != (pRenderer->uTargetGMask | pRenderer->uTargetBMask))
              pDst[y * Dst.lPitch / 2 + x] = pSrc[y * 640 + x];
          }
        }

              ErrD3D(pRenderer->pBackBuffer4->Unlock(0));

       /* while ( 1 )
        {
          while ( 1 )
          {
            v14 = *(int *)v10;
            v10 += 4;
            if ( v14 == v13 )
              break;
            if ( (short)v14 == (short)v13 )
            {
              *(int *)v11 = *(int *)v11 & 0xFFFF | v14 & 0xFFFF0000;
              v11 += 4;
              --uHalfWidth;
              if ( !uHalfWidth )
                goto LABEL_21;
            }
            else
            {
              v15 = __ROL__(v14, 16);
              if ( (short)v15 == (short)v13 )
              {
                v17 = __ROR__(v15, 16);
                *(int *)v11 = *(int *)v11 & 0xFFFF0000 | (unsigned __int16)v17;
                v11 += 4;
                --uHalfWidth;
                if ( !uHalfWidth )
                  goto LABEL_21;
              }
              else
              {
                v16 = __ROR__(v15, 16);
                *(int *)v11 = v16;
                v11 += 4;
                --uHalfWidth;
                if ( !uHalfWidth )
                  goto LABEL_21;
              }
            }
          }
          v11 += 4;
          --uHalfWidth;
          if ( !uHalfWidth )
          {
LABEL_21:
            v10 += v21;
            v11 += v22;
            uHalfWidth = v20;
            if ( !--uNumLines )
            {
              ErrD3D(pRenderer->pBackBuffer4->Unlock(0));
              return;
            }
          }
        }*/
      }
    }
  }
}

//----- (004A7063) --------------------------------------------------------
int __thiscall sub_4A7063(unsigned int uDiffuse, float a2)
{
  float v2; // ST1C_4@1
  double v3; // ST14_8@1
  signed int v4; // ebx@1
  float v5; // ST1C_4@1
  double v6; // ST14_8@1
  signed int v7; // edi@1
  double v8; // ST0C_8@1
  signed int v9; // esi@1
  signed int v10; // ecx@1
  double v12; // [sp+8h] [bp-18h]@1
  unsigned __int64 v13; // [sp+18h] [bp-8h]@1

  v2 = (double)(uDiffuse >> 24) * a2;
  v3 = v2 + 6.7553994e15;
  v4 = LODWORD(v3);
  v5 = (double)((uDiffuse >> 16) & 0xFF) * a2;
  v6 = v5 + 6.7553994e15;
  v7 = LODWORD(v6);
  *((float *)&v6 + 1) = (double)((unsigned __int16)uDiffuse >> 8) * a2;
  v8 = *((float *)&v6 + 1) + 6.7553994e15;
  v13 = __PAIR__(LODWORD(v6), LODWORD(v8));
  v9 = LODWORD(v8);
  *((float *)&v6 + 1) = (double)(unsigned __int8)uDiffuse * a2;
  v12 = *((float *)&v6 + 1) + 6.7553994e15;
  v10 = LODWORD(v12);
  if ( v4 > 255 )
    v4 = 255;
  if ( v4 < 0 )
    v4 = 0;
  if ( SHIDWORD(v13) > 255 )
    v7 = 255;
  if ( v7 < 0 )
    v7 = 0;
  if ( (signed int)v13 > 255 )
    v9 = 255;
  if ( v9 < 0 )
    v9 = 0;
  if ( SLODWORD(v12) > 255 )
    v10 = 255;
  if ( v10 < 0 )
    v10 = 0;
  return v10 | ((v9 | ((v7 | (v4 << 8)) << 8)) << 8);
}

//----- (004AD504) --------------------------------------------------------
int __fastcall sub_4AD504(signed int sFaceID)
{
  int result; // eax@1
  signed int v2; // ebx@1
  BLVFace *v3; // esi@3
  Texture *v4; // edi@6
  int v5; // eax@7
  int v6; // edx@7
  int v7; // ecx@7
  int v8; // ebx@8
  int v9; // eax@8
  int v10; // ebx@8
  unsigned int v11; // ebx@9
  int v12; // esi@10
  int v13; // eax@13
  unsigned __int16 *v14; // eax@13
  unsigned int v15; // eax@14
  unsigned __int16 *v16; // ebx@14
  int v17; // eax@15
  int v18; // ebx@15
  int v19; // esi@15
  int v20; // ecx@15
  int v21; // eax@15
  stru193_math *v22; // ebx@15
  int v23; // eax@15
  char *v24; // esi@16
  int v25; // eax@18
  int v26; // eax@18
  int v27; // eax@20
  signed int v28; // esi@20
  signed int v29; // edx@20
  signed int v30; // esi@20
  signed int v31; // edi@20
  int v32; // esi@20
  int v33; // eax@26
  int *v34; // esi@27
  unsigned __int16 *v35; // edi@27
  unsigned int v36; // edx@27
  int v37; // ebx@27
  char v38; // cl@27
  char v39; // ch@27
  int v40; // ebx@29
  int v41; // edx@29
  unsigned int v42; // ebx@30
  int v43; // edx@30
  int v44; // eax@33
  int *v45; // esi@34
  unsigned __int16 *v46; // edi@34
  unsigned int v47; // edx@34
  int v48; // ebx@34
  char v49; // cl@34
  char v50; // ch@34
  int v51; // ebx@36
  int v52; // edx@36
  unsigned __int16 v53; // bx@37
  int v54; // edx@37
  int v55; // eax@40
  int v56; // eax@41
  int *v57; // esi@42
  unsigned __int16 *v58; // edi@42
  unsigned int v59; // edx@42
  int v60; // ebx@42
  char v61; // cl@42
  char v62; // ch@42
  unsigned int v63; // ebx@44
  int v64; // edx@44
  unsigned int v65; // ebx@44
  int v66; // edx@44
  unsigned int v67; // ebx@45
  int v68; // edx@45
  int v69; // ebx@45
  int v70; // edx@45
  int v71; // eax@48
  int *v72; // esi@49
  unsigned __int16 *v73; // edi@49
  unsigned int v74; // edx@49
  int v75; // ebx@49
  char v76; // cl@49
  char v77; // ch@49
  unsigned int v78; // ebx@51
  int v79; // edx@51
  unsigned int v80; // ebx@51
  int v81; // edx@51
  unsigned int v82; // ebx@52
  int v83; // edx@52
  int v84; // ebx@52
  int v85; // edx@52
  unsigned __int8 *v86; // [sp+Ch] [bp-98h]@9
  unsigned __int8 *v87; // [sp+10h] [bp-94h]@9
  unsigned __int8 *v88; // [sp+14h] [bp-90h]@9
  unsigned __int8 *v89; // [sp+18h] [bp-8Ch]@9
  int v90; // [sp+1Ch] [bp-88h]@20
  BLVFace *v91; // [sp+20h] [bp-84h]@3
  int v92; // [sp+24h] [bp-80h]@7
  int i; // [sp+28h] [bp-7Ch]@7
  unsigned __int16 *v94; // [sp+2Ch] [bp-78h]@9
  unsigned int v95; // [sp+30h] [bp-74h]@1
  Texture *v96; // [sp+34h] [bp-70h]@6
  int v97; // [sp+38h] [bp-6Ch]@15
  unsigned int v98; // [sp+3Ch] [bp-68h]@9
  int v99; // [sp+40h] [bp-64h]@9
  int v100; // [sp+44h] [bp-60h]@24
  int v101; // [sp+48h] [bp-5Ch]@10
  int v102; // [sp+4Ch] [bp-58h]@20
  int v103; // [sp+50h] [bp-54h]@20
  int v104; // [sp+54h] [bp-50h]@9
  unsigned __int8 *v105; // [sp+58h] [bp-4Ch]@20
  int v106; // [sp+5Ch] [bp-48h]@24
  int v107; // [sp+60h] [bp-44h]@20
  int v108; // [sp+64h] [bp-40h]@20
  int v109; // [sp+68h] [bp-3Ch]@20
  int v110; // [sp+6Ch] [bp-38h]@15
  int v111; // [sp+70h] [bp-34h]@20
  int a1; // [sp+74h] [bp-30h]@12
  int a2; // [sp+78h] [bp-2Ch]@9
  int *v114; // [sp+7Ch] [bp-28h]@16
  int v115; // [sp+80h] [bp-24h]@18
  int v116; // [sp+84h] [bp-20h]@7
  unsigned int v117; // [sp+88h] [bp-1Ch]@15
  unsigned int v118; // [sp+8Ch] [bp-18h]@24
  int *v119; // [sp+90h] [bp-14h]@13
  int v120; // [sp+94h] [bp-10h]@15
  unsigned int v121; // [sp+98h] [bp-Ch]@15
  unsigned __int16 *v122; // [sp+9Ch] [bp-8h]@15
  unsigned int v123; // [sp+A0h] [bp-4h]@13

  result = pRenderer->uTargetSurfacePitch;
  v2 = sFaceID;
  v95 = pRenderer->uTargetSurfacePitch;
  if ( sFaceID >= 0 )
  {
    if ( sFaceID < (signed int)pIndoor->uNumFaces )
    {
      v3 = &pIndoor->pFaces[sFaceID];
      v91 = &pIndoor->pFaces[sFaceID];
      if ( !pRenderer->pRenderD3D )
      {
        result = sub_423B5D(sFaceID);
        if ( result )
        {
          result = sub_424829(result, &stru_F8A590, pBLVRenderParams->field_7C, v2);
          if ( result )
          {
            result = (int)v3->GetTexture();
            v4 = (Texture *)result;
            v96 = (Texture *)result;
            if ( result )
            {
              v5 = *(short *)(result + 38);
              LOBYTE(v3->uAttributes) |= 0x80u;
              v92 = v5;
              sub_4AE5F1(v2);
              ++pBLVRenderParams->field_84;
              v6 = stru_F8AD28.pDeltaUV[0];
              v116 = stru_F8AD28.pDeltaUV[1];
              v7 = 0;
              for ( i = bUseLoResSprites; v7 < stru_F8AD28.uNumLightsApplied; *(int *)v9 = v10 )
              {
                v8 = v116;
                stru_F8AD28._blv_lights_xs[v7] += v6;
                v9 = 4 * v7 + 16297672;
                v10 = v8 - stru_F8AD28._blv_lights_ys[v7++];
              }
              v94 = sr_sub_47C24C_get_palette(v3, v92, 0, 1);
              result = stru_F8A590._viewport_space_y;
              a2 = stru_F8A590._viewport_space_y;
              v11 = stru_F8A590._viewport_space_y * pRenderer->uTargetSurfacePitch;
              v99 = 640 * stru_F8A590._viewport_space_y;
              v86 = v4->pLevelOfDetail0_prolly_alpha_mask;
              v87 = v4->pLevelOfDetail1;
              v88 = v4->pLevelOfDetail2;
              v89 = v4->pLevelOfDetail3;
              v98 = stru_F8A590._viewport_space_y * pRenderer->uTargetSurfacePitch;
              v104 = 2 * pBLVRenderParams->field_0_timer_;
              if ( stru_F8A590._viewport_space_y <= stru_F8A590._viewport_space_w )
              {
                v12 = 2 * stru_F8A590._viewport_space_y;
                v101 = 2 * stru_F8A590._viewport_space_y;
                while ( 1 )
                {
                  a1 = *(__int16 *)((char *)stru_F8A590.viewport_left_side + v12);
                  sub_4AE313(a1, result, &stru_F81018.field_0);
                  if ( LOBYTE(viewparams->field_20) )
                  {
                    v15 = v95 * (v12 - pBLVRenderParams->uViewportY);
                    v119 = &pBLVRenderParams->pTargetZBuffer[2
                                                          * (*(__int16 *)((char *)stru_F8A590.viewport_left_side + v12)
                                                           + 320 * (v12 - pBLVRenderParams->uViewportY))
                                                          - pBLVRenderParams->uViewportX];
                    v16 = &pBLVRenderParams->pRenderTarget[v15
                                                        + 2 * *(__int16 *)((char *)stru_F8A590.viewport_left_side + v12)
                                                        - pBLVRenderParams->uViewportX];
                    v14 = &pBLVRenderParams->pRenderTarget[v15
                                                        + 2 * *(__int16 *)((char *)stru_F8A590.viewport_right_side + v12)
                                                        - pBLVRenderParams->uViewportX];
                    v123 = (unsigned int)v16;
                  }
                  else
                  {
                    v13 = *(__int16 *)((char *)stru_F8A590.viewport_left_side + v12);
                    v119 = &pBLVRenderParams->pTargetZBuffer[v13 + v99];
                    v123 = (unsigned int)&pBLVRenderParams->pRenderTarget[v13 + v11];
                    v14 = &pBLVRenderParams->pRenderTarget[v11 + *(__int16 *)((char *)stru_F8A590.viewport_right_side + v12)];
                  }
                  v117 = (unsigned int)v14;
                  HIWORD(v17) = HIWORD(stru_F81018.field_0.field_0);
                  v18 = stru_F81018.field_0.field_10;
                  LOWORD(v17) = 0;
                  v97 = stru_F8AD28.field_0 | v17;
                  v110 = sub_4AE491(SHIWORD(stru_F81018.field_0.field_4), SHIWORD(stru_F81018.field_0.field_8));
                  v19 = stru_F81018.field_0.field_4 >> i;
                  v20 = 11 - v18;
                  v116 = stru_F81018.field_0.field_8 >> i;
                  v21 = stru_F81018.field_0.field_8 >> i >> (11 - v18);
                  v22 = stru_5C6E00;
                  v122 = (unsigned __int16 *)v20;
                  v121 = (stru_F81018.field_0.field_4 >> i) + 4 * stru_5C6E00->Cos(v104 + v21);
                  v23 = stru_5C6E00->Sin(v104 + (v19 >> (char)v122));
                  v120 = v116 + 4 * v23;
                  if ( v123 < v117 )
                  {
                    v24 = (char *)&stru_F81018.field_34.field_8;
                    v114 = &stru_F81018.field_34.field_8;
                    a1 += 16;
                    while ( 1 )
                    {
                      sub_4AE313(a1, a2, (stru337_stru0 *)(v24 - 8));
                      v25 = *((int *)v24 - 2);
                      LOWORD(v25) = 0;
                      v115 = v97;
                      v26 = stru_F8AD28.field_0 | v25;
                      if ( v97 <= (unsigned int)v26 )
                        v115 = v26;
                      v97 = v26;
                      v27 = *((int *)v24 - 11);
                      v28 = *((int *)v24 - 1);
                      v105 = (&v86)[4 * v27];
                      v111 = v27 + 16;
                      v29 = v4->uWidthMinus1;
                      v109 = v27 + v27 + 16 - v4->uWidthLn2;
                      v107 = v29 >> v27;
                      v30 = v28 >> i;
                      v102 = v4->uHeightMinus1 >> v27 << (v27 + 16);
                      v31 = *v114 >> i;
                      v122 = (unsigned __int16 *)(11 - v27);
                      v116 = v30 + 4 * stru_5C6E00->Cos(v104 + (v31 >> (11 - v27)));
                      v90 = v31 + 4 * stru_5C6E00->Sin(v104 + (v30 >> (char)v122));
                      v108 = (signed int)(v116 - v121) >> 4;
                      v103 = (v90 - v120) >> 4;
                      v32 = v123 + 32;
                      if ( LOBYTE(viewparams->field_20) )
                        v32 = v123 + 64;
                      if ( v32 > v117 )
                        v32 = v117;
                      v118 = v32;
                      v100 = sub_4AE491(*((short *)v114 - 1), *((short *)v114 + 1));
                      v106 = (signed int)(v32 - v123) >> 1;
                      if ( v110 >> 16 == v100 >> 16 )
                      {
                        v122 = sr_sub_47C24C_get_palette(v91, v92, v110 >> 16, 1);
                        if ( LOBYTE(viewparams->field_20) )
                        {
                          v44 = v123;
                          if ( v123 < v118 )
                          {
                            v45 = v119;
                            v46 = v122;
                            v47 = v121;
                            v48 = v120;
                            v49 = v111;
                            v50 = v109;
                            if ( v106 & 2 )
                            {
                              *v119 = v115;
                              v45 -= 2;
                              v44 = v123 + 4;
                              goto LABEL_37;
                            }
                            do
                            {
                              v44 += 8;
                              v51 = *(&v105[v107 & (v47 >> v49)] + ((v102 & (unsigned int)v48) >> v50));
                              v52 = v115;
                              LOWORD(v51) = v46[v51];
                              *v45 = v115;
                              v45[1] = v52;
                              v45[640] = v52;
                              v45[641] = v52;
                              *(short *)(v44 - 8) = v51;
                              *(short *)(v44 - 6) = v51;
                              *(short *)(v44 + 1272) = v51;
                              *(short *)(v44 + 1274) = v51;
                              v45[2] = v52;
                              v45[3] = v52;
                              v45[642] = v52;
                              v45[643] = v52;
                              v121 += v108;
                              v120 += v103;
                              v47 = v121;
                              v48 = v120;
LABEL_37:
                              v45 += 4;
                              v53 = v46[*(&v105[v107 & (v47 >> v49)] + ((v102 & (unsigned int)v48) >> v50))];
                              v54 = v108;
                              *(short *)(v44 - 4) = v53;
                              *(short *)(v44 - 2) = v53;
                              *(short *)(v44 + 1276) = v53;
                              *(short *)(v44 + 1278) = v53;
                              v121 += v54;
                              v120 += v103;
                              v47 = v121;
                              v48 = v120;
                            }
                            while ( v44 < v118 );
                            v123 = v44;
                            v119 = v45;
                          }
                        }
                        else
                        {
                          v33 = v123;
                          if ( v123 < v118 )
                          {
                            v34 = v119;
                            v35 = v122;
                            v36 = v121;
                            v37 = v120;
                            v38 = v111;
                            v39 = v109;
                            if ( v106 & 1 )
                            {
                              *v119 = v115;
                              --v34;
                              v33 = v123 + 2;
                              goto LABEL_30;
                            }
                            do
                            {
                              v33 += 4;
                              v40 = *(&v105[v107 & (v36 >> v38)] + ((v102 & (unsigned int)v37) >> v39));
                              v41 = v115;
                              LOWORD(v40) = v35[v40];
                              *v34 = v115;
                              *(short *)(v33 - 4) = v40;
                              v34[1] = v41;
                              v121 += v108;
                              v120 += v103;
                              v36 = v121;
                              v37 = v120;
LABEL_30:
                              v42 = (unsigned int)(&v105[v107 & (v36 >> v38)] + ((v102 & (unsigned int)v37) >> v39));
                              v34 += 2;
                              v43 = v108;
                              *(short *)(v33 - 2) = v35[*(char *)v42];
                              v121 += v43;
                              v120 += v103;
                              v36 = v121;
                              v37 = v120;
                            }
                            while ( v33 < v118 );
                            v123 = v33;
                            v119 = v34;
                          }
                        }
                      }
                      else
                      {
                        v55 = v110 - ((v100 - v110) >> 4);
                        v110 = (v100 - v110) >> 4;
                        v122 = (unsigned __int16 *)v55;
                        if ( LOBYTE(viewparams->field_20) )
                        {
                          v71 = v123;
                          if ( v123 < v118 )
                          {
                            v72 = v119;
                            v73 = v94;
                            v74 = v121;
                            v75 = v120;
                            v76 = v111;
                            v77 = v109;
                            if ( v106 & 2 )
                            {
                              *v119 = v115;
                              v72 += 2;
                              v71 = v123 + 4;
                              goto LABEL_52;
                            }
                            do
                            {
                              v78 = (v107 & (v74 >> v76)) + ((v102 & (unsigned int)v75) >> v77);
                              v79 = (int)((char *)v122 + v110);
                              v71 += 8;
                              v122 = (unsigned __int16 *)v79;
                              v80 = ((v79 & 0xFFFF0000u) >> 8) + v105[v78];
                              v81 = v115;
                              LOWORD(v80) = v73[v80];
                              *v72 = v115;
                              v72[1] = v81;
                              v72[640] = v81;
                              v72[641] = v81;
                              *(short *)(v71 - 8) = v80;
                              *(short *)(v71 - 6) = v80;
                              *(short *)(v71 + 1272) = v80;
                              *(short *)(v71 + 1274) = v80;
                              v72[2] = v81;
                              v72[3] = v81;
                              v72[642] = v81;
                              v72[643] = v81;
                              v121 += v108;
                              v120 += v103;
                              v74 = v121;
                              v75 = v120;
                              v72 += 4;
LABEL_52:
                              v82 = (v107 & (v74 >> v76)) + ((v102 & (unsigned int)v75) >> v77);
                              v83 = (int)((char *)v122 + v110);
                              v84 = v105[v82];
                              v122 = (unsigned __int16 *)v83;
                              LOWORD(v84) = v73[((v83 & 0xFFFF0000u) >> 8) + v84];
                              v85 = v108;
                              *(short *)(v71 - 4) = v84;
                              *(short *)(v71 - 2) = v84;
                              *(short *)(v71 + 1276) = v84;
                              *(short *)(v71 + 1278) = v84;
                              v121 += v85;
                              v120 += v103;
                              v74 = v121;
                              v75 = v120;
                            }
                            while ( v71 < v118 );
                            v123 = v71;
                            v119 = v72;
                          }
                        }
                        else
                        {
                          v56 = v123;
                          if ( v123 < v118 )
                          {
                            v57 = v119;
                            v58 = v94;
                            v59 = v121;
                            v60 = v120;
                            v61 = v111;
                            v62 = v109;
                            if ( v106 & 1 )
                            {
                              *v119 = v115;
                              ++v57;
                              v56 = v123 + 2;
                              goto LABEL_45;
                            }
                            do
                            {
                              v63 = (v107 & (v59 >> v61)) + ((v102 & (unsigned int)v60) >> v62);
                              v64 = (int)((char *)v122 + v110);
                              v56 += 4;
                              v122 = (unsigned __int16 *)v64;
                              v65 = ((v64 & 0xFFFF0000u) >> 8) + v105[v63];
                              v66 = v115;
                              LOWORD(v65) = v58[v65];
                              *v57 = v115;
                              *(short *)(v56 - 4) = v65;
                              v57[1] = v66;
                              v121 += v108;
                              v120 += v103;
                              v59 = v121;
                              v60 = v120;
                              v57 += 2;
LABEL_45:
                              v67 = (v107 & (v59 >> v61)) + ((v102 & (unsigned int)v60) >> v62);
                              v68 = (int)((char *)v122 + v110);
                              v69 = v105[v67];
                              v122 = (unsigned __int16 *)v68;
                              LOWORD(v69) = v58[((v68 & 0xFFFF0000u) >> 8) + v69];
                              v70 = v108;
                              *(short *)(v56 - 2) = v69;
                              v121 += v70;
                              v120 += v103;
                              v59 = v121;
                              v60 = v120;
                            }
                            while ( v56 < v118 );
                            v123 = v56;
                            v119 = v57;
                          }
                        }
                      }
                      v114 += 13;
                      v110 = v100;
                      a1 += 16;
                      v4 = v96;
                      v121 = v116;
                      v120 = v90;
                      if ( v123 >= v117 )
                        break;
                      v24 = (char *)v114;
                      v22 = stru_5C6E00;
                    }
                  }
                  ++a2;
                  v98 += v95;
                  result = a2;
                  v101 += 2;
                  v99 += 640;
                  if ( a2 > stru_F8A590._viewport_space_w )
                    break;
                  v12 = v101;
                  result = a2;
                  v11 = v98;
                }
              }
            }
          }
        }
      }
    }
  }
  return result;
}
// 4AE491: using guessed type int __fastcall sub_4AE491(int, int);

//----- (004ADD1D) --------------------------------------------------------
void __fastcall sub_4ADD1D(int uFaceID)
{
  int v1; // edi@1
  BLVFace *v2; // esi@3
  signed int v3; // ebx@4
  Texture *v4; // edi@9
  signed int v5; // eax@9
  char *v6; // edi@12
  signed int v7; // eax@15
  unsigned int v8; // eax@16
  __int16 v9; // cx@19
  unsigned __int8 *v10; // eax@19
  unsigned __int16 *v11; // eax@19
  int v12; // edi@19
  int v13; // ebx@20
  stru352 *v14; // esi@20
  DWORD v15; // eax@22
  signed int v16; // ecx@22
  signed int v17; // ST68_4@22
  int v18; // eax@22
  int v19; // ecx@22
  unsigned int v20; // esi@23
  int v21; // edi@23
  int v22; // eax@23
  int *v23; // ebx@24
  int v24; // edx@24
  int v25; // ebx@25
  unsigned __int16 v26; // cx@25
  int v27; // edx@26
  unsigned __int16 v28; // cx@26
  unsigned __int8 v29; // sf@27
  unsigned __int8 v30; // of@27
  unsigned int v31; // esi@29
  int v32; // edi@29
  unsigned __int16 *v33; // eax@29
  int *v34; // ebx@30
  int v35; // edx@30
  int v36; // ebx@31
  unsigned __int16 v37; // cx@31
  int v38; // edx@32
  unsigned __int16 v39; // cx@32
  Texture *v40; // [sp-10h] [bp-6Ch]@16
  int v41; // [sp-Ch] [bp-68h]@15
  unsigned int v42; // [sp+10h] [bp-4Ch]@1
  signed int v43; // [sp+14h] [bp-48h]@12
  signed int v44; // [sp+14h] [bp-48h]@22
  int v45; // [sp+1Ch] [bp-40h]@22
  int v46; // [sp+20h] [bp-3Ch]@22
  int v47; // [sp+24h] [bp-38h]@19
  char v48; // [sp+28h] [bp-34h]@19
  int v49; // [sp+2Ch] [bp-30h]@19
  unsigned __int8 *v50; // [sp+30h] [bp-2Ch]@19
  unsigned __int16 *v51; // [sp+34h] [bp-28h]@19
  int v52; // [sp+38h] [bp-24h]@22
  int v53; // [sp+3Ch] [bp-20h]@22
  signed int v54; // [sp+40h] [bp-1Ch]@12
  int v55; // [sp+40h] [bp-1Ch]@20
  int v56; // [sp+44h] [bp-18h]@20
  stru352 *i; // [sp+48h] [bp-14h]@20
  unsigned __int16 *v58; // [sp+4Ch] [bp-10h]@23
  int v59; // [sp+50h] [bp-Ch]@4
  int v60; // [sp+50h] [bp-Ch]@19
  int v61; // [sp+54h] [bp-8h]@22
  int *v62; // [sp+58h] [bp-4h]@23
  int *v63; // [sp+58h] [bp-4h]@29

  v1 = uFaceID;
  v42 = pRenderer->uTargetSurfacePitch;
  if ( uFaceID >= 0 && uFaceID < (signed int)pIndoor->uNumFaces )
  {
    v2 = &pIndoor->pFaces[uFaceID];
    if ( pRenderer->pRenderD3D )
    {
      v3 = sub_424579(uFaceID, &stru_F8AD28);
      v59 = v3;
    }
    else
    {
      v59 = sub_423B5D(uFaceID);
      v3 = v59;
    }
    if ( v3 && (pRenderer->pRenderD3D || sub_424829(v3, &stru_F8A590, pBLVRenderParams->field_7C, v1)) )
    {
      v4 = v2->GetTexture();
      v5 = 0;
      if ( v4 )
      {
        if ( pRenderer->pRenderD3D )
        {
          if ( v3 > 0 )
          {
            v54 = v3;
            v43 = v3;
            v6 = (char *)&array_507D30[0].v;
            do
            {
              *((float *)v6 - 1) = (double)((GetTickCount() >> 5) - pBLVRenderParams->vPartyPos.x) + *((float *)v6 - 1);
              *(float *)v6 = (double)(pBLVRenderParams->vPartyPos.y + (GetTickCount() >> 5)) + *(float *)v6;
              v6 += 48;
              --v54;
            }
            while ( v54 );
            v3 = v59;
            v5 = v43;
          }
          v7 = v5;
          v41 = stru_F8AD28.field_0;
          array_507D30[v7].u = array_507D30[v7].u * 0.25;
          array_507D30[v7].v = array_507D30[v7].v * 0.25;
          if ( BYTE1(v2->uAttributes) & 0x40 )
          {
            v40 = v2->GetTexture();
            v8 = pTextureFrameTable->GetFrameTexture(v2->uBitmapID, pBLVRenderParams->field_0_timer_);
          }
          else
          {
            v40 = v2->GetTexture();
            v8 = v2->uBitmapID;
          }
          pRenderer->DrawIndoorPolygon(v3, v2, pBitmaps_LOD->pHardwareTextures[v8], v40, v41, -1, 0);
        }
        else
        {
          v49 = v4->uWidthMinus1;
          v47 = v4->uHeightMinus1 << 16;
          v9 = 16 - v4->uWidthLn2;
          v10 = v4->pLevelOfDetail0_prolly_alpha_mask;
          LOBYTE(v2->uAttributes) |= 0x80u;
          v48 = v9;
          v50 = v10;
          sub_4AF412();
          ++pBLVRenderParams->uNumFacesRenderedThisFrame;
          v11 = sr_sub_47C24C_get_palette(v2, v4->palette_id2, 0, 1);
          v12 = stru_F8A590._viewport_space_y;
          v51 = v11;
          v60 = stru_F8A590._viewport_space_y;
          if ( stru_F8A590._viewport_space_y <= stru_F8A590._viewport_space_w )
          {
            v13 = 2 * stru_F8A590._viewport_space_y;
            v14 = &stru_F83B80[stru_F8A590._viewport_space_y];
            v55 = 2 * stru_F8A590._viewport_space_y;
            v56 = 640 * stru_F8A590._viewport_space_y;
            for ( i = &stru_F83B80[stru_F8A590._viewport_space_y]; ; v14 = i )
            {
              sub_4AE1E7(v12, *(__int16 *)((char *)stru_F8A590.viewport_left_side + v13), v12);
              v14->field_0 += (GetTickCount() << 11) - (pBLVRenderParams->vPartyPos.x << 16);
              v15 = GetTickCount();
              v16 = v14->field_0;
              v14->field_4 += (32 * pBLVRenderParams->vPartyPos.y + v15) << 11;
              v45 = v14->field_4 >> 3;
              v44 = v16 >> 3;
              v17 = (signed int)((unsigned __int64)(SLODWORD(pBLVRenderParams->field_44) * (signed __int64)v14->field_28) >> 16) >> 3;
              v52 = (unsigned __int64)(v17 * (signed __int64)-pBLVRenderParams->sSineY) >> 16;
              v53 = (unsigned __int64)(v17 * (signed __int64)pBLVRenderParams->sCosineY) >> 16;
              v18 = v14->field_28;
              v19 = *(__int16 *)((char *)stru_F8A590.viewport_left_side + v13);
              LOWORD(v18) = 0;
              v46 = stru_F8AD28.field_0 | v18;
              v61 = *(__int16 *)((char *)stru_F8A590.viewport_right_side + v13) - v19;
              if ( LOBYTE(viewparams->field_20) )
              {
                v63 = &pBLVRenderParams->pTargetZBuffer[2 * (v19 + 320 * (v13 - pBLVRenderParams->uViewportY))
                                                     - pBLVRenderParams->uViewportX];
                v31 = v44;
                v32 = v45;
                v33 = &pBLVRenderParams->pRenderTarget[v42 * (v13 - pBLVRenderParams->uViewportY)
                                                    + 2 * v19
                                                    - pBLVRenderParams->uViewportX];
                if ( v61 & 1 )
                {
                  --v61;
                  v33 = &pBLVRenderParams->pRenderTarget[v42 * (v13 - pBLVRenderParams->uViewportY)
                                                      + 2 * v19
                                                      - pBLVRenderParams->uViewportX
                                                      - 2];
                  v34 = &pBLVRenderParams->pTargetZBuffer[2 * (v19 + 320 * (v13 - pBLVRenderParams->uViewportY))
                                                       - pBLVRenderParams->uViewportX];
                  v35 = v46;
                  v63 += 2;
                  goto LABEL_32;
                }
                while ( 1 )
                {
                  v30 = __OFSUB__(v61, 2);
                  v29 = v61 - 2 < 0;
                  v61 -= 2;
                  if ( v29 ^ v30 )
                    break;
                  v36 = *(&v50[v49 & (v31 >> 16)] + ((v47 & (unsigned int)v32) >> v48));
                  v31 += v52;
                  v37 = v51[v36];
                  v32 += v53;
                  v34 = v63;
                  v35 = v46;
                  *v33 = v37;
                  v33[1] = v37;
                  v33[640] = v37;
                  v33[641] = v37;
                  v63 += 4;
                  v34[2] = v46;
                  v34[3] = v46;
                  v34[642] = v46;
                  v34[643] = v46;
LABEL_32:
                  *v34 = v35;
                  v34[1] = v35;
                  v34[640] = v35;
                  v34[641] = v35;
                  v38 = v49 & (v31 >> 16);
                  v33 += 4;
                  v31 += v52;
                  v39 = v51[*(&v50[v38] + ((v47 & (unsigned int)v32) >> v48))];
                  v32 += v53;
                  *(v33 - 2) = v39;
                  *(v33 - 1) = v39;
                  v33[638] = v39;
                  v33[639] = v39;
                }
              }
              else
              {
                v58 = &pBLVRenderParams->pRenderTarget[v19 + v12 * pRenderer->uTargetSurfacePitch];
                v62 = &pBLVRenderParams->pTargetZBuffer[v56 + v19];
                v20 = v44;
                v21 = v45;
                v22 = (int)v58;
                if ( v61 & 1 )
                {
                  --v61;
                  v22 = (int)(v58 - 1);
                  v23 = &pBLVRenderParams->pTargetZBuffer[v56 + v19];
                  v24 = v46;
                  ++v62;
                  goto LABEL_26;
                }
                while ( 1 )
                {
                  v30 = __OFSUB__(v61, 2);
                  v29 = v61 - 2 < 0;
                  v61 -= 2;
                  if ( v29 ^ v30 )
                    break;
                  v25 = *(&v50[v49 & (v20 >> 16)] + ((v47 & (unsigned int)v21) >> v48));
                  v20 += v52;
                  v26 = v51[v25];
                  v21 += v53;
                  v23 = v62;
                  v24 = v46;
                  *(short *)v22 = v26;
                  v62 += 2;
                  v23[1] = v46;
LABEL_26:
                  *v23 = v24;
                  v27 = v49 & (v20 >> 16);
                  v22 += 4;
                  v20 += v52;
                  v28 = v51[*(&v50[v27] + ((v47 & (unsigned int)v21) >> v48))];
                  v21 += v53;
                  *(short *)(v22 - 2) = v28;
                }
              }
              ++v60;
              ++i;
              v56 += 640;
              v55 += 2;
              if ( v60 > stru_F8A590._viewport_space_w )
                break;
              v13 = v55;
              v12 = v60;
            }
          }
        }
      }
    }
  }
}

//----- (004AE1E7) --------------------------------------------------------
int __fastcall sub_4AE1E7(int a1, int a2, int a3)
{
  int v3; // ebx@1
  int v4; // edi@1
  int v5; // esi@1
  signed __int64 v6; // qtt@3
  int v7; // esi@3
  int v8; // eax@5
  int result; // eax@5
  int v10; // edx@5
  int v11; // [sp+Ch] [bp-8h]@1
  int v12; // [sp+1Ch] [bp+8h]@2

  v3 = pBLVRenderParams->uViewportCenterY - a3;
  v4 = pBLVRenderParams->uViewportCenterX - a2;
  v11 = a1;
  v5 = (pBLVRenderParams->uViewportCenterY - a3) * stru_F8AD28.vec_80.y
     + stru_F8AD28.field_7C
     + (pBLVRenderParams->uViewportCenterX - a2) * stru_F8AD28.vec_80.x;
  if ( v5 && (v12 = abs(stru_F8AD28.vec_80.z) >> 14, v12 <= abs(v5)) )
  {
    LODWORD(v6) = stru_F8AD28.vec_80.z << 16;
    HIDWORD(v6) = stru_F8AD28.vec_80.z >> 16;
    v7 = v6 / (v3 * stru_F8AD28.vec_80.y + stru_F8AD28.field_7C + v4 * stru_F8AD28.vec_80.x);
  }
  else
  {
    v7 = 1073741824;
  }
  v8 = stru_F8AD28.vec_9C.z;
  stru_F83B80[v11].field_0 = ((unsigned __int64)((v3 * stru_F8AD28.vec_8C.z
                                                + stru_F8AD28.vec_8C.x
                                                + v4 * stru_F8AD28.vec_8C.y)
                                               * (signed __int64)v7) >> 16)
                           + stru_F8AD28.field_98;
  result = (unsigned __int64)((v3 * v8 + stru_F8AD28.vec_9C.x + v4 * stru_F8AD28.vec_9C.y) * (signed __int64)v7) >> 16;
  v10 = result + stru_F8AD28.field_A8;
  stru_F83B80[v11].field_28 = v7;
  stru_F83B80[v11].field_4 = v10;
  return result;
}

//----- (004AE313) --------------------------------------------------------
int __fastcall sub_4AE313(int viewport_space_x, int viewport_space_y, stru337_stru0 *p)
{
  int _dy; // ebx@1
  int _dx; // edi@1
  int v5; // ecx@1
  int v6; // esi@1
  int v7; // ST18_4@2
  signed __int64 v8; // qtt@3
  unsigned int v9; // ecx@3
  int result; // eax@8
  int v11; // [sp+Ch] [bp-8h]@1

  _dy = pBLVRenderParams->uViewportCenterY - viewport_space_y;
  _dx = pBLVRenderParams->uViewportCenterX - viewport_space_x;
  ++pBLVRenderParams->field_88;
  v5 = (pBLVRenderParams->uViewportCenterY - viewport_space_y) * stru_F8AD28.vec_80.y + stru_F8AD28.field_7C;
  v6 = v5 + _dx * stru_F8AD28.vec_80.x;
  v11 = v5 + _dx * stru_F8AD28.vec_80.x;
  if ( v5 + _dx * stru_F8AD28.vec_80.x && (v7 = abs(stru_F8AD28.vec_80.z) >> 14, v7 <= abs(v6)) )
  {
    LODWORD(v8) = stru_F8AD28.vec_80.z << 16;
    HIDWORD(v8) = stru_F8AD28.vec_80.z >> 16;
    v9 = v8 / v11;
  }
  else
  {
    v9 = 0x40000000u;
  }
  if ( (signed int)v9 >= stru_F8AD28.field_34 )
    p->field_0 = v9;
  else
    p->field_0 = stru_F8AD28.field_34;
  p->field_4 = ((unsigned __int64)((_dy * stru_F8AD28.vec_8C.z + stru_F8AD28.vec_8C.x + _dx * stru_F8AD28.vec_8C.y)
                                 * (signed __int64)(signed int)v9) >> 16)
             + stru_F8AD28.field_98
             + (stru_F8AD28.pDeltaUV[0] << 16);
  p->field_8 = ((unsigned __int64)((_dy * stru_F8AD28.vec_9C.z + stru_F8AD28.vec_9C.x + _dx * stru_F8AD28.vec_9C.y)
                                 * (signed __int64)(signed int)v9) >> 16)
             + stru_F8AD28.field_A8
             + (stru_F8AD28.pDeltaUV[1] << 16);
  result = abs((__int64)(SLODWORD(pBLVRenderParams->field_44) * (signed __int64)(signed int)v9) >> 16);
  if ( result < 369620 || bUseLoResSprites )
  {
    if ( result < 184810 )
      p->field_10 = result > 92405;
    else
      p->field_10 = 2;
  }
  else
  {
    p->field_10 = 3;
  }
  return result;
}

//----- (004AE491) --------------------------------------------------------
int __fastcall sub_4AE491(signed int a1, signed int a2)
{
  signed int v2; // eax@1
  signed int v3; // edi@1
  int v4; // ecx@1
  int v5; // esi@2
  int v6; // eax@2
  int v7; // ebx@2
  unsigned int v8; // ecx@2
  int v9; // edx@5
  int v10; // edx@7
  unsigned int v11; // eax@8
  int v13; // [sp+4h] [bp-18h]@2
  int v14; // [sp+8h] [bp-14h]@2
  signed int v15; // [sp+Ch] [bp-10h]@1
  int v16; // [sp+14h] [bp-8h]@1
  int v17; // [sp+18h] [bp-4h]@1

  v2 = a1 >> SLOBYTE(stru_F8AD28.field_38);
  v3 = a2 >> SLOBYTE(stru_F8AD28.field_38);
  v17 = stru_F8AD28.uCurrentAmbientLightLevel;
  v4 = 0;
  v15 = v2;
  v16 = 0;
  if ( stru_F8AD28.uNumLightsApplied > 0 )
  {
    do
    {
      v5 = v16;
      v13 = abs(v2 - stru_F8AD28._blv_lights_xs[v16]);
      v14 = abs(v3 - stru_F8AD28._blv_lights_ys[v16]);
      v6 = stru_F8AD28._blv_lights_light_dot_faces[v16];
      v7 = v13;
      v8 = v14;
      if ( v6 < v13 )
      {
        v6 = v13;
        v7 = stru_F8AD28._blv_lights_light_dot_faces[v16];
      }
      if ( v6 < v14 )
      {
        v9 = v6;
        v6 = v14;
        v8 = v9;
      }
      if ( v7 < (signed int)v8 )
      {
        v10 = v8;
        v8 = v7;
        v7 = v10;
      }
      v11 = ((unsigned int)(11 * v7) >> 5) + (v8 >> 2) + v6;
      if ( (signed int)v11 < stru_F8AD28._blv_lights_radii[v5] )
        v17 += 30 * (v11 * stru_F8AD28._blv_lights_inv_radii[v5] - 65536);
      ++v16;
      v2 = v15;
    }
    while ( v16 < stru_F8AD28.uNumLightsApplied );
    v4 = 0;
  }
  if ( stru_F8AD28.field_3E4 != v4 )
    v17 -= stru_F8AD28.field_3E8 * (v2 - stru_F8AD28.field_3F0) + stru_F8AD28.field_3EC * (v3 - stru_F8AD28.field_3F4);
  if ( v17 >= v4 )
  {
    if ( v17 > 2031616 )
      v17 = 2031616;
  }
  else
  {
    v17 = v4;
  }
  ++pBLVRenderParams->field_8C;
  return v17;
}
// 4AE491: using guessed type int __fastcall sub_4AE491(int, int);

//----- (004AE5F1) --------------------------------------------------------
void __fastcall sub_4AE5F1(unsigned int uFaceID)
{
  BLVFace *v1; // esi@1
  BLVFaceExtra *v2; // ebx@1
  int v3; // eax@1
  int v4; // edi@1
  Texture *v5; // edi@1
  int v6; // eax@1
  unsigned int v7; // eax@1
  unsigned int v8; // ecx@1
  unsigned int v9; // eax@1
  unsigned int v10; // ecx@5
  int v11; // edi@10
  int v12; // ecx@10
  int v13; // eax@10
  int v14; // edx@10
  int v15; // ebx@12
  double v16; // st7@16
  int v17; // eax@16
  char *v18; // ebx@17
  int v19; // ecx@19
  int v20; // eax@19
  int v21; // edx@21
  int v22; // eax@23
  int v23; // ST04_4@26
  int v24; // edi@26
  double v25; // st6@26
  int v26; // eax@26
  double v27; // st6@26
  int v28; // ecx@26
  char v29; // al@26
  int v30; // edx@28
  int v31; // ecx@28
  BLVLightMM7 *v32; // ecx@32
  int v33; // edi@33
  int v34; // edx@33
  int v35; // eax@33
  int v36; // edi@35
  int v37; // edx@37
  int v38; // ebx@39
  int v39; // edi@42
  int v40; // eax@42
  char *v41; // ebx@45
  signed int v42; // ecx@47
  int v43; // edi@47
  int v44; // eax@49
  int v45; // edx@51
  int v46; // eax@53
  int v47; // ST04_4@55
  int v48; // edi@55
  double v49; // st6@55
  int v50; // eax@55
  double v51; // st6@55
  int v52; // eax@55
  int v53; // ecx@57
  int v54; // ecx@58
  int v55; // ecx@59
  int v56; // edx@62
  int v57; // ecx@62
  int v58; // eax@63
  int v59; // edx@64
  int v60; // ecx@67
  int v61; // edx@67
  int v62; // eax@68
  int v63; // edx@69
  signed int v64; // ecx@72
  double v65; // st7@75
  Vec3_int_ v66; // [sp+Ch] [bp-34h]@9
  Vec3_int_ v67; // [sp+18h] [bp-28h]@9
  BLVFaceExtra *v68; // [sp+24h] [bp-1Ch]@1
  int v69; // [sp+28h] [bp-18h]@10
  int v70; // [sp+2Ch] [bp-14h]@10
  int X; // [sp+30h] [bp-10h]@10
  int v72; // [sp+34h] [bp-Ch]@10
  int v73; // [sp+38h] [bp-8h]@10
  int v74; // [sp+3Ch] [bp-4h]@10

  v1 = &pIndoor->pFaces[uFaceID];
  v2 = &pIndoor->pFaceExtras[v1->uFaceExtraID];
  v3 = v1->uBitmapID;
  v4 = v1->uBitmapID;
  v68 = v2;
  v5 = (Texture *)(v4 != -1 ? (int)&pBitmaps_LOD->pTextures[v3] : 0);
  v6 = 8 * uFaceID;
  LOBYTE(v6) = PID(OBJECT_BModel,uFaceID);
  stru_F8AD28.field_0 = v6;
  stru_F8AD28.plane_4.vNormal.x = v1->pFacePlane_old.vNormal.x;
  stru_F8AD28.plane_4.vNormal.y = v1->pFacePlane_old.vNormal.y;
  stru_F8AD28.plane_4.vNormal.z = v1->pFacePlane_old.vNormal.z;
  stru_F8AD28.plane_4.dist = v1->pFacePlane_old.dist;
  stru_F8AD28.pDeltaUV[0] = v2->sTextureDeltaU;
  stru_F8AD28.pDeltaUV[1] = v2->sTextureDeltaV;
  v7 = GetTickCount();
  v8 = v1->uAttributes;
  v9 = v7 >> 3;
  if ( v8 & 4 )
  {
    stru_F8AD28.pDeltaUV[1] -= v9 & v5->uHeightMinus1;
  }
  else
  {
    if ( v8 & 0x20 )
      stru_F8AD28.pDeltaUV[1] += v9 & v5->uHeightMinus1;
  }
  v10 = v1->uAttributes;
  if ( BYTE1(v10) & 8 )
  {
    stru_F8AD28.pDeltaUV[0] -= v9 & v5->uWidthMinus1;
  }
  else
  {
    if ( v10 & 0x40 )
      stru_F8AD28.pDeltaUV[0] += v9 & v5->uWidthMinus1;
  }
  v1->_get_normals(&v67, &v66);
  stru_F8AD28.vec_14.x = v67.x;
  stru_F8AD28.vec_14.y = v67.y;
  stru_F8AD28.vec_14.z = v67.z;
  stru_F8AD28.vec_20.x = v66.x;
  stru_F8AD28.vec_20.y = v66.y;
  stru_F8AD28.vec_20.z = v66.z;
  stru_F8AD28.uDefaultAmbientLightLevel = v2->field_22;
  if ( pBLVRenderParams->sPartyRotX )
  {
    v74 = (unsigned __int64)(stru_F8AD28.plane_4.vNormal.y * (signed __int64)pBLVRenderParams->sSineY) >> 16;
    v74 = ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.x * (signed __int64)pBLVRenderParams->sCosineY) >> 16) - v74;
    X = (unsigned __int64)(stru_F8AD28.plane_4.vNormal.z * (signed __int64)pBLVRenderParams->sSineNegX) >> 16;
    stru_F8AD28.rotated_normal.x = ((unsigned __int64)(v74 * (signed __int64)pBLVRenderParams->sCosineNegX) >> 16) - X;
    stru_F8AD28.rotated_normal.y = ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.x
                                                     * (signed __int64)pBLVRenderParams->sSineY) >> 16)
                                 + ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.y
                                                     * (signed __int64)pBLVRenderParams->sCosineY) >> 16);
    stru_F8AD28.rotated_normal.z = ((unsigned __int64)(v74 * (signed __int64)pBLVRenderParams->sSineNegX) >> 16)
                                 + ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.z
                                                     * (signed __int64)pBLVRenderParams->sCosineNegX) >> 16);
    v70 = (unsigned __int64)(stru_F8AD28.vec_14.y * (signed __int64)pBLVRenderParams->sSineY) >> 16;
    v74 = ((unsigned __int64)(stru_F8AD28.vec_14.x * (signed __int64)pBLVRenderParams->sCosineY) >> 16) - v70;
    v70 = (unsigned __int64)(stru_F8AD28.vec_14.z * (signed __int64)pBLVRenderParams->sSineNegX) >> 16;
    stru_F8AD28.vec_60.y = ((unsigned __int64)(v74 * (signed __int64)pBLVRenderParams->sCosineNegX) >> 16) - v70;
    stru_F8AD28.vec_60.z = ((unsigned __int64)(stru_F8AD28.vec_14.x * (signed __int64)pBLVRenderParams->sSineY) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.vec_14.y * (signed __int64)pBLVRenderParams->sCosineY) >> 16);
    stru_F8AD28.field_6C = ((unsigned __int64)(v74 * (signed __int64)pBLVRenderParams->sSineNegX) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.vec_14.z * (signed __int64)pBLVRenderParams->sCosineNegX) >> 16);
    v70 = (unsigned __int64)(stru_F8AD28.vec_20.y * (signed __int64)pBLVRenderParams->sSineY) >> 16;
    v74 = ((unsigned __int64)(stru_F8AD28.vec_20.x * (signed __int64)pBLVRenderParams->sCosineY) >> 16) - v70;
    X = (unsigned __int64)(stru_F8AD28.vec_20.x * (signed __int64)pBLVRenderParams->sSineY) >> 16;
    v72 = (unsigned __int64)(stru_F8AD28.vec_20.y * (signed __int64)pBLVRenderParams->sCosineY) >> 16;
    v70 = (unsigned __int64)(stru_F8AD28.vec_20.z * (signed __int64)pBLVRenderParams->sSineNegX) >> 16;
    stru_F8AD28.vec_70.x = ((unsigned __int64)(v74 * (signed __int64)pBLVRenderParams->sCosineNegX) >> 16) - v70;
    stru_F8AD28.vec_70.y = ((unsigned __int64)(stru_F8AD28.vec_20.x * (signed __int64)pBLVRenderParams->sSineY) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.vec_20.y * (signed __int64)pBLVRenderParams->sCosineY) >> 16);
    stru_F8AD28.vec_70.z = ((unsigned __int64)(v74 * (signed __int64)pBLVRenderParams->sSineNegX) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.vec_20.z * (signed __int64)pBLVRenderParams->sCosineNegX) >> 16);
    v74 = pBLVRenderParams->sSineY * pBLVRenderParams->vPartyPos.y
        - pBLVRenderParams->sCosineY * pBLVRenderParams->vPartyPos.x;
    v11 = -(pBLVRenderParams->sCosineY * pBLVRenderParams->vPartyPos.y
          + pBLVRenderParams->sSineY * pBLVRenderParams->vPartyPos.x);
    v73 = -65536 * pBLVRenderParams->vPartyPos.z;
    v70 = (unsigned __int64)(-65536 * pBLVRenderParams->vPartyPos.z * (signed __int64)pBLVRenderParams->sSineNegX) >> 16;
    v12 = ((unsigned __int64)(v74 * (signed __int64)pBLVRenderParams->sCosineNegX) >> 16) - v70;
    v69 = (unsigned __int64)(v74 * (signed __int64)pBLVRenderParams->sSineNegX) >> 16;
    v13 = pBLVRenderParams->vPartyPos.y;
    v70 = ((unsigned __int64)(v74 * (signed __int64)pBLVRenderParams->sSineNegX) >> 16)
        + ((unsigned __int64)(-65536 * pBLVRenderParams->vPartyPos.z * (signed __int64)pBLVRenderParams->sCosineNegX) >> 16);
    v14 = pBLVRenderParams->vPartyPos.x;
  }
  else
  {
    v70 = (unsigned __int64)(stru_F8AD28.plane_4.vNormal.y * (signed __int64)pBLVRenderParams->sSineY) >> 16;
    stru_F8AD28.rotated_normal.x = ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.x
                                                     * (signed __int64)pBLVRenderParams->sCosineY) >> 16)
                                 - v70;
    stru_F8AD28.rotated_normal.z = stru_F8AD28.plane_4.vNormal.z;
    stru_F8AD28.rotated_normal.y = ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.x
                                                     * (signed __int64)pBLVRenderParams->sSineY) >> 16)
                                 + ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.y
                                                     * (signed __int64)pBLVRenderParams->sCosineY) >> 16);
    v70 = (unsigned __int64)(stru_F8AD28.vec_14.y * (signed __int64)pBLVRenderParams->sSineY) >> 16;
    stru_F8AD28.vec_60.y = ((unsigned __int64)(stru_F8AD28.vec_14.x * (signed __int64)pBLVRenderParams->sCosineY) >> 16)
                         - v70;
    stru_F8AD28.field_6C = stru_F8AD28.vec_14.z;
    stru_F8AD28.vec_60.z = ((unsigned __int64)(stru_F8AD28.vec_14.x * (signed __int64)pBLVRenderParams->sSineY) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.vec_14.y * (signed __int64)pBLVRenderParams->sCosineY) >> 16);
    v70 = (unsigned __int64)(stru_F8AD28.vec_20.y * (signed __int64)pBLVRenderParams->sSineY) >> 16;
    stru_F8AD28.vec_70.x = ((unsigned __int64)(stru_F8AD28.vec_20.x * (signed __int64)pBLVRenderParams->sCosineY) >> 16)
                         - v70;
    v69 = (unsigned __int64)(stru_F8AD28.vec_20.x * (signed __int64)pBLVRenderParams->sSineY) >> 16;
    v14 = pBLVRenderParams->vPartyPos.x;
    stru_F8AD28.vec_70.z = stru_F8AD28.vec_20.z;
    v13 = pBLVRenderParams->vPartyPos.y;
    stru_F8AD28.vec_70.y = ((unsigned __int64)(stru_F8AD28.vec_20.x * (signed __int64)pBLVRenderParams->sSineY) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.vec_20.y * (signed __int64)pBLVRenderParams->sCosineY) >> 16);
    v12 = pBLVRenderParams->sSineY * pBLVRenderParams->vPartyPos.y
        - pBLVRenderParams->sCosineY * pBLVRenderParams->vPartyPos.x;
    v11 = -(pBLVRenderParams->sCosineY * pBLVRenderParams->vPartyPos.y
          + pBLVRenderParams->sSineY * pBLVRenderParams->vPartyPos.x);
    v70 = -65536 * pBLVRenderParams->vPartyPos.z;
  }
  stru_F8AD28.field_7C = stru_F8AD28.rotated_normal.x;
  stru_F8AD28.vec_60.x = stru_F8AD28.plane_4.vNormal.z * pBLVRenderParams->vPartyPos.z
                       + stru_F8AD28.plane_4.dist
                       + stru_F8AD28.plane_4.vNormal.y * v13
                       + stru_F8AD28.plane_4.vNormal.x * v14;
  stru_F8AD28.vec_80.x = (unsigned __int64)(SLODWORD(pBLVRenderParams->field_44)
                                          * (signed __int64)stru_F8AD28.rotated_normal.y) >> 16;
  stru_F8AD28.vec_80.y = (unsigned __int64)(SLODWORD(pBLVRenderParams->field_44)
                                          * (signed __int64)stru_F8AD28.rotated_normal.z) >> 16;
  stru_F8AD28.vec_80.z = -stru_F8AD28.vec_60.x;
  stru_F8AD28.vec_8C.x = stru_F8AD28.vec_60.y;
  stru_F8AD28.vec_8C.y = (unsigned __int64)(SLODWORD(pBLVRenderParams->field_44) * (signed __int64)stru_F8AD28.vec_60.z) >> 16;
  stru_F8AD28.vec_8C.z = (unsigned __int64)(SLODWORD(pBLVRenderParams->field_44) * (signed __int64)stru_F8AD28.field_6C) >> 16;
  X = (unsigned __int64)(stru_F8AD28.vec_60.y * (signed __int64)v12) >> 16;
  v15 = v70;
  v70 = (unsigned __int64)(stru_F8AD28.field_6C * (signed __int64)v70) >> 16;
  stru_F8AD28.vec_9C.x = stru_F8AD28.vec_70.x;
  stru_F8AD28.field_98 = -(X + ((unsigned __int64)(stru_F8AD28.vec_60.z * (signed __int64)v11) >> 16) + v70);
  stru_F8AD28.vec_9C.y = (unsigned __int64)(SLODWORD(pBLVRenderParams->field_44) * (signed __int64)stru_F8AD28.vec_70.y) >> 16;
  stru_F8AD28.vec_9C.z = (unsigned __int64)(SLODWORD(pBLVRenderParams->field_44) * (signed __int64)stru_F8AD28.vec_70.z) >> 16;
  X = (unsigned __int64)(stru_F8AD28.vec_70.x * (signed __int64)v12) >> 16;
  v69 = (unsigned __int64)(stru_F8AD28.vec_70.y * (signed __int64)v11) >> 16;
  v70 = (unsigned __int64)(stru_F8AD28.vec_70.z * (signed __int64)v15) >> 16;
  stru_F8AD28.field_38 = 0;
  stru_F8AD28.field_A8 = -(X
                         + ((unsigned __int64)(stru_F8AD28.vec_70.y * (signed __int64)v11) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.vec_70.z * (signed __int64)v15) >> 16));
  if ( *(int *)&v68->field_4 || *(int *)&v68->field_8 )
  {
    stru_F8AD28.field_3E4 = 1;
    stru_F8AD28.field_3E8 = *(int *)&v68->field_4;
    stru_F8AD28.field_3EC = *(int *)&v68->field_8;
    stru_F8AD28.field_3F0 = v68->field_1E;
    stru_F8AD28.field_3F4 = v68->field_20;
  }
  else
  {
    stru_F8AD28.field_3E4 = 0;
  }
  v16 = 0.0039215689;
  v17 = 116 * v1->uSectorID;
  v69 = v17;
  v74 = 0;
  v73 = 0;
  stru_F8AD28.uCurrentAmbientLightLevel = (stru_F8AD28.uDefaultAmbientLightLevel + *(__int16 *)((char *)&pIndoor->pSectors->uMinAmbientLightLevel + v17)) << 16;
  v70 = pMobileLightsStack->uNumLightsActive;
  if ( pMobileLightsStack->uNumLightsActive > 0 )
  {
    v18 = (char *)&pMobileLightsStack->pLights[0].vPosition.y;
    do
    {
      if ( v74 >= 20 )
        break;
      v19 = *((short *)v18 + 2);
      v20 = *((short *)v18 - 1);
      if ( v20 > v1->pBounding.x1 - v19 )
      {
        if ( v20 < v19 + v1->pBounding.x2 )
        {
          v21 = *(short *)v18;
          if ( v21 > v1->pBounding.y1 - v19 )
          {
            if ( v21 < v19 + v1->pBounding.y2 )
            {
              v22 = *((short *)v18 + 1);
              if ( v22 > v1->pBounding.z1 - v19 )
              {
                if ( v22 < v19 + v1->pBounding.z2 )
                {
                  X = (v1->pFacePlane_old.dist
                     + *((short *)v18 + 1) * v1->pFacePlane_old.vNormal.z
                     + v21 * v1->pFacePlane_old.vNormal.y
                     + *((short *)v18 - 1) * v1->pFacePlane_old.vNormal.x) >> 16;
                  if ( X <= v19 )
                  {
                    v23 = X;
                    v24 = v74;
                    stru_F8AD28._blv_lights_radii[v74] = v19;
                    stru_F8AD28._blv_lights_inv_radii[v24] = 65536 / v19;
                    *(int *)((char *)&stru_F8AD28.field_240 + v24 * 4) = *((short *)v18 + 5) << 16;
                    stru_F8AD28._blv_lights_xs[v24] = *((short *)v18 - 1);
                    stru_F8AD28._blv_lights_ys[v24] = *(short *)v18;
                    stru_F8AD28._blv_lights_zs[v24] = *((short *)v18 + 1);
                    v68 = (BLVFaceExtra *)(unsigned __int8)v18[6];
                    v25 = (double)(signed int)v68 * v16;
                    v68 = (BLVFaceExtra *)(unsigned __int8)v18[7];
                    v26 = (unsigned __int8)v18[8];
                    *(float *)(v24 * 4 + 16297992) = v25;
                    v27 = (double)(signed int)v68;
                    v68 = (BLVFaceExtra *)v26;
                    *(float *)(v24 * 4 + 16298072) = v27 * v16;
                    *(float *)(v24 * 4 + 16298152) = (double)(signed int)v68 * v16;
                    v16 = 0.0039215689;
                    stru_F8AD28._blv_lights_light_dot_faces[v24] = abs(v23);
                    v28 = v74;
                    v29 = v18[9];
                    ++v74;
                    stru_F8AD28._blv_lights_types[v28] = v29;
                  }
                }
              }
            }
          }
        }
      }
      ++v73;
      v18 += 18;
      v17 = v69;
    }
    while ( v73 < v70 );
  }
  v30 = 0;
  v73 = 0;
  v31 = *(__int16 *)((char *)&pIndoor->pSectors->uNumLights + v17);
  v70 = *(__int16 *)((char *)&pIndoor->pSectors->uNumLights + v17);
  if ( v31 > 0 )
  {
    while ( v74 < 20 )
    {
      v32 = &pIndoor->pLights[*(&(*(BLVLightMM7 **)((char *)&pIndoor->pSectors->pLights + v17))->vPosition.x + v30)];
      if ( !(v32->uAtributes & 8) )
      {
        v33 = v1->pBounding.x1;
        v34 = v32->vPosition.x;
        X = v32->uBrightness;
        v35 = v32->uRadius;
        v68 = (BLVFaceExtra *)v32->uRadius;
        if ( v34 > v33 - v35 )
        {
          if ( v34 < v35 + v1->pBounding.x2 )
          {
            v36 = v32->vPosition.y;
            if ( v36 > v1->pBounding.y1 - v35 )
            {
              if ( v36 < v35 + v1->pBounding.y2 )
              {
                v37 = v32->vPosition.z;
                if ( v37 > v1->pBounding.z1 - v35 )
                {
                  if ( v37 < v35 + v1->pBounding.z2 )
                  {
                    v38 = (v1->pFacePlane_old.dist
                         + v32->vPosition.x * v1->pFacePlane_old.vNormal.x
                         + v37 * v1->pFacePlane_old.vNormal.z
                         + v36 * v1->pFacePlane_old.vNormal.y) >> 16;
                    if ( v38 >= 0 )
                    {
                      if ( v38 <= v35 && v35 )
                      {
                        v39 = v74;
                        stru_F8AD28._blv_lights_radii[v74] = v35;
                        stru_F8AD28._blv_lights_inv_radii[v39] = 65536 / (signed int)v68;
                        *(int *)((char *)&stru_F8AD28.field_240 + v39 * 4) = X << 16;
                        stru_F8AD28._blv_lights_xs[v39] = v32->vPosition.x;
                        stru_F8AD28._blv_lights_ys[v39] = v32->vPosition.y;
                        stru_F8AD28._blv_lights_zs[v39] = v32->vPosition.z;
                        v68 = (BLVFaceExtra *)v32->uRed;
                        stru_F8AD28._blv_lights_rs[v39] = (double)(signed int)v68 * v16;
                        v68 = (BLVFaceExtra *)v32->uGreen;
                        stru_F8AD28._blv_lights_gs[v39] = (double)(signed int)v68 * v16;
                        v68 = (BLVFaceExtra *)v32->uBlue;
                        stru_F8AD28._blv_lights_bs[v39] = (double)(signed int)v68 * v16;
                        v16 = 0.0039215689;
                        stru_F8AD28._blv_lights_light_dot_faces[v39] = abs(v38);
                        v40 = v74++;
                        stru_F8AD28._blv_lights_types[v40] = 1;
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
      v30 = v73++ + 1;
      if ( v73 >= v70 )
        break;
      v17 = v69;
    }
  }
  v73 = 0;
  if ( pStationaryLightsStack->uNumLightsActive > 0 )
  {
    v41 = (char *)&pStationaryLightsStack->pLights[0].vPosition.y;
    do
    {
      if ( v74 >= 20 )
        break;
      v42 = *((short *)v41 + 2);
      v43 = *((short *)v41 - 1);
      if ( v43 > v1->pBounding.x1 - v42 )
      {
        if ( v43 < v42 + v1->pBounding.x2 )
        {
          v44 = *(short *)v41;
          if ( v44 > v1->pBounding.y1 - v42 )
          {
            if ( v44 < v42 + v1->pBounding.y2 )
            {
              v45 = *((short *)v41 + 1);
              if ( v45 > v1->pBounding.z1 - v42 )
              {
                if ( v45 < v42 + v1->pBounding.z2 )
                {
                  v46 = (v1->pFacePlane_old.dist
                       + *(short *)v41 * v1->pFacePlane_old.vNormal.y
                       + v43 * v1->pFacePlane_old.vNormal.x
                       + v45 * v1->pFacePlane_old.vNormal.z) >> 16;
                  v69 = v46;
                  if ( v46 >= 0 )
                  {
                    if ( v46 <= v42 )
                    {
                      v47 = v69;
                      v48 = v74;
                      stru_F8AD28._blv_lights_radii[v74] = v42;
                      stru_F8AD28._blv_lights_inv_radii[v48] = 65536 / v42;
                      stru_F8AD28._blv_lights_xs[v48] = *((short *)v41 - 1);
                      stru_F8AD28._blv_lights_ys[v48] = *(short *)v41;
                      stru_F8AD28._blv_lights_zs[v48] = *((short *)v41 + 1);
                      v68 = (BLVFaceExtra *)(unsigned __int8)v41[6];
                      v49 = (double)(signed int)v68 * v16;
                      v68 = (BLVFaceExtra *)(unsigned __int8)v41[7];
                      v50 = (unsigned __int8)v41[8];
                      stru_F8AD28._blv_lights_rs[v48] = v49;
                      v51 = (double)(signed int)v68;
                      v68 = (BLVFaceExtra *)v50;
                      stru_F8AD28._blv_lights_gs[v48] = v51 * v16;
                      stru_F8AD28._blv_lights_bs[v48] = (double)(signed int)v68 * v16;
                      v16 = 0.0039215689;
                      stru_F8AD28._blv_lights_light_dot_faces[v48] = abs(v47);
                      v52 = v74++;
                      stru_F8AD28._blv_lights_types[v52] = 1;
                    }
                  }
                }
              }
            }
          }
        }
      }
      ++v73;
      v41 += 12;
    }
    while ( v73 < pStationaryLightsStack->uNumLightsActive );
  }
  stru_F8AD28.uNumLightsApplied = v74;
  v53 = v1->pBounding.x2;
  if ( pBLVRenderParams->vPartyPos.x <= v53 )
  {
    v55 = v1->pBounding.x1;
    if ( pBLVRenderParams->vPartyPos.x >= v55 )
      v54 = 0;
    else
      v54 = v55 - pBLVRenderParams->vPartyPos.x;
  }
  else
  {
    v54 = pBLVRenderParams->vPartyPos.x - v53;
  }
  v56 = v1->pBounding.y2;
  v57 = v54 * v54;
  if ( pBLVRenderParams->vPartyPos.y <= v56 )
  {
    v59 = v1->pBounding.y1;
    if ( pBLVRenderParams->vPartyPos.y >= v59 )
      v58 = 0;
    else
      v58 = v59 - pBLVRenderParams->vPartyPos.y;
  }
  else
  {
    v58 = pBLVRenderParams->vPartyPos.y - v56;
  }
  v60 = v58 * v58 + v57;
  v61 = v1->pBounding.z2;
  if ( pBLVRenderParams->vPartyPos.z <= v61 )
  {
    v63 = v1->pBounding.z1;
    if ( pBLVRenderParams->vPartyPos.z >= v63 )
      v62 = 0;
    else
      v62 = v63 - pBLVRenderParams->vPartyPos.z;
  }
  else
  {
    v62 = pBLVRenderParams->vPartyPos.z - v61;
  }
  v64 = v62 * v62 + v60;
  if ( v64 )
    stru_F8AD28.field_34 = integer_sqrt(v64) << 16;
  else
    stru_F8AD28.field_34 = 0;
  v68 = (BLVFaceExtra *)abs(stru_F8AD28.rotated_normal.y);
  v65 = (double)(signed int)v68;
  if ( v65 >= 655.36 )
  {
    if ( v65 >= 26214.4 )
    {
      if ( v65 >= 45875.2 )
      {
        stru_F8AD28.field_44 = 8;
        stru_F8AD28.field_48 = 3;
      }
      else
      {
        stru_F8AD28.field_44 = 16;
        stru_F8AD28.field_48 = 4;
      }
    }
    else
    {
      stru_F8AD28.field_44 = 32;
      stru_F8AD28.field_48 = 5;
    }
  }
  else
  {
    stru_F8AD28.field_44 = 64;
    stru_F8AD28.field_48 = 6;
  }
}
// 519AB4: using guessed type int uNumStationaryLightsApplied;

//----- (004AF412) --------------------------------------------------------
int __cdecl sub_4AF412()
{
  int v0; // ST20_4@2
  int v1; // ST20_4@2
  int v2; // ST20_4@2
  int v3; // esi@2
  int v4; // ST20_4@2
  int v5; // ecx@2
  int v6; // ebx@2
  int v7; // edi@2
  int v8; // edx@2
  int v9; // eax@2
  int result; // eax@4

  stru_F8AD28.plane_4.vNormal.z = -65536;
  stru_F8AD28.vec_20.y = -65536;
  stru_F8AD28.plane_4.vNormal.x = 0;
  stru_F8AD28.plane_4.vNormal.y = 0;
  stru_F8AD28.plane_4.dist = (pBLVRenderParams->vPartyPos.z + 800) << 16;
  stru_F8AD28.vec_14.x = 65536;
  stru_F8AD28.vec_14.y = 0;
  stru_F8AD28.vec_14.z = 0;
  stru_F8AD28.vec_20.x = 0;
  stru_F8AD28.vec_20.z = 0;
  stru_F8AD28.uDefaultAmbientLightLevel = 0;
  if ( pBLVRenderParams->sPartyRotX )
  {
    v0 = ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.x * (signed __int64)pBLVRenderParams->sCosineY) >> 16)
       - ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.y * (signed __int64)pBLVRenderParams->sSineY) >> 16);
    stru_F8AD28.rotated_normal.x = ((unsigned __int64)(v0 * (signed __int64)pBLVRenderParams->sCosineNegX) >> 16)
                                 - ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.z
                                                     * (signed __int64)pBLVRenderParams->sSineNegX) >> 16);
    stru_F8AD28.rotated_normal.y = ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.x
                                                     * (signed __int64)pBLVRenderParams->sSineY) >> 16)
                                 + ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.y
                                                     * (signed __int64)pBLVRenderParams->sCosineY) >> 16);
    stru_F8AD28.rotated_normal.z = ((unsigned __int64)(v0 * (signed __int64)pBLVRenderParams->sSineNegX) >> 16)
                                 + ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.z
                                                     * (signed __int64)pBLVRenderParams->sCosineNegX) >> 16);
    v1 = ((unsigned __int64)(stru_F8AD28.vec_14.x * (signed __int64)pBLVRenderParams->sCosineY) >> 16)
       - ((unsigned __int64)(stru_F8AD28.vec_14.y * (signed __int64)pBLVRenderParams->sSineY) >> 16);
    stru_F8AD28.vec_60.y = ((unsigned __int64)(v1 * (signed __int64)pBLVRenderParams->sCosineNegX) >> 16)
                         - ((unsigned __int64)(stru_F8AD28.vec_14.z * (signed __int64)pBLVRenderParams->sSineNegX) >> 16);
    stru_F8AD28.vec_60.z = ((unsigned __int64)(stru_F8AD28.vec_14.x * (signed __int64)pBLVRenderParams->sSineY) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.vec_14.y * (signed __int64)pBLVRenderParams->sCosineY) >> 16);
    stru_F8AD28.field_6C = ((unsigned __int64)(v1 * (signed __int64)pBLVRenderParams->sSineNegX) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.vec_14.z * (signed __int64)pBLVRenderParams->sCosineNegX) >> 16);
    v2 = ((unsigned __int64)(stru_F8AD28.vec_20.x * (signed __int64)pBLVRenderParams->sCosineY) >> 16)
       - ((unsigned __int64)(stru_F8AD28.vec_20.y * (signed __int64)pBLVRenderParams->sSineY) >> 16);
    stru_F8AD28.vec_70.x = ((unsigned __int64)(v2 * (signed __int64)pBLVRenderParams->sCosineNegX) >> 16)
                         - ((unsigned __int64)(stru_F8AD28.vec_20.z * (signed __int64)pBLVRenderParams->sSineNegX) >> 16);
    stru_F8AD28.vec_70.y = ((unsigned __int64)(stru_F8AD28.vec_20.x * (signed __int64)pBLVRenderParams->sSineY) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.vec_20.y * (signed __int64)pBLVRenderParams->sCosineY) >> 16);
    stru_F8AD28.vec_70.z = ((unsigned __int64)(v2 * (signed __int64)pBLVRenderParams->sSineNegX) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.vec_20.z * (signed __int64)pBLVRenderParams->sCosineNegX) >> 16);
    v3 = -(pBLVRenderParams->sCosineY * pBLVRenderParams->vPartyPos.y
         + pBLVRenderParams->sSineY * pBLVRenderParams->vPartyPos.x);
    v4 = pBLVRenderParams->sSineY * pBLVRenderParams->vPartyPos.y
       - pBLVRenderParams->sCosineY * pBLVRenderParams->vPartyPos.x;
    v5 = ((unsigned __int64)(v4 * (signed __int64)pBLVRenderParams->sCosineNegX) >> 16)
       - ((unsigned __int64)(-65536 * pBLVRenderParams->vPartyPos.z * (signed __int64)pBLVRenderParams->sSineNegX) >> 16);
    v6 = pBLVRenderParams->vPartyPos.z;
    v7 = ((unsigned __int64)(-65536 * pBLVRenderParams->vPartyPos.z * (signed __int64)pBLVRenderParams->sCosineNegX) >> 16)
       + ((unsigned __int64)(v4 * (signed __int64)pBLVRenderParams->sSineNegX) >> 16);
    v8 = pBLVRenderParams->vPartyPos.y;
    v9 = pBLVRenderParams->vPartyPos.x;
  }
  else
  {
    stru_F8AD28.rotated_normal.x = ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.x
                                                     * (signed __int64)pBLVRenderParams->sCosineY) >> 16)
                                 - ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.y
                                                     * (signed __int64)pBLVRenderParams->sSineY) >> 16);
    stru_F8AD28.rotated_normal.z = stru_F8AD28.plane_4.vNormal.z;
    stru_F8AD28.rotated_normal.y = ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.x
                                                     * (signed __int64)pBLVRenderParams->sSineY) >> 16)
                                 + ((unsigned __int64)(stru_F8AD28.plane_4.vNormal.y
                                                     * (signed __int64)pBLVRenderParams->sCosineY) >> 16);
    stru_F8AD28.vec_60.y = ((unsigned __int64)(stru_F8AD28.vec_14.x * (signed __int64)pBLVRenderParams->sCosineY) >> 16)
                         - ((unsigned __int64)(stru_F8AD28.vec_14.y * (signed __int64)pBLVRenderParams->sSineY) >> 16);
    stru_F8AD28.field_6C = stru_F8AD28.vec_14.z;
    stru_F8AD28.vec_60.z = ((unsigned __int64)(stru_F8AD28.vec_14.x * (signed __int64)pBLVRenderParams->sSineY) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.vec_14.y * (signed __int64)pBLVRenderParams->sCosineY) >> 16);
    stru_F8AD28.vec_70.x = ((unsigned __int64)(stru_F8AD28.vec_20.x * (signed __int64)pBLVRenderParams->sCosineY) >> 16)
                         - ((unsigned __int64)(stru_F8AD28.vec_20.y * (signed __int64)pBLVRenderParams->sSineY) >> 16);
    v8 = pBLVRenderParams->vPartyPos.y;
    stru_F8AD28.vec_70.y = ((unsigned __int64)(stru_F8AD28.vec_20.x * (signed __int64)pBLVRenderParams->sSineY) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.vec_20.y * (signed __int64)pBLVRenderParams->sCosineY) >> 16);
    stru_F8AD28.vec_70.z = stru_F8AD28.vec_20.z;
    v9 = pBLVRenderParams->vPartyPos.x;
    v5 = pBLVRenderParams->sSineY * pBLVRenderParams->vPartyPos.y
       - pBLVRenderParams->sCosineY * pBLVRenderParams->vPartyPos.x;
    v6 = pBLVRenderParams->vPartyPos.z;
    v3 = -(pBLVRenderParams->sCosineY * pBLVRenderParams->vPartyPos.y
         + pBLVRenderParams->sSineY * pBLVRenderParams->vPartyPos.x);
    v7 = -65536 * pBLVRenderParams->vPartyPos.z;
  }
  stru_F8AD28.field_7C = stru_F8AD28.rotated_normal.x;
  stru_F8AD28.vec_60.x = stru_F8AD28.plane_4.vNormal.y * v8
                       + stru_F8AD28.plane_4.dist
                       + stru_F8AD28.plane_4.vNormal.x * v9
                       + stru_F8AD28.plane_4.vNormal.z * v6;
  stru_F8AD28.vec_80.x = (unsigned __int64)(SLODWORD(pBLVRenderParams->field_44)
                                          * (signed __int64)stru_F8AD28.rotated_normal.y) >> 16;
  stru_F8AD28.vec_80.y = (unsigned __int64)(SLODWORD(pBLVRenderParams->field_44)
                                          * (signed __int64)stru_F8AD28.rotated_normal.z) >> 16;
  stru_F8AD28.vec_80.z = -stru_F8AD28.vec_60.x;
  stru_F8AD28.vec_8C.x = stru_F8AD28.vec_60.y;
  stru_F8AD28.vec_8C.y = (unsigned __int64)(SLODWORD(pBLVRenderParams->field_44) * (signed __int64)stru_F8AD28.vec_60.z) >> 16;
  stru_F8AD28.vec_8C.z = (unsigned __int64)(SLODWORD(pBLVRenderParams->field_44) * (signed __int64)stru_F8AD28.field_6C) >> 16;
  stru_F8AD28.vec_9C.x = stru_F8AD28.vec_70.x;
  stru_F8AD28.field_98 = -(((unsigned __int64)(stru_F8AD28.vec_60.y * (signed __int64)v5) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.vec_60.z * (signed __int64)v3) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.field_6C * (signed __int64)v7) >> 16));
  stru_F8AD28.vec_9C.y = (unsigned __int64)(SLODWORD(pBLVRenderParams->field_44) * (signed __int64)stru_F8AD28.vec_70.y) >> 16;
  stru_F8AD28.vec_9C.z = (unsigned __int64)(SLODWORD(pBLVRenderParams->field_44) * (signed __int64)stru_F8AD28.vec_70.z) >> 16;
  result = 0;
  stru_F8AD28.field_A8 = -(((unsigned __int64)(stru_F8AD28.vec_70.x * (signed __int64)v5) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.vec_70.y * (signed __int64)v3) >> 16)
                         + ((unsigned __int64)(stru_F8AD28.vec_70.z * (signed __int64)v7) >> 16));
  stru_F8AD28.field_38 = 0;
  stru_F8AD28.field_3E4 = 0;
  stru_F8AD28.uCurrentAmbientLightLevel = 0;
  stru_F8AD28.uNumLightsApplied = 0;
  stru_F8AD28.field_34 = 0;
  return result;
}

//----- (004B1447) --------------------------------------------------------
Player *__fastcall sub_4B1447_party_fine(int a1, int a2, int a3)
{
  signed int v3; // esi@1
  char v4; // sf@8
  int v5; // eax@8
  unsigned __int64 v6; // qax@12
  DDM_DLV_Header *v7; // eax@14
  Player **v8; // edi@18
  Player *result; // eax@19
  //char *v10; // esi@20

  v3 = 0;
  if ( a2 )
  {
    if ( a2 != 1 )
    {
      if ( a2 == 2 )
        v3 = 2;
      goto LABEL_13;
    }
    v3 = 2;
  }
  else
  {
    v3 = 1;
  }
  if ( pParty->uFine < 4000000 )
  {
    v4 = a3 + pParty->uFine < 0;
    v5 = a3 + pParty->uFine;
    pParty->uFine += a3;
    if ( v4 )
    {
      v5 = 0;
      pParty->uFine = 0;
    }
    if ( v5 > 4000000 )
      pParty->uFine = 4000000;
  }

  pParty->field_3C._shop_ban_times[a1] = pParty->uTimePlayed + 368640;
 
LABEL_13:
  pParty->InTheShopFlags[a1] = 1;
  if ( v3 )
  {
    v7 = &pOutdoor->ddm;
    if ( uCurrentlyLoadedLevelType != LEVEL_Outdoor )
      v7 = &pIndoor->dlv;
    v7->uReputation += v3;
    if ( v7->uReputation > 10000 )
      v7->uReputation = 10000;
  }
  v8 = &pPlayers[1];
  do
  {
    result = *v8;
    if ( pParty->uFine )
    {
      //v10 = result->_guilds_member_bits;
      result = (Player *)_449B57_test_bit((unsigned __int8 *)result->_guilds_member_bits, 1);
      if ( !(short)result )
		  __debugbreak();
        //result = (Player *)_449B7E_toggle_bit((unsigned char *)v10, 1, 1u);
    }
    ++v8;
  }
  while ( (signed int)v8 <= (signed int)&pPlayers[4] );
  return result;
}

//----- (004B1523) --------------------------------------------------------
void __thiscall sub_4B1523(int *_this)
{
  int v1; // esi@1
  int v2; // edx@1
  unsigned int v3; // eax@2
  int v4; // eax@4
  LONG v5; // ecx@4
  int v6; // eax@10
  char *v7; // ST44_4@12
  unsigned __int16 v8; // ax@12
  GUIWindow a1; // [sp+Ch] [bp-68h]@4
  unsigned int v11; // [sp+60h] [bp-14h]@1
  POINT a2; // [sp+64h] [bp-10h]@1
  int v13; // [sp+6Ch] [bp-8h]@4
  int v14; // [sp+70h] [bp-4h]@4

  v1 = *_this - 399;
  v2 = (*_this - 400) % 11 + 1;
  v11 = 4 * (*_this - 400) / 11;
 // sprintf(pTmpBuf.data(), "%s%03d", spellbook_texture_filename_suffices[v11 / 4], v2); not used
  if ( pMouse->GetCursorPos(&a2)->y <= 320 )
    v3 = pMouse->GetCursorPos(&a2)->y + 30;
  else
    v3 = 30;
  a1.Hint = 0;
  a1.uFrameY = v3;
  a1.uFrameWidth = 328;
  a1.uFrameHeight = 68;
  a1.uFrameX = 90;
  a1.uFrameZ = 417;
  a1.uFrameW = v3 + 67;
  a2.y = pFontSmallnum->GetLineWidth(pGlobalTXT_LocalizationStrings[431]);
  v14 = pFontSmallnum->GetLineWidth(pGlobalTXT_LocalizationStrings[433]);
  v13 = pFontSmallnum->GetLineWidth(pGlobalTXT_LocalizationStrings[432]);
  v4 = pFontSmallnum->GetLineWidth(pGlobalTXT_LocalizationStrings[96]);
  v5 = a2.y;
  if ( v14 > a2.y )
    v5 = v14;
  if ( v13 > v5 )
    v5 = v13;
  if ( v4 > v5 )
    v5 = v4;
  sprintf(pTmpBuf2.data(), "%s\n\n%s\t%03d:\t%03d%s\t000\n%s\t%03d:\t%03d%s\t000\n%s\t%03d:\t%03d%s\t000\n%s\t%03d:\t%03d%s",
    pSpellStats->pInfos[v1].pDescription, pGlobalTXT_LocalizationStrings[431],        // "Normal"
    v5 + 3, v5 + 10, pSpellStats->pInfos[v1].pBasicSkillDesc, pGlobalTXT_LocalizationStrings[433],        // "Expert"
    v5 + 3, v5 + 10, pSpellStats->pInfos[v1].pExpertSkillDesc, pGlobalTXT_LocalizationStrings[432],        // "Master"
    v5 + 3, v5 + 10, pSpellStats->pInfos[v1].pMasterSkillDesc, pGlobalTXT_LocalizationStrings[96],         // "Grand"
    v5 + 3, v5 + 10, pSpellStats->pInfos[v1].pGrandmasterSkillDesc);
  v6 = pFontSmallnum->CalcTextHeight(pTmpBuf2.data(), &a1, 0, 0);
  a1.uFrameHeight += v6;
  if ( (signed int)a1.uFrameHeight < 150 )
    a1.uFrameHeight = 150;
  a1.uFrameWidth = game_viewport_width;
  a1.DrawMessageBox(0);
  a1.uFrameWidth -= 12;
  a1.uFrameHeight -= 12;
  v7 = pSpellStats->pInfos[v1].pName;
  a1.uFrameZ = a1.uFrameX + a1.uFrameWidth - 1;
  a1.uFrameW = a1.uFrameHeight + a1.uFrameY - 1;
  v8 = TargetColor(0xFFu, 0xFFu, 0x9Bu);
  a1.DrawTitleText(pFontArrus, 0x78u, 0xCu, v8, v7, 3u);
  a1.DrawText(pFontSmallnum, 120, 44, 0, pTmpBuf2.data(), 0, 0, 0);
  a1.uFrameZ = a1.uFrameX + 107;
  a1.uFrameWidth = 108;
  a1.DrawTitleText(pFontComic, 0xCu, 0x4Bu, 0, pSkillNames[v11 / 4 + 12], 3u);
  sprintf(pTmpBuf.data(), "%s\n%d", pGlobalTXT_LocalizationStrings[522], *(&pSpellDatas[0].uNormalLevelMana + 10 * v1));
  a1.DrawTitleText(pFontComic, 0xCu, a1.uFrameHeight - LOBYTE(pFontComic->uFontHeight) - 16, 0, pTmpBuf.data(), 3);
}

//----- (004B1784) --------------------------------------------------------
bool __cdecl sub_4B1784_check_if_player_concious__draw_warning_else_mess_with_dlg_win()
{
  Player *pPlayer; // ebx@1
  bool result; // eax@2
  unsigned __int16 v2; // ST0C_2@3
  int v3; // eax@3
  GUIWindow v4; // [sp+4h] [bp-54h]@3

  pPlayer = pPlayers[uActiveCharacter];
  if ( pPlayer->CanAct() )
  {
    pDialogueWindow->pNumPresenceButton = dword_F8B1E0;
    result = 1;
  }
  else
  {
    pDialogueWindow->pNumPresenceButton = 0;
    memcpy(&v4, pPrimaryWindow, sizeof(v4));
    v4.uFrameX = 483;
    v4.uFrameWidth = 148;
    v4.uFrameZ = 334;
    sprintf(pTmpBuf.data(), pGlobalTXT_LocalizationStrings[427], pPlayer->pName, pGlobalTXT_LocalizationStrings[562]);// 
                                                // "%s is in no condition to %s"
                                                // "do anything"
    v2 = TargetColor(255, 255, 0x9Bu);
    v3 = pFontArrus->CalcTextHeight(pTmpBuf.data(), &v4, 0, 0);
    v4.DrawTitleText(pFontArrus, 0, (212 - v3) / 2 + 101, v2, pTmpBuf.data(), 3u);
    result = 0;
  }
  return result;
}
// F8B1E0: using guessed type int dword_F8B1E0;

// F8B198: using guessed type int dword_F8B198;
// F8B19C: using guessed type int dword_F8B19C;


// 507B94: using guessed type int dword_507B94;

//----- (004B1ECE) --------------------------------------------------------
void __cdecl sub_4B1ECE()
{
  __int16 *v0; // edi@1
  int v1; // ebx@3
  Player *v2; // esi@3
  int v3; // eax@4
  signed int v4; // eax@9
  int v5; // ebx@11
  Player *v6; // esi@13
  ItemGen *v7; // eax@14
  signed int v8; // edi@14
  ItemGen *v9; // [sp+Ch] [bp-Ch]@11
  signed int v10; // [sp+10h] [bp-8h]@13
  int v11; // [sp+14h] [bp-4h]@1
  Player *v12; // [sp+14h] [bp-4h]@11

  contract_approved = 0;
  v11 = 0;
  uDialogueType = 84;
  current_npc_text = (char *)pNPCTopics[667].pText;
  v0 = _4F0882_evt_VAR_PlayerItemInHands_vals.data();
  while ( 1 )
  {
    if ( (unsigned __int16)_449B57_test_bit(pParty->_quest_bits, *v0) )
    {
      v1 = 0;
      v2 = pParty->pPlayers;
      do
      {
        LOBYTE(v3) = v2->CompareVariable(VAR_PlayerItemInHands, *(v0+1));
        if ( v3 )
          break;
        ++v2;
        ++v1;
      }
      while ( (signed int)v2 < (signed int)pParty->pHirelings );
      if ( v1 == 4 )
        break;
    }
    ++v11;
    v0 += 2;
    if ( v0 > &_4F0882_evt_VAR_PlayerItemInHands_vals[53] )
	  break;
  }
  if ( v0 <= &_4F0882_evt_VAR_PlayerItemInHands_vals[53] )
  {
	  current_npc_text = (char *)pNPCTopics[666].pText; // Here's %s that you lost. Be careful
	  v4 = _4F0882_evt_VAR_PlayerItemInHands_vals[2 * v11];
	  contract_approved = _4F0882_evt_VAR_PlayerItemInHands_vals[2 * v11];
	  pParty->pPlayers[0].AddVariable(VAR_PlayerItemInHands, v4);
  }
  if ( contract_approved == 601 )
  {
    v5 = 0;
    v12 = pParty->pPlayers;//[0].uClass;
    v9 = 0;
    while ( 1 )
    {
      if ( v12->classType == PLAYER_CLASS_LICH )
      {
        v10 = 0;
        v6 = pParty->pPlayers;//[0].pInventoryItems[0].field_1A;
        do
        {
		  v7 = v6->pInventoryItems;
          v8 = 138;
          do
          {
			if ( v7->uItemID == 601 )
            {
			  if ( !v7->uHolderPlayer )
				  v9 = v7;
			  if ( v7->uHolderPlayer == v5 )
                v10 = 1;
            }
            ++v7;
            --v8;
          }
          while ( v8 );
          ++v6;
        }
		while ( v6 <= &pParty->pPlayers[3] );
        if ( !v10 )
          break;
      }
      ++v12;
      ++v5;
	  if ( v12 > &pParty->pPlayers[3] )
        return;
    }
    if ( v9 )
	  v9->uHolderPlayer = v5;
  }
}
// 4F08EC: using guessed type int dword_4F08EC[];
// 722B3C: using guessed type int dword_722B3C;
// 722B44: using guessed type int dword_722B44;
// F8B1A8: using guessed type int dword_F8B1A8;

//----- (004B254D) --------------------------------------------------------
char *__thiscall _4B254D_SkillMasteryTeacher(int _this)
{
  //Player *v1; // esi@1
  int v2; // edx@1
  int v3; // ecx@1
  int v4; // edi@1
  int pClassType; // eax@7
  int v6; // eax@7
  int v7; // ebx@7
  //int v8; // ebx@8
  signed int v9; // esi@8
  int v10; // eax@8
  char *v11; // ecx@8
  int v12; // edi@9
  char *v13; // edx@9
  signed int v14; // edi@10
  unsigned int v16; // eax@29
  //int v17; // eax@36
  char v18; // cl@46
  __int16 v19; // dx@56
  int v20; // eax@60
  //char *v21; // [sp-Ch] [bp-38h]@82
  //const char *v22; // [sp-8h] [bp-34h]@21
  //unsigned int v23; // [sp-8h] [bp-34h]@38
  //char *v24; // [sp-8h] [bp-34h]@82
  char *v25; // [sp-4h] [bp-30h]@14
  //int v26; // [sp-4h] [bp-30h]@38
  //int v27; // [sp-4h] [bp-30h]@82
  char v28[4]; // [sp+Ch] [bp-20h]@9
  int v29; // [sp+10h] [bp-1Ch]@13
  int v30; // [sp+14h] [bp-18h]@15
  int v31; // [sp+18h] [bp-14h]@16
  unsigned __int16 a1[2]; // [sp+1Ch] [bp-10h]@7
  //int v33; // [sp+20h] [bp-Ch]@7
  int v34; // [sp+24h] [bp-8h]@7
  char *v35; // [sp+28h] [bp-4h]@1

  contract_approved = 0;
  v2 = (_this - 200) % 3;
  v3 = (_this - 200) / 3;
  v4 = v2;
  v35 = (char *)pNPCTopics[127].pText;
  dword_F8B1AC_award_bit_number = v3;
  if ( v2 )
  {
    if ( v2 == 1 )
    {
      gold_transaction_amount = 5000;
      dword_F8B1B0 = 3;
    }
    else
    {
      if ( v2 == 2 )
      {
        gold_transaction_amount = 8000;
        dword_F8B1B0 = 4;
      }
    }
  }
  else
  {
    gold_transaction_amount = 2000;
    dword_F8B1B0 = 2;
  }
  pClassType = pPlayers[uActiveCharacter]->classType;
  //v33 = pClassType;
  v6 = byte_4ED970_skill_learn_ability_by_class_table[pClassType][v3];
  *(int *)a1 = pPlayers[uActiveCharacter]->pActiveSkills[v3];
  v7 = a1[0] & 0x3F;
  v34 = v2 + 2;
  if ( v6 < v2 + 2 )
  {
    //v8 = v33;
    v9 = 0;
    v10 = pClassType - pClassType % 4;
    v11 = &byte_4ED970_skill_learn_ability_by_class_table[pClassType - pClassType % 4][v3];
    do
    {
      v12 = (unsigned __int8)*v11;
      v13 = &v28[4 * v9];
      *(int *)v13 = 0;
      if ( v12 < v34 )
      {
        v14 = 1;
      }
      else
      {
        v14 = 1;
        *(int *)v13 = 1;
      }
      ++v9;
      v11 += 37;
    }
    while ( v9 < 4 );
    if ( v29 == v14 )
    {
      v25 = pClassNames[v10 + 1];
    }
    else
    {
      if ( v30 == v14 )//crash
      {
        if ( v31 == v14 )
        {
          sprintf(pTmpBuf.data(), pGlobalTXT_LocalizationStrings[634], pClassNames[v10 + 2], pClassNames[v10 + 3]);//Вы должны достичь звания %s или %s для обучения этому уровню навыка.
          return pTmpBuf.data();
        }
        v25 = pClassNames[v10 + 2];
      }
      else
      {
        if ( v31 != v14 )
        {
          sprintf(pTmpBuf.data(), pGlobalTXT_LocalizationStrings[632], pClassNames[pClassType]);//Этот уровень навыка не может быть постигнут классом %s.
          return pTmpBuf.data();
        }
        v25 = pClassNames[v10 + 3];
      }
    }
    sprintf(pTmpBuf.data(), pGlobalTXT_LocalizationStrings[633], v25);//Вы должны достичь звания %s для обучения этому уровню навыка.
    return pTmpBuf.data();
  }
  if ( !pPlayers[uActiveCharacter]->CanAct() )
    return (char *)pNPCTopics[122].pText;
  if ( !v7 )
    return (char *)pNPCTopics[131].pText;
  v16 = SkillToMastery(a1[0]);
  if ( (signed int)v16 > v4 + 1 )
    return (char *)pNPCTopics[v4 + 128].pText;
  if ( v34 != 2 )
  {
    if ( v34 == 3 )
    {
      if ( (signed int)v16 >= 2 && v7 >= 7 )
      {
        switch ( dword_F8B1AC_award_bit_number )
        {
          case 12:
          case 13:
          case 14:
          case 15:
          case 16:
          case 17:
          case 18:
            gold_transaction_amount = 4000;
            goto LABEL_42;
          case 19:
            v19 = 114;
            if ( !(unsigned __int16)_449B57_test_bit(pParty->_quest_bits, v19) )
              return v35;
            if ( !gold_transaction_amount )
              goto LABEL_79;
            goto LABEL_42;
          case 20:
            v19 = 110;
            if ( !(unsigned __int16)_449B57_test_bit(pParty->_quest_bits, v19) )
              return v35;
            if ( !gold_transaction_amount )
              goto LABEL_79;
            goto LABEL_42;
          case 22:
            v20 = pPlayers[uActiveCharacter]->GetBaseWillpower();
            if ( v20 < 50 )
              return v35;
            if ( !gold_transaction_amount )
              goto LABEL_79;
            goto LABEL_42;
          case 24:
            gold_transaction_amount = 2500;
            v20 = pPlayers[uActiveCharacter]->GetBaseEndurance();
            if ( v20 < 50 )
              return v35;
            if ( !gold_transaction_amount )
              goto LABEL_79;
            goto LABEL_42;
          case 36:
            v20 = pPlayers[uActiveCharacter]->GetBaseIntelligence();
            if ( v20 < 50 )
              return v35;
            if ( !gold_transaction_amount )
              goto LABEL_79;
            goto LABEL_42;
          case 21:
          case 23:
          case 25:
          case 26:
          case 29:
          case 32:
          case 34:
          case 35:
            gold_transaction_amount = 2500;
            goto LABEL_42;
          case 8:
          case 9:
          case 10:
          case 11:
            gold_transaction_amount = 3000;
            goto LABEL_42;
          case 7:
            gold_transaction_amount = 0;
            if ( !gold_transaction_amount )
              goto LABEL_79;
            goto LABEL_42;
          default:
            if ( !gold_transaction_amount )
              goto LABEL_79;
            goto LABEL_42;
        }
        gold_transaction_amount = 0;
        if ( !gold_transaction_amount )
          goto LABEL_79;
        goto LABEL_42;
      }
    }
    else
    {
      if ( v34 != 4 )
      {
        if ( !gold_transaction_amount )
          goto LABEL_79;
        goto LABEL_42;
      }
      if ( (signed int)v16 >= 3 && v7 >= 10 )
      {
        switch ( dword_F8B1AC_award_bit_number )
        {
          case 19:
            if ( pPlayers[uActiveCharacter]->_49327B(0x22u, 1) == 1 )
            {
              if ( !gold_transaction_amount )
                goto LABEL_79;
              goto LABEL_42;
            }
            if ( pPlayers[uActiveCharacter]->_49327B(26, 1) == 1 )
            {
              if ( !gold_transaction_amount )
                goto LABEL_79;
              goto LABEL_42;
            }
            return v35;
          case 20:
            if ( pPlayers[uActiveCharacter]->_49327B(0x23u, 1) == 1 )
            {
              if ( !gold_transaction_amount )
                goto LABEL_79;
              goto LABEL_42;
            }
            if ( pPlayers[uActiveCharacter]->_49327B(27, 1) == 1 )
            {
              if ( !gold_transaction_amount )
                goto LABEL_79;
              goto LABEL_42;
            }
            return v35;
          case 30:
            v18 = LOBYTE(pPlayers[uActiveCharacter]->pActiveSkills[31]);
            if ( (v18 & 0x3Fu) < 0xA )
              return v35;
            if ( !gold_transaction_amount )
              goto LABEL_79;
            goto LABEL_42;
          case 31:
            v18 = LOBYTE(pPlayers[uActiveCharacter]->pActiveSkills[30]);
            if ( (v18 & 0x3Fu) < 0xA )
              return v35;
            if ( !gold_transaction_amount )
              goto LABEL_79;
            goto LABEL_42;
          case 21:
          case 23:
          case 24:
          case 25:
          case 26:
          case 29:
          case 32:
          case 34:
          case 35:
            gold_transaction_amount = 6000;
            goto LABEL_42;
          case 8:
          case 9:
          case 10:
          case 11:
            gold_transaction_amount = 7000;
            goto LABEL_42;
          case 7:
            break;
          default:
            if ( !gold_transaction_amount )
              goto LABEL_79;
            goto LABEL_42;
        }
        gold_transaction_amount = 0;
        if ( !gold_transaction_amount )
          goto LABEL_79;
        goto LABEL_42;
      }
    }
    return v35;
  }
  if ( v7 < 4 )
    return v35;
  if ( dword_F8B1AC_award_bit_number > 27 )
  {
    if ( dword_F8B1AC_award_bit_number != 29
      && dword_F8B1AC_award_bit_number != 32
      && (dword_F8B1AC_award_bit_number <= 33 || dword_F8B1AC_award_bit_number > 35) )
    {
      if ( !gold_transaction_amount )
        goto LABEL_79;
      goto LABEL_42;
    }
    gold_transaction_amount = 500;
    if ( !gold_transaction_amount )
      goto LABEL_79;
    goto LABEL_42;
  }
  if ( dword_F8B1AC_award_bit_number >= 23 )
  {
    gold_transaction_amount = 500;
    if ( !gold_transaction_amount )
      goto LABEL_79;
    goto LABEL_42;
  }
  if ( dword_F8B1AC_award_bit_number == 7 )
  {
    gold_transaction_amount = 0;
    goto LABEL_79;
  }
  if ( dword_F8B1AC_award_bit_number <= 7 )
  {
    if ( !gold_transaction_amount )
      goto LABEL_79;
    goto LABEL_42;
  }
  if ( dword_F8B1AC_award_bit_number > 18 )
  {
    if ( dword_F8B1AC_award_bit_number != 21 )
    {
      if ( !gold_transaction_amount )
        goto LABEL_79;
      goto LABEL_42;
    }
    gold_transaction_amount = 500;
    if ( !gold_transaction_amount )
      goto LABEL_79;
    goto LABEL_42;
  }
  gold_transaction_amount = 1000;
LABEL_42:
  if ( gold_transaction_amount > pParty->uNumGold )
    return (char *)pNPCTopics[124].pText;
LABEL_79:
  contract_approved = 1;
  if ( v34 == 2 )
  {
    sprintfex(pTmpBuf2.data(), pGlobalTXT_LocalizationStrings[534],//Получить степень ^Pr[%s] в навыке ^Pr[%s] за ^I[%lu] золот^L[ой;ых;ых]
              pGlobalTXT_LocalizationStrings[433], pSkillNames[dword_F8B1AC_award_bit_number], gold_transaction_amount);//Эксперт
    return pTmpBuf2.data();
  }
  if ( v34 == 3 )
  {
    sprintfex(pTmpBuf2.data(), pGlobalTXT_LocalizationStrings[534],
              pGlobalTXT_LocalizationStrings[432], pSkillNames[dword_F8B1AC_award_bit_number], gold_transaction_amount);//Мастер
    return pTmpBuf2.data();
  }
  if ( v34 == 4 )
    sprintfex(pTmpBuf2.data(), pGlobalTXT_LocalizationStrings[534],
              pGlobalTXT_LocalizationStrings[225], pSkillNames[dword_F8B1AC_award_bit_number], gold_transaction_amount);//Великий Магистр
  return pTmpBuf2.data();
}

//----- (004B3A72) --------------------------------------------------------
void sub_4B3A72( int a1 )
	{
  int num_buttons; // esi@1

  num_buttons = 0;
  if ( a1 == 21 )
  {
    CreateButtonInColumn(0, 0x66u);
    num_buttons = 2;
    CreateButtonInColumn(1, 0x67u);
	if ( pParty->HasItem(651) ) //Arcomage Deck
    {
      num_buttons = 3;
      CreateButtonInColumn(2, 0x68u);
    }
  }
  pDialogueWindow->_41D08F_set_keyboard_control_group(num_buttons, 1, 0, 2);
  dword_F8B1E0 = pDialogueWindow->pNumPresenceButton;
}
// F8B1E0: using guessed type int dword_F8B1E0;

//----- (004B3AD4) --------------------------------------------------------
void sub_4B3AD4( signed int a1 )
	{
  if ( a1 > 0 )
  {
    if ( a1 <= 3 )
    {
      CreateButtonInColumn(0, 3u);
      CreateButtonInColumn(1, 4u);
      CreateButtonInColumn(2, 5u);
       pDialogueWindow->_41D08F_set_keyboard_control_group(3, 1, 0, 2);
    }
    if ( a1 == 4 )
    {
      CreateButtonInColumn(0, 3u);
      CreateButtonInColumn(1, 4u);
      pDialogueWindow->_41D08F_set_keyboard_control_group(2, 1, 0, 2);
    }
  }
  dword_F8B1E0 = pDialogueWindow->pNumPresenceButton;
}
// F8B1E0: using guessed type int dword_F8B1E0;

//----- (004B3B42) --------------------------------------------------------
void sub_4B3B42( signed int a1 )
	{
  int v1; // ecx@18
  int v2; // ecx@19
  int v3; // ecx@20
  signed int v4; // esi@22
  signed int v5; // eax@22
  unsigned int v6; // edx@24
  int v7; // ecx@24
  int result; // eax@43
  int v9; // [sp-10h] [bp-14h]@28
  int v10; // [sp-Ch] [bp-10h]@28
  int v11; // [sp-8h] [bp-Ch]@28
  unsigned int v12; // [sp-4h] [bp-8h]@4
  unsigned int v13; // [sp-4h] [bp-8h]@5
  unsigned int v14; // [sp-4h] [bp-8h]@9
  unsigned int v15; // [sp-4h] [bp-8h]@10
  unsigned int v16; // [sp-4h] [bp-8h]@14
  int v17; // [sp-4h] [bp-8h]@28

  if ( a1 > 13 )
  {
    if ( a1 > 22 )
    {
      if ( a1 == 23 )
      {
        CreateButtonInColumn(0, 0xAu);
        CreateButtonInColumn(1, 0xBu);
        v14 = 96;
LABEL_41:
        CreateButtonInColumn(2, v14);
        v17 = 2;
        v11 = 0;
        v10 = 1;
        v9 = 3;
        goto LABEL_42;
      }
      if ( a1 <= 26 )
        goto LABEL_43;
      if ( a1 > 28 )
      {
        if ( a1 != 30 )
          goto LABEL_43;
        CreateButtonInColumn(0, 0x11u);
        v16 = 96;
        goto LABEL_37;
      }
      CreateButtonInColumn(0, 0x69u);
      CreateButtonInColumn(1, 0x6Au);
      CreateButtonInColumn(2, 0x6Bu);
      v12 = 108;
    }
    else
    {
      if ( a1 == 22 )
      {
        CreateButtonInColumn(0, 7u);
        v16 = 8;
        goto LABEL_37;
      }
      v1 = a1 - 14;
      if ( !v1 )
      {
        CreateButtonInColumn(0, 0x12u);
        CreateButtonInColumn(1, 0x30u);
        CreateButtonInColumn(2, 0x31u);
        CreateButtonInColumn(3, 0x32u);
        CreateButtonInColumn(4, 0x33u);
        v17 = 2;
        v11 = 0;
        v10 = 1;
        v9 = 5;
        goto LABEL_42;
      }
      v2 = v1 - 1;
      if ( v2 )
      {
        v3 = v2 - 2;
        if ( v3 )
        {
          if ( v3 != 4 )
            goto LABEL_43;
          CreateButtonInColumn(0, 0xFu);
          CreateButtonInColumn(1, 0x10u);
          v4 = 3;
          CreateButtonInColumn(2, 0x60u);
          v5 = (signed int)window_SpeakInHouse->ptr_1C;
          if ( v5 < 108 || v5 > 120 )
            goto LABEL_28;
          v4 = 4;
          v6 = 101;
          v7 = 3;
        }
        else
        {
          v4 = 1;
          CreateButtonInColumn(0, 0x63u);
          if ( !pParty->uFine )
          {
LABEL_28:
            v17 = 2;
            v11 = 0;
            v10 = 1;
            v9 = v4;
LABEL_42:
            pDialogueWindow->_41D08F_set_keyboard_control_group(v9, v10, v11, v17);
            goto LABEL_43;
          }
          v4 = 2;
          v7 = 1;
          v6 = 100;
        }
        CreateButtonInColumn(v7, v6);
        goto LABEL_28;
      }
      CreateButtonInColumn(0, 0x12u);
      CreateButtonInColumn(1, 0x34u);
      CreateButtonInColumn(2, 0x35u);
      v12 = 54;
    }
LABEL_39:
    CreateButtonInColumn(3, v12);
    v17 = 2;
    v11 = 0;
    v10 = 1;
    v9 = 4;
    goto LABEL_42;
  }
  if ( a1 == 13 )
  {
    CreateButtonInColumn(0, 0x12u);
    v16 = 56;
LABEL_37:
    CreateButtonInColumn(1, v16);
    v17 = 2;
    v11 = 0;
    v10 = 1;
    v9 = 2;
    goto LABEL_42;
  }
  switch ( a1 )
  {
    case 1:
    case 2:
    case 3:
    case 4:
      CreateButtonInColumn(0, 2u);
      CreateButtonInColumn(1, 0x5Fu);
      CreateButtonInColumn(2, 0x5Eu);
      v12 = 96;
      goto LABEL_39;
    case 5:
      CreateButtonInColumn(0, 0x12u);
      v13 = 48;
      goto LABEL_9;
    case 6:
      CreateButtonInColumn(0, 0x12u);
      v13 = 49;
      goto LABEL_9;
    case 7:
      CreateButtonInColumn(0, 0x12u);
      v13 = 50;
      goto LABEL_9;
    case 8:
      CreateButtonInColumn(0, 0x12u);
      v13 = 51;
LABEL_9:
      CreateButtonInColumn(1, v13);
      v14 = 72;
      goto LABEL_41;
    case 9:
      CreateButtonInColumn(0, 0x12u);
      v15 = 52;
      goto LABEL_13;
    case 10:
      CreateButtonInColumn(0, 0x12u);
      v15 = 53;
      goto LABEL_13;
    case 11:
      CreateButtonInColumn(0, 0x12u);
      v15 = 54;
LABEL_13:
      CreateButtonInColumn(1, v15);
      v14 = 61;
      goto LABEL_41;
    case 12:
      CreateButtonInColumn(0, 0x12u);
      v16 = 55;
      goto LABEL_37;
    default:
      break;
  }
LABEL_43:
  dword_F8B1E0 = pDialogueWindow->pNumPresenceButton;
}
// F8B1E0: using guessed type int dword_F8B1E0;

//----- (004B3E1E) --------------------------------------------------------
void __cdecl sub_4B3E1E()
{
  NPCData *v0; // ST40_4@1
  signed int v1; // edi@1
  //GUIWindow *v2; // ecx@1

  __debugbreak();
  v0 = GetNPCData(sDialogue_SpeakingActorNPC_ID);
  v1 = 0;
  pDialogueWindow->eWindowType = WINDOW_MainMenu;
  pDialogueWindow->Release();
  pDialogueWindow = GUIWindow::Create(0, 0, 640, 480, WINDOW_Dialogue, 1, 0);
  if ( pNPCStats->pProfessions[v0->uProfession].pBenefits)//*(&pNPCStats->field_13A5C + 5 * v0->uProfession) )
  {
    pDialogueWindow->CreateButton(480, 160, 140, 28, 1, 0, UIMSG_SelectNPCDialogueOption, 77, 0, pGlobalTXT_LocalizationStrings[407], 0);
    v1 = 1;
  }
  pDialogueWindow->CreateButton(480, 30 * v1 + 160, 140, 30, 1, 0, UIMSG_SelectNPCDialogueOption, 76, 0, pGlobalTXT_LocalizationStrings[406], 0);//Нанять
  pDialogueWindow->_41D08F_set_keyboard_control_group(v1 + 1, 1, 0, 1);
}

// F8B19C: using guessed type int dword_F8B19C;

//----- (004B3FE5) --------------------------------------------------------
void __fastcall sub_4B3FE5(int a4)
{
  int v1; // edi@1
  char *v2; // edi@1

  v1 = a4;
  uDialogueType = 78;
  current_npc_text = (char *)pNPCTopics[a4 + 168].pText;
  _4B254D_SkillMasteryTeacher(a4);
  pDialogueWindow->Release();
  pDialogueWindow = GUIWindow::Create(0, 0, 640, 0x15Eu, WINDOW_MainMenu, v1, 0);
  v2 = "";
  pBtn_ExitCancel = pDialogueWindow->CreateButton( 0x1D7u, 0x1BDu, 0xA9u,  0x23u,   1,  0, UIMSG_Escape, 0,   0,
                 pGlobalTXT_LocalizationStrings[34], pIcons_LOD->GetTexture(uExitCancelTextureId), 0);
  pDialogueWindow->CreateButton(0, 0, 0, 0, 1, 0, UIMSG_BuyInShop_Identify_Repair, 0, 0, "", 0);
  if ( contract_approved )
    v2 = pGlobalTXT_LocalizationStrings[535];
  pDialogueWindow->CreateButton(0x1E0u, 0xA0u, 0x8Cu, 0x1Eu, 1, 0, UIMSG_ClickNPCTopic, 0x4Fu, 0, v2, 0);
  pDialogueWindow->_41D08F_set_keyboard_control_group(1, 1, 0, 2);
  dialog_menu_id = HOUSE_DIALOGUE_OTHER;
}
// F8B19C: using guessed type int dword_F8B19C;
// F8B1A8: using guessed type int dword_F8B1A8;

//----- (004B46A5) --------------------------------------------------------
void __fastcall DrawTextAtStatusBar( const char *Str, int a5 )
    {
  int v4; // eax@1
  pRenderer->DrawTextureRGB(0, 352, pTexture_StatusBar);
  v4 = pFontLucida->AlignText_Center(450, Str);
  pPrimaryWindow->DrawText(pFontLucida, v4 + 11, 357, a5, Str, 0, 0, 0);
}

//----- (004B46F8) --------------------------------------------------------
int __fastcall sub_4B46F8(int a1)
{
  int v1; // eax@1
  int i; // edx@1

  v1 = 0;
  for ( i = 0; i < a1; ++i )
    v1 += i + 1;
  return 1000 * v1;
}

//----- (004BC49B) --------------------------------------------------------
void OnSelectNPCDialogueOption(DIALOGUE_TYPE newDialogueType)
{
	//unsigned int v1; // esi@1
    NPCData *speakingNPC; // ebp@1
    //unsigned int v3; // eax@1
    int npc_event_id; // ecx@10
    signed int v5; // edi@14
    char *v6; // esi@15
    const char *v7; // ecx@22
    signed int v8; // edi@37
    //unsigned int v9; // eax@56
    unsigned int v10; // ecx@57
    void *v11; // [sp-Ch] [bp-1Ch]@46
    int v12; // [sp-8h] [bp-18h]@46
    char *v13; // [sp-8h] [bp-18h]@60
    size_t v14; // [sp-4h] [bp-14h]@46
    const char *v15; // [sp-4h] [bp-14h]@60

    //v1 = _this;
	speakingNPC = GetNPCData(sDialogue_SpeakingActorNPC_ID);
    //v3 = v1;
	uDialogueType = newDialogueType;
	if (!speakingNPC->uFlags)
	{
		speakingNPC->uFlags = 1;
		//v3 = uDialogueType;
	}

	if(newDialogueType == DIALOGUE_PROFESSION_DETAILS)
	{
		dialogue_show_profession_details = ~dialogue_show_profession_details;
	}
	else if(newDialogueType == DIALOGUE_76)
	{
		if (speakingNPC->Hired())
		{
			v8 = 0;
			if ( (signed int)pNPCStats->uNumNewNPCs > 0 )
			{
				v6 = (char *)pNPCStats->pNewNPCData;
				while ( !(v6[8] & 0x80) || strcmp(speakingNPC->pName, *(const char **)v6) )
				{
					++v8;
					v6 += 76;
					if ( v8 >= (signed int)pNPCStats->uNumNewNPCs )
						break;
				}
				if( v8 < (signed int)pNPCStats->uNumNewNPCs )
					v6[8] &= 0x7Fu;
			}
			if ( pParty->pHirelings[0].pName && !_stricmp(pParty->pHirelings[0].pName, speakingNPC->pName) )
			{
				v11 = pParty->pHirelings;
				memset(v11, 0, sizeof(NPCData));
			}
			else if ( pParty->pHirelings[1].pName && !_stricmp(pParty->pHirelings[1].pName, speakingNPC->pName) )
			{
				v11 = &pParty->pHirelings[1];
				memset(v11, 0, sizeof(NPCData));
			}
			pParty->field_709 = 0;
			pParty->CountHirelings();
			dword_591084 = 0;
			pMessageQueue_50CBD0->AddMessage(UIMSG_Escape, 1, 0);
			dword_7241C8 = 0;
			return;
		}
		if ( pParty->pHirelings[0].pName && pParty->pHirelings[1].pName )
		{
			v7 = pGlobalTXT_LocalizationStrings[533]; // ""I cannot join you, you're party is full""
			ShowStatusBarString(v7, 2u);
		}
		else
		{
			//v9 = v2->uProfession;
			if ( speakingNPC->uProfession != 51 )
			{
				v10 = pNPCStats->pProfessions[speakingNPC->uProfession - 1].uHirePrice;
																	if ( pParty->uNumGold < v10 )
			{
				ShowStatusBarString(pGlobalTXT_LocalizationStrings[155], 2u);// "You don't have enough gold"
				dialogue_show_profession_details = false;
				uDialogueType = 13;
				if ( uActiveCharacter )
					pPlayers[uActiveCharacter]->PlaySound(SPEECH_NotEnoughGold, 0);
				v7 = pGlobalTXT_LocalizationStrings[155];
				ShowStatusBarString(pGlobalTXT_LocalizationStrings[155], 2u);
				if ( !dword_7241C8 )
					pGame->Draw();
				dword_7241C8 = 0;
				return;
			}
				Party::TakeGold(v10);
			}
			LOBYTE(speakingNPC->uFlags) |= 0x80u;
			if ( pParty->pHirelings[0].pName )
			{
				memcpy(&pParty->pHirelings[1], speakingNPC, sizeof(pParty->pHirelings[1]));
				v15 = speakingNPC->pName;
				v13 = pParty->pHireling2Name;
			}
			else
			{
				memcpy(pParty->pHirelings, speakingNPC, 0x4Cu);
				v15 = speakingNPC->pName;
				v13 = pParty->pHireling1Name;
			}
			strcpy(v13, v15);
			pParty->field_709 = 0;
			pParty->CountHirelings();

			pMessageQueue_50CBD0->AddMessage(UIMSG_Escape, 1, 0);

			if ( sDialogue_SpeakingActorNPC_ID >= 0 )
				pDialogue_SpeakingActor->uAIState = Removed;
			if ( uActiveCharacter )
				pPlayers[uActiveCharacter]->PlaySound(SPEECH_61, 0);
		}
	}
	else if ( (signed int)newDialogueType > 84 && (signed int)newDialogueType <= 88 )
	{
		ArenaFight();
		return;
	}
	else if(newDialogueType == DIALOGUE_9)
	{
		if ( !sub_4BB756(speakingNPC->uProfession) )
		{
			if ( speakingNPC->uProfession != 41 )
				speakingNPC->bHasUsedTheAbility = 1;

			pMessageQueue_50CBD0->AddMessage(UIMSG_Escape, 1, 0);
		}
		else
		{
			ShowStatusBarString(pGlobalTXT_LocalizationStrings[140], 2u); //"Your packs are already full!"
		}
	}
	else if(newDialogueType == DIALOGUE_13)
	{
		if (!speakingNPC->Hired())
		{
			sub_4B3E1E();
			dialogue_show_profession_details = false;
		}
		else
		{
			v5 = 0;
			if ( (signed int)pNPCStats->uNumNewNPCs > 0 )
			{
				v6 = (char *)pNPCStats->pNewNPCData;
				while ( !(v6[8] & 0x80) || strcmp(speakingNPC->pName, *(const char **)v6) )
				{
					++v5;
					v6 += 76;
					if ( v5 >= (signed int)pNPCStats->uNumNewNPCs )
						break;
				}
				if ( v5 < (signed int)pNPCStats->uNumNewNPCs )
					v6[8] &= 0x7Fu;
			}
			if ( pParty->pHirelings[0].pName && !_stricmp(pParty->pHirelings[0].pName, speakingNPC->pName) )
			{
				v11 = pParty->pHirelings;
				memset(v11, 0, sizeof(NPCData));
			}
			else if ( pParty->pHirelings[1].pName && !_stricmp(pParty->pHirelings[1].pName, speakingNPC->pName) )
			{
				v11 = &pParty->pHirelings[1];
				memset(v11, 0, sizeof(NPCData));
			}
			pParty->field_709 = 0;
			pParty->CountHirelings();
			dword_591084 = 0;
			pMessageQueue_50CBD0->AddMessage(UIMSG_Escape, 1, 0);
			dword_7241C8 = 0;
			return;
		}
	}
	else if(newDialogueType >= 19 && newDialogueType <= 24)
	{
		switch(newDialogueType)
		{
			case DIALOGUE_19:  npc_event_id = speakingNPC->evt_A; break;
			case DIALOGUE_20:  npc_event_id = speakingNPC->evt_B; break;
			case DIALOGUE_21:  npc_event_id = speakingNPC->evt_C; break;
			case DIALOGUE_22:	 npc_event_id = speakingNPC->evt_D; break;
			case DIALOGUE_23:  npc_event_id = speakingNPC->evt_E; break;
			case DIALOGUE_24:	 npc_event_id = speakingNPC->evt_F; break;
		}
		if ( (npc_event_id >= 200) && (npc_event_id <= 310) )
			sub_4B3FE5(npc_event_id); //200-310
		else if (( npc_event_id >= 400) && (npc_event_id <= 410) )
		{ //400-410
			dword_F8B1D8 = newDialogueType;
			DrawJoinGuildWindow(npc_event_id - 400);
		}
		else
		{
			switch ( npc_event_id )
			{
				case 139:
					sub_4B1ECE();
					break;
				case 311:
					sub_4BBA85_bounties();
					break;
				case 399:
					sub_4BBCDD();
					break;
				default:
					activeLevelDecoration = (LevelDecoration*)1;
					current_npc_text = 0;
					EventProcessor(npc_event_id, 0, 1);
					activeLevelDecoration = NULL;
					break;
			}
		}
	}
	if ( !dword_7241C8 )
		pGame->Draw();
	dword_7241C8 = 0;
}

//----- (004BDAAF) --------------------------------------------------------
bool __fastcall MerchandiseTest(ItemGen *item, int _2da_idx)
{
  int v6; // edx@8
  int v7; // edx@9
  int v8; // edx@10
  unsigned __int8 v9; // zf@16
  char v10; // sf@16
  unsigned __int8 v11; // of@16
  bool test;

  if ( (p2DEvents[_2da_idx - 1].uType != 4 || (signed int)item->uItemID < 740 || (signed int)item->uItemID > 771)
    && ((signed int)item->uItemID >= 600 || (signed int)item->uItemID >= 529 && (signed int)item->uItemID <= 599) || item->Stolen())
    return 0;
  switch( p2DEvents[_2da_idx - 1].uType )
  {
    case BildingType_WeaponShop:
    {
      test = pItemsTable->pItems[item->uItemID].uEquipType <= 2;
      break;
    }
    case BildingType_ArmorShop:
    {
      test = pItemsTable->pItems[item->uItemID].uEquipType >= 3;
      break;
    }
    case BildingType_MagicShop:
    {
      test = pItemsTable->pItems[item->uItemID].uSkillType == 38 || pItemsTable->pItems[item->uItemID].uEquipType == 16;
      break;
    }
    case BildingType_AlchemistShop:
    {
      test = pItemsTable->pItems[item->uItemID].uEquipType == 13 || pItemsTable->pItems[item->uItemID].uEquipType == 14 
            || (pItemsTable->pItems[item->uItemID].uEquipType > 14 && !(pItemsTable->pItems[item->uItemID].uEquipType != 17 
            || (signed int)item->uItemID < 740) && item->uItemID != 771);
      break;
    }
    default:
    {
      test = false;
      break;
    }
  }
  return test;
/*
  if ( p2DEvents[a2 - 1].uType == 1 )
  {
    v11 = __OFSUB__(v5, 2);
    v9 = v5 == 2;
    v10 = v5 - 2 < 0;
    goto LABEL_23;
  }
  if ( p2DEvents[a2 - 1].uType > 2 )
  {
    if ( p2DEvents[a2 - 1].uType == 3 )
    {
      if ( pItemsTable->pItems[v4].uSkillType != 38 )
        return v5 == 16;
      return 1;
    }
    if ( p2DEvents[a2 - 1].uType != 4 || v5 < 13 )
      return 0;
	if ( p2DEvents[a2 - 1].uType == 4)
	{
		if ( v5 < 13 )
		  return 0;
		if ( v5 <= 14 )
		  return 1;
		if ( v5 != 17 || (signed int)v3 < 740 )
		  return 0;
		v11 = __OFSUB__(v3, 771);
		v9 = v3 == 771;
		v10 = ((v3 - 771) & 0x80000000u) != 0;
LABEL_23:
		if ( !((unsigned __int8)(v10 ^ v11) | v9) )
		  return 0;
		return 1;
	}
  }
  if ( p2DEvents[a2 - 1].uType == 2 )
  {
	  if ( v5 >= 3 )
	  {
		v11 = __OFSUB__(v5, 9);
		v9 = v5 == 9;
		v10 = v5 - 9 < 0;
		goto LABEL_23;
	  }
  }
  return 0;
*/
}