View Single Post
  #14  
Old 12-09-2011, 03:45 PM
Octocat Octocat is offline
Approved Member
 
Join Date: Dec 2011
Posts: 22
Default

Yes, because dynamic type resolution "var x = " works in functions only, and calls like "provider.GetBytes(buffer)" too.

In your case better to use constructor, to bury inside all temporary instances like "buffer" and "provider", when they perform their task. For example:

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

public class Mission : AMission
{
  // allowed to change only in constructor
  private readonly Random m_random;

  // Constructor (do not call manually)
  public Mission()
  {
    // initialize a new instance of the Random class
    var provider = new RNGCryptoServiceProvider();
    var buffer = new byte[4];
    provider.GetBytes(buffer);
    m_random = new Random(BitConverter.ToInt32(buffer, 0));
  }

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

  internal ISectionFile RandomiseMission()
  {
    //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 = (m_random.Next(minTime * 100, (maxTime) * 100) / 100f).ToString();
      secfile.add("MAIN", "TIME", value);
    }

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

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

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

    // ThermalActivity 
    if (RandomiseThermalActivity == true)
    {
      string value = m_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-09-2011 at 03:53 PM.
Reply With Quote