Selecting something from an array of data
#4

pawn Код:
#define MAX_BOXES           10

new Float:RandomBoxSpawn[MAX_BOXES][6] =
{
    // Positions, (X, Y, Z, rX, rY, rZ)
    {2783.76,-2450.73,14.596,0,0,0},
    {2783.67,-2449.26,13.797,0,0,0},
    {2791.95,-2461.6,13.796,0,0,0},
    {2791.87,-2487.78,14.593,0,0,0},
    {2797.43,-2487.67,13.793,0,0,0},
    {2783.74,-2499.64,14.659,0,0,0},
    {2783.75,-2407.97,13.827,0,0,0},
    {2791.9,-2426.13,14.633,0,0,0},
    {2783.83,-2424.59,13.055,0,0,0},
    {2791.93,-2447.75,13.044,0,0,0}
};


public Createboxes(playerid)
{
    new BoxNums[MAX_BOXES], bool:NewBoxFound = false;

    // Fill the array with invalid numbers (-1), since blank arrays contain "0" and might be a box-index from RandomBoxSpawn
    for (new i; i < MAX_BOXES; i++)
        BoxNums[i] = -1;

    new ammountofboxes = randomEx(3,10);

    // For the first box, just choose a random one and store it's index in the array
    BoxNums[0] = random(sizeof(RandomBoxSpawn));

    // Loop through the remaining amount of boxes that has been chosen
    for (new i = 1; i < amountofboxes; i++)
    {
        // Reset the NewBoxFound to false
        NewBoxFound = false;

        // Continue searching until a spare box has been found
        while (NewBoxFound == false)
        {
            // Select a new random box
            NextBox = random(MAX_BOXES);

            // First set this to true before checking
            NewBoxFound = true;

            // Check if it's already chosen
            for (new j; j < MAX_BOXES; j++)
            {
                if (BoxNums[j] == NextBox)
                {
                    NewBoxFound = false;
                }
            }

            // If the next randomly chosen box isn't used yet, store it in the next index of the array
            if (NexBoxFound == true)
                BoxNums[i] = NewBox;
        }
    }

    new Float:x, Float:y, Float:z, BoxNum;

    // Now it's time to create them
    for (new i; i < amountofboxes; i++)
    {
        BoxNum = BoxNums[i];

        x = RandomBoxSpawn[BoxNum][0];
        y = RandomBoxSpawn[BoxNum][1];
        z = RandomBoxSpawn[BoxNum][2];
        CreateObject (boxmodel, x, y, z, ...
    }

    return 1;
}
Something like this?
This code should find a random number of unique boxes and stores them in the BoxNums array.
Then, after it, your code can come to create them.
Reply


Messages In This Thread
Selecting something from an array of data - by jeffery30162 - 30.01.2015, 16:45
Re: Selecting something from an array of data - by jeffery30162 - 30.01.2015, 18:05
Re: Selecting something from an array of data - by jeffery30162 - 30.01.2015, 22:35
Re: Selecting something from an array of data - by PowerPC603 - 30.01.2015, 23:35
Re: Selecting something from an array of data - by DRIFT_HUNTER - 30.01.2015, 23:40
Re: Selecting something from an array of data - by jeffery30162 - 30.01.2015, 23:45

Forum Jump:


Users browsing this thread: 1 Guest(s)