Adding New Ammotypes

From Valve Developer Community
Revision as of 13:22, 5 December 2009 by Danm36 (talk | contribs) (Added programming catagory)
Jump to navigation Jump to search

Introduction

This tutorial will teach you on how to create your own ammunition types for your weapons.

What you need:

  • A basic knowled of C++
  • Microsoft Visual C++ 2003/2005

Defining the Ammunition

First you will need to define the Ammunition in the Game Rules

To do this, first navigate to hl2mp_gamerules.cpp/hl2_gamerules.cpp in the server project, (Under Source Files/HL2 DLL) and navigate to the function CAmmoDef *GetAmmoDef().

In this function, the necessary ammo types are defined in the table like structure, beginning with def.AddAmmoTypes.

At the bottom of these defines, add a new row and add your own, following the same structure (Copy, paste and edit will work). As an example, the following line will be used:


def.AddAmmoType("SMG2",	DMG_BULLET, TRACER_LINE_AND_WHIZ, "sk_plr_dmg_smg2", "sk_npc_dmg_smg2", "sk_max_smg2", BULLET_IMPULSE(200, 1225), 0 );


With this line, a new ammo type 'SMG2' is defined.

Explanation of def.AddAmmoType:

def.AddAmmoType(
const char *name //The name of the new ammo type (For use in weapon script). e.g "SMG2"
int damageType //The damage type(s) this ammo will have. e.g "DMG_BULLET . For more than one damage type, use a | seperator ( DMG_BULLET | DMG_SNIPER )
int tracerType //The trace type to use. Possible of TRACER_NONE, TRACER_LINE and TRACER_LINE_AND_WIZZ
int plr_dmg //An skill (sk_) convar to allow for dynamic modification of the damage this ammo will do if fired from the player. Define all necessary sk_ variables in the ConVar listing around line 100, or type a number here. e.g. sk_plr_dmg_smg2
int npc_dmg //The same as above except the damage dealt when an NPC uses this ammo
int carry //The max ammount of ammo allowed. Can be a number or a sk_ convar. e.g. sk_max_smg2
float physicsForceImpulse //The force of the ammunition. Can be a number, calculation or you can use the function BULLET_IMPULSE(grain, ftpersec) to convert real world units to ingame units easily.
int nFlags //Specific flags. Possible being AMMO_FORCE_DROP_IF_CARRIED (If the player is hit, he drops whatever physics prop he was holding) and AMMO_INTERPRET_PLRDAMAGE_AS_DAMAGE_TO_PLAYER (Makes the plr_dmg varable damage to player instead of from). Both of these can be used using a | seperator
)

They are all you should need.

Adding an ammobox entity

Nun muss ein Item(Entity) erstellt werden, damit man die Munition (des neuen Munitionstyp) aufheben kann.


In der Datei Item_Ammo.cpp sind die Klassen sowie die Implementierungen festgelegt.

Dort wird das Item(Entity) auch definiert und implementiert.


Für unser Beispiel wird dazu der SMG1 Code kopiert und umbennant.

Nun fügt man alle Codes am besten vor der CItem_AmmoCrate Klasse ein.

Unsere Klassen heißen statt:

  • CItem_SmallBoxSMG1
  • CItem_BigBoxSMG1

Nun:

  • CItem_SmallBoxSMG2
  • CItem_BigBoxSMG2


Wenn alles eingebaut wurde muss man noch 2 Dinge ändern. Diese Änderungen betreffen beide CSMG2 Klassen in der bool MyTouch( CBasePlayer *pPlayer ) Methode.

Dort müssen wir in der Abfrage if (ITEM_GiveAmmo( pPlayer, SIZE_AMMO_SMG1_LARGE, "SMG1")) zwei Dinge ändern.

Und zwar müssen wir SIZE_AMMO_SMG1_ entweder neu definieren, für das SMG2, oder können direkt die Größe der aufnehmbaren Munition definieren.

Wenn man es selbst definieren will muss nur die jeweilige Zahl an Kugeln eingefügt werden, die man beim Berühren aufnehmen soll.


Falls ihr es aber definieren wollt, dann müsst ihr die items.h öffnen. Dort müsst ihr nur zwei neue Defines einbauen.

Das zweite ist nur der Name der von SMG1 zu SMG2(eurem Munitionstypen) geändert werden muss.


Nun müssen noch die Spawn und die Prechace Methoden geändert werden.

Dort muss man die Namen der Model(Mit Pfad) eingeben. Falls ihr die Models nich ändern wollt dann lasst diese Methoden.


Damit haben wir die Klassen und Methoden. Nun folgt der Code für das Entity selbst.


Dazu muss man mit den LINK_TO_ENTITY Funktionen den Klassennamen mit dem Namen des Entity verknüpfen.


Nun muss das Entity noch in die FGD der Mod eingebunden werden.

Dazu muss man die FGD öffnen und folgendes reinschreiben:

@PointClass base(Item) studio("models/items/boxmrounds.mdl")= item_ammo_smg2 : "Box of SMG2 ammo" []

Damit wird ein Entity vom Typ Item mit dem Model models/items/boxmrounds.mdl für den Hammer bereit gestellt.

Das Item heißt dann item_ammo_smg2 und enthält die Beschreibung Box of SMG2 ammo.

Munitionstyp in eine Waffe implementieren

Problem:

  • Valve hat die txt Dateien der Waffen verschlüsselt

Man muss sich also selbst eine Waffe anlegen.


Falls ihr schon eine eigene Waffe gecodet habt, dann müsst ihr die weapon_WaffenName.txt öffnen.

Falls nicht öffnet die weapon_smg1.txt


Dort muss man nur die Zeile

"primary_ammo"			"SMG1"

durch

"primary_ammo"			"SMG2"

ersetzen.