Raised This Month: $101 Target: $400
 25% 

Even and Odd number


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Mathias The Vegan
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 06-26-2012 , 17:22   Even and Odd number
Reply With Quote #1

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;


Last edited by Mathias The Vegan; 06-27-2012 at 07:40.
Mathias The Vegan is offline
Leonardo
Veteran Member
Join Date: Feb 2010
Location: 90's
Old 06-26-2012 , 17:30   Re: Even and Odd number
Reply With Quote #2

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

Leonardo is offline
Mathias The Vegan
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 06-26-2012 , 19:04   Re: Even and Odd number
Reply With Quote #3

Thanks, nice optimization. UPDATED.
Mathias The Vegan is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 06-26-2012 , 19:23   Re: Even and Odd number
Reply With Quote #4

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

stock bool:IsOdd(num)
{
    return (num & 1) == 1;
}
__________________
Peace-Maker is offline
Mathias The Vegan
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 06-26-2012 , 20:18   Re: Even and Odd number
Reply With Quote #5

Thanks again, UPDATED.
Mathias The Vegan is offline
Leonardo
Veteran Member
Join Date: Feb 2010
Location: 90's
Old 06-27-2012 , 02:31   Re: Even and Odd number
Reply With Quote #6

return return ?
Quote:
Originally Posted by Black-Rabbit View Post
PHP Code:
stock bool:IsEven(num)
{
    return return (
num 1) == 0;

__________________

Last edited by Leonardo; 06-27-2012 at 03:09.
Leonardo is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 07-02-2012 , 21:55   Re: Even and Odd number
Reply With Quote #7

You can probably just leave off the equality.

Code:
stock bool:IsOdd(num)
{
    return (num & 1);
}
__________________
11530 is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 02-10-2018 , 17:01   Re: Even and Odd number
Reply With Quote #8

Sorry to bump but does it consider 0 as even?
__________________


Accepting private work. Contact me if you're interested.
eyal282 is online now
Addicted.
AlliedModders Donor
Join Date: Dec 2013
Location: 0xA9D0DC
Old 02-10-2018 , 17:54   Re: Even and Odd number
Reply With Quote #9

Quote:
Originally Posted by eyal282 View Post
Sorry to bump but does it consider 0 as even?
Test it
Addicted. is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 02-11-2018 , 01:30   Re: Even and Odd number
Reply With Quote #10

Quote:
Originally Posted by Addicted. View Post
Test it
Canít get on a computer for 3 days.
__________________


Accepting private work. Contact me if you're interested.
eyal282 is online now
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:35.


Powered by vBulletin®
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Theme made by Freecode