powersawgeneric.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "../idlib/precompiled.h"
  2. #pragma hdrstop
  3. #include "Game_local.h"
  4. const idEventDef EV_powersaw_spawndelay( "<powersaw_spawndelay>" );
  5. const idEventDef EV_powersaw_reset( "powersawreset" );
  6. CLASS_DECLARATION( idMover, idPowerSawGeneric )
  7. EVENT( EV_powersaw_spawndelay, idPowerSawGeneric::Spawn2)
  8. EVENT( EV_powersaw_reset, idPowerSawGeneric::reset)
  9. END_CLASS
  10. void idPowerSawGeneric::Save( idSaveGame *savefile ) const
  11. {
  12. savefile->WriteInt(state);
  13. savefile->WriteInt(nextSparktime);
  14. savefile->WriteString(callName.c_str());
  15. savefile->WriteObject(mover);
  16. savefile->WriteObject(splineEnt);
  17. savefile->WriteObject(sparks);
  18. savefile->WriteObject(light);
  19. }
  20. void idPowerSawGeneric::Restore( idRestoreGame *savefile )
  21. {
  22. savefile->ReadInt(state);
  23. savefile->ReadInt(nextSparktime);
  24. savefile->ReadString(callName);
  25. savefile->ReadObject(reinterpret_cast<idClass *&>(mover));
  26. savefile->ReadObject(reinterpret_cast<idClass *&>(splineEnt));
  27. savefile->ReadObject(reinterpret_cast<idClass *&>(sparks));
  28. savefile->ReadObject(reinterpret_cast<idClass *&>(light));
  29. }
  30. void idPowerSawGeneric::Spawn2( void )
  31. {
  32. idDict args;
  33. idStr splineName = spawnArgs.GetString( "spline" );
  34. splineName.StripTrailingWhitespace();
  35. splineName.StripLeading(' ');
  36. if ( !splineName.Length() )
  37. {
  38. gameLocal.Error( "idPowerSawGeneric '%s' at (%s) doesn't have 'spline' key specified", name.c_str(), GetPhysics()->GetOrigin().ToString(0) );
  39. }
  40. splineEnt = gameLocal.FindEntity( splineName );
  41. if (!splineEnt)
  42. {
  43. gameLocal.Error( "idPowerSawGeneric '%s' at (%s) can't find spline '%s'", name.c_str(), GetPhysics()->GetOrigin().ToString(0), splineName.c_str() );
  44. }
  45. splineEnt->Hide();
  46. int movetime = spawnArgs.GetFloat("movetime", "6");
  47. int acceltime = spawnArgs.GetFloat("acceltime", "2");
  48. int deceltime = spawnArgs.GetFloat("deceltime", "1");
  49. args.Clear();
  50. args.SetVector( "origin", splineEnt->GetPhysics()->GetOrigin() );
  51. args.Set( "model", "models/powersaw/tris.ase" );
  52. args.SetInt( "solid", 0 );
  53. args.SetInt( "frobbable", 0 );
  54. args.Set( "onsplineend", this->GetName() );
  55. mover = ( idMover * )gameLocal.SpawnEntityType( idMover::Type, &args );
  56. //mover->SetAngles( this->GetPhysics()->GetAxis().ToAngles() );
  57. mover->Event_SetMoveTime( movetime );
  58. mover->Event_SetAccellerationTime( acceltime);
  59. mover->Event_SetDecelerationTime( deceltime );
  60. args.Clear();
  61. args.SetVector("origin", mover->GetPhysics()->GetOrigin() );
  62. args.SetInt( "noshadows", 1);
  63. args.Set("texture", "lights/powersaw" );
  64. light = ( idLight * )gameLocal.SpawnEntityType( idLight::Type, &args );
  65. light->SetRadius(64);
  66. light->Event_FadeLight(0.3f, idVec3(0.8,0.5,0));
  67. light->Event_Off();
  68. light->Bind(mover, false);
  69. if (spawnArgs.GetInt("ceiling", "0") == 1)
  70. {
  71. idAngles moverAng = mover->GetPhysics()->GetAxis().ToAngles();
  72. moverAng.roll += 180;
  73. mover->SetAngles(moverAng);
  74. }
  75. mover->Hide();
  76. state = IDLE;
  77. nextSparktime = 0;
  78. BecomeActive( TH_THINK );
  79. }
  80. void idPowerSawGeneric::Spawn( void )
  81. {
  82. state = IDLE;
  83. //force a little delay to let all world entities spawn in.
  84. PostEventSec( &EV_powersaw_spawndelay, 0.3f );
  85. }
  86. void idPowerSawGeneric::Think( void )
  87. {
  88. if (gameLocal.time > nextSparktime && state == SAWING)
  89. {
  90. idDict args;
  91. nextSparktime = gameLocal.time + 2500;
  92. args.Clear();
  93. args.SetVector( "origin", this->mover->GetPhysics()->GetOrigin() );
  94. args.Set( "model", "sparkloop.prt" );
  95. args.SetBool( "start_off", false );
  96. sparks = gameLocal.SpawnEntityType( idExplodable::Type, &args );
  97. sparks->Bind(mover, false);
  98. StartSound( "snd_sawhit" , SND_CHANNEL_BODY2, 0, false, NULL );
  99. }
  100. idMover::Think();
  101. }
  102. void idPowerSawGeneric::OnSplineEnd( void )
  103. {
  104. if (state == IDLE)
  105. {
  106. //if the grate is reset during sawing, then we end up here.
  107. return;
  108. }
  109. //BC 7-20-2016 fix saveload crash. do not spline end if it's already done.
  110. if (state == DONE)
  111. {
  112. return;
  113. }
  114. state = DONE;
  115. sparks->PostEventMS( &EV_Remove, 0 );
  116. mover->Hide();
  117. light->Event_Off();
  118. StartSound( "snd_sawend" , SND_CHANNEL_ANY, 0, false, NULL );
  119. StopSound( SND_CHANNEL_BODY2, false );
  120. StopSound( SND_CHANNEL_BODY3, false );
  121. idStr scriptName = spawnArgs.GetString( "call" );
  122. const function_t *scriptFunction;
  123. scriptFunction = gameLocal.program.FindFunction( scriptName );
  124. if ( scriptFunction )
  125. {
  126. idThread *thread;
  127. thread = new idThread( scriptFunction );
  128. thread->DelayedStart( 0 );
  129. }
  130. }
  131. void idPowerSawGeneric::Frob( void )
  132. {
  133. //BC 5-15-2015
  134. //auto detect whether buzzsaw appears on floor or ceiling.
  135. if (spawnArgs.GetInt("ceiling") >= 2)
  136. {
  137. float eye_z = gameLocal.GetLocalPlayer()->GetEyePosition().z;
  138. float grate_z = GetPhysics()->GetOrigin().z;
  139. if (grate_z > eye_z)
  140. {
  141. //ceiling.
  142. idAngles moverAng = mover->GetPhysics()->GetAxis().ToAngles();
  143. moverAng.roll += 180;
  144. mover->SetAngles(moverAng);
  145. }
  146. else
  147. {
  148. //floor.
  149. }
  150. }
  151. this->isFrobbable = false;
  152. //start the mover.
  153. mover->Show();
  154. mover->Event_StartSpline(splineEnt);
  155. light->Event_On();
  156. StartSound( "snd_sawloop" , SND_CHANNEL_BODY3, 0, false, NULL );
  157. state = SAWING;
  158. }
  159. void idPowerSawGeneric::reset( void )
  160. {
  161. state = IDLE;
  162. nextSparktime = 0;
  163. this->isFrobbable = true;
  164. this->Event_StopMoving();
  165. mover->Event_StopMoving();
  166. mover->Hide();
  167. light->Event_Off();
  168. this->GetPhysics()->SetOrigin( this->originalPosition);
  169. this->GetPhysics()->SetAxis( this->originalAngle);
  170. this->UpdateVisuals();
  171. }