Basin Storage Optimization Example

Basin Storage Optimization Example

Before (Legacy Format)

Each basin stored as individual objects with coordinate lists:

{
  "basins": [
    {
      "id": "1#A",
      "tiles": ["10,5", "11,5", "12,5", "10,6", "11,6", "12,6", "10,7", "11,7", "12,7"],
      "volume": 150,
      "level": 2,
      "height": 1,
      "outlets": ["2#A"]
    },
    {
      "id": "2#A", 
      "tiles": ["15,8", "16,8", "17,8", "15,9", "16,9", "17,9"],
      "volume": 80,
      "level": 1,
      "height": 2,
      "outlets": []
    }
  ]
}

Size: ~400 characters for 2 small basins

After (Optimized Format)

Basin data stored as 2D map + tree structure + metadata:

{
  "basins": {
    "format": "optimized_v1",
    "basinIdMap": {
      "format": "string_rows", 
      "data": "0|0|0|0|0|0|0|0|0|0|1#A|1#A|1#A|0|0|2#A|2#A|2#A\n0|0|0|0|0|0|0|0|0|0|1#A|1#A|1#A|0|0|2#A|2#A|2#A\n0|0|0|0|0|0|0|0|0|0|1#A|1#A|1#A|0|0|0|0|0"
    },
    "basinTree": {
      "1#A": {"outlets": ["2#A"], "height": 1},
      "2#A": {"outlets": [], "height": 2}
    },
    "basinMetadata": {
      "1#A": {"volume": 150, "level": 2, "tileCount": 9},
      "2#A": {"volume": 80, "level": 1, "tileCount": 6}
    }
  }
}

Size: ~350 characters for same data + full spatial information

Benefits

  1. Space Efficiency: 10-50% smaller depending on basin distribution
  2. Faster Reconstruction: Direct array mapping instead of coordinate parsing
  3. Spatial Queries: Easy to find basin at any coordinate
  4. Tree Structure: Explicit outlet relationships preserved
  5. Scalability: Performance doesn’t degrade with basin complexity

RLE Alternative

For maps with large uniform areas, run-length encoding can provide even better compression:

{
  "basinIdMap": {
    "format": "rle_basin_ids",
    "data": [["0", 180], ["1#A", 9], ["0", 45], ["2#A", 6], ["0", 120]]
  }
}

This would compress a sparse 20x18 map (360 tiles) with just 2 small basins to ~50 characters.

            Shortcuts for codeblocks
            (hovered or for all with SHIFT)
            [L] - toggle line numbers
            [W] - wrap long lines
            [F] - fit codeblock to window size
            [R] - reset properties
            [CTRL+A] - copy all code in codeblock