Skip to content

Instantly share code, notes, and snippets.

@CodeAlDente
Last active January 24, 2018 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeAlDente/7bd3b2d8ca2427b2cdc73c850be91497 to your computer and use it in GitHub Desktop.
Save CodeAlDente/7bd3b2d8ca2427b2cdc73c850be91497 to your computer and use it in GitHub Desktop.
Open toll gate if player is in range, keep it open while player is still in range (with trailer), close it only if player leaves the range (e.g. drives away, died, left server)
// This file holds all functions for the toll-system
forward Toll();
public Toll()
{
// Loop through all players
for(new playerid; playerid < MAX_PLAYERS; playerid++)
{
// If the player isn't connected, skip to the next player
if (APlayerData[playerid][LoggedIn] == false)
continue;
// Check if the player is the driver of a vehicle
if (GetPlayerState(playerid) != PLAYER_STATE_DRIVER)
continue;
// Loop through all toll gates
for (new TollGate; TollGate < MAX_TOLLGATES; TollGate++)
{
// Check if this toll gate exists and if it is closed
if (ATollGates[TollGate][GateID] != 0 && ATollGates[TollGate][GateStatus] == 0)
{
// Check if the player is in range of the toll gate
if (IsPlayerInRangeOfPoint(playerid, 7.5, ATollGates[TollGate][CloseX], ATollGates[TollGate][CloseY], ATollGates[TollGate][CloseZ]))
{
new string[50];
// Open the toll gate
MoveObject(ATollGates[TollGate][GateID], ATollGates[TollGate][OpenX], ATollGates[TollGate][OpenY], ATollGates[TollGate][OpenZ], 3.0);
// Set status to OPEN
ATollGates[TollGate][GateStatus] = 1;
ATollGates[TollGate][OpenForPlayer] = playerid;
// Let the player pay the toll
RewardPlayer(playerid, -ATollGates[TollGate][TollPrice], 0);
format(string, sizeof(string), TXT_PlayerPaysToll, ATollGates[TollGate][TollPrice]);
GameTextForPlayer(playerid, string, 3000, 4);
// Start a timer that checks every 5 seconds if the toll gate can be closed
new TollGateTimer = SetTimerEx("CloseGate", 5000, true, "ii", TollGate, TollGateTimer);
}
}
}
}
}
forward CloseGate(TollGate, TollGateTimer);
public CloseGate(TollGate, TollGateTimer)
{
new inRange = 1;
if (0 != GetVehicleTrailer(GetPlayerVehicleID(ATollGates[TollGate][OpenForPlayer])))
{
if (7.5 <= GetVehicleDistanceFromPoint(GetVehicleTrailer(GetPlayerVehicleID(ATollGates[TollGate][OpenForPlayer])), ATollGates[TollGate][CloseX], ATollGates[TollGate][CloseY], ATollGates[TollGate][CloseZ]))
inRange = 0;
}
else
if (!IsPlayerInRangeOfPoint(ATollGates[TollGate][OpenForPlayer], 7.5, ATollGates[TollGate][CloseX], ATollGates[TollGate][CloseY], ATollGates[TollGate][CloseZ]))
inRange = 0;
if (0 == inRange)
{
// Close the toll gate
MoveObject(ATollGates[TollGate][GateID], ATollGates[TollGate][CloseX], ATollGates[TollGate][CloseY], ATollGates[TollGate][CloseZ], 3.0);
// Set status to CLOSE
ATollGates[TollGate][GateStatus] = 0;
ATollGates[TollGate][OpenForPlayer] = INVALID_PLAYER_ID;
KillTimer(TollGateTimer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment