Projectile based Weapons: Difference between revisions
mNo edit summary |
|||
Line 74: | Line 74: | ||
<pre> | <pre> | ||
for (int i = -10; i < 11; i += 2) { | for (int i = -10; i < 11; i += 2) | ||
{ | |||
Vector vecAiming = pOwner->GetAutoaimVector( 0 ); | Vector vecAiming = pOwner->GetAutoaimVector( 0 ); | ||
Vector vecSrc = pOwner->Weapon_ShootPosition(); | Vector vecSrc = pOwner->Weapon_ShootPosition(); |
Revision as of 13:28, 16 June 2006
Introduction
This article explains some of the possibilities for projectile weapons like the crossbow or RPG, instead of the standard guns that hit or miss instantly. All tweaks in this article are based on the crossbow code. 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 in the solution explorer
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.
In order to simplify debugging, you may want to change line 608
m_iClip1--;"
to
//m_iClip1--;"
and line 640
m_flNextPrimaryAttack = m_flNextSecondaryAttack = gpGlobals->curtime + 0.75;
to
m_flNextPrimaryAttack = m_flNextSecondaryAttack = gpGlobals->curtime + 0.15;
This gives you unlimited ammo and a quicker "reload" time.
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.
Firing multiple bolts (not working yet)
In order to create a shotgun-like effect, it may be desirable to fire multiple randomized bolts at once. I have tried it with the following loop:
for (int i = -10; i < 11; i += 2) { Vector vecAiming = pOwner->GetAutoaimVector( 0 ); Vector vecSrc = pOwner->Weapon_ShootPosition(); QAngle angAiming; VectorAngles( vecAiming, angAiming ); angAiming.x += ((rand() % 250) / 100.0) * (rand() % 2 == 1 ? -1 : 1); vecSrc.x += i; angAiming.y += ((rand() % 250) / 100.0) * (rand() % 2 == 1 ? -1 : 1); vecSrc.y += i; /*angAiming.z += ((rand() % 350) / 100.0) * (rand() % 2 == 1 ? -1 : 1); vecSrc.z += i;*/ AngleVectors(angAiming, &vecAiming); CCrossbowBolt *pBolt = CCrossbowBolt::BoltCreate( vecSrc, angAiming, pOwner ); if ( pOwner->GetWaterLevel() == 3 ) { pBolt->SetAbsVelocity(vecAiming * BOLT_WATER_VELOCITY ); } else { pBolt->SetAbsVelocity(vecAiming * BOLT_AIR_VELOCITY ); } }
Changing the vecSrc.x and vecSrc.y based on i results in a neat line of crossbow bolts in the instant that the trigger is pressed. However, just a moment later all but the last (or first?) in the line disappear, and only the remaining bolt continues on its trajectory, and it is only this bolt that glows. Changing the the vecSrc.x and vecSrc.y based on random numbers creates a more randomized spawn position for each bolt, and when moving around the crossbow actually shoots 3-5 bolts, but still not all 10 of them, and not all of the time. For now, the reason for this behavior eludes me.