SA-MP Forums Archive
[Tutorial] Range Commands. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Range Commands. (/showthread.php?tid=524265)



Range Commands. - Ghazal - 06.07.2014

Hello there!

Today i am coming up with a new tutorial.

How to make a range command. For example: range freeze, range unfreeze.

Let's begin!

First Step: Define the includes.

We are going to use the following includes:
pawn Код:
#include <a_samp>
#include <zcmd>
#include <sscanf>
Question: Why are you using zcmd?
Answer: Because its the fastest command processor in my opinion.

Second Step: Lets define the variables

We need to define If the player is frozen or spawned.

pawn Код:
new IsSpawned[MAX_PLAYERS];
new IsFrozen[MAX_PLAYERS];
Question: Why did we define this variables?
Answer: To avoid bugs.
Now we need to add the variable of isspawned = 1; under OnPlayerSpawn.

pawn Код:
public OnPlayerSpawn(playerid)
{
    IsSpawned[playerid] = 1;
    return 1;
}
Now we need to add the variable of IsSpawned[playerid] = 0; under OnPlayerDeath.
pawn Код:
public OnPlayerDeath playerid, killerid, reason)
{
    IsSpawned[playerid] = 0;
    return 1;
}
Now we are going to start scripting the commands.
Lets begin with rfreeze (rangefreeze)
First we are going to add this for sure.
pawn Код:
CMD:rfreeze(playerid,params[])
{
Now we are going to add some new variables like range and string
pawn Код:
new range;
new string[128];
Now we are going to use sscanf to make sure that the players will use the command right.
pawn Код:
if(sscanf(params,"i",range))
    {
        SendClientMessage(playerid,-1,"{CACA00}[INFO]{FFFFFF}: /rfreeze (Meters)");
        return 1;
    }
Now we are going to add something to make sure that the player who will use the command is spawned.
pawn Код:
if(IsSpawned[playerid] == 0)
    {
        SendClientMessage(playerid,-1,"{CACA00}[ERROR]{FFFFFF}You must be spawned in order to use this command.");
        return 1;
    }
And now, we are going to script the main part in the command.
We are going to check the players connected and the players near the player who use the command with the range he typed and toggle their control
pawn Код:
for(new i=0;i<MAX_PLAYERS;i++)
    {
        if(IsPlayerConnected(i) && GetDistanceBetweenPlayers(playerid,i) < range)
        {
            format(string, sizeof(string), "%s(%d) have Freezed all players in range of %d meters.",PlayerName(playerid),playerid,range);
            SendClientMessage(i,-1,string);
            TogglePlayerControllable(i,0);
            IsFrozen[i] =1;
        }
    }
Question:Why did we add IsFrozen[i] = 1;?
Answer: To set the players to frozen so when we can unfreeze them.
Question: Why did we add " [i] "
Answer: as you saw we defined i in the for(new to max_players which means we defined i as the all of the players connected.
And now we are going to end the command with
pawn Код:
return 1;
}
Full code for the /rfreeze
pawn Код:
CMD:rfreeze(playerid,params[])
{
    new string[128];
    new range;
    if(sscanf(params,"i",range))
    {
        SendClientMessage(playerid,-1,"{CACA00}[INFO]{FFFFFF}: /rfreeze (Meters)");
        return 1;
    }
    if(IsSpawned[playerid] == 0)
    {
        SendClientMessage(playerid,-1,"{CACA00}[ERROR]{FFFFFF}You must be spawned in order to use this command.");
        return 1;
    }
    for(new i=0;i<MAX_PLAYERS;i++)
    {
        if(IsPlayerConnected(i) && GetDistanceBetweenPlayers(playerid,i) < range)
        {
            format(string, sizeof(string), "%s(%d) have Freezed all players in range of %d meters.",PlayerName(playerid),playerid,range);
            SendClientMessage(i,-1,string);
            TogglePlayerControllable(i,0);
            IsFrozen[i] =1;
        }
    }
    return 1;
}
Also there is another important thing we should add.
Its a public and a forward for GetDistanceBetweenPlayers which let us know the distance between the players with meters.
pawn Код:
forward Float:GetDistanceBetweenPlayers(p1,p2);
public Float:GetDistanceBetweenPlayers(p1,p2)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    if (!IsPlayerConnected(p1) || !IsPlayerConnected(p2))
    {
        return -1.00;
    }
    GetPlayerPos(p1,x1,y1,z1);
    GetPlayerPos(p2,x2,y2,z2);
    return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
}
Also here is a full filterscript for Range freeze and unfreeze.
pawn Код:
#include <a_samp>
#include <zcmd>
#include <sscanf>
new IsSpawned[MAX_PLAYERS];
new IsFrozen[MAX_PLAYERS];
forward Float:GetDistanceBetweenPlayers(p1,p2);
public OnFilterScriptInit()
{
    SendClientMessageToAll(-1,"{878787}[NOTE] {FFFFFF}This server is using /rfreeze and /runfreeze by Maro06");
    return 1;
}
public OnFilterScriptExit()
{
    return 1;
}
public OnPlayerSpawn(playerid)
{
    IsSpawned[playerid] = 1;
    return 1;
}
public OnPlayerDeath playerid, killerid, reason)
{
    IsSpawned[playerid] = 0;
    return 1;
}
CMD:rfreeze(playerid,params[])
{
    new string[128];
    new range;
    if(sscanf(params,"i",range))
    {
        SendClientMessage(playerid,-1,"{CACA00}[INFO]{FFFFFF}: /rfreeze (Meters)");
        return 1;
    }
    if(IsSpawned[playerid] == 0)
    {
        SendClientMessage(playerid,-1,"{CACA00}[ERROR]{FFFFFF}You must be spawned in order to use this command.");
        return 1;
    }
    for(new i=0;i<MAX_PLAYERS;i++)
    {
        if(IsPlayerConnected(i) && GetDistanceBetweenPlayers(playerid,i) < range)
        {
            format(string, sizeof(string), "%s(%d) have Freezed all players in range of %d meters.",PlayerName(playerid),playerid,range);
            SendClientMessage(i,-1,string);
            TogglePlayerControllable(i,0);
            IsFrozen[i] =1;
        }
    }
    return 1;
}
CMD:runfreeze(playerid,params[])
{
    new string[128];
    new range;
    if(sscanf(params,"i",range))
    {
        SendClientMessage(playerid,-1,"{CACA00}[INFO]{FFFFFF}: /runfreeze (Meters)");
        return 1;
    }
    if(IsSpawned[playerid] == 0)
    {
        SendClientMessage(playerid,-1,"{CACA00}[ERROR]{FFFFFF}You must be spawned in order to use this command.");
        return 1;
    }
    for(new i=0;i<MAX_PLAYERS;i++)
    {
        if(IsPlayerConnected(i) && GetDistanceBetweenPlayers(playerid,i) < range)
        {
            format(string, sizeof(string), "%s(%d) have Un Freezed all players in range of %d meters.",PlayerName(playerid),playerid,range);
            SendClientMessage(i,-1,string);
            TogglePlayerControllable(i,1);
            IsFrozen[i] =0;
        }
    }
    return 1;
}
public Float:GetDistanceBetweenPlayers(p1,p2)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    if (!IsPlayerConnected(p1) || !IsPlayerConnected(p2))
    {
        return -1.00;
    }
    GetPlayerPos(p1,x1,y1,z1);
    GetPlayerPos(p2,x2,y2,z2);
    return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
}
stock PlayerName(playerid) {
  new name[255];
  GetPlayerName(playerid, name, 255);
  return name;
}



Re: Range Commands. - GeekSiMo - 06.07.2014

Nice? And must i add IsSpawned [playerid] = 0 in OnPlayerDeath


Re: Range Commands. - Ghazal - 06.07.2014

Quote:
Originally Posted by GeekSiMo
Посмотреть сообщение
Nice? And must i add IsSpawned [playerid] = 0 in OnPlayerDeath
Oh ye, I forgot to mention this on the tutorial.


Re: Range Commands. - Ghazal - 10.07.2014

Updated.


Re: Range Commands. - Blonctron - 17.11.2014

Will this work for any command? I have a cargoship and atm people can type /boardship from anywhere on the map, but i only want it when they are near the boat.


Re: Range Commands. - N.K.Stallone - 18.11.2014

Good job on the tutorial, mate