Jump to content
Sign in to follow this  
Skelt

Dynamic View Distance Script

Recommended Posts

Having a bit of trouble getting this to work. Is it missing a config or something? I was trying to call it from the init.sqf.

<A working mp compatible script version of this would be pretty sexy> I find myself to be rather anti-pbo.

Thanks.

dynamic_viewdistance.sqf

// Script to smoothly scale the viewdistance according to fps and whether scope is used.
// Should help stabilise FPS too.
// TPW 20110122

// Variables
tpw_vmin = _this select 1; // minimum draw distance
tpw_vmax = _this select 2; // maximum draw distance
tpw_fmin = _this select 3; // max framerate
tpw_fmax = _this select 4; // min framerate
tpw_avtime = _this select 5; // how often to adjust viewdistance (sec) ~10
_zoomed = "n"; // start off not zoomed in
_zf = 1.5; // zoom factor to increase viewdistance by when scope used 

// Adjust view distance per map
if (worldname in ["zargabad","chernarus"]) then {tpw_vmin = (0.75 * tpw_vmin)}; // allow for worse performance on these maps
if (worldname in ["thirsk","torabora","isoladicapraia","isladuala","utes","mb_manaisland","shapur_baf","desert_e","provinggrounds_pmc"]) then {tpw_vmin = (1.5 * tpw_vmin);tpw_vmax = (1.5 * tpw_vmax)}; // allow for better performance on these maps
tpw_steps = (tpw_vmax-tpw_vmin)/(tpw_fmax-tpw_fmin);

// Background process to determine average fps over tpw_avtime seconds and adjust view distance accordingly
tpw_lastfps = tpw_fmin;
[] spawn {while {alive player} do {
_sum = 0; for "_c" from 1 to tpw_avtime do {_sum = (_sum + diag_fps);sleep 1}; tpw_avfps = floor (_sum/tpw_avtime); // Average fps over tpw_avtime sec
_buffps = (tpw_avfps + tpw_lastfps)/2; // should stop huge oscillations in viewdistance
_mfactor = (_buffps - tpw_fmin); if (_mfactor < 0) then {_mfactor = 0};
tpw_vdist = floor (tpw_vmin + (tpw_steps*_mfactor)); // will linearly scale viewdistance from tpw_fmin = tpw_vmin to tpw_fmax = tpw_vmax 
tpw_lastfps = tpw_avfps;
};
};

// Initial viewdistance 
_oldvdist = tpw_vmin;
setviewdistance tpw_vmin;
sleep (tpw_avtime * 1.5 ); // free up CPU for other init stuff and wait for 1st run through background process

// Adjust viewdistance when zooming/unzooming scopes
while {alive player } do {
_vdist = tpw_vdist;
if ((cameraView == "GUNNER") and (_zoomed == "y")) then {_vdist = (_vdist * _zf)};
if ((cameraView == "GUNNER") and (_zoomed == "n")) then {_vdist = (_vdist * _zf); _zoomed="y"};
if ((cameraView == "INTERNAL") and (_zoomed == "y")) then {_zoomed="n"};

// Smoothly change viewdistance - if it has changed since last time
if (_vdist != _oldvdist) then 
{_inc = ((_vdist - _oldvdist)/10); // setviewdistance increments 1/10
_vdist = _oldvdist;
for "_i" from 1 to 10 do {_vdist = (_vdist + _inc); setviewdistance _vdist};
};
_oldvdist = _vdist;

//Debugging output
hintsilent format ["av fps:%1, vd:%2",tpw_avfps,_vdist];

sleep 1;
};

Share this post


Link to post
Share on other sites

Just had a quick look.... and can see something right away.

Shouldn't it be like this?... instead of what you have.

tpw_vmin = _this select [color="Red"]0[/color]; // minimum draw distance
tpw_vmax = _this select [color="Red"]1[/color]; // maximum draw distance
tpw_fmin = _this select [color="Red"]2[/color]; // max framerate
tpw_fmax = _this select [color="Red"]3[/color]; // min framerate
tpw_avtime = _this select [color="Red"]4[/color]; // how often to adjust viewdistance (sec) ~10

There might be other errors of course!

Share this post


Link to post
Share on other sites

Thanks for the reply, yeah my issue is with the array too.. I don't see where any of those variables are defined (minimum draw / max draw ect) so I'm thinking this script requires a config.cpp or something where those values are determined. So I'm thinking this is part of a pbo =/.

But I only know the basics of scripting so I'm unsure how to complete this to make it work. Only reason I'd like a script version rather then the DVD (Dynamic View Distance Addon) is to save the clients an added download - if we can script this functionality into the map, we should do so.

Thanks again for the reply.

-Bones

Share this post


Link to post
Share on other sites

Hi Skelt et al

Sorry I haven't had a lot of time or enthusiasm for scripting and Arma2 in general over the last few months - separating from wife will do that to a bloke.

It's been quite a while since I worked on the script and my sqf is a bit rusty! Please PM me and I can sort you out with a script version rather than a PBO version.

Share this post


Link to post
Share on other sites

Sounds great pm sent, and to anyone else whom reads this, we will keep this post open for discussion on this topic.

Share this post


Link to post
Share on other sites

I have to laugh, when I originally came up with this script I was bombarded with requests to make a PBO version!

So I dusted off the SQF cobwebs and had a look. For starters, you need the latest version of the script, see below.

Save the script wherever you want (eg your_arma_directory\scripts\dynamic_view_distance.sqf)

Put this in the init field of your player

nul = [this, 200,1800,4000,20,40,5,1.5,1.5,1] execVM "scripts\dynamic_view_distance.sqf";

An explanation of the numbers, which will be passed to the script:

200; // The minimum viewdistance which will be used

1800; // The preferred viewdistance which will be used. Viewdistance will go higher than this if the fps is higher than preferred.

4000; // Viewdistance will never go higher than this.

20; // View distance will be lowered to minimum for frame rates any lower than this

40; // A realistic setting for a preferred framerate you know your system can handle

5; // Time over which the framerate is averaged. In seconds. Must be > 1

1.5; // The factor to increase the viewdistance by when using scopes/binoculars. Set to 1 for no increase

1.5; // The factor to increase the viewdistance by when flying aircraft. Set to 1 for no increase

0; // 0 - no debugging info. 1 - Displays average framerate and viewdistance. Useful for establishing optimum settings for your system.

Feel free to change these to your taste!

// Script to smoothly scale the viewdistance according to fps and whether scope is used or in aircraft.
// Should help stabilise FPS too.
// Huge thanks to Das Attorney for help with the addon version of this script
// Thanks to TommyC81 for SMA help
// v1.04 TPW 20110205

// Variables
dvd_vmin = _this select 1; // minimum draw distance
dvd_vpref = _this select 2; // preferred draw distance
dvd_vmax = _this select 3; // maximum draw distance
dvd_fmin = _this select 4; // min framerate
dvd_fpref = _this select 5; // preferred framerate
dvd_avtime = _this select 6; // how often to adjust viewdistance (sec) ~10
dvd_zf = _this select 7; // zoom factor to increase viewdistance by when scope used 
dvd_azf = _this select 8; // zoom factor to increase viewdistance by when in aircraft 
dvd_debug = _this select 9; // debugging output 
_zoomed = "n"; // start off not zoomed in
dvd = 1;

//Set up
_airmax = dvd_vmax * dvd_azf;
hintsilent "Dynamic View Distance 1.04 Active";
dvd_avtime = floor dvd_avtime; 
if (dvd_avtime < 1) then {dvd_avtime = 1};
dvd_steps = (dvd_vpref-dvd_vmin)/(dvd_fpref-dvd_fmin);
sma_array = [dvd_fpref,dvd_fpref,dvd_fpref,dvd_fpref,dvd_fpref];
dvd_avfps = dvd_fpref;
dvd_fprefmin = dvd_fpref - dvd_fpref/10;
dvd_fprefmax = dvd_fpref + dvd_fpref/10;

// Background process to determine simple moving average (SMA) fps over 5 intervals and adjust view distance accordingly
dvd_lastfps = dvd_fmin;
[] spawn {while {dvd > 0} do {
_fps = diag_fps; 
dvd_avfps = dvd_avfps - ((sma_array select 4) / 5) + (_fps / 5);
sma_array = [_fps] + sma_array; sma_array resize 5;
_mfactor = dvd_avfps - dvd_fmin; if (_mfactor < 0) then {_mfactor = 0};
dvd_vdist = floor (dvd_vmin + (dvd_steps*_mfactor)); 
if (dvd_vdist > dvd_vmax) then {dvd_vdist = dvd_vmax};
sleep dvd_avtime;
};
};

// Initial viewdistance 
_oldvdist = dvd_vpref;
setviewdistance dvd_vpref;

// Adjust viewdistance when zooming/unzooming scopes
while {dvd > 0 } do {
_vdist = dvd_vdist;
if ((dvd_avfps > dvd_fprefmin) && (dvd_avfps < dvd_fprefmax)) then {_vdist = dvd_vpref};
if ((cameraView == "GUNNER") and (_zoomed == "y")) then {_vdist = (_vdist * dvd_zf); if (_vdist > dvd_vmax) then {_vdist = dvd_vmax};};
if ((cameraView == "GUNNER") and (_zoomed == "n")) then {_vdist = (_vdist * dvd_zf); if (_vdist > dvd_vmax) then {_vdist = dvd_vmax};_zoomed="y"};
if ((cameraView == "INTERNAL") and (_zoomed == "y")) then {_zoomed="n"};

if (vehicle player iskindof "air") then {_vdist = (_vdist * dvd_azf); if (_vdist > _airmax) then {_vdist = _airmax};};

// Smoothly change viewdistance - if it has changed since last time
if (_vdist != _oldvdist) then 
{_inc = ((_vdist - _oldvdist)/10); // setviewdistance increments 1/10
_vdist = _oldvdist;
for "_i" from 1 to 10 do {_vdist = (_vdist + _inc); setviewdistance _vdist};
};
_oldvdist = _vdist;

//Debugging output
if (dvd_debug == 1) then {hintsilent format ["av fps:%1, vd:%2",floor dvd_avfps,floor _vdist];};

sleep 1;
};

Good luck!

  • Thanks 1

Share this post


Link to post
Share on other sites

Scripts seem to scare some people, myself on the other hand, embrace them.

This is awesome, thanks.

ED: Tested::: Works wonderfully! FPS have never been better. (The game rarely runs choppy for me maxed at 100% 3d Resolution - and I have noticed an improvement over that!!)

Edited by Skelt

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×