THE BROKEN SEAL - SAVE FILE FORMAT
==================================
WORLD.DAT, WORLD.BAK, WORLD.NEW and every file in BACKUP/ share one format,
version 8 (the desktop edition's .bseal files carry the same world inside).
Versions 1 to 7 still load; the notes below mark what each version added.
The same file is written and read by every build (Linux, Windows
and DOS); nothing is written as a compiler's native record.

All numbers are little-endian:
  u8      1 byte, unsigned
  u16     2 bytes, unsigned
  i32     4 bytes, two's complement
  u32     4 bytes, unsigned
  string  1 length byte, then that many bytes (plain ASCII); each field has a
          maximum length and a longer one makes the file invalid
Booleans are u8, 0 or 1. Officer, army and treaty references are slot
numbers (1-based, 0 = none); faction references are 1-6 (0 = none or
independent); province references are 1-NumProv (0 = none; an officer's Loc is 0 only
while he is on the road, with TravelTo and TravelLeft set). Officer Id values are
issued once and never reused; Friend1, Friend2, Rival and Parent refer to
Ids, not slots. Adj and access masks have bit (n-1) set for province n.

The file:
  "BSEL"                   4 bytes, the magic
  format version           u16 (4)
  the world, as below
  CRC-32                   u32: the standard CRC-32 (polynomial EDB88320,
                           initial and final value FFFFFFFF) of every byte
                           before it, magic included

The world, in this order: the header, the NumProv provinces, the 6
factions, the 120 officer slots, the 64 army slots, the 64 treaty slots,
the 3 coalition slots, and (version 4, 32 provinces only) the realm map.

World header:
    WorldId                  u32
    Turn                     i32
    StartYear                i32
    Rng                      u32
    Seq                      u32
    Paused                   u8
    Won                      u8
    Winner                   u8
    WinMask                  u8
    WonTurn                  i32
    LastMaint                i32
    Capital                  u8
    NextId                   u32
    Difficulty               u8 (0-4, Easy to Hardest; before version 3
                             0-2, read as 1-3)
    EscapeDelay              u8
    Schedule                 string
    BbsId                    string
    Scenario                 string
    NoWin                    u8
    Wins                     i32
    NumProv                  u8, 24 or 32 (version 4; older worlds are 24)

Province (NumProv; 24 in grid order A to X, or 32 in reading order on the
realm map). GX and GY are the grid column and row (0-5, 0-3), or on the
realm map half the label point's x and y:
    Name                     string
    Terrain                  u8
    GX                       u8
    GY                       u8
    Adj                      u32
    Owner                    u8
    Pop                      i32
    PopMax                   i32
    Support                  u8
    Farm                     u16
    FarmMax                  u16
    Comm                     u16
    CommMax                  u16
    Fort                     u16
    FortMax                  u16
    Troops                   3 x i32 (infantry, archers, cavalry)
    Train                    u8
    Morale                   u8
    Grain                    i32
    Gold                     i32 (version 2)
    Governor                 u8
    Unrest                   u8
    SiegeBy                  u8
    SiegeTurns               u16
    Occupied                 u16
    Short                    u8
    Recruits                 i32 (version 5; older: population/30 x (50 + support)/100)
    Market                   i32 (version 5; older: commerce x 20)
    Snowed                   u8  (version 5; 1 = winter roads to town closed this month)
    Unsettled                u8  (version 7; 1 = taken by force, no governor named yet)

Faction (6):
    Used                     u8
    Name                     string
    Ruler                    u8
    Heir                     u8
    Gold                     i32
    Legit                    u8
    LegitPart                u8
    Tax                      u8
    Human                    u8
    UserName                 string
    UserKey                  string
    LastSeen                 i32
    Joined                   i32
    Submitted                i32
    Outlaw                   u8
    Camp                     u8
    CampTroops               3 x i32 (infantry, archers, cavalry)
    CampGrain                i32
    CampTrain                u8  (version 6; older worlds load with 50)
    Coalition                u8
    Endorse                  u8
    Progress                 u8
    LegitGain                u8
    Betray                   u8
    LeaveCoal                u8
    Msgs                     u8
    MoveCamp                 u8
    then 10 shipments (6 before version 2), each:
    From                     u8
    Dest                     u8
    Amount                   i32 (grain)
    Gold                     i32 (version 2)
    Keep                     u8
    then 3 gifts, each:
    Dest                     u8
    Gold                     i32
    Grain                    i32
    FromProv                 u8
    ToProv                   u8
    then NumProv intelligence records (one per province), each:
    Turn                     i32
    Owner                    u8
    Troops                   i32
    Grain                    i32
    Armies                   i32
    Fort                     u8
    Support                  u8
    Gov                      string
    Income                   i32
    Upkeep                   i32
    Reliefs                  u8
    LastGrow                 i32
    Seen                     u8

Officer (120 slots; Status 0 = empty slot):
    Id                       u32
    Name                     string
    Cmd                      u8
    War                      u8
    Adm                      u8
    Intr                     u8
    Loyalty                  u8
    Trait                    u8
    Health                   u8
    Origin                   u8
    BirthProv                u8
    BirthYear                i32
    Friend1                  u32
    Friend2                  u32
    Rival                    u32
    Parent                   u32
    Status                   u8
    Employer                 u8
    Captor                   u8
    Loc                      u8
    CaptiveSince             i32
    Task                     u8
    TaskType                 u8
    Prio                     u8
    TaskArg                  i32
    TaskRepeat               u8
    Unpaid                   u8
    LastReleased             i32
    Army                     u8
    Idle                     u16
    Rewarded                 i32
    Stamina                  u8  (version 5; older worlds load with 100)
    TravelTo                 u8  (version 8; on the road: the destination, else 0)
    TravelLeft               u8  (version 8; on the road: months still to go, else 0;
                                  older worlds load with nobody on the road)

Army (64 slots; Used 0 = empty):
    Used                     u8
    Id                       u32
    Owner                    u8
    Commander                u8
    Loc                      u8
    Troops                   3 x i32 (infantry, archers, cavalry)
    Morale                   u8
    Train                    u8
    Grain                    i32
    Gold                     i32 (version 2)
    Order                    u8
    Dest                     u8
    Plan                     u8
    RetreatPct               u8
    Fallback                 u8
    Occupier                 u8
    Persist                  u8
    Defend                   u8
    Grace                    u8
    Short                    u8
    Origin                   u8

Treaty (64 slots; Status 0 = empty):
    Kind                     u8
    Status                   u8
    A                        u8
    B                        u8
    Proposed                 i32
    Start                    i32
    EndTurn                  i32
    Duration                 u16
    AGold                    i32
    AGrain                   i32
    BGold                    i32
    BGrain                   i32
    ASrc                     u8
    ADst                     u8
    BSrc                     u8
    BDst                     u8
    AMask                    u32
    BMask                    u32
    Off1                     u8
    Off2                     u8
    CancelBy                 u8

Coalition (3 slots; Used 0 = empty):
    Used                     u8
    Name                     string
    Leader                   u8
    Members                  u8
    Founded                  i32
    Invite                   u8
    InviteOk                 u8
    Approvals                u8
    Expel                    u8
    ExpelVotes               u8
    Leaving                  u8
    LeaveTurn[I]             i32  [x NF]
    Progress                 u8
    Nominee                  u8

Realm map (version 4, NumProv 32), after the coalitions:
    MapSeed                  u32, the draw that made the map
    then 3 rivers, each:
    Points                   u8 (0-48)
    then each point: X u16, Y u16 (map pixels, may be just off the map)
    then 272 rows of 348 cells, each row:
    Runs                     u16 (1-348)
    then each run: Length u8 (1-255), Value u8
    A cell's value is 0 for the sea, 255 for the wild range, otherwise the
    province number. A row's runs must cover exactly 348 cells.

Loading checks the magic, the version, every string length, the length of
the file and the CRC, and then the world's own rules (owners exist, every
army has a serving commander of its own faction who commands it, no officer
Id is used twice or is not yet issued, ratings are in range, no negative
gold, grain or troops, borders go both ways, coalition membership matches the
factions' records, and more: see Validate in src/bsstore.pas). A file that
fails is not used.

Side files are plain text, one record per line, fields separated by TAB:
  REPORTS.DAT   turn, faction, category (0 headlines, 1 government,
                2 officers, 3 diplomacy, 4 military), text
  CHRON.TXT     turn, text
  MSGS.DAT      turn, from faction, to faction, text
