05.04.2019, 03:54
Quote:
Yeah, that's exactly what I mean. I'm using OnPlayerCommandText as the command client.
|
Then, since you want to do a comparison with all of your commands... you'll want to do something similar to below;
pawn Код:
mathMin(inp1,inp2)
return (inp1 > inp2 ? inp2 : inp1);
LevenshteinDistance(strs[], strt[])
{
new n = strlen(strs), m = strlen(strt);
new d[128][128];
if (n == 0)
return m;
if (m == 0)
return n;
for (new i; i <= n; i++)
d[i][0] = i;
for (new j; j <= m; j++)
d[0][j] = j;
for (new j = 1; j <= m; j++)
{
for (new i = 1; i <= n; i++)
{
if (strs[i - 1] == strt[j - 1])
d[i][j] = d[i - 1][j - 1];
else
d[i][j] = mathMin(mathMin(d[i - 1][j] + 1, d[i][j - 1] + 1), d[i - 1][j - 1] + 1);
}
}
return d[n][m];
}
// CHANGE THIS. 20 should be changed to your number of commands.
#define NUMBER_OF_COMMANDS 20
new commands[NUMBER_OF_COMMANDS][] = {
"help",
"kill",
"getplayer",
"spawncar"
// ALL OF YOUR COMMANDS
};
GetClosestCommand(string[])
{
new match, // matching command with least changes
distance, // number of changes to make command
least = 1000; // random high number
for(new i; i < sizeof(commands); i++)
{
if(strfind(commands[i], string, true) != -1)
{
distance = LevenshteinDistance(string, commands[i]);
if(least > distance)
{
match = i;
least = distance;
}
}
}
return commands[match];
}
For OnPlayerCommandText this may look like this;
pawn Код:
public OnPlayerCommandText(playerid,cmdtext[])
{
// all commands here
new response[128];
format(response, 128, "Error: This command didn't exist, did you mean /%s?", GetClosestCommand(cmdtext));
SendClientMessage(playerid, -1, response);
return 0; // this is still going to show the UnKnown command message unless you change this to return 1, however this this will break other scripts... Another reason to switch processors.
}