PDA

View Full Version : Dynamic Missions



SoW Reddog
Oct-11-2013, 20:29
I'm pretty sure that this is not rocket science, nor some revolutionary concept and that others have achieved this much more elegantly than I have. However, in the interests of sharing code, here is something that I'm working on. To see what it does, plonk your self in a plane of your choice in an empty map over Hornchurch at 1000m in the FMB, save the mission in your missions folder as SpawnTest.mis and save the below code in the same folder as SpawnTest.cs. The file location variables are a bit over complicated because I'm planning on using the same ones as for the RDF code and I haven't yet determined why they're in this format.



using System.Text;
using System;
using System.Collections;
using maddox.game;
using maddox.game.world;
using maddox.GP;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Diagnostics;
using System.IO;

public class Mission : AMission
{
//VARIABLE DECLARATIONS
private static string userdocpath = Environment.GetFolderPath(Environment.SpecialFolde r.MyDocuments);
private static string CLODO_FILE_PATH = userdocpath + @"\1C SoftClub\il-2 sturmovik cliffs of dover - mod\"; // THIS IS THE PATH OF YOUR MY DOCUMENTS IL-2 FOLDER
private static string FILE_PATH = @"missions\SpawnTest.mis"; // THIS MUST INDICATE THE MISSION FILE NAME THAT YOU ARE USING THIS SCRIPT IN AND ITS LOCATION FROM THE FOLDER ABOVE
private static string MISSION_FILE = CLODO_FILE_PATH + FILE_PATH;
public static string DYNMISSION_FILE = CLODO_FILE_PATH + @"missions\SpawnTestTemp.mis"; //This is the temp mission filename that will be created

int intDynMissionMax = 2; //Set this to the MAX number of dynamic missions you want to create.
int intDynMissionFlights = 3; //Set this to the MAX number of flights in the mission that you want to create.
int intDynMissionDone = 0; //DO NOT TOUCH THIS
int intDynMissionFlightsDone = 0; //DO NOT TOUCH THIS
int intDynMissionFlightTimer = 8; //This is the timing between spawning flights. Suggest you DO NOT TOUCH
public List<String> DynBombAircraft = new List<String>(); //List to hold the AI Bomber Aircraft you want to create
public Dictionary<String, String> DynSpawnPoints = new Dictionary<String, String>(); //Dictionary to hold the AI Bomber Spawn Points
public Dictionary<String, String> DynTargetPoints = new Dictionary<String, String>(); //Dictionary to hold the AI Bomber Target Coords
public Dictionary<String, String> DynLandingPoints = new Dictionary<String, String>(); //Dictionary to hold the AI Bomber Target Coords
Random random = new Random();

//Functions

public void SetAircraft()
{
DynBombAircraft.Add("Class Aircraft.Do-17Z-2"); //Add the AI BOMBER Aircraft you want to choose from in the dynamic missions
DynBombAircraft.Add("Class Aircraft.Do-17Z-1");
DynBombAircraft.Add("Class Aircraft.He-111H-2");
DynBombAircraft.Add("Class Aircraft.He-111P-2");
DynBombAircraft.Add("Class Aircraft.Ju-87B-2");
DynBombAircraft.Add("Class Aircraft.Ju-88A-1");
}

public void SetSpawns()
{
DynSpawnPoints.Add("DOVER","245577.51 234521.20");//DOVER //Add the coords in format "PLACENAME", "COORDS" as xxxxxx.xx xxxxxx.xx you want to choose from in the dynamic missions for SPAWNING
DynSpawnPoints.Add("Dymchurch","223734.62 222437.79");//DOVER

}

public void SetTargets()
{
DynTargetPoints.Add("FOLKSTONE", "235333.17 229568.64");//FOLKSTONE //Add the coords in format "PLACENAME", "COORDS" as xxxxxx.xx xxxxxx.xx you want to choose from in the dynamic missions for BOMBING
DynTargetPoints.Add("HAWKINGE", "234928.16 232485.60");//HAWKINGE

}

public void SetLandings()
{
DynLandingPoints.Add("French1", "272008 205356");// //Add the coords in format "PLACENAME", "COORDS" as xxxxxx.xx xxxxxx.xx you want to choose from in the dynamic missions for LANDING
DynLandingPoints.Add("French2", "275869 207515");//
DynLandingPoints.Add("French3", "278942 207266");
}

public override void OnBattleStarted()
{
base.OnBattleStarted();

MissionNumberListener = -1;
SetAircraft();
SetSpawns();
SetTargets();
SetLandings();
}

public void CreateNewDynMission()
{

if (File.Exists(DYNMISSION_FILE))
{
File.Delete(DYNMISSION_FILE);
}
List<string> SpawnkeyList = new List<string>(DynSpawnPoints.Keys);
List<string> TargetkeyList = new List<string>(DynTargetPoints.Keys);
List<string> LandingkeyList = new List<string>(DynLandingPoints.Keys);
string strAircraft = DynBombAircraft[random.Next(DynBombAircraft.Count)]; //Generate random aircraft
//Generate random spawn location
string strSpawnKey = SpawnkeyList[random.Next(SpawnkeyList.Count)];
string strSpawn = DynSpawnPoints[strSpawnKey];
//Generate random target location
string strTargetKey = TargetkeyList[random.Next(TargetkeyList.Count)];
string strTarget = DynTargetPoints[strTargetKey];
//Generate random Landing Location
string strLandingKey = LandingkeyList[random.Next(LandingkeyList.Count)];
string strLanding = DynLandingPoints[strLandingKey];

string tmp = string.Format("Aircraft are {0} spawned at {1} are heading for {2}", strAircraft, strSpawnKey, strTargetKey);
GamePlay.gpHUDLogCenter(tmp);
// Create a file to write to.
using (StreamWriter sw = File.CreateText(DYNMISSION_FILE))
{



sw.WriteLine("[PARTS]");
sw.WriteLine("core.100");
sw.WriteLine("bob.100");
sw.WriteLine("[MAIN]");
sw.WriteLine("MAP Land$English_Channel_1940");
sw.WriteLine("BattleArea 8500 8500 350000 350000 10000");
sw.WriteLine("TIME 9");
sw.WriteLine("WeatherIndex 0");
sw.WriteLine("CloudsHeight 1500");
sw.WriteLine("BreezeActivity 10");
sw.WriteLine("ThermalActivity 10");
sw.WriteLine("[GlobalWind_0]");
sw.WriteLine("Power 3.000 0.000 0.000");
sw.WriteLine("BottomBound 0.00");
sw.WriteLine("TopBound 1500.00");
sw.WriteLine("GustPower 0");
sw.WriteLine("GustAngle 45");
sw.WriteLine("[splines]");
sw.WriteLine("[AirGroups]");
sw.WriteLine("BoB_LW_KuFlGr_706.07");
sw.WriteLine("[BoB_LW_KuFlGr_706.07]");
sw.WriteLine("Flight0 1 2 3");
sw.WriteLine("Flight1 11 12 13");
sw.WriteLine("Flight2 21 22 23");
sw.WriteLine(strAircraft);
sw.WriteLine("Formation VIC3");
sw.WriteLine("CallSign 30");
sw.WriteLine("Fuel 100");
sw.WriteLine("Weapons 1 1 1 1 4 2");
sw.WriteLine("Skill 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3");
sw.WriteLine("[BoB_LW_KuFlGr_706.07_Way]");
sw.WriteLine("NORMFLY " + strSpawn + " 1000.00 300.00");
sw.WriteLine("GATTACK_POINT " + strTarget + " 1000.00 300.00");
sw.WriteLine("LANDING " + strLanding + " 500.00 300.00");
}



}

public override void OnTickGame()
{

if (Time.tickCounter() % 1800 == 0) //Check every 60 seconds if Mission should be loaded
{
if (intDynMissionMax > intDynMissionDone) //Do we need to create a mission
{
CreateNewDynMission(); //Calls the dynamic mission function
do
{
Timeout(intDynMissionFlightTimer + (intDynMissionFlightTimer * intDynMissionFlightsDone), () =>
{


GamePlay.gpPostMissionLoad(DYNMISSION_FILE); //spawn mission

});
intDynMissionFlightsDone++; //Increase count
} while (intDynMissionFlights > intDynMissionFlightsDone);
intDynMissionFlightsDone = 0; //Reset to zero for next mission
intDynMissionDone++; //Increases count of spawned missions.
}


}

}

}


I'm intending on adding to this code obviously. It'll need variable altitude, weapon loadouts and I'd like to add landfall and IP/egress waypoints to make it a little bit smoother. I can spawn in all 6 flights of 9 aircraft on my machine with nary a stutter, although clearly the intention isn't to do use this so intensively on the MP server.

Any comments or improvements, fire away.

Salmo
Oct-12-2013, 04:01
Building a mission on the fly is a great way to add variety to a battle. You may also want to explore the ISectionFile method, which creates & launches parts of mission files into your main battle without the need to write the entire mission file each time ...



ISectionFile f = GamePlay.gpCreateSectionFile();
// airgroups section
f.add("AirGroups", "BoB_LW_KuFlGr_706.07", null);

// each airgroups info
f.add("BoB_LW_KuFlGr_706.07", "Class", "Aircraft.He-111H-2");
f.add("BoB_LW_KuFlGr_706.07", "Fuel", "50");
// .... etc

// airgroup waypoints
f.add("BoB_LW_KuFlGr_706.07_Way", "NORMFLY " + strSpawn + " 1000.00 300.00");
f.add("BoB_LW_KuFlGr_706.07_Way", "GATTACK_POINT " + strTarget + " 1000.00 300.00");
f.add("BoB_LW_KuFlGr_706.07_Way", "LANDING " + strLanding + " 500.00 300.00");

// load the mission
GamePlay.gpPostMissionLoad(f);

SoW Reddog
Oct-12-2013, 05:06
Ah OK, thanks Salmo. I knew it wasn't going to be the most elegant solution. Will have to look at this and see what the best method is. I did think that the creating a .mis might be useful for creating a number of missions for reuse in other missions. Just increment and renumber the file and remove the delete.

AKA_Recon
Oct-13-2013, 08:37
I think we should start some sort of programmers group for guys to share code, get Q & A , etc...

There isn't much documentation on any of this, and it would help others ?

Is there any interest in this ?

VMF214_Jupp
Oct-13-2013, 23:47
Yes.

SoW Reddog
Oct-14-2013, 03:39
Yes I'm interested, but isn't this the place already?

SoW Reddog
Oct-14-2013, 14:43
// airgroup waypoints
f.add("BoB_LW_KuFlGr_706.07_Way", "NORMFLY " , strSpawn + " 1000.00 300.00");
f.add("BoB_LW_KuFlGr_706.07_Way", "GATTACK_POINT " , strTarget + " 1000.00 300.00");
f.add("BoB_LW_KuFlGr_706.07_Way", "LANDING " , strLanding + " 500.00 300.00");


Salmo, these three lines are giving me an error message I've never seen. "No overload for method 'add' takes 2 arguments".

Edit. Code corrected above. I'm a numpty and Salmo was probably asleep when trying to help me! :)

AKA_Recon
Oct-14-2013, 17:45
Yes I'm interested, but isn't this the place already?

True, but tough place to pit code snippets - maybe a wiki site?

SoW Reddog
Oct-15-2013, 04:51
Maybe, but I wonder how much work for what return. Who's going to contribute, and who's going to use it?