Created
October 30, 2024 17:44
-
-
Save SirLouen/bf56e0c57e66ed3156eb09cdc4a25431 to your computer and use it in GitHub Desktop.
MPRegex description
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Este código introduce un sistema de "triggers" (disparadores) de expresiones regulares para los mobprogs en un MUD, lo que permite a los NPCs reaccionar a entradas de texto que coincidan con ciertos patrones. Esto añade una flexibilidad avanzada para programar respuestas de los NPCs a los jugadores, permitiendo interacciones más dinámicas y detalladas. A continuación, te explico cómo funciona este sistema, cada parte del código y su propósito: | |
Ejemplo de Uso: Si un mobprog tiene la frase "my name is (\w+)" como regex, y un jugador dice "My name is bob", el NPC podrá capturar "bob" y usarlo en una respuesta como "Why hello there, bob". | |
Palrich | |
[email protected] | |
Head Imp, Divine Blood telnet://divineblood.tetradine.com:4000 | |
I hacked this up to do what one of my builders wanted. It's kind of neat, so | |
I rewrote it to work with stock code+OLC1.81. I'm not sure how it'll do with | |
other OLC's. Basically it uses the phrase of a mobprog as a regular expression, | |
checks it against speech text, and stores the first 3 matches in $a, $b, and | |
$c. | |
So you add the prog with trigger REGEX and phrase "my name is (\w+)" | |
Then the body of the prog is, | |
say Why hello there, $a. | |
When the player says "My name is bob", the mob says "Why hello there, bob" | |
Pretty simple and all. | |
If you use something like this a lot, you should store the compiled regexprec | |
rather than building and freeing it every time, because this here is hugely | |
inefficient. | |
// At the top of mob_prog.c | |
#include <regex.h> | |
// ... then in the global scope | |
char match[3][MAX_INPUT_LENGTH]; | |
// Inside expand_arg | |
case 'a': | |
i = match[0]; break; | |
case 'b': | |
i = match[1]; break; | |
case 'c': | |
i = match[2]; break; | |
//somewhere toward the bottom of mob_prog.c | |
void mp_regex_trigger(char *argument, CHAR_DATA *mob, CHAR_DATA *ch) | |
{ | |
regex_t exprec; | |
int nmemb = 3, i; | |
regmatch_t memb[nmemb]; | |
MPROG_LIST *prg; | |
for (prg = mob->pIndexData->mprogs; prg != NULL; prg = prg->next) | |
if ( prg->trig_type == TRIG_REGEXP) | |
{ | |
regcomp(&exprec, prg->trig_phrase, REG_EXTENDED|REG_ICASE); | |
if (!regexec(&exprec, argument, nmemb, memb, 0)) | |
{ | |
for (i = 0; i < nmemb && memb[i+1].rm_so > -1; i++) | |
{ | |
memcpy(match[i], argument+memb[i+1].rm_so, | |
memb[i+1].rm_eo-memb[i+1].rm_so); | |
match[i][memb[i+1].rm_eo-memb[i+1].rm_so] = '\0'; | |
} | |
program_flow(prg->vnum, prg->code, mob, ch, arg1, arg2); | |
} | |
regfree(&exprec); | |
return; | |
} | |
} | |
// merc.h, with the other TRIG_*s | |
#define TRIG_REGEX (Q) | |
// merc.h, with the other mp trigger function definitions | |
void mp_regex_trigger(char *argument, CHAR_DATA *mob, CHAR_DATA *ch); | |
// tables.c, in mprog_flags | |
{ "regex", TRIG_REGEX, TRUE }, | |
//mob_cmds.c, inside mprog_type_to_name | |
case TRIG_REGEX: return "REGEX"; | |
//act_comm.c, under the check for TRIG_SPEECH | |
if (IS_NPC(mob) && HAS_TRIGGER( mob, TRIG_REGEX)) | |
mp_regex_trigger(argument,mob,ch); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment