From 74b6036f3e8f477820f317f407ab9046b78ddafc Mon Sep 17 00:00:00 2001 From: esteldunedain Date: Sat, 23 Jan 2016 16:24:55 -0300 Subject: [PATCH] Handle destruction of tags placed on objects that don't support handledamage. --- addons/tagging/XEH_preInit.sqf | 1 + addons/tagging/functions/fnc_createTag.sqf | 52 ++++++++++++------- .../functions/fnc_tagTestingThread.sqf | 46 ++++++++++++++++ 3 files changed, 81 insertions(+), 18 deletions(-) create mode 100644 addons/tagging/functions/fnc_tagTestingThread.sqf diff --git a/addons/tagging/XEH_preInit.sqf b/addons/tagging/XEH_preInit.sqf index 2c11c4d42a..c3ceab4c03 100644 --- a/addons/tagging/XEH_preInit.sqf +++ b/addons/tagging/XEH_preInit.sqf @@ -7,5 +7,6 @@ PREP(createTag); PREP(tagDirection); PREP(tagGround); PREP(tagWall); +PREP(tagTestingThread); ADDON = true; diff --git a/addons/tagging/functions/fnc_createTag.sqf b/addons/tagging/functions/fnc_createTag.sqf index c0c2ea044d..1ac64aacbe 100644 --- a/addons/tagging/functions/fnc_createTag.sqf +++ b/addons/tagging/functions/fnc_createTag.sqf @@ -34,23 +34,39 @@ _tag setVectorDirAndUp _vectorDirAndUp; if (isNull _object) exitWith {}; // If the tag is applied to an object, handle its destruction +_object setVariable [QGVAR(testVar), true]; +if (_object getVariable [QGVAR(testVar), false]) then { + // The object supports variables and hence HandleDamage too + // Use the cheaper alternative for handling destruction: HandleDamage + + // If the object already has tags attached, just add the new one to the list + private _attachedTags = _object getVariable QGVAR(attachedTags); + if !(isNil "_attachedTags ") exitWith { + _attachedTags pushBack _tag; + }; + + _attachedTags = [_tag]; + _object setVariable [QGVAR(attachedTags), _attachedTags]; + + // If it's the first tag attached to that object, add a handledamage event handler + _object addEventHandler ["HandleDamage", { + params ["_object", "_selection", "_damage"]; + if (_selection == "" && _damage >= 1) then { + { + deleteVehicle _x; + } foreach (_object getVariable [QGVAR(attachedTags), []]); + _object setVariable [QGVAR(attachedTags), []]; + }; + }]; + +} else { + // The object doesn't supports variables + // Use the more costly alternative: periodic testing + GVAR(tagsToTest) pushBack [_tag, _tagPosASL, _vectorDirAndUp]; + + // Run the tes + if (!GVAR(testingThread)) then { + call FUNC(tagTestingThread); + }; -// If the object already has tags attached, just add the new one to the list -private _attachedTags = _object getVariable QGVAR(attachedTags); -if !(isNil "_attachedTags ") exitWith { - _attachedTags pushBack _tag; }; - -_attachedTags = [tags]; -_object setVariable [QGVAR(attachedTags), _attachedTags]; - -// If it's the first tag attached to that object, add a handledamage event handler -_object addEventHandler ["HandleDamage", { - params ["_object", "_selection", "_damage"]; - if (_selection == "" && _damage >= 1) then { - { - deleteVehicle _x; - } foreach (_object getVariable [QGVAR(attachedTags), []]); - _object setVariable [QGVAR(attachedTags), []]; - }; -}]; diff --git a/addons/tagging/functions/fnc_tagTestingThread.sqf b/addons/tagging/functions/fnc_tagTestingThread.sqf new file mode 100644 index 0000000000..18d14e2e4f --- /dev/null +++ b/addons/tagging/functions/fnc_tagTestingThread.sqf @@ -0,0 +1,46 @@ +/* + * Author: esteldunedain + * Checks if tags are still leaning on an object periodically. + * + * Arguments: + * None + * + * Return Value: + * None + * + * Example: + * [] call ace_tagging_fnc_tagTestingThread + * + * Public: No + */ + +#include "script_component.hpp" + +_fnc_isLeaning = { + params ["_tag", "_tagPosASL", "_vectorDirAndUp"]; + + _vectorDirAndUp params ["_v1", "_v2"]; + + private _endPosASL = _tagPosASL vectorAdd (_v1 vectorMultiply 0.08); + + // Check for intersections below the unit + private _intersections = lineIntersectsSurfaces [_tagPosASL, _endPosASL, _tag, objNull, true, 1, "GEOM", "FIRE"]; + + // If there's no intersections + if (_intersections isEqualTo []) exitWith { + TRACE_1("No intersections, deleting:",_tag); + deleteVehicle _tag; + false + }; + true +}; +GVAR(tagsToTest) = [GVAR(tagsToTest), _fnc_isLeaning] call EFUNC(common,filter); + +// If there's no more tag +if (GVAR(tagsToTest) isEqualTo []) exitWith { + GVAR(testingThread) = false; +}; + +// Schedule for execution again after 5 seconds +[DFUNC(tagTestingThread), [], 5] call EFUNC(common,waitAndExecute); +GVAR(testingThread) = true;