summaryrefslogtreecommitdiff
path: root/hammer/entityconnection.cpp
blob: 6a795b497ff06690ad78a1f0162208ed18b4ed48 (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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Defines a connection (output-to-input) between two entities.
//
//			The behavior in-game is as follows:
//
//			When the given output in the source entity is triggered, the given
//			input in the target entity is called after a specified delay, and
//			the parameter override (if any) is passed to the input handler. If
//			there is no parameter override, the default parameter is passed.
//
//			This behavior will occur a specified number of times before the
//			connection between the two entities is removed.
//
//=============================================================================//

#include "stdafx.h"
#include "EntityConnection.h"
#include "MapEntity.h"
#include "MapDoc.h"
#include "MapWorld.h"

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

//-----------------------------------------------------------------------------
// Purpose: Constructor.
//-----------------------------------------------------------------------------
CEntityConnection::CEntityConnection(void)
{
	memset(m_szSourceEntity, 0, sizeof(m_szSourceEntity));
	memset(m_szTargetEntity, 0, sizeof(m_szTargetEntity));
	memset(m_szOutput, 0, sizeof(m_szOutput));
	memset(m_szInput, 0, sizeof(m_szInput));
	memset(m_szParam, 0, sizeof(m_szParam));

	m_pSourceEntityList = new CMapEntityList;
	m_pTargetEntityList = new CMapEntityList;

	m_fDelay = 0;
	m_nTimesToFire = EVENT_FIRE_ALWAYS;
}

//-----------------------------------------------------------------------------
// Purpose: Copy Constructor.
//-----------------------------------------------------------------------------

CEntityConnection::CEntityConnection( const CEntityConnection &Other )
{
	m_pSourceEntityList = new CMapEntityList;
	m_pTargetEntityList = new CMapEntityList;

	*this = Other;	// Invoke the Operator= to complete the construction job
}

//-----------------------------------------------------------------------------
// Purpose: Destructor.
//-----------------------------------------------------------------------------
CEntityConnection::~CEntityConnection()
{
	if ( m_pSourceEntityList )
	{
        m_pSourceEntityList->RemoveAll();
		delete m_pSourceEntityList;
		m_pSourceEntityList = NULL;
	}
	if ( m_pTargetEntityList )
	{
		m_pTargetEntityList->RemoveAll();
		delete m_pTargetEntityList;
		m_pTargetEntityList = NULL;
	}
}

//-----------------------------------------------------------------------------
// Purpose: Operator= overload. Makes 'this' identical to 'Other'.
//-----------------------------------------------------------------------------
CEntityConnection &CEntityConnection::operator =(const CEntityConnection &Other)
{
	strcpy(m_szSourceEntity, Other.m_szSourceEntity);
	strcpy(m_szTargetEntity, Other.m_szTargetEntity);
	strcpy(m_szOutput, Other.m_szOutput);
	strcpy(m_szInput, Other.m_szInput);
	strcpy(m_szParam, Other.m_szParam);
	m_fDelay = Other.m_fDelay;
	m_nTimesToFire = Other.m_nTimesToFire;

	// Invoke EntityList operator= to make copies.
	*m_pSourceEntityList = *Other.m_pSourceEntityList;
	*m_pTargetEntityList = *Other.m_pTargetEntityList;

	return(*this);
}

//-----------------------------------------------------------------------------
// Purpose: Sets a new Input Name and sets links to any matching entities
//-----------------------------------------------------------------------------

void CEntityConnection::SetSourceName(const char *pszName) 
{
	// Save the name of the entity(ies)
	lstrcpyn(m_szSourceEntity, pszName ? pszName : "<<null>>", sizeof(m_szSourceEntity));
	
	// Update the source entity list
	// LinkSourceEntities(); // Changing the entity connection source name shouldnt change the source entity linkage, right?
}

//-----------------------------------------------------------------------------
// Purpose: Sets a new Output Name and sets links to any matching entities
//-----------------------------------------------------------------------------

void CEntityConnection::SetTargetName(const char *pszName) 
{
	// Save the name of the entity(ies)
	lstrcpyn(m_szTargetEntity, pszName ? pszName : "<<null>>", sizeof(m_szTargetEntity));

	// Update the target entity list
	LinkTargetEntities();
}

//-----------------------------------------------------------------------------
// Purpose: Links to any matching Source entities
//-----------------------------------------------------------------------------
void CEntityConnection::LinkSourceEntities()
{
	// Empty out the existing entity list
	m_pSourceEntityList->RemoveAll();

	// Get a list of all the entities in the world
	CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();

	if (pDoc)
	{
		CMapWorld *pWorld = pDoc->GetMapWorld();

		if (pWorld)
		{
			CMapEntityList matches;
			pWorld->FindEntitiesByName( matches, m_szSourceEntity, false );
		
			for ( int i = 0; i < matches.Count(); i++ )
			{
				CMapEntity *pEntity = matches.Element( i );

				m_pSourceEntityList->AddToTail( pEntity );
				//pEntity->Connection_Add( this ); // This should already be true on creation, investigate need for this func
			}
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: Links to any matching Target entities
//-----------------------------------------------------------------------------
void CEntityConnection::LinkTargetEntities()
{
	// Unlink us from the downstream entities.
	FOR_EACH_OBJ( *m_pTargetEntityList, pos )
	{
		CMapEntity *pEntity = m_pTargetEntityList->Element( pos );
		pEntity->Upstream_Remove( this );
	}

	// Empty out the existing entity list
	m_pTargetEntityList->RemoveAll();

	// Get a list of all the entities in the world
	CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();

	if (pDoc)
	{
		CMapWorld *pWorld = pDoc->GetMapWorld();

		if (pWorld)
		{
			CMapEntityList matches;
			pWorld->FindEntitiesByName( matches, m_szTargetEntity, false );
		
			for ( int i = 0; i < matches.Count(); i++ )
			{
				CMapEntity *pEntity = matches.Element( i );
		
				m_pTargetEntityList->AddToTail( pEntity );

				// Special -- Add this connection to the target entity connection list
				pEntity->Upstream_Add( this );
			}
		}
	}
}


//------------------------------------------------------------------------------
// Purpose: Tells if any of the target entities are visible.
//------------------------------------------------------------------------------
bool CEntityConnection::AreAnyTargetEntitiesVisible()
{
	CMapEntityList *pList = GetTargetEntityList();
	for ( int iTarget=0; iTarget < pList->Count(); iTarget++ )
	{
		if ( pList->Element( iTarget )->IsVisible() )
			return true;
	}

	return false;
}


//------------------------------------------------------------------------------
// Purpose: Returns true if output string is valid for all this entity
//------------------------------------------------------------------------------
bool CEntityConnection::ValidateOutput(CMapEntity *pEntity, const char* pszOutput)
{
	if (!pEntity)
	{
		return false;
	}

	GDclass*	pClass	= pEntity->GetClass();
	if (pClass != NULL)
	{
		if (pClass->FindOutput(pszOutput) == NULL)
		{
			return false;
		}
	}
	return true;
}

//------------------------------------------------------------------------------
// Purpose : Returns true if output string is valid for all the entities in 
//			 the entity list
// Input   :
// Output  :
//------------------------------------------------------------------------------
bool CEntityConnection::ValidateOutput(const CMapEntityList *pEntityList, const char* pszOutput)
{
	if (!pEntityList)
	{
		return false;
	}

	FOR_EACH_OBJ( *pEntityList, pos )
	{
		CMapEntity*	pEntity = pEntityList->Element(pos);
		if (!ValidateOutput(pEntity,pszOutput))
		{
			return false;
		}
	}
	return true;
}


//------------------------------------------------------------------------------
// Purpose: Returns true if the given entity list contains an entity of the
//			given target name
//------------------------------------------------------------------------------
bool CEntityConnection::ValidateTarget( const CMapEntityList *pEntityList, bool bVisibilityCheck, const char *pszTarget)
{
	if (!pEntityList || !pszTarget)
		return false;

	// These procedural names are always assumed to exist.
	if (!stricmp(pszTarget, "!activator") || !stricmp(pszTarget, "!caller") || !stricmp(pszTarget, "!player") || !stricmp(pszTarget, "!self"))
		return true;

	FOR_EACH_OBJ( *pEntityList, pos )
	{
		CMapEntity *pEntity = pEntityList->Element(pos);
		if ( bVisibilityCheck && !pEntity->IsVisible() )
			continue;

		if (pEntity->NameMatches(pszTarget))
			return true;
	}

	return false;
}


//------------------------------------------------------------------------------
// Purpose: Returns true if all entities with the given target name
//			have an input of the given input name
//------------------------------------------------------------------------------
bool CEntityConnection::ValidateInput(const char* pszTarget, const char *pszInput, bool bVisiblesOnly)
{
	// Allow any input into !activator and !player.
	// dvs: TODO: pass in the entity to resolve !self and check input list
	if (!stricmp(pszTarget, "!activator") || !stricmp(pszTarget, "!caller") || !stricmp(pszTarget, "!player") || !stricmp(pszTarget, "!self"))
	{
		return true;
	}

	CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
	CMapEntityList EntityList;
	pDoc->FindEntitiesByName(EntityList, pszTarget, bVisiblesOnly);

	if (EntityList.Count() == 0)
	{
		return false;
	}

	if (!MapEntityList_HasInput( &EntityList, pszInput))
	{
		return false;
	}

	return true;
}


//-----------------------------------------------------------------------------
// Purpose: Finds any output connections from this entity that are bad.
//			"Bad" is defined as:
//
//			1) An output that this entity doesn't actually have.
//			2) Connecting to a nonexistent entity.
//			3) Connecting to a nonexistent input in an entity that exists.
//
// Input  : pEntity - The entity to check for bad connections.
//-----------------------------------------------------------------------------
void CEntityConnection::FindBadConnections(CMapEntity *pEntity, bool bVisibilityCheck, CUtlVector<CEntityConnection *> &BadConnectionList, bool bIgnoreHiddenTargets)
{
	BadConnectionList.RemoveAll();

	if ((!pEntity) || (pEntity->Connections_GetCount() == 0))
	{
		return;
	}

	// Get a list of all the entities in the world
	const CMapEntityList *pAllWorldEntities = NULL;
	CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
	if (pDoc)
	{
		CMapWorld *pWorld = pDoc->GetMapWorld();
		if (pWorld)
		{
			pAllWorldEntities = pWorld->EntityList_GetList();
		}
	}

	// For each connection
	int nConnCount = pEntity->Connections_GetCount();
	for (int i = 0; i < nConnCount; i++)
	{
		CEntityConnection *pConnection = pEntity->Connections_Get(i);
		if (pConnection != NULL)
		{
			if ( bIgnoreHiddenTargets )
			{
				if ( pConnection->GetTargetEntityList()->Count() > 0 && !pConnection->AreAnyTargetEntitiesVisible() )
					continue;
			}
			
			// Check validity of output for this entity
			if (!CEntityConnection::ValidateOutput(pEntity, pConnection->GetOutputName()))
			{
				BadConnectionList.AddToTail(pConnection);
			}
			// Check validity of target entity (is it in the map?)
			else if (!CEntityConnection::ValidateTarget(pAllWorldEntities, bVisibilityCheck, pConnection->GetTargetName()))
			{
				BadConnectionList.AddToTail(pConnection);
			}
			// Check validity of input
			else if (!CEntityConnection::ValidateInput(pConnection->GetTargetName(), pConnection->GetInputName(), true))
			{
				BadConnectionList.AddToTail(pConnection);
			}
		}
	}
}


//------------------------------------------------------------------------------
// Purpose: Check if all the output connections in the given entity are valid.
// Output :		OUTPUTS_NONE	if entity has no outputs
//				OUTPUTS_GOOD	if all entity outputs are good
//				OUTPUTS_BAD		if any entity output is bad
//------------------------------------------------------------------------------
int CEntityConnection::ValidateOutputConnections(CMapEntity *pEntity, bool bVisibilityCheck, bool bIgnoreHiddenTargets)
{
	if (!pEntity)
	{
		return CONNECTION_NONE;
	}

	if (pEntity->Connections_GetCount() == 0)
	{
		return CONNECTION_NONE;
	}

	CUtlVector<CEntityConnection *> BadConnectionList;
	FindBadConnections(pEntity, bVisibilityCheck, BadConnectionList, bIgnoreHiddenTargets);

	if (BadConnectionList.Count() > 0)
	{
		return CONNECTION_BAD;
	}

	return CONNECTION_GOOD;	
}


//-----------------------------------------------------------------------------
// Purpose: Fixes any output connections from this entity that are bad.
// Input  : pEntity - The entity to fix.
//-----------------------------------------------------------------------------
void CEntityConnection::FixBadConnections(CMapEntity *pEntity, bool bVisibilityCheck )
{
	CUtlVector<CEntityConnection *> BadConnectionList;
	FindBadConnections(pEntity, bVisibilityCheck, BadConnectionList);

	// Remove the bad connections.
	int nBadConnCount = BadConnectionList.Count();
	for (int i = 0; i < nBadConnCount; i++)
	{
		CEntityConnection *pConnection = BadConnectionList.Element(i);
		pEntity->Connections_Remove( pConnection );
		
		//								
		// Remove the connection from the upstream list of all entities it targets.
		//
		CMapEntityList *pTargetList = pConnection->GetTargetEntityList();
		if ( pTargetList )
		{
			FOR_EACH_OBJ( *pTargetList, pos )
			{
				pEntity = pTargetList->Element( pos );
				pEntity->Upstream_Remove( pConnection );
			}
		}
				
		delete pConnection;
	}
}


//------------------------------------------------------------------------------
// Purpose: Check if all the output connections in the given entity are valid.
// Output :		INPUTS_NONE,	// if entity list has no inputs
//				INPUTS_GOOD,	// if all entity inputs are good
//				INPUTS_BAD,		// if any entity input is bad
//------------------------------------------------------------------------------
int CEntityConnection::ValidateInputConnections(CMapEntity *pEntity, bool bVisibilityCheck)
{
	if (!pEntity)
	{
		return CONNECTION_NONE;
	}

	// No inputs if entity doesn't have a target name
	const char *pszTargetName = pEntity->GetKeyValue("targetname");
	if (!pszTargetName)
	{
		return CONNECTION_NONE;
	}

	GDclass *pClass = pEntity->GetClass();
	if (!pClass)
	{
		return CONNECTION_NONE;
	}

	// Get a list of all the entities in the world
	const CMapEntityList *pAllWorldEntities = NULL;
	CMapDoc	*pDoc = CMapDoc::GetActiveMapDoc();
	if (pDoc)
	{
		CMapWorld *pWorld = pDoc->GetMapWorld();
		if (pWorld)
		{
			pAllWorldEntities = pWorld->EntityList_GetList();
		}
	}

	// Look at outputs from each entity in the world
	
	bool bHaveConnection = false;
	FOR_EACH_OBJ( *pAllWorldEntities, pos )
	{
		CMapEntity *pTestEntity = pAllWorldEntities->Element(pos);
		if (pTestEntity == NULL)
			continue;

		if ( bVisibilityCheck && !pTestEntity->IsVisible() )
			continue;

		int nConnCount = pTestEntity->Connections_GetCount();
		for (int i = 0; i < nConnCount; i++)
		{
			// If the connection targets me
			CEntityConnection *pConnection = pTestEntity->Connections_Get(i);
			if ( pConnection && pEntity->NameMatches( pConnection->GetTargetName() ) )
			{
				// Validate output
				if (!ValidateOutput(pTestEntity, pConnection->GetOutputName()))
				{
					return CONNECTION_BAD;
				}

				// Validate input
				if (pClass->FindInput(pConnection->GetInputName()) == NULL)
				{
					return CONNECTION_BAD;
				}

	// FIXME -- Validate the upstream connections the target entities.
				bHaveConnection = true;
			}
		}
	}


	if (bHaveConnection)
	{
		return CONNECTION_GOOD;
	}
	return CONNECTION_NONE;
}

//-----------------------------------------------------------------------------
// Purpose: Compares by delays. Used as a secondary sort by all other columns.
//-----------------------------------------------------------------------------
int CALLBACK CEntityConnection::CompareDelaysSecondary(CEntityConnection *pConn1, CEntityConnection *pConn2, SortDirection_t eDirection)
{
	float fDelay1;
	float fDelay2;

	if (eDirection == Sort_Ascending)
	{
		fDelay1 = pConn1->GetDelay();
		fDelay2 = pConn2->GetDelay();
	}
	else
	{
		fDelay1 = pConn2->GetDelay();
		fDelay2 = pConn1->GetDelay();
	}

	if (fDelay1 < fDelay2)
	{
		return(-1);
	}
	else if (fDelay1 > fDelay2)
	{
		return(1);
	}

	return(0);
}

//-----------------------------------------------------------------------------
// Purpose: Compares by delays, does a secondary compare by output name.
//-----------------------------------------------------------------------------
int CALLBACK CEntityConnection::CompareDelays(CEntityConnection *pConn1, CEntityConnection *pConn2, SortDirection_t eDirection)
{
	int nReturn = CompareDelaysSecondary(pConn1, pConn2, eDirection);
	if (nReturn != 0)
	{
		return(nReturn);
	}

	//
	// Always do a secondary sort by output name.
	//
	return(CompareOutputNames(pConn1, pConn2, Sort_Ascending));
}


//-----------------------------------------------------------------------------
// Purpose: Compares by output name, does a secondary compare by delay.
//-----------------------------------------------------------------------------
int CALLBACK CEntityConnection::CompareOutputNames(CEntityConnection *pConn1, CEntityConnection *pConn2, SortDirection_t eDirection)
{
	int nReturn = 0;

	if (eDirection == Sort_Ascending)
	{
		nReturn = stricmp(pConn1->GetOutputName(), pConn2->GetOutputName());
	}
	else
	{
		nReturn = stricmp(pConn2->GetOutputName(), pConn1->GetOutputName());
	}

	//
	// Always do a secondary sort by delay.
	//
	if (nReturn == 0)
	{
		nReturn = CompareDelaysSecondary(pConn1, pConn2, Sort_Ascending);
	}

	return(nReturn);
}


//-----------------------------------------------------------------------------
// Purpose: Compares by input name, does a secondary compare by delay.
//-----------------------------------------------------------------------------
int CALLBACK CEntityConnection::CompareInputNames(CEntityConnection *pConn1, CEntityConnection *pConn2, SortDirection_t eDirection)
{
	int nReturn = 0;

	if (eDirection == Sort_Ascending)
	{
		nReturn = stricmp(pConn1->GetInputName(), pConn2->GetInputName());
	}
	else
	{
		nReturn = stricmp(pConn2->GetInputName(), pConn1->GetInputName());
	}

	//
	// Always do a secondary sort by delay.
	//
	if (nReturn == 0)
	{
		nReturn = CompareDelaysSecondary(pConn1, pConn2, Sort_Ascending);
	}

	return(nReturn);
}

//-----------------------------------------------------------------------------
// Purpose: Compares by source name, does a secondary compare by delay.
//-----------------------------------------------------------------------------
int CALLBACK CEntityConnection::CompareSourceNames(CEntityConnection *pConn1, CEntityConnection *pConn2, SortDirection_t eDirection)
{
	int nReturn = 0;

	if (eDirection == Sort_Ascending)
	{
		nReturn = CompareEntityNames(pConn1->GetSourceName(), pConn2->GetSourceName());
	}
	else
	{
		nReturn = CompareEntityNames(pConn2->GetSourceName(), pConn1->GetSourceName());
	}

	//
	// Always do a secondary sort by delay.
	//
	if (nReturn == 0)
	{
		nReturn = CompareDelaysSecondary(pConn1, pConn2, Sort_Ascending);
	}

	return(nReturn);
}

//-----------------------------------------------------------------------------
// Purpose: Compares by target name, does a secondary compare by delay.
//-----------------------------------------------------------------------------
int CALLBACK CEntityConnection::CompareTargetNames(CEntityConnection *pConn1, CEntityConnection *pConn2, SortDirection_t eDirection)
{
	int nReturn = 0;

	if (eDirection == Sort_Ascending)
	{
		nReturn = CompareEntityNames(pConn1->GetTargetName(), pConn2->GetTargetName());
	}
	else
	{
		nReturn = CompareEntityNames(pConn2->GetTargetName(), pConn1->GetTargetName());
	}

	//
	// Always do a secondary sort by delay.
	//
	if (nReturn == 0)
	{
		nReturn = CompareDelaysSecondary(pConn1, pConn2, Sort_Ascending);
	}

	return(nReturn);
}