Ejemplo n.º 1
0
        public bool Add(GridCell g_cell, bool trigger_nav_data_change)
        {
            if (g_cell == null || g_cell.Cells.Count == 0)
            {
                return(false);
            }

            bool add_to_list = true;

            using (new WriteLock(DataLock))
            {
                // check if same grid is not already defined (merge then)
                GridCell base_grid_cell = m_GridCells.Find(x => x.AABB.Equals(g_cell.AABB));

                if (base_grid_cell != null)
                {
                    add_to_list = false;
                    base_grid_cell.Add(g_cell.Cells);

                    Log("[Nav] Grid cell {" + g_cell.GlobalId + "} with " + g_cell.Cells.Count + " cell(s) merged with grid cell {" + base_grid_cell.GlobalId + "}");
                }

                m_AllCells.AddRange(g_cell.Cells);

                foreach (GridCell grid_cell in m_GridCells)
                {
                    grid_cell.AddNeighbour(g_cell);
                }

                if (add_to_list)
                {
                    m_GridCells.Add(g_cell);

                    Log("[Nav] Grid cell {" + g_cell.GlobalId + "} with " + g_cell.Cells.Count + " cell(s) added");
                }
            }

            if (Explorator != null)
            {
                Explorator.OnGridCellAdded(g_cell, trigger_nav_data_change);
            }

            Navigator.RequestPathUpdate();

            return(true);
        }
Ejemplo n.º 2
0
        public virtual void Clear()
        {
            using (new WriteLock(DataLock))
                using (new WriteLock(InputLock))
                {
                    m_CellsCache.Clear();
                    m_AllCells.Clear();
                    m_GridCells.Clear();
                    m_Regions.Clear();
                    m_CellsOverlappedByRegions.Clear();

                    m_LastGridCellId = 0;
                    m_LastCellId     = 0;
                }

            if (Explorator != null)
            {
                Explorator.Clear();
            }

            Navigator.Clear();

            Log("[Nav] Navmesh cleared!");
        }
Ejemplo n.º 3
0
        private void FetchSceneData()
        {
            List <SceneData> new_scene_data = new List <SceneData>();

            //using (new Nav.Profiler("[Nav.D3.Navigation] Navmesh data aquired [{t}]", 70))
            {
                foreach (Enigma.D3.Scene scene in m_Engine.ObjectManager.x998_Scenes.Dereference())
                {
                    if (scene == null || scene.x000_Id < 0)
                    {
                        continue;
                    }

                    SceneData scene_data = new SceneData(scene);

                    if (m_AllowedAreasSnoId.Count > 0 && !m_AllowedAreasSnoId.Contains(scene_data.AreaSnoId) && !m_AllowedGridCellsId.Contains(scene_data.SceneSnoId))
                    {
                        continue;
                    }

                    if (m_ProcessedSceneId.Contains(scene_data.SceneUid))
                    {
                        continue;
                    }

                    new_scene_data.Add(scene_data);
                }
            }

            //using (new Nav.Profiler("[Nav.D3.Navigation] Navmesh data added [{t}]"))
            {
                int grid_cells_added = 0;

                foreach (SceneData scene_data in new_scene_data)
                {
                    SceneSnoNavData sno_nav_data = null;

                    // allow empty grids
                    m_SnoCache.TryGetValue(scene_data.SceneSnoId, out sno_nav_data);

                    GridCell grid_cell = new GridCell(scene_data.Min, scene_data.Max, scene_data.SceneSnoId, scene_data.AreaSnoId);
                    grid_cell.UserData = scene_data.AreaSnoId;

                    if (sno_nav_data != null)
                    {
                        int cell_id = 0;

                        foreach (Cell cell in sno_nav_data.Cells)
                        {
                            grid_cell.Add(new Cell(cell.Min + scene_data.Min, cell.Max + scene_data.Min, cell.Flags, cell_id++));
                        }
                    }

                    if (Add(grid_cell, false))
                    {
                        ++grid_cells_added;
                    }

                    m_ProcessedSceneId.Add(scene_data.SceneUid);
                }

                if (grid_cells_added > 0)
                {
                    Log("[Nav.D3] " + grid_cells_added + " grid cells added" + (Explorator == null ? " (EXPLORATOR NOT PRESENT!!!)" : "") + ".");

                    if (Explorator != null)
                    {
                        Explorator.OnNavDataChange();
                    }
                }
            }
        }
Ejemplo n.º 4
0
        protected virtual void OnSerialize(BinaryWriter w)
        {
            using (new ReadLock(DataLock))
                using (new ReadLock(InputLock))
                {
                    // write all cells global IDs
                    w.Write(m_AllCells.Count);
                    foreach (Cell cell in m_AllCells)
                    {
                        w.Write(cell.GlobalId);
                    }

                    foreach (Cell cell in m_AllCells)
                    {
                        cell.Serialize(w);

                        foreach (var neighbour in cell.Neighbours)
                        {
                            if (m_AllCells.Find(x => x.GlobalId == neighbour.cell.GlobalId) == null)
                            {
                                Log("[Nav] Cell neighbour not on all cells list!");
                            }
                        }
                    }

                    w.Write(Cell.LastCellGlobalId);

                    // write all grid cells global IDs
                    w.Write(m_GridCells.Count);
                    foreach (GridCell grid_cell in m_GridCells)
                    {
                        w.Write(grid_cell.GlobalId);
                    }

                    foreach (GridCell grid_cell in m_GridCells)
                    {
                        grid_cell.Serialize(w);
                    }

                    w.Write(GridCell.LastGridCellGlobalId);

                    w.Write(m_Regions.Count);
                    foreach (region_data region in m_Regions)
                    {
                        region.Serialize(w);
                    }

                    w.Write(m_CellsOverlappedByRegions.Count);
                    foreach (var entry in m_CellsOverlappedByRegions)
                    {
                        w.Write(entry.Key);
                        entry.Value.Serialize(w);
                    }

                    Navigator.Serialize(w);

                    if (Explorator != null)
                    {
                        Explorator.Serialize(w);
                    }
                }

            Log("[Nav] Navmesh serialized.");
        }
Ejemplo n.º 5
0
        public bool Load(string filename, bool clear = true)
        {
            if (filename == null)
            {
                return(false);
            }

            //using (new Profiler("[Nav] Loaded nav data [{t}]"))
            {
                if (clear)
                {
                    Clear();
                }

                try
                {
                    StreamReader stream = new StreamReader(filename);

                    if (stream != null)
                    {
                        string   line;
                        GridCell g_cell                   = null;
                        Vec3     cell_shrink_size         = Vec3.Empty;
                        HashSet <region_data> avoid_areas = new HashSet <region_data>();

                        CultureInfo inv = (CultureInfo)CultureInfo.CurrentCulture.Clone();
                        inv.NumberFormat.CurrencyDecimalSeparator = ",";
                        inv.NumberFormat.NumberDecimalSeparator   = ",";

                        while ((line = stream.ReadLine()) != null)
                        {
                            string[] data = line.Split(' ');

                            if (data[0] == "g")
                            {
                                // add previous grid cell
                                Add(g_cell, false);

                                m_LastCellId = 0;

                                g_cell = new GridCell(float.Parse(data[1], inv), float.Parse(data[2], inv), float.Parse(data[3], inv), float.Parse(data[4], inv), float.Parse(data[5], inv), float.Parse(data[6], inv), (data.Length > 7 ? int.Parse(data[7]) : m_LastGridCellId++));
                            }
                            else if (data[0] == "n")
                            {
                                MovementFlag flags = MovementFlag.Walk | MovementFlag.Fly;

                                if (data.Length > 7)
                                {
                                    flags = (MovementFlag)int.Parse(data[7]);
                                }

                                Cell n_cell = new Cell(new Vec3(float.Parse(data[1], inv), float.Parse(data[2], inv), float.Parse(data[3], inv)) - cell_shrink_size, new Vec3(float.Parse(data[4], inv), float.Parse(data[5], inv), float.Parse(data[6], inv)) - cell_shrink_size, flags, m_LastCellId++);
                                g_cell.Add(n_cell);
                            }
                            else if (data[0] == "r")
                            {
                                avoid_areas.Add(new region_data(new AABB(float.Parse(data[1], inv), float.Parse(data[2], inv), float.Parse(data[3], inv), float.Parse(data[4], inv), float.Parse(data[5], inv), float.Parse(data[6], inv)), float.Parse(data[7], inv)));
                            }
                            else if (data[0] == "s")
                            {
                                Navigator.CurrentPos = new Vec3(float.Parse(data[1], inv), float.Parse(data[2], inv), float.Parse(data[3], inv));
                            }
                            else if (data[0] == "e")
                            {
                                Navigator.Destination = new Vec3(float.Parse(data[1], inv), float.Parse(data[2], inv), float.Parse(data[3], inv));
                            }
                        }

                        // add last grid cell
                        Add(g_cell, false);

                        Random   rng = new Random();
                        GridCell random_grid_cell = null;

                        random_grid_cell = m_GridCells[rng.Next(m_GridCells.Count)];

                        if (Explorator != null)
                        {
                            Explorator.OnNavDataChange();
                        }

                        Regions = avoid_areas;

                        Log("[Nav] Navmesh loaded.");

                        stream.Close();

                        return(true);
                    }
                }
                catch (Exception)
                {
                }

                Log("[Nav] Navmesh load failed!");

                return(false);
            }
        }
Ejemplo n.º 6
0
        protected virtual void OnDeserialize(BinaryReader r)
        {
            using (new WriteLock(DataLock))
                using (new WriteLock(InputLock))
                {
                    //using (new Profiler("Navmesh deserialization took {t}"))
                    {
                        m_AllCells.Clear();
                        m_GridCells.Clear();
                        m_Regions.Clear();
                        m_CellsOverlappedByRegions.Clear();

                        Cell.CompareByGlobalId comp_by_global_id = new Cell.CompareByGlobalId();

                        int all_cells_count = r.ReadInt32();

                        // pre-allocate cells
                        for (int i = 0; i < all_cells_count; ++i)
                        {
                            Cell cell = new Cell(0, 0, 0, 0, 0, 0, MovementFlag.None);
                            cell.GlobalId = r.ReadInt32();
                            m_AllCells.Add(cell);
                        }

                        m_AllCells.Sort(comp_by_global_id);

                        foreach (Cell cell in m_AllCells)
                        {
                            cell.Deserialize(m_AllCells, r);
                        }

                        Cell.LastCellGlobalId = r.ReadInt32();

                        int grid_cells_count = r.ReadInt32();

                        // pre-allocate grid cells
                        for (int i = 0; i < grid_cells_count; ++i)
                        {
                            GridCell grid_cell = new GridCell(0, 0, 0, 0, 0, 0);
                            grid_cell.GlobalId = r.ReadInt32();
                            m_GridCells.Add(grid_cell);
                        }

                        m_GridCells.Sort(comp_by_global_id);

                        foreach (GridCell grid_cell in m_GridCells)
                        {
                            grid_cell.Deserialize(m_GridCells, m_AllCells, r);
                        }

                        GridCell.LastGridCellGlobalId = r.ReadInt32();

                        int regions_count = r.ReadInt32();
                        for (int i = 0; i < regions_count; ++i)
                        {
                            m_Regions.Add(new region_data(r));
                        }

                        int cells_overlapped_by_regions_count = r.ReadInt32();
                        for (int i = 0; i < cells_overlapped_by_regions_count; ++i)
                        {
                            int key = r.ReadInt32();
                            m_CellsOverlappedByRegions.Add(key, new overlapped_cell_data(m_AllCells, r));
                        }

                        Navigator.Deserialize(m_AllCells, r);

                        if (Explorator != null)
                        {
                            Explorator.Deserialize(m_AllCells, r);
                        }
                    }
                }

            Log("[Nav] Navmesh deserialized.");
        }