function ChangePaletteFader()
{
  global $linker, $space, $PAL, $settings;
  
  /*... Other code omitted ...*/

  if(true) // Close Town Doors At Night
  {
    $asm_name = sprintf('bin/towndoors_%s.bin', $PAL?'pal':'ntsc');
    $b = LoadAsmBlob('ChangeDoorsAtTransition', $asm_name);
    $b->SearchReplace("TRA", "Trampoline",       "x20LH");
    $b->SearchReplace("TRJ", "Trampoline_Jmp",   "x20LH");
    $b->SearchReplace("BN2", "TowndoorUpdater_bank2", "LHB");
    $linker->AddBlob($b, 0x1C000);

    $asm_name = sprintf('bin/towndoors_bank2_%s.bin', $PAL?'pal':'ntsc');
    $b = LoadAsmBlob('TowndoorUpdater_bank2', $asm_name);
    $b->SearchReplace("TRA", "Trampoline",       "x20LH");
    $b->SearchReplace("TRJ", "Trampoline_Jmp",   "x20LH");
    $b->SearchReplace("BN4", "TownDoors_OneMetaTileUpdater", "LHB");
    $linker->AddBlob($b, 0x8000); // In bank 2, same with level data screens

    $asm_name = sprintf('bin/towndoors_bank4_%s.bin', $PAL?'pal':'ntsc');
    $b = LoadAsmBlob('TownDoors_OneMetaTileUpdater', $asm_name);
    $b->SearchReplace("TRA", "Trampoline",       "x20LH");
    $b->SearchReplace("TRJ", "Trampoline_Jmp",   "x20LH");
    $linker->AddBlob($b, 0x10000); // In bank 4, same with the meta tile data itself
  }
}

function CloseTownDoorsAtNight()
{
  global $linker, $settings, $space, $PAL;
  
  $change_type = '32x32'; // Options: 8x8, 32x32, and none (none = optimize only)
  #$change_type = '8x8';
  #$change_type = 'none';
  
  /* Original code:
  
3     - jsr SubstituteBlockCheckFakeBlocks ($E6D1)
2       cmp #$FF
2       bne +
2       ldy $97
2       lda ($16),y
3     + sta $700,x
2       inc $97
1       inx
    (17 bytes)

    Found at $E541, continues:
            jsr AddTileAttributesTo94
            dec $93
            bne -
          end at $E559

    Found at $F449, continues:
            inc $5E
            dec $07
            beq $F488
            dec $93
            bne -
          end at $F464

    Found at $E84C, continues:
            jsr AddTilesAttributesTo94
            dec $93
            bne -
          end at $E864
    
    The original code to be changed into (this can be in bank 4):
    
           ; Inline SubstituteBlockCheckFakeBlocks:
2          lda $93
2          cmp #2
2          bne +
2          inc $0D
2        + ldy #0
3        - cpy $185 ; cpy #$20
2          bcs +
1          iny
1          iny
3          lda $172-2,y ;load X
2          and #$3F
2          cmp $0D      ; Skip if (Xcoord & 63) != ($0D)
2          bne -
3          lda $171-2,y ;load Y
2          and #$3F
2          sta $98
2          lda $6A
1          lsr
2          cmp $98   ; Skip if (Ycoord & 63) != ($6A >> 1)
2          bne -
           ; Ok, we got a block now.
           ; Y = ($0D & 1) + 2*($6A & 1) + 4*!!(Ycoord & 64)
           ; Use Y as index to $E728.
           ; But $E728 is a table of four zeros!
2          lda #0
2          beq ++
           ; This continues after SubstituteBlockCheckFakeBlocks:
1        - tay
2          lda IsNightTime
2          beq +++
3          lda Table_NightDoor-1,y
2          bne ++
2        + ldy $97
2          lda ($16),y
2          beq ++
2          cmp #9
2          bcc -
3    -- ++ sta $700,x
2          inc $97
1          inx
1          rts
       +++ cpy #7
           bcc --
           lda #$F0
           bne --
          
Total: 76 bytes.

AddTileAttributesTo94 could be written as:
           ; test byte 0
           ldy #0
           cmp (LevelData5BpointerLo),y
           bcs +
           tya
         - asl
           rol Temp94
           asl
           rol Temp94
           rts
           ; test byte 1
         + iny
           cmp (LevelData5BpointerLo),y
           bcs +
           lda #$C0
           bne -
           ; test byte 2
         + iny
           cmp (LevelData5BpointerLo),y
           lda #$80
           bcc -
           lsr
           bne -

     Space used by SubstituteBlockCheckFakeBlocks ($E6D1..$E72B) can be freed (90 bytes).
     Space used by AddTileAttributesTo94 ($EB6E..$EB95) can be freed (40 bytes).

     With Table_NightDoor: $77,$78,$79,$7B,$7C,$7D,$AF,$EF
     and  Table_DayDoor:   $00,$00,$00,$00,$00,$00,$F0,$F0
   */
   
  if($change_type == '8x8')
  {
    // put "AD 01 07 9D 94 02 05 97" at $85A9
    // and "94 03 06 97 94 02 05 97 94 04 08 97" at 86E1 in bank 4
    // This replaces the patterns for open town doors.
    // Tile numbers 1..8 are now indexes to Table_NightDoor.
    // On day, they are all $00 or $F0.
    $b = new Blob("TownClosedDoorsGFXtop", "\xAD\x01\x02\x9D\x94\x03\x04\x97");
    $location = 0x105A9; $linker->AddBlob($b, $location, true); #$linker->RemoveSpace($space, $location, $b->GetSize());
    $b = new Blob("TownClosedDoorsGFXbot", "\x94\x03\x04\x97\x94\x03\x04\x97\x94\x05\x06\x97");
    $location = 0x106E1; $linker->AddBlob($b, $location, true); #$linker->RemoveSpace($space, $location, $b->GetSize());

    $linker->AddBlob(new Blob("Table_NightDoor", "\x77\xAF\x78\x7C\x7B\xEF"), 0x10000);
    $b = new Blob("LoadBackgroundTile",
      "\xA5\x93\xC9\x02\xD0\x02\xE6\x0D".
      "\xA0\x00".
      "\xCC\x85\x01\xB0\x27". // - ... bcs +
      "\xC8\xC8\xB9\x70\x01\x29\x3F\xC5\x0D\xD0\xF0". // bne -
      "\xB9\x6F\x01\x29\x3F\x85\x98\xA5\x6A\x4A\xC5\x98\xD0\xE2". // bne -
      "\xA9\x00\xF0\x14".     // beq ++
      "\xA8\xA5\x82\xF0\x16". // - ... beq +++
      "\xB9");
    $b->AppendOffsetedPointerTo("Table_NightDoor", -1);
    $b->AppendRawData("\xD0\x0A". // bne ++
      "\xA4\x97\xB1\x16\xF0\x04". // + beq ++
      "\xC9\x07\x90\xEC". // bcc -
      "\x9D\x00\x07\xE6\x97\xE8\x60". // -- ++ ... rts
      "\xC0\x05\x90\xF5\xA9\xF0\xD0\xF1"); // +++ .. --, .. --
    $linker->AddBlob($b, Array(0x10000,0x1C000));
  }
  else
  {
    $b = new Blob("LoadBackgroundTile",
      "\xA5\x93\xC9\x02\xD0\x02\xE6\x0D".
      "\xA0\x00".
      "\xCC\x85\x01\xB0\x1D". // - ... bcs +
      "\xC8\xC8\xB9\x70\x01\x29\x3F\xC5\x0D\xD0\xF0". // bne -
      "\xB9\x6F\x01\x29\x3F\x85\x98\xA5\x6A\x4A\xC5\x98\xD0\xE2". // bne -
      "\xA9\x00\xF0\x04". // beq ++
      "\xA4\x97\xB1\x16". // +
      "\x9D\x00\x07\xE6\x97\xE8\x60"); // ++ ... rts
    $linker->AddBlob($b, Array(0x10000,0x1C000));
  }

  $linker->AddBlob(new Blob("AddTileAttributesTo94",
    "\xA0\x00\xD1\x6E\xB0\x08".
    "\x98".
    "\x0A\x26\x94\x0A\x26\x94\x60".
    "\xC8\xD1\x6E\xB0\x04". "\xA9\xC0\xD0\xF0".
    "\xC8\xD1\x6E\xA9\x80\x90\xE9\x4A\xD0\xE6"), Array(0x10000,0x1C000));

  if($change_type == '32x32')
  {
    // Patch all town screens that contain an open door
    $b = new Blob("Door836B", "\x94"); $linker->AddBlob($b, 0x836B, true); // scene 0_0 scr 0, scene 0_1 scr 0, scene 0_2 scr 4, scene 0_6 scr 0
    $b = new Blob("Door8373", "\xA8"); $linker->AddBlob($b, 0x8373, true); // scene 0_0 scr 0, scene 0_1 scr 0, scene 0_2 scr 4, scene 0_6 scr 0
    $b = new Blob("Door859A", "\x94"); $linker->AddBlob($b, 0x859A, true); // scene 0_0 scr 2, scene 0_1 scr 1, scene 0_3 scr 2, scene 0_4 scr 2
    $b = new Blob("Door85A2", "\xA8"); $linker->AddBlob($b, 0x85A2, true); // scene 0_0 scr 2, scene 0_1 scr 1, scene 0_3 scr 2, scene 0_4 scr 2
    $b = new Blob("Door8392", "\x94"); $linker->AddBlob($b, 0x8392, true); // scene 0_0 scr 3, scene 0_1 scr 3, scene 0_6 scr 3
    $b = new Blob("Door839A", "\xA8"); $linker->AddBlob($b, 0x839A, true); // scene 0_0 scr 3, scene 0_1 scr 3, scene 0_6 scr 3
    $b = new Blob("Door8329", "\x94"); $linker->AddBlob($b, 0x8329, true); // scene 0_2 scr 5, scene 0_5 scr 10
    $b = new Blob("Door8331", "\xA8"); $linker->AddBlob($b, 0x8331, true); // scene 0_2 scr 5, scene 0_5 scr 10
    $b = new Blob("Door8464", "\x94"); $linker->AddBlob($b, 0x8464, true); // scene 0_2 scr 7, scene 0_3 scr 7, scene 0_4 scr 11, scene 0_5 scr 11
    $b = new Blob("Door846C", "\xA8"); $linker->AddBlob($b, 0x846C, true); // scene 0_2 scr 7, scene 0_3 scr 7, scene 0_4 scr 11, scene 0_5 scr 11
    $b = new Blob("Door82E3", "\x94"); $linker->AddBlob($b, 0x82E3, true); // scene 0_3 scr 5
    $b = new Blob("Door82EB", "\xA8"); $linker->AddBlob($b, 0x82EB, true); // scene 0_3 scr 5
    $b = new Blob("Door844B", "\x94"); $linker->AddBlob($b, 0x844B, true); // scene 0_4 scr 1
    $b = new Blob("Door8453", "\xA8"); $linker->AddBlob($b, 0x8453, true); // scene 0_4 scr 1
    $b = new Blob("Door840C", "\x94"); $linker->AddBlob($b, 0x840C, true); // scene 0_5 scr 5
    $b = new Blob("Door8414", "\xA8"); $linker->AddBlob($b, 0x8414, true); // scene 0_5 scr 5

    $metatiles = Array(
    Array('',''), // original metatile 0
    Array("\xF6\xF8\xF6\xF8\xF7\xF9\xF7\xF9\x00\x00\x00\x00\x00\x00\x00\x00", "\x00"), // original metatile 1 ($01)
    Array("\xF3\xF4\xF5\xFA\x00\x74\x75\x00\x00\x00\x74\x75\x00\x00\x00\x74", "\x00"), // original metatile 2 ($02)
    Array("\xF5\xFA\xF1\xF2\x00\x72\x73\x00\x72\x73\x00\x00\x73\x00\x00\x00", "\x00"), // original metatile 3 ($03)
    Array("\x74\x75\x00\x00\x00\x74\x75\x00\x00\x00\x74\x75\x00\x00\x00\x74", "\x00"), // original metatile 4 ($04)
    Array("\x00\x00\x72\x73\x00\x72\x73\x00\x72\x73\x00\x00\x73\x00\x00\x00", "\x00"), // original metatile 5 ($05)
    Array("\xF6\xF8\x00\x00\xF7\xF9\x00\x00\xF6\xF8\x00\x00\xF7\xF9\x00\x00", "\x00"), // original metatile 6 ($06)
    Array("\x00\x00\xF6\xF8\x00\x00\xF7\xF9\x00\x00\xF6\xF8\x00\x00\xF7\xF9", "\x00"), // original metatile 7 ($07)
    Array("\xFB\xFD\x00\x00\xFC\xFE\x00\x00\xFB\xFD\x00\x00\xFC\xFE\x00\x00", "\x00"), // original metatile 8 ($08)
    Array("\x00\x00\xFB\xFD\x00\x00\xFC\xFE\x00\x00\xFB\xFD\x00\x00\xFC\xFE", "\x00"), // original metatile 9 ($09)
    Array("\xF6\xF8\xF6\xF8\xF7\xF9\xF7\xF9\x00\x00\x00\x00\x75\x00\x00\x00", "\x00"), // original metatile 10 ($0A)
    Array("\xF6\xF8\xF6\xF8\xF7\xF9\xF7\xF9\x00\x00\x00\x00\x00\x00\x00\x72", "\x00"), // original metatile 11 ($0B)
    Array("\xFB\xFD\xFB\xFD\xFC\xFE\xFC\xFE\x00\x00\x00\x00\x00\x00\x00\x00", "\x00"), // original metatile 12 ($0C)
    Array("\x65\x66\x66\x67\x65\x66\x66\x67\x00\x68\x00\x00\x00\x68\x00\x00", "\x00"), // original metatile 13 ($0D)
    Array("\xFB\xFD\xF6\xF8\xFC\xFE\xF7\xF9\xFB\xFD\xF6\xF8\xFC\xFE\xF7\xF9", "\x00"), // original metatile 14 ($0E)
    Array("\xF6\xF8\xFB\xFD\xF7\xF9\xFC\xFE\xF6\xF8\xFB\xFD\xF7\xF9\xFC\xFE", "\x00"), // original metatile 15 ($0F)
    Array("\x00\xB0\xE2\x7F\x00\xB2\xE2\x9C\x00\xC9\xC9\x91\x00\x96\x96\x96", "\x06"), // original metatile 16 ($10)
    Array("\x7E\xE3\xB0\x00\xAE\xE3\xB3\x00\xCF\xC9\xC9\x00\x96\x96\x96\x00", "\x09"), // original metatile 17 ($11)
    Array("\x00\xB0\xE2\x7F\x00\xB0\xE4\xA2\x00\xB1\xE2\x7F\x00\xB0\xE2\x7F", "\x66"), // original metatile 18 ($12)
    Array("\x7E\xE3\xB0\x00\xC6\xE5\xB0\x00\x7E\xE3\xB1\x00\x7E\xE3\xB0\x00", "\x99"), // original metatile 19 ($13)
    Array("\x7E\x7F\x7E\x7F\xDE\xDF\xE0\xE1\xAD\x00\x00\x9D\x94\x00\x00\x97", "\x55"), // original metatile 20 ($14)
    Array("\x7E\x7F\x81\x7F\x7E\xD6\xD6\xC6\x7E\xD7\xD7\x7F\x81\x93\x92\x7F", "\x55"), // original metatile 21 ($15)
    Array("\x00\xB0\xC8\xC8\x00\xB0\x00\x00\x00\xB1\x00\x93\x00\xB0\xE2\x7F", "\x62"), // original metatile 22 ($16)
    Array("\xC8\xC8\xB0\x00\x00\x00\xB0\x00\x92\x00\xB1\x00\x7E\xE3\xB0\x00", "\x98"), // original metatile 23 ($17)
    Array("\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0", "\xAA"), // original metatile 24 ($18)
    Array("\x81\xBA\xBB\x96\xBC\xBD\xBE\xBF\x81\xC0\xC1\xC6\x7E\xC2\xC3\xA2", "\x55"), // original metatile 25 ($19)
    Array("\x00\x00\x00\x93\x00\x00\x92\x9C\x00\x00\xCF\xC9\x00\x00\x96\x96", "\x15"), // original metatile 26 ($1A)
    Array("\x92\x00\x00\x00\xAE\x92\x00\x00\xC9\x91\x00\x00\x96\x96\x00\x00", "\x45"), // original metatile 27 ($1B)
    Array("\x7E\x7F\x9E\x9F\x81\x7F\x98\x99\xA3\x81\x98\x99\x7E\x82\x98\x99", "\x11"), // original metatile 28 ($1C)
    Array("\xC8\xC8\xC8\xC8\x00\x00\x00\x00\xD0\xD1\xD2\xD3\xA4\xA5\xA6\xA7", "\x50"), // original metatile 29 ($1D)
    Array("\xC8\xC8\xC8\xC8\x00\x00\x00\x00\x92\x93\x92\x93\x96\x7F\x7E\x7F", "\x50"), // original metatile 30 ($1E)
    Array("\x81\x7F\x7E\x7F\x7E\xAA\x81\x7F\x7E\x82\xA3\x81\x96\x7F\x7E\x82", "\x55"), // original metatile 31 ($1F)
    Array("\x7E\x7F\x7E\x7F\xDE\xDF\xE0\xE1\xAD\x77\xAF\x9D\x94\x78\x7C\x97", "\x55"), // original metatile 32 ($20) - also night-door top
    Array("\xCA\xCB\xDC\xDD\x83\x84\x85\x86\x87\x88\x89\x8A\x83\x84\x85\x86", "\x55"), // original metatile 33 ($21)
    Array("\x90\xA1\xC4\x00\x90\xA1\xC4\x00\x90\xA1\xC4\x00\x90\xA1\xC4\x00", "\xAA"), // original metatile 34 ($22)
    Array("\x90\xA1\xC4\x00\x90\xA1\xC4\x00\x90\xA1\xC4\x00\xC5\xCC\xCD\x00", "\xAA"), // original metatile 35 ($23)
    Array("\x00\x00\x00\x00\xDA\xDA\xDA\xDA\xDB\xDB\xDB\xDB\xDB\xDB\xDB\xDB", "\xFF"), // original metatile 36 ($24)
    Array("\x7E\x7F\x7E\x81\xAE\x95\xAE\x82\xCF\xC9\xC9\xC9\x96\x96\x96\x96", "\x05"), // original metatile 37 ($25)
    Array("\xAC\x7F\x7E\x7F\x00\xAC\xA3\x7F\x00\x00\xAC\xA2\x75\x00\x00\xAC", "\x45"), // original metatile 38 ($26)
    Array("\x7E\x7F\x7E\xAC\x7E\xA3\xAC\x00\xC6\xAC\x00\x00\xAC\x00\x00\x72", "\x15"), // original metatile 39 ($27)
    Array("\x94\x00\x00\x97\x94\x00\x00\x97\x94\xF0\xF0\x97\xAA\x8E\x8F\xAB", "\x55"), // original metatile 40 ($28)
    Array("\x92\x00\x00\x00\x7E\x93\x00\x00\x81\x7F\x92\x00\x7E\x7F\x7E\x93", "\x55"), // original metatile 41 ($29)
    Array("\x7E\x7F\x98\x99\xA2\x7F\xA8\xA9\x7E\x7F\x92\x93\x81\x7F\x7E\x7F", "\x51"), // original metatile 42 ($2A)
    Array("\x94\x79\x7D\x97\x94\x78\x7C\x97\x94\x7B\xEF\x97\xAA\x8E\x8F\xAB", "\x55"), // original metatile 43 ($2B)
    Array("\x87\x88\x89\x8A\x8B\x8C\x8D\x80\xD5\xD5\xD5\xD5\x92\x93\x92\x93", "\x55"), // original metatile 44 ($2C)
    Array("\x94\x9A\xC7\x97\x94\x9A\xC7\x97\x94\x9A\xC7\x97\x94\x9A\xC7\x97", "\x55"), // original metatile 45 ($2D)
    Array("\x94\x9A\xC7\x97\xD8\xD9\xD9\xCE\x92\x93\x92\x93\x7E\x7F\xC6\x81", "\x55"), // original metatile 46 ($2E)
    Array("\xCA\xCB\xDC\xDD\x83\x84\x85\x86\x87\x88\x89\x8A\x83\x84\x85\x86", "\xAA"), // original metatile 47 ($2F)
    Array("\xB4\xB6\xB6\xB5\xB8\xB9\xB9\xB7\x92\x93\x92\x93\x81\x7F\x82\x7F", "\x55"), // original metatile 48 ($30)
    Array("\xB4\xB6\xB6\xB5\xB4\xB6\xB6\xB5\xB4\xB6\xB6\xB5\xB4\xB6\xB6\xB5", "\x55"), // original metatile 49 ($31)
    Array("\xC6\xA2\x7E\x7F\x95\x7F\x81\x7F\xAE\x9C\xAE\x9C\x00\x00\x00\x00", "\x55"), // original metatile 50 ($32)
    Array("\x7E\x7F\x7E\x7F\xDE\xDF\xE0\xE1\xAD\xD4\x9B\x9D\x94\x9A\xC7\x97", "\x55"), // original metatile 51 ($33)
    Array("\x7E\x7F\x7E\x7F\x82\x7F\x7E\x82\xD0\xD1\xD2\xD3\xA4\xB6\xB6\xA7", "\x55"), // original metatile 52 ($34)
    Array("\x87\x88\x89\x8A\x8B\x8C\x8D\x80\xD5\xD5\xD5\xD5\x92\x93\x92\x93", "\x5A"), // original metatile 53 ($35)
    Array("\x9E\x9F\x81\x7F\x98\x99\x7E\x7F\x98\x99\x7E\x82\x98\x99\x96\x7F", "\x44"), // original metatile 54 ($36)
    Array("\xE6\xE7\xE7\xE8\xE9\xEA\xEA\xEB\x7E\x82\xA3\x81\x96\x7F\x7E\x82", "\x50"), // original metatile 55 ($37)
    Array("\x7E\x7F\xC6\xA2\x7E\xC6\xA2\xC6\xC6\xA2\xC6\x7F\x7E\x7F\x7E\x7F", "\x55"), // original metatile 56 ($38)
    Array("\x7E\x7F\x7E\xAC\x7E\xA3\xAC\x00\xC9\xAA\x00\x00\x96\x00\x00\x72", "\x05"), // original metatile 57 ($39)
    Array("\xAC\x7F\x7E\x7F\x00\xAC\xA3\x7F\x00\x00\xAB\xC9\x75\x00\x00\x96", "\x05"), // original metatile 58 ($3A)
    Array("\x00\x00\x00\x93\x00\x00\x92\x7F\x00\x93\x7E\x81\x92\x7F\x7E\x7F", "\x55"), // original metatile 59 ($3B)
    Array("\xEC\xED\xEC\xED\xEC\xEE\xED\xEC\x00\x00\x00\x00\x00\xB0\x00\x93", "\x65"), // original metatile 60 ($3C)
    Array("\xEC\xED\xEC\xED\xED\xEC\xED\xEC\x00\x00\x00\x00\x92\x93\x92\x93", "\x55"), // original metatile 61 ($3D)
    Array("\xEC\xED\xEC\xED\xED\xEC\xEE\xED\x00\x00\x00\x00\x92\x00\xB0\x00", "\x95"), // original metatile 62 ($3E)
    Array("\x98\x99\x7E\x7F\xA8\xA9\x7E\xC6\x92\x93\x81\x7F\x7E\x7F\x7E\x82", "\x54"), // original metatile 63 ($3F)
    Array("\xF6\xF8\xA0\xA0\xF7\xF9\xA0\xA0\xF6\xF8\xA0\xA0\xF7\xF9\xA0\xA0", "\x88"), // original metatile 64 ($40)
    Array("\xA0\xA0\xF6\xF8\xA0\xA0\xF7\xF9\xA0\xA0\xF6\xF8\xA0\xA0\xF7\xF9", "\x22"), // original metatile 65 ($41)
    Array("\xF6\xF8\xF6\xF8\xF7\xF9\xF7\xF9\xF6\xF8\xF6\xF8\xF7\xF9\xF7\xF9", "\x00"), // original metatile 66 ($42)
    Array("\x7E\x7F\x7E\x7F\xDE\xDF\xE0\xE1\xAD\x77\xAF\x9D\x94\x78\x7C\x97", "\x55"), // new metatile $43 (night door top) identical to $20
    Array("\x94\x78\x7C\x97\x94\x78\x7C\x97\x94\x7B\xEF\x97\xAA\x8E\x8F\xAB", "\x51"), // new metatile $44 (night door bottom)
    );

    $nmeta = count($metatiles) + 1;
    $s = chr($nmeta) . str_pad('', ($nmeta - 1)*17, "\0");
    foreach($metatiles as $n=>$meta) if($n)
    {
      $s = substr_replace($s, $meta[1], $n+1,           1);  // attribute
      $s = substr_replace($s, $meta[0], $nmeta + $n*16, 16); // metatile
    }
    $linker->AddBlob(new Blob("LevelData_MetaTiles_0_Towns", $s), Array(0x10000,0x1C000));

    $b = new Blob("Refer_TownMetatiles"); $b->AppendPointerTo("LevelData_MetaTiles_0_Towns");
    $linker->AddBlob($b, 0x1F7D1 - ($PAL?77:0), true);
    $space[0x1041D] = 0x10891-0x1041D; // Free up the space for old location of town metatiles

    $linker->AddBlob(new Blob("Load_Room_MetaTileNumber_StoreTo62",
      "\xA4\x97\xB1\x02".         //   ldy $97, lda ($02),y
      "\x30\x07".                 //   bmi +
      "\x85\x62".                 // - sta $62
      "\xA9\x04\x4C\x83\xC1".     //   lda #4, jmp SwitchBank_NewPage
      "\x29\x7F".                 // + and #$7F         (it's now $14 or $28).
      "\xA4\x82".                 //   ldy IsNightTime
      "\xF0\xF3".                 //   beq -
      "\xC9\x20\xA9\x00\x69\x43". //   cmp #$20, lda #0, adc #$43. This makes $14 into $43 and $28 into $44.
      "\xD0\xEB"), Array(0x8000,0x1C000));  //   bne -
  }
  else
  {
    $linker->AddBlob(new Blob("Load_Room_MetaTileNumber_StoreTo62",
      "\xA4\x97\xB1\x02".               // ldy $97, lda ($02),y
      "\x85\x62".                       // sta $62
      "\xA9\x04\x4C\x83\xC1"), Array(0x8000,0x1C000)); // lda #4, jmp SwitchBank_NewPage
  }

  $space[0x1EA4B - ($PAL?77:0)] = 12; // Old location of Load_Room_MetaTileNumber_StoreTo62
  $b = new Blob("MetaTileFetchPatch1", "\xA9\x02\x20\x83\xC1"); $b->AppendCallPointerTo("Load_Room_MetaTileNumber_StoreTo62");
  $linker->AddBlob($b, 0x1E41F-($PAL?77:0), true);
  $b = new Blob("MetaTileFetchPatch2", "\xA9\x02\x20\x83\xC1"); $b->AppendCallPointerTo("Load_Room_MetaTileNumber_StoreTo62");
  $linker->AddBlob($b, 0x1E460-($PAL?77:0), true);
  $b = new Blob("MetaTileFetchPatch3", "\xA9\x02\x20\x83\xC1"); $b->AppendCallPointerTo("Load_Room_MetaTileNumber_StoreTo62");
  $linker->AddBlob($b, 0x1EA61-($PAL?77:0), true);

  $space[0x1E6D1 - ($PAL?77:0)] = 0xE72C-0xE6D1;
  $space[0x1EB6E - ($PAL?77:0)] = 0xEB96-0xEB6E;

  $location = 0x1E541 - ($PAL?77:0);
  $space[$location] = 0xE559-0xE541;
  $b = new Blob("BackgroundRenderCode1"); // when scrolling vertically
  $b->AppendCallPointerTo("LoadBackgroundTile");
  $b->AppendCallPointerTo("AddTileAttributesTo94");
  $b->AppendRawData("\xC6\x93\xD0\xF6\xF0".chr($space[$location] - (6+3+3)));
  $linker->AddBlob($b, $location, true);
  $linker->RemoveSpace($space, $location, $b->GetSize());
  
  $location = 0x1E84C - ($PAL?77:0);
  $space[$location] = 0xE864-0xE84C;
  $b = new Blob("BackgroundRenderCode2"); // when scrolling horizontally
  $b->AppendCallPointerTo("LoadBackgroundTile");
  $b->AppendCallPointerTo("AddTileAttributesTo94");
  $b->AppendRawData("\xC6\x93\xD0\xF6\xF0".chr($space[$location] - (6+3+3)));
  $linker->AddBlob($b, $location, true);
  $linker->RemoveSpace($space, $location, $b->GetSize());

  $location = 0x1F449 - ($PAL?77:0);
  $space[$location] = 0xF464-0xF449;
  $b = new Blob("BackgroundRenderCode3"); // when undoing dialog box
  $b->AppendCallPointerTo("LoadBackgroundTile");
  $b->AppendRawData("\xE6\x5E\xC6\x07\xF0".chr(0xF488-0xF449-(3+6)));
  $b->AppendRawData("\xC6\x93\xD0\xF3\xF0".chr($space[$location] - (6+6+3)));
  $linker->AddBlob($b, $location, true);
  $linker->RemoveSpace($space, $location, $b->GetSize());
}