Projectile based Weapons

From Valve Developer Community
Revision as of 02:25, 16 June 2006 by Corner (talk | contribs)
Jump to navigation Jump to search

I have had several simple mod ideas that require projectile weapons like the crossbow or RPG, instead of the standard guns that hit or miss instantly. I will try to explore some of the possibilties in this article, all based on the crossbow code.

For now, the relevant code can all be found in the server side part of the crossbow code. This is found under hl -> source files -> HL2 DLL -> weapon_crossbow.cpp

Adding spread

Spread can be added rather quickly. The GetAutoAimVector used in other weapons appears to do no good here, so another approach is needed. I basically use the aiming vector converted to angles to change these angles, and then convert it back to the aiming vector. The relevant code is found in the CWeaponCrossbow::FireBolt method, the lines that need to be changed are:

594 - 595

	QAngle angAiming;
	VectorAngles( vecAiming, angAiming );

these can be exchanged with:

	QAngle angAiming;
	VectorAngles( vecAiming, angAiming );

	angAiming.x += ((rand() % 250) / 100.0) * (rand() % 2 == 1 ? -1 : 1);
	angAiming.y += ((rand() % 250) / 100.0) * (rand() % 2 == 1 ? -1 : 1);
		
	AngleVectors(angAiming, &vecAiming);

This changes the x and y angles with up to 2.5 degrees in either direction. This actually results in a square spread instead of a cone, but at least it works. If anybody knows of a better approach, I would be happy to hear about it.

Obeying gravity

Crossbow bolts do not obey the sv_gravity server variable, which can be changed with the console, or I believe even on a per map basis by the mapper. In order to make this work properly, find the CCrossbowBolt::Spawn method and edit line no 163 (unmodified file):

	SetGravity( 0.05f );

change it to:

	SetGravity( sv_gravity.GetFloat() / 600);

Since the original value was 0.05, and gravity usually is set to 600 or 800, this means that the new gravity value will be larger, and the bolt will fly in a more pronounced arc. You will have to tweak the value as you see fit, but the bolt should now obey any server side changes in gravity.

In order to make this compile you will need to add

#include "movevars_shared.h"

to the includes in the top of the file, otherwise the sv_gravity variable is not accessible.