Useful Snippets

Can somebody test this for me? It should desync rockets coming from hydras. So you can have hydras on your server but nobody will see when you shoot, and you will not see when somebody else shoots from hydra. I'm unable to test it.

pawn Код:
#include "a_samp.inc"

new bool:BlockUpdate[MAX_PLAYERS];

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if ((IsPlayerInAnyVehicle(playerid) && GetVehicleModel(GetPlayerVehicleID(playerid)) == 520) && (newkeys & KEY_FIRE) && !bool:BlockUpdate[playerid])
    {
        BlockUpdate[playerid] = true;
    }
    return 1;
}

public OnPlayerUpdate(playerid)
{
    BlockUpdate[playerid] = bool:!BlockUpdate[playerid];
    return bool:!BlockUpdate[playerid];
}
Reply

The include line is wrong, it should be <a_samp>. It would keep switching. bool:! is redundant (just use !). Using both IsPlayerInAnyVehicle and GetPlayerVehicleID is redundant (you can just get the ID and check whether it's 0).

Also, IIRC OnPlayerUpdate is called prior to OnPlayerKeyStateChange, which means you should just use GetPlayerKeys inside OnPlayerUpdate and return false if fire is being helt. That, however, would result in players being able to hold fire to desync themselves. If you make it disable only when pressing fire, players can just hold fire to get rockets synced.

I'd say it's safe to say that code won't work.
Reply

Okay, thanks.
EDIT: Include line isn't wrong, I'm unable to test it now, but I am 100% sure it works. I always write it like that.
Reply

Quote:
Originally Posted by Slice
Посмотреть сообщение
The include line is wrong, it should be <a_samp>. It would keep switching. bool:! is redundant (just use !). Using both IsPlayerInAnyVehicle and GetPlayerVehicleID is redundant (you can just get the ID and check whether it's 0).

Also, IIRC OnPlayerUpdate is called prior to OnPlayerKeyStateChange, which means you should just use GetPlayerKeys inside OnPlayerUpdate and return false if fire is being helt. That, however, would result in players being able to hold fire to desync themselves. If you make it disable only when pressing fire, players can just hold fire to get rockets synced.

I'd say it's safe to say that code won't work.
So you are saying the < > is is the shortcut for /pawn/includes or what? I saw scripts like that and worked fine, like " " < > or even nothing
Reply

F:\dmserver\gamemodes\A51DM.pwn(161) : warning 235: public function lacks forward declaration (symbol "AdminLock")

WTF??
Reply

Quote:
Originally Posted by sciman001
View Post
F:\dmserver\gamemodes\A51DM.pwn(161) : warning 235: public function lacks forward declaration (symbol "AdminLock")

WTF??
Somewhere out of publics (best under other forwards)
Code:
forward AdminLock(params);
I have no idea about params, write it yourself.
Reply

OnPlayerPause
pawn Code:
forward OnPlayerPause(playerid);
new OPP_Timer[MAX_PLAYERS];
public OnPlayerUpdate(playerid)
{
    KillTimer(OPP_Timer[playerid]);
    OPP_Timer[playerid] = SetTimerEx("OnPlayerPause", 1000, 0, "i", playerid);
    return 1;
}
OnPlayerUnPause and IsPlayerPaused
(needs OnPlayerPause above)
pawn Code:
public OnPlayerPause(playerid)
{
    OPP_PlayerPaused[playerid] = true;
}
forward OnPlayerUnPause(playerid);
new bool:OPP_PlayerPaused[MAX_PLAYERS] = {false, ...};
public OnPlayerConnect(playerid)
{
    OPP_PlayerPaused[playerid] = false;
    return 1;
}
#define IsPlayerPaused(playerid) (OPP_PlayerPaused[playerid])
public OnPlayerUpdate(playerid)
{
    if (OPP_PlayerPaused[playerid])
    {
        OPP_PlayerPaused[playerid] = false;
        SetTimerEx("OnPlayerUnPause", 0, 0, "i", playerid);
    }
    return 1;
}
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
What about spectating?
Spectating players still send updates, aren't they?
Reply

I doubt setting timers constantly in OnPlayerUpdate is a good idea. Instead you should have something more simple:
pawn Код:
new AFK[MAX_PLAYERS char];

public OnGameModeInit()
{
    SetTimer("checkAFK", 1000, true);
}

public OnPlayerUpdate(playerid)
{
    AFK{playerid} = 0;
}

forward checkAFK();
public checkAFK()
{
    foreach(Player, i)
    {
        if(AFK{i} < 3)
        {
            AFK{i} ++;
        }
        else if(AFK{i} != 10)
        {
            // Player went AFK
            AFK{i} = 10;
        }
    }
}
Something similar to how I do it. Anything wrong or such? By the way I just use the one toggle per player for easier manipulation. I'm sure you get it.
Reply

pawn Код:
#define IsPlayerPaused(%1) ((GetTickCount()-GetPVarInt(playerid,"AntiPause"))>5000)
public OnPlayerUpdate(playerid)
{
    SetPVarInt(playerid, "AntiPause", GetTickCount());
    return 1;
}
Another way?
Reply

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
Aren't PVars slow to assign and retrieve values?
I've heard so many positive and negative things about PVars I don't know what to think so I just leave them alone and stick to what I'm used to!
You can make it a variable as well, they work the same. Indeed, variables are faster than PVars.
Reply

c_bit (char bits)
A simple bit system for cases, when you only have to use less than 25 slots.
Memory usage:
1-7 slots: 1 byte
8-15 slots: 2 bytes
16-23 slots: 3 bytes
24-32 slots: 4 bytes (you can use any other bit systems if your task fits into this category)

Example usage
In this example, we do an ALS hook with cbits
pawn Код:
new CBitArray:hooks[2]; /* you have 2 callbacks to hook */

#define HAS_OPC (0)
#define HAS_OPDC (1)

public OnScriptInit()
{
    CBit_Set(hooks, HAS_OPC, (funcidx("S_OnPlayerConnect") != -1));
    CBit_Set(hooks, HAS_OPDC, (funcidx("S_OnPlayerDisconnect") != -1));
}

public OnPlayerConnect(playerid)
{
    // your code
    if (CBit_Get(hooks, HAS_OPC)) return CallLocalFunction("S_OnPlayerConnect", "i", playerid);
}

public OnPlayerDisconnect(playerid, reason)
{
    // your code
    if (CBit_Get(hooks, HAS_OPDC)) return CallLocalFunction("S_OnPlayerDisconnect", "ii", playerid, reason);
}
Credits
I have used Y-Less' y_bit to make the basic structure of tags.

Code
pawn Код:
#define CBitArray:%1[%2] CBit:%1[%2]

#define CBit_Bits(%1) (((%1) + 7) / 8) char

#define CBit_Get(%1,%2) !!(%1{(%2)>>>3} & CBit:(1 << ((%2) & 7)))
#define CBit_Let(%1,%2) (%1{(%2)>>>3} |= CBit:(1 << ((%2) & 7)))
#define CBit_Vet(%1,%2) (%1{(%2)>>>3} &= CBit:~(1 << ((%2) & 7)))

stock CBit_Set(CBitArray:array[], slot, bool:set)
{
    if (set) CBit_Let(array, slot);
    else CBit_Vet(array, slot);
}

#undef CBitArray
#define CBitArray:%1[%2] CBit:%1[CBit_Bits(%2)]
p.s. I originally wanted to post it in the Includes section, but this is far from an include in size.
Reply

Usage: RandomiseNumberPlate(vehicleid,format[])
Example usage: RandomiseNumberPlate(3,"SSddddSS");
Description: Generates and applies a random number plate to the specified vehicle. The number plate can be formatted with:
Код:
 i or d - Number from 0-9
 s - Lower case letter
 S - Upper case letter
 [space] - A space
pawn Код:
RandomiseNumberPlate(vehicleid,const format[])
{
    new numstr[8];
    for(new i;i<strlen(format)&&i<8)
    {
        switch(format[i])
        {
            case 'i','d':numstr[i]=random(10)+48;
            case 's':numstr[i]=random(26)+97;
            case 'S':numstr[i]=random(26)+65;
            case ' ','_':numstr[i]=' ';
        }
    }
    SetVehicleNumberPlate(vehicleid,numstr);
}
Reply

Quote:
Originally Posted by jonrb
Посмотреть сообщение
Usage: RandomiseNumberPlate(vehicleid,format[])
Example usage: RandomiseNumberPlate(3,"SSddddSS");
Description: Generates and applies a random number plate to the specified vehicle. The number plate can be formatted with:
Код:
 i or d - Number from 0-9
 s - Lower case letter
 S - Upper case letter
 [space] - A space
pawn Код:
RandomiseNumberPlate(vehicleid,const format[])
{
    new numstr[8];
    for(new i;i<strlen(format)&&i<8)
    {
        switch(format[i])
        {
            case 'i','d':numstr[i]=random(10)+48;
            case 's':numstr[i]=random(26)+97;
            case 'S':numstr[i]=random(26)+65;
            case ' ','_':numstr[i]=' ';
        }
    }
    SetVehicleNumberPlate(vehicleid,numstr);
}
Looks alright, just
pawn Код:
for(new i;i<strlen(format)&&i<8)
to
pawn Код:
new x = strlen(format);
if(x > 8) x = 8;
for(new i;i<x;i++)
Reply

Quote:
Originally Posted by wups
Посмотреть сообщение
Looks alright, just
pawn Код:
for(new i;i<strlen(format)&&i<8)
to
pawn Код:
new x = strlen(format);
if(x > 8) x = 8;
for(new i;i<x;i++)
Nice fix. Also, this is the coolest function i have ever seen. But, I add a A in mine for a space, and it just stops there... let me show you what i mean:

Код:
RandomiseNumberPlate(vid,"SSSAidi");
An example of what happens:

PUL

But I want something like: LCF 364... what to do? Other than that, AMAZING!
Reply

Oh. I feel so stupid... you meant an actual space or a _. I am sucj a noob.
Reply

Quote:
Originally Posted by sciman001
Посмотреть сообщение
Nice fix. Also, this is the coolest function i have ever seen. But, I add a A in mine for a space, and it just stops there... let me show you what i mean:

Код:
RandomiseNumberPlate(vid,"SSSAidi");
An example of what happens:

PUL

But I want something like: LCF 364... what to do? Other than that, AMAZING!
pawn Код:
RandomiseNumberPlate(vid,"SSS ddd");
Reply

yeah.. im a real noob for not noticing that
Reply

Also, a cool adition would be if you'd add:
pawn Код:
default:numstr[i]=format[i];
That way we could do:
pawn Код:
RandomiseNumberPlate(vehicleid,"SAMPii")
which would create numbers like SAMP01,SAMP85 etc...
Edit: oh wait, S is already used
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
Or only use invalid symbols:

pawn Код:
RandomiseNumberPlate(vehicleid, const form[])
{
    // Don't forget the NULL character.
    new numstr[33];
    // Don't do calculations you can avoid in the "while" segment.
    for(new i, j = min(strlen(form), sizeof (numstr) - 1); i != j; ++i)
    {
        switch (form[i])
        {
            // Hash for numbers.
            case '#':  numstr[i] = random(10) + '0';
            // LOW for LOWer.
            case '_':  numstr[i] = random(26) + 'a';
            // Pointing UP for UPper.
            case '^':  numstr[i] = random(26) + 'A';
            // Possibly not used (so ??? can generate 0-999).
            case '?':
            {
                new
                    p = i;
                while (form[++p] == '?') {}
                p -= i;
                format(numstr[i], sizeof (numstr) - i, "%*d", p, random(floatround(floatpower(10.0, p))));
                i += p - 1;
            }
            // Anything else.
            default: numstr[i] = form[i];
        }
    }
    SetVehicleNumberPlate(vehicleid, numstr);
}
You may not like those but I went for something pointing UP for UPper case, something LOW for LOWer case, and a hash for numbers as that's quite a common representation.

Edit: Added ? for maybe - as in "" is UP TO a 3 digit number. In England at least the old format was "^^^^", but "Y 1ESS" was valid because it was any number in the range 0-999 (guess what my ideal number plate is). Also according to the wiki you can have up to 32 characters, so I changed that.
I can't understand what is that function "min".
j = min
What is it?
Is there any further explanation?
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)