Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom event system and Action management #420

Merged
merged 16 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
KPLIB_fnc_common_createNamespace.sqf

File: fn_common_createNamespace.sqf
Author: KP Liberation Dev Team - https://github.com/KillahPotatoes
Date: 2018-05-20
Last Update: 2018-05-20
License: GNU General Public License v3.0 - https://www.gnu.org/licenses/gpl-3.0.html

Description:
Create namespace. Used to hold variables in more "object oriented" way.
Get and set variables via setVariable and getVariable.

Parameter(s):
0: BOOL - Should created namespace be global

Returns:
OBJECT/LOCATION - object if global namespace, location otherwise
*/
#define KPLIB_NAMESPACE_POS [-1000,-1000,0]

params [["_global", false]];

if(_global isEqualTo true) then {
// Create simple object as variable container. The simpler the model the better.
createSimpleObject ["A3\weapons_f\empty.p3d", KPLIB_NAMESPACE_POS];
} else {
// If namespace is not global use location as it is the "cheapest" object you can get.
createLocation ["INVISIBLE", KPLIB_NAMESPACE_POS, 0, 0];
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
KPLIB_fnc_core_spawnVehicle
KPLIB_fnc_common_spawnVehicle

File: fn_core_spawnVehicle.sqf
File: fn_common_spawnVehicle.sqf
Author: KP Liberation Dev Team - https://github.com/KillahPotatoes
Date: 2018-05-03
Last Update: 2018-05-03
Last Update: 2018-05-28
License: GNU General Public License v3.0 - https://www.gnu.org/licenses/gpl-3.0.html

Description:
Expand All @@ -20,7 +20,7 @@
OBJECT / BOOL - Depends on 3rd parameter. True -> returns object, False -> return true on success.
*/

params ["_classname", "_spawnPos", ["_spawnDir", random 360], ["_returnVeh", false]];
params [["_classname", nil, [""]], ["_spawnPos", nil, [[]]], ["_spawnDir", random 360], ["_returnVeh", false]];

// Return variable
private _return = true;
Expand Down Expand Up @@ -49,4 +49,6 @@ _vehicle allowDamage true;

if (_returnVeh) then {_return = _vehicle;};

["vehicle_spawned", [_vehicle]] call KPLIB_fnc_event_trigger;

_return
11 changes: 11 additions & 0 deletions Missionframework/modules/01_core/fnc/event/defines.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Defines for Event functions
*/

#define DEBUG_EVENTS

#ifdef DEBUG_EVENTS
#define DEBUG
#endif

#define LOG_PREFIX "[KP LIBERATION] [EVENT] "
45 changes: 45 additions & 0 deletions Missionframework/modules/01_core/fnc/event/fn_event_addHandler.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "defines.hpp"
/*
KPLIB_fnc_event_addHandler

File: fn_event_addHandler.sqf
Author: KP Liberation Dev Team - https://github.com/KillahPotatoes
Date: 2018-05-18
Last Update: 2018-05-23
License: GNU General Public License v3.0 - https://www.gnu.org/licenses/gpl-3.0.html

Description:
Adds Liberation scripted event handler

Parameter(s):
0: STRING - Event name
1: CODE - Code executed when event is triggered

Returns:
NUMBER - ID of the handler for given event
*/

params [
["_name", nil, [""]],
["_code", nil, [{}]]
];

private _namespace = KPLIB_eventNamespace;
private _handlers = _namespace getVariable _name;

// If there is no handlers array initialize new and return id
if(isNil "_handlers") exitWith {
_namespace setVariable [_name, [_code]];
0 // Return first handler ID
};

// Find first empty array position denoted by -1
private _id = _handlers find -1;

if (_id < 0) then { // No empty space append to array
_id = _handlers pushBack _code;
} else { // Empty space found, reuse index
_handlers set [_id, _code];
};
// Return handler ID
_id
45 changes: 45 additions & 0 deletions Missionframework/modules/01_core/fnc/event/fn_event_init.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "defines.hpp"
/*
KPLIB_fnc_event_init

File: fn_event_init.sqf
Author: KP Liberation Dev Team - https://github.com/KillahPotatoes
Date: 2018-05-18
Last Update: 2018-05-23
License: GNU General Public License v3.0 - https://www.gnu.org/licenses/gpl-3.0.html

Description:
Initializes Liberation event system

Parameter(s):
NONE

Returns:
BOOLEAN
*/

// Exit if already initialized
if(!isNil "KPLIB_event_namespace") exitWith {
diag_log LOG_PREFIX + "Event system already initialized!";
};

KPLIB_eventNamespace = call KPLIB_fnc_common_createNamespace;

// we can't add public var Eh at preinit, spawn will fix it.
// "Inspired" by: <https://github.com/CBATeam/CBA_A3/blob/435ae41e9282ab71d34150bb3c1a8621a18bcfb0/addons/events/XEH_preInit.sqf#L19>
[] spawn {
// Subscribe to global events
"KPLIB_eventGlobalReceiver" addPublicVariableEventHandler {
#ifdef DEBUG_EVENTS
diag_log format[LOG_PREFIX + "Recived: %1", _this joinString ", " ];
#endif
(_this select 1) call KPLIB_fnc_event_trigger;
};
};

// Create server side event loop
if(isServer) then {
execVM "modules\01_core\scripts\server\eventLoop.sqf";
};

true
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
KPLIB_fnc_event_removeHandler

File: fn_event_removeHandler.sqf
Author: KP Liberation Dev Team - https://github.com/KillahPotatoes
Date: 2018-05-18
Last Update: 2018-05-23
License: GNU General Public License v3.0 - https://www.gnu.org/licenses/gpl-3.0.html

Description:
Removes Liberation scripted event handler

Parameter(s):
0: STRING - Name of event
1: NUMBER - ID of the handler for given event

Returns:
BOOLEAN - TRUE if removed, FALSE if didn't exist
*/

params [
["_name", nil, [""]],
["_id", nil, [0]]
];

private _namespace = KPLIB_eventNamespace;
private _handlers = _namespace getVariable [_name, []];

// Id must be unsigned and in handlers array range
if(_id > -1 && (count _handlers) > _id) exitWith {
_handlers set [_id, -1];
//Handler found and removed
true
};

// No handler with given ID
false
48 changes: 48 additions & 0 deletions Missionframework/modules/01_core/fnc/event/fn_event_trigger.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "defines.hpp"
/*
KPLIB_fnc_event_trigger

File: fn_event_trigger.sqf
Author: KP Liberation Dev Team - https://github.com/KillahPotatoes
Date: 2018-05-18
Last Update: 2018-05-23
License: GNU General Public License v3.0 - https://www.gnu.org/licenses/gpl-3.0.html

Description:
Triggers Liberation scripted event handler

Parameter(s):
0: STRING - Name of event
1: ARRAY - Array of arguments passed to triggered event
2: BOOL - OPTIONAL - Should event be global. Local by default.

Returns:
BOOLEAN
*/
#ifdef DEBUG_EVENTS
diag_log format[LOG_PREFIX + "Triggered: %1", _this joinString ", "];
#endif

params [
["_name", nil, [""]],
["_arguments", [], [[]]],
["_global", false, [false]]
];

private _namespace = KPLIB_eventNamespace;
private _handlers = _namespace getVariable [_name, []];

// Call every handler for event
{
if (_x isEqualType {}) then {
_arguments call _x;
}
} forEach _handlers;

// Broadcast global event
if(_global) then {
KPLIB_eventGlobalReceiver = [_name, _arguments];
publicVariable "KPLIB_eventGlobalReceiver";
};

true
2 changes: 2 additions & 0 deletions Missionframework/modules/01_core/fnc/fn_core_buildFob.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ if (_interactive) then {
deleteVehicle _box;
private _marker = [_buildPos] call KPLIB_fnc_core_createFobMarker;
KPLIB_sectors_fobs pushBack _marker;
["fob_built", [_marker]] call KPLIB_fnc_event_trigger;
} else {
private _marker = [_buildPos] call KPLIB_fnc_core_createFobMarker;
KPLIB_sectors_fobs pushBack _marker;
["fob_built", [_marker]] call KPLIB_fnc_event_trigger;
};

publicVariable "KPLIB_sectors_fobs";
Expand Down
15 changes: 7 additions & 8 deletions Missionframework/modules/01_core/fnc/fn_core_cinematic.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

// Intro cinematic started
KPLIB_intro_started = true;
KPLIB_intro_running = true;

// Transition and position storage variables
private _lastTransition = -1;
Expand All @@ -41,13 +41,13 @@ _cam camcommit 0;
private _firstRound = true;

// Show the intro cinematic until it's aborted
while {KPLIB_intro_started} do {
while {KPLIB_intro_running} do {

// Wait until the last camera round is finished or the intro is aborted
waitUntil {!KPLIB_intro_started || camCommitted _cam};
waitUntil {!KPLIB_intro_running || camCommitted _cam};

// If the intro is still running, commit next camera round
if (KPLIB_intro_started) then {
if (KPLIB_intro_running) then {

// Deactivate NVG effect for the camera
camUseNVG false;
Expand All @@ -62,7 +62,7 @@ while {KPLIB_intro_started} do {
// Add 2 FOBs as possible camera targets
if ((count KPLIB_sectors_fobs) > 0) then {
for "_i" from 1 to 2 step 1 do {
_targetPositions pushBack (selectRandom KPLIB_sectors_fobs);
_targetPositions pushBack (getMarkerPos (selectRandom KPLIB_sectors_fobs));
};
};

Expand Down Expand Up @@ -299,14 +299,14 @@ while {KPLIB_intro_started} do {
// Get the name of the sector we're looking at
private _sectorName = "";
if (_actualTargetPos distance KPLIB_eden_startbase < 300) then {
_sectorName = "BEGIN OF OPERATION";
_sectorName = localize "STR_INTRO_BEGIN";
} else {
_sectorName = [300, _actualTargetPos] call KPLIB_fnc_core_getNearestSector;
if (_sectorName != "") then {
_sectorName = markertext _sectorName;
} else {
// If it's not a player, not a sector and not the starting base, it has to be a FOB
_nearFobs = KPLIB_sectors_fobs select {_x distance _actualTargetPos < 300};
_nearFobs = KPLIB_sectors_fobs select {getMarkerPos _x distance _actualTargetPos < 300};
if (count _nearFobs > 0) then {
_sectorName = format ["FOB %1", KPLIB_preset_alphabet select (KPLIB_sectors_fobs find (_nearFobs select 0))];
};
Expand All @@ -323,4 +323,3 @@ _cam cameraEffect ["Terminate", "BACK"];
camDestroy _cam;
camUseNVG false;
deleteVehicle _pointer;
KPLIB_intro_stopped = true;
6 changes: 6 additions & 0 deletions Missionframework/modules/01_core/fnc/fn_core_handleSector.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ private _sectorTimer = diag_tickTime - KPLIB_sectors_stayActiveTime;
* TODO:
* At this place we should add the spawning and other "upon sector activation" stuff when we reached that development stage.
*/
// Emit event about activated sector
["sector_activated", [_sectorMarkerName], true] call KPLIB_fnc_event_trigger;

// Loop until the sector is abandoned.
while {_sectorActive} do {
Expand All @@ -54,6 +56,7 @@ while {_sectorActive} do {
if (_sectorCaptureReady) then {
_sectorActive = false;
[_sectorMarkerName] call KPLIB_fnc_core_changeSectorOwner;
["sector_captured", [_sectorMarkerName], true] call KPLIB_fnc_event_trigger;
} else {
_sectorCaptureReady = true;
};
Expand All @@ -71,6 +74,9 @@ while {_sectorActive} do {
KPLIB_sectors_active = KPLIB_sectors_active - [_sectorMarkerName];
publicVariable "KPLIB_sectors_active";

// Emit event about deactivated sector
["sector_deactivated", [_sectorMarkerName], true] call KPLIB_fnc_event_trigger;

// As the argument for changing a sector color should be global this should be fine. If not, we've to change it to something like
// If isServer then call, else remoteExec on server (concerning sector handling by HCs in the end)
call KPLIB_fnc_core_updateSectorMarkers;
29 changes: 15 additions & 14 deletions Missionframework/modules/01_core/fnc/fn_core_intro.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,28 @@
// Wait until mission is loaded
waitUntil {time > 0};

// Spawn cinematic camera procedure
[] spawn KPLIB_fnc_core_cinematic;

// If intro is enabled, show intro texts. Otherwise skip.
if (KPLIB_param_intro > 0) then {
// Spawn cinematic camera procedure
[] spawn KPLIB_fnc_core_cinematic;

// Show intro texts
uiSleep 1;
cutRsc ["intro1", "PLAIN", 1, true];
uiSleep 5.5;
cutRsc ["intro2", "PLAIN", 1, true];
uiSleep 10;
};

// Show the intro dialog for play or tutorial
createDialog "KPLIB_introMenu";
waitUntil {dialog};
waitUntil {KPLIB_intro_startGame == 1 || KPLIB_intro_tutorial == 1 || !dialog};
closeDialog 0;

// Show tutorial, if selected
if (KPLIB_intro_tutorial == 0) then {
KPLIB_intro_started = false;
// Show the intro dialog for play or tutorial
createDialog "KPLIB_introMenu";
// If we are not able to open introMenu we should continue
waitUntil {dialog};
waitUntil {KPLIB_intro_startGame == 1 || KPLIB_intro_tutorial == 1 || !dialog};
closeDialog 0;

// Show tutorial, if selected
if (KPLIB_intro_tutorial == 0) then {
KPLIB_intro_running = false;
};
};

// Finish the intro
Expand Down
Loading