03.09.2010, 11:26
Is this possible?
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
if ((newkeys & KEY_FIRE) && !(oldkeys & KEY_FIRE))
{
if(IsPlayerInAnyVehicle(playerid))
{
AddVehicleComponent(GetPlayerVehicleID(playerid), 1010);
}
}
}
forward nitro();
public nitro()
{
for(new i=0; i<MAX_PLAYERS; i++)
{
if(IsPlayerConnected(playerid))
{
if(IsPlayerInAnyVehicle(playerid))
{
AddVehicleComponent(GetPlayerVehicleID(playerid), 1010); //nitro
}
}
}
return 1;
}
//OnGameModeInit
SetTimer("nitro", 2000, 1);
it's possible with a timer:
pawn Код:
|
//Func to check if it's not something like a bike
static invalid_nos_vehicle[] =
{
430,446,448,449,452,453,454,461,462,463,468,472,473,481,484,
493,509,510,520,521,522,523,537,538,569,570,581,586,590,595,429,461
};
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
new Vid = GetPlayerVehicleId(playerid);
new model = GetVehicleModel(playerid);
new invalid_model;
for (new i = 0; i < sizeof(invalid_nos_vehicle); i++)
{
if(model == invalid_nos_vehicle[i]) invalid_model = 1;
}
if (GetPlayerState(playerid) == PLAYER_STATE_DRIVER && newkeys & KEY_FIRE)
{
if(!invalid_model) AddVehicleComponent(Vid,1010);
}
if (GetPlayerState(playerid) == PLAYER_STATE_DRIVER && oldkeys & KEY_FIRE)
{
if(!invalid_model) RemoveVehicleComponent(Vid,1010);
}
return 1;
}
I'd just do it like this
pawn Код:
|
It looks cooler when you release the key if it disappears :P. Also for the loop, I really CBF making it efficient because I wrote it up in a short period of time. Feel free to fix it .
|
new InvalidNosVehicles[] =
{
430,446,448,449,452,453,454,461,462,463,468,472,473,481,484,
493,509,510,520,521,522,523,537,538,569,570,581,586,590,595
};
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
if(newkeys & 4)
{
if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
{
new
VehcileID,
VehicleModelID,
bool:Invalid;
VehicleID = GetPlayerVehicleID(playerid);
VehicleModelID = GetVehicleModel(VehicleID);
for(new a=0; a<sizeof(InvalidNosVehicles); a++)
{
if(VehicleModelID == InvalidNosVehicles[a])
{
Invalid = true;
break;
}
}
if(!Invalid) AddVehicleComponent(VehicleID, 1010);
}
}
return 1;
}
I'd just do it like this
pawn Код:
|
//Func to check if it's not something like a bike
new invalid_nos_vehicle[] =
{
430,446,448,449,452,453,454,461,462,463,468,472,473,481,484,
493,509,510,520,521,522,523,537,538,569,570,581,586,590,595,429,461
};
#define NITRO 1010
#define NITRO_RECHARGE_TICK 12000
#define NITRO_TIMER_WORKING (NITRO_TIMER != -1)
#define IsPlayerConnectedEx(%1) (iter_index[(%1)] != -1)
//checks if a timer is working
new NITRO_TIMER = -1;
//optimised iteration
new
iter[MAX_PLAYERS] = { INVALID_PLAYER_ID, ... },
iter_index[MAX_PLAYERS] = { -1, ... },
NUM_PLAYERS;
new CHECK_RECHARGE_NITRO[ MAX_PLAYERS ];
new CHECK_KEY_NITRO[ MAX_PLAYERS ];
public OnPlayerConnect( playerid )
{
//optimised iteration
iter[ NUM_PLAYERS ] = playerid;
iter_index[ playerid ] = NUM_PLAYERS;
NUM_PLAYERS ++;
//reset the nitro keycheck value
CHECK_KEY_NITRO[ playerid ] = 0;
return 1;
}
public OnPlayerDisconnect( playerid, reason )
{
NUM_PLAYERS --;
// trims the iteration list
iter[ iter_index[ playerid ] ] = iter[ NUM_PLAYERS ];
iter_index[ iter[ NUM_PLAYERS ] ] = iter_index[ playerid ];
// reset the variable for next use
iter_index[ playerid ] = -1;
iter[ NUM_PLAYERS ] = INVALID_PLAYER_ID;
//reset the nitro_recheck value
CHECK_RECHARGE_NITRO[ playerid ] = 0;
}
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
// do something
Something();
if( !CHECK_KEY_NITRO[ playerid ] ) return 1;
//check it with nitro-compatible autos
if( (newkeys & KEY_FIRE) )
{
CHECK_RECHARGE_NITRO[ playerid ] = 1;
AddVehicleComponent( GetPlayerVehicleID(playerid), NITRO );
if( !NITRO_TIMER_WORKING )
{
//enables a timer to keep nitro working
NITRO_TIMER = SetTimer( "KeepNitro", NITRO_RECHARGE_TICK, 1 );
}
}
else if( (oldkeys & KEY_FIRE) )
{
CHECK_RECHARGE_NITRO[ playerid ] = 0;
RemoveVehicleComponent( GetPlayerVehicleID(playerid), NITRO );
}
return 1;
}
public OnPlayerStateChange( playerid, newstate, oldstate )
{
//invalid model check
if ( newstate == PLAYER_STATE_DRIVER )
{
new model = GetVehicleModel( GetPlayerVehicleID(playerid) );
CHECK_KEY_NITRO[ playerid ] = 1;
for (new i = 0; i < sizeof(invalid_nos_vehicle); i++)
{
if( model == invalid_nos_vehicle[i] )
{
CHECK_KEY_NITRO[ playerid ] = 0;
break;
}
}
}
else if( oldstate == PLAYER_STATE_DRIVER )
{
CHECK_KEY_NITRO[ playerid ] = 0;
CHECK_RECHARGE_NITRO[ playerid ] = 0;
}
}
forward public KeepNitro();
public KeepNitro()
{
new KEEP_TIMER;
for( new i = 0 ; i < NUM_PLAYERS ; i ++ )
{
#define playerid iter[i]
if( CHECK_RECHARGE_NITRO[ playerid ] )
{
new key, updown, leftright;
GetPlayerKeys( playerid, key, updown, leftright );
if( ( key & KEY_FIRE ) )
{
KEEP_TIMER = 1;
AddVehicleComponent( GetPlayerVehicleID(playerid), NITRO );
}
}
#undef playerid
}
if( !KEEP_TIMER )
{
KillTimer( NITRO_TIMER );
NITRO_TIMER = -1;
}
}
You're pretty much doing 3 connection checks + 2 vehicle checks right there lol.
Having it on a timer would be the worst way, as every time you use it a new bottle would be applied to your vehicle. Which would cut the nos off. You'd have to press fire every 2 seconds to use it lol. EDIT: As for the other poster, its not AS bad i guess. But you're still doing 2 connection checks, Not checking if the player is the driver, and not seeing if its a safe vehicle to apply nos on (Is this still a problem in 0.3b? Or will it through a warning?). I have no idea why you're even checking the oldkeys at all . |