Passive Cloaking!

At this point many of you may be asking, "What is passive cloaking".  Well being passive means it's always turned on, but it doesn't mean you're always cloaked.  With this add-on, your player will cloak only if they stand still for 10 seconds (it's adjustable).  Of course you can turn and shoot, but you can't jump or move in any direction.  As long as you are moving, you will be uncloaked.  Messages will notify you of your visibility also, so you'll know when you cloak/uncloak.

Let's get started:

Add in the blue code and take out any pink code.

Open up g_local.h and place these variables in the gclient_s structure:

 

    int             machinegun_shots;     // for weapon raising

    int             passiveon;
    int             passiveoff;

 

Open up p_client.c and go down to the clientthink() function.  Add in the whole section as shown:

 

/*
==============
ClientThink

This will be called once for each client frame, which will
usually be a couple times for each server frame.
==============
*/
void ClientThink (edict_t *ent, usercmd_t *ucmd)
{
    gclient_t    *client;
    edict_t    *other;
    int        i, j;
    pmove_t    pm;

    level.current_entity = ent;
    client = ent->client;

    if (level.intermissiontime)
    {
        client->ps.pmove.pm_type = PM_FREEZE;
        // can exit intermission after five seconds
        if (level.time > level.intermissiontime + 5.0
            && (ucmd->buttons & BUTTON_ANY) )
            level.exitintermission = true;
        return;
    }
    pm_passent = ent;

    // set up for pmove
    memset (&pm, 0, sizeof(pm));

    if (ent->movetype == MOVETYPE_NOCLIP)
        client->ps.pmove.pm_type = PM_SPECTATOR;
    else if (ent->s.modelindex != 255)
        client->ps.pmove.pm_type = PM_GIB;
    else if (ent->deadflag)
        client->ps.pmove.pm_type = PM_DEAD;
    else
        client->ps.pmove.pm_type = PM_NORMAL;

    client->ps.pmove.gravity = sv_gravity->value;

//passive cloaking start

if ((ucmd->forwardmove == 0) && (ucmd->sidemove == 0) && (ucmd->upmove == 0))
{
    ent->client->passiveoff = 0;

            ent->client->passiveon ++;
            if (ent->client->passiveon == 300)
            {
                ent->svflags |= SVF_NOCLIENT;
                gi.centerprintf (ent, "You Are Cloaked!\n");
                ent->client->passiveon = 301;
            }
}

if ((ucmd->forwardmove != 0) || (ucmd->sidemove != 0) || (ucmd->upmove != 0))
{
            ent->client->passiveon = 0;
}
//for passive cloaking off
if (ent->svflags & SVF_NOCLIENT)
{
if ((ucmd->forwardmove != 0) || (ucmd->sidemove != 0) || (ucmd->upmove != 0))
{

                ent->client->passiveon = 0;
                ent->client->passiveoff ++;
                if (ent->client->passiveoff == 1)
                {
                    ent->svflags &= ~SVF_NOCLIENT;
                    gi.centerprintf (ent, "You Are Visible!\n");
                    ent->client->passiveon = 0;
                    ent->client->passiveoff = 2;
                }
}
}
//passive cloaking end

That's it!  You can adjust the time before the player cloaks in the first section with passiveon.  All that the part in the clientthink() function does is check to see if a player is moving or not moving.  If not moving, it runs the integer and when it hits a certain point it makes the player invisible.  On the other hand, if the player was cloaked and then moves, the clientthink calls on another counter which makes you become visible immediately.  Please note that this does pound the clientthink pretty hard, but I've used it on commercial servers and it seems to be fine as long as it's class based.  To make it for a single class, just do an if statement at the beginning of the part inside of clientthink and add another brace at the end.  If all your players can cloak like this, it can produce a good bit of lag.

Hope ya like it!

Tutorial by Willi