[Tutorial] Simple /flipcoin command [ZCMD, sscanf, foreach]
#1

Getting started
I haven't seen any tutorials about this, so I've decided to make one. It's pretty easy, anyone can do it and it can be useful in situations. What I have is a /flipcoin command, which will randomly select one of the textes to show to players that are near each other. I also have a /rpchance command which will determine whether the previous roleplay action was a success or a failure after 3 seconds.

How is /rpchance helpful? Well, in every RP server there will always be arguments about if a certain action would suceed or not. This will decide which of the players will succeed. Let's say, they are roleplaying a shootout and Player 1 goes:
* NotYoMama aims for Player 2's head and shoots.
/do What would be the result of Player 1's shooting? ((Player 1))


Player 2 goes:
/do Player 1 would miss Player 2


and they start arguing, an admin comes, they waste his time and so on and on.

And /flipcoin is just a small detail that makes your server better.


Here are the images of the final result:





A player is determining the action, for the reason of needing a proof. If it doesn't have a name, then anyone can just /rpchance and troll. Plus, it doesn't who types in the command, because it's random

Coding

/flipcoin command
Don't double include them if you already have them included.
The includes are:

PHP Code:
#include <a_samp>
#include <ZCMD>
#include <sscanf2>
#include <foreach> 
Put the define under all of the #includes or wherever your color defines are.
Make sure you don't already have it defined

PHP Code:
#define    COLOR_LIGHTBLUE 0x33CCFFAA 
You should put your stocks at the end of the script or wherever your other stocks are. Search if you already have some of these stocks, then just change their name in CMD:flipcoin & CMD:rpchance

The following stock will be used to replace characters in a string

PHP Code:
stock strreplace(string[], findreplace)
{
    for(new 
i=0string[i]; i++)
    {
        if(
string[i] == find)
        {
            
string[i] = replace;
        }
    }

Replacing the "_" with a blank space " ", so instead of Joseph_Colton it will show Joseph Colton

PHP Code:
stock GetName(playerid)
{
    new 
name[24];
    
GetPlayerName(playeridnamesizeof(name));
    
strreplace(name'_'' ');
    return 
name;

The ProxDetector is used to send a message to the players who are near the player who is sending a message.

PHP Code:
stock ProxDetector(Float:radiplayeridstring[],color)
{
    new 
Float:x,Float:y,Float:z;
    
GetPlayerPos(playerid,x,y,z);
    foreach(
Player,i)
    {
        if(
IsPlayerInRangeOfPoint(i,radi,x,y,z))
        {
            
SendClientMessage(i,color,string);
        }
    }

If you compile it, you should not get errors.

Finally, the command. I prefer using ZCMD. You should place this line above the stocks. That's at least where I place them. The command is pretty self-explanatory.

PHP Code:
CMD:flipcoin(playeridparams[])
{
    new 
string[128], rand random(2);
    if(
rand == 0)
    {
        
format(stringsizeof(string), "* %s throws a coin and it lands on the obverse side of it. (HEADS)"GetName(playerid));
        
ProxDetector(20playeridstringCOLOR_LIGHTBLUE);
    }
    else
    {
        
format(stringsizeof(string), "* %s throws a coin and it lands on the reverse side of it. (TAILS)"GetName(playerid));
        
ProxDetector(20playeridstringCOLOR_LIGHTBLUE);
    }
    return 
1;

/rpchance command

Don't double include them if you already have them included.
The includes are:

PHP Code:
#include <a_samp>
#include <ZCMD>
#include <sscanf2>
#include <foreach> 
You will need all of the stocks from the /flipcoin part, so just copy them from there.


PHP Code:
#define COLOR_GREYRED        0x70825BFF
#define COLOR_GREEN         0x33AA33AA
#define COLOR_RED            0xAA3333AA 
Place the forward at your other forwards or above the public function.
Place the public function somewhere under main(), near your other public functions

PHP Code:
forward RPChance(playerid);
public 
RPChance(playerid)
{
    new 
rand random(2);
    new 
string[128];
    if(
rand == 0)
    {
        
format(stringsizeof(string), "The previous roleplay action was a success!");
        
ProxDetector(20playeridstringCOLOR_GREEN);
    }
    else
    {
        
format(stringsizeof(string), "The previous roleplay action was a failure.");
        
ProxDetector(20playeridstringCOLOR_RED);
    }
    return 
1;

The timer is in miliseconds, 1.000 miliseconds = 1 second, so it' s set to 3 seconds.

PHP Code:
CMD:rpchance(playeridparams[])
{
    new 
string[128];
    
format(stringsizeof(string), "%s is determining how the previous roleplay action should go..."GetName(playerid));
    
ProxDetector(20playeridstringCOLOR_GREYRED);
    
SetTimerEx("RPChance"30000"i"playerid);
    return 
1;

Your feedback/suggestions are welcome!
Reply
#2

lol?
PHP Code:
stock randomex(minmax) { return random(max min) + min; }
YCMD:coin(playeridparams[], help){
    
#pragma unused help, params
    
new kint randomex(13);
    if( 
kint == 
        
SendClientMessage(playerid, -1"1");
    else 
        
SendClientMessage(playerid, -1"2");
    return 
1;

Reply
#3

You missed the most important thing... checking if they even have a coin to flip.
Reply
#4

Also, try spamming /rpchance and your server will be full of timers!

Did this tutorial take 6 months to make, by any chance?
Reply
#5

lol!(2)

PHP Code:
CMD:flipcoin(playeridparams[])

    new 
rand random(2); 
    if(
rand == ) return SendClientMessage(playerid, -1"+1 to CHANCE");
    else return 
SendClientMessage(playerid, -1"Chance is = '0'");
    return 
true

Reply
#6

Quote:
Originally Posted by MellikJKE
View Post
lol!(2)

PHP Code:
CMD:flipcoin(playeridparams[])

    new 
rand random(2); 
    if(
rand == ) return SendClientMessage(playerid, -1"+1 to CHANCE");
    else return 
SendClientMessage(playerid, -1"Chance is = '0'");
    return 
true

Defeats the point when other local players can't see the reaction.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)