Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //A script for controlling prop_vehicles
- //**All players MUST be removed from their vehicles before a round end or map change or the server will crash!**
- v_Params <- {}; //misc vehicle info
- v_Throttle <- 0.0; //current throttle setting (-1 to 1)
- v_Steer <- 0.0; //current steer setting (-1 to 1)
- v_Brakes <- false; //handbrake state
- v_Locked <- false; //lock state
- v_EyeAttach <- Vector(0,0,0); //eye attachment
- v_ExitAttach <- 0; //where the player gets out
- v_GameUI <- null; //game_ui ent for this vehicle **Must be named "<vehicle ent name>_controls"**
- v_Enter <- null; //func_button brush that allows the player to enter the vehicle. **Must be named "<vehicle ent name>_enter"**
- v_Driver <- null; //the current player in this vehicle. null if empty.
- //<output> = [<target entity>,<input>,<parameter>,<delay>,<times to fire>]
- v_GameUI_connections <-
- {
- PressedForward = [self,"RunScriptCode","Throttle(1)",0,-1],
- UnpressedForward = [self,"RunScriptCode","Throttle(0)",0,-1],
- PressedBack = [self,"RunScriptCode","Throttle(-1)",0,-1],
- UnpressedBack = [self,"RunScriptCode","Throttle(0)",0,-1],
- PressedMoveLeft = [self,"RunScriptCode","Steer(-1)",0,-1],
- PressedMoveRight = [self,"RunScriptCode","Steer(1)",0,-1],
- UnpressedMoveLeft = [self,"RunScriptCode","Steer(0)",0,-1],
- UnpressedMoveRight = [self,"RunScriptCode","Steer(0)",0,-1],
- //PressedAttack2 = [self,"RunScriptCode","Brakes(true)",0,-1],
- //UnpressedAttack2 = [self,"RunScriptCode","Brakes(false)",0,-1],
- PlayerOff = [self,"RunScriptCode","Exit()",0,-1]
- };
- FirstCall <- true;
- function OnPostSpawn()
- {
- if(FirstCall) //work around for OnPostSpawn getting called twice
- {
- DoIncludeScript("custom/Util.nut",null);
- DoIncludeScript("IOUtil.nut",null);
- Initialize();
- printl("***Vehicle script loaded.***"+self.GetName());
- }
- FirstCall = false;
- }
- function Think()
- {
- //foreach(k,v in v_Params.attachments)
- //{
- // DrawAxis(self.GetAttachmentOrigin(v),4,false,0.12);
- //}
- if(v_Driver != null)
- {
- if(self.GetUpVector().z < -0.5) //eject the player if the vehicle has flipped over
- {
- Exit();
- }
- }
- }
- function Initialize()
- {
- v_GameUI = Entities.FindByName(null,self.GetName() + "_controls");
- if(v_GameUI == null)
- {
- printl("***game_ui not found for " +self.GetName()+ "!***");
- }
- else
- {
- LinkIOTable(v_GameUI,v_GameUI_connections); //link all of the required outputs
- }
- v_Enter = Entities.FindByName(null,self.GetName() + "_enter");
- if(v_Enter == null)
- {
- printl("***entry button not found for " +self.GetName()+ "!***");
- }
- else
- {
- LinkIO(v_Enter,"OnIn",self,"RunScriptCode","Enter()",0,-1);
- }
- if(!v_Params.overrideView)
- {
- v_EyeAttach = self.GetAttachmentOrigin(v_Params.attachments.vehicle_driver_eyes) - self.GetOrigin();
- }
- else
- {
- v_Params.viewEnt = Entities.FindByName(null,self.GetName()+"_view");
- v_EyeAttach = v_Params.viewEnt.GetOrigin() - self.GetOrigin();
- }
- v_ExitAttach = v_Params.attachments.enter1;
- }
- function Steer(amt)
- {
- v_Steer = amt;
- for(local i = 0;i < 20;i++) //must be spammed 20 times to work right
- {
- EntFireByHandle(self,"steer",""+v_Steer,0.0,null,null);
- }
- }
- function Throttle(amt)
- {
- v_Throttle = amt;
- EntFireByHandle(self,"throttle",""+v_Throttle,0.0,null,null);
- }
- function Brakes(enabled)
- {
- v_Brakes = enabled;
- if(enabled)
- {
- EntFireByHandle(self,"handbrakeon","",0.0,null,null);
- }
- else
- {
- EntFireByHandle(self,"handbrakeoff","",0.0,null,null);
- }
- }
- function EnterPlayer(ply) //put a player in this vehicle
- {
- if(v_Driver ==
- zwuwnull && ply.GetClassname() == "player")
- {
- v_Driver = ply;
- EntFireByHandle(v_GameUI,"Activate","",0,v_Driver,null);
- EntFireByHandle(v_Driver,"setparent",self.GetName(),0.01,null,null);
- if(v_Params.teleportPlayer)
- {
- UpdateEyeAttach();
- local vpos = self.GetOrigin()+v_EyeAttach-Vector(0,0,v_Params.viewOffset);
- EntFireByHandle(v_Driver,"addoutput","origin " +vpos.x+ " " +vpos.y+ " " +vpos.z+ "",0,null,null);
- }
- Brakes(false);
- }
- }
- function Enter() //put the !activator in this vehicle
- {
- EnterPlayer(activator);
- }
- function Exit() //force the player out of this vehicle
- {
- if(v_Driver != null)
- {
- EntFireByHandle(v_GameUI,"Deactivate","",0,v_Driver,null);
- EntFireByHandle(v_Driver,"clearparent",self.GetName(),0,null,null);
- if(v_Params.teleportPlayer)
- {
- local vpos = self.GetAttachmentOrigin(v_ExitAttach)-Vector(0,0,52);
- EntFireByHandle(v_Driver,"addoutput","origin " +vpos.x+ " " +vpos.y+ " " +vpos.z+ "" ,0,null,null);
- }
- v_Driver = null;
- Throttle(0);
- Steer(0);
- Brakes(true);
- }
- }
- function Lock(enabled)
- {
- v_Locked = enabled;
- if(enabled)
- {
- EntFireByHandle(v_Enter,"lock","",0,null,null);
- }
- else
- {
- EntFireByHandle(v_Enter,"unlock","",0,null,null);
- }
- }
- function UpdateEyeAttach() //updates the point where the player's eyes are placed upon entering this vehicle
- {
- if(!v_Params.overrideView)
- {
- v_EyeAttach = self.GetAttachmentOrigin(v_Params.attachments.vehicle_driver_eyes) - self.GetOrigin();
- }
- else
- {
- v_EyeAttach = v_Params.viewEnt.GetOrigin() - self.GetOrigin();
- }
- }
Add Comment
Please, Sign In to add comment