AITemplate小模块合并至BOT模块并优化参数样式

修改所有之前的AITemplate至新格式
启用9s的hold开关(之前有开关但是没用)
准备好cc对接相关代码模板(不能用所以注释了,需要修改)
This commit is contained in:
MrZ626
2021-08-24 05:15:27 +08:00
parent 1c43e92577
commit 7817499da7
25 changed files with 189 additions and 129 deletions

View File

@@ -11,7 +11,7 @@
4deepShape
BlockedWells
]]
local min,abs,rnd=math.min,math.abs,math.random
local min,abs=math.min,math.abs
local ins,rem=table.insert,table.remove
local yield=coroutine.yield
@@ -121,7 +121,8 @@ local function getScore(field,cb,cy)
end
local bot_9s={}
function bot_9s.thread(P,keys)
function bot_9s.thread(bot)
local P,data,keys=bot.P,bot.data,bot.keys
while true do
--Thinking
yield()
@@ -135,7 +136,7 @@ function bot_9s.thread(P,keys)
end
end
for ifhold=0,P.gameEnv.holdCount>0 and 1 or 0 do
for ifhold=0,data.hold and P.gameEnv.holdCount>0 and 1 or 0 do
--Get block id
local bn
if ifhold==0 then
@@ -192,7 +193,7 @@ function bot_9s.thread(P,keys)
--Check if time to change target
yield()
if P.aiRND:random()<.00126 then
P:changeAtkMode(rnd()<.85 and 1 or #P.atker>3 and 4 or rnd()<.3 and 2 or 3)
P:changeAtkMode(P.aiRND:random()<.85 and 1 or #P.atker>3 and 4 or P.aiRND:random()<.3 and 2 or 3)
end
end
end

55
parts/bot/bot_cc.lua Normal file
View File

@@ -0,0 +1,55 @@
--[[ControlID:
1~5:mL,mR,rR,rL,rF,
6~10:hD,sD,H,A,R,
11~13:LL,RR,DD
]]
local ins,rem=table.insert,table.remove
local yield=coroutine.yield
local bot_cc={}
function bot_cc:pushNewNext(id)
self.bot:addNext(rem(self.nexts,1))
ins(self.nexts,id)
end
function bot_cc:thread()
local P,keys=self.P,self.keys
local ccBot=self.ccBot
while true do
--Start thinking
yield()
ccBot:think()
--Poll keys
local success,result,dest,hold,move
repeat
yield()
success,result,dest,hold,move=ccBot:getMove()
until not success or result==0 or result==2
if not success then break end
if result==2 then
break
elseif result==0 then
dest[5],dest[6]=dest[1][1],dest[1][2]
dest[7],dest[8]=dest[2][1],dest[2][2]
dest[1],dest[2]=dest[3][1],dest[3][2]
dest[3],dest[4]=dest[4][1],dest[4][2]
P.AI_dest=dest
if hold then keys[1]=8 end--Hold
while move[1]do
local m=rem(move,1)
if m<4 then
ins(keys,m+1)
elseif not P.AIdata._20G then
ins(keys,13)
end
end
ins(keys,6)
end
--Check if time to change target
yield()
if P.aiRND:random()<.00126 then
P:changeAtkMode(P.aiRND:random()<.85 and 1 or #P.atker>3 and 4 or P.aiRND:random()<.3 and 2 or 3)
end
end
end
return bot_cc

View File

@@ -1,4 +1,4 @@
local rem=table.remove
local ins,rem=table.insert,table.remove
local baseBot={
pushNewNext=NULL,
@@ -9,59 +9,103 @@ local baseBot={
}
function baseBot.update(bot)
local P=bot.P
local keys=bot.keys
if P.control and P.waiting==-1 then
local keyQueue=bot.keys
bot.delay=bot.delay-1
if not keyQueue[1]then
if not keys[1]then
if bot.runningThread then
pcall(bot.runningThread)
if not pcall(bot.runningThread)then
P:destroyBot()
end
else
P:act_hardDrop()
end
elseif bot.delay<=0 then
bot.delay=bot.delay0*.5
P:pressKey(keyQueue[1])P:releaseKey(keyQueue[1])
rem(keyQueue,1)
P:pressKey(keys[1])P:releaseKey(keys[1])
rem(keys,1)
end
end
end
local function undefMethod(self,k)
print('warn',"Undefined method: "..k)
print("Undefined method: "..k)
self[k]=NULL
return NULL
end
local botMeta={__index=undefMethod}
return{
new=function(P,data)
local bot={P=P}
if data.type=="CC"then
-- local ccBot=require"parts.bot.cc_wrapper"
-- setmetatable(bot,{__index=function(self,method)
-- if ccBot[method]then
-- ccBot[method](ccBot)
-- elseif baseBot[method]then
-- baseBot[method](self)
-- else
-- undefMethod(self,method)
-- end
-- end})
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)
bot.P:setRS('TRS')
bot.runningThread=coroutine.wrap(bot.thread)
bot.keys={}
bot.delay=data.delay
bot.delay0=data.delay
bot.runningThread(P,bot.keys)
setmetatable(bot,botMeta)
end
return bot
local BOT={}
local AISpeed={60,50,40,30,20,14,10,6,4,3}
function BOT.template(arg)
if arg.type=='CC'then
if not arg.hold then arg.hold=false else arg.hold=true end
return{
type='CC',
next=arg.next,
hold=arg.hold,
delay=AISpeed[arg.speedLV],
["args.node"]=arg.node,
}
elseif arg.type=='9S'then
return{
type='9S',
delay=math.floor(AISpeed[arg.speedLV]),
hold=arg.hold,
}
end
}
end
function BOT.new(P,data)
local bot={P=P,data=data}
if data.type=="CC"then
-- P:setRS('SRS')
-- bot.keys={}
-- bot.nexts={}
-- bot.delay=data.delay
-- bot.delay0=data.delay
-- 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()
-- 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)
-- local cc_lua=require"parts.bot.bot_cc"
-- setmetatable(bot,{__index=function(self,k)
-- if self.ccBot[k]then
-- self.ccBot[k](self.ccBot)
-- elseif cc_lua[k]then
-- cc_lua[k](self)
-- elseif baseBot[k]then
-- baseBot[k](self)
-- end
-- end})
-- for i,B in next,P.gameEnv.nextQueue do
-- if i<=data.next then
-- bot:addNext(B.id)
-- else
-- ins(bot.nexts,B.id)
-- end
-- 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)
P:setRS('TRS')
bot.keys={}
bot.delay=data.delay
bot.delay0=data.delay
bot.runningThread=coroutine.wrap(bot.thread)
bot.runningThread(bot)
setmetatable(bot,botMeta)
end
return bot
end
return BOT