AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   Even and Odd number (https://forums.alliedmods.net/showthread.php?t=188461)

Mathias. 06-26-2012 16:22

Even and Odd number
 
This is my first snipet and it is not fancy at all but it can be useful in some case.

It check if the integer is a Even or a Odd number:
PHP Code:

stock bool:IsEven(num)
{
    return (
num 1) == 0;
}

stock bool:IsOdd(num)
{
    return (
num 1) == 1;


Here an exemple of is use in a simple script that allow only one smoke grenade per two rounds:
PHP Code:

#include <sourcemod>
#include <cstrike>

new g_iClientSpawnCount[MAXPLAYERS];
new 
bool:g_bClientBuyedSmokeGrenade[MAXPLAYERS];

public 
OnPluginStart()
{
    
HookEvent("player_spawn"OnPlayerSpawn);
}

public 
OnClientPutInServer(client)
{
    
g_iClientSpawnCount[client] = 0;
    
g_bClientBuyedSmokeGrenade[client] = false;
}

public 
OnPlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event,"userid"));
    
g_iClientSpawnCount[client]++;
    
g_bClientBuyedSmokeGrenade[client] = false;
}

public 
Action:CS_OnBuyCommand(client, const String:item_name[])
{
    if (
StrEqual(item_name"smokegrenade"))
    {
        if (!
IsEven(g_iClientSpawnCount[client]) || g_bClientBuyedSmokeGrenade[client])
        {
            
PrintToChat(client"You are only allow to buy one smoke grenade every two rounds");
            return 
Plugin_Handled;
        }
        else
        {
            
g_bClientBuyedSmokeGrenade[client] = true;
        }
    }
    return 
Plugin_Continue;
}

stock bool:IsEven(num)
{
    return (
num 1) == 0;



Leonardo 06-26-2012 16:30

Re: Even and Odd number
 
PHP Code:

stock bool:IsEveniNum )
{
    return 
iNum == 0;



Mathias. 06-26-2012 18:04

Re: Even and Odd number
 
Thanks, nice optimization. UPDATED.

Peace-Maker 06-26-2012 18:23

Re: Even and Odd number
 
Code:

stock bool:IsEven(num)
{
    return (num & 1) == 0;
}

stock bool:IsOdd(num)
{
    return (num & 1) == 1;
}


Mathias. 06-26-2012 19:18

Re: Even and Odd number
 
Thanks again, UPDATED.

Leonardo 06-27-2012 01:31

Re: Even and Odd number
 
return return ?
Quote:

Originally Posted by Black-Rabbit (Post 1736738)
PHP Code:

stock bool:IsEven(num)
{
    return return (
num 1) == 0;




11530 07-02-2012 20:55

Re: Even and Odd number
 
You can probably just leave off the equality.

Code:

stock bool:IsOdd(num)
{
    return (num & 1);
}


eyal282 02-10-2018 16:01

Re: Even and Odd number
 
Sorry to bump but does it consider 0 as even?

Addicted. 02-10-2018 16:54

Re: Even and Odd number
 
Quote:

Originally Posted by eyal282 (Post 2577545)
Sorry to bump but does it consider 0 as even?

Test it

eyal282 02-11-2018 00:30

Re: Even and Odd number
 
Quote:

Originally Posted by Addicted. (Post 2577559)
Test it

Can’t get on a computer for 3 days.


All times are GMT -4. The time now is 09:43.

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