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

Add speed limit attribute #699

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
11 changes: 11 additions & 0 deletions addons/attributes/XEH_postInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ if (isServer) then {
_unit enableAIFeature [_x, _abilities select _forEachIndex];
} forEach AI_ABILITIES;
}] call CBA_fnc_addEventHandler;

["CAManBase", "Local", { // If locality changes, forceSpeed must be reapplied.
params ["_entity", "_isLocal"];
if (!_isLocal || {!alive _entity} || {!(_entity isKindOf "CAManBase")}) exitWith {};

private _setSpeed = _entity getVariable [QGVAR(setSpeed), 0];

if (_setSpeed != 0) then {
[_entity, _setSpeed] call FUNC(setSpeed);
};
}, true, [], true] call CBA_fnc_addClassEventHandler;
56 changes: 45 additions & 11 deletions addons/attributes/initAttributes.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,53 @@
{alive _entity && {_entity call EFUNC(common,getVehicleAmmo) != -1}}
] call FUNC(addAttribute);

[
[ // Helicopter
"Object",
[LSTRING(SpeedLimit), LSTRING(SpeedLimit_Tooltip)],
[ELSTRING(Modules,ModuleConvoyParameters_Speed), ELSTRING(Modules,ModuleConvoyParameters_Speed_Tooltip)],
QGVAR(slider),
[-50, 500, 5, false, 0],
[-50, 200, 5, false, 0],
{
{
[QEGVAR(common,limitSpeed), [_entity, _value], _entity] call CBA_fnc_targetEvent;
_entity setVariable [QGVAR(SpeedLimit), _value, true];
} forEach call EFUNC(common,getSelectedVehicles);
private _vehicles = [] call EFUNC(common,getSelectedVehicles) select {
alive _x && {!isPlayer driver _x} && {_x isKindOf "Helicopter"}
};
{ _x setVariable [QGVAR(setSpeed), _value, true]; } forEach _vehicles;
[QEGVAR(common,setSpeed), [_vehicles, _value], _vehicles] call CBA_fnc_targetEvent;
},
{_entity getVariable [QGVAR(SpeedLimit), 0]},
{GVAR(enableSpeedLimit) && {alive _entity} && {!isPlayer driver _entity} && {_entity isKindOf "LandVehicle" || {_entity isKindOf "Air"} || {_entity isKindOf "Ship"}}}
{_entity getVariable [QGVAR(setSpeed), 0]},
{GVAR(enableSetSpeed) && {alive _entity} && {!isPlayer driver _entity} && {_entity isKindOf "Helicopter"}}
] call FUNC(addAttribute);

[ // LandVehicle and Ship
"Object",
[ELSTRING(Modules,ModuleConvoyParameters_Speed), ELSTRING(Modules,ModuleConvoyParameters_Speed_Tooltip)],
QGVAR(slider),
[0, 100, 5, false, 0],
{
private _vehicles = [] call EFUNC(common,getSelectedVehicles) select {
alive _x && {!isPlayer driver _x} && {_x isKindOf "LandVehicle" || {_x isKindOf "Ship"}}
};
{ _x setVariable [QGVAR(setSpeed), _value, true]; } forEach _vehicles;
[QEGVAR(common,setSpeed), [_vehicles, _value], _vehicles] call CBA_fnc_targetEvent;
},
{_entity getVariable [QGVAR(setSpeed), 0]},
{GVAR(enableSetSpeed) && {alive _entity} && {!isPlayer driver _entity} && {_entity isKindOf "LandVehicle" || {_entity isKindOf "Ship"}}}
//{GVAR(enableSetSpeed) && {alive _entity} && {!isPlayer driver _entity} && {_entity isKindOf "LandVehicle" || {_entity isKindOf "Ship"}}}
] call FUNC(addAttribute);

[ // Plane
"Object",
[ELSTRING(Modules,ModuleConvoyParameters_Speed), ELSTRING(Modules,ModuleConvoyParameters_Speed_Tooltip)],
QGVAR(slider),
[0, 1000, 5, false, 0],
{
private _vehicles = [] call EFUNC(common,getSelectedVehicles) select {
alive _x && {!isPlayer driver _x} && {_x isKindOf "Plane"}
};
{ _x setVariable [QGVAR(setSpeed), _value, true]; } forEach _vehicles;
[QEGVAR(common,setSpeed), [_vehicles, _value], _vehicles] call CBA_fnc_targetEvent;
},
{_entity getVariable [QGVAR(setSpeed), 0]},
{GVAR(enableSetSpeed) && {alive _entity} && {!isPlayer driver _entity} && {_entity isKindOf "Plane"}}
] call FUNC(addAttribute);

[
Expand Down Expand Up @@ -155,7 +189,7 @@
} forEach call EFUNC(common,getSelectedVehicles);
},
{locked _entity},
{alive _entity} && {_entity isKindOf "LandVehicle" || {_entity isKindOf "Air"} || {_entity isKindOf "Ship"}}
{alive _entity && {_entity isKindOf "LandVehicle" || {_entity isKindOf "Air"} || {_entity isKindOf "Ship"}}}
] call FUNC(addAttribute);

[
Expand Down Expand Up @@ -264,7 +298,7 @@
{
_entity getVariable [QGVAR(respawnPos), []] param [0, sideEmpty]
},
{alive _entity} && {canMove _entity} && {_entity isKindOf "AllVehicles"} && {!(_entity isKindOf "Animal")}
{alive _entity && {canMove _entity} && {_entity isKindOf "AllVehicles"} && {!(_entity isKindOf "Animal")}}
] call FUNC(addAttribute);

[
Expand Down
4 changes: 2 additions & 2 deletions addons/attributes/initSettings.sqf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
QGVAR(enableSpeedLimit),
QGVAR(enableSetSpeed),
"CHECKBOX",
[LSTRING(SpeedLimit), LSTRING(EnableAttribute_Description)],
[LSTRING(SetSpeed), LSTRING(EnableAttribute_Description)],
[ELSTRING(main,DisplayName), "str_3den_display3den_menubar_attributes_text"],
true,
false
Expand Down
1 change: 1 addition & 0 deletions addons/common/XEH_PREP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ PREP(serializeInventory);
PREP(serializeObjects);
PREP(setLampState);
PREP(setMagazineAmmo);
PREP(setSpeed);
PREP(setTurretAmmo);
PREP(setVehicleAmmo);
PREP(showMessage);
Expand Down
6 changes: 6 additions & 0 deletions addons/common/XEH_postInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@
_vehicle limitSpeed _speed;
}] call CBA_fnc_addEventHandler;

[QGVAR(forceSpeed), {
params ["_vehicle", "_speed"];
_vehicle forceSpeed _speed;
}] call CBA_fnc_addEventHandler;

[QGVAR(forceFollowRoad), {
params ["_vehicle", "_mode"];
_vehicle forceFollowRoad _mode;
Expand Down Expand Up @@ -330,6 +335,7 @@
[QGVAR(setLampState), LINKFUNC(setLampState)] call CBA_fnc_addEventHandler;
[QGVAR(setMagazineAmmo), LINKFUNC(setMagazineAmmo)] call CBA_fnc_addEventHandler;
[QGVAR(setTurretAmmo), LINKFUNC(setTurretAmmo)] call CBA_fnc_addEventHandler;
[QGVAR(setSpeed), LINKFUNC(setSpeed)] call CBA_fnc_addEventHandler;
[QGVAR(showMessage), LINKFUNC(showMessage)] call CBA_fnc_addEventHandler;

if (isServer) then {
Expand Down
60 changes: 60 additions & 0 deletions addons/common/functions/fnc_setSpeed.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "script_component.hpp"
/*
* Author: Ampersand
* Sets the movement speed mode of the given vehicle(s).
*
* Arguments:
* 0: Vehicle <OBJECT|ARRAY>
* 1: Speed <NUMBER> (default: 0)
* Man: Speed modes
* Vehicles: km/h
*
* 2.12 | forceSpeed | limitSpeed
* _speed | neg zero pos | neg zero pos -(_man getSpeed "SLOW")
* -------|-------------------------|---------------------------------------------------------------------
* man | reset stop yes | reset stop yes reverse, doesn't work if combat mode
* car | reset stop yes | reset reset yes
* tank | reset stop yes | reset reset yes
* heli | no-eff no-eff no-eff | reverse stop yes
* plane | reset reset yes | min min yes
* vtol | reset reset yes | min min yes
* boat | reset stop yes | reset stop yes
*
* Return Value:
* None
*
* Example:
* [_vehicle, 1] call zen_common_fnc_setSpeed
*
* Public: No
*/

#define SPEED_RESET 0
#define SPEED_FAST 1
#define SPEED_NORMAL 2
#define SPEED_SLOW 3
#define SPEED_BACK 4

params ["_vehicle", "_speed"];

if (_vehicle isEqualType []) exitWith {
{
[_x, _speed] call FUNC(setSpeed);
Copy link
Contributor

@rautamiekka rautamiekka Mar 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if spawn would speed things along ?

^ Granted, my laptop doesn't run both the game and the Server instance simultaneously especially fast, and on it I noticed that forEach+spawn takes a noticeable time to get things done compared to call, like CBA_fnc_setLoadout can take something like 400-600ms (looks like it) to get the squad equipped (which also happens in a random order) instead of next to instantly (in the squad leader's command bar order).

^ Might help when someone has a less good Internet connection, dunno.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting, so each one can run "in parallel"?
As for network, I tried to structure the CBA event and the function such that the selected array is pre-filtered and then only sent on network once, if I'm getting your meaning correctly.

} forEach _vehicle;
};

if (!local _vehicle) exitWith {};

if (_vehicle isKindOf "Helicopter") exitWith {
if (_speed == 0) exitWith {
_vehicle limitSpeed (getNumber (configOf _vehicle >> "maxSpeed")); // Reset
};
_vehicle limitSpeed _speed; // Allow reverse
};

// Planes, LandVehicle, Ship
if (_speed == 0) then {
_speed = -1; // Reset
};

_vehicle forceSpeed (_speed / 3.6);