ai协程化

This commit is contained in:
MrZ626
2021-03-05 15:28:43 +08:00
parent 640403866f
commit 15ecee6767
3 changed files with 66 additions and 71 deletions

View File

@@ -8,6 +8,7 @@
]] ]]
local int,ceil,min,abs,rnd,modf=math.floor,math.ceil,math.min,math.abs,math.random,math.modf local int,ceil,min,abs,rnd,modf=math.floor,math.ceil,math.min,math.abs,math.random,math.modf
local ins,rem=table.insert,table.remove local ins,rem=table.insert,table.remove
local YIELD=YIELD
-- controlname: -- controlname:
-- 1~5:mL,mR,rR,rL,rF, -- 1~5:mL,mR,rR,rL,rF,
-- 6~10:hD,sD,H,A,R, -- 6~10:hD,sD,H,A,R,
@@ -41,6 +42,7 @@ if _CC then
setNode= _CC.set_max_nodes ,--(opt,bool) setNode= _CC.set_max_nodes ,--(opt,bool)
free= _CC.free ,--(opt/wei) free= _CC.free ,--(opt/wei)
} }
local CC=CC
function CC.updateField(P) function CC.updateField(P)
local F,i={},1 local F,i={},1
for y=1,#P.field do for y=1,#P.field do
@@ -196,8 +198,10 @@ local function getScore(field,cb,cy)
end end
------------------------------------------------- -------------------------------------------------
return{ return{
["9S"]={ ["9S"]=function(P,keys)
function(P,ctrl) while true do
--Thinking
YIELD()
local Tfield={}--Test field local Tfield={}--Test field
local best={x=1,dir=0,hold=false,score=-1e99}--Best method local best={x=1,dir=0,hold=false,score=-1e99}--Best method
local field_org=P.field local field_org=P.field
@@ -224,22 +228,18 @@ return{
local cy=#Tfield+1 local cy=#Tfield+1
--Move to bottom --Move to bottom
while true do while cy>1 and not ifoverlapAI(Tfield,cb,cx,cy-1)do
if cy==1 then break end
if not ifoverlapAI(Tfield,cb,cx,cy-1)then
cy=cy-1 cy=cy-1
else
break
end
end end
--Simulate lock --Simulate lock
for i=1,#cb do for i=1,#cb do
local y=cy+i-1 local y=cy+i-1
if not Tfield[y]then Tfield[y]=getRow(0)end if not Tfield[y]then Tfield[y]=getRow(0)end
local L=Tfield[y]
for j=1,#cb[1]do for j=1,#cb[1]do
if cb[i][j]then if cb[i][j]then
Tfield[y][cx+j-1]=1 L[cx+j-1]=1
end end
end end
end end
@@ -258,42 +258,36 @@ return{
while #Tfield>0 do while #Tfield>0 do
discardRow(rem(Tfield,1)) discardRow(rem(Tfield,1))
end end
local p=#ctrl+1
if best.hold then if best.hold then
ctrl[p]=8 ins(keys,8)
p=p+1
end end
local l=FCL[best.bn][best.dir+1][best.x] local l=FCL[best.bn][best.dir+1][best.x]
for i=1,#l do for i=1,#l do
ctrl[p]=l[i] ins(keys,l[i])
p=p+1
end end
ctrl[p]=6 ins(keys,6)
return 2
end, --Check if time to change target
function(P) YIELD()
if P:RND()<.00126 then if P:RND()<.00126 then
P:changeAtkMode(rnd()<.85 and 1 or #P.atker>3 and 4 or rnd()<.3 and 2 or 3) P:changeAtkMode(rnd()<.85 and 1 or #P.atker>3 and 4 or rnd()<.3 and 2 or 3)
end end
return 1
end,
},
["CC"]=CC and{
[0]=function(P)
LOG.print("CC is dead ("..P.id..")","error")
end,
function(P)--Start thinking
if not pcall(CC.think,P.AI_bot)then
return 0
end end
return 2
end, end,
function(P,ctrl)--Poll keys ["CC"]=CC and function(P,keys)
while true do
--Start thinking
YIELD()
if not pcall(CC.think,P.AI_bot)then goto ERR end
--Poll keys
YIELD()
while true do
local success,result,dest,hold,move=pcall(CC.getMove,P.AI_bot) local success,result,dest,hold,move=pcall(CC.getMove,P.AI_bot)
if success then if success then
if result==2 then if result==2 then
ins(ctrl,6) ins(keys,6)
return 3 break
elseif result==0 then elseif result==0 then
for i=1,#dest do for i=1,#dest do
for j=1,#dest[i]do for j=1,#dest[i]do
@@ -301,30 +295,30 @@ return{
end end
end end
P.AI_dest=dest P.AI_dest=dest
if hold then ctrl[1]=8 end--Hold if hold then keys[1]=8 end--Hold
while move[1]do while move[1]do
local m=rem(move,1) local m=rem(move,1)
if m<4 then if m<4 then
ins(ctrl,m+1) ins(keys,m+1)
elseif not P.AIdata._20G then elseif not P.AIdata._20G then
ins(ctrl,13) ins(keys,13)
end end
end end
ins(ctrl,6) ins(keys,6)
return 3 break
else
--Stay this stage
return 2
end end
else else
return 0 goto ERR
end end
end, end
function(P)--Check if time to change target
--Check if time to change target
YIELD()
if P:RND()<.00126 then if P:RND()<.00126 then
P:changeAtkMode(rnd()<.85 and 1 or #P.atker>3 and 4 or rnd()<.3 and 2 or 3) P:changeAtkMode(rnd()<.85 and 1 or #P.atker>3 and 4 or rnd()<.3 and 2 or 3)
end end
return 1 end
::ERR::
LOG.print("CC is dead ("..P.id..")","error")
end, end,
}, }--AI brains
}--AI think stage

View File

@@ -1424,7 +1424,6 @@ do--Player.drop(P)--Place piece
end end
function Player.loadAI(P,data)--Load AI params function Player.loadAI(P,data)--Load AI params
P.AI_mode=data.type P.AI_mode=data.type
P.AI_stage=1
P.AI_keys={} P.AI_keys={}
P.AI_delay=min(int(P.gameEnv.drop*.8),data.delta*rnd()*4) P.AI_delay=min(int(P.gameEnv.drop*.8),data.delta*rnd()*4)
P.AI_delay0=data.delta P.AI_delay0=data.delta
@@ -1462,6 +1461,8 @@ function Player.loadAI(P,data)--Load AI params
else else
P:setRS("TRS") P:setRS("TRS")
end end
P.AI_thread=coroutine.create(AIFUNC[data.type])
coroutine.resume(P.AI_thread,P,P.AI_keys)
end end
--------------------------</Methods>-------------------------- --------------------------</Methods>--------------------------

View File

@@ -145,8 +145,8 @@ function update.alive(P,dt)
local C=P.AI_keys local C=P.AI_keys
P.AI_delay=P.AI_delay-1 P.AI_delay=P.AI_delay-1
if not C[1]then if not C[1]then
if P.AI_stage then if coroutine.status(P.AI_thread)=="suspended"then
P.AI_stage=AIFUNC[P.AI_mode][P.AI_stage](P,C) coroutine.resume(P.AI_thread)
end end
elseif P.AI_delay<=0 then elseif P.AI_delay<=0 then
P:pressKey(C[1])P:releaseKey(C[1]) P:pressKey(C[1])P:releaseKey(C[1])