Adding Muzzle Flashes that lights up the world

From Valve Developer Community
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.

Coding

Let’s start. We’ll 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:

float EV_HLDM_PlayTextureSound( int idx, pmtrace_t *ptr, float *vecSrc, float *vecEnd, int iBulletType )

Below that, add this:

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);
}

Then, find the function definition:

void EV_FireWeaponName(event_args_t *args) // replace WeaponName with your weapon’s name

Find this in that function:

EV_GetGunPosition( args, vecSrc, origin );

Just after that, add this:

EV_HLDM_MuzzleFlash( vecSrc, 1.0 + gEngfuncs.pfnRandomFloat( -0.2, 0.2 ) );

And, you’re done!

Credits and Reference

  • This is not entirely made by me. I modified some elements of the code. The original one is from VERC and TWHL.
  • Also, there’s something called elights which doesn’t light up the world but entities and models.
  1. https://twhl.info/wiki/page/VERC%3A_Adding_muzzle_flashes_that_also_light_up_world_brushes
  2. https://web.archive.org/web/20061027233737/http://collective.valve-erc.com/index.php?doc=1031137353-13157900