Skip to content

Commit

Permalink
smart quit detection for common mistypes
Browse files Browse the repository at this point in the history
  • Loading branch information
timbergeron committed Sep 18, 2024
1 parent e242193 commit 49a97ac
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Quake/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,22 @@ const char *Cmd_CompleteCommand (const char *partial)
return NULL;
}

qboolean Cmd_IsQuitMistype (const char* input) // woods -- #smartquit
{
if (Cvar_FindVar(input) || Cmd_Exists2(input) || Cmd_AliasExists(input))
return false;

if (q_strncasecmp(input, "qu", 2) != 0)
return false; // Not a mistype for "quit"

const char* correct_cmd = "quit"; // Define the correct command

int threshold = 2; // Define a threshold for mistypes (e.g., distance <= 2)
int distance = LevenshteinDistance(input, correct_cmd); // Calculate the Levenshtein distance

return distance > 0 && distance <= threshold; // Return true if within the threshold, else false
}

/*
============
Cmd_ExecuteString
Expand All @@ -1262,6 +1278,13 @@ qboolean Cmd_ExecuteString (const char *text, cmd_source_t src)
if (!Cmd_Argc())
return true; // no tokens

if (Cmd_IsQuitMistype(Cmd_Argv(0))) // // woods -- #smartquit -- check for mistyped "quit" command
{
if (SCR_ModalMessage(va("you typed: %s\n\n do you want to quit? (y/n)\n", Cmd_Argv(0)), 0.0f))
Host_Quit_f();
return true;
}

// check functions
for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
{
Expand Down
43 changes: 43 additions & 0 deletions Quake/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -4644,4 +4644,47 @@ void SetChatInfo (int flags) // woods #chatinfo

snprintf(command, sizeof(command), "setinfo chat %d\n", chat_value);
Cbuf_AddText(command);
}

int LevenshteinDistance (const char* s, const char* t) // woods -- #smartquit -- function to calculate the Levenshtein Distance
{
int len_s = strlen(s);
int len_t = strlen(t);

// Allocate a matrix dynamically
int** matrix = malloc((len_s + 1) * sizeof(int*));
for (int i = 0; i <= len_s; i++)
{
matrix[i] = malloc((len_t + 1) * sizeof(int));
}

// Initialize the matrix
for (int i = 0; i <= len_s; i++) matrix[i][0] = i;
for (int j = 0; j <= len_t; j++) matrix[0][j] = j;


for (int i = 1; i <= len_s; i++) // Compute the Levenshtein distance
{
for (int j = 1; j <= len_t; j++) {
if (tolower(s[i - 1]) == tolower(t[j - 1]))
matrix[i][j] = matrix[i - 1][j - 1];
else {
int insert = matrix[i][j - 1] + 1;
int delete = matrix[i - 1][j] + 1;
int substitute = matrix[i - 1][j - 1] + 1;
matrix[i][j] = insert < delete ? (insert < substitute ? insert : substitute) :
(delete < substitute ? delete : substitute);
}
}
}

int distance = matrix[len_s][len_t];

// Free the matrix
for (int i = 0; i <= len_s; i++) {
free(matrix[i]);
}
free(matrix);

return distance;
}
1 change: 1 addition & 0 deletions Quake/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ size_t UTF8_WriteCodePoint(char* dst, size_t maxbytes, uint32_t codepoint); // w
#define CIF_AFK (1<<1) // set this flag if app loses focus, ie alt+tab -- woods #chatinfo

void SetChatInfo (int flags); // woods #chatinfo
int LevenshteinDistance (const char* s, const char* t); // woods -- #smartquit

//============================================================================

Expand Down

0 comments on commit 49a97ac

Please sign in to comment.