VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "clsUnit"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' clsUnit.cls
'   ta unit class.

Option Explicit
Option Compare Text

Public HPI As clsHPI ' unit archive file. '
Public Filename As String ' FBI filename within the hpi. '

Public Group As clsUnitArchive ' unit archive group pointer. '

' unit information. '
Public Name As String
Public Callname As String
Public Description As String
Public Class As String
Public ID As String
Public Side As String

' unit statistics. '
Public MaxDamage As String
Public BuildTime As String
Public CostEnergy As String
Public CostMetal As String
Public SightDistance As String
Public RadarDistance As String
Public SonarDistance As String

Public Weapons As New Collection ' unit weapons. '
Public WeaponNames As New Collection

Public Conflicts As New Collection ' conflicting units. '

' Load the unit from an FBI file. '
Public Function Load(Filename As String)
    Dim FileHandle As Integer
    Dim Flag As Boolean
    Dim Buffer As String
    Dim StartIndex As Long, EndIndex As Long
    Dim Key As String, Value As String
    
    On Error GoTo Error
    
'    HPI.CloseFile
'    Name = Space(100)
'    Side = Space(100)
'    Callname = Space(100)
'    Description = Space(100)
'    ID = Space(100)
'    MaxDamage = Space(100)
'    BuildTime = Space(100)
'    CostEnergy = Space(100)
'    CostMetal = Space(100)
'    Index = ReadUnit(HPI.Filename, Filename, Name, Side, Callname, Description, ID, MaxDamage, BuildTime, CostEnergy, CostMetal)
'    Name = StripNull(Name)
'    Side = StripNull(Side)
'    Callname = StripNull(Callname)
'    Description = StripNull(Description)
'    ID = StripNull(ID)
'    MaxDamage = StripNull(MaxDamage)
'    BuildTime = StripNull(BuildTime)
'    CostEnergy = StripNull(CostEnergy)
'    CostMetal = StripNull(CostMetal)
'    If Index <> 0 Then
'        Load = True
'    End If
'    Exit Function
    
    ' Extract the file. '
    If Not HPI.ExtractFile(Filename, "temp.fbi") Then Exit Function
    
    ' Parse the FBI file. '
    Load = True
    FileHandle = FreeFile
    Open "temp.fbi" For Binary As FileHandle
    Do While Not EOF(FileHandle)
        Line Input #FileHandle, Buffer
        Buffer = StripString(Buffer)
        
        On Error Resume Next
        StartIndex = InStr(1, Buffer, "=")
        If StartIndex = 0 Then StartIndex = 1
        EndIndex = InStr(1, Buffer, ";")
        If EndIndex = 0 Then EndIndex = Len(Buffer)
        Key = Left(Buffer, StartIndex - 1)
        Value = Mid(Buffer, StartIndex + 1, EndIndex - StartIndex - 1)
        On Error GoTo Error
        
        Select Case Key
            Case "unitname"
                Name = Value
                FrmProgress.Status = "Finding units... " & Name
                FrmProgress.RefreshForm
            Case "side"
                Side = Value
            Case "name"
                Callname = Value
            Case "tedclass"
                Class = Value
            Case "description"
                Description = Value
            Case "unitnumber"
                ID = Value
            Case "maxdamage"
                MaxDamage = Value
            Case "buildtime"
                BuildTime = Value
            Case "buildcostenergy"
                CostEnergy = Value
            Case "buildcostmetal"
                CostMetal = Value
            Case "weapon1", "weapon2", "weapon3", "weapon4", "weapon5"
                WeaponNames.Add Value
        End Select
    Loop
    
    Kill "temp.fbi"
Error:
    Close FileHandle
End Function

' Attach weapon pointers to unit. '
Public Sub AttachWeapons()
    Dim Index As Long, i As Long
    
    On Error Resume Next
    For Index = 1 To WeaponNames.Count
        Weapons.Add modUnits.Weapons(WeaponNames(Index)), WeaponNames(Index)
        modUnits.Weapons(WeaponNames(Index)).Units.Add Me, Name
    Next
End Sub

' Display unit information on frmMain. '
Public Sub Display()
    Dim Index As Long
    
    On Error GoTo Error
    Screen.MousePointer = vbHourglass
    With FrmMain
    
    ' Update status. '
    .Status.Panels(2).Text = Name
    .LblUnitname = Callname
    
    ' Update menu/toolbar. '
    If Enabled Then
        .Toolbar.Buttons(12).Image = 8
        .Toolbar.Buttons(12).ToolTipText = "Disable unit"
        .mnuUnitsEnable.Caption = "Disable &Unit"
    Else
        .Toolbar.Buttons(12).Image = 9
        .Toolbar.Buttons(12).ToolTipText = "Enable unit"
        .mnuUnitsEnable.Caption = "Enable &Unit"
    End If
    
    ' Create weapon listing. '
    .PicUnit.Picture = .Picture
    .LstWeapons.Clear
    For Index = 1 To Weapons.Count
        .LstWeapons.AddItem "#" & CStr(Weapons(Index).ID) & " - " & Weapons(Index).Name
    Next
    
    ' Display unit picture. '
    HPI.LoadFile
    HPI.ExtractFile "unitpics\" & Name & ".pcx", "temp.pcx"
    HPI.CloseFile
    If LoadPCX("temp.pcx") Then
        CopyMemory .UnitPic.addrBits, PcxBits(0), UBound(PcxBits) + 1
        .UnitPic.Show .PicUnit.hdc, .PicUnit.ScaleWidth / 2 - PcxWidth / 2, 0, PcxWidth, PcxHeight, 0, 0
        .PicUnit.Picture = .PicUnit.Image
    End If
    Kill "temp.pcx"
    End With

Error:
    Screen.MousePointer = vbNormal
End Sub

' Save the unit as a ufo file, and launch the cavedog unit viewer. '
Public Sub ExportToViewer()
    Dim rc As Long
    
    On Error Resume Next
    If Dir(UnitViewPath) = "" Or UnitViewPath = "" Then
        On Error GoTo Error
        MsgBox "Select your TA unit viewer...", vbInformation
        FrmMain.CommonDialog.Filename = ""
        FrmMain.CommonDialog.DefaultExt = "exe"
        FrmMain.CommonDialog.Filter = "Applications (*.exe)|*.exe"
        FrmMain.CommonDialog.ShowOpen
        If FrmMain.CommonDialog.Filename = "" Then GoTo Error
        UnitViewPath = FrmMain.CommonDialog.Filename
        On Error Resume Next
    End If
        
    Screen.MousePointer = vbHourglass
    If Not SaveTestHPI(GetDirectory(UnitViewPath) & "\arm_taum.ufo", "arm") Then Exit Sub
    If Not SaveTestHPI(GetDirectory(UnitViewPath) & "\cor_taum.ufo", "cor") Then Exit Sub
        
    ChDir GetDirectory(UnitViewPath)
    rc = ShellExecute(0, "open", UnitViewPath, "", GetDirectory(UnitViewPath), 10)
Error:
    Screen.MousePointer = vbNormal
End Sub

' Save a test HPI file. '
Public Function SaveTestHPI(Filename As String, Optional Prefix As String = "arm") As Boolean
    Dim rc As Long
    Dim FileHandle As Long
    Dim HPIHandle As Long
    Dim TempFile As String
    
    On Error GoTo Error
    Screen.MousePointer = vbHourglass
    DoEvents
    SaveTestHPI = False
    HPIHandle = HPIOpen(HPI.Filename)
    If HPIHandle = 0 Then Exit Function
    
    ' Extract the 3do file. '
    TempFile = App.Path & "\temp.3do"
    rc = HPIExtractFile(HPIHandle, "objects3d\" & Name & ".3do", TempFile)
    If rc = 0 Then
        rc = HPIClose(HPIHandle)
        Exit Function
    End If
    
    ' Extract the cob file. '
    TempFile = App.Path & "\temp.cob"
    rc = HPIExtractFile(HPIHandle, "scripts\" & Name & ".cob", TempFile)
    If rc = 0 Then
        rc = HPIClose(HPIHandle)
        Exit Function
    End If
    
    ' Extract the fbi file. '
'    TempFile = App.Path & "\temp.fbi"
'    rc = HPIExtractFile(HPIHandle, "units\" & Name & ".fbi", TempFile)
'    If rc = 0 Then
'        rc = HPIClose(HPIHandle)
'        Exit Function
'    End If
    
    rc = HPIClose(HPIHandle)
    
    ' Create an HPI file. '
    FileHandle = HPICreate(Filename, AddressOf HPICallBack)
    If FileHandle = 0 Then Exit Function
    
    If Prefix = "arm" Then
        rc = HPIAddFile(FileHandle, "units\" & Prefix & "TAUM.fbi", App.Path & "\armTAUMunit.fbi")
    Else
        rc = HPIAddFile(FileHandle, "units\" & Prefix & "TAUM.fbi", App.Path & "\corTAUMunit.fbi")
    End If
    If rc = 0 Then Exit Function
    rc = HPIAddFile(FileHandle, "scripts\" & Prefix & "TAUM.cob", App.Path & "\temp.cob")
    If rc = 0 Then Exit Function
    rc = HPIAddFile(FileHandle, "objects3d\" & Prefix & "TAUM.3do", App.Path & "\temp.3do")
    If rc = 0 Then Exit Function
    
    If HPIPackArchive(FileHandle, NO_COMPRESSION) = 0 Then Exit Function

    SaveTestHPI = True
    Kill App.Path & "\temp.fbi"
    Kill App.Path & "\temp.cob"
    Kill App.Path & "\temp.3do"
Error:
End Function

Public Property Get Enabled() As Boolean
    Enabled = HPI.Enabled
End Property

Public Property Let Enabled(Flag As Boolean)
    HPI.Enabled = Flag
End Property

Public Property Get Conflict() As Integer
    Dim Index As Integer, i As Integer, k As Integer
    
    Conflict = (Conflicts.Count > 0)
    For Index = 1 To Weapons.Count
        If Weapons(Index).Conflict Then
            For i = 1 To Weapons(Index).Conflicts.Count
                For k = 1 To Weapons(Index).Conflicts(i).Units.Count
                    If Weapons(Index).Conflicts(i).Units(k).Enabled Then
                        Conflict = True
                    Else
                        Conflict = 2 ' Conflicting unit is disabled, so there is no conflict. '
                    End If
                Next
            Next
        End If
    Next
End Property

' Return a description of the conflicts for this unit. '
Public Function ConflictDescription() As String
    Dim Index As Long, i As Long, k As Long
    Dim Buffer As String
    
    ' Weapon conflicts. '
    For Index = 1 To Weapons.Count
        If Weapons(Index).Conflict Then
            For i = 1 To Weapons(Index).Conflicts.Count
                Buffer = Buffer & "Conflicts with "
                For k = 1 To Weapons(Index).Conflicts(i).Units.Count
                    Buffer = Buffer & Weapons(Index).Conflicts(i).Units(k).Name
                    If k <> Weapons(Index).Conflicts(i).Units.Count Then
                        Buffer = Buffer & " and "
                    End If
                Next
                Buffer = Buffer & " (Weapon " & Weapons(Index).Name & " is conflicting with weapon " & Weapons(Index).Conflicts(i).Name & "). "
            Next
        End If
    Next
    ConflictDescription = Buffer
End Function


