Paste: cruiserspeedcam.lua

Author: Jonnis
Mode: lua
Date: Tue, 13 Mar 2012 12:23:48
Plain Text |
ticketTax = get("ticketTax")
allowExit = get("allowExit")
rangeOfRadar = get("rangeOfRadar")


function activateSpeedCamera(source, commandName, allowedspeed)
	local ok = 0	
	local radiuses = getElementsByType("colshape")
	for theKey, theRadius in ipairs(radiuses) do
		if getElementData(theRadius, "Creator") == tostring(getPlayerName(source)) then
			ok = 1
		end
	end
		
	if ok == 0 then	
	local theVehicle = getPedOccupiedVehicle ( source )
	if theVehicle then 
		local id = getElementModel ( theVehicle )
		integerspeed = tonumber(allowedspeed)
		if integerspeed then
			if id == 523 or id == 598 or id == 596 or id == 597 or id == 599 then
				if integerspeed > 59 and integerspeed <500 then
					setElementFrozen(theVehicle, false)
					setElementData(theVehicle, "Speedcamera", 1)
					local x, y, z = getElementPosition(theVehicle)
					radius = createColSphere(x, y, z, rangeOfRadar)
					local creator = getPlayerName(source)
					setElementData(theVehicle, "Creator", creator)
					setElementData(radius, "Creator", creator)
					setElementData(radius, "Allowedspeed", integerspeed)
					outputChatBox("#EEE685Speedcam has been activated on the cruiser.", source, 255, 255, 255, true)	
					playSoundFrontEnd ( source, 101 )
				end
			end
		else
		outputChatBox("SYNTAX: /speedon <speed allowed in km/h but bigger than 60>", source)
		outputChatBox("EXAMPLE: /speedon 70", source)
		end
	end
	else
		outputChatBox("#EEE685You can activate only one speedcam (Don't abuse. Nice try!)", source, 255, 255, 255, true)
	end
end
addCommandHandler("speedon", activateSpeedCamera)

function deactivateSpeedCamera(source, commandName)
	if isPedInVehicle(source) then
	local theVehicle = getPedOccupiedVehicle ( source )
	local id = getElementModel ( theVehicle )
	if id == 523 or id == 598 or id == 596 or id == 597 or id == 599 then
		setElementFrozen(theVehicle, false)
		setElementData(theVehicle, "Speedcamera", 0)
		local radiuses = getElementsByType("colshape")
		for theKey, theRadius in ipairs(radiuses) do
			if getElementData(theRadius, "Creator") == tostring(getPlayerName(source)) then
				destroyElement(theRadius)
			end
		end
		outputChatBox("#EEE685Speedcam has been deactivated on the cruiser.", source, 255, 255, 255, true)	
		playSoundFrontEnd ( source, 101 )
	end
	else
		outputChatBox("#EEE685You are not in a vehicle to use this command.", source, 255, 255, 255, true)
	end
end
addCommandHandler("speedoff", deactivateSpeedCamera)

function ifPlayerDisconnects()
	local vehicles = getElementsByType("vehicle")
	for theKey, theVehicle in ipairs(vehicles) do
		if getElementData(theVehicle, "Creator") == tostring(getPlayerName(source)) then
			setElementFrozen(theVehicle, false)
			setElementData(theVehicle, "Speedcamera", 0)
		end
	end
	local radiuses = getElementsByType("colshape")
	for theKey, theRadius in ipairs(radiuses) do
		if getElementData(theRadius, "Creator") == tostring(getPlayerName(source)) then
			destroyElement(theRadius)
		end
	end
	outputChatBox("#EEE685Speedcam has been deactivated on the cruiser.", source, 255, 255, 255, true)	
end
addEventHandler("onPlayerQuit", getRootElement(), ifPlayerDisconnects)

function ifPlayerDies()
	local vehicles = getElementsByType("vehicle")
	for theKey, theVehicle in ipairs(vehicles) do
		if getElementData(theVehicle, "Creator") == tostring(getPlayerName(source)) then
			setElementFrozen(theVehicle, false)
			setElementData(theVehicle, "Speedcamera", 0)
		end
	end
	local radiuses = getElementsByType("colshape")
	for theKey, theRadius in ipairs(radiuses) do
		if getElementData(theRadius, "Creator") == tostring(getPlayerName(source)) then
			destroyElement(theRadius)
		end
	end
	outputChatBox("#EEE685Speedcam has been deactivated on the cruiser.", source, 255, 255, 255, true)	
end
addEventHandler("onPlayerWasted", getRootElement(), ifPlayerDies)

function showspeed(theVehicle)
	if getElementType(theVehicle) == "vehicle" then
	local id = getElementModel(theVehicle)
		if id ~= 523 and id ~= 598 and id ~= 596 and id ~= 597 and id ~= 599 and id ~= 416 and id ~= 490 and id ~= 427 and id ~= 407 and id ~= 544 then  -- check if vehicles are not for government or emergency
		if getElementData(source, "Creator") then
			speedx, speedy, speedz = getElementVelocity ( theVehicle )
			actualspeed = (speedx^2 + speedy^2 + speedz^2)^(0.5)
			kmh = math.ceil(actualspeed * 180)
			if getElementData(source, "Allowedspeed") < kmh then
				local driver = getElementModel(theVehicle)
				local officer = getElementData(source, "Creator")
				outputChatBox("On coming vehicle - " ..getVehicleName(theVehicle).. " - was clocked doing " ..kmh.. " km/h")
				playSoundFrontEnd ( source, 101 )
			end
		end
		end
	end
end
addEventHandler("onColShapeHit", getRootElement(), showspeed)
				

function stopFromExit(thePlayer)
	if allowExit == false and getElementData(source, "Speedcamera") == 1 then
		cancelEvent()
		outputChatBox("#EEE685You cannot leave the vehicle when speed camera is activated.", thePlayer, 255, 255, 255, true)
	end
end
addEventHandler ( "onVehicleStartExit", getRootElement(), stopFromExit )

New Annotation

Summary:
Author:
Mode:
Body: