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.

152 lines
3.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "test_stressentities.h"
  9. #include "vstdlib/random.h"
  10. #include "world.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. CStressEntityReg *CStressEntityReg::s_pHead = NULL;
  14. // CStressEntityReg::s_pHead in array form for convenient access.
  15. CUtlVector<CStressEntityReg*> g_StressEntityRegs;
  16. CUtlVector<EHANDLE> g_StressEntities;
  17. CBaseEntity* MoveToRandomSpot( CBaseEntity *pEnt )
  18. {
  19. if ( pEnt )
  20. {
  21. CBasePlayer *pLocalPlayer = UTIL_GetLocalPlayer();
  22. if ( pLocalPlayer )
  23. {
  24. Vector vForward;
  25. pLocalPlayer->EyeVectors(&vForward );
  26. UTIL_SetOrigin( pEnt, GetRandomSpot() );
  27. }
  28. }
  29. return pEnt;
  30. }
  31. Vector GetRandomSpot()
  32. {
  33. CWorld *pEnt = GetWorldEntity();
  34. if ( pEnt )
  35. {
  36. Vector vMin, vMax;
  37. pEnt->GetWorldBounds( vMin, vMax );
  38. return Vector(
  39. RandomFloat( vMin.x, vMax.x ),
  40. RandomFloat( vMin.y, vMax.y ),
  41. RandomFloat( vMin.z, vMax.z ) );
  42. }
  43. else
  44. {
  45. return Vector( 0, 0, 0 );
  46. }
  47. }
  48. void Test_InitRandomEntitySpawner( const CCommand &args )
  49. {
  50. // Put the list of registered functions into array form for convenience.
  51. g_StressEntityRegs.Purge();
  52. for ( CStressEntityReg *pCur=CStressEntityReg::GetListHead(); pCur; pCur=pCur->GetNext() )
  53. g_StressEntityRegs.AddToTail( pCur );
  54. // Create slots for all the entities..
  55. int nSlots = 100;
  56. if ( args.ArgC() >= 2 )
  57. nSlots = atoi( args[ 1 ] );
  58. g_StressEntities.Purge();
  59. g_StressEntities.SetSize( nSlots );
  60. Msg( "Test_InitRandomEntitySpawner: created %d slots.\n", nSlots );
  61. }
  62. void Test_SpawnRandomEntities( const CCommand &args )
  63. {
  64. if ( args.ArgC() < 3 )
  65. {
  66. Error( "Test_SpawnRandomEntities <min # entities> <max # entities> missing arguments." );
  67. }
  68. if ( g_StressEntities.Count() == 0 )
  69. {
  70. Error( "Test_SpawnRandomEntities: not initialized (call Test_InitRandomEntitySpawner frst)." );
  71. }
  72. int nMin = atoi( args[ 1 ] );
  73. int nMax = atoi( args[ 2 ] );
  74. int count = RandomInt( nMin, nMax );
  75. for ( int i=0; i < count; i++ )
  76. {
  77. int iSlot = RandomInt( 0, g_StressEntities.Count() - 1 );
  78. // Remove any old entity in this slot.
  79. if ( g_StressEntities[iSlot].Get() )
  80. UTIL_RemoveImmediate( g_StressEntities[iSlot] );
  81. // Create a new one in this slot.
  82. int iType = RandomInt( 0, g_StressEntityRegs.Count() - 1 );
  83. g_StressEntities[iSlot] = g_StressEntityRegs[iType]->GetFn()();
  84. }
  85. }
  86. void Test_RandomizeInPVS( const CCommand &args )
  87. {
  88. if ( args.ArgC() < 2 )
  89. {
  90. Error( "Test_RandomizeInPVS <percentage chance to change>" );
  91. }
  92. int percent = atoi( args[ 1 ] );
  93. for ( int i=0; i < g_StressEntities.Count(); i++ )
  94. {
  95. CBaseEntity *pEnt = g_StressEntities[i];
  96. if ( pEnt )
  97. {
  98. if ( RandomInt( 0, 100 ) < percent )
  99. {
  100. if ( pEnt->IsEffectActive( EF_NODRAW ) )
  101. pEnt->RemoveEffects( EF_NODRAW );
  102. else
  103. pEnt->AddEffects( EF_NODRAW );
  104. }
  105. }
  106. }
  107. }
  108. void Test_RemoveAllRandomEntities()
  109. {
  110. for ( int i=0; i < g_StressEntities.Count(); i++ )
  111. {
  112. if ( g_StressEntities[i].Get() )
  113. UTIL_Remove( g_StressEntities[i] );
  114. }
  115. }
  116. ConCommand cc_Test_InitRandomEntitySpawner( "Test_InitRandomEntitySpawner", Test_InitRandomEntitySpawner, 0, FCVAR_CHEAT );
  117. ConCommand cc_Test_SpawnRandomEntities( "Test_SpawnRandomEntities", Test_SpawnRandomEntities, 0, FCVAR_CHEAT );
  118. ConCommand cc_Test_RandomizeInPVS( "Test_RandomizeInPVS", Test_RandomizeInPVS, 0, FCVAR_CHEAT );
  119. ConCommand cc_Test_RemoveAllRandomEntities( "Test_RemoveAllRandomEntities", Test_RemoveAllRandomEntities, 0, FCVAR_CHEAT );