Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Material modify control entity.
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. // memdbgon must be the last include file in a .cpp file!!!
  8. #include "tier0/memdbgon.h"
  9. //------------------------------------------------------------------------------
  10. // FIXME: This really should inherit from something more lightweight.
  11. //------------------------------------------------------------------------------
  12. #define MATERIAL_MODIFY_STRING_SIZE 255
  13. #define MATERIAL_MODIFY_ANIMATION_UNSET -1
  14. // Must match C_MaterialModifyControl.cpp
  15. enum MaterialModifyMode_t
  16. {
  17. MATERIAL_MODIFY_MODE_NONE = 0,
  18. MATERIAL_MODIFY_MODE_SETVAR = 1,
  19. MATERIAL_MODIFY_MODE_ANIM_SEQUENCE = 2,
  20. MATERIAL_MODIFY_MODE_FLOAT_LERP = 3,
  21. };
  22. ConVar debug_materialmodifycontrol( "debug_materialmodifycontrol", "0" );
  23. class CMaterialModifyControl : public CBaseEntity
  24. {
  25. public:
  26. DECLARE_CLASS( CMaterialModifyControl, CBaseEntity );
  27. CMaterialModifyControl();
  28. void Spawn( void );
  29. bool KeyValue( const char *szKeyName, const char *szValue );
  30. int UpdateTransmitState();
  31. int ShouldTransmit( const CCheckTransmitInfo *pInfo );
  32. void SetMaterialVar( inputdata_t &inputdata );
  33. void SetMaterialVarToCurrentTime( inputdata_t &inputdata );
  34. void InputStartAnimSequence( inputdata_t &inputdata );
  35. void InputStartFloatLerp( inputdata_t &inputdata );
  36. virtual int ObjectCaps( void ) { return BaseClass::ObjectCaps() & ~FCAP_ACROSS_TRANSITION; }
  37. DECLARE_SERVERCLASS();
  38. DECLARE_DATADESC();
  39. private:
  40. CNetworkString( m_szMaterialName, MATERIAL_MODIFY_STRING_SIZE );
  41. CNetworkString( m_szMaterialVar, MATERIAL_MODIFY_STRING_SIZE );
  42. CNetworkString( m_szMaterialVarValue, MATERIAL_MODIFY_STRING_SIZE );
  43. CNetworkVar( int, m_iFrameStart );
  44. CNetworkVar( int, m_iFrameEnd );
  45. CNetworkVar( bool, m_bWrap );
  46. CNetworkVar( float, m_flFramerate );
  47. CNetworkVar( bool, m_bNewAnimCommandsSemaphore );
  48. CNetworkVar( float, m_flFloatLerpStartValue );
  49. CNetworkVar( float, m_flFloatLerpEndValue );
  50. CNetworkVar( float, m_flFloatLerpTransitionTime );
  51. CNetworkVar( int, m_nModifyMode );
  52. };
  53. LINK_ENTITY_TO_CLASS(material_modify_control, CMaterialModifyControl);
  54. BEGIN_DATADESC( CMaterialModifyControl )
  55. // Variables.
  56. DEFINE_AUTO_ARRAY( m_szMaterialName, FIELD_CHARACTER ),
  57. DEFINE_AUTO_ARRAY( m_szMaterialVar, FIELD_CHARACTER ),
  58. DEFINE_AUTO_ARRAY( m_szMaterialVarValue, FIELD_CHARACTER ),
  59. DEFINE_FIELD( m_iFrameStart, FIELD_INTEGER ),
  60. DEFINE_FIELD( m_iFrameEnd, FIELD_INTEGER ),
  61. DEFINE_FIELD( m_bWrap, FIELD_BOOLEAN ),
  62. DEFINE_FIELD( m_flFramerate, FIELD_FLOAT ),
  63. DEFINE_FIELD( m_bNewAnimCommandsSemaphore, FIELD_BOOLEAN ),
  64. DEFINE_FIELD( m_flFloatLerpStartValue, FIELD_FLOAT ),
  65. DEFINE_FIELD( m_flFloatLerpEndValue, FIELD_FLOAT ),
  66. DEFINE_FIELD( m_flFloatLerpTransitionTime, FIELD_FLOAT ),
  67. DEFINE_FIELD( m_nModifyMode, FIELD_INTEGER ),
  68. // Inputs.
  69. DEFINE_INPUTFUNC( FIELD_STRING, "SetMaterialVar", SetMaterialVar ),
  70. DEFINE_INPUTFUNC( FIELD_VOID, "SetMaterialVarToCurrentTime", SetMaterialVarToCurrentTime ),
  71. DEFINE_INPUTFUNC( FIELD_STRING, "StartAnimSequence", InputStartAnimSequence ),
  72. DEFINE_INPUTFUNC( FIELD_STRING, "StartFloatLerp", InputStartFloatLerp ),
  73. END_DATADESC()
  74. IMPLEMENT_SERVERCLASS_ST(CMaterialModifyControl, DT_MaterialModifyControl)
  75. SendPropString( SENDINFO( m_szMaterialName ) ),
  76. SendPropString( SENDINFO( m_szMaterialVar ) ),
  77. SendPropString( SENDINFO( m_szMaterialVarValue ) ),
  78. SendPropInt( SENDINFO(m_iFrameStart), 8 ),
  79. SendPropInt( SENDINFO(m_iFrameEnd), 8 ),
  80. SendPropInt( SENDINFO(m_bWrap), 1, SPROP_UNSIGNED ),
  81. SendPropFloat( SENDINFO(m_flFramerate), 0, SPROP_NOSCALE ),
  82. SendPropInt( SENDINFO(m_bNewAnimCommandsSemaphore), 1, SPROP_UNSIGNED ),
  83. SendPropFloat( SENDINFO(m_flFloatLerpStartValue), 0, SPROP_NOSCALE ),
  84. SendPropFloat( SENDINFO(m_flFloatLerpEndValue), 0, SPROP_NOSCALE ),
  85. SendPropFloat( SENDINFO(m_flFloatLerpTransitionTime), 0, SPROP_NOSCALE ),
  86. SendPropInt( SENDINFO(m_nModifyMode), 2, SPROP_UNSIGNED ),
  87. END_SEND_TABLE()
  88. //-----------------------------------------------------------------------------
  89. // Purpose:
  90. //-----------------------------------------------------------------------------
  91. CMaterialModifyControl::CMaterialModifyControl()
  92. {
  93. m_iFrameStart = MATERIAL_MODIFY_ANIMATION_UNSET;
  94. m_iFrameEnd = MATERIAL_MODIFY_ANIMATION_UNSET;
  95. m_nModifyMode = MATERIAL_MODIFY_MODE_NONE;
  96. }
  97. //------------------------------------------------------------------------------
  98. // Purpose :
  99. //------------------------------------------------------------------------------
  100. void CMaterialModifyControl::Spawn( void )
  101. {
  102. Precache();
  103. SetSolid( SOLID_NONE );
  104. }
  105. //------------------------------------------------------------------------------
  106. // Purpose :
  107. //------------------------------------------------------------------------------
  108. bool CMaterialModifyControl::KeyValue( const char *szKeyName, const char *szValue )
  109. {
  110. if ( FStrEq( szKeyName, "materialName" ) )
  111. {
  112. Q_strncpy( m_szMaterialName.GetForModify(), szValue, MATERIAL_MODIFY_STRING_SIZE );
  113. return true;
  114. }
  115. if ( FStrEq( szKeyName, "materialVar" ) )
  116. {
  117. Q_strncpy( m_szMaterialVar.GetForModify(), szValue, MATERIAL_MODIFY_STRING_SIZE );
  118. return true;
  119. }
  120. return BaseClass::KeyValue( szKeyName, szValue );
  121. }
  122. //------------------------------------------------------------------------------
  123. // Purpose : Send even though we don't have a model.
  124. //------------------------------------------------------------------------------
  125. int CMaterialModifyControl::UpdateTransmitState()
  126. {
  127. // ALWAYS transmit to all clients.
  128. return SetTransmitState( FL_EDICT_FULLCHECK );
  129. }
  130. //-----------------------------------------------------------------------------
  131. // Send if the parent is being sent:
  132. //-----------------------------------------------------------------------------
  133. int CMaterialModifyControl::ShouldTransmit( const CCheckTransmitInfo *pInfo )
  134. {
  135. CBaseEntity *pEnt = GetMoveParent();
  136. if ( pEnt )
  137. {
  138. return pEnt->ShouldTransmit( pInfo );
  139. }
  140. return FL_EDICT_DONTSEND;
  141. }
  142. //-----------------------------------------------------------------------------
  143. // Purpose:
  144. //-----------------------------------------------------------------------------
  145. void CMaterialModifyControl::SetMaterialVar( inputdata_t &inputdata )
  146. {
  147. //if( debug_materialmodifycontrol.GetBool() && Q_stristr( GetDebugName(), "alyx" ) )
  148. //{
  149. //DevMsg( 1, "CMaterialModifyControl::SetMaterialVar %s %s %s=\"%s\"\n",
  150. //GetDebugName(), m_szMaterialName.Get(), m_szMaterialVar.Get(), inputdata.value.String() );
  151. //}
  152. Q_strncpy( m_szMaterialVarValue.GetForModify(), inputdata.value.String(), MATERIAL_MODIFY_STRING_SIZE );
  153. m_nModifyMode = MATERIAL_MODIFY_MODE_SETVAR;
  154. }
  155. //-----------------------------------------------------------------------------
  156. // Purpose:
  157. //-----------------------------------------------------------------------------
  158. void CMaterialModifyControl::SetMaterialVarToCurrentTime( inputdata_t &inputdata )
  159. {
  160. char temp[32];
  161. Q_snprintf( temp, 32, "%f", gpGlobals->curtime );
  162. Q_strncpy( m_szMaterialVarValue.GetForModify(), temp, MATERIAL_MODIFY_STRING_SIZE );
  163. m_nModifyMode = MATERIAL_MODIFY_MODE_SETVAR;
  164. }
  165. //-----------------------------------------------------------------------------
  166. // Purpose:
  167. //-----------------------------------------------------------------------------
  168. void CMaterialModifyControl::InputStartAnimSequence( inputdata_t &inputdata )
  169. {
  170. char parseString[255];
  171. Q_strncpy(parseString, inputdata.value.String(), sizeof(parseString));
  172. // Get the start & end frames
  173. char *pszParam = strtok(parseString," ");
  174. if ( pszParam && pszParam[0] )
  175. {
  176. int iFrameStart = atoi(pszParam);
  177. pszParam = strtok(NULL," ");
  178. if ( pszParam && pszParam[0] )
  179. {
  180. int iFrameEnd = atoi(pszParam);
  181. pszParam = strtok(NULL," ");
  182. if ( pszParam && pszParam[0] )
  183. {
  184. float flFramerate = atof(pszParam);
  185. pszParam = strtok(NULL," ");
  186. if ( pszParam && pszParam[0] )
  187. {
  188. bool bWrap = atoi(pszParam) != 0;
  189. // Got all the parameters. Save 'em and return;
  190. m_iFrameStart = iFrameStart;
  191. m_iFrameEnd = iFrameEnd;
  192. m_flFramerate = flFramerate;
  193. m_bWrap = bWrap;
  194. m_nModifyMode = MATERIAL_MODIFY_MODE_ANIM_SEQUENCE;
  195. m_bNewAnimCommandsSemaphore = !m_bNewAnimCommandsSemaphore;
  196. return;
  197. }
  198. }
  199. }
  200. }
  201. Warning("%s (%s) received StartAnimSequence input without correct parameters. Syntax: <Frame Start> <Frame End> <Frame Rate> <Loop>\nSetting <Frame End> to -1 uses the last frame of the texture. <Loop> should be 1 or 0.\n", GetClassname(), GetDebugName() );
  202. }
  203. //-----------------------------------------------------------------------------
  204. // Purpose:
  205. //-----------------------------------------------------------------------------
  206. void CMaterialModifyControl::InputStartFloatLerp( inputdata_t &inputdata )
  207. {
  208. char parseString[255];
  209. Q_strncpy(parseString, inputdata.value.String(), sizeof(parseString));
  210. // if( debug_materialmodifycontrol.GetBool() )//&& Q_stristr( GetDebugName(), "alyx" ) )
  211. // {
  212. // DevMsg( 1, "CMaterialModifyControl::InputStartFloatLerp %s %s %s \"%s\"\n",
  213. // GetDebugName(), m_szMaterialName.Get(), m_szMaterialVar.Get(), inputdata.value.String() );
  214. // }
  215. // Get the start & end values
  216. char *pszParam = strtok(parseString," ");
  217. if ( pszParam && pszParam[0] )
  218. {
  219. float flStartValue = atof(pszParam);
  220. pszParam = strtok(NULL," ");
  221. if ( pszParam && pszParam[0] )
  222. {
  223. float flEndValue = atof(pszParam);
  224. pszParam = strtok(NULL," ");
  225. if ( pszParam && pszParam[0] )
  226. {
  227. float flTransitionTime = atof(pszParam);
  228. pszParam = strtok(NULL," ");
  229. if ( pszParam && pszParam[0] )
  230. {
  231. bool bWrap = atoi(pszParam) != 0;
  232. // We don't implement wrap currently.
  233. NOTE_UNUSED( bWrap );
  234. // Got all the parameters. Save 'em and return;
  235. m_flFloatLerpStartValue = flStartValue;
  236. m_flFloatLerpEndValue = flEndValue;
  237. m_flFloatLerpTransitionTime = flTransitionTime;
  238. m_nModifyMode = MATERIAL_MODIFY_MODE_FLOAT_LERP;
  239. m_bNewAnimCommandsSemaphore = !m_bNewAnimCommandsSemaphore;
  240. return;
  241. }
  242. }
  243. }
  244. }
  245. Warning("%s (%s) received StartFloatLerp input without correct parameters. Syntax: <Start Value> <End Value> <Transition Time> <Loop>\n<Loop> should be 1 or 0.\n", GetClassname(), GetDebugName() );
  246. }