-- Buggy Rampage
-- By JetBoom

MAXSCORE = 1*60 -- In minutes
RACEMUSIC = true -- Highly annoying racing music
MUSICON = {}
GUNSELECT = {}
for i=1, _MaxPlayers() do
	MUSICON[i] = true
	GUNSELECT[i] = 1
end

BadEvents = {}
BadEvents[1] = {Name="Leeroy Ball", Function="LeeroyBall()"}
BadEvents[2] = {Name="Car Ball", Function="CarBall()"}
BadEvents[3] = {Name="Rocket Ball", Function="RocketBall()"}
--[[
BadEvents[3] = {Name="Lazah Ball", Function="LazahBall()"}
BadEvents[4] = {Name="Mine Ball", Function="MineBall()"}
BadEvents[5] = {Name="Meteor Ball", Function="MeteorBall()"}
]]

Weapons = {}
Weapons[1] = {Name="Semi-Auto Crossbow", Script="weapons/weapon_crossbowshotgun.lua"}
Weapons[2] = {Name="Car Strike", Script="weapons/weapon_carstrike.lua"}
Weapons[3] = {Name="Energy Grenade", Script="weapons/weapon_energygrenades.lua"}

function DoBadEvents()
	if not _EntExists(Vehicle) then return end
	local ran = math.random(1,table.getn(BadEvents))
	if not BadEvents[ran] then return end
	_GModText_Start( "ImpactMassive" )
	 _GModText_SetPos( -1, 0.6 )
	 _GModText_SetColor( 255, 0, 0, 255 )
	 _GModText_SetTime( 5, 0.1, 0.5 )
	 _GModText_SetText( BadEvents[ran].Name )
	_GModText_Send( 0, 2401 )
	local str = BadEvents[ran].Function
	_RunString(str)
end
AddTimer(30, 0, DoBadEvents)

function EnergyGrenade(prop, userid)
	AddTimer(2.5, 1, EnergyGrenadeExplode, prop, userid)
end

function EnergyGrenadeExplode(prop, userid)
	local ents = _EntitiesFindInSphere(_EntGetPos(prop), 250)
	for i=1, table.getn(ents) do
		if _phys.HasPhysics(ents[i]) then
			if IsPlayer(ents[i]) then
				_TraceAttack(ents[i], userid, userid, 75)
			end
		end
	end
	local shotpos = _EntGetPos(prop)
	
		local hitent = 0
		local bottompos = nil
		_TraceLine(shotpos, vector3(0,0,1), 4064, prop)
		local toppos = _TraceEndPos()
		_TraceLine(toppos, vector3(0,0,-1), 4064, prop)
		if _TraceHitNonWorld() then
			hitent = _TraceGetEnt()
			_TraceAttack(hitent, userid, userid, 50)
			bottompos = _EntGetPos(hitent)
		else
			bottompos = _TraceEndPos()
			if _TraceHitWorld() then
				_MakeDecal(math.random(67,70))
			end
		end

	local tempent = _EntCreate("info_target")
	if tempent > 0 then
		_EntSetPos(tempent, bottompos)
		_EntSetName(tempent, _PlayerInfo(userid, "name").."templighttarget")
		_EntSpawn(tempent)
		_EntFire(tempent, "kill", "", 0.41)
	end

	_EntEmitSound(tempent, "nox/lightning_impact.wav")
	--_EntEmitSound(tempent, "weapons/physcannon/energy_sing_explosion2.wav")
	_EntEmitSound(tempent, "ambient/levels/labs/electric_explosion"..math.random(1,5)..".wav")

	local laser = _EntCreate("env_laser")
	if laser > 0 then
		_EntSetPos(laser, toppos)
		_EntSetKeyValue(laser, "renderamt", "255")
		_EntSetKeyValue(laser, "rendercolor", "21 106 234")
		_EntSetKeyValue(laser, "width", "30")
		_EntSetKeyValue(laser, "texture", "Effects/laser1.vmt")
		_EntSetKeyValue(laser, "TextureScroll", "25")
		_EntSetKeyValue(laser, "damage", "0")
		_EntSetKeyValue(laser, "renderfx", "0")
		_EntSetKeyValue(laser, "LaserTarget", _PlayerInfo(userid, "name").."templighttarget")
		_EntSetKeyValue(laser, "NoiseAmplitude", "8")
		_EntSetKeyValue(laser, "spawnflags", "33")
		_EntSpawn(laser)
		_EntSetOwner(laser, userid)
		_EntFire(laser, "kill", "", 0.4)
	end
	local effect2 = _EntCreate("point_tesla")
	if effect2 > 0 then
		_EntSetKeyValue(effect2, "m_flRadius", "250")
		_EntSetKeyValue(effect2, "m_SoundName", "DoSpark")
		_EntSetKeyValue(effect2, "m_Color", "255 255 255")
		_EntSetKeyValue(effect2, "texture", "effects/laser1.vmt")
		_EntSetKeyValue(effect2, "beamcount_min", "6")
		_EntSetKeyValue(effect2, "beamcount_max", "10")
		_EntSetKeyValue(effect2, "thick_min", "20")
		_EntSetKeyValue(effect2, "thick_max", "50")
		_EntSetKeyValue(effect2, "lifetime_min", "0.5")
		_EntSetKeyValue(effect2, "lifetime_max", "1")
		_EntSetKeyValue(effect2, "interval_min", "0.1")
		_EntSetKeyValue(effect2, "interval_max", "0.25")
		_EntSetPos(effect2, vecAdd(bottompos, vector3(0,0,32)))
		_EntSpawn(effect2)
		_EntActivate(effect2)
		_EntFire(effect2, "DoSpark", "", 0.1)
		_EntFire(effect2, "DoSpark", "", 0.3)
		_EntFire(effect2, "DoSpark", "", 0.5)
		_EntFire(effect2, "kill", "", 0.8)
	end
	_EffectInit()
	_EffectSetEnt(userid)
	_EffectSetOrigin(bottompos)
	_EffectSetStart(bottompos)
	_EffectSetScale(70)
	_EffectSetMagnitude(10)
	_EffectSetRadius(10)
	_EffectDispatch("ThumperDust")
	_EffectDispatch("cball_explode")
	_util.ScreenShake(bottompos, math.random(10,30), 150.0, 0.75, 150)
	_EntFire(prop, "kill", "", 0.1)
end

function CarBall()
	for i=1, 8 do
	local model = "models/props_vehicles/car00"..math.random(2,5).."b.mdl"
	_EntPrecacheModel(model)
	local ent = _EntCreate("prop_physics")
	if ent > 0 then
		_EntSetPos(ent, vecAdd(_EntGetPos(Vehicle), vector3(0,0,1000 + (i*150))))
		_EntSetModel(ent, model)
		_EntSpawn(ent)
		_phys.ApplyForceCenter(ent, vector3(math.random(-900000, 900000),math.random(-900000, 900000), 0))
		_EntFire(ent, "kill", "", 20)
	end
	end
end

function RocketBall()
	for i=1, 8 do
		local ent = _EntCreate("rpg_missile")
		if ent > 0 then
			local pos = vecAdd(_EntGetPos(Vehicle), vector3(math.random(-1000, 1000), math.random(-1000, 1000), 1400))
			local ang = vecNormalize(vecSub(_EntGetPos(Vehicle), pos))
			_EntSetPos(ent, pos)
			_EntSetAng(ent, ang)
			_EntSetKeyValue(ent, "damage", "120")
			_EntSetVelocity(ent, vecMul(ang, 1000))
			_EntSpawn(ent)
		end
	end
end

function LeeroyBall()
	_EntEmitSoundEx(Vehicle, "leeroyjenkins.wav", 2, 1)
	AddTimer(3, 1, DoLeeroyBall)
end

function DoLeeroyBall()
	_EntEmitSoundEx(Vehicle, "ambient/explosions/explode_"..math.random(1,9)..".wav", 1.5, 0.9)
	_EntEmitSoundEx(Vehicle, "ambient/explosions/explode_"..math.random(1,9)..".wav", 1.0, 1.1)
	_util.ScreenShake(_EntGetPos(Vehicle), 50, 475, 3, 550)
	local ents = _EntitiesFindInSphere(_EntGetPos(Vehicle), 330)
	local damage = 0
	for i=1, table.getn(ents) do
		if _phys.HasPhysics(ents[i]) then
			if IsPlayer(ents[i]) then
				if _PlayerInfo(ents[i], "connected") then
					_EntSetVelocity(ents[i], vecAdd(vector3(math.random(-1000, 1000), math.random(-1000, 1000), math.random(500, 1500)), _EntGetVelocity(ents[i])))
					_TraceAttack(ents[i], Vehicle, Vehicle, 500)
				end
			end
		end
	end
	OrbExplosion(_EntGetPos(Vehicle), 0)
	local x = -90
	local y = -90
	local ent = 0
	for i=1, 3 do
		if x ~= 0 then
			ent = 0
			ent = _EntCreate("env_explosion")
			if ent > 0 then
				_EntSetPos(ent, vecAdd(vector3(x, 0, 0), _EntGetPos(Vehicle)))
				_EntSetKeyValue(ent, "spawnflags", "65")
				_EntSpawn(ent)
				_EntFire(ent, "explode", "", 0)
				_EntFire(ent, "kill", "", 0.15)
			end
			OrbExplosion(vecAdd(vector3(x, 0, 0), _EntGetPos(Vehicle)), 0.25)
		end
		x = x + 90
	end
	for ii=1, 3 do
		if y ~= 0 then
			ent = 0
			ent = _EntCreate("env_explosion")
			if ent > 0 then
				_EntSetPos(ent, vecAdd(vector3(0, y, 0), _EntGetPos(Vehicle)))
				_EntSetKeyValue(ent, "spawnflags", "65")
				_EntSpawn(ent)
				_EntFire(ent, "explode", "", 0)
				_EntFire(ent, "kill", "", 0.15)
			end
			OrbExplosion(vecAdd(vector3(0, y, 0), _EntGetPos(Vehicle)), 0.25)
		end
		y = y + 90
	end
end

function OrbExplosion(pos, delay)
	local explosion = _EntCreate("prop_combine_ball")
	if explosion > 0 then
		_EntSetPos(explosion, pos)
		_EntSpawn(explosion)
		_EntFire(explosion, "explode", "", delay)
		_EntRemove(explosion)
		_EntFire(explosion, "kill", "", delay+0.05)
	end
end

--------------------------------------------------------------
TEAM_RED = TEAM_RED
TEAM_BLUE = TEAM_BLUE
TEAM_GREEN = TEAM_GREEN  -- Warning Reduction
TEAM_YELLOW = TEAM_YELLOW
StartVector = vector3(0,0,0)
StartVector = _EntGetPos(_EntGetByName("startvector"))

_ServerCommand("gm_sv_allowspawning 0\n")
_ServerCommand("gm_sv_allownpc 0\n")
_ServerCommand("gm_sv_allweapons 0\n")
_ServerCommand("gm_sv_teamplay 1\n")
_ServerCommand("gm_sv_allowmultigun 0\n")
_ServerCommand("gm_sv_allowphysgun 0\n")
_ServerCommand("gm_sv_teamdamage 0\n")
_ServerCommand("gm_sv_playerdamage 1\n")
_ServerCommand("gm_sv_setrules\n")
_EntPrecacheModel("models/player/kleiner.mdl")
_EntPrecacheModel("models/player/monk.mdl")

UpdateHUDTimer = 0
ScoreTime = 0
PlayerInfo = {}
Driver = 0
Vehicle = 0
for i=1, _MaxPlayers() do
	PlayerInfo[i] = {}
	PlayerInfo[i].Time = 0
	if _PlayerInfo(i, "connected") then
		_PlayerRemoveAllWeapons(i)
		eventPlayerActive(_PlayerInfo(i, "name"), i, _PlayerInfo(i, "networkid"))
		_PlayerChangeTeam(i, TEAM_RED)
		_PlayerRespawn(i)
	end
end

function gamerulesThink()
	if not _EntExists(Vehicle) then
		local prop = _EntCreate("prop_physics")
		if prop > 0 then
			_EntSetKeyValue(prop, "model", "models/Roller.mdl")
			_EntSetPos(prop, vector3(0,0,0))
			_EntSpawn(prop)
			_EntSetName(prop, "ball")
			local fire = _EntCreate("env_smoketrail")
			local fire2 = _EntCreate("env_fire_trail")
			_EntSetKeyValue(fire, "firesprite", "sprites/glow02")
			_EntSetKeyValue(fire, "smokesprite", "sprites/physcannon_blueflare1")
			_EntSetPos(fire, _EntGetPos(prop))
			_EntSetPos(fire2, _EntGetPos(prop))
			_EntSpawn(fire)
			_EntSpawn(fire2)
			_EntSetParent(fire, prop)
			_EntSetParent(fire2, prop)
			_GModRect_Start( "deathnotify/swep_default")
				_GModRect_SetPos(0, 0, 0.035, 0.035)
				_GModRect_SetColor(255, 255, 255, 255)
				_GModRect_SetTime(99999, 0, 0)
				_GModRect_SetDelay(0)
				_GModRect_SetEntityOffset(vector3(0, 0, 16))
				_GModRect_SetEntity(prop)
			_GModRect_Send(0, 884)
			Vehicle = prop
		end
	end
	if Driver > 0 then
		if not IsInRadius(_EntGetPos(Driver), _EntGetPos(Vehicle), 300) then
			_PlayerChangeTeam(Driver, TEAM_RED)
			_PlayerSetModel(Driver, "models/Player/kleiner.mdl")
			_EntFire(Vehicle, "unlock", "", 0)
			Driver = 0
		end
	end
end

function ScoreTimer()
	if Driver > 0 then
		PlayerInfo[Driver].Time = PlayerInfo[Driver].Time+1
		_PlayerSetScore(Driver, PlayerInfo[Driver].Time)
		DrawScore(Driver)
		if PlayerInfo[Driver].Time >= MAXSCORE then
			DisplayWinner(Driver)
			for i=1, _MaxPlayers() do
				if _PlayerInfo(i, "connected") then
					PlayerInfo[i].Time = 0
					_PlayerSetScore(i, 0)
					_PlayerRespawn(i)
					DrawScore(i)
				end
			end
		end
	end
end

function PlayerSpawnChooseModel(playerid)
	_PlayerSetModel(playerid, "models/Player/kleiner.mdl")
end

function DisplayWinner(userid)
	local monah = (_TeamNumPlayers(TEAM_BLUE) + _TeamNumPlayers(TEAM_RED)) * 75
	if monah < 200 then
		_GModText_Start( "ImpactMassive" )
		 _GModText_SetPos( -1, -1 )
		 _GModText_SetColor( 255, 255, 255, 255 )
		 _GModText_SetTime( 4, 0, 0 )
		 _GModText_SetText( _PlayerInfo(userid, "name").." has won !!\nRestarting...")
		_GModText_Send( 0, 5151 )
		return
	end
	_GModText_Start( "ImpactMassive" )
	 _GModText_SetPos( -1, -1 )
	 _GModText_SetColor( 255, 255, 255, 255 )
	 _GModText_SetTime( 4, 0, 0 )
	 _GModText_SetText( _PlayerInfo(userid, "name").." has won $"..monah.."!!\nRestarting...")
	_GModText_Send( 0, 5151 )
	NDB.AddMoney(userid, monah)
end

TorsoModels = {}
TorsoModels[1] = "models/Zombie/Classic_torso.mdl"
TorsoModels[2] = "models/Humans/Charple03.mdl"

LegModels = {}
LegModels[1] = "models/Zombie/Classic_legs.mdl"
LegModels[2] = "models/Gibs/Fast_Zombie_Legs.mdl"

GibModels = {}
GibModels[1]="models/Gibs/Antlion_gib_medium_1.mdl"
GibModels[2]="models/Gibs/Antlion_gib_small_1.mdl"
GibModels[3]="models/Gibs/Antlion_gib_medium_2.mdl"
GibModels[4]="models/Gibs/Antlion_gib_medium_3.mdl"
GibModels[5]="models/Gibs/Antlion_gib_small_1.mdl"
GibModels[6]="models/Gibs/Antlion_gib_small_2.mdl"

_EntPrecacheModel("models/Gibs/Antlion_gib_medium_1.mdl")
_EntPrecacheModel("models/Gibs/Antlion_gib_small_1.mdl")
_EntPrecacheModel("models/Gibs/Antlion_gib_medium_2.mdl")
_EntPrecacheModel("models/Gibs/Antlion_gib_medium_3.mdl")
_EntPrecacheModel("models/Gibs/Antlion_gib_small_1.mdl")
_EntPrecacheModel("models/Gibs/Antlion_gib_small_2.mdl")
_EntPrecacheModel("models/gibs/HGIBS.mdl")
_EntPrecacheModel("models/gibs/HGIBS_rib.mdl")
_EntPrecacheModel("models/gibs/HGIBS_rib.mdl")
_EntPrecacheModel("models/gibs/HGIBS_rib.mdl")
_EntPrecacheModel("models/gibs/HGIBS_scapula.mdl")
_EntPrecacheModel("models/gibs/HGIBS_spine.mdl")

Gibbed = {}
for i=1, _MaxPlayers() do
	Gibbed[i] = false
end

function Gib(killed)
	if Gibbed[killed] then return end
	Gibbed[killed] = true
	_TraceLine(_EntGetPos(killed), vector3(0,0,-1), 100, killed)
	if _TraceHit() then
		_MakeDecal(math.random(46,51))
	end
	local velocity = vecLength(_EntGetVelocity(killed)) + 30
	Gibbed[killed] = true
	local ent = _EntCreate("env_shooter")
	if ent > 0 then
		_EntSetPos(ent, _EntGetPos(killed))
		_EntSetKeyValue(ent, "m_iGibs", "1")
		_EntSetKeyValue(ent, "m_flVelocity", velocity)
		_EntSetKeyValue(ent, "shootmodel", LegModels[math.random(1,2)])
		_EntSetKeyValue(ent, "m_flVariance", "5.0")
		_EntSetKeyValue(ent, "simulation", "2")
		_EntSpawn(ent)
		_EntFire(ent, "shoot", "", 0)
		_EntFire(ent, "kill", "", 1)
	end
	local ent = _EntCreate("env_shooter")
	if ent > 0 then
		_EntSetPos(ent, _EntGetPos(killed))
		_EntSetKeyValue(ent, "m_iGibs", "1")
		_EntSetKeyValue(ent, "m_flVelocity", velocity)
		_EntSetKeyValue(ent, "shootmodel", TorsoModels[math.random(1,2)])
		_EntSetKeyValue(ent, "m_flVariance", "5.0")
		_EntSetKeyValue(ent, "simulation", "2")
		_EntSpawn(ent)
		_EntFire(ent, "shoot", "", 0)
		_EntFire(ent, "kill", "", 1)
	end
	_EffectInit()
	_EffectSetOrigin(_EntGetPos(killed))
	_EffectSetScale(8)
	_EffectSetFlags(9)
	_EffectDispatch("bloodspray")
	_EffectDispatch("bloodspray")
	_EffectDispatch("bloodspray")
	local gibstospawn = {}
	for i=1, 3 do
		 gibstospawn[table.getn(gibstospawn)+1] = GibModels[math.random(1, table.getn(GibModels))]
	end
	for i in gibstospawn do
		local gib = _EntCreate("prop_physics")
		if gib > 0 then
			_EntSetModel(gib, gibstospawn[i])
			_EntSetKeyValue(gib, "spawnflags", "4")
			_EntSetPos(gib, vecAdd(_EntGetPos(killed), vector3(math.random(-10,10),math.random(-10,10),math.random(10,30))))
			_EntSetAngAngle(gib, vector3(math.random(0,359), math.random(0,359), math.random(0,359)))
			_EntSpawn(gib)
			_phys.ApplyForceCenter(gib, vecMul(_EntGetVelocity(killed), vector3(8,8,8)))
			_phys.ApplyTorqueCenter(gib, vector3(math.random(-10500, 10500), math.random(-10500, 10500), math.random(-3000, 13000)))
			_phys.ApplyForceCenter(gib, vector3(math.random(-8000, 8000), math.random(-8000, 8000), math.random(-3000, 8500)))
			_EntFire(gib, "kill", "", 7.5)
			local trail = _EntCreate("env_smoketrail")
			if trail > 0 then
				_EntSetPos(trail, _EntGetPos(gib))
				_EntSetKeyValue(trail, "spawnrate", "22")
				_EntSetKeyValue(trail, "lifetime", "1.2")
				_EntSetKeyValue(trail, "startcolor", "255 0 0")
				_EntSetKeyValue(trail, "endcolor", "255 0 0")
				_EntSetKeyValue(trail, "minspeed", "0")
				_EntSetKeyValue(trail, "maxspeed", "0")
				_EntSetKeyValue(trail, "startsize", "6")
				_EntSetKeyValue(trail, "endsize", "9")
				_EntSetKeyValue(trail, "spawnradius", "2")
				_EntSetKeyValue(trail, "firesprite", "sprites/redglow3.spr")
				_EntSetKeyValue(trail, "smokesprite", "sprites/redglow3.spr")
				_EntSpawn(trail)
				_EntSetParent(trail, gib)
			end
		end
	end
	_EntEmitSoundEx(killed, "physics/flesh/flesh_bloody_break.wav", 1.0, 0.75)
	AddTimer(0.1, 1, _PlayerSilentKill, killed, 1.5, true)
end

function onShowSpare2(userid)
	if MUSICON[userid] then
		_PrintMessage(userid, 3, "Music: off")
	else
		_PrintMessage(userid, 3, "Music: on")
	end
	MUSICON[userid] = not MUSICON[userid]
end

function onShowSpare1(userid)
	GUNSELECT[userid] = GUNSELECT[userid] + 1
	if not Weapons[GUNSELECT[userid]] then
		GUNSELECT[userid] = 1
	end
	_PrintMessage(userid, 3, "You will respawn with a "..Weapons[GUNSELECT[userid]].Name)
end

function DrawScore(userid)
	if _PlayerInfo(userid, "connected") then
		_GModRect_Start( "gmod/white" )
		 _GModRect_SetPos( 0.023, 0.023, 0.13, 0.03 )
		 _GModRect_SetColor( 0, 0, 0, 100 )
		 _GModRect_SetTime( 9999, 0, 0 )
		_GModRect_Send( userid, 665 )
		_GModText_Start( "Default" )
		 _GModText_SetPos( 0.025, 0.025 )
		 _GModText_SetColor( 255, 0, 0, 255 )
		 _GModText_SetTime( 9999, 0, 0 )
		 _GModText_SetText( ToMinutesSeconds(PlayerInfo[userid].Time).." / "..ToMinutesSeconds(MAXSCORE))
		_GModText_Send( userid, 666 )
	end
end

function CreateResFileString()
	local str = '"resources"\r\n'
	..'{\r\n'
	..'"sound/stfu.wav" "file"\r\n'
	..'"sound/banned.wav" "file"\r\n'
	..'"sound/racing12.mp3" "file"\r\n'
	..'"sound/leeroyjenkins.wav" "file"\r\n'
	..'"sound/nox/lightning_impact.wav" "file"\r\n'
	..'"sound/racing11.mp3" "file"\r\n'
	..'}'
	return str
end

function CreateResFile()
	_file.Write("maps/".._GetCurrentMap()..".res", CreateResFileString())
end

function RaceMusic()
	if RACEMUSIC then
		if Driver > 0 then
			for i=1, _MaxPlayers() do
				if MUSICON[i] then
					_PlaySoundPlayer(i, "racing12.mp3")
				end
			end
		else
			for i=1, _MaxPlayers() do
				if MUSICON[i] then
					_PlaySoundPlayer(i, "racing11.mp3")
				end
			end
		end
	end
end

function UpdateHUD()
	
end

START = false
function gamerulesStartMap()
	if START then return end
	for i=1, 4000 do
		if string.find(_EntGetType(i), "vehicle") ~= nil then
			_EntRemove(i)
		end
	end
	START = true
	_TeamSetName(TEAM_RED, "Angry Minges With Rockets")
	_TeamSetName(TEAM_BLUE, "BALL PERSON")
	_TeamSetName(TEAM_GREEN, "Angry Minges With Stunsticks")
	_TeamSetName(TEAM_YELLOW, "Stupid Faggots")
	UpdateHUDTimer = AddTimer(1, 0, UpdateHUD)
	ScoreTime = AddTimer(1, 0, ScoreTimer)
	CreateResFile()
	AddTimer(6.02, 0, RaceMusic)
end
gamerulesStartMap()

function IsInRadius(a, b, dist)
	if math.abs(vecLength(vecSub(a,b))) > dist then return false end
	return true
end

function PickDefaultSpawnTeam(userid)
	_PlayerChangeTeam(userid, TEAM_RED)
	return true
end

function eventPlayerSpawn(userid)
	Gibbed[userid] = false
end

function onGravGunPickup(userid, entity)
	if entity == Vehicle then
		if Driver > 0 then
			_PlayerChangeTeam(Driver, TEAM_RED)
			_PlayerSetModel(Driver, "models/Player/Kleiner.mdl")
		end
		Driver = userid
		_PlayerSetModel(userid, "models/Player/monk.mdl")
		_PlayerChangeTeam(userid, TEAM_BLUE)
		_PrintMessageAll(4, _PlayerInfo(userid, "name").." took the ball.")
		_PlaySound("weapons/physcannon/superphys_launch"..math.random(1,4)..".wav")
		_PlayerEnableSprint(userid, false)
		return true
	end
	return false
end

function onGravGunDrop(player, entity)
	return entity ~= Vehicle
end

function onGravGunPunt(player, entity)
	return entity ~= Vehicle
end

function onGravGunLaunch(player, entity)
	if entity == Vehicle then
		local prop = _EntCreate("prop_physics")
		if prop > 0 then
			_EntSetModel(prop, "models/Roller.mdl")
			_EntSetPos(prop, _EntGetPos(entity))
			_EntSetOwner(prop, entity)
			_EntSpawn(prop)
			local fire = _EntCreate("env_fire_trail")
			if fire > 0 then
				_EntSetPos(fire, _EntGetPos(prop))
				_EntSpawn(fire)
				_EntSetParent(fire, prop)
			end
			_EntFire(prop, "kill", "", 3)
			_phys.ApplyForceCenter(prop, vecMul(_PlayerGetShootAng(player), 100000))
			_EntitySetPhysicsAttacker(prop, player)
		end
		return false
	end
	return true
end

function eventPlayerDisconnect(name, userid, address, steamid, reason)
	if _PlayerInfo(userid,"connected") then
		PlayerInfo[userid].Time = 0
		if userid == Driver then
			Driver = 0
			_PrintMessageAll(4, "The ball was dropped.")
			_PlaySound("weapons/physcannon/energy_sing_flyby1.wav")
		end
	end
	MUSICON[userid] = true
end

function eventPlayerKilled(killed, attacker, weapon)
	if Driver == killed then
		_PrintMessageAll(4, _PlayerInfo(killed, "name").." dropped the ball.", 255, 255, 255, 255)
		_PlaySound("weapons/physcannon/energy_sing_flyby1.wav")
		Driver = 0
		_PlayerChangeTeam(killed, TEAM_RED)
		_PlayerSetModel(killed, "models/Player/Kleiner.mdl")
		_PlayerEnableSprint(killed, true)
	end
	Gib(killed)
end

function onShowTeam(userid)
end

function onShowHelp(userid)
	_GModRect_Start("gmod/white")
	 _GModRect_SetPos(0.0, 0.14, 1.0, 0.24)
	 _GModRect_SetColor(0, 0, 0, 175)
	 _GModRect_SetTime(10, 0.2, 1)
	_GModRect_Send(userid, 49)
	_GModText_Start("Trebuchet24")
	 _GModText_SetPos(-1, 0.16)
	 _GModText_SetColor(255, 255, 255, 255)
	 _GModText_SetTime(10, 0.2, 1.25)
	 _GModText_SetText("Roller Rampage\n a gamemode by JetBoom")
	_GModText_Send(userid, 50)
	_GModText_Start("Default")
	 _GModText_SetPos(-1, 0.3)
	 _GModText_SetColor(90, 90, 255, 240)
	 _GModText_SetTime(10, 0.2, 1.25)
	 _GModText_SetText( "Hold the ball longer then everyone else!\n Kill whoever has it!\nF4 = Toggle Music        F3 = Swap weapons" )
	_GModText_Send( userid, 51 )
end

function eventPlayerActive(name, userid, steamid)
	AddTimer(4.0, 1, onShowHelp, userid)
	AddTimer(4.0, 1, DrawScore, userid)
	if Vehicle > 0 then
	_GModRect_Start( "deathnotify/swep_default")
	_GModRect_SetPos(0, 0, 0.035, 0.035)
	_GModRect_SetColor(255, 255, 255, 255)
	_GModRect_SetTime(99999, 0, 0)
	_GModRect_SetDelay(0)
	_GModRect_SetEntityOffset(vector3(0, 0, 16))
	_GModRect_SetEntity(Vehicle)
	_GModRect_Send(0, 884)
	end
end

function IsVehicle(entity)
	return string.find(_EntGetType(entity), "prop_vehicle") ~= nil
end

function GiveDefaultItems(userid)
	_PlayerGiveItem(userid, "weapon_physcannon")
	_PlayerGiveSWEP(userid, Weapons[GUNSELECT[userid]].Script)
end

function eventPlayerDuplicateProp(userid, propid)
	return not IsVehicle(propid)
end

function onPlayerRemove(player, entity)
	return entity ~= Vehicle
end
