VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "cGAMLayer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private TileWidth        As Integer
Private TileHeight       As Integer
Private m_MapCells()     As tMapCell
Private MyMap            As cGAMMap
Private s2dtp            As cS2DTexturePool_i
Private CFm_MyLevel      As cGAMLevel
Private CFm_Alpha        As Integer
Private CFm_Additive     As Boolean
Private CFm_Color        As Long
Private CFm_Desc         As String

Public Visible As Boolean

Private AnimationBuffer As Integer
Private AnimationBuffer2 As Integer
Private AnimationCounter As Integer
Private AnimationDirection As Integer

Public m_TileSetID As Long
Public Redraw As Boolean
Dim BufID As Integer

Public Property Get TileSetID() As Long
    TileSetID = m_TileSetID
End Property

Public Property Let TileSetID(vNew As Long)
    m_TileSetID = vNew
End Property

Public Property Get MyLevel() As cGAMLevel
     Set MyLevel = CFm_MyLevel
End Property

Public Property Set MyLevel(PropVal As cGAMLevel)
    Set CFm_MyLevel = PropVal
End Property

Public Property Get Alpha() As Integer
    Alpha = CFm_Alpha
End Property

Public Property Let Alpha(ByVal PropVal As Integer)
    CFm_Alpha = PropVal
    If CFm_Alpha <> PropVal Then Stop
End Property

Public Property Get Additive() As Boolean
    Additive = CFm_Additive
End Property

Public Property Let Additive(ByVal PropVal As Boolean)
    CFm_Additive = PropVal
End Property

Public Property Get Color() As Long
    Color = CFm_Color
End Property

Public Property Let Color(ByVal PropVal As Long)
    CFm_Color = PropVal
End Property

Public Property Get Desc() As String
    Desc = CFm_Desc
End Property

Public Property Let Desc(ByVal PropVal As String)
    CFm_Desc = PropVal
End Property

Public Function AdditionalInfo(toX As Integer, toY As Integer)
    On Error Resume Next

    m_MapCells(toX, toY).AdditionalID = 0
    m_MapCells(toX, toY).Additional = 0

    Dim x As Integer, y As Integer

    For x = toX - 1 To toX + 1
        For y = toY - 1 To toY + 1
            m_MapCells(x, y).RM2KF = False
        Next y
    Next x

    If m_MapCells(toX, toY).DrawTile = False Then Exit Function
    Dim i As Integer
    i = GetPath(toX, toY, 0)

    If i > 0 Then
        m_MapCells(toX, toY).AdditionalID = i
        m_MapCells(toX, toY).Additional = 1: Exit Function
    End If

    i = GetAnimationPath(toX, toY, 0)
    If i > 0 Then
        m_MapCells(toX, toY).AdditionalID = i
        m_MapCells(toX, toY).Additional = 2: Exit Function
    End If

    i = GetAnimation(toX, toY)
    If i > 0 Then
        m_MapCells(toX, toY).AdditionalID = i
        m_MapCells(toX, toY).Additional = 3: Exit Function
    End If

    i = GetAnimationPathXP(toX, toY, 0)
    If i > 0 Then
        m_MapCells(toX, toY).AdditionalID = i
        m_MapCells(toX, toY).Additional = 4: Exit Function
    End If
End Function

Private Sub DrawRM2KAnimation(ByVal TexID As Integer, x As Long, y As Long, ByVal Animation As Integer, ByVal CFm_Alpha As Integer, ByVal CFm_Additive As Boolean, ByVal CFm_Color As Long, ByVal XTo As Double, ByVal YTo As Double)
    s2dtp.RenderTexture TexID, XTo, YTo, Const_TileWidth, Const_TileHeight, Const_TileWidth, Const_TileHeight, CInt(RM2K_Animations(Animation).x), RM2K_Animations(Animation).y + AnimationBuffer * Const_TileHeight, CFm_Alpha
End Sub

Private Sub CheckPos2(ByVal TexID As Integer, x As Long, y As Long, ByVal Path As Integer, ByVal CFm_Alpha As Integer, ByVal CFm_Additive As Boolean, ByVal CFm_Color As Long, ByVal XTo As Double, ByVal YTo As Double)
    On Error Resume Next
    
    If m_MapCells(x, y).RM2KF Then GoTo RenderNow
    m_MapCells(x, y).RM2KF = True
    
    Dim TX       As Integer
    Dim Ty       As Integer
    Dim X_offset As Integer
    Dim Y_offset As Integer
    Dim add_2_Y_offset As Integer
    Dim add_2_X_offset As Integer
    Dim std_tilex As Integer
    Dim std_tiley As Integer
    Dim tiles_xoffset As Integer
    Dim tOL      As RECT
    Dim tOR      As RECT
    Dim tUL      As RECT
    Dim tUR      As RECT

    ReDim Temp(0 To 2, 0 To 2)
    If Path = 0 Then
        Exit Sub
    End If
    Temp(0, 0) = GetAnimationPath(x - 1, y - 1, Path)
    Temp(0, 1) = GetAnimationPath(x - 1, CInt(y), Path)
    Temp(0, 2) = GetAnimationPath(x - 1, y + 1, Path)
    Temp(1, 2) = GetAnimationPath(CInt(x), y + 1, Path)
    Temp(2, 1) = GetAnimationPath(x + 1, CInt(y), Path)
    Temp(1, 0) = GetAnimationPath(CInt(x), y - 1, Path)
    Temp(2, 0) = GetAnimationPath(x + 1, y - 1, Path)
    Temp(1, 1) = GetAnimationPath(CInt(x), CInt(y), Path)
    Temp(2, 2) = GetAnimationPath(x + 1, y + 1, Path)

    Dim i As Integer
    For TX = 0 To 2
        For Ty = 0 To 2
            
            'Temp(TX, Ty) = 0
            
            For i = 1 To RM2K_AnimatedPath_Count
            If Temp(TX, Ty) = i Then Temp(TX, Ty) = Path
            Next i
        
            
        Next Ty
    Next TX

      
      'X_offset = AnimationBuffer2
      Y_offset = 0
      add_2_Y_offset = 0
      'std_tilex = AnimationBuffer2
      std_tiley = 0

   If RM2K_AnimatedPaths(Path).x = 0 And RM2K_AnimatedPaths(Path).y = 0 Then
       std_tiley = 4
       tiles_xoffset = 0
   End If

   If RM2K_AnimatedPaths(Path).x = 0 And RM2K_AnimatedPaths(Path).y = 6 * TileHeight Then
       std_tiley = 1
       add_2_Y_offset = -6
   End If

   If RM2K_AnimatedPaths(Path).x = 3 * TileWidth And RM2K_AnimatedPaths(Path).y = 0 Then
       'std_tilex = std_tilex - 3
       add_2_X_offset = 3
       std_tiley = 4
   End If


        X_offset = std_tilex
        If Temp(0, 1) + Temp(1, 0) = 0 Then ' ol
            Y_offset = 0
        
        ElseIf Not Temp(0, 1) + Temp(1, 0) = Path And Temp(0, 0) = 0 Then
            Y_offset = 3
        
        ElseIf Temp(1, 0) = 0 Then  ' oben
            Y_offset = 2
        
        ElseIf Temp(0, 1) = 0 Then ' links
            Y_offset = 1
        
        Else
        
          Y_offset = std_tiley - add_2_Y_offset
          X_offset = std_tilex - add_2_X_offset
          
        End If
        
          Y_offset = Y_offset + add_2_Y_offset
          
        tOL.Left = RM2K_AnimatedPaths(Path).x + X_offset * TileWidth
        tOL.Top = RM2K_AnimatedPaths(Path).y + Y_offset * TileHeight
        
        X_offset = std_tilex
        
        If Temp(1, 0) + Temp(2, 1) = 0 Then ' or
           Y_offset = 0
        
        ElseIf Not Temp(1, 0) + Temp(2, 1) = Path And Temp(2, 0) = 0 Then
           Y_offset = 3
        
        ElseIf Temp(1, 0) = 0 Then ' oben
           Y_offset = 2
        
        ElseIf Temp(2, 1) = 0 Then 'rechts
           Y_offset = 1
        
        Else
        
          Y_offset = std_tiley - add_2_Y_offset
          X_offset = std_tilex - add_2_X_offset
          
        End If

          Y_offset = Y_offset + add_2_Y_offset
          
        tOR.Left = RM2K_AnimatedPaths(Path).x + X_offset * TileWidth + TileWidth / 2
        tOR.Top = RM2K_AnimatedPaths(Path).y + Y_offset * TileHeight


        X_offset = std_tilex
        If Temp(0, 1) + Temp(1, 2) = 0 Then 'ul
          Y_offset = 0
        
        ElseIf Not Temp(0, 1) + Temp(1, 2) = Path And Temp(0, 2) = 0 Then
          Y_offset = 3
        
        ElseIf Temp(0, 1) = 0 Then ' links
          Y_offset = 1

        ElseIf Temp(1, 2) = 0 Then 'unten
          Y_offset = 2
        
        Else
        
          Y_offset = std_tiley - add_2_Y_offset
          X_offset = std_tilex - add_2_X_offset
          
        End If

        Y_offset = Y_offset + add_2_Y_offset
        
        
        tUL.Left = RM2K_AnimatedPaths(Path).x + X_offset * TileWidth
        tUL.Top = RM2K_AnimatedPaths(Path).y + Y_offset * TileHeight + TileHeight / 2

        X_offset = std_tilex
        
        If Temp(2, 1) + Temp(1, 2) = 0 Then ' ur
          Y_offset = 0
        
        ElseIf Not Temp(2, 1) + Temp(1, 2) = Path And Temp(2, 2) = 0 Then
          Y_offset = 3
        
        ElseIf Temp(2, 1) = 0 Then ' rechts
          Y_offset = 1
        
        ElseIf Temp(1, 2) = 0 Then ' unten
          Y_offset = 2
    
        Else
        
          Y_offset = std_tiley - add_2_Y_offset
          X_offset = std_tilex - add_2_X_offset
          
        End If

        Y_offset = Y_offset + add_2_Y_offset
        
        tUR.Left = RM2K_AnimatedPaths(Path).x + X_offset * TileWidth + TileWidth / 2
        tUR.Top = RM2K_AnimatedPaths(Path).y + Y_offset * TileHeight + TileHeight / 2

      m_MapCells(x, y).RM2KP(1).x = tOL.Left
      m_MapCells(x, y).RM2KP(1).y = tOL.Top
      m_MapCells(x, y).RM2KP(2).x = tOR.Left
      m_MapCells(x, y).RM2KP(2).y = tOR.Top
      m_MapCells(x, y).RM2KP(3).x = tUR.Left
      m_MapCells(x, y).RM2KP(3).y = tUR.Top
      m_MapCells(x, y).RM2KP(4).x = tUL.Left
      m_MapCells(x, y).RM2KP(4).y = tUL.Top
      
RenderNow:
        If m_MapCells(x, y).RM2KP(1).x = RM2K_AnimatedPaths(1).x And _
           m_MapCells(x, y).RM2KP(1).y = RM2K_AnimatedPaths(1).y + 4 * Const_TileHeight And _
           m_MapCells(x, y).RM2KP(3).y = RM2K_AnimatedPaths(1).y + 4 * Const_TileHeight + 8 Then
           
           If Path = 1 Or Path = 2 Then
             s2dtp.RenderTexture TexID, XTo, YTo, TileWidth, TileHeight, TileWidth, TileHeight, RM2K_AnimatedPaths(1).x + (AnimationBuffer2 * TileWidth), RM2K_AnimatedPaths(1).y + 4 * Const_TileHeight, CFm_Alpha, , , , , CFm_Color
           Else
             s2dtp.RenderTexture TexID, XTo, YTo, TileWidth, TileHeight, TileWidth, TileHeight, RM2K_AnimatedPaths(1).x + (AnimationBuffer2 * TileWidth), RM2K_AnimatedPaths(1).y + 4 * Const_TileHeight, CFm_Alpha, , , , , CFm_Color
           End If
             
        Else

        With s2dtp
            .RenderTexture TexID, XTo, YTo, TileWidth / 2, TileHeight / 2, TileWidth / 2, TileHeight / 2, m_MapCells(x, y).RM2KP(1).x + (AnimationBuffer2 * TileWidth), CInt(m_MapCells(x, y).RM2KP(1).y), CFm_Alpha, , , , , CFm_Color
            .RenderTexture TexID, XTo + TileWidth / 2, YTo, TileWidth / 2, TileHeight / 2, TileWidth / 2, TileHeight / 2, m_MapCells(x, y).RM2KP(2).x + (AnimationBuffer2 * TileWidth), CInt(m_MapCells(x, y).RM2KP(2).y), CFm_Alpha, , , , , CFm_Color
            .RenderTexture TexID, XTo + TileWidth / 2, YTo + TileHeight / 2, TileWidth / 2, TileHeight / 2, TileWidth / 2, TileHeight / 2, m_MapCells(x, y).RM2KP(3).x + (AnimationBuffer2 * TileWidth), CInt(m_MapCells(x, y).RM2KP(3).y), CFm_Alpha, , , , , CFm_Color
            .RenderTexture TexID, XTo, YTo + TileHeight / 2, TileWidth / 2, TileHeight / 2, TileWidth / 2, TileHeight / 2, m_MapCells(x, y).RM2KP(4).x + (AnimationBuffer2 * TileWidth), CInt(m_MapCells(x, y).RM2KP(4).y), CFm_Alpha, , , , , CFm_Color
        End With 's2dtp
    End If
End Sub

Private Sub CheckPos(ByVal TexID As Integer, x As Long, y As Long, ByVal Path As Integer, ByVal CFm_Alpha As Integer, ByVal CFm_Additive As Boolean, ByVal CFm_Color As Long, ByVal XTo As Double, ByVal YTo As Double)
    If m_MapCells(x, y).RM2KF Then GoTo RenderNow
    m_MapCells(x, y).RM2KF = True

    Dim TX       As Integer
    Dim Ty       As Integer
    Dim X_offset As Integer
    Dim Y_offset As Integer
    Dim tOL      As RECT
    Dim tOR      As RECT
    Dim tUL      As RECT
    Dim tUR      As RECT
    
    ReDim Temp(0 To 2, 0 To 2)
    If Path = 0 Then
        Exit Sub
    End If
    
    Temp(0, 0) = GetPath(x - 1, y - 1, Path)
    Temp(0, 1) = GetPath(x - 1, CInt(y), Path)
    Temp(0, 2) = GetPath(x - 1, y + 1, Path)
    Temp(1, 2) = GetPath(CInt(x), y + 1, Path)
    Temp(2, 1) = GetPath(x + 1, CInt(y), Path)
    Temp(1, 0) = GetPath(CInt(x), y - 1, Path)
    Temp(2, 0) = GetPath(x + 1, y - 1, Path)
    Temp(1, 1) = GetPath(CInt(x), CInt(y), Path)
    Temp(2, 2) = GetPath(x + 1, y + 1, Path)
    'Debug.Print Temp(0, 0) & Temp(1, 0) & Temp(2, 0)
    'Debug.Print Temp(0, 1) & Temp(1, 1) & Temp(2, 1)
    'Debug.Print Temp(0, 2) & Temp(1, 2) & Temp(2, 2)
    For TX = 0 To 2
        For Ty = 0 To 2
            If Not Temp(TX, Ty) = Path Then
                Temp(TX, Ty) = 0
            End If
        Next Ty
    Next TX
Nochma:
    If Temp(1, 1) = Path Then
        X_offset = 1
        Y_offset = 2
        If Temp(0, 1) + Temp(1, 0) = 0 Then ' ol
            X_offset = 0
            Y_offset = 1
         ElseIf Not Temp(0, 1) + Temp(1, 0) = Path And Temp(0, 0) = 0 Then 'NOT TEMP(0,...
            X_offset = 2
            Y_offset = 0
         ElseIf Temp(0, 1) + Temp(1, 2) = 0 Then ' ul'NOT NOT...
            X_offset = 0
            Y_offset = 3
         ElseIf Temp(1, 0) + Temp(2, 1) = 0 Then ' or'NOT TEMP(0,...
            X_offset = 2
            Y_offset = 1
         ElseIf Temp(2, 1) + Temp(1, 2) = 0 Then ' ur'NOT TEMP(1,...
            X_offset = 2
            Y_offset = 3
         ElseIf Temp(1, 0) = 0 Then ' oben'NOT TEMP(2,...
            X_offset = 1
            Y_offset = 1
         ElseIf Temp(0, 1) = 0 Then ' links'NOT TEMP(1,...
            X_offset = 0
            Y_offset = 2
         ElseIf Temp(2, 1) = 0 Then ' rechts'NOT TEMP(0,...
            X_offset = 2
            Y_offset = 2
         ElseIf Temp(1, 2) = 0 Then ' unten'NOT TEMP(2,...
            X_offset = 1
            Y_offset = 3
        End If
        tOL.Left = RM2K_Paths(Path).x + X_offset * TileWidth
        tOL.Top = RM2K_Paths(Path).y + Y_offset * TileHeight
        X_offset = 1
        Y_offset = 2
        If Temp(1, 0) + Temp(2, 1) = 0 Then ' or
            X_offset = 2
            Y_offset = 1
         ElseIf Not Temp(1, 0) + Temp(2, 1) = Path And Temp(2, 0) = 0 Then 'NOT TEMP(1,...
            X_offset = 2
            Y_offset = 0
         ElseIf Temp(2, 1) + Temp(1, 2) = 0 Then ' ur'NOT NOT...
            X_offset = 2
            Y_offset = 3
         ElseIf Temp(0, 1) + Temp(1, 0) = 0 Then ' ol'NOT TEMP(2,...
            X_offset = 0
            Y_offset = 1
         ElseIf Temp(0, 1) + Temp(1, 2) = 0 Then ' ul'NOT TEMP(0,...
            X_offset = 0
            Y_offset = 3
         ElseIf Temp(1, 0) = 0 Then ' oben'NOT TEMP(0,...
            X_offset = 1
            Y_offset = 1
         ElseIf Temp(2, 1) = 0 Then ' rechts'NOT TEMP(1,...
            X_offset = 2
            Y_offset = 2
         ElseIf Temp(1, 2) = 0 Then ' unten'NOT TEMP(2,...
            X_offset = 1
            Y_offset = 3
         ElseIf Temp(0, 1) = 0 Then ' links'NOT TEMP(1,...
            X_offset = 0
            Y_offset = 2
        End If
        tOR.Left = RM2K_Paths(Path).x + X_offset * TileWidth + TileWidth / 2
        tOR.Top = RM2K_Paths(Path).y + Y_offset * TileHeight
        X_offset = 1
        Y_offset = 2
        If Temp(0, 1) + Temp(1, 2) = 0 Then ' ul
            X_offset = 0
            Y_offset = 3
         ElseIf Not Temp(0, 1) + Temp(1, 2) = Path And Temp(0, 2) = 0 Then 'NOT TEMP(0,...
            X_offset = 2
            Y_offset = 0
         ElseIf Temp(0, 1) + Temp(1, 0) = 0 Then ' ol'NOT NOT...
            X_offset = 0
            Y_offset = 1
         ElseIf Temp(2, 1) + Temp(1, 2) = 0 Then ' ur'NOT TEMP(0,...
            X_offset = 2
            Y_offset = 3
         ElseIf Temp(1, 0) + Temp(2, 1) = 0 Then ' or     2'NOT TEMP(2,...
            X_offset = 2
            Y_offset = 1
         ElseIf Temp(1, 2) = 0 Then  ' unten'NOT TEMP(1,...
            X_offset = 1
            Y_offset = 3
         ElseIf Temp(0, 1) = 0 Then ' links'NOT TEMP(1,...
            X_offset = 0
            Y_offset = 2
         ElseIf Temp(1, 0) = 0 Then ' oben'NOT TEMP(0,...
            X_offset = 1
            Y_offset = 1
         ElseIf Temp(2, 1) = 0 Then ' rechts'NOT TEMP(1,...
            X_offset = 2
            Y_offset = 2
        End If
        tUL.Left = RM2K_Paths(Path).x + X_offset * TileWidth
        tUL.Top = RM2K_Paths(Path).y + Y_offset * TileHeight + TileHeight / 2
        X_offset = 1
        Y_offset = 2
        If Temp(2, 1) + Temp(1, 2) = 0 Then ' ur
            X_offset = 2
            Y_offset = 3
         ElseIf Not Temp(2, 1) + Temp(1, 2) = Path And Temp(2, 2) = 0 Then 'NOT TEMP(2,...
            X_offset = 2
            Y_offset = 0
         ElseIf Temp(0, 1) + Temp(1, 2) = 0 Then ' ul'NOT NOT...
            X_offset = 0
            Y_offset = 3
         ElseIf Temp(1, 0) + Temp(2, 1) = 0 Then ' or'NOT TEMP(0,...
            X_offset = 2
            Y_offset = 1
         ElseIf Temp(0, 1) + Temp(1, 0) = 0 Then ' ol'NOT TEMP(1,...
            X_offset = 0
            Y_offset = 1
         ElseIf Temp(1, 2) = 0 Then ' unten'NOT TEMP(0,...
            X_offset = 1
            Y_offset = 3
         ElseIf Temp(2, 1) = 0 Then ' rechts'NOT TEMP(1,...
            X_offset = 2
            Y_offset = 2
         ElseIf Temp(1, 0) = 0 Then ' oben'NOT TEMP(2,...
            X_offset = 1
            Y_offset = 1
         ElseIf Temp(0, 1) = 0 Then ' links'NOT TEMP(1,...
            X_offset = 0
            Y_offset = 2
        End If
        tUR.Left = RM2K_Paths(Path).x + X_offset * TileWidth + TileWidth / 2
        tUR.Top = RM2K_Paths(Path).y + Y_offset * TileHeight + TileHeight / 2
        
        m_MapCells(x, y).RM2KP(1).x = tOL.Left
        m_MapCells(x, y).RM2KP(1).y = tOL.Top
        m_MapCells(x, y).RM2KP(2).x = tOR.Left
        m_MapCells(x, y).RM2KP(2).y = tOR.Top
        m_MapCells(x, y).RM2KP(3).x = tUR.Left
        m_MapCells(x, y).RM2KP(3).y = tUR.Top
        m_MapCells(x, y).RM2KP(4).x = tUL.Left
        m_MapCells(x, y).RM2KP(4).y = tUL.Top
      
RenderNow:
        If m_MapCells(x, y).RM2KP(1).x = RM2K_Paths(Path).x + Const_TileWidth And m_MapCells(x, y).RM2KP(1).y = RM2K_Paths(Path).y + Const_TileHeight * 2 Then
            If m_MapCells(x, y).RM2KP(2).x = RM2K_Paths(Path).x + Const_TileWidth * 1.5 And m_MapCells(x, y).RM2KP(2).y = RM2K_Paths(Path).y + Const_TileHeight * 2 Then
                If m_MapCells(x, y).RM2KP(3).x = RM2K_Paths(Path).x + Const_TileWidth * 1.5 And m_MapCells(x, y).RM2KP(3).y = RM2K_Paths(Path).y + Const_TileHeight * 2.5 Then
                    If m_MapCells(x, y).RM2KP(4).x = RM2K_Paths(Path).x + Const_TileWidth And m_MapCells(x, y).RM2KP(4).y = RM2K_Paths(Path).y + Const_TileHeight * 2.5 Then
                        s2dtp.RenderTexture TexID, XTo, YTo, TileWidth, TileHeight, TileWidth, TileHeight, RM2K_Paths(Path).x + Const_TileWidth, RM2K_Paths(Path).y + Const_TileHeight * 2, CFm_Alpha, , , , , CFm_Color
                        Exit Sub
                    End If
                End If
            End If
        End If
        
        With s2dtp
            .RenderTexture TexID, XTo, YTo, TileWidth / 2, TileHeight / 2, TileWidth / 2, TileHeight / 2, CInt(m_MapCells(x, y).RM2KP(1).x), CInt(m_MapCells(x, y).RM2KP(1).y), CFm_Alpha, , , , , CFm_Color
            .RenderTexture TexID, XTo + TileWidth / 2, YTo, TileWidth / 2, TileHeight / 2, TileWidth / 2, TileHeight / 2, CInt(m_MapCells(x, y).RM2KP(2).x), CInt(m_MapCells(x, y).RM2KP(2).y), CFm_Alpha, , , , , CFm_Color
            .RenderTexture TexID, XTo + TileWidth / 2, YTo + TileHeight / 2, TileWidth / 2, TileHeight / 2, TileWidth / 2, TileHeight / 2, CInt(m_MapCells(x, y).RM2KP(3).x), CInt(m_MapCells(x, y).RM2KP(3).y), CFm_Alpha, , , , , CFm_Color
            .RenderTexture TexID, XTo, YTo + TileHeight / 2, TileWidth / 2, TileHeight / 2, TileWidth / 2, TileHeight / 2, CInt(m_MapCells(x, y).RM2KP(4).x), CInt(m_MapCells(x, y).RM2KP(4).y), CFm_Alpha, , , , , CFm_Color
        End With 's2dtp
    End If
End Sub

Private Sub Class_Initialize()
    Visible = True
    CFm_Alpha = 255
End Sub

Private Sub CheckPos3(ByVal TexID As Integer, x As Long, y As Long, ByVal Path As Integer, ByVal CFm_Alpha As Integer, ByVal CFm_Additive As Boolean, ByVal CFm_Color As Long, ByVal XTo As Double, ByVal YTo As Double)
    If m_MapCells(x, y).RM2KF Then GoTo RenderNow
    m_MapCells(x, y).RM2KF = True
    
    Dim TX       As Integer
    Dim Ty       As Integer
    Dim X_offset As Integer
    Dim Y_offset As Integer
    Dim tOL      As RECT
    Dim tOR      As RECT
    Dim tUL      As RECT
    Dim tUR      As RECT

    ReDim Temp(0 To 2, 0 To 2)
    If Path = 0 Then Exit Sub
    
    Temp(0, 0) = GetAnimationPathXP(x - 1, y - 1, Path)
    Temp(0, 1) = GetAnimationPathXP(x - 1, CInt(y), Path)
    Temp(0, 2) = GetAnimationPathXP(x - 1, y + 1, Path)
    Temp(1, 2) = GetAnimationPathXP(CInt(x), y + 1, Path)
    Temp(2, 1) = GetAnimationPathXP(x + 1, CInt(y), Path)
    Temp(1, 0) = GetAnimationPathXP(CInt(x), y - 1, Path)
    Temp(2, 0) = GetAnimationPathXP(x + 1, y - 1, Path)
    Temp(1, 1) = GetAnimationPathXP(CInt(x), CInt(y), Path)
    Temp(2, 2) = GetAnimationPathXP(x + 1, y + 1, Path)
    'Debug.Print Temp(0, 0) & Temp(1, 0) & Temp(2, 0)
    'Debug.Print Temp(0, 1) & Temp(1, 1) & Temp(2, 1)
    'Debug.Print Temp(0, 2) & Temp(1, 2) & Temp(2, 2)
    For TX = 0 To 2
        For Ty = 0 To 2
            If Not Temp(TX, Ty) = Path Then
                Temp(TX, Ty) = 0
            End If
        Next Ty
    Next TX
    
Nochma:
    If Temp(1, 1) = Path Then
        X_offset = 1
        Y_offset = 2
        If Temp(0, 1) + Temp(1, 0) = 0 Then ' ol
            X_offset = 0
            Y_offset = 1
         ElseIf Not Temp(0, 1) + Temp(1, 0) = Path And Temp(0, 0) = 0 Then 'NOT TEMP(0,...
            X_offset = 2
            Y_offset = 0
         ElseIf Temp(0, 1) + Temp(1, 2) = 0 Then ' ul'NOT NOT...
            X_offset = 0
            Y_offset = 3
         ElseIf Temp(1, 0) + Temp(2, 1) = 0 Then ' or'NOT TEMP(0,...
            X_offset = 2
            Y_offset = 1
         ElseIf Temp(2, 1) + Temp(1, 2) = 0 Then ' ur'NOT TEMP(1,...
            X_offset = 2
            Y_offset = 3
         ElseIf Temp(1, 0) = 0 Then ' oben'NOT TEMP(2,...
            X_offset = 1
            Y_offset = 1
         ElseIf Temp(0, 1) = 0 Then ' links'NOT TEMP(1,...
            X_offset = 0
            Y_offset = 2
         ElseIf Temp(2, 1) = 0 Then ' rechts'NOT TEMP(0,...
            X_offset = 2
            Y_offset = 2
         ElseIf Temp(1, 2) = 0 Then ' unten'NOT TEMP(2,...
            X_offset = 1
            Y_offset = 3
        End If
        tOL.Left = RMXP_AnimatedPaths(Path).x + X_offset * TileWidth
        tOL.Top = RMXP_AnimatedPaths(Path).y + Y_offset * TileHeight
        X_offset = 1
        Y_offset = 2
        If Temp(1, 0) + Temp(2, 1) = 0 Then ' or
            X_offset = 2
            Y_offset = 1
         ElseIf Not Temp(1, 0) + Temp(2, 1) = Path And Temp(2, 0) = 0 Then 'NOT TEMP(1,...
            X_offset = 2
            Y_offset = 0
         ElseIf Temp(2, 1) + Temp(1, 2) = 0 Then ' ur'NOT NOT...
            X_offset = 2
            Y_offset = 3
         ElseIf Temp(0, 1) + Temp(1, 0) = 0 Then ' ol'NOT TEMP(2,...
            X_offset = 0
            Y_offset = 1
         ElseIf Temp(0, 1) + Temp(1, 2) = 0 Then ' ul'NOT TEMP(0,...
            X_offset = 0
            Y_offset = 3
         ElseIf Temp(1, 0) = 0 Then ' oben'NOT TEMP(0,...
            X_offset = 1
            Y_offset = 1
         ElseIf Temp(2, 1) = 0 Then ' rechts'NOT TEMP(1,...
            X_offset = 2
            Y_offset = 2
         ElseIf Temp(1, 2) = 0 Then ' unten'NOT TEMP(2,...
            X_offset = 1
            Y_offset = 3
         ElseIf Temp(0, 1) = 0 Then ' links'NOT TEMP(1,...
            X_offset = 0
            Y_offset = 2
        End If
        tOR.Left = RMXP_AnimatedPaths(Path).x + X_offset * TileWidth + TileWidth / 2
        tOR.Top = RMXP_AnimatedPaths(Path).y + Y_offset * TileHeight
        X_offset = 1
        Y_offset = 2
        If Temp(0, 1) + Temp(1, 2) = 0 Then ' ul
            X_offset = 0
            Y_offset = 3
        ElseIf Not Temp(0, 1) + Temp(1, 2) = Path And Temp(0, 2) = 0 Then 'NOT TEMP(0,...
            X_offset = 2
            Y_offset = 0
        ElseIf Temp(0, 1) + Temp(1, 0) = 0 Then ' ol'NOT NOT...
            X_offset = 0
            Y_offset = 1
        ElseIf Temp(2, 1) + Temp(1, 2) = 0 Then ' ur'NOT TEMP(0,...
            X_offset = 2
            Y_offset = 3
        ElseIf Temp(1, 0) + Temp(2, 1) = 0 Then ' or     2'NOT TEMP(2,...
            X_offset = 2
            Y_offset = 1
        ElseIf Temp(1, 2) = 0 Then  ' unten'NOT TEMP(1,...
            X_offset = 1
            Y_offset = 3
        ElseIf Temp(0, 1) = 0 Then ' links'NOT TEMP(1,...
            X_offset = 0
            Y_offset = 2
        ElseIf Temp(1, 0) = 0 Then ' oben'NOT TEMP(0,...
            X_offset = 1
            Y_offset = 1
        ElseIf Temp(2, 1) = 0 Then ' rechts'NOT TEMP(1,...
            X_offset = 2
            Y_offset = 2
        End If
        tUL.Left = RMXP_AnimatedPaths(Path).x + X_offset * TileWidth
        tUL.Top = RMXP_AnimatedPaths(Path).y + Y_offset * TileHeight + TileHeight / 2
        X_offset = 1
        Y_offset = 2
        If Temp(2, 1) + Temp(1, 2) = 0 Then ' ur
            X_offset = 2
            Y_offset = 3
        ElseIf Not Temp(2, 1) + Temp(1, 2) = Path And Temp(2, 2) = 0 Then 'NOT TEMP(2,...
            X_offset = 2
            Y_offset = 0
        ElseIf Temp(0, 1) + Temp(1, 2) = 0 Then ' ul'NOT NOT...
            X_offset = 0
            Y_offset = 3
        ElseIf Temp(1, 0) + Temp(2, 1) = 0 Then ' or'NOT TEMP(0,...
            X_offset = 2
            Y_offset = 1
        ElseIf Temp(0, 1) + Temp(1, 0) = 0 Then ' ol'NOT TEMP(1,...
            X_offset = 0
            Y_offset = 1
        ElseIf Temp(1, 2) = 0 Then ' unten'NOT TEMP(0,...
            X_offset = 1
            Y_offset = 3
        ElseIf Temp(2, 1) = 0 Then ' rechts'NOT TEMP(1,...
            X_offset = 2
            Y_offset = 2
        ElseIf Temp(1, 0) = 0 Then ' oben'NOT TEMP(2,...
            X_offset = 1
            Y_offset = 1
        ElseIf Temp(0, 1) = 0 Then ' links'NOT TEMP(1,...
            X_offset = 0
            Y_offset = 2
        End If
        tUR.Left = RMXP_AnimatedPaths(Path).x + X_offset * TileWidth + TileWidth / 2
        tUR.Top = RMXP_AnimatedPaths(Path).y + Y_offset * TileHeight + TileHeight / 2
        
        m_MapCells(x, y).RM2KP(1).x = tOL.Left
        m_MapCells(x, y).RM2KP(1).y = tOL.Top
        m_MapCells(x, y).RM2KP(2).x = tOR.Left
        m_MapCells(x, y).RM2KP(2).y = tOR.Top
        m_MapCells(x, y).RM2KP(3).x = tUR.Left
        m_MapCells(x, y).RM2KP(3).y = tUR.Top
        m_MapCells(x, y).RM2KP(4).x = tUL.Left
        m_MapCells(x, y).RM2KP(4).y = tUL.Top
      
RenderNow:
        Dim OffX As Integer
        OffX = AnimationBuffer2 * 3 * TileWidth
        
        If m_MapCells(x, y).RM2KP(1).x = RMXP_AnimatedPaths(Path).x + Const_TileWidth And m_MapCells(x, y).RM2KP(1).y = RMXP_AnimatedPaths(Path).y + Const_TileHeight * 2 Then
            If m_MapCells(x, y).RM2KP(2).x = RMXP_AnimatedPaths(Path).x + Const_TileWidth * 1.5 And m_MapCells(x, y).RM2KP(2).y = RMXP_AnimatedPaths(Path).y + Const_TileHeight * 2 Then
                If m_MapCells(x, y).RM2KP(3).x = RMXP_AnimatedPaths(Path).x + Const_TileWidth * 1.5 And m_MapCells(x, y).RM2KP(3).y = RMXP_AnimatedPaths(Path).y + Const_TileHeight * 2.5 Then
                    If m_MapCells(x, y).RM2KP(4).x = RMXP_AnimatedPaths(Path).x + Const_TileWidth And m_MapCells(x, y).RM2KP(4).y = RMXP_AnimatedPaths(Path).y + Const_TileHeight * 2.5 Then
                        s2dtp.RenderTexture TexID, XTo, YTo, TileWidth, TileHeight, TileWidth, TileHeight, RMXP_AnimatedPaths(Path).x + Const_TileWidth + OffX, RMXP_AnimatedPaths(Path).y + Const_TileHeight * 2, CFm_Alpha, , , , , CFm_Color
                        Exit Sub
                    End If
                End If
            End If
        End If
        
        With s2dtp
            .RenderTexture TexID, XTo, YTo, TileWidth / 2, TileHeight / 2, TileWidth / 2, TileHeight / 2, CInt(m_MapCells(x, y).RM2KP(1).x) + OffX, CInt(m_MapCells(x, y).RM2KP(1).y), CFm_Alpha, , , , , CFm_Color
            .RenderTexture TexID, XTo + TileWidth / 2, YTo, TileWidth / 2, TileHeight / 2, TileWidth / 2, TileHeight / 2, CInt(m_MapCells(x, y).RM2KP(2).x) + OffX, CInt(m_MapCells(x, y).RM2KP(2).y), CFm_Alpha, , , , , CFm_Color
            .RenderTexture TexID, XTo + TileWidth / 2, YTo + TileHeight / 2, TileWidth / 2, TileHeight / 2, TileWidth / 2, TileHeight / 2, CInt(m_MapCells(x, y).RM2KP(3).x) + OffX, CInt(m_MapCells(x, y).RM2KP(3).y), CFm_Alpha, , , , , CFm_Color
            .RenderTexture TexID, XTo, YTo + TileHeight / 2, TileWidth / 2, TileHeight / 2, TileWidth / 2, TileHeight / 2, CInt(m_MapCells(x, y).RM2KP(4).x) + OffX, CInt(m_MapCells(x, y).RM2KP(4).y), CFm_Alpha, , , , , CFm_Color
        End With 's2dtp
    End If
End Sub

Public Sub Clear(ByVal m_TileSetID As Byte, ByVal TileX As Integer, ByVal TileY As Integer, ByVal Draw As Boolean)
    Dim x As Integer
    Dim y As Integer
    Dim m As Integer

    For x = 0 To MyLevel.MyMap.MapWidth - 1
        For y = 0 To MyLevel.MyMap.MapHeight - 1
            Me.m_TileSetID = m_TileSetID
            With m_MapCells(x, y)
                .TileX = TileX
                .TileY = TileY
                .DrawTile = Draw
            End With 'm_MapCells(X, Y)
        Next y
    Next x
End Sub

Private Function GetAnimation(ByVal x As Integer, ByVal y As Integer) As Integer
    On Error Resume Next
    
    Dim mc As tMapCell
    mc = m_MapCells(x, y)
    Dim i  As Integer
    
    For i = 1 To UBound(RM2K_Animations)
        If RM2K_Animations(i).TileSet = m_TileSetID Then
            If mc.TileX * Const_TileWidth = RM2K_Animations(i).x Then
                If mc.TileY * Const_TileHeight = RM2K_Animations(i).y Then
                    GetAnimation = i
                End If
            End If
        End If
    Next i
    
    On Error GoTo 0
End Function

Private Function GetPath(ByVal x As Integer, ByVal y As Integer, Path As Integer) As Integer
    On Error Resume Next
    
    Dim mc As tMapCell
    mc = m_MapCells(x, y)
    Dim i  As Integer
    
    If mc.DrawTile = False Then GetPath = 0: Exit Function
    
    For i = 1 To UBound(RM2K_Paths)
        If RM2K_Paths(i).TileSet = m_TileSetID Then
            If mc.TileX * Const_TileWidth = RM2K_Paths(i).x Then
                If mc.TileY * Const_TileHeight = RM2K_Paths(i).y Then
                    GetPath = i
                    Exit Function
                End If
            End If
        End If
    Next i
    
    If x = -1 Then GetPath = Path
    If y = -1 Then GetPath = Path
    If x = MyMap.MapWidth Then GetPath = Path
    If y = MyMap.MapHeight Then GetPath = Path
        
    On Error GoTo 0
End Function

Private Function GetAnimationPath(ByVal x As Integer, ByVal y As Integer, Path As Integer) As Integer
    On Error Resume Next
    
    Dim mc As tMapCell
    mc = m_MapCells(x, y)
    Dim i  As Integer
    
    For i = 1 To UBound(RM2K_AnimatedPaths)
        If RM2K_AnimatedPaths(i).TileSet = m_TileSetID Then
            If mc.TileX * Const_TileWidth = RM2K_AnimatedPaths(i).x Then
                If mc.TileY * Const_TileHeight = RM2K_AnimatedPaths(i).y Then
                    GetAnimationPath = i
                End If
            End If
        End If
    Next i
    
    If x = -1 Then GetAnimationPath = Path
    If y = -1 Then GetAnimationPath = Path
    If x = MyMap.MapWidth Then GetAnimationPath = Path
    If y = MyMap.MapHeight Then GetAnimationPath = Path
    
    On Error GoTo 0
End Function

Private Function GetAnimationPathXP(ByVal x As Integer, ByVal y As Integer, Path As Integer) As Integer
    On Error Resume Next
    
    Dim mc As tMapCell
    mc = m_MapCells(x, y)
    Dim i  As Integer
    
    If mc.DrawTile = False Then GetAnimationPathXP = 0: Exit Function
    
    For i = 1 To UBound(RMXP_AnimatedPaths)
        If RMXP_AnimatedPaths(i).TileSet = m_TileSetID Then
            If mc.TileX * Const_TileWidth = RMXP_AnimatedPaths(i).x Then
                If mc.TileY * Const_TileHeight = RMXP_AnimatedPaths(i).y Then
                   GetAnimationPathXP = i
                End If
            End If
        End If
    Next i

    If x = -1 Then GetAnimationPathXP = Path
    If y = -1 Then GetAnimationPathXP = Path
    If x = MyMap.MapWidth Then GetAnimationPathXP = Path
    If y = MyMap.MapHeight Then GetAnimationPathXP = Path
    
    On Error GoTo 0
End Function

Public Sub Initialize()
    If Not (MyLevel.MyMap.MapWidth <= 0 Or MyLevel.MyMap.MapHeight <= 0) Then
        ReDim m_MapCells(0 To MyLevel.MyMap.MapWidth - 1, 0 To MyLevel.MyMap.MapHeight - 1) As tMapCell
        ReDim m_SprIDs(0 To MyLevel.MyMap.MapWidth - 1, 0 To MyLevel.MyMap.MapHeight - 1) As Integer
    End If

    CFm_Color = vbWhite
    Set MyMap = MyLevel.MyMap
    Set s2dtp = MyMap.MyS2D.TexturePool

    TileWidth = Const_TileWidth
    TileHeight = Const_TileHeight
    
    Dim Size As Long
    Size = (BackBufferWidth / (TileWidth / 2)) * (BackBufferHeight / (TileHeight / 2)) * 3
    BufID = s2dtp.Pool1.CreateBuffer(Size)
    Redraw = True
End Sub

Public Sub UpdateDimensions(oldW As Integer, oldH As Integer)
    Dim newW As Long, newH As Long, w As Long, H As Long
    newW = MyLevel.MyMap.MapWidth
    newH = MyLevel.MyMap.MapHeight
    w = min(CLng(oldW), newW)
    H = min(CLng(oldH), newH)

    Dim TempC() As tMapCell
    ReDim TempC(0 To w + 1, 0 To H) As tMapCell
    
    Dim x As Integer, y As Integer
    For y = 0 To H - 1
        CopyMemory TempC(0, y), m_MapCells(0, y), Len(m_MapCells(0, 0)) * w
    Next y
    
    ReDim m_MapCells(0 To newW - 1, 0 To newH - 1) As tMapCell
    
    For y = 0 To H - 1
        CopyMemory m_MapCells(0, y), TempC(0, y), Len(m_MapCells(0, 0)) * w
    Next y
    
    Redraw = True
End Sub

Public Sub Load()
    Dim l As Integer
    Dim x As Integer
    Dim y As Integer

    Initialize
    Get #1, , l
    CFm_Desc = String$(l, Chr$(0))
    Get #1, , CFm_Desc
    Get #1, , CFm_Additive
    Get #1, , m_TileSetID
    For x = 0 To MyLevel.MyMap.MapWidth - 1
        For y = 0 To MyLevel.MyMap.MapHeight - 1
            Get #1, , m_MapCells(x, y)
        Next y
    Next x
    
    Redraw = True
End Sub


Public Property Get MapDrawing(x As Integer, y As Integer) As Boolean
    On Error Resume Next
    
    MapDrawing = m_MapCells(x, y).DrawTile
    On Error GoTo 0
End Property

Public Property Let MapDrawing(x As Integer, y As Integer, ByVal vNew As Boolean)
    On Error Resume Next
    
    m_MapCells(x, y).DrawTile = vNew
    Redraw = True
    On Error GoTo 0
End Property

Public Property Get MapTileX(x As Integer, y As Integer) As Integer
    On Error Resume Next
    
    MapTileX = m_MapCells(x, y).TileX
    On Error GoTo 0
End Property

Public Property Let MapTileX(x As Integer, y As Integer, ByVal vNew As Integer)
    On Error Resume Next
    
    m_MapCells(x, y).TileX = vNew
    Redraw = True
    On Error GoTo 0
End Property

Public Property Get MapTileY(x As Integer, y As Integer) As Integer
    On Error Resume Next
    
    MapTileY = m_MapCells(x, y).TileY
    On Error GoTo 0
End Property

Public Property Let MapTileY(x As Integer, y As Integer, ByVal vNew As Integer)
    On Error Resume Next
    
    m_MapCells(x, y).TileY = vNew
    Redraw = True
    On Error GoTo 0
End Property

Public Sub ClearLayer(TilesetSprID As Long, stTileX As Long, stTileY As Long, stWidth As Long, stHeight As Long, Drawing As Boolean)
    On Error Resume Next
    
    Dim x As Integer, y As Integer
    Dim sprid As Long
    
    sprid = MyLevel.MyMap.TileSets(TilesetSprID).TexID
    
    m_TileSetID = TilesetSprID
    For y = 0 To stHeight - 1
        For x = 0 To stWidth - 1
            With m_MapCells(x, y)
                .DrawTile = Drawing
                .TileX = stTileX + ((x) Mod stWidth)
                .TileY = stTileY + ((y) Mod stHeight)
                AdditionalInfo x, y
            End With
        Next x
    Next y
    
    DoEvents
    
    For y = 0 To MyLevel.MyMap.MapHeight  'stHeight
        For x = 0 To MyLevel.MyMap.MapWidth '- 1
            CopyMemory m_MapCells(x, y), m_MapCells(x Mod stWidth, y Mod stHeight), Len(m_MapCells(0, 0))
        Next x
    Next y
    
    Redraw = True
End Sub

Public Sub ReInit()
    Set MyMap = MyLevel.MyMap
    Set s2dtp = MyMap.MyS2D.TexturePool
    Redraw = True
End Sub

Public Sub Render(Optional toX As Long = 0, Optional toY As Long = 0)
    On Error GoTo out

    If Not Visible Then Exit Sub
    If m_TileSetID = 0 Then Exit Sub
    
    If MyMap.NoBuffer Then
        RenderAll toX, toY, Alpha
    Else
        If Me Is MyLevel.MyMap.Levels(CurrentLevel).Layers(CurrentLayer) Then
             RenderAll toX, toY, Alpha
        Else
            If Redraw Then
                s2dtp.Pool1.ClearBuffer BufID
                s2dtp.Pool1.BeginBuffer BufID
                RenderAll toX, toY, Alpha
                s2dtp.Pool1.EndBuffer
                Redraw = False
                s2dtp.Pool1.FlushBuffer BufID, Alpha
            Else
                s2dtp.Pool1.FlushBuffer BufID, Alpha
            End If
        End If
    End If
out:
End Sub

Public Sub RenderAll(Optional toX As Long = 0, Optional toY As Long = 0, Optional ByVal lAlpha As Integer = 255)
    Dim xOfs As Double
    Dim yOfs As Double
    Dim mc As tMapCell
    Dim StartX As Integer
    Dim StartY As Integer
    Dim EndX As Integer
    Dim EndY As Integer
    Dim x As Long
    Dim y As Long
    Dim XTo As Double
    Dim YTo As Double
    Dim XFrom As Integer
    Dim YFrom As Integer
    Dim TexID As Integer
  
    TexID = MyMap.TileSets(m_TileSetID).TexID
    xOfs = MyLevel.XOffset + toX
    yOfs = MyLevel.YOffset + toY
      
    StartX = -1 * (xOfs / Const_TileWidth) - 1
    StartY = -1 * (yOfs / Const_TileHeight) - 1
    EndX = StartX + (ScreenWidth / Const_TileWidth) + 2
    EndY = StartY + (ScreenHeight / Const_TileHeight) + 2
    
    If EndX >= MyMap.MapWidth Then EndX = MyMap.MapWidth - 1
    If EndY >= MyMap.MapHeight Then EndY = MyMap.MapHeight - 1
    If StartX < 0 Then StartX = 0
    If StartY < 0 Then StartY = 0
    
    MyLevel.MyMap.MyS2D.TexturePool.Pool1.OverrideZ = Me.MyLevel.Z
    
    For x = StartX To EndX
        For y = StartY To EndY
            mc = m_MapCells(x, y)
            If mc.DrawTile = False Then GoTo nextcell
            
            XTo = x * Const_TileWidth + toX '+ xOfs - 1
            YTo = y * Const_TileHeight + toY '+ yOfs - 1
            
            XFrom = mc.TileX * Const_TileWidth '+ 1
            YFrom = mc.TileY * Const_TileHeight '+ 1
            
            Select Case mc.Additional
            Case 0
                MyLevel.MyMap.MyS2D.TexturePool.RenderTexture TexID, x * Const_TileWidth + toX, y * Const_TileHeight + toY, Const_TileWidth, Const_TileHeight, Const_TileWidth, Const_TileHeight, XFrom, YFrom, lAlpha, , , , , CFm_Color
            Case 4
                CheckPos3 TexID, x, y, mc.AdditionalID, lAlpha, CFm_Additive, CFm_Color, XTo, YTo
            Case 3
                DrawRM2KAnimation TexID, x, y, mc.AdditionalID, lAlpha, CFm_Additive, CFm_Color, XTo, YTo
            Case 2
                CheckPos2 TexID, x, y, mc.AdditionalID, lAlpha, CFm_Additive, CFm_Color, XTo, YTo
            Case 1
                CheckPos TexID, x, y, mc.AdditionalID, lAlpha, CFm_Additive, CFm_Color, XTo, YTo
            End Select
nextcell:
        Next y
    Next x
       
    AnimationCounter = AnimationCounter + 1
    If AnimationCounter > 10 Then
        AnimationCounter = 0
        AnimationBuffer = AnimationBuffer + 1
        
        If AnimationDirection = 0 Then
            AnimationBuffer2 = AnimationBuffer2 + 1
        Else
            AnimationBuffer2 = AnimationBuffer2 - 1
        End If
        
        If AnimationBuffer2 = 2 Then AnimationDirection = 1
        If AnimationBuffer2 = 0 Then AnimationDirection = 0
        If AnimationBuffer > 3 Then AnimationBuffer = 0
    End If
    
    GFX.TexturePool.Pool1.OverrideZ = -1
End Sub

Public Sub Save()
    Dim x As Integer
    Dim y As Integer
    Dim l As Integer

    l = Len(CFm_Desc)
    Put #1, , l
    Put #1, , CFm_Desc
    Put #1, , CFm_Additive
    Put #1, , m_TileSetID
    
    For x = 0 To MyLevel.MyMap.MapWidth - 1
        For y = 0 To MyLevel.MyMap.MapHeight - 1
            Put #1, , m_MapCells(x, y)
        Next y
    Next x
End Sub

Private Function Pyt(H, w) As Double
    Pyt = Sqr((H * H) + (w * w))
End Function
