diff --git a/parts/ai.lua b/parts/ai.lua index b4fbcbe9..016aa36a 100644 --- a/parts/ai.lua +++ b/parts/ai.lua @@ -37,24 +37,43 @@ if type(_CC)=='table'then } local CC=CC function CC.updateField(P) + if not P.AI_thread then return end local F,n={},1 - for y=1,min(#P.field,40)do + local field=P.field + for y=1,min(#field,40)do + local sum=0 for x=1,10 do - F[n]=P.field[y][x]>0 + if field[y][x]>0 then + F[n]=true + sum=sum+1 + else + F[n]=false + end n=n+1 end + if sum==10 then + --[[ + --Print field and crash the game + for i=24,0,-1 do + local l=""for j=1,10 do l=l..(F[10*i+j]and"X"or"_")end print(l) + end + error("Row "..y.." full") + ]] + P.AI_thread=nil + return + end end while n<=400 do F[n]=false n=n+1 end if not pcall(CC.update,P.AI_bot,F,P.b2b>=100,P.combo)then - P.AI_bot=nil + P.AI_thread=nil end end function CC.switch20G(P) if not pcall(CC.destroy,P.AI_bot)then - P.AI_bot=nil + P.AI_thread=nil return end P.AIdata._20G=true @@ -281,20 +300,19 @@ return{ while true do --Start thinking yield() + if not P.AI_bot then break end if not pcall(CC.think,P.AI_bot)then break end --Poll keys local success,result,dest,hold,move repeat yield() + if not P.AI_bot then break end success,result,dest,hold,move=pcall(CC.getMove,P.AI_bot) until not success or result==0 or result==2 if not success then break end if result==2 then - while true do - yield() - ins(keys,6) - end + 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] diff --git a/parts/player/player.lua b/parts/player/player.lua index 29deb1ba..6b8acbe0 100644 --- a/parts/player/player.lua +++ b/parts/player/player.lua @@ -257,7 +257,7 @@ function Player:garbageRelease()--Check garbage buffer and try to release them break end end - if flag and self.AI_mode=='CC'and self.AI_bot then CC.updateField(self)end + if flag and self.AI_mode=='CC'then CC.updateField(self)end end function Player:garbageRise(color,amount,line)--Release n-lines garbage to field local _ @@ -518,32 +518,45 @@ function Player:freshBlock(mode)--string mode: push/move/fresh/newBlock end end function Player:lock() - local dest=self.AI_dest - local has_dest=dest~=nil - local CB=self.cur.bk - for i=1,#CB do - local y=self.curY+i-1 - if not self.field[y]then self.field[y],self.visTime[y]=FREEROW.get(0),FREEROW.get(0)end - for j=1,#CB[1]do - if CB[i][j]then - self.field[y][self.curX+j-1]=self.cur.color - self.visTime[y][self.curX+j-1]=self.showTime - if dest then + if self.AI_dest then + local dest=self.AI_dest + local CB=self.cur.bk + for i=1,#CB do + local y=self.curY+i-1 + if not self.field[y]then self.field[y],self.visTime[y]=FREEROW.get(0),FREEROW.get(0)end + for j=1,#CB[1]do + if CB[i][j]then + self.field[y][self.curX+j-1]=self.cur.color + self.visTime[y][self.curX+j-1]=self.showTime local x=self.curX+j-1 - for k=1,#dest,2 do - if x==dest[k]+1 and y==dest[k+1]+1 then - rem(dest,k)rem(dest,k) - goto BREAK_success + if dest then + for k=1,#dest,2 do + if x==dest[k]+1 and y==dest[k+1]+1 then + rem(dest,k)rem(dest,k) + goto BREAK_success + end end + dest=nil + ::BREAK_success:: end - dest=nil - ::BREAK_success:: end end end - end - if has_dest and not dest and self.AI_mode=='CC'and self.AI_bot then - CC.updateField(self) + if not dest and self.AI_mode=='CC'then + CC.updateField(self) + end + else + local CB=self.cur.bk + for i=1,#CB do + local y=self.curY+i-1 + if not self.field[y]then self.field[y],self.visTime[y]=FREEROW.get(0),FREEROW.get(0)end + for j=1,#CB[1]do + if CB[i][j]then + self.field[y][self.curX+j-1]=self.cur.color + self.visTime[y][self.curX+j-1]=self.showTime + end + end + end end end diff --git a/parts/player/update.lua b/parts/player/update.lua index e942475c..ce88c676 100644 --- a/parts/player/update.lua +++ b/parts/player/update.lua @@ -187,16 +187,20 @@ 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_thread and not pcall(P.AI_thread)then - P.AI_thread=false + if P.AI_thread then + if not pcall(P.AI_thread)then + P.AI_thread=nil + end + else + P:act_hardDrop() end elseif P.AI_delay<=0 then - P:pressKey(C[1])P:releaseKey(C[1]) if P.AI_mode~='CC'or C[1]>3 then P.AI_delay=P.AI_delay0*2 else P.AI_delay=P.AI_delay0*.5 end + P:pressKey(C[1])P:releaseKey(C[1]) rem(C,1) end end