2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-11-25 19:29:13 +01:00
hlsdk-xash3d/dlls/lights.cpp

197 lines
4.4 KiB
C++
Raw Normal View History

2016-06-04 15:24:23 +02:00
/***
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
/*
===== lights.cpp ========================================================
spawn and think functions for editor-placed lights
*/
#include "extdll.h"
#include "util.h"
#include "cbase.h"
class CLight : public CPointEntity
{
public:
2016-07-31 15:48:50 +02:00
virtual void KeyValue( KeyValueData* pkvd );
virtual void Spawn( void );
void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
static TYPEDESCRIPTION m_SaveData[];
2016-06-04 15:24:23 +02:00
private:
2016-07-31 15:48:50 +02:00
int m_iStyle;
string_t m_iszPattern;
2016-06-04 15:24:23 +02:00
};
LINK_ENTITY_TO_CLASS( light, CLight )
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
TYPEDESCRIPTION CLight::m_SaveData[] =
2016-06-04 15:24:23 +02:00
{
DEFINE_FIELD( CLight, m_iStyle, FIELD_INTEGER ),
DEFINE_FIELD( CLight, m_iszPattern, FIELD_STRING ),
};
IMPLEMENT_SAVERESTORE( CLight, CPointEntity )
2016-06-04 15:24:23 +02:00
//
// Cache user-entity-field values until spawn is called.
//
2016-07-31 15:48:50 +02:00
void CLight::KeyValue( KeyValueData* pkvd )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( FStrEq(pkvd->szKeyName, "style" ) )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
m_iStyle = atoi( pkvd->szValue );
2016-06-04 15:24:23 +02:00
pkvd->fHandled = TRUE;
}
2016-07-31 15:48:50 +02:00
else if( FStrEq(pkvd->szKeyName, "pitch" ) )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
pev->angles.x = atof( pkvd->szValue );
2016-06-04 15:24:23 +02:00
pkvd->fHandled = TRUE;
}
2016-07-31 15:48:50 +02:00
else if( FStrEq(pkvd->szKeyName, "pattern" ) )
2016-06-04 15:24:23 +02:00
{
m_iszPattern = ALLOC_STRING( pkvd->szValue );
pkvd->fHandled = TRUE;
}
else
{
CPointEntity::KeyValue( pkvd );
}
}
/*QUAKED light (0 1 0) (-8 -8 -8) (8 8 8) LIGHT_START_OFF
Non-displayed light.
Default light value is 300
Default style is 0
If targeted, it will toggle between on or off.
*/
2016-07-31 15:48:50 +02:00
void CLight::Spawn( void )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( FStringNull( pev->targetname ) )
{
// inert light
REMOVE_ENTITY(ENT( pev ) );
2016-06-04 15:24:23 +02:00
return;
}
2016-07-31 15:48:50 +02:00
if( m_iStyle >= 32 )
2016-06-04 15:24:23 +02:00
{
//CHANGE_METHOD(ENT(pev), em_use, light_use);
2016-07-31 15:48:50 +02:00
if( FBitSet( pev->spawnflags, SF_LIGHT_START_OFF ) )
LIGHT_STYLE( m_iStyle, "a" );
else if( m_iszPattern )
2017-06-29 15:56:03 +02:00
LIGHT_STYLE( m_iStyle, STRING( m_iszPattern ) );
2016-06-04 15:24:23 +02:00
else
2016-07-31 15:48:50 +02:00
LIGHT_STYLE( m_iStyle, "m" );
2016-06-04 15:24:23 +02:00
}
}
2016-07-31 15:48:50 +02:00
void CLight::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( m_iStyle >= 32 )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( !ShouldToggle( useType, !FBitSet( pev->spawnflags, SF_LIGHT_START_OFF ) ) )
2016-06-04 15:24:23 +02:00
return;
2016-07-31 15:48:50 +02:00
if( FBitSet( pev->spawnflags, SF_LIGHT_START_OFF ) )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( m_iszPattern )
2017-06-29 15:56:03 +02:00
LIGHT_STYLE( m_iStyle, STRING( m_iszPattern ) );
2016-06-04 15:24:23 +02:00
else
2016-07-31 15:48:50 +02:00
LIGHT_STYLE( m_iStyle, "m" );
ClearBits( pev->spawnflags, SF_LIGHT_START_OFF );
2016-06-04 15:24:23 +02:00
}
else
{
2016-07-31 15:48:50 +02:00
LIGHT_STYLE( m_iStyle, "a" );
SetBits( pev->spawnflags, SF_LIGHT_START_OFF );
2016-06-04 15:24:23 +02:00
}
}
}
//
// shut up spawn functions for new spotlights
//
LINK_ENTITY_TO_CLASS( light_spot, CLight )
2016-06-04 15:24:23 +02:00
class CEnvLight : public CLight
{
public:
2016-07-31 15:48:50 +02:00
void KeyValue( KeyValueData* pkvd );
void Spawn( void );
2016-06-04 15:24:23 +02:00
};
LINK_ENTITY_TO_CLASS( light_environment, CEnvLight )
2016-06-04 15:24:23 +02:00
void CEnvLight::KeyValue( KeyValueData* pkvd )
{
2016-07-31 15:48:50 +02:00
if( FStrEq(pkvd->szKeyName, "_light" ) )
2016-06-04 15:24:23 +02:00
{
int r, g, b, v, j;
char szColor[64];
j = sscanf( pkvd->szValue, "%d %d %d %d\n", &r, &g, &b, &v );
2016-07-31 15:48:50 +02:00
if( j == 1 )
2016-06-04 15:24:23 +02:00
{
g = b = r;
}
2016-07-31 15:48:50 +02:00
else if( j == 4 )
2016-06-04 15:24:23 +02:00
{
2018-07-08 14:49:23 +02:00
float vf = v / 255.0f;
r *= vf;
g *= vf;
b *= vf;
2016-06-04 15:24:23 +02:00
}
// simulate qrad direct, ambient,and gamma adjustments, as well as engine scaling
2019-10-13 13:49:25 +02:00
r = (int)( pow( r / 114.0f, 0.6f ) * 264.0f );
g = (int)( pow( g / 114.0f, 0.6f ) * 264.0f );
b = (int)( pow( b / 114.0f, 0.6f ) * 264.0f );
2016-06-04 15:24:23 +02:00
pkvd->fHandled = TRUE;
sprintf( szColor, "%d", r );
CVAR_SET_STRING( "sv_skycolor_r", szColor );
sprintf( szColor, "%d", g );
CVAR_SET_STRING( "sv_skycolor_g", szColor );
sprintf( szColor, "%d", b );
CVAR_SET_STRING( "sv_skycolor_b", szColor );
}
else
{
CLight::KeyValue( pkvd );
}
}
2016-07-31 15:48:50 +02:00
void CEnvLight::Spawn( void )
2016-06-04 15:24:23 +02:00
{
char szVector[64];
UTIL_MakeAimVectors( pev->angles );
2019-10-13 13:49:25 +02:00
sprintf( szVector, "%f", (double)gpGlobals->v_forward.x );
2016-06-04 15:24:23 +02:00
CVAR_SET_STRING( "sv_skyvec_x", szVector );
2019-10-13 13:49:25 +02:00
sprintf( szVector, "%f", (double)gpGlobals->v_forward.y );
2016-06-04 15:24:23 +02:00
CVAR_SET_STRING( "sv_skyvec_y", szVector );
2019-10-13 13:49:25 +02:00
sprintf( szVector, "%f", (double)gpGlobals->v_forward.z );
2016-06-04 15:24:23 +02:00
CVAR_SET_STRING( "sv_skyvec_z", szVector );
2016-07-31 15:48:50 +02:00
CLight::Spawn();
2016-06-04 15:24:23 +02:00
}