/*
 * fl#add_kit_ee - An ADD_KIT supplement for EE-type games
 * Author: Fredrik Lindgren, a.k.a., Wisp
 * Licence: public domain
 * Date: 2016-04-02
 */

OUTER_SPRINT fl#add_kit_ee#version "1.1.2"

DEFINE_ACTION_FUNCTION fl#add_kit_ee#get_row
  STR_VAR
    file = ""
    row_rame = ""
    column = 0
  RET
    row
BEGIN
  COPY_EXISTING "%file%.2da" override
    COUNT_2DA_COLS num_col
    READ_2DA_ENTRIES_NOW file num_col
    SPRINT row ""
    FOR (i = 0; i < file; ++i) BEGIN
      READ_2DA_ENTRY_FORMER file i column label
      PATCH_IF "%label%" STRING_EQUAL_CASE "%row_name%" BEGIN
        FOR (j = 1; j < num_col; ++j) BEGIN
          READ_2DA_ENTRY_FORMER file i j item
          SPRINT row "%row% %item%"
        END
      END
    END
  BUT_ONLY
END

DEFINE_ACTION_FUNCTION fl#add_kit_ee#get_column
  STR_VAR
    file = ""
    column_name = ""
  RET
    column
BEGIN
  COPY_EXISTING "%file%.2da" override
    COUNT_2DA_COLS num_col
    READ_2DA_ENTRIES_NOW file num_col - 1
    SPRINT column ""
    FOR (i = 0; i < num_col - 1; ++i) BEGIN
      READ_2DA_ENTRY_FORMER file 0 i label
      PATCH_IF "%label%" STRING_EQUAL_CASE "%column_name%" BEGIN
        FOR (j = 1; j < file; ++j) BEGIN
          READ_2DA_ENTRY_FORMER file j i + 1 item
          SPRINT column "%column% %item%"
        END
      END
    END
  BUT_ONLY
END

DEFINE_ACTION_FUNCTION fl#add_kit_ee#add_row
  STR_VAR
    kit_name = ""
    input = ""
    file = ""
    clone = ""
BEGIN
  ACTION_IF "%input%" STR_CMP "" BEGIN
    OUTER_SPRINT append "%kit_name% %input%"
    APPEND "%file%.2da" "%append%" UNLESS "%kit_name%"
  END ELSE ACTION_IF "%clone%" STR_CMP "" BEGIN
    LAF fl#add_kit_ee#get_row
      STR_VAR
        file
        row_name = EVAL "%clone%"
      RET
        row
    END
    OUTER_SPRINT append "%kit_name% %row%"
    APPEND "%file%.2da" "%append%" UNLESS "%kit_name%"
  END
END

DEFINE_ACTION_FUNCTION fl#add_kit_ee#add_column
  STR_VAR
    kit_name = ""
    input = ""
    file = ""
    clone = ""
BEGIN
  ACTION_IF "%input%" STR_CMP "" BEGIN
    OUTER_SPRINT append "$ $ %kit_name% %input%"
    APPEND_COL "%file%.2da" "%append%" UNLESS "%kit_name%"
  END ELSE ACTION_IF "%clone%" STR_CMP "" BEGIN
    LAF fl#add_kit_ee#get_column
      STR_VAR
        file
        column_name = EVAL "%clone%"
      RET
        column
    END
    OUTER_SPRINT append "$ $ %kit_name% %column%"
    APPEND_COL "%file%.2da" "%append%" UNLESS "%kit_name%"
  END
END

DEFINE_ACTION_FUNCTION fl#add_kit_ee#validate
  INT_VAR
    kit_number = 0
    kit_class = 0
  STR_VAR
    kit_name = ""
    backstab = ""
    clswpbon = ""
    hpclass = ""
    numwslot = ""
    thiefskl = ""
    traplimt = ""
    clascolr = ""
    clasiskl = ""
    clasthac = ""
    thiefscl = ""
    sneakatt = ""
    crippstr = ""
  RET
    backstab
    thiefskl
    traplimt
    sneakatt
    crippstr
BEGIN
  ACTION_IF kit_class != 4 AND ("%backstab%" STR_CMP "" OR
                                "%thiefskl%" STR_CMP "" OR
                                "%traplimt%" STR_CMP "" OR
                                "%sneakatt%" STR_CMP "" OR
                                "%crippstr%" STR_CMP "")
  BEGIN
    WARN ~WARNING: the arguments for thief-only 2DAs should only be provided for thief kits~
    OUTER_SPRINT backstab ""
    OUTER_SPRINT thiefskl ""
    OUTER_SPRINT traplimt ""
    OUTER_SPRINT sneakatt ""
    OUTER_SPRINT crippstr ""
  END
END

DEFINE_PATCH_FUNCTION fl#add_kit_ee#get_2da_value
  STR_VAR
    row = ""
    column = ""
    req_column_count = 2
    value = ""
  RET
    value
BEGIN
  COUNT_2DA_COLS num_col
  READ_2DA_ENTRIES_NOW file req_column_count
  FOR (i = 0; i < file; ++i) BEGIN
    READ_2DA_ENTRY_FORMER file i 0 row_label
    PATCH_IF "%row_label%" STRING_EQUAL_CASE "%row%" BEGIN
      // We assume the top row is off by one
      FOR (j = 0; j < num_col - 1; ++j) BEGIN
        READ_2DA_ENTRY_FORMER file 0 j col_label
        PATCH_IF "%col_label%" STRING_EQUAL_CASE "%column%" BEGIN
          READ_2DA_ENTRY_FORMER file i j + 1 value
          i = file
          j = num_col
        END
      END
    END
  END
  PATCH_IF "%value%" STRING_EQUAL "" BEGIN
    PATCH_FAIL "ERROR: fl#add_kit_ee failed to retrieve a value for column %column% and row %row% in %SOURCE_FILE%"
  END
END

DEFINE_PATCH_MACRO fl#add_kit_ee#validate_kitlist_values BEGIN
  PATCH_IF IS_AN_INT kit_number AND kit_number < 0 BEGIN
    PATCH_FAIL ~ERROR: fl#add_kit_ee could not find the kit %kit_name% in kitlist.2da~
  END
  PATCH_IF !IS_AN_INT kit_number BEGIN
    PATCH_FAIL ~ERROR: fl#add_kit_ee could not read a valid kit number for kit %kit_name% from kitlist.2da~
  END
  PATCH_IF !IS_AN_INT kit_lower BEGIN
    PATCH_FAIL ~ERROR: fl#add_kit_ee could not read a valid lower strref for kit %kit_name% from kitlist.2da~
  END
  PATCH_IF !IS_AN_INT kit_mixed BEGIN
    PATCH_FAIL ~ERROR: fl#add_kit_ee could not read a valid mixed strref for kit %kit_name% from kitlist.2da~
  END
  PATCH_IF !IS_AN_INT kit_desc BEGIN
    PATCH_FAIL ~ERROR: fl#add_kit_ee could not read a valid help strref for kit %kit_name% from kitlist.2da~
  END
  PATCH_IF !IS_AN_INT kit_class BEGIN
    PATCH_FAIL ~ERROR: fl#add_kit_ee could not read a valid class for kit %kit_name% from kitlist.2da~
  END
END

DEFINE_PATCH_MACRO fl#add_kit_ee#validate_kitids_value BEGIN
  PATCH_IF !"%number%" STRING_MATCHES_REGEXP "0x\([0-9]+\)" = 0 BEGIN
    PATCH_FAIL ~ERROR: fl#add_kit_ee could not read a valid kit number for %kit_name% from kit.ids~
  END
END

DEFINE_PATCH_MACRO fl#add_kit_ee#validate_clastext_value BEGIN
  PATCH_IF !IS_AN_INT value BEGIN
    PATCH_FAIL ~ERROR: fl#add_kit_ee could not read a valid value for column %column% for class %class% from clastext.2da~
  END
END

DEFINE_ACTION_FUNCTION fl#add_kit_ee
  INT_VAR
    biography = "-2"
    fallen = 0
    briefdesc = "-2"
    fallen_notice = "-2"
  STR_VAR
    kit_name = ""
    backstab = ""
    clsrcreq = ""
    clswpbon = ""
    hpclass = ""
    numwslot = ""
    thiefskl = ""
    traplimt = ""
    clascolr = ""
    clasiskl = ""
    clasthac = ""
    thiefscl = ""
    sneakatt = ""
    crippstr = ""
BEGIN
  ACTION_IF FILE_EXISTS_IN_GAME monkfist.2da BEGIN // "Rule" file as-of-yet unique to EE games
    PRINT "Doing EE-type kit extensions for kit %kit_name%"

    OUTER_SET kit_number = "-1"

    COPY_EXISTING kitlist.2da override
      READ_2DA_ENTRIES_NOW kitlist 9
      FOR (i = 0; i < kitlist; ++i) BEGIN
        READ_2DA_ENTRY_FORMER kitlist i 1 k
        PATCH_IF "%k%" STRING_EQUAL_CASE "%kit_name%" BEGIN
          READ_2DA_ENTRY_FORMER kitlist i 0 kit_number
          READ_2DA_ENTRY_FORMER kitlist i 2 kit_lower
          READ_2DA_ENTRY_FORMER kitlist i 3 kit_mixed
          READ_2DA_ENTRY_FORMER kitlist i 4 kit_desc
          READ_2DA_ENTRY_FORMER kitlist i 8 kit_class
        END
      END
      LPM fl#add_kit_ee#validate_kitlist_values
    BUT_ONLY

    LAF fl#add_kit_ee#validate
      INT_VAR
        kit_number
        kit_class
      STR_VAR
        kit_name
        backstab
        clsrcreq
        clswpbon
        hpclass
        numwslot
        thiefskl
        traplimt
        clascolr
        clasiskl
        clasthac
        thiefscl
        sneakatt
        crippstr
      RET
        backstab
        thiefskl
        traplimt
        sneakatt
        crippstr
    END

    COPY_EXISTING kitlist.2da override
      INNER_ACTION BEGIN
        COPY_EXISTING kit.ids override
          READ_2DA_ENTRIES_NOW file 2
          FOR (i = file - 1; i >= 0; --i) BEGIN
            READ_2DA_ENTRY_FORMER file i 1 k
            PATCH_IF "%k%" STRING_EQUAL_CASE "%kit_name%" BEGIN
              READ_2DA_ENTRY_FORMER file i 0 number
            END
          END
          LPM fl#add_kit_ee#validate_kitids_value
        BUT_ONLY
      END
      INNER_PATCH_SAVE kitids "%number%" BEGIN
        REPLACE_TEXTUALLY "0x\([0-9]+\)" "0x0000\1"
      END
      SPRINT w "[ %TAB%]+"
      SPRINT ow "[ %TAB%]*"
      SPRINT d "[0-9]+"
      SPRINT h "[0-9x]+"
      REPLACE_TEXTUALLY ~\(%d%%w%%kit_name%%w%%d%%w%%d%%w%%d%%w%.+%w%%d%%w%%h%%w%%d%%ow%$\)~ ~\1 %kitids%~
    BUT_ONLY

    OUTER_PATCH 0 BEGIN
      LOOKUP_IDS_SYMBOL_OF_INT class class kit_class
    END

    ACTION_IF biography < "-1" OR briefdesc < "-1" OR fallen_notice < "-1" BEGIN
      COPY_EXISTING clastext.2da override
        PATCH_FOR_EACH column IN biography briefdesc fallen_notice BEGIN
          PATCH_IF EVAL "%column%" < "-1" AND FILE_CONTAINS_EVALUATED (clastext.2da "%column%") BEGIN
            LPF fl#add_kit_ee#get_2da_value
              STR_VAR
                row = EVAL "%class%"
                column
                req_column_count = 8
              RET
                value
            END
            LPM fl#add_kit_ee#validate_clastext_value
            SET EVAL "%column%" = value
          END
        END
      BUT_ONLY
    END

    OUTER_SPRINT clastext "%kit_name% %kit_class% %kit_number% %kit_lower% %kit_desc% %kit_mixed% %biography% %fallen% %briefdesc%"

    ACTION_IF FILE_CONTAINS_EVALUATED (clastext.2da "FALLEN_NOTICE") BEGIN
      OUTER_SPRINT clastext "%clastext% %fallen_notice%"
    END

    APPEND clastext.2da "%clastext%" UNLESS "%kit_name%"

    ACTION_DEFINE_ARRAY row_files BEGIN
      backstab
      clsrcreq
      clswpbon
      hpclass
      numwslot
      thiefskl
      traplimt
      sneakatt
      crippstr
    END

    ACTION_DEFINE_ARRAY col_files BEGIN
      clascolr
      clasiskl
      clasthac
      thiefscl
    END

    ACTION_PHP_EACH row_files AS _ => item BEGIN
      ACTION_IF FILE_EXISTS_IN_GAME "%item%.2da" BEGIN
        ACTION_IF FILE_CONTAINS_EVALUATED ("%item%.2da" "^[ %TAB%]*%class%[ %TAB%]+") BEGIN
          OUTER_SPRINT clone "%class%"
        END ELSE BEGIN
          OUTER_SPRINT clone ""
        END
        LAF fl#add_kit_ee#add_row
          STR_VAR
            kit_name
            input = EVAL EVAL "%%item%%"
            file = EVAL "%item%"
            clone
        END
      END
    END

    ACTION_PHP_EACH col_files AS _ => item BEGIN
      ACTION_IF FILE_EXISTS_IN_GAME "%item%.2da" BEGIN
        LAF fl#add_kit_ee#add_column
          STR_VAR
            kit_name
            input = EVAL EVAL "%%item%%"
            file = EVAL "%item%"
            clone = EVAL "%class%"
        END
      END
    END

    ACTION_DEFINE_ARRAY cleanup BEGIN kitlist END
    OUTER_SET index = 1

    ACTION_PHP_EACH row_files AS _ => file BEGIN
      OUTER_SPRINT $cleanup("%index%") "%file%"
      OUTER_SET ++index
    END
    ACTION_PHP_EACH col_files AS _ => file BEGIN
      OUTER_SPRINT $cleanup("%index%") "%file%"
      OUTER_SET ++index
    END

    ACTION_PHP_EACH cleanup AS _ => file BEGIN
      ACTION_IF FILE_EXISTS_IN_GAME "%file%.2da" BEGIN
        COPY_EXISTING "%file%.2da" override
          PRETTY_PRINT_2DA
        BUT_ONLY
      END
    END
  END
END
