Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear empty vehicles on police spawns when new vehicle is spawned #63

Merged
merged 10 commits into from
Feb 15, 2024
39 changes: 33 additions & 6 deletions addons/police/functions/fnc_spawnVehicle.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* Public: No
*/

#define SPAWNPOINT_SAFEZONE 5

params ["_vehicleClassname", "_spawner"];

private _vehicleType = (_vehicleClassname call BIS_fnc_objectType) select 1;
Expand All @@ -27,13 +29,37 @@ private _spawnPoints = if (_vehicleType isEqualTo "Helicopter" || {_vehicleType
+(_spawner getVariable QGVAR(spawnPoints))
};

// Find empty spawn position
// Find spawn position
private _position = [];
private _direction = 0;
while {_position isEqualTo [] && {!(_spawnPoints isEqualTo [])}} do {
private _spawnPoint = [_spawnPoints] call EFUNC(common,deleteAtRandom);
private _objects = (getPos _spawnPoint) nearEntities 5;
if (_objects isEqualTo []) exitWith {

private _emptySpawnPointIndex = _spawnPoints findIf {getPos _x nearEntities SPAWNPOINT_SAFEZONE isEqualTo []};
if (_emptySpawnPointIndex isNotEqualTo -1) then {
LOG("Found empty spawnpoint");
private _spawnPoint = _spawnPoints select _emptySpawnPointIndex;
_position = getPos _spawnPoint;
_direction = getDir _spawnPoint;
} else {
// Maybe there is a position that has unoccupied vehicle
LOG("Looking for unoccupied vehicles");
private _fullSpawnPointsWithoutCrew = _spawnPoints select {
private _nearEntities = getPos _x nearEntities SPAWNPOINT_SAFEZONE;
if (_nearEntities isEqualTo []) exitWith { false };
_nearEntities findIf {crew _x isEqualTo []} isNotEqualTo -1
};

if (_fullSpawnPointsWithoutCrew isNotEqualTo []) exitWith {
LOG("Found unoccupied vehicles");
private _spawnPoint = [_fullSpawnPointsWithoutCrew] call EFUNC(common,deleteAtRandom);

// Clear the area
LOG("Deleting vehicles");
_spawnPoint nearEntities SPAWNPOINT_SAFEZONE
3Mydlo3 marked this conversation as resolved.
Show resolved Hide resolved
apply {
TRACE_1("Deleting vehicle %1",typeOf _x);
deleteVehicle _x;
};

_position = getPos _spawnPoint;
_direction = getDir _spawnPoint;
};
Expand All @@ -48,4 +74,5 @@ if (_position isEqualTo []) exitWith {
};

// Spawn vehicle
[_vehicleClassname, _position, _direction, true, false, true] call EFUNC(vehicles,createVehicle);
INFO_2("Creating vehicle %1 at position %2",_vehicleClassname,str _position);
[EFUNC(vehicles,createVehicle), [_vehicleClassname, _position, _direction, true, false, true]] call CBA_fnc_execNextFrame;