北大侠客行MUD论坛

 找回密码
 注册
搜索
热搜: 新手 wiki 升级
查看: 12128|回复: 16

活跃一下技术板块:mush脚本中几个常用的函数

[复制链接]
发表于 2016-9-10 08:34:22 | 显示全部楼层 |阅读模式
本帖最后由 nrm 于 2020-11-19 10:07 AM 编辑

最近技术板块都快长草了。俩版主也没啥动作,批评一下。
考虑到最近新人不少,很多人又是选择了mush作为客户端。作为一名老玩家,为了对各位表示欢迎,把我自己常用到的几个mush函数,在这里分享出来大家讨论讨论,算是激活一下技术板块的人气。
    这些内容都很小儿科,老bt们请绕行。

首先是汉字的数字转换成阿拉伯数字的函数:
_nums={}
_nums["一"]=1
_nums["二"]=2
_nums["三"]=3
_nums["四"]=4
_nums["五"]=5
_nums["六"]=6
_nums["七"]=7
_nums["八"]=8
_nums["九"]=9


ctonum=function(str)
        if (#str % 2) ==1 then
                return 0
        end
        result=0
        wan=1
        unit=1
        for i=#str -2 ,0,-2 do
                char=string.sub(str,i+1,i+2)
                if (char=="十") then
                        unit=10*wan
                        if (i==0) then
                                result=result+unit
                        elseif _nums[string.sub(str,i-1,i)]==nil then
                                result=result+unit
                        end
                elseif (char=="百") then
                        unit=100*wan
                elseif (char=="千") then
                        unit=1000*wan
                elseif (char=="万") then
                        unit=10000*wan
                        wan=10000
                else
                        if _nums[char]~=nil then
                                result=result+_nums[char]*unit
                        end
                end
        end
        return result
end
这个应用最广泛,机器中大量用到。
同时,你如果做个计时器啥的,还会用到时间的转换。
function timetonum(text)  ----中文时间转成秒
        local text=string.gsub(text,"天",",")
        text=string.gsub(text,"小时",",")
        text=string.gsub(text,"分",",")
        text=string.gsub(text,"秒","")
        local timelist=Split(text,",")
        local timelist2={}
        
        for i,v in ipairs(timelist) do--转成第一个元素为秒,往后排
                table.insert(timelist2,1,v)
        end
        
        local time_sec=0
        for i,v in ipairs(timelist2) do
                if i==1 then time_sec=time_sec+ctonum(v) end
                if i==2 then time_sec=time_sec+ctonum(v)*60 end
                if i==3 then time_sec=time_sec+ctonum(v)*3600 end
                if i==4 then time_sec=time_sec+ctonum(v)*3600*24 end
        end
        return time_sec        
end--endfunction


再然后呢,有时候你需要将汉字的方向转换成命令,比如“东”你想转换成“east",这其实最简单
function cnfx(fx)
       local fx=fx
       if fx=="上" then return "up"   end
       if fx=="下" then return "down"   end
       if fx=="南" then return "south"    end
       if fx=="东" then return "east"   end
       if fx=="西" then return "west"   end
       if fx=="北" then return "north"   end
       if fx=="南上" then return "southup"   end
       if fx=="南下" then return "southdown"   end
       if fx=="西上" then return "westup"   end
       if fx=="西下" then return "westdown"   end
       if fx=="东上" then return "eastup"   end
       if fx=="东下" then return "eastdown"   end
       if fx=="北上" then return "northup"   end
       if fx=="北下" then return "northdown"   end
       if fx=="西北" then return "northwest"   end
       if fx=="东北" then return "northeast"   end
       if fx=="西南" then return "southwest"   end
       if fx=="东南" then return "southeast"   end
       if fx=="小道" then return "xiaodao"   end
       if fx=="小路" then return "xiaolu"   end
       return fx
end

至于用途嘛,自己研究,老玩家都懂的。

还有时候呢,你想把地区信息分开来处理,比如”扬州南大街“你想得到”扬州“和”南大街“,那好,用这个函数:
areas={"中原","西湖梅庄","曲阜","信阳","泰山","长江南岸","长江","长江北岸","黄河南岸","黄河北岸","嘉兴","泉州","江州","牙山","临安府","西湖","福州","南昌","镇江","苏州","归云庄","小山村","张家口","麒麟村","大理城中","昆明","平西王府","桃源","岳阳","成都","建康府南城","建康府北城","北京","康亲王府","天坛","紫禁城","洛阳","灵州","晋阳","日月神教","神龙岛","襄阳","丝绸之路","长安","扬州","杀手帮","丐帮","岳王墓","姑苏慕容","桃花岛","峨嵋","天龙寺","武当山","华山","全真","古墓","少林寺","白驼山","星宿","明教","灵鹫","凌霄城","大轮寺","兰州","无量山","峨眉后山","杭州提督府","天地会"}
function get_place(str)
local place={}
for i=1,table.getn(areas) do
   str_l=string.len(areas)
   if areas==string.sub(str,1,str_l) then
      place.area=areas   -----区域名车
      place.room=string.sub(str,str_l+1,string.len(str))      -----房间名称
      break
   end
end
return place
end

返回的place是一个table,place.area就是区域,类似”扬州“,place.room是房间,类似”南大街"

以上内容全部来自论坛,我只是根据自己习惯稍加修改,在这里做了个汇总。你根据自己的习惯和需求再去修改修改。
好了,这一期就到这里吧。
btw:很多人跟我要少林新手机器,我只能说,这东西是不能分享的,而且拿来主义没意思,等有时间单开个帖子跟大家分享一下少林新手机器制作过程中的几个难点以及处理方式吧。
引用 icer 老大的一句话:此处授之以渔,mud本质其实是个社交平台。不要把练功当成最终目的,否则你会很失落的。

北大侠客行MUD,中国最好的MUD
复制粘贴会丢内容,干脆把这几个常用函数做了个附件。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2016-9-10 17:12:22 | 显示全部楼层
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2016-9-11 08:55:11 | 显示全部楼层
if areas==string.sub(str,1,str_l) then
      place.area=areas   -----区域名
      place.room=string.sub(str,str_l+1,string.len(str))      -----房间名称


nrm要区域名车莫非要带着李莫愁远走高飞??
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
 楼主| 发表于 2016-9-12 08:40:14 | 显示全部楼层
回复 3# creat


    这都被你发现了。
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2016-9-12 09:31:32 | 显示全部楼层
回复 1# nrm


    一个老板不好好赚钱研究啥mush,泡妞技术又是那么风骚,才华点太多了也不要这么浪费啊
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
 楼主| 发表于 2016-9-12 22:50:34 | 显示全部楼层
回复 5# seagull


    修身养性,韬光养晦。mud真乃收敛心神第一利器。
自从重回mud,腰也不疼了、腿也不酸了,走路也比原来有力气了。吃麻嘛香身体奔棒。
就连皮肤都比原来红润了
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2016-9-12 23:12:00 | 显示全部楼层
本帖最后由 seagull 于 2016-9-12 03:14 PM 编辑

回复 6# nrm

其实是远离女色的作用吧
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2016-9-14 08:59:41 | 显示全部楼层
向楼主求Split()函数
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2016-9-14 14:40:44 | 显示全部楼层
回复 8# songtao


function Split(szFullString, szSeparator)
local nFindStartIndex = 1
local nSplitIndex = 1
local nSplitArray = {}
while true do
   local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
   if not nFindLastIndex then
    nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
    break
   end
   nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
   nFindStartIndex = nFindLastIndex + string.len(szSeparator)
   nSplitIndex = nSplitIndex + 1
end
return nSplitArray
end

举例:Split(zuo_nc,";"),函数返回值就为已经分解为数组的zuo_nc的路径。

摘自:http://pkuxkx.com/forum/viewthread.php?tid=39080
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2016-9-14 14:46:10 | 显示全部楼层
function cnfx(fx)
       local fx=fx
       if fx=="上" then return "up"   end
       if fx=="下" then return "down"   end
       if fx=="南" then return "south"    end
       if fx=="东" then return "east"   end
       if fx=="西" then return "west"   end
       if fx=="北" then return "north"   end
       if fx=="南上" then return "southup"   end
       if fx=="南下" then return "southdown"   end
       if fx=="西上" then return "westup"   end
       if fx=="西下" then return "westdown"   end
       if fx=="东上" then return "eastup"   end
       if fx=="东下" then return "eastdown"   end
       if fx=="北上" then return "northup"   end
       if fx=="北下" then return "northdown"   end
       if fx=="西北" then return "northwest"   end
       if fx=="东北" then return "northeast"   end
       if fx=="西南" then return "southwest"   end
       if fx=="东南" then return "southeast"   end
       if fx=="小道" then return "xiaodao"   end
       if fx=="小路" then return "xiaolu"   end
       return fx
end


对于以上函数,个人比较倾向的写法是(dir_map这个table也可以拿到外面来,方便其他函数修改或复用?):


  1. function cnfx(fx)
  2.     local dir_map = {
  3.         ["上"] = "up",
  4.         ["下"] = "down",
  5.         ["南"] = "south",
  6.         -- ...
  7.     }
  8.     return dir_map[fx]
  9. end
复制代码



另外local fx=fx这个好像没必要=.=
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|北大侠客行MUD ( 京ICP备16065414号-1 )

GMT+8, 2024-5-9 08:18 AM , Processed in 0.012466 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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