Esempio n. 1
0
    void Start()
    {
        buildManager = FindObjectOfType <BuildManager>();
        tasksQueue   = FindObjectOfType <TasksQueue>();
        rend         = GetComponent <Renderer>();

        Plow();
    }
 public HostViewModelController(string applicationName)
 {
     ApplicationName = applicationName;
     Messenger       = new MessengerController();
     Configuration   = new Configuration(ApplicationName, null);
     TasksProcessor  = new TasksQueue();
     Scheduler       = new SchedulerController();
 }
Esempio n. 3
0
 void Awake()
 {
     if (tasksQueue)
     {
         Destroy(this);
     }
     else
     {
         tasksQueue = this;
     }
 }
Esempio n. 4
0
        public PointF DoTask(Task t)
        {
            PointF IPos = new PointF(0, 0);
            float  dist = Calc.Magnitude(Position, t.Objective);

            //
            if (t.Unread == true)
            {
                t.Unread = false;
                //
                PushOutput("Started going from " + Logic.PointToString(Position) + " to " + Logic.PointToString(t.Objective));
            }
            //
            if (dist <= ErrorMargin)
            {
                if (t.Type == TaskType.Immediate)
                {
                    if (ImmediateTasks.Count > 0)
                    {
                        ImmediateTasks.Pop();
                        PushOutput(" The car reached the objetive " + Logic.PointToString(t.Objective));
                    }
                    else
                    {
                        // Error
                    }
                }
                else if (t.Type == TaskType.Queue)
                {
                    TasksQueue.Dequeue();
                }
            }
            else
            {
                double ang = Calc.GetAngleOfLineBetweenTwoPoints(Position, t.Objective);
                //
                IPos.X += (float)(Math.Cos(ang) * Speed);
                IPos.Y += (float)(Math.Sin(ang) * Speed);
                //
                if (dist > SecureDistance)
                {
                    //SpeedUp();
                    SetObjectiveSpeed(MaximumSpeed);
                }
                else
                {
                    SetObjectiveSpeed(0.5f);
                }
            }
            //
            return(IPos);
        }
Esempio n. 5
0
        public QueueUserControl(TasksQueue tasksQueue)
        {
            Tasks = tasksQueue;

            InitializeComponent();

            //_bindingSource = new BindingSource();
            //_bindingSource.DataSource = Tasks.TasksList;


            dataGridViewQueue.AutoGenerateColumns = false;
            dataGridViewQueue.DataSource          = Tasks.TasksList;

            Tasks.OnTaskAdded += TasksOnOnTaskAdded;
        }
Esempio n. 6
0
        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            Logger.LogInformation(Resources.BackgroundTasksHostedServiceStarted);

            while (!cancellationToken.IsCancellationRequested)
            {
                var backgroundTaskItem = await TasksQueue.DequeueAsync(cancellationToken);

                try
                {
                    using var serviceScope = ServiceProvider.CreateScope();
                    var newCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, backgroundTaskItem.CancellationToken);
                    await backgroundTaskItem.Delegate(serviceScope.ServiceProvider, newCancellationTokenSource.Token);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, Resources.BackgroundTaskException);
                }
            }

            Logger.LogInformation(Resources.BackgroundTasksHostedServiceStopped);
        }
Esempio n. 7
0
        internal TaskEntry(TasksQueue tasks, DateTime executionDate, Statuses initialStatus, Action<TaskEntry> callback)
        {
            ASSERT( (initialStatus == Statuses.Delayed) || (initialStatus == Statuses.Queued), "Invalid 'initialStatus'" );
            ASSERT( executionDate.Kind == DateTimeKind.Utc, "'executionDate' is not in UTC" );

            ID = Guid.NewGuid();
            LockObject = this;
            Tasks = tasks;
            ExecutionDate = executionDate;
            Status = initialStatus;
            Callback = callback;

            if( Tasks.TransferCultureInfo )
            {
                CreatorCultureInfo = Thread.CurrentThread.CurrentCulture;
                CreatorUICultureInfo = Thread.CurrentThread.CurrentUICulture;
            }
            else
            {
                CreatorCultureInfo = null;
                CreatorUICultureInfo = null;
            }
        }
Esempio n. 8
0
        public void Test(
            [ValueSource(nameof(Cases))] TestCase testCase,
            [ValueSource(nameof(BufferSizes))] BufferSize bufferSize,
            [ValueSource(nameof(EnginesCount))] int enginesCount,
            [ValueSource(nameof(MaxThreadsCount))] int maxThreadsCount)
        {
            const string inputPath = "ZZZZZzzzzZzZZzzzZZZzzz",
           groupsPath = "WWwwwWWwwwWWWwwwwwwwww";

            var groupsFileLength = testCase.Lines
                                   .Sum(o => o.Length + Consts.EndLineBytesCount);

            byte[] groupsFileContent = new byte[groupsFileLength];
            var    inputSource       = testCase.Lines
                                       .SelectMany(line => new[] { line.Select(c => (byte)c), Consts.EndLineBytes })
                                       .Aggregate(Enumerable.Concat)
                                       .ToArray();

            var ioServiceMock = new Mock <IIoService>();
            var configMock    = new Mock <IConfig>();

            ioServiceMock
            .Setup(o => o.SizeOfFile(inputPath))
            .Returns(groupsFileLength);

            ioServiceMock
            .Setup(o => o.OpenRead(inputPath, It.IsAny <long>()))
            .Returns((string _, long position) =>
            {
                var inputStream     = new MemoryStream(inputSource);
                var inputReaderMock = new Mock <IFileReader>();
                inputReaderMock
                .SetupGet(o => o.Length)
                .Returns(() => inputStream.Length);

                inputReaderMock
                .SetupGet(o => o.Position)
                .Returns(() => inputStream.Position);

                inputReaderMock
                .SetupSet(o => o.Position = It.IsAny <long>())
                .Callback((long value) => inputStream.Position = value);

                inputReaderMock
                .Setup(o => o.ReadByte())
                .Returns(() => inputStream.ReadByte());

                inputReaderMock
                .Setup(o => o.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>()))
                .Returns((byte[] buff, int offset, int count) =>
                         inputStream.Read(buff, offset, count));

                inputReaderMock
                .Setup(o => o.Dispose())
                .Callback(() => inputStream.Dispose());

                inputReaderMock.Object.Position = position;
                return(inputReaderMock.Object);
            });

            ioServiceMock
            .Setup(o => o.OpenWrite(groupsPath, It.IsAny <long>(), false))
            .Returns((string _, long position, bool __) =>
            {
                var groupsStream = new MemoryStream(groupsFileContent);
                var writerMock   = new Mock <IFileWriter>();

                writerMock
                .SetupGet(o => o.Position)
                .Returns(() => groupsStream.Position);

                writerMock
                .SetupSet(o => o.Position = It.IsAny <long>())
                .Callback((long value) => groupsStream.Position = value);

                writerMock
                .SetupGet(o => o.Length)
                .Returns(() => groupsStream.Length);

                writerMock
                .Setup(o => o.Write(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>()))
                .Callback((byte[] buff, int offset, int count) =>
                          groupsStream.Write(buff, offset, count));

                writerMock
                .Setup(o => o.Dispose())
                .Callback(() => groupsStream.Close());

                return(writerMock.Object);
            });

            var maxLineLength = testCase.Lines
                                .Max(line => line.Length + Consts.EndLineBytesCount);

            int buffSize = new Dictionary <BufferSize, int>
            {
                { BufferSize.Min, maxLineLength + 1 },
                { BufferSize.Small, maxLineLength + 2 },
                { BufferSize.Medium, groupsFileLength + 1 },
                { BufferSize.Large, groupsFileLength * 2 }
            }[bufferSize];

            configMock
            .SetupGet(o => o.PhysicalBufferLength)
            .Returns(buffSize + sizeof(ulong));

            configMock
            .SetupGet(o => o.UsingBufferLength)
            .Returns(buffSize);

            configMock
            .SetupGet(o => o.MaxRunningTasksCount)
            .Returns(maxThreadsCount);

            configMock
            .SetupGet(o => o.GrouperEnginesCount)
            .Returns(enginesCount);

            configMock
            .SetupGet(o => o.InputFilePath)
            .Returns(inputPath);

            configMock
            .SetupGet(o => o.GroupsFilePath)
            .Returns(groupsPath);

            IGroupsInfoMarger groupsSummaryInfoMarger =
                new GroupsInfoMarger();

            ITasksQueue tasksQueue =
                new TasksQueue(configMock.Object);

            IBuffersPool buffersPool =
                new BuffersPool(configMock.Object);

            IInputReaderFactory inputReaderMaker =
                new InputReaderFactory(
                    ioServiceMock.Object,
                    tasksQueue,
                    buffersPool,
                    configMock.Object);

            IGroupsLinesOutputFactory linesWriterFactory =
                new GroupsLinesOutputFactory(
                    ioServiceMock.Object,
                    tasksQueue,
                    buffersPool,
                    configMock.Object);

            IGrouperIOs grouperIOs =
                new GrouperIOs(
                    inputReaderMaker,
                    linesWriterFactory,
                    ioServiceMock.Object,
                    configMock.Object);

            var grouper = new Grouper(
                groupsSummaryInfoMarger,
                grouperIOs,
                tasksQueue,
                configMock.Object);

            var trivialGrouper = new TrivialGrouper();
            var expectedGroups = trivialGrouper
                                 .SplitToGroups(testCase.Lines);

            var groupsInfo   = grouper.SeparateInputToGroups();
            var resultGroups = ExtractGroups(groupsInfo, groupsFileContent);

            Assert.IsTrue(resultGroups.Select(Group.IsValid).All(o => o));
            CollectionAssert.AreEqual(
                expectedGroups,
                resultGroups);
        }
Esempio n. 9
0
            public void Test(
                string inputFileSettings,
                string groupsFile,
                int bufferSize,
                int enginesCount,
                int maxThreadsCount,
                bool clear)
            {
                string inputFilePath = null;

                try
                {
                    if (inputFileSettings.StartsWith(UseExistanceFile))
                    {
                        inputFilePath = SplitString(inputFileSettings, ": ")[1];
                    }
                    else
                    {
                        var fileGenerationSettings = SplitString(inputFileSettings, " ");
                        Generator.Generate(sizeData: fileGenerationSettings[0],
                                           lineSettings: fileGenerationSettings[1],
                                           path: fileGenerationSettings[2]);
                        inputFilePath = fileGenerationSettings[2];
                    }

                    var configMock           = new Mock <IConfig>();
                    var physicalBufferLength = bufferSize + 1;

                    configMock
                    .SetupGet(o => o.PhysicalBufferLength)
                    .Returns(physicalBufferLength);

                    configMock
                    .SetupGet(o => o.UsingBufferLength)
                    .Returns(bufferSize);

                    configMock
                    .SetupGet(o => o.MaxRunningTasksCount)
                    .Returns(maxThreadsCount);

                    configMock
                    .SetupGet(o => o.GrouperEnginesCount)
                    .Returns(enginesCount);

                    configMock
                    .SetupGet(o => o.InputFilePath)
                    .Returns(inputFilePath);

                    configMock
                    .SetupGet(o => o.GroupsFilePath)
                    .Returns(groupsFile);

                    IGroupsInfoMarger groupsSummaryInfoMarger =
                        new GroupsInfoMarger();

                    ITasksQueue tasksQueue =
                        new TasksQueue(configMock.Object);

                    IBuffersPool buffersPool =
                        new InfinityBuffersPool(physicalBufferLength);

                    IIoService ioService =
                        new IoService(
                            buffersPool);

                    IInputReaderFactory inputReaderMaker =
                        new InputReaderFactory(
                            ioService,
                            tasksQueue,
                            buffersPool,
                            configMock.Object);

                    IGroupsLinesOutputFactory linesWriterFactory =
                        new GroupsLinesOutputFactory(
                            ioService,
                            tasksQueue,
                            buffersPool,
                            configMock.Object);

                    IGrouperIOs grouperIOs =
                        new GrouperIOs(
                            inputReaderMaker,
                            linesWriterFactory,
                            ioService,
                            configMock.Object);

                    ILinesIndexesExtractor linesIndexesExtractor =
                        new LinesIndexesExtractor(
                            configMock.Object);

                    IGroupsLoaderFactory groupsLoaderMaker =
                        new GroupsLoaderFactory(
                            buffersPool,
                            ioService,
                            configMock.Object);

                    var grouper = new Grouper(
                        groupsSummaryInfoMarger,
                        grouperIOs,
                        tasksQueue,
                        configMock.Object);

                    var trivialGrouper = new TrivialGrouper();
                    var expectedGroups = trivialGrouper.SplitToGroups(
                        ReadAllLinesFrom(inputFilePath));

                    var groupsInfo = grouper.SeparateInputToGroups();

                    var output = new IGroup[Consts.MaxGroupsCount];
                    var loader = groupsLoaderMaker.Create(groupsInfo, output);
                    loader.LoadNextGroups();

                    var expectedGroupIds = expectedGroups
                                           .Select(o => o.Id)
                                           .ToArray();

                    var actualGroupIds = groupsInfo
                                         .Select((group, id) => new { group, id })
                                         .Where(o => !GroupInfo.IsZero(o.group))
                                         .Select(o => o.id)
                                         .ToArray();
                    #region DEBUG
// #if DEBUG
//                     var expectedGroupPrefixes = expectedGroupIds
//                         .Select(ToPrefix)
//                         .ToArray();
//
//                     var actualGroupPrefixes = actualGroupIds
//                         .Select(ToPrefix)
//                         .ToArray();
//
//                     var expectedGroupPrefixesInLine =
//                         string.Join(" | ", expectedGroupPrefixes);
//
//                     var actualGroupPrefixesInLine =
//                         string.Join(" | ", actualGroupPrefixes);
//
//                     var actualIdsOnly = actualGroupIds
//                         .Except(expectedGroupIds)
//                         .Select(ToPrefix);
//
//                     var actualIdsOnlyInLine =
//                         string.Join(" | ", actualIdsOnly);
//
//                     var expectedIdsOnly = expectedGroupIds
//                         .Except(actualGroupIds)
//                         .Select(ToPrefix);
//
//                     var allPrefixes =
//                         new[]
//                         {
//                             new[] {string.Empty},
//
//                             Enumerable.Range(' ', '~' - ' ' + 1)
//                                       .Select(o => ((char) o).ToString()),
//
//                             Enumerable.Join(
//                                 Enumerable.Range(' ', '~' - ' ' + 1),
//                                 Enumerable.Range(' ', '~' - ' ' + 1),
//                                 _ => true,
//                                 _ => true,
//                                 (c1, c2) => new string(new []{(char)c1, (char)c2}))
//                         }
//                         .Aggregate(Enumerable.Concat)
//                         .ToArray();
//
//                     var allCalculatedIds = allPrefixes
//                         .Select(ToId)
//                         .OrderBy(o => o)
//                         .ToArray();
//
//                     var allCalculatedIdsDistinct =
//                         allCalculatedIds.Distinct().ToArray();
//
//                     var allCalculatedPrefixes = Enumerable
//                         .Range(0, Consts.MaxGroupsCount)
//                         .Select(ToPrefix)
//                         .ToArray();
//
//                     var allCalculatedPrefixesDistinct =
//                         allCalculatedPrefixes.Distinct().ToArray();
// #endif
                    #endregion
                    CollectionAssert.AreEqual(
                        expectedGroupIds,
                        actualGroupIds);

                    int j = 0;
                    for (int i = 0; i < Consts.MaxGroupsCount; i++)
                    {
                        var info = groupsInfo[i];
                        if (GroupInfo.IsZero(info))
                        {
                            continue;
                        }

                        var expectedInfo = expectedGroups[j];
                        Assert.AreEqual(expectedInfo.BytesCount, info.BytesCount);
                        Assert.AreEqual(expectedInfo.LinesCount, info.LinesCount);

                        linesIndexesExtractor.ExtractIndexes(output[i]);

                        var expectedLines = expectedInfo.Lines
                                            .Select(o => o.Content)
                                            .ToArray();

                        foreach (var line in expectedLines)
                        {
                            line[0] = Consts.EndLineByte1;
                        }

                        var expectedLinesDictionary =
                            new Dictionary <HashedBytesArray, int>(info.LinesCount);

                        for (int k = 0; k < info.LinesCount; k++)
                        {
                            var hashedLine = Hash(expectedLines[k]);
                            if (expectedLinesDictionary.ContainsKey(hashedLine))
                            {
                                ++expectedLinesDictionary[hashedLine];
                            }
                            else
                            {
                                expectedLinesDictionary.Add(hashedLine, 1);
                            }
                        }
                        #region DEBUG
// #if DEBUG
//                         var linesCountInDictionary = expectedLinesDictionary
//                             .Values.Sum(o => o);
// #endif
                        #endregion
                        var lines = output[i].Lines;
                        for (int k = 0; k < info.LinesCount; k++)
                        {
                            var lineIndexes = lines.Array[lines.Offset + k];
                            var lineLength  = lineIndexes.LettersCount
                                              + lineIndexes.DigitsCount
                                              + 3;

                            var buffers       = output[i].Buffers;
                            var bufferIndex   = lineIndexes.Start / bufferSize;
                            var indexInBuffer = lineIndexes.Start % bufferSize;
                            var line          = new byte[lineLength];

                            if (indexInBuffer + lineLength <= bufferSize)
                            {
                                Array.Copy(buffers.Array[buffers.Offset + bufferIndex], indexInBuffer,
                                           line, 0,
                                           lineLength);
                            }
                            else
                            {
                                var bufferRightLength = bufferSize - indexInBuffer;
                                Array.Copy(buffers.Array[buffers.Offset + bufferIndex], indexInBuffer,
                                           line, 0,
                                           bufferRightLength);

                                Array.Copy(buffers.Array[buffers.Offset + bufferIndex + 1], 0,
                                           line, bufferRightLength,
                                           lineLength - bufferRightLength);
                            }

                            var actualHashedLine = Hash(line);
                            Assert.IsTrue(expectedLinesDictionary.ContainsKey(actualHashedLine));
                            --expectedLinesDictionary[actualHashedLine];
                            if (expectedLinesDictionary[actualHashedLine] == 0)
                            {
                                expectedLinesDictionary.Remove(actualHashedLine);
                            }
                        }

                        Assert.AreEqual(0, expectedLinesDictionary.Count);
                        ++j;
                    }

                    Assert.AreEqual(expectedGroups.Length, j);
                    loader.Dispose();
                }
                finally
                {
                    if (clear)
                    {
                        if (!inputFileSettings.StartsWith(UseExistanceFile) &&
                            inputFilePath != null &&
                            File.Exists(inputFilePath))
                        {
                            File.Delete(inputFilePath);
                        }

                        if (File.Exists(groupsFile))
                        {
                            File.Delete(groupsFile);
                        }
                    }
                }
            }
 void Start()
 {
     buildManager = FindObjectOfType <BuildManager>();
     tasksQueue   = FindObjectOfType <TasksQueue>();
 }
Esempio n. 11
0
 public TasksQueueManager()
 {
     TasksQueue              = new TasksQueue();
     TasksQueue.OnTaskAdded += TasksQueueOnOnTaskAdded;
 }
Esempio n. 12
0
        public MessageHandler(TasksQueue tasksQueue, ConnectionList connectionList)
        {
            LOG( "Constructor" );

            TaskQueue = tasksQueue;
            ConnectionList = connectionList;
            FatalExceptionHandler = DefaultFatalExceptionHandler;

            ConnectionList.ConnectionRegistered += (value)=>
                {
                    CheckPendingQueueForConnection( value );
                };
            ConnectionList.ConnectionLost += ConnectionList_ConnectionLost;
        }
Esempio n. 13
0
        public static void Sort(string input, string output)
        {
            IConfig config =
                new Config(
                    input,
                    output);

            IGroupsInfoMarger groupsInfoMarger =
                new GroupsInfoMarger();

            IBuffersPool buffersPool =
                new BuffersPool(
                    config);

            IIoService ioService =
                new IoService(
                    buffersPool);

            ITasksQueue tasksQueue =
                new TasksQueue(
                    config);

            IInputReaderFactory inputReaderFactory =
                new InputReaderFactory(
                    ioService,
                    tasksQueue,
                    buffersPool,
                    config);

            IGroupsLinesOutputFactory groupsLinessOutputFactory =
                new GroupsLinesOutputFactory(
                    ioService,
                    tasksQueue,
                    buffersPool,
                    config);

            IGrouperIOs grouperIOs =
                new GrouperIOs(
                    inputReaderFactory,
                    groupsLinessOutputFactory,
                    ioService,
                    config);

            IGrouper grouper =
                new Grouper(
                    groupsInfoMarger,
                    grouperIOs,
                    tasksQueue,
                    config);

            IGroupsLoaderFactory groupsLoaderFactory =
                new GroupsLoaderFactory(
                    buffersPool,
                    ioService,
                    config);

            ISortingSegmentsSupplier sortingSegmentsSupplier =
                new SortingSegmentsSupplier(
                    config);

            ILinesIndexesExtractor linesIndexesExtractor =
                new LinesIndexesExtractor(
                    config);

            IGroupSorter groupSorter =
                new GroupSorter(
                    sortingSegmentsSupplier,
                    linesIndexesExtractor);

            ISortedGroupWriterFactory sortedGroupWriterFactory =
                new SortedGroupWriterFactory(
                    ioService,
                    config);

            ISorter sorter =
                new Sorter(
                    ioService,
                    grouper,
                    groupsLoaderFactory,
                    groupSorter,
                    sortedGroupWriterFactory,
                    config);

            sorter.Sort();
        }
Esempio n. 14
0
        public PointF DoAI()
        {
            // IPos
            PointF IPos = new PointF(0.00f, 0.00f);

            // Immediate Tasks
            bool hasImmediate = ImmediateTasks.Count != 0;

            if (hasImmediate)
            {
                Task   t    = ImmediateTasks.Peek();
                PointF diff = DoTask(t);
                IPos.X += diff.X;
                IPos.Y += diff.Y;
            }


            // Solve Tasks Queue
            if (TasksQueue.Count > 0 && !hasImmediate)
            {
                Task   t    = TasksQueue.Peek();
                PointF diff = DoTask(t);
                IPos.X += diff.X;
                IPos.Y += diff.Y;
            }

            // Nomadic Mode
            if (Nomadic)
            {
                Path cp = GetBelongingPath();
                if (cp != null)
                {
                    if (TasksQueue.Count == 0 && !hasImmediate)
                    {
                        PointF endOfPath = cp.GetPathCornerBasedOnDirection();
                        float  dist      = Calc.Modulus(Calc.Magnitude(endOfPath, Position));
                        if (dist < cp.ErrorMargin)
                        {
                            Path   ip            = cp.ParentPathGroup.GetWayWithDir(!cp.Direction);
                            PointF nearestCorner = ip.GetNearestCorner(Position);
                            Task   t             = CreateTask(nearestCorner, TaskType.Queue);
                            EnqueueTask(t);
                        }
                        else
                        {
                            //Task t = CreateTask(endOfPath, TaskType.Immediate);
                            //ImmediateTasks.Push(t);
                            //
                            Task t = CreateTask(endOfPath, TaskType.Queue);
                            EnqueueTask(t);
                        }
                    }
                }
            }

            // Path Info
            Path cCarPath = GetBelongingPath();
            int  cCarPId  = cCarPath != null ? cCarPath.ID : -1;

            // Speed Bump
            foreach (SpeedBump sb in CLogic.SpeedBumps)
            {
                if (sb.IsAlligned(this))
                {
                    if (sb.IsTowardSpeedBump(this))
                    {
                        float dist = sb.DistanceOfCar(this);
                        //
                        if (dist < SecureDistance)
                        {
                            SetObjectiveSpeed(sb.DesiredSpeed);
                        }
                    }
                }
            }

            ////////////////////////////////////////// IT'S NOT WORKING CORRECTLY! //////////////////////////////////////////////////////
            // Change Way (if there's another free way)
            if (cCarPath != null)
            {
                Car cNearest = cCarPath.GetNearestCarInPath(CLogic, Position, Id);
                if (cNearest != null)
                {
                    float dist = Calc.Modulus(Calc.Magnitude(Position, cNearest.Position));
                    if (dist < SecureDistance) // <--------------------------------------- TODO
                    {
                        List <Path> availablePaths = cCarPath.ParentPathGroup.GetWaysWithDir(cCarPath.Direction);
                        foreach (Path p in availablePaths)
                        {
                            if (p.ID == cCarPath.ID)
                            {
                                continue;
                            }
                            //
                            PointF nLinePos           = Calc.PointLineNormalizedIntersection(Position, p.P1, p.P2);
                            Car    nearestAnotherPath = p.GetNearestCarInPath(CLogic, nLinePos);
                            if (nearestAnotherPath == null)
                            {
                                //
                                PointF futurePoint = new PointF(nLinePos.X + (float)((Math.Cos(Angle) * Speed)),
                                                                nLinePos.Y + (float)((Math.Sin(Angle) * SecureDistance)));

                                // Go TO Another Path
                                Task task = CreateTask(futurePoint, TaskType.Immediate);
                                ImmediateTasks.Push(task);
                                TasksQueue.Clear();
                                break;
                            }
                            //
                            float distCarAnotherPath = Calc.Modulus(Calc.Magnitude(Position, nearestAnotherPath.Position));
                            if (distCarAnotherPath > SecureDistance)
                            {
                                //
                                PointF futurePoint = new PointF(nLinePos.X + (float)((Math.Cos(Angle) * Speed)),
                                                                nLinePos.Y + (float)((Math.Sin(Angle) * SecureDistance)));
                                // Go TO Another Path
                                Task task = CreateTask(futurePoint, TaskType.Immediate);
                                ImmediateTasks.Push(task);
                                TasksQueue.Clear();
                                break;
                            }
                        }
                    }
                }
            }

            // Car Operations
            for (int C = 0; C < CLogic.Cars.Count; C++)
            {
                if (CLogic.Cars[C].Id != Id)
                {
                    // Current Car
                    Car cCar = CLogic.Cars[C];

                    // Error margin
                    const float errAngle    = 30.0f;
                    double      errAngleRad = Calc.NormalizeRadian(Calc.DegreeToRadians(errAngle));

                    // Vars
                    double angAc    = Calc.NormalizeRadian(cCar.Angle);
                    double angAcInv = Calc.NormalizeRadian(cCar.Angle + Math.PI);
                    double angTc    = Calc.NormalizeRadian(Angle);
                    double angAcTc  = Calc.NormalizeRadian(Calc.GetAngleOfLineBetweenTwoPoints(cCar.Position, Position));
                    double angTcAc  = Calc.NormalizeRadian(Calc.GetAngleOfLineBetweenTwoPoints(Position, cCar.Position));
                    float  distBoth = Calc.Modulus(Calc.Magnitude(Position, cCar.Position));

                    // Test if the other car is going toward this car
                    if (Calc.IsInRange(angAcTc - (errAngleRad / 2.00d), angAcTc + (errAngleRad / 2.00d), angAc))
                    {
                        // Test if the other car is going in the same direction
                        if (Calc.IsInRange(angTc - (errAngleRad / 2.00d), angTc + (errAngleRad / 2.00d), angAcInv))
                        {
                            if (distBoth < SecureDistance)
                            {
                                //Break();
                                SetObjectiveSpeed(0.00f);
                            }
                        }
                    }

                    // Test if this car is going toward another car (few FOV to avoid the car speed down when the car is comming in the oposite way)
                    //if (Calc.IsInRange(angTcAc - (errAngleRad / 2.00d), angTcAc + (errAngleRad / 2.00d), angTc))
                    //{
                    //    if (distBoth < SecureDistance)
                    //    {
                    //        //Break();
                    //        SetObjectiveSpeed(0.00f);
                    //    }
                    //}

                    if (Calc.IsInRange(angTcAc - (Calc.DegreeToRadians(FOV) / 2.00d), angTcAc + (Calc.DegreeToRadians(FOV) / 2.00d), angTc))
                    {
                        Path bpcCar = cCar.GetBelongingPath();
                        if (bpcCar != null && GetBelongingPath() != null)
                        {
                            if (distBoth < SecureDistance && bpcCar.ID == GetBelongingPath().ID)
                            {
                                //Break();
                                SetObjectiveSpeed(0.00f);
                            }
                        }
                        else
                        {
                            if (distBoth < SecureDistance)
                            {
                                //Break();
                                SetObjectiveSpeed(0.00f);
                            }
                        }
                    }
                }
            }

            // Traffic Lights
            foreach (TrafficLightGroup tfg in CLogic.TrafficLights)
            {
                foreach (TrafficLight tl in tfg.TLs)
                {
                    if (tl.IsAlligned(this))
                    {
                        if (tl.IsTowardTrafficLight(this))
                        {
                            if (tl.Status == TrafficLightStatus.Red)
                            {
                                float dist = tl.DistanceOfCar(this);
                                //
                                if (dist < SecureDistance)
                                {
                                    SetObjectiveSpeed(0.00f);
                                }
                            }
                        }
                    }
                }
            }

            return(IPos);
        }
Esempio n. 15
0
 public void EnqueueTask(Task t)
 {
     TasksQueue.Enqueue(t);
 }