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-02-2011, 05:11 AM
salmo salmo is offline
Approved Member
 
Join Date: Mar 2011
Posts: 632
Default Randomising missions

I'm trying the write a script to randomise mission parameters in the "MAIN" section of the mission file; time, weather, cloud height, wind etc. Here's what I have, but it does not work, can anyone help out please?

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:31 AM.
Reply With Quote
  #2  
Old 12-02-2011, 06:09 AM
41Sqn_Banks 41Sqn_Banks is offline
Approved Member
 
Join Date: Oct 2007
Posts: 644
Default

Hi, first of all the "GamePlay.gpLoadSectionFile(fileName);" doesn't load the mission file into the game engine. It only creates a ISectionFile object that allows to read/write a mission file with the script and do some changes (like you are already do).

To post load a mission file into the running battle you need to call "GamePlay.gpPostMissionLoad(fileName);"

However I don't think it's possible to change the MAIN parameters by a mission file that is post loaded into a running battle.

Anyhow your script should look something like this:

Code:
using System;
using System.Collections.Generic;
using maddox.game;
using maddox.game.world;
using maddox.GP;
using System.Security.Cryptography;


public class Mission : AMission
{

public override void OnBattleInit()
{
      RandomiseMission();
}

internal ISectionFile RandomiseMission()
{

    //time of day
    bool RandomiseTime = true;
    int minTime = 6;
    int maxTime = 17;
    // WeatherIndex
    bool RandomiseWeatherIndex = true;
    int minWeatherIndex = 0;
    int maxWeatherIndex = 1;
    // cloud height
    bool RandomiseClouds = true;
    int minClouds = 1000;
    int maxClouds = 2000;
    // BreezeActivity
    bool RandomiseBreezeActivity = true;
    int minBreezeActivity = 0;
    int maxBreezeActivity = 3;
    // ThermalActivity
    bool RandomiseThermalActivity = true;
    int minThermalActivity = 0;
    int maxThermalActivity = 2;

    ISectionFile f = GamePlay.gpCreateSectionFile();
    string sect;
    string key;
    string value;
    // time of day
    if (RandomiseTime == true)
    {
        sect = "MAIN";
        key = "TIME";
        value = NextInt(minTime, maxTime).ToString();
        f.add(sect, key, value);
	GamePlay.gpHUDLogCenter("Time: " +  value); // for testing
    }

    // WeatherIndex 
    if (RandomiseWeatherIndex == true)
    {
        sect = "MAIN";
        key = "CloudsHeight";
        value = NextInt(minWeatherIndex, maxWeatherIndex).ToString();
        f.add(sect, key, value);
    }

    // clouds
    if (RandomiseClouds == true)
    {
        sect = "MAIN";
        key = "CloudsHeight";
        value = NextInt(minClouds, maxClouds).ToString();
        f.add(sect, key, value);
    }

    // minBreezeActivity
    if (RandomiseBreezeActivity == true)
    {
        sect = "MAIN";
        key = "BreezeActivity";
        value = NextInt(minBreezeActivity, maxBreezeActivity).ToString();
        f.add(sect, key, value); 
    }

    // ThermalActivity 
    if (RandomiseThermalActivity == true)
    {
        sect = "MAIN";
        key = "ThermalActivity";
        value = NextInt(minThermalActivity, maxThermalActivity).ToString();
        f.add(sect, key, value); 
    }

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

}

private static int NextInt(int min, int max)
{
    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
    byte[] buffer = new byte[4];

    rng.GetBytes(buffer);
    int result = BitConverter.ToInt32(buffer, 0);

    return new Random(result).Next(min, max);
}

}
Reply With Quote
  #3  
Old 12-02-2011, 07:19 AM
salmo salmo is offline
Approved Member
 
Join Date: Mar 2011
Posts: 632
Default

The script compiles without errors in the FMB compiler. When I run the mission, I get 2 outcomes in about 50/50 proportion:
(1) mission load & runs OK & time/weather is random
(2) mission load fails & I get an "object reference not set to an instance of an object" error dialog window.
__________________
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; 12-02-2011 at 07:22 AM.
Reply With Quote
  #4  
Old 12-02-2011, 07:57 AM
salmo salmo is offline
Approved Member
 
Join Date: Mar 2011
Posts: 632
Default

Here's a working (beta) of a script to randomise mission time/cloud height/weather/breeze & thermals. Feedback is very welcome.

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:31 AM.
Reply With Quote
  #5  
Old 12-08-2011, 09:32 AM
Octocat Octocat is offline
Approved Member
 
Join Date: Dec 2011
Posts: 22
Default

More concise and economical way to generate random numbers:

Code:
internal ISectionFile RandomiseMission()
{
    var random = new Random(Environment.TickCount);

    ...
    value = random.Next(minBreezeActivity, maxBreezeActivity).ToString();

    ...
    value = random.Next(minThermalActivity, maxThermalActivity).ToString();

    ...
}

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

Quote:
Originally Posted by Octocat View Post
More concise and economical way to generate random numbers:

code removed by author
It's a moot point. The random method certainly uses less resources & therefore is "more efficient", but many argue about the "randomness" of the numbers generated. The Cryptography system method uses more resources, but provides a "better" (more unpredictable) random number set. Of course, to get true randon numbers you would have to seed the generator with something truely random such as cosmic radiation or background noise.
__________________
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
  #7  
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
  #8  
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
  #9  
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
  #10  
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
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 06:42 PM.


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