优化性能:ai思考和绘制玩家场地

This commit is contained in:
MrZ626
2020-10-23 17:51:38 +08:00
parent ac06b08861
commit 8bc2d958e6
2 changed files with 25 additions and 22 deletions

View File

@@ -109,19 +109,19 @@ local FCL={
local LclearScore={[0]=0,-200,-120,-80,200} local LclearScore={[0]=0,-200,-120,-80,200}
local HclearScore={[0]=0,100,140,200,500} local HclearScore={[0]=0,100,140,200,500}
local function ifoverlapAI(f,bk,x,y) local function ifoverlapAI(f,bk,x,y)
if y<1 then return true end
if y>#f then return end
for i=1,#bk do for j=1,#bk[1]do for i=1,#bk do for j=1,#bk[1]do
if f[y+i-1]and bk[i][j]and f[y+i-1][x+j-1]>0 then return true end if f[y+i-1]and bk[i][j]and f[y+i-1][x+j-1]>0 then return true end
end end end end
end end
local discardRow=freeRow.discard
local getRow=freeRow.get
local function resetField(f0,f,start) local function resetField(f0,f,start)
for _=#f,start,-1 do for _=#f,start,-1 do
freeRow.discard(f[_]) discardRow(f[_])
f[_]=nil f[_]=nil
end end
for i=start,#f0 do for i=start,#f0 do
f[i]=freeRow.get(0) f[i]=getRow(0)
for j=1,10 do for j=1,10 do
f[i][j]=f0[i][j] f[i][j]=f0[i][j]
end end
@@ -130,7 +130,7 @@ end
local function getScore(field,cb,cy) local function getScore(field,cb,cy)
local score=0 local score=0
local highest=0 local highest=0
local height=freeRow.get(0) local height=getRow(0)
local clear=0 local clear=0
local hole=0 local hole=0
@@ -138,7 +138,7 @@ local function getScore(field,cb,cy)
for j=1,10 do for j=1,10 do
if field[i][j]==0 then goto L end if field[i][j]==0 then goto L end
end end
freeRow.discard(rem(field,i)) discardRow(rem(field,i))
clear=clear+1 clear=clear+1
::L:: ::L::
end end
@@ -171,7 +171,7 @@ local function getScore(field,cb,cy)
end end
sdh=sdh+min(dh^1.6,20) sdh=sdh+min(dh^1.6,20)
end end
freeRow.discard(height) discardRow(height)
score= score=
-#field*30 -#field*30
-#cb*15 -#cb*15
@@ -198,7 +198,7 @@ return{
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
for i=1,#field_org do for i=1,#field_org do
Tfield[i]=freeRow.get(0) Tfield[i]=getRow(0)
for j=1,10 do for j=1,10 do
Tfield[i][j]=field_org[i][j] Tfield[i][j]=field_org[i][j]
end end
@@ -220,14 +220,19 @@ return{
local cy=#Tfield+1 local cy=#Tfield+1
--Move to bottom --Move to bottom
while not ifoverlapAI(Tfield,cb,cx,cy-1)do while true 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]=freeRow.get(0)end if not Tfield[y]then Tfield[y]=getRow(0)end
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 Tfield[y][cx+j-1]=1
@@ -247,7 +252,7 @@ return{
--Release cache --Release cache
while #Tfield>0 do while #Tfield>0 do
freeRow.discard(rem(Tfield,1)) discardRow(rem(Tfield,1))
end end
local p=#ctrl+1 local p=#ctrl+1
if best.hold then if best.hold then

View File

@@ -410,9 +410,7 @@ local frameColor={
color.lPurple, color.lPurple,
color.lOrange, color.lOrange,
} }
local function drawCell(y,x,id) --local function drawCell(y,x,id)gc.draw(blockSkin[id],30*x-30,-30*y)end
gc.draw(blockSkin[id],30*x-30,-30*y)
end
local function drawGrid(P) local function drawGrid(P)
local FBN,FUP=P.fieldBeneath,P.fieldUp local FBN,FUP=P.fieldBeneath,P.fieldUp
gc.setLineWidth(1) gc.setLineWidth(1)
@@ -435,7 +433,7 @@ local function drawField(P)
if F[j][i]>0 then if F[j][i]>0 then
if V[j][i]>0 then if V[j][i]>0 then
gc.setColor(1,1,1,min(V[j][i]*.05,1)) gc.setColor(1,1,1,min(V[j][i]*.05,1))
drawCell(j,i,F[j][i]) gc.draw(blockSkin[F[j][i]],30*i-30,-30*j)-- drawCell(j,i,F[j][i])
elseif rep then elseif rep then
gc.setColor(1,1,1,.3+.08*sin(.5*(j-i)+Timer()*4)) gc.setColor(1,1,1,.3+.08*sin(.5*(j-i)+Timer()*4))
gc.rectangle("fill",30*i-30,-30*j,30,30) gc.rectangle("fill",30*i-30,-30*j,30,30)
@@ -460,7 +458,7 @@ local function drawField(P)
if F[j][i]>0 then if F[j][i]>0 then
if V[j][i]>0 then if V[j][i]>0 then
gc.setColor(1,1,1,min(V[j][i]*.05,1)) gc.setColor(1,1,1,min(V[j][i]*.05,1))
drawCell(j,i,F[j][i]) gc.draw(blockSkin[F[j][i]],30*i-30,-30*j)-- drawCell(j,i,F[j][i])
elseif rep then elseif rep then
gc.setColor(1,1,1,.2) gc.setColor(1,1,1,.2)
gc.rectangle("fill",30*i-30,-30*j,30,30) gc.rectangle("fill",30*i-30,-30*j,30,30)
@@ -496,7 +494,7 @@ local function drawFXs(P)
for i=1,#P.moveFX do for i=1,#P.moveFX do
local S=P.moveFX[i] local S=P.moveFX[i]
gc.setColor(1,1,1,.6-S[4]*.6) gc.setColor(1,1,1,.6-S[4]*.6)
drawCell(S[3],S[2],S[1]) gc.draw(blockSkin[S[1]],30*S[2]-30,-30*S[3])-- drawCell(S[3],S[2],S[1])
end end
--ClearFX --ClearFX
@@ -513,7 +511,7 @@ local function drawGhost(P,clr)
gc.setColor(1,1,1,P.gameEnv.ghost) gc.setColor(1,1,1,P.gameEnv.ghost)
for i=1,P.r do for j=1,P.c do for i=1,P.r do for j=1,P.c do
if P.cur.bk[i][j]then if P.cur.bk[i][j]then
drawCell(i+P.imgY-1,j+P.curX-1,clr) gc.draw(blockSkin[clr],30*(j+P.curX-1)-30,-30*(i+P.imgY-1))-- drawCell(i+P.imgY-1,j+P.curX-1,clr)
end end
end end end end
end end
@@ -535,7 +533,7 @@ local function drawBlock(P,clr)
gc.setColor(1,1,1) gc.setColor(1,1,1)
for i=1,P.r do for j=1,P.c do for i=1,P.r do for j=1,P.c do
if P.cur.bk[i][j]then if P.cur.bk[i][j]then
drawCell(i+P.curY-1,j+P.curX-1,clr) gc.draw(blockSkin[clr],30*(j+P.curX-1)-30,-30*(i+P.curY-1))-- drawCell(i+P.curY-1,j+P.curX-1,clr)
end end
end end end end
end end
@@ -554,7 +552,7 @@ local function drawHold(P,clr)
local B=P.hd.bk local B=P.hd.bk
for i=1,#B do for j=1,#B[1]do for i=1,#B do for j=1,#B[1]do
if B[i][j]then if B[i][j]then
drawCell(i+1.36-#B*.5,j+2.06-#B[1]*.5,clr) gc.draw(blockSkin[clr],30*(j+2.06-#B[1]*.5)-30,-30*(i+1.36-#B*.5))-- drawCell(i+1.36-#B*.5,j+2.06-#B[1]*.5,clr)
end end
end end end end
end end
@@ -752,7 +750,7 @@ local Pdraw_norm do
local b,c=P.next[N].bk,P.next[N].color local b,c=P.next[N].bk,P.next[N].color
for i=1,#b do for j=1,#b[1] do for i=1,#b do for j=1,#b[1] do
if b[i][j]then if b[i][j]then
drawCell(i-2.4*N-#b*.5,j+12.6-#b[1]*.5,c) gc.draw(blockSkin[c],30*(j+12.6-#b[1]*.5)-30,-30*(i-2.4*N-#b*.5))-- drawCell(i-2.4*N-#b*.5,j+12.6-#b[1]*.5,c)
end end
end end end end
N=N+1 N=N+1