[Tutorial] Making a CB Radio System.
#1

Making a CB Radio System
Aimed mainly at Trucking/Logistical Servers
Just recently, I've had several "new players" requesting help on creating CB radio systems (as I'm sure other servers have too). I actually got offered an administration position to help someone on this.

If I'm honest, it is a pretty easy thing. When I first started scripting for SAMP, this was one of my first accomplishments.

The Variables
You only need one, global, array. This will store the players virtual "CB Channel".

Call it something sensible, obviously.
For example:
pawn Code:
new pChannel[MAX_PLAYERS]; //MAX_PLAYERS, to create the array with that size (default to 500)
OnPlayerDisconnect
You will want to reset the CB Channel when a player disconnects. You'll be using the OnPlayerDisconnect callback to do this.

Set it to "0". So people later connecting with the same player id, won't see the CB messages that are being sent.

Stick this, in your "OnPlayerDisconnect" callback. (If you don't have this callback, add it in)
pawn Code:
pChannel[playerid] = 0;
The commands
Okay, I'll be using DCMD to create the commands, although I would recommend using something like ZCMD, because it is faster.

The /cbchannel (or /setchannel - whatever you want to call it) is a pretty easy thing to make! Just, when a player sends that command, check to see if there "parameters" are correct (between or equal to 1 and 40) and that it is not "" (blank).

I will show you this, however, don't copy it. Understand what it does, this way you won't have to figure out the /cb command aswell.

pawn Code:
dcmd_cbchannel(playerid, params[])
{
    if(!strlen(params) && /*!IsNumeric(params)*/) return SendClientMessage(playerid, COLOUR_RED, "No parameters (Usage: /cbchannel [1-40]");
    //If the length of params is 0 (non existent) - it returns an error message
    //I have commeneted our "IsNumeric" because I am not supplying it with the tutorial (because I can't find it!)
    if(strval(params) < 1 || strval(params) > 40) return SendClientMessage(playerid, COLOUR_RED, "Only channels 1 upto 40 exist");
    //If the integer value of the parameters string is lower than 1 or bigger than 40 then it returns another error message
    pChannel[playerid] = strval(params);
    //Set the players channel
        //You may also want to add a "Your channel is now %i" message.
    return 1;
    //Return 1 (command complete)
}
On now onto the CB command. This, is possibly the most simple command. All you need is a loop, to loop through all players, find a matching channel, and send the message! Again, don't copy - read and re-write.

pawn Code:
dcmd_cb(playerid, params[])
{
    if(!strlen(params)) return SendClientMessage(playerid, COLOUR_RED, "No parameters (Usage: /cb [message]");
    //Error message if the player did not specify any parameters
    static str[128];
    //Why static? It stops it being created more than once (in fact if you decompiled an AMX script you would find the "static" variable has become GLOBAL)
    format(str, sizeof(str), "[CB]%s: %s", ReturnName(playerid), params);
    //Format the output message (str)
    //ReturnName, is a function to return a players name - I will include it toward the end of this post
    //You could use colour embedding here!
    for(new i; i < GetMaxPlayers(); i++)
    {
        //Start the loop (GetMaxPlayers will return the MAXIMUM player slots on your server - you could use Foreach here instead)
        if(!IsPlayerConnected(i)) continue;
        //If the player is not connected, skip to the next one
        if(pChannel[i] == pChannel[playerid])
        {
            //If the player channel matches the other players channel
            SendClientMessage(i, COLOUR_YELLOW, str);
        }
    }
    return 1;
    //End the command (return 1)
}
Of course, to get these command to work, you will need the DCMD define and the OnPlayerCommandText (OPCT) callback.
To add a command into OPCT using DCMD, you must follow this format:
pawn Code:
dcmd(COMMAND_NAME, COMMAND_NAME_LENGTH, cmdtext);
To add the commands you just created in, use this:
pawn Code:
dcmd(cbchannel, 9, cmdtext);
dcmd(cb, 2, cmdtext);
//Of course, if you have renamed any commands, change them here too!
OnPlayerSpawn
Some people will want the players channel set when they spawn. It's pretty simple to do.

In "OnPlayerSpawn", add this:
pawn Code:
pChannel[playerid] = YOUR WANTED CHANNEL!;
You could use a switch, an if statement, or the ternary operator, to base the channel on a team or point system.
pawn Code:
if(GetPlayerTeam(playerid) == ...) //If statement
switch(GetPlayerTeam(playerid))
{
     case TEAM..: { }
}//Switch statement
( (GetPlayerTeam(playerid) == TEAM_...) ? pChannel[playerid]=1 : ... ) //Ternary / Triadic operator
If you're just starting out, keep it simple with things you know.

Anything else?
No, you're good to go!

One thing you will need to do though... I have used colour defines in my examples, you will need to change them to the hexadecimal values or make them correspond with your colour defines.

I also said I would give the "ReturnName" function and the DCMD definition:

pawn Code:
#define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1

stock ReturnName(id)
{
     static name[24];
     GetPlayerName(id, name, 24);
     return name;
}
You will need to include both somewhere in your script for the commands to work (unless you were to use GetPlayerName with an extra string variable in there somewhere and a different command processor).

Final Word
I hope that helped. If anyone asks how to create a CB radio system, I will forward them here.
Here is a filterscript I made by following my own tutorial (lol): https://sampforum.blast.hk/showthread.php?pid=1280326#pid1280326

Happy Scripting!
Reply
#2

lot of warning . Who can make filterscript?
Reply
#3

There are allready one made by funky1234...

https://sampforum.blast.hk/showthread.php?tid=267126
Reply
#4

tthanks
Reply
#5

If you're wondering what a "CB Radio" is, check this out: http://en.wikipedia.org/wiki/CB_Radio
Reply
#6

Quote:
Originally Posted by Rock_Ro
View Post
There are allready one made by funky1234...

https://sampforum.blast.hk/showthread.php?tid=267126
and who is the topic creator?" and whats does it say in the end of his tut?
Reply
#7

Nice dude
Reply
#8

pawn Code:
#include <a_samp>

new pChannel[MAX_PLAYERS];

#define COLOR_RED 0xFF0000FF
#define COLOR_YELLOW 0xFFFF00FF
#define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1

//publics

public OnPlayerDisconnect(playerid, reason)
{
    #pragma unused reason
    pChannel[playerid] = 0;
    return 1;
}

public OnPlayerSpawn(playerid)
{
    pChannel[playerid] = 1;
    return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
    dcmd(cbch, 4, cmdtext);
    dcmd(cb, 2, cmdtext);
    return 0;
}

public OnPlayerConnect(playerid)
{
    SendClientMessage(playerid, COLOR_RED, "CB-Radio-System. Use /cbch [1-40] to select a CH.");
    SendClientMessage(playerid, COLOR_YELLOW, "Use /cb [message] to speak with players into the same channel.");
    SendClientMessage(playerid, COLOR_RED, "have FUN with your new CB-Radio.");
    return 1;
}

//commands

dcmd_cbch(playerid, params[])
{
    if(!strlen(params)) /*&& !IsNumeric(params)*/ return SendClientMessage(playerid, COLOR_RED, "No parameters (Usage: /cbch [1-40]");
    //If the length of params is 0 (non existent) - it returns an error message
    //I have commeneted our "IsNumeric" because I am not supplying it with the tutorial (because I can't find it!)
    if(strval(params) < 1 || strval(params) > 40) return SendClientMessage(playerid, COLOR_RED, "Only channels 1 upto 40 exist");
    //If the integer value of the parameters string is lower than 1 or bigger than 40 then it returns another error message
    pChannel[playerid] = strval(params);
    //Set the players channel
        //You may also want to add a "Your channel is now %i" message.
    return 1;
    //Return 1 (command complete)
}

dcmd_cb(playerid, params[])
{
    if(!strlen(params)) return SendClientMessage(playerid, COLOR_RED, "No parameters (Usage: /cb [message]");
    //Error message if the player did not specify any parameters
    static str[128];
    //Why static? It stops it being created more than once (in fact if you decompiled an AMX script you would find the "static" variable has become GLOBAL)
    format(str, sizeof(str), "[CB]%s: %s", ReturnName(playerid), params);
    //Format the output message (str)
    //ReturnName, is a function to return a players name - I will include it toward the end of this post
    //You could use colour embedding here!
    for(new i; i < GetMaxPlayers(); i++)
    {
        //Start the loop (GetMaxPlayers will return the MAXIMUM player slots on your server - you could use Foreach here instead)
        if(!IsPlayerConnected(i)) continue;
        //If the player is not connected, skip to the next one
        if(pChannel[i] == pChannel[playerid])
        {
            //If the player channel matches the other players channel
            SendClientMessage(i, COLOR_YELLOW, str);
        }
    }
    return 1;
    //End the command (return 1)
}

stock ReturnName(playerid)
{
    static name[24];
    GetPlayerName(playerid, name, 24);
    return name;
}
What done I wrong
Reply
#9

Quote:
Originally Posted by fatspeeddog
View Post
pawn Code:
#include <a_samp>

new pChannel[MAX_PLAYERS];

#define COLOR_RED 0xFF0000FF
#define COLOR_YELLOW 0xFFFF00FF
#define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1

//publics

public OnPlayerDisconnect(playerid, reason)
{
    #pragma unused reason
    pChannel[playerid] = 0;
    return 1;
}

public OnPlayerSpawn(playerid)
{
    pChannel[playerid] = 1;
    return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
    dcmd(cbch, 4, cmdtext);
    dcmd(cb, 2, cmdtext);
    return 0;
}

public OnPlayerConnect(playerid)
{
    SendClientMessage(playerid, COLOR_RED, "CB-Radio-System. Use /cbch [1-40] to select a CH.");
    SendClientMessage(playerid, COLOR_YELLOW, "Use /cb [message] to speak with players into the same channel.");
    SendClientMessage(playerid, COLOR_RED, "have FUN with your new CB-Radio.");
    return 1;
}

//commands

dcmd_cbch(playerid, params[])
{
    if(!strlen(params)) /*&& !IsNumeric(params)*/ return SendClientMessage(playerid, COLOR_RED, "No parameters (Usage: /cbch [1-40]");
    //If the length of params is 0 (non existent) - it returns an error message
    //I have commeneted our "IsNumeric" because I am not supplying it with the tutorial (because I can't find it!)
    if(strval(params) < 1 || strval(params) > 40) return SendClientMessage(playerid, COLOR_RED, "Only channels 1 upto 40 exist");
    //If the integer value of the parameters string is lower than 1 or bigger than 40 then it returns another error message
    pChannel[playerid] = strval(params);
    //Set the players channel
        //You may also want to add a "Your channel is now %i" message.
    return 1;
    //Return 1 (command complete)
}

dcmd_cb(playerid, params[])
{
    if(!strlen(params)) return SendClientMessage(playerid, COLOR_RED, "No parameters (Usage: /cb [message]");
    //Error message if the player did not specify any parameters
    static str[128];
    //Why static? It stops it being created more than once (in fact if you decompiled an AMX script you would find the "static" variable has become GLOBAL)
    format(str, sizeof(str), "[CB]%s: %s", ReturnName(playerid), params);
    //Format the output message (str)
    //ReturnName, is a function to return a players name - I will include it toward the end of this post
    //You could use colour embedding here!
    for(new i; i < GetMaxPlayers(); i++)
    {
        //Start the loop (GetMaxPlayers will return the MAXIMUM player slots on your server - you could use Foreach here instead)
        if(!IsPlayerConnected(i)) continue;
        //If the player is not connected, skip to the next one
        if(pChannel[i] == pChannel[playerid])
        {
            //If the player channel matches the other players channel
            SendClientMessage(i, COLOR_YELLOW, str);
        }
    }
    return 1;
    //End the command (return 1)
}

stock ReturnName(playerid)
{
    static name[24];
    GetPlayerName(playerid, name, 24);
    return name;
}
What done I wrong
You have done nothing wrong.
Also, I replied to your post on the Filterscript thread.
Reply
#10

how to add parameters to 1-1000 not 1-40 because it was too little
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)