From 2a9358ab03ff30281f4c56095093d41ca61a341e Mon Sep 17 00:00:00 2001 From: SilentSpike Date: Wed, 30 Aug 2017 16:36:53 +0100 Subject: [PATCH] Fix virtual spectator breaking if template active (#5466) * Simplify virtual spectator code * Rename camTarget GVAR for linter * Fix respawn template breaking virtual spectators --- addons/spectator/XEH_postInit.sqf | 22 +++++++++---------- addons/spectator/functions/fnc_cam.sqf | 8 +++---- .../functions/fnc_cam_setCameraMode.sqf | 2 +- addons/spectator/functions/fnc_cam_tick.sqf | 4 ++-- .../functions/fnc_getCameraAttributes.sqf | 4 ++-- .../functions/fnc_respawnTemplate.sqf | 3 +++ .../functions/fnc_setCameraAttributes.sqf | 4 ++-- addons/spectator/functions/fnc_setFocus.sqf | 4 ++-- .../spectator/functions/fnc_setSpectator.sqf | 9 ++++---- .../spectator/functions/fnc_switchFocus.sqf | 2 +- addons/spectator/functions/fnc_ui_draw3D.sqf | 2 +- .../functions/fnc_ui_handleListClick.sqf | 2 +- .../functions/fnc_ui_handleMapClick.sqf | 2 +- .../fnc_ui_handleMouseButtonDown.sqf | 4 ++-- .../spectator/functions/fnc_ui_updateHelp.sqf | 2 +- .../functions/fnc_ui_updateListFocus.sqf | 2 +- .../functions/fnc_ui_updateWidget.sqf | 4 ++-- .../functions/fnc_updateCameraModes.sqf | 2 +- 18 files changed, 42 insertions(+), 40 deletions(-) diff --git a/addons/spectator/XEH_postInit.sqf b/addons/spectator/XEH_postInit.sqf index d077c622ed..462d036328 100644 --- a/addons/spectator/XEH_postInit.sqf +++ b/addons/spectator/XEH_postInit.sqf @@ -26,15 +26,13 @@ if (isServer) then { [QGVAR(stageSpectator), FUNC(stageSpectator)] call CBA_fnc_addEventHandler; -// Delay until local player (must not be ACE_Player) is fully initalized -[ - { !isNil { player } && { !isNull player } }, - { - // Initalise virtual spectator players (must not be ACE_Player) - [QGVAR(virtual),"initpost",{ - if !(GVAR(isSet)) then { - if (player == (_this select 0)) then { [true] call FUNC(setSpectator) }; - }; - },false,[],true] call CBA_fnc_addClassEventHandler; - },[] -] call CBA_fnc_waitUntilAndExecute; +// A virtual spectator cannot exist without an interface +if (hasInterface) then { + // Local player (not ACE_Player) must be initalized to check + [ + { !isNull player }, + { + if (player isKindOf QGVAR(virtual)) then { [true] call FUNC(setSpectator); }; + } + ] call CBA_fnc_waitUntilAndExecute; +}; diff --git a/addons/spectator/functions/fnc_cam.sqf b/addons/spectator/functions/fnc_cam.sqf index a0c6eecedd..74b7d23d73 100644 --- a/addons/spectator/functions/fnc_cam.sqf +++ b/addons/spectator/functions/fnc_cam.sqf @@ -28,7 +28,7 @@ if (_init) then { // Start tracking camera attributes if not pre-set by public function ISNILS(GVAR(camMode),MODE_FREE); ISNILS(GVAR(camVision),VISION_NORM); - ISNILS(GVAR(camTarget),objNull); + ISNILS(GVAR(camFocus),objNull); // Ticking related GVAR(camDeltaTime) = 0; @@ -79,7 +79,7 @@ if (_init) then { GVAR(camera) = _camera; // Create dummy target used for follow camera - GVAR(camDummy) = "Logic" createVehicleLocal getPosASLVisual GVAR(camTarget); + GVAR(camDummy) = "Logic" createVehicleLocal getPosASLVisual GVAR(camFocus); // Handle initial camera mode limitation if !(GVAR(camMode) in GVAR(availableModes)) then { @@ -87,7 +87,7 @@ if (_init) then { }; // If inital camera mode is not free cam and no focus, find initial focus - if (GVAR(camMode) != MODE_FREE && isNull GVAR(camTarget)) then { + if (GVAR(camMode) != MODE_FREE && isNull GVAR(camFocus)) then { [true] call FUNC(setFocus); }; @@ -126,7 +126,7 @@ if (_init) then { // Stop tracking everything GVAR(camMode) = nil; GVAR(camVision) = nil; - GVAR(camTarget) = nil; + GVAR(camFocus) = nil; GVAR(camDeltaTime) = nil; GVAR(camLastTickTime) = nil; GVAR(camHasTarget) = nil; diff --git a/addons/spectator/functions/fnc_cam_setCameraMode.sqf b/addons/spectator/functions/fnc_cam_setCameraMode.sqf index f69614f5bc..8d1c53d71c 100644 --- a/addons/spectator/functions/fnc_cam_setCameraMode.sqf +++ b/addons/spectator/functions/fnc_cam_setCameraMode.sqf @@ -22,7 +22,7 @@ params ["_newMode"]; private _oldMode = GVAR(camMode); private _modes = GVAR(availableModes); -private _focus = GVAR(camTarget); +private _focus = GVAR(camFocus); // If new mode isn't available then keep current (unless current also isn't) if !(_newMode in _modes) then { diff --git a/addons/spectator/functions/fnc_cam_tick.sqf b/addons/spectator/functions/fnc_cam_tick.sqf index 7609234a8e..1ba9ee2797 100644 --- a/addons/spectator/functions/fnc_cam_tick.sqf +++ b/addons/spectator/functions/fnc_cam_tick.sqf @@ -23,7 +23,7 @@ BEGIN_COUNTER(camTick); private _cameraMode = GVAR(camMode); -private _camTarget = GVAR(camTarget); +private _camTarget = GVAR(camFocus); // UI mouse handler makes use of delta time between camera ticks private _currentTime = diag_tickTime; @@ -56,7 +56,7 @@ if (_cameraMode != MODE_FREE) then { }; // Refresh the local variable -_camTarget = GVAR(camTarget); +_camTarget = GVAR(camFocus); // Focus get in / out of vehicle state if !(isNull _camTarget) then { diff --git a/addons/spectator/functions/fnc_getCameraAttributes.sqf b/addons/spectator/functions/fnc_getCameraAttributes.sqf index adaa8b57c8..3f43a9003a 100644 --- a/addons/spectator/functions/fnc_getCameraAttributes.sqf +++ b/addons/spectator/functions/fnc_getCameraAttributes.sqf @@ -17,12 +17,12 @@ #include "script_component.hpp" if !(isNil QGVAR(camera)) then { - [GVAR(camMode), GVAR(camTarget), GVAR(camVision), getPosATL GVAR(camera), getDirVisual GVAR(camera)] + [GVAR(camMode), GVAR(camFocus), GVAR(camVision), getPosATL GVAR(camera), getDirVisual GVAR(camera)] } else { // These values could be pre-set by function [ GETMVAR(GVAR(camMode),0), - GETMVAR(GVAR(camTarget),objNull), + GETMVAR(GVAR(camFocus),objNull), GETMVAR(GVAR(camVision),-2), GETMVAR(GVAR(camPos),[ARR_3(0,0,0)]), GETMVAR(GVAR(camDir),0) diff --git a/addons/spectator/functions/fnc_respawnTemplate.sqf b/addons/spectator/functions/fnc_respawnTemplate.sqf index 7cc8b28031..24a0e358f4 100644 --- a/addons/spectator/functions/fnc_respawnTemplate.sqf +++ b/addons/spectator/functions/fnc_respawnTemplate.sqf @@ -31,6 +31,9 @@ if (_respawn in [0,1,4,5]) exitWith { if (typeOf _newCorpse == "seagull") then { deleteVehicle _newCorpse; }; }; +// Virtual spectators should be ignored by the template (otherwise they break) +if (_newCorpse isKindOf QGVAR(virtual)) exitWith {}; + // If player died while already in spectator, ignore if (!GVAR(isSet) || {alive _newCorpse}) then { // Negligible respawn delay can result in entering spectator after respawn diff --git a/addons/spectator/functions/fnc_setCameraAttributes.sqf b/addons/spectator/functions/fnc_setCameraAttributes.sqf index 405a65817c..734503e25e 100644 --- a/addons/spectator/functions/fnc_setCameraAttributes.sqf +++ b/addons/spectator/functions/fnc_setCameraAttributes.sqf @@ -55,7 +55,7 @@ if !(isNil QGVAR(camera)) then { if !(isNil "_mode") then { // If mode not free and no focus, find focus - if ((_mode != MODE_FREE) && {isNull GVAR(camTarget)}) then { + if ((_mode != MODE_FREE) && {isNull GVAR(camFocus)}) then { [true] call FUNC(setFocus); }; @@ -80,7 +80,7 @@ if !(isNil QGVAR(camera)) then { _focus = ([] call FUNC(getTargetEntities)) select 0; }; - GVAR(camTarget) = _focus; + GVAR(camFocus) = _focus; }; if !(isNil "_mode") then { diff --git a/addons/spectator/functions/fnc_setFocus.sqf b/addons/spectator/functions/fnc_setFocus.sqf index bcb4df934d..e2054ecfe6 100644 --- a/addons/spectator/functions/fnc_setFocus.sqf +++ b/addons/spectator/functions/fnc_setFocus.sqf @@ -37,8 +37,8 @@ if (_newFocus isEqualType true) then { }; }; -if (_newFocus != GVAR(camTarget) && { !(isNull _newFocus && { isNull GVAR(camTarget) }) }) then { - GVAR(camTarget) = _newFocus; +if (_newFocus != GVAR(camFocus) && { !(isNull _newFocus && { isNull GVAR(camFocus) }) }) then { + GVAR(camFocus) = _newFocus; if (isNull _newFocus) then { if (GVAR(camMode) == MODE_FREE) then { diff --git a/addons/spectator/functions/fnc_setSpectator.sqf b/addons/spectator/functions/fnc_setSpectator.sqf index 523ad4411d..3d8a6ab5bf 100644 --- a/addons/spectator/functions/fnc_setSpectator.sqf +++ b/addons/spectator/functions/fnc_setSpectator.sqf @@ -22,6 +22,7 @@ #include "script_component.hpp" params [["_set",true,[true]], ["_force",true,[true]], ["_hide",true,[true]]]; +TRACE_3("Params",_set,_force,_hide); // Only clients can be spectators if !(hasInterface) exitWith {}; @@ -33,10 +34,10 @@ GVAR(uiForced) = _force; // Exit if no change (everything above this may need to be ran again) if (_set isEqualTo GVAR(isSet)) exitWith {}; -// Delay if local player (must not be ACE_Player) is not fully initalized -if (isNil { player } || { isNull player }) exitWith { +// Delay if local player (must not be ACE_Player) does not exist +if (isNull player) exitWith { [ - { !isNil { player } && { !isNull player } }, + { !isNull player }, FUNC(setSpectator), _this ] call CBA_fnc_waitUntilAndExecute; @@ -73,7 +74,7 @@ if (_set) then { EGVAR(nametags,showNamesForAI) = false; }; } else { - // Kill the display (ensure main display exists, handles edge case where spectator turned off before display exists) + // Kill the display (ensure main display exists, handles edge case where spectator turned off beforehand) [{ !isNull MAIN_DISPLAY },{ [false] call FUNC(ui) }] call CBA_fnc_waitUntilAndExecute; // This variable doesn't matter anymore diff --git a/addons/spectator/functions/fnc_switchFocus.sqf b/addons/spectator/functions/fnc_switchFocus.sqf index 9aea8b061a..444bd72b6d 100644 --- a/addons/spectator/functions/fnc_switchFocus.sqf +++ b/addons/spectator/functions/fnc_switchFocus.sqf @@ -18,7 +18,7 @@ private _next = param [0, true]; private _entities = [true] call FUNC(getTargetEntities); -private _focus = GVAR(camTarget); +private _focus = GVAR(camFocus); // No entities to switch to if ((_entities isEqualTo []) || (_entities isEqualTo [_focus])) exitWith {}; diff --git a/addons/spectator/functions/fnc_ui_draw3D.sqf b/addons/spectator/functions/fnc_ui_draw3D.sqf index fc0d39ea93..185d8f9dd9 100644 --- a/addons/spectator/functions/fnc_ui_draw3D.sqf +++ b/addons/spectator/functions/fnc_ui_draw3D.sqf @@ -18,7 +18,7 @@ #define HEIGHT_OFFSET 1.5 BEGIN_COUNTER(updateCursor); -private _camTarget = GVAR(camTarget); +private _camTarget = GVAR(camFocus); private _camTargetVeh = vehicle _camTarget; private _cursorObject = objNull; diff --git a/addons/spectator/functions/fnc_ui_handleListClick.sqf b/addons/spectator/functions/fnc_ui_handleListClick.sqf index 29dbe498bc..9616e77cac 100644 --- a/addons/spectator/functions/fnc_ui_handleListClick.sqf +++ b/addons/spectator/functions/fnc_ui_handleListClick.sqf @@ -41,7 +41,7 @@ if !(isNull _object) then { _handled = true; } else { - if (_object != GVAR(camTarget)) then { + if (_object != GVAR(camFocus)) then { [_object] call FUNC(setFocus); _handled = true; diff --git a/addons/spectator/functions/fnc_ui_handleMapClick.sqf b/addons/spectator/functions/fnc_ui_handleMapClick.sqf index 8d870ae0d0..6e74ed0e97 100644 --- a/addons/spectator/functions/fnc_ui_handleMapClick.sqf +++ b/addons/spectator/functions/fnc_ui_handleMapClick.sqf @@ -23,7 +23,7 @@ params ["", "", "_x", "_y"]; if (isNull GVAR(uiMapHighlighted)) then { // Give user feedback that camera is no longer focused - if !(isNull GVAR(camTarget)) then { + if !(isNull GVAR(camFocus)) then { playSound "ReadoutHideClick1"; }; diff --git a/addons/spectator/functions/fnc_ui_handleMouseButtonDown.sqf b/addons/spectator/functions/fnc_ui_handleMouseButtonDown.sqf index 365c85bd16..a6bb64d744 100644 --- a/addons/spectator/functions/fnc_ui_handleMouseButtonDown.sqf +++ b/addons/spectator/functions/fnc_ui_handleMouseButtonDown.sqf @@ -28,7 +28,7 @@ params ["", "_button"]; // Left click if (_button == 0) exitWith { if (isNull GVAR(cursorObject)) then { - if (GVAR(camMode) == MODE_FREE && { !isNull GVAR(camTarget) }) then { + if (GVAR(camMode) == MODE_FREE && { !isNull GVAR(camFocus) }) then { playSound "ReadoutHideClick1"; [objNull] call FUNC(setFocus); }; @@ -45,7 +45,7 @@ if (_button == 0) exitWith { // Right click if (_button == 1) then { - if (GVAR(camMode) == MODE_FREE && { !isNull GVAR(camTarget) } && { !isNull (attachedTo GVAR(camDummy)) }) then { + if (GVAR(camMode) == MODE_FREE && { !isNull GVAR(camFocus) } && { !isNull (attachedTo GVAR(camDummy)) }) then { [] call FUNC(cam_resetTarget); }; GVAR(holdingRMB) = true; diff --git a/addons/spectator/functions/fnc_ui_updateHelp.sqf b/addons/spectator/functions/fnc_ui_updateHelp.sqf index d2d4ba248f..74ab2d9a46 100644 --- a/addons/spectator/functions/fnc_ui_updateHelp.sqf +++ b/addons/spectator/functions/fnc_ui_updateHelp.sqf @@ -26,7 +26,7 @@ if !(GVAR(uiHelpVisible)) exitWith {}; private _cameraMode = GVAR(camMode); private _availableModes = GVAR(availableModes); -private _hasTarget = !isNull GVAR(camTarget); +private _hasTarget = !isNull GVAR(camFocus); private _controls = []; diff --git a/addons/spectator/functions/fnc_ui_updateListFocus.sqf b/addons/spectator/functions/fnc_ui_updateListFocus.sqf index d957af6f16..1e9dcafaec 100644 --- a/addons/spectator/functions/fnc_ui_updateListFocus.sqf +++ b/addons/spectator/functions/fnc_ui_updateListFocus.sqf @@ -16,4 +16,4 @@ #include "script_component.hpp" -CTRL_LIST tvSetCurSel ([[GVAR(camTarget)] call BIS_fnc_objectVar] call FUNC(ui_getTreeDataIndex)); +CTRL_LIST tvSetCurSel ([[GVAR(camFocus)] call BIS_fnc_objectVar] call FUNC(ui_getTreeDataIndex)); diff --git a/addons/spectator/functions/fnc_ui_updateWidget.sqf b/addons/spectator/functions/fnc_ui_updateWidget.sqf index 141397280f..068bff7cb0 100644 --- a/addons/spectator/functions/fnc_ui_updateWidget.sqf +++ b/addons/spectator/functions/fnc_ui_updateWidget.sqf @@ -22,9 +22,9 @@ #define IMG_UNARMED "" // TODO: Find suitable unarmed icon // Hide if no target or widget is toggled off -if (!GVAR(uiWidgetVisible) || {isNull GVAR(camTarget)}) exitWith {CTRL_WIDGET ctrlShow false}; +if (!GVAR(uiWidgetVisible) || {isNull GVAR(camFocus)}) exitWith {CTRL_WIDGET ctrlShow false}; -private _focus = GVAR(camTarget); +private _focus = GVAR(camFocus); private _name = ([_focus] call EFUNC(common,getName)) select [0, NAME_MAX_CHARACTERS]; if !(isPlayer _focus) then { _name = format ["%1: %2", localize "str_player_ai", _name]; }; diff --git a/addons/spectator/functions/fnc_updateCameraModes.sqf b/addons/spectator/functions/fnc_updateCameraModes.sqf index c092a615e8..54f3d67f9a 100644 --- a/addons/spectator/functions/fnc_updateCameraModes.sqf +++ b/addons/spectator/functions/fnc_updateCameraModes.sqf @@ -49,7 +49,7 @@ if (_newModes isEqualTo []) then { // Update camera in case of change if !(isNil QGVAR(camera)) then { // If mode was free and no longer available, find a focus - if (!(MODE_FREE in _newModes) && {GVAR(camMode) == MODE_FREE} && {isNull GVAR(camTarget)}) then { + if (!(MODE_FREE in _newModes) && {GVAR(camMode) == MODE_FREE} && {isNull GVAR(camFocus)}) then { [true] call FUNC(setFocus); };