Can't find what I'm doing wrong!
#1

pawn Код:
if(time == 00 || time == 15 || time == 30 || time == 45)
    {
        if(drugpointcounter == 10) { drugpointcounter = 0; }
        if(drugpointcounter == 0)
        {
            for(new i=0;i<10;i++)
            {
                usednumbers[i] = 99;
            }
        }
        new rand = random(10);
        drugclear = 0;
        while(drugclear == 0)
        {
            for(new j=0;j<10;j++)
            {
                if(usednumbers[j] == rand) { rand = random(10); }
                if(usednumbers[j] != rand) { drugclear = 1; usednumbers[drugpointcounter] = rand; }
            }
        }
        MoveSmugglingPoint(rand);
        drugpointcounter++;
    }
What basically this does, it gives a random number ranged from 0 to 9.
Then second time, it gives another, but makes sure it's not any of the last ones used.
Same for the third, till it reaches 10 when it resets.
usednumbers has 9 cells, to save the used numbers, and drugpointcounter makes sure to reset it the 10th time.
It won't but, and I can't understand why!
Reply
#2

As far as I can see your code repeats calling the random function till it finds a number which hasn't be taken
The problem is if there is only one number left it will probably hang your server for serveral seconds till it randomize the last number

I will use here a static usedNumber variable, so you can remove your usednumbers array
pawn Код:
//
    if(time == 00 || time == 15 || time == 30 || time == 45) {
        static
            usedNumber
        ;
        if(drugpointcounter == 10) {
            drugpointcounter = 0;
            usedNumber = 0;
        }
        new rand = random(10 - drugpointcounter);

        while(usedNumber & (1 << rand)) {
            rand++;
        }
        usedNumber |= 1 << rand;

        MoveSmugglingPoint(rand);
        drugpointcounter++;
    }
Thats a pretty easy implantation but it should work
Reply
#3

I'll try to put it in and see how it goes... since I'm still a newbie scripter, I'd like to know what the while loop is used for in this case, and operators like <<, |=, I see them for first time, so what do they do?
Reply
#4

That are bitwise operators

There is a good tutorial on the sa-mp forums

[Tutorial] An in-depth look at binary and binary operators.

Or you read pawn-lang.pdf (Bit manipulation: page 104 - 106)

Also their are tons of other tutorials in the www because these are in nearly all languages represent
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)