【新人向】实用型mud路径计算器(mapper)
mapper是mud中一个非常常用,基础的功能。我把我hellclient中的路径计算模块翻译成了lua5.1可用版本,供大家参考和使用。
github地址:
https://github.com/hellclient-scripts/mudmapper.lua
主要功能包括
[*]寻找起点和多个终点之间的最近路径。
[*]支持不同房间出口的步长,比如粘鼠板可以设置更长的步长参与计算
[*]支持标签,可以设置有具体标签或没有具体标签才能使用某出口。常见标签如门派,长袖2,正气,轻功等级,性别等。
[*]支持飞行路径。这个北侠用出不大。典型如新人的travel,可以从任意地点前往指定目标
附带一个利用特性1,能够使用标签进行动态计算的动态遍历路径生成器。
注意,由于mud现在属于客户端性能压制状态,代码都没有做性能优化。
具体的使用demo如下
-- 格式化输出路径列表
function Dumpsteps(path)
if path==nil then
return "nil"
end
local count=#path
local commnds={}
local delay=0
local to=path[#path].to
local from=path.from
local result=""
for i,v in pairs(path) do
table.insert(commnds,v.command)
delay=delay+v.delay
end
result=result.."count:"..count..","
result=result.."from:"..from..","
result=result.."to:"..to..","
result=result.."commands:"..table.concat(commnds,";")..","
result=result.."delay:"..delay..","
return result
end
local Mapper=require "mapper"
-- 新建mapper实例
local m=Mapper.newmapper()
-- 初始化房间
m:setroomname("shanghai","上海")
m:setroomname("beijing","北京")
m:setroomname("hangzhou","杭州")
m:setroomname("ningbo","宁波")
m:setroomname("xiaoxianchen1","小县城")
m:setroomname("xiaoxianchen2","小县城")
m:setroomname("xiaoxianchen3","小县城")
local paths={
{from="shanghai",to="beijing",command="坐火车"},
{from="beijing",to="shanghai",command="坐火车回"},
{from="hangzhou",to="shanghai",command="坐汽车",excludetags={yunche=true}},
{from="shanghai",to="hangzhou",command="坐汽车回",excludetags={yunche=true}},
{from="hangzhou",to="beijing",command="坐飞机",tags={rich=true}},
{from="beijing",to="hangzhou",command="坐飞机回",tags={rich=true}},
{from="hangzhou",to="shanghai",command="骑自行车",delay=10},
{from="shanghai",to="hangzhou",command="骑自行车回",delay=10},
{from="ningbo",to="shanghai",command="骑自行车2",delay=10},
{from="shanghai",to="ningbo",command="骑自行车回2",delay=10},
}
-- 引入飞行路径
local path=Mapper.newpath()
path.command="包车"
path.to="beijing"
m:setflylist({path})
for i,v in ipairs(paths) do
local path=Mapper.newpath()
path.from=v.from or ""
path.to=v.to or ""
path.command=v.command or ""
path.delay=v.delay or 0
path.tags=v.tags or {}
path.excludetags=v.excludetags or {}
m:addpath(path.from,path)
end
-- 获取hangzhou的可用出口,没有rich坐飞机不可用
-- 2
print(#m:getexits("hangzhou"))
-- 获取hangzhou的全部出口
-- 3
print(#m:getexits("hangzhou",true))
-- 获取shanghai的房间名
-- 上海
print(m:getroomname("shanghai"))
-- 获取不存在的newyork房间
-- ""
print(m:getroomname("newyork"))
-- 获取房间名为上海的房间
-- shanghai
print(table.concat(m:getroomid("上海"),","))
-- 获取房间名为纽约的房间
-- ""
print(table.concat(m:getroomid("纽约"),","))
-- 获取房间名为小县城的房间
-- xiaoxianchen3,xiaoxianchen2,xiaoxianchen1
print(table.concat(m:getroomid("小县城"),","))
-- 获取shanghai到beijing的路径
-- count:1,from:shanghai,to:beijing,commands:坐火车,delay:1,
print(Dumpsteps(m:getpath("shanghai",false,{"beijing"})))
-- 获取hangzhou到beijing的路径,经过shanghai
-- count:2,from:hangzhou,to:beijing,commands:坐汽车;坐火车,delay:2,
print(Dumpsteps(m:getpath("hangzhou",false,{"beijing"})))
-- 开启飞行列表
-- count:1,from:hangzhou,to:beijing,commands:包车,delay:1,
print(Dumpsteps(m:getpath("hangzhou",true,{"beijing"})))
-- 不存在的起点
-- nil
print(Dumpsteps(m:getpath("newyork",false,{"beijing"})))
-- 不存在的终点
-- nil
print(Dumpsteps(m:getpath("shanghai",false,{"newyork"})))
-- 终点中包含不可用房间
-- count:1,from:shanghai,to:beijing,commands:坐火车,delay:1,
print(Dumpsteps(m:getpath("shanghai",false,{"newyork","beijing"})))
-- 设置rich标签
m:settag("rich",true)
-- 有钱,所以坐飞机
-- count:1,from:hangzhou,to:beijing,commands:坐飞机,delay:1,
print(Dumpsteps(m:getpath("hangzhou",false,{"beijing"})))
-- 重置标签
m:flashtags()
-- 不晕车,可以做汽车
-- count:1,from:hangzhou,to:shanghai,commands:坐汽车,delay:1,
print(Dumpsteps(m:getpath("hangzhou",false,{"shanghai"})))
-- 阿,晕车了
m:settag("yunche",1)
-- 晕车了,只能骑自行车
-- count:1,from:hangzhou,to:shanghai,commands:骑自行车,delay:10,
print(Dumpsteps(m:getpath("hangzhou",false,{"shanghai"})))
-- 重置标签
m:flashtags()
-- 宁波去杭州,多条路线找delay最短的
-- count:2,from:ningbo,to:hangzhou,commands:骑自行车2;坐汽车回,delay:11,
print(Dumpsteps(m:getpath("ningbo",false,{"hangzhou"})))
-- 宁波就近去beijing或者hangzhou,杭州近
-- count:2,from:ningbo,to:beijing,commands:骑自行车2;坐火车,delay:11,
print(Dumpsteps(m:getpath("ningbo",false,{"beijing","hangzhou"})))
-- 动态遍历房间
local result=m:walkall({"shanghai","beijing","ningbo","hangzhou","xiaoxianchen1","xiaoxianchen2","xiaoxianchen3"},0)
-- 遍历成功的房间
-- shanghai,beijing,hangzhou,ningbo
print(table.concat(result.walked,","))
-- 遍历失败的房间
print(table.concat(result.notwalked,","))
-- 遍历路径
-- count:5,from:shanghai,to:ningbo,commands:坐火车;坐火车回;坐汽车回;坐汽车;骑自行车回2,delay:14,
print(Dumpsteps(result.steps))
m:flashtags()
-- 打印起效标签
-- ""
print(table.concat(m:alltags(),","))
-- 批量添加标签
m:addtags({"tag1","tag2","tag3","tag1"})
-- 打印起效标签
-- tag3,tag1,tag2
print(table.concat(m:alltags(),","))
注意,动态遍历本质是多次多点walk
只能说的确能实现遍历效果,但实际效率肯定不如预录遍历路径 感谢分享,希望能多些通俗的理论阐述{:7_279:} 这叫新人向?新人现在这么变态了吗? icer 发表于 2022-10-7 05:22 PM
这叫新人向?新人现在这么变态了吗?
在杰佬眼里,比他弱的都叫新人 我不配做新人
页:
[1]