NightVision Tutorial
Adding Nightvision To your Source Single Player Mod
Here's something that is part of NightFall. And one such thing that many people have been stumped on - Night Vision.
Summary
Before I start, this is only one, and a very early attempt (It works), of one way to get the Night Vision Mode to work with your mod.
I would like to credit Gnolfo from http://www.hl2coding.com and user:ts2do
Seting up the ConVar, The Meat and The Spuds
Open the file src/cl_dll/view_scene.cpp, and append the code below to the end of this ifdef
//----------------------------------------------------------------------------- // Precache of necessary materials //----------------------------------------------------------------------------- #ifdef HL2_CLIENT_DLL
Found around line 170 (to go to a line number quickly, press ctrl+g)
//Precache Night Vision CLIENTEFFECT_MATERIAL( "HUDoverlays/nightvision" )
Now jump to the end of the page (ctrl+end) and add this class
//nightfall - amckern - amckern@yahoo.com //NightVision static void ScreenOver_f( void ) { IMaterial *pMaterial = materials->FindMaterial( "HUDoverlays/nightvision", TEXTURE_GROUP_OTHER, true ); //This is the texture we are going to use for the 'effect' - never use an ext on material files { static bool bDisplayed = false; if( bDisplayed ) { // turn it off view->SetScreenOverlayMaterial( NULL ); engine->ClientCmd( "mat_fullbright 0\n" );//turn full bright off again CLocalPlayerFilter filter; C_BaseEntity::EmitSound( filter, 0, "Nightfall.NightVisOff" ); //play the off sound } else { // turn it on view->SetScreenOverlayMaterial( pMaterial ); //this is the HUDoverlays/nightvision texture we made a pointer to above engine->ClientCmd( "mat_fullbright 1\n" );//light up the world CLocalPlayerFilter filter; C_BaseEntity::EmitSound( filter, 0, "Nightfall.NightVisOn" ); //On we go - play a sound to let the player know that the NV is on } bDisplayed = !bDisplayed; } } static ConCommand r_screenover( "r_screenover", ScreenOver_f );
So what we have done here is set a bool cvar (on/off) that either enables or disables the night vision effect. If you read the comments, you will understand what each part does.
The \n char found at the end of the engine->ClientCmd ("mat_fullbright 1\n"); tells the engine to press enter - otherwise the desired convar would not be actiavted.
The Sound
Nightfall.NightVisOn / Nightfall.NightVisOff
This code is calling on the script file nightfall_engine_sounds.txt that in turn is preached in game_sounds_manifest.txt like so
"precache_file" "scripts/nightfall_engine_sounds.txt"
I preached the sounds with preachescript("Nightfall.NightVisOn"); in the player.cpp preache function, just incase you are wondering why the game lags when you go to use the sound effect.
You can always use a direct sound event like Engine->ClientCmd( "play your_effect_sound.wav\n"); if you dont know how to make a scipt sound file. the downside of this is the game will always lag while it loads the sound file for the first time, and from what i understand, there is no way to preache a sound file direct with in the code.
The Light
Now all we need to do now is disable the cheat portion of mat_fullbright and we are done. If we dont do this, then we would have had to add a enigen->ClientCmd ("sv_cheats 1\n"); and a enigen->ClientCmd ("sv_cheats 0\n"); part to the code, that will make it very hacky, and very open to player exploits.
The first one is cl_dll/c_rope.cpp at line 78 - comment out the FCVAR_CHEAT potion so it looks like below
static ConVar mat_fullbright( "mat_fullbright", "0"/*, FCVAR_CHEAT*/ ); // get it from the engine
The second is at cl_dll/detailobjectsystem.cpp on line 81. Comment out the FCVAR_CHEAT again
static ConVar mat_fullbright( "mat_fullbright", "0"/*, FCVAR_CHEAT*/ ); // hook into engine's cvars..
Adam McKern --Amckern 19:25, 9 Nov 2005 (PST)