找回密码
 注册
搜索
查看: 1218|回复: 14

用coroutine重构打造机器人

[复制链接]
门派:桃花岛
发表于 2020-3-1 19:31:02 | 显示全部楼层 |阅读模式
本帖最后由 zruo@tj 于 2020-3-1 20:26 编辑

最近打造机器人很火,我也凑个热闹。但是我这里纯粹只是展示一下如何利用coroutine写脚本
大家可以看看会不会更容易看懂点。

机器人基本类似小风神的功能,除了不能吃喝。我懒得加
用法就不细说了,主要是宣传下这种写法

  1. DaZao = {
  2.     dazaoID = "",
  3.     currnetCount = 0,
  4.     totalCount = 0,
  5.     expectValue = 2,
  6.     mainThread = nil,
  7.     findThread = nil,
  8.     autoDis = false,
  9.     jobName = 'dazaoArmor',
  10.     result = {}
  11. }

  12. function DaZao:new()
  13.     local o = {}
  14.     local l_result
  15.     l_result=utils.inputbox("你需要打造的护具ID是", "dazaoID", "", "宋体" , "12")
  16.     if not isNil(l_result) then
  17.         o.dazaoID = l_result
  18.     end

  19.     l_result=utils.inputbox ("你需要打造的次数", "dazaoTimes", 10, "宋体" , "12")
  20.     if not isNil(l_result) then
  21.         o.totalCount = tonumber(l_result)
  22.     end

  23.     l_result=utils.inputbox ("你需要保存的属性值(例如:2就是属性>=2的保存)。", "dazaoValue", 2, "宋体" , "12")
  24.     if not isNil(l_result) then
  25.         o.expectValue = tonumber(l_result)
  26.     end

  27.     l_result = utils.msgbox("是否自动分解不需要保存的装备", "dazaoDis",
  28.         "yesno", "?", 1)
  29.     if l_result and l_result == "yes" then
  30.         o.autoDis = true
  31.     else
  32.         o.autoDis = false
  33.     end
  34.     setmetatable(o, {__index = DaZao})
  35.     return o
  36. end

  37. function DaZao:start()
  38.     wait.make(function()
  39.         job.name=self.jobName
  40.         DeleteTimer('idle')
  41.         self.mainThread = coroutine.running()
  42.         self:checkJianDao(self.mainThread)
  43.         coroutine.yield()

  44.         print("买到剪刀")
  45.         weapon_unwield()
  46.         exe('wield jian dao')
  47.         -- now we have all we want, go crafting
  48.         await_go('zhiye/caifengpu1')
  49.         self:dazaoArmor()   
  50.     end)
  51. end

  52. function DaZao:checkJianDao(thread)
  53.     wait.make(function()
  54.         checkBags()  -- we should enhance this to be await like func
  55.         wait_busy()
  56.         while not Bag["剪刀"] do
  57.             await_go("扬州城","杂货铺")
  58.             local line, w
  59.             repeat
  60.                 exe('qu jian dao')
  61.                 line, w = wait.regexp(
  62.                                  '^(> )*(你把剪刀|你并没有保存)',1)
  63.             until line
  64.             if line:find('你并没有保存') then
  65.                 self:buyJianDao(coroutine.running())
  66.                 coroutine.yield()
  67.             end
  68.             checkBags()  -- we should enhance this to be await like func
  69.             wait_busy()
  70.         end
  71.         coroutine.resume(thread)
  72.     end)
  73. end
  74. function DaZao:dazaoArmor()
  75.     wait.make(function()
  76.         local dazaoThread = coroutine.running()
  77.         while self.currnetCount < self.totalCount or g_stop_flag do
  78.             print('本次打造第'..self.currnetCount..'次,预计打造'..self.totalCount..'次。')
  79.             exe('weave '..self.dazaoID)
  80.             local line,w = wait.regexp(
  81.                 '(> )*你很得意的拿起刚织造好的(\\D*)左|(> )*对于这种防具,您了解不多,还不会织造|(> )*你必须装备剪刀才能来织造')
  82.             if line and line:find("还不会织造") then
  83.                 print("打造失败!")
  84.                 break
  85.             elseif line and line:find('你必须装备剪刀') then
  86.                 self:checkJianDao(dazaoThread)
  87.                 coroutine.yield()
  88.                 await_go('zhiye/caifengpu1')
  89.             else
  90.                 self.armorName = w[2]
  91.                 print("打造成功!".. self.armorName)
  92.                
  93.                 self.currnetCount = self.currnetCount + 1
  94.                 self:checkProduct(dazaoThread)
  95.                 local status = coroutine.yield()
  96.                 if status == "abort" then
  97.                     break
  98.                 end
  99.             end
  100.         end
  101.         if g_stop_flag then
  102.             print("停止打造")            
  103.         else
  104.             print("全部打造完毕,一共打造了"..self.currnetCount..'次')
  105.         end
  106.         return
  107.     end)
  108. end

  109. function DaZao:checkProduct(thread)
  110.     wait.make(function()
  111.         local line,w
  112.         repeat            
  113.             exe('look '..self.dazaoID)
  114.             line,w = wait.regexp(
  115.                 '^(> )*它的功能有:【(\\N*)】',1)
  116.         until line
  117.         local value = w[2]
  118.         local wanttedAtr="身法根骨悟性力量"
  119.         local keep =false
  120.         for c,v in value:gmatch('(['..wanttedAtr..']+)+(%d+)') do
  121.             if wanttedAtr:find(c) then
  122.                 print(c,v)
  123.                 local number = tonumber(v)
  124.                 if number >= 5 then
  125.                     print("发现神器!!!")
  126.                     messageShow('出神器了,本次打造终止!','red','black')
  127.                     keep = true
  128.                     coroutine.resume(thread,"abort")
  129.                 elseif number >= self.expectValue then
  130.                     print("可以保存")
  131.                     fqyyArmorMessage('装备属性:'..value)
  132.                     keep = true
  133.                 end
  134.             end
  135.         end
  136.         if keep then
  137.             return self:cunArmor(thread)
  138.         elseif self.autoDis then
  139.             exe('dismantle '..self.dazaoID..';y')
  140.         else
  141.             print("不符合条件,但是不自动分解")
  142.         end
  143.         wait_busy()
  144.         coroutine.resume(thread)
  145.     end)
  146. end

  147. function DaZao:cunArmor(thread)
  148.     wait.make(function()
  149.         await_go('city/zahuopu')
  150.         exe('cun '..self.dazaoID)
  151.         checkBags()
  152.         wait_busy()
  153.         await_go('zhiye/caifengpu1')
  154.         coroutine.resume(thread)
  155.     end)
  156. end

  157. function DaZao:buyJianDao(thread)
  158.     wait.make(function()
  159.         await_go("changan/northjie2")
  160.         wait.time(2)
  161.         exe('look')
  162.         flag.times = 1
  163.         self.findThread = coroutine.running()
  164.         jobFindAgain[self.jobName] = function (...)
  165.             coroutine.resume(self.findThread)
  166.         end
  167.         find()
  168.         local status = coroutine.yield()
  169.         if status then
  170.             return
  171.         else
  172.             print('找不到婆婆')
  173.         end
  174.     end)
  175.     wait.make(function()
  176.         while true do
  177.             local line,_ = wait.regexp(
  178.                 '^(> )*\\s*(养蚕婆婆\\(Yangcan popo\\)|这里没有 yangcan popo|你决定跟随养蚕婆婆|你.*从养蚕婆婆那里买下了一把剪刀|老裁缝说道:「你想买的东西我这里没有)')
  179.             if line and line:find('Yangcan popo') then
  180.                 flag.wait = 1
  181.                 exe('follow yangcan popo')
  182.             elseif line and line:find('这里没有') then
  183.                 flag.wait = 0
  184.                 walk_wait()
  185.             elseif line and line:find('你决定跟随') then
  186.                 flag.find = 1
  187.                 exe('buy jian dao')
  188.             elseif line and line:find('没有') then
  189.                 exe('follow yangcan popo')
  190.                 exe('buy jian dao')
  191.             elseif line and line:find('一把剪刀') then
  192.                 exe('follow none')
  193.                 break
  194.             end
  195.         end        
  196.         coroutine.resume(thread)
  197.     end)
  198.    
  199. end


  200. function dazaoArmor()
  201.     local dz = DaZao.new()
  202.     tprint(dz)
  203.     dz:start()
  204. end
复制代码

评分

1

查看全部评分

门派:嵩山派
发表于 2020-3-1 22:10:37 来自手机 | 显示全部楼层
这种写法好处蛮多的,避免以前大量使用全局变量的混乱,不用担心内存泄露,dazaoArmor执行完整个object就会被释放掉。不用担心中间用的coroutine
门派:明教
发表于 2020-3-1 19:36:32 | 显示全部楼层
顶!不拆神器的都是好机器!

我那个就是抛砖引玉的!
门派:桃花岛
 楼主| 发表于 2020-3-1 20:21:08 | 显示全部楼层
kkfromch@tj 发表于 2020-3-1 19:36
顶!不拆神器的都是好机器!

我那个就是抛砖引玉的!

我也没出过神器。。不确定啊
门派:明教
发表于 2020-3-1 21:06:11 | 显示全部楼层
zruo@tj 发表于 2020-3-1 20:21
我也没出过神器。。不确定啊

难道也要交学费吗?
门派:嵩山派
发表于 2020-3-1 21:23:46 来自手机 | 显示全部楼层
aoxue@tj 发表于 2020-3-1 21:06
难道也要交学费吗?

来一起code review,
为了大家财产安全
门派:桃花岛
发表于 2020-3-2 07:46:52 | 显示全部楼层
用到类了,先顶再仔细研究一下,,
门派:天龙寺
发表于 2020-3-3 18:22:41 来自手机 | 显示全部楼层
完整版吗,可以去测试了么
门派:少林派
发表于 2020-3-15 20:17:03 | 显示全部楼层
顶一下,期望各位大佬高出智能版的mush机器人
门派:天龙寺
发表于 2020-3-16 08:58:47 | 显示全部楼层
完全看不懂啊,智能膜拜了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-6-7 03:15 , Processed in 0.052447 second(s), 28 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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