Raised This Month: $12 Target: $400
 3% 

Even and Odd number


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Mathias.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 06-26-2012 , 16: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.; 06-27-2012 at 06:40.
Mathias. is offline
Leonardo
Veteran Member
Join Date: Feb 2010
Location: 90's
Old 06-26-2012 , 16:30   Re: Even and Odd number
Reply With Quote #2

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

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

Thanks, nice optimization. UPDATED.
Mathias. is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 06-26-2012 , 18: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.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 06-26-2012 , 19:18   Re: Even and Odd number
Reply With Quote #5

Thanks again, UPDATED.
Mathias. is offline
Leonardo
Veteran Member
Join Date: Feb 2010
Location: 90's
Old 06-27-2012 , 01: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 02:09.
Leonardo is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 07-02-2012 , 20: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 , 16:01   Re: Even and Odd number
Reply With Quote #8

Sorry to bump but does it consider 0 as even?
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
Addicted.
AlliedModders Donor
Join Date: Dec 2013
Location: 0xA9D0DC
Old 02-10-2018 , 16: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 , 00: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.
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
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 02:01.


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