|
本帖最后由 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的处理,只要还是用到了多线程,使用单独的线程行走,发现函数中断线程,函数执行完成,走到对应的路径后,重启唤醒线程。
加油!!!!! |
|