Adding Muzzle Flashes that lights up the world

From Valve Developer Community
Revision as of 06:32, 18 October 2024 by Rixzzy (talk | contribs) (Made a minor edit.)
Jump to navigation Jump to search

In this tutorial, I will show you how to add dlights to a Half Life 1 Mod (GoldSrc Mod). This tutorial will create a muzzle flash dlight that lights up the world around the player dynamically.

Let’s start. Well navigate to ev_hldm.h and scroll to the bottom. After that, add this:

void EV_HLDM_MuzzleFlash( vec3_t pos, float amount );

Then, we will open ev_hldm.cpp. Find:

// play a strike sound based on the texture that was hit by the attack traceline. VecSrc/VecEnd are the
// original traceline endpoints used by the attacker, iBulletType is the type of bullet that hit the texture.
// returns volume of strike instrument (crowbar) to play
float EV_HLDM_PlayTextureSound( int idx, pmtrace_t *ptr, float *vecSrc, float *vecEnd, int iBulletType )

Below that, add this: <source lang=cpp> void EV_HLDM_MuzzleFlash(vec3_t pos, float amount) {

   // make a dlight first
   dlight_t *dl = gEngfuncs.pEfxAPI->CL_AllocDlight(0);
   // Original color values
   int originalR = 231;
   int originalG = 219;
   int originalB = 14;
   // Randomize color components within the range of +/- 20
   dl->color.r = originalR + gEngfuncs.pfnRandomLong(-20, 20);
   dl->color.g = originalG + gEngfuncs.pfnRandomLong(-20, 20);
   dl->color.b = originalB + gEngfuncs.pfnRandomLong(0, 0);
   // Randomize the die value by +/- 0.01
   dl->die = gEngfuncs.GetClientTime() + 0.05 + gEngfuncs.pfnRandomFloat(-0.01, 0.01);
   // Randomize the radius based on amount
   dl->radius = gEngfuncs.pfnRandomFloat(245.0f, 256.0f);
   // Randomize the decay value
   dl->decay = gEngfuncs.pfnRandomFloat(400.0f, 600.0f);

}