I'm not normally this nice, but because it's a personal thing...
PHP код:
#define MAX_ROADBLOCKS 20 // MAX_ROADBLOCKS will be set to 20 by default. This means that a player can only place up to 20 roadblocks at a time.
enum PlayerBlocks
{
pRoadblock[MAX_ROADBLOCKS], // Creates an array that will hold the object IDs for 20 roadblocks. This number depends on your MAX_ROADBLOCKS.
}
new Player[MAX_PLAYERS][PlayerBlocks];
CMD:roadblock(playerid, params[]) { // Command /roadblock
new free_rb_id = 0; // Set the initial object ID to '0'. Object IDs start at 1, so 0 is considered an invalid object ID.
for(new i = 0; i < MAX_ROADBLOCKS; i++) { // Start looping through all 20 roadblocks that could potentially be placed by the player.
if(!Player[playerid][pRoadblock][i]) { // If the roadblock is '0', a.k.a. does not exist, then we have an available slot.
free_rb_id = i; // Store the slot/index as our 'free roadblock ID'.
break; // Break the loop to stop it from continuing, because we have already found a valid slot.
}
}
if(!free_rb_id) return SendClientMessage(playerid, -1, "You cannot place any more roadblocks. Please remove one first.");
// If 'free_rb_id' is still 0, it means that the value was never changed in the loop. This means that every roadblock slot was being used.
new Float:Pos[4];
GetPlayerPos(playerid, Pos[0], Pos[1], Pos[2]);
GetPlayerFacingAngle(playerid, Pos[3]);
Pos[0] += (3 * floatsin(-Pos[3], degrees));
Pos[1] += (3 * floatcos(-Pos[3], degrees));
Player[playerid][pRoadblock][free_rb_id] = CreateObject(978, Pos[0], Pos[1], Pos[2], 0.0, 0.0, Pos[3]);
// Store the created object's ID into the available slot.
return 1;
}
CMD:removeall(playerid, params[]) { // Command /removeall
new Float:obx, Float:oby, Float:obz; // Create floats to store the roadblock's position.
for(new i = 0; i < MAX_ROADBLOCKS; i++) { // Start looping through all 20 roadblocks that the placed could have placed.
if(!Player[playerid][pRoadblock][i]) continue; // If the roadblock does not exist, move on to the next slot.
GetObjectPos(Player[playerid][pRoadblock][i], obx, oby, obz); // The roadblock exists, so we need to get its position.
if(!IsPlayerInRangeOfPoint(playerid, 100.0, obx, oby, obz)) continue; // If the player is not within 100.0 units of the roadblock, ignore it. It's too far away.
DestroyObject(Player[playerid][pRoadblock][i]); // The player is within 100.0 units of the object, meaning it can now be destroyed.
Player[playerid][pRoadblock][i] = 0; // Reset the variable, so we can use this slot in the future.
}
return 1;
}
CMD:removerb(playerid, params[]) { // Command /removerb
new Float:obx, Float:oby, Float:obz, Float:dist = 100.0, Float:temp = 100.0, rb_to_del = -1;
// These variables store the object's position, a starting distance and a temporary distance, and the ID of the roadblock we should be removing.
for(new i = 0; i < MAX_ROADBLOCKS; i++) { // Start looping through all possible 20 roadblocks.
if(!Player[playerid][pRoadblock][i]) continue; // If the roadblock doesn't exist, ignore it and continue to the next one.
GetObjectPos(Player[playerid][pRoadblock][i], obx, oby, obz); // Store the object's position.
if((dist = GetPlayerDistanceFromPoint(playerid, obx, oby, obz)) <= temp) { // Now we check if the distance between the player and the roadblock is less than the temporary value. The value of 'GetPlayerDistanceFromPoint' is stored in 'dist'.
// Our temporary value starts at 100.0, so the player still cannot remove the roadblock if they are too far away from it.
temp = dist; // Update our new temporary value to the distance, as this distance is shorter and this roadblock is closer than the previous one.
rb_to_del = i; // This is now our closest roadblock, so we store the roadblock's ID for later.
continue; // Continue on to the next roadblock, to see if it is even closer.
}
}
if(rb_to_del == -1) return SendClientMessage(playerid, -1, "You are not near any placed roadblocks."); // If rb_to_del is still -1, it means that either no roadblocks are placed, or there are no roadblocks within 100.0 units of the player.
DestroyObject(Player[playerid][pRoadblock][rb_to_del]); // Destroy our stored roadblock ID. This will remove the closest roadblock to us.
Player[playerid][pRoadblock][rb_to_del] = 0; // Reset the variable, so we can use the slot later.
return 1;
}
Note, when comparing floats, you can also use the function
Floatcmp.