Adding weapons: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(fixed hammer implementation)
No edit summary
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
This guide assumes that you started out with [[HL2:MP]] and [[Compiling under VS2005|VC++ Express Edition]] (available for free at microsoft.com).
{{disambig}}
 
'''Adding weapons''' may refer to:  
==Weapon models==
* [[Weapons]] - Adding weapons to maps
 
* [[Adding a new weapon to your mod]] - Creating custom weapons
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.
 
Once you have the models, they go into the following directory: <code>/name_of_your_mod/models/weapons</code>
 
==Weapon model textures==
 
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.
 
For this tutorial, we are going to add an [[wikipedia:AK-47|Avtomat Kalashnikov 1947]], also known as AK 47, in the game. The script file for it will be called <code>weapon_ak47.txt</code>.
 
<source lang=ini>
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-handed weapons
    AllowFlipping 1
    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
        }
    }
}
</source>
 
Place the file in the following directory: <code>/name_of_your_mod/scripts/</code>
 
;<code>"WeaponData"</code>
:This section configures the main aspects of the weapon.
;<code>"printname"</code>
:The name that the game prints for the weapon.
;<code>"viewmodel"</code>
:The model that the player sees when holding the weapon.
;<code>"playermodel"</code>
:The model that is seen when looking at another player or NPC holding the weapon. This model is also used when the weapon is on the ground ready to be picked up.
;<code>"animprefix"</code>
:This seems to be extremely outdated.  As of the Orange Box source code, the way this is used, is it looks up animation activities based on this "prefix".  However, the activities that are looked up only seem to be attack activities.  It changes it based on whether or not the player is crouching:
 
If they are:
"crouch_aim_" (animprefix)
otherwise:
"ref_aim_" (animprefix)
 
However, if it fails to find an animation with the animprefix, it defaults to the activity name bound to the action.  Thus, rendering the use of animprefix all but obsolete, considering that the crouch, and ref prefixes of animations are not currently in use.
 
;<code>"bucket"</code>
:Which bucket to place the weapon in for the HL2 style weapon selection.
;<code>"bucket_position"</code>
:Determines where in the bucket the weapon is placed. Lower numbers put the weapon higher, higher numbers put the weapon lower.
;<code>"clip"</code>
:The amount of ammunition in each clip or magazine.
;<code>"defaultclip"</code>
:The total amount of ammunition this weapon has when placed
;<code>"clip2_size"</code>
:The ammount of ammunition that can be stored in the secondary clip. If there is no secondary ammunition put "-1"
;<code>"default_clip2"</code>
:The total ammount of secondary ammunition this weapon has when placed. If there is no secondary ammunition put "-1"
;<code>"primary_ammo"</code>
:Identifier for the type of ammo this weapon uses.
;<code>"secondary_ammo"</code>
:Identifier for the secondary type of ammo this weapon uses. If it does not use any seconary ammo, put "None".
;<code>"weight"</code>
:The weight of the weapon, used for calculating physics when the weapon is on the ground.
;<code>"itemflags"</code>
:{{TODO|Find out and insert what this does and how to use it}}
;<code>"damage"</code>
:The ammount of damage this weapon does, per shot.
 
===Essential commands===
 
When creating your own weapon, replace <code>v_ak47.mdl</code> and <code>w_ak47.mdl</code> 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, <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>
 
In this tutorial, we will just use the AR2 sounds:
 
<source lang=ini>
"Weapon_ak47.Single"
{
channel CHAN_WEAPON
volume 0.8
soundlevel SNDLVL_GUNFIRE
pitch 85,95
 
rndwave
{
wave    weapons/ak47/ak47-1.wav
}
}
</source>
 
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>.
 
==Making your weapon show in Hammer==
 
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>.
 
Note however that there is no base class for weapon so copy an existing one and modify the parameters
 
Open it up in notepad or similar, and find the references to other (HL2) weapons (about line #3560). Add yours next to them, for example:
 
@PointClass base(Weapon) studio("models/weapons/w_ak47.mdl") = weapon_ak47 : "AK47" []
 
==Create C++ files==
 
It is necessary to create some c++ files because without them the source engine does not know how to handle the weapon. The c++ code determines how fast the player can fire, how accurate the weapon is and many other aspects of the weapon. Without the c++ code the weapons would cease to function.
 
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://web.archive.org/web/20061105031526/http://www.hl2coding.com/forums/viewtopic.php?t=1659](Broken Link). 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.
 
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>.
 
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.
 
For example:
 
STUB_WEAPON_CLASS( weapon_ar2, WeaponAR2, C_HLMachineGun );
 
Add the following underneath:
 
STUB_WEAPON_CLASS( weapon_m249, WeaponM4A1, C_HLMachineGun );
 
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:
 
def.AddAmmoType("MP5NAVY",DMG_BULLET,TRACER_LINE_AND_WHIZ,0,0,225,BULLET_IMPULSE(200,1225),0 );
 
Replace <code>MP5NAVY</code> 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 <code>:\yourmod\src\dlls\hl2mp_dll\hl2mp_player.cpp</code>. Change <code>void CHL2MP_Player::GiveDefaultItems( void )</code> (line 198).
 
{{note|To use your new weapon through the console (~), enable cheats in the game by typing "<code>sv_cheats 1</code>"  and then use the command "<code>give weapon_m249</code>" to grab it (replace weapon_m249 with your weapon's name)}}
 
[[Category:Tutorials]]
[[Category:Weapons programming]]

Latest revision as of 09:25, 15 March 2025

Icon-disambig.png
This is a disambiguation page, a list of pages that otherwise might share the same title.
If an article link referred you to this title, you might want to go back and fix it to point directly to the intended page.

Adding weapons may refer to: