Skip to content

Commit

Permalink
Clear empty vehicles on police spawns when new vehicle is spawned (#63)
Browse files Browse the repository at this point in the history
* Clear empty vehicles on police spawns when new vehicle is spawned

* Fix vehicle exploding on spawn

* Add missing semicolon

* Add isNotEqualTo keyword

* Remove semicolon

* Replace systemChat with LOG/TRACE/INFO

* Reorder TRACE log to actually be able to log something
  • Loading branch information
3Mydlo3 authored Feb 15, 2024
1 parent 81b7105 commit 5bfe82a
Showing 1 changed file with 33 additions and 6 deletions.
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
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;

0 comments on commit 5bfe82a

Please sign in to comment.