Attribute VB_Name = "Module1"
Option Explicit

Public Type MYSETTINGS
    Language As String
    BackGroundFileName As String
    BaseFileName As String
    Level As String
End Type

Public Const BASECOUNT = 14
Public Const POTODISTANCE = 4500

Public Second, Hour, Minute As Integer

Public XLevel As Integer
Public XMain As Integer

Public TimeLimit As Long

Public Is_In_Game As Boolean
Public XVariable As Integer

Public ActiveBase As Integer
Public BaseInUsed As Integer

Type COORDINATE
    xLeft As Single
    xTop As Single
End Type

Public XSecond As Long

Public BaseCoordinate(0 To BASECOUNT) As COORDINATE

Type Score
    Player As String
    Time As String
    Level As String
    Second As String
End Type

Public Sub RecolorAll()
    Dim XVariable As Integer
    For XVariable = 0 To MainForm.Poto.Count - 1
        MainForm.Poto(XVariable).Picture = MainForm.Dump(0).Picture
    Next XVariable
End Sub

Public Function ChangeMain(ByVal KeyCode As Integer)
    With MainForm
    Select Case XMain
        Case 0
            Select Case KeyCode
                Case vbKeyRight
                    RecolorAll
                    XMain = 2
                    .Poto(2).Picture = .Dump(1).Picture
                    MoveToPoto
                Case vbKeyLeft
                    RecolorAll
                    XMain = 1
                    .Poto(1).Picture = .Dump(1).Picture
                    MoveToPoto
            End Select
        Case 1
            Select Case KeyCode
                Case vbKeyRight
                    RecolorAll
                    XMain = 0
                    .Poto(0).Picture = .Dump(1).Picture
                    MoveToPoto
            End Select
        Case 2
            Select Case KeyCode
                Case vbKeyLeft
                    RecolorAll
                    XMain = 0
                    .Poto(0).Picture = .Dump(1).Picture
                    MoveToPoto
            End Select
    End Select
    End With
End Function

Public Function ReArrangeUp()
    Dim TempVar As Integer
    Dim XT
    For XT = 0 To MainForm.BaseList(XMain).ListCount - 2
        TempVar = Val(MainForm.BaseList(XMain).List(XT + 1))
        MainForm.BaseList(XMain).List(XT) = TempVar
    Next XT
    MainForm.BaseList(XMain).RemoveItem (MainForm.BaseList(XMain).ListCount - 1)
End Function

Public Function ReArrangeDown()
    Dim TempVar
    Dim XT
    MainForm.DumpList.Clear
    For XT = 0 To MainForm.BaseList(XMain).ListCount - 1
        MainForm.DumpList.AddItem MainForm.BaseList(XMain).List(XT)
    Next XT
    MainForm.BaseList(XMain).Clear
    MainForm.BaseList(XMain).AddItem ActiveBase
    For XT = 0 To MainForm.DumpList.ListCount - 1
        MainForm.BaseList(XMain).AddItem MainForm.DumpList.List(XT)
    Next XT
End Function

Public Function PickUp()
    Dim TempVar As Integer
    With MainForm
    If BaseInUsed <> 0 Then
        If ActiveBase = BaseInUsed Then
            Exit Function
        End If
    End If
    TempVar = Val(.BaseList(XMain).List(0))
    If Val(TempVar) = 0 Then
        Exit Function
    End If
    .Base(TempVar).Top = .Poto(XMain).Top
    ActiveBase = TempVar
    BaseInUsed = TempVar
    ReArrangeUp
    End With
End Function

Public Function PickDown()
    Dim TempVar As Integer
    With MainForm
    If ActiveBase = 0 Then
        Exit Function
    End If
    TempVar = Val(.BaseList(XMain).List(0))
    If Val(TempVar) < ActiveBase And TempVar <> 0 Then
        Exit Function
    End If
    If .BaseList(XMain).ListCount <> 0 Then
        .Base(ActiveBase).Top = .Base(TempVar).Top - .Base(TempVar).Height - 40
        PlaySoundRes 101
    Else
        .Base(ActiveBase).Top = .Base(TempVar).Top - .Base(TempVar).Height
        PlaySoundRes 101
    End If
    ReArrangeDown
    ActiveBase = 0
    End With
End Function

Public Function MoveToPoto()
    With MainForm
    If ActiveBase = 0 Then
        Exit Function
    End If
    .Base(ActiveBase).Top = .Poto(XMain).Top
    .Base(ActiveBase).Left = .Poto(XMain).Left + (.Poto(XMain).Width / 2) - (.Base(ActiveBase).Width) / 2 - 20
    End With
End Function

Public Function IsWin() As Boolean
    IsWin = False
    Dim AH As Integer
    Dim PZ As Integer
    If XMain = 0 Then
        Exit Function
    End If
    For AH = 0 To MainForm.BaseList(XMain).ListCount - 1
        If MainForm.BaseList(XMain).List(AH) = AH + 1 Then
            PZ = PZ + 1
        End If
    Next AH
    If PZ >= XLevel Then IsWin = True
End Function

Public Function ReInitializeToLevel(ByVal Level As Integer)
    Dim OX, OY As Integer
    With MainForm
    
        TimeLimit = (XLevel * 0.5) * 60
        
        RecolorAll
        XLevel = Level
        XMain = 0
        .Poto(0).Picture = .Dump(1).Picture
        For OX = 0 To .Poto.Count - 1
            .BaseList(OX).Clear
        Next OX
        If Level > BASECOUNT Then
            Exit Function
        End If
        For OY = 0 To 2
            .Poto(OY).Height = 735
        Next OY
        For OX = 1 To Level
            .BaseList(0).AddItem OX
            For OY = 0 To 2
                .Poto(OY).Height = .Poto(OY).Height + .Base(OX).Height
                .Poto(0).Top = (.Table.Height / 2) - (.Poto(0).Height / 2)
                .Poto(OY).Top = .Poto(0).Top
            Next OY
        Next OX
        
        .Poto(1).Left = .Poto(0).Left - POTODISTANCE
        .Poto(2).Left = .Poto(0).Left + POTODISTANCE
        .Base(0).Top = .Poto(0).Top + .Poto(0).Height
        
        For OX = 1 To Level
            .Base(OX).Visible = True
        Next OX
        
        For OX = Level + 1 To .Base.Count - 1
            .Base(OX).Visible = False
        Next OX
        
        ReArrangeBases
        
        For OX = 0 To BASECOUNT
            BaseCoordinate(OX).xLeft = .Base(OX).Left
            BaseCoordinate(OX).xTop = .Base(OX).Top
        Next OX
        
    End With
        
End Function

Public Function FormatTime()
    Dim sSecond, sMinute, sHour As String
    If Second < 10 Then
        sSecond = "0" & Second
    Else
        sSecond = Second
    End If
    If Minute < 10 Then
        sMinute = "0" & Minute
    Else
        sMinute = Minute
    End If
    If Hour < 10 Then
        sHour = "0" & Hour
    Else
        sHour = Hour
    End If
    FormatTime = sHour & ":" & sMinute & ":" & sSecond
End Function

Public Sub ReArrangeBases()
    Dim Cnt As Long
    Dim TmpTop As Long
    With MainForm
        TmpTop = .Base(0).Top + 30
        For Cnt = XLevel To 1 Step -1
            .Base(Cnt).Top = TmpTop - .Base(Cnt).Height - 30
            .Base(Cnt).Left = .Poto(0).Left - (.Base(Cnt).Width / 2) + (.Poto(0).Width / 2)
            TmpTop = .Base(Cnt).Top
        Next Cnt
    End With
End Sub
