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
|
|
|
#include "script_component.hpp"
|
|
|
|
|
2015-08-08 11:34:35 +00:00
|
|
|
private ["_nearestEnemyPlayers", "_allNearestPlayers", "_oldUnit", "_leave"];
|
2015-01-11 16:42:31 +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
|
2015-02-02 22:28:27 +00:00
|
|
|
if (!([_unit] call FUNC(isValidAi))) exitWith {};
|
|
|
|
|
|
|
|
// exit var
|
|
|
|
_leave = false;
|
|
|
|
|
|
|
|
if (GVAR(EnableSafeZone)) then {
|
2015-01-12 14:47:22 +00:00
|
|
|
_allNearestPlayers = [position _unit, GVAR(SafeZoneRadius)] call FUNC(nearestPlayers);
|
2015-01-13 05:14:27 +00:00
|
|
|
_nearestEnemyPlayers = [_allNearestPlayers, {((side GVAR(OriginalGroup)) getFriend (side _this) < 0.6) && !(_this getVariable [QGVAR(IsPlayerControlled), false])}] call EFUNC(common,filter);
|
2015-05-09 12:05:58 +00:00
|
|
|
|
2015-01-11 16:42:31 +00:00
|
|
|
if (count _nearestEnemyPlayers > 0) 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;
|
|
|
|
[[_unit, player], QUOTE({(_this select 0) setVariable [ARR_3(QUOTE(QGVAR(OriginalOwner)), owner (_this select 0), true)]; (_this select 0) setOwner owner (_this select 1)}), 1] call EFUNC(common,execRemoteFnc);
|
|
|
|
|
2015-08-08 11:34:35 +00:00
|
|
|
[{
|
2015-08-07 07:18:42 +00:00
|
|
|
private ["_respawnEhId", "_oldOwner"];
|
|
|
|
params ["_args", "_pfhId"];
|
|
|
|
_args params ["_unit", "_oldUnit"];
|
2015-05-09 12:05:58 +00:00
|
|
|
|
2015-02-02 22:28:27 +00:00
|
|
|
if (local _unit) exitWith {
|
|
|
|
_oldUnit setVariable [QGVAR(IsPlayerControlled), false, true];
|
|
|
|
_oldUnit setVariable [QGVAR(PlayerControlledName), "", true];
|
|
|
|
|
|
|
|
_respawnEhId = _unit getVariable [QGVAR(RespawnEhId), -1];
|
|
|
|
if (_respawnEhId != -1) then {
|
|
|
|
_oldUnit removeEventHandler ["Respawn", _respawnEhId];
|
|
|
|
};
|
|
|
|
|
|
|
|
selectPlayer _unit;
|
|
|
|
|
|
|
|
_unit setVariable [QGVAR(IsPlayerControlled), true, true];
|
|
|
|
_unit setVariable [QGVAR(PlayerControlledName), GVAR(OriginalName), true];
|
|
|
|
|
|
|
|
_respawnEhId = _unit addEventHandler ["Respawn", {
|
|
|
|
[GVAR(OriginalUnit), _this select 0] call FUNC(switchBack);
|
|
|
|
}];
|
|
|
|
_unit setVariable [QGVAR(RespawnEhId), _respawnEhId, true];
|
|
|
|
|
|
|
|
// set owner back to original owner
|
|
|
|
_oldOwner = _oldUnit getVariable[QGVAR(OriginalOwner), -1];
|
|
|
|
if (_oldOwner > -1) then {
|
|
|
|
[[_oldUnit, _oldOwner], QUOTE({(_this select 0) setOwner (_this select 1)}), 1] call EFUNC(common,execRemoteFnc);
|
|
|
|
};
|
|
|
|
|
2015-05-28 19:59:04 +00:00
|
|
|
[localize LSTRING(SwitchedUnit)] call EFUNC(common,displayTextStructured);
|
2015-05-09 12:05:58 +00:00
|
|
|
|
2015-08-07 07:18:42 +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;
|