AI Dungeon event injection

Just a quick modification to the standard A/N script (https://github.com/CoomersGuide/CoomersGuide.github.io/tree/main/Resources-And-Guides/Scripts/AuthorsNote) to trigger an event at regular intervals. No change to the input modifier.

Shared library:

// Some kind of Singleton?!
if (!state.setup) {
  state.setup = true // Ensure this is only set once and never wiped.
  state.authorsNote = "This is a descriptive, melancholic story about you and your girlfriend Christina spending the day together at the aquarium. Christina's anterograde amnesia causes her to frequently forget where she is or why she's there. The focus is on Christina's visible struggle to remain cheerful, despite her repeated memory loss." // String for Author's Note.
  state.authorsNoteDepth = 2 // AN inserted n lines from the end of context
  state.authorsNoteDisplay = false // Toggle display of the AN
  state.rawAuthorsNote = false // Whether to not surround the note with [Author's note: ]

  // Special sauce for having an event trigger after a given number of actions.
  // Make sure your context modifier also includes the event trigger code!
  state.eventPeriod = 30 // After how many actions should this trigger?
  state.eventText = "Suddenly, Christina realizes she has no idea why she's here." // String for the event.
  state.rawEvent = false // Whether to not surround the note with [ ]
}

Context modifier:

// Checkout the repo examples to get an idea of other ways you can use scripting
// https://github.com/latitudegames/Scripting/blob/master/examples

// info.memoryLength is the length of the memory section of text.
// info.maxChars is the maximum length that text can be. The server will truncate the text you return to this length.

// This modifier re-implements Author's Note as an example.
const modifier = (text) => {
  const contextMemory = info.memoryLength ? text.slice(0, info.memoryLength) : ''
  const context = info.memoryLength ? text.slice(info.memoryLength) : text
  const lines = context.split("\n")

  // If there are enough lines to insert the Author's Note and we actually have
  // one, splice it in at n lines from the end of the context, where n is the
  // depth. I've selected 2 lines instead of 3 to compensate for using Griffon
  // instead of Dragon. Defaults to 1 because we can't insert lower in the
  // context than the end.
  const depth = state.authorsNoteDepth > 1 ? state.authorsNoteDepth : 1
  if (
    lines.length > (depth - 1)
    && state.authorsNote
    && state.authorsNote.length > 0
  ) {
    lines.splice(-depth, 0, state.rawAuthorsNote ? state.authorsNote : `[Author's note: ${state.authorsNote}]`)
  }

  // Inject our event code every eventPeriod actions.
  // We do it after the Author's Note to ensure that it's as close to the end of
  // the context as possible.
  if (info.actionCount % state.eventPeriod == 0) {
    lines.splice(-1, 0, state.rawEvent ? state.eventText : `[${state.eventText}]`)
  }


  // Make sure the new context isn't too long, or it will get truncated by the server.
  const combinedLines = lines.join("\n").slice(-(info.maxChars - info.memoryLength))
  const finalText = [contextMemory, combinedLines].join("")
  return { text: finalText }
}

// Don't modify this part
modifier(text)
Edit Report
Pub: 04 Apr 2021 05:58 UTC
Edit: 09 Apr 2021 04:41 UTC
Views: 131