/*
     * private void OnDisable()
     * {
     *  string path = @"C:\Users\Administrator\Desktop\Aleksei\Parallel3DChaSi\GSCM1_InTimeDomain\Assets\";
     *  string path1 = path + "h_time1.csv";//Application.persistentDataPath + "/h_time1.csv";
     *
     *  using (var file = File.CreateText(path1))
     *  {
     *      foreach (var arr in h_save)
     *      {
     *          //if (String.IsNullOrEmpty(arr)) continue;
     *          file.Write(arr[0]);
     *          for (int i = 1; i < arr.Count; i++)
     *          {
     *              file.Write(',');
     *              file.Write(arr[i]);
     *          }
     *          file.WriteLine();
     *      }
     *  }
     *
     *  string path2 = path + filename;// Application.persistentDataPath + "/H_freq1.csv";
     *
     *  using (var file = File.CreateText(path2))
     *  {
     *      foreach (var arr in H_save)
     *      {
     *          //if (String.IsNullOrEmpty(arr)) continue;
     *          file.Write(arr[0]);
     *          for (int i = 1; i < arr.Count; i++)
     *          {
     *              file.Write(',');
     *              file.Write(arr[i]);
     *          }
     *          file.WriteLine();
     *      }
     *  }
     *
     *  Debug.Log("The lenght of the list<list> structure " + h_save.Count);
     * }
     */

    void Start()
    {
        FrameCounter     = 0;
        NeigbouringCount = 0;
        /// for Fourier transform
        X_inputValues = new double[H.Length];
        for (int i = 0; i < H.Length; i++)
        {
            X_inputValues[i] = i;
        }

        #region Reading Info from Scripts
        #region MPCs
        // MPCs Data
        GameObject MPC_Spawner = GameObject.Find("CorrectPolygons");
        Correcting_polygons_Native MPC_Native_Script = MPC_Spawner.GetComponent <Correcting_polygons_Native>();

        DMC_num  = MPC_Native_Script.ActiveV6_DMC_NativeList.Length;
        MPC1_num = MPC_Native_Script.ActiveV6_MPC1_NativeList.Length;
        MPC2_num = MPC_Native_Script.ActiveV6_MPC2_NativeList.Length;
        MPC3_num = MPC_Native_Script.ActiveV6_MPC3_NativeList.Length;


        CornersNormalsPerpendiculars = MPC_Native_Script.Active_CornersNormalsPerpendiculars;
        //DMC_Native = MPC_Native_Script.ActiveV6_DMC_NativeList;
        //MPC1_Native = MPC_Native_Script.ActiveV6_MPC1_NativeList;
        //MPC2_Native = MPC_Native_Script.ActiveV6_MPC2_NativeList;
        //MPC3_Native = MPC_Native_Script.ActiveV6_MPC3_NativeList;

        //DMC_attenuation = MPC_Native_Script.ActiveV6_DMC_Power;
        //MPC1_attenuation = MPC_Native_Script.ActiveV6_MPC1_Power;
        //MPC2_attenuation = MPC_Native_Script.ActiveV6_MPC2_Power;
        //MPC3_attenuation = MPC_Native_Script.ActiveV6_MPC3_Power;

        //DMC_perp = MPC_Native_Script.Active_DMC_Perpendiculars;
        //MPC1_perp = MPC_Native_Script.Active_MPC1_Perpendiculars;
        //MPC2_perp = MPC_Native_Script.Active_MPC2_Perpendiculars;
        //MPC3_perp = MPC_Native_Script.Active_MPC3_Perpendiculars;

        // for all MPCs
        MPC_num         = MPC_Native_Script.ActiveV6_MPC_NativeList.Length;
        MPC_Native      = MPC_Native_Script.ActiveV6_MPC_NativeList;
        MPC_attenuation = MPC_Native_Script.ActiveV6_MPC_Power;
        MPC_perp        = MPC_Native_Script.Active_MPC_Perpendiculars;

        #endregion

        #region LookUpTable
        // LookUp Table Data
        GameObject        LookUpTable = GameObject.Find("LookUpTablesUpd");
        LookUpTableGenUpd LUT_Script  = LookUpTable.GetComponent <LookUpTableGenUpd>();
        maxdistance = LUT_Script.MaxSeenDistance;
        //maxNumberSeenMPC2 = LUT_Script.maxlengthMPC2;
        //maxNumberSeenMPC3 = LUT_Script.maxlengthMPC3;

        LookUpTableMPC2 = LUT_Script.LookUpTableMPC2;
        MPC2LUTID       = LUT_Script.MPC2LUTID;
        LookUpTableMPC3 = LUT_Script.LookUpTableMPC3;
        MPC3SeenID      = LUT_Script.MPC3SeenID;
        #endregion

        #region Positions
        // Coordinates of antennas
        GameObject         VehiclesData  = GameObject.Find("MovementManager");
        AllVehiclesControl ControlScript = VehiclesData.GetComponent <AllVehiclesControl>();
        OldCoordinates = ControlScript.OldCoordinates;
        CarCoordinates = ControlScript.CarCoordinates;
        CarForwardVect = ControlScript.CarForwardVect;
        CarsSpeed      = ControlScript.CarsSpeed;
        #endregion

        #endregion

        car_num = CarCoordinates.Length;

        link_num = car_num * (car_num - 1) / 2;

        Links       = new NativeArray <Vector2Int>(link_num, Allocator.Persistent);
        Overlaps1   = new NativeArray <Overlap>(link_num, Allocator.Persistent);
        TxOverlaps2 = new NativeArray <Overlap>(link_num, Allocator.Persistent);
        RxOverlaps2 = new NativeArray <Overlap>(link_num, Allocator.Persistent);
        TxOverlaps3 = new NativeArray <Overlap>(link_num, Allocator.Persistent);
        RxOverlaps3 = new NativeArray <Overlap>(link_num, Allocator.Persistent);

        // LoS
        commandsLoS = new NativeArray <RaycastCommand>(2 * link_num, Allocator.Persistent);
        resultsLoS  = new NativeArray <RaycastHit>(2 * link_num, Allocator.Persistent);

        /*
         * // DMC
         * SoA0 = new NativeArray<float>(DMC_num * car_num, Allocator.Persistent);
         * Seen0 = new NativeArray<int>(DMC_num * car_num, Allocator.Persistent);
         * commands0 = new NativeArray<RaycastCommand>(DMC_num * car_num, Allocator.Persistent);
         * results0 = new NativeArray<RaycastHit>(DMC_num * car_num, Allocator.Persistent);
         * // MPC1
         * SoA1 = new NativeArray<float>(MPC1_num * car_num, Allocator.Persistent);
         * Seen1 = new NativeArray<int>(MPC1_num * car_num, Allocator.Persistent);
         * commands1 = new NativeArray<RaycastCommand>(MPC1_num * car_num, Allocator.Persistent);
         * results1 = new NativeArray<RaycastHit>(MPC1_num * car_num, Allocator.Persistent);
         * // MPC2
         * SoA2 = new NativeArray<float>(MPC2_num * car_num, Allocator.Persistent);
         * Seen2 = new NativeArray<int>(MPC2_num * car_num, Allocator.Persistent);
         * commands2 = new NativeArray<RaycastCommand>(MPC2_num * car_num, Allocator.Persistent);
         * results2 = new NativeArray<RaycastHit>(MPC2_num * car_num, Allocator.Persistent);
         * // MPC3
         * SoA3 = new NativeArray<float>(MPC3_num * car_num, Allocator.Persistent);
         * Seen3 = new NativeArray<int>(MPC3_num * car_num, Allocator.Persistent);
         * commands3 = new NativeArray<RaycastCommand>(MPC3_num * car_num, Allocator.Persistent);
         * results3 = new NativeArray<RaycastHit>(MPC3_num * car_num, Allocator.Persistent);
         */

        // MPCs
        SoA      = new NativeArray <float>((DMC_num + MPC1_num + MPC2_num + MPC3_num) * car_num, Allocator.Persistent);
        Seen     = new NativeArray <int>((DMC_num + MPC1_num + MPC2_num + MPC3_num) * car_num, Allocator.Persistent);
        commands = new NativeArray <RaycastCommand>((DMC_num + MPC1_num + MPC2_num + MPC3_num) * car_num, Allocator.Persistent);
        results  = new NativeArray <RaycastHit>((DMC_num + MPC1_num + MPC2_num + MPC3_num) * car_num, Allocator.Persistent);

        int link_count = 0;
        for (int i = 0; i < car_num; i++)
        {
            for (int j = i + 1; j < car_num; j++)
            {
                Links[link_count] = new Vector2Int(i, j);
                link_count       += 1;
            }
        }

        // Channels for all links
        H_LoS = new NativeArray <System.Numerics.Complex>(FFTNum * link_num, Allocator.Persistent);
        //H0 = new NativeArray<System.Numerics.Complex>(FFTNum * link_num, Allocator.Persistent);
        //H1 = new NativeArray<System.Numerics.Complex>(FFTNum * link_num, Allocator.Persistent);
        //H2 = new NativeArray<System.Numerics.Complex>(FFTNum * link_num, Allocator.Persistent);
        //H3 = new NativeArray<System.Numerics.Complex>(FFTNum * link_num, Allocator.Persistent);
        H_NLoS = new NativeArray <System.Numerics.Complex>(FFTNum * link_num, Allocator.Persistent);


        //EdgeEffectCoeff = 10.0f;
    }
    void Start()
    {
        GameObject MPC_Spawner = GameObject.Find("CorrectPolygons");
        Correcting_polygons_Native MPC_Native_Script = MPC_Spawner.GetComponent <Correcting_polygons_Native>();

        int MPC2_num = MPC_Native_Script.ActiveV6_MPC2_NativeList.Length;
        int MPC3_num = MPC_Native_Script.ActiveV6_MPC3_NativeList.Length;

        NativeArray <V6>      MPC2_Native = MPC_Native_Script.ActiveV6_MPC2_NativeList;
        NativeArray <Vector3> MPC2_Side   = MPC_Native_Script.Active_MPC2_Perpendiculars;
        NativeArray <V6>      MPC3_Native = MPC_Native_Script.ActiveV6_MPC3_NativeList;
        NativeArray <Vector3> MPC3_Side   = MPC_Native_Script.Active_MPC3_Perpendiculars;

        NativeArray <float> MPC3_Att = MPC_Native_Script.ActiveV6_MPC3_Power;

        Debug.Log("Size of MPC2 List = " + MPC2_Native.Length + "; MPC3 List = " + MPC3_Native.Length);

        // all about the LookUpTable for MPC2
        #region Generating LookUp Table for MPC2


        NativeArray <SeenPath2> allpath2 = new NativeArray <SeenPath2>(MPC2_num * (MPC2_num - 1), Allocator.TempJob);

        NativeArray <RaycastCommand> CommandsNativeArray_MPC2 = new NativeArray <RaycastCommand>(MPC2_num * (MPC2_num - 1), Allocator.TempJob);
        NativeArray <RaycastHit>     ResultsNativeArray_MPC2  = new NativeArray <RaycastHit>(MPC2_num * (MPC2_num - 1), Allocator.TempJob);
        NativeArray <Vector2Int>     MPC2_ID = new NativeArray <Vector2Int>(MPC2_num * (MPC2_num - 1), Allocator.TempJob);

        float t_V6 = Time.realtimeSinceStartup;

        #region Paralle Raycasting MPC2

        ParallelRayCastingDataV6 RayCastingData_MPC2 = new ParallelRayCastingDataV6
        {
            MPC_Array = MPC2_Native,
            commands  = CommandsNativeArray_MPC2,
            ID        = MPC2_ID,
        };
        JobHandle jobHandle_RayCastingData_MPC2 = RayCastingData_MPC2.Schedule(MPC2_num * (MPC2_num - 1), 1);
        jobHandle_RayCastingData_MPC2.Complete();

        // parallel raycasting
        JobHandle rayCastJobMPC1 = RaycastCommand.ScheduleBatch(CommandsNativeArray_MPC2, ResultsNativeArray_MPC2, 64, default);
        rayCastJobMPC1.Complete();
        #endregion

        #region Parallel Calculation of Path2 Parameters
        // parallel search of possiblte second order of paths
        ParallelPath2Search parallelPath2Search = new ParallelPath2Search
        {
            MPC_Array          = MPC2_Native,
            MPC_Perpendiculars = MPC2_Side,                    // Who is writing code like this??? It's actually Active_MPC2_Perpendiculars (perpendiculars Karl!!!)
            commands           = CommandsNativeArray_MPC2,
            results            = ResultsNativeArray_MPC2,
            ID = MPC2_ID,

            maxDistance    = MaxSeenDistance / 3,
            angleThreshold = (float)0.1,

            PossiblePath2 = allpath2,
        };

        JobHandle jobHandlePath2Search = parallelPath2Search.Schedule(MPC2_num * (MPC2_num - 1), 64);
        jobHandlePath2Search.Complete();
        #endregion

        #region Filtering Zero Valued Path2
        NativeList <int> indexes     = new NativeList <int>(Allocator.TempJob);
        IndexFilter      indexFilter = new IndexFilter
        {
            Array = allpath2,
        };
        JobHandle jobHandleIndexFilter = indexFilter.ScheduleAppend(indexes, allpath2.Length, 64);
        jobHandleIndexFilter.Complete();

        LookUpTableMPC2 = new NativeArray <SeenPath2>(indexes.Length, Allocator.Persistent);
        NativeArray <Vector2Int> LUTIndexArrayMPC2    = new NativeArray <Vector2Int>(indexes.Length, Allocator.TempJob);
        LookUpTableFinal         lookUpTableFinalMPC2 = new LookUpTableFinal
        {
            InArray    = allpath2,
            IndexArray = indexes,
            OutArray   = LookUpTableMPC2,
            IndxArray  = LUTIndexArrayMPC2,
        };
        JobHandle jobHandleLookUpTableMPC2 = lookUpTableFinalMPC2.Schedule(indexes.Length, 64);
        jobHandleLookUpTableMPC2.Complete();
        #endregion

        #region Testing Function for Preparing Indexes

        //Vector2Int[] MPC2LUTID = new Vector2Int[MPC2_num];
        MPC2LUTID = new NativeArray <Vector2Int>(MPC2_num, Allocator.Persistent);
        int progressionIndex = 0;

        for (int i = 0; i < MPC2_num; i++)
        {
            int index_min = progressionIndex;
            int index_max = progressionIndex;
            int flagIndex = 0; // if 1 then something is found
            for (int j = progressionIndex; j < LookUpTableMPC2.Length; j++)
            {
                if (i == LookUpTableMPC2[j].MPC_IDs.x)
                {
                    index_max = j; flagIndex = 1;
                }
                else
                {
                    break;
                }
            }

            // check if the MPC can see others or not
            if (flagIndex == 1)
            {
                progressionIndex = index_max + 1;
                MPC2LUTID[i]     = new Vector2Int(index_min, index_max);
                if (index_max - index_min + 1 > maxlengthMPC2)
                {
                    maxlengthMPC2 = index_max - index_min + 1;
                }
            } // in the case, it sees others
            else
            {
                MPC2LUTID[i] = new Vector2Int(-1, -1);
            } // in the case it doesn't see others
        }

        /* for checking weak paths
         * int weak_path2 = 0;
         * for (int i = 0; i < LookUpTableMPC2.Length; i++)
         * {
         *  if (LookUpTableMPC2[i].AngularGain < 0.001)
         *  {
         *      weak_path2 += 1;
         *  }
         * }*/
        #endregion

        Debug.Log("Time spent for parallel raycasting : " + ((Time.realtimeSinceStartup - t_V6) * 1000f) + " ms");

        #region drawing MPC2 connections

        /*
         * for (int i = 0; i < LookUpTableMPC2.Length; i++)
         * {
         *  int fromMPC = LookUpTableMPC2[i].MPC_IDs.x;
         *  int toMPC = LookUpTableMPC2[i].MPC_IDs.y;
         *  //Debug.DrawLine(MPC2_Native[fromMPC].Coordinates + new Vector3(0,1,0), MPC2_Native[toMPC].Coordinates + new Vector3(0, 1, 0), Color.green, 1.0f);
         *  Debug.DrawLine(MPC2_Native[fromMPC].Coordinates, MPC2_Native[toMPC].Coordinates, Color.green, 1.0f);
         * }
         *
         * for (int i = 0; i < allpath2.Length; i++)
         * {
         *  if (allpath2[i].AngularGain != 0)
         *  {
         *      Debug.DrawLine(MPC2_Native[MPC2_ID[i].x].Coordinates, MPC2_Native[MPC2_ID[i].y].Coordinates, Color.cyan, 1.0f);
         *      Debug.DrawLine(MPC2_Native[MPC2_ID[i].x].Coordinates, MPC2_Native[MPC2_ID[i].x].Coordinates + 5*MPC2_Native[MPC2_ID[i].x].Normal, Color.red, 1.0f);
         *  }
         * }*/
        #endregion

        #region Disposing MPC2 NativeArrays
        LUTIndexArrayMPC2.Dispose();
        indexes.Dispose();
        MPC2_ID.Dispose();
        CommandsNativeArray_MPC2.Dispose();
        ResultsNativeArray_MPC2.Dispose();
        allpath2.Dispose();
        #endregion

        #endregion

        // all about the LookUpTable for MPC3
        #region Generating LookUp Table for MPC3

        NativeArray <SeenPath2> allpath3_half = new NativeArray <SeenPath2>(MPC3_num * (MPC3_num - 1), Allocator.TempJob);

        NativeArray <RaycastCommand> CommandsNativeArray_MPC3 = new NativeArray <RaycastCommand>(MPC3_num * (MPC3_num - 1), Allocator.TempJob);
        NativeArray <RaycastHit>     ResultsNativeArray_MPC3  = new NativeArray <RaycastHit>(MPC3_num * (MPC3_num - 1), Allocator.TempJob);
        NativeArray <Vector2Int>     MPC3_ID = new NativeArray <Vector2Int>(MPC3_num * (MPC3_num - 1), Allocator.TempJob);

        // Start: calculation of the first level of path3
        #region Paralle Raycasting MPC3

        ParallelRayCastingDataV6 RayCastingData_MPC3 = new ParallelRayCastingDataV6
        {
            MPC_Array = MPC3_Native,
            commands  = CommandsNativeArray_MPC3,
            ID        = MPC3_ID,
        };
        JobHandle jobHandle_RayCastingData_MPC3 = RayCastingData_MPC3.Schedule(MPC3_num * (MPC3_num - 1), 1);
        jobHandle_RayCastingData_MPC3.Complete();

        // parallel raycasting
        JobHandle rayCastJobMPC3 = RaycastCommand.ScheduleBatch(CommandsNativeArray_MPC3, ResultsNativeArray_MPC3, 64, default);
        rayCastJobMPC3.Complete();
        #endregion

        #region Parallel Calculation of Path3 Parameters
        // parallel search of possiblte second order of paths
        ParallelPath2Search parallelPath3Search = new ParallelPath2Search
        {
            MPC_Array          = MPC3_Native,
            MPC_Perpendiculars = MPC3_Side,
            commands           = CommandsNativeArray_MPC3,
            results            = ResultsNativeArray_MPC3,
            ID = MPC3_ID,

            maxDistance    = MaxSeenDistance / 5,
            angleThreshold = (float)0.1,

            PossiblePath2 = allpath3_half,
        };

        JobHandle jobHandlePath3Search = parallelPath3Search.Schedule(MPC3_num * (MPC3_num - 1), 64);
        jobHandlePath3Search.Complete();
        #endregion

        #region Filtering Zero Valued First Floor Path3
        NativeList <int> indexes3     = new NativeList <int>(Allocator.TempJob);
        IndexFilter      indexFilter3 = new IndexFilter
        {
            Array = allpath3_half,
        };
        JobHandle jobHandleIndexFilter3 = indexFilter3.ScheduleAppend(indexes3, allpath3_half.Length, 64);
        jobHandleIndexFilter3.Complete();

        NativeArray <SeenPath2>  LookUpTableMPC3_half = new NativeArray <SeenPath2>(indexes3.Length, Allocator.TempJob);
        NativeArray <Vector2Int> LUTIndexArrayMPC3    = new NativeArray <Vector2Int>(indexes3.Length, Allocator.TempJob);
        LookUpTableFinal         lookUpTableFinalMPC3 = new LookUpTableFinal
        {
            InArray    = allpath3_half,
            IndexArray = indexes3,
            OutArray   = LookUpTableMPC3_half,
            IndxArray  = LUTIndexArrayMPC3,
        };
        JobHandle jobHandleLookUpTableMPC3 = lookUpTableFinalMPC3.Schedule(indexes3.Length, 64);
        jobHandleLookUpTableMPC3.Complete();
        #endregion

        #region Testing Function for Preparing Indexes for MPC3
        NativeArray <Vector2Int> MPC3LUTID = new NativeArray <Vector2Int>(MPC3_num, Allocator.TempJob);
        //Vector2Int[] MPC3LUTID = new Vector2Int[MPC3_num];
        int progressionIndex3 = 0;
        for (int i = 0; i < MPC3_num; i++)
        {
            int index_min = progressionIndex3;
            int index_max = progressionIndex3;
            int flagIndex = 0; // if 1 then something is found
            for (int j = progressionIndex3; j < LookUpTableMPC3_half.Length; j++)
            {
                if (i == LookUpTableMPC3_half[j].MPC_IDs.x)
                {
                    index_max = j; flagIndex = 1;
                }
                else
                {
                    break;
                }
            }

            // check if the MPC can see others or not
            if (flagIndex == 1)
            {
                progressionIndex3 = index_max + 1;
                MPC3LUTID[i]      = new Vector2Int(index_min, index_max);
            } // in the case, it sees others
            else
            {
                MPC3LUTID[i] = new Vector2Int(-1, -1);
            } // in the case it doesn't see others
        }
        #endregion
        // End: calculation of the first level of path3

        // Start: calculation of the whole path3s
        #region Finding All Possible Path3

        NativeArray <SeenPath3> allpath3    = new NativeArray <SeenPath3>(LookUpTableMPC3_half.Length * MPC3_num, Allocator.TempJob);
        ParallelPath3Search     path3Search = new ParallelPath3Search
        {
            In_AttArray = MPC3_Att,
            InArray     = LookUpTableMPC3_half,
            MPC_array   = MPC3_Native,
            EdgeIndexes = MPC3LUTID,
            OutArray    = allpath3,
        };
        JobHandle jobHandle_path3Searc = path3Search.Schedule(LookUpTableMPC3_half.Length * MPC3_num, 64);
        jobHandle_path3Searc.Complete();
        #endregion

        #region Filtering Out Inactive Path3
        NativeList <int> indexes3_full     = new NativeList <int>(Allocator.TempJob);
        IndexFilterPath3 indexFilter3_full = new IndexFilterPath3
        {
            Array = allpath3,
        };
        JobHandle jobHandleFilterPath3 = indexFilter3_full.ScheduleAppend(indexes3_full, allpath3.Length, 64);
        jobHandleFilterPath3.Complete();


        LookUpTableMPC3 = new NativeArray <SeenPath3>(indexes3_full.Length, Allocator.Persistent);
        NativeArray <Vector3Int> LookUpTable_test          = new NativeArray <Vector3Int>(indexes3_full.Length, Allocator.TempJob);
        LookUpTableFinalPath3    lookUpTableFinalMPC3_full = new LookUpTableFinalPath3
        {
            InArray       = allpath3,
            IndexArray    = indexes3_full,
            OutArray      = LookUpTableMPC3,
            OutVector3Int = LookUpTable_test
        };
        JobHandle jobHandleLUTMPC3 = lookUpTableFinalMPC3_full.Schedule(indexes3_full.Length, 64);
        jobHandleLUTMPC3.Complete();

        /* for checking how many weak paths we have
         * int weak_path3 = 0;
         * for (int i = 0; i < LookUpTableMPC3_full.Length; i++)
         * {
         *  if (LookUpTableMPC3_full[i].AngularGain < 0.001)
         *  {
         *      weak_path3 += 1;
         *  }
         * }
         */
        #endregion

        #region Testing Function for Preparing Indexes for Full Path3

        //Vector2Int[] MPC3SeenID = new Vector2Int[MPC3_num];
        MPC3SeenID = new NativeArray <Vector2Int>(MPC3_num, Allocator.Persistent);
        int progressionSeenIndex = 0;
        for (int i = 0; i < MPC3_num; i++)
        {
            int index_min = progressionSeenIndex;
            int index_max = progressionSeenIndex;
            int flagIndex = 0; // if flagIndex = 1, then something is found
            for (int j = progressionSeenIndex; j < LookUpTableMPC3.Length; j++)
            {
                if (i == LookUpTableMPC3[j].MPC_IDs.x)
                {
                    index_max = j; flagIndex = 1;
                }
                else
                {
                    break;
                }
            }

            // check if the MPC can see others or not
            if (flagIndex == 1)
            {
                progressionSeenIndex = index_max + 1;
                MPC3SeenID[i]        = new Vector2Int(index_min, index_max);
                if (index_max - index_min + 1 > maxlengthMPC3)
                {
                    maxlengthMPC3 = index_max - index_min + 1;
                }
            } // in the case, it sees others
            else
            {
                MPC3SeenID[i] = new Vector2Int(-1, -1);
            } // in the case it doesn't see others
        }
        #endregion

        #region drawing MPC3 connections

        /*
         * //for (int i = 0; i < LookUpTableMPC3.Length; i++)
         * for (int i = 0; i < 100; i++)
         * {
         *  int first_MPC = LookUpTableMPC3[i].MPC_IDs.x;
         *  int second_MPC = LookUpTableMPC3[i].MPC_IDs.y;
         *  int third_MPC = LookUpTableMPC3[i].MPC_IDs.z;
         *
         *  Debug.DrawLine(MPC3_Native[first_MPC].Coordinates, MPC3_Native[second_MPC].Coordinates, Color.yellow, 1.0f);
         *  Debug.DrawLine(MPC3_Native[second_MPC].Coordinates + new Vector3(0,1,0), MPC3_Native[third_MPC].Coordinates + new Vector3(0, 1, 0), Color.red, 1.0f);
         * }*/

        #endregion
        // End: calculation of the whole path3s


        #region Disposing MPC3 NativeArrays
        LUTIndexArrayMPC3.Dispose();
        LookUpTableMPC3_half.Dispose();
        indexes3.Dispose();
        MPC3_ID.Dispose();
        CommandsNativeArray_MPC3.Dispose();
        ResultsNativeArray_MPC3.Dispose();
        allpath3_half.Dispose();
        allpath3.Dispose();
        MPC3LUTID.Dispose();
        indexes3_full.Dispose();
        LookUpTable_test.Dispose();
        #endregion

        #endregion
    }