Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions addons/events/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,11 @@ class CfgFunctions {
PATHTO_FNC(registerChatCommand);
PATHTO_FNC(weaponEvents);
};

class Sounds {
PATHTO_FNC(executeWhenSoundWaveArrived);
PATHTO_FNC(playSoundSOS);
PATHTO_FNC(stopSoundSOS);
};
};
};
1 change: 1 addition & 0 deletions addons/events/XEH_preInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ if (isServer) then {

#include "backwards_comp.sqf"
#include "initSettings.sqf"
#include "initSOS.sqf"

ADDON = true;

Expand Down
65 changes: 65 additions & 0 deletions addons/events/fnc_executeWhenSoundWaveArrived.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_executeWhenSoundWaveArrived

Description:
Executes code snippet if sound from an origin has arrived at the camera.

Parameters:
_origin - Object or position from where sound emits <OBJECT, PosASL>
_code - Code to execute <CODE>
_arguments - Additional arguments to pass to code, default [] <ANY>

Returns:
Nothing.

Passed Arguments:
_origin - Same as above OR null object <OBJECT>
_position - Current position of origin OR initial position <PosASL>
_arguments - Same as above <NUMBER>

Examples:
(begin example)
[cursorTarget, {playSound (_this select 2)}, "Alarm"] call CBA_fnc_executeWhenSoundWaveArrived;
(end)

Author:
commy2
---------------------------------------------------------------------------- */

if (!hasInterface) exitWith {};

params [
["_origin", cameraOn, [objNull, []], [3]],
["_code", {}, [{}]],
["_arguments", []]
];

[{
params ["_code", "_origin", "_arguments", "_startTime"];

// Abort if origin of sound was deleted.
if (_origin isEqualTo objNull) exitWith {
true // quit
};

private _position = _origin;
if (_position isEqualType objNull) then {
_position = getPosASL _position;
} else {
_origin = objNull;
};

private _distanceToCamera = _position vectorDistance AGLToASL positionCameraToWorld [0,0,0];
private _travelledDistance = (CBA_missionTime - _startTime) * SPEED_OF_SOUND;
Comment thread
commy2 marked this conversation as resolved.

if (_distanceToCamera < _travelledDistance) exitWith {
[_origin, _position, _arguments] call _code;

true // quit
};

false // continue
}, {}, [_code, _origin, _arguments, CBA_missionTime]] call CBA_fnc_waitUntilAndExecute;

nil
53 changes: 53 additions & 0 deletions addons/events/fnc_playSoundSOS.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_playSoundSOS

Description:
Plays sound from object or position with speed of sound delay.

Parameters:
_origin - Object that emitted sound <OBJECT, PosASL>
_sound - CfgSounds class to play, rhs argument of say3D <STRING, ARRAY>

Returns:
Sound handler, used to stop the sound. <STRING>

Examples:
(begin example)
_handler = [cursorTarget, "Alarm"] call CBA_fnc_playSoundSOS;

private _dummy = "#particlesource" createVehicleLocal [0,0,0];
_dummy attachTo [tank1, [0,0,0], "memory_point"];
[_dummy, "Alarm"] call CBA_fnc_playSoundSOS;
(end)

Author:
commy2
---------------------------------------------------------------------------- */

params [["_origin", objNull], ["_sound", "", ["", []]]];

if (!isNil QGVAR(publicSounds)) then {
GVAR(publicSounds) = [] call CBA_fnc_createNamespace;
};

private _id = (GVAR(publicSounds) getVariable ["#id", -1]) + 1;

if (_id > 1E7) exitWith {
ERROR_1("Maximum amount of sound ids reached. Sound was %1.", _sound);
nil
};

GVAR(publicSounds) setVariable ["#id", _id];

private _handler = format ["%1:%2", CBA_clientID, _id];

if (_origin isEqualType objNull && {typeOf _origin isEqualTo ""}) then {
// Origin is dummy.
["CBA_playSoundSOS", [_origin, _sound, _handler]] call CBA_fnc_globalEvent;
} else {
// Origin is position or proper object, @todo CfgAmmo?
["CBA_playSoundSOS_createDummy", [_origin, _sound, _handler]] call CBA_fnc_globalEvent;
};

_handler
27 changes: 27 additions & 0 deletions addons/events/fnc_stopSoundSOS.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_stopSoundSOS

Description:
Stops sound from object or position with speed of sound delay.

Parameters:
_handler - Sound handler reported by CBA_fnc_playSoundSOS <ARRAY>

Returns:
Nothing.

Examples:
(begin example)
_handler call CBA_fnc_stopSoundSOS;
(end)

Author:
commy2
---------------------------------------------------------------------------- */

params [["_handler", "empty", [""]]];

["CBA_stopSoundSOS", _handler] call CBA_fnc_globalEvent;

nil
81 changes: 81 additions & 0 deletions addons/events/initSOS.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
if (hasInterface) then {
["CBA_playSoundSOS", {
params ["_origin", "_sound", "_handler"];

GVAR(publicSounds) setVariable [_handler, [objNull, _origin]];

[_origin, {
params ["_origin", "", "_args"];
_args params ["_sound", "_handler"];

private _source = allMissionObjects "#soundonvehicle";
_origin say3D _sound;
_source = (allMissionObjects "#soundonvehicle" - _source) param [0, objNull];

GVAR(publicSounds) setVariable [_handler, [_source, _origin]];
}, [_sound, _handler]] call CBA_fnc_executeWhenSoundWaveArrived;
}] call CBA_fnc_addEventHandler;

["CBA_stopSoundSOS", {
params ["_handler"];

(GVAR(publicSounds) getVariable [_handler, [nil, objNull]]) params ["", "_origin"];

[_origin, {
params ["", "", "_handler"];
(GVAR(publicSounds) getVariable _handler) params ["_source"];

deleteVehicle _source;
}, _handler] call CBA_fnc_executeWhenSoundWaveArrived;
}] call CBA_fnc_addEventHandler;

["CBA_playSoundSOS_createDummy", {
params ["_origin", "_sound", "_handler"];

GVAR(publicSounds) setVariable [_handler, [objNull, _origin]];

[_origin, {
params ["_origin", "_position", "_args"];
_args params ["_sound", "_handler"];

private _dummy = "#particlesource" createVehicleLocal ASLToAGL _position;

if (!isNull _origin) then {
_dummy attachTo [_origin, [0,0,0]];
};

private _source = allMissionObjects "#soundonvehicle";
_dummy say3D _sound;
_source = (allMissionObjects "#soundonvehicle" - _source) param [0, objNull];

GVAR(publicSounds) setVariable [_handler, [_source, _origin]];

// Source is deleted by game if sound finished, manually delete dummy.
[{
params ["_source"];
isNull _source // return
}, {
params ["", "_dummy"];
detach _dummy;
deleteVehicle _dummy;
}, [_source, _dummy]] call CBA_fnc_waitUntilAndExecute;
}, [_sound, _handler]] call CBA_fnc_executeWhenSoundWaveArrived;
}] call CBA_fnc_addEventHandler;

["CBA_playSoundSOSLooped", {
}] call CBA_fnc_addEventHandler;

["CBA_stopSoundSOSLooped", {
params ["_handler"];

(GVAR(publicSounds) getVariable [_handler, [nil, objNull]]) params ["", "_origin"];

[_origin, {
params ["", "", "_handler"];
(GVAR(publicSounds) getVariable _handler) params ["_source", "_dummy"];

deleteVehicle _source;
deleteVehicle _dummy;
}, _handler] call CBA_fnc_executeWhenSoundWaveArrived;
}] call CBA_fnc_addEventHandler;
};
2 changes: 2 additions & 0 deletions addons/events/script_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@
#define MOUSE_OFFSET 0xF0
#define MOUSE_WHEEL_OFFSET 0xF8 // MOUSE_OFFSET + 8 possible mouse keys
#define USERACTION_OFFSET 0xFA

#define SPEED_OF_SOUND 343 // in meters per second