Option Strict On
Option Explicit On

' Thank you: CREDITS
'
' Technical Documentation - Graphics.dat Item 562
' http://dmweb.free.fr/?q=node/1398

Friend Class file562
	
	'  i8  Byte1830[2];  // Start of graphic 0x232
	'0a 00, linefeed
	
	'  i32 Long1828[4]; // masks for text !not reversed!
	'  i32 Long1812[4]; // masks for text !not reversed!
	
	'  RectPos wRectPos1796;   // ... bottom of screen rect
	'  RectPos wRectPos1788;   // ... right side of screen rect
	'  RectPos wRectPos1780;   // ... top of screen rect
	
	'  SOUND sound1772[22]; //        ...
	' word[0-1] ... graphic address
	' byte[2] ... unused
	' byte[3] ... Timer type, always 0x70 (now unused)
	' byte[4] ... length of time sound exists
	' byte[5] ... unused
	' byte[6] ...  distance sound will travel as 'loud'
	' byte[7] ...  distance sound will travel as 'soft'
	
	'  i8  Byte1596[8]; 'rooms timertype function when receiving a 'switch'
	
	'  i16 Word1588;      // unused
	
	'  i16 DropOrder[30];//1586 //Order in which possessions are dropped
	
	'  i16 Word1526[4][3];   // offset? of each lifebar per character (where to draw)
	'  i16 Word1502[4][6];   // lifebar colors or widths maybe...
	
	'  RectPos wRectPos1454[4];  // champion facings rects
	
	'  i8  SpecialChars[6]; //1422 comma,period,semi-colon,colon,space, for wall texts
	
	'  i8  Byte1416[2]; // max number of cahracters to print while reincarnating?
	'  i8  Byte1414[2]; // .... same
	'  i8  Byte1412[6]; // resistances to ouches (getting hurt...) :P
	
	'  i16 Word1406[4];   // eye rectangle
	'  i16 Word1398[4];   // mouth rectangle
	'  i8  Byte1390[4];   // character portrait rect
	'  i8  Byte1386[4];   // character name colors
	'  i8  Byte1382[16];  // object in hand/cursor palette
	'  i8  Byte1366[16];  // object in hand/cursor palette
	
	'  i8  Byte1350[128]; // hand cursor? (actual graphic!)
	'  i8  Byte1222[128]; // Arrow cursor... (actual graphic!)
	
	'  i8  Byte1094[8];   // black area rect, assuming sleeping/gamefrozen stuff.
	
	'  i16 PaletteBrightness[6];//1086;   // swapped when read
	'                     // Value of Brightness for each palette.
	'         0 1 2 3 4 5 would be at a brightness level of 4, use the 5th palette...
	
	'  i16 Word1074[16];  // swapped when read       ...
	'                     // [2] added to 13282 when we put a
	'                     // Illumulet necklace on a character
	'    [2] added to brightness upon atk_Light
	'    seems to be word1074[torches-charges] = light added when held
	
	'  i16 CarryLocation[38];//1042 //swapped when read
	'                     // Word for each location that
	'                     // an object can be carried.
	'                     // See HandleClothingClick for a
	'                     // list of the locations and their
	'                     // numbers.  For example, not all
	'                     // objects can be put in all locations.
	'                     // You cannot put shoes on your head!
	'                     // There are 30 locations for body and
	'                     // backpack and 8 for the chest.
	'  RectPos wRectPos966;// poisoned draw rect box (static 48x12);
	'  RectPos wRectPos958;// water (24x12) 12 = transparent pixel?
	'  RectPos wRectPos950;// food (24x12) 12 = transparent pixel?
	'  RectPos wRectPos942;// item circle (16x12) 12 = transparent pixel?
	'  RectPos wRectPos934;// arrow/eye (8x8) 8 = transparent pixel?
	'  RectPos wRectPos926;// empty inventory box rect: 8 = transparent pixel?
	'  i16 Unused918;//Word918;        //
	'  ICONDISPLAY IconDisplay[46]; //916// swapped when read
	'                      // 46 entries.
	'                      //   8 for hands in portraits
	'                      //  30 for clothing/backpack
	'                      //   8 for chest
	'                      // 'struct { int pixelx, pixely, objecttype }
	'                      // pixelx = x coordinate, y = y coordinate,
	'                      // put a box around it automatically.
	'                      // objecttype = the image drawn while no item is there.
	
	'  i8  Byte640[16];     //  offset of torch graphic for x charges remaining
	
	'  RectPos wRectPos624;// held object name rect (for clearing only)
	'  FILL(616,612)       // another entry for word612?
	'  i16 Word612[7];     // Swapped when read
	'                      // The various objects in the game
	'                      // have bitmaps grouped in larger
	'                      // graphics.  This array tells the
	'                      // first OBJECTTYPE in each of the
	'                      // seven graphics.
	'  FILL(598,590)        // another entry for word612?
	
	'  ui8 uByte590[4];    // critical hit table, contains a mask for 'ouches'
	' A monsters word22 has direct relation to this.  If a monster hits you, it
	' has a chance to do 'ouches' from this table.  it takes a nibble off the
	' least significant side of word22, if it is less than a random(0-15),
	' it landed a crit and uss the mask of ubyte590[0].  It then takes another nibble
	' and tests if that is less than the same random(0-15) as before.  If it is
	' still less than it, it uses the mask of ubyte590[1], and moves all the way up
	' to ubyte590[3].  monsters with low values for the least significant nibbles in
	' word22 will do 'deep' crits which I assume deal many ouches.  This explains why
	' a single hit from a dragon almost is a guaranteed ouch =]
	
	'  i8  Byte586[8][4];// something to do with monsters hitting you from a
	'                   //direction your not facing and how to reposition your players
	'  i16 Word554;       // Swapped when read       ... if its 555 it stops some timers
	'  PALETTE Palette552[6]; // Swapped when read  ... we'll see
	'//  i8  Byte520[160];  //        ...
	'  PALETTE Palette360;   // Palette...// Swapped when read from file
	'  PALETTE Palette328;   // Palette...// Swapped when read from file
	'  ui16 DefaultGraphicList[70];  //  array of graphic numbers that are manditory at all times     ...
	'  i8  Byte156[16];   // special lightning bolt/fireball/poison/dispel palette?
	'  i16 Word140[4];    // rect of first character's graphic on screen,
	' +48 for each additional to the x coordinates. (note 1 = transparency)
	'  i16 Word132[4];    // rect of first character's death graphic.  +48 again.
	'note, 10 = transparent
	'  i16 Word124[4];    // some rect for 'restarting' the game, cleared when used
	'  i16 Word116[4];    // some rect for 'restarting' the game, cleared when used
	'  i16 Word108[4];    // "THE END" label rect (for graphic 6)
	'  i16 Word100[4];    // entrance door (right) rect
	'  i16 Word92[4];     // entrance door (left) rect
	'  i16 Word84[4];     // saved rect from before opening doors (saves a quick ss of this rect)
	'  i16 Word76[4];     // modifyable entrance door (right) rect
	'  i16 Word68[4];     // modifyable entrance door (left) rect (moves it as u open)
	'  i16 Word60[4];     // left part of interior rect as you open doors
	'  i16 Word52[4];     // intro graphic rect to delay display (its around "presents");
	'  i16 Word44[4];     // into graphic rect to delay display ("presents" again???)
	'  i16 Word36[4];     // into graphic rect for "stikes back"
	'  i16 Word28[4];     // movement arrow key rects
	'  i16 Word20[4];     // attack damage rect
	'  i16 Word12[4];     // magic spell area rect
    '                     // End of graphic 0x232

    ' They're 1 BASED ARRAY! ->
    Public Bytes1830(2) As Byte
    Public Long1828(4) As Integer
    Public Long1812(4) As Integer
    Public wRectPos1796 As RectPos
    Public wRectPos1788 As RectPos
    Public wRectPos1780 As RectPos
    Public sound1772(22) As SOUND
    Public Byte1596(8) As Byte
    Public Word1588 As Short
    Public DropOrder(30) As Short
    Public Word1526(4, 3) As Short
    Public Word1502(4, 6) As Short
    Public wRectPos1454(4) As RectPos
    Public SpecialChars(6) As Byte
    Public Byte1416(2) As Byte
    Public Byte1414(2) As Byte
    Public Byte1412(6) As Byte
    Public Word1406(4) As Short
    Public Word1398(4) As Short
    Public Byte1390(4) As Byte
    Public Byte1386(4) As Byte
    Public Byte1382(16) As Byte
    Public Byte1366(16) As Byte
    Public Byte1350(128) As Byte
    Public Byte1222(128) As Byte
    Public Byte1094(8) As Byte
    Public PaletteBrightness(6) As Short
    Public Word1074(16) As Short
    Public CarryLocation(38) As Short
    Public wRectPos966 As RectPos
    Public wRectPos958 As RectPos
    Public wRectPos950 As RectPos
    Public wRectPos942 As RectPos
    Public wRectPos934 As RectPos
    Public wRectPos926 As RectPos
    Public Unused918 As Short
    Public IconDisplay(46) As ICONDISPLAY
    Public Byte640 As Byte
    Public Reserved639(15) As Byte
    Public wRectPos624 As RectPos
    Public Reserved616(4) As Byte
    Public Word612(7) As Short
    Public Reserved598(8) As Byte
    Public uByte590(4) As Byte
    Public Byte586(8, 4) As Byte
    Public Word554 As Short
    Public Palette552(6) As PALETTE
    Public Palette360 As PALETTE
    Public Palette328 As PALETTE
    Public DefaultGraphicList(70) As Short
    Public Byte156(16) As Byte
    Public Word140(4) As Short
    Public Word132(4) As Short
    Public Word124(4) As Short
    Public Word116(4) As Short
    Public Word108(4) As Short
    Public Word100(4) As Short
    Public Word92(4) As Short
    Public Word84(4) As Short
    Public Word76(4) As Short
    Public Word68(4) As Short
    Public Word60(4) As Short
    Public Word52(4) As Short
    Public Word44(4) As Short
    Public Word36(4) As Short
    Public Word28(4) As Short
    Public Word20(4) As Short
    Public Word12(4) As Short
    ' <- They're 1 BASED ARRAY!

    Public itemdata As IO.MemoryStream

    Public Sub read()
        Dim i As Short, j As Short
        Dim br As CSBinaryReader = New CSBinaryReader(itemdata)

        For i = 1 To 2
            Bytes1830(i) = br.ReadByte()
        Next
        For i = 1 To 4
            Long1828(i) = br.ReadInt32()
        Next
        For i = 1 To 4
            Long1812(i) = br.ReadInt32()
        Next
        wRectPos1796 = br.ReadRectPos()
        wRectPos1788 = br.ReadRectPos()
        wRectPos1780 = br.ReadRectPos()
        For i = 1 To 22
            sound1772(i) = br.ReadSOUND()
        Next
        For i = 1 To 8
            Byte1596(i) = br.ReadByte()
        Next
        Word1588 = br.ReadInt16()
        For i = 1 To 30
            DropOrder(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            For j = 1 To 3
                Word1526(i, j) = br.ReadInt16()
            Next
        Next
        For i = 1 To 4
            For j = 1 To 6
                Word1502(i, j) = br.ReadInt16()
            Next
        Next
        For i = 1 To 4
            wRectPos1454(i) = br.ReadRectPos()
        Next
        For i = 1 To 6
            SpecialChars(i) = br.ReadByte()
        Next
        Byte1416(1) = br.ReadByte()
        Byte1416(2) = br.ReadByte()

        Byte1414(1) = br.ReadByte()
        Byte1414(2) = br.ReadByte()

        For i = 1 To 6
            Byte1412(i) = br.ReadByte()
        Next
        For i = 1 To 4
            Word1406(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word1398(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Byte1390(i) = br.ReadByte()
        Next
        For i = 1 To 4
            Byte1386(i) = br.ReadByte()
        Next
        For i = 1 To 16
            Byte1382(i) = br.ReadByte()
        Next
        For i = 1 To 16
            Byte1366(i) = br.ReadByte()
        Next
        For i = 1 To 128
            Byte1350(i) = br.ReadByte()
        Next
        For i = 1 To 128
            Byte1222(i) = br.ReadByte()
        Next
        For i = 1 To 8
            Byte1094(i) = br.ReadByte()
        Next
        For i = 1 To 6
            PaletteBrightness(i) = br.ReadInt16()
        Next
        For i = 1 To 16
            Word1074(i) = br.ReadInt16()
        Next
        For i = 1 To 38
            CarryLocation(i) = br.ReadInt16()
        Next
        wRectPos966 = br.ReadRectPos()
        wRectPos958 = br.ReadRectPos()
        wRectPos950 = br.ReadRectPos()
        wRectPos942 = br.ReadRectPos()
        wRectPos934 = br.ReadRectPos()
        wRectPos926 = br.ReadRectPos()
        Unused918 = br.ReadInt16()
        For i = 1 To 46
            IconDisplay(i) = br.ReadICONDISPLAY()
        Next
        Byte640 = br.ReadByte()
        For i = 1 To 15
            Reserved639(i) = br.ReadByte()
        Next
        wRectPos624 = br.ReadRectPos()
        For i = 1 To 4
            Reserved616(i) = br.ReadByte()
        Next
        For i = 1 To 7
            Word612(i) = br.ReadInt16()
        Next
        For i = 1 To 8
            Reserved598(i) = br.ReadByte()
        Next
        For i = 1 To 4
            uByte590(i) = br.ReadByte()
        Next
        For i = 1 To 8
            For j = 1 To 4
                Byte586(i, j) = br.ReadByte()
            Next
        Next
        Word554 = br.ReadInt16()
        For i = 1 To 6
            Palette552(i) = br.ReadPALETTE()
        Next
        Palette360 = br.ReadPALETTE()
        Palette328 = br.ReadPALETTE()
        For i = 1 To 70
            DefaultGraphicList(i) = br.ReadInt16()
        Next
        For i = 1 To 16
            Byte156(i) = br.ReadByte()
        Next
        For i = 1 To 4
            Word140(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word132(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word124(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word116(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word108(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word100(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word92(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word84(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word76(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word68(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word60(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word52(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word44(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word36(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word28(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word20(i) = br.ReadInt16()
        Next
        For i = 1 To 4
            Word12(i) = br.ReadInt16()
        Next
    End Sub

    Public Sub update()
        itemdata = New IO.MemoryStream
        Dim wr As CSBinaryWriter = New CSBinaryWriter(itemdata)

        Dim i As Integer
        Dim j As Integer

        For i = 1 To 2 : wr.WriteByte(Bytes1830(i)) : Next
        For i = 1 To 4 : wr.WriteInt32(Long1828(i)) : Next
        For i = 1 To 4 : wr.WriteInt32(Long1812(i)) : Next
        wr.WriteRectPos(wRectPos1796)
        wr.WriteRectPos(wRectPos1788)
        wr.WriteRectPos(wRectPos1780)
        For i = 1 To 22 : wr.WriteSOUND(sound1772(i)) : Next
        For i = 1 To 8 : wr.WriteByte(Byte1596(i)) : Next
        wr.WriteInt16(Word1588)
        For i = 1 To 30 : wr.WriteInt16(DropOrder(i)) : Next
        For i = 1 To 4 : For j = 1 To 3 : wr.WriteInt16(Word1526(i, j)) : Next : Next
        For i = 1 To 4 : For j = 1 To 6 : wr.WriteInt16(Word1502(i, j)) : Next : Next
        For i = 1 To 4 : wr.WriteRectPos(wRectPos1454(i)) : Next
        For i = 1 To 6 : wr.WriteByte(SpecialChars(i)) : Next
        For i = 1 To 2 : wr.WriteByte(Byte1416(i)) : Next
        For i = 1 To 2 : wr.WriteByte(Byte1414(i)) : Next
        For i = 1 To 6 : wr.WriteByte(Byte1412(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word1406(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word1398(i)) : Next
        For i = 1 To 4 : wr.WriteByte(Byte1390(i)) : Next
        For i = 1 To 4 : wr.WriteByte(Byte1386(i)) : Next
        For i = 1 To 16 : wr.WriteByte(Byte1382(i)) : Next
        For i = 1 To 16 : wr.WriteByte(Byte1366(i)) : Next
        For i = 1 To 128 : wr.WriteByte(Byte1350(i)) : Next
        For i = 1 To 128 : wr.WriteByte(Byte1222(i)) : Next
        For i = 1 To 8 : wr.WriteByte(Byte1094(i)) : Next
        For i = 1 To 6 : wr.WriteInt16(PaletteBrightness(i)) : Next
        For i = 1 To 16 : wr.WriteInt16(Word1074(i)) : Next
        For i = 1 To 38 : wr.WriteInt16(CarryLocation(i)) : Next
        wr.WriteRectPos(wRectPos966)
        wr.WriteRectPos(wRectPos958)
        wr.WriteRectPos(wRectPos950)
        wr.WriteRectPos(wRectPos942)
        wr.WriteRectPos(wRectPos934)
        wr.WriteRectPos(wRectPos926)
        wr.WriteInt16(Unused918)
        For i = 1 To 46 : wr.WriteICONDISPLAY(IconDisplay(i)) : Next
        wr.WriteByte(Byte640)
        For i = 1 To 15 : wr.WriteByte(Reserved639(i)) : Next
        wr.WriteRectPos(wRectPos624)
        For i = 1 To 4 : wr.WriteByte(Reserved616(i)) : Next
        For i = 1 To 7 : wr.WriteInt16(Word612(i)) : Next
        For i = 1 To 8 : wr.WriteByte(Reserved598(i)) : Next
        For i = 1 To 4 : wr.WriteByte(uByte590(i)) : Next
        For i = 1 To 8 : For j = 1 To 4 : wr.WriteByte(Byte586(i, j)) : Next : Next
        wr.WriteInt16(Word554)
        For i = 1 To 6 : wr.WritePALETTE(Palette552(i)) : Next
        wr.WritePALETTE(Palette360)
        wr.WritePALETTE(Palette328)
        For i = 1 To 70 : wr.WriteInt16(DefaultGraphicList(i)) : Next
        For i = 1 To 16 : wr.WriteByte(Byte156(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word140(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word132(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word124(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word116(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word108(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word100(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word92(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word84(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word76(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word68(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word60(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word52(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word44(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word36(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word28(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word20(i)) : Next
        For i = 1 To 4 : wr.WriteInt16(Word12(i)) : Next

        graphics.setgitemsize(562, CInt(itemdata.Length))
        graphics.setgitemdata(562, itemdata)
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="i">1 BASED</param>
    ''' <param name="p"></param>
    ''' <remarks></remarks>
    Public Sub SetPalette552(ByVal i As Integer, ByVal p As PALETTE)
        Dim x As Integer
        For x = 1 To 16
            Palette552(i).color(x) = p.color(x)
        Next
        'App.graphics.build_palettes()
    End Sub
    Public Sub SetPalette360(ByVal p As PALETTE)
        Dim x As Integer
        For x = 1 To 16
            Palette360.color(x) = p.color(x)
        Next
        'App.graphics.build_palettes()
    End Sub
    Public Sub SetPalette328(ByVal p As PALETTE)
        Dim x As Integer
        For x = 1 To 16
            Palette328.color(x) = p.color(x)
        Next
        'App.graphics.build_palettes()
    End Sub
End Class
