All pastes #961787 Raw Edit

propertyfs

public text v1 · immutable
#961787 ·published 2008-03-29 05:17 UTC
rendered paste body
#include <a_samp>

#define FILTERSCRIPT

#define PROP_VW    (10000)
#define MAX_INTERIORS	(146)
#define MAX_PROPERTIES  (400)

#define PROPERTY_FILENAME	"properties/properties.txt" // Location of properties file
#define PROPERTY_UNIQID_FILE    "properties/uniqId.txt" // Location of Uniq Interior Info

#define MAX_TYPES       (5)
#define TYPE_EMPTY      (0)
#define TYPE_HOUSE 		(1)
#define TYPE_BUSINESS	(2)
#define TYPE_BANK   	(3)
#define TYPE_COP        (4)

enum // Property Type Enum
	E_P_TYPES {
		tIcon,
		tName[32]
	}

enum // Uniq Interiors Enum
	E_INTERIORS {
		inIntID,
		Float:inExitX,
		Float:inExitY,
		Float:inExitZ,
		Float:inExitA,
		inName[64]
	};

enum // Properties Enum
	E_PROPERTIES {
		eInterior,
		eType,
		Float:eEntX,
		Float:eEntY,
		Float:eEntZ,
		Float:eEntA,
		Float:eExitX,
		Float:eExitY,
		Float:eExitZ,
		Float:eExitA,
		eOwner,
		ePrice,
		ePname[64]
	};

new
	unid,
	interiorInfo[MAX_INTERIORS][E_INTERIORS],
	propPickups[MAX_PROPERTIES] = {-1},
	properties[MAX_PROPERTIES][E_PROPERTIES],
	lastPickup[MAX_PLAYERS] = {-1},
	currentInt[MAX_PLAYERS] = {-1},
	Float:plPos[MAX_PLAYERS][3],
	plInt[MAX_PLAYERS];
	
new
	propIcons[MAX_TYPES][E_P_TYPES] =	{
										{ 0, "" }, 				// TYPE_EMPTY ( not used )
										{ 1273, "House" }, 		// TYPE_HOUSE green house icon
										{ 1272, "Business" }, 	// TYPE_BUSINESS blue house icon
										{ 1274, "Bank" }, 		// TYPE_BANK dollar sign icon
										{ 1247, "Police Station" } // Unknown Bribe Star
										};





// Property Variable Functions ----------------------------------------
stock Float:GetPropertyEntrance( id, &Float:x, &Float:y, &Float:z ){
	if( id > MAX_PROPERTIES ) return 0.0;
	x = properties[id][eEntX];
	y = properties[id][eEntY];
	z = properties[id][eEntZ];
	return properties[id][eEntA];
}

stock Float:GetPropertyExit( id, &Float:x, &Float:y, &Float:z ){
	if( id > MAX_PROPERTIES ) return 0.0;
	x = properties[id][eExitX];
	y = properties[id][eExitY];
	z = properties[id][eExitZ];
	return properties[id][eExitA];
}

stock GetPropertyInteriorId( id ){
	if( id > MAX_PROPERTIES ) return 0;
	else return properties[id][eInterior];
}

stock GetPropertyType( id ){
	if( id > MAX_PROPERTIES ) return 0;
	else return properties[id][eType];
}

stock GetPropertyOwner( id ){
	if( id > MAX_PROPERTIES ) return -1;
	else return properties[id][eOwner];
}

stock GetPropertyPrice( id ){
	if( id > MAX_PROPERTIES ) return -1;
	else return properties[id][ePrice];
}

stock GetPropertyName( id ){
	new tmp[64];
	if( id > MAX_PROPERTIES ) return tmp;
	
	else {
  		format( tmp, 64, "%s", properties[id][ePname] );
		return tmp;
	}
}

//-------------------------------------------------------------------

// Interior Variable Functions --------------------------------------
stock Float:GetInteriorExit( id, &Float:x, &Float:y, &Float:z ){
	if( id > MAX_INTERIORS ) return 0.0;
	else {
	    x = interiorInfo[id][inExitX];
	    y = interiorInfo[id][inExitY];
	    z = interiorInfo[id][inExitZ];
		return interiorInfo[id][inExitA];
	}
}

stock GetInteriorIntID( id ){
	if( id > MAX_INTERIORS ) return -1;
	else return interiorInfo[id][inIntID];
}

stock GetInteriorName( id ){
	new tmp[64];
	if( id > MAX_PROPERTIES ) return tmp;

	else {
  		format( tmp, 64, "%s", interiorInfo[id][inName] );
		return tmp;
	}
}

//--------------------------------------------------------------------

#if defined FILTERSCRIPT

public OnFilterScriptInit()
{
	print("\n--------------------------------------");
	print("damospidermans property filterscript");
	print("--------------------------------------\n");
	
	unid = 0;
   	for( new i = 0; i < MAX_PROPERTIES; i++ ){
   	    properties[i][eType] = TYPE_EMPTY;
	}
	ReadInteriorInfo( "properties/interiors.txt" );
   	ReadPropertyFile( PROPERTY_FILENAME );
	return 1;
}

public OnFilterScriptExit()
{
	for( new i = 0; i < MAX_PROPERTIES; i++ ){
	    if( propPickups[i] != -1 ){
	        DestroyPickup( i );
	        propPickups[i] = -1;
		}
	}
	return 1;
}

#else

main()
{
	print("\n----------------------------------");
	print(" Blank Gamemode by your name here");
	print("----------------------------------\n");
}

#endif

public OnGameModeInit()
{
	unid = 0;
   	for( new i = 0; i < MAX_PROPERTIES; i++ ){
   	    properties[i][eType] = TYPE_EMPTY;
	}
	
	ReadInteriorInfo( "properties/interiors.txt" );
   	ReadPropertyFile( PROPERTY_FILENAME );
	return 1;
}

public OnGameModeExit()
{
	for( new i = 0; i < MAX_PROPERTIES; i++ ){
	    if( propPickups[i] != -1 ){
	        DestroyPickup( i );
	        propPickups[i] = -1;
		}
	}
	return 1;
}

public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
{
	if( newinteriorid == 0 ){
		currentInt[playerid] = -1;
	}
	return 1;
}

public OnPlayerSpawn( playerid )
{
	PutPlayerInProperty( playerid, 2 );
	return 1;
}
public OnPlayerPickUpPickup(playerid, pickupid)
{
	printf( "DEBUG: Player %d pickedup Pickup %d Prop Id %d", playerid, pickupid );
	lastPickup[playerid] = pickupid;
	new id = propPickups[pickupid];
	if( properties[id][eType] > 0 ){

		switch( properties[id][eType] ){
		    case TYPE_HOUSE:{
		        // Insert House Crap Here
		        SendClientMessage( playerid, 0x550099FF, "House Property.. type /enter to enter" );
		        return 1;
			}
			
			case TYPE_BUSINESS:{
			    // Insert Business Crap Here
       			SendClientMessage( playerid, 0x550099FF, "Business Property.. type /enter to enter" );
		        return 1;
			}
			
			case TYPE_BANK:{
				// Insert Bank Crap Here
    			SendClientMessage( playerid, 0x550099FF, "Bank.. type /enter to enter" );
		        return 1;
			}
			
			case TYPE_COP:{
				// Insert Cop Crap Here
    			SendClientMessage( playerid, 0x550099FF, "Police Station.. type /enter to enter" );
		        return 1;
			}
		}
		
	}
	else SendClientMessage( playerid, 0xFF9900FF, "This property doesn't exist :S" );
	return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
	new idx;
	new cmd[256];

	cmd = strtok(cmdtext, idx);

	if (strcmp("/chouse", cmd, true) == 0)
	{
		PropertyCommand( playerid, cmd, cmdtext,idx, TYPE_HOUSE );
		return 1;
	}

	else if (strcmp("/cbus", cmd, true) == 0)
	{
 	    PropertyCommand( playerid, cmd, cmdtext,idx, TYPE_BUSINESS );
 	    return 1;
	}
	
	else if (strcmp("/ccop", cmd, true) == 0)
	{
		PropertyCommand( playerid, cmd, cmdtext,idx, TYPE_COP );
		return 1;
	}
	
	else if (strcmp("/cbank", cmd, true) == 0)
	{
		PropertyCommand( playerid, cmd, cmdtext,idx, TYPE_BANK );
		return 1;
	}
	
	else if(strcmp( "/view", cmd, true ) == 0) //Basically lets you view an interior from the interiors.txt file by id
	{
	
	    new
			tmp[256],
			string[128],
			uniqId,
			Float:x,
			Float:y,
			Float:z,
			Float:a;
		tmp = strtok( cmdtext, idx );
		if(!strlen(tmp)){
		    format( string, 128, "Usage: %s [uniqInteriorId]", cmd );
		    SendClientMessage( playerid, 0xFF00CC, string );
		    
		    return 1;
		}
		if(!isNumeric(tmp)){
		    SendClientMessage(playerid, 0x550000, "Uniq Interior Id must be a number" );
		    return 1;
		}
		uniqId = strval( tmp );

		if( uniqId > MAX_INTERIORS || uniqId < 0 ){
			SendClientMessage( playerid, 0xFFFFCC, "Invalid Uniq Interior Id" );
			return 1;
		}
		if( GetPlayerInterior( playerid ) == 0 ){
			GetPlayerPos( playerid, plPos[playerid][0], plPos[playerid][1], plPos[playerid][2] );
			plInt[playerid] = GetPlayerInterior( playerid );
		}
		a = GetInteriorExit( uniqId, x, y, z );
		SetPlayerInterior( playerid, GetInteriorIntID( uniqId ) );
		SetPlayerPos( playerid, x, y, z );
		SetPlayerFacingAngle( playerid, a );
		format( string, 128, "UniqId: %d InteriorId: %d Name: %s | Use /return to go to last position", uniqId,GetInteriorIntID( uniqId ), GetInteriorName( uniqId ));
		SendClientMessage( playerid, 0x556600FF, string );
		return 1;
	}
	
	else if( strcmp( "/return", cmd, true ) == 0 )
	{
	    SetPlayerPos( playerid, plPos[playerid][0], plPos[playerid][1], plPos[playerid][2] );
	    SetPlayerInterior( playerid, plInt[playerid] );
		return 1;
	}
	
	else if (strcmp("/enter", cmd, true) == 0)
	{
		if( lastPickup[playerid] != -1 || properties[lastPickup[playerid]][eType] > 0 ){
		    new
		        id = propPickups[lastPickup[playerid]],
		        Float:x,
		        Float:y,
		        Float:z;

			GetPropertyEntrance( id, x, y, z );
	    	if( IsPlayerInRangeOfPoint( playerid, 3.0, x, y, z )){
	    	    PutPlayerInProperty( playerid, id );
	    	    SendClientMessage( playerid, 0x005599FF, "You have entered a property.. type /exit to leave" );
	    	    return 1;
			}
		}
		return 1;
	}

	else if (strcmp("/exit", cmd, true) == 0)
	{
	    if( currentInt[playerid] > -1 && GetPlayerInterior(playerid) == GetPropertyInteriorId( currentInt[playerid] )){
	        new
	            id = currentInt[playerid],
				Float:x,
				Float:y,
				Float:z,
				Float:a;

			a = GetPropertyEntrance( id, x, y, z );
			SetPlayerPos( playerid, x, y, z );
			SetPlayerFacingAngle( playerid, a );
			SetPlayerInterior( playerid, 0 );
			SetPlayerVirtualWorld( playerid, 0 );
		}
		currentInt[playerid] = -1;
		return 1;
	}

	return 0;
}



/**
----------------------------------------------------------------------------
* Property System Functions
*---------------------------------------------------------------------------
*/

ReadInteriorInfo( fileName[] ){
	new
	    File:file_ptr,
	    buf[256],
	    tmp[64],
	    idx,
		uniqId;


	file_ptr = fopen( fileName, io_read );
	if( file_ptr ){
		while( fread( file_ptr, buf, 256 ) > 0){
		    idx = 0;

     		idx = token_by_delim( buf, tmp, ' ', idx );
			if(idx == (-1)) continue;
			uniqId = strval( tmp );

			if( uniqId >= MAX_INTERIORS ) return 0;

			idx = token_by_delim( buf, tmp, ' ', idx+1 );
		    if(idx == (-1)) continue;
		 	interiorInfo[uniqId][inIntID] = strval( tmp );

			idx = token_by_delim( buf, tmp, ' ', idx+1 );
		    if(idx == (-1)) continue;
			interiorInfo[uniqId][inExitX] = floatstr( tmp );

			idx = token_by_delim( buf, tmp, ' ', idx+1 );
		    if(idx == (-1)) continue;
			interiorInfo[uniqId][inExitY] = floatstr( tmp );

			idx = token_by_delim( buf, tmp, ' ', idx+1);
		    if(idx == (-1)) continue;
			interiorInfo[uniqId][inExitZ] = floatstr( tmp );

			idx = token_by_delim( buf, tmp, ' ', idx+1 );
		    if(idx == (-1)) continue;
			interiorInfo[uniqId][inExitA] = floatstr( tmp );

			idx = token_by_delim( buf, interiorInfo[uniqId][inName], ';', idx+1 );
		    if(idx == (-1)) continue;

			printf( "Added Prop - %d, %d, %f, %f, %f, %f ( %s )",
															uniqId,
															interiorInfo[uniqId][inIntID],
															interiorInfo[uniqId][inExitX],
															interiorInfo[uniqId][inExitY],
															interiorInfo[uniqId][inExitZ],
															interiorInfo[uniqId][inExitA],
															interiorInfo[uniqId][inName] );

		}
		printf( "Interiors File read successfully" );
		fclose( file_ptr );
		return 1;
	}
	printf( "Could Not Read Interiors file ( %s )", fileName );
	return 0;

}

// Gets interior exit info from uniq Interior Id. Returns InteriorId or -1 if interior doesn't exist
GetInteriorExitInfo( uniqIntId, &Float:exitX, &Float:exitY, &Float:exitZ, &Float:exitA ){
	if( uniqIntId < MAX_INTERIORS ){
	    exitX = interiorInfo[uniqIntId][inExitX];
	    exitY = interiorInfo[uniqIntId][inExitY];
	    exitZ = interiorInfo[uniqIntId][inExitZ];
	    exitA = interiorInfo[uniqIntId][inExitA];
		return interiorInfo[uniqIntId][inIntID];
	}
	return -1;
}

PutPlayerInProperty( playerid, propId, propVW = 0 ){
	new
	    Float:x,
	    Float:y,
	    Float:z,
	    Float:a;
    a = GetPropertyExit( propId, x, y, z );
	SetPlayerPos( playerid, x, y, z );
	SetPlayerFacingAngle( playerid, a );
	SetPlayerInterior( playerid, GetPropertyInteriorId( propId ));
	SetPlayerVirtualWorld( playerid, (propVW==0)? propId+PROP_VW:propVW );
	currentInt[playerid] = propId;
}
	
    


CreateProperty( iconId, Float:entX, Float:entY, Float:entZ, Float:entA, interior, Float:exitX, Float:exitY, Float:exitZ, Float:exitA, p_type ){

	new Id = CreatePickup( iconId ,23, entX, entY, entZ );
	propPickups[Id] = unid;

	properties[unid][eInterior] = interior;
	properties[unid][eEntX] 	= entX;
	properties[unid][eEntY] 	= entY;
	properties[unid][eEntZ] 	= entZ;
	properties[unid][eEntA] 	= entA;
	properties[unid][eExitX] 	= exitX;
	properties[unid][eExitY] 	= exitY;
	properties[unid][eExitZ] 	= exitZ;
	properties[unid][eExitA] 	= exitA;
	properties[unid][eOwner] 	= -1;
	properties[unid][eType] 	= p_type;
	return unid++;
}

ReadPropertyFile( fileName[] ){
	new
	    File:file_ptr,
	    tmp[128],
		buf[256],
		idx,
		Float:enX,
		Float:enY,
		Float:enZ,
		Float:enA,
		Float:exX,
		Float:exY,
		Float:exZ,
		Float:exA,
		interior,
		p_type,
		pIcon;
	print("Reading Files" );
	file_ptr = fopen( fileName, io_read );

	if(!file_ptr )return 0;

 	while( fread( file_ptr, buf, 256 ) > 0){
 	    idx = 0;
 	    
 	    idx = token_by_delim( buf, tmp, ',', idx );
		if(idx == (-1)) continue;
		pIcon = strval( tmp );

 	    idx = token_by_delim( buf, tmp, ',', idx+1 );
		if(idx == (-1)) continue;
		enX = floatstr( tmp );

  		idx = token_by_delim( buf, tmp, ',', idx+1 );
		if(idx == (-1)) continue;
		enY = floatstr( tmp );

		idx = token_by_delim( buf, tmp, ',', idx+1 );
		if(idx == (-1)) continue;
		enZ = floatstr( tmp );

 		idx = token_by_delim( buf, tmp, ',', idx+1 );
		if(idx == (-1)) continue;
		enA = floatstr( tmp );

		idx = token_by_delim( buf, tmp, ',', idx+1 );
		if(idx == (-1)) continue;
		interior = strval( tmp );

		idx = token_by_delim( buf, tmp, ',', idx+1 );
		if(idx == (-1)) continue;
		exX = floatstr( tmp );

		idx = token_by_delim( buf, tmp, ',', idx+1 );
		if(idx == (-1)) continue;
		exY = floatstr( tmp );

 		idx = token_by_delim( buf, tmp, ',', idx+1 );
		if(idx == (-1)) continue;
		exZ = floatstr( tmp );

		idx = token_by_delim( buf, tmp, ',', idx+1 );
		if(idx == (-1)) continue;
		exA = floatstr( tmp );

		idx = token_by_delim( buf, tmp, ';', idx+1 );
		if(idx == (-1)) continue;
		p_type = strval( tmp );

		printf( "Creating Prop - %d, %f, %f, %f, %f, %d, %f, %f, %f, %f, %d", pIcon, enX, enY, enZ, enA, interior, exX, exY, exZ, exA, p_type );
		CreateProperty( pIcon, enX, enY, enZ, enA, interior, exX, exY, exZ, exA, p_type );

	}
	fclose( file_ptr );
	return 1;
}

// Adds new property to property file
AddProperty( uniqIntId, Float:entX, Float:entY, Float:entZ, Float:entA, p_type, fileName[] ){
	new
	    Float:exitX,
	    Float:exitY,
	    Float:exitZ,
	    Float:exitA,
		interiorId,
		File:file_ptr,
		tmp[128];

	interiorId = GetInteriorExitInfo( uniqIntId, exitX, exitY, exitZ, exitA );

	if( interiorId != -1 ){
	    file_ptr = fopen( fileName, io_append );
	    if(file_ptr){
			format( tmp, 128, "%d, %f, %f, %f, %f, %d, %f, %f, %f, %f, %d ;\r\n",
		 					propIcons[p_type][tIcon],entX, entY, entZ, entA, interiorId, exitX, exitY, exitZ, exitA, p_type );

			fwrite( file_ptr, tmp );
			fclose( file_ptr );
			printf( "PropDB - %s", tmp );
			return CreateProperty( propIcons[p_type][tIcon], entX, entY, entZ, entA, interiorId, exitX, exitY, exitZ, exitA, p_type );
		}
	}
	return -1;
}



PropertyCommand( playerid, cmd[],cmdtext[],idx, p_type ){

	if( GetPlayerInterior(playerid) != 0 || GetPlayerVirtualWorld(playerid)!= 0 ){
	    SendClientMessage(playerid, 0x550000FF, "You can only create properties in Interior 0 and VW 0" );
	    return 1;
	}
	
	new
	        Float:x,
	        Float:y,
	        Float:z,
	        Float:a,
	        tmp[256],
	        string[128],
			uniqId,
			id;

	GetPlayerPos( playerid, x, y, z );
	GetPlayerFacingAngle( playerid, a );

	tmp = strtok( cmdtext, idx );
	if(!strlen(tmp)){
	    format( string, 128, "Usage: %s [uniqInteriorId]", cmd );
	    SendClientMessage( playerid, 0xFF00CC, string );
	    return 1;
	}
	if(!isNumeric(tmp)){
	    SendClientMessage(playerid, 0x550000, "Uniq Interior Id must be a number" );
	    return 1;
	}
	uniqId = strval( tmp );

	if( uniqId > MAX_INTERIORS || uniqId < 0 ){
		SendClientMessage( playerid, 0xFFFFCC, "Invalid Uniq Interior Id" );
		return 1;
	}

	id = AddProperty( uniqId, x, y, z, a, p_type, PROPERTY_FILENAME );

	if( id != -1 ){
		format( tmp, 256, "Property Type ( %d ) Added Successfully: UniqId: %d Interior: %d IntName: %s",p_type, id, interiorInfo[uniqId][inIntID], interiorInfo[uniqId][inName] );
	    SendClientMessage( playerid, 0xCC7700, tmp );
	}else{
	    SendClientMessage( playerid, 0x00FF55, "Error: Something went wrong" );
	}
	return 1;
}

/**
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
*/

strtok(const string[], &index)
{
	new length = strlen(string);
	while ((index < length) && (string[index] <= ' '))
	{
		index++;
	}

	new offset = index;
	new result[20];
	while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
	{
		result[index - offset] = string[index];
		index++;
	}
	result[index - offset] = EOS;
	return result;
}

// Tokenise by a delimiter
// Return string and index of the end determined by the
// provided delimiter in delim
token_by_delim(const string[], return_str[], delim, start_index)
{
	new x=0;
	while(string[start_index] != EOS && string[start_index] != delim) {
	    return_str[x] = string[start_index];
	    x++;
	    start_index++;
	}
	return_str[x] = EOS;
	if(string[start_index] == EOS) start_index = (-1);
	return start_index;
}


isNumeric(const string[])
{
  new length=strlen(string);
  if (length==0) return false;
  for (new i = 0; i < length; i++)
    {
      if (
            (string[i] > '9' || string[i] < '0' && string[i]!='-' && string[i]!='+') // Not a number,'+' or '-'
             || (string[i]=='-' && i!=0)                                             // A '-' but not at first.
             || (string[i]=='+' && i!=0)                                             // A '+' but not at first.
         ) return false;
    }
  if (length==1 && (string[0]=='-' || string[0]=='+')) return false;
  return true;
}