找回密码
 注册
搜索
查看: 313|回复: 6

分享我的定位系统!

[复制链接]
门派:桃花岛
发表于 2023-7-1 10:39:39 | 显示全部楼层 |阅读模式
本帖最后由 xinghan@tj 于 2023-7-1 10:43 编辑

定位系统,首先肯定绕不开所谓的房间数据,我把mush中room.lua改成了c# 部分代码如下
  public class Room
    {
        public string id { get; set; }
        public string name { get; set; }
        public bool no_fight { get; set; }
        public string[,] ways { get; set; }
        public string room_relative { get; set; }
        public List<string> PathRoom { get; set; }
        public string Path { get; set; }
        public string[,] objs { get; set; }
        public string cityid { get; set; }
        public string cityname { get; set; }
    }
    public class Map
    {

        public static string[,] lookCitys = new string[8, 2] { { "city", "扬州城" }, { "baituo", "白驼山" }, { "beijing", "京城" }, { "cangzhou", "沧州城" }, { "xiangyang", "襄阳城" }, { "lzz", "柳宗镇" }, { "village", "华山村" }, { "huashan", "华山" }};
public  List<Room> Rooms = new List<Room> {
/***************************************白驼山**************************************/
new Room {
    id = "baituo/btshan",
    name = "白驼山",
    cityid="baituo",
    cityname="白驼山",
    ways =new string[2, 2]  {
       {"westup","baituo/shanmen"},
       {"east","hj/gebin"}
    },
},
  new Room {
        id = "baituo/chucang",
        name = "储藏室",
     cityid="baituo",
    cityname="白驼山",
    ways =new string[1, 2]  {
        {"east","baituo/kongdi"}
        },
},
new Room
{
    id = "baituo/fengxi",
        name = "缝隙",
     cityid="baituo",
    cityname="白驼山",
    ways =new string[2, 2]  {
       {"north","baituo/fengxi1"},
                {"out","baituo/yuanzi2"}
        },
},
new Room
{
    id = "baituo/fengxi1",
        name = "缝隙",
     cityid="baituo",
    cityname="白驼山",
    ways =new string[3, 2]  {
       {"south","baituo/fengxi"},
                {"west","baituo/fengxi2"},
                {"out","baituo/yuanzi3"}
        },
},
new Room
{
    id = "baituo/fengxi2",
        name = "缝隙",
     cityid="baituo",
    cityname="白驼山",
    ways =new string[2, 2]  {
       {"east","baituo/fengxi1"},
                {"out","baituo/yuanzi4"}
        },
},
........................
有了房间数据,下来就是定位自己所在的房间,和目标房间,计算出行走的路径,然后进行行走
public static bool sfzd = false;
        public static string zdlj = "";
        public static string state = "";
        public static string statebz = "";
        public static void dujiang_trigger(string strData)
        {
            if (state == "渡江")
            {
                if (common.delColorStrng(strData).IndexOf("一艘渡船缓缓地驶了过来,艄公将一块踏脚板搭上堤岸,以便乘客上下") > -1)
                {

                    common.sendMsg("enter");
                }
                if (common.delColorStrng(strData).IndexOf("渡船猛地一震,已经靠岸") > -1)
                {
                    common.sendMsg("out");
                    state = "";
                    zdljs.RemoveAt(0);
                    tGo.Resume();
                }

            }


        }
        public static void locate_trigger(string strData)
        {

            if (state == "开始定位" && strData.IndexOf("【你现在正处于") != -1)
            {
                bool dwok = false;
                string localroomname = "";
                string localroom_relative = "";
                string localcityname = "";
                string localcityid = "";
                string localroomid = "";
                string regexContext = @"^【你现在正处于(.+)】\\n(.+)-";
                if (Regex.Match(strData.Replace("\r\n", "\\n"), regexContext).Success)
                {
                    localroom_relative = Regex.Match(strData.Replace("\r\n", "\\n"), regexContext).Result("$2");
                    localroom_relative = common.delColorStrng(localroom_relative.Replace("\\n", "").Replace(" ", ""));
                    localcityname = Regex.Match(strData.Replace("\r\n", "\\n"), regexContext).Result("$1");
                }
                else
                {
                    localroom_relative = "";
                    localcityname = "";
                }
                regexContext = @"(.+(?= -))";
                Regex regex = new Regex(regexContext);
                MatchCollection mc = regex.Matches(strData);
                if (mc.Count > 0)
                {
                    localroomname = mc[0].Value.Trim();
                }
                else
                {
                    localroomname = "";
                }

                if (localcityname == "")
                {
                    Program.fmM.MudOut.AppendText("不知道那里的" + localroomname + "\r\n");
                    dwok = false;
                    Program.fmM.MudOut.AppendText("定位失败!\r\n");
                    state = "定位失败";
                }
                else
                {               
                    for (int i = 0; i < Map.lookCitys.GetLength(0); i++)
                    {

                        if (localcityname.IndexOf(Map.lookCitys[i, 1]) != -1)
                        {

                            localcityid = Map.lookCitys[i, 0];
                             break;
                        }

                    }
                    if (localcityid + "" != "")
                    {
                        var rooms = new Map().Rooms.Where(m =>m.id.Contains(localcityid) && m.name == localroomname).ToList();
                    if (rooms!=null&&rooms.Count() > 1)
                    {
                        Program.fmM.MudOut.AppendText(localcityname + "存在多个同名" + localroomname + "房间,开始用出口进行判断!\r\n");
                        Program.fmM.MudOut.AppendText(localroom_relative+ "\r\n");
                        foreach (var room in rooms)
                        {
                            if (localroom_relative == room.room_relative)
                            {
                                localroomid = room.id;
                                dwok = true;
                                Program.fmM.MudOut.AppendText("定位成功!\r\n");
                                state = "定位成功";
                                break;
                            }
                        }
                    }
                    else if (rooms.Count() == 1)
                    {
                        localroomid = rooms.FirstOrDefault().id;
                        dwok = true;
                        Program.fmM.MudOut.AppendText("定位成功!\r\n");
                        state = "定位成功";
                    }
                    else
                    {
                        Program.fmM.MudOut.AppendText(localcityname + "不存在这个" + localroomname + "房间!\r\n");
                        dwok = false;
                        Program.fmM.MudOut.AppendText("定位失败!\r\n");
                        state = "定位失败";
                    }
                }
                }
                if (dwok)
                {
                    lujing.sfzd = false;
                    lujing.zdlj = "";
                    Room gorm=null;
                    for (int i = 0; i < Map.lookCitys.GetLength(0); i++)
                    {
                        if (statebz.IndexOf(Map.lookCitys[i, 1]) != -1)
                        {
                            string gocityid = Map.lookCitys[i, 0];
                            string gocityname = Map.lookCitys[i, 1];
                            string goroomname = statebz.Replace(Map.lookCitys[i, 1], "");
                            gorm = new Map().Rooms.Where(m => m.id.Contains(gocityid) && m.name == goroomname).FirstOrDefault();
                            break;
                          }
                    }
                    if (gorm == null)
                    {
                        bool cmz = false;
                        foreach (var tmproom in new Map().Rooms.Where(m=>m.objs!=null))
                        {
                            if (!cmz)
                            {
                                for (int j = 0; j < tmproom.objs.GetLength(0); j++)
                                {
                                    if (statebz.IndexOf(tmproom.objs[j, 0]) != -1)
                                    {
                                        gorm = tmproom;
                                        cmz = true;
                                        break;
                                    }
                                }
                            }
                            else {
                                break;
                            }
                        }
                    }
                    var localrm = new Map().Rooms.Where(m => m.id == localroomid).FirstOrDefault();
                    lujing.go(localrm, gorm);
                }
            }

        }


        public static void go(Room localrm, Room gorm)
        {
            localrm.PathRoom = null;
            gorm.PathRoom = null;
            getPath(localrm, gorm);
        }

        public static List<string> zdljs = null;

        static Thread tGo = new Thread(xz);
        public static void xz()
        {
            while (zdljs.Count != 0)
            {
                if (zdljs[0] != "#duCjiang")
                {
                    common.sendMsg(zdljs[0]);
                    zdljs.RemoveAt(0);
                }
                else
                {
                    state = "渡江";
                    tGo.Suspend();
                }
                if (zdljs.Count == 0)
                {
                    tGo.Suspend();
                }

            }
           
        }

        static bool zbz = true;
        public static void getPath(Room localrm, Room gorm)
        {
            if (localrm == null || gorm == null) return;
            if (!sfzd)
            {
                if (localrm.id == gorm.id)
                {
                    sfzd = true;
                    zdlj = localrm.Path;
                    zdlj = zdlj.TrimEnd(';');
                    zdljs = zdlj.Split(';').ToList();     
                    if (tGo.ThreadState == ThreadState.Suspended)
                    {
                        tGo.Resume();
                    }
                    else
                    {
                        tGo.Start();
                    }
                }
                else
                {
                    List<string> lst = localrm.PathRoom;
                    if (lst == null)
                    {
                        lst = new List<string>();
                    }
                    lst.Add(localrm.id);
                    localrm.PathRoom = lst;
                    string[,] ways = localrm.ways;
                    for (int i = 0; i < ways.GetLength(0); i++)
                    {
                        if (!localrm.PathRoom.Contains(ways[i, 1]))
                        {
                            var nextrm = new Map().Rooms.Where(m => m.id == ways[i, 1]).FirstOrDefault();
                            if (nextrm != null)
                            {
                                nextrm.PathRoom = lst;                  
                                nextrm.Path = localrm.Path + ways[i, 0] + ";";
                                getPath(nextrm, gorm);
                            }
                        }
                    }
                }
            }

目前正常定位行走没有问题,还需要处理的就是路径中含有#号需要调用函数处理的特殊出口
如长江 渡口的    { "#duCjiang","city/jiangnan"},  华山村菜地 {"#hscaidi", "huashan/path1"},及迷宫类的路径
行走指令会判断函数名做特殊处理,上面已经实现#duCjiang的处理,只要还是用到了多线程,使用单独的线程行走,发现函数中断线程,函数执行完成,走到对应的路径后,重启唤醒线程。

加油!!!!!
门派:桃花岛
 楼主| 发表于 2023-7-1 10:49:26 | 显示全部楼层
当时在照着mush的room.lua翻写C#的时候,都在考虑,直接用lua遍历rooms写数据库得了,最后想了想还是算了,其实还有一种自动化方案,类似代码生成器,通过mush读取room.lua然后生成c#代码也行,我都翻的差不多了,还是算了......
门派:天龙寺
发表于 2023-7-1 13:54:14 | 显示全部楼层
为什么要改成C, 看起来工程量有点大啊,加油
门派:桃花岛
 楼主| 发表于 2023-7-1 14:05:59 | 显示全部楼层
semic@tj 发表于 2023-7-1 13:54
为什么要改成C, 看起来工程量有点大啊,加油

玩这个游戏,玩的不是这些吗。
门派:桃花岛
发表于 2023-7-1 15:18:52 | 显示全部楼层
定位系统不需要遍历每个room吗
需要做出包含每个房间名称信息的数据库不
门派:桃花岛
 楼主| 发表于 2023-7-1 16:32:42 | 显示全部楼层
tmonk@ts2 发表于 2023-7-1 15:18
定位系统不需要遍历每个room吗
需要做出包含每个房间名称信息的数据库不

不需要,首先根据触发可以获取你当前房间的一些关键性信息,然后用Lambda 表达式直接取出符合条件的房间,在通过出口确定具体的房间,如果在迷宫,还不能确定只需要随意走动在确定,目标房间更好确定,直接通过你输入的目标信息直接用lambda获取房间对象就行,最后当前房间开循环走出口,一直走到目标房间,到目标房间的所走过的出口就是路径,然后一步步走过去就行。其中肯定也存在特殊的出口,比如坐船,菜地之类,就需要写对应的函数处理。
门派:华山派
发表于 2023-7-1 21:42:06 | 显示全部楼层
都是高手,羡慕,崇拜
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|书剑永恒MUD ( 闽ICP备14012032号|闽公网安备 35050202000162号 )

GMT+8, 2025-7-7 09:56 , Processed in 0.028863 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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