// *** Removes Wild Mage kit from character generation ***

// generating list of available race/class combination files
OUTER_SET combi = 0
COPY_EXISTING - ~kittable.2da~ ~override~
  COUNT_2DA_COLS numCols
  COUNT_2DA_ROWS numCols numRows
  FOR (row = 0; row < numRows; ++row) BEGIN
    READ_2DA_ENTRY row 0 numCols value
    PATCH_IF (~%value%~ STR_EQ ~MAGE~) BEGIN
      FOR (col = 1; col < numCols; ++col) BEGIN
        READ_2DA_ENTRY row col numCols value
        PATCH_IF (FILE_EXISTS_IN_GAME ~%value%.2DA~) BEGIN
          TEXT_SPRINT EVAL ~combi_%combi%~ ~%value%.2DA~
          SET combi += 1
        END
      END
      SET row = numRows
    END
  END

ACTION_IF (combi > 0) BEGIN
  // getting Wild Mage kit id
  OUTER_SET kit_id = "-1"
  COPY_EXISTING - ~kitlist.2da~ ~override~
    COUNT_2DA_COLS numCols
    COUNT_2DA_ROWS numCols numRows
    FOR (row = 0; row < numRows; ++row) BEGIN
      READ_2DA_ENTRY row 1 numCols value
      PATCH_IF (~%value%~ STR_EQ ~WILDMAGE~) BEGIN
        SET kit_id = row
        SET row = numRows
      END
    END

  // disabling Wild Mage kit for each race/class combo
  ACTION_IF (kit_id > 0) BEGIN
    OUTER_FOR (idx = 0; idx < combi; ++idx) BEGIN
      OUTER_TEXT_SPRINT file EVAL ~%combi_%idx%%~
      COPY_EXISTING ~%file%~ ~override~
        COUNT_2DA_COLS numCols
        COUNT_2DA_ROWS numCols numRows
        PATCH_IF (numCols >= 2) BEGIN
          SET startIndex = (numCols = 2) ? 1 : 0
          FOR (row = startIndex; row < numRows; ++row) BEGIN
            READ_2DA_ENTRY row 1 numCols value
            PATCH_IF (IS_AN_INT ~value~ && value = kit_id) BEGIN
              REMOVE_2DA_ROW row numCols
              SET row = numRows
            END
          END
        END
      BUT_ONLY IF_EXISTS
    END
  END
END
