发一段代码,高手来帮我批注一下每一句都是啥意思,实现了什么功能
下面的代码是别人写到一个脚本。我大概看懂了一些,但是没有完全懂。大概意思好像就可以在mush中使用和zmud一样的#3 w;n这样的带#还有带分号;的命令吧,还有就是#wait命令吧,还有好像就是#t+ #t-
那位有时间可以给我每句后面都加一句注释,说这句是啥意思
代码如下:
---------------------------------------------------------
没人理睬,删除了
[ 本帖最后由 selfmud 于 2011-9-9 09:11 PM 编辑 ] function ton(var)
--Note("打开 trigger:"..var)
EnableTriggerGroup(var, true)
end
function toff(var)
--Note("关闭 trigger:"..var)
EnableTriggerGroup(var, false)
end
function e(cmd)
cmdSender.add(cmd)
end
这个应该是一些简写吧 前来学习 原帖由 ruoyu 于 2011-9-9 06:36 PM 发表 http://pkuxkx.com/forum/images/common/back.gif
前来学习
学习啥,我这样的代码一大堆,就是我都看不懂
谁给我一句一句解释下
里面的有些--是我自己加上去的,也不知道对不对。
还有些根本没看懂
而且这些东西我还不知道咋用
谁负责解释的话我很多代码都可以发过去给解释
我手里很多。。。 算了,隐藏帖子 那个我看懂了,我发的代码在在顶楼,顶楼的删除了
我以为没人管呢,既然你在我就发出来把
代码如下:
---------------------------------------------------------
-- Command
--- Repeat: #4 xx (repeat 4 times for command xx)
--- Delay: @3 (delay 3 seconds)
---------------------------------------------------------
module("cmdSender",package.seeall)
local commands =
{
cmds = {},
isRunning = false,
thread = nil,
count = 0,
nowait = false,
time = os.clock(),
block = false,
}
function totable(cmds)
local retVal = {}
for k, v in pairs(utils.split(cmds, ";")) do
if (string.sub(v, 1, 1) == "#") then -- convert repeat command
local sb, se = string.find(v, "%s+")
assert(sb ~= nil and se ~= nil, "wrong repeat command format")
local scc = string.sub(v, 2, sb -1)
if scc == "t+" or scc == "T+" or scc == "T-" or scc == "t-" or scc == "w+" or scc == "w-" then
retVal[#retVal + 1] = v
else
local times = tonumber(string.sub(v, 2, sb - 1))
local cmd = string.sub(v, se + 1)
for i = 1, times, 1 do
retVal[#retVal + 1] = cmd
end
end
else
retVal[#retVal + 1] = v
end
end
return retVal
end
function add(incmds)
if commands.block then
print("堵塞命令:"..incmds)
return
end
local cmd = utils.split(incmds, ";")--没有特殊命令
--一次一次处理一个字符太慢了。搞一串进去
commands.cmds[#commands.cmds + 1] = cmd
wakeup()
end
function insert(cmds)
if (type(cmds) == "string") then
cmds = totable(cmds)
end
for k, v in pairs (cmds) do
table.insert(cmds, 1, v)
end
wakeup()
end
function clear()
commands.cmds = {}
commands.nowait = false
Note("commands clear...")
end
function wakeup()
if not commands.thread or coroutine.status(commands.thread) == "dead" then
print("生成 commands 协程")
commands.thread = nil
commands.thread = coroutine.create(cmdSender.run)
commands.isRunning = false
end
if (not commands.isRunning) then
local time1 = os.clock()
local dcounts = (time1 - commands.time) * 15
commands.time = time1
commands.count = commands.count - dcounts
--print(time.."delay:"..commands.delay.."count"..commands.count)
--< 0 似乎不用置0,那样网速卡的时候。或者delay多,就等次数恢复 mark
if commands.count < -16 then
commands.count = -16
end
resume()
end
end
function len()
return #commands.cmds
end
function resume()
if coroutine.running() ~= commands.thread then
coroutine.resume(commands.thread)
end
end
function process(cmd)
commands.isRunning = true
local h1 = string.sub(cmd, 1, 1)
if h1 == "@" then
local interval = tonumber(string.sub(cmd, 2))
if (interval > 0) then
commands.count = commands.count - interval * 1.6
if commands.count < -16 then
commands.count = -16
end
interval=interval/10
print("wait "..interval.." seconds...")
DoAfterSpecial(interval, "cmdSender.resume()", 12)
coroutine.yield()
end
elseif h1 == "$" then
local exc = string.sub(cmd, 2)
exec(exc)
elseif h1 == "#" then -- convert repeat command
local h2 = string.sub(cmd, 2, 2)
local h3 = string.sub(cmd, 3, 3)
if h2 == "w" then
if h3 == "+" then
commands.nowait = true
elseif h3 == "-" then
commands.nowait = false
else
send(cmd)
end
elseif h2 == "t" then
local sb, se = string.find(cmd, "%s+")
local group = string.sub(cmd, se + 1)
if h3 == "+" then
Note("Enable trigger Group "..group);
EnableTriggerGroup(group, true)
elseif h3 == "-" then
Note("Disable trigger Group "..group);
EnableTriggerGroup(group, false)
else
send(cmd)
end
else
local sb, se = string.find(cmd, "%s+")
local times = tonumber(string.sub(cmd, 2, sb - 1))
local cmd = string.sub(cmd, se + 1)
for i=1, times do
send(cmd)
end
end
else
send(cmd)
end
end
function run()
while true do
if commands.nowait == false and commands.count > 4 then
local delay = commands.count / 16 --按1秒16个命令来计算时间
print("wait "..delay.." second...")
commands.count = 0
commands.time = commands.time + delay --delay 时间要扣除掉
DoAfterSpecial(delay, "cmdSender.resume()", 12)
coroutine.yield()
else
local cmdsends = nil
if (#commands.cmds ~= 0) then
cmdsends = commands.cmds -- pick cmd in queue
table.remove(commands.cmds, 1) -- remove cmd in queue
end
if cmdsends ~= nil then
for k, v in pairs (cmdsends) do
process(v)
end
else
commands.isRunning = false
coroutine.yield()
end
end
end
end
function send(cmd)
SendImmediate(cmd)
commands.count = commands.count + 1
end
function exec(ec)
local _, i = string.gsub(ec, ";", ";")
if i == 0 then i = 1 end
commands.count = commands.count + i
Execute(ec)
end
function clearcount()
if commands.count < 0 then
commands.count = 0
end
commands.nowait = false
end
function setnowait(flag)
if flag == false then
commands.nowait = false
else
commands.nowait = true
end
end
function clearrunning()
if coroutine.running() == commands.thread then
commands.isRunning = true
else
commands.isRunning = false
end
end
function getwait()
local delay = commands.count / 16 --按1秒16个命令来计算时间
commands.count = 0
commands.time = commands.time + delay --delay 时间要扣除掉
if delay > 0.1 then
return delay
end
return 0
end
function getwaitflag()
return commands.nowait
end
function reset()
commands.thread = nil
commands.cmds = {}
commands.isRunning = false
commands.count = 0
commands.nowait = false
commands.time = os.clock()
commands.block = false
end
我大概看懂了一些,但是没有完全懂。
大概意思好像就可以在mush中使用和zmud一样的#3 w;n这样的带#还有带分号;的命令吧,还有就是#wait命令吧,还有好像就是#t+ #t-
那位有时间可以给我每句后面都加一句注释,说这句是啥意思
可能的话复制原来的帖子,然后后面加个 “--”来注释,颜色改成不要一样的吧
[ 本帖最后由 selfmud 于 2011-9-10 02:57 AM 编辑 ] 大赞ttk_03 哦。谢谢
下次不发这些东西了。下次问些不需要查资料的
页:
[1]