2023-09-12 18:58:10 +00:00
|
|
|
#include "..\script_component.hpp"
|
2015-01-11 16:42:31 +00:00
|
|
|
/*
|
2015-02-02 22:28:27 +00:00
|
|
|
* Author: bux578
|
|
|
|
* Switches to the new given player unit
|
|
|
|
*
|
|
|
|
* Arguments:
|
2015-08-07 07:18:42 +00:00
|
|
|
* 0: the unit to switch to <OBJECT>
|
2015-02-02 22:28:27 +00:00
|
|
|
*
|
|
|
|
* Return Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Example:
|
2015-08-07 07:18:42 +00:00
|
|
|
* [_unit] call ace_switchunits_fnc_switchUnit
|
2015-02-02 22:28:27 +00:00
|
|
|
*
|
|
|
|
* Public: Yes
|
|
|
|
*/
|
2015-01-12 14:47:22 +00:00
|
|
|
|
2015-08-07 07:18:42 +00:00
|
|
|
params ["_unit"];
|
2015-01-11 16:42:31 +00:00
|
|
|
|
|
|
|
// don't switch to original player units
|
2024-06-18 14:08:03 +00:00
|
|
|
if !([_unit] call FUNC(isValidAi)) exitWith {};
|
2015-02-02 22:28:27 +00:00
|
|
|
|
|
|
|
// exit var
|
2016-01-10 18:08:28 +00:00
|
|
|
private _leave = false;
|
2015-02-02 22:28:27 +00:00
|
|
|
|
|
|
|
if (GVAR(EnableSafeZone)) then {
|
2016-01-10 18:08:28 +00:00
|
|
|
private _allNearestPlayers = [position _unit, GVAR(SafeZoneRadius)] call FUNC(nearestPlayers);
|
2016-02-06 10:58:31 +00:00
|
|
|
private _nearestEnemyPlayers = _allNearestPlayers select {((side GVAR(OriginalGroup)) getFriend side _x < 0.6) && !(_x getVariable [QGVAR(IsPlayerControlled), false])};
|
2015-05-09 12:05:58 +00:00
|
|
|
|
2024-04-06 18:57:08 +00:00
|
|
|
if (_nearestEnemyPlayers isNotEqualTo []) exitWith {
|
2015-02-02 22:28:27 +00:00
|
|
|
_leave = true;
|
2015-01-11 16:42:31 +00:00
|
|
|
};
|
2015-02-02 22:28:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// exitWith doesn't exit past the "if(EnableSafeZone)" block
|
|
|
|
if (_leave) exitWith {
|
2015-05-28 19:59:04 +00:00
|
|
|
[localize LSTRING(TooCloseToEnemy)] call EFUNC(common,displayTextStructured);
|
2015-01-11 16:42:31 +00:00
|
|
|
};
|
2015-02-02 22:28:27 +00:00
|
|
|
|
|
|
|
// should switch locality
|
|
|
|
// This doesn't work anymore, because one's now able to switch to units from a different side
|
|
|
|
//[_unit] joinSilent group player;
|
2016-06-03 00:59:18 +00:00
|
|
|
[QGVAR(switchLocality), [_unit, player]] call CBA_fnc_serverEvent;
|
2015-02-02 22:28:27 +00:00
|
|
|
|
2015-08-08 11:34:35 +00:00
|
|
|
[{
|
2015-08-07 07:18:42 +00:00
|
|
|
params ["_args", "_pfhId"];
|
|
|
|
_args params ["_unit", "_oldUnit"];
|
2015-05-09 12:05:58 +00:00
|
|
|
|
2016-01-10 19:58:54 +00:00
|
|
|
[localize LSTRING(TryingToSwitch)] call EFUNC(common,displayTextStructured);
|
|
|
|
|
2015-02-02 22:28:27 +00:00
|
|
|
if (local _unit) exitWith {
|
|
|
|
_oldUnit setVariable [QGVAR(IsPlayerControlled), false, true];
|
|
|
|
_oldUnit setVariable [QGVAR(PlayerControlledName), "", true];
|
|
|
|
|
2016-01-10 19:58:54 +00:00
|
|
|
private _killedEhId = _unit getVariable [QGVAR(KilledEhId), -1];
|
|
|
|
if (_killedEhId != -1) then {
|
|
|
|
_oldUnit removeEventHandler ["Killed", _killedEhId];
|
2015-02-02 22:28:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
selectPlayer _unit;
|
|
|
|
|
|
|
|
_unit setVariable [QGVAR(IsPlayerControlled), true, true];
|
|
|
|
_unit setVariable [QGVAR(PlayerControlledName), GVAR(OriginalName), true];
|
|
|
|
|
2016-01-10 19:58:54 +00:00
|
|
|
|
|
|
|
_killedEhId = _unit addEventHandler ["Killed", {
|
2015-02-02 22:28:27 +00:00
|
|
|
[GVAR(OriginalUnit), _this select 0] call FUNC(switchBack);
|
|
|
|
}];
|
2016-01-10 19:58:54 +00:00
|
|
|
_unit setVariable [QGVAR(KilledEhId), _killedEhId, true];
|
|
|
|
|
2015-02-02 22:28:27 +00:00
|
|
|
|
|
|
|
// set owner back to original owner
|
2016-01-10 18:08:28 +00:00
|
|
|
private _oldOwner = _oldUnit getVariable[QGVAR(OriginalOwner), -1];
|
2015-02-02 22:28:27 +00:00
|
|
|
if (_oldOwner > -1) then {
|
2016-06-03 00:59:18 +00:00
|
|
|
["ace_setOwner", [_oldUnit, _oldOwner]] call CBA_fnc_serverEvent;
|
2015-02-02 22:28:27 +00:00
|
|
|
};
|
|
|
|
|
2015-05-28 19:59:04 +00:00
|
|
|
[localize LSTRING(SwitchedUnit)] call EFUNC(common,displayTextStructured);
|
2015-05-09 12:05:58 +00:00
|
|
|
|
2015-11-30 15:45:20 +00:00
|
|
|
[_pfhId] call CBA_fnc_removePerFrameHandler;
|
2015-02-02 22:28:27 +00:00
|
|
|
};
|
2015-08-08 11:34:35 +00:00
|
|
|
}, 0.2, [_unit, player]] call CBA_fnc_addPerFrameHandler;
|