Fulqrum Publishing Home   |   Register   |   Today Posts   |   Members   |   UserCP   |   Calendar   |   Search   |   FAQ

Go Back   Official Fulqrum Publishing forum > Fulqrum Publishing > IL-2 Sturmovik: Cliffs of Dover > FMB, Mission & Campaign builder Discussions

Reply
 
Thread Tools Display Modes
  #1  
Old 12-08-2011, 10:24 AM
Octocat Octocat is offline
Approved Member
 
Join Date: Dec 2011
Posts: 22
Default

Here the main idea: do not create an object of Random anew for each use. One would be enough.
Reply With Quote
  #2  
Old 12-08-2011, 12:16 PM
salmo salmo is offline
Approved Member
 
Join Date: Mar 2011
Posts: 632
Default

Quote:
Originally Posted by Octocat View Post
Here the main idea: do not create an object of Random anew for each use. One would be enough.
So are you saying that the NextInt routine would be better if the highlighted line were used?

code removed by author
__________________
When one engine fails on a two engine bomber, you will always have enough power left to get to the scene of the crash.

Get the latest COD Team Fusion patch info HERE

Last edited by salmo; 10-18-2012 at 09:32 AM.
Reply With Quote
  #3  
Old 12-08-2011, 12:37 PM
Octocat Octocat is offline
Approved Member
 
Join Date: Dec 2011
Posts: 22
Default

No, code "return Random(result).Next(min, max);" is syntactic error for C# compiler, because keyword new required.

I saying, you can remove NextInt function completely. Then create single instance of Random class in the RandomizeWeather function, and then use it as many times as you like, as in my example above.
Reply With Quote
  #4  
Old 12-08-2011, 12:51 PM
Octocat Octocat is offline
Approved Member
 
Join Date: Dec 2011
Posts: 22
Default

Quote:
The random method certainly uses less resources...
Random in not the method, but class.

Line below creates instance of Random class, and assigns the value to variable named random.

var random = new Random(Environment.TickCount);

Now, we can call method "Next" any number of times:

int randomNumber = random.Next(min, max);
Reply With Quote
  #5  
Old 12-08-2011, 01:23 PM
salmo salmo is offline
Approved Member
 
Join Date: Mar 2011
Posts: 632
Default

Thankyou Octocat. I known how random.next works, but I chose to go with the RNGCryptoServiceProvider rather than the Random class because I feel it gives me "better" random numbers. Anyone using the script is welcome to replace the NextInt routine with the random class as suggested.
__________________
When one engine fails on a two engine bomber, you will always have enough power left to get to the scene of the crash.

Get the latest COD Team Fusion patch info HERE
Reply With Quote
  #6  
Old 12-08-2011, 02:28 PM
Octocat Octocat is offline
Approved Member
 
Join Date: Dec 2011
Posts: 22
Default

My variant with RNGCryptoServiceProvider

- Code slightly reduced and refactored
- Added minutes randomization
- Fixed maxValue problem (random.Next always return value by 1 less then the specified maximum)

Code:
using System;
using maddox.game;
using System.Security.Cryptography;

public class Mission : AMission
{
  public override void OnBattleInit()
  {
    RandomiseMission();
  }

  internal ISectionFile RandomiseMission()
  {
    // initialize a new instance of the Random class
    var provider = new RNGCryptoServiceProvider();
    var buffer = new byte[4];
    provider.GetBytes(buffer);
    var random = new Random(BitConverter.ToInt32(buffer, 0));

    //time of day
    bool RandomiseTime = true;
    int minTime = 6;    // 6am
    int maxTime = 17;   // 5pm
    // WeatherIndex
    bool RandomiseWeatherIndex = true;
    int minWeatherIndex = 0;    // clear
    int maxWeatherIndex = 2;    // medium clouds
    // cloud height
    bool RandomiseClouds = true;
    int minClouds = 500;    // 500m
    int maxClouds = 1500;   // 1500m
    // BreezeActivity
    bool RandomiseBreezeActivity = true;
    int minBreezeActivity = 0;  // zero breeze
    int maxBreezeActivity = 10; // max breeze
    // ThermalActivity
    bool RandomiseThermalActivity = true;
    int minThermalActivity = 0;     // zero thermals
    int maxThermalActivity = 10;    // max thermals

    ISectionFile secfile = GamePlay.gpCreateSectionFile();
    
    // time of day
    if (RandomiseTime == true)
    {
      string value = (random.Next(minTime * 100, maxTime * 100) / 100f).ToString();
      secfile.add("MAIN", "TIME", value);
    }

    // WeatherIndex 
    if (RandomiseWeatherIndex == true)
    {
      string value = random.Next(minWeatherIndex, maxWeatherIndex + 1).ToString();
      secfile.add("MAIN", "WeatherIndex", value);
    }

    // clouds
    if (RandomiseClouds == true)
    {
      string value = random.Next(minClouds, maxClouds + 1).ToString();
      secfile.add("MAIN", "CloudsHeight", value);
    }

    // minBreezeActivity
    if (RandomiseBreezeActivity == true)
    {
      string value = random.Next(minBreezeActivity, maxBreezeActivity + 1).ToString();
      secfile.add("MAIN", "BreezeActivity", value);
    }

    // ThermalActivity 
    if (RandomiseThermalActivity == true)
    {
      string value = random.Next(minThermalActivity, maxThermalActivity + 1).ToString();
      secfile.add("MAIN", "ThermalActivity", value);
    }

    // This post loads the section file secfile into the current battle.
    // It's also possible to save the section file secfile first by secfile.save("filename") and then
    // call GamePlay.gpPostMissionLoad("filename"); if you want to save the randomised file
    // on your hard disk.
    GamePlay.gpPostMissionLoad(secfile);

    return secfile;
  }
}

Last edited by Octocat; 12-08-2011 at 03:28 PM.
Reply With Quote
  #7  
Old 12-09-2011, 12:12 AM
salmo salmo is offline
Approved Member
 
Join Date: Mar 2011
Posts: 632
Default

OK, I see what you've done. One declaration of var provider = new RNGCryptoServiceProvider(); Much better Thankyou.

1. I've moved the Random Class declaration outside the randomisemission routine so it can also be used in the randomisation of mission selection etc. (see below). But this construct produces an error CS1519: Invalid token '(' in class, struct, or interface member declaration on the provider.GetBytes(buffer); line. pehaps you can help?

code removed by author

2. Multiplayer implimentation

This script randomised the [MAIN] section of the mission file OK in single-player mode, but fails in multi-player(server) mode. I have in mind some pseudo code such as: Randomise [MAIN] on server battle start; save [MAIN] section file on the server side; as players connect reload the saved [MAIN] section file from server side so they get the same mission parameters in multi-player mode.
__________________
When one engine fails on a two engine bomber, you will always have enough power left to get to the scene of the crash.

Get the latest COD Team Fusion patch info HERE

Last edited by salmo; 10-18-2012 at 09:32 AM.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:40 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright © 2007 Fulqrum Publishing. All rights reserved.