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

cleanup laserpointer #2871

Merged
merged 1 commit into from
Nov 28, 2015
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
57 changes: 38 additions & 19 deletions addons/laserpointer/functions/fnc_drawLaserpoint.sqf
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
// by commy2 and esteldunedain
/*
* Author: commy2 and esteldunedain
* Draw a Laser Point
*
* Arguments:
* 0: Target unit <OBJECT>
* 1: Range <NUMBER>
* 2: is Green <BOOL>
* 3: Brightness <NUMBER>
*
* Return Value:
* None
*
* Example:
* [player, 10, false, 2] call ace_laserpointer_fnc_drawLaserpoint
*
* Public: No
*/
#include "script_component.hpp"

params ["_unit", "_range", "_isGreen", "_brightness"];
params ["_target", "_range", "_isGreen", "_brightness"];

private ["_p0", "_v1", "_v2", "_v3", "_p1", "_pL", "_distance", "_pL2", "_camPos", "_size"];
private _unit = ACE_player;

_p0 = AGLToASL (_unit modelToWorldVisual (_unit selectionPosition "righthand"));
private _p0 = AGLToASL (_target modelToWorldVisual (_target selectionPosition "righthand"));

// Find a system of orthogonal reference vectors
// _v1 points in the direction of the weapon
// _v2 points to the right of the weapon
// _v3 points to the top side of the weapon
_v1 = _unit weaponDirection currentWeapon _unit;
_v2 = vectorNormalized (_v1 vectorCrossProduct [0,0,1]);
_v3 = _v2 vectorCrossProduct _v1;
private _v1 = _target weaponDirection currentWeapon _target;
private _v2 = vectorNormalized (_v1 vectorCrossProduct [0,0,1]);
private _v3 = _v2 vectorCrossProduct _v1;

// Offset over the 3 reference axis
// This offset could eventually be configured by weapon in the config
Expand All @@ -22,23 +39,24 @@ _v3 = _v2 vectorCrossProduct _v1;
#define OFFV3 0.08

// Offset _p0, the start of the laser
_p0 = _p0 vectorAdd (_v1 vectorMultiply OFFV1) vectorAdd (_v3 vectorMultiply OFFV3) vectorAdd (_v2 vectorMultiply OFFV2);
_p0 = _p0 vectorAdd (_v1 vectorMultiply OFFV1) vectorAdd (_v3 vectorMultiply OFFV3) vectorAdd (_v2 vectorMultiply OFFV2);

// Calculate _p1, the potential end of the laser
_p1 = _p0 vectorAdd (_v1 vectorMultiply _range);
private _p1 = _p0 vectorAdd (_v1 vectorMultiply _range);

_pL = lineIntersectsSurfaces [_p0, _p1, ACE_player, vehicle ACE_player, true, 1, "GEOM", "FIRE"] select 0 select 0;
private _pL = lineIntersectsSurfaces [_p0, _p1, _unit, vehicle _unit] select 0 select 0;

// no intersection found, quit
if (isNil "_pL") exitWith {};

_distance = _p0 vectorDistance _pL;
private _distance = _p0 vectorDistance _pL;

//systemChat str _distance;
if (_distance < 0.5) exitWith {};

_pL = _p0 vectorAdd (_v1 vectorMultiply _distance);
_pL2 = _p0 vectorAdd (_v1 vectorMultiply (_distance - 0.5));

private _pL2 = _p0 vectorAdd (_v1 vectorMultiply (_distance - 0.5));

_pL = ASLtoAGL _pL;

Expand All @@ -50,19 +68,20 @@ drawLine3D [
];
*/

//systemChat str [_unit, "FIRE"] intersect [_camPos, _pL];
//systemChat str [_target, "FIRE"] intersect [_camPos, _pL];

private _camPos = positionCameraToWorld [0,0,0.2];

_camPos = positionCameraToWorld [0,0,0.2];
if (count ([_unit, "FIRE"] intersect [_camPos, _pL]) > 0) exitWith {};
if (count ([ACE_player, "FIRE"] intersect [_camPos, _pL]) > 0) exitWith {};
if (count ([_target, "FIRE"] intersect [_camPos, _pL]) > 0) exitWith {};
if (count ([_unit, "FIRE"] intersect [_camPos, _pL]) > 0) exitWith {};

// Convert _camPos to ASL
_camPos = AGLToASL _camPos;

if ( terrainIntersectASL [_camPos, _pL2]) exitWith {};
if ( lineIntersects [_camPos, _pL2]) exitWith {};
if (terrainIntersectASL [_camPos, _pL2]) exitWith {};
if (lineIntersects [_camPos, _pL2]) exitWith {};

_size = 2 * (_range - (positionCameraToWorld [0,0,0] vectorDistance _pL)) / _range;
private _size = 2 * (_range - (positionCameraToWorld [0,0,0] vectorDistance _pL)) / _range;

drawIcon3D [
format ["\a3\weapons_f\acc\data\collimdot_%1_ca.paa", ["red", "green"] select _isGreen],
Expand Down
43 changes: 21 additions & 22 deletions addons/laserpointer/functions/fnc_onDraw.sqf
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
// by commy2
/*
* Author: commy2
* Draw the visible laser beams of all cached units.
*
* Arguments:
* None
*
* Return Value:
* None
*
* Public: No
*/
#include "script_component.hpp"

private ["_brightness", "_cacheName", "_isIR", "_laser", "_laserID", "_weapon"];
private _isIR = currentVisionMode ACE_player;

_isIR = currentVisionMode ACE_player;
if (_isIR == 2) exitWith {};

_isIR = _isIR == 1;

_brightness = 2 - call EFUNC(common,ambientBrightness);
private _brightness = 2 - call EFUNC(common,ambientBrightness);

{
_weapon = currentWeapon _x;

_laser = switch (_weapon) do {
case (primaryWeapon _x): {
primaryWeaponItems _x select 1;
};
case (secondaryWeapon _x): {
secondaryWeaponItems _x select 1;
};
case (handgunWeapon _x): {
handgunItems _x select 1;
};
default {""};
};
private _weapon = currentWeapon _x;
private _laser = (_x weaponAccessories _weapon) select 1;

if (_laser != "") then {
_cacheName = format [QGVAR(laser_%1), _laser];
_laserID = missionNamespace getVariable [_cacheName, -1];
private _cacheName = format [QGVAR(laser_%1), _laser];
private _laserID = missionNamespace getVariable [_cacheName, -1];

if (missionNamespace getVariable [_cacheName, -1] == -1) then {
_laserID = getNumber (configFile >> "CfgWeapons" >> _laser >> "ACE_laserpointer");
missionNamespace setVariable [_cacheName, _laserID];
Expand All @@ -38,5 +37,5 @@ _brightness = 2 - call EFUNC(common,ambientBrightness);
[_x, 100, (_laserID == 2 || _isIR), _brightness] call FUNC(drawLaserpoint);
};
};

} forEach GVAR(nearUnits);
false
} count GVAR(nearUnits);
93 changes: 29 additions & 64 deletions addons/laserpointer/functions/fnc_switchLaserLightMode.sqf
Original file line number Diff line number Diff line change
@@ -1,89 +1,54 @@
// by commy2
/*
* Author: Commy2
* Switch between laser modes.
*
* Arguments:
* 0: Unit <OBJECT>
* 1: Weapon <STRING>
*
* Return Value:
* None
*
* Public: No
*/
#include "script_component.hpp"

private ["_unit", "_weapon", "_error"];
params ["_unit", "_weapon"];

_unit = _this select 0;
_weapon = _this select 1;

// 1 = primary, 2 = secondary, 3 = handgun
private "_currentWeaponType";
_currentWeaponType = 0;

private "_pointer";
_pointer = switch (_weapon) do {
case (primaryWeapon _unit): {
_currentWeaponType = 1;
primaryWeaponItems _unit select 1
};
case (secondaryWeapon _unit): {
_currentWeaponType = 2;
secondaryWeaponItems _unit select 1
};
case (handgunWeapon _unit): {
_currentWeaponType = 3;
handgunItems _unit select 1
};
default {""};
};
private _pointer = (_unit weaponAccessories _weapon) select 1;

if (_pointer == "") exitWith {};

private "_config";
_config = configFile >> "CfgWeapons" >> _pointer;
private _config = configFile >> "CfgWeapons" >> _pointer;

private "_nextPointer";
_nextPointer = getText (_config >> "ACE_nextModeClass");
private _nextPointer = getText (_config >> "ACE_nextModeClass");

if (_nextPointer == "") exitWith {};

//If system disabled, don't switch to a laser:
private "_nextPointerIsLaser";
_nextPointerIsLaser = getNumber (configFile >> "CfgWeapons" >> _nextPointer >> "ACE_laserpointer");
private _nextPointerIsLaser = getNumber (configFile >> "CfgWeapons" >> _nextPointer >> "ACE_laserpointer");
if ((!GVAR(enabled)) && {_nextPointerIsLaser == 1}) exitWith {};

// disable inheritance for this entry, because addons claim this as a base class for convenience
if !((_config >> "ACE_nextModeClass") in configProperties [_config, "true", false]) exitWith {};

private ["_description", "_picture"];
_unit addWeaponItem [_weapon, _nextPointer];

_description = getText (configFile >> "CfgWeapons" >> _nextPointer >> "ACE_modeDescription");
_picture = getText (configFile >> "CfgWeapons" >> _nextPointer >> "picture");
_error = false;
private _error = false;

switch (_currentWeaponType) do {
case 1: {
_unit removePrimaryWeaponItem _pointer;
_unit addPrimaryWeaponItem _nextPointer;
if (((primaryWeaponItems _unit) select 1) != _nextPointer) then {
ERROR("NextPointer not compatible");
_unit addPrimaryWeaponItem _pointer;
_error = true;
};
};
case 2: {
_unit removeSecondaryWeaponItem _pointer;
_unit addSecondaryWeaponItem _nextPointer;
if (((secondaryWeaponItems _unit) select 1) != _nextPointer) then {
ERROR("NextPointer not compatible");
_unit addSecondaryWeaponItem _pointer;
_error = true;
};
};
case 3: {
_unit removeHandgunItem _pointer;
_unit addHandgunItem _nextPointer;
if (((handgunItems _unit) select 1) != _nextPointer) then {
ERROR("NextPointer not compatible");
_unit addHandgunItem _pointer;
_error = true;
};
};
if ((_unit weaponAccessories _weapon) select 1 != _nextPointer) then {
ERROR("NextPointer not compatible");
_unit addWeaponItem [_weapon, _pointer];
_error = true;
};

if (!_error) then {
private _description = getText (configFile >> "CfgWeapons" >> _nextPointer >> "ACE_modeDescription");
private _picture = getText (configFile >> "CfgWeapons" >> _nextPointer >> "picture");

[_description, _picture] call EFUNC(common,displayTextPicture);
} else {
ACE_LOGERROR_3("Failed to add %1 to %2 - reverting to %3",_nextPointer,_weapon,_pointer);
ACE_LOGERROR_3("Failed to add %1 to %2 - reverting to %3",_nextPointer,_weapon,_pointer);
};

playSound "ACE_Sound_Click";