Common - Add wheel hitpoint function (#10075)

* Add wheel hitpoint function

* Moved cache to missionNamespace
This commit is contained in:
johnb432 2024-06-17 19:31:35 +02:00 committed by GitHub
parent c28a3d6cdf
commit 421071027e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 116 additions and 123 deletions

View File

@ -103,6 +103,7 @@ PREP(getWeaponAzimuthAndInclination);
PREP(getWeaponIndex);
PREP(getWeaponState);
PREP(getWeight);
PREP(getWheelHitPointsWithSelections);
PREP(getWindDirection);
PREP(getZoom);
PREP(goKneeling);

View File

@ -10,6 +10,7 @@ PREP_RECOMPILE_END;
GVAR(syncedEvents) = createHashMap;
GVAR(showHudHash) = createHashMap;
GVAR(vehicleIconCache) = createHashMap; // for getVehicleIcon
GVAR(wheelSelections) = createHashMap;
GVAR(blockItemReplacement) = false;

View File

@ -0,0 +1,104 @@
#include "..\script_component.hpp"
/*
* Author: commy2, johnb43
* Returns the wheel hitpoints and their selections.
*
* Arguments:
* 0: Vehicle <OBJECT>
*
* Return Value:
* 0: Wheel hitpoints <ARRAY>
* 1: Wheel hitpoint selections <ARRAY>
*
* Example:
* cursorObject call ace_common_fnc_getWheelHitPointsWithSelections
*
* Public: No
*/
params ["_vehicle"];
TRACE_1("params",_vehicle);
// TODO: Fix for GM vehicles
GVAR(wheelSelections) getOrDefaultCall [typeOf _vehicle, {
// Get the vehicles wheel config
private _wheels = configOf _vehicle >> "Wheels";
if (isClass _wheels) then {
// Get all hitpoints and selections
(getAllHitPointsDamage _vehicle) params ["_hitPoints", "_hitPointSelections"];
// Get all wheels and read selections from config
_wheels = "true" configClasses _wheels;
private _wheelHitPoints = [];
private _wheelHitPointSelections = [];
{
private _wheelName = configName _x;
private _wheelCenter = getText (_x >> "center");
private _wheelBone = getText (_x >> "boneName");
private _wheelBoneNameResized = _wheelBone select [0, 9]; // Count "wheel_X_Y"; // this is a requirement for physx. Should work for all addon vehicles.
TRACE_4("",_wheelName,_wheelCenter,_wheelBone,_wheelBoneNameResized);
private _wheelHitPoint = "";
private _wheelHitPointSelection = "";
// Commy's orginal method
{
if ((_wheelBoneNameResized != "") && {_x find _wheelBoneNameResized == 0}) exitWith { // same as above. Requirement for physx.
_wheelHitPoint = _hitPoints select _forEachIndex;
_wheelHitPointSelection = _hitPointSelections select _forEachIndex;
TRACE_2("wheel found [Orginal]",_wheelName,_wheelHitPoint);
};
} forEach _hitPointSelections;
if (_vehicle isKindOf "Car") then {
// Backup method, search for the closest hitpoint to the wheel's center selection pos.
// Ref #2742 - RHS's HMMWV
if (_wheelHitPoint == "") then {
private _wheelCenterPos = _vehicle selectionPosition _wheelCenter;
if (_wheelCenterPos isEqualTo [0, 0, 0]) exitWith {TRACE_1("no center?",_wheelCenter);};
private _bestDist = 99;
private _bestIndex = -1;
{
if (_x != "") then {
// Filter out things that definitly aren't wheeels (#3759)
if ((toLowerANSI (_hitPoints select _forEachIndex)) in ["hitengine", "hitfuel", "hitbody"]) exitWith {TRACE_1("filter",_x)};
private _xPos = _vehicle selectionPosition _x;
if (_xPos isEqualTo [0, 0, 0]) exitWith {};
private _xDist = _wheelCenterPos distance _xPos;
if (_xDist < _bestDist) then {
_bestIndex = _forEachIndex;
_bestDist = _xDist;
};
};
} forEach _hitPointSelections;
TRACE_2("closestPoint",_bestDist,_bestIndex);
if (_bestIndex != -1) then {
_wheelHitPoint = _hitPoints select _bestIndex;
_wheelHitPointSelection = _hitPointSelections select _bestIndex;
TRACE_2("wheel found [Backup]",_wheelName,_wheelHitPoint);
};
};
};
if ((_wheelHitPoint != "") && {_wheelHitPointSelection != ""}) then {
_wheelHitPoints pushBack _wheelHitPoint;
_wheelHitPointSelections pushBack _wheelHitPointSelection;
};
} forEach _wheels;
[_wheelHitPoints, _wheelHitPointSelections]
} else {
// Exit with nothing if the vehicle has no wheels class
TRACE_1("No Wheels",_wheels);
[[], []]
}
}, true] // return

View File

@ -17,38 +17,22 @@
*/
params ["_vehicle", "_position"];
TRACE_1("damageWheelsAndEngine",_vehicle);
TRACE_2("damageWheelsAndEngine",_vehicle,_position);
// Vehicle needs to be local and vulnerable
if !(local _vehicle && {isDamageAllowed _vehicle}) exitWith {};
// Burn tires
private _fnc_isWheelHitPoint = {
params ["_selectionName"];
// Wheels must use a selection named "wheel_X_Y_steering" for PhysX to work
_selectionName select [0, 6] == "wheel_" && {
_selectionName select [count _selectionName - 9] == "_steering"
} // return
};
private _config = configOf _vehicle >> "HitPoints";
{
private _wheelSelection = getText (_config >> _x >> "name");
if (_wheelSelection call _fnc_isWheelHitPoint) then {
private _wheelPosition = _vehicle modelToWorld (_vehicle selectionPosition _wheelSelection);
if (_position distance _wheelPosition < EFFECT_SIZE * 2) then {
_vehicle setHit [_wheelSelection, 1];
};
// If wheel is close enough to incendiary, burn it
if (_position distance (_vehicle modelToWorld (_vehicle selectionPosition _x)) < EFFECT_SIZE * 2) then {
_vehicle setHit [_x, 1];
};
} forEach (getAllHitPointsDamage _vehicle param [0, []]);
} forEach ((_vehicle call EFUNC(common,getWheelHitPointsWithSelections)) select 1);
// Burn car engines only
if (_vehicle isKindOf "Wheeled_APC_F") exitWith {};
private _engineSelection = getText (_config >> "HitEngine" >> "name");
private _engineSelection = getText (configOf _vehicle >> "HitPoints" >> "HitEngine" >> "name");
private _enginePosition = _vehicle modelToWorld (_vehicle selectionPosition _engineSelection);
if (_position distance _enginePosition < EFFECT_SIZE * 2) then {

View File

@ -25,7 +25,6 @@ PREP(getSelectionsToIgnore);
PREP(getPatchWheelTime);
PREP(getPostRepairDamage);
PREP(getRepairItems);
PREP(getWheelHitPointsWithSelections);
PREP(hasItems);
PREP(isEngineer);
PREP(isInRepairFacility);

View File

@ -4,6 +4,7 @@
#include "..\script_component.hpp"
addMissionEventHandler ["Draw3D", {
if (isGamePaused) exitWith {};
if !((cursorObject isKindOf "Car") || (cursorObject isKindOf "Tank") || (cursorObject isKindOf "Air")) exitWith {};
private _config = configOf cursorObject;
@ -11,7 +12,7 @@ addMissionEventHandler ["Draw3D", {
private _hitpointGroups = getArray (_config >> QGVAR(hitpointGroups));
(getAllHitPointsDamage cursorObject) params [["_hitPoints", []], ["_hitSelections", []]];
([cursorObject] call FUNC(getWheelHitPointsWithSelections)) params ["_wheelHitPoints", "_wheelHitSelections"];
([cursorObject] call EFUNC(common,getWheelHitPointsWithSelections)) params ["_wheelHitPoints", "_wheelHitSelections"];
private _output = [];

View File

@ -37,7 +37,7 @@ private _selectionsToIgnore = _vehicle call FUNC(getSelectionsToIgnore);
(getAllHitPointsDamage _vehicle) params [["_hitPoints", []], ["_hitSelections", []]]; // Since 1.82 these are all lower case
// get hitpoints of wheels with their selections
([_vehicle] call FUNC(getWheelHitPointsWithSelections)) params ["_wheelHitPoints", "_wheelHitSelections"];
([_vehicle] call EFUNC(common,getWheelHitPointsWithSelections)) params ["_wheelHitPoints", "_wheelHitSelections"];
private _hitPointsAddedNames = [];
private _hitPointsAddedStrings = [];

View File

@ -28,7 +28,7 @@ private _turretPaths = ((fullCrew [_vehicle, "gunner", true]) + (fullCrew [_vehi
(getAllHitPointsDamage _vehicle) params [["_hitPoints", []], ["_hitSelections", []]];
// get hitpoints of wheels with their selections
([_vehicle] call FUNC(getWheelHitPointsWithSelections)) params ["_wheelHitPoints", "_wheelHitSelections"];
([_vehicle] call EFUNC(common,getWheelHitPointsWithSelections)) params ["_wheelHitPoints", "_wheelHitSelections"];
private _indexesToIgnore = [];
private _processedSelections = [];

View File

@ -1,97 +0,0 @@
#include "..\script_component.hpp"
/*
* Author: commy2
* Returns the wheel hitpoints and their selections.
*
* Arguments:
* 0: Vehicle <OBJECT>
*
* Return Value:
* 0: Wheel hitpoints <ARRAY>
* 1: Wheel hitpoint selections in model coordinates <ARRAY>
*
* Example:
* [car1] call ace_repair_fnc_getWheelHitPointsWithSelections
*
* Public: No
*/
params ["_vehicle"];
TRACE_1("params",_vehicle);
// get the vehicles wheel config
private _wheels = configOf _vehicle >> "Wheels";
// exit with nothing if the vehicle has no wheels class
if !(isClass _wheels) exitWith {TRACE_1("No Wheels",_wheels); [[],[]]};
// get all hitpoints and selections
(getAllHitPointsDamage _vehicle) params ["_hitPoints", "_hitPointSelections"];
// get all wheels and read selections from config
_wheels = "true" configClasses _wheels;
private _wheelHitPoints = [];
private _wheelHitPointSelections = [];
{
private _wheelName = configName _x;
private _wheelCenter = getText (_x >> "center");
private _wheelBone = getText (_x >> "boneName");
private _wheelBoneNameResized = _wheelBone select [0, 9]; //ount "wheel_X_Y"; // this is a requirement for physx. Should work for all addon vehicles.
TRACE_4("",_wheelName,_wheelCenter,_wheelBone,_wheelBoneNameResized);
private _wheelHitPoint = "";
private _wheelHitPointSelection = "";
//Commy's orginal method
{
if ((_wheelBoneNameResized != "") && {_x find _wheelBoneNameResized == 0}) exitWith { // same as above. Requirement for physx.
_wheelHitPoint = _hitPoints select _forEachIndex;
_wheelHitPointSelection = _hitPointSelections select _forEachIndex;
TRACE_2("wheel found [Orginal]",_wheelName,_wheelHitPoint);
};
} forEach _hitPointSelections;
if (_vehicle isKindOf "Car") then {
//Backup method, search for the closest hitpoint to the wheel's center selection pos.
//Ref #2742 - RHS's HMMWV
if (_wheelHitPoint == "") then {
private _wheelCenterPos = _vehicle selectionPosition _wheelCenter;
if (_wheelCenterPos isEqualTo [0,0,0]) exitWith {TRACE_1("no center?",_wheelCenter);};
private _bestDist = 99;
private _bestIndex = -1;
{
if (_x != "") then {
//Filter out things that definitly aren't wheeels (#3759)
if ((toLowerANSI (_hitPoints select _forEachIndex)) in ["hitengine", "hitfuel", "hitbody"]) exitWith {TRACE_1("filter",_x)};
private _xPos = _vehicle selectionPosition _x;
if (_xPos isEqualTo [0,0,0]) exitWith {};
private _xDist = _wheelCenterPos distance _xPos;
if (_xDist < _bestDist) then {
_bestIndex = _forEachIndex;
_bestDist = _xDist;
};
};
} forEach _hitPointSelections;
TRACE_2("closestPoint",_bestDist,_bestIndex);
if (_bestIndex != -1) then {
_wheelHitPoint = _hitPoints select _bestIndex;
_wheelHitPointSelection = _hitPointSelections select _bestIndex;
TRACE_2("wheel found [Backup]",_wheelName,_wheelHitPoint);
};
};
};
if ((_wheelHitPoint != "") && {_wheelHitPointSelection != ""}) then {
_wheelHitPoints pushBack _wheelHitPoint;
_wheelHitPointSelections pushBack _wheelHitPointSelection;
};
} forEach _wheels;
[_wheelHitPoints, _wheelHitPointSelections]