GAM file format
FileExtension : GAM
Description

This format is used for the save game files. It doesn't store area specific information. Instead, it stores information on the party members and the global variables which affect the party members.
GAME file versions

    * V1.1: Baldur's Gate, Planescape: Torment, Icewind Dale
    * V2.0: Baldur's Gate 2
    * V2.2: Icewind Dale 2

GAME V1.1

Overall structure

    * Header
    * Party members
    * Party member CRE file data
    * NPCs (who are not in the party)
    * NPC kills statistics (embedded in NPC struct)
    * NPC CRE file data
    * (Torment only) Unknown section
    * Variables (in the GLOBAL namespace)
    * (Torment only) Kill variables (set when particular critters are killed)
    * Journal entries
    * (Torment only) 1720 bytes of Maze

GAME V1.1 Header
Offset Size (data type) Description
0x0000 4 (char array) Signature ('GAME')
0x0004 4 (char array) Version ('V1.1')
0x0008 4 (dword) Game time (300 units == 1 hour). According to Eddy Jansson, this field is exactly 7 hours (2100 units) ahead of the time displayed in the game. Precisely why this is so is unknown.
//it is because of the starting hours set in a 2da ???
0x000c 2 (word) Active formation
0x000e 2 (word) Formation1
0x0010 2 (word) Formation2
0x0012 2 (word) Formation3
0x0014 2 (word) Formation4
0x0016 2 (word) Formation5
0x0018 4 (dword) Party gold
0x001c 4 (dword) Weather bits (rain, snow, delay)
0x0020 4 (dword) Offset to NPC structs for party members.
0x0024 4 (dword) Count of NPC structs for party members. (i.e. how many characters in party)
0x0028 4 (dword) Offset to unknown field (doesn't get saved, but checked on load)
0x002c 4 (dword) Count of 20 bytes records of unknown field
0x0030 4 (dword) Offset to NPC structs for non-party members.
0x0034 4 (dword) Count of NPC structs for non-party members.
0x0038 4 (dword) Offset to GLOBAL namespace variables (i.e. non-local plot variables).
0x003c 4 (dword) Count of GLOBAL namespace variables.
0x0040 8 (resref) The resref of some area. (Possibly refers to a .are type resource.
0x0048 4 (dword) Unknown, usually 0xffffffff
0x004c 4 (dword) Count of journal entries
0x0050 4 (dword) Offset to journal entries (near end of file)

Now, the format diverges a little between BG2 and PST. In BG2, the next fields in the header look like:
Offset Size (data type) Description
0x0054 4 (dword) Reputation
0x0058 8 (resref) Current area
0x0060 4 (dword) Control flags (enables subwindows on main screen)
0x0064 4 (dword) Unknown
0x0068 4 (dword) Offset of familiars
0x006c 4 (dword) Offset to Saved locations
0x0070 4 (dword) Count of Saved locations
0x0074 4 (dword) Realtime
0x0078 60 (bytes) Unknown

In Planescape, the next fields look like:
Offset Size (data type) Description
0x0054 4 (dword) Offset to maze data 1720 bytes
0x0058 4 (dword) Reputation
0x005c 8 (resref) Current area (?)
0x0064 4 (dword) Offset to "kill variables", which are set when a particular creature type is killed. Which variable is set is determined by beasts.ini, I believe.
0x0068 4 (dword) Count of "kill variables".
0x006c 4 (dword) Offset to bestiary (known monsters, npcs)
0x0070 8 (resref) Current area (?)
0x0078 64 (bytes) Unknown
GAME V1.1 NPCs (both in-party and out-of-party NPCs)

This structure represents data about any character who can possibly be in your party.
Offset Size (data type) Description
0x0000 2 (word) PC selected (0x80 if unselectable)
0x0002 2 (word) Party order (0-5, or 0xffff if not in party)
0x0004 4 (dword) Offset from start of file to CRE resource data for this character.
0x0008 4 (dword) Size of CRE resource data for this character.
0x000c 8 (bytes) Creature resref (before loading)
0x0014 4 (dword) Facing direction
0x0018 8 (resref) Area where character is currently located.
0x0020 2 (word) Current X coordinate of character
0x0022 2 (word) Current Y coordinate of character
0x0024 2 (word) X coordinate of viewing rectangle. possibly this is stored per character so that multi-player games allow the characters to have different saved view rects?
0x0026 2 (word) Y coordinate of viewing rectangle
0x0028 2 (word) Modal action
0x002a 2 (word) Happiness 
0x002c 96 (bytes) Unknown

For BG/TotSC:
0x008c 2*4 (word*4) QuickWeapon Slots
0x0094 2*4 (word*4) QuickWeapon enablers
0x009c 3*8 (resref array) QuickSpell Slots
0x00b4 2*3 (word*3) QuickItem Slots
0x00ba 2*3 (word*3) QuickItem Slots enablers
0x00c0 32 (bytes) Name
0x00e0 4 (dword) Talkcount
0x00e4 116 bytes (Char stats) stats for this character
0x0158 8 (resref) Soundset

For Torment:
0x008c 2*4 (word*4) QuickWeapon Slots
0x0094 2*4 (word*4) QuickWeapon enablers
0x009c 3*8 (resref array) QuickSpell Slots
0x00b4 2*5 (word*5) QuickItem Slots
0x00be 2*5 (word*5) QuickItem Slots enablers
0x00c8 32 (bytes) Name
0x00e8 4 (dword) Talkcount
0x00ec 116 bytes (Char stats) stats for this character
0x0160 8 (resref) Soundset

For Icewind Dale:
0x008c 2*4 (word*4) QuickWeapon Slots
0x0094 2*4 (word*4) QuickWeapon enablers
0x009c 3*8 (resref array) QuickSpell Slots
0x00b4 2*3 (word*3) QuickItem Slots
0x00ba 2*3 (word*3) QuickItem Slots enablers
0x00c0 32 (bytes) Name
0x00e0 4 (dword) Talkcount
0x00e4 116 bytes (Char stats) stats for this character
0x0158 8 (resref) Soundset
0x0160 32 (bytes) Sound Folder

For Icewind Dale 2:
0x008c 2*8 (word*8) QuickWeapon Slots
0x009c 2*8 (word*8) QuickWeapon enablers
0x00ac 9*8 (resref array) QuickSpell Slots
0x00f4 9*8 (byte array) QuickSpell Classes
0x00fd 1 (byte) Unknown
0x00fe 2*3 (word*3) QuickItem Slots
0x0104 2*3 (word*3) QuickItem Slots enablers
0x010a 9*8 (resref array) QuickInnate Slots
0x0152 4*18 (dword*18) Unknown
0x01ac 9 (word) Configurable action slots (last 9)
0x01be 32 (bytes) Name
0x01de 4 (dword) Talkcount
0x01e2 116 bytes (Char stats) stats for this character
0x0256 8 (resref) Soundset
0x025e 32 (bytes) Sound Folder

GAME V1.1 Character stats (i.e. most powerful vanquished, etc)
Offset Size (data type) Description
0x0000 4 (strref) Name of "most powerful vanquished"
0x0004 4 (dword) XP of "most powerful vanquished"
0x0008 4*3 (dword array) Unknown
0x0014 4 (dword) kill XP (chapter)
0x0018 4 (dword) kill number (chapter)
0x001c 4 (dword) kill XP (game)
0x0020 4 (dword) kill number (game)
0x0024 4*8 (resref array) Favorite spells
0x0044 4*2 (word array) favorite spell count
0x004c 4*8 (resref array) Favorite weapons
0x006c 4*2 (word) favorite weapon count

GAME V1.1 Game variables (plot variables)

These structures represent variables with a textual name an an integer value. Most variables are in some namespace or other -- a namespace being a 6-byte character string. Most variables go in the "GLOBAL" namespace, though some go in area-specific namespaces.
Offset Size (data type) Description
0x0000 32 (char array) Variable name
0x0020 8 (bytes) Unknown
0x0028 4 (dword) Variable value
0x002c 40 (bytes) Unknown
GAME V1.1 Journal entries

Every time some text is entered into your journal, its strref is added to this table, along with the time (in seconds! not in 12-second increments, as the earlier game time in this file), as well as the "current chapter".
Offset Size (data type) Description
0x0000 4 (strref) Journal text
0x0004 4 (dword) Ingame date/time
0x0008 1 (byte) Current chapter number. (Always 0 for Torment).
0x0009 1 (byte) Unknown
0x000a 1 (byte) Section (only bg2/iwd2)
0x000b 1 (byte) Dialog group (gemrb extension)

