Option Strict Off
Option Explicit On

Imports System.IO

Module App

    'Option Explicit



    Public Const BI_RGB As Short = 0
    Public Const CBM_INIT As Integer = &H4
    Public Const DIB_RGB_COLORS As Short = 0
    Public Const SRCCOPY As Integer = &HCC0020

    Public Structure BITMAPINFOHEADER
        Dim biSize As Integer
        Dim biWidth As Integer
        Dim biHeight As Integer
        Dim biPlanes As Short
        Dim biBitCount As Short
        Dim biCompression As Integer
        Dim biSizeImage As Integer
        Dim biXPelsPerMeter As Integer
        Dim biYPelsPerMeter As Integer
        Dim biClrUsed As Integer
        Dim biClrImportant As Integer
    End Structure

    Public Structure BITMAPINFO_256
        Dim bmiHeader As BITMAPINFOHEADER
        <VBFixedArray(255)> Dim bmiColors() As Integer

        'UPGRADE_TODO: "Initialize" must be called to initialize instances of this structure. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="B4BFF9E0-8631-45CF-910E-62AB3970F27B"'
        Public Sub Initialize()
            ReDim bmiColors(255)
        End Sub
    End Structure
    Public Structure RGBQUAD
        Dim rgbBlue As Byte
        Dim rgbGreen As Byte
        Dim rgbRed As Byte
        Dim rgbReserved As Byte
    End Structure



    'blitting
    '	Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As Integer) As Integer
    '	Public Declare Function StretchBlt Lib "gdi32" (ByVal hDestDC As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal widthSrc As Integer, ByVal heightSrc As Integer, ByVal dwRop As Integer) As Integer
    '	Public Declare Function FloodFill Lib "gdi32" (ByVal hDestDC As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal color As Integer) As Integer
    '	Public Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Integer) As Integer
    '	Public Declare Function FillRgn Lib "gdi32" (ByVal hDestDC As Integer, ByVal hRgn As Integer, ByVal hBr As Integer) As Integer
    'Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    '	Public Declare Function CreateRectRgn Lib "gdi32" (ByVal nLeftRect As Integer, ByVal nTopRect As Integer, ByVal nRightRect As Integer, ByVal nBottomRect As Integer) As Integer


    'creating buffers / loading sprites
    '	Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Integer) As Integer
    '	Public Declare Function GetDC Lib "user32" (ByVal hwnd As Integer) As Integer

    'loading sprites
    '	Public Declare Function SelectObject Lib "gdi32" (ByVal hdc As Integer, ByVal hObject As Integer) As Integer

    'cleanup
    '	Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Integer) As Integer
    '	Public Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Integer) As Integer
    'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
    '	Public Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Integer, ByVal dwCount As Integer, ByRef lpBits As Any) As Integer
    'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
    '	Public Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Integer, ByVal dwCount As Integer, ByRef lpBits As Any) As Integer
    'Public Declare Function SetPixelV Lib "gdi32.dll" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long
    'Public Declare Function SetPixel Lib "gdi32.dll" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long
    '	Public Declare Function GetLastError Lib "kernel32" () As Integer
    'Public Declare Function CreateDIBitmap Lib "gdi32" (ByVal hDC As Long, ByRef lpInfoHeader As Any, ByVal dwUsage As Long, ByRef lpInitBits As Object, ByRef lpInitInfo As Any, ByVal wUsage As Long) As Long
    'Public Declare Function GetDIBColorTable Lib "gdi32" (ByVal hdc As Long, ByVal un1 As Long, ByVal un2 As Long, ByRef pcRGBQuad As Any) As Long
    'Public Declare Function SetDIBColorTable Lib "gdi32" (ByVal hdc As Long, ByVal un1 As Long, ByVal un2 As Long, ByRef pcRGBQuad As Any) As Long
    'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
    '	Public Declare Function GetDIBColorTable Lib "gdi32" (ByVal hdc As Integer, ByVal un1 As Integer, ByVal un2 As Integer, ByRef pRGBQuad As Any) As Integer
    'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
    '	Public Declare Function SetDIBColorTable Lib "gdi32" (ByVal hdc As Integer, ByVal un1 As Integer, ByVal un2 As Integer, ByRef pcRGBQuad As Any) As Integer
    '	Public Declare Function SetPixelV Lib "gdi32.dll" (ByVal hdc As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal crColor As Integer) As Integer
    '	Public Declare Function SetPixel Lib "gdi32.dll" (ByVal hdc As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal crColor As Integer) As Byte
    'UPGRADE_WARNING: Structure BITMAPINFO_256 may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
    '	Public Declare Function SetDIBits Lib "gdi32" (ByVal hdc As Integer, ByVal hBitmap As Integer, ByVal nStartScan As Integer, ByVal nNumScans As Integer, ByRef lpBits As Any, ByRef lpBI As BITMAPINFO_256, ByVal wUsage As Integer) As Integer
    'UPGRADE_WARNING: Structure BITMAPINFO_256 may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
    'UPGRADE_WARNING: Structure BITMAPINFOHEADER may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    '	Public Declare Function CreateDIBitmap Lib "gdi32" (ByVal hdc As Integer, ByRef lpInfoHeader As BITMAPINFOHEADER, ByVal dwUsage As Integer, ByRef lpInitBits As Any, ByRef lpInitInfo As BITMAPINFO_256, ByVal wUsage As Integer) As Integer
    'UPGRADE_WARNING: Structure BITMAPINFO_256 may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    '	Public Declare Function CreateDIBSection Lib "gdi32" (ByVal hdc As Integer, ByRef pBitmapInfo As BITMAPINFO_256, ByVal un As Integer, ByRef lplpVoid As Integer, ByVal handle As Integer, ByVal dw As Integer) As Integer

    'end of copy-paste here...
    Public dlg_progresswindow As New ProgressWindow
    Public dlg_objectlist As New ObjectListForm
    Public dlg_itemtypeeditor As New ItemTypeEditor
    Public dlg_comboeditor As New ComboEditor
    Public dlg_attackeditor As New AttackEditor
    Public dlg_dooreditor As New DoorEditor
    Public dlg_monsterdropeditor As New MonsterDropEditor
    Public dlg_spelleditor As New SpellEditor
    Public dlg_runeeditor As New RuneEditor
    Public dlg_monstereditor As New MonsterEditor
    Public dlg_importmanager As New ImportManager
    Public dlg_graphicsstructure As New GraphicsStructure

    Public dlg_floordecorationgraphicinfoeditor As New FloorDecorationGraphicInfoEditor
    Public dlg_walldecorationgraphicinfoeditor As New WallDecorationGraphicInfoEditor
    Public dlg_doordecorationgraphicinfoeditor As New DoorDecorationGraphicInfoEditor
    Public dlg_buttongraphicinfoeditor As New ButtonGraphicInfoEditor
    Public dlg_buttoneditor As New ButtonEditor
    Public dlg_itemgraphicinfoeditor As New ItemGraphicInfoEditor
    Public dlg_monstergraphicinfoeditor As New MonsterGraphicInfoEditor
    Public dlg_missilegraphicinfoeditor As New MissileGraphicInfoEditor
    Public dlg_flooritemgraphicinfoeditor As New FloorItemGraphicInfoEditor
    Public dlg_creaturegraphicinfoeditor As New CreatureGraphicInfoEditor
    Public dlg_teleporterinfoeditor As New TeleporterEditor
    Public dlg_graphicrectinfoeditor As New GraphicRectInfoEditor
    ' TODO: New editor form put here
    Public dlg_bytes20194editor As New Bytes20194Editor
    Public dlg_ingamepaletteeditor As New IngamePaletteEditor
    Public dlg_ingamepaletteeditorsub As New IngamePaletteEditorSub

    Public graphics As New Tgraphics
    Public mapfile As New Tmapfile
    Public dbg_file As IO.StreamWriter = Nothing
    Dim dbg As Boolean
    Public apppath As String

    Public BitsInBuffer%, LZWByteIndex%, BitBuffer%
    Public itemdata As IO.MemoryStream

    Public JewelIL As New ImageList

    Private CSBWinObjNames(180) As String

    Public Function CSBWinObjName(ByRef num As Short) As String
        CSBWinObjName = CSBWinObjNames(num)
    End Function

    Public Function zerostr(ByVal num As Integer, ByRef count As Integer) As String
        Dim i As Short
        zerostr = Trim(Str(num))
        i = Len(zerostr)
        i = count - Len(zerostr)
        If i > 0 Then
            zerostr = New String("0", count - Len(zerostr)) & zerostr
        End If
    End Function
    Public Function zerohex(ByRef num As Short, ByRef count As Short) As String
        Dim i As Short
        zerohex = Trim(Hex(num))
        i = Len(zerohex)
        i = count - Len(zerohex)
        If i > 0 Then
            zerohex = New String("0", i) & zerohex
        End If
    End Function
    Public Sub InitCSBWinObjNames()

        Dim i As Short

        i = 1
        CSBWinObjNames(i) = "OPEN SCROLL"
        i = i + 1
        CSBWinObjNames(i) = "CHEST"
        i = i + 1
        CSBWinObjNames(i) = "MON POTION(STAMINA)"
        i = i + 1
        CSBWinObjNames(i) = "UM POTION"
        i = i + 1
        CSBWinObjNames(i) = "DES POTION"
        i = i + 1
        CSBWinObjNames(i) = "VEN POTION(POISON)"
        i = i + 1
        CSBWinObjNames(i) = "SAR POTION"
        i = i + 1
        CSBWinObjNames(i) = "ZO POTION"
        i = i + 1
        CSBWinObjNames(i) = "ROS POTION(DEXTERITY)"
        i = i + 1
        CSBWinObjNames(i) = "KU POTION(STRENGTH)"
        i = i + 1
        CSBWinObjNames(i) = "DANE POTION(WISDOM)"
        i = i + 1
        CSBWinObjNames(i) = "NETA POTION(VITALITY"
        i = i + 1
        CSBWinObjNames(i) = "ANTIVENIN(CURE POISON)"
        i = i + 1
        CSBWinObjNames(i) = "MON POTION(STAMINA)"
        i = i + 1
        CSBWinObjNames(i) = "YA POTION(MAGIC SHIELD)"
        i = i + 1
        CSBWinObjNames(i) = "EE POTION(MANA)"
        i = i + 1
        CSBWinObjNames(i) = "VI POTION(HEALTH)"
        i = i + 1
        CSBWinObjNames(i) = "WATER FLASK"
        i = i + 1
        CSBWinObjNames(i) = "KATH BOMB"
        i = i + 1
        CSBWinObjNames(i) = "PEW BOMB"
        i = i + 1
        CSBWinObjNames(i) = "RA BOMB"
        i = i + 1
        CSBWinObjNames(i) = "FUL BOMB"
        i = i + 1
        CSBWinObjNames(i) = "EMPTY FLASK"
        i = i + 1
        CSBWinObjNames(i) = "EYE OF TIME"
        i = i + 1
        CSBWinObjNames(i) = "STORMRING"
        i = i + 1
        CSBWinObjNames(i) = "TORCH"
        i = i + 1
        CSBWinObjNames(i) = "FLAMITT"
        i = i + 1
        CSBWinObjNames(i) = "STAFF OF CLAWS"
        i = i + 1
        CSBWinObjNames(i) = "STORM"
        i = i + 1
        CSBWinObjNames(i) = "RA BLADE"
        i = i + 1
        CSBWinObjNames(i) = "THE FIRESTAFF"
        i = i + 1
        CSBWinObjNames(i) = "DAGGER"
        i = i + 1
        CSBWinObjNames(i) = "FALCHION"
        i = i + 1
        CSBWinObjNames(i) = "SWORD"
        i = i + 1
        CSBWinObjNames(i) = "RAPIER"
        i = i + 1
        CSBWinObjNames(i) = "BITER"
        i = i + 1
        CSBWinObjNames(i) = "SAMURAI SWORD"
        i = i + 1
        CSBWinObjNames(i) = "SIDE SPLITTER"
        i = i + 1
        CSBWinObjNames(i) = "DIAMOND EDGE"
        i = i + 1
        CSBWinObjNames(i) = "VORPAL BLADE"
        i = i + 1
        CSBWinObjNames(i) = "DRAGON FANG"
        i = i + 1
        CSBWinObjNames(i) = "AXE"
        i = i + 1
        CSBWinObjNames(i) = "EXECUTIONER"
        i = i + 1
        CSBWinObjNames(i) = "MACE"
        i = i + 1
        CSBWinObjNames(i) = "MACE OF ORDER"
        i = i + 1
        CSBWinObjNames(i) = "MORNINGSTAR"
        i = i + 1
        CSBWinObjNames(i) = "CLUB"
        i = i + 1
        CSBWinObjNames(i) = "STONE CLUB"
        i = i + 1
        CSBWinObjNames(i) = "CLAW BOW"
        i = i + 1
        CSBWinObjNames(i) = "CROSSBOW"
        i = i + 1
        CSBWinObjNames(i) = "ARROW"
        i = i + 1
        CSBWinObjNames(i) = "SLAYER"
        i = i + 1
        CSBWinObjNames(i) = "SLING"
        i = i + 1
        CSBWinObjNames(i) = "ROCK"
        i = i + 1
        CSBWinObjNames(i) = "POISON DART"
        i = i + 1
        CSBWinObjNames(i) = "THROWING STAR"
        i = i + 1
        CSBWinObjNames(i) = "STICK"
        i = i + 1
        CSBWinObjNames(i) = "STAFF"
        i = i + 1
        CSBWinObjNames(i) = "WAND"
        i = i + 1
        CSBWinObjNames(i) = "TEOWAND"
        i = i + 1
        CSBWinObjNames(i) = "YEW STAFF"
        i = i + 1
        CSBWinObjNames(i) = "STAFF OF IRRA"
        i = i + 1
        CSBWinObjNames(i) = "CROSS OF NETA"
        i = i + 1
        CSBWinObjNames(i) = "SERPENT STAFF"
        i = i + 1
        CSBWinObjNames(i) = "DRAGON SPIT"
        i = i + 1
        CSBWinObjNames(i) = "SCEPTRE OF LYF"
        i = i + 1
        CSBWinObjNames(i) = "HORN OF FEAR"
        i = i + 1
        CSBWinObjNames(i) = "SPEEDBOW"
        i = i + 1
        CSBWinObjNames(i) = "THE FIRESTAFF+"
        i = i + 1
        CSBWinObjNames(i) = "CAPE"
        i = i + 1
        CSBWinObjNames(i) = "CLOAK OF NIGHT"
        i = i + 1
        CSBWinObjNames(i) = "TATTERED PANTS"
        i = i + 1
        CSBWinObjNames(i) = "SANDALS"
        i = i + 1
        CSBWinObjNames(i) = "LEATHER BOOTS"
        i = i + 1
        CSBWinObjNames(i) = "TATTERED SHIRT"
        i = i + 1
        CSBWinObjNames(i) = "ROBE"
        i = i + 1
        CSBWinObjNames(i) = "FINE ROBE(TOP)"
        i = i + 1
        CSBWinObjNames(i) = "FINE ROBE(BOTTOM)"
        i = i + 1
        CSBWinObjNames(i) = "KIRTLE"
        i = i + 1
        CSBWinObjNames(i) = "SILK SHIRT"
        i = i + 1
        CSBWinObjNames(i) = "TABARD"
        i = i + 1
        CSBWinObjNames(i) = "GUNNA"
        i = i + 1
        CSBWinObjNames(i) = "ELVEN DOUBLET"
        i = i + 1
        CSBWinObjNames(i) = "ELVEN HUKE"
        i = i + 1
        CSBWinObjNames(i) = "ELVEN BOOTS"
        i = i + 1
        CSBWinObjNames(i) = "LEATHER JERKIN"
        i = i + 1
        CSBWinObjNames(i) = "LEATHER PANTS"
        i = i + 1
        CSBWinObjNames(i) = "SUEDE BOOTS"
        i = i + 1
        CSBWinObjNames(i) = "BLUE PANTS"
        i = i + 1
        CSBWinObjNames(i) = "TUNIC"
        i = i + 1
        CSBWinObjNames(i) = "GHI"
        i = i + 1
        CSBWinObjNames(i) = "GHI TROUSERS"
        i = i + 1
        CSBWinObjNames(i) = "CALISTA"
        i = i + 1
        CSBWinObjNames(i) = "CROWN OF NERRA"
        i = i + 1
        CSBWinObjNames(i) = "BEZERKER HELM"
        i = i + 1
        CSBWinObjNames(i) = "HELMET"
        i = i + 1
        CSBWinObjNames(i) = "BASINET"
        i = i + 1
        CSBWinObjNames(i) = "NETA SHIELD"
        i = i + 1
        CSBWinObjNames(i) = "CRYSTAL SHIELD"
        i = i + 1
        CSBWinObjNames(i) = "WOODEN SHIELD"
        i = i + 1
        CSBWinObjNames(i) = "SMALL SHIELD"
        i = i + 1
        CSBWinObjNames(i) = "MAIL AKETON"
        i = i + 1
        CSBWinObjNames(i) = "LEG MAIL"
        i = i + 1
        CSBWinObjNames(i) = "MITHRAL AKETON"
        i = i + 1
        CSBWinObjNames(i) = "MITHRAL MAIL"
        i = i + 1
        CSBWinObjNames(i) = "CASQUE 'N COIF"
        i = i + 1
        CSBWinObjNames(i) = "HOSEN"
        i = i + 1
        CSBWinObjNames(i) = "ARMET"
        i = i + 1
        CSBWinObjNames(i) = "TORSO PLATE"
        i = i + 1
        CSBWinObjNames(i) = "LEG PLATE"
        i = i + 1
        CSBWinObjNames(i) = "FOOT PLATE"
        i = i + 1
        CSBWinObjNames(i) = "SAR SHIELD"
        i = i + 1
        CSBWinObjNames(i) = "HELM OF RA"
        i = i + 1
        CSBWinObjNames(i) = "PLATE OF RA"
        i = i + 1
        CSBWinObjNames(i) = "POLEYN OF RA"
        i = i + 1
        CSBWinObjNames(i) = "GREAVE OF RA"
        i = i + 1
        CSBWinObjNames(i) = "SHIELD OF RA"
        i = i + 1
        CSBWinObjNames(i) = "DRAGON HELM"
        i = i + 1
        CSBWinObjNames(i) = "DRAGON PLATE"
        i = i + 1
        CSBWinObjNames(i) = "DRAGON POLEYN"
        i = i + 1
        CSBWinObjNames(i) = "DRAGON GREAVE"
        i = i + 1
        CSBWinObjNames(i) = "DRAGON SHIELD"
        i = i + 1
        CSBWinObjNames(i) = "DEXHELM"
        i = i + 1
        CSBWinObjNames(i) = "FLAMEBAIN"
        i = i + 1
        CSBWinObjNames(i) = "POWERTOWERS"
        i = i + 1
        CSBWinObjNames(i) = "BOOTS OF SPEED"
        i = i + 1
        CSBWinObjNames(i) = "HALTER"
        i = i + 1
        CSBWinObjNames(i) = "COMPASS"
        i = i + 1
        CSBWinObjNames(i) = "WATERSKIN"
        i = i + 1
        CSBWinObjNames(i) = "JEWEL SYMAL"
        i = i + 1
        CSBWinObjNames(i) = "ILLUMULET"
        i = i + 1
        CSBWinObjNames(i) = "ASHES"
        i = i + 1
        CSBWinObjNames(i) = "BONES(PARTY)"
        i = i + 1
        CSBWinObjNames(i) = "SAR COIN"
        i = i + 1
        CSBWinObjNames(i) = "SILVER COIN"
        i = i + 1
        CSBWinObjNames(i) = "GOR COIN"
        i = i + 1
        CSBWinObjNames(i) = "IRON KEY"
        i = i + 1
        CSBWinObjNames(i) = "KEY OF B"
        i = i + 1
        CSBWinObjNames(i) = "SOLID KEY"
        i = i + 1
        CSBWinObjNames(i) = "SQUARE KEY"
        i = i + 1
        CSBWinObjNames(i) = "TOURQUOISE KEY"
        i = i + 1
        CSBWinObjNames(i) = "CROSS KEY"
        i = i + 1
        CSBWinObjNames(i) = "ONYX KEY"
        i = i + 1
        CSBWinObjNames(i) = "SKELETON KEY"
        i = i + 1
        CSBWinObjNames(i) = "GOLD KEY"
        i = i + 1
        CSBWinObjNames(i) = "WINGED KEY"
        i = i + 1
        CSBWinObjNames(i) = "TOPAZ KEY"
        i = i + 1
        CSBWinObjNames(i) = "SAPPHIRE KEY"
        i = i + 1
        CSBWinObjNames(i) = "EMERALD KEY"
        i = i + 1
        CSBWinObjNames(i) = "RUBY KEY"
        i = i + 1
        CSBWinObjNames(i) = "RA KEY"
        i = i + 1
        CSBWinObjNames(i) = "MASTER KEY"
        i = i + 1
        CSBWinObjNames(i) = "BOULDER"
        i = i + 1
        CSBWinObjNames(i) = "BLUE GEM"
        i = i + 1
        CSBWinObjNames(i) = "ORANGE GEM"
        i = i + 1
        CSBWinObjNames(i) = "GREEN GEM"
        i = i + 1
        CSBWinObjNames(i) = "APPLE"
        i = i + 1
        CSBWinObjNames(i) = "CORN"
        i = i + 1
        CSBWinObjNames(i) = "BREAD"
        i = i + 1
        CSBWinObjNames(i) = "CHEESE"
        i = i + 1
        CSBWinObjNames(i) = "SCREAMER SLICE"
        i = i + 1
        CSBWinObjNames(i) = "WORM ROUND"
        i = i + 1
        CSBWinObjNames(i) = "SHANK"
        i = i + 1
        CSBWinObjNames(i) = "DRAGON STEAK"
        i = i + 1
        CSBWinObjNames(i) = "GEM OF AGES"
        i = i + 1
        CSBWinObjNames(i) = "EKKHARD CROSS"
        i = i + 1
        CSBWinObjNames(i) = "MOONSTONE"
        i = i + 1
        CSBWinObjNames(i) = "THE HELLION"
        i = i + 1
        CSBWinObjNames(i) = "PENDANT FERAL"
        i = i + 1
        CSBWinObjNames(i) = "MAGIC BOX(BLUE)"
        i = i + 1
        CSBWinObjNames(i) = "MAGIC BOX(GREEN)"
        i = i + 1
        CSBWinObjNames(i) = "MIRROR OF DAWN"
        i = i + 1
        CSBWinObjNames(i) = "ROPE"
        i = i + 1
        CSBWinObjNames(i) = "RABBIT'S FOOT"
        i = i + 1
        CSBWinObjNames(i) = "CORBUM"
        i = i + 1
        CSBWinObjNames(i) = "CHOKER"
        i = i + 1
        CSBWinObjNames(i) = "LOCK PICKS"
        i = i + 1
        CSBWinObjNames(i) = "MAGNIFIER"
        i = i + 1
        CSBWinObjNames(i) = "ZOKATHRA SPELL"
        i = i + 1
        CSBWinObjNames(i) = "BONES"

    End Sub

    Public Function EFChange16(ByRef ctrl As System.Windows.Forms.TextBox, ByRef low As Integer, ByRef high As Integer) As Integer
        EFChange16 = Val(ctrl.Text)
        If EFChange16 > high Then
            EFChange16 = high
        End If
        If EFChange16 < low Then
            EFChange16 = low
        End If

        If Str(EFChange16) <> ctrl.Text Then
            ctrl.Text = Str(EFChange16)
            ctrl.SelectionStart = 150
            ctrl.SelectionLength = 2
        End If

    End Function
    Public Function EFChange8(ByRef ctrl As System.Windows.Forms.TextBox, ByRef low As Integer, ByRef high As Integer) As Short
        EFChange8 = Val(ctrl.Text)
        If EFChange8 > high Then
            EFChange8 = high
        End If
        If EFChange8 < low Then
            EFChange8 = low
        End If

        If low < 0 Then
            If ctrl.Text = "-" Then
                Exit Function
            End If
        End If
        If Str(EFChange8) <> ctrl.Text Then
            ctrl.Text = Str(EFChange8)
            ctrl.SelectionStart = 150
            ctrl.SelectionLength = 2
        End If

    End Function
    Public Sub Main()
#If False Then
        MsgBox( _
            "My.Application.Info.DirectoryPath:" & vbCrLf & My.Application.Info.DirectoryPath & vbCrLf & vbCrLf & _
            "Application.CommonAppDataPath:" & vbCrLf & Application.CommonAppDataPath & vbCrLf & vbCrLf & _
            "Application.ExecutablePath:" & vbCrLf & Application.ExecutablePath & vbCrLf & vbCrLf & _
            "Application.LocalUserAppDataPath:" & vbCrLf & Application.LocalUserAppDataPath & vbCrLf & vbCrLf & _
            "Application.StartupPath:" & vbCrLf & Application.StartupPath & vbCrLf & vbCrLf & _
            "Application.UserAppDataPath:" & vbCrLf & Application.UserAppDataPath & vbCrLf & vbCrLf & _
            "AppDomain.CurrentDomain.BaseDirectory:" & vbCrLf & AppDomain.CurrentDomain.BaseDirectory & vbCrLf & vbCrLf & _
            "")
#End If

        apppath = My.Application.Info.DirectoryPath
        dbg = False
        debug_init()
        dprint(("debug file initialized"))
        Dim window As MainWindow= New MainWindow

        InitCSBWinObjNames()

        JewelIL.ImageSize = New Size(17, 17)
        JewelIL.Images.Add("BlueGem", My.Resources.BlueGem)
        JewelIL.Images.Add("Topaz", My.Resources.Topaz)
        JewelIL.Images.Add("Emerald", My.Resources.Emerald)
        JewelIL.Images.Add("EyeOfTime", My.Resources.EyeOfTime)

        System.Windows.Forms.Application.EnableVisualStyles()
        System.Windows.Forms.Application.Run(window)
    End Sub

    Public Sub debug_deinit()
        If dbg = False Then
            Exit Sub
        End If

        'UPGRADE_WARNING: Couldn't resolve default property of object dbg_file.Close. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        dbg_file.Close()
    End Sub
    Public Sub debug_init()
        If dbg = False Then
            Exit Sub
        End If

        'Dim fs As Object
        '
        'fs = CreateObject("Scripting.FileSystemObject")
        'If fs.FileExists("debug.txt") Then
        '    Set f = fs.GetFile("debug.txt")
        '    f.Delete
        'End If

        'UPGRADE_WARNING: Couldn't resolve default property of object fs.CreateTextFile. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        dbg_file = New IO.StreamWriter(App.apppath & "\debug.txt", False, System.Text.Encoding.Default)
    End Sub

    Public Sub dprint(ByVal s As String)

        If dbg = False Then
            Exit Sub
        End If

        'UPGRADE_WARNING: Couldn't resolve default property of object dbg_file.WriteLine. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        dbg_file.WriteLine(s)
        dbg_file.Flush()

    End Sub

    Function GetNextLZWCode(ByVal n As Integer) As Integer
        While (BitsInBuffer < n) And (LZWByteIndex < itemdata.Length) ' If there are enough bits in the buffer to make a code
            itemdata.Position = LZWByteIndex
            BitBuffer = BitBuffer + (itemdata.ReadByte() * 2 ^ BitsInBuffer) ' Add the next character to the left of the BitBuffer (hence the shift to the left)
            BitsInBuffer = BitsInBuffer + 8             ' 8 bits were added to the buffer
            LZWByteIndex = LZWByteIndex + 1             ' Go to the next input character
        End While
        'MsgBox     "BitsInBuffer: " & BitsInBuffer & vbCrLf & _
        '   "n: " & n & vbCrLf & _
        '   "LZWByteIndex: " & LZWByteIndex & vbCrLf & _
        '   "Len(ItemData): " & Len(ItemData) & vbCrLf & _
        '   "BitBuffer: " & BitBuffer

        If BitsInBuffer >= n Then               ' If there are enough bits in the buffer to make a code
            GetNextLZWCode = BitBuffer And (2 ^ n - 1)  ' Add to the list of codes the code made with the BitsInCode most significant bits in the buffer
            BitBuffer = BitBuffer \ (2 ^ n)         ' Remove the output bits from the buffer by shifting the buffer to the right
            BitsInBuffer = BitsInBuffer - n     ' The number of bits in the buffer is decreased
        Else
            GetNextLZWCode = -1
        End If
        'MsgBox "NextLZWCode Is: " & GetNextLZWCode
    End Function

    Function LZWUncompress() As IO.MemoryStream
        Dim s As String = LZWUncompress_Stro()
        Dim w As New IO.MemoryStream
        Dim x As Integer
        For x = 0 To s.Length - 1
            w.WriteByte(AscW(s(x)))
        Next
        ' Move file pointer to begin of file!
        w.Position = 0
        LZWUncompress = w
    End Function

    Function LZWUncompress_Stro() As String
        Dim Dictionary As New Generic.SortedDictionary(Of Integer, String)
        Dim i As Integer, j As String, k As Integer, l As String
        Dim LZWString As String, LZWChar As String, BitsInCode As Integer, OldCode As Integer, NewCode As Integer
        Dim a As String, b As String, c As String
        Dim d As Integer, x As Integer
        ' Initialize LZW bit buffer
        BitBuffer = 0           ' The BitBuffer is empty
        BitsInBuffer = 0        ' So there are no bits stored in the buffer
        LZWByteIndex = 0        ' Start uncompressing LZW data at the first character in string
        BitsInCode = 9
        d = 0

        LZWUncompress_Stro = ""
        If itemdata.Length > 1 Then       ' We need at least one 9 bits code, which means at least two bytes
            ' Create and populate the dictionary for the LZW algorithm
            For i = 0 To 255
                Dictionary.Add(Dictionary.Count, ChrW(i))
            Next
            Dictionary.Add(256, "Flush Dictionary")
            d = Dictionary.Count
            OldCode = GetNextLZWCode(BitsInCode)                ' Get the first code
            LZWUncompress_Stro = Dictionary.Item(OldCode)            ' Output the string (which is a character) associated with the first code
            LZWChar = Dictionary.Item(OldCode)              ' Get the string associated with the first code

            NewCode = GetNextLZWCode(BitsInCode)
            While NewCode <> -1
                If NewCode <> 256 Then                  ' If the code is not the special value 256
                    If Dictionary.ContainsKey(NewCode) Then      ' If the current code is already in the dictionary
                        LZWString = Dictionary.Item(NewCode)    ' Get the string associated with that current code
                    Else
                        LZWString = Dictionary.Item(OldCode)    ' Get the string associated with the previously read code
                        LZWString = LZWString & LZWChar     ' Add the latest input character read to the string
                    End If
                    LZWUncompress_Stro = LZWUncompress_Stro + LZWString   ' Output the string
                    LZWChar = Left(LZWString, 1)            ' Get the first character of the string
                    Dictionary.Add(d, (Dictionary.Item(OldCode) + LZWChar))  ' Add the new string to the dictionary
                    d = d + 1
                    If BitsInCode < 12 Then
                        If d = 2 ^ BitsInCode Then   ' If there are not anymore enough bits in a code to store the code's value
                            BitsInCode = BitsInCode + 1     ' Add a bit to the code length
                            'MsgBox "BitsInCode increased and is now: " & BitsInCode
                        End If
                    End If

                    OldCode = NewCode               ' Go on to next code
                Else
                    'MsgBox "Code 256 found, flushing dictionary...", vbOKOnly + vbInformation, "Information"
                    Dictionary.Clear()
                    For i = 0 To 255
                        Dictionary.Add(Dictionary.Count, ChrW(i))
                    Next
                    Dictionary.Add(256, "Flush Dictionary")
                    d = Dictionary.Count
                    BitsInCode = 9
                End If
                NewCode = GetNextLZWCode(BitsInCode)
            End While

            i = 1
            While i < LZWUncompress_Stro.Length
                j = Mid(LZWUncompress_Stro, i, 1)
                If AscW(j) = 144 Then
                    App.dprint("found 0x90 byte")
                    k = AscW(Mid(LZWUncompress_Stro, i + 1, 1))
                    If k = 0 Then
                        App.dprint("item in lzw if k=0 statement")
                        ' Remove the '00h' byte
                        'LZWUncompress_Stro = LeftB(LZWUncompress_Stro, i) + MidB(LZWUncompress_Stro, i + 2)
                        LZWUncompress_Stro = Left(LZWUncompress_Stro, i) + Right(LZWUncompress_Stro, Len(LZWUncompress_Stro) - i - 1)
                        i = i + 1
                    Else
                        App.dprint("item in lzw else statement (k=" + Str(k) + ")")
                        l = Mid(LZWUncompress_Stro, i - 1, 1)
                        a = Left(LZWUncompress_Stro, i - 1)
                        b = ""
                        For x = 1 To k - 1
                            b = b & l
                        Next
                        If Len(b) <> k - 1 Then
                            MsgBox("lenb(b) != k-1 (" + Str(Len(b)) + " != " + Str(k - 1) + ")")
                        End If
                        'b = String(k - 1, AscB(ChrB(l)))
                        c = Right(LZWUncompress_Stro, Len(LZWUncompress_Stro) - i - 1)
                        'c = MidB(LZWUncompress_Stro, i + 2)
                        LZWUncompress_Stro = a & b & c
                        i = i + k - 1
                    End If
                Else
                    i = i + 1
                End If
            End While
        End If
    End Function



    'gread = read from a file, specified as fnum
    'gwrite = write to a file, specified as fnum
    'gsread = read from a string?
    'gswrite = write to a string?

    Function gread_bytes(ByVal length As Integer, ByVal fnum As Stream) As Byte()
        'UPGRADE_ISSUE: InputB function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        gread_bytes = New BinaryReader(fnum).ReadBytes(length)
    End Function
    Function gread_byte(ByVal fnum As Stream) As Byte
        'UPGRADE_ISSUE: InputB function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'UPGRADE_ISSUE: AscB function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        gread_byte = New BinaryReader(fnum).ReadByte()
    End Function

    Function gread_int(ByVal fnum As Stream) As Integer
        Dim b1 As Byte
        Dim b2 As Byte
        b2 = gread_byte(fnum)
        'UPGRADE_WARNING: Couldn't resolve default property of object b1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        b1 = gread_byte(fnum)

        gread_int = b2
        gread_int = gread_int * 256
        'UPGRADE_WARNING: Couldn't resolve default property of object b1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        gread_int = gread_int + b1
    End Function
End Module