//Incremental update for TobEx beta 0018

//Scrollable Kit Selection
COPY_EXISTING "guicg.chu" "override/guicg.chu"
  READ_LONG 0x8 nPanels
  READ_LONG 0xc offControlTbl
  READ_LONG 0x10 offPanels
  
  SET i = 0
  SET nControls = 0
  FOR (i = 0; i < nPanels; i+= 1) BEGIN
      READ_SHORT (%offPanels% + %i% * 0x1c + 0xe) nPanelControls
      SET nControls += nPanelControls
  END

  //add scroll bar
  SET currPanelIdx  = 22
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) nPanelControls
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0x18) idxPanelControl

  //add control
  READ_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) offControlThis
  INSERT_BYTES (%offControlThis%) 0x28
  WRITE_SHORT (%offControlThis%) 15 //controlIdx
  WRITE_SHORT (%offControlThis% + 0x2) 0 //bufferSize
  WRITE_SHORT (%offControlThis% + 0x4) 290 //x
  WRITE_SHORT (%offControlThis% + 0x6) 50 //y
  WRITE_SHORT (%offControlThis% + 0x8) 16 //width
  WRITE_SHORT (%offControlThis% + 0xa) 220 //height
  WRITE_BYTE (%offControlThis% + 0xc) 7 //type = SCROLL_BAR
  WRITE_BYTE (%offControlThis% + 0xd) 0 //unknown
  WRITE_ASCII (%offControlThis% + 0xe) "GUISCRCW" #8 //BAM
  WRITE_SHORT (%offControlThis% + 0x16) 0 //anim cycle
  WRITE_SHORT (%offControlThis% + 0x18) 0 //frame-up unpressed
  WRITE_SHORT (%offControlThis% + 0x1a) 1 //frame-up pressed
  WRITE_SHORT (%offControlThis% + 0x1c) 2 //frame-down unpressed
  WRITE_SHORT (%offControlThis% + 0x1e) 3 //frame-down pressed
  WRITE_SHORT (%offControlThis% + 0x20) 5 //frame trough
  WRITE_SHORT (%offControlThis% + 0x22) 4 //frame slider
  WRITE_LONG (%offControlThis% + 0x24) 0xFFFFFFFF //text area id

  //updating all existing control table offsets
  FOR (i = %idxPanelControl% + %nPanelControls%; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x28
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //add control table entry
  INSERT_BYTES (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) 0x8
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) %offControlThis% //offset
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8 + 0x4) 0x28 //length

  SET nControls += 1

  //update all control table offsets
  FOR (i = 0; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x8
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //update control numbers
  SET nPanelControls += 1
  WRITE_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) %nPanelControls%

  //update panel control indices
  FOR (i = %currPanelIdx% + 1; i < nPanels; i += 1) BEGIN
    READ_SHORT (%offPanels% + %i% * 0x1c + 0x18) idxPanelControl
    SET idxPanelControl += 1
    WRITE_SHORT (%offPanels% + %i% * 0x1c + 0x18) %idxPanelControl%
  END
  


  //add button
  SET currPanelIdx  = 22
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) nPanelControls
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0x18) idxPanelControl

  //add control
  READ_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) offControlThis
  INSERT_BYTES (%offControlThis%) 0x20
  WRITE_SHORT (%offControlThis%) 16 //controlIdx
  WRITE_SHORT (%offControlThis% + 0x2) 0 //bufferSize
  WRITE_SHORT (%offControlThis% + 0x4) 18 //x
  WRITE_SHORT (%offControlThis% + 0x6) 250 //y
  WRITE_SHORT (%offControlThis% + 0x8) 271 //width
  WRITE_SHORT (%offControlThis% + 0xa) 20 //height
  WRITE_BYTE (%offControlThis% + 0xc) 0 //type = BUTTON
  WRITE_BYTE (%offControlThis% + 0xd) 0 //unknown
  WRITE_ASCII (%offControlThis% + 0xe) "GUICGBC" #8 //BAM
  WRITE_BYTE (%offControlThis% + 0x16) 8 //anim cycle
  WRITE_BYTE (%offControlThis% + 0x17) 0 //text flags
  WRITE_SHORT (%offControlThis% + 0x18) 0 //frame unpressed
  WRITE_SHORT (%offControlThis% + 0x1a) 1 //frame pressed
  WRITE_SHORT (%offControlThis% + 0x1c) 1 //frame selected
  WRITE_SHORT (%offControlThis% + 0x1e) 3 //frame disabled

  //updating all existing control table offsets
  FOR (i = %idxPanelControl% + %nPanelControls%; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x20
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //add control table entry
  INSERT_BYTES (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) 0x8
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) %offControlThis% //offset
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8 + 0x4) 0x20 //length

  SET nControls += 1

  //update all control table offsets
  FOR (i = 0; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x8
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //update control numbers
  SET nPanelControls += 1
  WRITE_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) %nPanelControls%

  //update panel control indices
  FOR (i = %currPanelIdx% + 1; i < nPanels; i += 1) BEGIN
    READ_SHORT (%offPanels% + %i% * 0x1c + 0x18) idxPanelControl
    SET idxPanelControl += 1
    WRITE_SHORT (%offPanels% + %i% * 0x1c + 0x18) %idxPanelControl%
  END

//Externalise Race Selection StrRef
COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/override/racetext.2da" "override/racetext.2da"

//Enlarge Tooltip Scroll
COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/override/toolscrl.bam" "override/toolscrl.bam"

//Scrollable Mage Spellbook
COPY_EXISTING "guimg.chu" "override/guimg.chu"
  READ_LONG 0x8 nPanels
  READ_LONG 0xc offControlTbl
  READ_LONG 0x10 offPanels

  SET i = 0
  SET nControls = 0
  FOR (i = 0; i < nPanels; i+= 1) BEGIN
      READ_SHORT (%offPanels% + %i% * 0x1c + 0xe) nPanelControls
      SET nControls += nPanelControls
  END

  //add scroll bar
  SET currPanelIdx  = 2
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) nPanelControls
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0x18) idxPanelControl

  //add control
  READ_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) offControlThis
  INSERT_BYTES (%offControlThis%) 0x20
  WRITE_SHORT (%offControlThis%) 65 //controlIdx
  WRITE_SHORT (%offControlThis% + 0x2) 0 //bufferSize
  WRITE_SHORT (%offControlThis% + 0x4) 423 //x
  WRITE_SHORT (%offControlThis% + 0x6) 240 //y
  WRITE_SHORT (%offControlThis% + 0x8) 11 //width
  WRITE_SHORT (%offControlThis% + 0xa) 28 //height
  WRITE_BYTE (%offControlThis% + 0xc) 0 //type = BUTTON
  WRITE_BYTE (%offControlThis% + 0xd) 0 //unknown
  WRITE_ASCII (%offControlThis% + 0xe) "GUISVRB" #8 //BAM
  WRITE_BYTE (%offControlThis% + 0x16) 0 //anim cycle
  WRITE_BYTE (%offControlThis% + 0x17) 0 //text flags
  WRITE_SHORT (%offControlThis% + 0x18) 0 //frame unpressed
  WRITE_SHORT (%offControlThis% + 0x1a) 1 //frame pressed
  WRITE_SHORT (%offControlThis% + 0x1c) 0 //frame selected
  WRITE_SHORT (%offControlThis% + 0x1e) 0 //frame disabled

  //updating all existing control table offsets
  FOR (i = %idxPanelControl% + %nPanelControls%; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x20
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //add control table entry
  INSERT_BYTES (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) 0x8
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) %offControlThis% //offset
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8 + 0x4) 0x20 //length

  SET nControls += 1

  //update all control table offsets
  FOR (i = 0; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x8
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //update control numbers
  SET nPanelControls += 1
  WRITE_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) %nPanelControls%

  //update panel control indices
  FOR (i = %currPanelIdx% + 1; i < nPanels; i += 1) BEGIN
    READ_SHORT (%offPanels% + %i% * 0x1c + 0x18) idxPanelControl
    SET idxPanelControl += 1
    WRITE_SHORT (%offPanels% + %i% * 0x1c + 0x18) %idxPanelControl%
  END
  


  //add button
  SET currPanelIdx  = 2
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) nPanelControls
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0x18) idxPanelControl

  //add control
  READ_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) offControlThis
  INSERT_BYTES (%offControlThis%) 0x20
  WRITE_SHORT (%offControlThis%) 66 //controlIdx
  WRITE_SHORT (%offControlThis% + 0x2) 0 //bufferSize
  WRITE_SHORT (%offControlThis% + 0x4) 423 //x
  WRITE_SHORT (%offControlThis% + 0x6) 270 //y
  WRITE_SHORT (%offControlThis% + 0x8) 11 //width
  WRITE_SHORT (%offControlThis% + 0xa) 28 //height
  WRITE_BYTE (%offControlThis% + 0xc) 0 //type = BUTTON
  WRITE_BYTE (%offControlThis% + 0xd) 0 //unknown
  WRITE_ASCII (%offControlThis% + 0xe) "GUISVRB" #8 //BAM
  WRITE_BYTE (%offControlThis% + 0x16) 1 //anim cycle
  WRITE_BYTE (%offControlThis% + 0x17) 0 //text flags
  WRITE_SHORT (%offControlThis% + 0x18) 0 //frame unpressed
  WRITE_SHORT (%offControlThis% + 0x1a) 1 //frame pressed
  WRITE_SHORT (%offControlThis% + 0x1c) 0 //frame selected
  WRITE_SHORT (%offControlThis% + 0x1e) 0 //frame disabled

  //updating all existing control table offsets
  FOR (i = %idxPanelControl% + %nPanelControls%; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x20
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //add control table entry
  INSERT_BYTES (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) 0x8
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) %offControlThis% //offset
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8 + 0x4) 0x20 //length

  SET nControls += 1

  //update all control table offsets
  FOR (i = 0; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x8
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //update control numbers
  SET nPanelControls += 1
  WRITE_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) %nPanelControls%

  //update panel control indices
  FOR (i = %currPanelIdx% + 1; i < nPanels; i += 1) BEGIN
    READ_SHORT (%offPanels% + %i% * 0x1c + 0x18) idxPanelControl
    SET idxPanelControl += 1
    WRITE_SHORT (%offPanels% + %i% * 0x1c + 0x18) %idxPanelControl%
  END

//Scrollable Priest Spellbook
COPY_EXISTING "guipr.chu" "override/guipr.chu"
  READ_LONG 0x8 nPanels
  READ_LONG 0xc offControlTbl
  READ_LONG 0x10 offPanels
  
  SET i = 0
  SET nControls = 0
  FOR (i = 0; i < nPanels; i+= 1) BEGIN
      READ_SHORT (%offPanels% + %i% * 0x1c + 0xe) nPanelControls
      SET nControls += nPanelControls
  END

  //add scroll bar
  SET currPanelIdx  = 2
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) nPanelControls
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0x18) idxPanelControl

  //add control
  READ_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) offControlThis
  INSERT_BYTES (%offControlThis%) 0x20
  WRITE_SHORT (%offControlThis%) 62 //controlIdx
  WRITE_SHORT (%offControlThis% + 0x2) 0 //bufferSize
  WRITE_SHORT (%offControlThis% + 0x4) 390 //x
  WRITE_SHORT (%offControlThis% + 0x6) 240 //y
  WRITE_SHORT (%offControlThis% + 0x8) 11 //width
  WRITE_SHORT (%offControlThis% + 0xa) 28 //height
  WRITE_BYTE (%offControlThis% + 0xc) 0 //type = BUTTON
  WRITE_BYTE (%offControlThis% + 0xd) 0 //unknown
  WRITE_ASCII (%offControlThis% + 0xe) "GUISVRB" #8 //BAM
  WRITE_BYTE (%offControlThis% + 0x16) 0 //anim cycle
  WRITE_BYTE (%offControlThis% + 0x17) 0 //text flags
  WRITE_SHORT (%offControlThis% + 0x18) 0 //frame unpressed
  WRITE_SHORT (%offControlThis% + 0x1a) 1 //frame pressed
  WRITE_SHORT (%offControlThis% + 0x1c) 0 //frame selected
  WRITE_SHORT (%offControlThis% + 0x1e) 0 //frame disabled

  //updating all existing control table offsets
  FOR (i = %idxPanelControl% + %nPanelControls%; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x20
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //add control table entry
  INSERT_BYTES (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) 0x8
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) %offControlThis% //offset
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8 + 0x4) 0x20 //length

  SET nControls += 1

  //update all control table offsets
  FOR (i = 0; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x8
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //update control numbers
  SET nPanelControls += 1
  WRITE_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) %nPanelControls%

  //update panel control indices
  FOR (i = %currPanelIdx% + 1; i < nPanels; i += 1) BEGIN
    READ_SHORT (%offPanels% + %i% * 0x1c + 0x18) idxPanelControl
    SET idxPanelControl += 1
    WRITE_SHORT (%offPanels% + %i% * 0x1c + 0x18) %idxPanelControl%
  END
  


  //add button
  SET currPanelIdx  = 2
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) nPanelControls
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0x18) idxPanelControl

  //add control
  READ_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) offControlThis
  INSERT_BYTES (%offControlThis%) 0x20
  WRITE_SHORT (%offControlThis%) 63 //controlIdx
  WRITE_SHORT (%offControlThis% + 0x2) 0 //bufferSize
  WRITE_SHORT (%offControlThis% + 0x4) 390 //x
  WRITE_SHORT (%offControlThis% + 0x6) 270 //y
  WRITE_SHORT (%offControlThis% + 0x8) 11 //width
  WRITE_SHORT (%offControlThis% + 0xa) 28 //height
  WRITE_BYTE (%offControlThis% + 0xc) 0 //type = BUTTON
  WRITE_BYTE (%offControlThis% + 0xd) 0 //unknown
  WRITE_ASCII (%offControlThis% + 0xe) "GUISVRB" #8 //BAM
  WRITE_BYTE (%offControlThis% + 0x16) 1 //anim cycle
  WRITE_BYTE (%offControlThis% + 0x17) 0 //text flags
  WRITE_SHORT (%offControlThis% + 0x18) 0 //frame unpressed
  WRITE_SHORT (%offControlThis% + 0x1a) 1 //frame pressed
  WRITE_SHORT (%offControlThis% + 0x1c) 0 //frame selected
  WRITE_SHORT (%offControlThis% + 0x1e) 0 //frame disabled

  //updating all existing control table offsets
  FOR (i = %idxPanelControl% + %nPanelControls%; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x20
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //add control table entry
  INSERT_BYTES (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) 0x8
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) %offControlThis% //offset
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8 + 0x4) 0x20 //length

  SET nControls += 1

  //update all control table offsets
  FOR (i = 0; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x8
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //update control numbers
  SET nPanelControls += 1
  WRITE_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) %nPanelControls%

  //update panel control indices
  FOR (i = %currPanelIdx% + 1; i < nPanels; i += 1) BEGIN
    READ_SHORT (%offPanels% + %i% * 0x1c + 0x18) idxPanelControl
    SET idxPanelControl += 1
    WRITE_SHORT (%offPanels% + %i% * 0x1c + 0x18) %idxPanelControl%
  END
  
//Scrollable Chargen Mage Spell Selection
COPY_EXISTING "guicg.chu" "override/guicg.chu"
  READ_LONG 0x8 nPanels
  READ_LONG 0xc offControlTbl
  READ_LONG 0x10 offPanels
  
  SET i = 0
  SET nControls = 0
  FOR (i = 0; i < nPanels; i+= 1) BEGIN
      READ_SHORT (%offPanels% + %i% * 0x1c + 0xe) nPanelControls
      SET nControls += nPanelControls
  END

  //add scroll bar
  SET currPanelIdx  = 7
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) nPanelControls
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0x18) idxPanelControl

  //add control
  READ_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) offControlThis
  INSERT_BYTES (%offControlThis%) 0x28
  WRITE_SHORT (%offControlThis%) 31 //controlIdx
  WRITE_SHORT (%offControlThis% + 0x2) 0 //bufferSize
  WRITE_SHORT (%offControlThis% + 0x4) 325 //x
  WRITE_SHORT (%offControlThis% + 0x6) 62 //y
  WRITE_SHORT (%offControlThis% + 0x8) 16 //width
  WRITE_SHORT (%offControlThis% + 0xa) 220 //height
  WRITE_BYTE (%offControlThis% + 0xc) 7 //type = SCROLL_BAR
  WRITE_BYTE (%offControlThis% + 0xd) 0 //unknown
  WRITE_ASCII (%offControlThis% + 0xe) "GUISCRCW" #8 //BAM
  WRITE_SHORT (%offControlThis% + 0x16) 0 //anim cycle
  WRITE_SHORT (%offControlThis% + 0x18) 0 //frame-up unpressed
  WRITE_SHORT (%offControlThis% + 0x1a) 1 //frame-up pressed
  WRITE_SHORT (%offControlThis% + 0x1c) 2 //frame-down unpressed
  WRITE_SHORT (%offControlThis% + 0x1e) 3 //frame-down pressed
  WRITE_SHORT (%offControlThis% + 0x20) 5 //frame trough
  WRITE_SHORT (%offControlThis% + 0x22) 4 //frame slider
  WRITE_LONG (%offControlThis% + 0x24) 0xFFFFFFFF //text area id

  //updating all existing control table offsets
  FOR (i = %idxPanelControl% + %nPanelControls%; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x28
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //add control table entry
  INSERT_BYTES (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) 0x8
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) %offControlThis% //offset
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8 + 0x4) 0x28 //length

  SET nControls += 1

  //update all control table offsets
  FOR (i = 0; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x8
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //update control numbers
  SET nPanelControls += 1
  WRITE_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) %nPanelControls%

  //update panel control indices
  FOR (i = %currPanelIdx% + 1; i < nPanels; i += 1) BEGIN
    READ_SHORT (%offPanels% + %i% * 0x1c + 0x18) idxPanelControl
    SET idxPanelControl += 1
    WRITE_SHORT (%offPanels% + %i% * 0x1c + 0x18) %idxPanelControl%
  END
  
//Scrollable Level Up Mage Spell Selection
COPY_EXISTING "guirec.chu" "override/guirec.chu"
  READ_LONG 0x8 nPanels
  READ_LONG 0xc offControlTbl
  READ_LONG 0x10 offPanels
  
  SET i = 0
  SET nControls = 0
  FOR (i = 0; i < nPanels; i+= 1) BEGIN
      READ_SHORT (%offPanels% + %i% * 0x1c + 0xe) nPanelControls
      SET nControls += nPanelControls
  END

  //add scroll bar
  SET currPanelIdx  = 8
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) nPanelControls
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0x18) idxPanelControl

  //add control
  READ_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) offControlThis
  INSERT_BYTES (%offControlThis%) 0x28
  WRITE_SHORT (%offControlThis%) 30 //controlIdx
  WRITE_SHORT (%offControlThis% + 0x2) 0 //bufferSize
  WRITE_SHORT (%offControlThis% + 0x4) 276 //x
  WRITE_SHORT (%offControlThis% + 0x6) 132 //y
  WRITE_SHORT (%offControlThis% + 0x8) 16 //width
  WRITE_SHORT (%offControlThis% + 0xa) 266 //height
  WRITE_BYTE (%offControlThis% + 0xc) 7 //type = SCROLL_BAR
  WRITE_BYTE (%offControlThis% + 0xd) 0 //unknown
  WRITE_ASCII (%offControlThis% + 0xe) "GUISCRCW" #8 //BAM
  WRITE_SHORT (%offControlThis% + 0x16) 0 //anim cycle
  WRITE_SHORT (%offControlThis% + 0x18) 0 //frame-up unpressed
  WRITE_SHORT (%offControlThis% + 0x1a) 1 //frame-up pressed
  WRITE_SHORT (%offControlThis% + 0x1c) 2 //frame-down unpressed
  WRITE_SHORT (%offControlThis% + 0x1e) 3 //frame-down pressed
  WRITE_SHORT (%offControlThis% + 0x20) 5 //frame trough
  WRITE_SHORT (%offControlThis% + 0x22) 4 //frame slider
  WRITE_LONG (%offControlThis% + 0x24) 0xFFFFFFFF //text area id

  //updating all existing control table offsets
  FOR (i = %idxPanelControl% + %nPanelControls%; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x28
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //add control table entry
  INSERT_BYTES (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) 0x8
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) %offControlThis% //offset
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8 + 0x4) 0x28 //length

  SET nControls += 1

  //update all control table offsets
  FOR (i = 0; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x8
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //update control numbers
  SET nPanelControls += 1
  WRITE_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) %nPanelControls%

  //update panel control indices
  FOR (i = %currPanelIdx% + 1; i < nPanels; i += 1) BEGIN
    READ_SHORT (%offPanels% + %i% * 0x1c + 0x18) idxPanelControl
    SET idxPanelControl += 1
    WRITE_SHORT (%offPanels% + %i% * 0x1c + 0x18) %idxPanelControl%
  END
  

  //add button
  SET currPanelIdx = 8
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) nPanelControls
  READ_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0x18) idxPanelControl

  //add control
  READ_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) offControlThis
  INSERT_BYTES (%offControlThis%) 0x20
  WRITE_SHORT (%offControlThis%) 24 //controlIdx
  WRITE_SHORT (%offControlThis% + 0x2) 0 //bufferSize
  WRITE_SHORT (%offControlThis% + 0x4) 231 //x
  WRITE_SHORT (%offControlThis% + 0x6) 345 //y
  WRITE_SHORT (%offControlThis% + 0x8) 42 //width
  WRITE_SHORT (%offControlThis% + 0xa) 42 //height
  WRITE_BYTE (%offControlThis% + 0xc) 0 //type = BUTTON
  WRITE_BYTE (%offControlThis% + 0xd) 0 //unknown
  WRITE_ASCII (%offControlThis% + 0xe) "GUIBTBUT" #8 //BAM
  WRITE_BYTE (%offControlThis% + 0x16) 2 //anim cycle
  WRITE_BYTE (%offControlThis% + 0x17) 0 //text flags
  WRITE_SHORT (%offControlThis% + 0x18) 0 //frame unpressed
  WRITE_SHORT (%offControlThis% + 0x1a) 1 //frame pressed
  WRITE_SHORT (%offControlThis% + 0x1c) 0 //frame selected
  WRITE_SHORT (%offControlThis% + 0x1e) 0 //frame disabled

  //updating all existing control table offsets
  FOR (i = %idxPanelControl% + %nPanelControls%; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x20
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //add control table entry
  INSERT_BYTES (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) 0x8
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8) %offControlThis% //offset
  WRITE_LONG (%offControlTbl% + (%idxPanelControl% + %nPanelControls%) * 0x8 + 0x4) 0x20 //length

  SET nControls += 1

  //update all control table offsets
  FOR (i = 0; i < nControls; i += 1) BEGIN
    READ_SHORT (%offControlTbl% + %i% * 0x8) offControl
    SET offControl += 0x8
    WRITE_SHORT (%offControlTbl% + %i% * 0x8) %offControl%
  END

  //update control numbers
  SET nPanelControls += 1
  WRITE_SHORT (%offPanels% + %currPanelIdx% * 0x1c + 0xe) %nPanelControls%

  //update panel control indices
  FOR (i = %currPanelIdx% + 1; i < nPanels; i += 1) BEGIN
    READ_SHORT (%offPanels% + %i% * 0x1c + 0x18) idxPanelControl
    SET idxPanelControl += 1
    WRITE_SHORT (%offPanels% + %i% * 0x1c + 0x18) %idxPanelControl%
  END
  
//Externalise Mage Spell Hiding
COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/override/hidespl.2da" "override/hidespl.2da"

//Externalise Class-Race Restrictions
COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/override/clsrcreq.2da" "override/clsrcreq.2da"
     "%TOBEX_MOD_DIRECTORY%/TobEx_redist/override/mgsrcreq.2da" "override/mgsrcreq.2da"

//Externalise Animation Walking Sounds
COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/override/aniwksnd.2da" "override/aniwksnd.2da"
     "%TOBEX_MOD_DIRECTORY%/TobEx_redist/override/anitnsnd.2da" "override/anitnsnd.2da"

//IWD Style Strength Mod and IWD Style Dexterity Mod
COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/override/clssplab.2da" "override/clssplab.2da"

//Externalise Item-Creature Exclusions
COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/override/itcrexcl.2da" "override/itcrexcl.2da"

//Externalise Encumbrance Restrictions
COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/override/encumber.2da" "override/encumber.2da"

//Expanded Stats
APPEND "stats.ids" "301 FIGHTERLEVEL" UNLESS "301 FIGHTERLEVEL"
APPEND "stats.ids" "302 MAGELEVEL" UNLESS "302 MAGELEVEL"
APPEND "stats.ids" "303 CLERICLEVEL" UNLESS "303 CLERICLEVEL"
APPEND "stats.ids" "304 THIEFLEVEL" UNLESS "304 THIEFLEVEL"
APPEND "stats.ids" "305 DRUIDLEVEL" UNLESS "305 DRUIDLEVEL"
APPEND "stats.ids" "306 RANGERLEVEL" UNLESS "306 RANGERLEVEL"
APPEND "stats.ids" "307 EFFECTIVECLERICLEVEL" UNLESS "307 EFFECTIVECLERICLEVEL"
APPEND "stats.ids" "400 WEIGHTALLOWANCEMOD" UNLESS "400 WEIGHTALLOWANCEMOD"

//Externalise HP Tables
COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/override/hpclass.2da" "override/hpclass.2da"
COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/override/hpbarb.2da" "override/hpbarb.2da"
