Jump to content
The Real Bunc

Dynamic camouflage - how best to run intermittently

Recommended Posts

I'm hoping I can get some advice and thoughts on the following;

 

I have developed a script which adjusts a soldiers camouflage coefficient dependent on a function which takes a number of environmental factors specifically rain, fog, darkness ( time of day)  and overcast level as parameters.

The basic script is working a treat and basically maps a function using these factors to give a camouflageCoef which I then set using setUnitTrait.

This works great and the script nicely adjusts the camouflague setting depending  on the combination of those factors. ( I'm happy to explain the function I'm using to do this and show the script later).

 

Currently I simply run my CamoScript.sqf from the soldiers ( or players ) init field and so of course it only runs once, at start up. This is better than nothing and it gives great camo when its dark and or rainy and or overcast but I would like it to be more dynamic and run either periodically or in response to changes such as new weather events.

 

So, my first question,

Any thoughts on what would be the most efficient and least demanding on resources way of having such a script run periodically either on a time basis such as every five minutes ( or more frequently perhaps)  ( preferably and or) or in response say to specific events such as a weather change? This would need to run throughout a mission or scenario so that the camouflage factor can be updated as conditions change.

 

Once I've sorted that bit I want to try to incorporate a test for uniform type so that for example a Ghillie suit would automatically adjust the camo factor etc to provide more camo. Uniform configs don't by any chance define a camo factor do they? I had a hunt and couldn't see anything.

 

Once the basics of efficiently repeatedly running the script are sorted I hope to also include tests for player being beside / within vegetation and for moon phase as well as long as I can do these with little performance hit.

 

 

Share this post


Link to post
Share on other sites

You typically run stuff inside a while loop (which checks eg "alive player") and use a sleep inside of it. Scripters often use those timed loops.  It basically only is important in terms of performance WHAT you do inside it, how intensive the check is... 

Outside, you spawn or execvm it and get a handle in return. And otherwise sleep() wouldnt be allowed ("scheduled environment") . With the handle, if desired you are able to terminate it.

 

Yes,events are a clean way if they exist. Look in wiki, I've got not many experience with them. 

 

I think in my weather scripts you can see how it can be done. E. g. look into tort_rain.sqf. I have enabled my scripted loops to be killed/restarted from outside. 

 

In some script I am checking an array of all units every 10s, and on new units I change their skill attributes. I'd say I do not see FPS change then.

If you think something is more intensive, like spawning units, do micro sleeps between any single action. 

Share this post


Link to post
Share on other sites

That's helpful.  Although initially I am running this on player I want to build it so it can be run on all combat units ( so that AI detection is camocoef influenced also for ai v ai interactions) and updated regularly or on changes. So I'm thinking I could store the current state of the factors I'm using then see if these have changed in the while condition and then only run the script if one of the factors or more has changed. That would mean having say six comparators in a condition and only if something has changed running the script itself once the while condition confirms a change in one of the parameters. Does that sound right?

Share this post


Link to post
Share on other sites

Erm well, sounds right, I think on complex stuff everybody is on his own 🙂 Of course a) as few comparisons as possible and b) use (native) functions as few as possible, put their output into variables and prefer to do comparisons with the variables then.

 

I can share with you my construction of creating looped scripts, which can then be later overridden or killed. So, if you re-call them, the prior instance will be killed. With a "stop" or "false" argument it is killed without being replaced by a new instance. This is called with an execVM and I prefix lots of my endless looped spawned code with it:

if (!isNil "tort_myScript") then {
   if (!scriptDone tort_myscript) then {
      terminate tort_myscript;
      waitUntil {scriptDone tort_myscript};
   };
};

if (tolower(str(_this#0)) find "stop" >= 0) exitWith {systemchat "tort_myScript has been terminated"};
if (tolower(str(_this#0)) find "false" >= 0) exitWith {systemchat "tort_myScript has been terminated"};

tort_myScript = _this spawn
{
   DAT ENDLESS LOOP CODE;
};
  • Like 1

Share this post


Link to post
Share on other sites

That's helpful again. I had decided Im also going to have stance influence camo - basically a prone stance should be more camouflaged than a crouch which should be more than stand. So I think im going to test for change of stance in the loop condition that way the player doesn't have to do anything at all.  A second approach Im thinking about is using an an addaction. This way the script would only run at player instruction - "Check camo" or something like that. The second has the benefit of not having a loop running but requires intervention by player. Ive got the script already working so sometime today/tomorrow Ill implement these approaches and post some code for people to try if they are interested.

  • Like 1

Share this post


Link to post
Share on other sites

Edit: I made changes to this so ive reposted it as a specific script thread on its own now.

 

Ok Here is my first working version of my DynamicCamo script;

What does it do?

It takes account of players stance and environmental factors to adjust the unitTrait CamouflageCoef.

A low camouflageCoef gives greater camouflage to the unit and makes it harder for ai to detect you.

Units appear to be created with camouflageCoef set t0 1 as default. This script dynamically resets this depending on 

a number of factors.

What influences the Dynamic Camo?

Player stance, Fog, Rain, Overcast and day/night.

Can the effect be adjusted

User can adjust impact of these factors by a) adjusting weights for things like stance and day/night and b) adjust ranges of the other factors and the actual result itself.

In short the user can identify exactly the balance of influence these various factors should have.

Installation

Copy script as camoscript.sqf to mission folder. 

In player init  put   camo = [this] spawn compile preprocessFile "camoscript.sqf";  

IMPORTANT - the script monitors keys associated with stand/crouch and prone through the use of User1.

Go to the menu  - controls>custom controls > User1  and ensure you set both the prone key AND the crouch stand key  for User1.

Use

You don't need to do anything other than occasionally crouch, stand up , go prone and watch the different camouflageCoef that is being set for you.

Try at night V day, with and without rain, fog etc and watch result.

NOTE - as far as I can tell it takes quite low values of camouflageCoef to make detection quite hard so this version comes with the factors and the result 

scaled towards that direction.

Further Development

I haven't fully tested ai to ai use of camouflageCoef on detection ability. Initial results indicate they tend to give themselves away anyway because of movements and movement detection ( and noise?)

But I want to implement the possibility of running this on your whole squad, or all soldiers on a side etc.  I also want to implement the same approach to adjust audibleCoef as sthis according to the documentation can make a unit harder to hear. I dont think it will take much to integrate this in probably using much the same factors.

Version

This is my annotated but fully working test version. I trust the annotations make the script self explanatory.

// DynamicCamo Script by Bunc version 1
// Free to use but credit me please.
// this script adjusts unitTrait camouflagecoef based on environmental factors and stance.
// future - include adjustment for specific types of uniform.

// Environmental factors are
// fog.  (0 is no fog 1 is full fog.)
// Camo - factor for uniform. 0 is high uniform camo. This is set to constant 1 at the moment.
// rain - rain density. 1 full rain 0 no rain.
// d -    darkness. Stepwise function to weight for night, dawn, midday, dusk. currently only night v day.
// predetermined weight value is assigned for each for each.
// uses sunrisesunset function. range 0 dark to 1 light.
// overcast - 0 clear skies to 1 full overcast.
// NB values may be subject to linear rescaling to give better weights where required.
// this also ensures we can avoid extreme value 0 in any factor otherwise whole result woudl be thrown to zero.
// NB camouflageCoef values in unit trait appear to have a fairly subtly effect
// until values are very small. rescaling if values is partly to
// ensure calculation produces figure in appropriate range.
// NB units appear to all start with camouflageCoeff set to 1 as far as I can tell.
// we want combination of these factors where they are all present to produce a small ccoef
// approaching zero and perhaps not too near 1 when there are no factors in play.
// we can also apply linear rescaling to the output if further adjustment is required.

// basic formula  -
// newcamcoef is the value that will be set as unit trait camouflageCoef
// newcamcoef = sqrt(  (1-fog) * camo * (1-rain) * darkvalue * (1-overcast)*stancefactor
// NB 0 for a factor would set camocoef to zero and we might also want some 
// effect even when a factor is 1 so we can scale these inputs to a smaller range eg 0.01 to 0.99
// the specific scaling needs to be tested for best effect.

// We start the main loop which continues while player alive
// then waituntil player presses User1 assigned key.

while {alive player} 
          do 
          { 
           waitUntil {inputAction "User1" > 0};
sleep 1;

//get factors and scale them.
// get fog and rescale result to desired range.
_foglevel = linearconversion [0,1,fog,0,0.9,true];

// Camo will be set constant for now. 0 ( or a very low number) will give full uniform camo.
// this section will expand to accomodate test of uniform and assign appropriate value.
// we set it to no effect atm.
_camo = 1;

// get rain level and scale it.
_rainLevel = linearconversion [0,1,rain,0,0.9,true];

// DARK LEVEL FACTOR
// 1 is full light 0 is dark.
// initialise _darklevel before if statement
// Later > change if statement switch to handle dusk and dawn also?

_sunriseSunsetTime = date call BIS_fnc_sunriseSunsetTime;
_darkLevel = .5;
_sunrise = _sunriseSunsetTime select 0;
_sunset = _sunriseSunsetTime select 1;

if (daytime >_sunrise && daytime < _sunset) then {_darkLevel = 0.5;}
 else { _darkLevel = .01;};

// get overcast level and scale it.
_overcastLevel = linearconversion [0,1,overcast,0.1,0.9,true];

// get stance and assign appropriate values
_stanceresult = stance player;
_st = 1;

switch (_stanceresult) do
       {
       case "STAND" : {_st = 1; hint "st";};
       case "CROUCH" : {_st = 0.5; hint "";};
       case "PRONE" : {_st = 0.1; hint "";};
       case "SWIMMING": {_st = 0.5;};
       case "UNDEFINED" : {_st = 1;};
        };


// run the calculation
_newCamCoef = sqrt (
                  (1-_foglevel) * _camo * (1 - _rainLevel) * _darkLevel * (1 - _overcastLevel) * _st
                  );

// scale our new camcoef to whatever range we desire
// in this case we set max to 0.7 so script always has impact on camcoef.

_newCamCoefScaled = linearconversion [0,1,_newCamCoef,0,0.7,true];

// set our calculated value as the UnitTrait.
_this select 0 setUnitTrait ["camouflageCoef", _newcamCoefscaled];

// tests
       _testit = _this select 0 getUnitTrait "camouflageCoef";
// show calculated result for tuning
      hint str _testit;
// sleep for a bit so that bobbing up and down in combat doesnt cause
// to many runs on the script. Also allows playyer to go prone,
// get extra camo then bob up again retaing prone camo for a short while?

sleep 2;

};

 

 

If anything isn't clear please ask. I hope this is of some interest.

 

For anyone who wants to muck about with how to weight the factors using a graphical interface I have a link to this setup 

in a desmos graphical calculator which you can muck about with here - https://www.desmos.com/calculator/6yzp5wfemp

 

UPDATE: I found that the script above was throwing an error after a while which Ive now sorted. Ive also tinkered with a couple of other bits. Rather than now post further on this thread which started with my request for an aspect of the script Im now going to start a new thread for the script itself. Any comments should be made there please. 

 

Share this post


Link to post
Share on other sites

Yes at some point hopefully fairly soon. I was having some discussions with Gunter and he thinks he can maybe get me some support and advice to do that. It wont be next week but on the other hand I'm going to have plenty of time at home to attend to it I guess. Just finishing something else before I turn back to this. Have you used it xrayfg89 ? What did you think? Any thoughts on it? There is a working mission ( I slapped together) in my workshop if you haven't seen it and a thread on here with the latest version ( 1.5 I think?)

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

×