VehicleTypes["Jeep"] = {                 -- The global reference to this type.
Name = "Jeep",                           -- The display name on vehicle menu.
Model = "models/props_vehicles/car002a.mdl",  -- Model. If none, the vehicle will not create.
Description = "Fast attack vehicle with machine gunner seat.",   -- Shown on the vehicle menu.
Stock = 2,                                -- How many each team can have at one time.
EntityType = "prop_vehicle_jeep",              -- _EntCreate( ) type. Default is "prop_physics", if not specified.
MaxHealth = 100,                          -- Max HP before destroyed.
DamageMultiply = 0.35,                    -- Damage taken is multiplyed by this amount.
PassengerSlots = 1,                       -- Amount of passengers able to ride in this vehicle.
DriverCamDistance = 256,                  -- Camera distance from vehicle for the driver.
PassengerCamDistance = 256,               -- Camera distance from vehicle for passengers.
CreationOffset = vector3(0,0,60),         -- Offset from vehicle pads.
ExitOffset = vector3(0,0,40),             -- Offset from vehicle that people exit at.

HUD = function(vehicle)
	local SendTo = {}
	if Vehicles[vehicle].Driver > 0 then
		SendTo[table.getn(SendTo)+1] = Vehicles[vehicle].Driver
	end
	for i=1, table.getn(Vehicles[vehicle].Passenger) do
		if Vehicles[vehicle].Passenger[i] > 0 then
			SendTo[table.getn(SendTo)+1] = Vehicles[vehicle].Passenger[i]
		end
	end

	local ms = "D:* GNR1:_ GNR2:_"
	if Vehicles[vehicle].Passenger[1] > 0 then
		ms = string.gsub(ms, "GNR1:_", "GNR1:*")
	end
	if Vehicles[vehicle].Passenger[2] > 0 then
		ms = string.gsub(ms, "GNR2:_", "GNR2:*")
	end
	for i=1, table.getn(SendTo) do
		_GModRect_Start("gmod/white")
		 _GModRect_SetPos(0.42, 0.85, 0.18, 0.1)
		 _GModRect_SetColor( 0, 0, 0, 100 )
		 _GModRect_SetTime( 9999, 0, 0 )
		_GModRect_Send( SendTo[i], 10000 )
		_GModText_Start( "Default" )
		 _GModText_SetPos( -1, 0.88 )
		 _GModText_SetColor( 255, 0, 0, 255 )
		 _GModText_SetTime( 9999, 0.2, 0.2 )
		 _GModText_SetText( "HP: "..math.ceil(Vehicles[vehicle].Health).."/"..VehicleTypes[Vehicles[vehicle].Type].MaxHealth )
		_GModText_Send( SendTo[i], 10001 )
		_GModText_Start( "Default" )
		 _GModText_SetPos( -1, 0.92 )
		 _GModText_SetColor( 255, 255, 255, 255 )
		 _GModText_SetTime( 9999, 0.2, 0.2 )
		 _GModText_SetText( ms )
		_GModText_Send( SendTo[i], 10002 )
	end
end,

onRemoved = function(vehicle)
	JEEPLASTATTACK[vehicle] = nil
end,

onDriverEnter = function(vehicle)
	_EntFire(vehicle, "TurnOn", "", 0)
	_EntFire(vehicle, "HandbrakeOff", "", 0)
end,

onDriverExit = function(vehicle)
	_EntFire(vehicle, "TurnOff", "", 0)
	_EntFire(vehicle, "HandbrakeOn", "", 0)
end,

onDestroyed = function(vehicle)
	JEEPLASTATTACK[vehicle] = nil
	_EntEmitSoundEx(vehicle, "ambient/explosions/explode_6.wav", 1.1, 1.0)
	_EntEmitSoundEx(vehicle, "ambient/explosions/explode_5.wav", 1.1, 0.8)
	_phys.ApplyForceCenter(vehicle, vector3(math.random(-500000,500000), math.random(-500000,500000), 5000000))
	_phys.ApplyTorqueCenter(vehicle, vector3(0, 0, 750000))
	for i=1, 10 do
		local ent = _EntCreate("env_explosion")
		if ent > 0 then
			_EntSetKeyValue(ent, "spawnflags", "17")
			_EntSetPos(ent, _EntGetPos(vehicle))
			_EntSpawn(ent)
			_EntSetParent(ent, vehicle)
			_EntFire(ent, "explode", "", i*0.25)
			_EntFire(ent, "kill", "", (i*0.25)+0.1)
		end
	end
	local ent2 = _EntCreate("env_ar2explosion")
	if ent2 > 0 then
		_EntSetPos(ent2, _EntGetPos(vehicle))
		_EntSpawn(ent2)
		_EntSetParent(ent2, vehicle)
		_EntFire(ent2, "explode", "", 2.45)
		_EntFire(ent2, "kill", "", 2.55)
		local pos = _EntGetPos(vehicle)
		AddTimer(2.4, 1, LuaExplosion, pos, 200, 400, 255)
	end
	AddTimer(2.45, 1, _EntEmitSoundEx, vehicle, "npc/combine_gunship/gunship_explode2.wav", 1.5, 1.0)
	_EntFire(vehicle, "break", "", 2.5)
	_EntFire(vehicle, "kill", "", 2.6)
end,

HideHUD = function(userid)
	_GModRect_Hide(userid, 10000, 0.2)
	_GModText_Hide(userid, 10001, 0.2)
	_GModText_Hide(userid, 10002, 0.2)
end,

onDamaged = function(vehicle, attacker, damage)
	if Vehicles[vehicle].Health < VehicleTypes[Vehicles[vehicle].Type].MaxHealth*0.5 and damage > 10 then
		local finalpos = vecAdd(_EntGetPos(vehicle), vector3(math.random(-20, 20), math.random(-20, 20), math.random(15, 20)))
		local ent = _EntCreate("env_fire_trail")
		if ent > 0 then
			_EntSetPos(ent, finalpos)
			_EntSpawn(ent)
			_EntSetParent(ent, vehicle)
			_EntFire(ent, "kill", "", math.random(3, 5))
		end
		local ent2 = _EntCreate("env_explosion")
		if ent2 > 0 then
			_EntSetKeyValue(ent2, "spawnflags", "17")
			_EntSetPos(ent2, finalpos)
			_EntSpawn(ent2)
			_EntFire(ent2, "explode", "", 0)
			_EntFire(ent2, "kill", "", 0.1)
		end
	end
end,

onPassengerAttack = function(vehicle, passenger)
	if _CurTime() > JEEPLASTATTACK[vehicle]+0.1 then
		JEEPLASTATTACK[vehicle] = _CurTime()
	else
		return
	end
	local userid = Vehicles[vehicle].Passenger[passenger]
	local startpos = vecAdd(_EntGetPos(vehicle), vector3(0,0,125))
	local plyang = vecAdd(_PlayerGetShootAng(userid), vector3(math.frandom(-0.04, 0.04), math.frandom(-0.04, 0.04), math.frandom(-0.04, 0.04)))
	_TraceLine(startpos, plyang, 2500, vehicle)
	if _TraceHitNonWorld() then
		_TraceAttack(_TraceGetEnt(), userid, userid, 9, "weak")
	end
	_EffectInit()
	 _EffectSetEnt(userid)
	 _EffectSetOrigin(startpos)
	 _EffectSetStart(_TraceEndPos())
	 _EffectSetScale(10000)
	_EffectDispatch("GunshipTracer")
	_EntEmitSound(vehicle, "nox/vulcan_fire.wav")
end,

onCreated = function(vehicle)
	JEEPLASTATTACK[vehicle] = _CurTime()
	_EntFire(vehicle, "Lock", "", 0)
end,

onPreCreated = function(vehicle)
	_EntSetKeyValue(vehicle, "model", "models/buggy.mdl")
	_EntSetKeyValue(vehicle, "vehiclescript", "scripts/vehicles/buggyracer.txt")
	_EntSetKeyValue(vehicle, "solid", "6")
end,

onThink = function(vehicle)
	if Vehicles[vehicle].Driver > 0 then
		_EntitySetPhysicsAttacker(vehicle, Vehicles[vehicle].Driver)
		if _PlayerIsKeyDown(Vehicles[vehicle].Driver, IN_FORWARD) then
			_EntFire(vehicle, "throttle", "1", 0)
		elseif _PlayerIsKeyDown(Vehicles[vehicle].Driver, IN_BACK) then
			_EntFire(vehicle, "throttle", "-1", 0)
		else
			_EntFire(vehicle, "throttle", "0", 0)
		end
		if _PlayerIsKeyDown(Vehicles[vehicle].Driver, IN_MOVELEFT) then
			for i=1, TICKRATE*66 do
				_EntFire(vehicle, "steer", "-1", 0)
			end
		elseif _PlayerIsKeyDown(Vehicles[vehicle].Driver, IN_MOVERIGHT) then
			for i=1, TICKRATE*66 do
				_EntFire(vehicle, "steer", "1", 0)
			end
		else
			_EntFire(vehicle, "steer", "0", 0)
		end
	elseif Vehicles[vehicle].Owner > 0 then
		_EntitySetPhysicsAttacker(vehicle, Vehicles[vehicle].Owner)
	end
end
}
JEEPLASTATTACK = {}
