Adding weapons: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(cleanup, let's hope I didn't break anything)
Line 1: Line 1:
{{cleanup}}
This guide assumes that you started out with [[HL2:MP]] and [[Compiling under VS2005|VC++ Express Edition]] (available for free at microsoft.com).


===Multiplayer===
==Weapon models==


***This guide assumes that you started out with HL2MP and VC++ Express Edition (It's FREE)***
Weapons consist of two models, each with their own prefix. A model named <code>v_yourweaponnamehere.mdl</code> is a [[viewmodel]], the model seen from the first person perspective. It normally consists of not only the weapon model but also the player's hands.


The other is the <code>w_yourweaponnamehere.mdl</code> &mdash; this is known as the [[worldmodel]]. These models are used for weapons laying on the ground and also for weapons that you can see in other characters' or players' hands.


For adding weapons to Source mods, you can make your own models or, for example, use the [[Counter-Strike:Source]] ones.


==The models==
Once you have the models, they go into the following directory: <code>/name_of_your_mod/models/weapons</code>
Weapons consist of 2 models, each with their own prefix. A model named v_yourweaponnamehere.mdl is a view model this is the model that is seen from the players perspective, this normally consists of not only the weapon model but also the player hands.
Next is the w_???.mdl; this is known as the world model, these models are used for weapons laying on the ground and also for weapons that you can see in other characters/players hands.


==Weapon model textures==


You can make your own or use the CSS one's.
The textures/skins for the weapons also need to be placed in the correct directory: <code>/name_of_your_mod/materials/models/weapons</code>


==Weapon script file: weapon_yourweaponname.txt==


Each weapon has its own script file, editable in any .txt program. The weapon script file has many different purposes, which include model file names, bucket info, clip size, ammo type, weight, sounds, icons and crosshairs. In adding weapons, for pistols you can start with the default <code>pistol.txt</code>, and for sub-machineguns and assault rifle you can use <code>smg1.txt</code> as a template.


Once you have made the relevant models, they go into the folder:
For this tutorial, we are going to add an [[wikipedia:Avtomat Kalashnikov 1947]], also known as AK 47, in the game. The script file for it will be called <code>weapon_ak47.txt</code>.
/nameofmodfolder/models/weapons/


==The Textures==
<pre>
The textures/skins for the weapons also need to be placed in the correct folder so once the textures are done, they need to be placed in:
WeaponData {
/nameofmodfolder/materials/models/weapons/
    // Weapon data is loaded by both the Game and Client DLLs.
    "printname" "#HL2_ak47"
    "viewmodel" "models/weapons/v_rif_ak47.mdl" // "models/weapons/v_ryourweapons.mdl"
    "playermodel" "models/weapons/w_rif_ak47.mdl"
    "anim_prefix" "ar2"
    "bucket" "2"
    "bucket_position" "1"
    "clip_size" "30"
    "default_clip" "90"
    "clip2_size" "-1"
    "default_clip2" "-1"
    "primary_ammo" "AK47"
    "secondary_ammo" "None"
    "weight" "5"
    "item_flags" "0"
    "BuiltRightHanded" "0" // These make sure you can have right-
    "AllowFlipping" "1"    // handed weapons
    "damage" "35"


    // Sounds for the weapon. There is a max of 16 sounds per category (i.e. max 16 "single_shot" sounds)
    SoundData {
        "special1" "Weapon_CombineGuard.Special1"
        "empty" "Weapon_IRifle.Empty"
        // "double_shot" "Weapon_IRifle.Single"
        "reload" "Weapon_AR2.Reload"
        "single_shot" "Weapon_ak47.Single"
       
        // NPC SECTION
        "single_shot_npc" "Weapon_AR2.NPC_Single"
        "reload_npc" "Weapon_AR2.NPC_Reload"
        "double_shot_npc" "Weapon_AR2.NPC_Double"
    }
   
    // Weapon Sprite data is loaded by the Client DLL.
    TextureData {
        "weapon" {
            "font" "WeaponIcons"
            "character" "l"
        }


==Scripts==
        "weapon_s" {   
weapon_yourweaponname.txt
            "font" "WeaponIconsSelected"
Each weapon has its own script file which can be edited in any .txt program, the weapon script file has many different purposes, which include:- model file names, bucket info, clip size, ammo type, weight, sounds, icons, crosshairs. For pistols start with pistol.txt, and for SMGs MGs, and assault rifle use smg1.txt as a template.
            "character" "l"
This is an example of the weapon_ak47.txt file:
        }


        "ammo" {
            "font" "WeaponIcons"
            "character" "u"
        }
       
        "ammo2" {
            "font" "WeaponIcons"
            "character" "z"
        }


        "crosshair" {
            "font" "Crosshairs"
            "character" "Q"
        }


//AK47
        "autoaim" {
WeaponData
            "file" "sprites/crosshairs"
{
            "x" "0"
:// Weapon data is loaded by both the Game and Client DLLs.
            "y" "48"
"printname": "#HL2_ak47"
            "width" "24"
:"viewmodel"
            "height" "24"
::: "models/weapons/v_rif_ak47.mdl" //"models/weapons/v_ryourweapons.mdl"
        }
:"playermodel"
    }
:::              "models/weapons/w_rif_ak47.mdl"
}
:"anim_prefix"
</pre>
:::"ar2"
:"bucket"
:::"2"
::"bucket_position"
::: "1"
:"clip_size" "30"
:"default_clip" "90"
:"clip2_size" "-1"
:"default_clip2" "-1"
:"primary_ammo" "AK47"
:"secondary_ammo" "None"
:"weight" "5"
:"item_flags" "0"
:"BuiltRightHanded" "0" //These makes sure you can have right-
:"AllowFlipping" "1"    //handed weapons
:"damage" "35"
:// Sounds for the weapon. There is a max of 16 sounds per category (i.e. max 16 "single_shot" sounds)
:SoundData
:{
::"special1" "Weapon_CombineGuard.Special1"
::"empty" "Weapon_IRifle.Empty"
:://"double_shot" "Weapon_IRifle.Single"
::"reload" "Weapon_AR2.Reload"
::"single_shot" "Weapon_ak47.Single"
::// NPC SECTION
::"single_shot_npc" "Weapon_AR2.NPC_Single"
::"reload_npc" "Weapon_AR2.NPC_Reload"
::"double_shot_npc" "Weapon_AR2.NPC_Double"
:}
:// Weapon Sprite data is loaded by the Client DLL.
:TextureData
:{
::"weapon"
::{
:::"font" "WeaponIcons"
:::"character" "l"
:}
:"weapon_s"
:{
::::"font" "WeaponIconsSelected"
::::"character" "l"
:}
:"ammo"
:{
:: "font" "WeaponIcons"
::"character" "u"
::}
::"ammo2"
::{
::"font" "WeaponIcons"
:::"character" "z"
::}
::"crosshair"
::{
:::"font" "Crosshairs"
:::"character" "Q"
::}
::"autoaim"
::{
:::"file" "sprites/crosshairs"
:::"x" "0"
:::"y" "48"
:::"width" "24"
:"height" "24"
::}
:}
}


Place the file in the following directory: <code>/name_of_your_mod/scripts/</code>


===Essential commands===


so create your weapon_nameofweapon.txt file in the same format as shown and save it in the following folder:
;<code>"printname"</code>
:{{todo|explain this}}


/nameofmodfolder/scripts/
;<code>"primary_ammo"</code>
:Identifier for the type of ammo this weapon uses.


When creating your own weapon, replace <code>v_ak47.mdl</code> and <code>w_ak47.mdl</code> with your model names.


"printname" "#HL2_Pistol" --Make this whatever you want
"special1" "Weapon_CombineGuard.Special1"
"empty" "Weapon_IRifle.Empty"
//"double_shot" "Weapon_IRifle.Single"
"reload" "Weapon_AR2.Reload"               
"single_shot" "Weapon_ak47.Single"


"primary_ammo" "AK47" - Name the ammo, you will create your ammo later in this guide
Change the above entries to correspond your own weapon name if you have custom sounds.


replace v_ak47.mdl and w_ak47.mdl with your model names
For example, <code>"reload" "Weapon_AR2.Reload"</code> becomes <code>"reload" "Weapon_AK47.Reload"</code>.


New sounds need to be defined in <code>:\Program Files\Valve\Steam\steamapps\SourceMods\yourmodname\scripts</code>


"special1" "Weapon_CombineGuard.Special1"
In this tutorial, we will just use the AR2 sounds:
"empty" "Weapon_IRifle.Empty"
//"double_shot" "Weapon_IRifle.Single"
"reload" "Weapon_AR2.Reload"               
"single_shot" "Weapon_ak47.Single"


<pre>
"Weapon_ak47.Single" {
    "channel" "CHAN_WEAPON"
    "volume" "0.8"
    "soundlevel" "SNDLVL_GUNFIRE"
    "pitch" "85,95"


change these to your weapon name if you have custom sounds
    "rndwave" {
        "wave"    "weapons/ak47/ak47-1.wav"
    }
}
</pre>


i.e. "reload" "Weapon_AR2.Reload" --> "reload" "Weapon_AK47.Reload"
where <code>weapons/ak47/ak47-1.wav</code> is a .wav file (hence "wave") and will have to be located in <code>:\Program Files\Valve\Steam\steamapps\SourceMods\name_of_your_mod\sound\weapons\ak47</code>.


If you change your name there then you will need to define the new sound in:
==Making your weapon show in Hammer - (Not Tested)==


C:\Program Files\Valve\Steam\steamapps\SourceMods\yourmodname\scripts
To get your weapon to show in Hammer you need to add the entry to your .fgd file, which should be saved in <code>\sourcesdk\bin\</code>.


I would just take the AR2 sounds in that file and copy like this:
Open it up in notepad or similar, and find the references to other (HL2) weapons. Add yours next to them, for example:


 
  @PointClass base(Weapon) studio("models/weapons/w_ak47.mdl") = weapon_ak47 : "AK47" []
 
 
 
  "Weapon_ak47.Single"
:{
: "channel" "CHAN_WEAPON"
: "volume" "0.8"
: "soundlevel" "SNDLVL_GUNFIRE"
: "pitch" "85,95"
:
: "rndwave"
: {
: "wave" "weapons/ak47/ak47-1.wav"
: }
:}
 
where
 
"wave" "weapons/ak47/ak47-1.wav"
 
is a .wav file (hence "wave") and it will look in C:\Program Files\Valve\Steam\steamapps\SourceMods\yourmod\sound\weapons\ak47
 
 
 
 
 
 
 
 
 
Making your weapon show in Hammer -- Not Tested sorry
To get your weapon to show in hammer you need to add the entry to your .fgd file, which should be saved in sourcesdk/bin/
 
Open it up in notepad or similar, and find where the other (HL2) weapons are referenced. Add yours with them, here is an example:
@PointClass base(Weapon) studio("models/weapons/w_ak47.mdl") = weapon_ak47 : "AK47" []


==Create C++ files==
==Create C++ files==


Go to File -> New -> File...
Navigate to <code>File</code> -> <code>New</code> -> <code>File</code>, click <code>Visual C++</code> on the left and select <code>.cpp</code>. Pick the first template from [http://www.hl2coding.com/forums/viewtopic.php?t=1659]. Follow the instructions there... Be careful not to copy the second template. Save as <code>weapon_yourweapon.cpp</code> and add it to the solution explorer.
 
Click Visual C++ on the left and select .cpp
 
 
 
Pick the first template from http://www.hl2coding.com/forums/viewtopic.php?t=1659
 
Follow the instructions there... be careful not to copy the second template.
 
Save as weapon_yourweapon.cpp and add it to the solution explorer:


Save the code files where ever you want, I would recommend your C:/modname/src folder. When you  
Save the code files where ever you want, <code>:/modname/src</code> is recommended. When you expand under <code>Server</code> -> <code>Source Files</code> -> <code>HL2MP</code> -> <code>Weapons</code>, right-click on <code>Weapons</code> then <code>Add</code> -> <code>Existing Item</code> and find your .cpp file. Do the same for <code>Client</code> -> <code>Sources Files</code> -> <code>HL2MP</code> -> <code>Weapons</code>.


expand under Server -> Sources Files -> HL2MP -> Weapons, right-click on Weapons then Add -> Existing Item and find your .cpp file
Open <code>:\yourmod\src\cl_dll\hl2_hud\c_weapon__stubs_hl2.cpp</code>. Add your weapon under <code>#ifndef HL2MP</code> (line 27). Just copy and paste one of the lines renaming the relevant information.


Do the same for Client -> Sources Files -> HL2MP -> Weapons
For example:
 
==Open:==
 
C:\yourmod\src\cl_dll\hl2_hud\c_weapon__stubs_hl2.cpp
 
add your gun under #ifndef HL2MP (line 27)
 
just copy and paste one of the lines renaming the relevant information
 
Example:


  STUB_WEAPON_CLASS( weapon_ar2, WeaponAR2, C_HLMachineGun );
  STUB_WEAPON_CLASS( weapon_ar2, WeaponAR2, C_HLMachineGun );


added this under it:
Add the following underneath:


  STUB_WEAPON_CLASS( weapon_m249, WeaponM4A1, C_HLMachineGun );
  STUB_WEAPON_CLASS( weapon_m249, WeaponM4A1, C_HLMachineGun );


==Open:==
Open <code>:\yourmod\src\game_shared\hl2mp\hl2mp_gamerules.cpp</code>. Find <code>CAmmoDef *GetAmmoDef()</code> (line 910). Copy and paste the line closest to your weapon type, for example:
C:\yourmod\src\game_shared\hl2mp\hl2mp_gamerules.cpp
 
Find CAmmoDef *GetAmmoDef()   (Line 910)
 
Copy and paste the line that is closest to your weapon type, I chose
 


  def.AddAmmoType("MP5NAVY",DMG_BULLET,TRACER_LINE_AND_WHIZ,0,0,225,BULLET_IMPULSE(200,1225),0 );
  def.AddAmmoType("MP5NAVY",DMG_BULLET,TRACER_LINE_AND_WHIZ,0,0,225,BULLET_IMPULSE(200,1225),0 );


Replace MP5NAVY with the ammo name in your script file (weapon_???.txt)
Replace <code>MP5NAVY</code> with the ammo name defined in your weapon script file.
 
 
 
==Setup defaults weapons==
 
You can set it so you start with the guns by editing:
 
c:\yourmod\src\dlls\hl2mp_dll\hl2mp_player.cpp
 
 
void CHL2MP_Player::GiveDefaultItems( void )      Line 198
 
 
 
 
 
 
 
 
 
 
 


==Setup default weapons==


I probably missed some stuff, post in discussion if something is left out.
You can make it so that you start with the weapon by editing <code>:\yourmod\src\dlls\hl2mp_dll\hl2mp_player.cpp</code>. Change <code>void CHL2MP_Player::GiveDefaultItems( void )</code> (line 198).

Revision as of 13:35, 24 October 2007

This guide assumes that you started out with HL2:MP and VC++ Express Edition (available for free at microsoft.com).

Weapon models

Weapons consist of two models, each with their own prefix. A model named v_yourweaponnamehere.mdl is a viewmodel, the model seen from the first person perspective. It normally consists of not only the weapon model but also the player's hands.

The other is the w_yourweaponnamehere.mdl — this is known as the worldmodel. These models are used for weapons laying on the ground and also for weapons that you can see in other characters' or players' hands.

For adding weapons to Source mods, you can make your own models or, for example, use the Counter-Strike:Source ones.

Once you have the models, they go into the following directory: /name_of_your_mod/models/weapons

Weapon model textures

The textures/skins for the weapons also need to be placed in the correct directory: /name_of_your_mod/materials/models/weapons

Weapon script file: weapon_yourweaponname.txt

Each weapon has its own script file, editable in any .txt program. The weapon script file has many different purposes, which include model file names, bucket info, clip size, ammo type, weight, sounds, icons and crosshairs. In adding weapons, for pistols you can start with the default pistol.txt, and for sub-machineguns and assault rifle you can use smg1.txt as a template.

For this tutorial, we are going to add an wikipedia:Avtomat Kalashnikov 1947, also known as AK 47, in the game. The script file for it will be called weapon_ak47.txt.

 WeaponData {
     // Weapon data is loaded by both the Game and Client DLLs.
     "printname" "#HL2_ak47"
     "viewmodel" "models/weapons/v_rif_ak47.mdl" // "models/weapons/v_ryourweapons.mdl"
     "playermodel" "models/weapons/w_rif_ak47.mdl"
     "anim_prefix" "ar2"
     "bucket" "2"
     "bucket_position" "1"
     "clip_size" "30"
     "default_clip" "90"
     "clip2_size" "-1"
     "default_clip2" "-1"
     "primary_ammo" "AK47"
     "secondary_ammo" "None" 
     "weight" "5"
     "item_flags" "0"
     "BuiltRightHanded" "0" // These make sure you can have right-
     "AllowFlipping" "1"    // handed weapons
     "damage" "35"

     // Sounds for the weapon. There is a max of 16 sounds per category (i.e. max 16 "single_shot" sounds)
     SoundData {
         "special1" "Weapon_CombineGuard.Special1"
         "empty" "Weapon_IRifle.Empty" 
         // "double_shot" "Weapon_IRifle.Single"
         "reload" "Weapon_AR2.Reload"
         "single_shot" "Weapon_ak47.Single"
        
         // NPC SECTION
         "single_shot_npc" "Weapon_AR2.NPC_Single"
         "reload_npc" "Weapon_AR2.NPC_Reload"
         "double_shot_npc" "Weapon_AR2.NPC_Double"
     }
    
     // Weapon Sprite data is loaded by the Client DLL.
     TextureData {
         "weapon" {
             "font" "WeaponIcons"
             "character" "l"
         }

         "weapon_s" {    
             "font" "WeaponIconsSelected"
             "character" "l"
         }

         "ammo" {
             "font" "WeaponIcons"
             "character" "u"
         }
        
         "ammo2" {
             "font" "WeaponIcons"
             "character" "z"
         }

         "crosshair" {
             "font" "Crosshairs"
             "character" "Q"
         }

         "autoaim" {
             "file" "sprites/crosshairs"
             "x" "0"
             "y" "48"
             "width" "24"
             "height" "24"
         }
     }
 }

Place the file in the following directory: /name_of_your_mod/scripts/

Essential commands

"printname"
Todo: explain this
"primary_ammo"
Identifier for the type of ammo this weapon uses.

When creating your own weapon, replace v_ak47.mdl and w_ak47.mdl with your model names.

"special1" "Weapon_CombineGuard.Special1" 
"empty" "Weapon_IRifle.Empty" 
//"double_shot" "Weapon_IRifle.Single" 
"reload" "Weapon_AR2.Reload"                 
"single_shot" "Weapon_ak47.Single"

Change the above entries to correspond your own weapon name if you have custom sounds.

For example, "reload" "Weapon_AR2.Reload" becomes "reload" "Weapon_AK47.Reload".

New sounds need to be defined in :\Program Files\Valve\Steam\steamapps\SourceMods\yourmodname\scripts

In this tutorial, we will just use the AR2 sounds:

 "Weapon_ak47.Single" {
     "channel" "CHAN_WEAPON"
     "volume" "0.8"
     "soundlevel" "SNDLVL_GUNFIRE"
     "pitch" "85,95"

     "rndwave" {
         "wave"    "weapons/ak47/ak47-1.wav" 
     }
 }

where weapons/ak47/ak47-1.wav is a .wav file (hence "wave") and will have to be located in :\Program Files\Valve\Steam\steamapps\SourceMods\name_of_your_mod\sound\weapons\ak47.

Making your weapon show in Hammer - (Not Tested)

To get your weapon to show in Hammer you need to add the entry to your .fgd file, which should be saved in \sourcesdk\bin\.

Open it up in notepad or similar, and find the references to other (HL2) weapons. Add yours next to them, for example:

@PointClass base(Weapon) studio("models/weapons/w_ak47.mdl") = weapon_ak47 : "AK47" []

Create C++ files

Navigate to File -> New -> File, click Visual C++ on the left and select .cpp. Pick the first template from [1]. Follow the instructions there... Be careful not to copy the second template. Save as weapon_yourweapon.cpp and add it to the solution explorer.

Save the code files where ever you want, :/modname/src is recommended. When you expand under Server -> Source Files -> HL2MP -> Weapons, right-click on Weapons then Add -> Existing Item and find your .cpp file. Do the same for Client -> Sources Files -> HL2MP -> Weapons.

Open :\yourmod\src\cl_dll\hl2_hud\c_weapon__stubs_hl2.cpp. Add your weapon under #ifndef HL2MP (line 27). Just copy and paste one of the lines renaming the relevant information.

For example:

STUB_WEAPON_CLASS( weapon_ar2, WeaponAR2, C_HLMachineGun );

Add the following underneath:

STUB_WEAPON_CLASS( weapon_m249, WeaponM4A1, C_HLMachineGun );

Open :\yourmod\src\game_shared\hl2mp\hl2mp_gamerules.cpp. Find CAmmoDef *GetAmmoDef() (line 910). Copy and paste the line closest to your weapon type, for example:

def.AddAmmoType("MP5NAVY",DMG_BULLET,TRACER_LINE_AND_WHIZ,0,0,225,BULLET_IMPULSE(200,1225),0 );

Replace MP5NAVY with the ammo name defined in your weapon script file.

Setup default weapons

You can make it so that you start with the weapon by editing :\yourmod\src\dlls\hl2mp_dll\hl2mp_player.cpp. Change void CHL2MP_Player::GiveDefaultItems( void ) (line 198).