private Reef CheckReefs(Coordinate closestObjCoord, IInheritanceTreeCollection <Geometry> MapObjects, Coordinate nextCoord)
        {
            var reefs = MapObjects.GetAll <Reef>();

            var subLine = new LineSegment(Coordinate, nextCoord);

            foreach (Reef reef in reefs)
            {
                var        reefLine   = new LineSegment(reef.Coordinates[0], reef.Coordinates[1]);
                Coordinate coordinate = reefLine.Intersection(subLine);

                if (coordinate != null)
                {
                    Console.WriteLine($"rX1 = {reefLine.P0.X}; rY1 = {reefLine.P0.Y}");
                    Console.WriteLine($"rX2 = {reefLine.P1.X}; rY2 = {reefLine.P1.Y}");

                    Console.WriteLine($"X1 = {coordinate.X}; Y1 = {coordinate.Y}");
                    Console.WriteLine($"X2 = {nextCoord.X}; Y2 = {nextCoord.Y}");

                    Console.WriteLine($"interX = {coordinate.X}; interY = {coordinate.Y}");

                    return(reef);
                }
            }
            return(null);
        }
Пример #2
0
        public ModulesLibrary(string osmFilePath, IInheritanceTreeCollection <Geometry> mapObjects)
        {
            var assemblies = new List <Assembly>();

            foreach (var file in Directory.EnumerateFiles(Constants.ModulesDirectoryPath, "*.dll"))
            {
                try
                {
                    assemblies.Add(AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.GetFullPath(file)));
                }
                catch (Exception exception)
                {
                    Console.WriteLine($"DLL {file} can't be loaded. Exception:\n{exception}");
                }
            }

            var moduleTypesToOrder = assemblies
                                     .SelectMany(a => a.GetExportedTypes())
                                     .Where(type => type.IsSubclassOf(typeof(OSMLSModule)))
                                     .ToDictionary(type => type, type =>
            {
                var initializationOrderAttribute = (CustomInitializationOrderAttribute)type
                                                   .GetCustomAttributes(typeof(CustomInitializationOrderAttribute), false)
                                                   .FirstOrDefault();

                var initializationOrder = (initializationOrderAttribute ?? new CustomInitializationOrderAttribute()).InitializationOrder;

                return(initializationOrder);
            }
                                                   );

            Console.WriteLine(moduleTypesToOrder.Count == 0 ? "No modules found." : "Starting modules initialization in order.");

            foreach (var type in moduleTypesToOrder.OrderBy(x => x.Value).Select(x => x.Key))
            {
                // Writes initialization order.
                Console.WriteLine($"{moduleTypesToOrder[type]}: {type.Name} initialization started.");

                try
                {
                    var moduleInstance = (OSMLSModule)Activator.CreateInstance(type);
                    moduleInstance.Initialize(osmFilePath, Modules, mapObjects);
                    Modules[type] = moduleInstance;
                }
                catch (Exception exception)
                {
                    Console.WriteLine($"Module {type.Name} can't be loaded. Exception:\n{exception}");
                }
            }
        }
        private IntrestingObj FindClosestObj(IInheritanceTreeCollection <Geometry> MapObjects)
        {
            var objects = MapObjects.GetAll <IntrestingObj>();

            IntrestingObj closest = objects[0];

            foreach (IntrestingObj obj in objects)
            {
                if (obj.Distance(this) < closest.Distance(this))
                {
                    closest = obj;
                }
            }
            return(closest);
        }
Пример #4
0
        public void Initialize(string osmFilePath, Dictionary <Type, OSMLSModule> modules, IInheritanceTreeCollection <Geometry> mapObjects)
        {
            lock (_initializationLock)
            {
                if (_isInitialized)
                {
                    throw new InvalidOperationException("Module already initialized.");
                }

                OsmFilePath = osmFilePath;
                _allModules = modules;
                MapObjects  = mapObjects;

                Initialize();

                _isInitialized = true;
            }
        }
        public void MoveToObj(IInheritanceTreeCollection <Geometry> MapObjects)
        {
            IntrestingObj closestObj = FindClosestObj(MapObjects);

            if (IsStuck == 1)
            {
                Console.WriteLine("Я застрял, пытаюсь найти край рифа");

                IsStuck = 2;

                Coordinate reefDownLeftCoordinate = StuckedReef.Coordinates[1];
                if (FindRelativeCoords(reefDownLeftCoordinate).Y < 0)
                {
                    Y = StuckedReef.Coordinates[0].Y + 50000;
                }
                else
                {
                    Y = reefDownLeftCoordinate.Y - 50000;
                }
            }
            else if (IsStuck == 2)
            {
                Console.WriteLine("Край нашёл, теперь обхожу");
                IsStuck = 0;

                Coordinate reefDownLeftCoordinate = StuckedReef.Coordinates[1];
                if (FindRelativeCoords(reefDownLeftCoordinate).X < 0)
                {
                    X = StuckedReef.Coordinates[0].X - 22000;
                }
                else
                {
                    X = reefDownLeftCoordinate.X + 22000;
                }


                StuckedReef = null;
            }
            else
            {
                if (closestObj.Distance(this) <= Speed / 5)
                {
                    if (WaitingTime == 0)
                    {
                        X = closestObj.Coordinate.X;
                        Y = closestObj.Coordinate.Y;
                    }
                    else if (WaitingTime == 10)
                    {
                        MapObjects.Remove(closestObj);
                        WaitingTime = 0;
                    }
                    WaitingTime++;
                }

                Coordinate relativeCoord = FindRelativeCoords(closestObj.Coordinate);
                double     curSpeedX     = Math.Abs(relativeCoord.X) > Speed ? Speed : Speed / 5;
                double     curSpeedY     = Math.Abs(relativeCoord.Y) > Speed ? Speed : Speed / 5;

                double nextX = X + curSpeedX * Math.Sign(relativeCoord.X);
                double nextY = Y + curSpeedY * Math.Sign(relativeCoord.Y);

                var nextCoord = new Coordinate(nextX, nextY);

                Reef stuckedReef = CheckReefs(relativeCoord, MapObjects, nextCoord);

                if (stuckedReef == null)
                {
                    X += curSpeedX * Math.Sign(relativeCoord.X);
                    Y += curSpeedY * Math.Sign(relativeCoord.Y);
                }
                else
                {
                    IsStuck = 1;

                    StuckedReef = stuckedReef;
                }
            }
        }