AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Out of bounds (https://forums.alliedmods.net/showthread.php?t=214175)

shavit 04-23-2013 19:44

Out of bounds
 
I am trying to recreate The RTLer from SourceMod in AMXX but I am getting "Index out of bounds" error in line 58 when I am saying 2 words or more.

Code:

#include <amxmodx>
#include <amxmisc>

#pragma semicolon 1

public plugin_init()
{
    register_plugin("The RTLer", "1.0", "alongub");
   
    register_cvar("amx_rtler_enabled", "1");
   
    register_clcmd("say", "Command_Say", -1, "Registers the say command in The RTLer");
    // register_clcmd("say_team", "Command_SayTeam", -1, "Registers the say_team command in The RTLer");
}

public Command_Say(client)
{
    if(!get_cvar_num("amx_rtler_enabled"))
    {
        return PLUGIN_CONTINUE;
    }
   
    new arg_string[255];
    read_args(arg_string, 255);
   
    client_print(client, print_chat, "RTLed words: %d", _RTLify(arg_string, arg_string));
   
    if(!_RTLify(arg_string, arg_string))
    {
        return PLUGIN_CONTINUE;
    }
   
    return PLUGIN_HANDLED_MAIN;
}

_RTLify(dest[], original[])
{
    new rtledWords;
   
    new casino[32][32];
    new words[sizeof(casino)][sizeof(casino[])];
   
    new n = ExplodeString(casino, 32, 32, original, ' ');
   
    for(new word; word < n; word++)
    {
        if(WordAnalysis(casino[word]) >= 0.1)
        {
            ReverseString(casino[word], sizeof(casino[]), words[n - 1 - word]);
            rtledWords++;
        }
       
        else
        {
            new firstWord = word;
            new lastWord = word;
           
            while(WordAnalysis(casino[lastWord]) < 0.1)
            {
                lastWord++;
            }
           
            for(new t = lastWord - 1; t >= firstWord; t--)
            {
                copy(words[n - 1 - word], sizeof(casino[]), casino[t]);
               
                if(t > firstWord)
                {
                    word++;
                }
            }
        }
    }
   
    ImplodeStrings(words, n, " ", dest, sizeof(words[]));
   
    return rtledWords;
}

stock ExplodeString( p_szOutput[][], p_nMax, p_nSize, p_szInput[], p_szDelimiter )
{
    new nIdx, l = strlen(p_szInput) ;
   
    new nLen = (1 + copyc(p_szOutput[nIdx], p_nSize, p_szInput, p_szDelimiter));
   
    while((nLen < l) && (++nIdx < p_nMax))
    {
        nLen += (1 + copyc(p_szOutput[nIdx], p_nSize, p_szInput[nLen], p_szDelimiter));
    }
   
    return nIdx;
}

ReverseString(str[], maxlength, buffer[])
{
    for(new character = strlen(str); character >= 0; character--)
    {
        if(str[character] >= 0xD6 && str[character] <= 0xDE)
        {
            continue;
        }
       
        if(character > 0 && str[character - 1] >= 0xD7 && str[character - 1] <= 0xD9)
        {
            format(buffer, maxlength, "%s%c%c", buffer, str[character - 1], str[character]);
        }
       
        else
        {
            format(buffer, maxlength, "%s%c", buffer, str[character]);
        }
    }
}

Float:WordAnalysis(word[])
{
    new count, length = strlen(word);
   
    for(new n; n < length - 1; n++)
    {
        if(IsRTLCharacter(word, n))
        {   
            count++;
            n++;
        }
    }
   
    return float(count) * 2 / length;
}

bool:IsRTLCharacter(str[], n)
{
    return (str[n] >= 0xD6 && str[n] <= 0xDE && str[n + 1] >= 0x80 && str[n + 1] <= 0xBF);
}

ImplodeStrings(const strings[][], numStrings, const join[], buffer[], maxLength)
{
    new total, length, part_length;
    new join_length = strlen(join);
   
    for(new i; i < numStrings; i++)
    {
        length = copy(buffer[total], maxLength - total, strings[i]);
        total += length;
       
        if(length < part_length)
        {
            break;
        }
       
        if(i != numStrings - 1)
        {
            length = copy(buffer[total], maxLength - total, join);
            total += length;
           
            if(length < join_length)
            {
                break;
            }
        }
    }
   
    return total;
}

Help would be appreciated. :)

hornet 04-23-2013 20:05

Re: Out of bounds
 
Print some debug messages and see how many iterations it's getting through.

fysiks 04-23-2013 23:31

Re: Out of bounds
 
"lastWord" is obviously 32 when this happens. You need to prevent it from getting larger than the cells in the array.

EDIT:
After looking at the SourceMod code, you should notice that the size of that array is much larger than yours.

shavit 04-24-2013 00:53

Re: Out of bounds
 
Quote:

Originally Posted by fysiks (Post 1938687)
"lastWord" is obviously 32 when this happens. You need to prevent it from getting larger than the cells in the array.

EDIT:
After looking at the SourceMod code, you should notice that the size of that array is much larger than yours.

For some reason I can't make "casino" too long.

fysiks 04-24-2013 21:30

Re: Out of bounds
 
Quote:

Originally Posted by shavit (Post 1938707)
For some reason I can't make "casino" too long.

And what do you mean by that?

shavit 04-25-2013 01:10

Re: Out of bounds
 
Quote:

Originally Posted by fysiks (Post 1939347)
And what do you mean by that?

I can't make the variable "casino" big enough so it won't get out of bounds.

fysiks 04-25-2013 02:24

Re: Out of bounds
 
Quote:

Originally Posted by shavit (Post 1939426)
I can't make the variable "casino" big enough so it won't get out of bounds.

Then something more fundamental is wrong.


All times are GMT -4. The time now is 10:55.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.