itemgate.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "../idlib/precompiled.h"
  2. #pragma hdrstop
  3. #include "Game_local.h"
  4. const idEventDef EV_staticactivate( "staticactivate", "d" );
  5. CLASS_DECLARATION( idTrigger, idItemGate )
  6. EVENT( EV_Touch, idItemGate::Event_Touch )
  7. EVENT( EV_staticactivate, idItemGate::Event_staticactivate)
  8. END_CLASS
  9. void idItemGate::Save( idSaveGame *savefile ) const
  10. {
  11. savefile->WriteInt(nextTriggerTime);
  12. savefile->WriteVec3(throwDir);
  13. savefile->WriteString(className);
  14. savefile->WriteString(weaponName);
  15. savefile->WriteString(callName);
  16. savefile->WriteInt(force);
  17. savefile->WriteBool(hasFoundTarget);
  18. savefile->WriteBool(isOn);
  19. savefile->WriteVec3(throwDestination);
  20. savefile->WriteInt(nextHitSoundTime);
  21. }
  22. void idItemGate::Restore( idRestoreGame *savefile )
  23. {
  24. savefile->ReadInt(nextTriggerTime);
  25. savefile->ReadVec3(throwDir);
  26. savefile->ReadString(className);
  27. savefile->ReadString(weaponName);
  28. savefile->ReadString(callName);
  29. savefile->ReadInt(force);
  30. savefile->ReadBool(hasFoundTarget);
  31. savefile->ReadBool(isOn);
  32. savefile->ReadVec3(throwDestination);
  33. savefile->ReadInt(nextHitSoundTime);
  34. }
  35. void idItemGate::Spawn( void )
  36. {
  37. className = spawnArgs.GetString( "class" );
  38. if ( !className.Length() )
  39. {
  40. gameLocal.Error( "idItemGate '%s' at (%s) doesn't have 'classname' key specified", name.c_str(), GetPhysics()->GetOrigin().ToString(0) );
  41. }
  42. weaponName = spawnArgs.GetString( "weapon" );
  43. if ( !weaponName.Length() )
  44. {
  45. gameLocal.Error( "idItemGate '%s' at (%s) doesn't have 'weapon' key specified", name.c_str(), GetPhysics()->GetOrigin().ToString(0) );
  46. }
  47. callName = spawnArgs.GetString( "callname" );
  48. force = spawnArgs.GetInt( "force", "160" );
  49. nextTriggerTime = 0;
  50. GetPhysics()->SetContents( CONTENTS_TRIGGER );
  51. StartSound( "snd_hum", SND_CHANNEL_BODY, 0, false, NULL );
  52. isOn = true;
  53. hasFoundTarget = false;
  54. nextHitSoundTime = 0;
  55. BecomeActive( TH_THINK );
  56. }
  57. void idItemGate::Think( void )
  58. {
  59. //11-4-2015
  60. //why does the trigger spin in circles sometimes?
  61. //why does adding this empty function fix this bug?
  62. //??????????
  63. }
  64. void idItemGate::Event_staticactivate( int value )
  65. {
  66. isOn = value;
  67. if (value >= 1)
  68. {
  69. StartSound( "snd_hum", SND_CHANNEL_BODY, 0, false, NULL );
  70. }
  71. else
  72. {
  73. StopSound( SND_CHANNEL_BODY, false );
  74. }
  75. }
  76. void idItemGate::repel( idEntity *ent )
  77. {
  78. idDict args;
  79. ent->GetPhysics()->SetLinearVelocity(throwDir * force);
  80. if (gameLocal.time > nextHitSoundTime)
  81. {
  82. StartSound( "snd_hit", SND_CHANNEL_ANY, 0, false, NULL );
  83. nextHitSoundTime = gameLocal.time + 1500;
  84. }
  85. args.Clear();
  86. args.SetVector( "origin", ent->GetPhysics()->GetOrigin() );
  87. args.Set( "model", "magnetfire.prt" );
  88. args.SetBool( "start_off", false );
  89. gameLocal.SpawnEntityType( idExplodable::Type, &args );
  90. }
  91. void idItemGate::Event_Touch( idEntity *other, trace_t *trace )
  92. {
  93. if (!isOn || g_skill.GetInteger() <= 0)
  94. {
  95. return;
  96. }
  97. if (!hasFoundTarget)
  98. {
  99. FindTargets();
  100. RemoveNullTargets();
  101. if ( !targets.Num() )
  102. {
  103. gameLocal.Error( "idItemGate '%s' at (%s) doesn't have 'target' key specified", name.c_str(), GetPhysics()->GetOrigin().ToString(0) );
  104. }
  105. throwDir = targets[0].GetEntity()->GetPhysics()->GetOrigin() - GetPhysics()->GetOrigin();
  106. throwDir.Normalize();
  107. throwDestination = targets[0].GetEntity()->GetPhysics()->GetOrigin();
  108. hasFoundTarget = true;
  109. }
  110. if ( nextTriggerTime > gameLocal.time )
  111. {
  112. // can't retrigger until the wait is over
  113. return;
  114. }
  115. if ( !other )
  116. {
  117. return;
  118. }
  119. nextTriggerTime = gameLocal.time + 1;
  120. //if player is holding item, then force player to drop the item.
  121. if (gameLocal.GetLocalPlayer()->pickerWeapon.dragEnt.IsValid())
  122. {
  123. if (gameLocal.GetLocalPlayer()->pickerWeapon.dragEnt.GetEntity()->spawnArgs.GetString("classname") == className)
  124. {
  125. idEntity *dragee;
  126. dragee = gameLocal.GetLocalPlayer()->pickerWeapon.dragEnt.GetEntity();
  127. gameLocal.GetLocalPlayer()->pickerWeapon.StopDrag(true, false);
  128. repel(dragee);
  129. return;
  130. }
  131. }
  132. //if in inventory, then remove item from inventory and spawn into world.
  133. if (gameLocal.GetLocalPlayer()->Event_hasWeapon(weaponName))
  134. {
  135. if (gameLocal.GetLocalPlayer()->RemoveWeaponItem(weaponName))
  136. {
  137. idVec3 spawnPos;
  138. idAngles forward;
  139. trace_t tr;
  140. //BC 8-20-2015 if player has the weapon currently selected, then switch to a different weapon.
  141. if (idStr::Icmp( weaponName, gameLocal.GetLocalPlayer()->GetCurrentWeapon()) == 0)
  142. {
  143. gameLocal.GetLocalPlayer()->SelectWeapon(0, true); //select unarmed weapon.
  144. }
  145. //BC 10-20-2015 simplify this to just spawn the item in the middle of the trigger.
  146. //BC 11-4-2015 allow for manually-placed spawnpoint for the spawned item.
  147. idStr spawnpointname = spawnArgs.GetString( "spawnpoint", "" );
  148. if (spawnpointname.Length())
  149. {
  150. idEntity *spawnEnt = gameLocal.FindEntity(spawnpointname.c_str());
  151. if (spawnEnt)
  152. {
  153. spawnPos = spawnEnt->GetPhysics()->GetOrigin();
  154. }
  155. }
  156. else
  157. {
  158. //center of the bounding box.
  159. spawnPos = this->GetPhysics()->GetAbsBounds().GetCenter();
  160. }
  161. //weapon removed. now spawn it.
  162. //first, determine whether the item already exists in the world. (is this case specific to the deck?)
  163. gameLocal.GetLocalPlayer()->Event_hudMessage( common->GetLanguageDict()->GetString( "#str_02118" ) );
  164. idEntity *launchee = gameLocal.FindEntityUsingDef(NULL, className);
  165. if (launchee)
  166. {
  167. launchee->SetOrigin( spawnPos );
  168. if (callName)
  169. {
  170. gameLocal.GetLocalPlayer()->UseFrob( launchee, callName );
  171. }
  172. repel(launchee);
  173. return;
  174. }
  175. else
  176. {
  177. //doesn't exist. spawn it.
  178. idDict args;
  179. idEntity *spawnee;
  180. args.Clear();
  181. args.SetVector( "origin", spawnPos );
  182. gameLocal.SpawnEntityDef( args, &spawnee );
  183. if (callName)
  184. {
  185. gameLocal.GetLocalPlayer()->UseFrob( spawnee, callName );
  186. }
  187. repel(spawnee);
  188. return;
  189. }
  190. }
  191. }
  192. if ( other->spawnArgs.GetString("classname") == className )
  193. {
  194. //launch the item.
  195. repel(other);
  196. }
  197. }