Guest User

vehicle

a guest
Jun 21st, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. //A script for controlling prop_vehicles
  2. //**All players MUST be removed from their vehicles before a round end or map change or the server will crash!**
  3.  
  4. v_Params <- {}; //misc vehicle info
  5. v_Throttle <- 0.0; //current throttle setting (-1 to 1)
  6. v_Steer <- 0.0; //current steer setting (-1 to 1)
  7. v_Brakes <- false; //handbrake state
  8. v_Locked <- false; //lock state
  9. v_EyeAttach <- Vector(0,0,0); //eye attachment
  10. v_ExitAttach <- 0; //where the player gets out
  11. v_GameUI <- null; //game_ui ent for this vehicle **Must be named "<vehicle ent name>_controls"**
  12. v_Enter <- null; //func_button brush that allows the player to enter the vehicle. **Must be named "<vehicle ent name>_enter"**
  13. v_Driver <- null; //the current player in this vehicle. null if empty.
  14.  
  15. //<output> = [<target entity>,<input>,<parameter>,<delay>,<times to fire>]
  16. v_GameUI_connections <-
  17. {
  18. PressedForward = [self,"RunScriptCode","Throttle(1)",0,-1],
  19. UnpressedForward = [self,"RunScriptCode","Throttle(0)",0,-1],
  20. PressedBack = [self,"RunScriptCode","Throttle(-1)",0,-1],
  21. UnpressedBack = [self,"RunScriptCode","Throttle(0)",0,-1],
  22. PressedMoveLeft = [self,"RunScriptCode","Steer(-1)",0,-1],
  23. PressedMoveRight = [self,"RunScriptCode","Steer(1)",0,-1],
  24. UnpressedMoveLeft = [self,"RunScriptCode","Steer(0)",0,-1],
  25. UnpressedMoveRight = [self,"RunScriptCode","Steer(0)",0,-1],
  26. //PressedAttack2 = [self,"RunScriptCode","Brakes(true)",0,-1],
  27. //UnpressedAttack2 = [self,"RunScriptCode","Brakes(false)",0,-1],
  28. PlayerOff = [self,"RunScriptCode","Exit()",0,-1]
  29. };
  30.  
  31. FirstCall <- true;
  32. function OnPostSpawn()
  33. {
  34. if(FirstCall) //work around for OnPostSpawn getting called twice
  35. {
  36. DoIncludeScript("custom/Util.nut",null);
  37. DoIncludeScript("IOUtil.nut",null);
  38.  
  39. Initialize();
  40.  
  41. printl("***Vehicle script loaded.***"+self.GetName());
  42. }
  43. FirstCall = false;
  44. }
  45. function Think()
  46. {
  47. //foreach(k,v in v_Params.attachments)
  48. //{
  49. // DrawAxis(self.GetAttachmentOrigin(v),4,false,0.12);
  50. //}
  51.  
  52. if(v_Driver != null)
  53. {
  54. if(self.GetUpVector().z < -0.5) //eject the player if the vehicle has flipped over
  55. {
  56. Exit();
  57. }
  58. }
  59. }
  60.  
  61. function Initialize()
  62. {
  63. v_GameUI = Entities.FindByName(null,self.GetName() + "_controls");
  64. if(v_GameUI == null)
  65. {
  66. printl("***game_ui not found for " +self.GetName()+ "!***");
  67. }
  68. else
  69. {
  70. LinkIOTable(v_GameUI,v_GameUI_connections); //link all of the required outputs
  71. }
  72.  
  73. v_Enter = Entities.FindByName(null,self.GetName() + "_enter");
  74. if(v_Enter == null)
  75. {
  76. printl("***entry button not found for " +self.GetName()+ "!***");
  77. }
  78. else
  79. {
  80. LinkIO(v_Enter,"OnIn",self,"RunScriptCode","Enter()",0,-1);
  81. }
  82.  
  83. if(!v_Params.overrideView)
  84. {
  85. v_EyeAttach = self.GetAttachmentOrigin(v_Params.attachments.vehicle_driver_eyes) - self.GetOrigin();
  86. }
  87. else
  88. {
  89. v_Params.viewEnt = Entities.FindByName(null,self.GetName()+"_view");
  90. v_EyeAttach = v_Params.viewEnt.GetOrigin() - self.GetOrigin();
  91. }
  92.  
  93. v_ExitAttach = v_Params.attachments.enter1;
  94. }
  95.  
  96. function Steer(amt)
  97. {
  98. v_Steer = amt;
  99. for(local i = 0;i < 20;i++) //must be spammed 20 times to work right
  100. {
  101. EntFireByHandle(self,"steer",""+v_Steer,0.0,null,null);
  102. }
  103. }
  104.  
  105. function Throttle(amt)
  106. {
  107. v_Throttle = amt;
  108. EntFireByHandle(self,"throttle",""+v_Throttle,0.0,null,null);
  109. }
  110. function Brakes(enabled)
  111. {
  112. v_Brakes = enabled;
  113.  
  114. if(enabled)
  115. {
  116. EntFireByHandle(self,"handbrakeon","",0.0,null,null);
  117. }
  118. else
  119. {
  120. EntFireByHandle(self,"handbrakeoff","",0.0,null,null);
  121. }
  122. }
  123.  
  124. function EnterPlayer(ply) //put a player in this vehicle
  125. {
  126. if(v_Driver ==
  127. zwuwnull && ply.GetClassname() == "player")
  128. {
  129. v_Driver = ply;
  130. EntFireByHandle(v_GameUI,"Activate","",0,v_Driver,null);
  131.  
  132. EntFireByHandle(v_Driver,"setparent",self.GetName(),0.01,null,null);
  133. if(v_Params.teleportPlayer)
  134. {
  135. UpdateEyeAttach();
  136. local vpos = self.GetOrigin()+v_EyeAttach-Vector(0,0,v_Params.viewOffset);
  137.  
  138. EntFireByHandle(v_Driver,"addoutput","origin " +vpos.x+ " " +vpos.y+ " " +vpos.z+ "",0,null,null);
  139. }
  140.  
  141. Brakes(false);
  142. }
  143. }
  144.  
  145. function Enter() //put the !activator in this vehicle
  146. {
  147. EnterPlayer(activator);
  148. }
  149.  
  150. function Exit() //force the player out of this vehicle
  151. {
  152. if(v_Driver != null)
  153. {
  154. EntFireByHandle(v_GameUI,"Deactivate","",0,v_Driver,null);
  155.  
  156. EntFireByHandle(v_Driver,"clearparent",self.GetName(),0,null,null);
  157. if(v_Params.teleportPlayer)
  158. {
  159. local vpos = self.GetAttachmentOrigin(v_ExitAttach)-Vector(0,0,52);
  160.  
  161. EntFireByHandle(v_Driver,"addoutput","origin " +vpos.x+ " " +vpos.y+ " " +vpos.z+ "" ,0,null,null);
  162. }
  163.  
  164. v_Driver = null;
  165. Throttle(0);
  166. Steer(0);
  167. Brakes(true);
  168. }
  169. }
  170.  
  171. function Lock(enabled)
  172. {
  173. v_Locked = enabled;
  174. if(enabled)
  175. {
  176. EntFireByHandle(v_Enter,"lock","",0,null,null);
  177. }
  178. else
  179. {
  180. EntFireByHandle(v_Enter,"unlock","",0,null,null);
  181. }
  182. }
  183.  
  184. function UpdateEyeAttach() //updates the point where the player's eyes are placed upon entering this vehicle
  185. {
  186. if(!v_Params.overrideView)
  187. {
  188. v_EyeAttach = self.GetAttachmentOrigin(v_Params.attachments.vehicle_driver_eyes) - self.GetOrigin();
  189. }
  190. else
  191. {
  192. v_EyeAttach = v_Params.viewEnt.GetOrigin() - self.GetOrigin();
  193. }
  194. }
Add Comment
Please, Sign In to add comment