summaryrefslogtreecommitdiff
path: root/game/server/tf2/tf_func_weldable_door.cpp
blob: 3e278e452d2a96ee7444c12b451909ccb4ee4459 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Func door that's weldable shut
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "beam_shared.h"
#include "tf_player.h"
#include "tf_team.h"
#include "tf_basecombatweapon.h"
#include "tf_func_weldable_door.h"
#include "IEffects.h"


LINK_ENTITY_TO_CLASS( func_door_weldable, CWeldableDoor );

BEGIN_DATADESC( CWeldableDoor )

	// keys 
	DEFINE_KEYFIELD( m_iszWeldPoints, FIELD_STRING, "weldpoints" ),

END_DATADESC()

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

	m_hWeldingPlayer = NULL;
	m_flMaxWeldedPercentage = 0.0;
	m_iWeldLeader = WL_UNASSIGNED;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CWeldableDoor::Activate( void )
{
	BaseClass::Activate();

	// Find my weld points
	if ( !m_iszWeldPoints )
	{
		Msg( "func_weldable_door without weldpoints specified.\n" );
		UTIL_Remove( this );
		return;
	}

	// Find the weld points. Doors will almost always have multiple weld point pairs.
	CWeldPoint *pWeldStartPoint = NULL;
	while( (pWeldStartPoint = (CWeldPoint*)gEntList.FindEntityByName( pWeldStartPoint, m_iszWeldPoints ) ) != NULL )
	{
		// Does it have an endpoint specified?
		if ( !pWeldStartPoint->m_target )
		{
			Msg( "func_weldable_door weldpoint '%s' didn't have an endpoint specified.\n", STRING(m_iszWeldPoints) );
			continue;
		}

		// Find the endpoint for this startpoint
		CWeldPoint *pWeldEndPoint = (CWeldPoint*)gEntList.FindEntityByName( NULL, pWeldStartPoint->m_target );
		if ( pWeldEndPoint )
		{
			// Connect the start point to the endpoint
			pWeldStartPoint->SetEndPoint( pWeldEndPoint->GetLocalOrigin() );

			// Add it to the list
			m_aWeldPoints.AddToTail( pWeldStartPoint );
		}
		else
		{
			Msg( "func_weldable_door weldpoint couldn't find it's endpoint of '%s'.\n", STRING(pWeldStartPoint->m_target) );
			continue;
		}
	}

	// Did we find any weldpoint pairs?
	if ( m_aWeldPoints.Size() == 0 )
	{
		Msg( "func_weldable_door couldn't find any weldpoints for '%s'.\n", STRING(m_iszWeldPoints) );
		UTIL_Remove( this );
		return;
	}
}

//-----------------------------------------------------------------------------
// Purpose: Returns true if someone's allowed to start welding on this door
//-----------------------------------------------------------------------------
bool CWeldableDoor::IsWeldable( CBaseTFPlayer *pWeldee )
{
	// Can't be welded if I'm open
	if ( m_toggle_state != TS_AT_BOTTOM )
		return false;

	// Can't be welded if I'm already being welded by someone else
	if ( m_hWeldingPlayer != NULL && (((CBaseEntity*)m_hWeldingPlayer) != pWeldee) )
		return false;

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Return the weld leader. 
//			Doors can be made up of multiple door entities, so one of them needs to volunteer to control the welding.
//-----------------------------------------------------------------------------
CWeldableDoor *CWeldableDoor::GetWeldLeader( void )
{
	// Am I the leader?
	if ( m_iWeldLeader == WL_WELD_LEADER )
		return this;

	// If this guy's unassigned, he's volunteering
	if ( m_iWeldLeader == WL_UNASSIGNED )
	{
		m_iWeldLeader = WL_WELD_LEADER;

		// Tell all other friends they're children
		CBaseEntity *pTarget = NULL;
		if ( GetEntityName() != NULL_STRING )
		{
			while ( (pTarget = gEntList.FindEntityByName( pTarget, GetEntityName() )) != NULL )
			{
				if ( pTarget != this )
				{
					CWeldableDoor *pWeldableDoor = dynamic_cast< CWeldableDoor * >( pTarget );
					if ( pWeldableDoor )
					{
						pWeldableDoor->m_iWeldLeader = WL_WELD_CHILD;
					}
				}
			}
		}

		return this;
	}

	// We're not the leader. so find him
	CBaseEntity *pTarget = NULL;
	if ( GetEntityName() != NULL_STRING )
	{
		while ( (pTarget = gEntList.FindEntityByName( pTarget, GetEntityName() )) != NULL )
		{
			if ( pTarget != this )
			{
				CWeldableDoor *pWeldableDoor = dynamic_cast< CWeldableDoor * >( pTarget );
				if ( pWeldableDoor && pWeldableDoor->m_iWeldLeader == WL_WELD_LEADER )
					return pWeldableDoor;
			}
		}
	}

	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose: The Player's going to start welding this door.
//-----------------------------------------------------------------------------
void CWeldableDoor::StartWelding( CBaseTFPlayer *pWeldee )
{
	m_hWeldingPlayer = pWeldee;

	// If this is the weld leader, tell all the door pieces that the player's welding them
	if ( m_iWeldLeader == WL_WELD_LEADER )
	{
		CBaseEntity *pTarget = NULL;
		if ( GetEntityName() != NULL_STRING )
		{
			while ( (pTarget = gEntList.FindEntityByName( pTarget, GetEntityName() )) != NULL )
			{
				if ( pTarget != this )
				{
					CWeldableDoor *pWeldableDoor = dynamic_cast< CWeldableDoor * >( pTarget );
					if ( pWeldableDoor )
					{
						pWeldableDoor->StartWelding( pWeldee );
					}
				}
			}
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: The player's stopped welding this door
//-----------------------------------------------------------------------------
void CWeldableDoor::StopWelding( void )
{
	m_hWeldingPlayer = NULL;

	// If this is the weld leader, tell all the door pieces that the player's stopped welding them
	if ( m_iWeldLeader == WL_WELD_LEADER )
	{
		CBaseEntity *pTarget = NULL;
		if ( GetEntityName() != NULL_STRING )
		{
			while ( (pTarget = gEntList.FindEntityByName( pTarget, GetEntityName() )) != NULL )
			{
				if ( pTarget != this )
				{
					CWeldableDoor *pWeldableDoor = dynamic_cast< CWeldableDoor * >( pTarget );
					if ( pWeldableDoor )
					{
						pWeldableDoor->StopWelding();
					}
				}
			}
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Return the amount the door's been welded
//-----------------------------------------------------------------------------
float CWeldableDoor::GetWeldPercentage( void )
{
	return m_flWeldedPercentage;
}

//-----------------------------------------------------------------------------
// Purpose: Update the amount the door's been welded
//-----------------------------------------------------------------------------
void CWeldableDoor::UpdateWeld( bool bCutting, float flWeldPercentage )
{
	if ( m_flMaxWeldedPercentage < flWeldPercentage )
		m_flMaxWeldedPercentage = flWeldPercentage;
	m_flWeldedPercentage = flWeldPercentage;

	// Did we cut away the entire weld?
	if ( m_flWeldedPercentage <= 0.0 )
	{
		// Clear welded
		m_flMaxWeldedPercentage = 0.0;

		if ( m_bLocked )
		{
			// Unlock all the pieces of this door
			m_bLocked = false;

			CBaseEntity *pTarget = NULL;
			if ( GetEntityName() != NULL_STRING )
			{
				while ( (pTarget = gEntList.FindEntityByName( pTarget, GetEntityName() )) != NULL )
				{
					if ( pTarget != this )
					{
						CWeldableDoor *pWeldableDoor = dynamic_cast< CWeldableDoor * >( pTarget );
						if ( pWeldableDoor )
						{
							pWeldableDoor->Unlock();
						}
					}
				}
			}
		}
	}
	else
	{
		if ( !m_bLocked )
		{
			// Lock all the pieces of this door
			m_bLocked = true;

			CBaseEntity *pTarget = NULL;
			if ( GetEntityName() != NULL_STRING )
			{
				while ( (pTarget = gEntList.FindEntityByName( pTarget, GetEntityName() )) != NULL )
				{
					if ( pTarget != this )
					{
						CWeldableDoor *pWeldableDoor = dynamic_cast< CWeldableDoor * >( pTarget );
						if ( pWeldableDoor )
						{
							pWeldableDoor->Lock();
						}
					}
				}
			}
		}
	}

	// Update the beams
	for ( int i = 0; i < m_aWeldPoints.Size(); i++ )
	{
		// First time we've updated?
		if ( m_aWeldBeams.Size() <= i )
		{
			CBeam *pBeam = CBeam::BeamCreate( "sprites/physbeam.vmt", 4.0 );
			pBeam->SetColor( 128, 128, 128 ); 
			pBeam->SetBrightness( 128 );
			pBeam->PointsInit( m_aWeldPoints[i]->GetStartPoint(), m_aWeldPoints[i]->GetEndPoint() );
			m_aWeldBeams.AddToTail( pBeam );
		}
		if ( m_aCutBeams.Size() <= i )
		{
			CBeam *pBeam = CBeam::BeamCreate( "sprites/physbeam.vmt", 4.0 );
			pBeam->SetColor( 255, 255, 255 ); 
			pBeam->SetBrightness( 255 );
			pBeam->PointsInit( m_aWeldPoints[i]->GetStartPoint(), m_aWeldPoints[i]->GetEndPoint() );
			m_aCutBeams.AddToTail( pBeam );
		}

		// Figure out how far we've welded
		Vector vecLine = ( m_aWeldPoints[i]->GetEndPoint() - m_aWeldPoints[i]->GetStartPoint());
		float flLength = vecLine.Length();
		VectorNormalize(vecLine);
		vecLine = vecLine * (flLength * flWeldPercentage);
		Vector vecWeldPoint = m_aWeldPoints[i]->GetStartPoint() + vecLine;

		// Update the beams
		m_aWeldBeams[i]->SetStartPos( m_aWeldPoints[i]->GetStartPoint() );
		m_aWeldBeams[i]->SetEndPos( vecWeldPoint );
		m_aWeldBeams[i]->RelinkBeam();
		if ( flWeldPercentage <= m_flMaxWeldedPercentage )
		{
			// Get the cut point
			VectorNormalize(vecLine);
			vecLine = vecLine * (flLength * m_flMaxWeldedPercentage);
			Vector vecCutPoint = m_aWeldPoints[i]->GetStartPoint() + vecLine;

			m_aCutBeams[i]->SetEndPos( vecWeldPoint );
			m_aCutBeams[i]->SetStartPos( vecCutPoint );
			m_aCutBeams[i]->RelinkBeam();
		}

		// Some sparks
		if ( random->RandomInt(0,2) == 0 )
		{
			g_pEffects->Sparks( vecWeldPoint );
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Find a weld point to watch for this player
//-----------------------------------------------------------------------------
Vector CWeldableDoor::GetPlayerWeldPoint( void )
{
	CBaseTFPlayer *pPlayer = (CBaseTFPlayer *)(CBaseEntity*)m_hWeldingPlayer;
	Vector vecSrc = pPlayer->Weapon_ShootPosition( );
	trace_t tr;

	for ( int i = 0; i < m_aWeldPoints.Size(); i++ )
	{
		// Figure out where the weldpoint is
		Vector vecLine = ( m_aWeldPoints[i]->GetEndPoint() - m_aWeldPoints[i]->GetStartPoint());
		float flLength = vecLine.Length();
		VectorNormalize(vecLine);
		vecLine = vecLine * (flLength * m_flWeldedPercentage);
		Vector vecWeldPoint = m_aWeldPoints[i]->GetStartPoint() + vecLine;

		// Is the weld point visible to our player?
		UTIL_TraceLine( vecSrc, vecWeldPoint, MASK_SOLID, pPlayer, TFCOLLISION_GROUP_WEAPON, &tr );
		if ( tr.fraction == 1.0 )
			return vecWeldPoint;
	}

	return vec3_origin;
}


//============================================================================================================
// WELD POINTS
//============================================================================================================

LINK_ENTITY_TO_CLASS( info_weldpoint, CWeldPoint );

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CWeldPoint::Spawn( void )
{
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CWeldPoint::SetEndPoint( const Vector &vecEndPoint )
{
	m_vecEndPoint = vecEndPoint;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
const Vector &CWeldPoint::GetStartPoint( void ) const
{
	return GetLocalOrigin();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
const Vector &CWeldPoint::GetEndPoint( void ) const
{
	return m_vecEndPoint;
}