玩家的随机数生成器独立为四个,尝试避免联网对战时不同步错误,整理代码

This commit is contained in:
MrZ626
2021-05-13 02:34:32 +08:00
parent adbb888fe4
commit cc1261c285
25 changed files with 82 additions and 77 deletions

View File

@@ -111,7 +111,10 @@ local function newEmptyPlayer(id,mini)
P.draw=ply_draw.norm
end
P.randGen=love.math.newRandomGenerator(GAME.seed)
P.seqRND=love.math.newRandomGenerator(GAME.seed)
P.atkRND=love.math.newRandomGenerator(GAME.seed)
P.holeRND=love.math.newRandomGenerator(GAME.seed)
P.aiRND=love.math.newRandomGenerator(GAME.seed)
P.frameRun=GAME.frameStart
P.alive=true

View File

@@ -121,10 +121,6 @@ end
--------------------------</FX>--------------------------
--------------------------<Method>--------------------------
function Player:RND(a,b)
local R=self.randGen
return R:random(a,b)
end
function Player:newTask(code,...)
local thread=coroutine.create(code)
resume(thread,self,...)
@@ -227,11 +223,11 @@ end
function Player:getHolePos()--Get a good garbage-line hole position
if self.garbageBeneath==0 then
return generateLine(self:RND(10))
return generateLine(self.holeRND:random(10))
else
local p=self:RND(10)
local p=self.holeRND:random(10)
if self.field[1][p]<=0 then
return generateLine(self:RND(10))
return generateLine(self.holeRND:random(10))
end
return generateLine(p)
end
@@ -1336,7 +1332,7 @@ do--Player.drop(self)--Place piece
local M=#self.atker
if M>0 then
for i=1,M do
self:attack(self.atker[i],send,sendTime,generateLine(self:RND(10)))
self:attack(self.atker[i],send,sendTime,generateLine(self.atkRND:random(10)))
end
else
T=randomTarget(self)
@@ -1349,7 +1345,7 @@ do--Player.drop(self)--Place piece
T=randomTarget(self)
end
if T then
self:attack(T,send,sendTime,generateLine(self:RND(10)))
self:attack(T,send,sendTime,generateLine(self.atkRND:random(10)))
end
end
if self.sound and send>3 then SFX.play('emit',min(send,7)*.1)end
@@ -1709,8 +1705,7 @@ function Player:lose(force)
if self.type=='remote'and not force then self.waiting=1e99 return end
if self.life>0 and not force then self:revive()return end
self:die()
local i=TABLE.find(PLY_ALIVE,self)
if i then rem(PLY_ALIVE,i)end
local p=TABLE.find(PLY_ALIVE,self)if p then rem(PLY_ALIVE,p)end
self.result='lose'
if GAME.modeEnv.royaleMode then
self:changeAtk()

View File

@@ -5,6 +5,7 @@ local yield=YIELD
local seqGenerators={
none=function()while true do yield()end end,
bag=function(P,seq0)
local rndGen=P.seqRND
local len=#seq0
local bag={}
while true do
@@ -14,12 +15,13 @@ local seqGenerators={
bag[i]=seq0[len-i+1]
end
end
P:getNext(rem(bag,P:RND(#bag)))
P:getNext(rem(bag,rndGen:random(#bag)))
end
yield()
end
end,
his=function(P,seq0)
local rndGen=P.seqRND
local len=#seq0
local hisLen=ceil(len*.5)
local history=TABLE.new(0,hisLen)
@@ -27,7 +29,7 @@ local seqGenerators={
while #P.nextQueue<6 do
local r
for _=1,hisLen do--Reroll up to [hisLen] times
r=P:RND(len)
r=rndGen:random(len)
for i=1,hisLen do
if r==history[i]then
goto CONTINUE_rollAgain
@@ -43,6 +45,7 @@ local seqGenerators={
end
end,
hisPool=function(P,seq0)
local rndGen=P.seqRND
local len=#seq0
local hisLen=ceil(len*.5)
local history=TABLE.new(0,hisLen)--Indexes of mino-index
@@ -51,7 +54,7 @@ local seqGenerators={
local droughtTimes=TABLE.new(len,len)--Drought times of seq0
local pool={}for i=1,len do for _=1,5 do ins(pool,i)end end--5 times indexes of seq0
local function poolPick()
local r=P:RND(poolLen)
local r=rndGen:random(poolLen)
local res=pool[r]
--Find droughtest(s) minoes
@@ -72,7 +75,7 @@ local seqGenerators={
--Update pool
-- print("Rem "..res)
pool[r]=droughtList[P:RND(#droughtList)]
pool[r]=droughtList[rndGen:random(#droughtList)]
-- print("Add "..pool[r])
return res
@@ -105,6 +108,7 @@ local seqGenerators={
end
end,
c2=function(P,seq0)
local rndGen=P.seqRND
local len=#seq0
local weight={}
for i=1,len do weight[i]=0 end
@@ -113,7 +117,7 @@ local seqGenerators={
while #P.nextQueue<6 do
local maxK=1
for i=1,len do
weight[i]=weight[i]*.5+P:RND()
weight[i]=weight[i]*.5+rndGen:random()
if weight[i]>weight[maxK]then
maxK=i
end
@@ -127,13 +131,14 @@ local seqGenerators={
rnd=function(P,seq0)
if #seq0==1 then
local i=seq0[1]
while true do P:getNext(i) yield() end
while true do P:getNext(i)yield()end
else
local rndGen=P.seqRND
local len=#seq0
local last=0
while true do
while #P.nextQueue<6 do
local r=P:RND(len-1)
local r=rndGen:random(len-1)
if r>=last then r=r+1 end
P:getNext(seq0[r])
last=r
@@ -143,32 +148,34 @@ local seqGenerators={
end
end,
mess=function(P,seq0)
local rndGen=P.seqRND
while true do
while #P.nextQueue<6 do
P:getNext(seq0[P:RND(#seq0)])
P:getNext(seq0[rndGen:random(#seq0)])
end
yield()
end
end,
reverb=function(P,seq0)
local rndGen=P.seqRND
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 r=rem(bufferSeq,rndGen:random(#bag))
local p=1
repeat
ins(bag,r)
p=p-.15-P:RND()
p=p-.15-rndGen:random()
until p<0
until #bufferSeq==0
for i=1,#bag do
bufferSeq[i]=bag[i]
end
end
P:getNext(rem(bag,P:RND(#bag)))
P:getNext(rem(bag,rndGen:random(#bag)))
end
yield()
end