序列生成器改用协程实现,整理代码

This commit is contained in:
MrZ626
2021-03-15 20:42:03 +08:00
parent 7fd8a23f55
commit 459bd9ad31
10 changed files with 206 additions and 213 deletions

View File

@@ -37,8 +37,7 @@ return{
ospin=true,deepDrop=false,
RS="TRS",
sequence="bag",
freshMethod=NULL,
bag={1,2,3,4,5,6,7},
seqData={1,2,3,4,5,6,7},
face=NULL,skin=NULL,
mission=NULL,

View File

@@ -142,7 +142,6 @@ local function newEmptyPlayer(id,mini)
P.holdQueue={}
P.holdTime=0
P.nextQueue={}
P.seqData={}
P.freshTime=0
P.spinLast=false
@@ -296,10 +295,11 @@ local function applyGameEnv(P)--Finish gameEnv processing
if ENV.sequence~="bag"and ENV.sequence~="loop"then
ENV.bagLine=false
else
ENV.bagLen=#ENV.bag
ENV.bagLen=#ENV.seqData
end
if ENV.nextCount==0 then ENV.nextPos=false end
prepareSequence(P)
if P.mini then
ENV.lockFX=false
@@ -355,7 +355,6 @@ function PLY.newDemoPlayer(id)
GAME.modeEnv=DemoEnv
loadGameEnv(P)
applyGameEnv(P)
prepareSequence(P)
P:loadAI{
type="CC",
next=5,
@@ -385,7 +384,6 @@ function PLY.newRemotePlayer(id,mini,playerData)
loadRemoteEnv(P,playerData.conf)
applyGameEnv(P)
prepareSequence(P)
end
function PLY.newAIPlayer(id,AIdata,mini)
@@ -397,7 +395,6 @@ function PLY.newAIPlayer(id,AIdata,mini)
ENV.face={0,0,0,0,0,0,0}
ENV.skin={1,7,11,3,14,4,9}
applyGameEnv(P)
prepareSequence(P)
P:loadAI(AIdata)
end
function PLY.newPlayer(id,mini)
@@ -407,7 +404,6 @@ function PLY.newPlayer(id,mini)
loadGameEnv(P)
applyGameEnv(P)
prepareSequence(P)
end
--------------------------</Public>--------------------------
return PLY

View File

@@ -1,13 +1,13 @@
------------------------------------------------------
--Notice: anything in this file or in any other file,
--var P stands for Player object. Don't forget that.
--local var P stands for Player object. Don't forget it.
------------------------------------------------------
local Player={}--Player class
local int,ceil,rnd=math.floor,math.ceil,math.random
local max,min,modf=math.max,math.min,math.modf
local ins,rem=table.insert,table.remove
local YIELD=YIELD
local resume,yield=coroutine.resume,coroutine.yield
local kickList=require"parts.kickList"
@@ -147,7 +147,7 @@ end
local function task_movePosition(P,x,y,size)
local x1,y1,size1=P.x,P.y,P.size
while true do
YIELD()
yield()
if (x1-x)^2+(y1-y)^2<1 then
P:setPosition(x,y,size)
return true
@@ -750,7 +750,7 @@ function Player.popNext(P,ifhold)--Pop nextQueue to hand
P.ctrlCount=0
P.cur=rem(P.nextQueue,1)
P:newNext()
assert(resume(P.newNext))
if P.cur then
P.pieceCount=P.pieceCount+1
if P.AI_mode=="CC"then
@@ -1516,7 +1516,7 @@ end
--------------------------<Ticks>--------------------------
local function tick_throwBadge(ifAI,sender,time)
while true do
YIELD()
yield()
time=time-1
if time%4==0 then
local S,R=sender,sender.lastRecv
@@ -1544,7 +1544,7 @@ local function tick_throwBadge(ifAI,sender,time)
end
local function tick_finish(P)
while true do
YIELD()
yield()
P.endCounter=P.endCounter+1
if P.endCounter<40 then
--Make field visible
@@ -1558,7 +1558,7 @@ local function tick_finish(P)
end
local function tick_lose(P)
while true do
YIELD()
yield()
P.endCounter=P.endCounter+1
if P.endCounter<40 then
--Make field visible
@@ -1591,7 +1591,7 @@ end
function tick_autoPause()
local time=0
while true do
YIELD()
yield()
time=time+1
if SCN.cur~="play"or GAME.frame<180 then
return

View File

@@ -1,132 +1,130 @@
local rnd=math.random
local ins,rem=table.insert,table.remove
local yield=coroutine.yield
local freshMethod
local freshPrepare={
rnd=function(P)
local bag=P.gameEnv.bag
P:getNext(bag[rnd(#bag)])
freshMethod.rnd(P)
end,
fixed=function(P)
local bag=P.gameEnv.bag
local L=#bag
for i=1,L do
P.seqData[i]=bag[L+1-i]
end
while #P.nextQueue<6 do
if P.seqData[1]then
P:getNext(rem(P.seqData))
else
break
local sequenceModes={
none=function() while true do yield()end end,
bag=function(P,seq0)
local len=#seq0
local bag={}
while true do
while #P.nextQueue<6 do
if #bag==0 then
for i=1,len do
bag[i]=seq0[len-i+1]
end
end
P:getNext(rem(bag,rnd(#bag)))
end
yield()
end
end,
}
freshMethod={
none=NULL,
bag=function(P)
local bag=P.seqData
while #P.nextQueue<6 do
if #bag==0 then--Copy a new bag
local bag0=P.gameEnv.bag
for i=1,#bag0 do bag[i]=bag0[i]end
end
P:getNext(rem(bag,P:RND(#bag)))
end
end,
his4=function(P)
while #P.nextQueue<6 do
local bag=P.gameEnv.bag
local L=#bag
for n=1,4 do
local j,i=0
repeat
i=bag[P:RND(L)]
j=j+1
until i~=P.seqData[1]and i~=P.seqData[2]and i~=P.seqData[3]and i~=P.seqData[4]or j==4
P.seqData[n]=i
P:getNext(i)
end
end
end,
rnd=function(P)
while #P.nextQueue<6 do
local bag=P.gameEnv.bag
local L=#bag
for i=1,4 do
local count=0
repeat
i=bag[P:RND(L)]
count=count+1
until i~=P.nextQueue[#P.nextQueue].id or count>=L
P:getNext(i)
end
end
end,
reverb=function(P)
local seq=P.seqData
while #P.nextQueue<6 do
if #seq==0 then
local bag0=P.gameEnv.bag
for i=1,#bag0 do seq[i]=bag0[i]end
local bag={}
repeat
local r=rem(seq,P:RND(#bag))
local p=1
his4=function(P,seq0)
local len=#seq0
while true do
while #P.nextQueue<6 do
for n=1,4 do
local j,i=0
repeat
ins(bag,r)
p=p-.15-P:RND()
until p<0
until #seq==0
for i=1,#bag do
seq[i]=bag[i]
i=seq0[P:RND(len)]
j=j+1
until i~=seq0[1]and i~=seq0[2]and i~=seq0[3]and i~=seq0[4]or j==4
seq0[n]=i
P:getNext(i)
end
end
P:getNext(rem(seq))
yield()
end
end,
loop=function(P)
while #P.nextQueue<6 do
if #P.seqData==0 then
local bag=P.gameEnv.bag
local L=#bag
for i=1,L do
P.seqData[i]=bag[L+1-i]
rnd=function(P,seq0)
P:getNext(seq0[rnd(#seq0)])
while true do
while #P.nextQueue<6 do
local len=#seq0
for i=1,4 do
local count=0
repeat
i=seq0[P:RND(len)]
count=count+1
until i~=P.nextQueue[#P.nextQueue].id or count>=len
P:getNext(i)
end
end
P:getNext(rem(P.seqData))
yield()
end
end,
fixed=function(P)
while #P.nextQueue<6 do
if P.seqData[1]then
P:getNext(rem(P.seqData))
else
if not(P.cur or P.holdQueue[1])then P:lose(true)end
return
reverb=function(P,seq0)
local bufferSeq,bag={},{}
while true do
while #P.nextQueue<6 do
if #bag==0 then
for i=1,#seq0 do bufferSeq[i]=seq0[i]end
repeat
local r=rem(bufferSeq,P:RND(#bag))
local p=1
repeat
ins(bag,r)
p=p-.15-P:RND()
until p<0
until #bufferSeq==0
for i=1,#bag do
bufferSeq[i]=bag[i]
end
end
P:getNext(rem(bag,rnd(#bag)))
end
yield()
end
end,
loop=function(P,seq0)
local len=#seq0
local bag={}
while true do
while #P.nextQueue<6 do
if #bag==0 then
for i=1,len do
bag[i]=seq0[len-i+1]
end
end
P:getNext(rem(bag))
end
yield()
end
end,
fixed=function(P,seq0)
local seq={}
for i=#seq0,1,-1 do
ins(seq,seq0[i])
end
while true do
while #P.nextQueue<6 do
if not(seq[1]or P.cur or P.holdQueue[1])then
P:lose(true)
break
end
P:getNext(rem(seq))
end
yield()
end
end,
}
local function prepareSequence(P)--Call freshPrepare and set newNext
return function(P)--Set newNext funtion for player P
local ENV=P.gameEnv
::tryAgain::
if type(ENV.sequence)=="string"then
P.newNext=freshMethod[ENV.sequence]
if freshPrepare[ENV.sequence]then
freshPrepare[ENV.sequence](P)
if sequenceModes[ENV.sequence]then
P.newNext=coroutine.create(sequenceModes[ENV.sequence])
else
P:newNext()
end
else
if type(ENV.freshMethod)=="function"then
if ENV.sequence then ENV.sequence(P)end
P.newNext=ENV.freshMethod
else
LOG.print("Wrong sequence generator code","warn")
LOG.print("No sequence mode called "..ENV.sequence,"warn")
ENV.sequence="bag"
prepareSequence(P)
goto tryAgain
end
elseif type(ENV.sequence)=="function"then
P.newNext=coroutine.create(ENV.sequence)
else
LOG.print("Wrong sequence generator","warn")
ENV.sequence="bag"
goto tryAgain
end
end
return prepareSequence
assert(coroutine.resume(P.newNext,P,ENV.seqData))
end