适配CC API (#233)

* 适配CC API

* 修正上游代码
This commit is contained in:
Trebor Huang
2021-08-26 14:29:41 +08:00
committed by GitHub
parent 118182a6cc
commit db4f193046
2 changed files with 60 additions and 30 deletions

View File

@@ -6,26 +6,25 @@
local ins,rem=table.insert,table.remove
local yield=coroutine.yield
local bot_cc={}
function bot_cc:switch20G()
self._20G=true
end
function bot_cc:checkDest()
local dest=self.destFX
local dest=self.P.destFX
if not dest then return end
local CB=self.cur.bk
local CB=self.P.cur.bk
for k=1,#dest,2 do
local r=CB[dest[k+1]-self.curY+2]
if not r or not r[dest[k]-self.curX+2]then
if self.bot then
self.bot:lockWrongPlace()
end
self.destFX=nil
local r=CB[dest[k+1]-self.P.curY+2]
if not r or not r[dest[k]-self.P.curX+2]then
self:lockWrongPlace()
self.P.destFX=nil
return
end
end
end
function bot_cc:revive()
TABLE.cut(self.P.holdQueue)
self.P:loadAI(self.data)
end
function bot_cc:pushNewNext(id)
self.bot:addNext(rem(self.nexts,1))
self.ccBot:addNext(rem(self.nexts,1))
ins(self.nexts,id)
end
function bot_cc:thread()
@@ -40,7 +39,7 @@ function bot_cc:thread()
local success,result,dest,hold,move
repeat
yield()
success,result,dest,hold,move=ccBot:getMove()
success,result,dest,hold,move=pcall(ccBot.getMove,ccBot)
until not success or result==0 or result==2
if not success then break end
if result==2 then
@@ -56,7 +55,7 @@ function bot_cc:thread()
local m=rem(move,1)
if m<4 then
ins(keys,m+1)
elseif not self._20G then
elseif not self.data._20G then
ins(keys,13)
end
end
@@ -70,4 +69,25 @@ function bot_cc:thread()
end
end
end
function bot_cc:updateField()
local P=self.P
local F,i={},1
for y=1,#P.field do
for x=1,10 do
F[i],i=P.field[y][x]>0,i+1
end
end
while i<=400 do
F[i],i=false,i+1
end
if not pcall(self.ccBot.reset,self.ccBot,F,P.b2b>=100,P.combo)then
print("CC is dead ("..P.id..")","error")
end
end
function bot_cc:switch20G()
TABLE.cut(self.P.holdQueue)
self.data._20G=true
self.P:loadAI(self.data)
end
bot_cc.lockWrongPlace=bot_cc.updateField
return bot_cc

View File

@@ -38,15 +38,28 @@ local botMeta={__index=_undefMethod}
local BOT={}
local AISpeed={60,50,40,30,20,14,10,6,4,3}
-- For CC bot:
-- TODO you still need to switch20G() at the right time.
-- since it's not cc-specific I'm not dealing with it for now
--[[
next: number of nexts
hold: holdable?
speedLV: level
node: search nodes
randomizer: random generator
_20G: 20G?
]]
function BOT.template(arg)
if arg.type=='CC'then
if not arg.hold then arg.hold=false else arg.hold=true end
if not arg.randomizer then arg.randomizer='bag' end
return{
type='CC',
next=arg.next,
hold=arg.hold,
delay=AISpeed[arg.speedLV],
node=arg.node,
bag=arg.randomizer=='bag',
_20G=arg._20G,
}
elseif arg.type=='9S'then
return{
@@ -65,28 +78,26 @@ function BOT.new(P,data)
bot.nexts={}
bot.delay=data.delay
bot.delay0=data.delay
bot._20G=P._20G
if P.gameEnv.holdCount and P.gameEnv.holdCount>1 then P:setHold(1)end
local cc=require"parts.bot.cc_wrapper"
local opt,wei=cc.getConf()
local cc=require"CCloader"
local opt,wei=cc.getDefaultConfig()
wei:fastWeights()
opt:setHold(P.AIdata.hold)
opt:set20G(P.AIdata._20G)
opt:setBag(P.AIdata.bag=='bag')
opt:setNode(P.AIdata.node)
bot.ccBot=cc.new(opt,wei)
opt:setHold(data.hold)
opt:set20G(data._20G)
opt:setBag(data.bag=='bag')
opt:setNode(data.node)
bot.ccBot=cc.launchAsync(opt,wei)
local cc_lua=require"parts.bot.bot_cc"
setmetatable(bot,{__index=function(self,k)
return
self.ccBot[k]and function(_,...)self.ccBot[k](self.ccBot,...)end or
cc_lua[k]and function(_,...)cc_lua[k](self,...)end or
baseBot[k]or
error("No action called "..k)
self.ccBot[k] and function(_,...)self.ccBot[k](self.ccBot,...)end or
cc_lua[k] and function(_,...)cc_lua[k](self,...)end or
baseBot[k] and baseBot[k] or
error("MrZ did something bad again! He just wanted "..k)
end})
for i,B in next,P.gameEnv.nextQueue do
for i,B in next,P.nextQueue do
if i<=data.next then
bot:addNext(B.id)
else
@@ -95,7 +106,6 @@ function BOT.new(P,data)
end
bot.runningThread=coroutine.wrap(cc_lua.thread)
bot.runningThread(bot)
setmetatable(bot,botMeta)
elseif data.type=="9S"or true then--9s or else
TABLE.cover(baseBot,bot)
TABLE.cover(require"parts.bot.bot_9s",bot)