Attribute VB_Name = "MapModule"
'// Programmed by Ervin Louie B. Pe Benito
'// MapModule.bas
'// map functions modules
Option Explicit

Public dmapversion As String
Private MapVersion As String * 5
Private MapName As String * 10
Private mapFilename As String
Const MapMaxX = 18
Const MapMaxY = 12
'used for determining map position
Public PresentMapX As Integer
Public PresentMapY As Integer

Public Type NPC
    Name As String * 5
    Posx As Integer
    Posy As Integer
    Direction As Integer
    PlayerID As Integer
End Type

Private PlayerentryX As Integer
Private PlayerentryY As Integer

Public Type maptiles
    BGT As Integer
    FGT As Integer
    Walkable As Byte
    Behavior As Long
End Type

Public NPCs() As NPC
Public maxNPC As Integer

Public Type xMap
    maptile(MapMaxX, MapMaxY) As maptiles
    enabled As Boolean
End Type

Public GameMap(1 To 6) As xMap
Private MapDelay As Integer
'*******************************
'*****Map Editor Codes**********

'Public Function getmapPlayerentryY() As Integer
'    getmapPlayerentryY = PlayerentryY
'End Function

'Public Function getmapVersion() As String
'    getmapVersion = MapVersion
'End Function

'Public Function getBGT(iheight As Integer, iwidth As Integer) As Long
'    getBGT = maptile(iheight, iwidth).BGT
'End Function

'Public Function getTGT(iheight As Integer, iwidth As Integer) As Long
'    getTGT = maptile(iheight, iwidth).TGT
'End Function

'Public Function getWalkable(iheight As Integer, iwidth As Integer) As Byte
'    getWalkable = maptile(iheight, iwidth).Walkable
'End Function

'Public Function getBehavior(iheight As Integer, iwidth As Integer) As Long
'    getBehavior = maptile(iheight, iwidth).Behavior
'End Function

'Public Sub putmapPlayerentryX(ivalue As Integer)
'    PlayerentryX = ivalue
'End Sub

'Public Sub putmapPlayerentryY(ivalue As Integer)
'    PlayerentryY = ivalue
'End Sub

'Public Sub PutBGT(iHx As Integer, iWx As Integer, lBGT As Integer)
'    maptile(iHx, iWx).BGT = lBGT
'End Sub

'Public Sub PutFGT(iHx As Integer, iWx As Integer, lFGT As Integer)
'    maptile(iHx, iWx).FGT = lFGT
'End Sub

'Public Sub PutWalkable(iHx As Integer, iWx As Integer, bWalkable As Byte)
'    maptile(iHx, iWx).Walkable = bWalkable
'End Sub

'Public Sub PutBehavior(iHx As Integer, iWx As Integer, lBehavior As Long)
'    maptile(iHx, iWx).Behavior = lBehavior
'End Sub

'Public Function NewMap(sMapName As String, sFilename As String) As Boolean
'Dim x, y As Integer
'Dim lfile As Long
'MapVersion = dmapversion
'    MapName = sMapName
'
'    For x = 1 To MapMaxX
'        For y = 1 To MapMaxY
'            maptile(x, y).BGT = 0
'            maptile(x, y).FGT = 0
'            maptile(x, y).Walkable = 0
'            maptile(x, y).Behavior = 0
'        Next
'    Next
'
'    NewMap = True
'    Exit Function
    
'ErrorHandler:
'    NewMap = False
'End Function

'Public Function SaveMap(sMapName As String, sFilename As String) As Boolean
'Dim x, y As Integer
'Dim lfile As Long

'    MapName = sMapName
'
'    lfile = FreeFile
'    mapFilename = sFilename
'    Open App.Path & "\" & mapFilename For Binary As lfile
'    On Error GoTo ErrorHandler
'    Put #lfile, , MapVersion
'    Put #lfile, , MapName
'    Put #lfile, , PlayerentryX
'    Put #lfile, , PlayerentryY
'
'    For x = 1 To MapMaxX
'        For y = 1 To MapMaxY
'            Put #lfile, , maptile(x, y).BGT
'            Put #lfile, , maptile(x, y).FGT
'            Put #lfile, , maptile(x, y).Walkable
'            Put #lfile, , maptile(x, y).Behavior
'        Next
'    Next
'    Close #lfile
'    SaveMap = True
'    Exit Function
    
'ErrorHandler:
'    SaveMap = False
'End Function
'****** Map Editor Code Ends Here
Public Function LoadMap(filename As String, iNum As Integer) As Boolean
Dim x, y As Integer
Dim lfile As Long
    
    lfile = FreeFile
    mapFilename = filename
    Open App.Path & "\maps\" & mapFilename For Binary As lfile
    On Error GoTo ErrorHandler
    Get #lfile, , MapVersion
    Get #lfile, , MapName
    Get #lfile, , PlayerentryX
    Get #lfile, , PlayerentryY
    
    For x = 1 To MapMaxX
        For y = 1 To MapMaxY
            Get #lfile, , GameMap(iNum).maptile(x, y).BGT
            Get #lfile, , GameMap(iNum).maptile(x, y).FGT
            Get #lfile, , GameMap(iNum).maptile(x, y).Walkable
            Get #lfile, , GameMap(iNum).maptile(x, y).Behavior
        Next
    Next
    Close #lfile
    LoadMap = True
    Exit Function
    
ErrorHandler:
    LoadMap = False
End Function

Public Sub Drawmap(iMapNum As Integer)
    Dim iNum1, iNum2, iMapx, iMapy, x, y As Integer
    iMapx = 1
    For x = GameMapScreenX To GameMapScreenWidth Step 32
        iMapy = 1
        For y = GameMapScreenY To GameMapScreenHeight Step 32
            iNum1 = GameMap(iMapNum).maptile(iMapx, iMapy).BGT
            iNum2 = GameMap(iMapNum).maptile(iMapx, iMapy).FGT
            'background tiles
            Dev.SetTexture 0, Tiles(iNum1)
            Set_Vertex x, y, 32, 32, 16777215, 16777215, 16777215, 16777215
            Dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex(0), Len(Vertex(0))
            'forground tiles
            Dev.SetTexture 0, Tiles(iNum2)
            Set_Vertex x, y, 32, 32, 16777215, 16777215, 16777215, 16777215
            Dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex(0), Len(Vertex(0))
            
            iMapy = iMapy + 1
        Next
        iMapx = iMapx + 1
    Next
End Sub
'Subroutine for determining map position
Public Sub UpdateMapPos()
    PresentMapX = Int((Userchar.MapX + GameMapScreenX - 16) * 0.03125)
    PresentMapY = Int((Userchar.MapY + GameMapScreenY - 8) * 0.03125) - 2
End Sub
Public Sub CheckIfWalkable(idirection As Integer, iMapNum As Integer)
    UpdateMapPos
    'movedown
    If idirection = 0 Then
        If PresentMapY < 12 Then
            'used for fixing the collision bug!
            PresentMapX = Int((Userchar.MapX + GameMapScreenX - 17) * 0.03125)
            If GameMap(iMapNum).maptile(PresentMapX, PresentMapY).Walkable = 1 Then
                Userchar.MapY = Userchar.MapY - 8
            End If
        End If
    End If
    'moveright
    If idirection = 1 Then
        If PresentMapX < 18 Then
            If GameMap(iMapNum).maptile(PresentMapX, PresentMapY).Walkable = 1 Then
                Userchar.MapX = Userchar.MapX - 8
            End If
        End If
    End If
    'moveup
    If idirection = 2 Then
        If PresentMapY > 1 Then
            'used for fixing the collision bug!
            PresentMapX = Int((Userchar.MapX + GameMapScreenX - 17) * 0.03125)
            If GameMap(iMapNum).maptile(PresentMapX, PresentMapY).Walkable = 1 Then
                Userchar.MapY = Userchar.MapY + 8
            End If
        End If
    End If
    'moveleft
    If idirection = 3 Then
        If PresentMapX > 1 Then
            'used for fixing the collision bug!
            PresentMapX = Int((Userchar.MapX + GameMapScreenX - 18) * 0.03125)
            If GameMap(iMapNum).maptile(PresentMapX, PresentMapY).Walkable = 1 Then
                Userchar.MapX = Userchar.MapX + 8
            End If
        End If
    End If
    
End Sub
Public Sub CheckBehavior(idirection As Integer, iMapNum As Integer)
    UpdateMapPos
    'movedown
    If idirection = 0 Then
        If PresentMapY < 12 Then
            'used for fixing the collision bug!
            PresentMapX = Int((Userchar.MapX + GameMapScreenX - 17) * 0.03125)
        End If
    End If
    'moveright
    If idirection = 1 Then
        If PresentMapX < 18 Then
            If GameMap(1).enabled = True And MapDelay < 0 Then
                If GameMap(1).maptile(PresentMapX, PresentMapY).Behavior = 5 Then
                    DrawScreen = 9
                    Userchar.MapX = 64
                    GameMap(1).enabled = False
                    GameMap(3).enabled = True
                    Music_Stop 3
                    Music_Start 6
                    MapDelay = 10
                End If
            End If
            If GameMap(2).enabled = True And MapDelay < 0 Then
                If GameMap(2).maptile(PresentMapX, PresentMapY).Behavior = 6 Then
                    DrawScreen = 5
                    Userchar.MapX = 64
                    GameMap(2).enabled = False
                    GameMap(1).enabled = True
                    Music_Stop 5
                    Music_Start 3
                    MapDelay = 10
                End If
            End If
            If GameMap(3).enabled = True And MapDelay < 0 Then
                If GameMap(3).maptile(PresentMapX, PresentMapY).Behavior = 8 Then
                    DrawScreen = 10
                    Userchar.MapX = 64
                    GameMap(3).enabled = False
                    GameMap(4).enabled = True
                    Music_Stop 6
                    Music_Start 7
                    MapDelay = 10
                End If
            End If
            If GameMap(4).enabled = True And MapDelay < 0 Then
                If GameMap(4).maptile(PresentMapX, PresentMapY).Behavior = 10 Then
                    DrawScreen = 11
                    Userchar.MapX = 64
                    GameMap(4).enabled = False
                    GameMap(5).enabled = True
                    Music_Stop 7
                    Music_Start 8
                    MapDelay = 10
                End If
            End If
            If GameMap(5).enabled = True And MapDelay < 0 Then
                If GameMap(5).maptile(PresentMapX, PresentMapY).Behavior = 12 Then
                    DrawScreen = 12
                    Userchar.MapX = 64
                    GameMap(5).enabled = False
                    GameMap(6).enabled = True
                    Music_Stop 8
                    Music_Start 3
                    MapDelay = 10
                End If
            End If
        End If
    End If
    'moveup
    If idirection = 3 Then
            'used for fixing the collision bug!
            PresentMapX = Int((Userchar.MapX + GameMapScreenX - 17) * 0.03125)
        If GameMap(iMapNum).maptile(PresentMapX, PresentMapY - 1).Behavior = 1 Then
            DrawScreen = 7
        ElseIf GameMap(iMapNum).maptile(PresentMapX, PresentMapY - 1).Behavior = 2 Then
            MapNumber = iMapNum
            DrawScreen = 13
        ElseIf GameMap(iMapNum).maptile(PresentMapX, PresentMapY - 1).Behavior = 3 Then
            DrawScreen = 17
            bPrayle = DeterminePrayle
        ElseIf GameMap(iMapNum).maptile(PresentMapX, PresentMapY - 1).Behavior = 14 Then
            DrawScreen = 18
        End If
    End If
    'moveleft
    If idirection = 2 Then
        If PresentMapX > 1 Then
            'used for fixing the collision bug!
            PresentMapX = Int((Userchar.MapX + GameMapScreenX - 18) * 0.03125)
            If GameMap(1).enabled = True And MapDelay < 0 Then
                If GameMap(1).maptile(PresentMapX, PresentMapY).Behavior = 4 Then
                    DrawScreen = 8
                    Userchar.MapX = GameMapScreenWidth - 64
                    GameMap(1).enabled = False
                    GameMap(2).enabled = True
                    Music_Stop 3
                    Music_Start 5
                    MapDelay = 10
                End If
            End If
            If GameMap(3).enabled = True And MapDelay < 0 Then
                If GameMap(3).maptile(PresentMapX, PresentMapY).Behavior = 7 Then
                    DrawScreen = 5
                    Userchar.MapX = GameMapScreenWidth - 64
                    GameMap(3).enabled = False
                    GameMap(1).enabled = True
                    Music_Stop 6
                    Music_Start 3
                    MapDelay = 10
                End If
            End If
            If GameMap(4).enabled = True And MapDelay < 0 Then
                If GameMap(4).maptile(PresentMapX, PresentMapY).Behavior = 9 Then
                    DrawScreen = 9
                    Userchar.MapX = GameMapScreenWidth - 64
                    GameMap(4).enabled = False
                    GameMap(3).enabled = True
                    Music_Stop 7
                    Music_Start 6
                    MapDelay = 10
                End If
            End If
            If GameMap(5).enabled = True And MapDelay < 0 Then
                If GameMap(5).maptile(PresentMapX, PresentMapY).Behavior = 11 Then
                    DrawScreen = 10
                    Userchar.MapX = GameMapScreenWidth - 64
                    GameMap(5).enabled = False
                    GameMap(4).enabled = True
                    Music_Stop 8
                    Music_Start 7
                    MapDelay = 10
                End If
            End If
            If GameMap(6).enabled = True And MapDelay < 0 Then
                If GameMap(6).maptile(PresentMapX, PresentMapY).Behavior = 13 Then
                    DrawScreen = 11
                    Userchar.MapX = GameMapScreenWidth - 64
                    GameMap(6).enabled = False
                    GameMap(5).enabled = True
                    Music_Stop 3
                    Music_Start 7
                    MapDelay = 10
                End If
            End If
        End If
    End If
    If MapDelay < -50 Then
        MapDelay = -1
    Else
        MapDelay = MapDelay - 1
    End If
End Sub

Public Function DetermineChurch() As Boolean
Dim iNum As Integer
     iNum = (CInt(Rnd()) * 12)
    If iNum < 3 Then
        DetermineChurch = True
    Else
        DetermineChurch = False
    End If
End Function

Private Function DeterminePrayle() As Boolean
Dim iNum As Integer
    iNum = CInt(Rnd() * 100) + 1
    If iNum < 10 Then
        DeterminePrayle = False
    Else
        DeterminePrayle = True
    End If
End Function
