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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Mirrors the movement of a camera about a given point.
//
//=============================================================================//
#include "cbase.h"
#include "baseentity.h"
#include "modelentities.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//////////////////////////////////////////////////////////////////////////
// CLogicMirrorMovement
// This will record the vector offset of an entity's center from a given reference point
// (most likely the center of a mirror or portal) and place an entity (most likely a point camera)
// at a the same offset, mirrored about the reference point and orientation.
//////////////////////////////////////////////////////////////////////////
class CLogicMirrorMovement : public CLogicalEntity
{
DECLARE_DATADESC();
DECLARE_CLASS( CLogicMirrorMovement, CLogicalEntity );
private:
void SetMirrorTarget( const char *pName ); // Set entity to watch and mirror (ex. the player)
void SetTarget( const char *pName ); // Set entity to move based on the Mirror Target entity (ex. a point_camera)
void SetMirrorRelative( const char* pName ); // Set the point about which to measure an offset to orient based upon (ex. portal 1)
void SetRemoteTarget ( const char *pName ); // Entity's orientation/location from which to offset the movement target (ex. portal 2)
void SetDrawingSurface ( const char *pName );
void InputSetMirrorTarget( inputdata_t &inputdata );
void InputSetTarget( inputdata_t &inputdata );
void InputSetRemoteTarget ( inputdata_t &inputdata );
void InputSetMirrorRelative ( inputdata_t &inputdata );
void Think();
virtual void Activate();
string_t m_strMirrorTarget;
string_t m_strRemoteTarget;
string_t m_strMirrorRelative;
EHANDLE m_hRemoteTarget;
EHANDLE m_hMirrorTarget;
EHANDLE m_hMovementTarget;
EHANDLE m_hMirrorRelative;
};
LINK_ENTITY_TO_CLASS( logic_mirror_movement, CLogicMirrorMovement );
BEGIN_DATADESC( CLogicMirrorMovement )
DEFINE_KEYFIELD( m_strMirrorTarget, FIELD_STRING, "MirrorTarget" ),
DEFINE_KEYFIELD( m_strRemoteTarget, FIELD_STRING, "RemoteTarget" ),
DEFINE_KEYFIELD( m_strMirrorRelative, FIELD_STRING, "MirrorRelative" ),
DEFINE_FIELD( m_hMirrorTarget, FIELD_EHANDLE ),
DEFINE_FIELD( m_hMovementTarget, FIELD_EHANDLE ),
DEFINE_FIELD( m_hRemoteTarget, FIELD_EHANDLE ),
DEFINE_FIELD( m_hMirrorRelative, FIELD_EHANDLE ),
DEFINE_INPUTFUNC( FIELD_STRING, "SetMirrorTarget", InputSetMirrorTarget ),
DEFINE_INPUTFUNC( FIELD_STRING, "SetTarget", InputSetTarget ),
DEFINE_INPUTFUNC( FIELD_STRING, "SetRemoteTarget", InputSetRemoteTarget ),
DEFINE_INPUTFUNC( FIELD_STRING, "SetMirrorRelative", InputSetMirrorRelative ),
DEFINE_THINKFUNC( Think ),
END_DATADESC()
void CLogicMirrorMovement::Activate()
{
BaseClass::Activate();
SetMirrorTarget( STRING(m_strMirrorTarget) );
SetTarget( STRING(m_target) );
SetRemoteTarget( STRING(m_strRemoteTarget ) );
SetMirrorRelative( STRING( m_strMirrorRelative) );
SetThink( &CLogicMirrorMovement::Think );
SetNextThink( gpGlobals->curtime + TICK_INTERVAL );
}
void CLogicMirrorMovement::SetMirrorTarget( const char *pName )
{
m_hMirrorTarget = gEntList.FindEntityByName( NULL, pName );
if ( !m_hMirrorTarget )
{
if ( Q_strnicmp( STRING(m_strMirrorTarget), "!player", 8 ) )
{
Warning("logic_mirror_movement: Unable to find mirror target entity %s\n", pName );
}
}
}
void CLogicMirrorMovement::SetTarget( const char *pName )
{
m_hMovementTarget = gEntList.FindEntityByName( NULL, pName );
if ( !m_hMovementTarget )
{
Warning("logic_mirror_movement: Unable to find movement target entity %s\n", pName );
}
}
void CLogicMirrorMovement::SetRemoteTarget(const char *pName )
{
m_hRemoteTarget = gEntList.FindEntityByName( NULL, pName );
if ( !m_hRemoteTarget )
{
Warning("logic_mirror_movement: Unable to find remote target entity %s\n", pName );
}
}
void CLogicMirrorMovement::SetMirrorRelative(const char* pName )
{
m_hMirrorRelative = gEntList.FindEntityByName( NULL, pName );
if ( !m_hMirrorRelative )
{
Warning("logic_mirror_movement: Unable to find mirror relative entity %s\n", pName );
}
}
void CLogicMirrorMovement::InputSetMirrorTarget( inputdata_t &inputdata )
{
m_strMirrorTarget = AllocPooledString( inputdata.value.String() );
SetMirrorTarget( inputdata.value.String() );
}
void CLogicMirrorMovement::InputSetTarget( inputdata_t &inputdata )
{
m_target = AllocPooledString( inputdata.value.String() );
SetTarget( inputdata.value.String() );
}
void CLogicMirrorMovement::InputSetRemoteTarget(inputdata_t &inputdata )
{
m_strRemoteTarget = AllocPooledString( inputdata.value.String() );
SetRemoteTarget( inputdata.value.String() );
}
void CLogicMirrorMovement::InputSetMirrorRelative(inputdata_t &inputdata )
{
m_strMirrorRelative = AllocPooledString ( inputdata.value.String() );
SetMirrorRelative( inputdata.value.String() );
}
void CLogicMirrorMovement::Think()
{
// Attempt to get the player's handle because it didn't exist at Activate time
if ( !m_hMirrorTarget.Get() )
{
// If we will never find a target, we don't have a use... shutdown
if ( m_strMirrorTarget == NULL_STRING )
SetNextThink ( NULL );
//BUGBUG: If m_strSetMirrorTarget doesn't exist in ent list, we get per-think searches with no results ever...
SetMirrorTarget ( STRING(m_strMirrorTarget) );
}
// Make sure all entities are valid
if ( m_hMirrorTarget.Get() && m_hMovementTarget.Get() && m_hRemoteTarget.Get() && m_hMirrorRelative.Get() )
{
// Get our two portal's world transforms transforms
VMatrix matPortal1ToWorldInv, matPortal2ToWorld;
MatrixInverseGeneral( m_hMirrorRelative->EntityToWorldTransform(), matPortal1ToWorldInv );
matPortal2ToWorld = m_hRemoteTarget->EntityToWorldTransform();
VMatrix matTransformToRemotePortal = matPortal1ToWorldInv * matPortal2ToWorld;
// Get our scene camera's current orientation
Vector ptCameraPosition, vCameraLook, vCameraRight, vCameraUp;
ptCameraPosition = m_hMirrorTarget->EyePosition();
m_hMirrorTarget->GetVectors ( &vCameraLook, &vCameraRight, &vCameraUp );
// map this position and orientation to the remote portal, mirrored (invert the result)
Vector ptNewPosition, vNewLook;
ptNewPosition = matPortal1ToWorldInv * ptCameraPosition;
ptNewPosition = matPortal2ToWorld*( Vector( -ptNewPosition.x, -ptNewPosition.y, ptNewPosition.z ) );
vNewLook = matPortal1ToWorldInv.ApplyRotation( vCameraLook );
vNewLook = matPortal2ToWorld.ApplyRotation( Vector( -vNewLook.x, -vNewLook.y, vNewLook.z) );
// Set the point camera to the new location/orientation
QAngle qNewAngles;
VectorAngles( vNewLook, qNewAngles );
m_hMovementTarget->Teleport( &ptNewPosition, &qNewAngles, NULL );
}
SetNextThink( gpGlobals->curtime + TICK_INTERVAL );
}
|