panel.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. #include "../idlib/precompiled.h"
  2. #pragma hdrstop
  3. #include "Game_local.h"
  4. #define CONNECT_TIME 2000
  5. #define SCREW_DELAY 0.5f
  6. #define CONNECT_DISTANCE 48
  7. #define ATTRACTOR_LIFETIME 1000 //milliseconds for attractor to stay alive.
  8. #define CONNECT_DELAY 400
  9. const idEventDef EV_Panel_OnConnect( "<Onconnect>" );
  10. const idEventDef EV_Panel_FirstOpen( "<Panel_FirstOpen>" );
  11. const idEventDef EV_Panel_Disconnect( "PanelDisconnect" );
  12. const idEventDef EV_Panel_Reset( "PanelReset" );
  13. CLASS_DECLARATION( idAnimatedEntity, idPanel )
  14. EVENT( EV_Panel_OnConnect, idPanel::Event_Panel_OnConnect)
  15. EVENT( EV_Panel_FirstOpen, idPanel::Event_Panel_Open)
  16. EVENT( EV_Panel_Disconnect, idPanel::Event_Panel_Disconnect)
  17. EVENT( EV_Panel_Reset, idPanel::Event_Panel_Reset)
  18. END_CLASS
  19. void idPanel::Save( idSaveGame *savefile ) const
  20. {
  21. savefile->WriteInt(state);
  22. savefile->WriteInt(unscrewedCount);
  23. savefile->WriteObject(relay);
  24. savefile->WriteObject(frobcube0);
  25. savefile->WriteObject(frobcube1);
  26. savefile->WriteObject(frobcube_opened);
  27. savefile->WriteObject(frobcube_closed);
  28. savefile->WriteObject(bundle);
  29. savefile->WriteObject(gatorCable);
  30. savefile->WriteObject(beamStart);
  31. savefile->WriteObject(beamEnd);
  32. savefile->WriteObject(screw0);
  33. savefile->WriteObject(screw1);
  34. }
  35. void idPanel::Restore( idRestoreGame *savefile )
  36. {
  37. savefile->ReadInt(state);
  38. savefile->ReadInt(unscrewedCount);
  39. savefile->ReadObject(reinterpret_cast<idClass *&>(relay));
  40. savefile->ReadObject(reinterpret_cast<idClass *&>(frobcube0));
  41. savefile->ReadObject(reinterpret_cast<idClass *&>(frobcube1));
  42. savefile->ReadObject(reinterpret_cast<idClass *&>(frobcube_opened));
  43. savefile->ReadObject(reinterpret_cast<idClass *&>(frobcube_closed));
  44. savefile->ReadObject(reinterpret_cast<idClass *&>(bundle));
  45. savefile->ReadObject(reinterpret_cast<idClass *&>(gatorCable));
  46. savefile->ReadObject(reinterpret_cast<idClass *&>(beamStart));
  47. savefile->ReadObject(reinterpret_cast<idClass *&>(beamEnd));
  48. savefile->ReadObject(reinterpret_cast<idClass *&>(screw0));
  49. savefile->ReadObject(reinterpret_cast<idClass *&>(screw1));
  50. }
  51. void idPanel::Spawn( void )
  52. {
  53. //SPAWN SCREWS & SCREW FROB CUBES.
  54. idVec3 screwPos0, screwPos1;
  55. idVec3 forward, right, up;
  56. this->GetPhysics()->GetAxis().ToAngles().ToVectors(&forward, &right, &up);
  57. screwPos0 = GetPhysics()->GetOrigin() + (right * -6.8f) + (up * 6.8f);
  58. idDict args;
  59. args.Clear();
  60. args.SetVector( "origin", screwPos0 );
  61. args.Set( "model", "models/frob/cube_8x8.ase" );
  62. args.SetInt( "frobbable", 1 );
  63. args.SetInt( "corpse", 1 );
  64. args.SetInt( "noGrab", 1 );
  65. args.Set( "owner", this->GetName() );
  66. args.SetInt( "index", IDX_SCREW0 );
  67. args.SetInt( "role", this->spawnArgs.GetInt("role"));
  68. args.SetInt( "recordable", this->spawnArgs.GetInt("recordable") );
  69. this->frobcube0 = gameLocal.SpawnEntityType( idStaticEntity::Type, &args );
  70. screwPos1 = GetPhysics()->GetOrigin() + (right * 6.8f) + (up * 6.8f);
  71. args.Clear();
  72. args.SetVector( "origin", screwPos1 );
  73. args.Set( "model", "models/frob/cube_8x8.ase" );
  74. args.SetInt( "frobbable", 1 );
  75. args.SetInt( "corpse", 1 );
  76. args.SetInt( "noGrab", 1 );
  77. args.Set( "owner", this->GetName() );
  78. args.SetInt( "index", IDX_SCREW1 );
  79. args.SetInt( "role", this->spawnArgs.GetInt("role"));
  80. args.SetInt( "recordable", this->spawnArgs.GetInt("recordable") );
  81. this->frobcube1 = gameLocal.SpawnEntityType( idStaticEntity::Type, &args );
  82. args.Clear();
  83. args.SetVector( "origin", screwPos0 );
  84. args.Set( "classname", "env_screwanim" );
  85. gameLocal.SpawnEntityDef( args, &this->screw0 );
  86. this->screw0->SetAngles( GetPhysics()->GetAxis().ToAngles() );
  87. args.Clear();
  88. args.SetVector( "origin", screwPos1 );
  89. args.Set( "classname", "env_screwanim" );
  90. gameLocal.SpawnEntityDef( args, &this->screw1 );
  91. this->screw1->SetAngles( GetPhysics()->GetAxis().ToAngles() );
  92. args.Clear();
  93. args.SetVector( "origin", GetPhysics()->GetOrigin() + (forward * -3) + (up * 2) );
  94. args.Set( "model", "models/wirebundle/wirebundle_16x3.ase" );
  95. args.Set( "clipmodel", "models/wirebundle/wirebundle_16x3_cm.ase" );
  96. args.SetInt( "frobbable", 1 );
  97. args.SetInt( "corpse", 1 );
  98. args.SetInt( "noGrab", 1 );
  99. args.SetInt( "solid", 0 );
  100. args.Set( "owner", this->GetName() );
  101. args.SetInt( "index", IDX_BUNDLE );
  102. args.SetInt( "role", this->spawnArgs.GetInt("role1"));
  103. //args.SetInt( "recordable", this->spawnArgs.GetInt("recordable") );
  104. this->bundle = gameLocal.SpawnEntityType( idStaticEntity::Type, &args );
  105. this->bundle->SetAngles( GetPhysics()->GetAxis().ToAngles() );
  106. this->bundle->GetPhysics()->SetContents( 0 );
  107. args.Clear();
  108. args.SetVector( "origin", GetPhysics()->GetOrigin() );
  109. args.Set( "model", "models/panel_16x16/tris_closed_cm.ase" );
  110. args.SetInt( "frobbable", 1 );
  111. args.SetInt( "corpse", 1 );
  112. args.SetInt( "solid", 0 );
  113. args.SetInt( "noGrab", 1 );
  114. args.Set( "owner", this->GetName() );
  115. args.SetInt( "index", IDX_DOOR_CLOSED );
  116. this->frobcube_closed = gameLocal.SpawnEntityType( idStaticEntity::Type, &args );
  117. this->frobcube_closed->SetAngles( GetPhysics()->GetAxis().ToAngles() );
  118. this->frobcube_closed->GetPhysics()->SetContents( 0 );
  119. args.Clear();
  120. args.SetVector( "origin", GetPhysics()->GetOrigin() );
  121. args.Set( "model", "models/panel_16x16/tris_open_cm.ase" );
  122. args.SetInt( "frobbable", 1 );
  123. args.SetInt( "corpse", 1 );
  124. args.SetInt( "solid", 0 );
  125. args.SetInt( "noGrab", 1 );
  126. args.Set( "owner", this->GetName() );
  127. args.SetInt( "index", IDX_DOOR_OPENED );
  128. this->frobcube_opened = gameLocal.SpawnEntityType( idStaticEntity::Type, &args );
  129. this->frobcube_opened->SetAngles( GetPhysics()->GetAxis().ToAngles() );
  130. this->frobcube_opened->GetPhysics()->SetContents( 0 );
  131. args.Clear();
  132. args.Set("classname", "trigger_relay" );
  133. args.Set("target", this->spawnArgs.GetString("target"));
  134. args.Set("target0", this->spawnArgs.GetString("target0"));
  135. gameLocal.SpawnEntityDef(args, &relay);
  136. //INITIAL SETTINGS.
  137. unscrewedCount = 0;
  138. BecomeActive( TH_THINK );
  139. this->state = IDLE;
  140. this->gatorCable = NULL;
  141. }
  142. void idPanel::Event_Panel_Reset( void )
  143. {
  144. //Reset.
  145. CancelEvents( &EV_Panel_OnConnect );
  146. CancelEvents( &EV_Panel_FirstOpen );
  147. this->frobcube_closed->GetPhysics()->SetContents( 0 );
  148. this->frobcube_opened->GetPhysics()->SetContents( 0 );
  149. Event_Panel_Disconnect();
  150. //delete cable.
  151. if (this->gatorCable != NULL)
  152. {
  153. this->gatorCable->PostEventMS( &EV_Remove, 0 );
  154. this->gatorCable = NULL;
  155. }
  156. //reset screws.
  157. gameLocal.GetLocalPlayer()->UseFrob( screw0, "reset" );
  158. gameLocal.GetLocalPlayer()->UseFrob( screw1, "reset" );
  159. this->frobcube0->GetPhysics()->SetContents( CONTENTS_CORPSE );
  160. this->frobcube1->GetPhysics()->SetContents( CONTENTS_CORPSE );
  161. //reset anim.
  162. Event_PlayAnim("closed", 4);
  163. //settings.
  164. unscrewedCount = 0;
  165. this->bundle->GetPhysics()->SetContents( 0 );
  166. }
  167. void idPanel::Event_Panel_OnConnect( void )
  168. {
  169. if (spawnArgs.GetBool("recordable") > 0)
  170. {
  171. static_cast<idTrigger_Multi *>( relay )->Event_Trigger(this);
  172. }
  173. else
  174. {
  175. ActivateTargets( this );
  176. }
  177. }
  178. void idPanel::Event_Panel_Disconnect( void )
  179. {
  180. CancelEvents( &EV_Panel_OnConnect );
  181. if (this->gatorCable != NULL)
  182. {
  183. //frobbed the cable. If connected to deck, then disconnect.
  184. this->gatorCable->SetSkin(declManager->FindSkin("skins/cable/phone"));
  185. }
  186. if (this->state == CONNECTED)
  187. {
  188. //disconnect the deck.
  189. this->state = IDLE;
  190. idEntity* deck = gameLocal.FindEntityUsingDef(NULL, "moveable_item_deck");
  191. if (deck)
  192. {
  193. deck->spawnArgs.SetInt("phone", 0);
  194. gameLocal.GetLocalPlayer()->UseFrob( deck, "PhoneOff" );
  195. deck->StopSound( SND_CHANNEL_VOICE, false );
  196. deck->StopSound( SND_CHANNEL_VOICE2, false );
  197. }
  198. if (this->gatorCable != NULL)
  199. {
  200. //make the cable jump a little.
  201. this->gatorCable->ApplyImpulse( this->gatorCable, 13 , gatorCable->GetPhysics()->GetOrigin(), idVec3(0, 0, 4096));
  202. }
  203. }
  204. }
  205. void idPanel::Event_Panel_Open( void )
  206. {
  207. this->frobcube_opened->GetPhysics()->SetContents( CONTENTS_CORPSE );
  208. this->frobcube_closed->GetPhysics()->SetContents( 0 );
  209. this->bundle->GetPhysics()->SetContents( CONTENTS_CORPSE );
  210. Event_PlayAnim("opening", 4);
  211. this->StartSound( "snd_open" , SND_CHANNEL_BODY, 0, false, NULL );
  212. }
  213. void idPanel::Event_Panel_Close( void )
  214. {
  215. Event_Panel_Disconnect();
  216. if (this->gatorCable != NULL)
  217. {
  218. this->gatorCable->PostEventMS( &EV_Remove, 0 );
  219. this->gatorCable = NULL;
  220. this->state = IDLE;
  221. }
  222. this->frobcube_opened->GetPhysics()->SetContents( 0 );
  223. this->frobcube_closed->GetPhysics()->SetContents( CONTENTS_CORPSE );
  224. this->bundle->GetPhysics()->SetContents( 0 );
  225. Event_PlayAnim("closing", 4);
  226. this->StartSound( "snd_close" , SND_CHANNEL_BODY, 0, false, NULL );
  227. }
  228. void idPanel::OnFrob( idEntity* activator )
  229. {
  230. int index;
  231. if (!activator->spawnArgs.GetInt( "index", "", index))
  232. {
  233. return;
  234. }
  235. if (index == IDX_DOOR_OPENED)
  236. {
  237. //is opened. Close it.
  238. Event_Panel_Close();
  239. return;
  240. }
  241. if (index == IDX_DOOR_CLOSED)
  242. {
  243. //is opened. Close it.
  244. Event_Panel_Open();
  245. return;
  246. }
  247. //frob screws.
  248. if (index == IDX_SCREW0 || index == IDX_SCREW1)
  249. {
  250. activator->Hide();
  251. unscrewedCount++;
  252. if (index == IDX_SCREW0)
  253. {
  254. gameLocal.GetLocalPlayer()->UseFrob( this->screw0, "playanim" );
  255. }
  256. else
  257. {
  258. gameLocal.GetLocalPlayer()->UseFrob( this->screw1, "playanim" );
  259. }
  260. if (unscrewedCount >= 2)
  261. {
  262. PostEventSec( &EV_Panel_FirstOpen, SCREW_DELAY );
  263. }
  264. return;
  265. }
  266. if (index == IDX_CABLE)
  267. {
  268. Event_Panel_Disconnect();
  269. return;
  270. }
  271. if (index == IDX_BUNDLE)
  272. {
  273. //frobbed the wire bundle.
  274. if (this->gatorCable == NULL)
  275. {
  276. idAngles cableAngle;
  277. idVec3 forward, right, up;
  278. idDict args;
  279. idVec3 cablePos;
  280. //this->connectTime = gameLocal.time;
  281. this->StartSound( "snd_plug" , SND_CHANNEL_VOICE, 0, false, NULL );
  282. //turn off bundle frobbability.
  283. this->bundle->GetPhysics()->SetContents( 0 );
  284. this->GetPhysics()->GetAxis().ToAngles().ToVectors(&forward, &right, &up);
  285. //Spawn the cable.
  286. cablePos = this->bundle->GetPhysics()->GetOrigin() + (forward * 6) + (up * 2);
  287. args.Clear();
  288. args.SetVector( "origin", cablePos );
  289. args.Set( "classname", "env_cable_gator" );
  290. args.Set( "owner", this->GetName() );
  291. args.SetInt( "index", IDX_CABLE );
  292. gameLocal.SpawnEntityDef( args, &this->gatorCable );
  293. cableAngle = this->GetPhysics()->GetAxis().ToAngles();
  294. cableAngle.pitch += 100;
  295. this->gatorCable->SetAngles(cableAngle);
  296. args.Clear();
  297. args.SetVector( "origin", this->bundle->GetPhysics()->GetOrigin() );
  298. args.Set( "model", "sparkburst2.prt" );
  299. args.SetBool( "start_off", false );
  300. gameLocal.SpawnEntityType( idExplodable::Type, &args );
  301. /*
  302. const idDeclParticle *smokeFly = NULL;
  303. smokeFly = static_cast<const idDeclParticle *>( declManager->FindType( DECL_PARTICLE, "sparkburst2.prt" ) );
  304. if (smokeFly)
  305. {
  306. gameLocal.smokeParticles->EmitSmoke( smokeFly, gameLocal.time, 0, cablePos, mat3_zero, timeGroup );
  307. }
  308. */
  309. idVec3 offset;
  310. idMat3 axis;
  311. jointHandle_t jointHandle;
  312. jointHandle = this->gatorCable->GetAnimator()->GetJointHandle( "bone16" );
  313. if ( static_cast< idAnimatedEntity * >(gatorCable)->GetJointWorldTransform(jointHandle, gameLocal.time, offset, axis ) )
  314. {
  315. args.Clear();
  316. args.SetVector( "origin", offset );
  317. args.Set( "target", "deck1_beamend" );
  318. args.SetBool( "start_off", false );
  319. args.Set( "skin", "skins/beam_cable" );
  320. this->beamStart = ( idBeam * )gameLocal.SpawnEntityType( idBeam::Type, &args );
  321. this->beamStart->BindToJoint(this->gatorCable, "bone16", false);
  322. }
  323. gameLocal.GetLocalPlayer()->Event_usePicker(this->gatorCable);
  324. }
  325. return;
  326. }
  327. }
  328. void idPanel::Think( void )
  329. {
  330. idEntity* deck = gameLocal.FindEntityUsingDef(NULL, "moveable_item_deck");
  331. if (this->gatorCable != NULL && deck != NULL && state == IDLE)
  332. {
  333. idVec3 deckForward, deckRight, deckUp;
  334. idVec3 connectPos, phonePos;
  335. float distance;
  336. idMat3 axis;
  337. jointHandle_t jointHandle;
  338. trace_t tr;
  339. if (deck->spawnArgs.GetInt("phone", "0") > 0)
  340. {
  341. return;
  342. }
  343. //ignore if player is holding the deck.
  344. if (gameLocal.GetLocalPlayer()->Event_isHolding(deck))
  345. {
  346. return;
  347. }
  348. jointHandle = this->gatorCable->GetAnimator()->GetJointHandle( "bone16" );
  349. if ( !static_cast< idAnimatedEntity * >(gatorCable)->GetJointWorldTransform(jointHandle, gameLocal.time, phonePos, axis ) )
  350. {
  351. gameLocal.Warning( "Joint # %d out of range on entity '%s'", jointHandle, name.c_str() );
  352. return;
  353. }
  354. //gameRenderWorld->DebugCircle( colorWhite, offset, idVec3(0,0,1), 2, 5, 200);
  355. deck->GetPhysics()->GetAxis().ToAngles().ToVectors(&deckForward, &deckRight, &deckUp);
  356. connectPos = deck->GetPhysics()->GetOrigin() + (deckRight * 8) + (deckUp * 3);
  357. //Check LOS between deck and coupler.
  358. gameLocal.clip.TracePoint( tr, phonePos, connectPos, MASK_SOLID, this );
  359. if (gameLocal.GetLocalPlayer()->Event_isHolding(this->gatorCable))
  360. {
  361. if (tr.fraction < 1)
  362. {
  363. this->beamStart->Hide();
  364. }
  365. else if (this->beamStart->IsHidden())
  366. {
  367. this->beamStart->Show();
  368. }
  369. return;
  370. }
  371. if (!this->beamStart->IsHidden())
  372. {
  373. this->beamStart->Hide();
  374. }
  375. //if (gameLocal.time > this->connectTime + CONNECT_DELAY)
  376. {
  377. //Check distance between coupler and deck.
  378. distance = (phonePos - connectPos).LengthFast();
  379. //gameRenderWorld->DebugCircle( colorGreen, connectPos, idVec3(0,0,1), 2, 5, 200);
  380. if (CONNECT_DISTANCE >= distance)
  381. {
  382. deck->StartSound( "snd_plug" , SND_CHANNEL_VOICE, 0, false, NULL );
  383. deck->StartSound( "snd_phonedial" , SND_CHANNEL_VOICE2, 0, false, NULL );
  384. gameLocal.GetLocalPlayer()->UseFrob( deck, "PhoneOn" );
  385. this->gatorCable->SetSkin(declManager->FindSkin("skins/cable/nophone"));
  386. this->state = CONNECTED;
  387. //Spawn attractor for cable.
  388. idDict args;
  389. args.Clear();
  390. args.SetVector( "origin", deck->GetPhysics()->GetOrigin() + (deckRight * 10) + (deckUp * 1) + (deckForward * -4));
  391. args.Set( "joint", "bone14" );
  392. args.Set( "entity", this->gatorCable->GetName() );
  393. idEntity *attractor = gameLocal.SpawnEntityType( idAttractor::Type, &args );
  394. attractor->Bind(deck, false);
  395. attractor->PostEventMS( &EV_Remove, ATTRACTOR_LIFETIME );
  396. this->PostEventMS( &EV_Panel_OnConnect, CONNECT_TIME );
  397. const char *smokeName = "tinyburst.prt";
  398. if ( *smokeName != '\0' )
  399. {
  400. const idDeclParticle *smokeFly = NULL;
  401. smokeFly = static_cast<const idDeclParticle *>( declManager->FindType( DECL_PARTICLE, smokeName ) );
  402. if (smokeFly)
  403. {
  404. gameLocal.smokeParticles->EmitSmoke( smokeFly, gameLocal.time, 0,
  405. connectPos, mat3_zero, timeGroup );
  406. }
  407. }
  408. }
  409. }
  410. }
  411. //
  412. //delete this;
  413. Present();
  414. }
  415. /*
  416. ================
  417. Present
  418. Present is called to allow entities to generate refEntities, lights, etc for the renderer.
  419. ================
  420. */
  421. void idPanel::Present( void )
  422. {
  423. // don't present to the renderer if the entity hasn't changed
  424. if ( !( thinkFlags & TH_UPDATEVISUALS ) )
  425. {
  426. return;
  427. }
  428. BecomeInactive( TH_UPDATEVISUALS );
  429. // if set to invisible, skip
  430. if ( !renderEntity.hModel || IsHidden() )
  431. {
  432. return;
  433. }
  434. // add to refresh list
  435. if ( modelDefHandle == -1 )
  436. {
  437. modelDefHandle = gameRenderWorld->AddEntityDef( &renderEntity );
  438. }
  439. else
  440. {
  441. gameRenderWorld->UpdateEntityDef( modelDefHandle, &renderEntity );
  442. }
  443. }