Advertisement
Guest User

Untitled

a guest
Oct 26th, 2013
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.54 KB | None | 0 0
  1. /*
  2. General init for the tracer function
  3. */
  4. hyp_var_tracer_tracedUnits = [];
  5.  
  6. addMissionEventHandler ["Draw3D", {
  7.     {
  8.         private["_unit"];
  9.         _unit = _x;
  10.         {
  11.             private["_positions","_color","_muzzleVelocity","_velocity"];
  12.             _positions = _unit getVariable [format["hyp_var_tracer_projectile_%1", _x], []];
  13.             _color     = _unit getVariable ["hyp_var_tracer_color", [1,0,0,1]];
  14.             _muzzleVelocity = _positions select 0 select 1;
  15.             _velocity = 0;
  16.            
  17.             for "_i" from 0 to (count _positions) - 2 do
  18.             {
  19.                 _velocity = (_positions select _i) select 1;
  20.            
  21.                 if (_velocity / _muzzleVelocity > .75) then {_color = [1,0,0,1];};
  22.                 if (_velocity / _muzzleVelocity <= .75) then {_color = [.5,.5,0,1];};
  23.                 if (_velocity / _muzzleVelocity <= .50) then {_color = [0,1,0,1];};
  24.                 if (_velocity / _muzzleVelocity <= .25) then {_color = [0,0,1,1];};
  25.                 if (_velocity / _muzzleVelocity <= .1) then {_color = [1,1,1,1];};
  26.                
  27.                 drawLine3D [_positions select _i select 0, _positions select (_i + 1) select 0, _color];
  28.             };
  29.         } forEach ( _unit getVariable["hyp_var_tracer_activeIndexes", []] );
  30.     } forEach hyp_var_tracer_tracedUnits;
  31. }];
  32.  
  33. //Adding of the option to manually clear lines.  If you don't want the addaction, simply remove these 4 lines
  34. (player) addAction["Clear Lines", {
  35.     {
  36.         [_x] call hyp_fnc_traceFireClear;
  37.     } forEach hyp_var_tracer_tracedUnits;
  38. }]; //Clears the lines of all drawn projectiles
  39.  
  40.  
  41. /*
  42. Syntax:
  43.     [_unit, _color, _lifetime, _interval, _maxDistance, _maxDuration] call hyp_fnc_traceFire;
  44. Params:
  45.     _unit:        Either a vehicle or unit.  (Default player)
  46.     _color:       Color array for the lines. (Default [1,0,0,1])
  47.     _lifetime:    Duration after landing to keep the line drawn.  (Default -1, for indefinite duration)
  48.     _interval:    Number of frames to wait between recording locations (Default 0 for record on every frame)
  49.     _maxDistance: Maximum Distance from origin point to record positions (Default -1, for no max)
  50.     _maxDuration: Maximum Duration from time of firing to record posiitons (Default -1, for no max)
  51. Return Value:
  52.     Scalar: The ID of the "fired" EventHandler that was added.
  53. */
  54.  
  55. hyp_fnc_traceFire = {
  56.     private["_this","_unit","_color","_lifetime","_interval","_maxDistance","_maxDuration","_eventHandle"];
  57.     _unit        = [_this, 0, player, [objNull]] call BIS_fnc_param;
  58.     _color       = [_this, 1, [1,0,0,1], [[]], [4]] call BIS_fnc_param;
  59.     _lifetime    = [_this, 2, -1, [0]] call BIS_fnc_param;
  60.     _interval    = [_this, 3, 0, [0]] call BIS_fnc_param;
  61.     _maxDistance = [_this, 4, -1, [0]] call BIS_fnc_param;
  62.     _maxDuration = [_this, 5, -1, [0]] call BIS_fnc_param;
  63.  
  64.     //Ensure that lifetime is at least as long as the maxDuration:
  65.     //if (_lifetime < _maxDuration) then {_lifetime = _maxDuration;};
  66.  
  67.     _unit setVariable ["hyp_var_tracer_color", _color];
  68.     _unit setVariable ["hyp_var_tracer_lifetime", _lifetime];
  69.     _unit setVariable ["hyp_var_tracer_interval", _interval];
  70.     _unit setVariable ["hyp_var_tracer_maxDistance", _maxDistance];
  71.     _unit setVariable ["hyp_var_tracer_maxDuration", _maxDuration];
  72.     _unit setVariable ["hyp_var_tracer_currentIndex", 0];
  73.     _unit setVariable ["hyp_var_tracer_activeIndexes", []];
  74.  
  75.     _eventHandle = _unit addEventHandler ["fired", {
  76.         [_this, (position(_this select 6)),(velocity (_this select 6)) distance [0,0,0]] spawn hyp_fnc_traceFireEvent;
  77.     }];
  78.     _unit setVariable ["hyp_var_tracer_eventHandle", _eventHandle];
  79.     hyp_var_tracer_tracedUnits set [count hyp_var_tracer_tracedUnits, _unit];
  80. };  
  81.  
  82. hyp_fnc_traceFireEvent = {
  83.     private["_this","_params","_initialPos","_unit","_projectile","_color","_lifetime","_interval","_maxDistance",
  84.             "_maxDuration","_startTime","_skippedFrames","_positions","_projIndex","_activeIndexes","_mVel"];
  85.     _params        = _this select 0;
  86.     _initialPos    = _this select 1;
  87.     _unit          = _params select 0;
  88.     _projectile    = _params select 6;
  89.     _color         = _unit getVariable "hyp_var_tracer_color";
  90.     _lifetime      = _unit getVariable "hyp_var_tracer_lifetime";
  91.     _interval      = _unit getVariable "hyp_var_tracer_interval";
  92.     _maxDistance   = _unit getVariable "hyp_var_tracer_maxDistance";
  93.     _maxDuration   = _unit getVariable "hyp_var_tracer_maxDuration";
  94.     _startTime     = diag_tickTime;
  95.     _mVel          = _this select 2;
  96.     _skippedFrames = _interval; //Number of frames since last full operation.  Starts at interval value to record first position
  97.     _positions     = [[_initialPos,_mVel]];
  98.     _projIndex     = -1;
  99.     _activeIndexes = [];
  100.  
  101.     _projIndex     = _unit getVariable "hyp_var_tracer_currentIndex"; //Get the index to assign to the bullet
  102.     _unit setVariable ["hyp_var_tracer_currentIndex", _projIndex + 1]; //Increment index for next bullet
  103.  
  104.     //Initialize final array into which all positions for the current projectile will be stored...
  105.     _unit setVariable [format["hyp_var_tracer_projectile_%1", _projIndex], _positions];
  106.     //...Then update the activeIndexes to indicate that the projectile is active
  107.     _activeIndexes = _unit getVariable "hyp_var_tracer_activeIndexes";
  108.     _activeIndexes set [count _activeIndexes, _projIndex];
  109.     _unit setVariable ["hyp_var_tracer_activeIndexes", _activeIndexes];
  110.     _activeIndexes = nil; //Completely nil this variable just as a safety measure, as the data it holds may be outdated now
  111.  
  112.     //Loop to run as long as the projectile's line is being updated
  113.     waitUntil {
  114.    
  115.         //First, handle skipping frames on an interval
  116.         if (_interval != 0 && _skippedFrames < _interval) exitWith {_skippedFrames = _skippedFrames + 1; false}; //Check and handle if frame should be skipped
  117.         if (_interval != 0) then {_skippedFrames = 0;}; //Reset skipped frame counter on recording a frame
  118.         //Next, check if the bullet still exists
  119.         if (isNull _projectile) exitWith {true};
  120.         //Finally, handle the duration and distance checks
  121.         if (_maxDuration != -1 && ((diag_tickTime - _startTime) >= _maxDuration)) exitWith {true}; //Break loop if duration for tracking has been exceeded
  122.         if (_maxDistance != -1 && ((_initialPos distance _projectile) >= _maxDistance)) exitWith {true}; //Break loop if distance for tracking has been exceeded
  123.    
  124.         //Now, checks have all been run, so let's do the actual bullet tracking stuff
  125.         _positions set [count _positions, [position _projectile,(velocity _projectile) distance [0,0,0]]];
  126.         _unit setVariable [format["hyp_var_tracer_projectile_%1", _projIndex], _positions];
  127.         !alive _projectile
  128.     };
  129.  
  130.     //Now, if a lifetime is specified, wait until it has elapsed, then delete all data for that projectile
  131.     if (_lifetime != -1) then {
  132.         waitUntil {(diag_tickTime - _startTime) >= _lifetime};
  133.         //Remove the current projectile's index from the activeIndexes...
  134.         _activeIndexes = _unit getVariable "hyp_var_tracer_activeIndexes";
  135.         _activeIndexes = _activeIndexes - [_projIndex];
  136.         _unit setVariable ["hyp_var_tracer_activeIndexes", _activeIndexes];
  137.         //... Then delete the data for the projectile itself
  138.         _unit setVariable [format["hyp_var_tracer_projectile_%1", _projIndex], nil]; //Delete the projectile's data
  139.     };
  140. };
  141.  
  142. //Clears all lines created by a given unit manually
  143. hyp_fnc_traceFireClear = {
  144.     private["_this","_unit"];
  145.     _unit = _this select 0;
  146.     {
  147.         _unit setVariable [format["hyp_var_tracer_projectile_%1", _x], nil];
  148.     } forEach (_unit getVariable ["hyp_var_tracer_activeIndexes", []]);
  149.     _unit setVariable ["hyp_var_tracer_activeIndexes", []];
  150. };
  151.  
  152. //Completely removes this script from a unit
  153. hyp_fnc_traceFireRemove = {
  154.     private["_this","_unit"];
  155.     _unit = _this select 0;
  156.  
  157.     _unit removeEventHandler ["fired", (_unit getVariable ["hyp_var_tracer_eventHandle", 0])];
  158.     {
  159.         _unit setVariable [format["hyp_var_tracer_projectile_%1", _x], nil];
  160.     } forEach (_unit getVariable ["hyp_var_tracer_activeIndexes", []]);
  161.     _unit setVariable ["hyp_var_tracer_color", nil];
  162.     _unit setVariable ["hyp_var_tracer_lifetime", nil];
  163.     _unit setVariable ["hyp_var_tracer_interval", nil];
  164.     _unit setVariable ["hyp_var_tracer_maxDistance", nil];
  165.     _unit setVariable ["hyp_var_tracer_maxDuration", nil];
  166.     _unit setVariable ["hyp_var_tracer_currentIndex", nil];
  167.     _unit setVariable ["hyp_var_tracer_activeIndexes", []];
  168.     _unit setVariable ["hyp_var_tracer_eventHandle", nil];
  169. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement