PDA

View Full Version : Randomising AI skill & fuel



Salmo
Apr-10-2013, 21:27
Tired of AI flights that behave the same every time a mission is played? So am I. Let the mayhem begin :)

Code removed by author ...

ATAG_Torian
Apr-10-2013, 21:45
Well done Salmo and thx for sharing.

Hood
Apr-24-2013, 09:14
Awesome stuff. Do you copy and past everything into a .cs file or just part of it?

Hood

Salmo
Apr-24-2013, 09:30
Awesome stuff. Do you copy and past everything into a .cs file or just part of it?

Hood

The script was designed for server missions or scripts that use the gpPostMissionload method to load a sub-mission into the battle after the battle starts. If you're doing this, then just copy-paste the SalMissionLoad routine into your script & replace all occurrances of GamePlay.gpPostMissionload(MissionName) with SalMissionLoad(MissionName).

Hood
Apr-24-2013, 10:26
The script was designed for server missions or scripts that use the gpPostMissionload method to load a sub-mission into the battle after the battle starts. If you're doing this, then just copy-paste the SalMissionLoad routine into your script & replace all occurrances of GamePlay.gpPostMissionload(MissionName) with SalMissionLoad(MissionName).

Ok so with SalMissionLoad(MissionName) the randomisation will happen as the mission loads and before "Start Battle" is hit, and GamePlay.gpPostMissionload(MissionName) will load once "Start Battle" has been hit? My objective is to create a co-op style mission with several groups of AI aircraft that I want to make random ideally before anyone spawns in.

Many thanks

Hood

Salmo
Apr-25-2013, 22:43
Ok so with SalMissionLoad(MissionName) the randomisation will happen as the mission loads and before "Start Battle" is hit,
Not quite. SalMissionLoad is in your main mission's script, & calls (loads) a sub-mission into the main battle. It randomises Ai skill (& other Ai atributes) as it loads the sub-mission. here's what to do:

1. Make 2 missions, a main mission & at least 1 sub-mission.
2. The main mission should contain all those things you want in the battle (ground objects, statics etc) except the Ai aircraft.
3. The submission should contain only the Ai aircraft.
4. Now get the main mission script file to load the sub-mission after you start the main mission (battle start). Here's the code for the main mission script file. Note the OnBattleStarted routine code.



// ---------------------------------------------------------------------------------------
// Copyright (C) 2013 Lee Miles ALL RIGHTS RESERVED
// This code is subject to copyright & intellectual property laws.
// Please do not copy, reporoduce OR alter any part of this code without the
// expressed permission of the author.
// Email: salmo@netspace.net.au
// ---------------------------------------------------------------------------------------

using System;
using maddox.game;
using maddox.game.world;
using maddox.GP;
using System.IO;

public class Mission : AMission
{
private static string userdocpath = Environment.GetFolderPath(Environment.SpecialFolde r.MyDocuments); // DO NOT CHANGE
private static string CLOD_PATH = userdocpath + @"/1C SoftClub/il-2 sturmovik cliffs of dover - MOD/"; // DO NOT CHANGE
Random random = new Random();

public override void OnBattleStarted()
{
base.OnBattleStarted();
MissionNumberListener = -1;

Timeout(1.0, () => // load SubMissionName 1 second after "battle start"
{
GamePlay.SalMissionLoad(SubMissionName); // load randomised sub-mission
});
}

private void SalMissionLoad(string MissionName)
{
//================================================== ========================================
// Purpose: Randomises certain charactistics in the specified mission file
//
// Input: Mission file name
//
// Output: A temporary mission (text) file, which is deleted after mission is loaded
// into the batle
//
// Use: Replace all occurrances of GamePlay.gpPostMissionload(MissionName) in
// in your C# scripts with SalMissionLoad(MissionName)
//
// How it works: Reads specified missionfile one line at a time & writes each line to a
// new (temporary) mission file. When a 'skill' line or 'fuel' line is found,
// the script will construct a new line with randomised parameters for these
// charateristics. Once reading the mission file is completed, the temporary
// mission file is loaded into the battle & then deleted.
//
// Works best if, in FMB, you select a one plane in the Ai flight & give it
// a custom skill level (change at least 1 skill parameters). This will force
// FMB to write a seperate skill line in the mission file for every plane in
// the flight. If you don't do this, the the entire flight will have just one
// skill line in the mission file & randomising skill will result in all planes
// in the flight having the same skill level. ie. All will be rookies or
// average or veteran or aces, which is probably not a good idea.
//
// Date: 11 April 2013
// Author: Salmo
// Email: salmo@netspace.net.au
//================================================== ========================================

// make a new unique mission file identifier
int Identifier = random.Next(100000);
string @tmpFILENAME = @CLOD_PATH + @"tmpMission" + Identifier.ToString() + ".mis";
if (File.Exists(tmpFILENAME)) File.Delete(tmpFILENAME); // remove existing mission file

StreamReader sr = new StreamReader(@MissionName); // a reader for the mission file
StreamWriter sw = new StreamWriter(tmpFILENAME, true); // a writer for the new temporary mission file
string LineIn;

while ((LineIn = sr.ReadLine()) != null) // read mission file one line at a time
{
// random skill levels
if (LineIn.Contains("Skill")) // is this the 'skill' line?
{
// ===== AI SKILL LEVELS CHANGE AS REQUIRED (MUST SUM TO 1.0) ====
double rookie = 0.30; // 30% are rookies
double average = 0.45; // 45% are average
double veteran = 0.20; // 20% are veterans
double ace = 0.05; // 5% are aces
//================================================== ==============

// skill percentages intergrity check
double skillsum = rookie + average + veteran + ace;
if (skillsum != 1.0)
{
double diff = (1.0 - skillsum) / 4;
rookie = rookie + diff;
average = average + diff;
veteran = veteran + diff;
ace = ace + diff;
}

string NewLine = null; // a new skill line
string[] words = LineIn.Split(' '); // break up the line we just read
for (int j = 0; j < words.GetUpperBound(0); j++)
{
if (words[j].Contains("Skill")) NewLine = " " + words[j] + " "; // start a new skill line
}

// skill variables
double SkillLow = 0.01;
double SkillHigh = 0.99;

for (int i = 1; i < 9; i++)
{
// find what skill catagory is the pilot
double SkillCatagory = random.NextDouble(); // decide whether pilot is rookie, average, veteran or ace
if (SkillCatagory <= rookie)
{ // ROOKIE
SkillLow = 0.01; // lowest skill level for a rookie pilot
SkillHigh = 0.30; // highest skill level for a rookie pilot
}
else if (SkillCatagory <= (rookie + average))
{ // AVERAGE
SkillLow = 0.20; // lowest skill level for an average pilot
SkillHigh = 0.55; // highest skill level for an average pilot
}
else if (SkillCatagory <= (rookie + average + veteran))
{ // VETERAN
SkillLow = 0.45; // lowest skill level for a veteran pilot
SkillHigh = 0.80; // highest skill level for a veteran pilot
}
else if (SkillCatagory > (rookie + average + veteran))
{ // ACE
SkillLow = 0.75; // lowest skill level for an ace pilot
SkillHigh = 1.00; // highest skill level for an ace pilot
}
else
{ // DEFAULT
SkillLow = 0.20; // lowest default skill level
SkillHigh = 0.55; // highest default skill level
}

double skill = -1; // initialise the skill level
while (skill < SkillLow || skill > SkillHigh) skill = random.NextDouble(); // random skill level for this skill catagory
NewLine = NewLine + skill.ToString("0.00") + " "; // construct skill line
}
sw.WriteLine(NewLine); // write this line to temporary mission file
}

// random fuel levels
else if (LineIn.Contains("Fuel")) // is this the 'fuel' line?
{
// ========= AI FUEL LEVELS CHANGE AS REQUIRED =======
int maxFuel = 100; // 100% the maximum fuel level
int minFuel = 50; // 50% the minimum fuel level
//================================================== ==
string NewLine = " Fuel " + random.Next(minFuel, maxFuel + 1).ToString(); // construct a new fuel line
sw.WriteLine(NewLine); // write this line to temporary mission file
}

else
{
sw.WriteLine(LineIn); // write unchanged line to temporary mission file
}
}
sr.Close(); // close the streamreader object
sw.Close(); // close the streamwriter object
GamePlay.gpPostMissionLoad(tmpFILENAME); // load the temporary mission file into the battle
Console.WriteLine("Randomised " + MissionName); // log what we've done to the console
File.Delete(tmpFILENAME); // remove the temporary mission file
}
}

nacy
May-11-2013, 06:12
Hello Salmo

can you give us an overview with a folder with a mission and sub-mission? (downloading)

To better understand how it works.

Thank you for the work done in the script for COD.

Forgive me for my English.Nacy (FRANCE)

Barraclough
May-14-2013, 10:09
Hi Salmo,

I cant get this script working.Some time ago on the IL2-CoD forum you gave me (as Lookaloft) some nice advice about your script Ship oil fire effect so I venture to ask you helping me out for a second time.

I have made two single missions. Main mission (with some objects, statics and me flying a spitfire) and a submission (a flight of ai-ju 88 bombers). Main mission got the name Sal, the submission: SalSub and your script: Sal.cs. I saved these files in one directory named Sal in Mydoc\1CSoftClub\il-2sturmovik cliffs of dover-MOD/missions/Single/ ....
Then I started up the main mission but no luck, couldnt find the Jerrys.
The script was in the main mission. Compile: OK.

With regards,
Barraclough.