Results 1 to 12 of 12

Thread: Despawning Trains on Trigger

  1. #1
    Combat pilot
    Join Date
    Aug 2020
    Posts
    103
    Post Thanks / Like
    Total Downloaded
    117.50 MB

    Despawning Trains on Trigger

    I am trying to write a script to remove trains 30min after they arrived in the station on a trigger or destroy and remove. AiGroundGroupType.Train I found in the code, but I cannot get them off the map. Or a trigger for despawn in the future patch. OnAiActorCreated we can put a script in for destroy but this does not work on Trains. I have used all the destroy samples in the _all folders.


    PLS any help would be great!

    BlacKnight

  2. #2
    Team Fusion ATAG_Oskar's Avatar
    Join Date
    Nov 2017
    Location
    Canada
    Posts
    951
    Post Thanks / Like
    Total Downloaded
    908.31 MB

    Re: Despawning Trains on Trigger

    Can't you call the destroy() function?

  3. #3
    Novice Pilot
    Join Date
    Oct 2021
    Posts
    92
    Post Thanks / Like
    Total Downloaded
    11.69 MB

    Re: Despawning Trains on Trigger

    Hello BlacKnight, you are right. Wherever an object is movable, we should be able to treat the object as an AiActor.

    Oleg Maddox and his team allowed that the 'Trains' get an AiActor extra ability and the method <OnActorCreated> may be used.

    Declare a train, e.g. a <M7_0-4-4_c0> or a <57xx_0-6-0PT>, as AiCart and you will reach your goal:

    Code:
    List<AiCart> ListOfTrains = new List<AiCart>();
    
    public override void OnActorCreated(int missionNumber, string shortName, AiActor actor) {
    	base.OnActorCreated(missionNumber, shortName, actor);
    	
    	Timeout(0.01, () => {
    
    		if ((actor is AiCart) && (actor as AiCart).InternalTypeName().Contains("Wagon")){
    			
    			ListOfTrains.Add(actor as AiCart);
    			/*
    			string str = "AiCart.Name(): ";
    			str += (actor as AiCart).Name();
    			str += ", AiCart.InternalTypeName(): ";
    			str += (actor as AiCart).InternalTypeName();
    			str += ", AiCart.Army(): ";
    			str += (actor as AiCart).Army();
    			GamePlay.gpLogServer(null, str, null);
    			*/
    		}
    	});
    }
    The result between /* ... and ... */ is somewhat like this:

    Server: AiCart.Name(): 0_Chief0, AiCart.InternalTypeName(): bob:Wagon.M7_0-4-4, AiCart.Army(): 2
    Server: AiCart.Name(): 1_Chief0, AiCart.InternalTypeName(): bob:Wagon.57xx_0-6-0PT, AiCart.Army(): 1

    You see, the 'Train' is treated as a 'Wagon'.

  4. #4
    Team Fusion ATAG_Oskar's Avatar
    Join Date
    Nov 2017
    Location
    Canada
    Posts
    951
    Post Thanks / Like
    Total Downloaded
    908.31 MB

    Re: Despawning Trains on Trigger

    The Train object has a list of Wagon objects. The Train is a ChiefGround object which has a destroy() function.

  5. Likes TWC_Flug liked this post
  6. #5
    Combat pilot
    Join Date
    Aug 2020
    Posts
    103
    Post Thanks / Like
    Total Downloaded
    117.50 MB

    Re: Despawning Trains on Trigger

    I will try both ways, I have tried the destroy many time and it might be how I am writing the code as an AiCart...

    Thanks guys!

  7. Likes GANIX liked this post
  8. #6
    Team Fusion ATAG_Oskar's Avatar
    Join Date
    Nov 2017
    Location
    Canada
    Posts
    951
    Post Thanks / Like
    Total Downloaded
    908.31 MB

    Re: Despawning Trains on Trigger

    Quote Originally Posted by BlacKnight View Post
    I will try both ways, I have tried the destroy many time and it might be how I am writing the code as an AiCart...

    Thanks guys!
    Try this:

    Code:
            List<AiGroundGroup> trains = new List<AiGroundGroup>();
            AiGroundGroup[] ggs = GamePlay.gpGroundGroups(1);
            foreach (AiGroundGroup ag in ggs)
            {
                if (ag.GroupType() == AiGroundGroupType.Train)
                {
                    trains.Add(ag);
                }
            }

    EDIT:

    Or to get Trains try this.

    Code:
            List<Train> trains = new List<Train>();
            AiGroundGroup[] ggs = GamePlay.gpGroundGroups(1);  // army number 1 is Britain
            foreach (AiGroundGroup ag in ggs)
            {
                if (ag.GroupType() == AiGroundGroupType.Train)
                {
                    trains.Add((Train)ag);
                }
            }
    Last edited by ATAG_Oskar; Mar-20-2023 at 15:48.

  9. #7
    Combat pilot
    Join Date
    Aug 2020
    Posts
    103
    Post Thanks / Like
    Total Downloaded
    117.50 MB

    Re: Despawning Trains on Trigger

    Ok Here is the Script I am Trying was sent to me a while ago by Oskar:
    Code:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    
    using maddox.game;
    using maddox.game.world;
    using maddox.GP;
    
    
    
    public class Mission : AMission {
    
        static string USER_DOC_PATH = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        static string MISSION_PATH = USER_DOC_PATH + "/1C SoftClub/il-2 sturmovik cliffs of dover/missions/Multi/Co-Op/BlacKnight/";
    	
    	int RED = 1;
    	Stopwatch time = new Stopwatch();
    	
    	string[] 	NAME = {"Liverpool-East"};
    	int[]		TIME = { 4, 2 };
    	Stopwatch[]	TIMER = { new Stopwatch(), new Stopwatch() };
    
    	public override void OnBattleStarted() {
    
            MissionNumberListener = -1;
    		loadTrains();
            GamePlay.gpHUDLogCenter("Starting");
        }
    
        public override void OnTickGame() {
    
    		for (int i = 0 ; i < NAME.Length; i++) {
    			if (TIMER[i].Elapsed.Minutes >= TIME[i]) {
    				processTrain(i);
    			}
    		}
    	}
    
    	private void loadTrains() {
    
    		for (int i = 0 ; i < NAME.Length; i++) {
    			GamePlay.gpPostMissionLoad(MISSION_PATH + NAME[i] + ".mis");
    			TIMER[i].Start();
    		}
    	}
    	
    	private void processTrain(int i) {
    
    		AiGroundGroup[] groups = GamePlay.gpGroundGroups(RED);
    		foreach(AiGroundGroup g in groups) {
    			if (g.GroupType() == AiGroundGroupType.Train) {
    				if (g.Name().Contains(NAME[i])) {
    					AiActor[] actors = g.GetItems();
    					foreach (AiActor a in actors) {
    						((AiCart)a).Destroy();
    					}
    					GamePlay.gpPostMissionLoad(MISSION_PATH + NAME[i] + ".mis");
    					TIMER[i].Restart();
    				}
    			}
    		}
    	}
    }
    Files: https://drive.google.com/file/d/1suq...ew?usp=sharing

    Video: https://www.youtube.com/watch?v=Ne7a9P88vqY Sorry no mic will fix tonight!

    Hope ya can help!
    Last edited by BlacKnight; Mar-27-2023 at 13:48.

  10. #8
    Combat pilot
    Join Date
    Aug 2020
    Posts
    103
    Post Thanks / Like
    Total Downloaded
    117.50 MB

    Re: Despawning Trains on Trigger

    Ok I noticed anything with wagons are not removed.

    The simple Train without cars follows the script but the other train just sits there. So I believe the goal is to remove the AiChief with the combination of cars. anyway.

    Hope you guys can figure out how to destroy a full Train. Nothing I have done works...

    BlacKnight

  11. #9
    Team Fusion ATAG_Oskar's Avatar
    Join Date
    Nov 2017
    Location
    Canada
    Posts
    951
    Post Thanks / Like
    Total Downloaded
    908.31 MB

    Re: Despawning Trains on Trigger

    Simpler like this.

    Code:
    using System.Diagnostics;
    
    using maddox.game;
    using maddox.game.world;
    
    
    public class Mission : AMission
    {
        int ARMY_RED = 1;
        static string[] TRAIN_MISSION_NAMES = { "Liverpool-East.mis" };
        int[] TRAIN_MISSION_TIMES = { 4, 2 };
        Stopwatch[] TRAIN_TIMERS = new Stopwatch[TRAIN_MISSION_NAMES.Length];
    
        public override void OnBattleStarted()
        {
            loadTrains();
            GamePlay.gpHUDLogCenter("Starting");
        }
    
    
        public override void OnTickGame()
        {
            for (int i = 0; i < TRAIN_MISSION_NAMES.Length; i++)
            {
                if (TRAIN_TIMERS[i].Elapsed.Minutes >= TRAIN_MISSION_TIMES[i])
                {
                    processTrain(i);
                }
            }
        }
    
    
        private void loadTrains()
        {
            for (int i = 0; i < TRAIN_MISSION_NAMES.Length; i++)
            {
                GamePlay.gpPostMissionLoad(BaseMissionPath + TRAIN_MISSION_NAMES[i]);
                TRAIN_TIMERS[i] = new Stopwatch();
                TRAIN_TIMERS[i].Start();
            }
        }
    
    
        private void processTrain(int i)
        {
            AiGroundGroup[] groups = GamePlay.gpGroundGroups(ARMY_RED);
            foreach (AiGroundGroup g in groups)
            {
                if ((g.GroupType() == AiGroundGroupType.Train) && (g.Name().Contains(TRAIN_MISSION_NAMES[i])))
                {
                    AiActor[] actors = g.GetItems();
                    foreach (AiActor a in actors)
                    {
                        ((AiCart)a).Destroy();
                    }
                    GamePlay.gpPostMissionLoad(BaseMissionPath + TRAIN_MISSION_NAMES[i]);
                    TRAIN_TIMERS[i].Restart();
                }
            }
        }
    }

  12. #10
    Team Fusion ATAG_Oskar's Avatar
    Join Date
    Nov 2017
    Location
    Canada
    Posts
    951
    Post Thanks / Like
    Total Downloaded
    908.31 MB

    Re: Despawning Trains on Trigger

    Quote Originally Posted by BlacKnight View Post
    Ok I noticed anything with wagons are not removed.

    The simple Train without cars follows the script but the other train just sits there. So I believe the goal is to remove the AiChief with the combination of cars. anyway.

    Hope you guys can figure out how to destroy a full Train. Nothing I have done works...

    BlacKnight
    I don't see any obvious way to do it but destroying the whole train with one call would be desirable. I'll investigate.

  13. #11
    Team Fusion ATAG_Oskar's Avatar
    Join Date
    Nov 2017
    Location
    Canada
    Posts
    951
    Post Thanks / Like
    Total Downloaded
    908.31 MB

    Re: Despawning Trains on Trigger

    Just noticed a bug in my code. Here's a different approach.

    Code:
    using System.Collections.Generic;
    using System.Diagnostics;
    
    using maddox.ai.ground;
    using maddox.game;
    using maddox.game.world;
    
    
    public class Mission : AMission
    {
        int ARMY_RED = 1;
        static string[] TRAIN_MISSION_NAMES = { "Liverpool-East.mis" };
        int[] TRAIN_MISSION_TIMES = { 4, 2 };
        Stopwatch[] TRAIN_TIMERS = new Stopwatch[TRAIN_MISSION_NAMES.Length];
        List<AiGroundGroup> trains = new List<AiGroundGroup>();
    
    
        public override void OnBattleStarted()
        {
            loadTrains();
            GamePlay.gpHUDLogCenter("Starting");
        }
    
    
        public override void OnTickGame()
        {
            for (int i = 0; i < TRAIN_MISSION_NAMES.Length; i++)
            {
                if (TRAIN_TIMERS[i].Elapsed.Minutes >= TRAIN_MISSION_TIMES[i])
                {
                    processTrain(i);
                }
            }
        }
    
    
        private void loadTrains()
        {
            for (int i = 0; i < TRAIN_MISSION_NAMES.Length; i++)
            {
                GamePlay.gpPostMissionLoad(BaseMissionPath + TRAIN_MISSION_NAMES[i]);
                TRAIN_TIMERS[i] = new Stopwatch();
                TRAIN_TIMERS[i].Start();
            }
            AiGroundGroup[] groups = GamePlay.gpGroundGroups(ARMY_RED);
            foreach (AiGroundGroup g in groups)
            {
                if (g.GroupType() == AiGroundGroupType.Train)
                {
                    trains.Add(g);
                }
            }
        }
    
    
        private void processTrain(int i)
        {
            AiActor[] actors = trains[i].GetItems();
            foreach (AiActor a in actors)
            {
                ((AiCart)a).Destroy();
            }
            GamePlay.gpPostMissionLoad(BaseMissionPath + TRAIN_MISSION_NAMES[i]);
            TRAIN_TIMERS[i].Restart();
            AiGroundGroup[] groups = GamePlay.gpGroundGroups(ARMY_RED);
            foreach (AiGroundGroup g in groups)
            {
                if (trains.IndexOf(g) < 0)
                {
                    trains[i] = g;
                }
            }
        }
    }

  14. #12
    Combat pilot
    Join Date
    Aug 2020
    Posts
    103
    Post Thanks / Like
    Total Downloaded
    117.50 MB

    Re: Despawning Trains on Trigger

    Can we not just load a mission from the main script and then use a destroy script like this to remove the train: train.miss and train.cs
    Code:
          AiGroundGroup[] groups = GamePlay.gpGroundGroups(RED);
    		foreach(AiGroundGroup g in groups) {
    			if (g.GroupType() == AiGroundGroupType.Train) {
    				if (g.Name().Contains(NAME[i])) {
    					AiActor[] actors = g.GetItems();
    					foreach (AiActor a in actors) {
    						((AiCart)a).Destroy();
    					}
    Code:
        public override void OnActorCreated(int missionNumber, string shortName, AiActor actor)
        {
            base.OnActorCreated(missionNumber,shortName,actor);
    		//ships will die after 55 min when counted from their birth
    		if ((actor as AiGroundActor).Type() == maddox.game.world.AiGroundActorType.Ship)  
    				Timeout (3300, () => 
    									{ if (actor != null)
    										{ (actor as AiGroundActor).Destroy(); }
    									}
    						);
        }

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
  •