ai协程化
This commit is contained in:
86
parts/ai.lua
86
parts/ai.lua
@@ -8,6 +8,7 @@
|
||||
]]
|
||||
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 YIELD=YIELD
|
||||
-- controlname:
|
||||
-- 1~5:mL,mR,rR,rL,rF,
|
||||
-- 6~10:hD,sD,H,A,R,
|
||||
@@ -41,6 +42,7 @@ if _CC then
|
||||
setNode= _CC.set_max_nodes ,--(opt,bool)
|
||||
free= _CC.free ,--(opt/wei)
|
||||
}
|
||||
local CC=CC
|
||||
function CC.updateField(P)
|
||||
local F,i={},1
|
||||
for y=1,#P.field do
|
||||
@@ -196,8 +198,10 @@ local function getScore(field,cb,cy)
|
||||
end
|
||||
-------------------------------------------------
|
||||
return{
|
||||
["9S"]={
|
||||
function(P,ctrl)
|
||||
["9S"]=function(P,keys)
|
||||
while true do
|
||||
--Thinking
|
||||
YIELD()
|
||||
local Tfield={}--Test field
|
||||
local best={x=1,dir=0,hold=false,score=-1e99}--Best method
|
||||
local field_org=P.field
|
||||
@@ -224,22 +228,18 @@ return{
|
||||
local cy=#Tfield+1
|
||||
|
||||
--Move to bottom
|
||||
while true do
|
||||
if cy==1 then break end
|
||||
if not ifoverlapAI(Tfield,cb,cx,cy-1)then
|
||||
while cy>1 and not ifoverlapAI(Tfield,cb,cx,cy-1)do
|
||||
cy=cy-1
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
--Simulate lock
|
||||
for i=1,#cb do
|
||||
local y=cy+i-1
|
||||
if not Tfield[y]then Tfield[y]=getRow(0)end
|
||||
local L=Tfield[y]
|
||||
for j=1,#cb[1]do
|
||||
if cb[i][j]then
|
||||
Tfield[y][cx+j-1]=1
|
||||
L[cx+j-1]=1
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -258,42 +258,36 @@ return{
|
||||
while #Tfield>0 do
|
||||
discardRow(rem(Tfield,1))
|
||||
end
|
||||
local p=#ctrl+1
|
||||
if best.hold then
|
||||
ctrl[p]=8
|
||||
p=p+1
|
||||
ins(keys,8)
|
||||
end
|
||||
local l=FCL[best.bn][best.dir+1][best.x]
|
||||
for i=1,#l do
|
||||
ctrl[p]=l[i]
|
||||
p=p+1
|
||||
ins(keys,l[i])
|
||||
end
|
||||
ctrl[p]=6
|
||||
return 2
|
||||
end,
|
||||
function(P)
|
||||
ins(keys,6)
|
||||
|
||||
--Check if time to change target
|
||||
YIELD()
|
||||
if P:RND()<.00126 then
|
||||
P:changeAtkMode(rnd()<.85 and 1 or #P.atker>3 and 4 or rnd()<.3 and 2 or 3)
|
||||
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
|
||||
return 2
|
||||
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)
|
||||
if success then
|
||||
if result==2 then
|
||||
ins(ctrl,6)
|
||||
return 3
|
||||
ins(keys,6)
|
||||
break
|
||||
elseif result==0 then
|
||||
for i=1,#dest do
|
||||
for j=1,#dest[i]do
|
||||
@@ -301,30 +295,30 @@ return{
|
||||
end
|
||||
end
|
||||
P.AI_dest=dest
|
||||
if hold then ctrl[1]=8 end--Hold
|
||||
if hold then keys[1]=8 end--Hold
|
||||
while move[1]do
|
||||
local m=rem(move,1)
|
||||
if m<4 then
|
||||
ins(ctrl,m+1)
|
||||
ins(keys,m+1)
|
||||
elseif not P.AIdata._20G then
|
||||
ins(ctrl,13)
|
||||
ins(keys,13)
|
||||
end
|
||||
end
|
||||
ins(ctrl,6)
|
||||
return 3
|
||||
else
|
||||
--Stay this stage
|
||||
return 2
|
||||
ins(keys,6)
|
||||
break
|
||||
end
|
||||
else
|
||||
return 0
|
||||
goto ERR
|
||||
end
|
||||
end,
|
||||
function(P)--Check if time to change target
|
||||
end
|
||||
|
||||
--Check if time to change target
|
||||
YIELD()
|
||||
if P:RND()<.00126 then
|
||||
P:changeAtkMode(rnd()<.85 and 1 or #P.atker>3 and 4 or rnd()<.3 and 2 or 3)
|
||||
end
|
||||
return 1
|
||||
end
|
||||
::ERR::
|
||||
LOG.print("CC is dead ("..P.id..")","error")
|
||||
end,
|
||||
},
|
||||
}--AI think stage
|
||||
}--AI brains
|
||||
@@ -1424,7 +1424,6 @@ do--Player.drop(P)--Place piece
|
||||
end
|
||||
function Player.loadAI(P,data)--Load AI params
|
||||
P.AI_mode=data.type
|
||||
P.AI_stage=1
|
||||
P.AI_keys={}
|
||||
P.AI_delay=min(int(P.gameEnv.drop*.8),data.delta*rnd()*4)
|
||||
P.AI_delay0=data.delta
|
||||
@@ -1462,6 +1461,8 @@ function Player.loadAI(P,data)--Load AI params
|
||||
else
|
||||
P:setRS("TRS")
|
||||
end
|
||||
P.AI_thread=coroutine.create(AIFUNC[data.type])
|
||||
coroutine.resume(P.AI_thread,P,P.AI_keys)
|
||||
end
|
||||
--------------------------</Methods>--------------------------
|
||||
|
||||
|
||||
@@ -145,8 +145,8 @@ function update.alive(P,dt)
|
||||
local C=P.AI_keys
|
||||
P.AI_delay=P.AI_delay-1
|
||||
if not C[1]then
|
||||
if P.AI_stage then
|
||||
P.AI_stage=AIFUNC[P.AI_mode][P.AI_stage](P,C)
|
||||
if coroutine.status(P.AI_thread)=="suspended"then
|
||||
coroutine.resume(P.AI_thread)
|
||||
end
|
||||
elseif P.AI_delay<=0 then
|
||||
P:pressKey(C[1])P:releaseKey(C[1])
|
||||
|
||||
Reference in New Issue
Block a user