cybervendor.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #include "../idlib/precompiled.h"
  2. #pragma hdrstop
  3. #include "Game_local.h"
  4. #define TIME_TO_FRONTMACHINE 400
  5. #define TIME_TO_PAUSE 400
  6. #define TIME_TO_LOOK_UP 500
  7. //const idEventDef EV_ButtonSwitcher_activate( "buttonswitcheractivate", "f" );
  8. CLASS_DECLARATION( idStaticEntity, idCybervendor )
  9. //EVENT( EV_ButtonSwitcher_activate, idButtonSwitcher::Event_buttonswitcheractivate)
  10. END_CLASS
  11. void idCybervendor::Save( idSaveGame *savefile ) const
  12. {
  13. savefile->WriteInt(state);
  14. savefile->WriteInt(nextUpdate);
  15. savefile->WriteInt(lerpStart);
  16. savefile->WriteInt(lerpEnd);
  17. savefile->WriteVec3(posStart);
  18. savefile->WriteVec3(posEnd);
  19. savefile->WriteVec3(angStart);
  20. savefile->WriteVec3(angEnd);
  21. savefile->WriteInt(count);
  22. }
  23. void idCybervendor::Restore( idRestoreGame *savefile )
  24. {
  25. savefile->ReadInt(state);
  26. savefile->ReadInt(nextUpdate);
  27. savefile->ReadInt(lerpStart);
  28. savefile->ReadInt(lerpEnd);
  29. savefile->ReadVec3(posStart);
  30. savefile->ReadVec3(posEnd);
  31. savefile->ReadVec3(angStart);
  32. savefile->ReadVec3(angEnd);
  33. savefile->ReadInt(count);
  34. }
  35. void idCybervendor::Spawn( void )
  36. {
  37. BecomeActive( TH_THINK );
  38. GetPhysics()->SetContents( CONTENTS_SOLID );
  39. GetPhysics()->SetClipMask( MASK_SOLID | CONTENTS_BODY | CONTENTS_CORPSE | CONTENTS_MOVEABLECLIP );
  40. this->noGrab = true;
  41. this->isFrobbable = true;
  42. state = 0;
  43. nextUpdate = 0;
  44. lerpStart = 0;
  45. lerpEnd = 0;
  46. count = 2;
  47. }
  48. void idCybervendor::OnFrob()
  49. {
  50. if (state == 0)
  51. {
  52. float player_z = gameLocal.GetLocalPlayer()->GetPhysics()->GetOrigin().z;
  53. float machine_z = this->GetPhysics()->GetOrigin().z;
  54. if (player_z < machine_z - 4)
  55. {
  56. StartSound( "snd_error", SND_CHANNEL_ANY, 0, false, NULL );
  57. return;
  58. }
  59. count *= 2;
  60. gameLocal.GetLocalPlayer()->Event_setFrozen(1);
  61. isFrobbable = 0;
  62. lerpStart = gameLocal.time;
  63. lerpEnd = gameLocal.time + TIME_TO_FRONTMACHINE; //time to transition to the machine front.
  64. idVec3 forward, right;
  65. GetPhysics()->GetAxis().ToAngles().ToVectors( &forward, &right );
  66. posEnd = this->GetPhysics()->GetOrigin() + (forward * 48) + (right * 12);
  67. posStart = gameLocal.GetLocalPlayer()->GetPhysics()->GetOrigin();
  68. angStart.x = gameLocal.GetLocalPlayer()->viewAngles.pitch;
  69. angStart.y = gameLocal.GetLocalPlayer()->viewAngles.yaw;
  70. angStart.z = gameLocal.GetLocalPlayer()->viewAngles.roll;
  71. if (angStart.y < 0)
  72. angStart.y += 360;
  73. if (angStart.y < 180)
  74. angStart.y += 360;
  75. angEnd.x = GetPhysics()->GetAxis().ToAngles().pitch + 5;
  76. angEnd.y = GetPhysics()->GetAxis().ToAngles().yaw + 180;
  77. angEnd.z = GetPhysics()->GetAxis().ToAngles().roll;
  78. if (angEnd.y < 0)
  79. angEnd.y += 360;
  80. if (angEnd.y < 180)
  81. angEnd.y += 360;
  82. StartSound( "snd_jingle", SND_CHANNEL_ANY, 0, false, NULL );
  83. state = 1;
  84. }
  85. }
  86. void idCybervendor::Think()
  87. {
  88. if (state == 1)
  89. {
  90. //lerp the player to the machine front AND look at the machine front.
  91. float lerp;
  92. float currentTime = gameLocal.time;
  93. float lookTimeMax = this->lerpEnd - this->lerpStart;
  94. float lookTimeMin = this->lerpStart;
  95. currentTime -= this->lerpStart;
  96. lerp = currentTime / lookTimeMax;
  97. idVec3 newPosition;
  98. newPosition.x = idMath::Lerp(posStart.x, posEnd.x, lerp);
  99. newPosition.y = idMath::Lerp(posStart.y, posEnd.y, lerp);
  100. newPosition.z = idMath::Lerp(posStart.z, posEnd.z, lerp);
  101. gameLocal.GetLocalPlayer()->GetPhysics()->SetOrigin(newPosition);
  102. idVec3 lookAng;
  103. lookAng.x = idMath::Lerp(this->angStart.x, this->angEnd.x, lerp);
  104. lookAng.y = idMath::Lerp(this->angStart.y, this->angEnd.y, lerp);
  105. lookAng.z = idMath::Lerp(this->angStart.z, this->angEnd.z, lerp);
  106. //common->Printf("%f : %f : %f %f %f\n", angStart.y, angEnd.y, lookAng.x, lookAng.y, lookAng.z);
  107. gameLocal.GetLocalPlayer()->Event_setviewangle(lookAng);
  108. //common->Printf("%f\n", lerp);
  109. if (lerp >= 1)
  110. {
  111. lerpEnd = gameLocal.time + TIME_TO_PAUSE;
  112. state = 2;
  113. }
  114. }
  115. else if (state == 2)
  116. {
  117. //pause.
  118. if (gameLocal.time >= lerpEnd)
  119. {
  120. angStart.x = gameLocal.GetLocalPlayer()->viewAngles.pitch;
  121. angStart.y = gameLocal.GetLocalPlayer()->viewAngles.yaw;
  122. angStart.z = gameLocal.GetLocalPlayer()->viewAngles.roll;
  123. if (angStart.y < 0)
  124. angStart.y += 360;
  125. if (angStart.y < 180)
  126. angStart.y += 360;
  127. angEnd.x = spawnArgs.GetInt("drillangle", "50");
  128. angEnd.y = GetPhysics()->GetAxis().ToAngles().yaw + 180;
  129. angEnd.z = GetPhysics()->GetAxis().ToAngles().roll;
  130. if (angEnd.y < 0)
  131. angEnd.y += 360;
  132. if (angEnd.y < 180)
  133. angEnd.y += 360;
  134. lerpStart = gameLocal.time;
  135. lerpEnd = gameLocal.time + TIME_TO_LOOK_UP; //time to transition to the machine front.
  136. state = 3;
  137. }
  138. }
  139. else if (state == 3)
  140. {
  141. //look at the hole.
  142. float lerp;
  143. float currentTime = gameLocal.time;
  144. float lookTimeMax = this->lerpEnd - this->lerpStart;
  145. float lookTimeMin = this->lerpStart;
  146. currentTime -= this->lerpStart;
  147. lerp = currentTime / lookTimeMax;
  148. //look up.
  149. idVec3 lookAng;
  150. lookAng.x = idMath::Lerp(this->angStart.x, this->angEnd.x, lerp);
  151. lookAng.y = idMath::Lerp(this->angStart.y, this->angEnd.y, lerp);
  152. lookAng.z = idMath::Lerp(this->angStart.z, this->angEnd.z, lerp);
  153. //common->Printf("%f : %f : %f %f %f\n", angStart.y, angEnd.y, lookAng.x, lookAng.y, lookAng.z);
  154. gameLocal.GetLocalPlayer()->Event_setviewangle(lookAng);
  155. if (lerp >= 1)
  156. {
  157. StartSound( "snd_drill", SND_CHANNEL_ANY, 0, false, NULL );
  158. idVec3 forward, right, up;
  159. GetPhysics()->GetAxis().ToAngles().ToVectors( &forward, &right, &up );
  160. posStart = this->GetPhysics()->GetOrigin() + (forward * 32) + (right * 12) + (up * 55);
  161. gameLocal.GetLocalPlayer()->Event_viewlook(posStart, 0.2f, 0, 1);
  162. lerpEnd = gameLocal.time + 1500;
  163. //gameLocal.GetLocalPlayer()->Event_ToggleBloom(1);
  164. cvarSystem->SetCVarString("g_testpostprocess", "textures/fx/fastforward_fast");
  165. state = 4;
  166. }
  167. }
  168. else if (state == 4)
  169. {
  170. //drilling.
  171. gameLocal.GetLocalPlayer()->Event_setviewangle(angEnd);
  172. if (gameLocal.time >= lerpEnd)
  173. {
  174. gameLocal.GetLocalPlayer()->Event_viewlook(posStart, 0.4f, 1, 0);
  175. StartSound( "snd_ding", SND_CHANNEL_ANY, 0, false, NULL );
  176. angStart.x = gameLocal.GetLocalPlayer()->viewAngles.pitch;
  177. angEnd.x = 20;
  178. angEnd.y = gameLocal.GetLocalPlayer()->viewAngles.yaw;
  179. lerpStart = gameLocal.time;
  180. lerpEnd = gameLocal.time + 300; //time to transition to the machine front.
  181. cvarSystem->SetCVarString("g_testpostprocess", "0");
  182. renderEntity.gui[0]->HandleNamedEvent("done");
  183. //spew some decals into the world.
  184. if (count <= 256)
  185. {
  186. idVec3 forward, right, up;
  187. GetPhysics()->GetAxis().ToAngles().ToVectors( &forward, &right, &up );
  188. idVec3 spitPoint = this->GetPhysics()->GetOrigin() + (forward * 25) + (right * 12) + (up * 128);
  189. int i;
  190. for (i = 0; i < count; i++)
  191. {
  192. idVec3 randSpot;
  193. randSpot = this->GetPhysics()->GetOrigin() + (forward * (25 + gameLocal.random.RandomFloat() * 64)) + (right * (12 - 64 + gameLocal.random.RandomFloat() * 128) ) + (up * 128);
  194. trace_t groundTr;
  195. gameLocal.clip.TracePoint( groundTr, randSpot, randSpot + idVec3(0,0,-1024), MASK_SOLID, this );
  196. if ((randSpot - spitPoint).Normalize() > 64)
  197. continue;
  198. gameLocal.ProjectDecal( groundTr.c.point, -groundTr.c.normal, 0.5f, true, 4 + gameLocal.random.RandomFloat() * 36, spawnArgs.GetString("mtr_oil") );
  199. }
  200. }
  201. state = 5;
  202. }
  203. }
  204. else if (state == 5)
  205. {
  206. //look up.
  207. float lerp;
  208. float currentTime = gameLocal.time;
  209. float lookTimeMax = this->lerpEnd - this->lerpStart;
  210. float lookTimeMin = this->lerpStart;
  211. currentTime -= this->lerpStart;
  212. lerp = currentTime / lookTimeMax;
  213. //look up.
  214. idVec3 lookAng;
  215. lookAng.x = idMath::Lerp(this->angStart.x, this->angEnd.x, lerp);
  216. lookAng.y = angEnd.y;
  217. lookAng.z = 0;
  218. //common->Printf("%f : %f : %f %f %f\n", angStart.y, angEnd.y, lookAng.x, lookAng.y, lookAng.z);
  219. gameLocal.GetLocalPlayer()->Event_setviewangle(lookAng);
  220. if (lerp >= 1)
  221. {
  222. gameLocal.GetLocalPlayer()->Event_setFrozen(0);
  223. isFrobbable = 1;
  224. state = 0;
  225. ActivateTargets( this );
  226. }
  227. }
  228. if (gameLocal.time > nextUpdate && state == 0)
  229. {
  230. //check if player angle faces the FRONT of the machine.
  231. idAngles playerAng = gameLocal.GetLocalPlayer()->viewAngles;
  232. playerAng.pitch = 0;
  233. playerAng.roll = 0;
  234. float vdot = DotProduct( this->GetPhysics()->GetAxis().ToAngles().ToForward(), playerAng.ToForward() );
  235. //check if player is positioned in FRONT of the machine.
  236. idVec3 forward;
  237. GetPhysics()->GetAxis().ToAngles().ToVectors( &forward );
  238. idVec3 frontOfMachinePos = this->GetPhysics()->GetOrigin() + (forward * 24);
  239. idVec3 toPlayer = frontOfMachinePos - gameLocal.GetLocalPlayer()->GetPhysics()->GetOrigin();
  240. idVec3 machineFacing = this->GetPhysics()->GetAxis().ToAngles().ToForward();
  241. float facingResult = DotProduct( toPlayer, machineFacing );
  242. //gameLocal.GetLocalPlayer()->DebugMessage(va("dot %f", facingResult));
  243. if (vdot < -0.1f && facingResult < 0)
  244. {
  245. this->isFrobbable = true;
  246. }
  247. else
  248. {
  249. this->isFrobbable = false;
  250. }
  251. nextUpdate = gameLocal.time + 300; //update every X milliseconds.
  252. }
  253. Present();
  254. }