Exemplo n.º 1
0
        // Everything inside this method is rendered with transformation resulting from ModifyRenderMatrix.
        protected virtual void OnRenderData(PaintEventArgs e)
        {
            try
            {
                int cells_count      = 0;
                int grid_cells_count = 0;

                if (m_RenderGrids || m_RenderCells)
                {
                    using (m_Navmesh.AcquireReadDataLock())
                    {
                        var grid_cells = m_Navmesh.dbg_GetGridCells();

                        if (m_RenderGrids)
                        {
                            foreach (Nav.GridCell grid_cell in grid_cells)
                            {
                                RenderHelper.Render(grid_cell, m_RenderCenter, e, m_RenderConnections, m_RenderIds);
                            }

                            grid_cells_count = grid_cells.Count;
                        }

                        if (m_RenderCells)
                        {
                            float max_move_cost_mult = 1;

                            foreach (Nav.GridCell grid_cell in grid_cells)
                            {
                                foreach (Nav.Cell cell in grid_cell.GetCells())
                                {
                                    RenderHelper.Render(cell, m_RenderCenter, e, m_RenderConnections, m_RenderIds, m_LastMaxMoveCostMult);
                                    max_move_cost_mult = Math.Max(max_move_cost_mult, cell.MovementCostMult);
                                }

                                cells_count += grid_cell.GetCellsCount();
                            }

                            m_LastMaxMoveCostMult = max_move_cost_mult;
                        }
                    }
                }

                if (m_RenderExploreCells || m_RenderExploreArea)
                {
                    using (m_Explorer.AquireReadDataLock())
                    {
                        var explore_cells = m_Explorer.dbg_GetExploreCells();

                        foreach (Nav.ExploreCell explore_cell in explore_cells)
                        {
                            RenderHelper.Render(explore_cell, m_Explorer.ExploreDestPrecision, m_RenderCenter, e, m_RenderConnections, m_RenderIds);

                            if (m_RenderExploreArea)
                            {
                                RenderHelper.DrawString(e.Graphics, Brushes.Black, m_RenderCenter, explore_cell.Position, explore_cell.CellsArea().ToString(), 5);
                            }
                        }
                    }
                }

                if (m_RenderRegions)
                {
                    var regions = m_Navmesh.Regions;

                    foreach (var region in regions)
                    {
                        RenderHelper.DrawRectangle(e.Graphics, Pens.Black, m_RenderCenter, region.area.Min, region.area.Max);
                    }

                    //Vec3 safe_point = m_Navigator.GetNearestGridCellOutsideAvoidAreas();

                    //if (!safe_point.IsEmpty)
                    //    RenderHelper.DrawPoint(e.Graphics, Pens.Green, render_center, safe_point);
                }

                if (m_RenderAxis)
                {
                    e.Graphics.DrawString("X", new Font("Arial", 6 / m_RenderScale), Brushes.Black, 25 / m_RenderScale, 0);
                    e.Graphics.DrawLine(RenderHelper.AXIS_PEN, -25 / m_RenderScale, 0, 25 / m_RenderScale, 0);
                    e.Graphics.DrawString("Y", new Font("Arial", 6 / m_RenderScale), Brushes.Black, 0, 25 / m_RenderScale);
                    e.Graphics.DrawLine(RenderHelper.AXIS_PEN, 0, -25 / m_RenderScale, 0, 25 / m_RenderScale);
                }

                if (!m_RenderOriginalPath && m_RenderPath)
                {
                    DestType last_path_dest_type = DestType.None;
                    if (m_Navigator.TryGetPath(ref m_LastPath, ref last_path_dest_type))
                    {
                        m_LastPath.Insert(0, m_Navigator.CurrentPos);
                    }
                    RenderHelper.DrawLines(e.Graphics, RenderHelper.PATH_PEN, m_RenderCenter, m_LastPath, 1);
                }

                if (m_RenderBacktrackPath)
                {
                    if (m_Navigator.TryGetBackTrackPath(ref m_LastBacktrackPath))
                    {
                        m_LastBacktrackPath.Insert(0, m_Navigator.CurrentPos);
                    }
                    RenderHelper.DrawLines(e.Graphics, Pens.Blue, m_RenderCenter, m_LastBacktrackPath, 1);
                }

                if (m_RenderPositionsHistory)
                {
                    m_Navigator.TryGetDebugPositionsHistory(ref m_LastPositionsHistory);
                    RenderHelper.DrawLines(e.Graphics, Pens.Green, m_RenderCenter, m_LastPositionsHistory, 1);
                }

                if (!m_Navigator.CurrentPos.IsZero())
                {
                    RenderHelper.DrawPoint(e.Graphics, Pens.Blue, m_RenderCenter, m_Navigator.CurrentPos);
                }
                if (!m_Navigator.Destination.IsZero())
                {
                    RenderHelper.DrawPoint(e.Graphics, Pens.LightBlue, m_RenderCenter, m_Navigator.Destination);
                }

                {
                    Vec3 curr = m_Navigator.CurrentPos;
                    Vec3 dest = m_Navigator.Destination;

                    if (!curr.IsZero() && !dest.IsZero())
                    {
                        if (m_RenderOriginalPath)
                        {
                            List <Vec3> path = new List <Vec3>();
                            m_Navigator.FindPath(curr, dest, MovementFlag.Walk, ref path, -1, false, false, 0, false, 0, false);
                            path.Insert(0, curr);
                            RenderHelper.DrawLines(e.Graphics, Pens.Black, m_RenderCenter, path, 1);
                        }

                        if (m_RenderRayCast)
                        {
                            Vec3 intersection = default(Vec3);
                            RenderHelper.DrawLine(e.Graphics, m_Navmesh.RayCast2D(curr, dest, MovementFlag.Walk, ref intersection) ? Pens.Green : Pens.Red, m_RenderCenter, curr, intersection);
                        }
                    }
                }

                var waypoints = m_Navigator.Waypoints;
                if (waypoints.Count > 0)
                {
                    RenderHelper.DrawLines(e.Graphics, Pens.Red, m_RenderCenter, waypoints, 1);
                }

                if (m_Bot != null)
                {
                    //if (!m_Bot.Paused && m_CenterOnBot)
                    //    m_RenderCenter = new PointF(m_Bot.Position.X, m_Bot.Position.Y);
                    m_Bot.Render(e.Graphics, m_RenderCenter);
                }
            }
            catch (Exception)
            {
            }
        }
Exemplo n.º 2
0
        private void Render(object sender, PaintEventArgs e)
        {
            try
            {
                int location = -1;

                if (engine != null)
                {
                    LevelArea level_area = Engine.Current.LevelArea;
                    if (level_area != null)
                    {
                        location = level_area.x044_SnoId;
                    }
                }

                if (last_location != location)
                {
                    if (auto_clear_navmesh)
                    {
                        m_Navmesh.Clear();
                        LoadDebugConfig();
                    }
                    last_location = location;
                }

                Matrix m = new Matrix();
                m.Scale(render_scale, render_scale);
                m.Translate((Width - 16) / (2 * render_scale), (Height - 30) / (2 * render_scale));

                // when Diablo is running display navmesh in the same manner as Diablo does
                if (engine != null)
                {
                    m.Rotate(135);
                    Matrix flip_x_m = new Matrix(1, 0, 0, -1, 0, 0);
                    m.Multiply(flip_x_m);
                }

                e.Graphics.Transform          = m;
                e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected;
                e.Graphics.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                int cells_count      = 0;
                int grid_cells_count = 0;

                if (render_grids || render_cells)
                {
                    using (m_Navmesh.AquireReadDataLock())
                    {
                        List <GridCell> grid_cells = m_Navmesh.dbg_GetGridCells();

                        if (render_grids)
                        {
                            foreach (Nav.GridCell grid_cell in grid_cells)
                            {
                                RenderHelper.Render(grid_cell, render_center, e, render_connections, render_id);
                            }

                            grid_cells_count = grid_cells.Count;
                        }

                        if (render_cells)
                        {
                            float max_move_cost_mult = 1;

                            foreach (Nav.GridCell grid_cell in grid_cells)
                            {
                                foreach (Nav.Cell cell in grid_cell.Cells)
                                {
                                    RenderHelper.Render(cell, render_center, e, render_connections, render_id, m_LastMaxMoveCostMult);
                                    max_move_cost_mult = Math.Max(max_move_cost_mult, cell.MovementCostMult);
                                }

                                cells_count += grid_cell.Cells.Count;
                            }

                            m_LastMaxMoveCostMult = max_move_cost_mult;
                        }
                    }
                }

                if (render_explore_cells || render_explore_dist)
                {
                    using (m_Navmesh.Explorator.AquireReadDataLock())
                    {
                        List <ExploreCell> explore_cells = m_Navmesh.Explorator.dbg_GetExploreCells();

                        if (render_explore_cells)
                        {
                            foreach (Nav.ExploreCell explore_cell in explore_cells)
                            {
                                RenderHelper.Render(explore_cell, m_Navmesh.Navigator.ExploreCellPrecision, render_center, e, render_connections, render_id);
                            }
                        }

                        if (render_explore_dist)
                        {
                            if (explore_cells.Exists(c => c.Id == explore_cell_id_to_render_dists))
                            {
                                RenderHelper.Render(m_Navmesh, explore_cells.Find(c => c.Id == explore_cell_id_to_render_dists), render_center, e, render_id);
                            }
                        }
                    }
                }

                if (render_regions)
                {
                    var regions = m_Navmesh.Regions;

                    foreach (var region in regions)
                    {
                        RenderHelper.DrawRectangle(e.Graphics, Pens.Black, render_center, region.area.Min, region.area.Max);
                    }

                    //Vec3 safe_point = m_Navmesh.Navigator.GetNearestGridCellOutsideAvoidAreas();

                    //if (!safe_point.IsEmpty)
                    //    RenderHelper.DrawPoint(e.Graphics, Pens.Green, render_center, safe_point);
                }

                if (render_axis)
                {
                    e.Graphics.DrawString("X", new Font("Arial", 6 / render_scale), Brushes.Black, 25 / render_scale, 0);
                    e.Graphics.DrawLine(RenderHelper.AXIS_PEN, -25 / render_scale, 0, 25 / render_scale, 0);
                    e.Graphics.DrawString("Y", new Font("Arial", 6 / render_scale), Brushes.Black, 0, 25 / render_scale);
                    e.Graphics.DrawLine(RenderHelper.AXIS_PEN, 0, -25 / render_scale, 0, 25 / render_scale);
                }

                if (render_explore_cells && m_Navmesh.Explorator is Nav.ExploreEngine.TSP)
                {
                    ((Nav.ExploreEngine.TSP)m_Navmesh.Explorator).TryGetExplorePath(ref last_explore_path);
                    RenderHelper.DrawLines(e.Graphics, RenderHelper.EXPLORE_PATH_PEN, render_center, last_explore_path, 1);
                }

                if (!render_original_path && render_path)
                {
                    DestType last_path_dest_type = DestType.None;
                    if (m_Navmesh.Navigator.TryGetPath(ref last_path, ref last_path_dest_type))
                    {
                        last_path.Insert(0, m_Navmesh.Navigator.CurrentPos);
                    }
                    RenderHelper.DrawLines(e.Graphics, RenderHelper.PATH_PEN, render_center, last_path, 1);
                }

                if (render_backtrack_path)
                {
                    if (m_Navmesh.Navigator.TryGetBackTrackPath(ref last_back_track_path))
                    {
                        last_back_track_path.Insert(0, m_Navmesh.Navigator.CurrentPos);
                    }
                    RenderHelper.DrawLines(e.Graphics, Pens.Blue, render_center, last_back_track_path, 1);
                }

                if (render_positions_history)
                {
                    m_Navmesh.Navigator.TryGetDebugPositionsHistory(ref last_positions_history);
                    RenderHelper.DrawLines(e.Graphics, Pens.Green, render_center, last_positions_history, 1);
                }

                if (!m_Navmesh.Navigator.CurrentPos.IsEmpty)
                {
                    RenderHelper.DrawPoint(e.Graphics, Pens.Blue, render_center, m_Navmesh.Navigator.CurrentPos);
                }
                if (!m_Navmesh.Navigator.Destination.IsEmpty)
                {
                    RenderHelper.DrawPoint(e.Graphics, Pens.LightBlue, render_center, m_Navmesh.Navigator.Destination);
                }

                {
                    Vec3 curr = m_Navmesh.Navigator.CurrentPos;
                    Vec3 dest = m_Navmesh.Navigator.Destination;

                    if (!curr.IsEmpty && !dest.IsEmpty)
                    {
                        if (render_original_path)
                        {
                            List <Vec3> path = new List <Vec3>();
                            m_Navmesh.Navigator.FindPath(curr, dest, MovementFlag.Walk, ref path, -1, false, false, 0, false, 0, false);
                            path.Insert(0, curr);
                            RenderHelper.DrawLines(e.Graphics, Pens.Black, render_center, path, 1);
                        }

                        if (render_ray_cast)
                        {
                            RenderHelper.DrawLine(e.Graphics, m_Navmesh.RayCast2D(curr, dest, MovementFlag.Walk) ? Pens.Green : Pens.Red, render_center, curr, dest);
                        }
                    }
                }

                if (waypoints_paths.Count > 0)
                {
                    int waypoint_id = 1;
                    foreach (List <Vec3> p in waypoints_paths)
                    {
                        if (p.Count > 0)
                        {
                            RenderHelper.DrawCircle(e.Graphics, Pens.Black, render_center, p[0], 3);
                            RenderHelper.DrawString(e.Graphics, Brushes.Black, render_center, p[0], waypoint_id.ToString(), 10);
                        }
                        RenderHelper.DrawLines(e.Graphics, Pens.Red, render_center, p, 1);
                        ++waypoint_id;
                    }
                }

                if (bot != null)
                {
                    if (!bot.Paused && center_on_bot)
                    {
                        render_center = new PointF(bot.Position.X, bot.Position.Y);
                    }
                    bot.Render(e.Graphics, render_center);
                }

                e.Graphics.ResetTransform();

                Font legend_font = new Font("Arial", 8, FontStyle.Bold);
                Font stats_font  = new Font("Arial", 8);

                TextRenderer.DrawText(e.Graphics, "L: Toggle render legend", legend_font, new Point(10, 10), render_legend ? Color.White : Color.Black, render_legend ? Color.Black : Color.Transparent);

                if (render_legend)
                {
                    e.Graphics.DrawString("F1: Reload waypoints", legend_font, Brushes.Black, 10, 25);
                    e.Graphics.DrawString("F2: Reload nav data", legend_font, Brushes.Black, 10, 40);
                    e.Graphics.DrawString("F3: Dump nav data", legend_font, Brushes.Black, 10, 55);
                    e.Graphics.DrawString("F4: Clear nav data", legend_font, Brushes.Black, 10, 70);
                    e.Graphics.DrawString("F5: Serialize nav data", legend_font, Brushes.Black, 10, 85);
                    e.Graphics.DrawString("F6: Deserialize nav data", legend_font, Brushes.Black, 10, 100);
                    e.Graphics.DrawString("F10: Activate some test", legend_font, Brushes.Black, 10, 115);
                    TextRenderer.DrawText(e.Graphics, "1: Toggle render grid cells", legend_font, new Point(10, 130), render_grids ? Color.White : Color.Black, render_grids ? Color.Black : Color.Transparent);
                    TextRenderer.DrawText(e.Graphics, "2: Toggle render cells", legend_font, new Point(10, 145), render_cells ? Color.White : Color.Black, render_cells ? Color.Black : Color.Transparent);
                    TextRenderer.DrawText(e.Graphics, "3: Toggle render explore cells", legend_font, new Point(10, 160), render_explore_cells ? Color.White : Color.Black, render_explore_cells ? Color.Black : Color.Transparent);
                    TextRenderer.DrawText(e.Graphics, "4: Toggle render connections", legend_font, new Point(10, 175), render_connections ? Color.White : Color.Black, render_connections ? Color.Black : Color.Transparent);
                    TextRenderer.DrawText(e.Graphics, "5: Toggle render IDs", legend_font, new Point(10, 190), render_id ? Color.White : Color.Black, render_id ? Color.Black : Color.Transparent);
                    TextRenderer.DrawText(e.Graphics, "6: Toggle render axis", legend_font, new Point(10, 205), render_axis ? Color.White : Color.Black, render_axis ? Color.Black : Color.Transparent);
                    TextRenderer.DrawText(e.Graphics, "7: Toggle render regions", legend_font, new Point(10, 220), render_regions ? Color.White : Color.Black, render_regions ? Color.Black : Color.Transparent);
                    TextRenderer.DrawText(e.Graphics, "8: Toggle render original path", legend_font, new Point(10, 235), render_original_path ? Color.White : Color.Black, render_original_path ? Color.Black : Color.Transparent);
                    TextRenderer.DrawText(e.Graphics, "9: Toggle render ray cast", legend_font, new Point(10, 250), render_ray_cast ? Color.White : Color.Black, render_ray_cast ? Color.Black : Color.Transparent);
                    TextRenderer.DrawText(e.Graphics, "0: Toggle render back track path", legend_font, new Point(10, 265), render_backtrack_path ? Color.White : Color.Black, render_backtrack_path ? Color.Black : Color.Transparent);
                    e.Graphics.DrawString("S: Set current pos", legend_font, Brushes.Black, 10, 280);
                    e.Graphics.DrawString("E: Set destination pos", legend_font, Brushes.Black, 10, 295);
                    e.Graphics.DrawString("B: Run bot", legend_font, Brushes.Black, 10, 310);
                    TextRenderer.DrawText(e.Graphics, "A: Toggle auto clear navmesh", legend_font, new Point(10, 325), auto_clear_navmesh ? Color.White : Color.Black, auto_clear_navmesh ? Color.Black : Color.Transparent);
                    e.Graphics.DrawString("F7: Reload debug.ini", legend_font, Brushes.Black, 10, 340);
                    TextRenderer.DrawText(e.Graphics, "Ctrl+1: Toggle render path", legend_font, new Point(10, 355), render_path ? Color.White : Color.Black, render_path ? Color.Black : Color.Transparent);
                    TextRenderer.DrawText(e.Graphics, "Ctrl+2: Toggle regions", legend_font, new Point(10, 370), m_Navmesh.RegionsEnabled ? Color.White : Color.Black, m_Navmesh.RegionsEnabled ? Color.Black : Color.Transparent);
                    TextRenderer.DrawText(e.Graphics, "Ctrl+3: Toggle danger regions", legend_font, new Point(10, 385), m_Navmesh.DangerRegionsEnabled ? Color.White : Color.Black, m_Navmesh.DangerRegionsEnabled ? Color.Black : Color.Transparent);
                    TextRenderer.DrawText(e.Graphics, "Ctrl+4: Toggle render positions history", legend_font, new Point(10, 400), render_positions_history ? Color.White : Color.Black, render_positions_history ? Color.Black : Color.Transparent);
                }

                e.Graphics.DrawString("Cells count: " + cells_count, stats_font, Brushes.Black, 10, Height - 55);
            }
            catch (Exception)
            {
            }
        }