16AA-Milsim / MissionFramework

16AA Missionframework
2 stars 2 forks source link

[Feature] remember ACRE channels on respawn #5

Closed MildlyInterested closed 1 year ago

MildlyInterested commented 3 years ago

PabstMirror in Slack

player addEventHandler ["Respawn", {
    params ["_unit", "_corpse"];
    _unit setUnitLoadout [[_corpse] call acre_api_fnc_filterUnitLoadout, false];
    {
        private _radioType = _x;
        private _radios = [_radioType, _corpse] call acre_api_fnc_getAllRadiosByType;
        private _radioData = [];
        {
            private _radioID = _x;
            private _currentChannel = [_radioID] call acre_api_fnc_getRadioChannel;
            systemChat format ["%1 was on %2", _radioID, _currentChannel];
            _radioData pushBack _currentChannel;
        } forEach _radios;

        [{ // delay 5 sec, so unit can get unique radio
            params ["_newUnit", "_radioType", "_radioData"];
            private _radios = [_radioType, _newUnit] call acre_api_fnc_getAllRadiosByType;
            {
                private _radioID = _x;
                private _channel = _radioData select _forEachIndex;
                systemChat format ["Setting %1 to %2", _radioID, _channel];
                [_radioID, _channel] call acre_api_fnc_setRadioChannel;
            } forEach _radios;
        }, [_newUnit, _radioType, _radioData], 5] call CBA_fnc_waitAndExecute;
    } forEach ["ACRE_PRC148", "ACRE_PRC343"];
}];

used by Alablm (Slack)

if (isClass (configFile >> "CfgPatches" >> "acre_main")) then {
    private _radioCheck = [_corpse] call acre_api_fnc_hasRadio;
    if (_radioCheck) then {
        _corpse setUnitLoadout [[_corpse] call acre_api_fnc_filterUnitLoadout, false];
    };
};

_entity setUnitLoadout [_loadout, false];

if (isClass (configFile >> "CfgPatches" >> "acre_main")) then {
    [{
        params ["_entity"];
        private _radioCheck = [_entity] call acre_api_fnc_hasRadio;
        if (_radioCheck) then {
            private _radios = _entity call acre_api_fnc_getCurrentRadioList;
            [(_radios select 0)] call acre_sys_radio_fnc_setActiveRadio;
        };
    }, [_entity], 1] call CBA_fnc_waitAndExecute;
};

relevant discussion in ACRE2 Slack: https://ace3public.slack.com/archives/C02012P6T52/p1636353132028800

MildlyInterested commented 2 years ago
player addEventHandler ["Respawn", {
    params ["_unit", "_corpse"];
    _unit setUnitLoadout [[_corpse] call acre_api_fnc_filterUnitLoadout, false]; //copies old radios from corpse to new player?
    {
        private _radioType = _x;
        private _radios = [_radioType, _corpse] call acre_api_fnc_getAllRadiosByType; //returns arry of unique Radio IDs from corpse
        private _radioData = [];
        {//interate through all radios by IDs
            private _radioID = _x;
            private _currentChannel = [_radioID] call acre_api_fnc_getRadioChannel; //get channel of Radio
            systemChat format ["%1 was on %2", _radioID, _currentChannel]; //tell channel of radio as hint
            _radioData pushBack _currentChannel; //add channel to radioData
        } forEach _radios;
        //now we have a array that has each radiochannel for every radio the player had in it
        [{ // delay 5 sec, so unit can get unique radio
            params ["_newUnit", "_radioType", "_radioData"];
            private _radios = [_radioType, _newUnit] call acre_api_fnc_getAllRadiosByType;
            {
                private _radioID = _x;
                private _channel = _radioData select _forEachIndex;
                systemChat format ["Setting %1 to %2", _radioID, _channel];
                [_radioID, _channel] call acre_api_fnc_setRadioChannel;
            } forEach _radios;
        }, [_newUnit, _radioType, _radioData], 5] call CBA_fnc_waitAndExecute;
    } forEach ["ACRE_PRC152", "ACRE_PRC343"];
}];

tested like that, works in local multiplayer

MildlyInterested commented 2 years ago

TODO

Darojax commented 2 years ago
player addEventHandler ["Respawn",
    {
        params ["_unit", "_corpse"];
        _unit setUnitLoadout [[_corpse] call acre_api_fnc_filterUnitLoadout, false]; //copies old radio items and types from corpse to respawned player
        {
            private _radioType = _x;
            private _radios = [_radioType, _corpse] call acre_api_fnc_getAllRadiosByType; //returns array of unique Radio IDs from corpse
            private _radioChannel = [];
            private _radioVolume = [];
            private _radioSpatial = [];
            {//interate through all radios by IDs
                private _radioID = _x;
                private _currentChannel = [_radioID] call acre_api_fnc_getRadioChannel; //get channel of Radio
                private _currentVolume = [_radioID] call acre_api_fnc_getRadioVolume; //get volume of Radio
                private _currentSpatial = [_radioID] call acre_api_fnc_getRadioSpatial; //get spatial of Radio
                systemChat format ["Radio %1 was on channel %2 at volume %3 listening on %4", _radioID, _currentChannel, _currentVolume, _currentSpatial]; //Tell radio data as hint
                _radioChannel pushBack _currentChannel; //add channel to radioData
                _radioVolume pushBack _currentVolume; //add volume to radioVolume
                _radioSpatial pushBack _currentSpatial; //add spatial to radioSpatial
            } forEach _radios;
            //now we have three arrays that has radiochannel, volume level and spatial for every radio the player had in it
            [{ // delay 5 sec, so unit can get unique radio
                params ["_newUnit", "_radioType", "_radioChannel", "_radioVolume", "_radioSpatial"];
                private _radios = [_radioType, _newUnit] call acre_api_fnc_getAllRadiosByType;
                {
                    private _radioID = _x;
                    private _channel = _radioChannel select _forEachIndex;
                    private _volume = _radioVolume select _forEachIndex;
                    private _spatial = _radioSpatial select _forEachIndex;
                    systemChat format ["Setting radio %1 to channel %2 at volume %3 on %4", _radioID, _channel, _volume, _spatial];
                    [_radioID, _channel] call acre_api_fnc_setRadioChannel;
                    [_radioID, _volume] call acre_api_fnc_setRadioVolume;
                    [_radioID, _spatial] call acre_api_fnc_setRadioSpatial;
                } forEach _radios;
            }, [_newUnit, _radioType, _radioChannel, _radioVolume, _radioSpatial], 5] call CBA_fnc_waitAndExecute;
        } forEach ["ACRE_PRC343", "ACRE_PRC152", "ACRE_PRC117"];
        0 spawn {
            sleep 6;
            systemChat format ["Remember to re-set PTT buttons manually!"];
        };
    }];

Re-sets Channel, Volume and Spatial (Left, Center, Right) on all radios after respawn, still lacking the PTT setting. Tested and works in local multiplayer.

Darojax commented 2 years ago
player addEventHandler ["Respawn",
    {
        _newRadioList = [];
        params ["_unit", "_corpse", "_newRadioList"];
        _unit setUnitLoadout [[_corpse] call acre_api_fnc_filterUnitLoadout, false]; //copy old radio items from corpse to respawned player
        {
            private _radioType = _x;
            private _radios = [_radioType, _corpse] call acre_api_fnc_getAllRadiosByType; //return array of unique Radio IDs from corpse
            private _radioChannel = [];
            private _radioVolume = [];
            private _radioSpatial = [];
            private _pttList = [];
            _pttRadioList = [] call acre_api_fnc_getMultiPushToTalkAssignment; //return array of radioIDs assigned to each PTT key
            {
                //iterate through all radios by IDs, add their properties to arrays in a set order
                private _radioID = _x;
                private _currentChannel = [_radioID] call acre_api_fnc_getRadioChannel; //get channel of Radio
                private _currentVolume = [_radioID] call acre_api_fnc_getRadioVolume; //get volume of Radio
                private _currentSpatial = [_radioID] call acre_api_fnc_getRadioSpatial; //get spatial of Radio
                switch (_radioID) do 
                { 
                    //if ptt1 radio, add int 1
                    case (_pttRadioList select 0): 
                    { 
                        (_pttList pushBack 1); 
                    }; 
                    //if ptt2 radio, add int 2
                    case (_pttRadioList select 1): 
                    { 
                        (_pttList pushBack 2); 
                    }; 
                    //if ptt3 radio, add int 3
                    case (_pttRadioList select 2): 
                    { 
                        (_pttList pushBack 3); 
                    }; 
                    //if radio without ptt set, add int 9
                    default 
                    { 
                        (_pttList pushBack 9); 
                    }; 
                };
                _radioChannel pushBack _currentChannel; //add channel to radioChannel
                _radioVolume pushBack _currentVolume; //add volume to radioVolume
                _radioSpatial pushBack _currentSpatial; //add spatial to radioSpatial
            } forEach _radios;
            //now we have four arrays - radioChannel, radioVolume, radioSpatial and pttList setup for every radio the player had
            [{
                params ["_newUnit", "_radioType", "_radioChannel", "_radioVolume", "_radioSpatial", "_pttList", "_newRadioList"];
                private _radios = [_radioType, _newUnit] call acre_api_fnc_getAllRadiosByType;
                {
                    //iterate through all NEW radios by IDs, add properties in the order set above
                    private _radioID = _x;
                    private _channel = _radioChannel select _forEachIndex;
                    private _volume = _radioVolume select _forEachIndex;
                    private _spatial = _radioSpatial select _forEachIndex;
                    private _pttKey = _pttList select _forEachIndex;
                    if (_pttKey == 1) then {_newRadioList set [0, _radioID]}; //assign new radio to PTT slot 1
                    if (_pttKey == 2) then {_newRadioList set [1, _radioID]}; //assign new radio to PTT slot 2
                    if (_pttKey == 3) then {_newRadioList set [2, _radioID]}; //assign new radio to PTT slot 3
                    [_radioID, _channel] call acre_api_fnc_setRadioChannel;
                    [_radioID, _volume] call acre_api_fnc_setRadioVolume;
                    [_radioID, _spatial] call acre_api_fnc_setRadioSpatial;
                } forEach _radios;
            }, [_newUnit, _radioType, _radioChannel, _radioVolume, _radioSpatial, _pttList, _newRadioList], 3] call CBA_fnc_waitAndExecute; //delay 3 secs for new radio IDs
        } forEach ["ACRE_PRC77", "ACRE_PRC117F", "ACRE_PRC148", "ACRE_PRC152", "ACRE_PRC343", "ACRE_SEM52SL", "ACRE_SEM70", "ACRE_BF888S"]; // for each available ACRE RADIO
        [{
            params ["_newRadioList"];
            //assign the radio IDs of those with assigned PTTs
            private _ptt1 = _newRadioList select 0;
            private _ptt2 = _newRadioList select 1; 
            private _ptt3 = _newRadioList select 2; 
            _pttNewRadioList = [ [_ptt1, _ptt2, _ptt3] ] call acre_api_fnc_setMultiPushToTalkAssignment; //assign new radios to old PTT setup
            systemChat format ["Your radio settings have been restored."];
        }, [_newRadioList], 5] call CBA_fnc_waitAndExecute; //delay 5 secs to allow above block to complete
    }];

Done. Add above script code to initPLayerLocal.sqf. All ACRE2 settings are retained after respawn.

Darojax commented 2 years ago
// Re-set radio settings on respawn START
player addEventHandler ["Respawn",
    {
        _newRadioList set [0, 1];
        _newRadioList set [1, 1];
        _newRadioList set [2, 1];
        params ["_unit", "_corpse", "_newRadioList"];
        _unit setUnitLoadout [[_corpse] call acre_api_fnc_filterUnitLoadout, false]; //copy old radio items from corpse to respawned player
        {
            private _radioType = _x;
            private _radios = [_radioType, _corpse] call acre_api_fnc_getAllRadiosByType; //return array of unique Radio IDs from corpse
            private _radioChannel = [];
            private _radioVolume = [];
            private _radioSpatial = [];
            private _pttList = [];
            _pttRadioList = [] call acre_api_fnc_getMultiPushToTalkAssignment; //return array of radioIDs assigned to each PTT key
            {
                //iterate through all radios by IDs, add their properties to arrays in a set order
                private _radioID = _x;
                private _currentChannel = [_radioID] call acre_api_fnc_getRadioChannel; //get channel of Radio
                private _currentVolume = [_radioID] call acre_api_fnc_getRadioVolume; //get volume of Radio
                private _currentSpatial = [_radioID] call acre_api_fnc_getRadioSpatial; //get spatial of Radio
                switch (_radioID) do 
                { 
                    //if ptt1 radio, add int 1
                    case (_pttRadioList select 0): 
                    { 
                        (_pttList pushBack 1); 
                    }; 
                    //if ptt2 radio, add int 2
                    case (_pttRadioList select 1): 
                    { 
                        (_pttList pushBack 2); 
                    }; 
                    //if ptt3 radio, add int 3
                    case (_pttRadioList select 2): 
                    { 
                        (_pttList pushBack 3); 
                    }; 
                    //if radio without ptt set, add int 9
                    default 
                    { 
                        (_pttList pushBack 9); 
                    }; 
                };
                _radioChannel pushBack _currentChannel; //add channel to radioChannel
                _radioVolume pushBack _currentVolume; //add volume to radioVolume
                _radioSpatial pushBack _currentSpatial; //add spatial to radioSpatial
            } forEach _radios;
            //now we have four arrays - radioChannel, radioVolume, radioSpatial and pttList setup for every radio the player had
            [{
                params ["_newUnit", "_radioType", "_radioChannel", "_radioVolume", "_radioSpatial", "_pttList", "_newRadioList"];
                private _radios = [_radioType, _newUnit] call acre_api_fnc_getAllRadiosByType;
                {
                    //iterate through all NEW radios by IDs, add properties in the order set above
                    private _radioID = _x;
                    private _channel = _radioChannel select _forEachIndex;
                    private _volume = _radioVolume select _forEachIndex;
                    private _spatial = _radioSpatial select _forEachIndex;
                    private _pttKey = _pttList select _forEachIndex;
                    if (_pttKey == 1) then {_newRadioList set [0, _radioID]}; //assign new radio to PTT slot 1
                    if (_pttKey == 2) then {_newRadioList set [1, _radioID]}; //assign new radio to PTT slot 2
                    if (_pttKey == 3) then {_newRadioList set [2, _radioID]}; //assign new radio to PTT slot 3
                    [_radioID, _channel] call acre_api_fnc_setRadioChannel;
                    [_radioID, _volume] call acre_api_fnc_setRadioVolume;
                    [_radioID, _spatial] call acre_api_fnc_setRadioSpatial;
                } forEach _radios;
            }, [_newUnit, _radioType, _radioChannel, _radioVolume, _radioSpatial, _pttList, _newRadioList], 3] call CBA_fnc_waitAndExecute; //delay 3 secs for new radio IDs
        } forEach ["ACRE_PRC77", "ACRE_PRC117F", "ACRE_PRC148", "ACRE_PRC152", "ACRE_PRC343", "ACRE_SEM52SL", "ACRE_SEM70", "ACRE_BF888S"]; // for each available ACRE RADIO
        [{
            params ["_newRadioList"];
            //assign the radio IDs of those with assigned PTTs
            private _ptt1 = _newRadioList select 0;
            private _ptt2 = _newRadioList select 1; 
            private _ptt3 = _newRadioList select 2; 
            _pttNewRadioList = [ [_ptt1, _ptt2, _ptt3] ] call acre_api_fnc_setMultiPushToTalkAssignment; //assign new radios to old PTT setup
            systemChat format ["Your radio settings have been restored."];
        }, [_newRadioList], 5] call CBA_fnc_waitAndExecute; //delay 5 secs to allow above block to complete
    }];
// Re-set radio settings on respawn END
Darojax commented 2 years ago

This code seems to remove the "Error Zero Divisor" when respawning and only carrying one, or no radios. Changed _newRadioList = []; to _newRadioList set [0, 1]; _newRadioList set [1, 1]; _newRadioList set [2, 1];

in the start of the code.

MildlyInterested commented 2 years ago
_newRadioList params [
  "_ptt1",
  ["_ptt2", ""],
  ["_ptt3", ""]
];

by veteran29 https://discord.com/channels/976165959041679380/976228128156446731/1029520540144255046