All pastes #1497897 Raw Edit

CPStream

public c v1 · immutable
#1497897 ·published 2009-07-17 06:57 UTC
rendered paste body
/************************* 	     CPstream 	    ************************	Author: 		Donny (k)		Version: 		0.3rAbout:		Just a rewrite of the .2b version, I broke the .2r version before releasing it so I guess that version		wasn't so popular and I never read the post again until a few days ago. This version is area based, 		it's basically the same as before but now has areas included and you can use just an area or an area 		with a checkpoint so it covers tons more as it can now be used in many ways such as for a bank or a 		no go area which takes away your health/weapons etc.				By the way if you create a checkpoint outside your areas radius or the size of the checkpoint is too large		or too small or the radius is too small or whatever, the script will automatically fix all this. You can controll		how the checkpoints are repositioned with the below define, uncomment it to suit your needs:		*/		//#define USE_CENTER/*		If the above define is uncommented then checkpoints which are created outside of their area will be		repositioned to the central point of your area but if left commented out they will be dragged into		your areas sphere but this will have negative effects as now your area will be larger on one side.		If that doesn't make sense then read below, if it does then skip to the natives.				A = center of your area		B = center of your checkpoint		C = radius of your area		D = intersection point		E = new checkpoint position				Imagine a line drawn from point A to point B and then imagine a circle drawn with a radius of C and using		point A as an anchor point (this is where it grows from), if we look at where the line intersects (D) with the 		circle closest to point B then we draw a new line from A to D we can now subtract the size of the checkpoint 		from the length of the line upto point D (going from point D to A) and we have a point within our area where 		we can place our checkpoint (E). Now imagine a line drawn from E to D, as you will notice the line from A to E 		is a lot larger than the line from E to D so now our area is a lot larger on one side, it's the distance from D to E 		on one and the distance from A to E on the other side so it will activate at what seem to be different distances 		from the checkpoint but this is due to the distance being calculated from point A to D and not point E to D, the 		area radius is always the distance calculated while the checkpoints distance to the areas center is ignored.				Natives:		native CreateArea( Float:x, Float:y, Float:z, Float:radius = MIN_RADIUS, bool:use_checkpoint = false, Float:cpx = 0.0, Float:cpy = 0.0, Float:cpz = 0.0, Float:size = MIN_SIZE )		native CreateCheckpoint( Float:x, Float:y, Float:z, Float:radius = MIN_SIZE, Float:size = MIN_RADIUS )		native IsPlayerInCheckpointID( playerid, cpid )		native IsPlayerInAreaID( playerid, areaid )		native GetPlayerCheckpoint( playerid )		native GetPlayerArea( playerid )		native DestroyCheckpoint( cpid )		native DestroyArea( areaid )		native IsValidArea( areaid )		native GetAreaCount()		Notes:		Areas and checkpoints are checked for activity every 500 ms + the scripts lag so if		you go through one too quick it won't register, use the race checkpoints for this which		come with SAMP, this is just for normal checkpoints (walk in). Also make sure you have		read the above explination about the USE_CENTER define and how it works, I won't 		answer questions by people which have not read this !!!*/#if defined _CPstream_included	#endinput#endif#define _CPstream_included#pragma library CPstream#if !defined _a_samp_included	#include <a_samp>#endif#define MAX_LEN				128#define MAX_AREAS			10 /* <<< make this the # of areas you want to use */#define INVALID_AREA		-1#define INVALID_CHECKPOINT	-1#define MIN_RADIUS			2.0#define MAX_RADIUS			2000.0#define MIN_SIZE			1.0#define MAX_SIZE			( MAX_RADIUS - 2.0 )enum eArea{	id,	Float:area_x,	Float:area_y,	Float:area_z,		Float:area_cpx,				 	Float:area_cpy, 				 	Float:area_cpz, 				 	Float:area_cpsize,	Float:area_radius,		bool:area_hascp}enum ePlayer{	player_area, 	player_checkpoint}new 		gArea[ MAX_AREAS ][ eArea ],	gPlayer[ MAX_PLAYERS ][ ePlayer ],  	bool:gSystemRunning;	forward CheckSpheres();forward OnPlayerEnterArea( playerid, areaid );forward OnPlayerLeaveArea( playerid, areaid );forward OnPlayerEnterCheckpointEx( playerid, cpid );forward OnPlayerLeaveCheckpointEx( playerid, cpid );stock CreateArea( Float:x, Float:y, Float:z, Float:radius = MIN_RADIUS, bool:use_checkpoint = false, Float:cpx = 0.0, Float:cpy = 0.0, Float:cpz = 0.0, Float:size = MIN_SIZE ){			if ( !gSystemRunning ) 	{		gSystemRunning = true;		SetTimer( "CheckSpheres", 500, 0 );				new 			year, 			month, 			day,			hour, 			minute, 			second,			string[ 128 ];					getdate( year, month, day );		gettime( hour, minute, second );    		 		if ( hour < 10 ) format( string, sizeof( string ), "0%d", hour );		else format( string, sizeof( string ), "%d", hour );		if ( minute < 10 ) format( string, sizeof( string ), "%s:0%d", string, minute );		else format( string, sizeof( string ), "%s:%d", string, minute );		if ( second < 10 ) format( string, sizeof( string ), "%s:0%d", string, second );		else format( string, sizeof( string ), "%s:%d", string, second );		printf( "CPstream system version 0.3r activated on %d/%d/%d - %s", day, month, year, string );				for ( new i; i < MAX_AREAS; i ++ ) DestroyArea( i );	}	for ( new i; i < MAX_AREAS; i ++ )	{		if ( gArea[ i ][ id ] == INVALID_AREA )		{			gArea[ i ][ id ] = i;			if ( radius > MAX_RADIUS ) radius = MAX_RADIUS;			while ( radius <= MIN_SIZE ) radius += MIN_SIZE;								if ( use_checkpoint )			{									if ( size < MIN_SIZE ) size = MIN_SIZE;								if ( size > MAX_SIZE ) size = MAX_SIZE;							while ( radius <= size ) radius += MIN_SIZE;							if ( !IsPointInRangeOfPoint( radius, cpx, cpy, cpz, x, y, z ) )				{					#if !defined USE_CENTER						if ( cpx > ( x + radius ) ) cpx = ( ( x + radius ) - ( size ) );						if ( cpy > ( y + radius ) ) cpy = ( ( y + radius ) - ( size ) );						if ( cpz > ( z + radius ) ) cpz = ( ( z + radius ) - ( size ) );						if ( cpx < ( x - radius ) ) cpx = ( ( x - radius ) + ( size ) );						if ( cpy < ( y - radius ) ) cpy = ( ( y - radius ) + ( size ) );						if ( cpz < ( z - radius ) ) cpz = ( ( z - radius ) + ( size ) );						printf( "Checkpoint ID: %d was outside it's areas radius,\nit has been moved to within the areas radius", gArea[ i ][ id ] );					#else						cpx = x;						cpy = y;						cpz = z;						printf( "Checkpoint ID: %d was outside it's areas radius,\nit has been moved to the areas center", gArea[ i ][ id ] );											#endif				}											}						gArea[ i ][ area_x ] = x;				gArea[ i ][ area_y ] = y;					gArea[ i ][ area_z ] = z;													gArea[ i ][ area_radius ] = radius;						gArea[ i ][ area_hascp ] = use_checkpoint;										gArea[ i ][ area_cpx ] = cpx;				gArea[ i ][ area_cpy ] = cpy;					gArea[ i ][ area_cpz ] = cpz;															gArea[ i ][ area_cpsize ] = size;										return gArea[ i ][ id ];		}	}	return INVALID_AREA;}	stock CreateCheckpoint( Float:x, Float:y, Float:z, Float:size = MIN_SIZE, Float:radius = MIN_RADIUS ){	return CreateArea( x, y, z, size, true, x, y, z, radius );}#define IsPlayerInCheckpointID(%1,%2) \			( gPlayer[ %1 ][ player_checkpoint ] == %2 )#define IsPlayerInAreaID(%1,%2)	\			( gPlayer[ %1 ][ player_area ] == %2 )#define GetPlayerCheckpoint(%1)	\			( gPlayer[ %1 ][ player_checkpoint ] )#define GetPlayerArea(%1)	\			( gPlayer[ %1 ][ player_area ] )			//no Set****, just set the players position and the streamer will do the rest						stock DestroyCheckpoint( cpid ){	for ( new i; i < MAX_AREAS; i ++ )	{		if ( gArea[ i ][ id ] == cpid )		{			if ( gArea[ i ][ area_hascp ] )			{				gArea[ i ][ area_hascp ] = false;				gArea[ i ][ area_cpx ] = 0.0;					gArea[ i ][ area_cpy ] = 0.0;						gArea[ i ][ area_cpz ] = 0.0;						gArea[ i ][ area_cpsize ] = 0.0;						for ( new j; j < MAX_PLAYERS; j ++ )				{					if ( GetPlayerArea( j ) == gArea[ i ][ id ] )					{						DisablePlayerCheckpoint( j );						gPlayer[ j ][ player_checkpoint ] = INVALID_CHECKPOINT;					}				}				return true;			}		}	}	return false;}stock DestroyArea( areaid ){	for ( new i; i < MAX_AREAS; i ++ )	{		if ( gArea[ i ][ id ] == areaid )		{					for ( new j; j < MAX_PLAYERS; j ++ )			{				if ( GetPlayerArea( j ) == gArea[ i ][ id ] )				{					if ( gArea[ i ][ area_hascp ] )					{						DisablePlayerCheckpoint( j );						gPlayer[ j ][ player_checkpoint ] = INVALID_CHECKPOINT;					}					gPlayer[ j ][ player_area ] = INVALID_AREA;				}			}					gArea[ i ][ area_x ] = 0.0;				gArea[ i ][ area_y ] = 0.0;					gArea[ i ][ area_z ] = 0.0;					gArea[ i ][ area_radius ] = 0.0;					gArea[ i ][ area_hascp ] = false;			gArea[ i ][ area_cpx ] = 0.0;				gArea[ i ][ area_cpy ] = 0.0;					gArea[ i ][ area_cpz ] = 0.0;					gArea[ i ][ area_cpsize ] = 0.0;						gArea[ i ][ id ] = INVALID_AREA;						return true;		}	}	return false;}stock IsValidArea( areaid ){	for ( new i; i < MAX_AREAS; i ++ )	{		if ( gArea[ i ][ id ] == areaid ) return true;	}	return false;}stock GetAreaCount(){	new		i,		count;			for ( i = 0; i < MAX_AREAS; i ++ )	{		if ( gArea[ i ][ id ] != INVALID_AREA ) count ++;	}	return count;}stock IsPointInRangeOfPoint( Float:range, Float:px, Float:py, Float:pz, Float:x, Float:y, Float:z ){			px -= x;	py -= y;	pz -= z;	return ( ( px * px ) + ( py * py ) + ( pz * pz ) ) < ( range * range );}public CheckSpheres() {			new		Float:x,		Float:y,		Float:z;		    for ( new i; i < MAX_PLAYERS; i ++ ) 	{		if ( GetPlayerPos( i, x, y, z ) )		{			for ( new j; j < MAX_AREAS; j ++ )			{				if ( gArea[ j ][ id ] != INVALID_AREA )				{					if ( IsPointInRangeOfPoint( gArea[ j ][ area_radius ], x, y, z, gArea[ j ][ area_x ], gArea[ j ][ area_y ], gArea[ j ][ area_z ] ) ) 					{						if ( gArea[ j ][ id ] != GetPlayerArea( i ) ) 						{							if ( gArea[ j ][ area_hascp ] ) SetPlayerCheckpoint( i, gArea[ j ][ area_cpx ], gArea[ j ][ area_cpy ], gArea[ j ][ area_cpz ], gArea[ j ][ area_cpsize ] );																			gPlayer[ i ][ player_area ] = gArea[ j ][ id ];							CallRemoteFunction( "OnPlayerEnterArea", "ii", i, gArea[ j ][ id ] );						}						else						{													if ( gArea[ j ][ area_hascp ] )							{								if ( IsPlayerInCheckpoint( i ) && GetPlayerCheckpoint( i ) != gArea[ j ][ id ] )															{									gPlayer[ i ][ player_checkpoint ] = gArea[ j ][ id ];															CallRemoteFunction( "OnPlayerEnterCheckpointEx", "ii", i, gArea[ j ][ id ] );								}								if ( !IsPlayerInCheckpoint( i ) && GetPlayerCheckpoint( i ) == gArea[ j ][ id ] ) 								{									gPlayer[ i ][ player_checkpoint ] = INVALID_CHECKPOINT;									CallRemoteFunction( "OnPlayerLeaveCheckpointEx", "ii", i, gArea[ j ][ id ] );										}																	}						} 										} 					else 					{						if ( gArea[ j ][ id ] == GetPlayerArea( i ) ) 						{							if ( gArea[ j ][ area_hascp ] ) 							{								DisablePlayerCheckpoint( i ); 																				gPlayer[ i ][ player_checkpoint ] = INVALID_CHECKPOINT;													}							gPlayer[ i ][ player_area ] = INVALID_AREA;							CallRemoteFunction( "OnPlayerLeaveArea", "ii", i, gArea[ j ][ id ] );						}					}				}			}		}    		else		{			gPlayer[ i ][ player_checkpoint ] = INVALID_CHECKPOINT;									gPlayer[ i ][ player_area ] = INVALID_AREA;				}	}	SetTimer( "CheckSpheres", 500, 0 );}