simplify sequence generator

This commit is contained in:
Imple Lee
2023-07-11 19:50:52 +08:00
parent b3a8724538
commit b7b02ab5bc
5 changed files with 183 additions and 213 deletions

View File

@@ -3,24 +3,19 @@ local yield=coroutine.yield
local seqGenerators={
none=function() while true do yield() end end,
bag=function(P,seq0)
local rndGen=P.seqRND
bag=function(rndGen,seq0)
local len=#seq0
local bag={}
while true do
while #P.nextQueue<10 do
if #bag==0 then
for i=1,len do
bag[i]=seq0[len-i+1]
end
if #bag==0 then
for i=1,len do
bag[i]=seq0[len-i+1]
end
P:getNext(rem(bag,rndGen:random(#bag)))
end
yield()
yield(rem(bag,rndGen:random(#bag)))
end
end,
bagES=function(P,seq0)
local rndGen=P.seqRND
bagES=function(rndGen,seq0)
local len=#seq0
local bag=TABLE.shift(seq0)
do-- Get a good first-bag
@@ -40,52 +35,44 @@ local seqGenerators={
end
end
-- Finish
for i=1,len do P:getNext(bag[i]) end
for i=1,len do yield(bag[i]) end
end
bag={}
while true do
while #P.nextQueue<10 do
if #bag==0 then
for i=1,len do
bag[i]=seq0[len-i+1]
end
if #bag==0 then
for i=1,len do
bag[i]=seq0[len-i+1]
end
P:getNext(rem(bag,rndGen:random(#bag)))
end
yield()
yield(rem(bag,rndGen:random(#bag)))
end
end,
his=function(P,seq0)
local rndGen=P.seqRND
his=function(rndGen,seq0)
local len=#seq0
local hisLen=math.ceil(len*.5)
local history=TABLE.new(0,hisLen)
while true do
while #P.nextQueue<10 do
local r
for _=1,hisLen do-- Reroll up to [hisLen] times
r=rndGen:random(len)
local rollAgain
for i=1,hisLen do
if r==history[i] then
rollAgain=true
break-- goto CONTINUE_rollAgain
end
local r
for _=1,hisLen do-- Reroll up to [hisLen] times
r=rndGen:random(len)
local rollAgain
for i=1,hisLen do
if r==history[i] then
rollAgain=true
break-- goto CONTINUE_rollAgain
end
if not rollAgain then break end
-- ::CONTINUE_rollAgain::
end
if history[1]~=0 then
P:getNext(seq0[r])
end
rem(history,1)
ins(history,r)
if not rollAgain then break end
-- ::CONTINUE_rollAgain::
end
yield()
if history[1]~=0 then
yield(seq0[r])
end
rem(history,1)
ins(history,r)
end
end,
hisPool=function(P,seq0)
local rndGen=P.seqRND
hisPool=function(rndGen,seq0)
local len=#seq0
local hisLen=math.ceil(len*.5)
local history=TABLE.new(0,hisLen)-- Indexes of mino-index
@@ -126,147 +113,113 @@ local seqGenerators={
end
while true do
while #P.nextQueue<10 do
-- print"======================"
-- Pick a mino from pool
local tryTime=0
local r
repeat-- ::REPEAT_pickAgain::
local pickAgain
r=_poolPick()-- Random mino-index in pool
for i=1,len do
if r==history[i] then
tryTime=tryTime+1
if tryTime<hisLen then
pickAgain=true
break-- goto REPEAT_pickAgain
end
-- print"======================"
-- Pick a mino from pool
local tryTime=0
local r
repeat-- ::REPEAT_pickAgain::
local pickAgain
r=_poolPick()-- Random mino-index in pool
for i=1,len do
if r==history[i] then
tryTime=tryTime+1
if tryTime<hisLen then
pickAgain=true
break-- goto REPEAT_pickAgain
end
end
if not pickAgain then break end
until true
-- Give mino to player & update history
if history[1]~=0 then
P:getNext(seq0[r])
end
rem(history,1)
ins(history,r)
-- print("Player GET: "..r)
-- print("History: "..table.concat(history,","))
-- local L=TABLE.new("",len)
-- for _,v in next,pool do L[v]=L[v].."+" end
-- for i=1,#L do print(i,droughtTimes[i],L[i]) end
if not pickAgain then break end
until true
-- Give mino to player & update history
if history[1]~=0 then
yield(seq0[r])
end
yield()
rem(history,1)
ins(history,r)
-- print("Player GET: "..r)
-- print("History: "..table.concat(history,","))
-- local L=TABLE.new("",len)
-- for _,v in next,pool do L[v]=L[v].."+" end
-- for i=1,#L do print(i,droughtTimes[i],L[i]) end
end
end,
c2=function(P,seq0)
local rndGen=P.seqRND
c2=function(rndGen,seq0)
local len=#seq0
local weight=TABLE.new(0,len)
while true do
while #P.nextQueue<10 do
local maxK=1
for i=1,len do
weight[i]=weight[i]*.5+rndGen:random()
if weight[i]>weight[maxK] then
maxK=i
end
local maxK=1
for i=1,len do
weight[i]=weight[i]*.5+rndGen:random()
if weight[i]>weight[maxK] then
maxK=i
end
weight[maxK]=weight[maxK]/3.5
P:getNext(seq0[maxK])
end
yield()
weight[maxK]=weight[maxK]/3.5
yield(seq0[maxK])
end
end,
rnd=function(P,seq0)
rnd=function(rndGen,seq0)
if #seq0==1 then
local i=seq0[1]
while true do
while #P.nextQueue<10 do P:getNext(i) end
yield()
yield(i)
end
else
local rndGen=P.seqRND
local len=#seq0
local last=0
while true do
while #P.nextQueue<10 do
local r=rndGen:random(len-1)
if r>=last then
r=r+1
end
P:getNext(seq0[r])
last=r
local r=rndGen:random(len-1)
if r>=last then
r=r+1
end
yield()
yield(seq0[r])
last=r
end
end
end,
mess=function(P,seq0)
local rndGen=P.seqRND
mess=function(rndGen,seq0)
while true do
while #P.nextQueue<10 do
P:getNext(seq0[rndGen:random(#seq0)])
end
yield()
yield(seq0[rndGen:random(#seq0)])
end
end,
reverb=function(P,seq0)
local rndGen=P.seqRND
reverb=function(rndGen,seq0)
local bufferSeq,bag={},{}
while true do
while #P.nextQueue<10 do
if #bag==0 then
for i=1,#seq0 do bufferSeq[i]=seq0[i] end
if #bag==0 then
for i=1,#seq0 do bufferSeq[i]=seq0[i] end
repeat
local r=rem(bufferSeq,rndGen:random(#bag))
local p=1
repeat
local r=rem(bufferSeq,rndGen:random(#bag))
local p=1
repeat
ins(bag,r)
p=p-.15-rndGen:random()
until p<0
until #bufferSeq==0
for i=1,#bag do
bufferSeq[i]=bag[i]
end
ins(bag,r)
p=p-.15-rndGen:random()
until p<0
until #bufferSeq==0
for i=1,#bag do
bufferSeq[i]=bag[i]
end
P:getNext(rem(bag,rndGen:random(#bag)))
end
yield()
yield(rem(bag,rndGen:random(#bag)))
end
end,
loop=function(P,seq0)
loop=function(rndGen,seq0)
local len=#seq0
local bag={}
while true do
while #P.nextQueue<10 do
if #bag==0 then
for i=1,len do
bag[i]=seq0[len-i+1]
end
if #bag==0 then
for i=1,len do
bag[i]=seq0[len-i+1]
end
P:getNext(rem(bag))
end
yield()
yield(rem(bag))
end
end,
fixed=function(P,seq0)
local seq={}
fixed=function(rndGen,seq0)
for i=#seq0,1,-1 do
ins(seq,seq0[i])
end
while true do
while #P.nextQueue<10 do
if seq[1] then
P:getNext(rem(seq))
else
break
end
end
yield()
yield(seq0[i])
end
end,
}