diff --git a/docs/commands/all-commands.md b/docs/commands/all-commands.md index c5e5fdf9..b936f036 100644 --- a/docs/commands/all-commands.md +++ b/docs/commands/all-commands.md @@ -275,10 +275,12 @@ do_things element: ## Strings -| Command | Example | Description | -| -------------------------------- | --------------------------- | ---------------------------------------------------------------------------- | -| [concat](string-commands/concat) | `concat "Hello" "World"` | Concatenates two or more strings | -| [join](string-commands/join) | `join ", " "Hello" "World"` | Joins x strings, with the first character being the join string between them | +| Command | Example | Description | +| -------------------------------- | ------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- | +| [concat](string-commands/concat) | `concat "Hello" "World"` | Concatenates two or more strings | +| [join](string-commands/join) | `join ", " "Hello" "World"` | Joins x strings, with the first character being the join string between them | +| str_search | `var result (str_search "Hello world" "world")` | Searches for a substring in a string and returns the index of the first occurrence. Returns -1 if not found. | +| regex_search | `var result (regex_search "Hello world" "world")` | Searches for a regex pattern in a string and returns the index of the first occurrence. Returns -1 if not found. | ## Screen Objects diff --git a/packages/narrat/src/examples/default/scripts/default.narrat b/packages/narrat/src/examples/default/scripts/default.narrat index c92a5f45..1f3428ef 100644 --- a/packages/narrat/src/examples/default/scripts/default.narrat +++ b/packages/narrat/src/examples/default/scripts/default.narrat @@ -1,13 +1,28 @@ dev_test: + run test_regex run test_text_autoadvance menu_return +test_regex: + var text "uwu i want to open a door mommy" + var res (str_search $text "open a door") + log $res + if (!= $res -1): + "Found the text" + else: + "Didn't find the text" + + var text "0 1 2 a b c d e f g" + var regex_res (regex_search $text \w) + log $regex_res + if (!= $regex_res -1): + "Found the text with regex" + else: + "Didn't find the text with regex" + talk_shortcut text: talk player idle $text -main: - - "Hello I'm the player" - test_text_autoadvance: narrate "Hi!" narrate "with delay " 2000 @@ -16,6 +31,7 @@ test_text_autoadvance: narrate "auto advance waiting for text to print" 1 true narrate "This is a normal line" + talk player idle "Hello!" talk player idle "This will auto advance" 1 true talk player idle "This will not auto advance but has a delay" 2000 false diff --git a/packages/narrat/src/vm/commands/index.ts b/packages/narrat/src/vm/commands/index.ts index 9fed89de..5fa276aa 100644 --- a/packages/narrat/src/vm/commands/index.ts +++ b/packages/narrat/src/vm/commands/index.ts @@ -105,7 +105,12 @@ import { randomFromArrayPlugin, randomIntPlugin, } from './random-commands'; -import { stringConcatPlugin, stringJoinPlugin } from './string-commands'; +import { + regexSearchPlugin, + stringConcatPlugin, + stringJoinPlugin, + stringSearchPlugin, +} from './string-commands'; import { ceilPlugin, clampPlugin, @@ -304,7 +309,8 @@ export function registerBaseCommands(vm: VM) { // Strings vm.addCommand(stringConcatPlugin); vm.addCommand(stringJoinPlugin); - + vm.addCommand(stringSearchPlugin); + vm.addCommand(regexSearchPlugin); // Maths vm.addCommand(minPlugin); vm.addCommand(maxPlugin); diff --git a/packages/narrat/src/vm/commands/string-commands.ts b/packages/narrat/src/vm/commands/string-commands.ts index df3f46eb..b39894d6 100644 --- a/packages/narrat/src/vm/commands/string-commands.ts +++ b/packages/narrat/src/vm/commands/string-commands.ts @@ -43,3 +43,34 @@ export const stringJoinPlugin = new CommandPlugin<{ a: number; b: number }>( }, ''); }, ); + +export const stringSearchPlugin = CommandPlugin.FromOptions<{ + str: string; + matcher: string; +}>({ + keyword: 'str_search', + argTypes: [ + { name: 'str', type: 'string' }, + { name: 'matcher', type: 'string' }, + ], + runner: async (cmd) => { + const { str, matcher } = cmd.options; + return str.search(matcher); + }, +}); + +export const regexSearchPlugin = CommandPlugin.FromOptions<{ + str: string; + regex: string; +}>({ + keyword: 'regex_search', + argTypes: [ + { name: 'str', type: 'string' }, + { name: 'regex', type: 'string' }, + ], + runner: async (cmd) => { + const { str, regex } = cmd.options; + const matcher = new RegExp(regex); + return str.search(matcher); + }, +}); diff --git a/packages/narrat/src/vm/commands/text.ts b/packages/narrat/src/vm/commands/text.ts index e537c8a0..850d1755 100644 --- a/packages/narrat/src/vm/commands/text.ts +++ b/packages/narrat/src/vm/commands/text.ts @@ -20,16 +20,11 @@ export class BaseTextCommand< Options extends BaseTextCommandArgs, > extends CommandPlugin { static async ManageAutoAdvance(cmd: Parser.Command) { - console.log('not interactive'); await useVM().waitForEndTextAnimation(); - console.log('text animation ended'); await timeout(cmd.options.delay || 0); - console.log('delay ended'); if (cmd.options.autoAdvance) { - console.log('player answered'); playerAnswered(0); } else { - console.log('make dialog interactive'); useDialogStore().makeLastDialogInteractive(); } } @@ -131,7 +126,6 @@ export const narrateCommand = BaseTextCommand.FromOptions({ export const textParser = (): CommandParserFunction<{}, { text: string }> => { const parser = generateParser<{}, { text: string }>('text', []); return (ctx, parsed) => { - console.log(parsed); const result = parser(ctx, parsed); parsed.command.staticOptions = { text: parsed.code.substring(1, parsed.code.length - 1),