Author Topic: Grouping journal entries in Enhanced Edition patch 2.0  (Read 1703 times)

Offline Argent77

  • Planewalker
  • *****
  • Posts: 187
Patch v2.0 of the Enhanced Editions changed the way how quest entries are organized in the journal (again). Since WeiDU does not yet support this method I have coded a function for my mods which takes care of it in a similar fashion as ADD_JOURNAL. Feel free to use it in your own mods.

Function ADD_JOURNAL_V2 (requires WeiDU v240 or higher):
Code: [Select]
/**
 * This action function patches the BGEE.LUA of Enhanced Edition games v2.0 or higher with the
 * provided journal entries, so they will be correctly added to the quest tab of the journal.
 * The function does nothing if the game is not an Enhanced Edition game or is patched to an
 * earlier version. Requires WeiDU version 240 or higher!
 *
 * INT_VAR as_strref  Defines how journal title and entries are treated.
 *                    Set to 0 if journal entries are TRA references.
 *                    Set to non-zero if journal entries are straight strref values.
 *                    (Default: 0)
 * INT_VAR existing   Indicates whether to add entries to an existing journal group.
 *                    Set to 0 if you want to force this function to create a new journal group
 *                    even if a group of the exact same title already exists.
 *                    Set to non-zero if you want to reuse existing journal groups when available.
 *                    (Default: 0)
 * INT_VAR title      A optional title for the journal group where all entries are added to,
 *                    defined as TRA reference or strref value (depending on "as_strref").
 *                    If no title is given then the first line of the first available journal entry
 *                    is used instead.
 * STR_VAR entries    An array of TRA references or strref values (depending on "as_strref") for the
 *                    respective journal entries that are added to the current journal group.
 *                    You can use ACTION_DEFINE_ARRAY or ACTION_DEFINE_ASSOCIATIVE_ARRAY to build
 *                    the array. (Note: This function uses the value part of associative arrays.)
 * STR_VAR traFile    An optional TRA file that will be used for the TRA references defined in
 *                    "entries".
 * RET success        Returns 1 on success and 0 on recoverable error or if game is not supported.
 */
DEFINE_ACTION_FUNCTION ADD_JOURNAL_V2
INT_VAR
  as_strref = 0
  existing  = 0
  title     = "-1"
STR_VAR
  entries   = ~~
  traFile   = ~~
RET
  success
BEGIN
  OUTER_SET success = 0
  WITH_TRA ~%traFile%~ BEGIN
    ACTION_IF (FILE_EXISTS_IN_GAME ~bgee.lua~ AND VARIABLE_IS_SET $EVAL "%entries%"(0)) BEGIN
      // preparing journal title
      ACTION_IF (title < 0) BEGIN
        // extracting title from first journal entry
        OUTER_TEXT_SPRINT tmp $entries(0)
        OUTER_SET strref = as_strref ? tmp : RESOLVE_STR_REF((AT tmp))
        ACTION_GET_STRREF strref text
        OUTER_PATCH_SAVE title_string ~%text%~ BEGIN
          REPLACE_TEXTUALLY ~^\(.+?\)\([%WNL%%LNL%%MNL%]?.*\)*$~ ~\1~
        END
        ACTION_IF (STRING_LENGTH ~%title_string%~ > 0) BEGIN
          ACTION_IF (existing = 0) BEGIN
            // creating unique strref even for existing strings
            OUTER_SET rnd = RANDOM(1000000000 2147483647)
            OUTER_TEXT_SPRINT tmp_string ~A7_dummy_entry_%rnd%~
            OUTER_SET title = RESOLVE_STR_REF(~%tmp_string%~)
            STRING_SET_EVALUATE title ~%title_string%~
          END ELSE BEGIN
            OUTER_SET title = RESOLVE_STR_REF(~%title_string%~)
          END
        END
      END ELSE BEGIN
        // using specified title
        ACTION_IF (as_strref = 0) BEGIN
          ACTION_IF (existing = 0) BEGIN
            // creating unique strref even for existing strings
            OUTER_SET rnd = RANDOM(1000000000 2147483647)
            OUTER_TEXT_SPRINT tmp_string ~A7_dummy_entry_%rnd%~
            OUTER_SET strref = RESOLVE_STR_REF(~%tmp_string%~)
            STRING_SET_EVALUATE strref (AT title)
            OUTER_SET title = strref
          END ELSE BEGIN
            OUTER_SET title = RESOLVE_STR_REF((AT title))
          END
        END
      END

      // processing journal entries
      ACTION_IF (title >= 0) BEGIN
        COPY_EXISTING ~bgee.lua~ ~override~
          READ_ASCII 0 text ELSE ~~ (SOURCE_SIZE)
          // Determining the correct new line sequence
          LPF A7_GET_NEWLINE STR_VAR text = EVAL ~%text%~ RET newline END

          // preparing quest group (doesn't add duplicates)
          PATCH_IF (~%text%~ STRING_CONTAINS_REGEXP ~createQuest[%TAB% ]*([%TAB% ]*%title%[%TAB% ]*)~ = 0) BEGIN
            TEXT_SPRINT createQuest_var ~~
          END ELSE BEGIN
            TEXT_SPRINT createQuest_var ~%newline%%TAB%createQuest    ( %title% )~
          END

          // preparing quest entries
          TEXT_SPRINT createEntry_var ~~
          PHP_EACH entries AS _ => value BEGIN
            SET jour_value = as_strref ? value : RESOLVE_STR_REF((AT value))
            TEXT_SPRINT createEntry_var ~%createEntry_var%%newline%%TAB%createEntry    ( %title%, -1, %jour_value%, {}, nil )~
          END

          // applying changes to BGEE.LUA
          REPLACE_TEXTUALLY ~\(create\(Entry\|Quest\).+)\)[%WNL%%LNL%%MNL%%TAB% ]*end~
                            ~\1%createQuest_var%%createEntry_var%%newline%end~
        BUT_ONLY

        OUTER_SET success = 1
      END ELSE BEGIN
        FAIL ~No quest group title available~
      END
    END
  END
END

// Attempts to determine the new line sequence used in the specified string.
// Defaults to Linux new line.
DEFINE_PATCH_FUNCTION A7_GET_NEWLINE
STR_VAR
  text = ~~
RET
  newline
BEGIN
  PATCH_IF (~%text%~ STRING_CONTAINS_REGEXP ~%WNL%~ = 0) BEGIN
    TEXT_SPRINT newline ~%WNL%~
  END ELSE PATCH_IF (~%text%~ STRING_CONTAINS_REGEXP ~%MNL%~ = 0) BEGIN
    TEXT_SPRINT newline ~%MNL%~
  END ELSE BEGIN
    TEXT_SPRINT newline ~%LNL%~
  END
END


Example usage:

The setup-example.tp2:
Code: [Select]
BACKUP ~Example/backup~
AUTHOR ~Myself~
VERSION ~1.0~

LANGUAGE  ~English~ ~english~ ~Example/languages/english/setup.tra~

BEGIN @100  // Journal example
  REQUIRE_PREDICATE GAME_IS ~bgee sod bg2ee eet iwdee~ @101 // Enhanced Edition game is required.

  INCLUDE ~%MOD_FOLDER%/lib/journals.tph~

  // Add all journal entries for a single quest group into an array.
  // You can either use straight strref values and set the the parameter "as_strref" to 1,
  // or use TRA references (without '@' sign) as done in this example.
  ACTION_DEFINE_ARRAY entries BEGIN 101 102 151 END
  LAF ADD_JOURNAL_V2
    STR_VAR
      entries = ~entries~
      traFile = EVAL ~%MOD_FOLDER%/languages/english/journal.tra~    // using specific TRA file for journal entries
    RET
      success
  END
  ACTION_IF (NOT success) BEGIN
    // falls back to conventional journal function if ADD_JOURNAL_V2 wasn't successful
    ADD_JOURNAL @101 @102 @151 USING ~%MOD_FOLDER%/languages/english/journal.tra~
  END

The required setup.tra:
Code: [Select]
// Install string
@101 = ~Journal example~
@102 = ~Enhanced Edition game is required.~

And journal.tra:
Code: [Select]
// Example journal entries (using identical @ values as setup.tra)
@101 = ~My first quest

I have just started my first quest.~
@102 = ~My first quest

The quest turns out to be a simple fetch quest. How disappointing.~
@151 = ~My first quest

It's finally done. Yay!~
« Last Edit: August 06, 2016, 11:09:15 AM by Argent77 »

Offline Selis

  • Planewalker
  • *****
  • Posts: 2
Re: Grouping journal entries in Enhanced Edition patch 2.0
« Reply #1 on: May 30, 2016, 09:48:37 AM »
Nice!

Offline Argent77

  • Planewalker
  • *****
  • Posts: 187
Re: Grouping journal entries in Enhanced Edition patch 2.0
« Reply #2 on: August 06, 2016, 11:11:16 AM »
I have updated ADD_JOURNAL_V2 and example script in my first post by adding support of the "USING traFile" parameter (made possible by the new WeiDU command WITH_TRA).

ADD_JOURNAL_V2 should now have the same feature set of the original ADD_JOURNAL function.

 

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Name: Email:
Verification:
Type the letters shown in the picture
Listen to the letters / Request another image
Type the letters shown in the picture:
What color is grass?:
What is the seventh word in this sentence?:
What is five minus two (use the full word)?: