aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/server/hl2/look_door.cpp
blob: 488c4a6f29e740ba6e0ae24c0add983f1679dcc7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Implements doors that move when you look at them.
//
// $NoKeywords: $
//=============================================================================//


#include "cbase.h"
#include "basecombatcharacter.h"
#include "entitylist.h"
#include "func_movelinear.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

#define SF_LDOOR_THRESHOLD			8192
#define SF_LDOOR_INVERT				16384
#define SF_LDOOR_FROM_OPEN			32768


class CLookDoor : public CFuncMoveLinear
{
public:
	DECLARE_CLASS( CLookDoor, CFuncMoveLinear );

	void	Spawn( void );
	void	MoveThink( void );

	// Inputs
	void InputInvertOn( inputdata_t &inputdata );
	void InputInvertOff( inputdata_t &inputdata );

	float	m_flProximityDistance;	// How far before I start reacting
	float	m_flProximityOffset;	
	float	m_flFieldOfView;
	
	EHANDLE	m_hLooker;				// Who is looking

	DECLARE_DATADESC();
};


class CLookDoorThinker : public CLogicalEntity
{
public:
	DECLARE_CLASS( CLookDoorThinker, CLogicalEntity );

	void	LookThink( void );
	EHANDLE	m_hLookDoor;				// Who owns me
	
	DECLARE_DATADESC();
};


BEGIN_DATADESC( CLookDoorThinker )

	DEFINE_FIELD( m_hLookDoor, FIELD_EHANDLE ),

	// Function Pointers
	DEFINE_FUNCTION(LookThink),

END_DATADESC()


LINK_ENTITY_TO_CLASS( lookdoorthinker, CLookDoorThinker );


//------------------------------------------------------------------------------
// Purpose :
//------------------------------------------------------------------------------
void CLookDoorThinker::LookThink(void)
{
	if (m_hLookDoor)
	{
		((CLookDoor*)(CBaseEntity*)m_hLookDoor)->MoveThink();
		SetNextThink( gpGlobals->curtime + 0.01f );
	}
	else
	{
		UTIL_Remove(this);
	}
}


BEGIN_DATADESC( CLookDoor )

	DEFINE_KEYFIELD( m_flProximityDistance,	FIELD_FLOAT, "ProximityDistance"),
	DEFINE_KEYFIELD( m_flProximityOffset,	FIELD_FLOAT, "ProximityOffset"),
	DEFINE_KEYFIELD( m_flFieldOfView,		FIELD_FLOAT, "FieldOfView" ),
	DEFINE_FIELD(m_hLooker,				FIELD_EHANDLE),

	// Inputs
	DEFINE_INPUTFUNC( FIELD_VOID,		"InvertOn",		InputInvertOn ),
	DEFINE_INPUTFUNC( FIELD_VOID,		"InvertOff",	InputInvertOff ),

	// Function Pointers
	DEFINE_FUNCTION(MoveThink),

END_DATADESC()

LINK_ENTITY_TO_CLASS( func_lookdoor, CLookDoor );


//------------------------------------------------------------------------------
// Purpose : Input handlers.
//------------------------------------------------------------------------------
void CLookDoor::InputInvertOn( inputdata_t &inputdata )
{
	m_spawnflags |= SF_LDOOR_INVERT;
}

void CLookDoor::InputInvertOff( inputdata_t &inputdata )
{
	m_spawnflags &= ~SF_LDOOR_INVERT;
}


//------------------------------------------------------------------------------
// Purpose :
//------------------------------------------------------------------------------
void CLookDoor::Spawn(void)
{
	BaseClass::Spawn();

	if (m_target == NULL_STRING)
	{
		Warning( "ERROR: DoorLook (%s) given no target.  Rejecting spawn.\n",GetDebugName());
		return;
	}
	CLookDoorThinker* pLookThinker = (CLookDoorThinker*)CreateEntityByName("lookdoorthinker");
	if (pLookThinker)
	{
		pLookThinker->SetThink(&CLookDoorThinker::LookThink);
		pLookThinker->m_hLookDoor = this;
		pLookThinker->SetNextThink( gpGlobals->curtime + 0.1f );
	}
}


//------------------------------------------------------------------------------
// Purpose :
//------------------------------------------------------------------------------
void CLookDoor::MoveThink(void)
{
	// --------------------------------
	// Make sure we have a looker
	// --------------------------------
	if (m_hLooker == NULL)
	{
		m_hLooker = (CBaseEntity*)gEntList.FindEntityByName( NULL, m_target );

		if (m_hLooker == NULL)
		{
			return;
		}
	}

	//--------------------------------------
	// Calculate an orgin for the door
	//--------------------------------------
	Vector vOrigin = WorldSpaceCenter() - GetAbsOrigin();

	// If FROM_OPEN flag is set, door proximity is measured
	// from the open and not the closed position
	if (FBitSet (m_spawnflags, SF_LDOOR_FROM_OPEN))
	{
		vOrigin += m_vecPosition2;
	}

	// ------------------------------------------------------
	//  First add movement based on proximity
	// ------------------------------------------------------
	float flProxMove = 0;
	if (m_flProximityDistance > 0)
	{
		float flDist = (m_hLooker->GetAbsOrigin() - vOrigin).Length()-m_flProximityOffset;
		if (flDist < 0) flDist = 0;

		if (flDist < m_flProximityDistance)
		{
			if (FBitSet (m_spawnflags, SF_LDOOR_THRESHOLD))
			{
				flProxMove = 1.0;
			}
			else
			{
				flProxMove = 1-flDist/m_flProximityDistance;
			}
		}
	}

	// ------------------------------------------------------
	//  Then add movement based on view angle
	// ------------------------------------------------------
	float flViewMove = 0;
	if (m_flFieldOfView > 0)
	{
		// ----------------------------------------
		// Check that toucher is facing the target
		// ----------------------------------------
		Assert( dynamic_cast< CBaseCombatCharacter* >( m_hLooker.Get() ) );
		CBaseCombatCharacter* pBCC = (CBaseCombatCharacter*)m_hLooker.Get();
		Vector vTouchDir = pBCC->EyeDirection3D( );
		Vector vTargetDir =  vOrigin - pBCC->EyePosition();
		VectorNormalize(vTargetDir);

		float flDotPr = DotProduct(vTouchDir,vTargetDir);
		if (flDotPr < m_flFieldOfView)
		{
			flViewMove = 0.0;
		}
		else
		{
			flViewMove = (flDotPr-m_flFieldOfView)/(1.0 - m_flFieldOfView);
		}
	}

	//---------------------------------------
	// Summate the two moves
	//---------------------------------------
	float flMove = flProxMove + flViewMove;
	if (flMove > 1.0)
	{
		flMove = 1.0;
	}

	// If behavior is inverted do the reverse
	if (FBitSet (m_spawnflags, SF_LDOOR_INVERT))
	{
		flMove = 1-flMove;
	}

	// Move the door
	SetPosition( flMove );
}