Exemplo n.º 1
0
        public void parkTrains(Train.Condition condition, string locId)
        {
            lock (this)
            {
                //Debug.Print("Route length for user {0} is {1}", UserName, route.Count);
                IEnumerable <Train> trains = trains_.Where(t => condition == null || condition(t)).
                                             OrderByDescending(t => t.MaxWaggons);
                foreach (Train train in trains)
                {
                    List <RouteStop> trainRoute = new List <RouteStop>();
                    string           routeStart = train.routeStart();
                    // 3. For each filtered train calculate path to route start
                    Path p = findPath(routeStart, locId);
                    if (p.Count < 2)
                    {
                        continue;
                    }

                    foreach (string node in p)
                    {
                        trainRoute.Add(new RouteStop
                        {
                            dest_id = node,
                        });
                    }
                    // 4. Set route
                    train.setRoute(trainRoute);
                }
            }
        }
Exemplo n.º 2
0
        public void setRoute(Train.Condition condition, params string[] stops)
        {
            refreshTrains();
            if (stops.Length < 2)
            {
                return;
            }

            // 1. Dermine min era
            int min_engine_era = 0;

            List <RouteStop> route = new List <RouteStop>();
            // 2. Calculate route
            RouteStop firstStop = null;
            RouteStop lastStop  = null;

            foreach (string stopId in stops)
            {
                RouteStop currStop = new RouteStop
                {
                    dest_id      = stopId,
                    scheduleType = 1,
                    wait         = 0,
                };
                if (firstStop == null)
                {
                    firstStop = currStop;
                }

                LocationInfo loc = World.getLocation(stopId);
                if (!isLocationConnected(stopId))
                {
                    Trace.TraceWarning("{0} Location {2} is not connected", UserName, loc.Name);
                    return;
                }
                if (loc is Factory)
                {
                    Factory factory = loc as Factory;
                    currStop.loadType = factory.ProductType;

                    if (totalHaulCapacity(currStop.loadType.era()) > waggonCount(currStop.loadType))
                    {
                        Trace.TraceWarning("{0} Not enough waggons for {1} (needs {2} has {3})", UserName,
                                           currStop.loadType, totalHaulCapacity(currStop.loadType.era()), waggonCount(currStop.loadType));
                    }

                    if (currStop.loadType.era() > min_engine_era)
                    {
                        min_engine_era = currStop.loadType.era();
                    }
                }
                if (lastStop != null)
                {
                    currStop.unloadType = lastStop.loadType;
                    // Find path
                    Path p = findPath(lastStop.dest_id, currStop.dest_id);
                    // Add all path locations to route
                    if (p.Count < 2)
                    {
                        // TODO Throw an exception
                        return;
                    }
                    p.RemoveAt(0);           // Start point
                    p.RemoveAt(p.Count - 1); // Goal point
                    foreach (string node in p)
                    {
                        route.Add(new RouteStop {
                            dest_id      = node,
                            scheduleType = 1
                        });
                    }
                }
                // Add current location to the route
                lastStop = currStop;
                route.Add(lastStop);
            }
            // We don't load anythin at the last stop
            lastStop.loadType = ProductType.UNDEFINED;
            // Find path from last stop to the first one
            Path back = findPath(lastStop.dest_id, firstStop.dest_id);

            back.RemoveAt(0);              // Start point
            back.RemoveAt(back.Count - 1); // Goal point
            // Add all path locations to route
            foreach (string node in back)
            {
                route.Add(new RouteStop
                {
                    dest_id      = node,
                    scheduleType = 1
                });
            }

            //waggons_.Where(w => w.Type);
            lock (this)
            {
                if (hasCouplingFor(min_engine_era))
                {
                    --min_engine_era;
                }
                Debug.Print("Route length for user {0} is {1}", UserName, route.Count);
                IEnumerable <Train> trains = trains_.Where(t => t.Era >= min_engine_era && (condition == null || condition(t))).
                                             OrderByDescending(t => t.MaxWaggons);
                foreach (Train train in trains)
                {
                    List <RouteStop> trainRoute = new List <RouteStop>();
                    string           routeStart = train.routeStart();
                    // 3. For each filtered train calculate path to route start
                    Path p = findPath(routeStart, firstStop.dest_id);
                    if (p.Count < 2)
                    {
                        continue;
                    }
                    p.RemoveAt(p.Count - 1);

                    foreach (string node in p)
                    {
                        trainRoute.Add(new RouteStop
                        {
                            dest_id = node,
                        });
                    }
                    foreach (RouteStop stop in route)
                    {
                        trainRoute.Add(stop.copy(train.MaxWaggons));
                    }
                    // 4. Set route
                    train.setRoute(trainRoute);
                }
            }
        }