[Tutorial] Creating a boundaries system
#1

Hi,
I thought this tutorial might be useful for some people who work on a TDM/DM servers, this tutorial will 'probably' teach you how to keep players within the allowed area only, getting out will kill them(how sad).

Anyway, here we begin:
pawn Code:
new MapZone[MAX_PLAYERS]; // we need this to identify the area/zone or whatever.
Now, this:
pawn Code:
new Float:Max[2] = {569.5944,2742.9941};
new Float:Min[2] = {-838.9635,1163.5237};
Max should ALWAYS be the bigger number, if not, everywhere will be out of boundaries! So be careful.
Replace them with your preferred co-ordinates. These co-ordinates are from the abandoned airport to that big bridge.

Now under OnGameModeInit() :
pawn Code:
MapZone = GangZoneCreate(Min[0],Min[1],Max[0],Max[1]);
We have created the zone, but it's not visible YET.
Let me explain, as you've seen above, I've set it to Float:Max[2], 2 means the two numbers between the brackets, 569.5944 and 2742.9941, so by using Max[0], it means that we have selected the first number. I guess you understand now. But you always should start with 0.

Under OnPlayerDisconnect :
pawn Code:
GangZoneHideForPlayer(playerid, MapZone); // hides the zone/area when the player leaves.
Now under OnPlayerSpawn :
pawn Code:
GangZoneShowForPlayer(playerid, MapZone, 0xBBBBBB88); // Shows the zone to the player when he spawns, '0xBBBBBB88' is the color GREY.
Now let's create a timer that loops every 1 second. (DO NOT USE OnPlayerUpdate! It's NOT per second!).
pawn Code:
new maintimer; // our timer <3
You can rename that to whatever you want.
___
Go back to OnGameModeInit()
pawn Code:
maintimer = SetTimer("MainTimer", 1000, true); // 1000 = one second. true = loops/repeats every 1 second.
You can now go anywhere you like, and forward your MainTimer and add your functions:

pawn Code:
forward MainTimer();
public MainTimer()
{
        new Float:HP; // player health
        for(new i=0; i < MAX_PLAYERS; i++) // loops through players
        {
                if(IsPlayerConnected(i) && (GetPVarInt(i, "Spectate") == 0)) //You can add more exceptions here, where you don't want boundaries e.g. dms, duels, etc.
                {
                        if(IsPlayerInArea(i,Min[0], Max[0], Min[1], Max[1]) != 1) // check if the player is not in the area/zone.
                        {
                                GetPlayerHealth(i, HP); // gets player health
                                SetPlayerHealth(i, HP-5); // reduces -5%, every 1 second.
                                GameTextForPlayer(i, "~r~Stay inside boundaries~n~or you will be killed!", 3000, 3); // Shows a game text for the player who got out of the boundaries, 3000 = 3 seconds, 3 = font style.
                        }
                }
        }
}
Now go under OnGameModeExit() :
pawn Code:
KillTimer(maintimer); // kill the timer, just in case.
Full code:
pawn Code:
new MapZone[MAX_PLAYERS]; // we need this to identify the area/zone or whatever.
new Float:Max[2] = {569.5944,2742.9941};
new Float:Min[2] = {-838.9635,1163.5237};
public OnGameModeInit()
{
    MapZone = GangZoneCreate(Min[0],Min[1],Max[0],Max[1]);
    maintimer = SetTimer("MainTimer", 1000, true); // 1000 = one second. true = loops/repeats every 1 second.
    return 1;
}
public OnGameModeExit()
{
    KillTimer(maintimer); // kill the timer, just in case.
    return 1;
}
forward MainTimer();
public MainTimer()
{
        new Float:HP; // player health
        for(new i=0; i < MAX_PLAYERS; i++) // loops through players
        {
                if(IsPlayerConnected(i) && (GetPVarInt(i, "Spectate") == 0)) //You can add more exceptions here, where you don't want boundaries e.g. dms, duels, etc.
                {
                        if(IsPlayerInArea(i,Min[0], Max[0], Min[1], Max[1]) != 1) // check if the player is not in the area/zone.
                        {
                                GetPlayerHealth(i, HP); // gets player health
                                SetPlayerHealth(i, HP-5); // reduces -5%, every 1 second.
                                GameTextForPlayer(i, "~r~Stay inside boundaries~n~or you will be killed!", 3000, 3); // Shows a game text for the player who got out of the boundaries, 3000 = 3 seconds, 3 = font style.
                        }
                }
        }
}
Thank you for taking time to read this, and hope you got some useful information.
Feedback would be appreciated.
Reply
#2

Your idea could also be done on servers to force the players to get moved to the location by themselves, though I prefer SetPlayerWorldBounds for DM activities.
Reply
#3

Okay, thanks for your feedback.
Reply
#4

Quote:
Originally Posted by Lordz™
View Post
Your idea could also be done on servers to force the players to get moved to the location by themselves, though I prefer SetPlayerWorldBounds for DM activities.
well that is highly abused in DMs. Sometime to avoid get killed some nabs jump toward boundry and does suicide via it. so for general DM i wont be liking that function. however to cover stunt areas (if any) or some other stuff i am unable to take up in mind , Setbound function is good!

anyways good tutorial.
Reply
#5

Yes but when they die, it counts as a death in their statistics, which is not good for them.
Reply
#6

Quote:
Originally Posted by MythicalMarauder
View Post
Yes but when they die, it counts as a death in their statistics, which is not good for them.
Otherwise too some typical nabs are willing to die rather than being killed by another player , have been facing this since years!
Reply
#7

Hi.

This is very limiting code, as you'll have to declare (AREAS) * 2 arrays of 2 cells for all the areas in your mode, MANUALLY, write the code for each manually.. why not teach new people a better way?

pawn Code:
static mapZones[MAX_GANGZONES] = {-1, ...},
        mainTimer;

new Float: gZones[][4] =
{
    {-838.9635, 1163.5237, 569.5944, 2742.9941}
//  {minX,  minY,   maxX,   maxY}
};

forward public MainTimer();

public OnGameModeInit()
{
    for(new i = 0; i < sizeof(gZones); i++)
        mapZones[i] = GangZoneCreate(gZones[i][0], gZones[i][1], gZones[i][2], gZones[i][3]);
   
    mainTimer = SetTimer("MainTimer", 1000, true);
   
    return 1;
}

public OnPlayerDisconnect(playerid)
{  
    for(new i = 0; i < sizeof(gZones); i++)
        GangZoneHideForPlayer(playerid, mapZones[i]);
       
    return 1;
}

public OnPlayerConnect(playerid)
{
    for(new i = 0; i < sizeof(gZones); i++)
        GangZoneShowForPlayer(playerid, MapZone, 0xBBBBBB88);
       
    // Again, per zone colors can be added in the array too!
   
    return 1;
}

public MainTimer()
{
    new Float:HP; // player health
    for(new i= 0; i < MAX_PLAYERS; i++) // loops through players
    {
        if(IsPlayerConnected(i)) //You can add more exceptions here, where you don't want boundaries e.g. dms, duels, etc.
        {
            for(new j = 0; j < sizeof(gZones); j++)
            {
                if(IsPlayerInArea(i, gZones[j][0], gZones[j][1], gZones[j][2], gZones[j][3] ) != 1) // check if the player is not in the area/zone.
                {
                    GetPlayerHealth(i, HP); // gets player health
                    SetPlayerHealth(i, HP-5); // reduces -5%, every 1 second.
                    GameTextForPlayer(i, "~r~Stay inside boundaries~n~or you will be killed!", 3000, 3); // Shows a game text for the player who got out of the boundaries, 3000 = 3 seconds, 3 = font style.
                }
            }
        }
    }
}
I've just added some code, and copied some of yours. Sorry, lazy me
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)