Post tutorial Report RSS How to create custom items and item_speed

Hello Team. I've been part of this community for quite some time and I got a lot of knowledge from you. So it's only natural to share some of my own as well. Below you can find explanation on how to add new items in Half-Life 1, plus 1 item i have coded for my mod FlatLine Arena Below I will outline the steps needed to code a generic item and then I'll give the code for my "Extra Speed" item for my mod "FlatLine Arena" (well most of it anyway)

Posted by on - Intermediate Server Side Coding

Hello Team. I've been part of this community for quite some time and I got a lot of knowledge from you.

So it's only natural to share some of my own as well. Below you can find explanation on how to add new items in Half-Life 1, plus 1 item i have coded for my mod FlatLine Arena.

Below I will outline the steps needed to code a generic item and then I'll give the code for my "Extra Speed" item for my mod "FlatLine Arena" (well most of it anyway; i have extra options, messages, effects and sounds - but let's keep it clear and simple).

I will try to keep it simple, but at the end of this you should be able to create your own custom items and you will have a working item that upon acquisition and pressing the "+speedpack" key will allow you to move a lot faster in all directions (including swimming and flying, assuming you have flight in your mod).

So what is an item? - Well that's easy it's a pick up that you can activate to use or a pickup that gives you a boost of stats when obtained.

Examples? Really - longjump module, battery, healthkit etc.

So what do you need to create custom items:

1. Code that will be the effect for the item, let's say you have an awesome code that can make you player fly, invisible, or give him extra HP, armor or DMG (in my example I will give the player more speed). :)

2. In "items.cpp" you need to define your new item routines.

3. In "weapons.h", look for "#define your item (make sure to pick unique number sine you will get errors if there is a conflict)

4. In "weapons.ccp" you need to precache your new item, or the game will crash the time you try to span/obtain your item, and you don't what that right?
(well you can create item that crash the game if that's the objective... for example when a player uses impulse 101, but I prefer different medicine.. like spawning a Gargantua or a black hole when using 101)

5. You can add the item to the impulse command in "player.cpp"; look for "case 101:"

6. Create an entry in the fgd file for mappers to place in maps.

7. That's pretty much everything you need to do in order to create a new item. Compile and profit

Bonus: You can add extra effects, sounds etc; you can make the item to take HEV battery etc. it's up to you. There are endless possibilities.

That said the code part starts here. The tutorial is pretty much copy-paste, but I hope you'll learn something from it.

Example of the speed cvar :)

item speed01


Screenshot of the actual item code

item speed01

Here's a video of the item in action:

We'll work only on the server side of the SDK code.

================================
Extra Speed Item :) Faster than a speeding bullet!
================================

1. This needs to be added in "player.h" in the public section of "class CBasePlayer : public CBaseMonster"
below this:

char m_szTeamName[TEAM_NAME_LENGTH];

add this:


// Napoleon was here
void ExtraSpeed( ); // Faster than a speeding bullet!
BOOL m_fExtraSpeed; // Yeah I do have the item...
BOOL m_fExtraSpeedKeyDown; // Yeah I see you're pressing the speed key
// End Speed code

2. This need to be added in : "player.cpp"
Note, normally I add those right above the routines for DeadHev; still anywhere in player.cpp will do.


//=========================================================
// ExtraSpeed routines (used for item)
//=========================================================
// Napoleon was here
// Entry for the extra speed item
void CBasePlayer::ExtraSpeed( )

{
if(!m_fExtraSpeed) //if we don't have extraspeed return
return;

if(m_fExtraSpeedKeyDown) // are we pressing the speed key?
{
if ( !FBitSet(pev->flags, FL_ONGROUND) ) // are we on the ground?
{
pev->movetype = MOVETYPE_WALK; // move!

return;
}

if( pev->button & IN_FORWARD ) // are we moving forward?
{
pev->movetype = MOVETYPE_FLY; // hack!
UTIL_MakeVectors( pev->v_angle + pev->punchangle );
Vector New_Position = pev->origin + gpGlobals->v_forward * 500; // how far forward?

pev->velocity = New_Position- pev->origin;

return;
}

if( pev->button & IN_BACK ) // are we moving back?
{
pev->movetype = MOVETYPE_FLY; // hack!
UTIL_MakeVectors( pev->v_angle + pev->punchangle );
Vector New_Position = pev->origin - gpGlobals->v_forward * 500; // how far back?
pev->velocity = New_Position- pev->origin;

return;

}

if( pev->button & IN_MOVERIGHT ) // are we moving right?
{
pev->movetype = MOVETYPE_FLY; // hack!
UTIL_MakeVectors( pev->v_angle + pev->punchangle );
Vector New_Position = pev->origin + gpGlobals->v_right * 500; // how far right?
pev->velocity = New_Position- pev->origin;

return;
}

if( pev->button & IN_MOVELEFT ) // are we moving left?
{
pev->movetype = MOVETYPE_FLY; // hack!
UTIL_MakeVectors( pev->v_angle + pev->punchangle );
Vector New_Position = pev->origin - gpGlobals->v_right * 500; // how far left?
pev->velocity = New_Position- pev->origin;

return;
}
}
}

// End Extra speed

Note you can experiment with the speed, I find it best at 500.

3. Now go to void CBasePlayer::PreThink(void) (again in player.cpp); and add the below code between:

UTIL_MakeVectors(pev->v_angle); // is this still used?

and

ItemPreFrame( );

add this:


// Napoleon was here
// extra speed entry :P
ExtraSpeed(); // Faster than a speeding bullet!
// End speed entry

Now why did I added this here? To get the player speeding when the "+speedpack" button is pressed. "+speedpack" button? Yeah, we'll define it in a second. Still before that...

4. One last thing to do in player.cpp we want to make sure they don't keep the speedpack when they die.

look for:
// this client isn't going to be thinking for a while, so reset the sound until they respawn


and above it add:


// Napoleon was here
// fatser than a speeding bullet
m_fExtraSpeed = FALSE; // you died so no speed bonus for you when respawning (unless you get the item again)
// end speed

5. Now on the topic of the speed button... OK now open client.cpp so we can add the "+speedpack" command. search for

GetClassPtr((CBasePlayer *)pev)->SelectLastItem();

and below it add this:


// Napoleon was here
// faster than a speeding bullet
// you need to bind "+speedpack" to a key for this to work
else if (FStrEq(pcmd, "+speedpack" ))
{
//We are holding the key set this.
GetClassPtr((CBasePlayer *)pev)->m_fExtraSpeedKeyDown = TRUE;
}
// you don't need to bind this
else if (FStrEq(pcmd, "-speedpack" ))
{
//We are not holding the key un-set this.
GetClassPtr((CBasePlayer *)pev)->m_fExtraSpeedKeyDown = FALSE;
}
// end speeding bullet

6. OK, now we have the awesome move code and we have the command for the speedpack activation, yet we're missing the item. Let's create it.

This needs to be added in "items.cpp" (at the very bottom)


// Napoleon was here
// ExtraSpeed item code goes below :)
class CItemExtraSpeed : public CItem
{
void Spawn( void )
{
Precache( );
//the mdl we see on the ground; make sure o precache it... or the game will crash
SET_MODEL(ENT(pev), "models/w_longjump.mdl"); // feel free to change the model
CItem::Spawn( );
}
void Precache( void )
{
//precache it here, else the game crash
PRECACHE_MODEL ("models/w_longjump.mdl"); // feel free to change the model
}
BOOL MyTouch( CBasePlayer *pPlayer )
{
if ( pPlayer->m_fExtraSpeed )
{
return FALSE; //don't let us pick up a pack if we have one; you don't need to be that fast...
}

pPlayer->m_fExtraSpeed = TRUE;// player now has extra speed

// Go tell the world the good news (well tell the player)
MESSAGE_BEGIN( MSG_ONE, gmsgItemPickup, NULL, pPlayer->pev );
WRITE_STRING( STRING(pev->classname) ); //i dunno why but this should be here
MESSAGE_END();

return TRUE;
}
};

//bind the class to entity
LINK_ENTITY_TO_CLASS( item_speed, CItemExtraSpeed ); // we use this for placement on maps

// End Extra speed

7. So now we're almost done. Few more touching moves.

We need to precahe the item, to include it in impulse 101 (the first one is mandatory, else expect crashes, the second one is to get the extra speed item)
we need to #define item_speed in "weapons.h". Don't forget to define that the player starts with no super speed :P

go to "weapons.h", look for

#define ITEM_BATTERY 4

and below that add the following:


// Napoleon was here
#define ITEM_SPEED 5 // entry for the speed item
// End speed item

Note: Make sure to place the appropriate number for the item (I'm assuming you have no extra items defined, if you do you need to change the value).

go to "weapons.ccp", in "void W_Precache(void)", look for

// common world objects

and below the entry for "item_longjump" add your item:


// Napoleon was here
// if you don't precache it here the game will crash when you try to get the item
UTIL_PrecacheOther( "item_speed" ); // Faster than a speeding bullet
// end speed item


now you'd like to add the new item to the impulse 101 cheat right? (admit it... you love cheating!)

go back to "player.cpp"; look for "case 101:" and "GiveNamedItem( "item_longjump" );" ad add the following below


// Napoleon was here
// and you do like cheating
GiveNamedItem( "item_speed" ); // faster than a speeding bullet
// end speed item

Again in "player.cpp" look for " " and below it add


// Napoleon was here
m_fExtraSpeed = FALSE; // no speedpack on spawn
// end extraspeed


8. Go and compile the server and the client dlls and try our new speed item.

(sv_cheats 1; restart; impule 101 or give item_speed ); Don't forget you need to bind a key to "+speedpack" to get the boost of speed when you have the item and you press the button :)

9. Now about the fgd file

Open the file with notepad++ and add this at the bottom of the file


@PointClass size(-16 -16 0, 16 16 36) base(Weapon, Targetx) studio("models/w_longjump.mdl") = item_speed : "SpeedPack Module" []

Change the mode if you added different model.

10. For extra points you can also add:
- announcer sounds when picked up
- sprites for extra effects and when picked up (for that you need knowledge on how to send messages)
- you can make it to use HEV battery and to make sound when used

The possibilities are endless. Still I'll leave that for you.

Feel free to use in your mod and give me a feedback if you like it.
If you use my code, please give credit when credit is due. :)

Have fun and happy coding. :)

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: