[FilterScript] Chaotic's Custom Emergency Siren
#1


This filterscript allows players who are driving emergency vehicles to play an Audio for all other nearby players (both, in vehicles and standing upright). If you'd like to make your server a bit similar to the way GTA 5 functions, you can use this. I haven't added my credits to it, so do what you want with it, fine by me. I've released this filterscript, because I've noticed some persons are having difficulties using the PlayAudioStreamForPlayer function. Before you continue to read the body of this thread, I must inform you that this script isn't configured to check if the player is in an emergency vehicle or not. You can configure it to suit that requirement. If you're unable to do it, I'll gladly assist after you comment with the issue(s) you have. Enjoy!


Code:
#define FILTERSCRIPT // This is to define the script as a FS
#include <a_samp>
#include <zcmd>
#include <sscanf2>
#include <foreach>

#define MAX_DYNAMIC_CARS 500 // If this isn't already in your script, add it

enum VehicleSirenInfo
{
	EmergencyCopSiren
}

new VehicleInfo[MAX_DYNAMIC_CARS][VehicleSirenInfo];

// If a player moves out of the area, the siren will turn off (not fade out), and if they enter the zone with the siren again, it'll
// start the siren audio over again.

stock InAreaWithCopSiren(playerid) // This is to check if a player is in the area with the siren audio
{
	new Float:x, Float:y, Float:z;
	for(new i = 0; i < MAX_DYNAMIC_CARS; i ++)
	{
		GetVehiclePos(i, x, y, z);
		if(IsPlayerInRangeOfPoint(playerid, 100.0, x, y, z))
		{
			if(VehicleInfo[i][EmergencyCopSiren] != 0)
			{
				SetPVarFloat(playerid, "CopSirenX", x);
				SetPVarFloat(playerid, "CopSirenY", y);
				SetPVarFloat(playerid, "CopSirenZ", z);
				return 1;
			}
		}
	}
	return false;
}

public OnPlayerUpdate(playerid)
{
	if(InAreaWithCopSiren(playerid))
	{
			if(!GetPVarInt(playerid, "CopSiren"))
			{
				SetPVarInt(playerid,"CopSiren", 1);
				new link[128];
				format(link, 128, "https://falcon-host.org/uploads/siren.mp3"); // This is the siren which I made and merged, but you can change it.
				PlayAudioStreamForPlayer(playerid, link, GetPVarFloat(playerid, "CopSirenX"), GetPVarFloat(playerid, "CopSirenY"), GetPVarFloat(playerid, "CopSirenZ"), 100.0 , 0);
			}
	}
	else
	{
		if(GetPVarInt(playerid, "CopSiren") != 0)
		{
			DeletePVar(playerid, "CopSiren");
			StopAudioStreamForPlayer(playerid);
		}
	}
	return 1;
}

// This is mandatory because if several emergency cars are in the area at the same time, and the player disconnects, the siren will be turned off.
// It is also added to prevent bugs from occurring, to restart the server over and over.
public OnPlayerDisconnect(playerid, reason)
{											
	if(IsPlayerInAnyVehicle(playerid))
	{
		new Float:x, Float:y, Float:z;
		new VehicleID = GetPlayerVehicleID(playerid);
		if(VehicleInfo[VehicleID][EmergencyCopSiren] != 0)
		{
			GetVehiclePos(VehicleID, x, y, z);
			VehicleInfo[VehicleID][EmergencyCopSiren] = 0;
			foreach(new i : Player)
			{
				if(IsPlayerInRangeOfPoint(i, 100.0, x, y, z))
				{
					if(GetPVarInt(i, "CopSiren") != 0) 
                                        {
						StopAudioStreamForPlayer(i);
						SetPVarInt(i, "CopSiren", 0);
					}
				}
			}
		}
	}
	return 1;
}

public OnVehicleDeath(vehicleid, killerid)
{
	if(VehicleInfo[vehicleid][EmergencyCopSiren] != 0)
	{ 
		new Float:x, Float:y, Float:z;
		GetVehiclePos(vehicleid, x, y, z);
		foreach(new i : Player)
		{
			if(IsPlayerInRangeOfPoint(i, 100.0, x, y, z))
			{
				if(GetPVarInt(i,"CopSiren") != 0)
				{
					StopAudioStreamForPlayer(i);
					SetPVarInt(i, "CopSiren", 0);
				}
			}
		}
		VehicleInfo[vehicleid][EmergencyCopSiren] = 0;
	}
	return 1;
}

// This is the command to be used: /siren on = turns on the siren, and /siren off = turns it off.
CMD:siren(playerid, params[])
{
	new Float:x, Float:y, Float:z, VehicleID, option[16];
	if(sscanf(params, "s[16]", option))
	{
		SendClientMessage(playerid, 0xFF6347FF, "Usage: /siren [parameter]");
		SendClientMessage(playerid, 0xFF6347FF, "Parameters: {FFFFFF}on, off");
		return true;
	}
	if(strcmp(option, "on", true) == 0)
	{
		VehicleID = GetPlayerVehicleID(playerid);
		GetVehiclePos(VehicleID, x, y, z);
		new link[128];
		format(link, 128, "https://falcon-host.org/uploads/siren.mp3");
		foreach(new i : Player)
		{
			if(IsPlayerInRangeOfPoint(i, 100.0, x, y, z))
			{
				PlayAudioStreamForPlayer(i, link, x, y, z, 100.0, 0);
				SetPVarInt(i, "CopSiren", 1);
			}
		}
		VehicleInfo[VehicleID][EmergencyCopSiren] = 1; // 1 = true
	}
	if(strcmp(option, "off", true) == 0)
	{
		VehicleID = GetPlayerVehicleID(playerid);
		GetVehiclePos(VehicleID, x, y, z);
		foreach(new i : Player)
		{
			if(IsPlayerInRangeOfPoint(i, 100.0, x, y, z))
			{
				if(GetPVarInt(i,"CopSiren") != 0)
				{
					StopAudioStreamForPlayer(i);
					SetPVarInt(i, "CopSiren", 0);
				}
			}
		}
		VehicleInfo[VehicleID][EmergencyCopSiren] = 0; // 0 = not true
	}
	return 1;
}

Links: https://discordapp.com/invite/jr5Ywkm OR https://discord.io/ChaoticTheDev


sscanf Include: https://falcon-host.org/uploads/sscanf.rar
sscanf Plugin: https://falcon-host.org/uploads/sscanf-plugin.rar

foreach Include: https://falcon-host.org/uploads/foreach.rar

a_samp Include: https://falcon-host.org/uploads/a_samp.rar

zcmd Include: https://falcon-host.org/uploads/zcmd.rar


Please Rep me if you like this filterscript


I'll upload a video in the future to let you see what it looks like.
Reply
#2

where demo?
Reply
#3

Quote:

where demo?

Working on it, but you can try it out in your server if you want to. Be sure to leave a rep if it's handy in your projects. You can also use it to play other audios from URLs, instead of the siren only. Good Luck!
Reply
#4

Good job..
Reply
#5

Niceone.
Reply
#6

I like how your release is beautifully detailed and presented.
Reply
#7

Yes, presentation is good, can't say the same for the code.
Reply
#8

Looks amazing, well done!
Reply
#9

1.)
Code:
#define MAX_DYNAMIC_CARS 500 // If this isn't already in your script, add it
There are 2000 vehicles max this should be MAX_VEHICLES.

2.)
Code:
 enum VehicleSirenInfo
{
	EmergencyCopSiren
}
No need for an enum here at all.

Code:
stock InAreaWithCopSiren(playerid) // This is to check if a player is in the area with the siren audio
{
	new Float:x, Float:y, Float:z;
	for(new i = 0; i < MAX_DYNAMIC_CARS; i ++)
	{
		GetVehiclePos(i, x, y, z);
		if(IsPlayerInRangeOfPoint(playerid, 100.0, x, y, z))
		{
			if(VehicleInfo[i][EmergencyCopSiren] != 0)
			{
				SetPVarFloat(playerid, "CopSirenX", x);
				SetPVarFloat(playerid, "CopSirenY", y);
				SetPVarFloat(playerid, "CopSirenZ", z);
				return 1;
			}
		}
	}
	return false;
}
Wouldn't checking if the vehicle is actually streamed in first be a good idea before doing anything?
Use of pvars is never recommended and should not be used here.

Code:
public OnPlayerUpdate(playerid)
This system is NOT a use case for OnPlayerUpdate() WHY? you ask. Instead of having the players check let the vehicles with sirens on do the checking! It boils down to if you have 100 players and 3 vehicles with sirens on you are having all 100 players check all vehicles each update. It makes a heck of a lot more sense to have 3 vehicles check 100 players every second since 100 meters is such a relatively wide distance +/- 20 meters when it comes to activation should give unnoticeable differentiation when it comes to performance. Even in the best case scenario that all players are standing still so OPU is only called once a second.

Assume 2000 vehicles
Assume 100 players
Assume 3 vehicles activated

Current method - 2000 x 100 = 200,000 iterations
Revised method - 3 x 100 = 300 iterations

That is a massive difference so if you are going to use OPU make sure it is absolutely necessary.

Code:
format(link, 128, "https://falcon-host.org/uploads/siren.mp3"); // This is the siren which I made and merged, but you can change it.
I really don't like hardcoded links in FS you have to assume that any kind of link has expired before the script is even released. Yes this can be changed but if you are going to have hardcoded links define them at the top of the script so the user knows right away this is a configurable option.
Reply
#10

@KevTheJoker I am getting a Error when I compiled
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)