请选择 进入手机版 | 继续访问电脑版

lomcn

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 272|回复: 2

自动寻路卡顿

[复制链接]
  • TA的每日心情
    开心
    2022-8-5 09:24
  • 1

    主题

    7

    回帖

    77

    积分

    注册会员

    积分
    77
    金钱
    69
    元宝
    10
    发表于 2022-7-13 14:28:42 | 显示全部楼层 |阅读模式
    本帖最后由 feijimj 于 2022-7-13 14:32 编辑

    根据龙哥寻路贴子LOMCN论坛寻路算法转载

    代码本身是传2的代码,但是都是吉米的所以挪过来用是可以的,只需代码轻微改动,然后我就把带弄到我的原版代码里了!
    因为龙已经在教程里说得很详细了,所以在这里就不赘述了!
    这里要说的是照搬过来以后出现的寻路过程中出现卡顿的问题,群里茶密大佬告诉我是移动的格子和位置不统一的问题,可是我昨天下午debug了一下午也没看出问题来~!
    下面说说改动的文件,希望大佬帮我分析一下
    PathFinder.cs
    修改了调用的类库,其他未做更改
    BigMapDialog.cs
    地图添加Image_MouseClick,Image_MouseClick事件代码,鼠标右键触发事件
    1. private void Image_MouseClick(object sender, MouseEventArgs e)
    2.         {

    3.             //if (SelectedInfo != GameScene.Game.MapControl.MapInfo) return;

    4.             //if (MapObject.User.Buffs.All(z => z.Type != BuffType.Developer))
    5.             //if (!SelectedInfo.AllowRT || !SelectedInfo.AllowTT || !GameScene.Game.MapControl.MapInfo.AllowRT || !GameScene.Game.MapControl.MapInfo.AllowTT) return;


    6.             if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
    7.             {
    8.                 //TODO Teleport Ring
    9.                 int x = (int)((e.Location.X - Image.DisplayArea.X) / ScaleX);
    10.                 int y = (int)((e.Location.Y - Image.DisplayArea.Y) / ScaleY);

    11.                 //CEnvir.Enqueue(new C.TeleportRing { Location = new Point(x, y), Index = SelectedInfo.Index });
    12.                
    13.                 var path = GameScene.Game.MapControl.PathFinder.FindPath(MapObject.User.CurrentLocation, new Point(x, y));

    14.                 if (path == null || path.Count == 0)
    15.                 {
    16.                     GameScene.Game.ReceiveChat("寻路失败,请再试...", MessageType.System);
    17.                     GameScene.Game.MapControl.AutoPath = false;
    18.                 }
    19.                 else
    20.                 {
    21.                     GameScene.Game.MapControl.CurrentPath = path;
    22.                     GameScene.Game.MapControl.AutoPath = true;
    23.                 }
    24.             }
    25.         }
    复制代码

    Draw部分和龙哥贴里一样

    MapControl.cs
    在public void ProcessInput()方法中添加
    1. if (AutoPath)
    2.             {
    3.                 if (CurrentPath == null || CurrentPath.Count == 0)
    4.                 {
    5.                     AutoPath = false;
    6.                     return;
    7.                 }
    8.                 //var path = GameScene.Game.MapControl.PathFinder.FindPath(MapObject.User.CurrentLocation, new Point(x, y));
    9.                 var path = GameScene.Game.MapControl.PathFinder.FindPath(MapObject.User.CurrentLocation, CurrentPath.Last().Location);
    10.                
    11.                 if (path != null && path.Count > 0)
    12.                     GameScene.Game.MapControl.CurrentPath = path;
    13.                 else
    14.                 {
    15.                     AutoPath = false;
    16.                     return;
    17.                 }

    18.                 Node currentNode = CurrentPath.SingleOrDefault(x => User.CurrentLocation == x.Location);
    19.                 if (currentNode != null)
    20.                 {
    21.                     while (true)
    22.                     {
    23.                         Node first = CurrentPath.First();
    24.                         CurrentPath.Remove(first);

    25.                         if (first == currentNode)
    26.                             break;
    27.                     }
    28.                 }

    29.                 if (CurrentPath.Count > 0)
    30.                 {
    31.                     MirDirection dir = Functions.DirectionFromPoint(User.CurrentLocation, CurrentPath.First().Location);
    32.                     //EEOO Edit FindPath-Move
    33.                     
    34.                     int steps = 1;

    35.                     if (GameScene.Game.CanRun
    36.                         && CEnvir.Now >= User.NextRunTime
    37.                         && User.BagWeight <= User.Stats[Stat.BagWeight]
    38.                         && User.WearWeight <= User.Stats[Stat.WearWeight]
    39.                         && (Math.Abs(User.CurrentLocation.X- CurrentPath.Last().Location.X)>1
    40.                         || Math.Abs(User.CurrentLocation.Y - CurrentPath.Last().Location.Y)>1))
    41.                     {
    42.                         steps++;
    43.                         if (User.Horse != HorseType.None)
    44.                             steps++;
    45.                     }

    46.                     for (int i = 1; i <= steps; i++)
    47.                     {
    48.                         if (CanMove(dir, i)) continue;

    49.                         MirDirection best1 = MouseDirectionBest(dir, 1);

    50.                         if (best1 == dir)
    51.                         {
    52.                             if (i == 1)
    53.                             {
    54.                                 if (dir != User.Direction)
    55.                                     MapObject.User.AttemptAction(new ObjectAction(MirAction.Standing, dir, MapObject.User.CurrentLocation));
    56.                                 return;
    57.                             }

    58.                             steps = i - 1;
    59.                         }
    60.                         else
    61.                         {
    62.                             steps = 1;
    63.                         }
    64.                         dir = best1;
    65.                         break;
    66.                     }

    67.                     MapObject.User.AttemptAction(new ObjectAction(MirAction.Moving, dir, Functions.Move(MapObject.User.CurrentLocation, dir, steps), steps, MagicType.None));
    68.                     
    69.                 }
    70.             }
    复制代码

    修改了寻路部分,在每次中重新设置路径,然后遇到怪物和障碍物时,能自动避让,但是问题就是卡顿?求大佬帮忙分析一下~!

    需要贴啥代码您说...我贴出来!
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2022-8-5 09:24
  • 1

    主题

    7

    回帖

    77

    积分

    注册会员

    积分
    77
    金钱
    69
    元宝
    10
     楼主| 发表于 2022-7-13 14:30:41 | 显示全部楼层
    1. if (AutoPath)
    2.             {
    3.                 if (CurrentPath == null || CurrentPath.Count == 0)
    4.                 {
    5.                     AutoPath = false;
    6.                     return;
    7.                 }
    8.                 //var path = GameScene.Game.MapControl.PathFinder.FindPath(MapObject.User.CurrentLocation, new Point(x, y));
    9.                 var path = GameScene.Game.MapControl.PathFinder.FindPath(MapObject.User.CurrentLocation, CurrentPath.Last().Location);
    10.                
    11.                 if (path != null && path.Count > 0)
    12.                     GameScene.Game.MapControl.CurrentPath = path;
    13.                 else
    14.                 {
    15.                     AutoPath = false;
    16.                     return;
    17.                 }

    18.                 Node currentNode = CurrentPath.SingleOrDefault(x => User.CurrentLocation == x.Location);
    19.                 if (currentNode != null)
    20.                 {
    21.                     while (true)
    22.                     {
    23.                         Node first = CurrentPath.First();
    24.                         CurrentPath.Remove(first);

    25.                         if (first == currentNode)
    26.                             break;
    27.                     }
    28.                 }

    29.                 if (CurrentPath.Count > 0)
    30.                 {
    31.                     MirDirection dir = Functions.DirectionFromPoint(User.CurrentLocation, CurrentPath.First().Location);
    32.                     //EEOO Edit FindPath-Move
    33.                     
    34.                     int steps = 1;

    35.                     if (GameScene.Game.CanRun
    36.                         && CEnvir.Now >= User.NextRunTime
    37.                         && User.BagWeight <= User.Stats[Stat.BagWeight]
    38.                         && User.WearWeight <= User.Stats[Stat.WearWeight]
    39.                         && (Math.Abs(User.CurrentLocation.X- CurrentPath.Last().Location.X)>1
    40.                         || Math.Abs(User.CurrentLocation.Y - CurrentPath.Last().Location.Y)>1))
    41.                     {
    42.                         steps++;
    43.                         if (User.Horse != HorseType.None)
    44.                             steps++;
    45.                     }

    46.                     for (int i = 1; i <= steps; i++)
    47.                     {
    48.                         if (CanMove(dir, i)) continue;

    49.                         MirDirection best1 = MouseDirectionBest(dir, 1);

    50.                         if (best1 == dir)
    51.                         {
    52.                             if (i == 1)
    53.                             {
    54.                                 if (dir != User.Direction)
    55.                                     MapObject.User.AttemptAction(new ObjectAction(MirAction.Standing, dir, MapObject.User.CurrentLocation));
    56.                                 return;
    57.                             }

    58.                             steps = i - 1;
    59.                         }
    60.                         else
    61.                         {
    62.                             steps = 1;
    63.                         }
    64.                         dir = best1;
    65.                         break;
    66.                     }

    67.                     MapObject.User.AttemptAction(new ObjectAction(MirAction.Moving, dir, Functions.Move(MapObject.User.CurrentLocation, dir, steps), steps, MagicType.None));
    68.                     
    69.                 }
    70.             }
    复制代码
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2022-8-5 09:24
  • 1

    主题

    7

    回帖

    77

    积分

    注册会员

    积分
    77
    金钱
    69
    元宝
    10
     楼主| 发表于 2022-7-13 14:31:55 | 显示全部楼层
    1. private void Image_MouseClick(object sender, MouseEventArgs e)
    2.         {

    3.             //if (SelectedInfo != GameScene.Game.MapControl.MapInfo) return;

    4.             //if (MapObject.User.Buffs.All(z => z.Type != BuffType.Developer))
    5.             //if (!SelectedInfo.AllowRT || !SelectedInfo.AllowTT || !GameScene.Game.MapControl.MapInfo.AllowRT || !GameScene.Game.MapControl.MapInfo.AllowTT) return;


    6.             if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
    7.             {
    8.                 //TODO Teleport Ring
    9.                 int x = (int)((e.Location.X - Image.DisplayArea.X) / ScaleX);
    10.                 int y = (int)((e.Location.Y - Image.DisplayArea.Y) / ScaleY);

    11.                 //CEnvir.Enqueue(new C.TeleportRing { Location = new Point(x, y), Index = SelectedInfo.Index });
    12.                
    13.                 var path = GameScene.Game.MapControl.PathFinder.FindPath(MapObject.User.CurrentLocation, new Point(x, y));

    14.                 if (path == null || path.Count == 0)
    15.                 {
    16.                     GameScene.Game.ReceiveChat("寻路失败,请再试...", MessageType.System);
    17.                     GameScene.Game.MapControl.AutoPath = false;
    18.                 }
    19.                 else
    20.                 {
    21.                     GameScene.Game.MapControl.CurrentPath = path;
    22.                     GameScene.Game.MapControl.AutoPath = true;
    23.                 }
    24.             }
    25.         }
    复制代码
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|服务|手机版|小黑屋|lomcn

    GMT+8, 2023-3-29 20:37 , Processed in 0.121169 second(s), 20 queries .

    Powered by Discuz! X3.5

    © 2001-2023 Discuz! Team.

    快速回复 返回顶部 返回列表