VERC: Hiding in Shadow Last edited 4 years ago2019-04-20 12:26:06 UTC

You are viewing an older revision of this wiki page. The current revision may be more detailed and up-to-date. Click here to see the current revision of this page.
In this article I will explain how to make a monster disregard the player if they're within a certain level of light, and how you can define this level of light from within the map.

Reading the Map Properties

First we need to get the data from the map properties and store it in a variable. Open dlls world.cpp and add the bold code just before the CWorld :: KeyValue, around line line 674.
int g_darklevel; // new code

//
// Just to ignore the "wad" field.
//
void CWorld :: KeyValue( KeyValueData *pkvd )
Then, just before the last else, add the bold code:
else if ( FStrEq(pkvd->szKeyName, "defaultteam") )
{
    if ( atoi(pkvd->szValue) )
    {
        pev->spawnflags |= SF_WORLD_FORCETEAM;
    }
    pkvd->fHandled = TRUE;
}
// start new code
else if ( FStrEq(pkvd->szKeyName, "darklevel") )
{
    g_darklevel = atoi(pkvd->szValue);
    pkvd->fHandled = TRUE;
}
// end new code
else
{
    CBaseEntity::KeyValue( pkvd );
}
Now we will need to declare the variable as a global, as we will need to read it from another part of the DLL for the next part. We could make our own .h, but it's easier to just use cbase.h as it's already included where we need the variable. Open dlls cbase.h and add the bold code in the very beginning:
extern int g_darklevel;

The Actual Check

Open dlls player.cpp, and as the first thing in the CBasePlayer::Classify, around line 1603, add the bold code:
//
// ID's player as such.
//
int CBasePlayer::Classify ( void )
{
    // start new code
    if (Illumination() <= g_darklevel)
    return CLASS_NONE;
    // end new code

    return CLASS_PLAYER;
}
This just compares the variable to the current illumination. If this checks out, it returns CLASS_NONE which monsters ignore; if the illumination is higher, it returns CLASS_PLAYER and monsters will attack. The illumination is a function that is already in the player.cpp. It simply takes the brightness of the light map under the player and adds a virtual muzzle flash light value to it (if the player fires a gun).

The brightness of the muzzle flashes can be changed accordingly in dlls weapons.h (Note that this doesn't change the actual muzzle flash). Also note that any values above 255 will simply add to the time of the effect.
#define BRIGHT_GUN_FLASH 512
#define NORMAL_GUN_FLASH 256
#define DIM_GUN_FLASH 128
Muzzle flash definitions for various weapons:
mp5grenade      BRIGHT_GUN_FLASH
python          BRIGHT_GUN_FLASH
rpg             BRIGHT_GUN_FLASH
mp5             NORMAL_GUN_FLASH

shotgun         NORMAL_GUN_FLASH
glock           NORMAL_GUN_FLASH

hornetgun       DIM_GUN_FLASH
The Gauss and Egon should also have an effect on the illumination, correct? Note that we can add any of the 3 muzzle flash definitions to the weapons, but for this article we will be using BRIGHT_GUN_FLASH. Now for the Egon, open dlls egon.cpp and go to line 289. Add the bold code after case FIRE_WIDE: but outside the #ifndef CLIENT_DLL, as the light check happens on the server side.
case FIRE_WIDE:

m_pPlayer->m_iWeaponFlash = BRIGHT_GUN_FLASH; // new code

#ifndef CLIENT_DLL
Then for the Gauss, open dlls gauss.cpp and add the muzzle flash next to the fire animation, like so.
    // player "shoot" animation
    m_pPlayer->SetAnimation( PLAYER_ATTACK1 );

    m_pPlayer->m_iWeaponFlash = BRIGHT_GUN_FLASH; // new code
}
Say, for example, we also need it to illuminate when the Gauss overcharges. Here we want the player to glow for the same amount of time that the effect lasts, so we use a fixed value for the muzzle flash. Add the bold code where that happens.
if ( m_pPlayer->m_flStartCharge < gpGlobals->time - 10 )
{
    // Player charged up too long. Zap him.
    m_pPlayer->m_iWeaponFlash = 768; // new code
However, the illumination doesn't include dynamic lights like the flashlight, so we will have to add this ourselves. Back in dlls player.cpp find CBasePlayer :: Illumination, which should be around line 4130. Add the bold code:
int CBasePlayer :: Illumination( void )
{
    int iIllum = CBaseEntity::Illumination( );

    // start new code
    if (!FlashlightIsOn() == false) iIllum += 200;
    // end new code

    iIllum += m_iWeaponFlash;
    if (iIllum > 255) return 255;
    return iIllum;
You could also add other conditions (such as crouching) in this way.

Developer Tools

Assuming you will want to debug maps that use this feature, you would probably want to add a developer command to test illumination levels around the map. To do this, we will have to add a CVAR. This is basically done the same way we added the global variable. Open dlls game.cpp and add the bold code right after the #include's :
#include "extdll.h"
#include "eiface.h"
#include "util.h"
#include "game.h"

cvar_t devlight = { "dev_light", "0" }; // new code
Then move down to the GameDLLInit and add the bold code
void GameDLLInit( void )
{
    // Register cvars here:

    CVAR_REGISTER (&devlight); // new code
Now open dlls game.h and right after extern void GameDLLInit( add the bold code.
extern void GameDLLInit( void );

extern cvar_t devlight; // new code

extern cvar_t displaysoundlist;
Then open player.cpp and find PreThink, around line line 1769. Add the bold code to the beginning of the function:
void CBasePlayer::PreThink(void)
{
    // start new code
    if (devlight.value != 0)
    ALERT ( at_console, "You are standing in %d light!\n",Illumination() );
    // end new code

    int buttonsChanged = (m_afButtonLast ^ pev->button); // These buttons have changed this frame
I use PreThink because it updates every frame, so we will get the light value real-time. To activate the light tester in-game, you have to be in developer mode, which you can activate by bringing down the console and type dev_light 1.

The FGD

Finally, we need to edit the FGD to include this new option. Open a Half-Life FGD in Wordpad, and search for worldspawn, then add the bold code in between the other options. Make sure the proper game configuration and FGD are selected when loading Valve Hammer Editor.
@SolidClass = worldspawn : "World entity"
[
    message(string) : "Map Description / Title"
    skyname(string) : "environment map (cl_skyname)"
    darklevel(integer) : "Hide shadow 0-255 (0=black)" : 15" // new line
    sounds(integer) : "CD track to play" : 1
Then, to set the needed light level in a map within Valve Hammer Editor, go to Map Properties and edit the value in the Hide shadow field. ("Map Properties" essentially accesses the worldspawn entity, in case you haven't noticed.)

Lastly, I would like to thank Teh_Freak for helping me with some of the functions.
This article was originally published on the Valve Editing Resource Collective (VERC).
TWHL only archives articles from defunct websites. For more information on TWHL's archiving efforts, please visit the TWHL Archiving Project page.

Comments

You must log in to post a comment. You can login or register a new account.