Beispiel #1
0
        public RouteTracker(RouteTaskChain chain, bool audio)
        {
            TaskChain = chain;
            TakeTask();
            this.audio = audio;

            /*if(audio)
             * {
             *  InputSystem
             * }*/
        }
Beispiel #2
0
        public static RouteTaskChain FromDVTask(Task task)
        {
            switch (task.InstanceTaskType)
            {
            case TaskType.Transport:
            case TaskType.Warehouse:
                return(new RouteTaskChain()
                {
                    tasks = new List <RouteTask>()
                    {
                        RouteTask.FromDVTask(task)
                    }
                });

            case TaskType.Sequential:
                RouteTaskChain first = null;
                RouteTaskChain prev  = null;
                foreach (var nestedTask in task.GetTaskData().nestedTasks)
                {
                    if (nestedTask.IsTaskCompleted())
                    {
                        continue;
                    }

                    RouteTaskChain current = null;

                    switch (nestedTask.InstanceTaskType)
                    {
                    case TaskType.Transport:
                    case TaskType.Warehouse:
                    case TaskType.Parallel:
                        current = FromDVTask(nestedTask); break;

                    default:
                        Terminal.Log($"Not supported task type {nestedTask.InstanceTaskType} inside sequence"); break;
                    }

                    if (current != null)
                    {
                        if (prev != null)
                        {
                            prev.nextTasks = current;
                        }

                        prev = current;
                    }

                    if (first == null)
                    {
                        first = current;
                    }
                }
                return(first);

            case TaskType.Parallel:
                RouteTaskChain result = new RouteTaskChain();
                result.tasks = task.GetTaskData().nestedTasks
                               .Where(t => !t.IsTaskCompleted())
                               .Select(t => RouteTask.FromDVTask(t))
                               .Where(rt => rt != null)
                               .ToList();

                return(result);
            }

            return(null);
        }
Beispiel #3
0
        public async static System.Threading.Tasks.Task DoCommand(CommandArg[] args)
        {
            if (args.Length == 0)
            {
                throw new CommandException("No command args");
            }

            if (args[0].String == "job" || args[0].String == "loco")
            {
                List <JobBooklet> allJobBooklets = new List <JobBooklet>(JobBooklet.allExistingJobBooklets);
                JobBooklet        jobBooklet     = null;

                if (allJobBooklets.Count == 0)
                {
                    throw new CommandException("No current job");
                }

                if (allJobBooklets.Count > 1 || args.Length > 1)
                {
                    if (args.Length < 2)
                    {
                        throw new CommandException("Multiple job, specify job name");
                    }

                    jobBooklet = allJobBooklets.FirstOrDefault(j => j.job.ID == args[1].String);
                }
                else
                {
                    jobBooklet = allJobBooklets[0];
                }


                if (jobBooklet == null)
                {
                    throw new CommandException("Unknown job");
                }

                RouteTaskChain chain = RouteTaskChain.FromDVJob(jobBooklet.job);

                if (chain == null)
                {
                    throw new CommandException("Not supported job type yet");
                }

                Terminal.Log($"Using job {jobBooklet.job.ID} chain {chain}");

                TrainCar trainCar;


                RouteTracker routeTracker = new RouteTracker(chain, true);

                if (args[0].String == "loco")
                {
                    trainCar = PlayerManager.LastLoco;

                    if (trainCar == null)
                    {
                        throw new CommandException("No last loco");
                    }
                }
                else
                {
                    trainCar = routeTracker.CurrentTask.TrainSets.FirstOrDefault()?.firstCar;
                }

                if (routeTracker.CurrentTask == null)
                {
                    throw new CommandException("No suitable task");
                }

                Track startTrack = trainCar.trainset.firstCar.Bogies[0].track.logicTrack;

                await FindAndSwitch(startTrack, routeTracker.CurrentTask.DestinationTrack, ReversingStrategy.ChooseBest, trainCar.trainset);

                routeTracker.SetRoute(Module.ActiveRoute.Route, trainCar.trainset);

                Module.ActiveRoute.RouteTracker = routeTracker;
            }
            else if (args[0].String == "tracker")
            {
                TrainCar trainCar = PlayerManager.LastLoco;

                if (trainCar == null)
                {
                    throw new CommandException("No last loco");
                }

                if (Module.ActiveRoute.RouteTracker.CurrentTask == null)
                {
                    throw new CommandException("Tracker has no task");
                }

                Track startTrack = trainCar.trainset.firstCar.Bogies[0].track.logicTrack;

                await FindAndSwitch(startTrack, Module.ActiveRoute.RouteTracker.CurrentTask.DestinationTrack, ReversingStrategy.ChooseBest, trainCar.trainset);

                Module.ActiveRoute.RouteTracker.SetRoute(Module.ActiveRoute.Route, trainCar.trainset);
            }
            else if (args[0].String == "from" && args.Length == 4 && args[2].String == "to")
            {
                Trainset trainset = null; //default value if we don't have consist (trainset)

                if (args[1].String == "loco")
                {
                    TrainCar trainCar = PlayerManager.LastLoco;

                    if (trainCar == null)
                    {
                        throw new CommandException("No last loco");
                    }

                    if (trainCar.trainset.firstCar.logicCar.BogiesOnSameTrack)
                    {
                        args[1].String = trainCar.trainset.firstCar.logicCar.CurrentTrack.ID.FullID;
                    }
                    else
                    {
                        args[1].String = trainCar.trainset.firstCar.logicCar.FrontBogieTrack.ID.FullID;
                    }

                    trainset = trainCar.trainset;
                    Terminal.Log($"current loco track end");
                }
                else if (args[1].String == "job.trainset")
                {
                    //TODO
                }

                if (trainset == null)
                {
                    throw new CommandException("Goal track must be associated with car");
                }


                RailTrack startTrack = RailTrackRegistry.AllTracks.FirstOrDefault((RailTrack track) => track?.logicTrack.ID.FullID == args[1].String);

                RailTrack goalTrack = RailTrackRegistry.AllTracks.FirstOrDefault((RailTrack track) => track?.logicTrack.ID.FullID == args[3].String);

                RouteTaskChain chain   = RouteTaskChain.FromDestination(goalTrack.logicTrack, trainset);
                var            tracker = new RouteTracker(chain, false);


                if (startTrack == null || goalTrack == null)
                {
                    throw new CommandException("start track or goal track not found");
                }

                await FindAndSwitch(startTrack.logicTrack, goalTrack.logicTrack, Module.settings.ReversingStrategy, trainset);

                tracker.SetRoute(Module.ActiveRoute.Route, trainset);
                Module.ActiveRoute.RouteTracker = tracker;
            }
            else if (args[0].String == "opposite")
            {
                if (!Module.ActiveRoute.IsSet)
                {
                    throw new CommandException("No route active");
                }


                var route = await Module.ActiveRoute.Route.FindOppositeRoute();

                if (route != null)
                {
                    Module.ActiveRoute.Route = route;
                    route.AdjustSwitches();

                    Module.ActiveRoute.RouteTracker.SetRoute(route, route.Trainset);

                    Terminal.Log($"Route {route.Length} {route?.SecondTrack?.logicTrack.ID.FullDisplayID}");
                }
                else
                {
                    throw new CommandException("Opposite route could not be found");
                }
            }
            else if (args[0].String == "clear")
            {
                if (Module.ActiveRoute.IsSet)
                {
                    Module.ActiveRoute.ClearRoute();
                }
                else
                {
                    throw new CommandException("no active route");
                }
            }
            else if (args[0].String == "info")
            {
                if (Module.ActiveRoute.IsSet)
                {
                    Terminal.Log("Active route:");
                    Terminal.Log(Module.ActiveRoute.Route.ToString());
#if DEBUG
                    Terminal.Log(Module.ActiveRoute.Route.Path.Select(t => t.logicTrack.ID.FullID).Aggregate((i, j) => i + "->" + j));
#endif
                }
                else
                {
                    throw new CommandException("no active route");
                }
            }
#if DEBUG
            else if (args[0].String == "track")
            {
                var track = RailTrackRegistry.AllTracks.Where(t => t.logicTrack.ID.FullID.ToLower() == args[1].String.ToLower()).FirstOrDefault();
                if (track == null)
                {
                    throw new CommandException("track not found");
                }

                Terminal.Log($"{track.logicTrack.ID.FullID} {track.logicTrack.length}m");

                if (track.inIsConnected)
                {
                    Terminal.Log("IN: " + track.GetAllInBranches().Select(b => b.track.logicTrack.ID.FullID).Aggregate((a, b) => a + "; " + b));
                }
                if (track.outIsConnected)
                {
                    Terminal.Log("OUT: " + track.GetAllOutBranches().Select(b => b.track.logicTrack.ID.FullID).Aggregate((a, b) => a + "; " + b));
                }
            }
            else if (args[0].String == "trainset")
            {
                TrainCar trainCar = PlayerManager.LastLoco;

                if (trainCar == null)
                {
                    throw new CommandException("No last loco");
                }

                Terminal.Log($"loco trainset cars: {trainCar.trainset.cars.Count}");
                Terminal.Log($"first car: {trainCar.trainset.firstCar.logicCar.ID}");
                Terminal.Log($"last car: {trainCar.trainset.lastCar.logicCar.ID}");
                Terminal.Log($"first car track: {trainCar.trainset.firstCar.logicCar.CurrentTrack?.ID.FullDisplayID}");
                Terminal.Log($"last car track: {trainCar.trainset.lastCar.logicCar.CurrentTrack?.ID.FullDisplayID}");
                Terminal.Log($"first car front boogie track: {trainCar.trainset.firstCar.logicCar.FrontBogieTrack?.ID.FullDisplayID}");
                Terminal.Log($"first car rear boogie track: {trainCar.trainset.firstCar.logicCar.RearBogieTrack?.ID.FullDisplayID}");
            }
#endif
            else if (args[0].String == "auto")
            {
                TrainCar trainCar = PlayerManager.LastLoco;

                if (trainCar == null)
                {
                    throw new CommandException("No last loco");
                }

                if (args[1].String == "stop")
                {
                    var locoAI = Module.GetLocoAI(trainCar);
                    locoAI.Stop();
                    return;
                }

                RailTrack goalTrack = RailTrackRegistry.AllTracks.FirstOrDefault((RailTrack track) => track?.logicTrack.ID.FullID == args[1].String);
                if (goalTrack == null)
                {
                    throw new CommandException("Goal track not found");
                }

                RouteTaskChain chain   = RouteTaskChain.FromDestination(goalTrack.logicTrack, trainCar.trainset);
                var            tracker = new RouteTracker(chain, false);

                Track startTrack = trainCar.trainset.firstCar.Bogies[0].track.logicTrack;

                var route = await Route.FindRoute(startTrack, tracker.CurrentTask.DestinationTrack, ReversingStrategy.ChooseBest, trainCar.trainset);

                tracker.SetRoute(route, trainCar.trainset);

                var driver = Module.GetLocoAI(trainCar);
                driver.StartAI(tracker);
            }
            else
            {
                throw new CommandException($"Unknown subcommand {args[0]}");
            }
        }