train driver fixes

This commit is contained in:
Lorenc Pekaj 2018-09-19 17:51:10 +10:00
parent ea170efba9
commit 875bc6c7b2
2 changed files with 76 additions and 49 deletions

View File

@ -26,7 +26,7 @@ enum E_AIRPORT_DATA
Float: E_X, Float: E_Y, Float: E_Z,
};
new
static stock
g_DropOffLocations[ ] [ E_PILOT_DATA ] =
{
// SF

View File

@ -12,8 +12,8 @@
#define IsVehicleTrain(%0) (GetVehicleModel(%0) == 538)
/* ** Definitions ** */
#define INVALID_TRAIN_ROUTE ( 0xFF )
#define LOADING_SPEED ( 5 ) // in mph
#define INVALID_TRAIN_ROUTE ( 0xFF ) // keep this as 0xFF since -1 will not work in a char array
#define LOADING_SPEED ( 30 ) // in mph
/* ** Variables ** */
enum E_STATION_DATA
@ -22,7 +22,7 @@ enum E_STATION_DATA
Float: E_X, Float: E_Y, Float: E_Z
};
new
static stock
g_StationData[ ] [ E_STATION_DATA ] =
{
{ "Prickle Pine LV", 1474.3666, 2634.2493, 10.8203 },
@ -34,11 +34,11 @@ new
bool: p_hasTrainJob [ MAX_PLAYERS char ],
p_TrainMapIcon [ MAX_PLAYERS ] = { 0xFFFF, ... },
p_TrainCheckPoint [ MAX_PLAYERS ] = { 0xFFFF, ... },
p_TrainPositionTimer [ MAX_PLAYERS ] = { 0xFFFF, ... },
p_TrainLoadTimer [ MAX_PLAYERS ] = { 0xFFFF, ... },
p_TrainCancelTimer [ MAX_PLAYERS ] = { 0xFFFF, ... },
p_TrainMapIcon [ MAX_PLAYERS ] = { -1, ... },
p_TrainCheckPoint [ MAX_PLAYERS ] = { -1, ... },
p_TrainPositionTimer [ MAX_PLAYERS ] = { -1, ... },
p_TrainLoadTimer [ MAX_PLAYERS ] = { -1, ... },
p_TrainCancelTimer [ MAX_PLAYERS ] = { -1, ... },
p_TrainTimeElapsed [ MAX_PLAYERS ],
Float: p_TrainDistance [ MAX_PLAYERS ],
@ -71,26 +71,26 @@ hook OnPlayerDeath( playerid, killerid, reason )
return 1;
}
hook OnPlayerUpdateEx( playerid )
/*hook OnPlayerUpdateEx( playerid )
{
new player_vehicle = GetPlayerVehicleID( playerid );
if ( GetPlayerState( playerid ) == PLAYER_STATE_DRIVER && player_vehicle && p_hasTrainJob{ playerid } && !IsVehicleTrain( player_vehicle ) && p_TrainCancelTimer[ playerid ] == 0xFFFF ) {
if ( GetPlayerState( playerid ) == PLAYER_STATE_DRIVER && player_vehicle && p_hasTrainJob{ playerid } && !IsVehicleTrain( player_vehicle ) && p_TrainCancelTimer[ playerid ] == -1 ) {
cancelPlayerTrainWork( playerid, player_vehicle, .ticks = 60 );
}
return 1;
}
}*/
hook OnPlayerStateChange( playerid, newstate, oldstate )
{
if ( newstate != PLAYER_STATE_DRIVER && oldstate == PLAYER_STATE_DRIVER && p_hasTrainJob{ playerid } ) {
cancelPlayerTrainWork( playerid, GetPlayerVehicleID( playerid ), .ticks = 0 );
KillTimer( p_TrainCancelTimer[ playerid ] ); // just in-case
cancelPlayerTrainWork( playerid, GetPlayerVehicleID( playerid ), 15 );
}
else if ( newstate == PLAYER_STATE_DRIVER && oldstate != PLAYER_STATE_DRIVER && p_hasTrainJob{ playerid } ) {
KillTimer( p_TrainCancelTimer[ playerid ] );
p_TrainCancelTimer[ playerid ] = 0xFFFF;
p_TrainCancelTimer[ playerid ] = -1;
}
else if ( newstate == PLAYER_STATE_DRIVER && IsPlayerInAnyVehicle(playerid) && !p_hasTrainJob{ playerid })
@ -104,6 +104,11 @@ hook OnPlayerStateChange( playerid, newstate, oldstate )
return 1;
}
CMD:getspeed(playerid,params[]){
SendServerMessage( playerid, "You are travelling at %d", Train_GetSpeed( GetPlayerVehicleID( playerid ) ));
return 1;
}
hook OnPlayerEnterDynRaceCP( playerid, checkpointid )
{
if ( checkpointid == p_TrainCheckPoint[ playerid ] )
@ -120,27 +125,28 @@ hook OnPlayerEnterDynRaceCP( playerid, checkpointid )
{
static aPlayer[ 1 ]; aPlayer[ 0 ] = playerid;
p_TrainCancelTimer[ playerid ] = SetTimerEx( "OnTrainPositionUpdate", 750, false, "dd", playerid, p_TrainRoute[ playerid ] { 1 } );
KillTimer( p_TrainPositionTimer[ playerid ] ); // just in-case
p_TrainPositionTimer[ playerid ] = SetTimerEx( "OnTrainPositionUpdate", 750, false, "dd", playerid, p_TrainRoute[ playerid ] { 1 } );
PlayerTextDrawShow( playerid, p_TruckingTD[ playerid ] );
KillTimer( p_TrainLoadTimer[ playerid ] );
p_TrainLoadTimer [ playerid ] = 0xFFFF;
p_TrainLoadTimer[ playerid ] = SetTimerEx( "OnTrainLoadPassengers", 5000, false, "d", playerid );
TogglePlayerControllable( playerid, false );
ShowPlayerHelpDialog( playerid, 5000, "Please wait while your passengers are getting inside your train!" );
p_TrainMapIcon [ playerid ] = CreateDynamicMapIconEx( g_StationData[ p_TrainRoute[ playerid ] { 1 } ] [ E_X ], g_StationData[ p_TrainRoute[ playerid ] { 1 } ] [ E_Y ], g_StationData[ p_TrainRoute[ playerid ] { 1 } ] [ E_Z ], 51, 0, MAPICON_GLOBAL, 6000.0, { -1 }, { -1 }, aPlayer );
p_TrainCheckPoint [ playerid ] = CreateDynamicRaceCP( 0, g_StationData[ p_TrainRoute[ playerid ] { 1 } ] [ E_X ], g_StationData[ p_TrainRoute[ playerid ] { 1 } ] [ E_Y ], g_StationData[ p_TrainRoute[ playerid ] { 1 } ] [ E_Z ], 15.0, 0.0, 0.0, 10.0, -1, -1, playerid );
return ( p_TrainRoute[ playerid ] { 0 } = INVALID_TRAIN_ROUTE ), 1;
p_TrainRoute[ playerid ] { 0 } = INVALID_TRAIN_ROUTE;
Streamer_Update( playerid );
return 1;
}
else if ( p_TrainRoute[ playerid ] { 1 } != INVALID_TRAIN_ROUTE )
{
new
iCashEarned = floatround( p_TrainDistance[ playerid ] * 1.0 );
iCashEarned = floatround( p_TrainDistance[ playerid ] * 1.4 );
GivePlayerScore( playerid, 1 + floatround( p_TrainDistance[ playerid ] / 1000.0 ) );
GivePlayerCash( playerid, iCashEarned );
@ -148,7 +154,8 @@ hook OnPlayerEnterDynRaceCP( playerid, checkpointid )
ShowPlayerHelpDialog( playerid, 5000, "You have earned ~y~%s ~w~for transporting passengers!", cash_format( iCashEarned ) );
StopPlayerTrainWork( playerid );
return ( p_TrainRoute[ playerid ] { 1 } = INVALID_TRAIN_ROUTE ), 1;
p_TrainRoute[ playerid ] { 1 } = INVALID_TRAIN_ROUTE;
return 1;
}
return 1;
@ -175,21 +182,19 @@ stock StopPlayerTrainWork( playerid )
{
DestroyDynamicRaceCP ( p_TrainCheckPoint[ playerid ] );
DestroyDynamicMapIcon ( p_TrainMapIcon[ playerid ] );
KillTimer ( p_TrainCancelTimer[ playerid ] );
KillTimer ( p_TrainPositionTimer[ playerid ] );
p_TrainDistance [ playerid ] = 0.0;
p_hasTrainJob { playerid } = false;
p_TrainCheckPoint [ playerid ] = 0xFFFF;
p_TrainMapIcon [ playerid ] = 0xFFFF;
p_TrainCancelTimer [ playerid ] = 0xFFFF;
p_TrainLoadTimer [ playerid ] = 0xFFFF;
p_TrainPositionTimer [ playerid ] = 0xFFFF;
p_TrainRoute [ playerid ] { 0 } = INVALID_PILOT_ROUTE;
p_TrainRoute [ playerid ] { 1 } = INVALID_PILOT_ROUTE;
p_TrainCheckPoint [ playerid ] = -1;
p_TrainMapIcon [ playerid ] = -1;
p_TrainCancelTimer [ playerid ] = -1;
p_TrainLoadTimer [ playerid ] = -1;
p_TrainPositionTimer [ playerid ] = -1;
p_TrainRoute [ playerid ] { 0 } = INVALID_TRAIN_ROUTE;
p_TrainRoute [ playerid ] { 1 } = INVALID_TRAIN_ROUTE;
PlayerTextDrawHide( playerid, p_TruckingTD[ playerid ] );
return 1;
}
function OnTrainLoadPassengers( playerid )
@ -203,28 +208,25 @@ function OnTrainLoadPassengers( playerid )
TogglePlayerControllable( playerid, true );
ShowPlayerHelpDialog( playerid, 7500, "Great! All of your passengers are sitting comfortably!" );
return KillTimer( p_TrainLoadTimer[ playerid ] ), p_TrainLoadTimer[ playerid ] = 0xFFFF, 1;
return KillTimer( p_TrainLoadTimer[ playerid ] ), p_TrainLoadTimer[ playerid ] = -1, 1;
}
function OnTrainPositionUpdate( playerid, routeid )
{
if ( !IsPlayerInAnyVehicle( playerid ) && !p_hasTrainJob{ playerid } && ( p_TrainRoute[ playerid ] { 0 } == 0 && p_TrainRoute[ playerid ] { 1 } == 0 ) ) {
PlayerTextDrawHide( playerid, p_TruckingTD[ playerid ] );
return ( p_TrainPositionTimer[ playerid ] = 0xFFFF );
if ( ! IsPlayerInAnyVehicle( playerid ) && ! p_hasTrainJob{ playerid } ) {
return StopPlayerTrainWork( playerid );
}
new
Float: fDistance = GetPlayerDistanceFromPoint( playerid, g_StationData[ routeid ][ E_X ] , g_StationData[ routeid ][ E_Y ] , g_StationData[ routeid ][ E_Z ] );
PlayerTextDrawSetString( playerid, p_TruckingTD[ playerid ], sprintf( "~b~Location:~w~ %s~n~~b~Distance:~w~ %0.2fm", g_StationData[ routeid ] [ E_NAME ], fDistance ) );
return ( p_TrainPositionTimer[ playerid ] = SetTimerEx( "OnTrainPositionUpdate", 750, false, "dd", playerid, routeid ) );
p_TrainPositionTimer[ playerid ] = SetTimerEx( "OnTrainPositionUpdate", 750, false, "dd", playerid, routeid );
return 1;
}
function cancelPlayerTrainWork( playerid, vehicleid, ticks )
{
if ( IsVehicleTrain( vehicleid ) && ticks )
return KillTimer( p_TrainCancelTimer[ playerid ] ), p_TrainCancelTimer[ playerid ] = 0xFFFF;
if ( ticks < 1 || !IsPlayerConnected( playerid ) || !IsPlayerSpawned( playerid ) || ! p_hasTrainJob{ playerid } )
{
StopPlayerTrainWork( playerid );
@ -235,7 +237,6 @@ function cancelPlayerTrainWork( playerid, vehicleid, ticks )
ShowPlayerHelpDialog( playerid, 1000, "You have %d seconds to get back inside the train you were using.", ticks - 1 );
p_TrainCancelTimer[ playerid ] = SetTimerEx( "cancelPlayerTrainWork", 980, false, "ddd", playerid, vehicleid, ticks - 1 );
}
return 1;
}
@ -264,19 +265,29 @@ CMD:train( playerid, params[ ] )
static aPlayer[ 1 ]; aPlayer[ 0 ] = playerid;
DestroyDynamicMapIcon( p_TrainMapIcon[ playerid ] );
DestroyDynamicRaceCP( p_TrainCheckPoint[ playerid ] );
p_hasTrainJob { playerid } = true;
p_WorkCooldown [ playerid ] = g_iTime + 60;
p_TrainRoute [ playerid ] { 0 } = random ( sizeof( g_StationData ) );
p_TrainRoute [ playerid ] { 1 } = random ( sizeof( g_StationData ) );
new
valid_train_routes[ sizeof( g_StationData ) ] = { -1, ... }; // -1 means valid
p_TrainRoute [ playerid ] { 0 } = GetClosestTrainStation( playerid ); // get the closest to the player first
valid_train_routes [ p_TrainRoute[ playerid ] { 0 } ] = p_TrainRoute[ playerid ] { 0 };
// ensure second route is not the same as first route
p_TrainRoute [ playerid ] { 1 } = randomExcept( valid_train_routes, sizeof( valid_train_routes ), .available_element_value = -1 );
p_TrainTimeElapsed [ playerid ] = g_iTime;
p_TrainDistance [ playerid ] = GetDistanceBetweenPoints( g_StationData[ p_TrainRoute[ playerid ] { 0 } ] [ E_X ], g_StationData[ p_TrainRoute[ playerid ] { 0 } ] [ E_Y ], g_StationData[ p_TrainRoute[ playerid ] { 0 } ] [ E_Z ], g_StationData[ p_TrainRoute[ playerid ] { 1 } ] [ E_X ], g_StationData[ p_TrainRoute[ playerid ] { 1 } ] [ E_Y ], g_StationData[ p_TrainRoute[ playerid ] { 1 } ] [ E_Z ] );
p_TrainMapIcon [ playerid ] = CreateDynamicMapIconEx( g_StationData[ p_TrainRoute[ playerid ] { 0 } ] [ E_X ], g_StationData[ p_TrainRoute[ playerid ] { 0 } ] [ E_Y ], g_StationData[ p_TrainRoute[ playerid ] { 0 } ] [ E_Z ], 51, 0, MAPICON_GLOBAL, 6000.0, { -1 }, { -1 }, aPlayer );
p_TrainCheckPoint [ playerid ] = CreateDynamicRaceCP( 0, g_StationData[ p_TrainRoute[ playerid ] { 0 } ] [ E_X ], g_StationData[ p_TrainRoute[ playerid ] { 0 } ] [ E_Y ], g_StationData[ p_TrainRoute[ playerid ] { 0 } ] [ E_Z ], 0.0, 0.0, 0.0, 10.0, -1, -1, playerid );
p_PilotPositionTimer[ playerid ] = SetTimerEx( "OnTrainPositionUpdate", 750, false, "dd", playerid, p_TrainRoute[ playerid ] { 0 } );
Streamer_Update( playerid );
KillTimer( p_TrainPositionTimer[ playerid ] );
p_TrainPositionTimer[ playerid ] = SetTimerEx( "OnTrainPositionUpdate", 750, false, "dd", playerid, p_TrainRoute[ playerid ] { 0 } );
PlayerTextDrawShow( playerid, p_TruckingTD[ playerid ] );
}
else SendError( playerid, "You already have a train job started! Cancel it with "COL_GREY"/train stop"COL_WHITE"." );
@ -287,3 +298,19 @@ CMD:train( playerid, params[ ] )
}
return 1;
}
stock GetClosestTrainStation( playerid, &Float: distance = FLOAT_INFINITY )
{
new
iCurrent = -1, Float: fTmp;
for ( new i = 0; i < sizeof( g_StationData ); i ++ )
{
if ( 0.0 < ( fTmp = GetPlayerDistanceFromPoint( playerid, g_StationData[ i ] [ E_X ], g_StationData[ i ] [ E_Y ], g_StationData[ i ] [ E_Z ] ) ) < distance )
{
distance = fTmp;
iCurrent = i;
}
}
return iCurrent;
}