view mm7_5.cpp @ 1318:0ec2f944fccc

Слияние
author Ritor1
date Wed, 26 Jun 2013 14:22:02 +0600
parents 8c2f689b5f0b
children e70fe5d77928
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 "MapInfo.h"
#include "BSPModel.h"
#include "OutdoorCamera.h"
#include "LightmapBuilder.h"
#include "DecalBuilder.h"
#include "Mouse.h"
#include "Keyboard.h"
#include "GammaControl.h"
#include "stru11.h"
#include "mm7_data.h"
#include "FactionTable.h"
#include "Vis.h"
#include "mm7.h"
#include "Game.h"
#include "GUIWindow.h"
#include "GUIFont.h"
#include "GUIProgressBar.h"
#include "Party.h"
#include "AudioPlayer.h"
#include "Outdoor.h"
#include "Arcomage.h"
#include "LOD.h"
#include "Actor.h"
#include "Events.h"
#include "Viewport.h"
#include "FrameTableInc.h"
#include "Math.h"
#include "SpriteObject.h"
#include "ObjectList.h"
#include "Chest.h"
#include "PaletteManager.h"
#include "SaveLoad.h"
#include "Time.h"
#include "TurnEngine.h"
#include "CastSpellInfo.h"
#include "stru298.h"
#include "Events2D.h"
#include "texts.h"
#include "Log.h"
#include "UI\UIHouses.h"
#include "Lights.h"
#include "Lights.h"

//----- (004304E7) --------------------------------------------------------
void __cdecl GameUI_MsgProc()
{
  //signed int v0; // edi@6
  //char *v1; // esi@6
  unsigned int v2; // edx@7
  Actor *pActor; // ecx@13
  int v4; // ecx@18
  //NPCData *pNPCData0; // eax@18
  //int v6; // edx@20
  //int v7; // ecx@29
  //unsigned int v8; // edx@59
  //unsigned int v9; // ecx@60
  unsigned int v10; // ecx@73
  //unsigned int v11; // eax@75
  unsigned __int8 v12; // sf@75
  unsigned __int8 v13; // of@75
  int v14; // eax@98
  unsigned int v15; // eax@102
  unsigned __int8 v16; // al@104
  unsigned __int8 v17; // al@105
  int v18; // eax@106
  float v19; // ST64_4@121
  unsigned int v20; // ecx@121
  float v21; // ST64_4@126
  float v22; // ST64_4@127
  unsigned int v23; // ecx@135
  unsigned int v24; // ecx@149
  unsigned int v25; // ecx@165
  GUIWindow *pWindow; // eax@204
  unsigned int v27; // edx@204
  unsigned int v28; // ecx@204
  GUIWindow *pWindow2; // ecx@248
  //int v30; // edx@258
  //const char *v31; // ecx@262
  signed int v32; // eax@269
  unsigned int v33; // eax@277
  unsigned __int8 v34; // al@279
  unsigned __int8 v35; // al@280
  //GUIWindow *pWindow3; // ecx@332
  int v37; // eax@341
  int v38; // eax@358
  SHORT v39; // ax@365
  //signed int v40; // eax@365
  char *v41; // eax@380
  int v42; // eax@396
  POINT *pPoint; // eax@397
  signed int v44; // eax@398
  int v45; // edx@398
  signed int v46; // ecx@398
  char v47; // zf@399
  char v48; // zf@405
  BLVFace *pBLVFace; // ecx@410
  ODMFace *pODMFace; // ecx@412
  CastSpellInfo *pSpellInfo; // ecx@415
  void *v52; // eax@424
  __int16 v53; // ax@431
  int v54; // eax@432
  int v55; // ecx@432
  int v56; // edx@432
  int v57; // eax@432
  Player *pPlayer; // edx@442
  unsigned int v59; // eax@445
  signed int v60; // ST64_4@459
  NPCData *pNPCData2; // eax@467
  unsigned __int64 v62; // kr00_8@467
  __int16 v63; // dx@479
  unsigned int v64; // eax@486
  int v65; // ecx@486
  int v66; // eax@488
  char *v67; // eax@489
  __int16 v68; // dx@498
  char *v69; // eax@512
  int v70; // eax@525
  int v71; // edi@527
  NPCData *pNPCData3; // esi@527
  char *v73; // ecx@533
  signed int v74; // edi@535
  int v75; // eax@535
  int v76; // esi@535
  int v77; // eax@537
  Player *pPlayer2; // ecx@549
  //int v79; // ecx@550
  //unsigned int v80; // edx@550
  signed int v81; // eax@552
  POINT *pPoint2; // eax@553
  signed int v83; // ecx@554
  signed int v84; // ecx@554
  GUIButton *pButton; // eax@578
  unsigned int v86; // eax@583
  const char *v87; // ecx@595
  const char *v88; // ecx@596
  //unsigned int v89; // eax@598
  unsigned int v90; // eax@602
  int v91; // edx@605
  int v92; // eax@605
  int v93; // edx@605
  int pPlayerNum; // edx@611
  int v95; // eax@611
  //const char *v96; // ecx@621
  unsigned int v97; // eax@624
  int v98; // eax@636
  unsigned __int8 v99; // al@643
  Player *pPlayer3; // eax@648
  int v101; // ecx@648
  int v102; // edx@652
  int v103; // eax@671
  Player *pPlayer4; // ecx@718
  int v105; // eax@718
  Player *pPlayer5; // ST78_4@758
  unsigned int v107; // eax@758
  unsigned int v108; // eax@758
  unsigned int v115; // eax@764
  int v116; // eax@776
  POINT *pPoint3; // eax@777
  unsigned int v118; // eax@785
  unsigned int v119; // ecx@786
  unsigned int v120; // [sp-28h] [bp-624h]@86
  unsigned int v121; // [sp-28h] [bp-624h]@711
  unsigned int v122; // [sp-24h] [bp-620h]@86
  unsigned int v123; // [sp-24h] [bp-620h]@711
  unsigned int v124; // [sp-20h] [bp-61Ch]@86
  unsigned int v125; // [sp-20h] [bp-61Ch]@711
  unsigned int v126; // [sp-1Ch] [bp-618h]@86
  int v127; // [sp-1Ch] [bp-618h]@107
  unsigned int v128; // [sp-1Ch] [bp-618h]@711
  int v129; // [sp-18h] [bp-614h]@86
  //signed int v130; // [sp-18h] [bp-614h]@107
  int v131; // [sp-14h] [bp-610h]@86
  //unsigned int v132; // [sp-14h] [bp-610h]@107
  //unsigned int v133; // [sp-10h] [bp-60Ch]@60
  unsigned int v134; // [sp-10h] [bp-60Ch]@86
  //signed int v135; // [sp-10h] [bp-60Ch]@107
  unsigned int v136; // [sp-10h] [bp-60Ch]@121
  //unsigned int v137; // [sp-Ch] [bp-608h]@60
  unsigned int v138; // [sp-Ch] [bp-608h]@86
  //signed int v139; // [sp-Ch] [bp-608h]@107
  unsigned int v140; // [sp-Ch] [bp-608h]@121
  enum WindowType pWindowType; // [sp-8h] [bp-604h]@56
  //enum WindowType pWindowType1; // [sp-8h] [bp-604h]@60
  unsigned __int8 v143; // [sp-8h] [bp-604h]@86
  //int v144; // [sp-8h] [bp-604h]@107
  enum WindowType pWindowType2; // [sp-8h] [bp-604h]@121
  //const char *v146; // [sp-8h] [bp-604h]@449
  //unsigned int v147; // [sp-8h] [bp-604h]@550
  //int v148; // [sp-4h] [bp-600h]@56
  GUIButton *pButton2; // [sp-4h] [bp-600h]@59
  const char *v150; // [sp-4h] [bp-600h]@86
  //unsigned int v151; // [sp-4h] [bp-600h]@107
  int v152; // [sp-4h] [bp-600h]@121
  int v153; // [sp-4h] [bp-600h]@135
  //int v154; // [sp-4h] [bp-600h]@149
  int v155; // [sp-4h] [bp-600h]@165
  int v156; // [sp-4h] [bp-600h]@204
  //const char *v157; // [sp-4h] [bp-600h]@444
  unsigned int v158; // [sp-4h] [bp-600h]@449
  //__int16 v159; // [sp-4h] [bp-600h]@550
  int v160; // [sp-4h] [bp-600h]@599
  const char *v161; // [sp-4h] [bp-600h]@637
  //int v162; // [sp+0h] [bp-5FCh]@56
  //int v163; // [sp+0h] [bp-5FCh]@59
  Texture *pTexture; // [sp+0h] [bp-5FCh]@86
  //int v165; // [sp+0h] [bp-5FCh]@107
  int v166; // [sp+0h] [bp-5FCh]@121
  int v167; // [sp+0h] [bp-5FCh]@135
  //int v168; // [sp+0h] [bp-5FCh]@149
  int v169; // [sp+0h] [bp-5FCh]@165
  int v170; // [sp+0h] [bp-5FCh]@204
  //signed int v171; // [sp+0h] [bp-5FCh]@259
  KeyToggleType pKeyToggleType; // [sp+0h] [bp-5FCh]@287
  char *v173; // [sp+0h] [bp-5FCh]@444
  char *v174; // [sp+0h] [bp-5FCh]@449
  //int v175; // [sp+0h] [bp-5FCh]@550
  int v176; // [sp+0h] [bp-5FCh]@599
  const char *v177; // [sp+0h] [bp-5FCh]@629
  char *v178; // [sp+0h] [bp-5FCh]@637
  int v179; // [sp+4h] [bp-5F8h]@0
  //signed int _this; // [sp+14h] [bp-5E8h]@22
  signed int thisa; // [sp+14h] [bp-5E8h]@251
  signed int thisb; // [sp+14h] [bp-5E8h]@272
  Player *pPlayer7; // [sp+14h] [bp-5E8h]@373
  Player *pPlayer8; // [sp+14h] [bp-5E8h]@377
  char *thise; // [sp+14h] [bp-5E8h]@445
  Player *pPlayer9; // [sp+14h] [bp-5E8h]@455
  int thisg; // [sp+14h] [bp-5E8h]@467
  int thish; // [sp+14h] [bp-5E8h]@528
  signed int thisi; // [sp+14h] [bp-5E8h]@535
  MapInfo *pMapInfo; // [sp+14h] [bp-5E8h]@604
  Player *pPlayer10; // [sp+14h] [bp-5E8h]@641
  int thisl; // [sp+14h] [bp-5E8h]@648
  int uMessageParam; // [sp+18h] [bp-5E4h]@7
  int uAction; // [sp+1Ch] [bp-5E0h]@18
  NPCData *pNPCData4; // [sp+20h] [bp-5DCh]@23
  unsigned int uNumSeconds; // [sp+24h] [bp-5D8h]@18
  char v197; // [sp+2Bh] [bp-5D1h]@101
  enum UIMessageType uMessage; // [sp+2Ch] [bp-5D0h]@7
  unsigned int v199; // [sp+30h] [bp-5CCh]@7
  char *v200; // [sp+34h] [bp-5C8h]@518
  POINT v201; // [sp+38h] [bp-5C4h]@553
  POINT v202; // [sp+40h] [bp-5BCh]@141
  POINT a2; // [sp+48h] [bp-5B4h]@127
  POINT v204; // [sp+50h] [bp-5ACh]@777
  POINT v205; // [sp+58h] [bp-5A4h]@171
  POINT v206; // [sp+60h] [bp-59Ch]@553
  POINT v207; // [sp+68h] [bp-594h]@155
  POINT v208; // [sp+70h] [bp-58Ch]@397
  POINT v209; // [sp+78h] [bp-584h]@777
  POINT v210; // [sp+80h] [bp-57Ch]@397
  POINT v211; // [sp+88h] [bp-574h]@704
  __int64 v212; // [sp+90h] [bp-56Ch]@467
  int v213; // [sp+98h] [bp-564h]@385
  char pLevelName[32]; // [sp+9Ch] [bp-560h]@380
  char pOut[32]; // [sp+BCh] [bp-540h]@370
  FrameTableTxtLine v216; // [sp+DCh] [bp-520h]@524
  int v217[9]; // [sp+158h] [bp-4A4h]@652
  FrameTableTxtLine v218; // [sp+17Ch] [bp-480h]@524
  char a1[64]; // [sp+1F8h] [bp-404h]@467
  char Str2[128]; // [sp+238h] [bp-3C4h]@527
  Actor actor; // [sp+2B8h] [bp-344h]@4

  dword_50CDC8 = 0;
  if ( !pEventTimer->bPaused )
  {
    pParty->sEyelevel = pParty->uDefaultEyelevel;
    pParty->uPartyHeight = pParty->uDefaultPartyHeight;
  }
  if ( bDialogueUI_InitializeActor_NPC_ID )
  {
    //Actor::Actor(&actor);
    memset(&actor, 0, 0x344u);
    dword_5B65D0_dialogue_actor_npc_id = bDialogueUI_InitializeActor_NPC_ID;
    actor.sNPC_ID = bDialogueUI_InitializeActor_NPC_ID;
    GameUI_InitializeDialogue(&actor, false);
    bDialogueUI_InitializeActor_NPC_ID = 0;
  }
  if ( pMessageQueue_50CBD0->uNumMessages )
  {
    //v1 = "";
    while ( 2 )
    {
      if ( !pMessageQueue_50CBD0->uNumMessages )
        break;

      pMessageQueue_50CBD0->PopMessage(&uMessage, &uMessageParam, (int *)&v199);
      switch ( uMessage )
      {
        case UIMSG_ChangeGameState:
          uGameState = GAME_FINISHED;
          continue;
        case UIMSG_PlayArcomage:
          pVideoPlayer->_4BF5B2();
          pArcomageGame->bGameInProgress = 1;
          PrepareArcomage();
          continue;
        case UIMSG_StartNPCDialogue:
          if ( !uActiveCharacter )
            continue;
          viewparams->field_48 = 1;
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          dword_5B65D0_dialogue_actor_npc_id = pActors[uMessageParam].sNPC_ID;
          pActor = &pActors[uMessageParam];
          //goto _actor_init_dlg;
          GameUI_InitializeDialogue(pActor, true);
          continue;
        case UIMSG_StartHireling1Dialogue:
        case UIMSG_StartHireling2Dialogue:
        {
          if (bNoNPCHiring || pCurrentScreen)
            continue;

            if ( pMessageQueue_50CBD0->uNumMessages )
              pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
            uAction = 0;
            int hireling_idx = uMessage - UIMSG_StartHireling1Dialogue;
            v4 = 0;

            for (uint i = 0; i < 2; ++i)
            //pNPCData0 = pParty->pHirelings;
            //do
            {
              if (pParty->pHirelings[i].pName)
              //{
                //v6 = uAction++;
                pTmpBuf[uAction++] = i;
              //}
              //++pNPCData0;
              //++v4;
            }
            //while ( (signed int)pNPCData0 < (signed int)&pParty->pPickedItem );

            //_this = 0;
            for (uint i = 0; i < pNPCStats->uNumNewNPCs; ++i)
            {
              auto npc = pNPCStats->pNewNPCData + i;
              //do
              //{
                if (npc->Hired() &&
                    (!pParty->pHirelings[0].pName || strcmp(npc->pName, pParty->pHirelings[0].pName)) &&
                    (!pParty->pHirelings[1].pName || strcmp(npc->pName, pParty->pHirelings[1].pName)) )
                {
                  //v7 =;
                  pTmpBuf[uAction++] = i + 2;
                }
                //++_this;
                //++pNPCData4;
              //}
              //while ( _this < (signed int)pNPCStats->uNumNewNPCs );
            }

            if ( (signed int)(hireling_idx + (unsigned __int8)pParty->field_709) < uAction )
            {
              //Actor::Actor(&actor);
              memset(&actor, 0, 0x344u);
              actor.sNPC_ID += -1 - (unsigned __int8)pParty->field_709 - hireling_idx;
              pActor = &actor;
              GameUI_InitializeDialogue(&actor, true);
            }
          }
          continue;

        case UIMSG_BuyInShop_Identify_Repair:
          UIShop_Buy_Identify_Repair();
          continue;
        case UIMSG_ClickNPCTopic:
          ClickNPCTopic(uMessageParam);
          continue;
        case UIMSG_SelectShopDialogueOption:
          OnSelectShopDialogueOption(uMessageParam);
          continue;
        case UIMSG_SelectNPCDialogueOption:
          OnSelectNPCDialogueOption((DIALOGUE_TYPE)uMessageParam);
          continue;
        case UIMSG_ClickHouseNPCPortrait:
          _4B4224_UpdateNPCTopics(uMessageParam);
          continue;
        case UIMSG_StartNewGame:
          if ( dword_6BE138 == 124 || uMessageParam )
          {
            pIcons_LOD->SyncLoadedFilesCount();
            pIcons_LOD->_4114F2();
            if ( pMessageQueue_50CBD0->uNumMessages )
              pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
            pGUIWindow_CurrentMenu->Release();
            uGameState = GAME_STATE_NEWGAME_OUT_GAMEMENU;
            pCurrentScreen = SCREEN_GAME;
            viewparams->bRedrawGameUI = 1;
          }
          else
          {
            ShowStatusBarString(pGlobalTXT_LocalizationStrings[201], 2u);// "Are you sure?  Click again to start a New Game"
            pAudioPlayer->PlaySound(SOUND_20001, 0, 0, -1, 0, 0, 0, 0);
            dword_6BE138 = 124;
          }
          stru_506E40.Release();
          continue;
        case UIMSG_Game_OpenLoadGameDialog:
          pIcons_LOD->SyncLoadedFilesCount();
          pIcons_LOD->_4114F2();
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          pGUIWindow_CurrentMenu->Release();
          pCurrentScreen = SCREEN_LOADGAME;
          LoadUI_Load(1);
          continue;
        case UIMSG_Quit:
          if ( dword_6BE138 == 132 || uMessageParam )
          {
            pIcons_LOD->SyncLoadedFilesCount();
            pIcons_LOD->_4114F2();
            if ( pMessageQueue_50CBD0->uNumMessages )
              pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
            pGUIWindow_CurrentMenu->Release();
            pCurrentScreen = SCREEN_GAME;
            viewparams->bRedrawGameUI = 1;
            if ( !uMessageParam )
              pAudioPlayer->PlaySound((SoundID)(SOUND_EnteringAHouse|0x1), 0, 0, -1, 0, 0, 0, 0);
            uGameState = GAME_STATE_GAME_QUITTING_TO_MAIN_MENU;
          }
          else
          {
            ShowStatusBarString(pGlobalTXT_LocalizationStrings[82], 2);// "Are you sure?  Click again to quit"
            pAudioPlayer->PlaySound(SOUND_20001, 0, 0, -1, 0, 0, 0, 0);
            dword_6BE138 = 132;
          }
          stru_506E40.Release();
          continue;
        case UIMSG_80:
			__debugbreak();
          pIcons_LOD->SyncLoadedFilesCount();
          pIcons_LOD->_4114F2();
          pGUIWindow_CurrentMenu->Release();
          pCurrentScreen = SCREEN_OPTIONS;
          pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640, 480, WINDOW_8, 0, 0);
          continue;
        case UIMSG_ArrowUp:
          --pSaveListPosition;
          if ( pSaveListPosition < 0 )
            pSaveListPosition = 0;
          GUIWindow::Create(215, 199, 17, 17, WINDOW_PressedButton2, (int)pBtnArrowUp, 0);
          continue;
        case UIMSG_DownArrow:
          ++pSaveListPosition;
          if ( pSaveListPosition >= uMessageParam )
            pSaveListPosition = uMessageParam - 1;
          GUIWindow::Create(215, 323, 17, 17, WINDOW_PressedButton2, (int)pBtnDownArrow, 0);
          continue;
        case UIMSG_Cancel:
          GUIWindow::Create(350, 302, 106, 42, WINDOW_CloseRestWindowBtn, (int)pBtnCancel, 0);
          continue;
        case UIMSG_SaveLoadBtn:
          GUIWindow::Create(241, 302, 106, 42, WINDOW_SaveLoadBtn, (int)pBtnLoadSlot, 0);
          continue;
        case UIMSG_SelectLoadSlot:
          if ( pGUIWindow_CurrentMenu->receives_keyboard_input_2 == WINDOW_INPUT_IN_PROGRESS)
            pKeyActionMap->SetWindowInputStatus(WINDOW_INPUT_NONE);
          if ( pCurrentScreen != SCREEN_SAVEGAME || uLoadGameUI_SelectedSlot != pSaveListPosition + uMessageParam )
          {
            v10 = pSaveListPosition + uMessageParam;
            if ( dword_6BE138 == pSaveListPosition + uMessageParam )
            {
              pMessageQueue_50CBD0->AddMessage(UIMSG_SaveLoadBtn, 0, 0);
              pMessageQueue_50CBD0->AddMessage(UIMSG_LoadGame, 0, 0);
            }
            uLoadGameUI_SelectedSlot = v10;
            dword_6BE138 = v10;
          }
          else
          {
            pKeyActionMap->EnterText(0, 19, pGUIWindow_CurrentMenu);
            if ( strcmp((const char *)&pSavegameHeader[uLoadGameUI_SelectedSlot], pGlobalTXT_LocalizationStrings[72]) )// "Empty"
              strcpy((char *)pKeyActionMap->pPressedKeysBuffer, (const char *)&pSavegameHeader[uLoadGameUI_SelectedSlot]);
            pKeyActionMap->uNumKeysPressed = strlen((const char *)pKeyActionMap->pPressedKeysBuffer);
          }
          continue;
        case UIMSG_LoadGame:
          if ( pSavegameUsedSlots[uLoadGameUI_SelectedSlot] )
          {
            LoadGame(uLoadGameUI_SelectedSlot);
            uGameState = GAME_STATE_LOADING_GAME;
          }
          stru_506E40.Release();
          continue;
        case UIMSG_SaveGame:
          if ( pGUIWindow_CurrentMenu->receives_keyboard_input_2 == WINDOW_INPUT_IN_PROGRESS)
          {
            pKeyActionMap->SetWindowInputStatus(WINDOW_INPUT_NONE);
            strcpy((char *)&pSavegameHeader[uLoadGameUI_SelectedSlot], (const char *)pKeyActionMap->pPressedKeysBuffer);
          }
          DoSavegame(uLoadGameUI_SelectedSlot);
          stru_506E40.Release();
          continue;
        case UIMSG_Game_OpenSaveGameDialog:
          pGUIWindow_CurrentMenu->Release();
          pCurrentScreen = SCREEN_SAVEGAME;
          SaveUI_Load();
          continue;
        case UIMSG_Game_OpenOptionsDialog://Open
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          pGUIWindow_CurrentMenu->Release();
          viewparams->field_48 = 1;
          pCurrentScreen = SCREEN_OPTIONS;

          options_menu_skin.uTextureID_Background     = pIcons_LOD->LoadTexture("ControlBG", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_TurnSpeed[2]   = pIcons_LOD->LoadTexture("con_16x", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_TurnSpeed[1]   = pIcons_LOD->LoadTexture("con_32x", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_TurnSpeed[0]   = pIcons_LOD->LoadTexture("con_Smoo", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_ArrowLeft      = pIcons_LOD->LoadTexture("con_ArrL", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_ArrowRight     = pIcons_LOD->LoadTexture("con_ArrR", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_SoundLevels[0] = pIcons_LOD->LoadTexture("convol10", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_SoundLevels[1] = pIcons_LOD->LoadTexture("convol20", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_SoundLevels[2] = pIcons_LOD->LoadTexture("convol30", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_SoundLevels[3] = pIcons_LOD->LoadTexture("convol40", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_SoundLevels[4] = pIcons_LOD->LoadTexture("convol50", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_SoundLevels[5] = pIcons_LOD->LoadTexture("convol60", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_SoundLevels[6] = pIcons_LOD->LoadTexture("convol70", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_SoundLevels[7] = pIcons_LOD->LoadTexture("convol80", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_SoundLevels[8] = pIcons_LOD->LoadTexture("convol90", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_SoundLevels[9] = pIcons_LOD->LoadTexture("convol00", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_FlipOnExit     = pIcons_LOD->LoadTexture("option04", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_AlwaysRun      = pIcons_LOD->LoadTexture("option03", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_ShowDamage     = pIcons_LOD->LoadTexture("option02", TEXTURE_16BIT_PALETTE);
          options_menu_skin.uTextureID_WalkSound      = pIcons_LOD->LoadTexture("option01", TEXTURE_16BIT_PALETTE);

          pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640, 480, WINDOW_Options, 0, 0);
          pGUIWindow_CurrentMenu->CreateButton(22, 270,
                                               pIcons_LOD->GetTexture(options_menu_skin.uTextureID_TurnSpeed[2])->uTextureWidth,
                                               pIcons_LOD->GetTexture(options_menu_skin.uTextureID_TurnSpeed[2])->uTextureHeight,
                                               1, 0, UIMSG_SetTurnSpeed, 0x80, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(93, 270,
                                               pIcons_LOD->GetTexture(options_menu_skin.uTextureID_TurnSpeed[1])->uTextureWidth,
                                               pIcons_LOD->GetTexture(options_menu_skin.uTextureID_TurnSpeed[1])->uTextureHeight,
                                               1, 0, UIMSG_SetTurnSpeed, 0x40u, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(164, 270,
                                               pIcons_LOD->GetTexture(options_menu_skin.uTextureID_TurnSpeed[0])->uTextureWidth,
                                               pIcons_LOD->GetTexture(options_menu_skin.uTextureID_TurnSpeed[0])->uTextureHeight,
                                               1, 0, UIMSG_SetTurnSpeed, 0, 0, "", 0);

          pGUIWindow_CurrentMenu->CreateButton(20, 303,
                                               pIcons_LOD->GetTexture(options_menu_skin.uTextureID_WalkSound)->uTextureWidth,
                                               pIcons_LOD->GetTexture(options_menu_skin.uTextureID_WalkSound)->uTextureHeight,
                                               1, 0, UIMSG_ToggleWalkSound, 0, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(128, 303,
                                               pIcons_LOD->GetTexture(options_menu_skin.uTextureID_ShowDamage)->uTextureWidth,
                                               pIcons_LOD->GetTexture(options_menu_skin.uTextureID_ShowDamage)->uTextureHeight,
                                               1, 0, UIMSG_ToggleShowDamage, 0, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(20, 325,
                                               pIcons_LOD->GetTexture(options_menu_skin.uTextureID_AlwaysRun)->uTextureWidth,
                                               pIcons_LOD->GetTexture(options_menu_skin.uTextureID_AlwaysRun)->uTextureHeight,
                                               1, 0, UIMSG_ToggleAlwaysRun, 0, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(128, 325,
                                               pIcons_LOD->GetTexture(options_menu_skin.uTextureID_FlipOnExit)->uTextureWidth,
                                               pIcons_LOD->GetTexture(options_menu_skin.uTextureID_FlipOnExit)->uTextureHeight,
                                               1, 0, UIMSG_ToggleFlipOnExit, 0, 0, "", 0);

          pBtn_SliderLeft  = pGUIWindow_CurrentMenu->CreateButton(243, 162, 16, 16, 1, 0, UIMSG_ChangeSoundVolume, 4, 0, "", pIcons_LOD->GetTexture(options_menu_skin.uTextureID_ArrowLeft), 0);
          pBtn_SliderRight = pGUIWindow_CurrentMenu->CreateButton(435, 162, 16, 16, 1, 0, UIMSG_ChangeSoundVolume, 5, 0, "", pIcons_LOD->GetTexture(options_menu_skin.uTextureID_ArrowRight), 0);
          pGUIWindow_CurrentMenu->CreateButton(263, 162, 172, 17, 1, 0, UIMSG_ChangeSoundVolume, 0, 0, "", 0);

          pBtn_SliderLeft  = pGUIWindow_CurrentMenu->CreateButton(243, 216, 16, 16, 1, 0, UIMSG_ChangeMusicVolume, 4, 0, "", pIcons_LOD->GetTexture(options_menu_skin.uTextureID_ArrowLeft), 0);
          pBtn_SliderRight = pGUIWindow_CurrentMenu->CreateButton(435, 216, 16, 16, 1, 0, UIMSG_ChangeMusicVolume, 5, 0, "", pIcons_LOD->GetTexture(options_menu_skin.uTextureID_ArrowRight), 0);
          pGUIWindow_CurrentMenu->CreateButton(263, 216, 172, 17, 1, 0, UIMSG_ChangeMusicVolume, 0, 0, "", 0);

          pBtn_SliderLeft  = pGUIWindow_CurrentMenu->CreateButton(243, 270, 16, 16, 1, 0, UIMSG_ChangeVoiceVolume, 4, 0, "", pIcons_LOD->GetTexture(options_menu_skin.uTextureID_ArrowLeft), 0);
          pBtn_SliderRight = pGUIWindow_CurrentMenu->CreateButton(435, 270, 16, 16, 1, 0, UIMSG_ChangeVoiceVolume, 5, 0, "", pIcons_LOD->GetTexture(options_menu_skin.uTextureID_ArrowRight), 0);
          pGUIWindow_CurrentMenu->CreateButton(263, 270, 172, 17, 1, 0, UIMSG_ChangeVoiceVolume, 0, 0, "", 0);

          pGUIWindow_CurrentMenu->CreateButton(241, 302, 214, 40, 1, 0, UIMSG_Escape, 0, 0, pGlobalTXT_LocalizationStrings[619], 0); // "Return to Game"
          pGUIWindow_CurrentMenu->CreateButton( 19, 140, 214, 40, 1, 0, UIMSG_OpenKeyMappingOptions, 0, 0x4Bu, "", 0);
          pGUIWindow_CurrentMenu->CreateButton( 19, 194, 214, 40, 1, 0, UIMSG_OpenVideoOptions, 0, 86, "", 0);
          continue;

        case UIMSG_OpenKeyMappingOptions://Open
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          pGUIWindow_CurrentMenu->Release();
          viewparams->field_48 = 1;
          pCurrentScreen = SCREEN_KEYBOARD_OPTIONS;
          uTextureID_Optkb[0] = pIcons_LOD->LoadTexture("optkb", TEXTURE_16BIT_PALETTE);
          uTextureID_Optkb[1] = pIcons_LOD->LoadTexture("optkb_h", TEXTURE_16BIT_PALETTE);
          uTextureID_Optkb[2] = pIcons_LOD->LoadTexture("resume1", TEXTURE_16BIT_PALETTE);
          uTextureID_Optkb[3] = pIcons_LOD->LoadTexture("optkb_1", TEXTURE_16BIT_PALETTE);
          uTextureID_Optkb[4] = pIcons_LOD->LoadTexture("optkb_2", TEXTURE_16BIT_PALETTE);
          pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640, 480, WINDOW_KeyMappingOptions, 0, 0);
          pGUIWindow_CurrentMenu->CreateButton(0xF1u, 0x12Eu, 0xD6u, 0x28u, 1, 0, UIMSG_Escape, 0, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(19u, 0x12Eu, 0x6Cu, 0x14u, 1, 0, UIMSG_SelectKeyPage1, 0, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(127u, 0x12Eu, 0x6Cu, 0x14u, 1, 0, UIMSG_SelectKeyPage2, 0, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(127u, 0x144u, 0x6Cu, 0x14u, 1, 0, UIMSG_ResetKeyMapping, 0, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(19u, 0x144u, 0x6Cu, 0x14u, 1, 0, UIMSG_Game_OpenOptionsDialog, 0, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(129u, 0x94u, 0x46u, 0x13u, 1, 0, UIMSG_ChangeKeyButton, 0, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(129u, 0xA7u, 0x46u, 0x13u, 1, 0, UIMSG_ChangeKeyButton, 1u, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(129u, 0xBAu, 0x46u, 0x13u, 1, 0, UIMSG_ChangeKeyButton, 2u, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(129u, 0xCDu, 0x46u, 0x13u, 1, 0, UIMSG_ChangeKeyButton, 3u, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(129u, 224u, 70u, 19u, 1, 0, UIMSG_ChangeKeyButton, 4u, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(129u, 243u, 70u, 0x13u, 1, 0, UIMSG_ChangeKeyButton, 5u, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(129u, 262u, 70u, 0x13u, 1, 0, UIMSG_ChangeKeyButton, 6u, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(350u, 148u, 70u, 0x13u, 1, 0, UIMSG_ChangeKeyButton, 7u, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(350u, 167u, 70u, 0x13u, 1, 0, UIMSG_ChangeKeyButton, 8u, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(350u, 186u, 70u, 0x13u, 1, 0, UIMSG_ChangeKeyButton, 9u, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(350u, 205u, 70u, 0x13u, 1, 0, UIMSG_ChangeKeyButton, 0xAu, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(350u, 224u, 70u, 0x13u, 1, 0, UIMSG_ChangeKeyButton, 0xBu, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(350u, 243u, 70u, 0x13u, 1, 0, UIMSG_ChangeKeyButton, 0xCu, 0, "", 0);
          pGUIWindow_CurrentMenu->CreateButton(350u, 262u, 70u, 0x13u, 1, 0, UIMSG_ChangeKeyButton, 0xDu, 0, "", 0);
          uGameMenuUI_CurentlySelectedKeyIdx = -1;
          KeyboardPageNum = 1;
          memset(GameMenuUI_InvaligKeyBindingsFlags.data(), 0, sizeof(GameMenuUI_InvaligKeyBindingsFlags));
          //*(_WORD *)KeyButtonArray[28] = 0;
          memcpy(pPrevVirtualCidesMapping.data(), pKeyActionMap->pVirtualKeyCodesMapping, 0x78u);
          //v1 = "";
          //v0 = 1;
          continue;
        case UIMSG_ChangeKeyButton:
          if ( uGameMenuUI_CurentlySelectedKeyIdx != -1 )
          {
            pAudioPlayer->PlaySound((SoundID)27, 0, 0, -1, 0, 0, 0, 0);
            continue;
          }
          v14 = uMessageParam;
          if ( KeyboardPageNum != 1 )
            v14 = uMessageParam + 14;
          uGameMenuUI_CurentlySelectedKeyIdx = v14;
          pKeyActionMap->EnterText(0, 1, pGUIWindow_CurrentMenu);
          continue;
        case UIMSG_ResetKeyMapping:
          v197 = 1;
          pKeyActionMap->SetDefaultMapping();
          uAction = 0;
          do
          {
            v15 = pKeyActionMap->GetActionVKey((enum InputAction)uAction);
            if ( v15 != pPrevVirtualCidesMapping[uAction] )
            {
              if ( v197 )
              {
                v16 = pKeyActionMap->GetActionVKey((enum InputAction)uAction);
                GUI_ReplaceHotkey(LOBYTE(pPrevVirtualCidesMapping[uAction]), v16, 1);
                v197 = 0;
              }
              else
              {
                v17 = pKeyActionMap->GetActionVKey((enum InputAction)uAction);
                GUI_ReplaceHotkey(LOBYTE(pPrevVirtualCidesMapping[uAction]), v17, 0);
              }
            }
            pPrevVirtualCidesMapping[uAction] = pKeyActionMap->GetActionVKey((enum InputAction)uAction);
            v18 = uAction++;
            v13 = uAction > 28;
            v12 = uAction - 28 < 0;
            GameMenuUI_InvaligKeyBindingsFlags[v18] = 0;
          }
          while ( v12 ^ v13 );
          pAudioPlayer->PlaySound((SoundID)219, 0, 0, -1, 0, 0, 0, 0);
          continue;
        case UIMSG_SelectKeyPage1:
          KeyboardPageNum = 1;
          continue;
        case UIMSG_SelectKeyPage2:
          KeyboardPageNum = 2;
          continue;
        case UIMSG_OpenVideoOptions:
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          pGUIWindow_CurrentMenu->Release();
          viewparams->field_48 = 1;
          pCurrentScreen = SCREEN_VIDEO_OPTIONS;
          uTextureID_507C10 = pIcons_LOD->LoadTexture("optvid", TEXTURE_16BIT_PALETTE);
          uTextureID_507C14 = pIcons_LOD->LoadTexture("opvdH-bs", TEXTURE_16BIT_PALETTE);
          uTextureID_507C18 = pIcons_LOD->LoadTexture("opvdH-cl", TEXTURE_16BIT_PALETTE);
          uTextureID_507C1C = pIcons_LOD->LoadTexture("opvdH-tn", TEXTURE_16BIT_PALETTE);
          uTextureID_507C20 = pIcons_LOD->LoadTexture("con_ArrL", TEXTURE_16BIT_PALETTE);
          uTextureID_507C24 = pIcons_LOD->LoadTexture("con_ArrR", TEXTURE_16BIT_PALETTE);
          pTextureIDs_GammaPositions[0] = pIcons_LOD->LoadTexture("convol10", TEXTURE_16BIT_PALETTE);
          pTextureIDs_GammaPositions[1] = pIcons_LOD->LoadTexture("convol20", TEXTURE_16BIT_PALETTE);
          pTextureIDs_GammaPositions[2] = pIcons_LOD->LoadTexture("convol30", TEXTURE_16BIT_PALETTE);
          pTextureIDs_GammaPositions[3] = pIcons_LOD->LoadTexture("convol40", TEXTURE_16BIT_PALETTE);
          pTextureIDs_GammaPositions[4] = pIcons_LOD->LoadTexture("convol50", TEXTURE_16BIT_PALETTE);
          pTextureIDs_GammaPositions[5] = pIcons_LOD->LoadTexture("convol60", TEXTURE_16BIT_PALETTE);
          pTextureIDs_GammaPositions[6] = pIcons_LOD->LoadTexture("convol70", TEXTURE_16BIT_PALETTE);
          pTextureIDs_GammaPositions[7] = pIcons_LOD->LoadTexture("convol80", TEXTURE_16BIT_PALETTE);
          pTextureIDs_GammaPositions[8] = pIcons_LOD->LoadTexture("convol90", TEXTURE_16BIT_PALETTE);
          pTextureIDs_GammaPositions[9] = pIcons_LOD->LoadTexture("convol00", TEXTURE_16BIT_PALETTE);
          uTextureID_507C50 = pIcons_LOD->LoadTexture("opvdG-bs", TEXTURE_16BIT_PALETTE);
          uTextureID_507C54 = pIcons_LOD->LoadTexture("opvdG-cl", TEXTURE_16BIT_PALETTE);
          uTextureID_507C58 = pIcons_LOD->LoadTexture("opvdG-tn", TEXTURE_16BIT_PALETTE);
          pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640, 480, WINDOW_VideoOptions, 0, 0);
          pGUIWindow_CurrentMenu->CreateButton(0xF1u, 0x12Eu, 0xD6u, 0x28u, 1, 0, UIMSG_Escape, 0, 0, "", 0);
          if ( pRenderer->pRenderD3D )
          {
            pGUIWindow_CurrentMenu->CreateButton(0x13u, 0x118u, 0xD6u, 0x12u, 1, 0, UIMSG_ToggleBloodsplats, 0, 0, "", 0);
            pGUIWindow_CurrentMenu->CreateButton(0x13u, 0x12Eu, 0xD6u, 0x12u, 1, 0, UIMSG_ToggleColoredLights, 0, 0, "", 0);
            pGUIWindow_CurrentMenu->CreateButton(0x13u, 0x144u, 0xD6u, 0x12u, 1, 0, UIMSG_ToggleTint, 0, 0, "", 0);
          }
          if ( !pRenderer->bWindowMode )
          {
            //v0 = 1;
            if ( GammaController::IsGammaSupported() )
            {
              pBtn_SliderLeft = pGUIWindow_CurrentMenu->CreateButton(0x15u, 0xA1u, 0x10u, 0x10u, 1, 0, UIMSG_1A9, 4u, 0, "", pIcons_LOD->GetTexture(uTextureID_507C20), 0);
              pBtn_SliderRight = pGUIWindow_CurrentMenu->CreateButton(0xD5u, 0xA1u, 0x10u, 0x10u, 1, 0, UIMSG_1A9, 5u, 0, "", pIcons_LOD->GetTexture(uTextureID_507C24), 0);
              pGUIWindow_CurrentMenu->CreateButton(42, 162, 170, 18, 1, 0, UIMSG_1A9, 0, 0, "", 0);
            }
          }
          continue;
        case UIMSG_1A9:
			__debugbreak();
          if ( uMessageParam == 4 )
          {
            //--uGammaPos;
            if ( (uGammaPos -- -1) < 0 )
            {
              uGammaPos = 0;
              pAudioPlayer->PlaySound((SoundID)21, 0, 0, -1, 0, 0, 0, 0);
              continue;
            }
            v19 = (double)(signed int)uGammaPos * 0.1 + 0.6;
            pGame->pGammaController->Initialize(v19);
            GUIWindow::Create(21, 161, 0, 0, WINDOW_PressedButton2, (int)pBtn_SliderLeft, (char *)1);
            pAudioPlayer->PlaySound((SoundID)21, 0, 0, -1, 0, 0, 0, 0);
            continue;
          }
          if ( uMessageParam == 5 )
          {
            ++uGammaPos;
            if ( (signed int)uGammaPos <= 9 )
            {
              v21 = (double)(signed int)uGammaPos * 0.1 + 0.6;
              pGame->pGammaController->Initialize(v21);
              GUIWindow::Create(213, 161, 0, 0, WINDOW_PressedButton2, (int)pBtn_SliderRight, (char *)1);
              pAudioPlayer->PlaySound((SoundID)21, 0, 0, -1, 0, 0, 0, 0);
              continue;
            }
            uGammaPos = 9;
          }
          else
          {
            uGammaPos = (pMouse->GetCursorPos(&a2)->x - 42) / 17;
            v22 = (double)(signed int)uGammaPos * 0.1 + 0.6;
            pGame->pGammaController->Initialize(v22);
          }
          pAudioPlayer->PlaySound((SoundID)21, 0, 0, -1, 0, 0, 0, 0);
          continue;
        case UIMSG_ToggleBloodsplats:
          pGame->ToggleFlags2(0x20u);
          continue;
        case UIMSG_ToggleColoredLights:
          pRenderer->bUseColoredLights = pRenderer->bUseColoredLights == 0;
          continue;
        case UIMSG_ToggleTint:
          pRenderer->bTinting = pRenderer->bTinting == 0;
          continue;
        case UIMSG_ChangeMusicVolume:
          if ( uMessageParam == 4 )
          {
            --uMusicVolimeMultiplier;
            if ( (char)uMusicVolimeMultiplier < 1 )
              uMusicVolimeMultiplier = 0;
            GUIWindow::Create(243, 0xD8u, 0, 0, WINDOW_PressedButton2, (int)pBtn_SliderLeft, (char *)1);
            if ( uMusicVolimeMultiplier )
              pAudioPlayer->PlaySound(SOUND_Bell, -1, 0, -1, 0, 0, pSoundVolumeLevels[(char)uMusicVolimeMultiplier] * 64.0f, 0);
            pAudioPlayer->SetMusicVolume(pSoundVolumeLevels[(char)uMusicVolimeMultiplier] * 64.0f);
            continue;
          }
          if ( uMessageParam == 5 )
          {
            ++uMusicVolimeMultiplier;
            if ( (char)uMusicVolimeMultiplier > 9 )
              uMusicVolimeMultiplier = 9;
            GUIWindow::Create(435, 0xD8u, 0, 0, WINDOW_PressedButton2, (int)pBtn_SliderRight, (char *)1);
            if ( uMusicVolimeMultiplier )
              pAudioPlayer->PlaySound(SOUND_Bell, -1, 0, -1, 0, 0, pSoundVolumeLevels[(char)uMusicVolimeMultiplier] * 64.0f, 0);
            pAudioPlayer->SetMusicVolume(pSoundVolumeLevels[(char)uMusicVolimeMultiplier] * 64.0f);
            continue;
          }
          uMusicVolimeMultiplier = (pMouse->GetCursorPos(&v202)->x - 263) / 17;
          if ( (char)uMusicVolimeMultiplier > 9 )
            uMusicVolimeMultiplier = 9;
          if ( uMusicVolimeMultiplier )
            pAudioPlayer->PlaySound(SOUND_Bell, -1, 0, -1, 0, 0, pSoundVolumeLevels[uMusicVolimeMultiplier] * 64.0f, 0);
          pAudioPlayer->SetMusicVolume(pSoundVolumeLevels[uMusicVolimeMultiplier] * 64.0f);
          continue;
        case UIMSG_ChangeSoundVolume:
          if ( uMessageParam == 4 )//reduce sound level button left
          {
            --uSoundVolumeMultiplier;
            if ( (char)uSoundVolumeMultiplier < 1 )
              uSoundVolumeMultiplier = 0;
            GUIWindow::Create(243, 162, 0, 0, WINDOW_PressedButton2, (int)pBtn_SliderLeft, (char *)1);
            pAudioPlayer->SetMasterVolume(pSoundVolumeLevels[uSoundVolumeMultiplier] * 128.0f);
            pAudioPlayer->PlaySound((SoundID)218, -1, 0, -1, 0, 0, 0, 0);
            continue;
          }
          if ( uMessageParam == 5 )//Increase sound level button right
          {
            ++uSoundVolumeMultiplier;
            if ( (char)uSoundVolumeMultiplier > 8 )
              uSoundVolumeMultiplier = 9;
            //v168 = 1;
            v24 = 435;
            //v154 = (int)pBtn_SliderRight;
            GUIWindow::Create(v24, 0xA2u, 0, 0, WINDOW_PressedButton2, (int)pBtn_SliderRight, (char *)1);
            pAudioPlayer->SetMasterVolume(pSoundVolumeLevels[uSoundVolumeMultiplier] * 128.0f);
            pAudioPlayer->PlaySound((SoundID)218, -1, 0, -1, 0, 0, 0, 0);
            continue;
          }
          uSoundVolumeMultiplier = (pMouse->GetCursorPos(&v207)->x - 263) / 17;
          if ( (char)uSoundVolumeMultiplier > 8 )
            uSoundVolumeMultiplier = 9;
          pAudioPlayer->SetMasterVolume(pSoundVolumeLevels[uSoundVolumeMultiplier] * 128.0f);
          pAudioPlayer->PlaySound((SoundID)218, -1, 0, -1, 0, 0, 0, 0);
          continue;
        case UIMSG_ToggleFlipOnExit:
          bFlipOnExit = bFlipOnExit == 0;
          continue;
        case UIMSG_ToggleAlwaysRun:
          bAlwaysRun = bAlwaysRun == 0;
          continue;
        case UIMSG_ToggleWalkSound:
          bWalkSound = bWalkSound == 0;
          continue;
        case UIMSG_ToggleShowDamage:
          bShowDamage = bShowDamage == 0;
          continue;
        case UIMSG_ChangeVoiceVolume:
          if ( uMessageParam == 4 )
          {
            --uVoicesVolumeMultiplier;
            if ( (char)uVoicesVolumeMultiplier < 1 )
              uVoicesVolumeMultiplier = 0;
            GUIWindow::Create(243, 270, 0, 0, WINDOW_PressedButton2, (int)pBtn_SliderLeft, (char *)1);
            if ( !uVoicesVolumeMultiplier )
              continue;
            pAudioPlayer->PlaySound((SoundID)5788, -1, 0, -1, 0, 0, pSoundVolumeLevels[uVoicesVolumeMultiplier] * 128.0f, 0);
            continue;
          }
          if ( uMessageParam == 5 )
          {
            ++uVoicesVolumeMultiplier;
            if ( (char)uVoicesVolumeMultiplier > 8 )
              uVoicesVolumeMultiplier = 9;
            GUIWindow::Create(435, 270, 0, 0, WINDOW_PressedButton2, (int)pBtn_SliderRight, (char *)1);
            if ( !uVoicesVolumeMultiplier )
              continue;
            pAudioPlayer->PlaySound((SoundID)5788, -1, 0, -1, 0, 0, pSoundVolumeLevels[uVoicesVolumeMultiplier] * 128.0f, 0);
            continue;
          }
          uVoicesVolumeMultiplier = (pMouse->GetCursorPos(&v205)->x - 263) / 17;
          if ( (char)uVoicesVolumeMultiplier > 8 )
            uVoicesVolumeMultiplier = 9;
          if ( !uVoicesVolumeMultiplier )
            continue;
            pAudioPlayer->PlaySound((SoundID)5788, -1, 0, -1, 0, 0, pSoundVolumeLevels[uVoicesVolumeMultiplier] * 128.0f, 0);
            continue;
        case UIMSG_SetTurnSpeed:
          if ( uMessageParam )
            pParty->sRotationY = uMessageParam * pParty->sRotationY / uMessageParam;
          uTurnSpeed = uMessageParam;
          continue;
        case UIMSG_SetGraphicsMode:
          if ( !bUseLoResSprites )
          {
            byte_6BE388_graphicsmode = uMessageParam;
            MM7Initialization();
            continue;
          }
          if ( uMessageParam )
          {
            if ( uMessageParam == 1 )
            {
              byte_6BE388_graphicsmode = 0;
            }
            else
            {
              if ( uMessageParam != 2 )
                continue;
              byte_6BE388_graphicsmode = 1;
            }
            MM7Initialization();
            continue;
          }
          ModalWindow(pNPCTopics[453].pText, 0);
          continue;
        case UIMSG_GameMenu_ReturnToGame:
          pGUIWindow_CurrentMenu->Release();
          pEventTimer->Resume();
          pCurrentScreen = SCREEN_GAME;
          viewparams->bRedrawGameUI = 1;
          stru_506E40.Release();
          continue;
        case UIMSG_OpenQuestBook:
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          if ( pCurrentScreen )
            pGUIWindow_CurrentMenu->Release();
          pEventTimer->Pause();
          pAudioPlayer->StopChannels(-1, -1);
          pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640u, 480u, WINDOW_Book, uMessage, 0);
          pBooksWindow = GUIWindow::Create(493u, 355u, 0, 0, WINDOW_BooksWindow, (int)pBtn_Quests, 0);
          bFlashQuestBook = 0;
          continue;
        case UIMSG_OpenAutonotes:
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          if ( pCurrentScreen )
            pGUIWindow_CurrentMenu->Release();
          pEventTimer->Pause();
          pAudioPlayer->StopChannels(-1, -1);
          pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640u, 480u, WINDOW_Book, uMessage, 0);
          pBooksWindow = GUIWindow::Create(527u, 353u, 0, 0, WINDOW_BooksWindow, (int)pBtn_Autonotes, 0);
          bFlashAutonotesBook = 0;
          continue;
        case UIMSG_OpenMapBook:
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          if ( pCurrentScreen )
            pGUIWindow_CurrentMenu->Release();
          pEventTimer->Pause();
          viewparams->sViewCenterX = pParty->vPosition.x;
          viewparams->sViewCenterY = pParty->vPosition.y;
          pAudioPlayer->StopChannels(-1, -1);
          pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640, 480, WINDOW_Book, uMessage, 0);
          pBooksWindow = GUIWindow::Create(546, 353, 0, 0, WINDOW_BooksWindow, (int)pBtn_Maps, 0);
          continue;
        case UIMSG_OpenCalendar:
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          if ( pCurrentScreen )
            pGUIWindow_CurrentMenu->Release();
          pEventTimer->Pause();
          pAudioPlayer->StopChannels(-1, -1);
          pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640, 480, WINDOW_Book, uMessage, 0);
          pBooksWindow = GUIWindow::Create(570, 354, 0, 0, WINDOW_BooksWindow, (int)pBtn_Calendar, 0);
          continue;
        case UIMSG_OpenHistoryBook:
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          if ( pCurrentScreen )
            pGUIWindow_CurrentMenu->Release();
          pEventTimer->Pause();
          pAudioPlayer->StopChannels(-1, -1);
          pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640, 480, WINDOW_Book, uMessage, 0);
          pBooksWindow = GUIWindow::Create(0x258u, 0x169u, 0, 0, WINDOW_BooksWindow, (int)pBtn_History, 0);
          bFlashHistoryBook = 0;
          continue;
        case UIMSG_Escape://  Escape and return to game
          back_to_game();
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          switch ( pCurrentScreen )
          {
            case SCREEN_E:
				__debugbreak();
            case SCREEN_NPC_DIALOGUE:
            case SCREEN_CHEST:
            case SCREEN_CHEST_INVENTORY:
            case SCREEN_CHANGE_LOCATION:
            case SCREEN_INPUT_BLV:
            case SCREEN_QUICK_REFERENCE:
              if ( dword_50CDC8 )
                break;
              CloseWindowBackground();
              uMessageParam = 1;
              break;
            case SCREEN_HOUSE:
              if ( !dword_50CDC8 && !dword_5C35C8 )
              {
                CloseWindowBackground();
                dword_5C35C8 = 0;
                uMessageParam = 1;
                break;
              }
              break;
          }
          if ( !ptr_507BDC )
          {
            pRenderer->ClearZBuffer(0, 479);
            viewparams->bRedrawGameUI = 1;
            viewparams->field_48 = 1;
            if ( pCurrentScreen )
            {
              if ( pCurrentScreen > SCREEN_67 )
              {
                if ( pCurrentScreen == SCREEN_QUICK_REFERENCE )
                {
                  pIcons_LOD->_4114F2();
                  if ( pGUIWindow_Settings )
                  {
                    if ( pCurrentScreen == SCREEN_CHARACTERS )
                    {
                      pMouse->SetCursorBitmap("MICON2");
                    }
                    else
                    {
                      pGUIWindow_Settings->Release();
                      pGUIWindow_Settings = 0;
                      pMouse->SetCursorBitmap("MICON1");
                      GameUI_Footer_TimeLeft = 0;
                      unk_50C9A0 = 0;
                      back_to_game();
                    }
                  }
                  if ( (signed int)uActiveCharacter < 1 || (signed int)uActiveCharacter > 4 )
                    uActiveCharacter = pParty->GetNextActiveCharacter();
                  pGUIWindow_CurrentMenu->Release();
                  if ( pGUIWindow_CurrentMenu == window_SpeakInHouse )
                    window_SpeakInHouse = 0;
                  pGUIWindow_CurrentMenu = 0;
                  pEventTimer->Resume();
                  pCurrentScreen = SCREEN_GAME;
                  viewparams->bRedrawGameUI = 1;
                  pIcons_LOD->_4355F7();
                  continue;
                }
              }
              else
              {
                if ( pCurrentScreen < SCREEN_64 )
                {
                  switch ( pCurrentScreen )
                  {
                    case SCREEN_CASTING:
                      pIcons_LOD->_4114F2();
                      if ( some_active_character )
                      {
                        uActiveCharacter = some_active_character;
                        uActiveCharacter = pParty->GetNextActiveCharacter();
                        some_active_character = 0;
                        if ( pParty->bTurnBasedModeOn )
                          pTurnEngine->_40471C();
                        dword_50C9D0 = 0;
                        dword_50C9D4 = 0;
                        dword_50C9D8 = 0;
                      }
                      if ( ptr_50C9A4 && ptr_50C9A4->uItemID )
                      {
                        LOBYTE(ptr_50C9A4->uAttributes) &= 0xFu;
                        _50C9A8_item_enchantment_timer = 0;
                        ptr_50C9A4 = 0;
                      }
                      if ( pGUIWindow_Settings )
                      {
                       if ( pCurrentScreen == SCREEN_CHARACTERS )
                       {
                        pMouse->SetCursorBitmap("MICON2");
                       }
                       else
                       {
                         pGUIWindow_Settings->Release();
                         pGUIWindow_Settings = 0;
                         pMouse->SetCursorBitmap("MICON1");
                         GameUI_Footer_TimeLeft = 0;
                         unk_50C9A0 = 0;
                         back_to_game();
                        }
                       }
                       if ( (signed int)uActiveCharacter < 1 || (signed int)uActiveCharacter > 4 )
                       uActiveCharacter = pParty->GetNextActiveCharacter();
                       pGUIWindow_CurrentMenu->Release();
                       if ( pGUIWindow_CurrentMenu == window_SpeakInHouse )
                        window_SpeakInHouse = 0;
                       pGUIWindow_CurrentMenu = 0;
                       pEventTimer->Resume();
                       pCurrentScreen = SCREEN_GAME;
                       viewparams->bRedrawGameUI = 1;
                       pIcons_LOD->_4355F7();
                       continue;
                    case SCREEN_BOOKS:
                      pBooksWindow->Release();
                      //crt_deconstruct_ptr_6A0118();
                      pBooksWindow = 0;
                      pEventTimer->Resume();
                      if ( pGUIWindow_Settings )
                      {
                        if ( pCurrentScreen == SCREEN_CHARACTERS )
                        {
                         pMouse->SetCursorBitmap("MICON2");
                        }
                        else
                        {
                         pGUIWindow_Settings->Release();
                         pGUIWindow_Settings = 0;
                         pMouse->SetCursorBitmap("MICON1");
                         GameUI_Footer_TimeLeft = 0;
                         unk_50C9A0 = 0;
                         back_to_game();
                        }
                      }
                      if ( (signed int)uActiveCharacter < 1 || (signed int)uActiveCharacter > 4 )
                        uActiveCharacter = pParty->GetNextActiveCharacter();
                      pGUIWindow_CurrentMenu->Release();
                      if ( pGUIWindow_CurrentMenu == window_SpeakInHouse )
                        window_SpeakInHouse = 0;
                      pGUIWindow_CurrentMenu = 0;
                      pEventTimer->Resume();
                      pCurrentScreen = SCREEN_GAME;
                      viewparams->bRedrawGameUI = 1;
                      pIcons_LOD->_4355F7();
                      continue;
                    case SCREEN_SAVEGAME:
                    case SCREEN_LOADGAME:
                      pIcons_LOD->_4114F2();
                      //crt_deconstruct_ptr_6A0118();
                      stru_506E40.Release();
                      break;
                    case SCREEN_CHEST_INVENTORY:
                      pCurrentScreen = SCREEN_CHEST;
                      continue;
                    case SCREEN_CHEST:
                      pWindow2 = pChestWindow;
                      pWindow2->Release();
                      pIcons_LOD->_4114F2();
                      pCurrentScreen = SCREEN_GAME;
                      viewparams->bRedrawGameUI = 1;
                      pEventTimer->Resume();
                      continue;
                    case SCREEN_19:
						__debugbreak();
                      pWindow2 = ptr_507BC8;
                      pWindow2->Release();
                      pIcons_LOD->_4114F2();
                      pCurrentScreen = SCREEN_GAME;
                      viewparams->bRedrawGameUI = 1;
                      pEventTimer->Resume();
                      continue;
                    case SCREEN_OPTIONS://Close
                      options_menu_skin.Relaease();
                      pIcons_LOD->SyncLoadedFilesCount();
                      WriteWindowsRegistryInt("soundflag", (char)uSoundVolumeMultiplier);
                      WriteWindowsRegistryInt("musicflag", (char)uMusicVolimeMultiplier);
                      WriteWindowsRegistryInt("CharVoices", (char)uVoicesVolumeMultiplier);
                      WriteWindowsRegistryInt("WalkSound", bWalkSound);
                      WriteWindowsRegistryInt("ShowDamage", bShowDamage);
                      WriteWindowsRegistryInt("graphicsmode", (unsigned __int8)byte_6BE388_graphicsmode);
                      WriteWindowsRegistryInt("valAlwaysRun", bAlwaysRun);
                      WriteWindowsRegistryInt("FlipOnExit", bFlipOnExit);
                      if ( !uTurnSpeed )
                      {
                        WriteWindowsRegistryInt("TurnDelta", 3);
                        stru_506E40.Release();
                        break;
                      }
                      if ( uTurnSpeed == 64 )
                      {
                        WriteWindowsRegistryInt("TurnDelta", 2);
                        stru_506E40.Release();
                        break;
                      }
                      if ( uTurnSpeed != 128 )
                        {
                          stru_506E40.Release();
                          break;
                        }
                      WriteWindowsRegistryInt("TurnDelta", 1);
                      stru_506E40.Release();
                      break;
                    case SCREEN_MENU:
                      pIcons_LOD->SyncLoadedFilesCount();
                      pIcons_LOD->_4114F2();
                      pIcons_LOD->SyncLoadedFilesCount();
                      pIcons_LOD->_4114F2();
                      stru_506E40.Release();
                      break;
                    case SCREEN_VIDEO_OPTIONS:
                      if ( pRenderer->pRenderD3D )
                      {
                        WriteWindowsRegistryInt("Colored Lights", pRenderer->bUseColoredLights);
                        WriteWindowsRegistryInt("Tinting", pRenderer->bTinting);
                        WriteWindowsRegistryInt("Bloodsplats", (LOBYTE(pGame->uFlags2) >> 5) & 1);
                      }
                      if ( pRenderer->bWindowMode )
                        {
                          stru_506E40.Release();
                          break;
                        }
                      //v30 = uGammaPos;
                      //v31 = "GammaPos";
                      WriteWindowsRegistryInt("GammaPos", uGammaPos);
                      stru_506E40.Release();
                      break;

                    case SCREEN_KEYBOARD_OPTIONS://Return to game
                      v197 = 1;
                      v32 = 0;
                      while ( !GameMenuUI_InvaligKeyBindingsFlags[v32])
                      {
                        ++v32;
                        if ( v32 >= 28 )
                        {
                          thisb = (signed int)&uTextureID_Optkb;
                          assert(false && "Invalid condition values");
                          do
                          {
                            if ( *(int *)thisb )
                              pIcons_LOD->pTextures[*(int *)thisb].Release();
                            thisb += 4;
                          }
                          while ( thisb < (signed int)0x00507C08 );

                          memset(&uTextureID_Optkb, 0, 0x14u);
                          pIcons_LOD->SyncLoadedFilesCount();
                          uAction = 0;
                          do
                          {
                            v33 = pKeyActionMap->GetActionVKey((enum InputAction)uAction);
                            if ( v33 != pPrevVirtualCidesMapping[uAction] )
                            {
                              if ( v197 )
                              {
                                v34 = pKeyActionMap->GetActionVKey((enum InputAction)uAction);
                                GUI_ReplaceHotkey(v34, LOBYTE(pPrevVirtualCidesMapping[uAction]), 1);
                                v197 = 0;
                              }
                              else
                              {
                                v35 = pKeyActionMap->GetActionVKey((enum InputAction)uAction);
                                GUI_ReplaceHotkey(v35, LOBYTE(pPrevVirtualCidesMapping[uAction]), 0);
                              }
                            }
                            if ( uAction && uAction != 2 && uAction != 3 && uAction != 1 && uAction != 25 && uAction != 26 )
                              pKeyToggleType = (KeyToggleType)1;
                            else
                              pKeyToggleType = (KeyToggleType)0;
                            pKeyActionMap->SetKeyMapping(uAction, pPrevVirtualCidesMapping[uAction], pKeyToggleType);
                            ++uAction;
                          }
                          while ( uAction < 28 );
                          pKeyActionMap->StoreMappings();
                          stru_506E40.Release();
                          break;
                        }
                      }
                      break;
                    case SCREEN_REST://close rest screen
                      if ( dword_506F14 )
                      {
                        Rest(_506F18_num_hours_to_sleep);
                        pParty->pPlayers[3].pConditions[2] = 0i64;
                        pParty->pPlayers[2].pConditions[2] = 0i64;
                        pParty->pPlayers[1].pConditions[2] = 0i64;
                        pParty->pPlayers[0].pConditions[2] = 0i64;
                      }
                      pTexture_RestUI_CurrentSkyFrame->Release();
                      pTexture_RestUI_CurrentHourglassFrame->Release();
                      pTexture_RestUI_CurrentHourglassFrame = 0;
                      pTexture_RestUI_CurrentSkyFrame = 0;
                      pIcons_LOD->SyncLoadedFilesCount();
                      pIcons_LOD->_4114F2();
                      _506F18_num_hours_to_sleep = 0;
                      dword_506F14 = 0;
                      dword_507B94 = 1;
                      if ( pGUIWindow_Settings )
                      {
                        if ( pCurrentScreen == SCREEN_CHARACTERS )
                        {
                          pMouse->SetCursorBitmap("MICON2");
                        }
                        else
                        {
                          pGUIWindow_Settings->Release();
                          pGUIWindow_Settings = 0;
                          pMouse->SetCursorBitmap("MICON1");
                          GameUI_Footer_TimeLeft = 0;
                          unk_50C9A0 = 0;
                          back_to_game();
                         }
                       }
                       if ( (signed int)uActiveCharacter < 1 || (signed int)uActiveCharacter > 4 )
                        uActiveCharacter = pParty->GetNextActiveCharacter();
                       pGUIWindow_CurrentMenu->Release();
                       if ( pGUIWindow_CurrentMenu == window_SpeakInHouse )
                       window_SpeakInHouse = 0;
                       pGUIWindow_CurrentMenu = 0;
                       pEventTimer->Resume();
                       pCurrentScreen = SCREEN_GAME;
                       viewparams->bRedrawGameUI = 1;
                       pIcons_LOD->_4355F7();
                       continue;
                    case SCREEN_E:
						__debugbreak();
                      pGUIWindow_CurrentMenu->Release();
                      pCurrentScreen = SCREEN_HOUSE;
                      pIcons_LOD->_4114F2();
                      continue;
                    case SCREEN_HOUSE:
                      if ( uDialogueType )
                        uDialogueType = 0;
                      if ( uGameState == GAME_STATE_2 )
                      {
                        while ( sub_4BD8B5() )
                          ;
                      }
                      else
                      {
                        if ( sub_4BD8B5() )
                          continue;
                      }
                      sub_4B1D27();
                      pAudioPlayer->PlaySound(SOUND_7, 814, 0, -1, 0, 0, 0, 0);
                      pVideoPlayer->Unload();
                      pGUIWindow_CurrentMenu = window_SpeakInHouse;
                      if ( pGUIWindow_Settings )
                      {
                        if ( pCurrentScreen == SCREEN_CHARACTERS )
                        {
                          pMouse->SetCursorBitmap("MICON2");
                        }
                        else
                        {
                          pGUIWindow_Settings->Release();
                          pGUIWindow_Settings = 0;
                          pMouse->SetCursorBitmap("MICON1");
                          GameUI_Footer_TimeLeft = 0;
                          unk_50C9A0 = 0;
                          back_to_game();
                        }
                      }
                      if ( (signed int)uActiveCharacter < 1 || (signed int)uActiveCharacter > 4 )
                        uActiveCharacter = pParty->GetNextActiveCharacter();
                      pGUIWindow_CurrentMenu->Release();
                      if ( pGUIWindow_CurrentMenu == window_SpeakInHouse )
                        window_SpeakInHouse = 0;
                      pGUIWindow_CurrentMenu = 0;
                      pEventTimer->Resume();
                      pCurrentScreen = SCREEN_GAME;
                      viewparams->bRedrawGameUI = true;
                      pIcons_LOD->_4355F7();
                      continue;
                    case SCREEN_INPUT_BLV://click escape
                      if ( uCurrentHouse_Animation == 153 )
                        PlayHouseSound(0x99u, HouseSound_Greeting_2);
                      pVideoPlayer->Unload();
                      if ( dword_5B65CC )
                      {
                        pParty->field_709 = 0;
                        LOBYTE(pNPCStats->pNewNPCData[dword_5B65CC].uFlags) &= 0x7Fu;
                        pParty->CountHirelings();
                        viewparams->bRedrawGameUI = true;
                        dword_5B65CC = 0;
                      }
                      DialogueEnding();
                      pCurrentScreen = SCREEN_GAME;
                      viewparams->bRedrawGameUI = true;
                      continue;
                    case SCREEN_NPC_DIALOGUE://click escape
                      if ( dword_5B65CC )
                      {
                        pParty->field_709 = 0;
                        LOBYTE(pNPCStats->pNewNPCData[dword_5B65CC].uFlags) &= 0x7Fu;
                        pParty->CountHirelings();
                        viewparams->bRedrawGameUI = true;
                        dword_5B65CC = 0;
                      }
                      //goto LABEL_317;
                      DialogueEnding();
                      pCurrentScreen = SCREEN_GAME;
                      viewparams->bRedrawGameUI = true;
                      continue;
                    case SCREEN_BRANCHLESS_NPC_DIALOG://click escape
                      memset(GameUI_Footer_TimedString.data(), 0, 0xC8u);
                      sub_4452BB();
                      DialogueEnding();
                      pCurrentScreen = SCREEN_GAME;
                      viewparams->bRedrawGameUI = true;
                      continue;
                    case SCREEN_CHANGE_LOCATION://click escape
                      if ( pParty->vPosition.x < -22528 )
                        pParty->vPosition.x = -22528;
                      if ( pParty->vPosition.x > 22528 )
                        pParty->vPosition.x = 22528;
                      if ( pParty->vPosition.y < -22528 )
                        pParty->vPosition.y = -22528;
                      if ( pParty->vPosition.y > 22528 )
                        pParty->vPosition.y = 22528;
                      DialogueEnding();
                      pCurrentScreen = SCREEN_GAME;
                      viewparams->bRedrawGameUI = true;
                      continue;
                    case SCREEN_VIDEO:
                      pVideoPlayer->Unload();
                      continue;
                    case SCREEN_CHARACTERS:
                      CharacterUI_ReleaseButtons();
                      sub_419379();
                      pIcons_LOD->_4114F2();
                      if ( pGUIWindow_Settings )
                      {
                        if ( pCurrentScreen == SCREEN_CHARACTERS )
                        {
                          pMouse->SetCursorBitmap("MICON2");
                        }
                        else
                        {
                          pGUIWindow_Settings->Release();
                          pGUIWindow_Settings = 0;
                          pMouse->SetCursorBitmap("MICON1");
                          GameUI_Footer_TimeLeft = 0;
                          unk_50C9A0 = 0;
                          back_to_game();
                        }
                      }
                      if ( (signed int)uActiveCharacter < 1 || (signed int)uActiveCharacter > 4 )
                        uActiveCharacter = pParty->GetNextActiveCharacter();
                      pGUIWindow_CurrentMenu->Release();
                      if ( pGUIWindow_CurrentMenu == window_SpeakInHouse )
                        window_SpeakInHouse = 0;
                      pGUIWindow_CurrentMenu = 0;
                      pEventTimer->Resume();
                      pCurrentScreen = SCREEN_GAME;
                      viewparams->bRedrawGameUI = true;
                      pIcons_LOD->_4355F7();
                      continue;
                    default:
                      if ( pGUIWindow_Settings )
                      {
                        if ( pCurrentScreen == SCREEN_CHARACTERS )
                        {
                          pMouse->SetCursorBitmap("MICON2");
                        }
                        else
                        {
                          pGUIWindow_Settings->Release();
                          pGUIWindow_Settings = 0;
                          pMouse->SetCursorBitmap("MICON1");
                          GameUI_Footer_TimeLeft = 0;
                          unk_50C9A0 = 0;
                          back_to_game();
                        }
                      }
                      if ( (signed int)uActiveCharacter < 1 || (signed int)uActiveCharacter > 4 )
                        uActiveCharacter = pParty->GetNextActiveCharacter();
                      pGUIWindow_CurrentMenu->Release();
                      if ( pGUIWindow_CurrentMenu == window_SpeakInHouse )
                        window_SpeakInHouse = 0;
                      pGUIWindow_CurrentMenu = 0;
                      pEventTimer->Resume();
                      pCurrentScreen = SCREEN_GAME;
                      viewparams->bRedrawGameUI = 1;
                      pIcons_LOD->_4355F7();
                      continue;
                  }
                  if ( pGUIWindow_Settings )
                  {
                    if ( pCurrentScreen == SCREEN_CHARACTERS )
                    {
                      pMouse->SetCursorBitmap("MICON2");
                    }
                    else
                    {
                      pGUIWindow_Settings->Release();
                      pGUIWindow_Settings = 0;
                      pMouse->SetCursorBitmap("MICON1");
                      GameUI_Footer_TimeLeft = 0;
                      unk_50C9A0 = 0;
                      back_to_game();
                    }
                  }
                  if ( (signed int)uActiveCharacter < 1 || (signed int)uActiveCharacter > 4 )
                    uActiveCharacter = pParty->GetNextActiveCharacter();
                  pGUIWindow_CurrentMenu->Release();
                  if ( pGUIWindow_CurrentMenu == window_SpeakInHouse )
                    window_SpeakInHouse = 0;
                  pGUIWindow_CurrentMenu = 0;
                  pEventTimer->Resume();
                  pCurrentScreen = SCREEN_GAME;
                  viewparams->bRedrawGameUI = true;
                  pIcons_LOD->_4355F7();
                  continue;
                }
                CharacterUI_ReleaseButtons();
                sub_419379();
                pIcons_LOD->_4114F2();
              }
              if ( pGUIWindow_Settings )
              {
                if ( pCurrentScreen == SCREEN_CHARACTERS )
                {
                  pMouse->SetCursorBitmap("MICON2");
                }
                else
                {
                  pGUIWindow_Settings->Release();
                  pGUIWindow_Settings = 0;
                  pMouse->SetCursorBitmap("MICON1");
                  GameUI_Footer_TimeLeft = 0;
                  unk_50C9A0 = 0;
                  back_to_game();
                }
              }
              if ( (signed int)uActiveCharacter < 1 || (signed int)uActiveCharacter > 4 )
                uActiveCharacter = pParty->GetNextActiveCharacter();
              pGUIWindow_CurrentMenu->Release();
              if ( pGUIWindow_CurrentMenu == window_SpeakInHouse )
                window_SpeakInHouse = 0;
              pGUIWindow_CurrentMenu = 0;
              pEventTimer->Resume();
              pCurrentScreen = SCREEN_GAME;
              viewparams->bRedrawGameUI = true;
              pIcons_LOD->_4355F7();
              continue;
            }
            if ( !pGUIWindow_Settings )//Draw Menu
            {
              dword_6BE138 = -1;
              GUIWindow::Create(0x25Au, 0x1C2u, 0, 0, WINDOW_PressedButton2, (int)pBtn_GameSettings, (char *)1);
              pEventTimer->Pause();
              pAudioPlayer->StopChannels(-1, -1);
              pCurrentScreen = SCREEN_MENU;
              
              ++pIcons_LOD->uTexturePacksCount;
              if ( !pIcons_LOD->uNumPrevLoadedFiles )
                pIcons_LOD->uNumPrevLoadedFiles = pIcons_LOD->uNumLoadedFiles;

              pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640, 480, WINDOW_OptionsButtons, 0, 0);
              uTextureID_Options = pIcons_LOD->LoadTexture("options", TEXTURE_16BIT_PALETTE);
              uTextureID_New1 = pIcons_LOD->LoadTexture("new1", TEXTURE_16BIT_PALETTE);
              uTextureID_Load1 = pIcons_LOD->LoadTexture("load1", TEXTURE_16BIT_PALETTE);
              uTextureID_Save1 = pIcons_LOD->LoadTexture("save1", TEXTURE_16BIT_PALETTE);
              uTextureID_Controls1 = pIcons_LOD->LoadTexture("controls1", TEXTURE_16BIT_PALETTE);
              uTextureID_Resume1 = pIcons_LOD->LoadTexture("resume1", TEXTURE_16BIT_PALETTE);
              uTextureID_Quit1 = pIcons_LOD->LoadTexture("quit1", TEXTURE_16BIT_PALETTE);
              pBtn_NewGame = pGUIWindow_CurrentMenu->CreateButton(0x13u, 0x9Bu, 0xD6u, 0x28u, 1, 0, UIMSG_StartNewGame, 0, 0x4Eu,
                             pGlobalTXT_LocalizationStrings[614],// "New Game"
                             pIcons_LOD->GetTexture(uTextureID_New1), 0);
              pBtn_SaveGame = pGUIWindow_CurrentMenu->CreateButton(0x13u, 0xD1u, 0xD6u, 0x28u, 1, 0, UIMSG_Game_OpenSaveGameDialog, 0, 0x53u,
                             pGlobalTXT_LocalizationStrings[615],// "Save Game"
                             pIcons_LOD->GetTexture(uTextureID_Save1), 0);
              pBtn_LoadGame = pGUIWindow_CurrentMenu->CreateButton(19, 263, 0xD6u, 0x28u, 1, 0, UIMSG_Game_OpenLoadGameDialog, 0, 0x4Cu,
                             pGlobalTXT_LocalizationStrings[616],// "Load Game"
                             pIcons_LOD->GetTexture(uTextureID_Load1), 0);
              pBtn_GameControls = pGUIWindow_CurrentMenu->CreateButton(241, 155, 214, 40, 1, 0, UIMSG_Game_OpenOptionsDialog, 0, 0x43u,
                             pGlobalTXT_LocalizationStrings[617],// ""Sound, Keyboard, Game Options:""
                             pIcons_LOD->GetTexture(uTextureID_Controls1), 0);
              pBtn_QuitGame = pGUIWindow_CurrentMenu->CreateButton(241, 209, 214, 40, 1, 0, UIMSG_Quit, 0, 0x51u,
                             pGlobalTXT_LocalizationStrings[618],// "Quit"
                             pIcons_LOD->GetTexture(uTextureID_Quit1), 0);
              pBtn_Resume = pGUIWindow_CurrentMenu->CreateButton(241, 263, 214, 40, 1, 0, UIMSG_GameMenu_ReturnToGame, 0, 0x52u,
                             pGlobalTXT_LocalizationStrings[619],// "Return to Game"
                             pIcons_LOD->GetTexture(uTextureID_Resume1), 0);
              pGUIWindow_CurrentMenu->_41D08F_set_keyboard_control_group(6, 1, 0, 0);
              viewparams->field_48 = 1;
              stru_506E40.Release();
              uNumSeconds = (unsigned int)MakeScreenshot(155, 117);
              pRenderer->SavePCXImage("gamma.pcx", (char *)uNumSeconds, 155, 117);
              free((void *)uNumSeconds);
              stru_506E40._40E55E("gamma.pcx", 0);
              continue;
            }
            pGUIWindow_Settings->Release();
            pGUIWindow_Settings = 0;
            pMouse->SetCursorBitmap("MICON1");
            GameUI_Footer_TimeLeft = 0;
            unk_50C9A0 = 0;
            back_to_game();
            continue;
          }
          sub_41426F();
          continue;
        case UIMSG_ScrollNPCPanel://Right and Left button for NPCPanel
          if ( uMessageParam )
          {
            GUIWindow::Create(626, 179, 0, 0, WINDOW_PressedButton2, (int)pBtn_NPCRight, 0);
            v37 = (pParty->pHirelings[0].pName != 0) + (pParty->pHirelings[1].pName != 0) + (unsigned __int8)pParty->field_70A - 2;
            if ( (unsigned __int8)pParty->field_709 < v37 )
            {
              ++pParty->field_709;//??? maybe number of the first cell???
              if ( (unsigned __int8)pParty->field_709 >= v37 )
                pParty->field_709 = (pParty->pHirelings[0].pName != 0) + (pParty->pHirelings[1].pName != 0) + pParty->field_70A - 2;
            }
          }
          else
          {
            GUIWindow::Create(469, 179, 0, 0, WINDOW_PressedButton2, (int)pBtn_NPCLeft, 0);
            if ( pParty->field_709 )
            {
              --pParty->field_709;
              if ( pParty->field_709 < 1 )
                pParty->field_709 = 0;
            }
          }
          GameUI_DrawHiredNPCs();
          continue;
        case UIMSG_TransitionUI_Confirm:
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          dword_50CDC8 = 1;
          sub_42FBDD();
          PlayHouseSound(uCurrentHouse_Animation, HouseSound_NotEnoughMoney_TrainingSuccessful);
          pVideoPlayer->Unload();
          DialogueEnding();
          viewparams->bRedrawGameUI = 1;
          if ( dword_59117C_teleportx | dword_591178_teleporty | dword_591174_teleportz | dword_591170_teleport_directiony | dword_59116C_teleport_directionx | dword_591168_teleport_speedz )
          {
            if ( dword_59117C_teleportx )
            {
              pParty->vPosition.x = dword_59117C_teleportx;
              _5B65A8_npcdata_uflags_or_other = dword_59117C_teleportx;
            }
            if ( dword_591178_teleporty )
            {
              pParty->vPosition.y = dword_591178_teleporty;
              _5B65AC_npcdata_fame_or_other = dword_591178_teleporty;
            }
            if ( dword_591174_teleportz )
            {
              pParty->vPosition.z = dword_591174_teleportz;
              _5B65B0_npcdata_rep_or_other = dword_591174_teleportz;
              pParty->uFallStartY = dword_591174_teleportz;
            }
            if ( dword_591170_teleport_directiony )
            {
              pParty->sRotationY = dword_591170_teleport_directiony;
              _5B65B4_npcdata_loword_house_or_other = dword_591170_teleport_directiony;
            }
            if ( dword_59116C_teleport_directionx )
            {
              pParty->sRotationX = dword_59116C_teleport_directionx;
              _5B65B8_npcdata_hiword_house_or_other = dword_59116C_teleport_directionx;
              v38 = dword_591168_teleport_speedz;
              pParty->uFallSpeed = dword_591168_teleport_speedz;
              dword_5B65BC = dword_591168_teleport_speedz;
            }
            else
            {
              v38 = dword_5B65BC;
            }
            if ( *dword_591164_teleport_map_name != 48 )
            {
              pGameLoadingUI_ProgressBar->uType = (GUIProgressBar::Type)2;
              dword_5B65C0 = _5B65A8_npcdata_uflags_or_other | _5B65AC_npcdata_fame_or_other | _5B65B0_npcdata_rep_or_other | _5B65B4_npcdata_loword_house_or_other | _5B65B8_npcdata_hiword_house_or_other | v38;
              OnMapLeave();
              sub_44987B(dword_591164_teleport_map_name, MapStartPoint_Party);
            }
          }
          else
          {
            EventProcessor(dword_5C3418, 0, 1, dword_5C341C);
          }
          if ( !_stricmp(byte_6BE3B0.data(), "d05.blv") )
            pParty->uTimePlayed += 1474560i64;
          continue;
        case UIMSG_TransitionWindowCloseBtn:
          CloseWindowBackground();
          pVideoPlayer->Unload();
          DialogueEnding();
          viewparams->bRedrawGameUI = true;
          continue;
        case UIMSG_CycleCharacters:
          v39 = GetAsyncKeyState(VK_SHIFT);
          uActiveCharacter = CycleCharacter(v39);
          viewparams->bRedrawGameUI = true;
          continue;
        case UIMSG_OnTravelByFoot:
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          dword_50CDC8 = 1;
          sub_42FBDD();
          pNPCData4 = (NPCData *)GetTravelTime();
          strcpy(pOutdoor->pLevelFilename, pCurrentMapName.data());
          if ( bUnderwater != 1 && pParty->bFlying
            || pOutdoor->GetTravelDestination(pParty->vPosition.x, pParty->vPosition.y, pOut, 20) != 1 )
          {
            viewparams->bRedrawGameUI = 1;
            CloseWindowBackground();
            if ( pParty->vPosition.x < -22528 )
              pParty->vPosition.x = -22528;
            if ( pParty->vPosition.x > 22528 )
              pParty->vPosition.x = 22528;
            if ( pParty->vPosition.y < -22528 )
              pParty->vPosition.y = -22528;
            if ( pParty->vPosition.y > 22528 )
              pParty->vPosition.y = 22528;
            DialogueEnding();
            pCurrentScreen = SCREEN_GAME;
          }
          else
          {
            pParty->field_6E4 = 0;
            pParty->field_6E0 = 0;
            pCastSpellInfo.data()->_427D48(1);
            DialogueEnding();
            pEventTimer->Pause();
            pGameLoadingUI_ProgressBar->Initialize(GUIProgressBar::TYPE_Box);
            ++pGameLoadingUI_ProgressBar->uProgressMax;
            SaveGame(1, 0);
            pGameLoadingUI_ProgressBar->Progress();
            RestAndHeal(1440 * (signed int)pNPCData4);
            if ( pParty->uNumFoodRations )
            {
              pParty->RestAndHeal();
              if ( ((pParty->uNumFoodRations - (signed int)pNPCData4) & 0x80000000u) != 0 )
              {
                pPlayer7 = pParty->pPlayers;
                do
                {
                  pPlayer7->SetCondition(1, 0);
                  ++pPlayer7;
                }
                while ( (signed int)pPlayer7 < (signed int)pParty->pHirelings );
                ++pParty->days_played_without_rest;
              }
              Party::TakeFood((unsigned int)pNPCData4);
            }
            else
            {
              pPlayer8 = pParty->pPlayers;
              do
              {
                pPlayer8->SetCondition(1, 0);
                ++pPlayer8;
              }
              while ( (signed int)pPlayer8 < (signed int)pParty->pHirelings );
              ++pParty->days_played_without_rest;
            }
            pPaletteManager->ResetNonLocked();
            pSpriteFrameTable->ResetSomeSpriteFlags();
            strcpy(pCurrentMapName.data(), pOut);
            strcpy(pLevelName, pCurrentMapName.data());
            v41 = strtok(pLevelName, ".");
            strcpy(pLevelName, v41);
            Level_LoadEvtAndStr(pLevelName);
            pDecalBuilder->Reset(0);
            LoadLevel_InitializeLevelEvt();
            uLevelMapStatsID = pMapStats->GetMapInfo(pCurrentMapName.data());
            bUnderwater = 0;
            bNoNPCHiring = 0;
            pGame->uFlags2 &= 0xFFFFFFF7u;
            if ( Is_out15odm_underwater() )
            {
              bUnderwater = 1;
              pGame->uFlags2 |= 8u;
            }
            if ( !_stricmp(pCurrentMapName.data(), "out15.odm") || !_stricmp(pCurrentMapName.data(), "d47.blv") )
              bNoNPCHiring = 1;
            PrepareToLoadODM(1u, (OutdoorCamera *)1);
            pAudioPlayer->SetMapEAX();
            bDialogueUI_InitializeActor_NPC_ID = 0;
            OnMapLoad();
            pOutdoor->SetFog();
            TeleportToStartingPoint(uLevel_StartingPointType);
            pParty->vPosition.z = GetTerrainHeightsAroundParty2(pParty->vPosition.x, pParty->vPosition.y, &v213, 0);
            pParty->uFallStartY = pParty->vPosition.z;
            sub_461103();
            pEventTimer->Resume();
            viewparams->bRedrawGameUI = 1;
            pCurrentScreen = SCREEN_GAME;
            pGameLoadingUI_ProgressBar->Release();
          }
          viewparams->bRedrawGameUI = 1;
          continue;
        case UIMSG_CHANGE_LOCATION_ClickCencelBtn:
          CloseWindowBackground();
          if ( pParty->vPosition.x < -22528 )
            pParty->vPosition.x = -22528;
          if ( pParty->vPosition.x > 22528 )
            pParty->vPosition.x = 22528;
          if ( pParty->vPosition.y < -22528 )
            pParty->vPosition.y = -22528;
          if ( pParty->vPosition.y > 22528 )
            pParty->vPosition.y = 22528;
          DialogueEnding();
          pCurrentScreen = SCREEN_GAME;
          viewparams->bRedrawGameUI = 1;
          continue;
        case UIMSG_CastSpell_BE://???
          __debugbreak();
          if ( pRenderer->pRenderD3D )
          {
            LOWORD(v42) = pGame->pVisInstance->get_picked_object_zbuf_val();
          }
          else
          {
            uNumSeconds = (unsigned int)pMouse->GetCursorPos(&v210);
            pPoint = pMouse->GetCursorPos(&v208);
            v42 = pRenderer->pActiveZBuffer[*(int *)uNumSeconds + pSRZBufferLineOffsets[pPoint->y]];
          }
          v44 = (unsigned __int16)v42;
          v45 = PID_TYPE(v44);
          uNumSeconds = v44;
          v46 = PID_ID(v44);
          if ( v45 == 3 )
          {
            v47 = pActors[v46].uAIState == Dead;
            if ( !v47 )
              continue;
            pSpellInfo = (CastSpellInfo *)pGUIWindow_Settings->ptr_1C;
            LOBYTE(pSpellInfo->field_8) &= 0xBFu;
            pSpellInfo->uPlayerID_2 = uMessageParam;
            pSpellInfo->spell_target_pid = v44;
            pParty->pPlayers[pSpellInfo->uPlayerID].SetRecoveryTime(300);
            pGUIWindow_Settings->Release();
            pGUIWindow_Settings = 0;
            pMouse->SetCursorBitmap("MICON1");
            GameUI_Footer_TimeLeft = 0;
            unk_50C9A0 = 0;
            back_to_game();
            continue;
          }
          if ( v45 == 2 )
          {
            v47 = (pObjectList->pObjects[pSpriteObjects[v46].uObjectDescID].uFlags & 0x10) == 0;
            if ( !v47 )
              continue;
            pSpellInfo = (CastSpellInfo *)pGUIWindow_Settings->ptr_1C;
            LOBYTE(pSpellInfo->field_8) &= 0xBFu;
            pSpellInfo->uPlayerID_2 = uMessageParam;
            pSpellInfo->spell_target_pid = v44;
            pParty->pPlayers[pSpellInfo->uPlayerID].SetRecoveryTime(300);
            pGUIWindow_Settings->Release();
            pGUIWindow_Settings = 0;
            pMouse->SetCursorBitmap("MICON1");
            GameUI_Footer_TimeLeft = 0;
            unk_50C9A0 = 0;
            back_to_game();
            continue;
          }
          if ( v45 == 5 )
          {
            v48 = pLevelDecorations[v46].field_16_event_id == 0;
          }
          else
          {
            if ( v45 != 6 )
              continue;
            if ( uCurrentlyLoadedLevelType != 1 )
            {
              pODMFace = &pOutdoor->pBModels[v44 >> 9].pFaces[v46 & 0x3F];
              if ( !(pODMFace->uAttributes & 0x02000000) || !pODMFace->sCogTriggeredID )
                continue;
              v44 = uNumSeconds;
              pSpellInfo = (CastSpellInfo *)pGUIWindow_Settings->ptr_1C;
              LOBYTE(pSpellInfo->field_8) &= 0xBFu;
              pSpellInfo->uPlayerID_2 = uMessageParam;
              pSpellInfo->spell_target_pid = v44;
              pParty->pPlayers[pSpellInfo->uPlayerID].SetRecoveryTime(300);
              pGUIWindow_Settings->Release();
              pGUIWindow_Settings = 0;
              pMouse->SetCursorBitmap("MICON1");
              GameUI_Footer_TimeLeft = 0;
              unk_50C9A0 = 0;
              back_to_game();
              continue;
            }
            pBLVFace = &pIndoor->pFaces[v46];
            if ( !(BYTE3(pBLVFace->uAttributes) & 2) )
              continue;
            v48 = pIndoor->pFaceExtras[pBLVFace->uFaceExtraID].uEventID == 0;
          }
          if ( v48 )
            continue;
          pSpellInfo = (CastSpellInfo *)pGUIWindow_Settings->ptr_1C;
          LOBYTE(pSpellInfo->field_8) &= 0xBFu;
          pSpellInfo->uPlayerID_2 = uMessageParam;
          pSpellInfo->spell_target_pid = v44;
          pParty->pPlayers[pSpellInfo->uPlayerID].SetRecoveryTime(300);
          pGUIWindow_Settings->Release();
          pGUIWindow_Settings = 0;
          pMouse->SetCursorBitmap("MICON1");
          GameUI_Footer_TimeLeft = 0;
          unk_50C9A0 = 0;
          back_to_game();
          continue;
        case UIMSG_CastSpell_Character_Big_Improvement://Preservation and blessing, treatment paralysis, hand hammers(individual upgrade)
        case UIMSG_CastSpell_Character_Small_Improvement://Fate, cure
        case UIMSG_HiredNPC_CastSpell:
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          if ( unk_50C9A0 )
          {
            uActiveCharacter = uMessageParam;
            viewparams->bRedrawGameUI = 1;
          }
          else
          {
            if ( pGUIWindow_Settings )
            {
              pSpellInfo = (CastSpellInfo *)pGUIWindow_Settings->ptr_1C;
              switch ( uMessage )
              {
                case UIMSG_CastSpell_Character_Big_Improvement:
                  LOBYTE(pSpellInfo->field_8) &= 0xFDu;
                  break;
                case UIMSG_CastSpell_Character_Small_Improvement:
                  HIBYTE(pSpellInfo->field_8) &= 0xFEu;
                  break;
                case UIMSG_HiredNPC_CastSpell:
                  HIBYTE(pSpellInfo->field_8) &= 0xFDu;
                  break;
              }
              pSpellInfo->uPlayerID_2 = uMessageParam;
              pParty->pPlayers[pSpellInfo->uPlayerID].SetRecoveryTime(300);
              pGUIWindow_Settings->Release();
              pGUIWindow_Settings = 0;
              pEventTimer->Resume();
              pMouse->SetCursorBitmap("MICON1");
              GameUI_Footer_TimeLeft = 0;
              unk_50C9A0 = 0;
            }
          }
          continue;
        case UIMSG_BF:
			__debugbreak();
          dword_50CDC8 = 1;
          sub_42FBDD();
          SaveGame(1, 0);
          strcpy(pCurrentMapName.data(), pMapStats->pInfos[uHouse_ExitPic].pFilename);
          dword_6BE364_game_settings_1 |= 1;
          uGameState = GAME_STATE_2;
          //v53 = p2DEvents_minus1_::30[26 * (unsigned int)ptr_507BC0->ptr_1C];
          v53 = p2DEvents[(unsigned int)window_SpeakInHouse->ptr_1C - 1]._quest_related;
          if ( v53 < 0 )
          {
            v54 = abs(v53) - 1;
            _5B65B8_npcdata_hiword_house_or_other = 0;
            dword_5B65BC = 0;
            v55 = dword_4E4560[v54];
            _5B65AC_npcdata_fame_or_other = dword_4E4578[v54];
            v56 = dword_4E4590[v54];
            v57 = dword_4E45A8[v54];
            _5B65A8_npcdata_uflags_or_other = v55;
            _5B65B4_npcdata_loword_house_or_other = v57;
            _5B65B0_npcdata_rep_or_other = v56;
            dword_5B65C0 = v55 | _5B65AC_npcdata_fame_or_other | v56 | v57;
          }
          sub_4BD8B5();
          //goto LABEL_434;
          /*if ( (signed int)pMessageQueue_50CBD0->uNumMessages < 40 )
          {
            pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].eType = UIMSG_Escape;
            pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].param = v0;
            *(&pMessageQueue_50CBD0->uNumMessages + 3 * pMessageQueue_50CBD0->uNumMessages + 3) = 0;
            ++pMessageQueue_50CBD0->uNumMessages;
          }*/
          pMessageQueue_50CBD0->AddMessage(UIMSG_Escape, 1, 0);
          continue;

        case UIMSG_OnCastTownPortal:
          pAudioPlayer->StopChannels(-1, -1);
          pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640, 480, WINDOW_Book, WINDOW_TownPortal, (char *)uMessageParam);
        continue;

        case UIMSG_OnCastLloydsBeacon:
          pAudioPlayer->StopChannels(-1, -1);
          pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640, 480, WINDOW_Book, WINDOW_LloydsBeacon, 0);
        continue;

        case UIMSG_LloydsBeacon_FlippingBtn:
          bRecallingBeacon = uMessageParam;
          v127 = uMessageParam + 204;
          pAudioPlayer->PlaySound((SoundID)v127, 0, 0, -1, 0, 0, 0, 0);
          continue;
        case UIMSG_HintBeaconSlot:
          if ( !pGUIWindow_CurrentMenu )
            continue;
          pPlayer = pPlayers[_506348_current_lloyd_playerid + 1];
          uNumSeconds = (unsigned int)&pPlayer->pInstalledBeacons[uMessageParam];
          if ( bRecallingBeacon )
          {
            if ( !*((int *)&pSavegameThumbnails.data()->pPixels + 10 * uMessageParam) )
              continue;
            v173 = pMapStats->pInfos[sub_410D99_get_map_index(HIWORD(pPlayer->pInstalledBeacons[uMessageParam].field_18))].pName;
            sprintfex(pTmpBuf.data(), pGlobalTXT_LocalizationStrings[474], v173);// "Recall to %s"
            GameUI_SetFooterString(pTmpBuf.data());
            continue;
          }
          v59 = pMapStats->GetMapInfo(pCurrentMapName.data());
          thise = "Not in Map Stats";
          if ( v59 )
            thise = pMapStats->pInfos[v59].pName;
          if ( !*((int *)&pSavegameThumbnails.data()->pPixels + 10 * uMessageParam) || !v59 )
          {
            sprintfex(pTmpBuf.data(), pGlobalTXT_LocalizationStrings[476], thise);// "Set to %s"
            GameUI_SetFooterString(pTmpBuf.data());
            continue;
          }
          v174 = pMapStats->pInfos[sub_410D99_get_map_index(*(short *)(uNumSeconds + 26))].pName;
          v158 = (unsigned int)thise;
          sprintf(pTmpBuf.data(), pGlobalTXT_LocalizationStrings[475], v158, v174);// "Set %s over %s"
          GameUI_SetFooterString(pTmpBuf.data());
          continue;
        case UIMSG_CloseAfterInstallBeacon:
          dword_50CDC8 = 1;
          pMessageQueue_50CBD0->AddMessage(UIMSG_Escape, 0, 0);
          /*if ( (signed int)pMessageQueue_50CBD0->uNumMessages >= 40 )
            continue;
          pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].eType = UIMSG_Escape;
          pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].param = 0;
          *(&pMessageQueue_50CBD0->uNumMessages + 3 * pMessageQueue_50CBD0->uNumMessages + 3) = 0;
          ++pMessageQueue_50CBD0->uNumMessages;*/
          continue;
        case UIMSG_InstallBeacon:
          pPlayer9 = pPlayers[_506348_current_lloyd_playerid + 1];
          pNPCData4 = (NPCData *)&pPlayer9->pInstalledBeacons[uMessageParam];
          if ( !*(_QWORD *)&pNPCData4->pName && bRecallingBeacon )
            continue;
          byte_506360 = 1;
          pPlayer9->CanCastSpell(uRequiredMana);
          if ( pParty->bTurnBasedModeOn )
          {
            v60 = sRecoveryTime;
            pParty->pTurnBasedPlayerRecoveryTimes[_506348_current_lloyd_playerid] = sRecoveryTime;
            pPlayer9->SetRecoveryTime(v60);
            pTurnEngine->_40471C();
          }
          else
          {
            pPlayer9->SetRecoveryTime((signed __int64)(flt_6BE3A4_debug_recmod1 * (double)sRecoveryTime * 2.133333333333333));
          }
          pAudioPlayer->PlaySound((SoundID)word_4EE088_sound_ids[dword_506338], 0, 0, -1, 0, dword_50633C, 0, 0);
          if ( bRecallingBeacon )
          {
            if ( _stricmp(pCurrentMapName.data(), (const char *)&pGames_LOD->pSubIndices[HIWORD(pNPCData4->uProfession)]) )
            {
              SaveGame(1, 0);
              OnMapLeave();
              strcpy(pCurrentMapName.data(), (const char *)&pGames_LOD->pSubIndices[HIWORD(pNPCData4->uProfession)]);
              dword_6BE364_game_settings_1 |= 1;
              uGameState = GAME_STATE_2;
              _5B65A8_npcdata_uflags_or_other = pNPCData4->uFlags;
              _5B65AC_npcdata_fame_or_other = pNPCData4->fame;
              _5B65B0_npcdata_rep_or_other = pNPCData4->rep;
              _5B65B4_npcdata_loword_house_or_other = LOWORD(pNPCData4->Location2D);
              _5B65B8_npcdata_hiword_house_or_other = HIWORD(pNPCData4->Location2D);
              dword_5B65C0 = 1;
            }
            else
            {
              pParty->vPosition.x = pNPCData4->uFlags;
              pParty->vPosition.y = pNPCData4->fame;
              pParty->vPosition.z = pNPCData4->rep;
              pParty->uFallStartY = pParty->vPosition.z;
              pParty->sRotationY = LOWORD(pNPCData4->Location2D);
              pParty->sRotationX = HIWORD(pNPCData4->Location2D);
            }
            pMessageQueue_50CBD0->AddMessage(UIMSG_Escape, 1, 0);
            /*if ( (signed int)pMessageQueue_50CBD0->uNumMessages < 40 )
            {
              pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].eType = UIMSG_Escape;
              pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].param = v0;
              *(&pMessageQueue_50CBD0->uNumMessages + 3 * pMessageQueue_50CBD0->uNumMessages + 3) = 0;
              ++pMessageQueue_50CBD0->uNumMessages;
            }*/
            //crt_deconstruct_ptr_6A0118();
            pBooksWindow->Release();
            pGUIWindow_CurrentMenu->Release();
            pBooksWindow = 0;
            pGUIWindow_CurrentMenu = 0;
          }
          else
          {
            sprintf(a1, "data\\lloyd%d%d.pcx", _506348_current_lloyd_playerid + 1, uMessageParam + 1);
            SaveScreenshot(a1);
            LoadThumbnailLloydTexture(uMessageParam, _506348_current_lloyd_playerid + 1);
            v212 = qword_506350 << 7;
            pNPCData2 = pNPCData4;
            thisg = 0;
            v62 = pParty->uTimePlayed + (signed __int64)((double)(qword_506350 << 7) * 0.033333335);
            pNPCData4->pName = (char *)v62;
            pNPCData2->uPortraitID = HIDWORD(v62);
            pNPCData2->uFlags = pParty->vPosition.x;
            pNPCData2->fame = pParty->vPosition.y;
            pNPCData2->rep = pParty->vPosition.z;
            LOWORD(pNPCData2->Location2D) = LOWORD(pParty->sRotationY);
            HIWORD(pNPCData2->Location2D) = LOWORD(pParty->sRotationX);
            if ( (signed int)pGames_LOD->uNumSubDirs / 2 <= 0 )
              continue;
            uAction = 0;
            while ( _stricmp((const char *)pGames_LOD->pSubIndices + uAction, pCurrentMapName.data()) )
            {
              ++thisg;
              uAction += 32;
              if ( thisg >= (signed int)pGames_LOD->uNumSubDirs / 2 )
                continue;
            }
            HIWORD(pNPCData4->uProfession) = thisg;
          }
          continue;
        case UIMSG_ClickTownInTP:
          if ( uMessageParam )
          {
            switch ( uMessageParam )
            {
              case 1:
                v63 = 208;
                break;
              case 2:
                v63 = 207;
                break;
              case 3:
                v63 = 211;
                break;
              case 4:
                v63 = 209;
                break;
              default:
                if ( uMessageParam != 5 )
                {
LABEL_486:
                  SaveGame(1, 0);
                  v64 = pMapStats->GetMapInfo(pCurrentMapName.data());
                  v65 = uMessageParam;
                  if ( v64 == TownPortalList[uMessageParam].uMapInfoID )
                  {
                    pParty->vPosition.x = TownPortalList[v65].pos.x;
                    pParty->vPosition.y = TownPortalList[v65].pos.y;
                    pParty->vPosition.z = TownPortalList[v65].pos.z;
                    pParty->uFallStartY = pParty->vPosition.z;
                    pParty->sRotationY = TownPortalList[v65].rot_y;
                    pParty->sRotationX = TownPortalList[v65].rot_x;
                  }
                  else
                  {
                    SaveGame(1, 0);
                    OnMapLeave();
                    dword_6BE364_game_settings_1 |= 1;
                    uGameState = GAME_STATE_2;
                    strcpy(pCurrentMapName.data(), pMapStats->pInfos[TownPortalList[uMessageParam].uMapInfoID].pFilename);
                    dword_5B65C0 = 1;
                    _5B65A8_npcdata_uflags_or_other = TownPortalList[uMessageParam].pos.x;
                    _5B65AC_npcdata_fame_or_other = TownPortalList[uMessageParam].pos.y;
                    _5B65B0_npcdata_rep_or_other = TownPortalList[uMessageParam].pos.z;
                    v66 = TownPortalList[uMessageParam].rot_x;
                    _5B65B4_npcdata_loword_house_or_other = TownPortalList[uMessageParam].rot_y;
                    _5B65B8_npcdata_hiword_house_or_other = v66;
                    InitializeActors();
                  }
                  v67 = (char*)pGUIWindow_CurrentMenu->Hint;
                  if ( v67 )
                    *((int *)v67 + 17) = 1;
                  else
                    pParty->pPlayers[(unsigned __int8)town_portal_caster_id].CanCastSpell(0x14u);
                  /*if ( (signed int)pMessageQueue_50CBD0->uNumMessages < 40 )
                  {
                    pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].eType = UIMSG_Escape;
                    pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].param = v0;
                    *(&pMessageQueue_50CBD0->uNumMessages + 3 * pMessageQueue_50CBD0->uNumMessages + 3) = 0;
                    ++pMessageQueue_50CBD0->uNumMessages;
                  }*/
                  pMessageQueue_50CBD0->AddMessage(UIMSG_Escape, 1, 0);
                  continue;
                }
                v63 = 210;
                break;
            }
          }
          else
          {
            v63 = 206;
          }
          if ( !(unsigned __int16)_449B57_test_bit(pParty->_quest_bits, v63) )
            return;
          goto LABEL_486;
        case UIMSG_HintTownPortal:
          if ( uMessageParam )
          {
            switch ( uMessageParam )
            {
              case 1:
                v68 = 208;
                break;
              case 2:
                v68 = 207;
                break;
              case 3:
                v68 = 211;
                break;
              case 4:
                v68 = 209;
                break;
              default:
                if ( uMessageParam != 5 )
                  //goto LABEL_506;
                {
                  if ( uMessageParam )
                  {
                    switch ( uMessageParam )
                    {
                    case 1:
                      v69 = pMapStats->pInfos[4].pName;
                      break;
                    case 2:
                      v69 = pMapStats->pInfos[3].pName;
                      break;
                    case 3:
                      v69 = pMapStats->pInfos[10].pName;
                      break;
                    case 4:
                      v69 = pMapStats->pInfos[7].pName;
                      break;
                    default:
                    if ( uMessageParam != 5 )
                    {
                      sprintfex(pTmpBuf.data(), pGlobalTXT_LocalizationStrings[35], v200);
                      GameUI_SetFooterString(pTmpBuf.data());
                      continue;
                    }
                    v69 = pMapStats->pInfos[8].pName;
                    break;
                  }
                }
                else
                {
                  v69 = pMapStats->pInfos[21].pName;
                }
                sprintfex(pTmpBuf.data(), pGlobalTXT_LocalizationStrings[35], v69);
                GameUI_SetFooterString(pTmpBuf.data());
                continue;
              }
              v68 = 210;
              break;
            }
          }
          else
          {
            v68 = 206;
          }
          if ( !(unsigned __int16)_449B57_test_bit(pParty->_quest_bits, v68) )
          {
            pRenderer->DrawTextureRGB(0, 0x160u, pTexture_StatusBar);
            continue;
          }
//LABEL_506:
          if ( uMessageParam )
          {
            switch ( uMessageParam )
            {
              case 1:
                v69 = pMapStats->pInfos[4].pName;
                break;
              case 2:
                v69 = pMapStats->pInfos[3].pName;
                break;
              case 3:
                v69 = pMapStats->pInfos[10].pName;
                break;
              case 4:
                v69 = pMapStats->pInfos[7].pName;
                break;
              default:
                if ( uMessageParam != 5 )
                  //goto LABEL_519;
                  {
                    sprintfex(pTmpBuf.data(), pGlobalTXT_LocalizationStrings[35], v200);
                    GameUI_SetFooterString(pTmpBuf.data());
                    continue;
                  }
                v69 = pMapStats->pInfos[8].pName;
                break;
            }
          }
          else
          {
            v69 = pMapStats->pInfos[21].pName;
          }
          sprintfex(pTmpBuf.data(), pGlobalTXT_LocalizationStrings[35], v69);
          GameUI_SetFooterString(pTmpBuf.data());
          continue;
        case UIMSG_ShowFinalWindow:
          sprintf(pFinalMessage.data(), "%s\n \n%s\n \n%s", pGlobalTXT_LocalizationStrings[151],// "Congratulations Adventurer."
            pGlobalTXT_LocalizationStrings[118],// "We hope that you've enjoyed playing Might and Magic VII as much as we did making it. We have saved this screen as MM7_WIN.PCX in your MM7 directory. You can print it out as proof of your accomplishment."
            pGlobalTXT_LocalizationStrings[167]);// "- The Might and Magic VII Development Team."   
          ModalWindow(pFinalMessage.data(), 196);
          uGameState = GAME_STATE_FINAL_WINDOW;
          continue;
        case UIMSG_C4:
			__debugbreak();
          uGameState = GAME_STATE_PLAYING;
          strcpy((char *)pKeyActionMap->pPressedKeysBuffer, "2");
        case UIMSG_DD:
			__debugbreak();
          sprintf(pTmpBuf.data(), "%s", pKeyActionMap->pPressedKeysBuffer);
          memcpy(&v216, txt_file_frametable_parser((const char *)pKeyActionMap->pPressedKeysBuffer, &v218), sizeof(v216));
          if ( v216.uPropCount == 1 )
          {
            pNPCData4 = (NPCData *)((signed int)pGames_LOD->uNumSubDirs / 2);
            v70 = atoi(v216.pProperties[0]);
            if ( v70 <= 0 || v70 >= 77 )
              continue;
            v71 = v70;
            strcpy(Str2, pMapStats->pInfos[v70].pFilename);
            pNPCData3 = 0;
            if ( (signed int)pNPCData4 > 0 )
            {
              thish = 0;
              do
              {
                if ( !_stricmp((const char *)&pGames_LOD->pSubIndices[thish], Str2) )
                  break;
                ++thish;
                pNPCData3 = (NPCData *)((char *)pNPCData3 + 1);
              }
              while ( (signed int)pNPCData3 < (signed int)pNPCData4 );
              if ( (signed int)pNPCData3 < (signed int)pNPCData4 )
              {
                strcpy(pCurrentMapName.data(), (const char *)&pGames_LOD->pSubIndices[(int)pNPCData3]);
                dword_6BE364_game_settings_1 |= 1u;
                uGameState = GAME_STATE_2;
                OnMapLeave();
                continue;
              }
            }
            sprintf(Str2, "No map found for %s", pMapStats->pInfos[v71].pName);
            v73 = Str2;
          }
          else
          {
            if ( v216.uPropCount != 3 )
              continue;
            v74 = atoi(v216.pProperties[0]);
            thisi = atoi(v216.pProperties[1]);
            v75 = atoi(v216.pProperties[2]);
            v76 = v75;
            if ( uCurrentlyLoadedLevelType == LEVEL_Indoor)
            {
              if ( pIndoor->GetSector(v74, thisi, v75) )
              {
                v77 = thisi;
                pParty->vPosition.x = v74;
                pParty->vPosition.y = v77;
                pParty->vPosition.z = v76;
                pParty->uFallStartY = v76;
                continue;
              }
            }
            else
            {
              if ( v74 > -32768 )
              {
                if ( v74 < 32768 )
                {
                  v77 = thisi;
                  if ( thisi > -32768 )
                  {
                    if ( thisi < 32768 && v76 >= 0 && v76 < 10000 )
                    {
                      pParty->vPosition.x = v74;
                      pParty->vPosition.y = v77;
                      pParty->vPosition.z = v76;
                      pParty->uFallStartY = v76;
                      continue;
                    }
                  }
                }
              }
            }
            pAudioPlayer->PlaySound(SOUND_27, 0, 0, -1, 0, 0, 0, 0);
            v73 = "Can't jump to that location!";
          }
          ShowStatusBarString(v73, 6u);
          continue;
        case UIMSG_CastQuickSpell:
          if ( bUnderwater == 1 )
          {
            ShowStatusBarString(pGlobalTXT_LocalizationStrings[652], 2);// "You can not do that while you are underwater!"
            pAudioPlayer->PlaySound((SoundID)27, 0, 0, -1, 0, 0, 0, 0);
            continue;
          }
          if ( !uActiveCharacter || (pPlayer2 = pPlayers[uActiveCharacter], pPlayer2->uTimeToRecovery) )
            continue;
          _42777D_CastSpell_UseWand_ShootArrow(pPlayer2->uQuickSpell, uActiveCharacter - 1, 0, 0, uActiveCharacter);
          continue;
        case UIMSG_CastSpell_Monster_Improvement:
        case UIMSG_CastSpell_Shoot_Monster://FireBlow, Lightning, Ice Lightning, Swarm, 
          if ( pRenderer->pRenderD3D )
          {
            v81 = pGame->pVisInstance->get_picked_object_zbuf_val();
          }
          else
          {
            uNumSeconds = (unsigned int)pMouse->GetCursorPos(&v206);
            pPoint2 = pMouse->GetCursorPos(&v201);
            v81 = pRenderer->pActiveZBuffer[*(int *)uNumSeconds + pSRZBufferLineOffsets[pPoint2->y]];
          }
          v83 = v81;
          v44 = (unsigned __int16)v81;
          v84 = v83 >> 16;
          if ( PID_TYPE(v44) != 3 || v84 >= 5120 )
            continue;
          pSpellInfo = (CastSpellInfo *)pGUIWindow_Settings->ptr_1C;
          if ( uMessage == UIMSG_CastSpell_Shoot_Monster )
          {
            LOBYTE(pSpellInfo->field_8) &= 0xF7;
          }
          else
          {
            if ( uMessage == UIMSG_CastSpell_Monster_Improvement )
              HIBYTE(pSpellInfo->field_8) &= 0xFEu;
            else
              HIBYTE(pSpellInfo->field_8) &= 0xFDu;
          }
          pSpellInfo->uPlayerID_2 = uMessageParam;
          pSpellInfo->spell_target_pid = v44;
          pParty->pPlayers[pSpellInfo->uPlayerID].SetRecoveryTime(300);
          pGUIWindow_Settings->Release();
          pGUIWindow_Settings = 0;
          pMouse->SetCursorBitmap("MICON1");
          GameUI_Footer_TimeLeft = 0;
          unk_50C9A0 = 0;
          back_to_game();
          continue;
        case UIMSG_1C:
			__debugbreak();
          if ( !uActiveCharacter || pCurrentScreen )
            continue;
          ptr_507BC8 = GUIWindow::Create(0, 0, 640, 480, WINDOW_68, uMessageParam, 0);
          pCurrentScreen = SCREEN_19;
          pEventTimer->Pause();
          continue;
        case UIMSG_1B:
			__debugbreak();
          if ( !uActiveCharacter )
            continue;
          if ( pParty->bTurnBasedModeOn != 1 )
          {
            if ( pActors[uMessageParam].uAIState == 5 )
              stru_50C198.LootActor(&pActors[uMessageParam]);
            else
              Actor::StealFrom(uMessageParam);
            continue;
          }
          if ( pTurnEngine->field_4 == 1 || pTurnEngine->field_4 == 3 )
            continue;
          if ( !(pTurnEngine->field_18 & 2) )
          {
            if ( pActors[uMessageParam].uAIState == 5 )
              stru_50C198.LootActor(&pActors[uMessageParam]);
            else
              Actor::StealFrom(uMessageParam);
          }
          continue;

        case UIMSG_Attack:
          if ( !uActiveCharacter )
            continue;
          if ( pParty->bTurnBasedModeOn != 1 )
          {
            _42ECB5_PlayerAttacksActor();
            continue;
          }
          if ( pTurnEngine->field_4 == 1 || pTurnEngine->field_4 == 3 )
            continue;
          if ( !(pTurnEngine->field_18 & 2) )
            _42ECB5_PlayerAttacksActor();
          continue;
        case UIMSG_ExitRest:
          GUIWindow::Create(pButton_RestUI_Exit->uX, pButton_RestUI_Exit->uY, 0, 0, WINDOW_CloseRestWindowBtn, (int)pButton_RestUI_Exit, pGlobalTXT_LocalizationStrings[81]);// "Exit Rest"
          continue;
        case UIMSG_Wait5Minutes:
          if ( dword_506F14 == 2 )
          {
            ShowStatusBarString(pGlobalTXT_LocalizationStrings[477], 2);// "You are already resting!"
            pAudioPlayer->PlaySound((SoundID)27, 0, 0, -1, 0, 0, 0, 0);
            continue;
          }
          GUIWindow::Create(pButton_RestUI_Wait5Minutes->uX, pButton_RestUI_Wait5Minutes->uY, 0, 0, WINDOW_PressedButton2,
            (int)pButton_RestUI_Wait5Minutes, pGlobalTXT_LocalizationStrings[238]);// "Wait 5 Minutes"
          dword_506F14 = 1;
          _506F18_num_hours_to_sleep = 5;
          continue;
        case UIMSG_Wait1Hour:
          if ( dword_506F14 == 2 )
          {
            ShowStatusBarString(pGlobalTXT_LocalizationStrings[477], 2);// "You are already resting!"
            pAudioPlayer->PlaySound((SoundID)27, 0, 0, -1, 0, 0, 0, 0);
            continue;
          }
          GUIWindow::Create(pButton_RestUI_Wait1Hour->uX, pButton_RestUI_Wait1Hour->uY, 0, 0, WINDOW_PressedButton2,
            (int)pButton_RestUI_Wait1Hour, pGlobalTXT_LocalizationStrings[239]);// "Wait 1 Hour"
          dword_506F14 = 1;
          _506F18_num_hours_to_sleep = 60;
          continue;
        case UIMSG_RentRoom:
          dword_506F14 = 2;
          RestUI_Load();
          v86 = 60 * (_494820_training_time(pParty->uCurrentHour) + 1) - pParty->uCurrentMinute;
          _506F18_num_hours_to_sleep = v86;
          if ( uMessageParam == 111 || uMessageParam == 114 || uMessageParam == 116 )
            _506F18_num_hours_to_sleep = v86 + 720;
          dword_506F14 = 2;
          pParty->RestAndHeal();
          pParty->days_played_without_rest = 0;
          pParty->pPlayers[3].pConditions[Player::Condition_Sleep] = pParty->uTimePlayed;
          pParty->pPlayers[2].pConditions[Player::Condition_Sleep] = pParty->uTimePlayed;
          pParty->pPlayers[1].pConditions[Player::Condition_Sleep] = pParty->uTimePlayed;
          pParty->pPlayers[0].pConditions[Player::Condition_Sleep] = pParty->uTimePlayed;
          continue;
        case UIMSG_RestWindow:
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          if ( pCurrentScreen )
            continue;
          if ( sub_42F4DA() )
          {
            if ( pParty->bTurnBasedModeOn == 1 )
            {
              ShowStatusBarString(pGlobalTXT_LocalizationStrings[478], 2);// "You can't rest in turn-based mode!"
              continue;
            }
            v88 = pGlobalTXT_LocalizationStrings[480];// "There are hostile enemies near!"
            if ( pParty->uFlags & 0x88 )
              v88 = pGlobalTXT_LocalizationStrings[479];// "You can't rest here!"
            ShowStatusBarString(v88, 2);
            if ( !uActiveCharacter )
              continue;
            pPlayers[uActiveCharacter]->PlaySound((PlayerSpeech)13, 0);
            continue;
          }
          if ( pParty->bTurnBasedModeOn == 1 )
          {
            ShowStatusBarString(pGlobalTXT_LocalizationStrings[478], 2);// "You can't rest in turn-based mode!"
            continue;
          }
          if ( !(pParty->uFlags & 0x88) )
          {
            RestUI_Load();
            continue;
          }
          if ( pParty->bTurnBasedModeOn == 1 )
          {
            ShowStatusBarString(pGlobalTXT_LocalizationStrings[478], 2);// "You can't rest in turn-based mode!"
            continue;
          }
          v88 = pGlobalTXT_LocalizationStrings[480];// "There are hostile enemies near!"
          if ( pParty->uFlags & 0x88 )
            v88 = pGlobalTXT_LocalizationStrings[479];// "You can't rest here!"
          ShowStatusBarString(v88, 2u);
          if ( !uActiveCharacter )
            continue;
          pPlayers[uActiveCharacter]->PlaySound((PlayerSpeech)13, 0);
          continue;
        case UIMSG_Rest8Hour:
          if ( dword_506F14 )
          {
            ShowStatusBarString(pGlobalTXT_LocalizationStrings[477], 2);// "You are already resting!"
            pAudioPlayer->PlaySound((SoundID)27, 0, 0, -1, 0, 0, 0, 0);
            continue;
          }
          if ( pParty->uNumFoodRations < uRestUI_FoodRequiredToRest )
          {
            ShowStatusBarString(pGlobalTXT_LocalizationStrings[482], 2u);// "You don't have enough food to rest"
            if ( uActiveCharacter && pPlayers[uActiveCharacter]->CanAct() )
              pPlayers[uActiveCharacter]->PlaySound((PlayerSpeech)SPEECH_108, 0);
          }
          else
          {
            pParty->pPlayers[3].pConditions[2] = pParty->uTimePlayed;
            pParty->pPlayers[2].pConditions[2] = pParty->uTimePlayed;
            pParty->pPlayers[1].pConditions[2] = pParty->uTimePlayed;
            pParty->pPlayers[0].pConditions[2] = pParty->uTimePlayed;
            v90 = pMapStats->GetMapInfo(pCurrentMapName.data());
            if ( !v90 )
              v90 = rand() % (signed int)pMapStats->uNumMaps + 1;
            pMapInfo = &pMapStats->pInfos[v90];
            if ( rand() % 100 + 1 <= pMapInfo->Encounter_percent )
            {
              v91 = rand() % 100;
              v92 = pMapInfo->EncM1percent;
              v93 = v91 + 1;
              if ( v93 > v92 )
                pNPCData4 = (NPCData *)((v93 > v92 + pMapInfo->EncM2percent) + 2);
              else
                pNPCData4 = (NPCData *)1;
              if ( !sub_45063B(pMapInfo, (int)pNPCData4) )
                pNPCData4 = 0;
              if ( pNPCData4 )
              {
                pPlayerNum = rand() % 4;
                LODWORD(pParty->pPlayers[pPlayerNum].pConditions[2]) = 0;
                HIDWORD(pParty->pPlayers[pPlayerNum].pConditions[2]) = 0;
                v95 = rand();
                Rest(v95 % 6 + 60);
                _506F18_num_hours_to_sleep = 0;
                dword_506F14 = 0;
                /*if ( (signed int)pMessageQueue_50CBD0->uNumMessages < 40 )
                {
                  pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].eType = UIMSG_Escape;
                  pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].param = 0;
                  *(&pMessageQueue_50CBD0->uNumMessages + 3 * pMessageQueue_50CBD0->uNumMessages + 3) = 0;
                  ++pMessageQueue_50CBD0->uNumMessages;
                }*/
                pMessageQueue_50CBD0->AddMessage(UIMSG_Escape, 0, 0);
                ShowStatusBarString(pGlobalTXT_LocalizationStrings[481], 2);// "Encounter!"
                pAudioPlayer->PlaySound((SoundID)227, 0, 0, -1, 0, 0, 0, 0);
                continue;
              }
            }
            Party::TakeFood(uRestUI_FoodRequiredToRest);
            _506F18_num_hours_to_sleep = 480;
            dword_506F14 = 2;
            pParty->RestAndHeal();
            pParty->days_played_without_rest = 0;
            pParty->pPlayers[3].pConditions[Player::Condition_Sleep] = pParty->uTimePlayed;
            pParty->pPlayers[2].pConditions[Player::Condition_Sleep] = pParty->uTimePlayed;
            pParty->pPlayers[1].pConditions[Player::Condition_Sleep] = pParty->uTimePlayed;
            pParty->pPlayers[0].pConditions[Player::Condition_Sleep] = pParty->uTimePlayed;
          }
          continue;
        case UIMSG_AlreadyResting:
          if ( dword_506F14 == 2 )
          {
            ShowStatusBarString(pGlobalTXT_LocalizationStrings[477], 2);// "You are already resting!"
            pAudioPlayer->PlaySound((SoundID)27, 0, 0, -1, 0, 0, 0, 0);
            continue;
          }
          GUIWindow::Create(pButton_RestUI_WaitUntilDawn->uX, pButton_RestUI_WaitUntilDawn->uY, 0, 0, WINDOW_PressedButton2,
            (int)pButton_RestUI_WaitUntilDawn, pGlobalTXT_LocalizationStrings[237]);// "Wait until Dawn"
          v97 = _494820_training_time(pParty->uCurrentHour);
          dword_506F14 = 1;
          _506F18_num_hours_to_sleep = 60 * v97 - pParty->uCurrentMinute;
          continue;
        case UIMSG_HintSelectRemoveQuickSpellBtn:
          if ( quick_spell_at_page && byte_506550 )
          {
            v173 = pSpellStats->pInfos[quick_spell_at_page + 11 * pPlayers[uActiveCharacter]->lastOpenedSpellbookPage].pName;
            sprintfex(pTmpBuf.data(), pGlobalTXT_LocalizationStrings[483], v173);
          }
          else
          {
            if ( pPlayers[uActiveCharacter]->uQuickSpell )
              v177 = pGlobalTXT_LocalizationStrings[584];// "Click here to remove your Quick Spell"
            else
              v177 = pGlobalTXT_LocalizationStrings[484];// "Select a spell then click here to set a QuickSpell"
            strcpy(pTmpBuf.data(), v177);
          }
          GameUI_SetFooterString(pTmpBuf.data());
          continue;
        case UIMSG_SPellbook_ShowHightlightedSpellInfo:
          if ( !uActiveCharacter || (uNumSeconds = (unsigned int)pPlayers[uActiveCharacter],
                !*(char *)(uNumSeconds + 11 * *(char *)(uNumSeconds + 6734) + uMessageParam + 402)) )
            continue;
          if ( sub_4637E0_is_there_popup_onscreen() )
            dword_507B00_spell_info_to_draw_in_popup = uMessageParam + 1;
          v98 = *(char *)(uNumSeconds + 6734);
          if ( quick_spell_at_page - 1 == uMessageParam )
          {
            v178 = pSpellStats->pInfos[uMessageParam + 11 * v98 + 1].pName;
            v161 = pGlobalTXT_LocalizationStrings[485];
          }
          else
          {
            v178 = pSpellStats->pInfos[uMessageParam + 11 * v98 + 1].pName;
            v161 = pGlobalTXT_LocalizationStrings[486];
          }
          sprintfex(pTmpBuf.data(), v161, v178);
          GameUI_SetFooterString(pTmpBuf.data());
          continue;
        case UIMSG_ClickInstallRemoveQuickSpellBtn:
          GUIWindow::Create(pBtn_InstallRemoveSpell->uX, pBtn_InstallRemoveSpell->uY, 0, 0, WINDOW_PressedButton2, (int)pBtn_InstallRemoveSpell, 0);
          if ( !uActiveCharacter )
            continue;
          pPlayer10 = pPlayers[uActiveCharacter];
          if ( !byte_506550 || !quick_spell_at_page )
          {
            pPlayer10->uQuickSpell = 0;
            quick_spell_at_page = 0;
            pAudioPlayer->PlaySound((SoundID)203, 0, 0, -1, 0, 0, 0, 0);
            continue;
          }
          v99 = quick_spell_at_page + 11 * pPlayers[uActiveCharacter]->lastOpenedSpellbookPage;
          pPlayers[uActiveCharacter]->uQuickSpell = v99;
          stru_A750F8[uActiveCharacter + 3]._494836(v99, uActiveCharacter);
          if ( uActiveCharacter )
            pPlayer10->PlaySound(SPEECH_12, 0);
          byte_506550 = 0;
          continue;
        case UIMSG_SpellBook_PressTab:
			//__debugbreak();
          if ( !uActiveCharacter )
            continue;
          pPlayer3 = pPlayers[uActiveCharacter];
          pNPCData4 = 0;
          uAction = 0;
          v101 = 0;
          thisl = (int)&pPlayer3->pActiveSkills[12];
          do
          {
            if ( *(short *)thisl )
            {
              if ( pPlayer3->lastOpenedSpellbookPage == v101 )
                uAction = (int)pNPCData4;
              v102 = (int)pNPCData4;
              pNPCData4 = (NPCData *)((char *)pNPCData4 + 1);
              v217[v102] = v101;
            }
            thisl += 2;
            ++v101;
          }
          while ( v101 < 9 );
          if ( !pNPCData4 )
          {
            v127 = rand() % 2 + 204;
            pAudioPlayer->PlaySound((SoundID)v127, 0, 0, -1, 0, 0, 0, 0);
            continue;
          }
          if ( GetAsyncKeyState(16) )
          {
            --uAction;
            if ( uAction < 0 )
              uAction = (int)((char *)pNPCData4 - 1);
          }
          else
          {
            ++uAction;
            if ( uAction >= (signed int)pNPCData4 )
              uAction = 0;
          }
          sub_41140B();
          pPlayers[uActiveCharacter]->lastOpenedSpellbookPage = LOBYTE(v217[uAction]);
          pGUIWindow_CurrentMenu->OpenSpellBook();
          v127 = rand() % 2 + 204;
          pAudioPlayer->PlaySound((SoundID)v127, 0, 0, -1, 0, 0, 0, 0);
          continue;
        case UIMSG_OpenSpellbookPage:
          if ( pTurnEngine->field_4 == 3 || !uActiveCharacter || uMessageParam == pPlayers[uActiveCharacter]->lastOpenedSpellbookPage )
            continue;
          sub_41140B();
          pPlayers[uActiveCharacter]->lastOpenedSpellbookPage = uMessageParam;
          pGUIWindow_CurrentMenu->OpenSpellBook();
          v127 = rand() % 2 + 204;
          pAudioPlayer->PlaySound((SoundID)v127, 0, 0, -1, 0, 0, 0, 0);
          continue;
        case UIMSG_SelectSpell:
        {
          if (pTurnEngine->field_4 == 3)
            continue;
          if (!uActiveCharacter)
            continue;

            //  uNumSeconds = (unsigned int)pPlayers[uActiveCharacter];
          auto player = pPlayers[uActiveCharacter];
          if (player->spellbook.pChapters[player->lastOpenedSpellbookPage].bIsSpellAvailable[uMessageParam])
              //if ( *(char *)(uNumSeconds + 11 * *(char *)(uNumSeconds + &lastOpenedSpellbookPage) + uMessageParam + 402) )
          {
                if ( quick_spell_at_page - 1 == uMessageParam )
                {
                  pGUIWindow_CurrentMenu->Release();
                  pEventTimer->Resume();
                  viewparams->bRedrawGameUI = 1;
                  pCurrentScreen = SCREEN_GAME;
                  pIcons_LOD->_4114F2();
                  v103 = quick_spell_at_page + 11 * player->lastOpenedSpellbookPage;
                  /*if ( dword_50C9E8 < 40 )
                  {
                    dword_50C9EC[3 * dword_50C9E8] = UIMSG_CastSpellFromBook;
                    dword_50C9EC[3 * dword_50C9E8 + 1] = v103;
                    dword_50C9EC[3 * dword_50C9E8 + 2] = uActiveCharacter - 1;
                    ++dword_50C9E8;
                  }*/
                  pMessageQueue_50C9E8->AddMessage(UIMSG_CastSpellFromBook, v103, uActiveCharacter - 1);
                //  pMessageQueue_50CBD0->AddMessage(UIMSG_CastSpellFromBook, v103, uActiveCharacter - 1);
                }
                else
                {
                  byte_506550 = 1;
                  quick_spell_at_page = uMessageParam + 1;
                }
          }
        }
        continue;

        case UIMSG_CastSpellFromBook:
          if ( pTurnEngine->field_4 != 3 )
            _42777D_CastSpell_UseWand_ShootArrow(uMessageParam, v199, 0, 0, 0);
        continue;

        case UIMSG_SpellScrollUse:
			__debugbreak();
          if ( pTurnEngine->field_4 != 3 )
            _42777D_CastSpell_UseWand_ShootArrow(uMessageParam, v199, 133, 1, 0);
          continue;
        case UIMSG_SpellBookWindow:
          if ( pTurnEngine->field_4 == 3 )
            continue;
          if ( bUnderwater == 1 )
          {
            ShowStatusBarString(pGlobalTXT_LocalizationStrings[652], 2);// "You can not do that while you are underwater!"
            pAudioPlayer->PlaySound((SoundID)27, 0, 0, -1, 0, 0, 0, 0);
          }
          else
          {
            if ( pMessageQueue_50CBD0->uNumMessages )
              pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
            if ( uActiveCharacter && !pPlayers[uActiveCharacter]->uTimeToRecovery )
            {
              if ( !pCurrentScreen )
              {
                GUIWindow::Create(0x1DCu, 0x1C2u, 0, 0, WINDOW_PressedButton2, (int)pBtn_CastSpell, 0);
                pCurrentScreen = SCREEN_SPELL_BOOK;
                pEventTimer->Pause();
                pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640, 480, WINDOW_SpellBook, 0, 0);
                pAudioPlayer->PlaySound((SoundID)48, 0, 0, -1, 0, 0, 0, 0);
                viewparams->field_48 = 1;
                continue;
              }
              if ( pCurrentScreen != SCREEN_REST
                && pCurrentScreen != SCREEN_CHARACTERS
                && (pCurrentScreen <= SCREEN_63
                 || pCurrentScreen > SCREEN_67) )
              {
                pGUIWindow_CurrentMenu->Release();
                GUIWindow::Create(0x1DCu, 0x1C2u, 0, 0, WINDOW_PressedButton2, (int)pBtn_CastSpell, 0);
                pCurrentScreen = SCREEN_SPELL_BOOK;
                pEventTimer->Pause();
                pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640, 480, WINDOW_SpellBook, 0, 0);
                pAudioPlayer->PlaySound((SoundID)48, 0, 0, -1, 0, 0, 0, 0);
                viewparams->field_48 = 1;
                continue;
              }
            }
          }
          continue;
        case UIMSG_QuickReference:
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          if ( pCurrentScreen )
            pGUIWindow_CurrentMenu->Release();
          ++pIcons_LOD->uTexturePacksCount;
          if ( !pIcons_LOD->uNumPrevLoadedFiles )
            pIcons_LOD->uNumPrevLoadedFiles = pIcons_LOD->uNumLoadedFiles;
          GUIWindow::Create(0x230u, 0x1C2u, 0, 0, WINDOW_PressedButton2, (int)pBtn_QuickReference, 0);
          viewparams->bRedrawGameUI = 1;
          pEventTimer->Pause();
          pAudioPlayer->StopChannels(-1, -1);
          pCurrentScreen = SCREEN_QUICK_REFERENCE;
          pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640, 480, WINDOW_QuickReference, 5, 0);
          papredoll_dbrds[2] = pIcons_LOD->LoadTexture("BUTTEXI1", TEXTURE_16BIT_PALETTE);
          pBtn_ExitCancel = pGUIWindow_CurrentMenu->CreateButton(0x187u, 0x13Cu, 0x4Bu, 0x21u, 1, 0, UIMSG_Escape, 0, 0,
                         pGlobalTXT_LocalizationStrings[79],// "Exit"
                         pIcons_LOD->GetTexture(uTextureID_BUTTDESC2), 0); //, v179);
          continue;
        case UIMSG_GameMenuButton:
          if ( pCurrentScreen )
          {
            pGUIWindow_CurrentMenu->Release();
            pEventTimer->Resume();
            pCurrentScreen = SCREEN_GAME;
            viewparams->bRedrawGameUI = 1;
          }
          stru_506E40.Release();
          uNumSeconds = (unsigned int)MakeScreenshot(155, 117);
          pRenderer->SavePCXImage("gamma.pcx", (char *)uNumSeconds, 155, 117);
          free((void *)uNumSeconds);
          stru_506E40._40E55E("gamma.pcx", 0);
          GUIWindow::Create(0x25Au, 0x1C2u, 0, 0, WINDOW_PressedButton, (int)pBtn_GameSettings, 0);
//LABEL_453:
          /*if ( (signed int)pMessageQueue_50CBD0->uNumMessages >= 40 )
            continue;
          pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].eType = UIMSG_Escape;
          //goto LABEL_770;
          pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].param = 0;
          *(&pMessageQueue_50CBD0->uNumMessages + 3 * pMessageQueue_50CBD0->uNumMessages + 3) = 0;
          ++pMessageQueue_50CBD0->uNumMessages;*/
          pMessageQueue_50CBD0->AddMessage(UIMSG_Escape, 0, 0);
          continue;
        case UIMSG_ClickAwardScrollBar:
          dword_50651C = 1;
          if ( pMouse->GetCursorPos(&v211)->y > 178 )
            dword_50651C = -1;
          continue;
        case UIMSG_ClickAwardsUpBtn:
          GUIWindow::Create(pBtn_Up->uX, pBtn_Up->uY, 0, 0, WINDOW_CharactersPressedButton, (int)pBtn_Up, 0);
          BtnUp_flag = 1;
          continue;
        case UIMSG_ClickAwardsDownBtn:
          GUIWindow::Create(pBtn_Down->uX, pBtn_Down->uY, 0, 0, WINDOW_CharactersPressedButton, (int)pBtn_Down, 0);
          BtnDown_flag = 1;
          continue;
        case UIMSG_ChangeDetaliz:
          bRingsShownInCharScreen ^= 1;
          pCharacterScreen_DetalizBtn->Release();
          pCharacterScreen_DollBtn->Release();
          if ( bRingsShownInCharScreen )
          {
            v128 = pIcons_LOD->GetTexture(uTextureID_detaliz_close_button)->uTextureHeight;
            v125 = pIcons_LOD->GetTexture(uTextureID_detaliz_close_button)->uTextureWidth;
            v123 = 445;
            v121 = 470;
          }
          else
          {
            v128 = 30;
            v125 = 30;
            v123 = 300;
            v121 = 600;
          }
          pCharacterScreen_DetalizBtn = pGUIWindow_CurrentMenu->CreateButton(v121, v123, v125, v128, 1, 0, UIMSG_ChangeDetaliz, 0, 0,
                         pGlobalTXT_LocalizationStrings[64],// "Detail Toggle"
                         0);
          pCharacterScreen_DollBtn = pGUIWindow_CurrentMenu->CreateButton(0x1DCu, 0, 0xA4u, 0x159u, 1, 0, UIMSG_ClickPaperdoll, 0, 0, "", 0);
          viewparams->bRedrawGameUI = 1;
          continue;
        case UIMSG_ClickPaperdoll:
          OnPaperdollLeftClick();
          continue;
        case UIMSG_ClickStatsBtn:
          pWindowList_at_506F50_minus1_indexing_buttons____and_an_int_[0] = 100;
          CharacterUI_ReleaseButtons();
          sub_419379();
          GUIWindow::Create(pCharacterScreen_StatsBtn->uX, pCharacterScreen_StatsBtn->uY, 0, 0, WINDOW_CharactersPressedButton, (int)pCharacterScreen_StatsBtn, 0);
          continue;
        case UIMSG_ClickSkillsBtn:
          pWindowList_at_506F50_minus1_indexing_buttons____and_an_int_[0] = 101;
          CharacterUI_ReleaseButtons();
          sub_419379();
          CharacterUI_SkillsTab_CreateButtons();
          GUIWindow::Create(pCharacterScreen_SkillsBtn->uX, pCharacterScreen_SkillsBtn->uY, 0, 0, WINDOW_CharactersPressedButton, (int)pCharacterScreen_SkillsBtn, 0);
          continue;
        case UIMSG_SkillUp:
          pPlayer4 = pPlayers[uActiveCharacter];
          v105 = (int)&pPlayer4->pActiveSkills[uMessageParam];
          LOWORD(v2) = *(short *)v105;
          uNumSeconds = v2;
          if ( pPlayer4->uSkillPoints < (v2 & 0x3F) + 1 )
          {
            v87 = pGlobalTXT_LocalizationStrings[488];// "You don't have enough skill points!"
          }
          else
          {
            if ( (uNumSeconds & 0x3F) < 0x3C )
            {
              *(short *)v105 = uNumSeconds + 1;
              pPlayer4->uSkillPoints -= pPlayer4->pActiveSkills[uMessageParam] & 0x3F;
              pPlayer4->PlaySound(SPEECH_14, 0);
              pAudioPlayer->PlaySound((SoundID)20001, 0, 0, -1, 0, 0, 0, 0);
              continue;
            }
            v87 = pGlobalTXT_LocalizationStrings[487];// "You have already mastered this skill!"
          }
          ShowStatusBarString(v87, 2);
          continue;
        case UIMSG_ClickInventoryBtn:
          pWindowList_at_506F50_minus1_indexing_buttons____and_an_int_[0] = 103;
          sub_419379();
          CharacterUI_ReleaseButtons();
          GUIWindow::Create(pCharacterScreen_InventoryBtn->uX, pCharacterScreen_InventoryBtn->uY, 0, 0, WINDOW_CharactersPressedButton, (int)pCharacterScreen_InventoryBtn, 0);
          continue;
        case UIMSG_ClickExitCharacterWindowBtn:
          GUIWindow::Create(pCharacterScreen_ExitBtn->uX, pCharacterScreen_ExitBtn->uY, 0, 0, WINDOW_ExitCharacterWindow, (int)pCharacterScreen_ExitBtn, 0);
          continue;
        case UIMSG_ClickAwardsBtn:
          sub_419379();
          CharacterUI_ReleaseButtons();
          sub_419220();
          pWindowList_at_506F50_minus1_indexing_buttons____and_an_int_[0] = 102;
          GUIWindow::Create(pCharacterScreen_AwardsBtn->uX, pCharacterScreen_AwardsBtn->uY, 0, 0, WINDOW_CharactersPressedButton, (int)pCharacterScreen_AwardsBtn, 0);
          FillAwardsData();
          continue;
        case UIMSG_ClickBooksBtn:
          switch ( uMessageParam )
          {
            case 11://Page UP
              BtnUp_flag = 1;
              pButton = pBtn_Book_2;
              break;
            case 10://Page DOWN
              BtnDown_flag = 1;
              pButton = pBtn_Book_1;
              break;
            case 0://Zoom plus
              pButton = pBtn_Book_1;
              BtnDown_flag = 1;
              break;
            case 1://Zoom minus
              pButton = pBtn_Book_2;
              BtnUp_flag = 1;
              break;
            case 2://Potions
              Book_PageBtn3_flag = 1;
              if ( dword_506364 )
                continue;
              pButton = pBtn_Book_3;
              break;
            case 3://fountains
              Book_PageBtn4_flag = 1;
              if ( dword_506364 )
                continue;
              pButton = pBtn_Book_4;
              break;
            case 4://obelisks
              Book_PageBtn5_flag = 1;//Autonotes_Obelisks_page_flag
              if ( dword_506364 )
                continue;
              pButton = pBtn_Book_5;
              break;
            case 5://seer
              Book_PageBtn6_flag = 1;//Autonotes_Seer_page_flag
              if ( dword_506364 )
                continue;
              pButton = pBtn_Book_6;
              break;
            case 6://misc
              pButton = pBtn_Autonotes_Misc;
              Autonotes_Misc_page_flag = 1;
              break;
            case 7://Instructors
              pButton = pBtn_Autonotes_Instructors;
              Autonotes_Instructors_page_flag = 1;
              break;
            default:
              continue;
          }
          GUIWindow::Create(pButton->uX, pButton->uY, 0, 0, WINDOW_PressedButton, (int)pButton, (char *)1);
          continue;
        case UIMSG_SelectCharacter:
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          GameUI_OnPlayerPortraitLeftClick(uMessageParam);
          continue;
        case UIMSG_ShowStatus_Funds:
          v174 = (char *)pParty->uNumGoldInBank;
          v158 = pParty->uNumGold + pParty->uNumGoldInBank;
          sprintfex(pTmpBuf.data(), pGlobalTXT_LocalizationStrings[489], v158, v174);// "You have %d total gold, %d in the Bank"
          GameUI_SetFooterString(pTmpBuf.data());
          continue;
        case UIMSG_ShowStatus_DateTime:
          pNPCData4 = (NPCData *)pParty->uCurrentHour;
          if ( (signed int)pParty->uCurrentHour <= 12 )
          {
            if ( !pNPCData4 )
              pNPCData4 = (NPCData *)12;
          }
          else
          {
            pNPCData4 = (NPCData *)((char *)pNPCData4 - 12);
          }
          if ( pParty->uCurrentHour < 0xC || (uNumSeconds = 1, pParty->uCurrentHour >= 0x18) )
            uNumSeconds = 0;
          sprintf(pTmpBuf.data(), "%d:%02d%s %s %d %s %d", pNPCData4, pParty->uCurrentMinute, aAMPMNames[uNumSeconds], aDayNames[pParty->uDaysPlayed % 7],
            7 * pParty->uCurrentMonthWeek + pParty->uDaysPlayed % 7 + 1, aMonthNames[pParty->uCurrentMonth], pParty->uCurrentYear);
          GameUI_SetFooterString(pTmpBuf.data());
          continue;
        case UIMSG_ShowStatus_Food:
          sprintfex(pTmpBuf.data(), pGlobalTXT_LocalizationStrings[501], pParty->uNumFoodRations); // "You have %lu food"
          GameUI_SetFooterString(pTmpBuf.data());
          continue;
        case UIMSG_ShowStatus_Player:
          pPlayer5 = pPlayers[uMessageParam];
          sprintfex(pTmpBuf.data(), pGlobalTXT_LocalizationStrings[429], pPlayer5->pName, pClassNames[pPlayer5->classType]);// "%s the %s"
          strcat(pTmpBuf.data(), ": ");
          v107 = pPlayer5->GetMajorConditionIdx();
          strcat(pTmpBuf.data(), aCharacterConditionNames[v107]);
          GameUI_SetFooterString(pTmpBuf.data());
          v108 = 8 * uMessageParam - 8;
          LOBYTE(v108) = v108 | 4;
          pMouse->uPointingObjectID = PID(OBJECT_Player,v108);
          continue;
        case UIMSG_ShowStatus_ManaHP:
          sprintf(pTmpBuf.data(), "%d / %d %s    %d / %d %s", pPlayers[uMessageParam]->sHealth, pPlayers[uMessageParam]->GetMaxHealth(), 
                  pGlobalTXT_LocalizationStrings[108], pPlayers[uMessageParam]->sMana, pPlayers[uMessageParam]->GetMaxMana(), 
                  pGlobalTXT_LocalizationStrings[212]);
          GameUI_SetFooterString(pTmpBuf.data());
          continue;
        case UIMSG_CHEST_ClickItem:
          if ( pCurrentScreen == SCREEN_CHEST_INVENTORY )
          {
            OnInventoryLeftClick();
            continue;
          }
          OnChestLeftClick();
          continue;
        case UIMSG_InventoryLeftClick:
          OnInventoryLeftClick();
          continue;
        case UIMSG_MouseLeftClickInGame:
          if ( !pRenderer->pRenderD3D )
          {
            if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
            OnGameViewportClick();
            continue;
          }
          v115 = pMessageQueue_50CBD0->uNumMessages;
          if ( !pMessageQueue_50CBD0->uNumMessages )
          {
            pMessageQueue_50CBD0->AddMessage(UIMSG_MouseLeftClickInScreen, 0, 0);
            /*if ( (signed int)v115 < 40 )
            //goto LABEL_769;
            {
              pMessageQueue_50CBD0->pMessages[v115].eType = UIMSG_MouseLeftClickInScreen;
              pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].param = 0;
              *(&pMessageQueue_50CBD0->uNumMessages + 3 * pMessageQueue_50CBD0->uNumMessages + 3) = 0;
              ++pMessageQueue_50CBD0->uNumMessages;
              continue;
            }*/
            continue;
          }
          if ( pMessageQueue_50CBD0->pMessages[0].field_8 )
          {
            pMessageQueue_50CBD0->uNumMessages = 1;
            pMessageQueue_50CBD0->AddMessage(UIMSG_MouseLeftClickInScreen, 0, 0);
            /*v115 = v0;
            pMessageQueue_50CBD0->uNumMessages = v0;
            pMessageQueue_50CBD0->pMessages[v115].eType = UIMSG_MouseLeftClickInScreen;
            pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].param = 0;
            //goto LABEL_771;
            *(&pMessageQueue_50CBD0->uNumMessages + 3 * pMessageQueue_50CBD0->uNumMessages + 3) = 0;
            ++pMessageQueue_50CBD0->uNumMessages;*/
            continue;
          }
          v115 = 0;
          pMessageQueue_50CBD0->uNumMessages = 0;
          pMessageQueue_50CBD0->AddMessage(UIMSG_MouseLeftClickInScreen, 0, 0);
          /*if ( (signed int)v115 < 40 )
            //goto LABEL_769;
          {
            pMessageQueue_50CBD0->pMessages[v115].eType = UIMSG_MouseLeftClickInScreen;
            pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].param = 0;
            *(&pMessageQueue_50CBD0->uNumMessages + 3 * pMessageQueue_50CBD0->uNumMessages + 3) = 0;
            ++pMessageQueue_50CBD0->uNumMessages;
            continue;
          }*/
          continue;
        case UIMSG_MouseLeftClickInScreen://        UIMSG_MouseLeftClickInGame
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          OnGameViewportClick();
          continue;
        case UIMSG_F:
			__debugbreak();
          if ( pRenderer->pRenderD3D )
          {
            LOWORD(v116) = pGame->pVisInstance->get_picked_object_zbuf_val();
          }
          else
          {
            uNumSeconds = (unsigned int)pMouse->GetCursorPos(&v209);
            pPoint3 = pMouse->GetCursorPos(&v204);
            v116 = pRenderer->pActiveZBuffer[*(int *)uNumSeconds + pSRZBufferLineOffsets[pPoint3->y]];
          }
          pButton2 = (GUIButton *)(unsigned __int16)v116;
          GUIWindow::Create(0, 0, 0, 0, WINDOW_F, (int)pButton2, 0);
          continue;
        case UIMSG_54:
			__debugbreak();
          pButton2 = (GUIButton *)uMessageParam;
          GUIWindow::Create(0, 0, 0, 0, WINDOW_22, (int)pButton2, 0);
          continue;
        case UIMSG_Game_Action:
          if ( pMessageQueue_50CBD0->uNumMessages )
            pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50CBD0->pMessages[0].field_8 != 0;
          OnPressSpace();
          continue;
        case UIMSG_ClickZoomOutBtn:
          if ( pCurrentScreen )
            continue;
          pParty->uFlags |= 2u;
          GUIWindow::Create(519, 136, 0, 0, WINDOW_PressedButton2, (int)pBtn_ZoomOut, 0);
          uNumSeconds = 131072;
          v118 = 2 * viewparams->uMinimapZoom;
          ++viewparams->field_28;
          viewparams->uMinimapZoom *= 2;
          if ( uCurrentlyLoadedLevelType != LEVEL_Outdoor)
          {
            if ( (signed int)v118 > 4096 )
            {
              viewparams->uMinimapZoom = 4096;
              viewparams->field_28 = 12;
            }
            continue;
          }
          v119 = 2048;
          if ( (signed int)v118 <= 2048 )
          {
            _576E2C_current_minimap_zoom = v118;
            dword_576E28 = viewparams->field_28;
            break;
          }
          viewparams->field_28 = 11;
          viewparams->uMinimapZoom = v119;
          _576E2C_current_minimap_zoom = v119;
          dword_576E28 = viewparams->field_28;
          break;
        case UIMSG_ClickZoomInBtn:
          if ( pCurrentScreen )
            continue;
          pParty->uFlags |= 2u;
          GUIWindow::Create(574, 136, 0, 0, WINDOW_PressedButton2, (int)pBtn_ZoomIn, 0);
          uNumSeconds = 32768;
          v118 = (unsigned __int64)((signed __int64)(signed int)viewparams->uMinimapZoom << 15) >> 16;
          --viewparams->field_28;
          viewparams->uMinimapZoom = (unsigned __int64)((signed __int64)(signed int)viewparams->uMinimapZoom << 15) >> 16;
          if ( uCurrentlyLoadedLevelType == LEVEL_Outdoor)
          {
            v119 = 512;
            if ( (signed int)v118 < 512 )
            {
              viewparams->field_28 = 9;
              v118 = v119;
              viewparams->uMinimapZoom = v119;
            }
            _576E2C_current_minimap_zoom = v118;
            dword_576E28 = viewparams->field_28;
          }
          else
          {
            if ( (signed int)v118 < 256 )
            {
              viewparams->uMinimapZoom = 256;
              viewparams->field_28 = 8;
            }
          }
        default:
          continue;
      }
    }
  }
  pMessageQueue_50CBD0->uNumMessages = pMessageQueue_50C9E8->uNumMessages;//dword_50C9E8;
  memcpy(pMessageQueue_50CBD0->pMessages, pMessageQueue_50C9E8->pMessages, sizeof(GUIMessage) * pMessageQueue_50C9E8->uNumMessages);
  //memcpy(pMessageQueue_50CBD0->pMessages, dword_50C9EC, 12 * dword_50C9E8);
  //dword_50C9E8 = 0;


   pMessageQueue_50C9E8->uNumMessages=0;
  if ( dword_50C9DC )
  {
    /*if ( (signed int)pMessageQueue_50CBD0->uNumMessages < 40 )
    {
      pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].eType = (UIMessageType)dword_50C9DC;
      pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].param = (int)ptr_50C9E0;
      *(&pMessageQueue_50CBD0->uNumMessages + 3 * pMessageQueue_50CBD0->uNumMessages + 3) = 0;
      ++pMessageQueue_50CBD0->uNumMessages;
    }*/
    pMessageQueue_50CBD0->AddMessage((UIMessageType)dword_50C9DC, (int)ptr_50C9E0, 0);
    dword_50C9DC = 0;
  }
  else
  {
    if ( dword_50C9D0 > 0 )
    {
      dword_50C9D8 -= pEventTimer->uTimeElapsed;
      if ( dword_50C9D8 <= 0 )
      {
        /*if ( (signed int)pMessageQueue_50CBD0->uNumMessages < 40 )
        {
          pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].eType = (UIMessageType)dword_50C9D0;
          pMessageQueue_50CBD0->pMessages[pMessageQueue_50CBD0->uNumMessages].param = dword_50C9D4;
          *(&pMessageQueue_50CBD0->uNumMessages + 3 * pMessageQueue_50CBD0->uNumMessages + 3) = 0;
          ++pMessageQueue_50CBD0->uNumMessages;
        }*/
        pMessageQueue_50CBD0->AddMessage((UIMessageType)dword_50C9D0, dword_50C9D4, 0);
        dword_50C9D0 = 0;
        dword_50C9D4 = 0;
        dword_50C9D8 = 0;
      }
    }
  }
  pCastSpellInfo.data()->_427E01_cast_spell();
}

//----- (004356FF) --------------------------------------------------------
void __cdecl back_to_game()
{
  dword_507BF0_is_there_popup_onscreen = 0;
  dword_4E455C = 1;
  dword_50CDCC = 0;
  if ( pGUIWindow_ScrollWindow )
    free_book_subwindow();
  if ( !pCurrentScreen && !pGUIWindow_Settings )
    pEventTimer->Resume();
  viewparams->bRedrawGameUI = 1;
}

//----- (00435748) --------------------------------------------------------
void __cdecl GUI_MainMenuMessageProc()
{
  Player *pPlayer; // ebx@2
  void *v3; // edi@21
  signed int v4; // eax@29
  int v5; // ecx@29
  PLAYER_SKILL_TYPE v6; // edi@37
  GUIWindow *pWindow; // eax@56
  GUIButton *pButton; // eax@59
  int v15; // edi@70
  char v20; // dl@116
  unsigned int v21; // eax@116
  unsigned int v25; // eax@120
  unsigned int v26; // ecx@127
  SoundID pSoundID; // [sp-2Ch] [bp-3Ch]@36
  signed int v41; // [sp-10h] [bp-20h]@29
  int pParam; // [sp+4h] [bp-Ch]@3
  UIMessageType pUIMessageType; // [sp+8h] [bp-8h]@3
  int pSex; // [sp+Ch] [bp-4h]@3

  if ( pMessageQueue_50CBD0->uNumMessages )
  {
    pPlayer = pParty->pPlayers;
    do
    {
      int param2;
      pMessageQueue_50CBD0->PopMessage(&pUIMessageType, &pParam, &param2);
      auto player = &pParty->pPlayers[pParam];

      switch (pUIMessageType) // For buttons of window MainMenu
      {
        case UIMSG_MainMenu_ShowPartyCreationWnd:
          GUIWindow::Create(495, 172, 0, 0, WINDOW_PressedButton2, (int)pMainMenu_BtnNew, 0);
          SetCurrentMenuID(MENU_NEWGAME);
          break;
        case UIMSG_MainMenu_ShowLoadWindow:
          GUIWindow::Create(495, 227, 0, 0, WINDOW_PressedButton2, (int)pMainMenu_BtnLoad, 0);
          SetCurrentMenuID(MENU_SAVELOAD);
          break;
        case UIMSG_ShowCredits:
          GUIWindow::Create(495, 282, 0, 0, WINDOW_PressedButton2, (int)pMainMenu_BtnCredits, 0);
          SetCurrentMenuID(MENU_CREDITS);
          break;
        case UIMSG_ExitToWindows:
          GUIWindow::Create(495, 337, 0, 0, WINDOW_PressedButton2, (int)pMainMenu_BtnExit, 0);
          SetCurrentMenuID(MENU_EXIT_GAME);
          break;
        case UIMSG_PlayerCreation_SelectAttribute:
          pGUIWindow_CurrentMenu->pCurrentPosActiveItem = (pGUIWindow_CurrentMenu->pCurrentPosActiveItem - pGUIWindow_CurrentMenu->pStartingPosActiveItem)
                                                    % 7 + pGUIWindow_CurrentMenu->pStartingPosActiveItem + 7 * pParam;
          uPlayerCreationUI_SelectedCharacter = pParam;
          pAudioPlayer->PlaySound(SOUND_Button, 0, 0, -1, 0, 0, 0, 0);
          break;
        case UIMSG_PlayerCreation_VoicePrev:
          pSex = player->GetSexByVoice();
          do
          {
            if (player->uVoiceID == 0)
              player->uVoiceID = 19;
            else --player->uVoiceID;
          }
          while (player->GetSexByVoice() != pSex);
          pButton = pCreationUI_BtnPressLeft2[pParam];
          GUIWindow::Create(pButton->uX, pButton->uY, 0, 0, WINDOW_PressedButton, (int)pButton, (char *)1);
          pAudioPlayer->PlaySound(SOUND_Button, 0, 0, -1, 0, 0, 0, 0);
          player->PlaySound(SPEECH_PickMe, 0);
          break;
        case UIMSG_PlayerCreation_VoiceNext:
          pSex = player->GetSexByVoice();
          do
          {
            player->uVoiceID = (player->uVoiceID + 1) % 20;
          }
          while (player->GetSexByVoice() != pSex);
          pButton = pCreationUI_BtnPressRight2[pParam];
          GUIWindow::Create(pButton->uX, pButton->uY, 0, 0, WINDOW_PressedButton, (int)pButton, (char *)1);
          pAudioPlayer->PlaySound(SOUND_Button, 0, 0, -1, 0, 0, 0, 0);
          player->PlaySound(SPEECH_PickMe, 0);
          break;
        case UIMSG_PlayerCreation_FacePrev:
          pPlayer = &pParty->pPlayers[pParam];
          if (!pPlayer->uCurrentFace)
            pPlayer->uCurrentFace = 19;
          else
            pPlayer->uCurrentFace -= 1;
          pPlayer->uVoiceID = pPlayer->uCurrentFace;
          pPlayer->SetInitialStats();
          pPlayer->SetSexByVoice();
          pPlayer->RandomizeName();
          v25 = pParam;
          pGUIWindow_CurrentMenu->pCurrentPosActiveItem = (pGUIWindow_CurrentMenu->pCurrentPosActiveItem - pGUIWindow_CurrentMenu->pStartingPosActiveItem)
                                                    % 7 + pGUIWindow_CurrentMenu->pStartingPosActiveItem + 7 * pParam;
          uPlayerCreationUI_SelectedCharacter = v25;
          GUIWindow::Create(pCreationUI_BtnPressLeft[v25]->uX, pCreationUI_BtnPressLeft[v25]->uY, 0, 0, WINDOW_PressedButton, (int)pCreationUI_BtnPressLeft[v25], (char *)1);
          pAudioPlayer->PlaySound(SOUND_Button, 0, 0, -1, 0, 0, 0.0, 0);
          pPlayer->PlaySound(SPEECH_PickMe, 0);
          break;
        case UIMSG_PlayerCreation_FaceNext:
          pPlayer = &pParty->pPlayers[pParam];
          v20 = (char)((int)pPlayer->uCurrentFace + 1) % 20;
          pPlayer->uCurrentFace = v20;
          pPlayer->uVoiceID = v20;
          pPlayer->SetInitialStats();
          pPlayer->SetSexByVoice();
          pPlayer->RandomizeName();
          v21 = pParam;
          pGUIWindow_CurrentMenu->pCurrentPosActiveItem = (pGUIWindow_CurrentMenu->pCurrentPosActiveItem - pGUIWindow_CurrentMenu->pStartingPosActiveItem)
                                                    % 7 + pGUIWindow_CurrentMenu->pStartingPosActiveItem + 7 * pParam;
          uPlayerCreationUI_SelectedCharacter = v21;
          GUIWindow::Create(pCreationUI_BtnPressRight[v21]->uX, pCreationUI_BtnPressRight[v21]->uY, 0, 0, WINDOW_PressedButton, (int)pCreationUI_BtnPressRight[v21], (char *)1);
          pAudioPlayer->PlaySound(SOUND_Button, 0, 0, -1, 0, 0, 0, 0);
          pPlayer->PlaySound(SPEECH_PickMe, 0);
          break;
        case UIMSG_PlayerCreationClickPlus:
          GUIWindow::Create(613, 393, 0, 0, WINDOW_PressedButton2, (int)pPlayerCreationUI_BtnPlus, (char *)1);
          pPlayer[uPlayerCreationUI_SelectedCharacter].IncreaseAttribute((pGUIWindow_CurrentMenu->pCurrentPosActiveItem - pGUIWindow_CurrentMenu->pStartingPosActiveItem) % 7);
          pAudioPlayer->PlaySound((SoundID)20, 0, 0, -1, 0, 0, 0, 0);
          break;
        case UIMSG_PlayerCreationClickMinus:
          GUIWindow::Create(523, 393, 0, 0, WINDOW_PressedButton2, (int)pPlayerCreationUI_BtnMinus, (char *)1);
          pPlayer[uPlayerCreationUI_SelectedCharacter].DecreaseAttribute((pGUIWindow_CurrentMenu->pCurrentPosActiveItem - pGUIWindow_CurrentMenu->pStartingPosActiveItem) % 7);
          pAudioPlayer->PlaySound((SoundID)23, 0, 0, -1, 0, 0, 0, 0);
          break;
        case UIMSG_PlayerCreationSelectActiveSkill:
          pPlayer = &pPlayer[uPlayerCreationUI_SelectedCharacter];
          v6 = pPlayer->GetSkillIdxByOrder(pParam + 4);
          if ( pPlayer->GetSkillIdxByOrder(3) == 37 )
            pParty->pPlayers[0].pActiveSkills[v6 + 3486 * uPlayerCreationUI_SelectedCharacter] = 1;
          pAudioPlayer->PlaySound((SoundID)24, 0, 0, -1, 0, 0, 0, 0);
          break;
        case UIMSG_PlayerCreationSelectClass:
          pPlayer[uPlayerCreationUI_SelectedCharacter].Reset((PLAYER_CLASS_TYPE)pParam);
          pAudioPlayer->PlaySound(SOUND_Button, 0, 0, -1, 0, 0, 0, 0);
          break;
        case UIMSG_PlayerCreationClickOK:
          GUIWindow::Create(580, 431, 0, 0, WINDOW_PressedButton2, (int)pPlayerCreationUI_BtnOK, 0);
          if ( PlayerCreation_GetUnspentAttributePointCount() || !PlayerCreation_Chose4Skills() )
            GameUI_Footer_TimeLeft = GetTickCount() + 4000;
          else
            uGameState = GAME_STATE_STARTING_NEW_GAME;
          break;
        case UIMSG_PlayerCreationClickReset:
          GUIWindow::Create(527, 431, 0, 0, WINDOW_PressedButton2, (int)pPlayerCreationUI_BtnReset, 0);
          pParty->Reset();
          break;
        case UIMSG_PlayerCreationRemoveUpSkill:
          uPlayerCreationUI_SelectedCharacter = pParam;
          v4 = pGUIWindow_CurrentMenu->pCurrentPosActiveItem - pGUIWindow_CurrentMenu->pStartingPosActiveItem;
          //int pSex = pGUIWindow_CurrentMenu->pStartingPosActiveItem;
          v41 = 2;
          v5 = v4 % 7 + pGUIWindow_CurrentMenu->pStartingPosActiveItem + 7 * pParam;
          pGUIWindow_CurrentMenu->pCurrentPosActiveItem = v5;
          pParty->pPlayers[0].pActiveSkills[(&pPlayer[uPlayerCreationUI_SelectedCharacter])->GetSkillIdxByOrder(v41)
                                         + 3486 * uPlayerCreationUI_SelectedCharacter] = 0;
          break;
        case UIMSG_PlayerCreationChangeName:
          pAudioPlayer->PlaySound((SoundID)24, 0, 0, -1, 0, 0, 0, 0);
          v3 = (void *)pParam;
          uPlayerCreationUI_SelectedCharacter = pParam;
          pKeyActionMap->EnterText(0, 15, pGUIWindow_CurrentMenu);
          pGUIWindow_CurrentMenu->ptr_1C = v3;
          break;
        case UIMSG_ChangeGameState:
          uGameState = GAME_FINISHED;
          break;
        case UIMSG_11:
          pMouse->SetCursorBitmap("MICON2");
          break;
        case UIMSG_3A:
          SetCurrentMenuID(MENU_10);
          break;
        case UIMSG_LoadGame:
          if (!pSavegameUsedSlots[uLoadGameUI_SelectedSlot])
            break;
          SetCurrentMenuID(MENU_LoadingProcInMainMenu);
          break;
        case UIMSG_SelectLoadSlot:
            //main menu save/load wnd   clicking on savegame lines
          if (pGUIWindow_CurrentMenu->receives_keyboard_input_2 == WINDOW_INPUT_IN_PROGRESS)
            pKeyActionMap->SetWindowInputStatus(WINDOW_INPUT_NONE);
          if ( pCurrentScreen != SCREEN_SAVEGAME || uLoadGameUI_SelectedSlot != pParam + pSaveListPosition )
          {
            //load clicked line
              v26 = pParam + pSaveListPosition;
              if ( dword_6BE138 == pParam + pSaveListPosition )
              {
                pMessageQueue_50CBD0->AddMessage(UIMSG_SaveLoadBtn, 0, 0);
                pMessageQueue_50CBD0->AddMessage(UIMSG_LoadGame, 0, 0);
              }
              uLoadGameUI_SelectedSlot = v26;
              dword_6BE138 = v26;
          }
          else
          {
            //typing in the line
              pKeyActionMap->EnterText(0, 19, pGUIWindow_CurrentMenu);
              strcpy((char *)pKeyActionMap->pPressedKeysBuffer, pSavegameHeader[uLoadGameUI_SelectedSlot].pName);
              pKeyActionMap->uNumKeysPressed = strlen((const char *)pKeyActionMap->pPressedKeysBuffer);
          }
          break;
        case UIMSG_SaveLoadBtn:
          GUIWindow::Create(pGUIWindow_CurrentMenu->uFrameX + 241, pGUIWindow_CurrentMenu->uFrameY + 302, 61, 28, WINDOW_SaveLoadBtn, (int)pBtnLoadSlot, 0);
          break;
        case UIMSG_DownArrow:
          ++pSaveListPosition;
          if ( pSaveListPosition >= pParam )
            pSaveListPosition = pParam - 1;
          if ( pSaveListPosition < 1 )
            pSaveListPosition = 0;
          pWindow = pGUIWindow_CurrentMenu;
          GUIWindow::Create(pWindow->uFrameX + 215, pGUIWindow_CurrentMenu->uFrameY + 323, 0, 0, WINDOW_PressedButton2, (int)pBtnDownArrow, 0);
          break;
        case UIMSG_Cancel:
          GUIWindow::Create(pGUIWindow_CurrentMenu->uFrameX + 350, pGUIWindow_CurrentMenu->uFrameY + 302, 61, 28, WINDOW_LoadGame_CancelBtn, (int)pBtnCancel, 0);
          break;
        case UIMSG_ArrowUp:
          --pSaveListPosition;
          if ( pSaveListPosition < 0 )
            pSaveListPosition = 0;
          pWindow = pGUIWindow_CurrentMenu;
          GUIWindow::Create(pWindow->uFrameX + 215, pGUIWindow_CurrentMenu->uFrameY + 197, 0, 0, WINDOW_PressedButton2, (int)pBtnArrowUp, 0);
          break;
        case UIMSG_AD:
          GUIWindow::Create(pMainMenu_BtnNew->uX, pMainMenu_BtnNew->uY, 0, 0, WINDOW_PressedButton2, (int)pMainMenu_BtnNew, 0);
          SetCurrentMenuID(MENU_LoadingProcInMainMenu);
          break;
        case UIMSG_AE:
          GUIWindow::Create(pMainMenu_BtnExit->uX, pMainMenu_BtnExit->uY, 0, 0, WINDOW_PressedButton2, (int)pMainMenu_BtnExit, 0);
          SetCurrentMenuID(MENU_10);
          break;
        case UIMSG_Escape:
          if ( ptr_507BDC )
          {
            sub_41426F();
            break;
          }
          if ( !(BYTE1(dword_6BE364_game_settings_1) & 0x40) )
            break;
          v15 = 1;
          pVideoPlayer->bStopBeforeSchedule = 1;
          viewparams->bRedrawGameUI = 1;
          viewparams->field_48 = 1;
          if ( !GetCurrentMenuID() || GetCurrentMenuID() == MENU_CREATEPARTY || GetCurrentMenuID() == MENU_NAMEPANELESC )
          {
            if ( pCurrentScreen == SCREEN_VIDEO )
              pVideoPlayer->FastForwardToFrame(pVideoPlayer->pResetflag);
            if (GetCurrentMenuID() == MENU_NAMEPANELESC)
            {
              SetCurrentMenuID(MENU_CREATEPARTY);
              break;
            }
            if (GetCurrentMenuID() == MENU_CREDITSPROC)
            {
              SetCurrentMenuID(MENU_CREDITSCLOSE);
              break;
            }
            pMessageQueue_50CBD0->AddMessage(UIMSG_ChangeGameState, 0, 0);
            break;
          }
          if ( GetCurrentMenuID() == MENU_CREDITSPROC && !pCurrentScreen )
          {
            if ( pCurrentScreen == SCREEN_VIDEO )
              pVideoPlayer->FastForwardToFrame(pVideoPlayer->pResetflag);
            if (GetCurrentMenuID() == MENU_NAMEPANELESC)
            {
              SetCurrentMenuID(MENU_CREATEPARTY);
              break;
            }
            if (GetCurrentMenuID() == MENU_CREDITSPROC)
            {
              SetCurrentMenuID(MENU_CREDITSCLOSE);
              break;
            }
            pMessageQueue_50CBD0->AddMessage(UIMSG_ChangeGameState, 0, 0);
            break;
          }
          if ( pCurrentScreen == SCREEN_LOADGAME )
          {
            pIcons_LOD->_4114F2();
            //crt_deconstruct_ptr_6A0118();
            pTexture_PCX.Release();
            pTexture_PCX.Load("title.pcx", 0);
            SetCurrentMenuID(MENU_MAIN);
            v15 = 1;
            pGUIWindow_CurrentMenu->Release();
            pGUIWindow2->Release();
            pGUIWindow2 = 0;
            pEventTimer->Resume();
            pCurrentScreen = SCREEN_GAME;
            viewparams->bRedrawGameUI = v15;
            break;
          }
          if ( pCurrentScreen == SCREEN_VIDEO )
          {
            pVideoPlayer->Unload();
          }
          else
          {
            if ( pCurrentScreen != SCREEN_1B )
            {
              pGUIWindow_CurrentMenu->Release();
              pGUIWindow2->Release();
              pGUIWindow2 = 0;
              pEventTimer->Resume();
              pCurrentScreen = SCREEN_GAME;
              viewparams->bRedrawGameUI = v15;
              break;
            }
            //VideoPlayer::dtor();
          }
          break;
        case UIMSG_PlayerCreationRemoveDownSkill:
          uPlayerCreationUI_SelectedCharacter = pParam;
          pGUIWindow_CurrentMenu->pCurrentPosActiveItem = (pGUIWindow_CurrentMenu->pCurrentPosActiveItem - pGUIWindow_CurrentMenu->pStartingPosActiveItem) % 7
             + pGUIWindow_CurrentMenu->pStartingPosActiveItem + 7 * pParam;
          pParty->pPlayers[0].pActiveSkills[(&pPlayer[uPlayerCreationUI_SelectedCharacter])->GetSkillIdxByOrder(3)
                                             + 3486 * uPlayerCreationUI_SelectedCharacter] = 0;
          break;
        default:
          break;
      }
    }
    while ( pMessageQueue_50CBD0->uNumMessages );
  }
}

//----- (00436427) --------------------------------------------------------
double __cdecl get_shading_dist_mist()
{
  double result; // st7@2

  if ( uCurrentlyLoadedLevelType == LEVEL_Outdoor)
    result = (double)pOutdoorCamera->shading_dist_mist;
  else
    result = 16192.0;
  return result;
}

//----- (0043648F) --------------------------------------------------------
void Vec3_short__to_RenderVertexSoft(RenderVertexSoft *_this, Vec3_short_ *a2)
{
  _this->flt_2C = 0.0;

  _this->vWorldPosition.x = a2->x;
  _this->vWorldPosition.y = a2->y;
  _this->vWorldPosition.z = a2->z;
}

//----- (004369DB) --------------------------------------------------------
void Vec3_float_::Normalize()
{
  this->x = (1.0 / sqrt(this->x * this->x + this->y * this->y + this->z * this->z)) * this->x;
  this->y = (1.0 / sqrt(this->x * this->x + this->y * this->y + this->z * this->z)) * this->y;
  this->z = (1.0 / sqrt(this->x * this->x + this->y * this->y + this->z * this->z)) * this->z;
}

//----- (00438526) --------------------------------------------------------
stru11::stru11()
{
  stru11 *v1; // esi@1
  signed int v2; // eax@1
  char *v3; // edx@1

  v1 = this;
  v2 = 0;
  this->cpuid_00000000_eax_numops = 0;
  this->cpuid_00000001_eax = 0;
  this->cpuid_00000001_edx = 0;
  this->field_38 = 0;
  this->field_3C = 0;
  this->cpuid_80000000_edx = 0;
  this->cpuid_80000001_eax = 0;
  this->cpuid_80000001_edx = 0;
  this->cpuid_80000005_ebx = 0;
  this->cpuid_80000005_ecx = 0;
  this->cpuid_80000005_edx = 0;
  this->cpuid_80000006_ecx = 0;
  this->field_AC = 0;
  this->uProcessorManufacturer = 0;
  LOBYTE(this->cpuid_80000002_registers2[0]) = 0;
  v3 = (char *)&this->cpuid_00000002_eax;
  do
  {
    if ( v2 < 3 )
      *((int *)v3 - 9) = 0;
    if ( v2 < 4 )
      *(int *)v3 = 0;
    if ( v2 < 12 )
      *((int *)v3 + 17) = 0;
    if ( v2 < 13 )
      this->pCPUString[v2] = 0;
    if ( v2 < 30 )
      *((char *)this->field_40 + v2) = 0;
    *((char *)&this->cpuid_80000002_registers2[0] + v2++ + 1) = 0;
    v3 += 4;
  }
  while ( v2 < 48 );
  CheckCPU();
}

//----- (004385B5) --------------------------------------------------------
void stru11::CheckCPU()
{
  LOG_DECOMPILATION_WARNING();
  /*
  int v5; // [sp-4h] [bp-10h]@0
  signed int v6; // [sp+4h] [bp-8h]@1
  signed int v7; // [sp+8h] [bp-4h]@1

  _CF = 0;
  _OF = 0;
  _ZF = 1;
  _SF = 0;
  v7 = 0;
  __asm { pushf }
  v6 = 3;
  if ( v5 != (v5 ^ 0x40000) )
  {
    __asm { popf }
    v6 = 4;
    __asm { pushf }
    if ( v5 != (v5 ^ 0x200000) )
    {
      v7 = 1;
      v6 = 0;
    }
  }
  if ( v6 == 3 )
  {
    this->field_AC = 1;
  }
  else
  {
    if ( v6 == 4 )
    {
      this->field_AC = 2;
    }
    else
    {
      if ( v7 )
      {
        this->field_AC = 2;
        RunCPUID();
      }
      else
      {
        this->field_AC = 0;
      }
    }
  }*/
}

//----- (00438659) --------------------------------------------------------
void stru11::RunCPUID()
{
  __debugbreak();
  /*stru11 *v6; // esi@1
  unsigned int uNumOps; // edi@1
  int uNumExtOps; // edi@16
  char pCyrixString[16]; // [sp+Ch] [bp-60h]@1
  char pCentaurString[16]; // [sp+1Ch] [bp-50h]@1
  char pAMDString[16]; // [sp+2Ch] [bp-40h]@1
  char pIntelString[16]; // [sp+3Ch] [bp-30h]@1
  char pCPUString[16]; // [sp+4Ch] [bp-20h]@1
  stru11 *thisa; // [sp+5Ch] [bp-10h]@1
  char *v35; // [sp+60h] [bp-Ch]@1
  int v36; // [sp+64h] [bp-8h]@1
  int v37; // [sp+68h] [bp-4h]@1

  thisa = this;
  *(int *)pIntelString = *(int *)"GenuineIntel";
  *(int *)&pIntelString[4] = *(int *)"ineIntel";
  *(int *)&pIntelString[8] = *(int *)"ntel";
  pIntelString[12] = aGenuineintel[12];
  *(int *)pAMDString = *(int *)"AuthenticAMD";
  *(int *)&pAMDString[4] = *(int *)"enticAMD";
  *(int *)&pAMDString[8] = *(int *)"cAMD";
  pAMDString[12] = aAuthenticamd[12];
  *(int *)pCyrixString = *(int *)"CyrixInstead";
  *(int *)&pCyrixString[4] = *(int *)"xInstead";
  *(int *)&pCyrixString[8] = *(int *)"tead";
  pCyrixString[12] = aCyrixinstead[12];
  *(int *)pCentaurString = *(int *)"CentaurHauls";
  *(int *)&pCentaurString[4] = *(int *)"aurHauls";
  *(int *)&pCentaurString[8] = *(int *)"auls";
  v37 = 0;
  v36 = 0;
  pCentaurString[12] = aCentaurhauls[12];
  v35 = pCPUString;
  pCPUString[12] = 0;
  _EAX = 0;
  __asm { cpuid }
  v37 = _EAX;
  *(int *)pCPUString = _EBX;
  *(int *)&pCPUString[4] = _EDX;
  *(int *)&pCPUString[8] = _ECX;
  v6 = thisa;
  uNumOps = _EAX;
  thisa->cpuid_00000000_ebx_vendorstr1 = _EBX;
  v6->cpuid_00000000_edx_vendorstr2 = *(int *)&pCPUString[4];
  v6->cpuid_00000000_ecx_vendorstr3 = *(int *)&pCPUString[8];
  v6->cpuid_00000000_eax_numops = _EAX;
  strcpy(v6->pCPUString, pCPUString);
  if ( strcmp(pIntelString, pCPUString) )
  {
    if ( strcmp(pAMDString, pCPUString) )
    {
      if ( strcmp(pCyrixString, pCPUString) )
      {
        if ( strcmp(pCentaurString, pCPUString) )
          v6->uProcessorManufacturer = 0;
        else
          v6->uProcessorManufacturer = 4;
      }
      else
      {
        v6->uProcessorManufacturer = 3;
      }
    }
    else
    {
      v6->uProcessorManufacturer = 2;
    }
  }
  else
  {
    v6->uProcessorManufacturer = 1;
  }
  if ( uNumOps >= 1 )
    RunCPUID_op1();
  if ( uNumOps >= 2 )
    RunCPUID_op2();
  _EAX = 0x80000000u;
  __asm { cpuid }
  if ( !(_EAX & 0x80000000) )
    _EAX = 0x80000000u;
  v36 = _EAX;
  v6->cpuid_80000000_edx = _EAX;
  uNumExtOps = _EAX ^ 0x80000000;
  if ( (_EAX ^ 0x80000000u) >= 1 )
  {
    _EAX = 0x80000001u;
    __asm { cpuid }
    thisa = (stru11 *)_EAX;
    v35 = (char *)_EDX;
    v6->cpuid_80000001_eax = _EAX;
    v6->cpuid_80000001_edx = (int)v35;
  }
  if ( (unsigned int)uNumExtOps >= 4 )
    RunCPUID_ext2_3_4();
  if ( (unsigned int)uNumExtOps >= 5 )
  {
    _EAX = 0x80000005u;
    __asm { cpuid }
    thisa = (stru11 *)_EBX;
    v35 = (char *)_ECX;
    v36 = _EDX;
    v6->cpuid_80000005_ebx = _EBX;
    v6->cpuid_80000005_ecx = (int)v35;
    v6->cpuid_80000005_edx = v36;
  }
  if ( (unsigned int)uNumExtOps >= 6 )
  {
    _EAX = 0x80000006u;
    __asm { cpuid }
    v6->cpuid_80000006_ecx = _ECX;
  }*/
}

//----- (00438821) --------------------------------------------------------
void stru11::RunCPUID_op1()
{
  __debugbreak();
  /*stru11 *v1; // esi@1
  unsigned int v7; // ecx@1
  int v8; // eax@3
  int v9; // eax@10
  signed int v10; // eax@11
  int v11; // ecx@11
  int v12; // eax@15
  signed int v13; // eax@19
  int v14; // ecx@19
  signed int v15; // eax@25
  int v16; // ecx@25
  signed int v17; // eax@34
  int v18; // ecx@34

  v1 = this;
  _EAX = 1;
  __asm { cpuid }
  v7 = _EAX;
  v1->cpuid_00000001_edx = _EDX;
  v1->cpuid_00000001_eax = _EAX;
  if ( (_EAX & 0x3000) == 8192 )
  {
    LOBYTE(v1->cpuid_80000002_registers2[0]) = 1;
    v7 = 0;
  }
  v8 = (v7 >> 8) & 0xF;
  switch ( v8 )
  {
    case 4:
      v1->field_AC = 2;
      break;
    case 5:
      v1->field_AC = 15;
      break;
    case 6:
      v1->field_AC = 36;
      break;
    default:
      v1->field_AC = 49;
      break;
  }
  v9 = v1->uProcessorManufacturer;
  if ( v9 == 1 )
  {
    v10 = 0;
    v11 = v7 & 0x3FF0;
    while ( v11 != dword_4E4948[2 * v10] )
    {
      ++v10;
      if ( v10 >= 17 )
        return;
    }
    v12 = dword_4E494C[2 * v10];
    goto LABEL_39;
  }
  if ( v9 == 2 )
  {
    if ( (v7 & 0xF00) == dword_4E49D0[0] )
    {
      v12 = dword_4E49D4[0];
    }
    else
    {
      v13 = 1;
      v14 = v7 & 0x3FF0;
      while ( v14 != dword_4E49D0[2 * v13] )
      {
        ++v13;
        if ( v13 >= 9 )
          return;
      }
      v12 = dword_4E49D4[2 * v13];
    }
    goto LABEL_39;
  }
  if ( v9 != 3 )
  {
    if ( v9 != 4 )
      return;
    v17 = 0;
    v18 = v7 & 0x3FF0;
    while ( v18 != dword_4E4A40[2 * v17] )
    {
      ++v17;
      if ( v17 >= 2 )
        return;
    }
    v12 = dword_4E4A44[2 * v17];
    goto LABEL_39;
  }
  v15 = 1;
  v16 = v7 & 0x3FF0;
  while ( v16 != dword_4E4A18[2 * v15] )
  {
    ++v15;
    if ( v15 >= 5 )
      goto LABEL_30;
  }
  v1->field_AC = dword_4E4A1C[2 * v15];
LABEL_30:
  v12 = 24;
  if ( v1->field_AC == 24 )
  {
    if ( _EDX != 1 )
      v12 = (((_EDX != 261) - 1) & 0xA) + 15;
LABEL_39:
    v1->field_AC = v12;
  }*/
}

//----- (00438992) --------------------------------------------------------
void stru11::RunCPUID_op2()
{
  __debugbreak();
  /*
  stru11 *v1; // edi@1
  int v7; // eax@1
  signed int v8; // ecx@2
  unsigned __int8 v9; // al@3
  signed int v10; // ecx@14
  unsigned __int8 v11; // al@15
  int v12; // [sp+Ch] [bp-14h]@1
  int v13; // [sp+10h] [bp-10h]@1
  int v14; // [sp+14h] [bp-Ch]@1
  int v15; // [sp+18h] [bp-8h]@1
  int *v16; // [sp+1Ch] [bp-4h]@1

  v1 = this;
  v16 = &v12;
  _EAX = 2;
  __asm { cpuid }
  v12 = _EAX;
  v13 = _EBX;
  v14 = _ECX;
  v15 = _EDX;
  v1->cpuid_00000002_eax = _EAX;
  v1->cpuid_00000002_ebx = v13;
  v1->cpuid_00000002_ecx = v14;
  v1->cpuid_00000002_edx = v15;
  v7 = v1->field_AC;
  if ( v7 == 40 )
  {
    v8 = 0;
    while ( 1 )
    {
      v9 = *((char *)&v12 + v8);
      if ( v9 == 64 )
      {
        v1->field_AC = 43;
        return;
      }
      if ( v9 >= 0x41u && v9 <= 0x43u )
      {
        v1->field_AC = 41;
        return;
      }
      if ( v9 >= 0x44u && v9 <= 0x45u )
        break;
      ++v8;
      if ( v8 >= 16 )
        return;
    }
    v1->field_AC = 42;
  }
  else
  {
    if ( v7 == 45 )
    {
      v10 = 0;
      while ( 1 )
      {
        v11 = *((char *)&v12 + v10);
        if ( v11 >= 0x40u && v11 <= 0x43u )
        {
          v1->field_AC = 46;
          return;
        }
        if ( v11 >= 0x44u && v11 <= 0x45u )
          break;
        ++v10;
        if ( v10 >= 16 )
          return;
      }
      v1->field_AC = 47;
    }
  }*/
}

//----- (00438A67) --------------------------------------------------------
void stru11::RunCPUID_ext2_3_4()
{
  __debugbreak();
  /*stru11 *v1; // edi@1
  stru11 *v17; // eax@1
  int v18[3][4]; // [sp+Ch] [bp-38h]@1
  stru11 *v19; // [sp+3Ch] [bp-8h]@1

  v1 = this;
  v19 = this;
  _EAX = 0x80000002u;
  __asm { cpuid }
  v18[0][0] = _EAX;
  *(_QWORD *)&v18[0][1] = __PAIR__(_ECX, _EBX);
  v18[0][3] = _EDX;
  _EAX = 0x80000003u;
  __asm { cpuid }
  v18[1][0] = _EAX;
  *(_QWORD *)&v18[1][1] = __PAIR__(_ECX, _EBX);
  v18[1][3] = _EDX;
  _EAX = 0x80000004u;
  __asm { cpuid }
  v18[2][0] = _EAX;
  *(_QWORD *)&v18[2][1] = __PAIR__(_ECX, _EBX);
  v18[2][3] = _EDX;
  v17 = v19;
  memcpy(v1->cpuid_80000002_registers, v18, 0x30u);
  memcpy((char *)&v17->cpuid_80000002_registers2[0] + 1, v18, 0x30u);*/
}

//----- (00438F8F) --------------------------------------------------------
void __cdecl area_of_effect__damage_evaluate()
{
  int v0; // edx@1
  char *v1; // esi@2
  int v2; // ecx@3
  signed int v3; // eax@3
  SpriteObject *v4; // eax@4
  signed int v5; // eax@4
  signed int v6; // eax@6
  unsigned int v7; // edi@6
  int v8; // eax@6
  __int16 v9; // bx@8
  int v10; // edi@8
  Vec3_int_ v11; // ST04_12@9
  int v12; // ST0C_4@10
  Actor *v13; // edi@11
  int v14; // edx@15
  int v15; // edx@15
  int v16; // ebx@15
  signed int v17; // eax@15
  int v18; // edi@15
  int v19; // edi@15
  Vec3_int_ v20; // ST04_12@16
  Vec3_int_ *v21; // eax@17
  unsigned int v22; // edx@17
  int v23; // edx@18
  int v24; // eax@18
  Vec3_int_ v25; // ST04_12@19
  unsigned int v26; // ebx@20
  Player *v27; // edi@20
  Actor *v28; // edi@27
  int v29; // ebx@29
  int v30; // eax@29
  int v31; // edx@29
  int v32; // eax@29
  int v33; // ST24_4@29
  Vec3_int_ v34; // ST04_12@30
  Vec3_int_ *v35; // eax@31
  SpriteObject *v36; // [sp+0h] [bp-28h]@0
  signed int v37; // [sp+4h] [bp-24h]@5
  int v38; // [sp+4h] [bp-24h]@15
  signed int v39; // [sp+8h] [bp-20h]@3
  int v40; // [sp+8h] [bp-20h]@15
  int v41; // [sp+Ch] [bp-1Ch]@5
  int v42; // [sp+Ch] [bp-1Ch]@15
  int v43; // [sp+10h] [bp-18h]@1
  int v44; // [sp+14h] [bp-14h]@15
  unsigned int uActorID; // [sp+18h] [bp-10h]@6
  Vec3_int_ *pVelocity; // [sp+1Ch] [bp-Ch]@2
  int a1; // [sp+20h] [bp-8h]@8
  int v48; // [sp+24h] [bp-4h]@8

  v0 = 0;
  v43 = 0;
  if ( stru_50FE08.count > 0 )
  {
    pVelocity = &stru_50FE08.vec_4B4;
    v1 = (char *)stru_50FE08.pZs;
    do
    {
      v2 = PID_TYPE(*((short *)v1 - 300));
      v3 = PID_ID((signed int)*((short *)v1 - 300));
      v39 = PID_ID((signed int)*((short *)v1 - 300));
      if ( v2 == 2 )
      {
        v4 = &pSpriteObjects[v3];
        v36 = v4;
        v5 = v4->spell_caster_pid;
        v2 = PID_TYPE(v5);
        v3 = PID_ID(v5);
      }
      v41 = v2;
      v37 = v3;
      if ( stru_50FE08.field_3EC[v0] & 1 )
      {
        v6 = ai_near_actors_targets_pid[v3];
        v7 = PID_ID(v6);
        v8 = PID_TYPE(v6) - 3;
        uActorID = v7;
        if ( v8 )
        {
          if ( v8 == 1 )
          {
            v9 = *(short *)v1;
            v10 = pParty->vPosition.y - *((short *)v1 - 100);
            a1 = pParty->vPosition.x - *((short *)v1 - 200);
            v48 = v10;
            if ( a1 * a1
               + v10 * v10
               + ((signed int)(pParty->vPosition.z + pParty->uPartyHeight) >> (1 - v9))
               * ((signed int)(pParty->vPosition.z + pParty->uPartyHeight) >> (1 - v9)) < (unsigned int)((*((short *)v1 + 100) + 32) * (*((short *)v1 + 100) + 32)) )
            {
              v11.z = *(short *)v1;
              v11.y = *((short *)v1 - 100);
              v11.x = *((short *)v1 - 200);
              if ( sub_407A1C(pParty->vPosition.x, pParty->vPosition.y, pParty->vPosition.z + pParty->sEyelevel, v11) )
              {
                v12 = stru_50C198.which_player_would_attack(&pActors[v39]);
                DamagePlayerFromMonster(*((short *)v1 - 300), stru_50FE08.field_450[v43], pVelocity, v12);
              }
            }
          }
        }
        else
        {
          v13 = &pActors[v7];
          if ( SHIDWORD(v13->pActorBuffs[6].uExpireTime) > 0
            || SHIDWORD(v13->pActorBuffs[6].uExpireTime) >= 0 && LODWORD(v13->pActorBuffs[6].uExpireTime)
            || v13->CanAct() )
          {
            v14 = v13->vPosition.y;
            v40 = v13->vPosition.x;
            v38 = v14;
            v15 = v14 - *((short *)v1 - 100);
            v16 = *(short *)v1;
            v42 = v13->vPosition.z;
            a1 = v40 - *((short *)v1 - 200);
            v17 = v13->uActorHeight;
            v18 = v13->uActorRadius;
            v44 = v42;
            v19 = *((short *)v1 + 100) + v18;
            v48 = v15;
            if ( a1 * a1 + v15 * v15 + (v42 + (v17 >> 1) - v16) * (v42 + (v17 >> 1) - v16) < (unsigned int)(v19 * v19) )
            {
              v20.z = *(short *)v1;
              v20.y = *((short *)v1 - 100);
              v20.x = *((short *)v1 - 200);
              if ( sub_407A1C(v40, v38, v42 + 50, v20) )
              {
                Vec3_int_::Normalize(&a1, &v48, &v44);
                v21 = pVelocity;
                v22 = uActorID;
                pVelocity->x = a1;
                v21->y = v48;
                v21->z = v44;
                sub_43B1B0(*((short *)v1 - 300), v22, v21, stru_50FE08.field_450[v43]);
              }
            }
          }
        }
      }
      else
      {
        v23 = pParty->vPosition.y - *((short *)v1 - 100);
        v24 = ((signed int)pParty->uPartyHeight >> 1) - *(short *)v1;
        a1 = pParty->vPosition.x - *((short *)v1 - 200);
        v48 = v23;
        if ( a1 * a1 + v23 * v23 + (pParty->vPosition.z + v24) * (pParty->vPosition.z + v24) < (unsigned int)((*((short *)v1 + 100) + 32) * (*((short *)v1 + 100) + 32)) )
        {
          v25.z = *(short *)v1;
          v25.y = *((short *)v1 - 100);
          v25.x = *((short *)v1 - 200);
          if ( sub_407A1C(pParty->vPosition.x, pParty->vPosition.y, pParty->vPosition.z + pParty->sEyelevel, v25) )
          {
            v26 = 0;
            v27 = pParty->pPlayers;//[0].pConditions[15];
            do
            {
				if ( !(HIDWORD(v27->pConditions[14]) | LODWORD(v27->pConditions[14])) && !v27->pConditions[15] && !v27->pConditions[16] )
                DamagePlayerFromMonster(*((short *)v1 - 300), stru_50FE08.field_450[v43], pVelocity, v26);
              ++v27;
              ++v26;
            }
			while ( v27 <= &pParty->pPlayers[3] );
          }
        }
        uActorID = 0;
        if ( (signed int)uNumActors > 0 )
        {
          v28 = pActors.data();//[0].vPosition.z;
          do
          {
            if ( v28->CanAct() )
            {
              v29 = *(short *)v1;
			  v30 = v28->vPosition.y - *((short *)v1 - 100);
			  a1 = v28->vPosition.x - *((short *)v1 - 200);
			  v31 = v28->vPosition.z;
              v48 = v30;
              v44 = v31;
			  v32 = (v28->uActorHeight >> 1) - v29;
			  v33 = v28->uActorRadius + *((short *)v1 + 100);
              if ( a1 * a1 + v48 * v48 + (v31 + v32) * (v31 + v32) < (unsigned int)(v33 * v33) )
              {
                v34.z = *(short *)v1;
                v34.y = *((short *)v1 - 100);
                v34.x = *((short *)v1 - 200);
				if ( sub_407A1C(v28->vPosition.x, v28->vPosition.y, v28->vPosition.z + 50, v34) )
                {
                  Vec3_int_::Normalize(&a1, &v48, &v44);
                  v35 = pVelocity;
                  pVelocity->x = a1;
                  v35->y = v48;
                  v35->z = v44;
                  switch ( v41 )
                  {
                    case OBJECT_Player:
                      DamageMonsterFromParty(*((short *)v1 - 300), uActorID, v35);
                      break;
                    case OBJECT_Actor:
                      if ( v36 && pActors[v37].GetActorsRelation(v28) )
                        sub_43B1B0(*((short *)v1 - 300), uActorID, pVelocity, v36->field_61);
                      break;
                    case OBJECT_Item:
                      sub_43B057(*((short *)v1 - 300), uActorID, v35);
                      break;
                  }
                }
              }
            }
            ++uActorID;
            ++v28;
          }
          while ( (signed int)uActorID < (signed int)uNumActors );
        }
      }
      ++pVelocity;
      v0 = v43 + 1;
      v1 += 2;
      ++v43;
    }
    while ( v43 < stru_50FE08.count );
  }
  stru_50FE08.count = 0;
}
// 50FE08: using guessed type stru298 stru_50FE08;

// 4D864C: using guessed type char byte_4D864C;

//----- (0043A97E) --------------------------------------------------------
void __fastcall sub_43A97E(unsigned int uLayingItemID, signed int a2)
{
  if ( PID_TYPE(a2) == OBJECT_BLVDoor)
  {
    layingitem_vel_50FDFC.x = pSpriteObjects[uLayingItemID].vVelocity.x;
    layingitem_vel_50FDFC.y = pSpriteObjects[uLayingItemID].vVelocity.y;
    layingitem_vel_50FDFC.z = pSpriteObjects[uLayingItemID].vVelocity.z;

    Vec3_int_::Normalize(&layingitem_vel_50FDFC.x, &layingitem_vel_50FDFC.y, &layingitem_vel_50FDFC.z);
    DamagePlayerFromMonster(PID(OBJECT_Item, uLayingItemID), pSpriteObjects[uLayingItemID].field_61, &layingitem_vel_50FDFC, 0xFFFFFFFFu);
  }
  else if (PID_TYPE(a2) == OBJECT_Actor)
  {
    layingitem_vel_50FDFC.x = pSpriteObjects[uLayingItemID].vVelocity.x;
    layingitem_vel_50FDFC.y = pSpriteObjects[uLayingItemID].vVelocity.y;
    layingitem_vel_50FDFC.z = pSpriteObjects[uLayingItemID].vVelocity.z;

    Vec3_int_::Normalize(&layingitem_vel_50FDFC.x, &layingitem_vel_50FDFC.y, &layingitem_vel_50FDFC.z);
    switch (PID_TYPE(pSpriteObjects[uLayingItemID].spell_caster_pid))
    {
      case OBJECT_Actor:
        sub_43B1B0(PID(OBJECT_Item, uLayingItemID), PID_ID(a2), &layingitem_vel_50FDFC, pSpriteObjects[uLayingItemID].field_61);
        break;
      case OBJECT_Player:
        DamageMonsterFromParty(PID(OBJECT_Item, uLayingItemID), PID_ID(a2), &layingitem_vel_50FDFC);
        break;
      case OBJECT_Item:
        sub_43B057(PID(OBJECT_Item, uLayingItemID), PID_ID(a2), &layingitem_vel_50FDFC);
        break;
    }
  }
}

//----- (0043AA99) --------------------------------------------------------
void __fastcall Vec3_int_::Rotate(int sDepth, int sRotY, int sRotX, Vec3_int_ v, int *outx, int *outy, int *outz)
{
 float cosf_x = cosf(3.14159265f * sRotX / 1024.0f),
       sinf_x = sinf(3.14159265f * sRotX / 1024.0f),
       cosf_y = cosf(3.14159265f * sRotY / 1024.0f),
       sinf_y = sinf(3.14159265f * sRotY / 1024.0f);

 *outx = v.x + ((unsigned __int64)(sinf_y * (signed __int64)((unsigned __int64)(cosf_x * (signed __int64)sDepth)>> 16)));
 *outy = v.y + ((unsigned __int64)(cosf_y * (signed __int64)((unsigned __int64)(cosf_x * (signed __int64)sDepth)>> 16)));
 *outz = v.z + ((unsigned __int64)(sinf_x * (signed __int64)sDepth) >> 16);
}

//----- (0043AB61) --------------------------------------------------------
void Vec3_int_::Normalize(int *x, int *y, int *z)
{
  *x *= 65536 / (integer_sqrt(*y * *y + *z * *z + *x * *x) | 1);
  *y *= 65536 / (integer_sqrt(*y * *y + *z * *z + *x * *x) | 1);
  *z *= 65536 / (integer_sqrt(*y * *y + *z * *z + *x * *x) | 1);
}

//----- (0043AE12) --------------------------------------------------------
double __fastcall sub_43AE12(signed int a1)
{
  signed int v1; // ST00_4@1
  signed int v2; // ecx@1
  double v3; // st7@1
  double result; // st7@6

  v1 = a1;
  v2 = 0;
  v3 = (double)v1;
  do
  {
    if ( v3 < flt_4E4A80[v2 + 5] )
      break;
    ++v2;
  }
  while ( v2 < 5 );
  if ( v2 <= 0 || v2 >= 5 )
  {
    if ( v2 )
      result = flt_4E4A80[4];
    else
      result = flt_4E4A80[0];
  }
  else
  {
    result = (flt_4E4A80[v2] - flt_4E4A80[v2 - 3]) * (v3 - flt_4E4A80[v2 + 4]) / (flt_4E4A80[v2 + 5] - flt_4E4A80[v2 + 4]) + flt_4E4A80[v2];
  }
  return result;
}

//----- (0043B057) --------------------------------------------------------
void __fastcall sub_43B057(unsigned int uObjID, unsigned int uActorID, Vec3_int_ *pVelocity)
{
  Actor *pActor; // esi@1
  SpriteObject *v4; // eax@3
  int v5; // ecx@3
  int v6; // eax@4
  int v7; // edi@4
  unsigned int uActorIDa; // [sp+8h] [bp-8h]@1
  unsigned int a2; // [sp+Ch] [bp-4h]@1
  int a2a; // [sp+Ch] [bp-4h]@8

  uActorIDa = uActorID;
  pActor = &pActors[uActorID];
  a2 = uObjID;
  if ( !pActors[uActorID].IsNotAlive() )
  {
    if ( PID_TYPE(a2) == OBJECT_Item)
    {
      v4 = &pSpriteObjects[PID_ID(a2)];
      v5 = v4->spell_id;
      if ( v5 )
      {
        v6 = _43AFE3_calc_spell_damage(v5, v4->spell_level, v4->spell_skill, pActor->sCurrentHP);
        v7 = stru_50C198.CalcMagicalDamageToActor(pActor, 0, v6);
        pActor->sCurrentHP -= v7;
        if ( v7 )
        {
          if ( pActor->sCurrentHP > 0 )
            Actor::AI_Stun(uActorIDa, a2, 0);
          else
            Actor::Die(uActorIDa);
          a2a = 20 * v7 / (signed int)pActor->pMonsterInfo.uHP;
          if ( 20 * v7 / (signed int)pActor->pMonsterInfo.uHP > 10 )
            a2a = 10;
          if ( !MonsterStats::BelongsToSupertype(pActor->pMonsterInfo.uID, MONSTER_SUPERTYPE_TREANT) )
          {
            pVelocity->x = (unsigned __int64)(a2a * (signed __int64)pVelocity->x) >> 16;
            pVelocity->y = (unsigned __int64)(a2a * (signed __int64)pVelocity->y) >> 16;
            pVelocity->z = (unsigned __int64)(a2a * (signed __int64)pVelocity->z) >> 16;
            pActor->vVelocity.x = 50 * LOWORD(pVelocity->x);
            pActor->vVelocity.y = 50 * LOWORD(pVelocity->y);
            pActor->vVelocity.z = 50 * LOWORD(pVelocity->z);
          }
          Actor::AddBloodsplatOnDamageOverlay(uActorIDa, 1, v7);
        }
        else
        {
          Actor::AI_Stun(uActorIDa, a2, 0);
        }
      }
    }
  }
}

//----- (0043B1B0) --------------------------------------------------------
void sub_43B1B0(signed int a1, unsigned int a2, Vec3_int_ *pVelocity, signed int a4)
{
  int v4; // ebx@1
  SpriteObject *v5; // eax@2
  bool v6; // eax@3
  Actor *v7; // esi@4
  Actor *v8; // edi@4
  char v9; // zf@5
  __int64 v10; // qax@8
  signed __int16 v11; // cx@9
  signed int v12; // ecx@20
  int v13; // ecx@22
  int v14; // edi@30
  unsigned int uActorID; // [sp+Ch] [bp-8h]@1
  signed int v17; // [sp+10h] [bp-4h]@1
  int v18; // [sp+20h] [bp+Ch]@34

  v4 = 0;
  uActorID = a2;
  v17 = a1;
  if ( PID_TYPE(a1) == OBJECT_Item)
  {
    v5 = &pSpriteObjects[PID_ID(a1)];
    v4 = v5->field_60_distance_related_prolly_lod;
    v17 = v5->spell_caster_pid;
  }
  LOWORD(v6) = PID_TYPE(v17);
  if ( v6 == OBJECT_Actor)
  {
    v7 = &pActors[a2];
    v8 = &pActors[PID_ID(v17)];
    v6 = pActors[a2].IsNotAlive();
    if ( !v6 )
    {
      v9 = v7->uAIState == 7;
      v7->uLastCharacterIDToHit = v17;
      if ( v9 )
        BYTE2(v7->uAttributes) |= 2u;
      v6 = stru_50C198._4273BB(v8, v7, v4, 0);
      if ( v6 )
      {
        v10 = (unsigned int)Actor::_43B3E0_CalcDamage(v8, a4);
        if ( (signed __int64)v8->pActorBuffs[3].uExpireTime > 0 )
        {
          v11 = v8->pActorBuffs[3].uPower;
          if ( v11 )
            v10 = (signed int)v10 / (unsigned __int16)v11;
        }
        if ( SHIDWORD(v7->pActorBuffs[5].uExpireTime) >= SHIDWORD(v10)
          && (SHIDWORD(v7->pActorBuffs[5].uExpireTime) > SHIDWORD(v10)
           || LODWORD(v7->pActorBuffs[5].uExpireTime) > HIDWORD(v10)) )
          LODWORD(v10) = 0;
        if ( a4 != HIDWORD(v10) )
        {
          if ( a4 - HIDWORD(v10) == 1 )
          {
            v12 = v8->pMonsterInfo.uAttack2Type;
            if ( SHIDWORD(v7->pActorBuffs[15].uExpireTime) >= SHIDWORD(v10)
              && (SHIDWORD(v7->pActorBuffs[15].uExpireTime) > SHIDWORD(v10)
               || LODWORD(v7->pActorBuffs[15].uExpireTime) > HIDWORD(v10)) )
              LODWORD(v10) = (signed int)v10 >> 1;
            goto LABEL_30;
          }
          if ( a4 - HIDWORD(v10) == 2 )
          {
            v13 = v7->pMonsterInfo.uSpell1ID;
          }
          else
          {
            if ( a4 - HIDWORD(v10) != 3 )
            {
              if ( a4 - HIDWORD(v10) == 4 )
                v12 = v8->pMonsterInfo.field_3C_some_special_attack;
              else
                v12 = 4;
LABEL_30:
              v14 = stru_50C198.CalcMagicalDamageToActor(v7, v12, v10);
              v7->sCurrentHP -= v14;
              if ( v14 )
              {
                if ( v7->sCurrentHP > 0 )
                  Actor::AI_Stun(uActorID, v17, 0);
                else
                  Actor::Die(uActorID);
                Actor::AggroSurroundingPeasants(uActorID, 0);
                v18 = 20 * v14 / (signed int)v7->pMonsterInfo.uHP;
                if ( 20 * v14 / (signed int)v7->pMonsterInfo.uHP > 10 )
                  v18 = 10;
                if ( !MonsterStats::BelongsToSupertype(v7->pMonsterInfo.uID, MONSTER_SUPERTYPE_TREANT) )
                {
                  pVelocity->x = (unsigned __int64)(v18 * (signed __int64)pVelocity->x) >> 16;
                  pVelocity->y = (unsigned __int64)(v18 * (signed __int64)pVelocity->y) >> 16;
                  pVelocity->z = (unsigned __int64)(v18 * (signed __int64)pVelocity->z) >> 16;
                  v7->vVelocity.x = 50 * LOWORD(pVelocity->x);
                  v7->vVelocity.y = 50 * LOWORD(pVelocity->y);
                  v7->vVelocity.z = 50 * LOWORD(pVelocity->z);
                }
                Actor::AddBloodsplatOnDamageOverlay(uActorID, 1, v14);
              }
              else
              {
                Actor::AI_Stun(uActorID, v17, 0);
              }
              return;
            }
            v13 = v7->pMonsterInfo.uSpell2ID;
          }
          v12 = LOBYTE(pSpellStats->pInfos[v13].uSchool);
          goto LABEL_30;
        }
        v12 = v8->pMonsterInfo.uAttack1Type;
        goto LABEL_30;
      }
    }
  }
}

// A750D8: using guessed type __int64 qword_A750D8;


// A750D8: using guessed type __int64 qword_A750D8;


// A750D8: using guessed type __int64 qword_A750D8;

//----- (0043F515) --------------------------------------------------------
void FindBillboardsLightLevels_BLV()
{
  for (uint i = 0; i < uNumBillboardsToDraw; ++i)
  {
    auto p = &pBillboardRenderList[i];

    if (p->field_1E & 2 || uCurrentlyLoadedLevelType == LEVEL_Indoor && !p->uIndoorSectorID)
      p->dimming_level = 0;
    else
      p->dimming_level = _43F55F_get_billboard_light_level(p, -1);
  }
}

//----- (0043F55F) --------------------------------------------------------
int __fastcall _43F55F_get_billboard_light_level(RenderBillboard *a1, int uBaseLightLevel)
{
  signed int v3; // ecx@2

  if ( uCurrentlyLoadedLevelType == LEVEL_Indoor )
  {
    v3 = pIndoor->pSectors[a1->uIndoorSectorID].uMinAmbientLightLevel;
  }
  else
  {
    if ( uBaseLightLevel == -1 )
      v3 = a1->dimming_level;
    else
      v3 = uBaseLightLevel;
  }
  return _43F5C8_get_point_light_level_with_respect_to_lights(v3, a1->uIndoorSectorID, a1->world_x, a1->world_y, a1->world_z);
}

//----- (0043F5C8) --------------------------------------------------------
int __fastcall _43F5C8_get_point_light_level_with_respect_to_lights(unsigned int uBaseLightLevel, int uSectorID, float x, float y, float z)
{
  int v5; // esi@1
  signed int v6; // edi@1
  int v8; // eax@6
  int v9; // ebx@6
  unsigned int v10; // ecx@6
  unsigned int v11; // edx@9
  unsigned int v12; // edx@11
  signed int v13; // ecx@12
  BLVLightMM7 *v16; // esi@20
  int v17; // ebx@21
  int v18; // eax@24
  int v19; // ebx@24
  unsigned int v20; // ecx@24
  int v21; // edx@25
  unsigned int v22; // edx@27
  unsigned int v23; // edx@29
  signed int v24; // ecx@30
  int v26; // ebx@35
  int v27; // eax@38
  int v28; // ebx@38
  unsigned int v29; // ecx@38
  int v30; // edx@39
  unsigned int v31; // edx@41
  unsigned int v32; // edx@43
  signed int v33; // ecx@44
  int v37; // [sp+Ch] [bp-18h]@37
  int v38; // [sp+10h] [bp-14h]@5
  int v39; // [sp+10h] [bp-14h]@23
  int v40; // [sp+10h] [bp-14h]@36
  int v42; // [sp+14h] [bp-10h]@22
  unsigned int v43; // [sp+18h] [bp-Ch]@12
  unsigned int v44; // [sp+18h] [bp-Ch]@30
  unsigned int v45; // [sp+18h] [bp-Ch]@44

  v6 = uBaseLightLevel;
  for (uint i = 0; i < pMobileLightsStack->uNumLightsActive; ++i)
  {
    auto p = pMobileLightsStack->pLights + i;

      auto distX = abs(p->vPosition.x - x);
      if ( distX <= p->uRadius)
      {
        auto distY = abs(p->vPosition.y - y);
        if ( distY <= p->uRadius)
        {
          auto distZ = abs(p->vPosition.z - z);
          if ( distZ <= p->uRadius)
          {
            v8 = distX;
            v9 = distY;
            v10 = distZ;
            if (distX < distY)
            {
              v8 = distY;
              v9 = distX;
            }
            if ( v8 < distZ )
            {
              v11 = v8;
              v8 = distZ;
              v10 = v11;
            }
            if ( v9 < (signed int)v10 )
            {
              v12 = v10;
              v10 = v9;
              v9 = v12;
            }
            v43 = ((unsigned int)(11 * v9) / 32) + (v10 / 4) + v8;
            v13 = p->uRadius;
            if ( (signed int)v43 < v13 )
              v6 += ((unsigned __int64)(30i64 * (signed int)(v43 << 16) / v13) >> 16) - 30;
          }
        }
      }
  }

  if ( uCurrentlyLoadedLevelType == LEVEL_Indoor)
  {
    auto pSector = pIndoor->pSectors + uSectorID;

    for (uint i = 0; i < pSector->uNumLights; ++i)
    {
        v16 = pIndoor->pLights + pSector->pLights[i];
        if (~v16->uAtributes & 8)
        {
          v17 = abs(v16->vPosition.x - x);
          if ( v17 <= v16->uRadius )
          {
            v42 = abs(v16->vPosition.y - y);
            if ( v42 <= v16->uRadius )
            {
              v39 = abs(v16->vPosition.z - z);
              if ( v39 <= v16->uRadius )
              {
                v18 = v17;
                v19 = v42;
                v20 = v39;
                if ( v18 < v42 )
                {
                  v21 = v18;
                  v18 = v42;
                  v19 = v21;
                }
                if ( v18 < v39 )
                {
                  v22 = v18;
                  v18 = v39;
                  v20 = v22;
                }
                if ( v19 < (signed int)v20 )
                {
                  v23 = v20;
                  v20 = v19;
                  v19 = v23;
                }
                v44 = ((unsigned int)(11 * v19) >> 5) + (v20 >> 2) + v18;
                v24 = v16->uRadius;
                if ( (signed int)v44 < v24 )
                  v6 += ((unsigned __int64)(30i64 * (signed int)(v44 << 16) / v24) >> 16) - 30;
              }
            }
          }
        }
    }
  }

  for (uint i = 0; i < pStationaryLightsStack->uNumLightsActive; ++i)
  {
    auto p = pStationaryLightsStack->pLights + i;
      v26 = abs(p->vPosition.x - x);
      if ( v26 <= p->uRadius)
      {
        v40 = abs(p->vPosition.y - y);
        if ( v40 <= p->uRadius)
        {
          v37 = abs(p->vPosition.z - z);
          if ( v37 <= p->uRadius)
          {
            v27 = v26;
            v28 = v40;
            v29 = v37;
            if ( v27 < v40 )
            {
              v30 = v27;
              v27 = v40;
              v28 = v30;
            }
            if ( v27 < v37 )
            {
              v31 = v27;
              v27 = v37;
              v29 = v31;
            }
            if ( v28 < (signed int)v29 )
            {
              v32 = v29;
              v29 = v28;
              v28 = v32;
            }
            v45 = ((unsigned int)(11 * v28) >> 5) + (v29 >> 2) + v27;
            v33 = p->uRadius;
            if ( (signed int)v45 < v33 )
              v6 += ((unsigned __int64)(30i64 * (signed int)(v45 << 16) / v33) >> 16) - 30;
          }
        }
      }
  }

  if ( v6 <= 31 )
  {
    if ( v6 < 0 )
      v6 = 0;
  }
  else
  {
    v6 = 31;
  }
  return v6;
}

//----- (00406051) --------------------------------------------------------
__int16 stru262_TurnBased::StartTurn()
{
  stru262_TurnBased *v1; // esi@1
  int v2; // ecx@1
  int v3; // ebx@1
  char *v4; // edi@1
  int v5; // eax@4
  int v6; // eax@12
  char *v7; // eax@15
  TurnBased_QueueElem *v8; // edi@17
  unsigned int v9; // edx@23
  char *v10; // eax@26
  int v11; // eax@30
  int v12; // ebx@30
  char *v13; // edi@31
  int v15; // [sp+Ch] [bp-10h]@5
  TurnBased_QueueElem *v16; // [sp+10h] [bp-Ch]@3
  int v17; // [sp+14h] [bp-8h]@2
  int v18; // [sp+14h] [bp-8h]@16
  signed int v19; // [sp+18h] [bp-4h]@1
  int v20; // [sp+18h] [bp-4h]@14

  v1 = this;
  v2 = 0;
  v19 = 0;
  v3 = v1->uActorQueueSize;
  v1->field_1C = 0;
  v4 = (char *)&v1->pQueue[v3].field_4;
  do
  {
    v17 = v2;
    if ( v1->uActorQueueSize <= v2 )
      goto LABEL_11;
    v16 = v1->pQueue;
    while ( 1 )
    {
      v5 = v16->uPackedID;
      if ( PID_TYPE(v16->uPackedID) == OBJECT_Player)
        break;
LABEL_8:
      ++v17;
      ++v16;
      if ( v17 >= v1->uActorQueueSize )
        goto LABEL_11;
    }
    v15 = PID_ID(v5);
    if ( !pPlayers[(PID_ID(v5)) + 1]->CanAct() || v19 != v15 )
    {
      v2 = 0;
      goto LABEL_8;
    }
    v2 = 0;
LABEL_11:
    if ( v17 == v1->uActorQueueSize )
    {
      *(int *)v4 = 100;
      v6 = 8 * v19;
      LOBYTE(v6) = PID(OBJECT_Player,v19);
      *((int *)v4 + 2) = v2;
      *((int *)v4 - 1) = v6;
      *((int *)v4 + 1) = v2;
      ++v3;
      v4 += 16;
    }
    ++v19;
  }
  while ( v19 < 4 );
  v1->uActorQueueSize = v3;
  v20 = v2;
  if ( ai_arrays_size > v2 )
  {
    v7 = (char *)&v1->pQueue[v3].field_4;
    do
    {
      v18 = v2;
      if ( v1->uActorQueueSize > v2 )
      {
        v8 = v1->pQueue;
        do
        {
          if ( PID_TYPE(v8->uPackedID) == OBJECT_Actor && ai_near_actors_ids[v20] == PID_ID(v8->uPackedID) )
            break;
          ++v18;
          ++v8;
        }
        while ( v18 < v1->uActorQueueSize );
        v2 = 0;
      }
      if ( v18 == v1->uActorQueueSize )
      {
        v9 = ai_near_actors_ids[v20];
        *(int *)v7 = 1;
        *((int *)v7 + 2) = v2;
        *((int *)v7 - 1) = PID(OBJECT_Actor,v9);
        *((int *)v7 + 1) = v2;
        ++v3;
        v7 += 16;
      }
      ++v20;
    }
    while ( v20 < ai_arrays_size );
  }
  ++v1->field_0;
  v1->uActorQueueSize = v3;
  v1->field_10 = 100;
  if ( v3 > 0 )
  {
    v10 = (char *)&v1->pQueue[0].field_4;
    do
    {
      if ( *(int *)v10 <= 0 )
        *(int *)v10 = 100;
      ++v2;
      v10 += 16;
    }
    while ( v2 < v1->uActorQueueSize );
  }
  LOWORD(v11) = v1->_4063A1();
  v12 = 0;
  if ( v1->uActorQueueSize > 0 )
  {
    v13 = (char *)&v1->pQueue[0].field_4;
    do
    {
      v11 = PID_TYPE(*((int *)v13 - 1));
      if ( (char)v11 == 4 )
        break;
      if ( *(int *)v13 > 0 )
        break;
      LOWORD(v11) = v1->_40680F(v12++);
      v13 += 16;
    }
    while ( v12 < v1->uActorQueueSize );
  }
  return v11;
}
// 4F75D8: using guessed type int ai_arrays_size;

//----- (004061CA) --------------------------------------------------------
void stru262_TurnBased::NextTurn()
{
  stru262_TurnBased *v1; // esi@1
  TurnBased_QueueElem *v2; // ebp@1
  int v3; // ebx@1
  int v4; // edi@7
  Actor *v5; // eax@9
  int v6; // ecx@9
  signed int v7; // ebx@17
  int v8; // ebp@27
  TurnBased_QueueElem *v9; // edi@28
  int v10; // ecx@30
  unsigned __int16 v11; // ax@30
  signed int v12; // edx@35
  signed int v13; // [sp+10h] [bp-4h]@7

  v1 = this;
  _404544();
  v2 = v1->pQueue;
  v3 = 0;
  if (PID_TYPE(v1->pQueue[0].uPackedID) == OBJECT_Player)
    uActiveCharacter = PID_ID(v1->pQueue[0].uPackedID) + 1;
  else
    uActiveCharacter = 0;
  viewparams->bRedrawGameUI = 1;
  if ( v1->field_1C )
  {
    pTurnEngine->field_18 |= 2u;
    return;
  }
  pTurnEngine->field_18 &= 0xFFFFFFFDu;
  if ( v1->pQueue[0].field_4 <= 0 )
    return;
  v4 = 0;
  v13 = 0;
  if ( v1->uActorQueueSize <= 0 )
    goto LABEL_27;
  do
  {
    if (PID_TYPE(v2->uPackedID) != OBJECT_Player)
    {
      v5 = &pActors[PID_ID(v2->uPackedID)];
      LOWORD(v6) = v5->uAIState;
      if ( (short)v6 == 4
        || (short)v6 == 8
        || (short)v6 == 2
        || (short)v6 == 3
        || (short)v6 == 12
        || (short)v6 == 13
        || (short)v6 == 18
        || (short)v6 == 17 )
      {
        v7 = v5->uCurrentActionLength;
        v5->uCurrentActionTime += pEventTimer->uTimeElapsed;
        if ( (signed int)v5->uCurrentActionTime < v7 )
        {
          v13 = 1;
LABEL_19:
          v3 = 0;
          goto LABEL_20;
        }
        v6 = (signed __int16)v6;
        if ( (signed __int16)v6 == 4 )
        {
          v3 = 0;
          v5->uAIState = Dead;
          v5->uCurrentActionTime = 0;
          v5->uCurrentActionLength = 0;
          v5->UpdateAnimation();
        }
        else
        {
          if ( v6 != 8 )
            goto LABEL_19;
          v3 = 0;
          Actor::AI_StandOrBored(PID_ID(v2->uPackedID), ai_near_actors_targets_pid[PID_ID(v2->uPackedID)], 32, 0);
        }
      }
    }
LABEL_20:
    ++v4;
    ++v2;
  }
  while ( v4 < v1->uActorQueueSize );
  if ( v13 != v3 )
  {
    v1->field_18 |= 1u;
    return;
  }
LABEL_27:
  v1->field_18 &= 0xFFFFFFFEu;
  v8 = 0;
  if ( v1->uActorQueueSize > v3 )
  {
    v9 = v1->pQueue;
    do
    {
      if (PID_TYPE(v9->uPackedID) != OBJECT_Player)
      {
        v10 = PID_ID(v9->uPackedID);
        v11 = pActors[v10].uAIState;
        if ( v11 != 5 )
        {
          if ( v11 != 4 && v11 != 11 && v11 != 19 )
          {
            if ( v11 != 17 )
            {
              v12 = ai_near_actors_targets_pid[v10];
              v9->uActionLength = v3;
              Actor::AI_StandOrBored(v10, v12, 32, (AIDirection *)v3);
            }
          }
        }
      }
      ++v8;
      ++v9;
    }
    while ( v8 < v1->uActorQueueSize );
  }
  v1->field_4 = 3;
  pParty->uTimePlayed += __PAIR__(v3, 213);
  _494035_timed_effects__water_walking_damage__etc();
  v1->uActionPointsLeft = 130;
}

//----- (004063A1) --------------------------------------------------------
int stru262_TurnBased::_4063A1()
{
  stru262_TurnBased *v1; // esi@1
  int v2; // ebp@1
  signed int result; // eax@1
  int v4; // edx@1
  int v5; // edx@3
  int v6; // edi@4
  int v7; // ecx@5
  Actor *v8; // ecx@11
  unsigned __int16 v9; // dx@12
  int v10; // edi@16
  int v11; // edx@17
  char v12; // zf@18

  v1 = this;
  _404544();
  v2 = (int)&v1->pQueue[0].field_4;
  result = 1;
  viewparams->bRedrawGameUI = 1;
  v4 = v1->pQueue[0].field_4;
  if ( v4 )
  {
    if (PID_TYPE(v1->pQueue[0].uPackedID) == OBJECT_Player)
    {
      v5 = v1->uActorQueueSize;
      while ( 1 )
      {
        v6 = 0;
        if ( v5 > 0 )
        {
          v7 = (int)&v1->pQueue[0].field_4;
          do
          {
            --*(int *)v7;
            v5 = v1->uActorQueueSize;
            ++v6;
            v7 += 16;
          }
          while ( v6 < v5 );
        }
        --v1->field_10;
        if ( v1->field_10 <= 0 )
          break;
        if ( !*(int *)v2 )
          goto LABEL_9;
      }
LABEL_24:
      result = 1;
    }
    else
    {
      v8 = &pActors[PID_ID(v1->pQueue[0].uPackedID)];
      if ( v4 > 0 )
      {
        do
        {
          v9 = v8->uAIState;
          if ( v9 == 5 )
            break;
          if ( v9 == 4 || v9 == 19 || v9 == 11 )
            break;
          v10 = 0;
          if ( v1->uActorQueueSize > 0 )
          {
            v11 = (int)&v1->pQueue[0].field_4;
            do
            {
              v12 = (*(int *)v11)-- == 1;
              if ( v12 )
                *(int *)(v11 + 4) = 0;
              ++v10;
              v11 += 16;
            }
            while ( v10 < v1->uActorQueueSize );
          }
          --v1->field_10;
          if ( v1->field_10 <= 0 )
            goto LABEL_24;
        }
        while ( *(int *)v2 > 0 );
      }
LABEL_9:
      result = 0;
    }
  }
  return result;
}

//----- (00406457) --------------------------------------------------------
int stru262_TurnBased::_406457(int a2)
{
  stru262_TurnBased *v2; // esi@1
  signed int v3; // eax@1
  signed int v4; // ecx@2
  char *v5; // edx@2
  signed int v6; // eax@2
  int result; // eax@11
  int v8; // edx@13
  int v9; // ecx@14
  char v10; // zf@15

  v2 = this;
  v3 = *(&this->field_0 + 4 * (a2 + 2));
  if ( (*((char *)&this->field_0 + 16 * PID_TYPE(a2 + 2))) == OBJECT_Player)
  {
    v4 = PID_ID(v3);
    v5 = (char *)&pParty->pTurnBasedPlayerRecoveryTimes[PID_ID(v3)];
    v6 = *(int *)v5;
    if ( *(int *)v5 )
      *(int *)v5 = 0;
    else
      v6 = pPlayers[v4 + 1]->GetAttackRecoveryTime(0);
    if ( v6 < 30 )
      v6 = 30;
  }
  else
  {
    v6 = pMonsterStats->pInfos[pActors[PID_ID(v3)].pMonsterInfo.uID].uRecoveryTime;
  }
  v2->pQueue[a2].field_4 = v6;
  v2->_404544();
  if (PID_TYPE(v2->pQueue[0].uPackedID) == OBJECT_Player)
    uActiveCharacter = PID_ID(v2->pQueue[0].uPackedID) + 1;
  else
    uActiveCharacter = 0;
  result = (int)&v2->pQueue[0].field_4;
  viewparams->bRedrawGameUI = 1;
  while ( *(int *)result > 0 )
  {
    if ( v2->field_10 <= 0 )
      break;
    v8 = 0;
    if ( v2->uActorQueueSize > 0 )
    {
      v9 = (int)&v2->pQueue[0].field_4;
      do
      {
        v10 = (*(int *)v9)-- == 1;
        if ( v10 )
          *(int *)(v9 + 4) = 0;
        ++v8;
        v9 += 16;
      }
      while ( v8 < v2->uActorQueueSize );
    }
    --v2->field_10;
    if ( v2->field_10 <= 0 )
      break;
  }
  return result;
}

//----- (0040652A) --------------------------------------------------------
void stru262_TurnBased::_40652A()
{
  int *v1; // edx@2
  Actor *v2; // eax@5
  unsigned __int16 v3; // si@5
  unsigned int v4; // esi@8
  int v5; // [sp+Ch] [bp-4h]@1

  v5 = 0;
  if ( this->uActorQueueSize > 0 )
  {
    v1 = &this->pQueue[0].field_4;
    do
    {
      if ( !*(int *)v1 )
      {
        if ( PID_TYPE(*(v1 - 4)) == OBJECT_Player)
          return;
        v2 = &pActors[PID_ID(*(v1 - 1))];
        v3 = v2->uAIState;
        if (v3 == Standing || v3 == Fleeing || v3 == Fidgeting)
        {
          v4 = pMonsterStats->pInfos[v2->pMonsterInfo.uID].uRecoveryTime;
          *(int *)v1 = v4;
          if ( (signed __int64)v2->pActorBuffs[7].uExpireTime > 0 )
            *(int *)v1 = 2 * v4;
        }
      }
      ++v5;
      v1 += 16;
    }
    while ( v5 < this->uActorQueueSize );
  }
}

//----- (004065B0) --------------------------------------------------------
void stru262_TurnBased::_4065B0()
{
  stru262_TurnBased *v1; // esi@1
  signed int v2; // eax@1
  int v3; // ebx@6
  int v4; // edi@7
  unsigned int v5; // edi@14

  v1 = this;
  LOWORD(v2) = _404544();
  if ( v1->pQueue[0].field_4 <= 0
    || ((_4063A1(), v2 = v1->pQueue[0].uPackedID, PID_TYPE(v1->pQueue[0].uPackedID) != OBJECT_Player) ? (uActiveCharacter = 0) : (v2 = (PID_ID(v2)) + 1, uActiveCharacter = v2),
        viewparams->bRedrawGameUI = 1,
        v1->pQueue[0].field_4 <= 0) )
  {
    v3 = 0;
    if ( v1->uActorQueueSize > 0 )
    {
      v4 = (int)v1->pQueue;
      do
      {
        v2 = *(int *)v4;
        if ( PID_TYPE(*(char *)v4) == OBJECT_Player || *(int *)(v4 + 4) > 0 )
          break;
        if ( *(int *)(v4 + 8) <= 0 )
        {
          LOWORD(v2) = v2 & 3;
          if ( (char)v2 == 3 )
            LOWORD(v2) = v1->_40680F(v3);
        }
        ++v3;
        v4 += 16;
      }
      while ( v3 < v1->uActorQueueSize );
    }
  }
  v5 = 0;
  if ( v1->uActorQueueSize > 0 )
  {
    do
      v1->_406648(v5++);
    while ( (signed int)v5 < v1->uActorQueueSize );
  }
}

//----- (00406648) --------------------------------------------------------
void stru262_TurnBased::_406648(unsigned int a2)
{
  TurnBased_QueueElem *v1; // ecx@1
  int v3; // eax@1
  unsigned int v4; // ebx@2
  Actor *v5; // esi@2
  signed int v6; // edi@5
  int v7; // ecx@6
  int v8; // eax@6
  int v9; // eax@7
  int v10; // eax@8
  int v11; // eax@9
  int v12; // eax@10
  int v13; // eax@11
  int v14; // eax@14
  unsigned int v15; // ecx@14
  unsigned int v16; // edx@14
  int v17; // eax@17
  int v18; // eax@20
  char v19; // al@24
  unsigned int v21; // [sp-8h] [bp-50h]@16
  int v22; // [sp-8h] [bp-50h]@17
  AIDirection *v23; // [sp-4h] [bp-4Ch]@14
  unsigned int v24; // [sp-4h] [bp-4Ch]@17
  char v25; // [sp-4h] [bp-4Ch]@20
  AIDirection a3; // [sp+Ch] [bp-3Ch]@2
  AIDirection a4; // [sp+28h] [bp-20h]@2
  TurnBased_QueueElem *v28; // [sp+44h] [bp-4h]@1
  unsigned int a2a; // [sp+50h] [bp+8h]@2

  v1 = (TurnBased_QueueElem *)((char *)this + 16 * (a2 + 2));
  v28 = v1;
  v3 = v1->uPackedID;
  if (PID_TYPE(v1->uPackedID) != OBJECT_Player)
  {
    v4 = PID_ID(v3);
    a2a = ai_near_actors_targets_pid[PID_ID(v3)];
    memcpy(&a3, Actor::GetDirectionInfo(v1->uPackedID, ai_near_actors_targets_pid[PID_ID(v3)], &a3, 0), sizeof(a3));
    memcpy(&a4, &a3, sizeof(a4));
    v5 = &pActors[PID_ID(v3)];
    LOWORD(v3) = v5->uAIState;
    if ( (short)v3 != Dead )
    {
      if ( (short)v3 != Disabled )
      {
        if ( (short)v3 != Removed )
        {
          v6 = v5->uCurrentActionLength;
          v5->uCurrentActionTime += pEventTimer->uTimeElapsed;
          if ( (signed int)v5->uCurrentActionTime >= v6 )
          {
            v7 = (signed __int16)v3;
            v8 = (signed __int16)v3 - 2;
            if ( !v8 )
            {
              v19 = stru_50C198.special_ability_use_check(&pActors[v4], v4);
              stru_50FE08.Add(
                v28->uPackedID,
                5120,
                v5->vPosition.x,
                v5->vPosition.y,
                v5->vPosition.z + ((signed int)v5->uActorHeight >> 1),
                v19,
                1);
              goto LABEL_25;
            }
            v9 = v8 - 1;
            if ( v9 )
            {
              v10 = v9 - 1;
              if ( !v10 )
                goto LABEL_21;
              v11 = v10 - 4;
              if ( !v11 )
                goto LABEL_25;
              v12 = v11 - 4;
              if ( v12 )
              {
                v13 = v12 - 1;
                if ( v13 )
                {
                  if ( v13 != Dead )
                  {
                    if ( v7 != 4 )
                    {
                      v14 = rand();
                      v15 = v4;
                      v23 = &a4;
                      v16 = a2a;
                      if ( !(v14 % 2) )
                      {
                        Actor::AI_Bored(v4, a2a, &a4);
                        return;
                      }
                      v21 = 64;
LABEL_26:
                      Actor::AI_Stand(v15, v16, v21, v23);
                      return;
                    }
LABEL_21:
                    v5->uCurrentActionTime = 0;
                    v5->uCurrentActionLength = 0;
                    v5->uAIState = Dead;
                    pActors[v4].UpdateAnimation();
                    return;
                  }
                  v24 = v5->pMonsterInfo.uSpellSkillAndMastery2;
                  v22 = 3;
                  v17 = v5->pMonsterInfo.uSpell2ID;
                }
                else
                {
                  v24 = v5->pMonsterInfo.uSpellSkillAndMastery1;
                  v22 = 2;
                  v17 = v5->pMonsterInfo.uSpell1ID;
                }
                Actor::AI_SpellAttack(v4, &a4, v17, v22, v24);
LABEL_25:
                v16 = a2a;
                v23 = &a4;
                v21 = 0;
                v15 = v4;
                goto LABEL_26;
              }
              v18 = v5->pMonsterInfo.uMissleAttack2Type;
              v25 = 1;
            }
            else
            {
              v18 = v5->pMonsterInfo.uMissleAttack1Type;
              v25 = 0;
            }
            Actor::AI_RangedAttack(v4, &a4, v18, v25);
            goto LABEL_25;
          }
        }
      }
    }
  }
}
// 50FE08: using guessed type stru298 stru_50FE08;

//----- (0040680F) --------------------------------------------------------
__int16 stru262_TurnBased::_40680F(int a2)
{
  TurnBased_QueueElem *v2; // eax@1
  unsigned int v3; // eax@1
  unsigned int v4; // edi@2
  Actor *v5; // ebx@2
  unsigned int *v6; // esi@7
  AIDirection *v7; // esi@10
  int v8; // eax@10
  unsigned int v9; // ecx@10
  signed int v10; // eax@13
  unsigned __int8 v11; // sf@19
  unsigned __int8 v12; // of@19
  int v13; // esi@29
  bool v14; // eax@29
  unsigned __int8 v15; // cl@33
  AIDirection a3; // [sp+Ch] [bp-44h]@10
  AIDirection v18; // [sp+28h] [bp-28h]@10
  int a2a; // [sp+44h] [bp-Ch]@2
  unsigned int v20; // [sp+48h] [bp-8h]@10
  TurnBased_QueueElem *v21; // [sp+4Ch] [bp-4h]@1
  signed int v22; // [sp+58h] [bp+8h]@10

  v2 = (TurnBased_QueueElem *)((char *)this + 16 * (a2 + 2));
  v21 = v2;
  v2->uActionLength = 0;
  v3 = v2->uPackedID;
  if ( (unsigned __int8)v3 & 3 )
  {
    v3 = PID_ID(v3);
    v4 = v3;
    a2a = v3;
    v5 = &pActors[v3];
    LOWORD(v3) = v5->uAIState;
    if ( (short)v3 != 5 )
    {
      if ( (short)v3 != 4 && (short)v3 != 11 && (short)v3 != 19 && (short)v3 != 17 )
      {
        v6 = (unsigned int *)&ai_near_actors_targets_pid[(int)v4];
        Actor::_SelectTarget((unsigned int)v4, &ai_near_actors_targets_pid[(int)v4], true);
        if ( v5->pMonsterInfo.uHostilityType && !*v6 )
          v5->pMonsterInfo.uHostilityType = MonsterInfo::Hostility_Friendly;
        v22 = *v6;
        v7 = Actor::GetDirectionInfo(PID(OBJECT_Actor,(int)v4), *v6, &a3, 0);
        v8 = v5->uActorRadius;
        memcpy(&a3, v7, sizeof(a3));
        memcpy(&v18, &a3, sizeof(v18));
        v9 = a3.uDistance - v8;
        v20 = a3.uDistance - v8;
        if ( ((a3.uDistance - v8) & 0x80000000u) != 0 )
        {
          v9 = 0;
          v20 = 0;
        }
        if (PID_TYPE(v22) == OBJECT_Actor)
          //v10 = (unsigned __int8)*(&byte_5C8D1A[89 * (pMonsterStats->pInfos[pActors[PID_ID(v22)].pMonsterInfo.uID].uID - 1) / 3] + (v5->pMonsterInfo.uID - 1) / 3);
          v10 = pFactionTable->relations[(pMonsterStats->pInfos[pActors[PID_ID(v22)].pMonsterInfo.uID].uID) / 3 + 1][(v5->pMonsterInfo.uID - 1) / 3 + 1];
        else
          v10 = 4;
        if ( v10 == 1 )
        {
          if ( (double)(signed int)v20 < 307.2 )
            goto LABEL_25;
        }
        else
        {
          if ( v10 == 2 )
          {
            v12 = __OFSUB__(v9, 1024);
            v11 = ((v9 - 1024) & 0x80000000u) != 0;
          }
          else
          {
            if ( v10 == 3 )
            {
              v12 = __OFSUB__(v9, 2560);
              v11 = ((v9 - 2560) & 0x80000000u) != 0;
            }
            else
            {
              if ( v10 != 4 )
                goto LABEL_26;
              v12 = __OFSUB__(v9, 5120);
              v11 = ((v9 - 5120) & 0x80000000u) != 0;
            }
          }
          if ( v11 ^ v12 )
          {
LABEL_25:
            v5->pMonsterInfo.uHostilityType = MonsterInfo::Hostility_Long;
            goto LABEL_26;
          }
        }
LABEL_26:
        if ( v5->pMonsterInfo.uHostilityType != 4 || !v22 || (signed int)v9 >= 5120 )
        {
          v13 = a2a;
          goto LABEL_47;
        }
        v13 = a2a;
        v14 = stru_50C198.special_ability_use_check(v5, a2a);
        if ( v14 == 1 )
        {
          if ( v5->pMonsterInfo.uMissleAttack2Type )
          {
            Actor::AI_MissileAttack2(v13, v22, &v18);
            goto LABEL_43;
          }
        }
        else
        {
          if ( v14 > 1 && v14 <= 3 )
          {
            if ( v14 == 2 )
              v15 = v5->pMonsterInfo.uSpell1ID;
            else
              v15 = v5->pMonsterInfo.uSpell2ID;
            if ( v15 )
            {
              if ( v14 == 2 )
                Actor::AI_SpellAttack1(v13, v22, &v18);
              else
                Actor::AI_SpellAttack2(v13, v22, &v18);
              goto LABEL_43;
            }
            goto LABEL_44;
          }
          if ( v5->pMonsterInfo.uMissleAttack1Type )
          {
            Actor::AI_MissileAttack1(v13, v22, &v18);
LABEL_43:
            //v3 = v21;
            v21->field_C = 1;
LABEL_48:
            v21->uActionLength = v5->uCurrentActionLength;
            //return (signed __int16)v3;
			return (signed __int16)&v21;
          }
        }
LABEL_44:
        if ( (double)(signed int)v20 < 307.2 )
        {
          Actor::AI_MeleeAttack(v13, v22, &v18);
          //v3 = v21;
          v21->field_C = 3;
          goto LABEL_48;
        }
LABEL_47:
        Actor::AI_Stand(v13, v22, 0x40u, &v18);
        //v3 = v21;
        v21->field_C = 0;
        goto LABEL_48;
      }
    }
  }
  return (signed __int16)v3;
}

//----- (00406A63) --------------------------------------------------------
void stru262_TurnBased::_406A63()
{
  stru262_TurnBased *v1; // ebx@1
  int v2; // esi@1
  unsigned __int8 v3; // zf@1
  unsigned __int8 v4; // sf@1
  signed int v5; // ecx@4
  AIDirection a3; // [sp+8h] [bp-44h]@5
  AIDirection v7; // [sp+24h] [bp-28h]@5
  unsigned int v8; // [sp+40h] [bp-Ch]@5
  signed int a2; // [sp+44h] [bp-8h]@1
  TurnBased_QueueElem *v10; // [sp+48h] [bp-4h]@2

  v1 = this;
  v2 = 0;
  this->field_8 = 64;
  dword_50C994 = 0;
  uActiveCharacter = 0;
  v3 = this->uActorQueueSize == 0;
  v4 = this->uActorQueueSize < 0;
  a2 = 0;
  if ( !(v4 | v3) )
  {
    v10 = this->pQueue;
    while ( 1 )
    {
      v5 = v10->uPackedID;
      if (PID_TYPE(v10->uPackedID) == OBJECT_Actor)
      {
        v8 = ai_near_actors_targets_pid[PID_ID(v5)];
        memcpy(&v7, Actor::GetDirectionInfo(v5, v8, &a3, v2), sizeof(v7));
        if ( !v1->_406D10(a2) )
          Actor::AI_Stand(PID_ID(v10->uPackedID), v8, 0x20u, &v7);
      }
      ++a2;
      ++v10;
      if ( a2 >= v1->uActorQueueSize )
        break;
      v2 = 0;
    }
  }
}
// 50C994: using guessed type int dword_50C994;

//----- (00406AFE) --------------------------------------------------------
void stru262_TurnBased::_406AFE()
{
  stru262_TurnBased *v1; // edi@1
  TurnBased_QueueElem *v2; // ebx@2
  int v3; // esi@4
  int v4; // ecx@5
  AIDirection a3; // [sp+4h] [bp-48h]@5
  AIDirection v6; // [sp+20h] [bp-2Ch]@5
  stru262_TurnBased *v7; // [sp+3Ch] [bp-10h]@1
  unsigned int v8; // [sp+40h] [bp-Ch]@4
  unsigned int v9; // [sp+44h] [bp-8h]@5
  int v10; // [sp+48h] [bp-4h]@1

  v10 = 0;
  v1 = this;
  v7 = this;
  if ( this->uActorQueueSize > 0 )
  {
    v2 = this->pQueue;
    do
    {
      if (PID_TYPE(v2->uPackedID) == OBJECT_Actor)
      {
        v3 = PID_ID(v2->uPackedID);
        v8 = v3;
        if ( pActors[v3].CanAct() )
        {
          v4 = v2->uPackedID;
          v9 = ai_near_actors_targets_pid[v3];
          memcpy(&v6, Actor::GetDirectionInfo(v4, v9, &a3, 0), sizeof(v6));
          Actor::AI_Stand(v8, v9, 0x20u, &v6);
          v2->field_C = 0;
          v2->uActionLength = 0;
          v1 = v7;
        }
      }
      ++v10;
      ++v2;
    }
    while ( v10 < v1->uActorQueueSize );
  }
  v1->field_4 = 2;
  v1->field_8 = 100;
}

//----- (00406B9F) --------------------------------------------------------
int stru262_TurnBased::_406B9F()
{
  signed int result; // eax@1
  unsigned __int8 v2; // zf@1
  unsigned __int8 v3; // sf@1
  int v4; // esi@4
  unsigned int v5; // esi@5
  Actor *v6; // ebx@5
  unsigned __int16 v7; // cx@15
  AIDirection a3; // [sp+0h] [bp-50h]@15
  AIDirection v9; // [sp+1Ch] [bp-34h]@15
  signed int a1; // [sp+38h] [bp-18h]@4
  stru262_TurnBased *thisa; // [sp+3Ch] [bp-14h]@1
  unsigned int v12; // [sp+40h] [bp-10h]@5
  unsigned int v13; // [sp+44h] [bp-Ch]@8
  TurnBased_QueueElem *v14; // [sp+48h] [bp-8h]@2
  signed int a2; // [sp+4Ch] [bp-4h]@1

  result = 0;
  thisa = this;
  v2 = this->uActorQueueSize == 0;
  v3 = this->uActorQueueSize < 0;
  a2 = 0;
  if ( !(v3 | v2) )
  {
    v14 = this->pQueue;
    while ( 1 )
    {
      v4 = v14->uPackedID;
      a1 = v14->uPackedID;
      if (PID_TYPE(a1) != OBJECT_Player)
      {
        v5 = PID_ID(v4);
        v12 = v5;
        v6 = &pActors[v5];
        if ( SHIDWORD(v6->pActorBuffs[5].uExpireTime) < result
          || SHIDWORD(v6->pActorBuffs[5].uExpireTime) <= result && LODWORD(v6->pActorBuffs[5].uExpireTime) <= result )
          v13 = result;
        else
          v13 = 1;
        if ( SHIDWORD(v6->pActorBuffs[6].uExpireTime) >= result
          && (SHIDWORD(v6->pActorBuffs[6].uExpireTime) > result || LODWORD(v6->pActorBuffs[6].uExpireTime) > result) )
          result = 1;
        if ( !(v13 != 0 || result || v6->uAIState == 5 || v6->uAIState == 11 || v6->uAIState == 19) )
        {
          v13 = ai_near_actors_targets_pid[v5];
          memcpy(&v9, Actor::GetDirectionInfo(a1, v13, &a3, 0), sizeof(v9));
          v7 = v6->uAIState;
          if ( v7 == 6 || v7 == 1 )
          {
            if ( (double)(signed int)v9.uDistance < 307.2 )
              goto LABEL_26;
          }
          v6->uCurrentActionTime += pEventTimer->uTimeElapsed;
          if ( (signed int)v6->uCurrentActionTime > v6->uCurrentActionLength )
          {
            if ( v7 == 4 )
            {
              v6->uCurrentActionTime = 0;
              v6->uCurrentActionLength = 0;
              v6->uAIState = Dead;
              v6->UpdateAnimation();
            }
            if ( !thisa->_406D10(a2) )
LABEL_26:
              Actor::AI_Stand(v12, v13, 0x20u, &v9);
          }
        }
      }
      ++a2;
      result = (signed int)thisa;
      ++v14;
      if ( a2 >= thisa->uActorQueueSize )
        break;
      result = 0;
    }
  }
  return result;
}

//----- (00406D10) --------------------------------------------------------
bool stru262_TurnBased::_406D10(signed int a2)
{
  int v2; // ecx@1
  //int v3; // ecx@2
  Actor *actor; // ebx@2
  //unsigned __int16 v5; // dx@2
  int *v6; // esi@7
  TurnBased_QueueElem *v7; // edi@7
  int v8; // eax@7
  AIDirection *v9; // esi@10
  int v10; // eax@10
  unsigned int v11; // ecx@10
  unsigned __int8 pHostileType; // al@12
  unsigned __int8 v13; // sf@16
  unsigned __int8 v14; // of@16
  unsigned int v15; // edx@22
  unsigned int v16; // ecx@23
  TurnBased_QueueElem *v17; // eax@25
  double v18; // st7@33
  double v19; // st6@33
  int v21; // [sp-8h] [bp-5Ch]@23
  int v22; // [sp-8h] [bp-5Ch]@26
  AIDirection *v23; // [sp-4h] [bp-58h]@23
  int v24; // [sp-4h] [bp-58h]@26
  AIDirection a3; // [sp+Ch] [bp-48h]@10
  AIDirection pDir; // [sp+28h] [bp-2Ch]@10
  int v27; // [sp+44h] [bp-10h]@33
  unsigned int v28; // [sp+48h] [bp-Ch]@10
  TurnBased_QueueElem *v29; // [sp+4Ch] [bp-8h]@7
  unsigned int uActorID; // [sp+50h] [bp-4h]@2
  unsigned int a2a; // [sp+5Ch] [bp+8h]@7

  __debugbreak();//      
  v2 = *(&this->field_0 + 4 * (a2 + 2));
  if (PID_TYPE(v2) == OBJECT_Player)
    return 0;
  uActorID = PID_ID(v2);
  //uActorID = v3;
  actor = &pActors[uActorID];
  //v5 = v4->uAIState;
  if ( actor->uAIState == 5 || actor->uAIState == 4 || actor->uAIState == 11 || actor->uAIState == 19 || actor->uAIState == 17 )
    return 1;
  v6 = &ai_near_actors_targets_pid[uActorID];
  v7 = &pTurnEngine->pQueue[a2];
  v8 = *v6;
  v29 = &pTurnEngine->pQueue[a2];
  a2a = v8;
  Actor::_SelectTarget(uActorID, &ai_near_actors_targets_pid[uActorID], true);
  if ( actor->pMonsterInfo.uHostilityType && !*v6 )
    actor->pMonsterInfo.uHostilityType = MonsterInfo::Hostility_Friendly;
  v9 = Actor::GetDirectionInfo(v7->uPackedID, a2a, &a3, 0);
  v10 = actor->uActorRadius;
  memcpy(&a3, v9, sizeof(a3));
  memcpy(&pDir, &a3, sizeof(pDir));
  v11 = a3.uDistance - v10;
  v28 = a3.uDistance - v10;
  if ( ((a3.uDistance - v10) & 0x80000000u) != 0 )
  {
    v11 = 0;
    v28 = 0;
  }
  pHostileType = actor->pMonsterInfo.uHostilityType;
  if ( pHostileType == 1 )
  {
    if ( (double)(signed int)v28 >= 307.2 )
      goto LABEL_21;
    actor->pMonsterInfo.uHostilityType = MonsterInfo::Hostility_Long;
    goto LABEL_21;
  }
  if ( pHostileType == 2 )
  {
    v14 = __OFSUB__(v11, 1024);
    v13 = ((v11 - 1024) & 0x80000000u) != 0;
  }
  else
  {
    if ( pHostileType != 3 )
      goto LABEL_21;
    v14 = __OFSUB__(v11, 2560);
    v13 = ((v11 - 2560) & 0x80000000u) != 0;
  }
  if ( v13 ^ v14 )
  {
    actor->pMonsterInfo.uHostilityType = MonsterInfo::Hostility_Long;
  }
LABEL_21:
  if ( (signed __int64)actor->pActorBuffs[4].uExpireTime > 0 )
  {
    if ( (signed int)v11 < 10240 )
    {
      Actor::AI_Flee(uActorID, a2a, 0, &pDir);
      v29->field_C = 4;
      v29->uActionLength = actor->uCurrentActionLength;
      return 1;
    }
    Actor::AI_4032B2(uActorID, a2a, 1024, 0);
    v29->field_C = 2;
    v29->uActionLength = actor->uCurrentActionLength;
    return 1;
  }
  if ( actor->pMonsterInfo.uHostilityType != 4 )
    goto LABEL_46;
  if ( !(actor->uAttributes & 0x020000) || actor->pMonsterInfo.uAIType == 1 )
  {
    if ( actor->pMonsterInfo.uAIType == 1 )
    {
      if ( actor->pMonsterInfo.uMovementType == 5 )
      {
        Actor::AI_Stand(uActorID, a2a, 32, 0);
        v29->field_C = 4;
        v29->uActionLength = actor->uCurrentActionLength;
        return 1;
      }
      Actor::AI_Flee(uActorID, a2a, 32, 0);
      v29->field_C = 4;
      v29->uActionLength = actor->uCurrentActionLength;
      return 1;
    }
    if ( actor->pMonsterInfo.uAIType == 2 )
    {
      v27 = actor->sCurrentHP;
      v18 = (double)v27;
      v19 = (double)(signed int)actor->pMonsterInfo.uHP * 0.2;
      if ( v19 > v18 && (signed int)v11 < 10240 )
      {
        if ( actor->pMonsterInfo.uMovementType == 5 )
        {
          Actor::AI_Stand(uActorID, a2a, 32, 0);
          v29->field_C = 4;
          v29->uActionLength = actor->uCurrentActionLength;
          return 1;
        }
        Actor::AI_Flee(uActorID, a2a, 32, 0);
        v29->field_C = 4;
        v29->uActionLength = actor->uCurrentActionLength;
        return 1;
      }
      goto LABEL_39;
    }
    if ( actor->pMonsterInfo.uAIType == 3 )
    {
      v27 = actor->sCurrentHP;
      v18 = (double)v27;
      v19 = (double)(signed int)actor->pMonsterInfo.uHP * 0.1;
      if ( v19 > v18 && (signed int)v11 < 10240 )
      {
        if ( actor->pMonsterInfo.uMovementType == 5 )
        {
          Actor::AI_Stand(uActorID, a2a, 32, 0);
          v29->field_C = 4;
          v29->uActionLength = actor->uCurrentActionLength;
          return 1;
        }
        Actor::AI_Flee(uActorID, a2a, 32, 0);
        v29->field_C = 4;
        v29->uActionLength = actor->uCurrentActionLength;
        return 1;
      }
      goto LABEL_39;
    }
  }
LABEL_39:
  if ( (double)(signed int)v28 < 307.2 )
    return 0;
  if ( (signed int)v11 < 5120 )
  {
    if ( actor->pMonsterInfo.uMissleAttack1Type && (signed int)v11 < 1024 )
      Actor::AI_Pursue1(uActorID, a2a, uActorID, 32, &pDir);
    else
      Actor::AI_Pursue2(uActorID, a2a, 32, &pDir, 307);
    v29->field_C = 2;
    v29->uActionLength = actor->uCurrentActionLength;
     return 1;
  }
LABEL_46:
  if ( !actor->pMonsterInfo.uMovementType )
  {
    Actor::AI_4032B2(uActorID, a2a, 1024, 32);
    v29->field_C = 2;
    v29->uActionLength = actor->uCurrentActionLength;
    return 1;
  }
  if ( actor->pMonsterInfo.uMovementType == 1 )
  {
    Actor::AI_4032B2(uActorID, a2a, 2560, 32);
    v29->field_C = 2;
    v29->uActionLength = actor->uCurrentActionLength;
    return 1;
  }
  if ( actor->pMonsterInfo.uMovementType == 2 )
  {
    Actor::AI_4032B2(uActorID, a2a, 5120, 32);
    v29->field_C = 2;
    v29->uActionLength = actor->uCurrentActionLength;
    return 1;
  }
  if ( actor->pMonsterInfo.uMovementType == 4 )
  {
    Actor::AI_4032B2(uActorID, a2a, 10240, 32);
    v29->field_C = 2;
    v29->uActionLength = actor->uCurrentActionLength;
    return 1;
  }
  if ( actor->pMonsterInfo.uMovementType == 5 )
  {
    Actor::AI_Stand(uActorID, a2a, 32, 0);
    v29->field_C = 2;
    v29->uActionLength = actor->uCurrentActionLength;
    return 1;
  }
  return 1;
}

//----- (00406FA8) --------------------------------------------------------
void stru262_TurnBased::_406FA8()
{
  unsigned __int8 v1; // zf@1
  unsigned __int8 v2; // sf@1
  TurnBased_QueueElem *v3; // edi@2
  int v4; // eax@4
  Actor *v5; // ebx@4
  unsigned __int16 v6; // cx@4
  unsigned int *v7; // edx@8
  unsigned int v8; // esi@8
  unsigned __int8 v9; // of@13
  AIDirection a3; // [sp+Ch] [bp-6Ch]@8
  AIDirection v11; // [sp+28h] [bp-50h]@8
  AIDirection a4; // [sp+44h] [bp-34h]@8
  stru262_TurnBased *v13; // [sp+60h] [bp-18h]@1
  TurnBased_QueueElem *v14; // [sp+64h] [bp-14h]@2
  int uActorID; // [sp+68h] [bp-10h]@4
  unsigned int a2; // [sp+6Ch] [bp-Ch]@8
  int v17; // [sp+70h] [bp-8h]@1
  unsigned int v18; // [sp+74h] [bp-4h]@2

  v17 = 0;
  v1 = this->uActorQueueSize == 0;
  v2 = this->uActorQueueSize < 0;
  v13 = this;
  if ( !(v2 | v1) )
  {
    v3 = this->pQueue;
    v18 = (char *)&pTurnEngine - (char *)this;
    v14 = this->pQueue;
    do
    {
      if (PID_TYPE(v3->uPackedID) != OBJECT_Player)
      {
        v4 = PID_ID(v3->uPackedID);
        uActorID = v4;
        v5 = &pActors[v4];
        v6 = v5->uAIState;
        if ( v6 != 5 )
        {
          if ( v6 != 11 )
          {
            if ( v6 != 19 )
            {
              if ( v6 != 17 )
              {
                v7 = (unsigned int *)&ai_near_actors_targets_pid[v4];
                a2 = *v7;
                v8 = a2;
                Actor::_SelectTarget(v4, (int *)v7, true);
                memcpy(&v11, Actor::GetDirectionInfo(*(int *)((char *)&v3->uPackedID + v18), v8, &a3, 0), sizeof(v11));
                memcpy(&a4, &v11, sizeof(a4));
                v5->uCurrentActionTime += pMiscTimer->uTimeElapsed;
                if ( (signed int)v5->uCurrentActionTime >= v5->uCurrentActionLength )
                {
                  if (v5->uAIState == Dying)
                  {
                    v5->uCurrentActionTime = 0;
                    v5->uCurrentActionLength = 0;
                    v5->uAIState = Dead;
                    v5->UpdateAnimation();
                    return;
                  }
                  if ( rand() % 2 )
                    Actor::AI_Stand(uActorID, a2, 0x40u, &a4);
                  else
                    Actor::AI_Bored(uActorID, a2, &a4);
                }
              }
            }
          }
        }
      }
      ++v17;
      v3 = v14 + 1;
      v9 = __OFSUB__(v17, v13->uActorQueueSize);
      v2 = v17 - v13->uActorQueueSize < 0;
      ++v14;
    }
    while ( v2 ^ v9 );
  }
}

//----- (004070EF) --------------------------------------------------------
bool __fastcall sub_4070EF_prolly_collide_objects(unsigned int uObjID, unsigned int uObj2ID)
{
  signed int v2; // eax@1
  unsigned int v3; // ecx@1
  signed int v4; // esi@1
  int v5; // ecx@2
  signed int v6; // eax@4
  int v7; // eax@4
  float v8; // ST24_4@5
  double v9; // ST18_8@5
  signed int v10; // eax@6
  int v11; // ecx@6
  signed int v12; // eax@7
  int v13; // esi@7
  int v14; // esi@8
  int v15; // esi@9
  signed int v16; // eax@11
  int v17; // edi@11
  int v18; // esi@11
  int v19; // eax@13
  float v20; // ST24_4@14
  double v21; // ST18_8@14
  signed int v22; // eax@15
  int v23; // ebx@16
  signed int v24; // ecx@16
  int v25; // eax@18
  int v26; // eax@28
  BLVSector *v27; // edx@31
  int v28; // ecx@31
  BLVFace *v29; // ebx@32
  Vec3_short_ *v30; // esi@32
  int v31; // eax@32
  int v32; // ST50_4@44
  int v33; // ST54_4@44
  int v34; // eax@44
  char v35; // zf@44
  int v36; // edi@44
  int v37; // eax@45
  signed int v38; // esi@45
  int v39; // ST4C_4@49
  signed __int64 v40; // qtt@50
  __int16 v42; // bx@58
  int v43; // [sp-8h] [bp-70h]@11
  int v44; // [sp-4h] [bp-6Ch]@11
  int v45; // [sp+Ch] [bp-5Ch]@32
  __int16 v46; // [sp+10h] [bp-58h]@32
  int v47; // [sp+18h] [bp-50h]@20
  int v48; // [sp+1Ch] [bp-4Ch]@20
  int v49; // [sp+20h] [bp-48h]@20
  int v50; // [sp+24h] [bp-44h]@16
  signed int v51; // [sp+24h] [bp-44h]@27
  signed int v52; // [sp+28h] [bp-40h]@26
  signed int v53; // [sp+2Ch] [bp-3Ch]@23
  signed int v54; // [sp+30h] [bp-38h]@22
  signed int v55; // [sp+34h] [bp-34h]@21
  signed int v56; // [sp+38h] [bp-30h]@20
  signed int v57; // [sp+3Ch] [bp-2Ch]@28
  int v58; // [sp+44h] [bp-24h]@50
  int v59; // [sp+48h] [bp-20h]@44
  int v60; // [sp+50h] [bp-18h]@11
  signed int v61; // [sp+50h] [bp-18h]@31
  int v62; // [sp+54h] [bp-14h]@16
  int v63; // [sp+58h] [bp-10h]@4
  int v64; // [sp+5Ch] [bp-Ch]@4
  int v65; // [sp+60h] [bp-8h]@4
  int v66; // [sp+64h] [bp-4h]@7

  v2 = PID_ID(uObjID);
  v3 = PID_TYPE(uObjID) - 2;
  v4 = uObj2ID;
  if ( v3 )
  {
    v5 = v3 - 1;
    if ( v5 )
    {
      if ( v5 != 2 )
        return 0;
      v6 = v2;
      v63 = pLevelDecorations[v6].vPosition.x;
      v64 = pLevelDecorations[v6].vPosition.y;
      v65 = pLevelDecorations[v6].vPosition.z;
      v7 = pIndoor->GetSector(v63, v64, v65);
    }
    else
    {
      v63 = pActors[v2].vPosition.x;
      v64 = pActors[v2].vPosition.y;
      v8 = (double)pActors[v2].uActorHeight * 0.69999999;
      v9 = v8 + 6.7553994e15;
      v65 = LODWORD(v9) + pActors[v2].vPosition.z;
      v7 = pActors[v2].uSectorID;
    }
  }
  else
  {
    v10 = v2;
    v63 = pSpriteObjects[v10].vPosition.x;
    v64 = pSpriteObjects[v10].vPosition.y;
    v11 = pSpriteObjects[v10].vPosition.z;
    v7 = pSpriteObjects[v10].uSectorID;
    v65 = v11;
  }
  v66 = v7;
  v12 = PID_ID(v4);
  v13 = PID_TYPE(v4) - 2;
  if ( v13 )
  {
    v14 = v13 - 1;
    if ( v14 )
    {
      v15 = v14 - 1;
      if ( v15 )
      {
        if ( v15 != 1 )
          return 0;
        v16 = v12;
        v17 = pLevelDecorations[v16].vPosition.z;
        v18 = pLevelDecorations[v16].vPosition.x;
        v44 = pLevelDecorations[v16].vPosition.z;
        v60 = pLevelDecorations[v16].vPosition.y;
        v43 = pLevelDecorations[v16].vPosition.y;
      }
      else
      {
        v18 = pParty->vPosition.x;
        v17 = pParty->sEyelevel + pParty->vPosition.z;
        v60 = pParty->vPosition.y;
        v44 = pParty->sEyelevel + pParty->vPosition.z;
        v43 = pParty->vPosition.y;
      }
      v19 = pIndoor->GetSector(v18, v43, v44);
    }
    else
    {
      v60 = pActors[v12].vPosition.y;
      v18 = pActors[v12].vPosition.x;
      v20 = (double)pActors[v12].uActorHeight * 0.69999999;
      v21 = v20 + 6.7553994e15;
      v17 = LODWORD(v21) + pActors[v12].vPosition.z;
      v19 = pActors[v12].uSectorID;
    }
  }
  else
  {
    v22 = v12;
    v18 = pSpriteObjects[v22].vPosition.x;
    v17 = pSpriteObjects[v22].vPosition.z;
    v60 = pSpriteObjects[v22].vPosition.y;
    v19 = pSpriteObjects[v22].uSectorID;
  }
  v62 = v19;
  v23 = v18 - v63;
  v50 = v17 - v65;
  v24 = integer_sqrt(v23 * v23 + (v60 - v64) * (v60 - v64) + v50 * v50);
  if ( v24 > 5120 )
    return 0;
  if ( uCurrentlyLoadedLevelType == LEVEL_Outdoor)
    return 1;
  v25 = 65536;
  if ( v24 )
    v25 = 65536 / v24;
  v49 = v23 * v25;
  v47 = v50 * v25;
  v48 = (v60 - v64) * v25;
  v56 = v63;
  if ( v63 < v18 )
  {
    v55 = v18;
  }
  else
  {
    v56 = v18;
    v55 = v63;
  }
  v54 = v64;
  if ( v64 < v60 )
  {
    v53 = v60;
  }
  else
  {
    v54 = v60;
    v53 = v64;
  }
  v52 = v65;
  if ( v65 < v17 )
  {
    v51 = v17;
  }
  else
  {
    v52 = v17;
    v51 = v65;
  }
  v57 = 0;
  v26 = v66;
  while ( 2 )
  {
    if ( v26 == v62 )
      return 1;
    if ( v57 >= 30 || (v27 = pIndoor->pSectors, v28 = v66, v61 = 0, pIndoor->pSectors[v66].uNumPortals <= 0) )
      break;
    while ( 1 )
    {
      v29 = &pIndoor->pFaces[v27[v28].pPortals[v61]];
      v30 = &pIndoor->pVertices[*v29->pVertexIDs];
      v45 = *(int *)&v30->x;
      v46 = v30->z;
      v31 = v29->pFacePlane_old.vNormal.z * (v46 - v65)
          + v29->pFacePlane_old.vNormal.y * (SHIWORD(v45) - v64)
          + v29->pFacePlane_old.vNormal.x * ((signed __int16)v45 - v63);
      if ( v66 != v29->uSectorID )
        v31 = -v31;
      if ( v31 >= 0 && (signed __int16)v45 != v63 && SHIWORD(v45) != v64 && v46 != v65
        || v56 > v29->pBounding.x2
        || v55 < v29->pBounding.x1
        || v54 > v29->pBounding.y2
        || v53 < v29->pBounding.y1
        || v52 > v29->pBounding.z2
        || v51 < v29->pBounding.z1 )
        goto LABEL_54;
      v32 = (unsigned __int64)(v49 * (signed __int64)v29->pFacePlane_old.vNormal.x) >> 16;
      v33 = (unsigned __int64)(v47 * (signed __int64)v29->pFacePlane_old.vNormal.z) >> 16;
      v34 = (unsigned __int64)(v48 * (signed __int64)v29->pFacePlane_old.vNormal.y) >> 16;
      v35 = v32 + v33 + v34 == 0;
      v36 = v32 + v33 + v34;
      v59 = v32 + v33 + v34;
      if ( v35 )
        goto LABEL_53;
      v37 = v65 * v29->pFacePlane_old.vNormal.z;
      v38 = -(v29->pFacePlane_old.dist + v37 + v63 * v29->pFacePlane_old.vNormal.x + v64 * v29->pFacePlane_old.vNormal.y);
      if ( v36 <= 0 )
        break;
      if ( v29->pFacePlane_old.dist + v37 + v63 * v29->pFacePlane_old.vNormal.x + v64 * v29->pFacePlane_old.vNormal.y <= 0 )
        goto LABEL_49;
LABEL_53:
      v27 = pIndoor->pSectors;
LABEL_54:
      ++v61;
      if ( v61 >= v27[v28].uNumPortals )
        goto LABEL_55;
    }
    if ( v29->pFacePlane_old.dist + v37 + v63 * v29->pFacePlane_old.vNormal.x + v64 * v29->pFacePlane_old.vNormal.y < 0 )
      goto LABEL_53;
LABEL_49:
    v39 = abs(-(v29->pFacePlane_old.dist
              + v37
              + v63 * v29->pFacePlane_old.vNormal.x
              + v64 * v29->pFacePlane_old.vNormal.y)) >> 14;
    if ( v39 > abs(v36)
      || (LODWORD(v40) = v38 << 16, HIDWORD(v40) = v38 >> 16, v58 = v40 / v59, (signed int)(v40 / v59) < 0)
      || !sub_4075DB(
            v63 + ((signed int)(((unsigned __int64)(v58 * (signed __int64)v49) >> 16) + 32768) >> 16),
            v64 + ((signed int)(((unsigned __int64)(v58 * (signed __int64)v48) >> 16) + 32768) >> 16),
            v65 + ((signed int)(((unsigned __int64)(v58 * (signed __int64)v47) >> 16) + 32768) >> 16),
            v29) )
    {
      v28 = v66;
      goto LABEL_53;
    }
    if ( v29->uSectorID == v66 )
      v42 = v29->uBackSectorID;
    else
      v42 = v29->uSectorID;
    v26 = v42;
    if ( v42 != v66 )
    {
      ++v57;
      v66 = v42;
      continue;
    }
    break;
  }
LABEL_55:
  if ( v66 != v62 )
    return 0;
  return 1;
}

//----- (004075DB) --------------------------------------------------------
bool __fastcall sub_4075DB(int a1, int a2, int a3, BLVFace *a4)
{
  BLVFace *v4; // eax@1
  unsigned int v5; // esi@1
  signed int v6; // esi@2
  char v7; // zf@2
  int v8; // edi@2
  Vec3_short_ *v9; // ecx@3
  int v10; // edx@4
  signed int v11; // esi@7
  Vec3_short_ *v12; // ecx@8
  int v13; // edx@9
  signed int v14; // esi@11
  Vec3_short_ *v15; // ecx@12
  int v16; // edx@13
  int v17; // eax@14
  bool v18; // esi@14
  int v19; // ecx@14
  int v20; // ecx@16
  int v21; // edx@16
  int v22; // eax@17
  signed int v23; // ebx@18
  int v24; // esi@20
  signed int v25; // eax@22
  signed __int64 v26; // qtt@22
  bool result; // eax@25
  int v28; // [sp+8h] [bp-14h]@14
  int v29; // [sp+10h] [bp-Ch]@14
  signed int a3a; // [sp+24h] [bp+8h]@14
  int a4a; // [sp+28h] [bp+Ch]@2

  v4 = a4;
  v5 = a4->uAttributes;
  if ( v5 & 0x100 )
  {
    v6 = 0;
    v7 = a4->uNumVertices == 0;
    a4a = a1;
    v8 = a2;
    if ( !v7 )
    {
      v9 = pIndoor->pVertices;
      do
      {
        dword_4F5D98_xs[v6] = v9[v4->pVertexIDs[v6]].x;
        v10 = v4->pVertexIDs[v6++];
        dword_4F5CC4_ys[v6] = v9[v10].y;
      }
      while ( v6 < v4->uNumVertices );
    }
  }
  else
  {
    v8 = a3;
    if ( v5 & 0x200 )
    {
      v11 = 0;
      v7 = a4->uNumVertices == 0;
      a4a = a1;
      if ( !v7 )
      {
        v12 = pIndoor->pVertices;
        do
        {
          dword_4F5D98_xs[v11] = v12[v4->pVertexIDs[v11]].x;
          v13 = v4->pVertexIDs[v11++];
          dword_4F5CC4_ys[v11] = v12[v13].z;
        }
        while ( v11 < v4->uNumVertices );
      }
    }
    else
    {
      v14 = 0;
      v7 = a4->uNumVertices == 0;
      a4a = a2;
      if ( !v7 )
      {
        v15 = pIndoor->pVertices;
        do
        {
          dword_4F5D98_xs[v14] = v15[v4->pVertexIDs[v14]].y;
          v16 = v4->pVertexIDs[v14++];
          dword_4F5CC4_ys[v14] = v15[v16].z;
        }
        while ( v14 < v4->uNumVertices );
      }
    }
  }
  v17 = v4->uNumVertices;
  a3a = 0;
  dword_4F5D98_xs[v17] = dword_4F5D98_xs[0];
  dword_4F5CC4_ys[v17 + 1] = dword_4F5CC4_ys[1];
  v28 = v17;
  v18 = dword_4F5CC4_ys[1] >= v8;
  v19 = 0;
  v29 = 0;
  if ( v17 <= 0 )
    goto LABEL_29;
  do
  {
    if ( a3a >= 2 )
      break;
    v20 = v19;
    v21 = dword_4F5CC4_ys[v20 + 2];
    if ( v18 ^ (v21 >= v8) )
    {
      v22 = dword_4F5D98_xs[v20 + 1];
      if ( v22 >= a4a )
        v23 = 0;
      else
        v23 = 2;
      v24 = v23 | (dword_4F5D98_xs[v20] < a4a);
      if ( v24 != 3 )
      {
        if ( !v24
          || (v25 = v22 - dword_4F5D98_xs[v20],
              LODWORD(v26) = v25 << 16,
              HIDWORD(v26) = v25 >> 16,
              dword_4F5D98_xs[v20]
            + ((signed int)(((unsigned __int64)(v26
                                              / (v21 - dword_4F5CC4_ys[v20 + 1])
                                              * ((v8 - dword_4F5CC4_ys[v20 + 1]) << 16)) >> 16)
                          + 32768) >> 16) >= a4a) )
          ++a3a;
      }
    }
    v18 = v21 >= v8;
    v19 = v29++ + 1;
  }
  while ( v29 < v28 );
  result = 1;
  if ( a3a != 1 )
LABEL_29:
    result = 0;
  return result;
}

//----- (004077F1) --------------------------------------------------------
bool __fastcall sub_4077F1(int a1, int a2, int a3, ODMFace *a4, BSPVertexBuffer *a5)
{
  ODMFace *v5; // eax@1
  unsigned int v6; // esi@1
  signed int v7; // edi@1
  char v8; // zf@2
  unsigned __int16 *v9; // edx@3
  int v10; // ecx@4
  unsigned __int16 *v11; // edx@8
  int v12; // ecx@9
  int v13; // edx@12
  int v14; // ecx@13
  int v15; // esi@14
  bool v16; // edi@14
  int v17; // ecx@16
  int v18; // edx@16
  int v19; // eax@17
  signed int v20; // ebx@18
  int v21; // edi@20
  signed int v22; // eax@22
  signed __int64 v23; // qtt@22
  bool result; // eax@25
  int v25; // [sp+14h] [bp-8h]@14
  int a4a; // [sp+28h] [bp+Ch]@2
  signed int a5a; // [sp+2Ch] [bp+10h]@14

  v5 = a4;
  v6 = a4->uAttributes;
  v7 = 0;
  if ( v6 & 0x100 )
  {
    v8 = a4->uNumVertices == 0;
    a4a = a1;
    a3 = a2;
    if ( !v8 )
    {
      v9 = v5->pVertexIDs;
      do
      {
        ++v7;
        dword_4F5BF4_xs[v7] = a5->pVertices[*v9].x;
        v10 = *v9;
        ++v9;
        dword_4F5B24_ys[v7] = a5->pVertices[v10].y;
      }
      while ( v7 < v5->uNumVertices );
    }
  }
  else
  {
    if ( v6 & 0x200 )
    {
      v8 = a4->uNumVertices == 0;
      a4a = a1;
      if ( !v8 )
      {
        v11 = v5->pVertexIDs;
        do
        {
          ++v7;
          dword_4F5BF4_xs[v7] = a5->pVertices[*v11].x;
          v12 = *v11;
          ++v11;
          dword_4F5B24_ys[v7] = a5->pVertices[v12].z;
        }
        while ( v7 < v5->uNumVertices );
      }
    }
    else
    {
      v8 = a4->uNumVertices == 0;
      a4a = a2;
      if ( !v8 )
      {
        v13 = (int)v5->pVertexIDs;
        do
        {
          ++v7;
          dword_4F5BF4_xs[v7] = a5->pVertices[*(short *)v13].y;
          v14 = *(short *)v13;
          v13 += 2;
          dword_4F5B24_ys[v7] = a5->pVertices[v14].z;
        }
        while ( v7 < v5->uNumVertices );
      }
    }
  }
  v15 = v5->uNumVertices;
  a5a = 0;
  dword_4F5BF4_xs[v15 + 1] = dword_4F5BF4_xs[1];
  dword_4F5B24_ys[v15 + 1] = dword_4F5B24_ys[1];
  v25 = 0;
  v16 = dword_4F5B24_ys[1] >= a3;
  if ( v15 <= 0 )
    goto LABEL_29;
  do
  {
    if ( a5a >= 2 )
      break;
    v17 = v25;
    v18 = dword_4F5B24_ys[v25 + 2];
    if ( v16 ^ (v18 >= a3) )
    {
      v19 = dword_4F5BF4_xs[v17 + 2];
      if ( v19 >= a4a )
        v20 = 0;
      else
        v20 = 2;
      v21 = v20 | (dword_4F5BF4_xs[v17 + 1] < a4a);
      if ( v21 != 3 )
      {
        if ( !v21
          || (v22 = v19 - dword_4F5BF4_xs[v17 + 1],
              LODWORD(v23) = v22 << 16,
              HIDWORD(v23) = v22 >> 16,
              dword_4F5BF4_xs[v17 + 1]
            + ((signed int)(((unsigned __int64)(v23
                                              / (v18 - dword_4F5B24_ys[v17 + 1])
                                              * ((a3 - dword_4F5B24_ys[v17 + 1]) << 16)) >> 16)
                          + 32768) >> 16) >= a4a) )
          ++a5a;
      }
    }
    ++v25;
    v16 = v18 >= a3;
  }
  while ( v25 < v15 );
  result = 1;
  if ( a5a != 1 )
LABEL_29:
    result = 0;
  return result;
}

//----- (004088E9) --------------------------------------------------------
signed int __fastcall sub_4088E9(int a1, int a2, int a3, int a4, int a5, int a6)
{
  int v6; // esi@1
  int v7; // edi@1
  int v8; // eax@1
  signed int result; // eax@1
  int v10; // [sp+Ch] [bp-4h]@1
  int v11; // [sp+18h] [bp+8h]@1

  v6 = a1;
  v10 = a2;
  v7 = a3 - a1;
  v11 = abs(a3 - a1);
  v8 = abs(a4 - v10);
  result = integer_sqrt(v11 * v11 + v8 * v8);
  if ( result )
    result = abs((v7 * (v10 - a6) - (a4 - v10) * (v6 - a5)) / result);
  return result;
}

//----- (0040DEDB) --------------------------------------------------------
unsigned int __stdcall R8G8B8_to_TargetFormat(int uColor)
{
  return TargetColor((unsigned __int8)uColor, BYTE1(uColor), BYTE2(uColor));
}

//----- (0040DEF3) --------------------------------------------------------
unsigned short TargetColor( unsigned __int16 r, unsigned __int16 g, unsigned __int16 b )
{
  return ((unsigned int)b >> (8 - LOBYTE(pRenderer->uTargetBBits))) | pRenderer->uTargetGMask & (g << (LOBYTE(pRenderer->uTargetGBits) + 
                           LOBYTE(pRenderer->uTargetBBits) - 8)) | pRenderer->uTargetRMask & (r << (LOBYTE(pRenderer->uTargetGBits) + 
                           LOBYTE(pRenderer->uTargetRBits) + LOBYTE(pRenderer->uTargetBBits) - 8));
}

//----- (0040DFA7) --------------------------------------------------------
int __stdcall retzero_sub_40DFA7(int a1)
{
  return 0;
}
// 40DFA7: using guessed type int __stdcall retzero_sub_40DFA7(int);



//----- (0040F82D) --------------------------------------------------------
void __fastcall ZBuffer_Fill(int *pZBuffer, int uTextureId, int iZValue)
{
  assert(uTextureId != -1);
  ZBuffer_DoFill(pZBuffer, pIcons_LOD->GetTexture(uTextureId), iZValue);
}



//----- (0040F89C) --------------------------------------------------------
void __fastcall ZBuffer_DoFill(int *pZBuffer, Texture *pTex, int uZValue)
{
  void *v3; // eax@3
  void *v4; // esi@5
  int *v5; // edi@5
  int v6; // eax@5
  int v7; // ecx@6
  Texture *v8; // [sp+Ch] [bp-14h]@1
  int *v9; // [sp+10h] [bp-10h]@1
  int v10; // [sp+14h] [bp-Ch]@1
  int v11; // [sp+18h] [bp-8h]@1
  void *v12; // [sp+1Ch] [bp-4h]@5

  v10 = pTex->uTextureWidth;
  v8 = pTex;
  v9 = pZBuffer;
  v11 = pTex->uTextureHeight;
  if ( pIcons_LOD->dword_011BA4 && pTex->uDecompressedSize )
    v3 = pTex->UnzipPalette();
  else
    v3 = pTex->pLevelOfDetail0_prolly_alpha_mask;
  v12 = v3;
  v4 = v3;
  v5 = v9;
  v6 = 0;
  do
  {
    v7 = v10;
    do
    {
      LOBYTE(v6) = *(char *)v4;
      v4 = (char *)v4 + 1;
      if ( v6 )
        *v5 = uZValue;
      ++v5;
      --v7;
    }
    while ( v7 );
    v5 += 640 - v10;
    --v11;
  }
  while ( v11 );
  if ( pIcons_LOD->dword_011BA4 )
  {
    if ( v8->uDecompressedSize )
      free(v12);
  }
}

//----- (0040F92A) --------------------------------------------------------
void __fastcall sub_40F92A(int *pZBuffer, Texture *a2, int a3)
{
  Texture *v3; // esi@1
  void *v4; // eax@3
  int *v5; // edi@5
  int v6; // ecx@6
  int *v7; // [sp+Ch] [bp-10h]@1
  int v8; // [sp+10h] [bp-Ch]@1
  int v9; // [sp+18h] [bp-4h]@1

  v3 = a2;
  v7 = pZBuffer;
  v8 = a2->uTextureWidth;
  v9 = a2->uTextureHeight;
  if ( pIcons_LOD->dword_011BA4 && a2->uDecompressedSize )
    v4 = a2->UnzipPalette();
  else
    v4 = a2->pLevelOfDetail0_prolly_alpha_mask;
  v5 = v7;
  do
  {
    v6 = v8;
    do
    {
      *v5 = a3;
      ++v5;
      --v6;
    }
    while ( v6 );
    v5 += 640 - v8;
    --v9;
  }
  while ( v9 );
  if ( pIcons_LOD->dword_011BA4 )
  {
    if ( v3->uDecompressedSize )
      free(v4);
  }
}

//----- (00410D99) --------------------------------------------------------
signed int __fastcall sub_410D99_get_map_index(int a1)
{
  int v1; // edi@1
  signed int v2; // ebp@1
  const char **v3; // esi@2
  char *v4; // ebx@2

  v1 = a1;
  v2 = 1;
  if ( _stricmp(pMapStats->pInfos[1].pFilename, (const char *)&pGames_LOD->pSubIndices[a1]) )
  {
    v3 = (const char **)&pMapStats->pInfos[1].pFilename;
    v4 = (char *)&pMapStats->pInfos[1].pFilename;
    do
    {
      if ( !*(int *)v4 )
        break;
      v3 += 17;
      ++v2;
      v4 = (char *)v3;
    }
    while ( _stricmp(*v3, (const char *)&pGames_LOD->pSubIndices[v1]) );
  }
  return v2;
}

// 4E28F8: using guessed type int pCurrentScreen;

void LoadFonts_and_DrawCopyrightWindow()
{
  MainMenuUI_LoadFontsAndSomeStuff();
  DrawCopyrightWindow();
}


//----- (00416196) --------------------------------------------------------
void identify_item()
{
  Player *v0; // esi@2
  POINT *v1; // edi@2
  signed int v2; // ecx@5
  signed int v3; // eax@5
  int v4; // eax@5
  char v5; // sf@5
  int v6; // eax@5
  int v7; // eax@7
  unsigned __int16 v8; // ax@10
  int v9; // edi@10
  int v10; // eax@10
  int v11; // edx@13
  LONG v12; // edx@23
  signed int v13; // edx@23
  int v14; // edx@25
  unsigned int v15; // edi@25
  signed int v16; // edx@27
  int v17; // eax@54
  unsigned int v18; // edx@57
  int v19; // eax@65
  int v20; // edi@67
  int v21; // eax@72
  int v22; // ecx@74
  int v23; // edx@78
  int v24; // eax@79
  int v25; // eax@80
  int v26; // eax@84
  ItemGen *v27; // esi@98
  unsigned int v28; // eax@98
  ItemGen *v29; // edi@98
  unsigned int v30; // ecx@103
  double v31; // st7@112
  unsigned __int64 v32; // qax@113
  unsigned int v33; // ecx@117
  int v34; // eax@126
  unsigned int v35; // ecx@127
  double v36; // st7@132
  signed __int64 v37; // qax@135
  unsigned int v38; // eax@138
  Vec3_int_ v39; // [sp-18h] [bp-A8h]@83
  int *v40; // [sp-Ch] [bp-9Ch]@83
  int *v41; // [sp-8h] [bp-98h]@83
  int *v42; // [sp-4h] [bp-94h]@83
  GUIWindow v43; // [sp+Ch] [bp-84h]@137
  unsigned int pY; // [sp+60h] [bp-30h]@2
  int v45; // [sp+64h] [bp-2Ch]@10
  unsigned int pX; // [sp+68h] [bp-28h]@2
  int v47; // [sp+6Ch] [bp-24h]@25
  POINT y; // [sp+70h] [bp-20h]@2
  POINT a2; // [sp+78h] [bp-18h]@2
  Vec3_int_ pOut; // [sp+80h] [bp-10h]@2
  unsigned int v51; // [sp+8Ch] [bp-4h]@23

  if ( dword_50CDCC )
    return;
  v0 = pPlayers[uActiveCharacter];
  v1 = pMouse->GetCursorPos(&a2);
  pOut.z = pRenderer->pActiveZBuffer[v1->x + pSRZBufferLineOffsets[pMouse->GetCursorPos(&y)->y]] & 0xFFFF;
  pMouse->GetClickPos(&pX, &pY);
  if ( !pOut.z )
  {
    if ( (signed int)pX <= 13
      || (signed int)pX >= 462
      || (v2 = pX - 14,
          v3 = pY - 17,
          pX = v2,
          pY = v3,
          v4 = 14 * (v3 >> 5),
          v2 >>= 5,
          v5 = v2 + v4 < 0,
          v6 = v2 + v4,
          pOut.z = v6,
          v5)
      || v6 > 126
      || (v7 = v0->GetItemIDAtInventoryIndex(&pOut.z)) == 0 )
      return;
    pOut.z = v7;
  }
  if ( !v0->CanAct() )
  {
    sprintf(pTmpBuf.data(), pGlobalTXT_LocalizationStrings[427], v0->pName, pGlobalTXT_LocalizationStrings[541]);
    v43.Hint = pTmpBuf.data();
    v43.uFrameWidth = 384;
    v43.uFrameHeight = 180;
    v43.uFrameY = 40;
    if ( pMouse->GetCursorPos(&a2)->x <= 320 )
      v38 = pMouse->GetCursorPos(&a2)->x + 30;
    else
      v38 = pMouse->GetCursorPos(&a2)->x - 414;
    v43.uFrameX = v38;
    v43.DrawMessageBox(0);
    return;
  }
  --pOut.z;
  LOBYTE(v8) = v0->GetActualSkillLevel(PLAYER_SKILL_ALCHEMY);
  v9 = v8 & 0x3F;
  v45 = v8 & 0x3F;
  pOut.y = SkillToMastery(v8);
  v10 = pOut.z;
  if ( pParty->pPickedItem.uItemID == 220 )
  {
LABEL_116:
    GameUI_DrawItemInfo(&v0->pInventoryItems[v10]);
    return;
  }
  if ( (signed int)pParty->pPickedItem.uItemID < 200
    || (signed int)pParty->pPickedItem.uItemID > 219
    || (v11 = (int)((char *)v0 + 36 * pOut.z), *(int *)(v11 + 532) != 220) )
  {
    if ( (signed int)pParty->pPickedItem.uItemID < 221
      || (signed int)pParty->pPickedItem.uItemID > 271
      || (v12 = (LONG)((char *)v0 + 36 * pOut.z), a2.y = v12, v13 = *(int *)(v12 + 532), v51 = v13, v13 < 221)
      || v13 > 271 )
    {
      if ( pParty->pPickedItem.uItemID != 236
        && ((signed int)pParty->pPickedItem.uItemID < 246 || (signed int)pParty->pPickedItem.uItemID > 250)
        && pParty->pPickedItem.uItemID != 263
        && pParty->pPickedItem.uItemID != 233 )
        goto LABEL_116;
      v27 = &v0->pInventoryItems[pOut.z];
      v28 = v27->uItemID;
      v51 = pItemsTable->pItems[v27->uItemID].uEquipType;
      v29 = (ItemGen *)(1800 * pParty->pPickedItem.uEnchantmentType);
      if ( pParty->pPickedItem.uItemID != 233 )
      {
        if ( pParty->pPickedItem.uItemID == 236 )
        {
          v35 = v27->uAttributes;
          if ( v35 & 2 || (v51 & 0x80000000u) != 0 || (signed int)v51 > 12 || (signed int)v28 >= 500 )
            goto LABEL_92;
          LOWORD(v35) = v35 | 0x210;
          v27->uAttributes = v35;
        }
        else
        {
          if ( (signed int)pParty->pPickedItem.uItemID <= 245 )
            goto LABEL_92;
          if ( (signed int)pParty->pPickedItem.uItemID <= 250 )
          {
            v27->UpdateTempBonus(pParty->uTimePlayed);
            v33 = v27->uItemID;
            if ( (signed int)v27->uItemID >= 64 && (signed int)v33 <= 65
              || (pOut.y = v27->uAttributes, pOut.y & 2)
              || v27->uSpecEnchantmentType
              || v27->uEnchantmentType
              || v51 && v51 != 1 && v51 != 2
              || (signed int)v33 >= 500 )
              goto LABEL_92;
            v34 = *(int *)&aSbwb00[4 * pParty->pPickedItem.uItemID + 4];
            a2.y = (int)v29 << 7;
            v31 = (double)(signed int)((int)v29 << 7);
            v27->uSpecEnchantmentType = v34;
          }
          else
          {
            if ( pParty->pPickedItem.uItemID != 263
              || (v27->UpdateTempBonus(pParty->uTimePlayed), v30 = v27->uItemID, (signed int)v27->uItemID >= 64)
              && (signed int)v30 <= 65
              || (pOut.y = v27->uAttributes, pOut.y & 2)
              || v27->uSpecEnchantmentType
              || v27->uEnchantmentType
              || v51 && v51 != 1 && v51 != 2
              || (signed int)v30 >= 500 )
              goto LABEL_92;
            a2.y = (int)v29 << 7;
            v27->uSpecEnchantmentType = 40;
            v31 = (double)a2.y;
          }
          v32 = pParty->uTimePlayed + (signed int)(signed __int64)(v31 * 0.033333335);
          LODWORD(v27->uExpireTime) = v32;
          LODWORD(v32) = pOut.y;
          LOBYTE(v32) = LOBYTE(pOut.y) | 0x18;
          HIDWORD(v27->uExpireTime) = HIDWORD(v32);
          v27->uAttributes = v32;
        }
        _50C9A8_item_enchantment_timer = 256;
        goto LABEL_92;
      }
      v36 = (70.0 - (double)pParty->pPickedItem.uEnchantmentType) * 0.01;
      if ( v36 < 0.0 )
        v36 = 0.0;
      if ( v51 != 12 )
      {
        pAudioPlayer->PlaySound(SOUND_27, 0, 0, -1, 0, 0, 0, 0);
        return;
      }
      a2.y = v27->uMaxCharges;
      v37 = (signed __int64)((double)a2.y - v36 * (double)a2.y);
      v27->uMaxCharges = v37;
      v27->uNumCharges = (unsigned __int8)v37;
      goto LABEL_92;
    }
    v14 = v13 - 222;
    v15 = pParty->pPickedItem.uItemID - 222;
    v47 = v14;
    y.y = 0;
    if ( pParty->pPickedItem.uItemID == 221 || v51 == 221 )
    {
      v16 = 5;
    }
    else
    {
      v16 = *(short *)&pItemsTable->field_EDE0[2 * (v14 + 50 * v15) + 388];
      v10 = pOut.z;
    }
    v51 = v16;
    if ( v45 )
    {
      if ( v16 < 225 || v16 > 227 )
      {
        if ( v16 < 228 || v16 > 239 )
        {
          if ( v16 < 240 || v16 > 261 )
          {
            if ( v16 >= 262 && pOut.y != 4 )
            {
LABEL_53:
              v51 = 4;
              goto LABEL_54;
            }
          }
          else
          {
            if ( pOut.y <= 2 )
            {
LABEL_38:
              v51 = 3;
              goto LABEL_54;
            }
          }
        }
        else
        {
          if ( pOut.y == 1 )
          {
LABEL_35:
            v51 = 2;
            goto LABEL_54;
          }
        }
      }
    }
    else
    {
      if ( v16 >= 225 && v16 <= 227 )
      {
        v51 = 1;
        goto LABEL_54;
      }
      if ( v16 >= 228 && v16 <= 239 )
        goto LABEL_35;
      if ( v16 >= 240 && v16 <= 261 )
        goto LABEL_38;
      if ( v16 >= 262 )
        goto LABEL_53;
    }
LABEL_54:
    pOut.y = 0;
    pOut.x = v10 + 1;
    v17 = (int)v0->pInventoryIndices;
    while ( *(int *)v17 != pOut.x )
    {
      ++pOut.y;
      v17 += 4;
      if ( pOut.y >= 126 )
      {
        v18 = y.y;
        goto LABEL_59;
      }
    }
    v18 = pOut.y;
LABEL_59:
    if ( !v51 )
    {
LABEL_115:
      v10 = pOut.z;
      goto LABEL_116;
    }
    if ( v51 == 1 )
    {
      v0->RemoveItemAtInventoryIndex(v18);
      v26 = rand();
      v0->ReceiveDamage(v26 % 11 + 10, DMGT_FIRE);
      pAudioPlayer->PlaySound(SOUND_8, 0, 0, -1, 0, 0, 0, 0);

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

      v42 = (int *)&a2.y;
      v41 = (int *)&y.y;
      v40 = (int *)&pOut;
      v39.z = pParty->vPosition.z + pParty->sEyelevel;
    }
    else
    {
      if ( v51 == 2 )
      {
        v0->RemoveItemAtInventoryIndex(v18);
        v25 = rand();
        v0->ReceiveDamage(v25 % 71 + 30, DMGT_FIRE);
        v23 = 1;
      }
      else
      {
        if ( v51 == 3 )
        {
          v0->RemoveItemAtInventoryIndex(v18);
          v24 = rand();
          v0->ReceiveDamage(v24 % 201 + 50, DMGT_FIRE);
          v23 = 5;
        }
        else
        {
          if ( v51 != 4 )
          {
            if ( v51 != 5 )
            {
              v19 = v0->AddItem(-1, 0xDCu);
              if ( v19 )
               // *(int *)&v0->field_1F5[36 * v19 + 15] = 1;
                 v0->pOwnItems[v19-1].uAttributes=ITEM_IDENTIFIED;
              v20 = v47 + 50 * v15;
              v0->pInventoryItems[pOut.z].uItemID = v51;
              v0->pInventoryItems[pOut.z].uEnchantmentType = (pParty->pPickedItem.uEnchantmentType
                                                       + v0->pInventoryItems[pOut.z].uEnchantmentType)
                                                      / 2;
              v0->SetVariable(VAR_AutoNotes, *(short *)&pItemsTable->potion_note[2 * v20 ]);//field_10168  + 388
LABEL_74:
              v22 = (int)((char *)v0 + 36 * pOut.z);
              if ( !(pItemsTable->pItems[*(int *)(v22 + 532) ].uItemID_Rep_St) )
                *(int *)(v22 + 552) |= 1u;
              if ( !dword_4E455C )
                goto LABEL_92;
              v0->PlaySound(SPEECH_16, 0);
              goto LABEL_91;
            }
            if ( v45 )
            {
              if ( *(int *)(a2.y + 532) == 221 )
                *(int *)(a2.y + 532) = pParty->pPickedItem.uItemID;
              else
                *(int *)(a2.y + 536) = pParty->pPickedItem.uEnchantmentType;
              v21 = v0->AddItem(-1, 0xDCu);
              if ( v21 )
                //*(int *)&v0->field_1F5[36 * v21 + 15] = 1;
                v0->pOwnItems[v21-1].uAttributes=ITEM_IDENTIFIED;
              goto LABEL_74;
            }
            goto LABEL_115;
          }
          v0->RemoveItemAtInventoryIndex(v18);
          v0->SetCondition(0x10u, 0);
          v23 = 0;
        }
      }
      v0->ItemsEnchant(v23);
      pAudioPlayer->PlaySound(SOUND_8, 0, 0, -1, 0, 0, 0, 0);

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

      v42 = (int *)&a2.y;
      v41 = (int *)&y.y;
      v40 = (int *)&pOut;
      v39.z = pParty->vPosition.z + pParty->sEyelevel;
    }
    *(_QWORD *)&v39.x = *(_QWORD *)&pParty->vPosition.x;
    Vec3_int_::Rotate(64, pParty->sRotationY, pParty->sRotationX, v39, v40, v41, v42);
    sub_42F7EB_DropItemAt(0x41Bu, pOut.x, y.y, a2.y, 0, 1, 0, 0, 0);
    if ( dword_4E455C )
    {
      if ( v0->CanAct() )
        v0->PlaySound(SPEECH_17, 0);
      ShowStatusBarString(pGlobalTXT_LocalizationStrings[444], 2u);
LABEL_91:
      dword_4E455C = 0;
    }
LABEL_92:
    pMouse->RemoveHoldingItem();
    dword_50CDCC = 1;
    return;
  }
  *(int *)(v11 + 536) = v9 + pItemsTable->pItems[pParty->pPickedItem.uItemID].uDamageDice;
  switch ( pParty->pPickedItem.uItemID )
  {
    case 0xC8u:
    case 0xC9u:
    case 0xCAu:
    case 0xCBu:
    case 0xCCu:
      v0->pInventoryItems[pOut.z].uItemID = 222;
      break;
    case 0xCDu:
    case 0xCEu:
    case 0xCFu:
    case 0xD0u:
    case 0xD1u:
      v0->pInventoryItems[pOut.z].uItemID = 223;
      break;
    case 0xD2u:
    case 0xD3u:
    case 0xD4u:
    case 0xD5u:
    case 0xD6u:
      v0->pInventoryItems[pOut.z].uItemID = 224;
      break;
    case 0xD7u:
    case 0xD8u:
    case 0xD9u:
    case 0xDAu:
    case 0xDBu:
      v0->pInventoryItems[pOut.z].uItemID = 221;
      break;
    default:
      break;
  }
  pMouse->RemoveHoldingItem();
  dword_50CDCC = 1;
  if ( dword_4E455C )
  {
    v0->PlaySound(SPEECH_16, 0);
    dword_4E455C = 0;
  }
}

//----- (004179BC) --------------------------------------------------------
void __fastcall sub_4179BC_draw_tooltip( const char *a1, const char *a2 )
    {
  const char *v2; // ebx@1
  const char *v3; // edi@1
  unsigned int v4; // eax@1
  GUIWindow Dst; // [sp+Ch] [bp-5Ch]@1
  POINT v6; // [sp+60h] [bp-8h]@1

  v2 = a2;
  v3 = a1;
  memset(&Dst, 0, 0x54u);
  Dst.uFrameWidth = 384;
  Dst.uFrameHeight = 256;
  Dst.uFrameX = 128;
  Dst.uFrameY = pMouse->GetCursorPos(&v6)->y + 30;
  Dst.uFrameHeight = pFontSmallnum->CalcTextHeight(v2, &Dst, 24, 0) + 2 * LOBYTE(pFontLucida->uFontHeight) + 24;
  Dst.uFrameZ = Dst.uFrameX + Dst.uFrameWidth - 1;
  Dst.uFrameW = Dst.uFrameY + Dst.uFrameHeight - 1;
  Dst.DrawMessageBox(0);
  Dst.uFrameX += 12;
  Dst.uFrameWidth -= 24;
  Dst.uFrameY += 12;
  Dst.uFrameHeight -= 12;
  Dst.uFrameZ = Dst.uFrameX + Dst.uFrameWidth - 1;
  Dst.uFrameW = Dst.uFrameY + Dst.uFrameHeight - 1;
  v4 = TargetColor(0xFFu, 0xFFu, 0x9Bu);
  sprintf(pTmpBuf.data(), format_4E2D80, v4, v3);
  Dst.DrawTitleText(pFontCreate, 0, 0, 0, pTmpBuf.data(), 3u);
  Dst.DrawText(pFontSmallnum, 1, LOBYTE(pFontLucida->uFontHeight), 0, v2, 0, 0, 0);
}

//----- (00417AD4) --------------------------------------------------------
unsigned int GetSkillColor(unsigned int uPlayerClass, PLAYER_SKILL_TYPE uPlayerSkillType, signed int skill_level)
{
  int v8; // eax@10
  int v12; // eax@14
  unsigned int yellow; // [sp+Ch] [bp-Ch]@1
  unsigned int red; // [sp+10h] [bp-8h]@1
  unsigned int white; // [sp+14h] [bp-4h]@1

  white = TargetColor(0xFFu, 0xFFu, 0xFFu);
  red = TargetColor(0xFFu, 0, 0);
  yellow = TargetColor(0xFFu, 0xFFu, 0);
  if ( !(uPlayerClass % 4) )
  {
    if ( byte_4ED970_skill_learn_ability_by_class_table[uPlayerClass][uPlayerSkillType] >= skill_level )
      return white;
    if ( byte_4ED970_skill_learn_ability_by_class_table[uPlayerClass + 1][uPlayerSkillType] < skill_level
      && byte_4ED970_skill_learn_ability_by_class_table[uPlayerClass + 2][uPlayerSkillType] < skill_level )
    {
      v12 = byte_4ED970_skill_learn_ability_by_class_table[uPlayerClass + 3][uPlayerSkillType];
      if ( v12 < skill_level )
        return red;
    }
    return yellow;
  }
  if ( (uPlayerClass % 4) == 1 )
  {
    if ( byte_4ED970_skill_learn_ability_by_class_table[uPlayerClass][uPlayerSkillType] >= skill_level )
      return white;
    if ( byte_4ED970_skill_learn_ability_by_class_table[uPlayerClass + 1][uPlayerSkillType] < skill_level )
    {
      v8 = byte_4ED970_skill_learn_ability_by_class_table[uPlayerClass + 2][uPlayerSkillType];
      if ( v8 < skill_level)
        return red;
    }
    return yellow;
  }
  if ( (uPlayerClass % 4) == 2 || (uPlayerClass % 4) == 3 )
  {
    if ( byte_4ED970_skill_learn_ability_by_class_table[uPlayerClass][uPlayerSkillType] < skill_level )
      return red;
  }
  return white;
}