Attribute VB_Name = "PlayerStatsModule"
'programmed by Ervin Pe Benito
'// PlayerstatsModule.bas
Option Explicit
Private statsFilename As String
Public Const LevelUpPoints = 2
Private Const LevelUpBase = 10
Private Type Stats
    Name As String * 10
    Level As Long
    Experience As Long
    nextExperience As Long
    Strength As Long 'determines the attack rating
    Vitality As Long 'determines the amount of life
    'determines the 1st attack
    Dexterity As Long
    'determines the chance of blocking/miss of an enemy attack
    Agility As Long
    Money As Long 'amount of money carried
    Kills As Long
    Damage As Long 'amount of damage
    Armor As Long 'amount of armor
    Life As Long 'amount of Life
    LifeMax As Long
End Type
Private Type Belt
    Slot(0 To 3) As Long 'slot for placing an item
End Type
'main character's statistics
Public PlayerStats As Stats
'main character's belt
Public PlayerBelt As Belt
Public MapNumber As Integer
Public files() As String
Public Filenum As Long

Public Sub NewPlayer()
    'initialize the starting player stats
    With PlayerStats
        .Strength = 10
        .Vitality = 20
        .Dexterity = 10
        .Agility = 10
        .Money = 10
        .Kills = 0
        .Level = 1
        .Damage = 0
        .Armor = 0
        .Life = 0
        .LifeMax = 0
        .Experience = 0
        .nextExperience = 10
    End With
    'redefining the amount, this will elimante a bug!
    With PlayerBelt
        .Slot(0) = 0
        .Slot(1) = 0
        .Slot(2) = 0
        .Slot(3) = 0
    End With
    CheckCheats
    RefreshStats
End Sub
Public Sub SavePlayer()
    Dim lfile, loc, fpointer As Long
    Dim sTemp As String
    
    lfile = FreeFile
    statsFilename = PlayerStats.Name
    Open App.Path & "\savegames\" & Trim(statsFilename) & ".sav" For Binary As lfile
    On Error GoTo ErrorHandler
    'saving player stats
    Put #lfile, , PlayerStats.Name
    Put #lfile, , PlayerStats.Level
    Put #lfile, , PlayerStats.Experience
    Put #lfile, , PlayerStats.nextExperience
    Put #lfile, , PlayerStats.Strength
    Put #lfile, , PlayerStats.Vitality
    Put #lfile, , PlayerStats.Dexterity
    Put #lfile, , PlayerStats.Agility
    Put #lfile, , PlayerStats.Money
    Put #lfile, , PlayerStats.Kills
    Put #lfile, , PlayerStats.Damage
    Put #lfile, , PlayerStats.Armor
    Put #lfile, , PlayerStats.Life
    Put #lfile, , PlayerStats.LifeMax
    'saving belt slots
    Put #lfile, , PlayerBelt.Slot(0)
    Put #lfile, , PlayerBelt.Slot(1)
    Put #lfile, , PlayerBelt.Slot(2)
    Put #lfile, , PlayerBelt.Slot(3)
    'saving other player data
    Put #lfile, , iLastStage
    Put #lfile, , Userchar.Direction
    Put #lfile, , Userchar.MapX
    Put #lfile, , Userchar.MapY
    'saving currentmap number
    Put #lfile, , MapNumber
    Close #lfile
       
    
ErrorHandler:

End Sub
Public Sub LoadPlayer(fname As String)
    Dim lfile As Long
          
    lfile = FreeFile
    Open App.Path & "\savegames\" & fname For Binary As lfile
    'saving player stats
    Get #lfile, , PlayerStats.Name
    Get #lfile, , PlayerStats.Level
    Get #lfile, , PlayerStats.Experience
    Get #lfile, , PlayerStats.nextExperience
    Get #lfile, , PlayerStats.Strength
    Get #lfile, , PlayerStats.Vitality
    Get #lfile, , PlayerStats.Dexterity
    Get #lfile, , PlayerStats.Agility
    Get #lfile, , PlayerStats.Money
    Get #lfile, , PlayerStats.Kills
    Get #lfile, , PlayerStats.Damage
    Get #lfile, , PlayerStats.Armor
    Get #lfile, , PlayerStats.Life
    Get #lfile, , PlayerStats.LifeMax
    'saving belt slots
    Get #lfile, , PlayerBelt.Slot(0)
    Get #lfile, , PlayerBelt.Slot(1)
    Get #lfile, , PlayerBelt.Slot(2)
    Get #lfile, , PlayerBelt.Slot(3)
    'saving other player data
    Get #lfile, , iLastStage
    Get #lfile, , Userchar.Direction
    Get #lfile, , Userchar.MapX
    Get #lfile, , Userchar.MapY
    'saving currentmap number
    Get #lfile, , MapNumber
    Close #lfile
End Sub

Public Sub SetNextExperience()
    PlayerStats.nextExperience = (PlayerStats.Level) * LevelUpBase
End Sub
Private Sub SetDamage()
    PlayerStats.Damage = CLng(PlayerStats.Strength * 0.5)
End Sub

Private Sub SetLife()
    PlayerStats.Life = CLng(PlayerStats.Vitality * 0.8)
End Sub
Private Sub SetArmor()
    PlayerStats.Armor = CLng(PlayerStats.Agility * 0.3)
End Sub
Private Sub SetLifeMax()
    PlayerStats.LifeMax = PlayerStats.Life
End Sub
Public Sub RefreshStats()
    SetDamage
    SetLife
    SetLifeMax
    SetArmor
End Sub
Public Sub InputName(sName As String)
    If Len(Trim(sName)) = 0 Then
        PlayerStats.Name = "NONAME"
    Else
        PlayerStats.Name = Trim(sName)
    End If
End Sub

Public Sub ListSaves(x, y As Long)
Dim t1 As Long
    For t1 = 1 To Filenum
        If t1 < 15 Then
            Draw_Text x + 64, y, 200, 200, t1 & " " & files(t1), &HFFFFFFFF, DT_TOP Or DT_LEFT
            y = y + 16
        End If
    Debug.Print Filenum
    Next
End Sub
Public Sub GetFiles()
Dim a, num As Long
Dim ff As String
    Filenum = 0
    ff = Dir(App.Path & "\savegames\*.sav")
    While ff <> ""
        If ff <> "" Then Filenum = Filenum + 1
        ff = Dir()
    Wend
    If Filenum > 0 Then
        ReDim files(1 To Filenum)
        
        ff = Dir(App.Path & "\savegames\*.sav")
        While ff <> ""
            If ff <> "" Then
                num = num + 1
                files(num) = ff
            End If
            ff = Dir()
        Wend
    End If
End Sub
Public Sub KillFile(filename As String)
    Kill App.Path & "\savegames\" & filename
End Sub
'cheats, this is used in debugging
Private Sub CheckCheats()
    If RTrim(PlayerStats.Name) = "MANOLING" Then
        PlayerStats.Money = 1000
        bDevMode = False
    ElseIf RTrim(PlayerStats.Name) = "ENDUREME" Then
        bDevMode = True
    Else
        bDevMode = False
    End If
End Sub
