changeset 1347:84db43e7db93

Merge
author Grumpy7
date Fri, 12 Jul 2013 23:08:35 +0200
parents 1434b36f4bdb (current diff) b99aeb077d4f (diff)
children 3ed6dcf82912
files Player.cpp
diffstat 28 files changed, 2239 insertions(+), 2933 deletions(-) [+]
line wrap: on
line diff
--- a/Actor.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/Actor.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -1618,243 +1618,198 @@
 AIDirection *__fastcall Actor::GetDirectionInfo(unsigned int uObj1ID, unsigned int uObj2ID, AIDirection *pOut, int a4)
 {
   signed int v4; // eax@1
-  unsigned int v5; // ecx@1
-  unsigned int v6; // ebx@1
+  signed int v5; // ecx@1
+  //unsigned int v6; // ebx@1
   int v7; // ecx@2
   int v8; // ecx@3
   int v9; // ecx@4
-  BLVFace *v10; // eax@8
-  unsigned int v11; // ecx@8
-  signed int v12; // eax@9
-  int v13; // ecx@9
-  int v14; // eax@9
+  //BLVFace *v10; // eax@8
+  //unsigned int v11; // ecx@8
+  //signed int v12; // eax@9
+  //int v13; // ecx@9
+  //int v14; // eax@9
   int v15; // eax@11
   int v16; // eax@12
   int v17; // eax@13
   int v18; // edx@15
-  signed int v19; // eax@25
-  signed int v20; // eax@28
-  int v21; // ebx@28
-  signed int v22; // eax@28
-  BLVFace *v23; // eax@35
-  int v24; // edx@35
-  unsigned int v25; // ecx@35
-  signed int v26; // eax@36
-  int v27; // ecx@36
-  int v28; // eax@36
-  int v29; // eax@37
-  signed int v30; // eax@42
+  //signed int v19; // eax@25
+  //signed int v20; // eax@28
+  //int v21; // ebx@28
+  //signed int v22; // eax@28
+  //BLVFace *v23; // eax@35
+  //int v24; // edx@35
+  //unsigned int v25; // ecx@35
+  //signed int v26; // eax@36
+  //int v27; // ecx@36
+  //int v28; // eax@36
+  //int v29; // eax@37
+  //signed int v30; // eax@42
   double v31; // st7@45
   double v32; // st6@45
   double v33; // st7@45
   AIDirection *result; // eax@48
-  int v35; // [sp-18h] [bp-64h]@17
-  int v36; // [sp-14h] [bp-60h]@17
+  //int v35; // [sp-18h] [bp-64h]@17
+  //int v36; // [sp-14h] [bp-60h]@17
   Vec3_int_ v37; // [sp-10h] [bp-5Ch]@15
-  int *v38; // [sp-4h] [bp-50h]@15
-  int *v39; // [sp+0h] [bp-4Ch]@15
-  int *v40; // [sp+4h] [bp-48h]@15
+  //int *v38; // [sp-4h] [bp-50h]@15
+  //int *v39; // [sp+0h] [bp-4Ch]@15
+  //int *v40; // [sp+4h] [bp-48h]@15
   AIDirection v41; // [sp+14h] [bp-38h]@46
   float v42; // [sp+30h] [bp-1Ch]@23
   float v43; // [sp+34h] [bp-18h]@45
-  float v44; // [sp+38h] [bp-14h]@33
-  float v45; // [sp+3Ch] [bp-10h]@33
+  float outy2; // [sp+38h] [bp-14h]@33
+  float outx2; // [sp+3Ch] [bp-10h]@33
   int outz; // [sp+40h] [bp-Ch]@6
   int outy; // [sp+44h] [bp-8h]@6
   int outx; // [sp+48h] [bp-4h]@6
   float a4a; // [sp+58h] [bp+Ch]@45
 
   v4 = PID_ID(uObj1ID);
-  v5 = PID_TYPE(uObj1ID);
-  v6 = uObj2ID;
-  switch(v5)
+  //v6 = uObj2ID;
+  v5 = PID_ID(uObj2ID);
+  switch( PID_TYPE(uObj1ID) )
   {
-	case OBJECT_Item:
-		{
-		v19 = v4;
-		outx = pSpriteObjects[v19].vPosition.x;
-		v13 = pSpriteObjects[v19].vPosition.y;
-		v14 = pSpriteObjects[v19].vPosition.z;
-		outy = v13;
-		outz = v14;
-		break;
-	    }
-	case OBJECT_Actor:
-		{
-		outx = pActors[v4].vPosition.x;
-		outy = pActors[v4].vPosition.y;
-		LODWORD(v42) = pActors[v4].uActorHeight;
-		v11 = pActors[v4].vPosition.z - (unsigned int)(signed __int64)((double)SLODWORD(v42) * -0.75);
-		outz = v11;
-		break;
-		}
-	case OBJECT_Player:
-		{
-		if ( !v4 )
-		{
-			outx = pParty->vPosition.x;
-			outy = pParty->vPosition.y;
-			v14 = pParty->vPosition.z + (signed int)pParty->uPartyHeight / 3;
-			outz = v14;
-			break;;
-		}
-		if ( v4 == 4 )
-		{
-			v40 = &outz;
-			v39 = &outy;
-			v38 = &outx;
-			v18 = pParty->sRotationY - stru_5C6E00->uIntegerHalfPi;
-			v37.z = pParty->vPosition.z + (signed int)pParty->uPartyHeight / 3;
-			*(_QWORD *)&v37 = *(_QWORD *)&pParty->vPosition.x;
-			v36 = 0;
-			v35 = 24;
-			Vec3_int_::Rotate(v35, v18, v36, v37, v38, v39, v40);
-			break;
-		}
-		if ( v4 == 3 )
-		{
-			v40 = &outz;
-			v39 = &outy;
-			v38 = &outx;
-			v18 = pParty->sRotationY - stru_5C6E00->uIntegerHalfPi;
-			v37.z = pParty->vPosition.z + (signed int)pParty->uPartyHeight / 3;
-			*(_QWORD *)&v37.x = *(_QWORD *)&pParty->vPosition.x;
-			v36 = 0;
-			v35 = 8;
-			Vec3_int_::Rotate(v35, v18, v36, v37, v38, v39, v40);
-			break;
-		}
-		if ( v4 == 2 )
-		{
-			v40 = &outz;
-			v39 = &outy;
-			v38 = &outx;
-			v37.z = pParty->vPosition.z + (signed int)pParty->uPartyHeight / 3;
-			v18 = stru_5C6E00->uIntegerHalfPi + pParty->sRotationY;
-			*(_QWORD *)&v37.x = *(_QWORD *)&pParty->vPosition.x;
-			v36 = 0;
-			v35 = 8;
-			Vec3_int_::Rotate(v35, v18, v36, v37, v38, v39, v40);
-			break;
-		}
-		if ( v4 == 1 )
-		{
-			v40 = &outz;
-			v39 = &outy;
-			v38 = &outx;
-			v37.z = pParty->vPosition.z + (signed int)pParty->uPartyHeight / 3;
-			v18 = stru_5C6E00->uIntegerHalfPi + pParty->sRotationY;
-			*(_QWORD *)&v37 = *(_QWORD *)&pParty->vPosition.x;
-			v36 = 0;
-			v35 = 24;
-			Vec3_int_::Rotate(v35, v18, v36, v37, v38, v39, v40);
-			break;
-		}
-		}
-	case OBJECT_Decoration:
-		{
-		v12 = v4;
-		outx = pLevelDecorations[v12].vPosition.x;
-		v13 = pLevelDecorations[v12].vPosition.y;
-		v14 = pLevelDecorations[v12].vPosition.z;
-		outy = v13;
-		outz = v14;
-		break;
-		}
-	default:
-		{
-		outz = 0;
-		outy = 0;
-		outx = 0;
-		break;
-		}
-	case OBJECT_BModel:
-		{
-		if ( uCurrentlyLoadedLevelType == LEVEL_Indoor )
-		{
-			v10 = &pIndoor->pFaces[v4];
-			outx = (v10->pBounding.x1 + v10->pBounding.x2) >> 1;
-			outy = (v10->pBounding.y1 + v10->pBounding.y2) >> 1;
-			v11 = (v10->pBounding.z1 + v10->pBounding.z2) >> 1;
-			outz = v11;
-		}
-		break;
-		}
+    case OBJECT_Item:
+    {
+      outx = pSpriteObjects[v4].vPosition.x;
+      outy = pSpriteObjects[v4].vPosition.y;
+      outz = pSpriteObjects[v4].vPosition.z;
+      break;
+    }
+    case OBJECT_Actor:
+    {
+      outx = pActors[v4].vPosition.x;
+      outy = pActors[v4].vPosition.y;
+      outz = pActors[v4].vPosition.z - (unsigned int)(signed __int64)((double)pActors[v4].uActorHeight * -0.75);
+      break;
+    }
+    case OBJECT_Player:
+    {
+      if ( !v4 )
+      {
+        outx = pParty->vPosition.x;
+        outy = pParty->vPosition.y;
+        outz = pParty->vPosition.z + (signed int)pParty->uPartyHeight / 3;
+        break;
+      }
+      if ( v4 == 4 )
+      {
+        v18 = pParty->sRotationY - stru_5C6E00->uIntegerHalfPi;
+        v37.z = pParty->vPosition.z + (signed int)pParty->uPartyHeight / 3;
+        *(_QWORD *)&v37 = *(_QWORD *)&pParty->vPosition.x;
+        Vec3_int_::Rotate(24, v18, 0, v37, &outx, &outy, &outz);
+        break;
+      }
+      if ( v4 == 3 )
+      {
+        v18 = pParty->sRotationY - stru_5C6E00->uIntegerHalfPi;
+        v37.z = pParty->vPosition.z + (signed int)pParty->uPartyHeight / 3;
+        *(_QWORD *)&v37.x = *(_QWORD *)&pParty->vPosition.x;
+        Vec3_int_::Rotate(8, v18, 0, v37, &outx, &outy, &outz);
+        break;
+      }
+      if ( v4 == 2 )
+      {
+        v37.z = pParty->vPosition.z + (signed int)pParty->uPartyHeight / 3;
+        v18 = stru_5C6E00->uIntegerHalfPi + pParty->sRotationY;
+        *(_QWORD *)&v37.x = *(_QWORD *)&pParty->vPosition.x;
+        Vec3_int_::Rotate(8, v18, 0, v37, &outx, &outy, &outz);
+        break;
+      }
+      if ( v4 == 1 )
+      {
+        v37.z = pParty->vPosition.z + (signed int)pParty->uPartyHeight / 3;
+        v18 = stru_5C6E00->uIntegerHalfPi + pParty->sRotationY;
+        *(_QWORD *)&v37 = *(_QWORD *)&pParty->vPosition.x;
+        Vec3_int_::Rotate(24, v18, 0, v37, &outx, &outy, &outz);
+        break;
+      }
+    }
+    case OBJECT_Decoration:
+    {
+      outx = pLevelDecorations[v4].vPosition.x;
+      outy = pLevelDecorations[v4].vPosition.y;
+      outz = pLevelDecorations[v4].vPosition.z;
+      break;
+    }
+    default:
+    {
+      outz = 0;
+      outy = 0;
+      outx = 0;
+      break;
+    }
+    case OBJECT_BModel:
+    {
+      if ( uCurrentlyLoadedLevelType == LEVEL_Indoor )
+      {
+        outx = (pIndoor->pFaces[v4].pBounding.x1 + pIndoor->pFaces[v4].pBounding.x2) >> 1;
+        outy = (pIndoor->pFaces[v4].pBounding.y1 + pIndoor->pFaces[v4].pBounding.y2) >> 1;
+        outz = (pIndoor->pFaces[v4].pBounding.z1 + pIndoor->pFaces[v4].pBounding.z2) >> 1;
+      }
+      break;
+    }
   }
-  v20 = v6;
-  v21 = PID_TYPE(v6);
-  v22 = PID_ID(v20);
-  switch(v21)
+
+  switch( PID_TYPE(uObj2ID) )
   {
-	case OBJECT_Item:
-		{
-		v30 = v22;
-		LODWORD(v45) = pSpriteObjects[v30].vPosition.x;
-		v27 = pSpriteObjects[v30].vPosition.y;
-		v28 = pSpriteObjects[v30].vPosition.z;
-		LODWORD(v44) = v27;
-		a4 = v28;
-		break;
-	    }
-	case OBJECT_Actor:
-		{
-		LODWORD(v45) = pActors[v22].vPosition.x;
-		LODWORD(v44) = pActors[v22].vPosition.y;
-		v25 = pActors[v22].vPosition.z - (unsigned int)(signed __int64)((double)pActors[v22].uActorHeight * -0.75);
-		a4 = v25;
-		break;
-		}
-	case OBJECT_Player:
-		{
-		LODWORD(v45) = pParty->vPosition.x;
-		LODWORD(v44) = pParty->vPosition.y;
-		v29 = a4;
-		if ( !a4 )
-		  v29 = pParty->sEyelevel;
-		v28 = pParty->vPosition.z + v29;
-		a4 = v28;
-		break;
-		}
-	case OBJECT_Decoration:
-		{
-	    v26 = v22;
-		LODWORD(v45) = pLevelDecorations[v26].vPosition.x;
-		v27 = pLevelDecorations[v26].vPosition.y;
-		v28 = pLevelDecorations[v26].vPosition.z;
-		LODWORD(v44) = v27;
-		a4 = v28;
-		break;
-		}
-	default:
-		{
-		a4 = 0;
-		v44 = 0.0;
-		v45 = 0.0;
-		break;
-		}
-	case OBJECT_BModel:
-		{
-		if ( uCurrentlyLoadedLevelType == LEVEL_Indoor )
-		{
-			v23 = &pIndoor->pFaces[v22];
-			v24 = v23->pBounding.y1;
-			LODWORD(v45) = (v23->pBounding.x1 + v23->pBounding.x2) >> 1;
-			LODWORD(v44) = (v24 + v23->pBounding.y2) >> 1;
-			v25 = (v23->pBounding.z1 + v23->pBounding.z2) >> 1;
-			a4 = v25;
-		}
-		break;
-		}
+    case OBJECT_Item:
+    {
+      outx2 = pSpriteObjects[v5].vPosition.x;
+      outy2 = pSpriteObjects[v5].vPosition.y;
+      a4 = pSpriteObjects[v5].vPosition.z;
+      break;
+    }
+    case OBJECT_Actor:
+    {
+      outx2 = pActors[v5].vPosition.x;
+      outy2 = pActors[v5].vPosition.y;
+      a4 = pActors[v5].vPosition.z - (unsigned int)(signed __int64)((double)pActors[v5].uActorHeight * -0.75);
+      break;
+    }
+    case OBJECT_Player:
+    {
+      outx2 = pParty->vPosition.x;
+      outy2 = pParty->vPosition.y;
+      if ( !a4 )
+        a4 = pParty->sEyelevel;
+      a4 = pParty->vPosition.z + a4;
+      break;
+    }
+    case OBJECT_Decoration:
+    {
+      outx2 = pLevelDecorations[v5].vPosition.x;
+      outy2 = pLevelDecorations[v5].vPosition.y;
+      a4 = pLevelDecorations[v5].vPosition.z;
+      break;
+    }
+    default:
+    {
+      outx2 = 0.0;
+      outy2 = 0.0;
+      a4 = 0;
+      break;
+    }
+    case OBJECT_BModel:
+    {
+      if ( uCurrentlyLoadedLevelType == LEVEL_Indoor )
+      {
+        outx2 = (pIndoor->pFaces[v5].pBounding.x1 + pIndoor->pFaces[v5].pBounding.x2) >> 1;
+        outy2 = (pIndoor->pFaces[v5].pBounding.y1 + pIndoor->pFaces[v5].pBounding.y2) >> 1;
+        a4 = (pIndoor->pFaces[v5].pBounding.z1 + pIndoor->pFaces[v5].pBounding.z2) >> 1;
+      }
+      break;
+    }
   }
   
-  v31 = (double)SLODWORD(v45) - (double)outx;
+  v31 = (double)outx2 - (double)outx;
   v42 = v31;
-  v32 = (double)SLODWORD(v44) - (double)outy;
+  v32 = (double)outy2 - (double)outy;
   v43 = v32;
   a4a = (double)a4 - (double)outz;
-  v45 = v32 * v32;
-  v44 = v31 * v31;
-  v33 = sqrt(a4a * a4a + v44 + v45);
+  outx2 = v32 * v32;
+  outy2 = v31 * v31;
+  v33 = sqrt(a4a * a4a + outy2 + outx2);
   if ( v33 <= 1.0 )
   {
     v41.vDirection.x = 65536;
@@ -1871,12 +1826,12 @@
     v41.vDirection.y = (signed __int64)(1.0 / v33 * v43 * 65536.0);
     v41.vDirection.z = (signed __int64)(1.0 / v33 * a4a * 65536.0);
     v41.uDistance = (signed __int64)v33;
-    v41.uDistanceXZ = (signed __int64)sqrt(v44 + v45);
+    v41.uDistanceXZ = (signed __int64)sqrt(outy2 + outx2);
     v41.uYawAngle = stru_5C6E00->Atan2((signed __int64)v42, (signed __int64)v43);
     v41.uPitchAngle = stru_5C6E00->Atan2(v41.uDistanceXZ, (signed __int64)a4a);
   }
   result = pOut;
-  memcpy(pOut, &v41, 0x1Cu);
+  memcpy(pOut, &v41, sizeof(AIDirection));
   return result;
 }
 
@@ -4233,7 +4188,7 @@
 			for (uint j = 0; j < 22; ++j)
 			{
 				if (j != 10)
-				pActor->pActorBuffs[j]._4585CA(pParty->uTimePlayed);
+				pActor->pActorBuffs[j].IsBuffExpiredToTime(pParty->uTimePlayed);
 			}
 			/*do
 			{
@@ -4332,7 +4287,7 @@
 			{
 				if ( i != 10 )
 				{
-					pActor->pActorBuffs[i]._4585CA(pParty->uTimePlayed);
+					pActor->pActorBuffs[i].IsBuffExpiredToTime(pParty->uTimePlayed);
 				}
 			}
 			if ( (signed __int64)pActor->pActorBuffs[ACTOR_BUFF_SHRINK].uExpireTime < 0 )
--- a/AudioPlayer.h	Fri Jul 12 23:07:14 2013 +0200
+++ b/AudioPlayer.h	Fri Jul 12 23:08:35 2013 +0200
@@ -102,6 +102,8 @@
   SOUND_Arcomage_131 = 0x83,
   SOUND_Arcomage_WallUpgrade = 0x84,
   SOUND_GoldReceived = 0xC8,
+  SOUND_206 =206,
+  SOUND_207 =207,
   SOUND_OpenChest = 208,
   SOUND_PlayerCantCastSpell = 0xD1,
   SOUND_Bell = 0xD9,
--- a/CastSpellInfo.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/CastSpellInfo.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -4165,7 +4165,7 @@
   //  || (result = pTurnEngine->field_4, pTurnEngine->field_4 != 1) && pTurnEngine->field_4 != 3 )
   if (pParty->bTurnBasedModeOn)
   {
-    if (pTurnEngine->field_4 == 1 || pTurnEngine->field_4 == 3)
+    if (pTurnEngine->turn_stage == 1 || pTurnEngine->turn_stage == 3)
       return;
   }
 
--- a/Indoor.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/Indoor.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -768,7 +768,7 @@
             v126 = stru_F8AD28.pDeltaUV[0];
             v128 = stru_F8AD28.pDeltaUV[1];
             v107 = bUseLoResSprites;
-            v7 = sub_423B5D(v1);
+            v7 = GetPortalScreenCoord(v1);
             if ( v7 )
             {
               if ( sub_424829(v7, &stru_F8A590, pBLVRenderParams->field_7C, v1) )
@@ -1352,25 +1352,25 @@
   if (v9 >= 0)
     return;
 
-  auto num_vertices = sub_423B5D(uFaceID);
+  auto num_vertices = GetPortalScreenCoord(uFaceID);
   if (num_vertices < 2)
     return;
 
-  auto face_min_screenspace_x = stru_50B700._screen_space_x[0],
-       face_max_screenspace_x = stru_50B700._screen_space_x[0];
-  auto face_min_screenspace_y = stru_50B700._screen_space_y[0],
-       face_max_screenspace_y = stru_50B700._screen_space_y[0];
+  auto face_min_screenspace_x = PortalFace._screen_space_x[0],
+       face_max_screenspace_x = PortalFace._screen_space_x[0];
+  auto face_min_screenspace_y = PortalFace._screen_space_y[0],
+       face_max_screenspace_y = PortalFace._screen_space_y[0];
   for (uint i = 1; i < num_vertices; ++i)
   {
-    if (face_min_screenspace_x > stru_50B700._screen_space_x[i])
-      face_min_screenspace_x = stru_50B700._screen_space_x[i];
-    if (face_max_screenspace_x < stru_50B700._screen_space_x[i])
-      face_max_screenspace_x = stru_50B700._screen_space_x[i];
-
-    if (face_min_screenspace_y > stru_50B700._screen_space_y[i])
-      face_min_screenspace_y = stru_50B700._screen_space_y[i];
-    if (face_max_screenspace_y < stru_50B700._screen_space_y[i])
-      face_max_screenspace_y = stru_50B700._screen_space_y[i];
+    if (face_min_screenspace_x > PortalFace._screen_space_x[i])
+      face_min_screenspace_x = PortalFace._screen_space_x[i];
+    if (face_max_screenspace_x < PortalFace._screen_space_x[i])
+      face_max_screenspace_x = PortalFace._screen_space_x[i];
+
+    if (face_min_screenspace_y > PortalFace._screen_space_y[i])
+      face_min_screenspace_y = PortalFace._screen_space_y[i];
+    if (face_max_screenspace_y < PortalFace._screen_space_y[i])
+      face_max_screenspace_y = PortalFace._screen_space_y[i];
   }
 	  //_screen_space_x = 719, 568, 493
 	  //savegame: qw , 0Bh and 0x1D4h
@@ -1500,29 +1500,29 @@
       v9 = -v9;
     if ( v9 < 0 )
     {
-      v10 = sub_423B5D(uFaceID);
+      v10 = GetPortalScreenCoord(uFaceID);
       v19 = v10;
       if ( v10 )
       {
-        v11 = stru_50B700._screen_space_x[0];
-        v12 = stru_50B700._screen_space_y[0];
-        v23 = stru_50B700._screen_space_x[0];
+        v11 = PortalFace._screen_space_x[0];
+        v12 = PortalFace._screen_space_y[0];
+        v23 = PortalFace._screen_space_x[0];
         v13 = 1;
-        v22 = stru_50B700._screen_space_y[0];
+        v22 = PortalFace._screen_space_y[0];
         if ( v10 > 1 )
         {
           do
           {
-            v14 = stru_50B700._screen_space_x[v13];
+            v14 = PortalFace._screen_space_x[v13];
             if ( v14 < v23 )
-              v23 = stru_50B700._screen_space_x[v13];
+              v23 = PortalFace._screen_space_x[v13];
             if ( v14 > v11 )
-              v11 = stru_50B700._screen_space_x[v13];
-            v15 = stru_50B700._screen_space_y[v13];
+              v11 = PortalFace._screen_space_x[v13];
+            v15 = PortalFace._screen_space_y[v13];
             if ( v15 < v22 )
-              v22 = stru_50B700._screen_space_y[v13];
+              v22 = PortalFace._screen_space_y[v13];
             if ( v15 > v12 )
-              v12 = stru_50B700._screen_space_y[v13];
+              v12 = PortalFace._screen_space_y[v13];
             v10 = v19;
             ++v13;
           }
@@ -1588,7 +1588,7 @@
       {
         v2 = pBspRenderer->faces[i].uFaceID;
         pBLVRenderParams->field_7C = &pBspRenderer->nodes[pBspRenderer->faces[i].uNodeID].field_C;
-        v3 = sub_423B5D(v2);
+        v3 = GetPortalScreenCoord(v2);
         if ( v3 )
         {
           if ( sub_424829(v3, &stru_F8A590, pBLVRenderParams->field_7C, v1) )
@@ -3893,7 +3893,7 @@
           v6 *= 2;
           v61 = v6;
         }
-        if ( pParty->bTurnBasedModeOn == 1 && pTurnEngine->field_4 == 1 )
+        if ( pParty->bTurnBasedModeOn == 1 && pTurnEngine->turn_stage == 1 )
           v6 = (signed __int64)((double)v61 * flt_6BE3AC_debug_recmod1_x_1_6);
         if ( v6 > 1000 )
           v6 = 1000;
@@ -4196,7 +4196,7 @@
               Actor::AI_FaceObject(v63, v36, v22, (AIDirection *)v22);
               goto LABEL_119;
             }
-            if ( pParty->bTurnBasedModeOn == 1 && (pTurnEngine->field_4 == 2 || pTurnEngine->field_4 == 3) )
+            if ( pParty->bTurnBasedModeOn == 1 && (pTurnEngine->turn_stage == 2 || pTurnEngine->turn_stage == 3) )
               goto LABEL_119;
             if ( v0->pMonsterInfo.uHostilityType )
             {
@@ -4224,7 +4224,7 @@
         }
         else
         {
-          if ( pParty->bTurnBasedModeOn == 1 && (pTurnEngine->field_4 == 2 || pTurnEngine->field_4 == 3) )
+          if ( pParty->bTurnBasedModeOn == 1 && (pTurnEngine->turn_stage == 2 || pTurnEngine->turn_stage == 3) )
             goto LABEL_123;
           if ( !v0->pMonsterInfo.uHostilityType || v56 != v22 )
           {
@@ -6910,10 +6910,10 @@
   //return true;
   if ( pNumVertices <= 1 )
     return false;
-  min_y = stru_50B700._screen_space_y[0];
+  min_y = PortalFace._screen_space_y[0];
   v65 = 0;
-  max_y = stru_50B700._screen_space_y[0];
-  if ( !stru_50B700.field_0 )
+  max_y = PortalFace._screen_space_y[0];
+  if ( !PortalFace.direction )
   {
     v63 = 1;
     v64 = -1;
@@ -6926,15 +6926,15 @@
 
   for ( v6 = 1; v6 < pNumVertices; ++v6 )
   {
-    if ( stru_50B700._screen_space_y[v6] >= min_y )
+    if ( PortalFace._screen_space_y[v6] >= min_y )
     {
-      if ( stru_50B700._screen_space_y[v6] > max_y )
-        max_y = stru_50B700._screen_space_y[v6];
+      if ( PortalFace._screen_space_y[v6] > max_y )
+        max_y = PortalFace._screen_space_y[v6];
     }
-    if ( stru_50B700._screen_space_y[v6] < min_y )
+    if ( PortalFace._screen_space_y[v6] < min_y )
     {
       v65 = v6;
-      min_y = stru_50B700._screen_space_y[v6];
+      min_y = PortalFace._screen_space_y[v6];
     }
   }
   if ( max_y == min_y )
@@ -6955,12 +6955,12 @@
     }
     else
       v10 -= pNumVertices;
-    if ( stru_50B700._screen_space_y[v10] <= stru_50B700._screen_space_y[v65] )
+    if ( PortalFace._screen_space_y[v10] <= PortalFace._screen_space_y[v65] )
     {
       v55 = v10;
       v65 = v10;
     }
-    if ( stru_50B700._screen_space_y[v10] == max_y )
+    if ( PortalFace._screen_space_y[v10] == max_y )
       break;
   }
   v13 = v55 + v64;
@@ -6971,11 +6971,11 @@
   }
   else
     v13 -= pNumVertices;
-  if ( stru_50B700._screen_space_y[v13] != stru_50B700._screen_space_y[v55] )
-  {
-    v62 = stru_50B700._screen_space_x[v55] << 16;
-    v54 = ((stru_50B700._screen_space_x[v13] - stru_50B700._screen_space_x[v55]) << 16) / (stru_50B700._screen_space_y[v13] - stru_50B700._screen_space_y[v55]);
-    a2->viewport_left_side[min_y] = LOWORD(stru_50B700._screen_space_x[v55]);
+  if ( PortalFace._screen_space_y[v13] != PortalFace._screen_space_y[v55] )
+  {
+    v62 = PortalFace._screen_space_x[v55] << 16;
+    v54 = ((PortalFace._screen_space_x[v13] - PortalFace._screen_space_x[v55]) << 16) / (PortalFace._screen_space_y[v13] - PortalFace._screen_space_y[v55]);
+    a2->viewport_left_side[min_y] = LOWORD(PortalFace._screen_space_x[v55]);
   }
   v15 = v65;
   v61 = v65;
@@ -6990,12 +6990,12 @@
     }
     else
       v15 -= pNumVertices;
-    if ( stru_50B700._screen_space_y[v15] <= stru_50B700._screen_space_y[v65] )
+    if ( PortalFace._screen_space_y[v15] <= PortalFace._screen_space_y[v65] )
     {
       v61 = v15;
       v65 = v15;
     }
-    if ( stru_50B700._screen_space_y[v15] == max_y )
+    if ( PortalFace._screen_space_y[v15] == max_y )
       break;
   }
   v18 = v63 + v61;
@@ -7008,11 +7008,11 @@
     v18 -= pNumVertices;
   v19 = v18;
   v20 = v61;
-  if ( stru_50B700._screen_space_y[v19] != stru_50B700._screen_space_y[v61] )
-  {
-    v61 = stru_50B700._screen_space_x[v20] << 16;
-    v53 = ((stru_50B700._screen_space_x[v19] - stru_50B700._screen_space_x[v20]) << 16) / stru_50B700._screen_space_y[v19] - stru_50B700._screen_space_y[v20];
-    a2->viewport_right_side[max_y] = LOWORD(stru_50B700._screen_space_x[v20]);
+  if ( PortalFace._screen_space_y[v19] != PortalFace._screen_space_y[v61] )
+  {
+    v61 = PortalFace._screen_space_x[v20] << 16;
+    v53 = ((PortalFace._screen_space_x[v19] - PortalFace._screen_space_x[v20]) << 16) / PortalFace._screen_space_y[v19] - PortalFace._screen_space_y[v20];
+    a2->viewport_right_side[max_y] = LOWORD(PortalFace._screen_space_x[v20]);
   }
   v22 = min_y;
   if ( min_y <= max_y )
@@ -7022,7 +7022,7 @@
     for ( v70 = min_y; v70 <= max_y; ++v70 )
     {
       v24 = v13;
-      if ( v22 >= stru_50B700._screen_space_y[v13] && v22 != max_y )
+      if ( v22 >= PortalFace._screen_space_y[v13] && v22 != max_y )
       {
         v13 = v64 + v13;
         if ( v13 < pNumVertices )
@@ -7033,15 +7033,15 @@
         else
           v13 -= pNumVertices;
         v26 = v13;
-        //v27 = stru_50B700._screen_space_y[v26] - stru_50B700._screen_space_y[v24];
-        if ( stru_50B700._screen_space_y[v26] - stru_50B700._screen_space_y[v24] > 0 )
+        //v27 = PortalFace._screen_space_y[v26] - PortalFace._screen_space_y[v24];
+        if ( PortalFace._screen_space_y[v26] - PortalFace._screen_space_y[v24] > 0 )
         {
-          v54 = ((stru_50B700._screen_space_x[v26] - stru_50B700._screen_space_x[v24]) << 16) / stru_50B700._screen_space_y[v26] - stru_50B700._screen_space_y[v24];
-          v62 = stru_50B700._screen_space_x[v24] << 16;
+          v54 = ((PortalFace._screen_space_x[v26] - PortalFace._screen_space_x[v24]) << 16) / PortalFace._screen_space_y[v26] - PortalFace._screen_space_y[v24];
+          v62 = PortalFace._screen_space_x[v24] << 16;
         }
       }
       v29 = v18;
-      if ( v70 >= stru_50B700._screen_space_y[v18] && v70 != max_y )
+      if ( v70 >= PortalFace._screen_space_y[v18] && v70 != max_y )
       {
         v18 += v63;
         if ( v18 < pNumVertices )
@@ -7052,11 +7052,11 @@
         else
           v18 -= pNumVertices;
         v31 = v18;
-        //v32 = stru_50B700._screen_space_y[v31] - stru_50B700._screen_space_y[v29];
-        if ( stru_50B700._screen_space_y[v31] - stru_50B700._screen_space_y[v29] > 0 )
+        //v32 = PortalFace._screen_space_y[v31] - PortalFace._screen_space_y[v29];
+        if ( PortalFace._screen_space_y[v31] - PortalFace._screen_space_y[v29] > 0 )
         {
-          v53 = ((stru_50B700._screen_space_x[v31] - stru_50B700._screen_space_x[v29]) << 16) / stru_50B700._screen_space_y[v31] - stru_50B700._screen_space_y[v29];
-          v61 = stru_50B700._screen_space_x[v29] << 16;
+          v53 = ((PortalFace._screen_space_x[v31] - PortalFace._screen_space_x[v29]) << 16) / PortalFace._screen_space_y[v31] - PortalFace._screen_space_y[v29];
+          v61 = PortalFace._screen_space_x[v29] << 16;
         }
       }
 	  //v34 = (char *)a2->array_18 - (char *)a2->array_3D8;
@@ -7164,141 +7164,93 @@
   return true;
 }
 //----- (00423B5D) --------------------------------------------------------
-int __fastcall sub_423B5D(unsigned int uFaceID)
+int __fastcall GetPortalScreenCoord(unsigned int uFaceID)
 {
   BLVFace *pFace; // ebx@1
-  //Vec3_short_ *v2; // esi@1
-  //int v3; // ST28_4@1
-  //__int16 v4; // ST2C_2@1
-  //signed int v5; // esi@1
-  //Vec3_short_ *v6; // eax@4
-  //signed int v7; // edi@5
-  //signed int bFound; // eax@5
-  //signed int v9; // ecx@10
-  //int v10; // eax@10
-  //int v11; // edx@11
-  int v12; // ST28_4@12
+  int pNextVertices; // edx@11
+  int t; // ST28_4@12
   signed int v13; // edx@12
   signed __int64 v14; // qtt@12
-  char *v15; // ebx@12
-  int v16; // ST28_4@14
   signed int v17; // eax@14
   signed __int64 v18; // qtt@14
-  signed int v19; // edx@15
   signed int v20; // edx@17
-  signed int v21; // ebx@19
-  signed int v22; // esi@20
-  int v23; // edi@21
-  int v24; // eax@21
   int v25; // eax@22
-  int v26; // eax@22
-  signed int v27; // ST30_4@24
-  signed __int64 v28; // qtt@24
-  int v29; // ST18_4@25
-  int v30; // eax@26
+  int pScreenX; // eax@22
   int v31; // eax@27
-  int v32; // eax@27
-  signed int v33; // ST30_4@29
-  signed __int64 v34; // qtt@29
+  int pScreenY; // eax@27
   int v35; // ST30_4@30
-  signed int v36; // edi@31
-  unsigned int v37; // eax@31
-  bool v38; // edx@31
-  int v39; // ecx@31
-  int v40; // ecx@32
-  int v41; // esi@32
-  int v42; // eax@34
-  signed int v43; // ebx@41
-  unsigned int v44; // eax@41
-  signed int v45; // ecx@42
-  int v46; // esi@42
-  int v47; // eax@44
-  signed int v48; // edi@51
-  unsigned int v49; // eax@51
-  bool v50; // edx@51
-  int v51; // ecx@51
-  int v52; // ecx@52
-  int v53; // esi@52
-  int v54; // eax@54
-  int v55; // ebx@61
-  unsigned int v56; // eax@61
-  signed int v57; // ecx@62
-  int v58; // esi@62
-  int v59; // eax@64
-  char v61; // zf@72
+  signed int left_num_vertices; // edi@31
+  signed int right_num_vertices; // ebx@41
+  signed int top_num_vertices; // edi@51
+  int bottom_num_vertices; // ebx@61
   signed int v62; // edx@75
   int v63; // ecx@76
   int v64; // esi@76
   int v65; // ecx@83
-  //signed int v66; // [sp+14h] [bp-14h]@3
-  int v67; // [sp+14h] [bp-14h]@34
-  int v68; // [sp+14h] [bp-14h]@44
-  int v69; // [sp+14h] [bp-14h]@54
-  int v70; // [sp+14h] [bp-14h]@64
   signed int v71; // [sp+14h] [bp-14h]@75
-  //IndoorCameraD3D *_this; // [sp+18h] [bp-10h]@1
   bool current_vertices_flag; // [sp+18h] [bp-10h]@9
   int thisb; // [sp+18h] [bp-10h]@12
-  int thisc; // [sp+18h] [bp-10h]@20
-  bool thisd; // [sp+18h] [bp-10h]@41
-  bool thise; // [sp+18h] [bp-10h]@61
   int thisf; // [sp+18h] [bp-10h]@74
   signed int depth_num_vertices; // [sp+1Ch] [bp-Ch]@9
   int v80; // [sp+1Ch] [bp-Ch]@76
   bool next_vertices_flag; // [sp+20h] [bp-8h]@10
-  bool v82; // [sp+20h] [bp-8h]@32
-  bool v83; // [sp+20h] [bp-8h]@42
-  bool v84; // [sp+20h] [bp-8h]@52
-  bool v85; // [sp+20h] [bp-8h]@62
-  //signed int v86; // [sp+24h] [bp-4h]@9
-  signed int v87; // [sp+24h] [bp-4h]@19
-  signed int v88; // [sp+24h] [bp-4h]@31
-  signed int v89; // [sp+24h] [bp-4h]@41
   signed int v90; // [sp+24h] [bp-4h]@51
-  signed int v91; // [sp+24h] [bp-4h]@61
 
   pFace = &pIndoor->pFaces[uFaceID];
-  memset(&stru_50B700, 0, sizeof(stru367));
-  //_this = pGame->pIndoorCameraD3D;
-  //v2 = &pIndoor->pVertices[pIndoor->pFaces[uFaceID].pVertexIDs[0]];
-  //v3 = *(_DWORD *)pIndoor->pVertices[pIndoor->pFaces[uFaceID].pVertexIDs[0]].x;
-  //v4 = pIndoor->pVertices[pIndoor->pFaces[uFaceID].pVertexIDs[0]].z;
-  //v5 = 0;
+  memset(&PortalFace, 0, sizeof(stru367));
+
+  //get direction the face(определение направленности фейса)*********************************************************************************
   if ( pFace->pFacePlane_old.vNormal.x * (pIndoor->pVertices[pIndoor->pFaces[uFaceID].pVertexIDs[0]].x - pBLVRenderParams->vPartyPos.x)
      + pFace->pFacePlane_old.vNormal.y * (pIndoor->pVertices[pIndoor->pFaces[uFaceID].pVertexIDs[0]].y - pBLVRenderParams->vPartyPos.y)
      + pFace->pFacePlane_old.vNormal.z * (pIndoor->pVertices[pIndoor->pFaces[uFaceID].pVertexIDs[0]].z - pBLVRenderParams->vPartyPos.z) < 0 )
   {
-    stru_50B700.field_0 = 1;
+    PortalFace.direction = true;
   }
   else
   {
-    stru_50B700.field_0 = 0;
+    PortalFace.direction = false;
     if ( !(pFace->Portal()) )
       return 0;
   }
-  //v66 = pFace->uNumVertices;
+  //*****************************************************************************************************************************************
+  //generate/cinvertetion in camera location coordinates(генерация/конвертирование в координаты пространства камеры)
+
+  //for new coordinates:
+  //int x = 0x AAAA BBBB;
+  //AAAA - integer(целая часть), BBBB - fractional(дробная)
+  //float v = HIWORD(x) + LOWORD(x) / 65535.0f;
+  //0x0351A281 это 849(351 в шестнадцатиричной) в целой части и A281 в дробной(хотя как точно BBBB считалась не помню)
+  //if in HIWORD: FFFF = -1
+  //FFFE = -2
+  //FFFD = -3
+  //....
+  //8000 = -32767
+  //7FFF = 32767
+  //7FFE = 32766
+  //если в LOWORD например лежит FFFF то не совсем понятно, что это
+  //потому что если и hiword и loword равны FFFF FFFF то двойное отрицание как бы, нужно тестировать что конкретно получается чтобы понять что это значит
+  //всё что больше 7FFF для верхнего слова это идёт уже с минусом/Nomad/
+
   if ( (signed int)pFace->uNumVertices > 0 )
   {
     for (uint i = 0; i < pFace->uNumVertices; ++i)
     {
-      //v6 = &pIndoor->pVertices[pFace->pVertexIDs[v5]];
-      pGame->pIndoorCameraD3D->ApplyViewTransform_TrueIfStillVisible(
-        pIndoor->pVertices[pFace->pVertexIDs[i]].x,
-        pIndoor->pVertices[pFace->pVertexIDs[i]].y,
-        pIndoor->pVertices[pFace->pVertexIDs[i]].z,
-        &stru_50B700._view_transformed_xs[i + 3],
-        &stru_50B700._view_transformed_zs[i + 3],
-        &stru_50B700._view_transformed_ys[i + 3],
-        0);
+      pGame->pIndoorCameraD3D->ApplyViewTransform_TrueIfStillVisible(pIndoor->pVertices[pFace->pVertexIDs[i]].x,
+                                                                     pIndoor->pVertices[pFace->pVertexIDs[i]].y,
+                                                                     pIndoor->pVertices[pFace->pVertexIDs[i]].z,
+        &PortalFace._view_transformed_z[i + 3], &PortalFace._view_transformed_x[i + 3], &PortalFace._view_transformed_y[i + 3], 0);
     }
   }
-  //v7 = pFace->uNumVertices;
+  //х: 55681665, 55681665, 53050369, 47787777, 45156481, 45156481
+  //x: 0x0351 a281,0x0351 a281,0x0329 7c01,0x02d9 2f01,0x02b1 0881,0x02b1 0881
+  //*****************************************************************************************************************************************
+  //check vertices for the nearest plane(проверка вершин есть ли в области за ближайшей плоскостью)
   if ( pFace->uNumVertices <= 0 )
     return 0;
   bool bFound = false;
   for (uint i = 0; i < pFace->uNumVertices; ++i)
   {
-    if ( stru_50B700._view_transformed_xs[i + 3] >= 524288 )
+    if ( PortalFace._view_transformed_z[i + 3] >= 524288 )// 8.0(0x80000)
     {
       bFound = true;
       break;
@@ -7306,345 +7258,314 @@
   }
   if ( !bFound )
     return 0;
-
+  //*****************************************************************************************************************************************
+  //check for near clip plane(проверка по ближней границе)
+  //   
+  //     v0                 v1
+  //      ._________________.
+  //     /                   \
+  //    /                     \
+  // v5.                       . v2
+  //   |                       |
+  //   |                       |
+  //   |                       |
+  //  ---------------------------- 8.0(near_clip)
+  //   |                       |
+  //   ._______________________.
+  //  v4                        v3
   depth_num_vertices = 0;
-  stru_50B700._view_transformed_xs[pFace->uNumVertices + 3] = stru_50B700._view_transformed_xs[3];
-  stru_50B700._view_transformed_zs[pFace->uNumVertices + 3] = stru_50B700._view_transformed_zs[3];
-  stru_50B700._view_transformed_ys[pFace->uNumVertices + 3] = stru_50B700._view_transformed_ys[3];
-  current_vertices_flag = stru_50B700._view_transformed_xs[3] >= 524288;
-  //v86 = 1;
+  PortalFace._view_transformed_z[pFace->uNumVertices + 3] = PortalFace._view_transformed_z[3];
+  PortalFace._view_transformed_x[pFace->uNumVertices + 3] = PortalFace._view_transformed_x[3];
+  PortalFace._view_transformed_y[pFace->uNumVertices + 3] = PortalFace._view_transformed_y[3];
+  current_vertices_flag = PortalFace._view_transformed_z[3] >= 524288;// 8.0(0x80000)
   if ( pFace->uNumVertices >= 1 )
   {
     for ( uint i = 1; i <= pFace->uNumVertices; ++i)
     {
-      //v9 = v86;
-      //v10 = stru_50B700._view_transformed_xs[v86 + 3];
-      next_vertices_flag = stru_50B700._view_transformed_xs[i + 3] >= 524288;
-      if ( current_vertices_flag ^ next_vertices_flag )
+      next_vertices_flag = PortalFace._view_transformed_z[i + 3] >= 524288;// 8.0(0x80000)
+      if ( current_vertices_flag ^ next_vertices_flag )//или текущая или следующая вершина за ближней границей
       {
-        //v11 = stru_50B700._view_transformed_xs[i + 2];
-        if ( stru_50B700._view_transformed_xs[i + 3] >= 524288 )
+        if ( next_vertices_flag )//следующая вершина за ближней границей
         {
-          v12 = stru_50B700._view_transformed_xs[i + 3] - stru_50B700._view_transformed_xs[i + 2];
-          v13 = 524288 - stru_50B700._view_transformed_xs[i + 2];
+          v13 = 524288 - PortalFace._view_transformed_z[i + 2];
           LODWORD(v14) = v13 << 16;
           HIDWORD(v14) = v13 >> 16;
-          v15 = (char *)&stru_50B700._view_transformed_ys[i + 2];
-          stru_50B700._view_transformed_zs[depth_num_vertices] = ((unsigned __int64)((stru_50B700._view_transformed_zs[i + 3]
-                                                                     - stru_50B700._view_transformed_zs[i + 2])
-                                                                    * v14
-                                                                    / v12) >> 16)
-                                                + stru_50B700._view_transformed_zs[i + 2];
-          thisb = (unsigned __int64)((stru_50B700._view_transformed_ys[i + 3] - stru_50B700._view_transformed_ys[i + 2])
-                                   * v14
-                                   / v12) >> 16;
+          //t = near_clip - v0.z / v1.z - v0.z
+          t = v14 / PortalFace._view_transformed_z[i + 3] - PortalFace._view_transformed_z[i + 2];
+          //New_x = (v1.x - v0.x)*t + v0.x
+          PortalFace._view_transformed_x[depth_num_vertices] = ((unsigned __int64)((PortalFace._view_transformed_x[i + 3]
+                                                           - PortalFace._view_transformed_x[i + 2]) * t) >> 16) + PortalFace._view_transformed_x[i + 2];
+          //New_y = (v1.y - v0.y)*t + v0.y
+          PortalFace._view_transformed_y[depth_num_vertices] = ((unsigned __int64)((PortalFace._view_transformed_y[i + 3] - PortalFace._view_transformed_y[i + 2])
+                                   * t) >> 16) + PortalFace._view_transformed_y[i + 2];
+          //New_z = 8.0(0x80000)
+          PortalFace._view_transformed_z[depth_num_vertices] = 524288;
         }
-        else
+        else// текущая вершина за ближней границей
         {
-          v16 = stru_50B700._view_transformed_xs[i + 2] - stru_50B700._view_transformed_xs[i + 3];
-          v17 = 524288 - stru_50B700._view_transformed_xs[i + 3];
+          v17 = 524288 - PortalFace._view_transformed_z[i + 3];
           LODWORD(v18) = v17 << 16;
           HIDWORD(v18) = v17 >> 16;
-          v15 = (char *)&stru_50B700._view_transformed_ys[i + 3];
-          stru_50B700._view_transformed_zs[depth_num_vertices] = ((unsigned __int64)((stru_50B700._view_transformed_zs[i + 2]
-                                                                     - stru_50B700._view_transformed_zs[i + 3])
-                                                                    * v18
-                                                                    / v16) >> 16)
-                                                + stru_50B700._view_transformed_zs[i + 3];
-          thisb = (unsigned __int64)((stru_50B700._view_transformed_ys[i + 2] - stru_50B700._view_transformed_ys[i + 3])
-                                   * v18
-                                   / v16) >> 16;
+          //t = near_clip - v1.z / v0.z - v1.z
+          t = v18 / PortalFace._view_transformed_z[i + 2] - PortalFace._view_transformed_z[i + 3];
+          //New_x = (v0.x - v1.x)*t + v1.x
+          PortalFace._view_transformed_x[depth_num_vertices] = ((unsigned __int64)((PortalFace._view_transformed_x[i + 2]
+                                                   - PortalFace._view_transformed_x[i + 3]) * t) >> 16) + PortalFace._view_transformed_x[i + 3];
+          //New_y = (v0.x - v1.y)*t + v1.y
+          PortalFace._view_transformed_y[depth_num_vertices] = ((unsigned __int64)((PortalFace._view_transformed_y[i + 2] - PortalFace._view_transformed_y[i + 3])
+                                   * t) >> 16) + PortalFace._view_transformed_y[i + 3];
+          //New_z = 8.0(0x80000)
+          PortalFace._view_transformed_z[depth_num_vertices] = 524288;
         }
-        v19 = depth_num_vertices++;
-        //v7 = pFace->uNumVertices;
-        stru_50B700._view_transformed_ys[v19] = thisb + *(_DWORD *)v15;
-        stru_50B700._view_transformed_xs[v19] = 524288;
+        depth_num_vertices++;
       }
-      if ( next_vertices_flag )
+      if ( next_vertices_flag )//если следующая вершина за ближней границей 
       {
-        v20 = depth_num_vertices++;
-        stru_50B700._view_transformed_xs[v20] = stru_50B700._view_transformed_xs[i + 3];
-        stru_50B700._view_transformed_zs[v20] = stru_50B700._view_transformed_zs[i + 3];
-        stru_50B700._view_transformed_ys[v20] = stru_50B700._view_transformed_ys[i + 3];
+        pNextVertices = depth_num_vertices++;
+        PortalFace._view_transformed_z[pNextVertices] = PortalFace._view_transformed_z[i + 3];
+        PortalFace._view_transformed_x[pNextVertices] = PortalFace._view_transformed_x[i + 3];
+        PortalFace._view_transformed_y[pNextVertices] = PortalFace._view_transformed_y[i + 3];
       }
       current_vertices_flag = next_vertices_flag;
     }
   }
-
-  v87 = 0;
-  v21 = depth_num_vertices;
-  stru_50B700._view_transformed_xs[depth_num_vertices] = stru_50B700._view_transformed_xs[0];
-  stru_50B700._view_transformed_zs[depth_num_vertices] = stru_50B700._view_transformed_zs[0];
-  for ( stru_50B700._view_transformed_ys[depth_num_vertices] = stru_50B700._view_transformed_ys[0];
-        v87 < depth_num_vertices;
-        stru_50B700._screen_space_y[v22 + 12] = pBLVRenderParams->uViewportCenterY - v35 )
-  {
-    v22 = v87;
-    thisc = abs(stru_50B700._view_transformed_xs[v87]);
-    if ( abs(stru_50B700._view_transformed_zs[v87]) >> 13 <= thisc )
-    {
-      v27 = stru_50B700._view_transformed_zs[v22];
-      LODWORD(v28) = v27 << 16;
-      HIDWORD(v28) = v27 >> 16;
-      v26 = v28 / stru_50B700._view_transformed_xs[v22];
-      v23 = 0;
-    }
+  //результат: нет моргания на границе портала(когда проходим сквозь портал)
+  //************************************************************************************************************************************
+  //convertion in screen coordinates(конвертирование в координаты экрана)
+  PortalFace._view_transformed_z[depth_num_vertices] = PortalFace._view_transformed_z[0];
+  PortalFace._view_transformed_x[depth_num_vertices] = PortalFace._view_transformed_x[0];
+  PortalFace._view_transformed_y[depth_num_vertices] = PortalFace._view_transformed_y[0];
+  for ( uint i = 0; i < depth_num_vertices; ++i )
+  {
+    if ( (abs(PortalFace._view_transformed_x[i]) >> 13) <= abs(PortalFace._view_transformed_z[i]) )
+      pScreenX = fixpoint_div(PortalFace._view_transformed_x[i],  PortalFace._view_transformed_z[i]);
     else
     {
-      v23 = 0;
-      v24 = 0;
-      if ( stru_50B700._view_transformed_zs[v22] >= 0 )
+      if ( PortalFace._view_transformed_x[i] >= 0 )
       {
-        //LOBYTE(v24) = stru_50B700._view_transformed_xs[v22] >= 0;
-        //v26 = ((v24 - 1) & 0xFF800000) + 4194304;
-        if (stru_50B700._view_transformed_xs[v22] >= 0)
-          v26 = 0x400000;   // 64.0
+        if (PortalFace._view_transformed_z[i] >= 0)
+          pScreenX = 0x400000;   // 64.0
         else
-          v26 = 0xFFC00000;
+          pScreenX = 0xFFC00000;  // -63.0
       }
       else
       {
-        //LOBYTE(v24) = stru_50B700._view_transformed_xs[v22] >= 0;
-        //v25 = v24 - 1;
-        //v26 = (v25 & 0x800000) - 4194304;
-        if (stru_50B700._view_transformed_xs[v22] >= 0)
-          v26 = 0xFFC00000;  // -63.0
+        if (PortalFace._view_transformed_z[i] >= 0)
+          pScreenX = 0xFFC00000;  // -63.0
         else
-          v26 = 0x400000;
+          pScreenX = 0x400000; // 64.0
       }
     }
 
-    v29 = stru_50B700._view_transformed_ys[v22];
-    stru_50B700._screen_space_x[v22 + 12] = v26;
-    if ( abs(v29) >> 13 <= thisc )
-    {
-      v33 = stru_50B700._view_transformed_ys[v22];
-      LODWORD(v34) = v33 << 16;
-      HIDWORD(v34) = v33 >> 16;
-      v32 = v34 / stru_50B700._view_transformed_xs[v22];
-    }
+    if ( (abs(PortalFace._view_transformed_y[i]) >> 13) <= abs(PortalFace._view_transformed_z[i]) )
+      pScreenY = fixpoint_div(PortalFace._view_transformed_y[i],  PortalFace._view_transformed_z[i]);
     else
     {
-      v30 = 0;
-      if ( stru_50B700._view_transformed_ys[v22] >= v23 )
+      if ( PortalFace._view_transformed_y[i] >= 0 )
       {
-        //LOBYTE(v30) = stru_50B700._view_transformed_xs[v22] >= v23;
-        //v32 = ((v30 - 1) & 0xFF800000) + 4194304;
-        if (stru_50B700._view_transformed_xs[v22] >= v23)
-          v32 = 0x400000;   // 64.0
+        if (PortalFace._view_transformed_z[i] >= 0)
+          pScreenY = 0x400000;   // 64.0
         else
-          v32 = 0xFFC00000;
+          pScreenY = 0xFFC00000;  // -63.0
       }
       else
       {
-        //LOBYTE(v30) = stru_50B700._view_transformed_xs[v22] >= v23;
-        //v31 = v30 - 1;
-        //v32 = (v31 & 0x800000) - 4194304;
-        if (stru_50B700._view_transformed_xs[v22] >= v23)
-          v32 = 0xFFC00000;  // -63.0
+        if (PortalFace._view_transformed_z[i] >= 0)
+          pScreenY = 0xFFC00000;  // -63.0
         else
-          v32 = 0x400000;
+          pScreenY = 0x400000;  // 64.0
       }
     }
-    stru_50B700._screen_space_y[v22 + 12] = v32;
-    stru_50B700._screen_space_x[v22 + 12] = (unsigned __int64)(SHIWORD(pBLVRenderParams->field_40)
-                                                             * (signed __int64)stru_50B700._screen_space_x[v22 + 12]) >> 16;
-    v35 = (unsigned __int64)(SHIWORD(pBLVRenderParams->field_40) * (signed __int64)stru_50B700._screen_space_y[v22 + 12]) >> 16;
-    stru_50B700._screen_space_x[v22 + 12] = pBLVRenderParams->uViewportCenterX - stru_50B700._screen_space_x[v22 + 12];
-    ++v87;
-  }
-  v36 = 0;
-  stru_50B700._screen_space_x[v21 + 12] = stru_50B700._screen_space_x[12];
-  stru_50B700._screen_space_y[v21 + 12] = stru_50B700._screen_space_y[12];
-  v37 = pBLVRenderParams->uViewportX;
-  v38 = stru_50B700._screen_space_x[12] < (signed int)pBLVRenderParams->uViewportX;
-  LOBYTE(v38) = stru_50B700._screen_space_x[12] >= (signed int)pBLVRenderParams->uViewportX;
-  v39 = 1;
-  v88 = 1;
+    PortalFace._screen_space_x[i + 12] = pBLVRenderParams->uViewportCenterX - ((unsigned __int64)(SHIWORD(pBLVRenderParams->field_40) * (signed __int64)pScreenX) >> 16);
+    PortalFace._screen_space_y[i + 12] = pBLVRenderParams->uViewportCenterY - ((unsigned __int64)(SHIWORD(pBLVRenderParams->field_40) * (signed __int64)pScreenY) >> 16);
+  }
+  //х -34, 4, 61, 83, 83, -34
+  //y 211, 152, 172, 216, 273, 293
+  // результат: при повороте камеры, когда граница портала сдвигается к краю экрана, портал остается прозрачным(видимым)
+  //******************************************************************************************************************************************
+  //check for left_clip plane(порверка по левой границе)
+  left_num_vertices = 0;
+  PortalFace._screen_space_x[depth_num_vertices + 12] = PortalFace._screen_space_x[12];
+  PortalFace._screen_space_y[depth_num_vertices + 12] = PortalFace._screen_space_y[12];
+  current_vertices_flag = PortalFace._screen_space_x[12] >= (signed int)pBLVRenderParams->uViewportX;
   if ( depth_num_vertices < 1 )
     return 0;
-  do
-  {
-    v40 = v39;
-    v41 = stru_50B700._screen_space_x[v40 + 12];
-    v82 = v41 >= (signed int)v37;
-    if ( v38 ^ v82 )
+  for ( uint i = 1; i <= depth_num_vertices; ++i )
+  {
+    next_vertices_flag = PortalFace._screen_space_x[i + 12] >= (signed int)pBLVRenderParams->uViewportX;
+    if ( current_vertices_flag ^ next_vertices_flag )
     {
-      if ( v41 >= (signed int)v37 )
+      if ( next_vertices_flag )
       {
-        v67 = (signed int)(v37 - stru_50B700._screen_space_x[v40 + 11])
-            * (signed __int64)(stru_50B700._screen_space_y[v40 + 12] - stru_50B700._screen_space_y[v40 + 11])
-            / (v41 - stru_50B700._screen_space_x[v40 + 11]);
-        v42 = stru_50B700._screen_space_y[v40 + 11];
+        //t = left_clip - v0.x / v1.x - v0.x
+        t = (signed __int64)(pBLVRenderParams->uViewportX - PortalFace._screen_space_x[i + 11])
+                          / (PortalFace._screen_space_x[i + 12] - PortalFace._screen_space_x[i + 11]);
+        //New_y = (v1.y - v0.y)*t + v0.y
+        PortalFace._screen_space_y[left_num_vertices + 9] = (signed int)(PortalFace._screen_space_y[i + 12]- PortalFace._screen_space_y[i + 11])
+                                                            * t + PortalFace._screen_space_y[i + 11];
+        //New_x = left_clip
+        PortalFace._screen_space_x[left_num_vertices + 9] = pBLVRenderParams->uViewportX;
       }
       else
       {
-        v67 = (signed int)(v37 - v41)
-            * (signed __int64)(stru_50B700._screen_space_y[v40 + 11] - stru_50B700._screen_space_y[v40 + 12])
-            / (stru_50B700._screen_space_x[v40 + 11] - v41);
-        v42 = stru_50B700._screen_space_y[v40 + 12];
+        //t = left_clip - v1.x / v0.x - v1.x
+        t = (signed __int64)(pBLVRenderParams->uViewportX - PortalFace._screen_space_x[i + 12])
+                          / (PortalFace._screen_space_x[i + 11] - PortalFace._screen_space_x[i + 12]);
+        //New_y = (v0.y - v1.y)*t + v1.y
+        PortalFace._screen_space_y[left_num_vertices + 9] = (signed int)( PortalFace._screen_space_y[i + 11] - PortalFace._screen_space_y[i + 12])
+                                                            * t + PortalFace._screen_space_y[i + 12];
+        //New_x = left_clip
+        PortalFace._screen_space_x[left_num_vertices + 9] = pBLVRenderParams->uViewportX;
       }
-      ++v36;
-      stru_50B700._screen_space_y[v36 + 8] = v67 + v42;
-      v37 = pBLVRenderParams->uViewportX;
-      stru_50B700._screen_space_x[v36 + 8] = pBLVRenderParams->uViewportX;
+      left_num_vertices++;
     }
-    v38 = v82;
-    if ( v82 )
+    if ( next_vertices_flag )
     {
-      stru_50B700._screen_space_x[v36 + 9] = stru_50B700._screen_space_x[v40 + 12];
-      stru_50B700._screen_space_y[v36++ + 9] = stru_50B700._screen_space_y[v40 + 12];
+      pNextVertices = left_num_vertices++;
+      PortalFace._screen_space_x[pNextVertices + 9] = PortalFace._screen_space_x[i + 12];
+      PortalFace._screen_space_y[pNextVertices + 9] = PortalFace._screen_space_y[i + 12];
     }
-    v39 = v88++ + 1;
-  }
-  while ( v88 <= depth_num_vertices );
-  if ( !v36
-    || (v43 = 0,
-        stru_50B700._screen_space_x[v36 + 9] = stru_50B700._screen_space_x[9],
-        stru_50B700._screen_space_y[v36 + 9] = stru_50B700._screen_space_y[9],
-        v44 = pBLVRenderParams->uViewportZ,
-        thisd = stru_50B700._screen_space_x[9] <= (signed int)pBLVRenderParams->uViewportZ,
-        v89 = 1,
-        v36 < 1) )
+    current_vertices_flag = next_vertices_flag;
+  }
+//*********************************************************************************************************************************
+//for right_clip plane(проверка по правой плоскости)
+  right_num_vertices = 0;
+  PortalFace._screen_space_x[left_num_vertices + 9] = PortalFace._screen_space_x[9];
+  PortalFace._screen_space_y[left_num_vertices + 9] = PortalFace._screen_space_y[9];
+  current_vertices_flag = PortalFace._screen_space_x[9] <= (signed int)pBLVRenderParams->uViewportZ;
+  if (left_num_vertices < 1)
     return 0;
-  do
-  {
-    v45 = v89;
-    v46 = stru_50B700._screen_space_x[v89 + 9];
-    v83 = v46 <= (signed int)v44;
-    if ( thisd ^ v83 )
+  for ( uint i = 1; i <= left_num_vertices; ++i )
+  {
+    next_vertices_flag = PortalFace._screen_space_x[i + 9] <= (signed int)pBLVRenderParams->uViewportZ;
+    if ( current_vertices_flag ^ next_vertices_flag )
     {
-      if ( v46 <= (signed int)v44 )
+      if ( next_vertices_flag )
       {
-        v68 = (signed int)(v44 - stru_50B700._screen_space_x[v45 + 8])
-            * (signed __int64)(stru_50B700._screen_space_y[v45 + 9] - stru_50B700._screen_space_y[v45 + 8])
-            / (v46 - stru_50B700._screen_space_x[v45 + 8]);
-        v47 = stru_50B700._screen_space_y[v45 + 8];
+        //t = right_clip - v1.x / v0.x - v1.x
+        t = (signed __int64)(pBLVRenderParams->uViewportZ - PortalFace._screen_space_x[i + 9])
+                          / (PortalFace._screen_space_x[i + 8] - PortalFace._screen_space_x[i + 9]);
+        //New_y = (v0.y - v1.y)*t + v1.y
+        PortalFace._screen_space_y[right_num_vertices + 6] = (signed int)(PortalFace._screen_space_y[i + 8] - PortalFace._screen_space_y[i + 9])
+                                                           * t + PortalFace._screen_space_y[i + 9];
+        //New_x = right_clip
+        PortalFace._screen_space_x[right_num_vertices + 6] = pBLVRenderParams->uViewportZ;
       }
       else
       {
-        v68 = (signed int)(v44 - v46)
-            * (signed __int64)(stru_50B700._screen_space_y[v45 + 8] - stru_50B700._screen_space_y[v45 + 9])
-            / (stru_50B700._screen_space_x[v45 + 8] - v46);
-        v47 = stru_50B700._screen_space_y[v45 + 9];
+        //t = right_clip - v0.x / v1.x - v0.x
+        t = (signed __int64)(pBLVRenderParams->uViewportZ - PortalFace._screen_space_x[i + 8])
+                          / (PortalFace._screen_space_x[i + 9] - PortalFace._screen_space_x[i + 8]);
+        //New_y = (v1.y - v0.y)*t + v0.y
+        PortalFace._screen_space_y[right_num_vertices + 6] = (signed int)(PortalFace._screen_space_y[i + 9] - PortalFace._screen_space_y[i + 8])
+                                                           * t + PortalFace._screen_space_y[i + 8];
+        //New_x = right_clip
+        PortalFace._screen_space_x[right_num_vertices + 6] = pBLVRenderParams->uViewportZ;
       }
-      ++v43;
-      stru_50B700._screen_space_y[v43 + 5] = v68 + v47;
-      v44 = pBLVRenderParams->uViewportZ;
-      stru_50B700._screen_space_x[v43 + 5] = pBLVRenderParams->uViewportZ;
+      right_num_vertices++;
     }
-    if ( v83 )
+    if ( next_vertices_flag )
     {
-      stru_50B700._screen_space_x[v43 + 6] = stru_50B700._screen_space_x[v45 + 9];
-      stru_50B700._screen_space_y[v43++ + 6] = stru_50B700._screen_space_y[v45 + 9];
+      pNextVertices = right_num_vertices++;
+      PortalFace._screen_space_x[pNextVertices + 6] = PortalFace._screen_space_x[i + 9];
+      PortalFace._screen_space_y[pNextVertices + 6] = PortalFace._screen_space_y[i + 9];
     }
-    ++v89;
-    thisd = v83;
-  }
-  while ( v89 <= v36 );
-  if ( !v43
-    || (v48 = 0,
-        stru_50B700._screen_space_x[v43 + 6] = stru_50B700._screen_space_x[6],
-        stru_50B700._screen_space_y[v43 + 6] = stru_50B700._screen_space_y[6],
-        v49 = pBLVRenderParams->uViewportY,
-        v50 = stru_50B700._screen_space_y[6] < (signed int)pBLVRenderParams->uViewportY,
-        LOBYTE(v50) = stru_50B700._screen_space_y[6] >= (signed int)pBLVRenderParams->uViewportY,
-        v51 = 1,
-        v90 = 1,
-        v43 < 1) )
+    current_vertices_flag = next_vertices_flag;
+  }
+  //************************************************************************************************************************************
+  // for top clip plane
+  top_num_vertices = 0;
+  PortalFace._screen_space_x[right_num_vertices + 6] = PortalFace._screen_space_x[6];
+  PortalFace._screen_space_y[right_num_vertices + 6] = PortalFace._screen_space_y[6];
+
+  current_vertices_flag = PortalFace._screen_space_y[6] >= (signed int)pBLVRenderParams->uViewportY;
+  if ( right_num_vertices < 1 )
     return 0;
-  do
-  {
-    v52 = v51;
-    v53 = stru_50B700._screen_space_y[v52 + 6];
-    v84 = v53 >= (signed int)v49;
-    if ( v50 ^ v84 )
+  for ( uint i = 1; i <= right_num_vertices; ++i )
+  {
+    next_vertices_flag = PortalFace._screen_space_y[i + 6] >= (signed int)pBLVRenderParams->uViewportY;
+    if ( current_vertices_flag ^ next_vertices_flag )
     {
-      if ( v53 >= (signed int)v49 )
+      if ( next_vertices_flag )
       {
-        v69 = (signed int)(v49 - stru_50B700._screen_space_y[v52 + 5])
-            * (signed __int64)(stru_50B700._screen_space_x[v52 + 6] - stru_50B700._screen_space_x[v52 + 5])
-            / (v53 - stru_50B700._screen_space_y[v52 + 5]);
-        v54 = stru_50B700._screen_space_x[v52 + 5];
+        t = (signed __int64)(pBLVRenderParams->uViewportY  - PortalFace._screen_space_y[i + 5])
+                          / (PortalFace._screen_space_y[i + 6] - PortalFace._screen_space_y[i + 5]);
+        PortalFace._screen_space_x[top_num_vertices + 3] = (signed int)(PortalFace._screen_space_x[i + 6] - PortalFace._screen_space_x[i + 5])
+            * t + PortalFace._screen_space_x[i + 5];
+        PortalFace._screen_space_y[top_num_vertices + 3] = pBLVRenderParams->uViewportY;
       }
       else
       {
-        v69 = (signed int)(v49 - v53)
-            * (signed __int64)(stru_50B700._screen_space_x[v52 + 5] - stru_50B700._screen_space_x[v52 + 6])
-            / (stru_50B700._screen_space_y[v52 + 5] - v53);
-        v54 = stru_50B700._screen_space_x[v52 + 6];
+        t = (signed __int64)(pBLVRenderParams->uViewportY - PortalFace._screen_space_y[i + 6])
+                          / (PortalFace._screen_space_y[i + 5] - PortalFace._screen_space_y[i + 6]);
+        PortalFace._screen_space_x[top_num_vertices + 3] = (signed int)(PortalFace._screen_space_x[i + 5]- PortalFace._screen_space_x[i + 6])
+            * t + PortalFace._screen_space_x[i + 6];
+        PortalFace._screen_space_y[top_num_vertices + 3] = pBLVRenderParams->uViewportY;
       }
-      ++v48;
-      stru_50B700._screen_space_x[v48 + 2] = v69 + v54;
-      v49 = pBLVRenderParams->uViewportY;
-      stru_50B700._screen_space_y[v48 + 2] = pBLVRenderParams->uViewportY;
+      top_num_vertices++;
     }
-    v50 = v84;
-    if ( v84 )
+    current_vertices_flag = next_vertices_flag;
+    if ( next_vertices_flag )
     {
-      stru_50B700._screen_space_x[v48 + 3] = stru_50B700._screen_space_x[v52 + 6];
-      stru_50B700._screen_space_y[v48++ + 3] = stru_50B700._screen_space_y[v52 + 6];
+      pNextVertices = top_num_vertices++;
+      PortalFace._screen_space_x[pNextVertices + 3] = PortalFace._screen_space_x[i + 6];
+      PortalFace._screen_space_y[pNextVertices + 3] = PortalFace._screen_space_y[i + 6];
     }
-    v51 = v90++ + 1;
-  }
-  while ( v90 <= v43 );
-  if ( !v48
-    || (v55 = 0,
-        stru_50B700._screen_space_x[v48 + 3] = stru_50B700._screen_space_x[3],
-        stru_50B700._screen_space_y[v48 + 3] = stru_50B700._screen_space_y[3],
-        v56 = pBLVRenderParams->uViewportW,
-        thise = stru_50B700._screen_space_y[3] <= (signed int)pBLVRenderParams->uViewportW,
-        v91 = 1,
-        v48 < 1) )
+  }
+//**********************************************************************************************************************************
+//for bottom_clip plane(проверка по нижней плоскости)
+  bottom_num_vertices = 0;
+  PortalFace._screen_space_x[top_num_vertices + 3] = PortalFace._screen_space_x[3];
+  PortalFace._screen_space_y[top_num_vertices + 3] = PortalFace._screen_space_y[3];
+  current_vertices_flag = PortalFace._screen_space_y[3] <= (signed int)pBLVRenderParams->uViewportW;
+  if ( top_num_vertices < 1 )
     return 0;
-  do
-  {
-    v57 = v91;
-    v58 = stru_50B700._screen_space_y[v91 + 3];
-    v85 = v58 <= (signed int)v56;
-    if ( thise ^ v85 )
+  for ( uint i =1; i <= top_num_vertices; ++i )
+  {
+    next_vertices_flag = PortalFace._screen_space_y[i + 3] <= (signed int)pBLVRenderParams->uViewportW;
+    if ( current_vertices_flag ^ next_vertices_flag )
     {
-      if ( v58 <= (signed int)v56 )
+      if ( next_vertices_flag )
       {
-        v70 = (signed int)(v56 - stru_50B700._screen_space_y[v57 + 2])
-            * (signed __int64)(stru_50B700._screen_space_x[v57 + 3] - stru_50B700._screen_space_x[v57 + 2])
-            / (v58 - stru_50B700._screen_space_y[v57 + 2]);
-        v59 = stru_50B700._screen_space_x[v57 + 2];
+        t = (signed __int64)(pBLVRenderParams->uViewportW - PortalFace._screen_space_y[i + 3])
+                          / (PortalFace._screen_space_y[i + 2] - PortalFace._screen_space_y[i + 3]);
+        PortalFace._screen_space_x[bottom_num_vertices] = (signed int)(PortalFace._screen_space_x[i + 2] - PortalFace._screen_space_x[i + 3])
+            * t + PortalFace._screen_space_x[i + 3];
+        PortalFace._screen_space_y[bottom_num_vertices] = pBLVRenderParams->uViewportW;
       }
       else
       {
-        v70 = (signed int)(v56 - v58)
-            * (signed __int64)(stru_50B700._screen_space_x[v57 + 2] - stru_50B700._screen_space_x[v57 + 3])
-            / (stru_50B700._screen_space_y[v57 + 2] - v58);
-        v59 = stru_50B700._screen_space_x[v57 + 3];
+        t = (signed __int64)(pBLVRenderParams->uViewportW - PortalFace._screen_space_y[i + 2])
+                          / (PortalFace._screen_space_y[i + 3] - PortalFace._screen_space_y[i + 2]);
+        PortalFace._screen_space_x[bottom_num_vertices] = (signed int)(PortalFace._screen_space_x[i + 3] - PortalFace._screen_space_x[i + 2])
+            * t + PortalFace._screen_space_x[i + 2];
+        PortalFace._screen_space_y[bottom_num_vertices] = pBLVRenderParams->uViewportW;
       }
-      ++v55;
-      //stru_50B700._screen_space_y[v55 + 59] = v70 + v59;
-	  stru_50B700._screen_space_x[v55 - 1] = v70 + v59;
-      v56 = pBLVRenderParams->uViewportW;
-      //stru_50B700._view_transformed_xs[v55 + 47] = pBLVRenderParams->uViewportW;
-	  stru_50B700._screen_space_y[v55 - 1] = pBLVRenderParams->uViewportW;
+      bottom_num_vertices++;
     }
-    if ( v85 )
+    if ( next_vertices_flag )
     {
-      stru_50B700._screen_space_x[v55] = stru_50B700._screen_space_x[v57 + 3];
-      stru_50B700._screen_space_y[v55++] = stru_50B700._screen_space_y[v57 + 3];
+      pNextVertices = bottom_num_vertices++;
+      PortalFace._screen_space_x[pNextVertices] = PortalFace._screen_space_x[i + 3];
+      PortalFace._screen_space_y[pNextVertices] = PortalFace._screen_space_y[i + 3];
     }
-    ++v91;
-    thise = v85;
-  }
-  while ( v91 <= v48 );
-  if ( !v55 )
+    current_vertices_flag = next_vertices_flag;
+  }
+  //out_x: 83,8,8,61,83
+  //out_y: 273,293,152,172,216
+//***************************************************************************************************************************************
+
+  if ( !bottom_num_vertices )
     return 0;
-  v61 = pRenderer->pRenderD3D == 0;
-  stru_50B700._screen_space_x[v55] = stru_50B700._screen_space_x[0];
-  stru_50B700._screen_space_y[v55] = stru_50B700._screen_space_y[0];
-  if ( v61 && v55 > 3 )
-  {
-    stru_50B700._screen_space_x[v55 + 1] = stru_50B700._screen_space_x[1];
-    stru_50B700._screen_space_y[v55 + 1] = stru_50B700._screen_space_y[1];
-    thisf = 2 * (stru_50B700.field_0 != 0) - 1;
-    if ( v55 > 0 )
+  PortalFace._screen_space_x[bottom_num_vertices] = PortalFace._screen_space_x[0];
+  PortalFace._screen_space_y[bottom_num_vertices] = PortalFace._screen_space_y[0];
+//check for software(проверка для софтвар)
+  if ( !pRenderer->pRenderD3D && bottom_num_vertices > 3 )
+  {
+    PortalFace._screen_space_x[bottom_num_vertices + 1] = PortalFace._screen_space_x[1];
+    PortalFace._screen_space_y[bottom_num_vertices + 1] = PortalFace._screen_space_y[1];
+    thisf = PortalFace.direction == true ? 1 : - 1;
+    if ( bottom_num_vertices > 0 )
     {
       v62 = 1;
       v71 = 1;
@@ -7653,17 +7574,16 @@
         v63 = v62 - 1;
         v64 = v62 + 1;
         v80 = v62 + 1;
-        if ( v62 - 1 >= v55 )
-          v63 -= v55;
-        if ( v62 >= v55 )
-          v62 -= v55;
-        if ( v64 >= v55 )
-          v64 -= v55;
-        if ( thisf
-           * ((stru_50B700._screen_space_y[v64] - stru_50B700._screen_space_y[v63])
-            * (stru_50B700._screen_space_x[v62] - stru_50B700._screen_space_x[v63])
-            - (stru_50B700._screen_space_y[v62] - stru_50B700._screen_space_y[v63])
-            * (stru_50B700._screen_space_x[v64] - stru_50B700._screen_space_x[v63])) < 0 )
+        if ( v62 - 1 >= bottom_num_vertices )
+          v63 -= bottom_num_vertices;
+        if ( v62 >= bottom_num_vertices )
+          v62 -= bottom_num_vertices;
+        if ( v64 >= bottom_num_vertices )
+          v64 -= bottom_num_vertices;
+        if ( thisf * ((PortalFace._screen_space_y[v64] - PortalFace._screen_space_y[v63])
+                   * (PortalFace._screen_space_x[v62] - PortalFace._screen_space_x[v63])
+                   - (PortalFace._screen_space_y[v62] - PortalFace._screen_space_y[v63])
+                   * (PortalFace._screen_space_x[v64] - PortalFace._screen_space_x[v63])) < 0 )
         {
           v62 = v80;
           v71 = v80;
@@ -7672,24 +7592,20 @@
         {
           v62 = v71;
           v65 = v71;
-          if ( v71 < v55 || (v65 = v71 - v55, v71 - v55 < v55) )
+          if ( v71 < bottom_num_vertices || (v65 = v71 - bottom_num_vertices, v71 - bottom_num_vertices < bottom_num_vertices) )
           {
-            memcpy(
-              &stru_50B700._screen_space_y[v65],
-              &stru_50B700._screen_space_y[v65 + 1],
-              4 * ((unsigned int)(4 * (v55 - v65)) >> 2));
-            memcpy(
-              &stru_50B700._screen_space_x[v65],
-              &stru_50B700._screen_space_x[v65 + 1],
-              4 * ((unsigned int)(4 * (v55 - v65)) >> 2));
+            memcpy(&PortalFace._screen_space_y[v65], &PortalFace._screen_space_y[v65 + 1],
+              4 * ((unsigned int)(4 * (bottom_num_vertices - v65)) >> 2));
+            memcpy(&PortalFace._screen_space_x[v65], &PortalFace._screen_space_x[v65 + 1],
+              4 * ((unsigned int)(4 * (bottom_num_vertices - v65)) >> 2));
           }
-          --v55;
+          --bottom_num_vertices;
         }
       }
-      while ( v62 - 1 < v55 );
+      while ( v62 - 1 < bottom_num_vertices );
     }
-    stru_50B700._screen_space_x[v55] = stru_50B700._screen_space_x[0];
-    stru_50B700._screen_space_y[v55] = stru_50B700._screen_space_y[0];
-  }
-  return v55;
+    PortalFace._screen_space_x[bottom_num_vertices] = PortalFace._screen_space_x[0];
+    PortalFace._screen_space_y[bottom_num_vertices] = PortalFace._screen_space_y[0];
+  }
+  return bottom_num_vertices;
 }
\ No newline at end of file
--- a/Monsters.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/Monsters.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -802,11 +802,11 @@
                         {
                     case 's': pInfos[curr_rec_num].uMovementType=0;// short
                         if (tolower(test_string[1])!='h')
-                            pInfos[curr_rec_num].uMovementType=5; //??
+                            pInfos[curr_rec_num].uMovementType=5; //stationary?
                         break;  //short
                     case 'l': pInfos[curr_rec_num].uMovementType=2;  break; //long
                     case 'm': pInfos[curr_rec_num].uMovementType=1; break; //med
-                    case 'g': pInfos[curr_rec_num].uMovementType=3; break; //???     
+                    case 'g': pInfos[curr_rec_num].uMovementType=3; break; //global?     
                     default:
                         pInfos[curr_rec_num].uMovementType=4; //free
                         }       
--- a/Mouse.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/Mouse.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -630,8 +630,8 @@
 
     if ( pParty->bTurnBasedModeOn == 1 )
     {
-      if ( pTurnEngine->field_4 == 3 )
-        pTurnEngine->field_18 |= 8u;
+      if ( pTurnEngine->turn_stage == 3 )
+        pTurnEngine->field_18 |= TE_FLAG_8;
     }
   }
 }
--- a/Outdoor.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/Outdoor.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -1147,7 +1147,7 @@
   pTilemap = (unsigned __int8 *)pAllocator->AllocNamedChunk(pTilemap, 0x4000u, "TMAP");//tile map
   pAttributemap = (unsigned __int8 *)pAllocator->AllocNamedChunk(pAttributemap, 0x4000u, "AMAP");//карта атрибутов
   pDmap=(struct DMap *)pAllocator->AllocNamedChunk(pDmap, 0x8000u, "DMAP");
-  if (pHeightmap&&pTilemap&&pAttributemap&&pDmap)
+  if ( pHeightmap && pTilemap && pAttributemap && pDmap )
     return true;
   else
     return false;
--- a/Outdoor_stuff.h	Fri Jul 12 23:07:14 2013 +0200
+++ b/Outdoor_stuff.h	Fri Jul 12 23:08:35 2013 +0200
@@ -15,7 +15,7 @@
   }
 
   int _479295();
-  void _4811A3();
+  void DrawBorderTiles();
   void _48276F_sr();
   void _486089_normalize_v_18();
   void _48607B(struct stru149 *a2);
@@ -68,7 +68,7 @@
 {
   int _48616B(int a2, int a3, int a4, int a5, int a6, int a7);
   int _48653D(int a2, int a3, int a4, int a5, int a6, int a7);
-  int _48694B();
+  int sky_48694B();
 
   int field_0_party_dir_x;
   int field_4_party_dir_y;
--- a/Overlays.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/Overlays.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -110,14 +110,14 @@
   if ( pCurrentScreen != SCREEN_GAME || !pParty->bTurnBasedModeOn)
     return;
 
-  if ( pTurnEngine->field_4 == 3 )
+  if ( pTurnEngine->turn_stage == 3 )
   {
     v6 = pEventTimer->uStartTime;
     v5 = pIconIDs_Turn[5 - pTurnEngine->uActionPointsLeft / 26];
   }
   else
   {
-    if ( pTurnEngine->field_4 == 1 )
+    if ( pTurnEngine->turn_stage == 1 )
     {
       v6 = dword_50C994;
       v3 = pIconsFrameTable;
@@ -127,7 +127,7 @@
         v5 = uIconID_TurnHour;
       goto LABEL_12;
     }
-    if ( pTurnEngine->field_4 != 2 )
+    if ( pTurnEngine->turn_stage != 2 )
     {
       v4 = this;
       goto LABEL_14;
--- a/Party.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/Party.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -164,7 +164,7 @@
   v12 = 0;
   if ( pParty->bTurnBasedModeOn == 1 )
   {
-    if ( pTurnEngine->field_4 != 2 || PID_TYPE(pTurnEngine->pQueue[0].uPackedID) != OBJECT_Player)
+    if ( pTurnEngine->turn_stage != 2 || PID_TYPE(pTurnEngine->pQueue[0].uPackedID) != OBJECT_Player)
       return 0;
     v2 = PID_ID(pTurnEngine->pQueue[0].uPackedID);
     return v2 + 1;
--- a/Player.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/Player.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -906,10 +906,12 @@
 {
   bool result; // eax@2
 
-  if ( this->pConditions[2] | this->pConditions[12] | this->pConditions[13] | this->pConditions[14] | this->pConditions[15] | this->pConditions[16] )
-    result = 0;
+  if ( this->pConditions[Condition_Sleep] || this->pConditions[Condition_Paralyzed] || 
+       this->pConditions[Condition_Unconcious] || this->pConditions[Condition_Dead] || 
+       this->pConditions[Condition_Pertified] || this->pConditions[Condition_Eradicated] )
+    result = false;
   else
-    result = 1;
+    result = true;
   return result;
 }
 
@@ -6523,7 +6525,7 @@
     thisb = this;
     v3 = &pParty->pPlayers[player_num-1];
     v73 = 1;
-    if ( pParty->bTurnBasedModeOn == 1 && (pTurnEngine->field_4 == 1 || pTurnEngine->field_4 == 3) )
+    if ( pParty->bTurnBasedModeOn == 1 && (pTurnEngine->turn_stage == 1 || pTurnEngine->turn_stage == 3) )
         return;
     if ( pItemsTable->pItems[pParty->pPickedItem.uItemID].uEquipType == EQUIP_REAGENT )
         {
--- a/Render.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/Render.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -566,7 +566,7 @@
     return;
 
   array_77EC08[1999]._48607B(&stru_8019C8);
-  array_77EC08[1999].ptr_38->_48694B();
+  array_77EC08[1999].ptr_38->sky_48694B();
 
   if (pOutdoor->uMainTile_BitmapID == -1)
   {
@@ -589,7 +589,7 @@
   cos((double)pIndoorCamera->sRotationX * 0.0030664064);
   sin((double)pIndoorCamera->sRotationX * 0.0030664064);
   array_77EC08[1999]._48607B(&stru_8019C8);
-  array_77EC08[1999].ptr_38->_48694B();
+  array_77EC08[1999].ptr_38->sky_48694B();
 
   if (pOutdoor->uSky_TextureID == -1)
   {
@@ -1670,24 +1670,13 @@
 // 4D864C: using guessed type char byte_4D864C;
 
 
-void Render::RenderTerrainD3D() // New function. It's temporary
+void Render::RenderTerrainD3D() // New function
 {
   char result; // al@3
-  //int v1; // eax@3
-  //int v2; // ebx@4
-  //struct ODMFace *v4; // esi@6
   int v6; // ecx@8
-  //int v7; // ecx@8
- struct stru148 *v8; // ebx@8
-// RenderVertexSoft *v8a; // edi@3
-  //char v11; // zf@8
+ struct stru148 *pTile; // ebx@8
  struct stru148 *v16;
   unsigned int v18; // edi@22
-  //int v20; // edi@34
-  //int v28; // eax@50
-  //int v29; // ecx@55
-  //int v30; // eax@57
-  int v31; // eax@57
   int v35; // edi@63
   int v37; // eax@73
   int v39; // eax@80
@@ -1697,156 +1686,145 @@
   int v43; // [sp-Ch] [bp-64h]@2
   const char *v44; // [sp-8h] [bp-60h]@2
   int v45; // [sp-4h] [bp-5Ch]@2
-  //float v48; // [sp+14h] [bp-44h]@8
-  //void *v52; // [sp+24h] [bp-34h]@3
-  bool v54; // [sp+2Ch] [bp-2Ch]@10
-  int v55; // [sp+30h] [bp-28h]@34
   int v56;
   int v57; // [sp+38h] [bp-20h]@36
-  int v58; // [sp+3Ch] [bp-1Ch]@8
-  int v63; // [sp+50h] [bp-8h]@3
   int v64; // [sp+57h] [bp-1h]@2
   int v62;
-  //struct IndoorCameraD3D *pIndoorCameraD3D;
-  //RenderVertexSoft *pVertices; // [sp+C4h] [bp-Ch]@6
-
 
   //warning: the game uses CW culling by default, ccw is incosistent
   pRenderer->pRenderD3D->pDevice->SetRenderState(D3DRENDERSTATE_CULLMODE, D3DCULL_CCW);
 
-
-  v63 = 0;
-  // basic optimizations
-  unsigned int uStartX, uEndX, uStartZ, uEndZ;
-  if (pIndoorCamera->sRotationY >= 0 && pIndoorCamera->sRotationY <= 1024)
-  {
-    uStartX = 0, uEndX = 128;
-    uStartZ = 0, uEndZ = 64 + 16;
-  }
-  else if (pIndoorCamera->sRotationY >= 512 && pIndoorCamera->sRotationY <= 1536)
-  {
-    uStartX = 0, uEndX = 64 + 16;
-    uStartZ = 0, uEndZ = 128;
-  }
-  else if (pIndoorCamera->sRotationY >= 1536 || pIndoorCamera->sRotationY <= 512)
-  {
-    uStartX = 64 - 16, uEndX = 128;
-    uStartZ = 0, uEndZ = 128;
-  }
-  else
-  {
-    uStartX = 0, uEndX = 128;
-    uStartZ = 64 - 16, uEndZ = 128;
-  }
-  uStartX = 0, uEndX = 128;
-  uStartZ = 0, uEndZ = 128;
-  static RenderVertexSoft pTerrainVertices[128 * 128];
-  for (unsigned int z = uStartZ; z < uEndZ; ++z)
-  {
-    for (unsigned int x = uStartX; x < uEndX; ++x)
-    {
-      pTerrainVertices[z * 128 + x].vWorldPosition.x = (-64 + (signed)x) * 512;
-      pTerrainVertices[z * 128 + x].vWorldPosition.y = (64 - (signed)z) * 512;
-      pTerrainVertices[z * 128 + x].vWorldPosition.z = 32 * pOutdoor->pTerrain.pHeightmap[z * 128 + x];
-      //pIndoorCameraD3D = pGame->pIndoorCameraD3D;
+  static RenderVertexSoft pTerrainVertices[128 * 128];//vertexCountX and vertexCountZ
+
+  //Генерация местоположения вершин-------------------------------------------------------------------------
+  //решётка вершин делится на две части от -64 до 0 и от 0 до 64
+  //
+  // -64  X  0     64
+  //  --------------- 64
+  //  |      |      |
+  //  |      |      |
+  //  |      |      |
+  // 0|------+------| Z
+  //  |      |      |
+  //  |      |      |
+  //  |      |      |
+  //  ---------------
+  //                -64
+
+  int blockScale = 512;
+  int heightScale = 32;
+  for (unsigned int z = 0; z < 128; ++z)
+  {
+    for (unsigned int x = 0; x < 128; ++x)
+    {
+      pTerrainVertices[z * 128 + x].vWorldPosition.x = (-64 + (signed)x) * blockScale;
+      pTerrainVertices[z * 128 + x].vWorldPosition.y = (64 - (signed)z) * blockScale;
+      pTerrainVertices[z * 128 + x].vWorldPosition.z = heightScale * pOutdoor->pTerrain.pHeightmap[z * 128 + x];
       pGame->pIndoorCameraD3D->ViewTransform(&pTerrainVertices[z * 128 + x], 1);
       pGame->pIndoorCameraD3D->Project(&pTerrainVertices[z * 128 + x], 1, 0);
     }
   }
-
-  for (unsigned int z = uStartZ; z < uEndZ - 1; ++z)
-  {
-    for (unsigned int x = uStartX; x < uEndX - 1; ++x)
-    {
-      v8 = &array_77EC08[pOutdoorCamera->numStru148s];
-      v8->flags = 0;
-      v8->field_32 = 0;
-      //unsigned int uTileID = pOutdoor->pTerrain.pTilemap[z * 128 + x];
-      //struct TileDesc *pTile = pTileTable->GetTileById(uTileID);
-      v8->uTileBitmapID = pOutdoor->DoGetTileTexture(x, z);
-      //v6 = v8->uTileBitmapID;
-      v8->pTexture = (Texture *)&pBitmaps_LOD->pHardwareTextures[v8->uTileBitmapID];
-      if (v8->uTileBitmapID == 0xFFFF)
+//--------------------------------------------------------------------------------------------------------------------
+
+  //
+  for (unsigned int z = 0; z < 127; ++z)
+  {
+    for (unsigned int x = 0; x < 127; ++x)
+    {
+      pTile = &array_77EC08[pOutdoorCamera->numStru148s];
+      pTile->flags = 0;
+      pTile->field_32 = 0;
+      pTile->uTileBitmapID = pOutdoor->DoGetTileTexture(x, z);
+      pTile->pTexture = (Texture *)&pBitmaps_LOD->pHardwareTextures[pTile->uTileBitmapID];
+      if (pTile->uTileBitmapID == 0xFFFF)
         continue;
 
-      v8->flags = 0x8010 |pOutdoor->GetSomeOtherTileInfo(x, z);
-      v8->field_32 = 0;
-      v8->field_59 = 1;
-      //v8->field_5D = (char)WorldPosToGridCellZ(floorf((pVertices->vWorldPosition.z + v8a->vWorldPosition.z) / 2 + 0.5f));
-      //v8->field_5C = WorldPosToGridCellX(floorf((v101->vWorldPosition.x + v8->vWorldPosition.x) / 2 + 0.5f));;
-      v8->sTextureDeltaU = 0;
-      v8->sTextureDeltaV = 0;
-      memcpy(array_73D150 + 0, &pTerrainVertices[z * 128 + x], sizeof(RenderVertexSoft));
+      //pTile->flags = 0x8010 |pOutdoor->GetSomeOtherTileInfo(x, z);
+      pTile->flags = pOutdoor->GetSomeOtherTileInfo(x, z);
+      pTile->field_32 = 0;
+      pTile->field_59 = 1;
+      pTile->sTextureDeltaU = 0;
+      pTile->sTextureDeltaV = 0;
+//  x,z         x+1,z
+//  .____________.
+//  |            |
+//  |            |
+//  |            |
+//  |            |
+//  |            |
+//  .____________.
+//  x,z+1       x+1,z+1
+      memcpy(&array_73D150[0], &pTerrainVertices[z * 128 + x], sizeof(RenderVertexSoft));//x, z
       array_73D150[0].u = 0;
       array_73D150[0].v = 0;
-      memcpy(array_73D150 + 1, &pTerrainVertices[z * 128 + x + 1], sizeof(RenderVertexSoft));
+      memcpy(&array_73D150[1], &pTerrainVertices[z * 128 + x + 1], sizeof(RenderVertexSoft));//x + 1, z
       array_73D150[1].u = 1;
       array_73D150[1].v = 0;
-      memcpy(array_73D150 + 2, &pTerrainVertices[(z + 1) * 128 + x + 1], sizeof(RenderVertexSoft));
+      memcpy(&array_73D150[2], &pTerrainVertices[(z + 1) * 128 + x + 1], sizeof(RenderVertexSoft));//x + 1, z + 1
       array_73D150[2].u = 1;
       array_73D150[2].v = 1;
-      memcpy(array_73D150 + 3, &pTerrainVertices[(z + 1) * 128 + x], sizeof(RenderVertexSoft));
+      memcpy(&array_73D150[3], &pTerrainVertices[(z + 1) * 128 + x], sizeof(RenderVertexSoft));//x, z + 1
       array_73D150[3].u = 0;
       array_73D150[3].v = 1;
-      v55 = 0;
-      v54 = false;
-      v58 = 0;
-      if (v58 == 4) // if all y == first y;  primitive in xz plane 
-        v8->field_32 |= 0x0001;
-      v8->pODMFace = nullptr;
-      v8->uNumVertices = 4;
-      v8->field_59 = 5;
-
-
-      uint norm_idx = pTerrainNormalIndices[2 * (x + 128 * z) + 1];
+      //v58 = 0;
+      //if (v58 == 4) // if all y == first y;  primitive in xz plane 
+        //pTile->field_32 |= 0x0001;
+      pTile->pODMFace = nullptr;
+      pTile->uNumVertices = 4;
+      pTile->field_59 = 5;
+
+//shading (затенение)----------------------------------------------------------------------------
+      //uint norm_idx = pTerrainNormalIndices[2 * (z * 128 + x) + 1];
+      uint norm_idx = pTerrainNormalIndices[2 * (x * 128 + z) + 1];//есть несовпадение в 1 кубик, тень надо поднять на 1
       assert(norm_idx < uNumTerrainNormals);
 
       auto norm = pTerrainNormals + norm_idx;
-      float _f = (norm->x * (float)pOutdoor->vSunlight.x / 65536.0 -
-                  norm->y * (float)pOutdoor->vSunlight.y / 65536.0 -
-                  norm->z * (float)pOutdoor->vSunlight.z / 65536.0);
-      v8->dimming_level = 20.0 - floorf(20.0 * _f + 0.5f);
+      float _f = ((norm->x * (float)pOutdoor->vSunlight.x / 65536.0) -
+                  (norm->y * (float)pOutdoor->vSunlight.y / 65536.0) -
+                  (norm->z * (float)pOutdoor->vSunlight.z / 65536.0));
+      pTile->dimming_level = 20.0 - floorf(20.0 * _f + 0.5f);
+//-----------------------------------------------------------------------------------------------
 
       ++pOutdoorCamera->numStru148s;
       ++pOutdoorCamera->field_44;
       assert(pOutdoorCamera->numStru148s < 20000);
 
-      v8->uBModelID = 0;
-      v8->uBModelFaceID = 0;
-      v31 = (8 * (0 | (0 << 6)));
-      v8->field_50 = v31 | 6;
-      for (unsigned int k = 0; k < v8->uNumVertices; ++k)
+      pTile->uBModelID = 0;
+      pTile->uBModelFaceID = 0;
+      pTile->field_50 = (8 * (0 | (0 << 6))) | 6;
+      for (unsigned int k = 0; k < pTile->uNumVertices; ++k)
       {
         memcpy(&array_50AC10[k], &array_73D150[k], sizeof(struct RenderVertexSoft));
         array_50AC10[k]._rhw = 1.0 / (array_73D150[k].vWorldViewPosition.x + 0.0000001000000011686097);
       }
 
+// check the transparency and texture (tiles) mapping (проверка прозрачности и наложение текстур (тайлов))----------------------
       bool transparent = false;
-            if ( v8->flags & 2 && v8->uTileBitmapID == pRenderer->hd_water_tile_id)
-            {
-              //v80 = false;
-              v6 = pRenderer->pHDWaterBitmapIDs[pRenderer->hd_water_current_frame];
-            }
-            else
-            {
-              v6 = v8->uTileBitmapID;
-              if ( !_strnicmp(pBitmaps_LOD->pTextures[v8->uTileBitmapID].pName, "wtrdr", 5) )
-                transparent = true;
-            }
+      if ( !( pTile->flags & 1 ) ) // не поддерживается TextureFrameTable
+      {
+        if ( pTile->flags & 2 && pTile->uTileBitmapID == pRenderer->hd_water_tile_id)
+        {
+          //transparent = false;
+          v6 = pRenderer->pHDWaterBitmapIDs[pRenderer->hd_water_current_frame];
+        }
+        else
+        {
+          v6 = pTile->uTileBitmapID;
+          if ( !_strnicmp(pBitmaps_LOD->pTextures[pTile->uTileBitmapID].pName, "wtrdr", 5) )
+            transparent = true;
+        }
 
       assert(v6 < 1000); // many random crashes here
 
       // for all shore tiles - draw a tile water under them since they're half-empty
-      if (!_strnicmp(pBitmaps_LOD->pTextures[v8->uTileBitmapID].pName, "wtrdr", 5))  // all shore tile filenames are wtrdrXXX
-      {
-        pRenderD3D->pDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, FALSE);  // no Z writing: the shore tile will be draw in the same place, so taking care about z-fighting
-        pRenderer->DrawTerrainPolygon(v8->uNumVertices, v8, pBitmaps_LOD->pHardwareTextures[pRenderer->pHDWaterBitmapIDs[pRenderer->hd_water_current_frame]], false, true);
-        pRenderD3D->pDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, TRUE);
-        
-      }
-
-      pRenderer->DrawTerrainPolygon(v8->uNumVertices, v8, pBitmaps_LOD->pHardwareTextures[v6], transparent, true);
-
+      if (!_strnicmp(pBitmaps_LOD->pTextures[pTile->uTileBitmapID].pName, "wtrdr", 5))  // all shore tile filenames are wtrdrXXX
+        pTile->DrawBorderTiles();
+
+      pRenderer->DrawTerrainPolygon(pTile->uNumVertices, pTile, pBitmaps_LOD->pHardwareTextures[v6], transparent, true);
+      }
+      //else //здесь уже пограничные тайлы воды
+        //pTile->DrawBorderTiles();
+//--------------------------------------------------------------------------------------------------------------------------------
 
       --pOutdoorCamera->numStru148s;
       --pOutdoorCamera->field_44;
@@ -1870,15 +1848,12 @@
   double v13; // ST2C_8@6
   int v14; // eax@6
   double v15; // st7@6
-  stru148 *v16; // ebx@12
+  stru148 *pTile; // ebx@12
   unsigned __int16 v17; // ax@12
   int v18; // eax@13
-  int v19; // ecx@13
-  int v20; // eax@13
-  int v21; // eax@13
   signed int v22; // eax@13
-  Vec3_float_ *v23; // eax@15
-  double v24; // st6@17
+  Vec3_float_ *norm; // eax@15
+  //double v24; // st6@17
   double v25; // ST54_8@17
   unsigned __int8 v26; // sf@17
   unsigned __int8 v27; // of@17
@@ -1893,7 +1868,7 @@
   char v35; // zf@47
   unsigned int v36; // eax@50
   int v37; // eax@54
-  stru148 *v38; // ecx@55
+  //stru148 *v38; // ecx@55
   unsigned int v39; // eax@59
   stru148 *v40; // ebx@62
   unsigned __int16 pTileBitmapsID; // ax@62
@@ -1945,9 +1920,6 @@
   float v87; // [sp+78h] [bp-58h]@122
   int v88; // [sp+7Ch] [bp-54h]@1
   int v89; // [sp+80h] [bp-50h]@6
-  float v90; // [sp+84h] [bp-4Ch]@1
-  float v91; // [sp+88h] [bp-48h]@1
-  float v92; // [sp+8Ch] [bp-44h]@1
   int v93; // [sp+90h] [bp-40h]@2
   int X; // [sp+94h] [bp-3Ch]@1
   float v95; // [sp+98h] [bp-38h]@21
@@ -1985,9 +1957,6 @@
   v4 = 0;
   v88 = 0;
   v84 = a1 - 1;
-  v90 = (float)pOutdoor->vSunlight.x / 65536.0;
-  v91 = (float)pOutdoor->vSunlight.y / 65536.0;
-  v92 = (float)pOutdoor->vSunlight.z / 65536.0;
 /*  if ( a1 - 1 > 0 )
   {
     while ( 1 )
@@ -2043,31 +2012,28 @@
         if ( v8->vWorldPosition.y != pVertices->vWorldPosition.y || pVertices->vWorldPosition.y != pVertices2->vWorldPosition.y 
              || pVertices2->vWorldPosition.y != v101->vWorldPosition.y )
           break;*/
-        v16 = &array_77EC08[pOutdoorCamera->numStru148s];
-        v16->uTileBitmapID = pOutdoor->GetTileTexture(sX, sY);
-        if ( v16->uTileBitmapID != -1 )
-        {
-          v19 = v97;
-          v16->flags = 0x8010 |pOutdoor->GetSomeOtherTileInfo(sX, sY);
-          v16->field_32 = 0;
-          v20 = v93;
-          v16->field_59 = 1;
-          v16->terrain_grid_x = (char)v19;
-          v16->field_34 = *(_WORD *)(v20 + 2);
-          v21 = v89;
-          v16->terrain_grid_z = v89;
-          v22 = pTerrainNormalIndices[2 * (v19 + 128 * v21) + 1];
+        pTile = &array_77EC08[pOutdoorCamera->numStru148s];
+        pTile->uTileBitmapID = pOutdoor->GetTileTexture(sX, sY);
+        if ( pTile->uTileBitmapID != -1 )
+        {
+          pTile->flags = 0x8010 |pOutdoor->GetSomeOtherTileInfo(sX, sY);
+          pTile->field_32 = 0;
+          pTile->field_59 = 1;
+          pTile->terrain_grid_x = (char)v97;
+          pTile->field_34 = *(_WORD *)(v93 + 2);
+          pTile->terrain_grid_z = v89;
+          v22 = pTerrainNormalIndices[2 * (v97 + 128 * v89) + 1];
           if ( v22 < 0 || v22 > uNumTerrainNormals - 1 )
-            v23 = 0;
+            norm = 0;
           else
-            v23 = &pTerrainNormals[v22];
-          v24 = v92 * v23->z;
-          //v99 = v23;
-          thisf = 20.0 - (-v24 - v91 * v23->y - v90 * v23->x) * 20.0;
+            norm = &pTerrainNormals[v22];
+          thisf = 20.0 - ( -(((float)pOutdoor->vSunlight.x / 65536.0) * norm->x) -
+                            (((float)pOutdoor->vSunlight.y / 65536.0) * norm->y) -
+                            (((float)pOutdoor->vSunlight.z / 65536.0) * norm->z)) * 20.0;
           //v25 = thisf + 6.7553994e15;
           //v27 = pOutdoorCamera->numStru148s > 1999;
           //v26 = pOutdoorCamera->numStru148s - 1999 < 0;
-          v16->dimming_level = floorf(thisf + 0.5f);
+          pTile->dimming_level = floorf(thisf + 0.5f);
           if ( pOutdoorCamera->numStru148s >= 1999 )
             return;
           ++pOutdoorCamera->numStru148s;
@@ -2101,8 +2067,8 @@
           }*/
           v32 = (struct8 *)array_50AC10;
           v97 = (int)pGame->pLightmapBuilder;
-          pGame->pLightmapBuilder->StackLights_TerrainFace(v23, &v95, array_50AC10, 4, 1);
-          pDecalBuilder->_49BE8A(v16, v23, &v95, array_50AC10, 4, 1);
+          pGame->pLightmapBuilder->StackLights_TerrainFace(norm, &v95, array_50AC10, 4, 1);
+          pDecalBuilder->_49BE8A(pTile, norm, &v95, array_50AC10, 4, 1);
           a5 = 4;
           if ( byte_4D864C && pGame->uFlags & 0x80 )
           {
@@ -2125,55 +2091,54 @@
               v33 = 3;
             else
               v33 = v108 != 0 ? 5 : 0;
-            static_sub_0048034E_stru_154.ClassifyPolygon(v23, v95);
+            static_sub_0048034E_stru_154.ClassifyPolygon(norm, v95);
             if ( pDecalBuilder->uNumDecals > 0 )
-              pDecalBuilder->ApplyDecals(31 - v16->dimming_level, 4, &static_sub_0048034E_stru_154, a5, array_50AC10, 0, *(float *)&v33, -1);
+              pDecalBuilder->ApplyDecals(31 - pTile->dimming_level, 4, &static_sub_0048034E_stru_154, a5, array_50AC10, 0, *(float *)&v33, -1);
           }
           if ( stru_F8AD28.uNumLightsApplied > 0 )
             pGame->pLightmapBuilder->ApplyLights(&stru_F8AD28, &static_sub_0048034E_stru_154, a5, array_50AC10, 0, v33);
           v34 = a5;
           //v35 = byte_4D864C == 0;
-          v16->uNumVertices = a5;
+          pTile->uNumVertices = a5;
           if ( !byte_4D864C || ~pGame->uFlags & 0x80 )
           {
             if ( this_3 )
             {
               v36 = sr_424CD7(v34);
-              v16->uNumVertices = v36;
+              pTile->uNumVertices = v36;
               OutdoorCamera::Project(v36);
             }
             if ( v108 )
             {
               v36 = sr_424EE0_MakeFanFromTriangle(v34);
-              v16->uNumVertices = v36;
+              pTile->uNumVertices = v36;
               OutdoorCamera::Project(v36);
             }
           }
           //v37 = *(int *)&v16->flags;
-          if ( ~v16->flags & 1 )
+          if ( ~pTile->flags & 1 )
           {
-            if ( v16->flags & 2 && v16->uTileBitmapID == pRenderer->hd_water_tile_id )
+            if ( pTile->flags & 2 && pTile->uTileBitmapID == pRenderer->hd_water_tile_id )
             {
               v80 = false;
               v39 = pRenderer->pHDWaterBitmapIDs[pRenderer->hd_water_current_frame];
             }
             else
             {
-              v39 = v16->uTileBitmapID;
+              v39 = pTile->uTileBitmapID;
               v80 = true;
             }
             //v79 = 0;
             //v78 = pBitmaps_LOD->pHardwareTextures[v39];
-            v16->pTexture = (Texture *)&pBitmaps_LOD->pHardwareTextures[v39];// Ritor1: It's temporary
-            v77 = (int)v16;
+            pTile->pTexture = (Texture *)&pBitmaps_LOD->pHardwareTextures[v39];// Ritor1: It's temporary
+            v77 = (int)pTile;
             //v76 = v16->uNumVertices;
 //LABEL_161:
-            pRenderer->DrawTerrainPolygon(v16->uNumVertices, v16, pBitmaps_LOD->pHardwareTextures[v39], false, v80);
+            pRenderer->DrawTerrainPolygon(pTile->uNumVertices, pTile, pBitmaps_LOD->pHardwareTextures[v39], false, v80);
             goto LABEL_162;
           }
-          v38 = v16;
 LABEL_56:
-          v38->_4811A3();
+          pTile->DrawBorderTiles();
         }
 LABEL_162:
         v4 = v88 + 1;
@@ -2298,7 +2263,7 @@
       v57 = *(int *)&v40->flags;
       if ( BYTE1(v57) & 1 )
       {
-        v40->_4811A3();
+        v40->DrawBorderTiles();
       }
       else
       {
--- a/Spells.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/Spells.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -256,9 +256,9 @@
 }
 
 //----- (004585CA) --------------------------------------------------------
-signed int SpellBuff::_4585CA(__int64 a2)
-{
-  if (uExpireTime && uExpireTime < a2)
+bool SpellBuff::IsBuffExpiredToTime( __int64 time_end )
+    {
+  if (uExpireTime && (uExpireTime < time_end))
   {
     uExpireTime = 0;
     uPower = 0;
@@ -270,9 +270,9 @@
 }
 
 //----- (004584E0) --------------------------------------------------------
-bool SpellBuff::Apply(unsigned __int64 uExpireTime, unsigned __int16 uSkillLevel, unsigned __int16 uPower, int uOverlayID, unsigned __int8 caster)
-{
-  if (this->uExpireTime && uExpireTime < this->uExpireTime)
+bool SpellBuff::Apply( signed __int64 uExpireTime, unsigned __int16 uSkillLevel, unsigned __int16 uPower, int uOverlayID, unsigned __int8 caster )
+    {
+  if (this->uExpireTime && (uExpireTime < this->uExpireTime))
     return false;
 
   this->uSkill = uSkillLevel;
--- a/Spells.h	Fri Jul 12 23:07:14 2013 +0200
+++ b/Spells.h	Fri Jul 12 23:08:35 2013 +0200
@@ -155,12 +155,12 @@
     uFlags = 0;
   }
 
-  bool Apply(unsigned __int64 uExpireTime, unsigned __int16 uSkillLevel, unsigned __int16 uPower, int uOverlayID, unsigned __int8 caster);
+  bool Apply(signed __int64 uExpireTime, unsigned __int16 uSkillLevel, unsigned __int16 uPower, int uOverlayID, unsigned __int8 caster);
   void Reset();
-  signed int _4585CA(__int64 a2);
+  bool IsBuffExpiredToTime(__int64 time_end);
   bool NotExpired();
 
-  unsigned __int64 uExpireTime;
+  signed __int64 uExpireTime;
   unsigned __int16 uPower;
   unsigned __int16 uSkill;
   unsigned __int16 uOverlayID;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TurnEngine.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -0,0 +1,1351 @@
+
+#include "MM7.h"
+#include "mm7_data.h"
+#include "Actor.h"
+#include "Player.h"
+#include "Party.h"
+#include "AudioPlayer.h"
+#include "SpriteObject.h"
+#include "Time.h"
+#include "stru298.h"
+#include "IconFrameTable.h"
+#include "Viewport.h"
+#include "FactionTable.h"
+
+#include "TurnEngine.h"
+
+
+#include "TurnEngine.h"
+struct stru262_TurnBased *pTurnEngine = new stru262_TurnBased;
+
+
+//----- (00404544) --------------------------------------------------------
+void stru262_TurnBased::SortTurnQueue()
+    {
+
+    int active_actors;
+    TurnBased_QueueElem *v7; // eax@16
+    TurnBased_QueueElem *v8; // ecx@18
+    TurnBased_QueueElem temp_elem;
+    int i,j;
+    unsigned int p_type;
+    unsigned int p_id;
+
+    active_actors = this->uActorQueueSize;
+
+    for( i=0; i<uActorQueueSize; ++i) 
+        {
+        p_type = PID_TYPE(pQueue[i].uPackedID);
+        p_id   = PID_ID(pQueue[i].uPackedID);
+
+        if ( p_type == OBJECT_Actor )
+            {
+            pActors[p_id].uAttributes |= 0x80u;
+            if ( !pActors[p_id].CanAct() )
+                {
+                --active_actors;
+                pQueue[i].field_4 = 1001;
+                pActors[p_id].uAttributes &= ~0x80;
+                }
+            }
+        else if ( p_type == OBJECT_Player)
+            {
+            if ( !pParty->pPlayers[p_id].CanAct() )
+                {
+                --active_actors;
+                pQueue[i].field_4 = 1001;
+                }
+            }
+        }
+     //sort
+if (uActorQueueSize>0)
+    {
+    for( i=0; i<uActorQueueSize-1; ++i) 
+        {
+       v7=&pQueue[i];
+        for(j=i+1; j<uActorQueueSize;++j )
+            {  
+            v8=&pQueue[j];           
+            if ( v8->field_4 < v7->field_4  || 
+                   ((v8->field_4 == v7->field_4) && 
+                       (
+                          ((PID_TYPE(v8->uPackedID) == OBJECT_Player) && (PID_TYPE(v7->uPackedID) == OBJECT_Actor)) || 
+                          ((PID_TYPE(v8->uPackedID) == PID_TYPE(v7->uPackedID)) && (PID_ID(v8->uPackedID) < PID_ID(v7->uPackedID)))      
+                       ) 
+                   )
+                )
+                {
+                memcpy(&temp_elem,v7,sizeof(TurnBased_QueueElem));
+                memcpy(v7,v8, sizeof(TurnBased_QueueElem));
+                memcpy(v8, &temp_elem, sizeof(TurnBased_QueueElem));
+                }
+            }
+        }
+    }
+    uActorQueueSize = active_actors;
+    if ( PID_TYPE(pQueue[0].uPackedID) == OBJECT_Player)
+        {
+        uActiveCharacter = PID_ID(pQueue[0].uPackedID)+1;
+        field_18 |= TE_PLAYER_TURN;
+        }
+    else
+        {
+        uActiveCharacter = 0;
+        field_18 &= ~TE_PLAYER_TURN;
+        }
+    for(i=0; i<uActorQueueSize; ++i)
+        {
+        if ( PID_TYPE(pQueue[i].uPackedID) == OBJECT_Player)
+            pParty->pPlayers[PID_ID(pQueue[i].uPackedID)].uTimeToRecovery = (signed __int64)((double)pQueue[i].field_4 * 0.46875);
+        }
+
+
+    }
+
+//----- (0040471C) --------------------------------------------------------
+void stru262_TurnBased::_40471C()
+    {
+    if ( pParty->bTurnBasedModeOn == 1 )
+        {
+        if ( pTurnEngine->turn_stage == 2 )
+            _406457(0);
+        }
+    }
+
+//----- (004059DB) --------------------------------------------------------
+void stru262_TurnBased::Start()
+    {
+
+    int v3; // esi@1
+    unsigned int actor_id; // esi@7
+    Actor *pActor; // edi@7
+    unsigned int v8; // edx@10
+    signed int v16; // qax@19
+    int v17; // edx@22
+    AIDirection v30; // [sp+Ch] [bp-68h]@10
+    AIDirection v31; // [sp+28h] [bp-4Ch]@10
+    AIDirection a3; // [sp+44h] [bp-30h]@10
+    Player *pPlayer; // [sp+6Ch] [bp-8h]@1
+    int v40b;
+    int activ_players[4];
+    int players_recovery_time[4];
+    int a_players_count;
+    int i,j;
+    int temp;
+
+    pTurnEngine->field_18 &= ~TE_FLAG_2;
+    pEventTimer->TrackGameTime();
+    pAudioPlayer->StopChannels(-1, -1);
+    pAudioPlayer->PlaySound(SOUND_207, 0, 0, -1, 0, 0, 0, 0);
+    pPlayer = pParty->pPlayers;
+    dword_50C998_turnbased_icon_1A = 8 * pIconsFrameTable->pIcons[uIconID_TurnStart].uAnimLength;
+    dword_50C994 = 0;
+
+    this->field_10 = 100;
+    this->field_0 = 0;
+    this->field_8 = 64;
+    this->turn_stage = 1;
+    this->uActorQueueSize = 0;
+
+    for ( v3 = 0; v3 < 4 ; ++v3 )
+        {
+        if ( pParty->pPlayers[v3].CanAct() )
+            {
+            this->pQueue[this->uActorQueueSize].uPackedID = PID(OBJECT_Player,v3);
+            this->pQueue[this->uActorQueueSize].field_C = 2;
+            this->pQueue[this->uActorQueueSize].uActionLength = 0;
+            pParty->pTurnBasedPlayerRecoveryTimes[this->uActorQueueSize] = 0;
+            ++this->uActorQueueSize;
+            }
+        }
+
+    for ( v3 = 0; v3 < ai_arrays_size ; ++v3 )
+        { 
+        actor_id = ai_near_actors_ids[v3];
+        if (actor_id == 10)
+            continue;
+        if ( pActors[actor_id].CanAct() )
+            {
+            if ( pActors[actor_id].uAttributes & 0x8000 )
+                {
+                v8 = ai_near_actors_targets_pid[actor_id];
+                pActors[actor_id].uAttributes |= 0x80;
+                memcpy(&v31, Actor::GetDirectionInfo(PID(OBJECT_Actor,actor_id), v8, &a3, 0), sizeof(AIDirection));
+                memcpy(&v30, &v31, sizeof(AIDirection));
+                Actor::AI_StandOrBored(actor_id, 4, 32, &v30);
+                this->pQueue[this->uActorQueueSize].uPackedID = PID(OBJECT_Actor,actor_id);
+                this->pQueue[this->uActorQueueSize].field_C = 2;
+                this->pQueue[this->uActorQueueSize].uActionLength = 0;
+                ++this->uActorQueueSize;
+                }
+            }
+        }
+
+    a_players_count=0;
+    for ( v40b = 0; v40b < this->uActorQueueSize; ++v40b )
+        {
+        //v13 = PID_TYPE(this->pQueue[0].uPackedID);
+        if ( PID_TYPE(this->pQueue[v40b].uPackedID) == OBJECT_Player )
+            {
+            if ( pPlayers[PID_ID(this->pQueue[v40b].uPackedID) + 1]->uTimeToRecovery != 0 )
+                {
+                //v33 = pPlayers[(this->pQueue[0].uPackedID >> 3) + 1]->uTimeToRecovery;
+                v16 = (signed int)((double)pPlayers[PID_ID(this->pQueue[v40b].uPackedID) + 1]->uTimeToRecovery * 0.46875);
+                this->pQueue[v40b].field_4 = v16;
+                }
+            else
+                {   
+                activ_players[a_players_count] = v40b;
+                ++a_players_count;
+                }
+            }
+        else if ( PID_TYPE(this->pQueue[v40b].uPackedID) == OBJECT_Actor )
+            {
+            v17 = rand() % 99;
+            if ( v17 < 33 )
+                this->pQueue[v40b].field_4 = 1;
+            else 
+                this->pQueue[v40b].field_4= (v17 >= 66)? 5 : 3; 
+            }
+        else 
+            {
+            this->pQueue[v40b].field_4 = 666;
+            }
+        this->pQueue[v40b].field_4 += 16;
+        }
+
+    if ( a_players_count > 0 )
+        {
+        for (i=0; i<a_players_count; ++i)
+            players_recovery_time[i] = pParty->pPlayers[PID_ID(this->pQueue[activ_players[i]].uPackedID)].GetAttackRecoveryTime(0);
+        //sort players by recovery time
+        for (i=0; i < a_players_count-1; ++i)
+            {  
+            for(j=i+1; j<a_players_count;++j )
+                {       
+                if (players_recovery_time[j] < players_recovery_time[i]) //swap values
+                    {
+                    temp = players_recovery_time[i];
+                    players_recovery_time[i] = players_recovery_time[j];
+                    players_recovery_time[j] = temp;
+
+                    temp = activ_players[i];
+                    activ_players[i] = activ_players[j];
+                    activ_players[j] = temp;
+                    }
+                }
+            }
+
+        for (i=0; i<a_players_count; ++i)
+            {
+            this->pQueue[activ_players[i]].field_4 = i+2;
+            }
+        }
+    this->SortTurnQueue();
+    }
+
+
+
+//----- (00405CFF) --------------------------------------------------------
+void stru262_TurnBased::End(bool bPlaySound)
+    {
+   
+    ObjectType objType; // eax@13
+    int objID; // esi@13
+    int i; 
+
+    this->turn_stage = 0;
+
+    for( i=0; i<uActorQueueSize; ++i) 
+        { 
+        if ( PID_TYPE(pQueue[i].uPackedID) == OBJECT_Actor )
+            pActors[PID_ID(pQueue[i].uPackedID)].uAttributes &= ~0x80;
+        }
+
+    for( i=0; i<uNumSpriteObjects; ++i) 
+        {
+         if (pSpriteObjects[i].uAttributes & 4)
+             pSpriteObjects[i].uAttributes &= ~0x04;
+        }
+
+    for( i=0; i<uActorQueueSize; ++i) 
+        { 
+        objType = (ObjectType)PID_TYPE(pQueue[i].uPackedID);
+        objID = PID_ID(pQueue[i].uPackedID);
+        if ( objType == OBJECT_Player )
+            {
+            pPlayers[objID + 1]->uTimeToRecovery = (signed __int64)((double)pQueue[i].field_4 * 2.133333333333333);
+            }
+        else if ( objType == OBJECT_Actor )
+            {
+             pActors[objID].pMonsterInfo.uRecoveryTime = (signed __int64)((double)pQueue[i].field_4 * 2.133333333333333);
+            }
+        }
+   
+    pAudioPlayer->StopChannels(-1, -1);
+    if ( bPlaySound != 0 )
+        pAudioPlayer->PlaySound(SOUND_206, 0, 0, -1, 0, 0, 0, 0);
+    pTurnEngine->field_18 &= ~TE_FLAG_2;
+    pEventTimer->StopGameTime();
+    dword_50C994 = 0;
+    dword_50C998_turnbased_icon_1A = 0;
+    }
+// 50C994: using guessed type int dword_50C994;
+// 50C998: using guessed type int dword_50C998_turnbased_icon_1A;
+
+//----- (00405E14) --------------------------------------------------------
+void stru262_TurnBased::_405E14()
+    {
+    AIDirection *v6; // esi@21
+    int v7; // eax@21
+    AIDirection a3; // [sp+4h] [bp-68h]@21
+    AIDirection v14; // [sp+20h] [bp-4Ch]@21
+    AIDirection v15; // [sp+3Ch] [bp-30h]@21
+    Actor *curr_actor; // [sp+58h] [bp-14h]@2
+    int target_pid; // [sp+5Ch] [bp-10h]@6
+    int shrinked;
+    int i,j;
+
+    for (i =0; i<uNumActors; ++i )
+        {
+        curr_actor=&pActors[i];
+        shrinked=pActors[i].pActorBuffs[ACTOR_BUFF_SHRINK].uExpireTime > 0;
+        for (j =0; j<22; ++j) //check expired spell Buffs
+            {
+            if(j != 10)
+                pActors[i].pActorBuffs[j].IsBuffExpiredToTime(pParty->uTimePlayed);
+            }
+        if (shrinked && pActors[i].pActorBuffs[ACTOR_BUFF_SHRINK].uExpireTime <=0) //buff 3 expired
+            {
+            pActors[i].uActorHeight = pMonsterList->pMonsters[pActors[i].pMonsterInfo.uID - 1].uMonsterHeight;
+            }
+        if(!(curr_actor->uAttributes&0x80)&&
+              (!curr_actor->pActorBuffs[ACTOR_BUFF_STONED].uExpireTime)&&
+              (!curr_actor->pActorBuffs[ACTOR_BUFF_PARALYZED].uExpireTime)) 
+            {
+            curr_actor->uCurrentActionTime += pMiscTimer->uTimeElapsed;
+            if (curr_actor->uCurrentActionTime>=curr_actor->uCurrentActionLength)
+                {
+                target_pid = ai_near_actors_targets_pid[i];
+                v6 = Actor::GetDirectionInfo(PID(OBJECT_Actor,i), target_pid, &a3, 0);  
+                memcpy(&v15, v6, sizeof(AIDirection));   
+                memcpy(&v14, &v15, sizeof(AIDirection));
+                v7 = curr_actor->uAIState;
+                if(v7==AIState::Dying)
+                    {
+                    curr_actor->uCurrentActionTime = 0;
+                    curr_actor->uCurrentActionLength = 0;
+                    curr_actor->uAIState = Dead;
+                    curr_actor->UpdateAnimation();
+                    }
+                else  if ( (v7 > AIState::Removed)&&(v7 < AIState::Disabled))
+                    {
+                     Actor::AI_StandOrBored(i, target_pid, 32, &v14);
+                    }
+                }
+            }
+        }
+    
+    if ( turn_stage == 1 )
+        {
+        if ( field_8 == 64 )
+            {
+            _406A63();
+            }
+        else  if ( field_8 > 0 )
+                {
+                _406B9F();
+                }
+        else
+                {
+                _406AFE();
+                field_10 = 100;
+                }
+        field_8 -= pEventTimer->uTimeElapsed;
+        }
+    else if ( turn_stage == 2 )
+        {
+        if ( !(field_18 & TE_FLAG_1) )
+            {
+            if ( field_10 == 100 )
+                {
+                StartTurn();
+                _40652A();
+                }
+            if ( field_10 > 0 || pQueue[0].field_4 <= 0 )
+                {
+                _4065B0();
+                _40652A();
+                }
+            }
+        else
+            NextTurn();
+        }
+    else if ( turn_stage == 3 )
+        {
+        if ( uActionPointsLeft <= 0 || field_18 & TE_FLAG_8 )
+            {
+            field_18 &= ~TE_FLAG_8;
+            turn_stage = 1;
+            field_8 = 64;
+            }
+        else
+            {
+            _406FA8();
+            }
+        }
+    }
+
+
+//----- (00406051) --------------------------------------------------------
+ void stru262_TurnBased::StartTurn()
+    {
+    int player_num, actor_num, i, j;
+
+    field_1C = 0;
+    for(player_num=0; player_num<4; ++player_num) 
+        {
+        for(j=0; j<uActorQueueSize; ++j) 
+            {
+            if (PID_TYPE(pQueue[j].uPackedID)== OBJECT_Player)
+                {
+                if (pPlayers[PID_ID(pQueue[j].uPackedID) + 1]->CanAct() && (player_num != PID_ID(pQueue[j].uPackedID)) )
+                    break;
+                }
+            }
+         if (j==uActorQueueSize )
+             {
+             pQueue[uActorQueueSize].uPackedID = PID(OBJECT_Player,player_num);
+             pQueue[uActorQueueSize].field_4 = 100;
+             pQueue[uActorQueueSize].uActionLength = 0;
+             pQueue[uActorQueueSize].field_C = 0;
+             ++uActorQueueSize;
+             }
+        }
+
+    for(actor_num=0; actor_num<ai_arrays_size; ++actor_num) 
+        {
+        for(j=0; j<uActorQueueSize; ++j) 
+            {
+            if ((PID_TYPE(pQueue[j].uPackedID)== OBJECT_Actor)&&
+                 ai_near_actors_ids[actor_num] == PID_ID(pQueue[j].uPackedID))
+                    break;
+            }
+        if (j==uActorQueueSize )
+            {
+            pQueue[uActorQueueSize].uPackedID = PID(OBJECT_Actor,ai_near_actors_ids[actor_num]);
+            pQueue[uActorQueueSize].field_4 = 1;
+            pQueue[uActorQueueSize].uActionLength = 0;
+            pQueue[uActorQueueSize].field_C = 0;
+            ++uActorQueueSize;
+            }
+        }
+
+        ++field_0;
+        field_10 = 100;
+
+        for(i=0; i<uActorQueueSize; ++i) 
+            {
+            if (pQueue[i].field_4 == 0 )
+                pQueue[i].field_4 = 100;
+            }
+
+        _4063A1();
+        for(i=0; i<uActorQueueSize; ++i) 
+            {
+            if ((PID_TYPE(pQueue[i].uPackedID)==OBJECT_Player)||(pQueue[i].field_4 > 0))
+                break;
+            _40680F(i);
+            }
+    }
+// 4F75D8: using guessed type int ai_arrays_size;
+
+ //----- (004061CA) --------------------------------------------------------
+ void stru262_TurnBased::NextTurn()
+     {
+     Actor *curr_actor; // eax@9
+     int ai_state; // ecx@9
+     int v13; // [sp+10h] [bp-4h]@7
+     int i;
+     int monster_ai_state;
+     int monster; // eax@5
+
+     SortTurnQueue();
+     if (PID_TYPE(pQueue[0].uPackedID) == OBJECT_Player)
+         uActiveCharacter = PID_ID(pQueue[0].uPackedID) + 1;
+     else
+         uActiveCharacter = 0;
+     viewparams->bRedrawGameUI = 1;
+
+     if ( field_1C )
+         {
+         pTurnEngine->field_18 |= TE_FLAG_2;
+         return;
+         }
+     pTurnEngine->field_18 &= ~TE_FLAG_2;
+     if ( pQueue[0].field_4 <= 0 )
+         return;
+
+     v13 = 0;
+     if (uActorQueueSize > 0 )
+         {   
+
+         for (i=0; i<uActorQueueSize; ++i )
+             {
+             if (PID_TYPE(pQueue[i].uPackedID) == OBJECT_Actor)
+                 {
+                 curr_actor = &pActors[PID_ID(pQueue[i].uPackedID)];
+                 ai_state = curr_actor->uAIState;
+                 if ( (ai_state == AIState::Dying) || (ai_state == AIState::Stunned) || (ai_state == AIState::AttackingMelee) ||
+                     (ai_state == AIState::AttackingRanged1) || (ai_state == AIState::AttackingRanged2) || 
+                     (ai_state == AIState::AttackingRanged3) || (ai_state == AIState::AttackingRanged4) || (ai_state ==AIState::Summoned))
+                     {
+                     curr_actor->uCurrentActionTime += pEventTimer->uTimeElapsed;
+                     if ( curr_actor->uCurrentActionTime < curr_actor->uCurrentActionLength )
+                         {
+                         v13 = 1;
+                         }
+                     else if ( ai_state == 4 )// Dying
+                         {
+                         curr_actor->uAIState = AIState::Dead;
+                         curr_actor->uCurrentActionTime = 0;
+                         curr_actor->uCurrentActionLength = 0;
+                         curr_actor->UpdateAnimation();
+                         }
+                     else
+                         {
+                         if ( ai_state == 8 ) //Stunned
+                             Actor::AI_StandOrBored(PID_ID(pQueue[i].uPackedID), ai_near_actors_targets_pid[PID_ID(pQueue[i].uPackedID)], 32, 0);
+                         }
+                     }
+                 }
+             }
+         if ( v13 != 0 )
+             {
+             field_18 |= TE_FLAG_1;
+             return;
+             }
+         }
+
+     field_18 &= ~TE_FLAG_1;       
+     for (i=0; i<uActorQueueSize; ++i )
+         {      
+         if(PID_TYPE(pQueue[i].uPackedID) == OBJECT_Actor) 
+             {
+             monster=PID_ID(pQueue[i].uPackedID);
+             monster_ai_state=pActors[monster].uAIState;
+             if ((monster_ai_state != AIState::Dead) && 
+                 (monster_ai_state != AIState::Dying) &&
+                 (monster_ai_state != AIState::Removed) &&
+                 (monster_ai_state != AIState::Summoned) &&
+                 (monster_ai_state != AIState::Disabled))
+                 {
+                 pQueue[i].uActionLength = 0;
+                 Actor::AI_StandOrBored(monster, ai_near_actors_targets_pid[monster], 32, nullptr);
+                 }
+             }
+         }
+     turn_stage = 3;
+     pParty->uTimePlayed += 213i64;
+     _494035_timed_effects__water_walking_damage__etc();
+     uActionPointsLeft = 130;
+     }
+
+ //----- (004063A1) --------------------------------------------------------
+ int stru262_TurnBased::_4063A1()
+     {
+     int v9; // dx@12 
+     int j;
+
+     SortTurnQueue();
+     viewparams->bRedrawGameUI = 1;
+     if ( pQueue[0].field_4 )
+         {
+         if (PID_TYPE(pQueue[0].uPackedID) == OBJECT_Player)
+             {
+             do
+                 {
+                 for (j=0; j<uActorQueueSize; ++j )
+                     {      
+                     --pQueue[j].field_4;
+                     }
+                 --field_10;
+                 if (field_10 == 0)
+                     return 1;
+                 }
+                 while (pQueue[0].field_4 > 0);
+             }
+         else
+             {
+             v9 = pActors[PID_ID(pQueue[0].uPackedID)].uAIState;
+             if (!(v9 == AIState::Dying || v9 == AIState::Dead || 
+                   v9 == AIState::Disabled || v9 == AIState::Removed))
+                 {
+                 do
+                     {
+                     for (j=0; j<uActorQueueSize; ++j )
+                         {      
+                         --pQueue[j].field_4;
+                         if (pQueue[j].field_4 == 1)
+                             pQueue[j].uActionLength = 0;
+                         }
+                     --field_10;
+                     if (field_10 == 0)
+                         return 1;
+                     }
+                     while (pQueue[0].field_4 > 0); 
+                 }
+             }
+         }
+     return 0;
+     }
+
+//----- (00406457) --------------------------------------------------------
+void 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
+    int i;
+
+    if (  PID_TYPE(pQueue[a2].uPackedID) == OBJECT_Player)
+        {
+        v4 = PID_ID(pQueue[a2].uPackedID);
+        if ( pParty->pTurnBasedPlayerRecoveryTimes[v4] )
+            pParty->pTurnBasedPlayerRecoveryTimes[v4] = 0;
+        else
+            v6 = pPlayers[v4 + 1]->GetAttackRecoveryTime(0);
+        if ( v6 < 30 )
+            v6 = 30;
+        }
+    else
+        {
+        v6 = pMonsterStats->pInfos[pActors[PID_ID(pQueue[a2].uPackedID)].pMonsterInfo.uID].uRecoveryTime;
+        }
+
+    pQueue[a2].field_4 = v6;
+    SortTurnQueue();
+    if (PID_TYPE(pQueue[0].uPackedID) == OBJECT_Player)
+        uActiveCharacter = PID_ID(pQueue[0].uPackedID) + 1;
+    else
+        uActiveCharacter = 0;
+    viewparams->bRedrawGameUI = 1;
+    if (pQueue[0].field_4 > 0)
+        {
+        while(field_10 > 0)
+            {
+            for (i=0; i<uActorQueueSize; ++i)
+                {
+                --pQueue[i].field_4;
+                if (pQueue[i].field_4==0)
+                    pQueue[i].uActionLength=0;
+                }
+            --field_10;
+            }
+        }
+    }
+
+//----- (0040652A) --------------------------------------------------------
+void stru262_TurnBased::_40652A()
+    {
+    int i;
+    int monster_ai_state;
+    Actor *monster; // eax@5
+
+    for (i=0; i<uActorQueueSize; ++i )
+        {
+        if (pQueue[i].field_4 == 0)
+            {
+            if(PID_TYPE(pQueue[i].uPackedID) == OBJECT_Player) 
+                break;
+            monster=&pActors[PID_ID(pQueue[i].uPackedID)];
+            monster_ai_state=monster->uAIState;
+            if (monster_ai_state == AIState::Standing || 
+                monster_ai_state == AIState::Fleeing || 
+                monster_ai_state == AIState::Fidgeting)
+                {
+                pQueue[i].field_4 = pMonsterStats->pInfos[monster->pMonsterInfo.uID].uRecoveryTime;
+                if (monster->pActorBuffs[7].uExpireTime > 0)
+                    pQueue[i].field_4*=2;
+                }
+            }
+        }
+    }
+
+//----- (004065B0) --------------------------------------------------------
+void stru262_TurnBased::_4065B0()
+{
+    int i;
+
+    SortTurnQueue();
+    if (pQueue[0].field_4 <= 0)
+        {  
+        for (i=0; i<uActorQueueSize; ++i )
+            {
+            if ((PID_TYPE(pQueue[i].uPackedID)==OBJECT_Player)|| (pQueue[i].field_4 > 0) )
+                break;
+            if ((pQueue[i].uActionLength<=0) && (PID_TYPE(pQueue[i].uPackedID)==OBJECT_Actor))
+                _40680F(i);
+            }
+        }
+    else
+        {
+        _4063A1();
+        if (PID_TYPE(pQueue[0].uPackedID) == OBJECT_Player) 
+            uActiveCharacter = PID_ID(pQueue[0].uPackedID) + 1;
+        else
+            uActiveCharacter = 0;
+        viewparams->bRedrawGameUI = 1;
+        }
+
+    for (i=0; i<uActorQueueSize; ++i )
+        AIRangedAttacks(i);
+}
+
+//----- (00406648) --------------------------------------------------------
+void stru262_TurnBased::AIRangedAttacks( unsigned int queue_index )
+    {
+    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 = &pQueue[queue_index];
+    v28 = v1;
+    v3 = v1->uPackedID;
+    if (PID_TYPE(pQueue[queue_index].uPackedID) == OBJECT_Actor)
+        {
+        v4 = PID_ID(v3);
+        a2a = ai_near_actors_targets_pid[PID_ID(pQueue[queue_index].uPackedID)];
+        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 != AIState::Dead )&& ( (short)v3 != AIState::Disabled )&&( (short)v3 != AIState::Removed ))  
+                    {
+                    v5->uCurrentActionTime += pEventTimer->uTimeElapsed;
+                    if ( (signed int)v5->uCurrentActionTime >= v5->uCurrentActionLength )
+                        {
+                         switch (v3)
+                         {
+                           case  AIState::AttackingMelee:
+                             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);
+                             Actor::AI_Stand(v4, a2a, 0, &a4);
+                             break;
+                           case AIState::AttackingRanged1:
+                             Actor::AI_RangedAttack(v4, &a4, v5->pMonsterInfo.uMissleAttack1Type, 0);
+                             Actor::AI_Stand(v4, a2a, 0,&a4);
+                             break;
+                           case AIState::Dying:
+                             v5->uCurrentActionTime = 0;
+                             v5->uCurrentActionLength = 0;
+                             v5->uAIState = Dead;
+                             pActors[v4].UpdateAnimation();
+                             break;
+                           case AIState::Stunned:
+                              Actor::AI_Stand(v4, a2a, 0,&a4);
+                              break;
+                           case AIState::AttackingRanged2:
+                             Actor::AI_RangedAttack(v4, &a4, v5->pMonsterInfo.uMissleAttack2Type, 1);
+                             Actor::AI_Stand(v4, a2a, 0,&a4);
+                             break;
+                           case AIState::AttackingRanged3:
+                             Actor::AI_SpellAttack(v4, &a4, v5->pMonsterInfo.uSpell1ID, 2, v5->pMonsterInfo.uSpellSkillAndMastery1);
+                             Actor::AI_Stand(v4, a2a, 0, &a4);
+                             break;
+                           case AIState::AttackingRanged4:
+                             Actor::AI_SpellAttack(v4, &a4, v5->pMonsterInfo.uSpell2ID, 3, v5->pMonsterInfo.uSpellSkillAndMastery2);
+                             Actor::AI_Stand(v4, a2a, 0, &a4);
+                             break;
+                           default:
+                             if ( !(rand() % 2) )
+                               Actor::AI_Bored(v4, a2a, &a4);
+                             else
+                               Actor::AI_Stand(v4, a2a, 64,&a4);
+                         }
+                    }
+        }
+
+        }
+        
+    }
+// 50FE08: using guessed type stru298 stru_50FE08;
+
+//----- (0040680F) --------------------------------------------------------
+void 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 = &pQueue[a2];
+    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;
+                        }
+                    }
+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, 64, &v18);
+                //v3 = v21;
+                v21->field_C = 0;
+                goto LABEL_48;
+                }
+            }
+        }
+    }
+
+//----- (00406A63) --------------------------------------------------------
+void stru262_TurnBased::_406A63()
+    {
+  
+    AIDirection a3; // [sp+8h] [bp-44h]@5
+    AIDirection v7; // [sp+24h] [bp-28h]@5
+    unsigned int target_pid; // [sp+40h] [bp-Ch]@5
+    int i;
+
+    this->field_8 = 64;
+    dword_50C994 = 0;
+    uActiveCharacter = 0;
+    for (i=0; i<uActorQueueSize; ++i )
+        {
+        if (PID_TYPE(pQueue[i].uPackedID) == OBJECT_Actor)
+            {
+            target_pid = ai_near_actors_targets_pid[PID_ID(pQueue[i].uPackedID)];
+            memcpy(&v7, Actor::GetDirectionInfo(pQueue[i].uPackedID, target_pid, &a3, 0), sizeof(AIDirection));
+            if ( !ActorTurn(i) )
+                Actor::AI_Stand(PID_ID(pQueue[i].uPackedID), target_pid, 32, &v7);
+            }
+        }   
+    }
+// 50C994: using guessed type int dword_50C994;
+
+//----- (00406AFE) --------------------------------------------------------
+void stru262_TurnBased::_406AFE()
+    {
+    AIDirection a3; // [sp+4h] [bp-48h]@5
+    AIDirection v7; // [sp+20h] [bp-2Ch]@5
+    unsigned int target_pid; 
+    int i;
+
+    for (i=0; i<uActorQueueSize; ++i )
+        {
+        if (PID_TYPE(pQueue[i].uPackedID) == OBJECT_Actor)
+            {
+            target_pid = ai_near_actors_targets_pid[PID_ID(pQueue[i].uPackedID)];
+            memcpy(&v7, Actor::GetDirectionInfo(pQueue[i].uPackedID, target_pid, &a3, 0), sizeof(AIDirection));
+            Actor::AI_Stand(PID_ID(pQueue[i].uPackedID), target_pid, 32, &v7);
+            pQueue[i].field_C = 0;
+            pQueue[i].uActionLength = 0;
+            }
+        }   
+    turn_stage = 2;
+    field_8 = 100;
+
+    }
+
+//----- (00406B9F) --------------------------------------------------------
+void stru262_TurnBased::_406B9F()
+    {
+    unsigned int v5; // esi@5
+    Actor *v6; // ebx@5
+    AIDirection a3; // [sp+0h] [bp-50h]@15
+    AIDirection v9; // [sp+1Ch] [bp-34h]@15
+    unsigned int v12; // [sp+40h] [bp-10h]@5
+    unsigned int v13; // [sp+44h] [bp-Ch]@8
+    int i;
+
+    for (i=0; i<uActorQueueSize; ++i )
+        {
+        if (PID_TYPE(pQueue[i].uPackedID) == OBJECT_Actor)
+            {
+            v6 = &pActors[PID_ID(pQueue[i].uPackedID)];
+            if ( !(v6->pActorBuffs[5].uExpireTime > 0|| (v6->pActorBuffs[6].uExpireTime > 0) || 
+                    v6->uAIState == AIState::Dead || v6->uAIState == AIState::Removed || v6->uAIState == AIState::Disabled) )
+                {
+                v13 = ai_near_actors_targets_pid[PID_ID(pQueue[i].uPackedID)];
+                memcpy(&v9, Actor::GetDirectionInfo(pQueue[i].uPackedID, v13, &a3, 0), sizeof(AIDirection));
+                if ( v6->uAIState == AIState::Pursuing || v6->uAIState == AIState::Tethered ) 
+                    {
+                    if ( (double)(signed int)v9.uDistance < 307.2 )
+                        Actor::AI_Stand(PID_ID(pQueue[i].uPackedID), v13, 32, &v9);
+                    }
+                else
+                    {           
+                    v6->uCurrentActionTime += pEventTimer->uTimeElapsed;
+                    if ( v6->uCurrentActionTime > v6->uCurrentActionLength )
+                        {
+                        if ( v6->uAIState == AIState::Dying )
+                            {
+                            v6->uCurrentActionTime = 0;
+                            v6->uCurrentActionLength = 0;
+                            v6->uAIState = AIState::Dead;
+                            v6->UpdateAnimation();
+                            }
+                        if ( !ActorTurn(i) )
+                            Actor::AI_Stand(PID_ID(pQueue[i].uPackedID), v13, 32, &v9);
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+//----- (00406D10) --------------------------------------------------------
+bool stru262_TurnBased::ActorTurn(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
+    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 = pQueue[a2].uPackedID;
+    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 == 0 )
+        {
+        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 ) // Dead 
+                    {
+                    if ( v6 != 11 )// Removed
+                        {
+                        if ( v6 != 19 ) //Disabled 
+                            {
+                            if ( v6 != 17 ) //Summoned
+                                {
+                                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(AIDirection));
+                                memcpy(&a4, &v11, sizeof(AIDirection));
+                                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, 64, &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 );
+        }
+    }
+
--- a/TurnEngine.h	Fri Jul 12 23:07:14 2013 +0200
+++ b/TurnEngine.h	Fri Jul 12 23:08:35 2013 +0200
@@ -1,6 +1,14 @@
 #pragma once
 
 
+enum TURN_ENGINE_FLAGS
+    {
+    TE_FLAG_1 =1,
+    TE_FLAG_2 =2,
+    TE_PLAYER_TURN =4,
+    TE_FLAG_8 =8
+    };
+
 
 /*  299 */
 #pragma pack(push, 1)
@@ -27,7 +35,7 @@
   inline stru262_TurnBased()
   {
     field_0 = 0;
-    field_4 = 0;
+    turn_stage = 0;
     field_8 = 0;
     uActorQueueSize = 0;
     field_10 = 0;
@@ -36,35 +44,35 @@
     field_1C = 0;
   }
 
-  signed int _404544();
+  void  SortTurnQueue();
   void _40471C();
-  signed int Start();
+  void  Start();
   void End(bool bPlaySound);
   void _405E14();
-  __int16 StartTurn();
+  void StartTurn();
   void NextTurn();
   int _4063A1();
-  int _406457(int a2);
+  void _406457(int a2);
   void _40652A();
   void _4065B0();
-  void _406648(unsigned int a2);
-  __int16 _40680F(int a2);
+  void AIRangedAttacks(unsigned int queue_index);
+  void  _40680F(int a2);
   void _406A63();
   void _406AFE();
-  int _406B9F();
-  bool _406D10(signed int a2);
+  void  _406B9F();
+  bool ActorTurn(signed int a2);
   void _406FA8();
 
 
   int field_0;
-  int field_4;
+  int turn_stage;
   int field_8;
-  int uActorQueueSize;
+  int uActorQueueSize; //c
   int field_10;
-  int uActionPointsLeft;
+  int uActionPointsLeft; //14
   int field_18;
   int field_1C;
-  TurnBased_QueueElem pQueue[2];
+  TurnBased_QueueElem pQueue[504]; //20
 };
 #pragma pack(pop)
 
--- a/UI/Books/UISpellBook.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/UI/Books/UISpellBook.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -182,7 +182,6 @@
     while ( v4 - 1 < 11 );
   }
 
-
   v9 = pMouse->GetCursorPos(&a2);
   v10 = pRenderer->pActiveZBuffer[v9->x + pSRZBufferLineOffsets[pMouse->GetCursorPos(&v24)->y]] & 0xFFFF;
   if ( v10 )
--- a/UI/UiGame.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/UI/UiGame.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -2289,7 +2289,7 @@
         }
     if ( pParty->bTurnBasedModeOn == 1 )
         {
-        if ( pTurnEngine->field_4 != 1 )
+        if ( pTurnEngine->turn_stage != 1 )
             {
             if (PID_TYPE(pTurnEngine->pQueue[0].uPackedID) == OBJECT_Player)
                 {
--- a/Viewport.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/Viewport.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -467,16 +467,16 @@
         pMessageQueue_50CBD0->AddMessage(UIMSG_StartNPCDialogue, v18, 0);
         return;
       }
-      if ( pParty->bTurnBasedModeOn == 1 && pTurnEngine->field_4 == 3 )
+      if ( pParty->bTurnBasedModeOn == 1 && pTurnEngine->turn_stage == 3 )
       {
-        pTurnEngine->field_18 |= 8;
+        pTurnEngine->field_18 |= TE_FLAG_8;
         return;
       }
       pMessageQueue_50CBD0->AddMessage(UIMSG_Attack, 0, 0);
     }
     else
     {
-      if ( pParty->bTurnBasedModeOn == 1 && pTurnEngine->field_4 == 3 )
+      if ( pParty->bTurnBasedModeOn == 1 && pTurnEngine->turn_stage == 3 )
       {
         pParty->uFlags |= PARTY_FLAGS_1_FALLING;
         return;
--- a/mm7_1.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/mm7_1.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -547,11 +547,11 @@
      + v2->pFacePlane_old.vNormal.y * ((signed __int16)(*(int *)&v4->x >> 16) - pBLVRenderParams->vPartyPos.y)
      + v2->pFacePlane_old.vNormal.z * (v4->z - pBLVRenderParams->vPartyPos.z) < 0 )
   {
-    stru_50B700.field_0 = 1;
+    PortalFace.field_0 = 1;
   }
   else
   {
-    stru_50B700.field_0 = 0;
+    PortalFace.field_0 = 0;
     if ( !(v2->uAttributes & 1) )
       return 0;
   }
@@ -786,7 +786,7 @@
   return result;*/
 }
 
-// 50B700: using guessed type int stru_50B700.field_0;
+// 50B700: using guessed type int PortalFace.field_0;
 
 //----- (00424CD7) --------------------------------------------------------
 signed int __fastcall sr_424CD7(unsigned int uVertexID)
--- a/mm7_2.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/mm7_2.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -6152,7 +6152,7 @@
   assert(sizeof(GUIWindow) == 0x54);
   assert(sizeof(GUIProgressBar) == 0x1B8);
   assert(sizeof(GUIFont) == 0x1020);
-  assert(sizeof(stru262_TurnBased) == 0x40);
+ // assert(sizeof(stru262_TurnBased) == 0x40);
   assert(sizeof(ArcomageGame) == 0xFB);
   assert(sizeof(CastSpellInfo) == 0x14);
   assert(sizeof(ArcomageCard) == 0x6C);
--- a/mm7_3.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/mm7_3.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -997,7 +997,7 @@
         v8 *= 2;
         v73 = v8;
       }
-      if ( pParty->bTurnBasedModeOn == 1 && pTurnEngine->field_4 == 1 )
+      if ( pParty->bTurnBasedModeOn == 1 && pTurnEngine->turn_stage == 1 )
         v8 = (signed __int64)((double)v73 * flt_6BE3AC_debug_recmod1_x_1_6);
       if ( v8 > 1000 )
         v8 = 1000;
@@ -1176,7 +1176,7 @@
       switch ( PID_TYPE(v38) )
       {
         case OBJECT_Actor:
-          if ( pTurnEngine->field_4 != 2 && pTurnEngine->field_4 != 3 || pParty->bTurnBasedModeOn != 1 )
+          if ( pTurnEngine->turn_stage != 2 && pTurnEngine->turn_stage != 3 || pParty->bTurnBasedModeOn != 1 )
           {
 			//if(pParty->bTurnBasedModeOn == 1)
 				//v34 = 0;
@@ -1874,7 +1874,7 @@
     stru_721530.field_24 = pParty->uFallSpeed;
     stru_721530.uSectorID = uSectorID;
     v38 = 0;
-    if ( pParty->bTurnBasedModeOn == 1 && pTurnEngine->field_4 == 3 )
+    if ( pParty->bTurnBasedModeOn == 1 && pTurnEngine->turn_stage == 3 )
       v38 = 13312;
     if ( stru_721530._47050A(v38) )
       break;
@@ -2778,7 +2778,7 @@
     stru_721530.field_24 = v121;
     v36 = 0;
     stru_721530.uSectorID = 0;
-    if ( pParty->bTurnBasedModeOn == 1 && pTurnEngine->field_4 == 3 )
+    if ( pParty->bTurnBasedModeOn == 1 && pTurnEngine->turn_stage == 3 )
       v36 = 13312;
     if ( stru_721530._47050A(v36) )
       break;
@@ -5958,36 +5958,21 @@
 {
   int v0; // esi@2
   int v1; // eax@2
-  //double v2; // st7@2
-  //double v3; // st6@2
-  //double v4; // st5@2
-  //double v5; // st4@2
-  //double v6; // st7@2
-  //char *v7; // esi@3
   int v8; // eax@4
   int v9; // eax@4
   int v10; // ebx@4
   signed __int64 v11; // qax@6
-  double v12; // st7@6
+  //double v12; // st7@6
   int v13; // edi@6
   int v14; // ecx@6
-  int v15; // eax@8
+  //int v15; // eax@8
   int v16; // eax@12
   signed __int64 v17; // qtt@13
-  signed int v18; // ecx@13
-  //Texture *v19; // eax@15
-  //double v20; // st6@15
-  //double v21; // st7@15
-  //double v22; // st6@15
-  //unsigned __int8 v23; // sf@15
-  //unsigned __int8 v24; // of@15
-  stru148 _this; // [sp+14h] [bp-150h]@1
-  //double v26; // [sp+120h] [bp-44h]@4
-  //float v27; // [sp+128h] [bp-3Ch]@4
+  signed int pShading; // ecx@13
+  stru148 pSky; // [sp+14h] [bp-150h]@1
   float v28; // [sp+12Ch] [bp-38h]@2
   int v29; // [sp+130h] [bp-34h]@4
   int v30; // [sp+134h] [bp-30h]@1
-  //int v31; // [sp+138h] [bp-2Ch]@2
   int v32; // [sp+13Ch] [bp-28h]@6
   int v33; // [sp+140h] [bp-24h]@2
   int v34; // [sp+144h] [bp-20h]@1
@@ -6007,26 +5992,32 @@
 
   v30 = ((double)(pOutdoorCamera->int_fov_rad * pIndoorCamera->pos.z)
         / ((double)pOutdoorCamera->int_fov_rad + 8192.0) + pViewport->uScreenCenterY);
-  v38 = pViewport->uScreenCenterY - 
-        pOutdoorCamera->int_fov_rad / (pOutdoorCamera->shading_dist_mist * cos(pIndoorCamera->sRotationX * 0.003066406352445483) + 0.0000001000000011686097) *
-        (pOutdoorCamera->shading_dist_mist * -sin(pIndoorCamera->sRotationX * 0.003066406352445483) - pIndoorCamera->pos.z);
-  _this._48607B(&stru_8019C8);
-  _this.ptr_38->_48694B();
-  _this.uTileBitmapID = pOutdoor->uSky_TextureID;
-  _this.pTexture = (Texture *)(SLOWORD(pOutdoor->uSky_TextureID) != -1 ? &pBitmaps_LOD->pTextures[SLOWORD(pOutdoor->uSky_TextureID)] : 0);
+  v38 = pViewport->uScreenCenterY - pOutdoorCamera->int_fov_rad /
+       (pOutdoorCamera->shading_dist_mist * cos(pIndoorCamera->sRotationX * 0.003066406352445483) + 0.0000001000000011686097) *
+       (pOutdoorCamera->shading_dist_mist * -sin(pIndoorCamera->sRotationX * 0.003066406352445483) - pIndoorCamera->pos.z);
+
+  pSky._48607B(&stru_8019C8);
+  pSky.ptr_38->sky_48694B();
+  pSky.uTileBitmapID = pOutdoor->uSky_TextureID;//номер текстуры
+  pSky.pTexture = (Texture *)(pOutdoor->uSky_TextureID != -1 ? &pBitmaps_LOD->pTextures[pOutdoor->uSky_TextureID] : 0);//текстура неба
   if (pOutdoor->uSky_TextureID == -1)
     return;
 
-  _this.dimming_level = 0;
-  _this.uNumVertices = 4;
-  _this.v_18.x = -stru_5C6E00->Sin(pIndoorCamera->sRotationX + 16);
-  _this.v_18.y = 0;
-  _this.v_18.z = -stru_5C6E00->Cos(pIndoorCamera->sRotationX + 16);
-  _this.field_24 = 0x2000000;
-
-  _this.sTextureDeltaU = 224 * pMiscTimer->uTotalGameTimeElapsed;
-  _this.sTextureDeltaV = 224 * pMiscTimer->uTotalGameTimeElapsed;
-
+  pSky.dimming_level = 0;//затемнение
+  pSky.uNumVertices = 4;//количество вершин
+
+  //centering(центруем)---------------------------------------------------------------
+  pSky.v_18.x = -stru_5C6E00->Sin(pIndoorCamera->sRotationX + 16);//-3216
+  pSky.v_18.y = 0;
+  pSky.v_18.z = -stru_5C6E00->Cos(pIndoorCamera->sRotationX + 16);//-65457
+  //---------------------------------------------------------------------------
+
+  pSky.field_24 = 0x2000000;
+
+  pSky.sTextureDeltaU = 224 * pMiscTimer->uTotalGameTimeElapsed;
+  pSky.sTextureDeltaV = 224 * pMiscTimer->uTotalGameTimeElapsed;
+  
+  //sky position(положение неба)----------------------------------------------
   array_50AC10[0].vWorldViewProjX = pViewport->uViewportTL_X;
   array_50AC10[0].vWorldViewProjY = pViewport->uViewportTL_Y;
 
@@ -6038,9 +6029,10 @@
 
   array_50AC10[3].vWorldViewProjX = pViewport->uViewportBR_X;
   array_50AC10[3].vWorldViewProjY = pViewport->uViewportTL_Y;
+  //--------------------------------------------------------------------------
 
   //pParty->sRotationY / 2048.0f
-    
+  // move sky(двидение неба)--------------include-----------------------------
     float t = (GetTickCount() % 96000) / 96000.0f;
     array_50AC10[0].u = t - pParty->sRotationY / 1024.0f;
     array_50AC10[0].v = t - pParty->sRotationX / 512.0f;
@@ -6053,100 +6045,101 @@
 
     array_50AC10[3].u = 1 + t - pParty->sRotationY / 1024.0f;
     array_50AC10[3].v = t - pParty->sRotationX / 512.0f;
-
-  v36 = (double)(pViewport->uViewportBR_X - pViewport->uViewportTL_X) * 0.5;
-  v33 = 65536 / (signed int)(v36 / tan(0.6457717418670654) + 0.5);
-
-  for (uint i = 0; i < _this.uNumVertices; ++i)
-  {
-    v29 = floorf(array_50AC10[i].vWorldViewProjY + 0.5f);
-    v39 = (unsigned __int64)(_this.ptr_38->field_14 * v33 * (v30 - v29)) / 65536;
-    v8 = v39 + _this.ptr_38->field_C;
-
-    v39 = (unsigned __int64)(_this.ptr_38->field_20 * v33 * (v30 - v29)) / 65536;
-    v36 = v39 + _this.ptr_38->field_18;
-
-    v9 = (unsigned __int64)(_this.v_18.z * v33 * (v30 - v29)) / 65536;
-    v10 = _this.v_18.x + v9;
-    v39 = _this.v_18.x + v9;
-    if ( _this.v_18.x + v9 > 0 )
+  //--------------------------------------------------------------------------
+
+  //rotate skydome(вращение купола неба)--------------------------------------
+  v36 = (pViewport->uViewportBR_X - pViewport->uViewportTL_X) * 0.5;//центр экрана 230
+  v33 = 65536 / (signed int)(v36 / tan(0.6457717418670654) + 0.5);//214
+
+  for (uint i = 0; i < pSky.uNumVertices; ++i)
+  {
+    v29 = floorf(array_50AC10[i].vWorldViewProjY + 0.5f);//8
+    v39 = (unsigned __int64)(pSky.ptr_38->field_14 * v33 * (v30 - v29)) / 65536;//0
+    v8 = v39 + pSky.ptr_38->field_C;//0
+
+    v39 = (unsigned __int64)(pSky.ptr_38->field_20 * v33 * (v30 - v29)) / 65536;//0
+    v36 = v39 + pSky.ptr_38->field_18;//65536
+
+    v9 = (unsigned __int64)(pSky.v_18.z * v33 * (v30 - v29)) / 65536;//27917
+    v10 = pSky.v_18.x + v9;//24701
+    v39 = pSky.v_18.x + v9;//24701
+    if ( pSky.v_18.x + v9 > 0 )
     {
       v10 = 0;
       v39 = 0;
     }
     //v11 = (signed __int64)array_50AC10[i].vWorldViewProjX;
     v38 = v10;
-    v12 = array_50AC10[i].vWorldViewProjY - 1.0;
+    //v12 = array_50AC10[i].vWorldViewProjY - 1.0;
     v13 = v33 * (pViewport->uScreenCenterX - (signed __int64)array_50AC10[i].vWorldViewProjX);
-    v34 = -_this.field_24;
-    v32 = (signed __int64)v12;
+    v34 = -pSky.field_24;
+    v32 = (signed __int64)array_50AC10[i].vWorldViewProjY - 1.0;
     v14 = v33 * (v30 - v32);
+
     while ( 1 )
     {
-      //v40 = v14;
+      v40 = v14;
       if ( !v10 )
       {
-        v37 = _this.v_18.z;
-        v16 = (unsigned __int64)(_this.v_18.z * (signed __int64)v14) >> 16;
+        v37 = pSky.v_18.z;
+        v16 = (unsigned __int64)(pSky.v_18.z * (signed __int64)v14) >> 16;
         --v32;
         v14 += v33;
-        v10 = _this.v_18.x + v16;
-        v39 = _this.v_18.x + v16;
-        v38 = _this.v_18.x + v16;
+        v10 = pSky.v_18.x + v16;
+        v39 = pSky.v_18.x + v16;
+        v38 = pSky.v_18.x + v16;
         break;
       }
       v37 = abs(v34 >> 14);
-      v15 = abs(v10);
-      if ( v37 <= v15 || v32 <= pViewport->uViewportTL_Y )
+      if ( abs(v34 >> 14) <= abs(v10) || v32 <= pViewport->uViewportTL_Y )
       {
         if ( v39 <= 0 )
           break;
       }
-      //v14 = v40;
-      v37 = _this.v_18.z;
-      v16 = (unsigned __int64)(_this.v_18.z * v14) >> 16;
+      v14 = v40;
+      v37 = pSky.v_18.z;
+      v16 = (unsigned __int64)(pSky.v_18.z * v14) >> 16;
       --v32;
       v14 += v33;
-      v10 = _this.v_18.x + v16;
-      v39 = _this.v_18.x + v16;
-      v38 = _this.v_18.x + v16;
+      v10 = pSky.v_18.x + v16;
+      v39 = pSky.v_18.x + v16;
+      v38 = pSky.v_18.x + v16;
       break;
     }
+
     LODWORD(v17) = v34 << 16;
     HIDWORD(v17) = v34 >> 16;
-    v18 = v17 / v38;
-    if ( v18 < 0 )
-      v18 = pOutdoorCamera->shading_dist_mist;
-
-    v37 += ((unsigned __int64)(_this.ptr_38->field_10 * v13) >> 16);
-    v36 += ((unsigned __int64)(_this.ptr_38->field_1C * v13) >> 16);
-    v35 = 224 * pMiscTimer->uTotalGameTimeElapsed + fixpoint_sub0(v37, v18) / 8;
-    v36 = 224 * pMiscTimer->uTotalGameTimeElapsed + fixpoint_sub0(v36, v18) / 8;
+    pShading = v17 / v38;
+    if ( pShading < 0 )
+      pShading = pOutdoorCamera->shading_dist_mist;
+
+    v37 += ((unsigned __int64)(pSky.ptr_38->field_10 * v13) >> 16);
+    v36 += ((unsigned __int64)(pSky.ptr_38->field_1C * v13) >> 16);
+    v35 = 224 * pMiscTimer->uTotalGameTimeElapsed + fixpoint_sub0(v37, pShading) / 8;
+    v36 = 224 * pMiscTimer->uTotalGameTimeElapsed + fixpoint_sub0(v36, pShading) / 8;
 
     //array_50AC10[i].vWorldViewPosition.x = pOutdoorCamera->shading_dist_mist;
-    //array_50AC10[i].vWorldPosition.x = v36 / (_this.pTexture->uTextureHeight * 65536.0);
+    //array_50AC10[i].vWorldPosition.x = v36 / (pSky.pTexture->uTextureHeight * 65536.0);
     //array_50AC10[i].vWorldPosition.y = 1.0 / (pOutdoorCamera->shading_dist_mist >> 16);
-    //array_50AC10[i].vWorldPosition.z = v35 / (_this.pTexture->uTextureWidth * 65536.0);
+    //array_50AC10[i].vWorldPosition.z = v35 / (pSky.pTexture->uTextureWidth * 65536.0);
     //array_50AC10[i]._rhw = 1.0f / (v18 >> 16);
-    //array_50AC10[i].u = (double)v35 / (65536.0 * _this.pTexture->uTextureWidth);
-    //array_50AC10[i].v = (double)v36 / (65536.0 * _this.pTexture->uTextureHeight);
-    //float t = (GetTickCount() % 96000) / 96000.0f;
+    //array_50AC10[i].u = (double)v35 / (65536.0 * pSky.pTexture->uTextureWidth);
+    //array_50AC10[i].v = (double)v36 / (65536.0 * pSky.pTexture->uTextureHeight);
+  //-----------------------------------------------------------------------------------------
+
     array_50AC10[i]._rhw = 1.0f;
-    //array_50AC10[i].u = t;
-    //array_50AC10[i].v = t;
-  }
-    //if ( i  == _this.uNumVertices - 1 )
-    //{
-      pRenderer->DrawSkyPolygon(_this.uNumVertices, &_this, pBitmaps_LOD->pHardwareTextures[_this.uTileBitmapID]);
-
-      array_50AC10[0].vWorldViewProjY = v38;
-      array_50AC10[1].vWorldViewProjY = array_50AC10[1].vWorldViewProjY + 30.0;
-      array_50AC10[2].vWorldViewProjY = array_50AC10[2].vWorldViewProjY + 30.0;
-      array_50AC10[3].vWorldViewProjY = v38;
-
-      pRenderer->DrawSkyPolygon(_this.uNumVertices, &_this, pBitmaps_LOD->pHardwareTextures[_this.uTileBitmapID]);
-      //return;
-    //}
+  }
+  //if ( i  == _this.uNumVertices - 1 )
+  //{
+    pRenderer->DrawSkyPolygon(pSky.uNumVertices, &pSky, pBitmaps_LOD->pHardwareTextures[pSky.uTileBitmapID]);
+
+    array_50AC10[0].vWorldViewProjY = v38;
+    array_50AC10[1].vWorldViewProjY = array_50AC10[1].vWorldViewProjY + 30.0;
+    array_50AC10[2].vWorldViewProjY = array_50AC10[2].vWorldViewProjY + 30.0;
+    array_50AC10[3].vWorldViewProjY = v38;
+
+    pRenderer->DrawSkyPolygon(pSky.uNumVertices, &pSky, pBitmaps_LOD->pHardwareTextures[pSky.uTileBitmapID]);
+  //}
 }
 
 //----- (00479A53) --------------------------------------------------------
@@ -7452,17 +7445,16 @@
 }
 
 //----- (004811A3) --------------------------------------------------------
-void stru148::_4811A3()
-{
-  __debugbreak();
-
-  ErrD3D(pRenderer->pRenderD3D->pDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, 0));
+void stru148::DrawBorderTiles()
+{
+  //__debugbreak();
+
+  pRenderer->pRenderD3D->pDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, false);
   pRenderer->DrawTerrainPolygon(uNumVertices, this,
-    pBitmaps_LOD->pHardwareTextures[pRenderer->pHDWaterBitmapIDs[pRenderer->hd_water_tile_id]],
-    0, 0);
-
-  ErrD3D(pRenderer->pRenderD3D->pDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, 1));
-  pRenderer->DrawTerrainPolygon(uNumVertices, this, pBitmaps_LOD->pHardwareTextures[uTileBitmapID], 1, 1);
+    pBitmaps_LOD->pHardwareTextures[pRenderer->pHDWaterBitmapIDs[pRenderer->hd_water_current_frame]], false, true);
+
+  pRenderer->pRenderD3D->pDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, true);
+  //pRenderer->DrawTerrainPolygon(uNumVertices, this, pBitmaps_LOD->pHardwareTextures[uTileBitmapID], true, true);
 }
 
 //----- (00481DB2) --------------------------------------------------------
@@ -10536,7 +10528,7 @@
 }
 
 //----- (0048694B) --------------------------------------------------------
-int stru149::_48694B()
+int stru149::sky_48694B()
 {
   int v1; // eax@1
   int v2; // ST04_4@1
@@ -12418,367 +12410,3 @@
     return pCosTable[v2];
 }
 
-//----- (00404544) --------------------------------------------------------
-signed int stru262_TurnBased::_404544()
-{
-  stru262_TurnBased *v1; // ebx@1
-  TurnBased_QueueElem *v2; // esi@2
-  Actor *v3; // edi@4
-  Actor *v4; // ecx@4
-  Player *v5; // eax@7
-  int v6; // ecx@15
-  TurnBased_QueueElem *v7; // eax@16
-  TurnBased_QueueElem *v8; // ecx@18
-  int v9; // edx@19
-  int v10; // esi@19
-  int v11; // esi@21
-  int v12; // ST0C_4@25
-  int v13; // ST10_4@25
-  int v14; // ST14_4@25
-  int v15; // ST18_4@25
-  signed int result; // eax@28
-  TurnBased_QueueElem *v17; // edi@32
-  int v18; // [sp+20h] [bp-Ch]@17
-  int v19; // [sp+24h] [bp-8h]@1
-  int v20; // [sp+28h] [bp-4h]@1
-  signed int v21; // [sp+28h] [bp-4h]@16
-  int v22; // [sp+28h] [bp-4h]@31
-
-  v20 = 0;
-  v1 = this;
-  v19 = this->uActorQueueSize;
-  if ( this->uActorQueueSize > 0 )
-  {
-    v2 = this->pQueue;
-    do
-    {
-      if ( PID_TYPE(v2->uPackedID) == OBJECT_Actor )
-      {
-        v3 = &pActors[PID_ID(v2->uPackedID)];
-        v4 = &pActors[PID_ID(v2->uPackedID)];
-        LOBYTE(v4->uAttributes) |= 0x80u;
-        if ( !v4->CanAct() )
-        {
-          --v19;
-          v2->field_4 = 1001;
-          LOBYTE(v3->uAttributes) &= 0x7Fu;
-        }
-      }
-      if ( PID_TYPE(v2->uPackedID) == OBJECT_Player)
-      {
-        v5 = &pParty->pPlayers[v2->uPackedID >> 3];
-        if ( v5->pConditions[14]
-          || v5->pConditions[16]
-          || v5->pConditions[15]
-          || v5->pConditions[13]
-          || v5->pConditions[12]
-          || v5->pConditions[2] )
-        {
-          --v19;
-          v2->field_4 = 1001;
-        }
-      }
-      ++v20;
-      ++v2;
-    }
-    while ( v20 < v1->uActorQueueSize );
-  }
-  v6 = v1->uActorQueueSize;
-  if ( v6 > 0 )
-  {
-    v21 = 1;
-    v7 = v1->pQueue;
-    do
-    {
-      v18 = v21;
-      if ( v21 < v6 )
-      {
-        v8 = v7 + 1;
-        do
-        {
-          v9 = v8->field_4;
-          v10 = v7->field_4;
-          if ( v9 < v10
-            || v9 == v10
-            && ((v11 = PID_TYPE(v8->uPackedID), v11 == OBJECT_Player) && PID_TYPE(v7->uPackedID) == OBJECT_Actor
-             || v11 == PID_TYPE(v7->uPackedID) && (v8->uPackedID & 0xFFFFFFF8) < (v7->uPackedID & 0xFFFFFFF8)) )
-          {
-            v12 = v7->uPackedID;
-            v13 = v7->field_4;
-            v14 = v7->uActionLength;
-            v15 = v7->field_C;
-            v7->uPackedID = v8->uPackedID;
-            v7->field_4 = v8->field_4;
-            v7->uActionLength = v8->uActionLength;
-            v7->field_C = v8->field_C;
-            v8->uPackedID = v12;
-            v8->field_4 = v13;
-            v8->uActionLength = v14;
-            v8->field_C = v15;
-          }
-          ++v18;
-          ++v8;
-        }
-        while ( v18 < v1->uActorQueueSize );
-      }
-      ++v21;
-      v6 = v1->uActorQueueSize;
-      ++v7;
-    }
-    while ( v21 - 1 < v6 );
-  }
-  v1->uActorQueueSize = v19;
-  result = v1->pQueue[0].uPackedID;
-  if ( PID_TYPE(v1->pQueue[0].uPackedID) == OBJECT_Player)
-  {
-    result = PID_ID(result) + 1;
-    uActiveCharacter = result;
-    v1->field_18 |= 4u;
-  }
-  else
-  {
-    uActiveCharacter = 0;
-    v1->field_18 &= 0xFFFFFFFBu;
-  }
-  v22 = 0;
-  if ( v1->uActorQueueSize > 0 )
-  {
-    v17 = v1->pQueue;
-    do
-    {
-      if ( PID_TYPE(v17->uPackedID) == OBJECT_Player)
-        pParty->pPlayers[PID_ID(v17->uPackedID)].uTimeToRecovery = (signed __int64)((double)v17->field_4 * 0.46875);
-      ++v22;
-      ++v17;
-      result = v22;
-    }
-    while ( v22 < v1->uActorQueueSize );
-  }
-  return result;
-}
-
-//----- (0040471C) --------------------------------------------------------
-void stru262_TurnBased::_40471C()
-{
-  if ( pParty->bTurnBasedModeOn == 1 )
-  {
-    if ( pTurnEngine->field_4 == 2 )
-      _406457(0);
-  }
-}
-
-//----- (004059DB) --------------------------------------------------------
-signed int stru262_TurnBased::Start()
-{
-  //stru262_TurnBased *v1; // ebx@1
-  unsigned int v2; // edi@1
-  int v3; // esi@1
-  int v4; // eax@5
-  unsigned int v5; // esi@7
-  Actor *pActor; // edi@7
-  unsigned int v7; // eax@9
-  unsigned int v8; // edx@10
-  //unsigned __int8 v9; // zf@14
-  //unsigned __int8 v10; // sf@14
-  //unsigned __int8 v11; // of@14
-  //char *v12; // esi@15
-  //int v13; // ecx@16
-  //unsigned __int16 v14; // ax@17
-  int v15; // ecx@18
-  signed __int64 v16; // qax@19
-  //int v17; // edx@22
-  unsigned int v18; // esi@27
-  char *v19; // esi@32
-  int v20; // edx@33
-  char *v21; // eax@33
-  int v22; // ecx@33
-  int v23; // eax@34
-  char *v24; // eax@34
-  char *v25; // ecx@34
-  int v26; // edx@34
-  int v27; // eax@38
-  int v28; // ecx@38
-  AIDirection v30; // [sp+Ch] [bp-68h]@10
-  AIDirection v31; // [sp+28h] [bp-4Ch]@10
-  AIDirection a3; // [sp+44h] [bp-30h]@10
-  //int v33; // [sp+60h] [bp-14h]@10
-  int *v34; // [sp+64h] [bp-10h]@6
-  int v35; // [sp+68h] [bp-Ch]@5
-  Player *pPlayer; // [sp+6Ch] [bp-8h]@1
-  int v40b;
-  unsigned int v37; // [sp+70h] [bp-4h]@7
-
-  pTurnEngine->field_18 &= 0xFFFFFFFDu;
-  //v1 = this;
-  pEventTimer->TrackGameTime();
-  pAudioPlayer->StopChannels(-1, -1);
-  v2 = 0;
-  pAudioPlayer->PlaySound((SoundID)(SOUND_GoldReceived|SOUND_EnteringAHouse|0x1), 0, 0, -1, 0, 0, 0, 0);
-  pPlayer = pParty->pPlayers;
-  dword_50C998_turnbased_icon_1A = 8 * pIconsFrameTable->pIcons[uIconID_TurnStart].uAnimLength;
-  dword_50C994 = 0;
-  this->field_10 = 100;
-  this->field_0 = 0;
-  this->field_8 = 64;
-  this->field_4 = 1;
-  this->uActorQueueSize = 0;
-  for ( v3 = 0; pPlayer <= &pParty->pPlayers[3]; ++v3 )
-  {
-    if ( pPlayer->CanAct() )
-    {
-      this->pQueue[this->uActorQueueSize].uPackedID = PID(OBJECT_Player,v3);
-      this->pQueue[this->uActorQueueSize].field_C = 2;
-      this->pQueue[this->uActorQueueSize].uActionLength = 0;
-      pParty->pTurnBasedPlayerRecoveryTimes[this->uActorQueueSize++] = 0;
-    }
-    ++pPlayer;
-  }
-  v35 = this->uActorQueueSize;
-  v4 = this->uActorQueueSize;
-  v40b = this->uActorQueueSize;
-  if ( v40b < v40b + ai_arrays_size )
-  {
-    v34 = (int *)ai_near_actors_ids.data();
-    do
-    {
-      v37 = *v34;
-      v5 = v37;
-      pActor = &pActors[v37];
-      if ( v37 != 10 )
-      {
-        if ( pActors[v37].CanAct() )
-        {
-          v7 = pActor->uAttributes;
-          if ( pActor->uAttributes & 0x8000 )
-          {
-            v8 = ai_near_actors_targets_pid[v5];
-            LOBYTE(v7) = v7 | 0x80;
-            pActor->uAttributes = v7;
-            //v33 = PID(OBJECT_Actor,v5);
-            memcpy(&v31, Actor::GetDirectionInfo(PID(OBJECT_Actor,v5), v8, &a3, 0), sizeof(v31));
-            memcpy(&v30, &v31, sizeof(v30));
-            Actor::AI_StandOrBored(v37, 4, 32, &v30);
-            this->pQueue[this->uActorQueueSize].uPackedID = PID(OBJECT_Actor,v5);
-            this->pQueue[this->uActorQueueSize].field_C = 2;
-            this->pQueue[this->uActorQueueSize++].uActionLength = 0;
-          }
-        }
-        v4 = v35;
-      }
-      ++v40b;
-      ++v34;
-    }
-    while ( v40b < v4 + ai_arrays_size );
-    v2 = 0;
-  }
-  //v11 = __OFSUB__(this->uActorQueueSize, v2);
-  //v9 = this->uActorQueueSize == v2;
-  //v10 = ((this->uActorQueueSize - v2) & 0x80000000u) != 0;
-  v37 = v2;
-  //if ( !((unsigned __int8)(v10 ^ v11) | v9) )
-  if ( this->uActorQueueSize > v2 )
-  {
-    //v12 = (char *)&this->pQueue[0].field_4;
-
-    //while ( 1 )
-    for ( v40b = v2; v40b < this->uActorQueueSize; ++v40b )
-    {
-      //v13 = PID_TYPE(this->pQueue[0].uPackedID);
-      if ( PID_TYPE(this->pQueue[0].uPackedID) != OBJECT_Player )
-        break;
-      //v14 = pPlayers[(this->pQueue[0].uPackedID >> 3) + 1]->uTimeToRecovery;
-      if ( pPlayers[(this->pQueue[0].uPackedID >> 3) + 1]->uTimeToRecovery != (short)v2 )
-      {
-        //v33 = pPlayers[(this->pQueue[0].uPackedID >> 3) + 1]->uTimeToRecovery;
-        v16 = (signed __int64)((double)pPlayers[(this->pQueue[0].uPackedID >> 3) + 1]->uTimeToRecovery * 0.46875);
-        *(int *)this->pQueue[0].field_4 = v16;
-        this->pQueue[0].field_4 += 16;
-        continue;
-      }
-      v15 = v37++;
-      *(&a3.uDistance + v15) = v40b;
-
-      if ( PID_TYPE(this->pQueue[0].uPackedID) != 3 )
-      {
-        this->pQueue[0].field_4 = 666;
-        this->pQueue[0].field_4 += 16;
-        continue;
-      }
-      //v17 = rand() % 99;
-      if ( rand() % 99 < 33 )
-      {
-        *(int *)this->pQueue[0].field_4 = 1;
-        this->pQueue[0].field_4 += 16;
-        continue;
-      }
-      //LODWORD(v16) = SHIDWORD(v16) < 66;
-      LOBYTE(v16) = SHIDWORD(v16) >= 66;
-      LODWORD(v16) = 2 * v16 + 3;
-      *(int *)this->pQueue[0].field_4 = v16;
-      this->pQueue[0].field_4 += 16;
-    }
-  }
-
-LABEL_27:
-  v18 = 0;
-  if ( (signed int)v37 > (signed int)v2 )
-  {
-    do
-    {
-      //__debugbreak();
-      *(&v31.uDistance + v18) = pParty->pPlayers[this->pQueue[*(&a3.uDistance + v18)].uPackedID >> 3].GetAttackRecoveryTime(v2);//result crash
-      ++v18;
-    }
-    while ( (signed int)v18 < (signed int)v37 );
-    if ( (signed int)v37 > (signed int)v2 )
-    {
-      v35 = v2;
-      v34 = (int *)1;
-      do
-      {
-        v40b = (int)v34;
-        if ( (signed int)v34 < (signed int)v37 )
-        {
-          v19 = (char *)&v31.uDistance + v35;
-          do
-          {
-            v20 = *(int *)v19;
-            //v33 = 4 * v40b;
-            v21 = (char *)(&v31.uDistance + v40b);
-            v22 = *(int *)v21;
-            if ( *(int *)v21 < v20 )
-            {
-              *(int *)v21 = v20;
-              v23 = 4 * v40b;
-              *(int *)v19 = v22;
-              v24 = (char *)&a3.uDistance + v23;
-              v25 = (char *)&a3.uDistance + v35;
-              v26 = *(int *)v24;
-              *(int *)v24 = *(unsigned int *)((char *)&a3.uDistance + v35);
-              *(int *)v25 = v26;
-              v2 = 0;
-            }
-            ++v40b;
-          }
-          while ( v40b < (signed int)v37 );
-        }
-        v34 = (int *)((char *)v34 + 1);
-        v35 += 4;
-      }
-      while ( (signed int)((char *)v34 - 1) < (signed int)v37 );
-      if ( (signed int)v37 > (signed int)v2 )
-      {
-        do
-        {
-          v27 = v2 + 2;
-          v28 = *(&a3.uDistance + v2++);
-          //v11 = __OFSUB__(v2, v37);
-          //v10 = ((v2 - v37) & 0x80000000u) != 0;
-          this->pQueue[v28].field_4 = v27;
-        }
-        while ( v37 > v2 );
-      }
-    }
-  }
-  return this->_404544();
-}
--- a/mm7_4.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/mm7_4.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -1875,7 +1875,7 @@
     v43 = (signed __int64)v21->pPlayerBuffs[7].uExpireTime > 0;
 
     for (uint k = 0; k < 24; ++k)
-      v21->pPlayerBuffs[k]._4585CA(pParty->uTimePlayed);
+      v21->pPlayerBuffs[k].IsBuffExpiredToTime(pParty->uTimePlayed);
 
     if ( v43 && (signed __int64)v21->pPlayerBuffs[7].uExpireTime <= 0 )
       v21->SetCondition(1u, 0);
@@ -1886,7 +1886,7 @@
 
   for (uint i = 0; i < 20; ++i)
   {
-    if (pParty->pPartyBuffs[i]._4585CA(pParty->uTimePlayed) == 1)
+    if (pParty->pPartyBuffs[i].IsBuffExpiredToTime(pParty->uTimePlayed) == 1)
       viewparams->bRedrawGameUI = 1;
   }
 
@@ -3494,7 +3494,7 @@
       v91 = &pIndoor->pFaces[sFaceID];
       if ( !pRenderer->pRenderD3D )
       {
-        result = sub_423B5D(sFaceID);
+        result = GetPortalScreenCoord(sFaceID);
         if ( result )
         {
           result = sub_424829(result, &stru_F8A590, pBLVRenderParams->field_7C, v2);
@@ -3959,7 +3959,7 @@
     }
     else
     {
-      v59 = sub_423B5D(uFaceID);
+      v59 = GetPortalScreenCoord(uFaceID);
       v3 = v59;
     }
     if ( v3 && (pRenderer->pRenderD3D || sub_424829(v3, &stru_F8A590, pBLVRenderParams->field_7C, v1)) )
@@ -6276,250 +6276,3 @@
 
 
 
-//----- (00405CFF) --------------------------------------------------------
-void stru262_TurnBased::End(bool bPlaySound)
-{
-  stru262_TurnBased *v2; // edi@1
-  int v3; // ebx@1
-  int v4; // esi@1
-  unsigned __int8 v5; // zf@1
-  unsigned __int8 v6; // sf@1
-  TurnBased_QueueElem *v7; // ecx@2
-  unsigned __int16 *pAttributes; // ecx@7
-  size_t v9; // edx@7
-  unsigned __int16 v10; // ax@8
-  TurnBased_QueueElem *v11; // ebx@12
-  ObjectType objType; // eax@13
-  int objID; // esi@13
-  int v14; // [sp+Ch] [bp-4h]@11
-
-  v2 = this;
-  v3 = 0;
-  v4 = 0;
-  v5 = this->uActorQueueSize == 0;
-  v6 = this->uActorQueueSize < 0;
-  this->field_4 = 0;
-  if ( !(v6 | v5) )
-  {
-    v7 = this->pQueue;
-    do
-    {
-      if ( PID_TYPE(v7->uPackedID) == OBJECT_Actor )
-        LOBYTE(pActors[PID_ID(v7->uPackedID)].uAttributes) &= 0x7Fu;
-      ++v4;
-      ++v7;
-    }
-    while ( v4 < v2->uActorQueueSize );
-  }
-  if ( (signed int)uNumSpriteObjects > 0 )
-  {
-    pAttributes = &pSpriteObjects[0].uAttributes;
-    v9 = uNumSpriteObjects;
-    do
-    {
-      v10 = *pAttributes;
-      if ( *pAttributes & 4 )
-      {
-        LOBYTE(v10) = v10 & 0xFB;
-        *pAttributes = v10;
-      }
-      pAttributes += 56;
-      --v9;
-    }
-    while ( v9 );
-  }
-  v14 = 0;
-  if ( v2->uActorQueueSize > 0 )
-  {
-    v11 = v2->pQueue;
-    do
-    {
-      objType = (ObjectType)PID_TYPE(v11->uPackedID);
-      objID = PID_ID(v11->uPackedID);
-      if ( objType == OBJECT_Player )
-      {
-        pPlayers[objID + 1]->uTimeToRecovery = (signed __int64)((double)v11->field_4 * 2.133333333333333);
-      }
-      else
-      {
-        if ( objType == OBJECT_Actor )
-          pActors[objID].pMonsterInfo.uRecoveryTime = (signed __int64)((double)v11->field_4 * 2.133333333333333);
-      }
-      ++v14;
-      ++v11;
-    }
-    while ( v14 < v2->uActorQueueSize );
-    v3 = 0;
-  }
-  pAudioPlayer->StopChannels(-1, -1);
-  if ( bPlaySound != v3 )
-    pAudioPlayer->PlaySound((SoundID)(SOUND_GoldReceived|SOUND_EnteringAHouse), v3, v3, -1, v3, v3, v3, v3);
-  pTurnEngine->field_18 &= 0xFFFFFFFDu;
-  pEventTimer->StopGameTime();
-  dword_50C994 = v3;
-  dword_50C998_turnbased_icon_1A = v3;
-}
-// 50C994: using guessed type int dword_50C994;
-// 50C998: using guessed type int dword_50C998_turnbased_icon_1A;
-
-//----- (00405E14) --------------------------------------------------------
-void stru262_TurnBased::_405E14()
-{
-  stru262_TurnBased *v1; // esi@1
-  int v2; // ecx@1
-  SpellBuff *v3; // edi@2
-  Actor *v4; // ebx@3
-  signed int v5; // edx@20
-  AIDirection *v6; // esi@21
-  int v7; // eax@21
-  int v8; // eax@21
-  int v9; // eax@22
-  int v10; // eax@23
-  int v11; // eax@37
-  int v12; // eax@44
-  AIDirection a3; // [sp+4h] [bp-68h]@21
-  AIDirection v14; // [sp+20h] [bp-4Ch]@21
-  AIDirection v15; // [sp+3Ch] [bp-30h]@21
-  Actor *v16; // [sp+58h] [bp-14h]@2
-  int v17; // [sp+5Ch] [bp-10h]@6
-  stru262_TurnBased *v18; // [sp+60h] [bp-Ch]@1
-  int v19; // [sp+64h] [bp-8h]@8
-  unsigned int v20; // [sp+68h] [bp-4h]@1
-
-  v1 = this;
-  v2 = 0;
-  v18 = v1;
-  v20 = 0;
-  if ( (signed int)uNumActors > 0 )
-  {
-    //v3 = pActors;//[0].pActorBuffs;
-    v16 = pActors.data();//[0].pActorBuffs;
-	v3 = v16->pActorBuffs;
-    do
-    {
-      v4 = v16;
-	  if ( SHIDWORD(v3[3].uExpireTime) < v2 || SHIDWORD(v3[3].uExpireTime) <= v2 && LODWORD(v3[3].uExpireTime) <= v2 )
-        v17 = v2;
-      else
-        v17 = 1;
-      v19 = v2;
-      do
-      {
-        if ( v19 != 10 )
-        {
-          v3->_4585CA(pParty->uTimePlayed);
-          v2 = 0;
-        }
-        ++v19;
-        ++v3;
-      }
-      while ( v19 < 22 );
-      if ( v17 != v2
-        && SHIDWORD(v4->pActorBuffs[3].uExpireTime) <= v2
-        && (SHIDWORD(v4->pActorBuffs[3].uExpireTime) < v2 || LODWORD(v4->pActorBuffs[3].uExpireTime) <= v2) )
-        v4->uActorHeight = pMonsterList->pMonsters[v4->pMonsterInfo.uID - 1].uMonsterHeight;
-      if ( !(v4->uAttributes & 0x80) )
-      {
-        if ( !v4->pActorBuffs[5].uExpireTime )
-        {
-          if ( !v4->pActorBuffs[6].uExpireTime )
-          {
-            v5 = v4->uCurrentActionLength;
-            v4->uCurrentActionTime += pMiscTimer->uTimeElapsed;
-            if ( (signed int)v4->uCurrentActionTime >= v5 )
-            {
-              v17 = ai_near_actors_targets_pid[v20];
-              v6 = Actor::GetDirectionInfo(PID(OBJECT_Actor,v20), v17, &a3, v2);
-              v7 = v4->uAIState;
-              memcpy(&v15, v6, sizeof(v15));
-              v8 = v7 - 4;
-              memcpy(&v14, &v15, sizeof(v14));
-              if ( v8 )
-              {
-                v9 = v8 - 1;
-                if ( v9 )
-                {
-                  v10 = v9 - 6;
-                  if ( v10 )
-                  {
-                    if ( v10 != 8 )
-                      Actor::AI_StandOrBored(v20, v17, 32, &v14);
-                  }
-                }
-              }
-              else
-              {
-                v4->uCurrentActionTime = 0;
-                v4->uCurrentActionLength = 0;
-                v4->uAIState = Dead;
-                v4->UpdateAnimation();
-              }
-              v1 = v18;
-              v2 = 0;
-            }
-          }
-        }
-      }
-      ++v20;
-	  v3 = v16[1].pActorBuffs;
-      ++v16;
-    }
-    while ( (signed int)v20 < (signed int)uNumActors );
-  }
-  if ( v1->field_4 == 1 )
-  {
-    v12 = v1->field_8;
-    if ( v12 == 64 )
-    {
-      v1->_406A63();
-    }
-    else
-    {
-      if ( v12 > v2 )
-      {
-        v1->_406B9F();
-      }
-      else
-      {
-        v1->_406AFE();
-        v1->field_10 = 100;
-      }
-    }
-    v1->field_8 -= pEventTimer->uTimeElapsed;
-    return;
-  }
-  if ( v1->field_4 == 2 )
-  {
-    if ( !(v1->field_18 & 1) )
-    {
-      v11 = v1->field_10;
-      if ( v11 == 100 )
-      {
-        v1->StartTurn();
-LABEL_39:
-        v1->_40652A();
-        return;
-      }
-      if ( v11 > v2 || v1->pQueue[0].field_4 <= v2 )
-      {
-        v1->_4065B0();
-        goto LABEL_39;
-      }
-    }
-    v1->NextTurn();
-    return;
-  }
-  if ( v1->field_4 == 3 )
-  {
-    if ( v1->uActionPointsLeft <= v2 || v1->field_18 & 8 )
-    {
-      v1->field_18 &= 0xFFFFFFF7u;
-      v1->field_4 = 1;
-      v1->field_8 = 64;
-    }
-    else
-    {
-      v1->_406FA8();
-    }
-  }
-}
\ No newline at end of file
--- a/mm7_5.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/mm7_5.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -2427,9 +2427,9 @@
               Actor::StealFrom(uMessageParam);
             continue;
           }
-          if ( pTurnEngine->field_4 == 1 || pTurnEngine->field_4 == 3 )
+          if ( pTurnEngine->turn_stage == 1 || pTurnEngine->turn_stage == 3 )
             continue;
-          if ( !(pTurnEngine->field_18 & 2) )
+          if ( !(pTurnEngine->field_18 & TE_FLAG_2) )
           {
             if ( pActors[uMessageParam].uAIState == 5 )
               stru_50C198.LootActor(&pActors[uMessageParam]);
@@ -2446,9 +2446,9 @@
             _42ECB5_PlayerAttacksActor();
             continue;
           }
-          if ( pTurnEngine->field_4 == 1 || pTurnEngine->field_4 == 3 )
+          if ( pTurnEngine->turn_stage == 1 || pTurnEngine->turn_stage == 3 )
             continue;
-          if ( !(pTurnEngine->field_18 & 2) )
+          if ( !(pTurnEngine->field_18 & TE_FLAG_2) )
             _42ECB5_PlayerAttacksActor();
           continue;
         case UIMSG_ExitRest:
@@ -2720,7 +2720,7 @@
           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 )
+          if ( pTurnEngine->turn_stage == 3 || !uActiveCharacter || uMessageParam == pPlayers[uActiveCharacter]->lastOpenedSpellbookPage )
             continue;
           sub_41140B();
           pPlayers[uActiveCharacter]->lastOpenedSpellbookPage = uMessageParam;
@@ -2730,7 +2730,7 @@
           continue;
         case UIMSG_SelectSpell:
         {
-          if (pTurnEngine->field_4 == 3)
+          if (pTurnEngine->turn_stage == 3)
             continue;
           if (!uActiveCharacter)
             continue;
@@ -2768,17 +2768,17 @@
         continue;
 
         case UIMSG_CastSpellFromBook:
-          if ( pTurnEngine->field_4 != 3 )
+          if ( pTurnEngine->turn_stage != 3 )
             _42777D_CastSpell_UseWand_ShootArrow(uMessageParam, v199, 0, 0, 0);
         continue;
 
         case UIMSG_SpellScrollUse:
 			__debugbreak();
-          if ( pTurnEngine->field_4 != 3 )
+          if ( pTurnEngine->turn_stage != 3 )
             _42777D_CastSpell_UseWand_ShootArrow(uMessageParam, v199, 133, 1, 0);
           continue;
         case UIMSG_SpellBookWindow:
-          if ( pTurnEngine->field_4 == 3 )
+          if ( pTurnEngine->turn_stage == 3 )
             continue;
           if ( bUnderwater == 1 )
           {
@@ -4777,1278 +4777,6 @@
   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)
--- a/mm7_6.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/mm7_6.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -2919,7 +2919,7 @@
               pPartyActionQueue->Add(partyAction);
               break;
             }
-            if (pTurnEngine->field_4 != 1 && pTurnEngine->field_4 != 2 && pTurnEngine->uActionPointsLeft > 0 )
+            if (pTurnEngine->turn_stage != 1 && pTurnEngine->turn_stage != 2 && pTurnEngine->uActionPointsLeft > 0 )
             {
               pTurnEngine->uActionPointsLeft -= 26;
               if ( pParty->uFlags2 & PARTY_FLAGS_2_RUNNING)
@@ -2942,7 +2942,7 @@
               pPartyActionQueue->Add(partyAction);
               break;
             }
-            if ( pTurnEngine->field_4 != 1 && pTurnEngine->field_4 != 2 && pTurnEngine->uActionPointsLeft > 0 )
+            if ( pTurnEngine->turn_stage != 1 && pTurnEngine->turn_stage != 2 && pTurnEngine->uActionPointsLeft > 0 )
             {
               pTurnEngine->uActionPointsLeft -= 26;
               if ( pParty->uFlags2 & 2 )
@@ -2962,7 +2962,7 @@
               pPartyActionQueue->Add(partyAction);
               break;
             }
-            if ( pTurnEngine->field_4 == 1 || pTurnEngine->field_4 == 2 || pTurnEngine->uActionPointsLeft <= 0 )
+            if ( pTurnEngine->turn_stage == 1 || pTurnEngine->turn_stage == 2 || pTurnEngine->uActionPointsLeft <= 0 )
               break;
             pTurnEngine->uActionPointsLeft -= 26;
             partyAction = PARTY_StrafeLeft;
@@ -2977,7 +2977,7 @@
               pPartyActionQueue->Add(partyAction);
               break;
             }
-            if ( pTurnEngine->field_4 == 1 || pTurnEngine->field_4 == 2 || pTurnEngine->uActionPointsLeft <= 0 )
+            if ( pTurnEngine->turn_stage == 1 || pTurnEngine->turn_stage == 2 || pTurnEngine->uActionPointsLeft <= 0 )
               break;
             pTurnEngine->uActionPointsLeft -= 26;
             partyAction = PARTY_StrafeRight;
@@ -2990,7 +2990,7 @@
             {
               if (pParty->bTurnBasedModeOn)
               {
-                if ( pTurnEngine->field_4 == 1 || pTurnEngine->field_4 == 2 || pTurnEngine->uActionPointsLeft <= 0 )
+                if ( pTurnEngine->turn_stage == 1 || pTurnEngine->turn_stage == 2 || pTurnEngine->uActionPointsLeft <= 0 )
                   break;
                 pTurnEngine->uActionPointsLeft -= 26;
               }
@@ -3016,7 +3016,7 @@
             {
               if (pParty->bTurnBasedModeOn)
               {
-                if ( pTurnEngine->field_4 == 1 || pTurnEngine->field_4 == 2 || pTurnEngine->uActionPointsLeft <= 0 )
+                if ( pTurnEngine->turn_stage == 1 || pTurnEngine->turn_stage == 2 || pTurnEngine->uActionPointsLeft <= 0 )
                   break;
                 pTurnEngine->uActionPointsLeft -= 26;
               }
@@ -3051,9 +3051,9 @@
           case INPUT_Pass:
             if ( pCurrentScreen )
               break;
-            if (pParty->bTurnBasedModeOn && pTurnEngine->field_4 == 3)
+            if (pParty->bTurnBasedModeOn && pTurnEngine->turn_stage == 3)
             {
-              pTurnEngine->field_18 |= 8u;
+              pTurnEngine->field_18 |= TE_FLAG_8;
               break;
             }
             if ( uActiveCharacter )
@@ -3073,7 +3073,7 @@
             {
               if (pParty->bTurnBasedModeOn)
               {
-                if (pTurnEngine->field_4 == OBJECT_Actor || PID_TYPE(pTurnEngine->pQueue[0].uPackedID) == OBJECT_Player)
+                if (pTurnEngine->turn_stage == 3 || PID_TYPE(pTurnEngine->pQueue[0].uPackedID) == OBJECT_Player)
                 {
                   pParty->bTurnBasedModeOn = 0;
                   pTurnEngine->End(true);
@@ -3089,9 +3089,9 @@
           case INPUT_CastReady:
             if (pCurrentScreen != SCREEN_GAME)
               break;
-            if (pParty->bTurnBasedModeOn && pTurnEngine->field_4 == 3)
+            if (pParty->bTurnBasedModeOn && pTurnEngine->turn_stage == 3)
             {
-              pTurnEngine->field_18 |= 8u;
+              pTurnEngine->field_18 |= TE_FLAG_8;
               break;
             }
             if ( !uActiveCharacter )
@@ -3118,9 +3118,9 @@
           case INPUT_Attack:
             if (pCurrentScreen != SCREEN_GAME)
               break;
-            if (pParty->bTurnBasedModeOn == 1 && pTurnEngine->field_4 == 3)
+            if (pParty->bTurnBasedModeOn == 1 && pTurnEngine->turn_stage == 3)
             {
-              pTurnEngine->field_18 |= 8u;
+              pTurnEngine->field_18 |= TE_FLAG_8;
               break;
             }
             pMessageQueue_50CBD0->AddMessage(UIMSG_Attack, 0, 0);
--- a/mm7_data.cpp	Fri Jul 12 23:07:14 2013 +0200
+++ b/mm7_data.cpp	Fri Jul 12 23:08:35 2013 +0200
@@ -33,8 +33,7 @@
 #include "MapInfo.h"
 struct MapStats *pMapStats;
 
-#include "TurnEngine.h"
-struct stru262_TurnBased *pTurnEngine = new stru262_TurnBased;
+
 
 #include "CastSpellInfo.h"
 std::array<CastSpellInfo, 10> pCastSpellInfo;
@@ -968,25 +967,25 @@
 int dword_507CD8; // weak
 std::array<int, 50> dword_50B570; // weak
 std::array<int, 50> dword_50B638; // weak
-stru367 stru_50B700;
-/*int stru_50B700.field_0; // weak
-int stru_50B700.field_38[777]; // idb
-int stru_50B700._view_transformed_ys[45];
-int stru_50B700.field_128[777];
-int stru_50B700._view_transformed_zs[45];
-int stru_50B700.field_218[777];
-int stru_50B700._view_transformed_xs[45];
-int stru_50B700._screen_space_y[777];
-int stru_50B700.field_3E4[777];
-int stru_50B700.field_2F0[2]; // idb
-int stru_50B700._ys[3 + 45];
-int stru_50B700._ys2[48]; // idb
-int stru_50B700._screen_space_x[777]; // idb
-int stru_50B700.field_3D4[777]; // weak
-int stru_50B700._xs[777]; // weak
+stru367 PortalFace;
+/*int PortalFace.field_0; // weak
+int PortalFace.field_38[777]; // idb
+int PortalFace._view_transformed_ys[45];
+int PortalFace.field_128[777];
+int PortalFace._view_transformed_zs[45];
+int PortalFace.field_218[777];
+int PortalFace._view_transformed_xs[45];
+int PortalFace._screen_space_y[777];
+int PortalFace.field_3E4[777];
+int PortalFace.field_2F0[2]; // idb
+int PortalFace._ys[3 + 45];
+int PortalFace._ys2[48]; // idb
+int PortalFace._screen_space_x[777]; // idb
+int PortalFace.field_3D4[777]; // weak
+int PortalFace._xs[777]; // weak
 int dword_50BAE8[777]; // weak
-int stru_50B700._xs2[3 + 45]; // weak
-int stru_50B700._xs3[48]; // weak*/
+int PortalFace._xs2[3 + 45]; // weak
+int PortalFace._xs3[48]; // weak*/
 std::array<int, 100> dword_50BC10; // weak
 std::array<int, 100> dword_50BDA0; // weak
 std::array<int, 100> dword_50BF30; // weak
--- a/mm7_data.h	Fri Jul 12 23:07:14 2013 +0200
+++ b/mm7_data.h	Fri Jul 12 23:08:35 2013 +0200
@@ -610,25 +610,25 @@
 extern int dword_507CD8; // weak
 extern std::array<int, 50> dword_50B570; // weak
 extern std::array<int, 50> dword_50B638; // weak
-extern struct stru367 stru_50B700;
-/*extern int stru_50B700.field_0; // weak
-extern int stru_50B700.field_38[]; // idb
-extern int stru_50B700._view_transformed_ys[];
-extern int stru_50B700.field_128[];
-extern int stru_50B700._view_transformed_zs[];
-extern int stru_50B700.field_218[];
-extern int stru_50B700._view_transformed_xs[];
-extern int stru_50B700._screen_space_y[];
-extern int stru_50B700.field_3E4[];
-extern int stru_50B700.field_2F0[]; // idb
-extern int stru_50B700._ys[];
-extern int stru_50B700._ys2[]; // idb
-extern int stru_50B700._screen_space_x[]; // idb
-extern int stru_50B700.field_3D4[]; // weak
-extern int stru_50B700._xs[]; // weak
+extern struct stru367 PortalFace;
+/*extern int PortalFace.field_0; // weak
+extern int PortalFace.field_38[]; // idb
+extern int PortalFace._view_transformed_ys[];
+extern int PortalFace.field_128[];
+extern int PortalFace._view_transformed_zs[];
+extern int PortalFace.field_218[];
+extern int PortalFace._view_transformed_xs[];
+extern int PortalFace._screen_space_y[];
+extern int PortalFace.field_3E4[];
+extern int PortalFace.field_2F0[]; // idb
+extern int PortalFace._ys[];
+extern int PortalFace._ys2[]; // idb
+extern int PortalFace._screen_space_x[]; // idb
+extern int PortalFace.field_3D4[]; // weak
+extern int PortalFace._xs[]; // weak
 extern int dword_50BAE8[]; // weak
-extern int stru_50B700._xs2[]; // weak
-extern int stru_50B700._xs3[]; // weak*/
+extern int PortalFace._xs2[]; // weak
+extern int PortalFace._xs3[]; // weak*/
 extern std::array<int, 100> dword_50BC10; // weak
 extern std::array<int, 100> dword_50BDA0; // weak
 extern std::array<int, 100> dword_50BF30; // weak
@@ -1149,7 +1149,7 @@
 void __cdecl reset_some_strus_flt_2Cs();
 void __cdecl j_sub_423B4A();
 void __cdecl sub_423B4A();
-int __fastcall sub_423B5D(unsigned int uFaceID);
+int __fastcall GetPortalScreenCoord(unsigned int uFaceID);
 signed int __fastcall sub_424579(int uFaceID, struct stru320 *a2);
 bool sub_424829(int pNumVertices, struct BspRenderer_stru2 *a2, struct BspRenderer_stru2 *a3, int uFaceID);
 signed int __fastcall sr_424CD7(unsigned int uVertexID); // idb
--- a/stru367.h	Fri Jul 12 23:07:14 2013 +0200
+++ b/stru367.h	Fri Jul 12 23:08:35 2013 +0200
@@ -6,11 +6,11 @@
 #pragma pack(push, 1)
 struct stru367
 {
-	int field_0;
+	bool direction;
 	std::array<int, 13> field_4;
-	std::array<int, 60> _view_transformed_ys;
-	std::array<int, 60> _view_transformed_zs;
-	std::array<int, 48> _view_transformed_xs;
+	std::array<int, 60> _view_transformed_x;
+	std::array<int, 60> _view_transformed_y;
+	std::array<int, 48> _view_transformed_z;
 	std::array<int, 60> _screen_space_y;
 	std::array<int, 60> _screen_space_x;
 };