Results 1 to 11 of 11

Thread: Launching a random sub-mission TUTORIAL

  1. #1
    Team Fusion Salmo's Avatar
    Join Date
    Nov 2011
    Posts
    2,332
    Post Thanks / Like
    Total Downloaded
    191.25 MB

    Launching a random sub-mission TUTORIAL

    A very quick & dirty tutorial on how to launch a random sub-mission from inside your main mission file. Firstly, there are many ways to do this, here's one of my methods. I try to make my code 'utilitarian'. ie. I construct it in such a way that it is flexible & can be reused from mission to mission with having to alter too much of the code itself. For example, I have many random mission I want to generate from my main mission. I put the main mis file & the main script file into a directory on their own. Then I create a series of sub-directoriess below the main directory. My red sub-missions will be in a folder called 'red', by blue submission will be in a folder called 'blue' etc. There's no correct way to do this I just recommend a subfolder organisation that makes sense for you.

    1. GLOBAL VARIABLES - I like to setup a few global variables at the start of my scripts & reference that global variable inside the script inself. That way I can just change global variable parameter without having to alter any of the remaining script lines. Note the lines in the // global variables section. They define the path to the CLOD missions folders & where in those folders my actual mission is located. Note also that this is a full-path.

    2. RANDOMNESS - To start generating random outcomes, you'll need to initialise a 'random' class somewhere at the start of your script. Note the Random random = new Random(); line. This initiates a new random class call "random". We can use this class later in the script to find random missions.

    3. ONAIRCRAFT KILLED - We want to generate a random mission when an aircraft is 'killed' (destroyed). So the code to find a random mission to launch has to go inside the OnAircraftKilled game event. We could put this code anywhere inside other game events like OnAircraftTookoff etc, but for this tutorial we use the OnAircraftKilled event. Let's go through that code line by line.

    The List<string> .... line looks in a sub-directory called 'red' & makes a list of all mission files in that sub-directory. Note how I use a function called GetFilenamesFromDirectory that can recieve a mission filename 'mask'. That way I can have (say) fighter & bomber missions in the same red subdirectory but only make a list of (in this case) just the missions with the word 'bomber' in the mission name. So my bomber missions will be named 'bomber1.mis, bomber2,mis etc.

    The string RandomMission = .... line will pick one mission from the list of missions. Note this string will be the FULL-PATH to that mission.

    The GamePlay.gpPostMissionLoad .... line will load that submission into the game.


    Code:
    using System;
    using System.Collections;
    using maddox.game;
    using maddox.game.world;
    using part;
    using System.Collections.Generic;
    using System.IO;
    
    public class Mission : AMission
    {
        // global variables
        private static string userdocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);   // DO NOT CHANGE
        private static string CLOD_PATH = userdocpath + @"/1C SoftClub/il-2 sturmovik cliffs of dover - MOD/";  // DO NOT CHANGE
        private static string FILE_PATH = @"missions/Multi/Dogfight/OperationHomePlateV3.0/";   // mission install directory (CHANGE AS NEEDED)
        Random random = new Random();
    
        public override void OnAircraftKilled(int missionNumber, string shortName, AiAircraft aircraft)
        {
            base.OnAircraftKilled(missionNumber, shortName, aircraft);
    
            List<string> RandomMissions = GetFilenamesFromDirectory(CLOD_PATH + FILE_PATH + "/red", "bomber"); // bomber missions from the red directory
            if (RandomMissions.Count > 0)
            {
                 string RandomMission = RandomMissions[random.Next(RandomMissions.Count)];
                 GamePlay.gpPostMissionLoad(RandomMission);
            }
        }
    
        public List<string> GetFilenamesFromDirectory(string dirPath, string mask = null)
        {
            List<string> list = new List<string>();
            string[] filenames = Directory.GetFiles(dirPath, "*" + mask + "*.mis");
            list = new List<string>(filenames);
            return list;
        }
    }
    Last edited by Salmo; Nov-17-2014 at 18:42.

  2. #2
    Team Fusion ♣_Spiritus_♣'s Avatar
    Join Date
    Dec 2013
    Location
    The Demon-Haunted World: Science as a Candle in the Dark
    Posts
    5,600
    Post Thanks / Like
    Blog Entries
    2
    Total Downloaded
    0

    Re: Launching a random sub-mission TUTORIAL

    This needs a sticky.


  3. #3
    Team Fusion ATAG_Bliss's Avatar
    Join Date
    Feb 2011
    Location
    United States of China
    Posts
    4,135
    Post Thanks / Like
    Total Downloaded
    457.02 MB

    Re: Launching a random sub-mission TUTORIAL

    Quote Originally Posted by Spiritus_Mortem View Post
    This needs a sticky.

    Yes it does!.... Stickied!

    Thanks for posting this up Salmo!


    "The dissenter is every human being at those moments of his life when he resigns momentarily from the herd and thinks for himself". - Archibald Macleish


  4. #4
    Novice Pilot
    Join Date
    Feb 2013
    Posts
    38
    Post Thanks / Like
    Total Downloaded
    0

    Re: Launching a random sub-mission TUTORIAL

    Thanks Salmo, I can get a random mission to load and no need for case or if statements
    Cheers
    Jacko
    F/Lt Jacko
    No1 Squadron Yellow 2 (JX-J)
    http://www.tangmerepilots.co.uk

  5. #5
    Team Fusion Salmo's Avatar
    Join Date
    Nov 2011
    Posts
    2,332
    Post Thanks / Like
    Total Downloaded
    191.25 MB

    Re: Launching a random sub-mission TUTORIAL

    Quote Originally Posted by No1_Jacko View Post
    Thanks Salmo, I can get a random mission to load and no need for case or if statements
    Cheers
    Jacko
    That's what I meant by creating flexibile code. In this example, you just need to drop a new mission into the appropriate folder & it is available as a random mission without having to change any lines of code.

  6. #6
    Veteran Combat pilot
    Join Date
    Jan 2013
    Location
    chicago
    Posts
    369
    Post Thanks / Like
    Total Downloaded
    0

    Re: Launching a random sub-mission TUTORIAL

    I have made a simple mission with bombers and G50's and have added the above script to the mission (spitscript.mis/ spitscript.cs) i am unclear as to how i designate which aircraft death will trigger the sub mission.

    thnx for any help

    mission
    [PARTS]
    core.100
    bob.100
    [MAIN]
    MAP Land$English_Channel_1940
    BattleArea 150000 100000 100000 150000 1000
    TIME 12
    WeatherIndex 2
    CloudsHeight 1000
    BreezeActivity 10
    ThermalActivity 10
    player BoB_RA_56St_20Gruppo_353Sq.000
    [GlobalWind_0]
    Power 3.000 0.000 0.000
    BottomBound 0.00
    TopBound 1500.00
    GustPower 3
    GustAngle 44
    [splines]
    [AirGroups]
    BoB_RA_56St_20Gruppo_353Sq.07
    BoB_RAF_F_FatCat_Early.01
    [BoB_RA_56St_20Gruppo_353Sq.07]
    Flight0 1
    Flight1 11 12 13
    Flight2 21 22 23
    Class Aircraft.G50
    Formation VIC3
    CallSign 30
    Fuel 100
    Weapons 1
    Skill 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3
    [BoB_RA_56St_20Gruppo_353Sq.07_Way]
    NORMFLY 288576.00 179904.00 500.00 300.00
    AATTACK_BOMBERS 278808.00 176388.00 500.00 300.00
    [BoB_RAF_F_FatCat_Early.01]
    Flight0 1 2 3 4
    Class Aircraft.BlenheimMkIVF
    Formation VIC3
    CallSign 26
    Fuel 100
    Weapons 1
    Skill 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3
    [BoB_RAF_F_FatCat_Early.01_Way]
    NORMFLY 272333.47 173800.73 500.00 300.00
    NORMFLY 278533.23 176234.62 500.00 300.00
    [CustomChiefs]
    [Stationary]
    [Buildings]
    [BuildingsLinks]
    [Trigger]
    tankrespwn TGroundDestroyed 20 287526 136094 600
    bf110spawn TTime 30
    [Action]
    tankrespwn ASpawnGroup 0 2_Chief
    bf110spawn ASpawnGroup 0 BoB_LW_LG2_I.04
    script

    using System;
    using System.Collections;
    using maddox.game;
    using maddox.game.world;
    using part;
    using System.Collections.Generic;
    using System.IO;

    public class Mission : AMission
    {
    // global variables
    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
    private static string FILE_PATH = @"missions/Multi/Dogfight/spitscript/"; // mission install directory (CHANGE AS

    NEEDED)
    Random random = new Random();

    public override void OnAircraftKilled(int missionNumber, string shortName, AiAircraft aircraft)
    {
    base.OnAircraftKilled(missionNumber, shortName, aircraft);

    List<string> RandomMissions = GetFilenamesFromDirectory(CLOD_PATH + FILE_PATH + "/red", "bomber"); // bomber

    missions from the red directory
    if (RandomMissions.Count > 0)
    {
    string RandomMission = RandomMissions[random.Next(RandomMissions.Count)];
    GamePlay.gpPostMissionLoad(RandomMission);
    }
    }

    public List<string> GetFilenamesFromDirectory(string dirPath, string mask = null)
    {
    List<string> list = new List<string>();
    string[] filenames = Directory.GetFiles(dirPath, "*" + mask + "*.mis");
    list = new List<string>(filenames);
    return list;
    }
    }
    “Ah, women. They make the highs higher and the lows more frequent.” Friedrich Nietzsche.

    Current rig: Ryzen 3600 ,16GB DDR4 3200 ram, 1TB Crucial NVMe, 500GB Samsung EVO, Seasonic platinum prime 750W, ASrock pro4, Vega56 VC, 2560x1440 144Hz Win10 64bit

  7. #7
    Team Fusion TWC_Fatal_Error's Avatar
    Join Date
    Dec 2013
    Location
    Florida
    Posts
    678
    Post Thanks / Like
    Total Downloaded
    226.63 MB

    Re: Launching a random sub-mission TUTORIAL

    Sal I would like to set up a trigger to detect a condition, then we can intercept the trigger, when it is triggered, in the .cs file. Then we can do whatever we want at that point, including launch a sub-mission or whatever. We could also prevent the trigger from being triggered too often (such as, maximum once every 5 minutes, once every 15 minutes, or whatever).

    That **should be** pretty easy to do.

    Also it should be pretty easy to set up a routine in the .cs file that would monitor all aircraft once every minute, two minutes, or other pre-set time, and see if any of the planes from the selected army are in the selected area, and then do something according to whatever constraints we wish.

    would something like this work?



    public override void OnTrigger(int missionNumber, string shortName, active)
    {
    base.OnTrigger(missionNumber, shortName, active);
    if ("F1".Equals(shortName) && active)

    AiAction action = GamePlay.gpGetAction("action1");
    if (action != null)
    {
    action.Do();
    }
    List<string> RandomMissions = GetFilenamesFromDirectory(CLOD_PATH + FILE_PATH + "/red", "bomber"); // bomber missions from the red directory
    if (RandomMissions.Count > 0)
    {
    string RandomMission = RandomMissions[random.Next(RandomMissions.Count)];
    GamePlay.gpPostMissionLoad(RandomMission);
    }
    }
    Last edited by TWC_Fatal_Error; Mar-28-2017 at 22:45.
    i7 2700 ,32 gigs, 4 ssd's ,1 7200rpm 2terabyte,1 samsung 55 display,2 mfd's,EDTRACKER,Warthog ,X55 stick, 7inch liliput monitor in mfd's 21 I-inc secondary display for instruments using virtual cockpit
    [*]iustus facere unus[*] JUST MAKE ONE ( FATAL ERROR) Commander TWC http://twcpilots.com

  8. #8
    Team Fusion Salmo's Avatar
    Join Date
    Nov 2011
    Posts
    2,332
    Post Thanks / Like
    Total Downloaded
    191.25 MB

    Re: Launching a random sub-mission TUTORIAL

    I'm guessing here, without any testing. Try this ....

    Code:
    public override void OnTrigger(int missionNumber, string shortName, active)
    {
         base.OnTrigger(missionNumber, shortName, active);
    
         if ("F1".Equals(shortName) && active) 
         {
             AiAction action = GamePlay.gpGetAction(shortName);
             if (action != null)
             {
                // action.Do(); // doesn't matter what the pre-set trigger action was, we'll do something different
                List<string> RandomMissions = GetFilenamesFromDirectory(CLOD_PATH + FILE_PATH + "/red", "bomber"); // bomber missions from the red directory
                if (RandomMissions.Count > 0)
                {
                   string RandomMission = RandomMissions[random.Next(RandomMissions.Count)];
                   GamePlay.gpPostMissionLoad(RandomMission);
                }
             }
             GamePlay.gpGetTrigger("F1").Enable = true;  // need to reactivate the trigger, can be time delayed if u want
        }
    }
    Last edited by Salmo; Apr-05-2017 at 10:52.

  9. #9
    Supporting Member
    Join Date
    Sep 2019
    Location
    Yorkshire
    Posts
    138
    Post Thanks / Like
    Total Downloaded
    1.39 GB

    Re: Launching a random sub-mission TUTORIAL

    Hi I'm new to this functionality and don't quite understand it. Is a sub-mission launched while the main mission is still playing i.e. it alters the current mission? Or does it specify the 'next' mission to be played in a campaign?

    I'm thinking of placing some objects on the map and have them load into all missions with this kind of script, rather than having to add the objects to every mission. Am I on the right track?

    Thank you,

    Martin

  10. #10
    Ace 1lokos's Avatar
    Join Date
    Jan 2012
    Posts
    5,323
    Post Thanks / Like
    Total Downloaded
    1.04 GB

    Re: Launching a random sub-mission TUTORIAL

    The idea is change the actual mission, when a event is triggered, e.g. a plane destroyed.

    http://forum.1cpublishing.eu/showthr...416#post279416

    An example is that "Ambulance" script: when you landing, depends on condition of your plane some types of support vehicles appear to "help" you, can be a fuel truck, a ammo truck, or a ambulance.

    An good idea "on paper" that result weird due CloD bugs, since spline lines embed on airfield (and map) became broken in some of Luthier "FPS" patch, vehicles behavior is crazy, sometimes rolling over player plane.

    I'm thinking of placing some objects on the map and have them load into all missions with this kind of script, rather than having to add the objects to every mission.
    Seems that is not possible load buildings via sub-mission:

    http://forum.1cpublishing.eu/showthr...t=sub-missions

  11. Likes Marcost liked this post
  12. #11
    Supporting Member
    Join Date
    Sep 2019
    Location
    Yorkshire
    Posts
    138
    Post Thanks / Like
    Total Downloaded
    1.39 GB

    Re: Launching a random sub-mission TUTORIAL

    Thank you for your help 1Lokos! i saw your comments on splines bug in other threads, so good that you have confirmed it is still an issue.

    Trying to catch up on 8 years of development, it is hard to tell what is still relevant!

    Regards,

    M

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •