Welcome, Guest! Login | Register

CTF Gameplay Part 2 [Print this Article]
Posted by: DarkNight
Date posted: Apr 14 2003
User Rating: 5 out of 5.0
Number of views: 1639
Number of comments: 0
Description: Creating the Capture Point
This part of the tutorial will show you how to create the Capture Point for the CTF Gamerules.

NOTE: This code will need modifying to suit your team system,ect so don't take this as a complete C&P (which it basically is).



Open up ctf_gameplay.h and put the following at the bottom:
 CODE  


// Capture point for Team 1

class CCaptureTeam1 : public CBaseEntity
{
public:
   
       void Spawn( );
       void Precache( );
       void EXPORT Touch(CBaseEntity *);
       void EXPORT Think( );
       void KeyValue( KeyValueData* );
};



// Capture point for Team 2

class CCaptureTeam2 : public CBaseEntity
{
public:
   
       void Spawn( );
       void Precache( );
       void EXPORT Touch( CBaseEntity *);
       void EXPORT Think( );
       void KeyValue( KeyValueData* );
};


In player.h, put the following in class CBasePlayer : public CBaseMonster
in the publics under bool m_fHasObject;
 CODE  


    bool m_bInCapture;


Open up ctf_gameplay.cpp and put the following at the bottom:
 CODE  


LINK_ENTITY_TO_CLASS( capture_team1, CCaptureTeam1 );

void CCaptureTeam1 :: Spawn( )
{


    // Calls the precache function to precache any models, sounds,ect
    // you need before it spawns

    Precache( );
   
    SET_MODEL( ENT(pev), STRING(pev->model) );
   
    UTIL_SetOrigin( pev, pev->origin );
    pev->movetype = MOVETYPE_FLY;


    // This allows you to trigger it by walking through it

    pev->solid = SOLID_TRIGGER;


    // This just sets our Touch to Touch and our Think to Think
    // which is down a little further.

    SetTouch(Touch);
    SetThink(Think);


    // Sets next think time

    pev->nextthink = gpGlobals->time + 0.1;


    // Make the entity invisible

    pev->rendermode = kRenderTransTexture;
    pev->renderamt = 0;
   
}


// This is the Precache function which is called from the Spawn function.


void CCaptureTeam1 :: Precache( )
{
    PRECACHE_MODEL( (char*)STRING(pev->model) );
}
void CCaptureTeam1 :: Think( )
{

    // loop through every player and check if they are in the area

    for(int i=0; i<gpGlobals->maxClients; i++)
    {
         CBasePlayer *pPlayer = (CBasePlayer *)UTIL_PlayerByIndex(i);
         if(pPlayer && pPlayer->m_iTeam == 1)
         {
    if((pPlayer->pev->origin.x >= pev->mins.x) && (pPlayer->pev->origin.x <= pev->maxs.x) &&
    (pPlayer->pev->origin.y >= pev->mins.y) && (pPlayer->pev->origin.y <= pev->maxs.y) &&
    (pPlayer->pev->origin.z >= pev->mins.z) && (pPlayer->pev->origin.z <= pev->maxs.z))
    pPlayer->m_bInCapture = true;
    else
    pPlayer->m_bInCapture = false;
          }
    }

   
    // Set next think time

    pev->nextthink = gpGlobals->time + 0.1;  
}


// This is the Touch function which was set above, in the Spawn function,
// its basically the stuff that runs when the capture point has been touched by a player

void CCaptureTeam1 :: Touch( CBaseEntity* pOther )
{

    // Determine if the object that touches it, is a player
    // and check if the player is alive.

    if(!pOther)
        return;
    if(!pOther->IsPlayer())
        return;
    if(!pOther->IsAlive())
        return;

    CBasePlayer *pPlayer = (CBasePlayer *)pOther;

    if(pPlayer && pPlayer->m_iTeam == 1)
    {

        // Check to see if they have the object

        if( !pPlayer->m_fHasObject )
             return;
        else
        {

                // Print to HUD who has captured the flag

                UTIL_ClientPrintAll( HUD_PRINTCENTER, UTIL_VarArgs
                ( "%s has captured the Flag!\n", STRING( pPlayer->pev->netname )));

                 // Remove the flag

                 pPlayer->m_fHasObject = false;

                 PLAYBACK_EVENT_FULL(0, pPlayer->edict(), g_usObject, 0, (float *)&g_vecZero,
                 (float *)&g_vecZero, 0, 0, FLAG_CAPTURE, 0, 0, 0);

                
                 // Reset the flag

                 CObjectFlag *pFlag = (CObjectFlag *)UTIL_FindEntityByClassname(NULL, "object_flag");
                 if(pFlag)
                 {
                        pFlag->m_fIsInPlay = false;

                        // Do a funky effect on the flag when it gets reset

                        pFlag->pev->effects = EF_BRIGHTFIELD;
                 }
        }
    }
    else
        return;
// wrong team
}

void CCaptureTeam1 :: KeyValue( KeyValueData* Data )
{
   // This will store the points but its not fully implemented. I'll leave it for you to do.

    if( FStrEq( Data->szKeyName, "points" ) )
    {
    int m_iPoints = atoi(Data->szValue);
    Data->fHandled = true;
    }

    CBaseEntity::KeyValue(Data);
// call the parent function
}



 CODE  


LINK_ENTITY_TO_CLASS( capture_team2, CCaptureTeam2 );

void CCaptureTeam2 :: Spawn( )
{

    // Calls the precache function to precache any models, sounds,ect    
    // you need before it spawns.

    Precache( );
   
    SET_MODEL( ENT(pev), STRING(pev->model) );
   
    UTIL_SetOrigin( pev, pev->origin );
    pev->movetype = MOVETYPE_FLY;


    // This allows you to trigger it by walking through it

    pev->solid = SOLID_TRIGGER;


    // This just sets our Touch to Touch and our Think to Think
    // which is down a little further.

    SetTouch(Touch);
    SetThink(Think);


    // Set next think time

    pev->nextthink = gpGlobals->time + 0.1;


    // Make the entity invisible

    pev->rendermode = kRenderTransTexture;
    pev->renderamt = 0;
}

void CCaptureTeam2 :: Precache( )
{
    PRECACHE_MODEL( (char*)STRING(pev->model) );
}
void CCaptureTeam2 :: Think( )
{

    // loop through every player and check if they are in the area

    for(int i=0; i<gpGlobals->maxClients; i++)
    {
         CBasePlayer *pPlayer = (CBasePlayer *)UTIL_PlayerByIndex(i);
         if(pPlayer && pPlayer->m_iTeam == 2)
         {
             if((pPlayer->pev->origin.x >= pev->mins.x) && (pPlayer->pev->origin.x <= pev->maxs.x) &&
             (pPlayer->pev->origin.y >= pev->mins.y) && (pPlayer->pev->origin.y <= pev->maxs.y) &&
             (pPlayer->pev->origin.z >= pev->mins.z) && (pPlayer->pev->origin.z <= pev->maxs.z))
             pPlayer->m_bInCapture = true;
             else
             pPlayer->m_bInCapture = false;
         }
    }
   

    // Set the next think time

    pev->nextthink = gpGlobals->time + 0.1;
   
}
void CCaptureTeam2 :: Touch( CBaseEntity* pOther )
{

    // Determine if the object that touches it, is a player
    // and check if the player is alive.

    if(!pOther)
        return;
    if(!pOther->IsPlayer())
        return;
    if(!pOther->IsAlive())
        return;

    CBasePlayer *pPlayer = (CBasePlayer *)pOther;

     if(pPlayer && pPlayer->m_iTeam == 2)
     {

          // Check to see if they have the object

          if( !pPlayer->m_fHasObject )
              return;
          else
          {    

              // Print to HUD who has captured the flag

              UTIL_ClientPrintAll( HUD_PRINTCENTER, UTIL_VarArgs
              ( "%s has captured the Flag!\n", STRING( pPlayer->pev->netname )));

  
              // Remove the object

              pPlayer->m_fHasObject = false;

              PLAYBACK_EVENT_FULL(0, pPlayer->edict(), g_usObject, 0, (float *)&g_vecZero,
              (float *)&g_vecZero, 0, 0, FLAG_CAPTURE, 0, 0, 0);


              // Reset the object

              CObjectFlag *pFlag = (CObjectFlag *)UTIL_FindEntityByClassname(NULL, "object_flag");
              if(pFlag)
              {
                   pFlag->m_fIsInPlay = false;


                   // Do a funky effect on the flag when it gets reset

                   pFlag->pev->effects = EF_BRIGHTFIELD;
              }
          }
    }
    else
        return;
// Wrong team
}


void CCaptureTeam2 :: KeyValue( KeyValueData* Data )
{
// This will store the points but its not fully implemented. I'll leave it for you to do.

    if( FStrEq( Data->szKeyName, "points" ) )
    {
        int m_iPoints = atoi(Data->szValue);
        Data->fHandled = true;
    }

    CBaseEntity::KeyValue(Data);
// call the parent function
}



That just about raps it up for this part of the tutorial. If you have suggestions,complaints or need help with something from this tutorial, please email me at the_dark_wonderer@hotmail.com

Rate This Article
This article is currently rated: 5 out of 5.0 (1 Votes)

You have to register to rate this article.
User Comments

No User Comments

You must register to post a comment. If you have already registered, you must login.
Donate
Any money that is donated will go towards the server costs that I incur for running our server. Don't feel you HAVE to donate, but the link is there if you want to help us out a little. Thanks :)

Latest Articles
3rd person View in Multiplayer
Half-Life 2 | Coding | Client Side Tutorials
How to enable it in HL2DM

By: cct | Nov 13 2006

Making a Camera
Half-Life 2 | Level Design
This camera is good for when you join a map, it gives you a view of the map before you join a team

By: slackiller | Mar 04 2006

Making a camera , Part 2
Half-Life 2 | Level Design
these cameras are working monitors that turn on when a button is pushed.

By: slackiller | Mar 04 2006

Storing weapons on ladder
Half-Life 2 | Coding | Snippets
like Raven Sheild or BF2

By: British_Bomber | Dec 24 2005

Implementation of a string lookup table
Half-Life 2 | Coding | Snippets
A string lookup table is a set of functions that is used to convert strings to pre-defined values

By: deathz0rz | Nov 13 2005


Latest Comments
3 State Zoom For Any Weapon
Half-Life 2 | Coding | Server Side Tutorials
By: Ennuified | Oct 18 2007
 
Storing weapons on ladder
Half-Life 2 | Coding | Snippets
By: cct | Sep 07 2007
 
CTF Gameplay Part 1
Half-Life | Coding | Shared Tutorials
By: DarkNight | Aug 28 2007
 
CTF Gameplay Part 1
Half-Life | Coding | Shared Tutorials
By: deedok | Aug 20 2007
 
Debugging your half-life 2 mod
Half-Life 2 | Coding | Articles
By: blackshark | Aug 17 2007
 
How to add Ammocrates for missing ammo type and/or create new ammo types
Half-Life 2 | Coding | Server Side Tutorials
By: EoD | Aug 15 2007
 
GameUI
Half-Life 2 | Coding | Client Side Tutorials
By: G.I. Jimbo | May 19 2007
 
Reading and Writing Entire Structures
General | General Coding
By: monokrome | Jan 27 2007
 
VGUI Scope
Half-Life | Coding | Client Side Tutorials
By: Pongles | Jan 19 2007
 
Where do we go from here
General | News
By: SilentSounD | Jan 18 2007
 

Site Info
296 Approved Articless
3 Pending Articles
3940 Registered Members
0 People Online (7 guests)
About - Credits - Contact Us

Wavelength version: 3.0.0.9
Valid XHTML 1.0! Valid CSS!