Skip to content

Commit b1a3cae

Browse files
committed
不同用途的随机使用不同随机生成器,框架跟进
1 parent 5421812 commit b1a3cae

18 files changed

Lines changed: 134 additions & 128 deletions

assets/game/acryPlayer.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ function AP:tickStep()
582582
end
583583

584584
if IsKeyDown('f4') then
585+
-- TODO: debug
585586
local acry=F[1][1]
586587
if acry then
587588
print("--------------------------")

assets/game/basePlayer.lua

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ local sign,expApproach=MATH.sign,MATH.expApproach
1212
---@field isMain boolean
1313
---@field sound boolean
1414
---@field remote boolean
15+
---@field seed number
16+
---@field rngPool table<string, love.RandomGenerator>
1517
---@field settings Techmino.Mode.Setting.Brik | Techmino.Mode.Setting.Gela | Techmino.Mode.Setting.Acry
1618
---@field buffedKey table
1719
---@field modeData Techmino.PlayerModeData Warning: may contain anything, choose variable name carefully, suggested to be >=6 characters in total & multiple words (eg. `tspinCount`)
1820
---@field soundTimeHistory table
19-
---@field RND love.RandomGenerator
2021
---@field pos {x:number, y:number, k:number, a:number, dx:number, dy:number, dk:number, da:number, vx:number, vy:number, vk:number, va:number}
2122
---@field finished Techmino.EndReason | boolean Did game finish
2223
---@field realTime number Real time, [float] s
@@ -167,26 +168,30 @@ end
167168
--------------------------------------------------------------
168169
-- Game methods
169170

171+
function P:getRNG(t)
172+
if not self.rngPool[t] then self.rngPool[t]=love.math.newRandomGenerator(self.seed) end
173+
return self.rngPool[t]
174+
end
170175
---Random Int or 0~1
171-
function P:random(a,b)
172-
return self.RND:random(a,b)
176+
function P:random(t,a,b)
177+
return self:getRNG(t):random(a,b)
173178
end
174179
---Random Float
175-
function P:rand(a,b)
176-
return a+self.RND:random()*(b-a)
180+
function P:rand(t,a,b)
181+
return a+self:getRNG(t):random()*(b-a)
177182
end
178183
---Random value
179-
function P:coin(head,tail)
180-
return self.RND:random()>.5 and head or tail
184+
function P:coin(t,head,tail)
185+
return self:getRNG(t):random()>.5 and head or tail
181186
end
182187
---Random boolean
183-
function P:roll(chance)
184-
return self.RND:random()<(chance or .5)
188+
function P:roll(t,chance)
189+
return self:getRNG(t):random()<(chance or .5)
185190
end
186191
---Random int with custom weight
187-
function P:randFreq(fList)
192+
function P:randFreq(t,fList)
188193
local sum=MATH.sum(fList)
189-
local r=self.RND:random()*sum
194+
local r=self:getRNG(t):random()*sum
190195
for i=1,#fList do
191196
r=r-fList[i]
192197
if r<0 then return i end
@@ -753,13 +758,13 @@ function P.new(remote)
753758
self.isMain=false
754759
self.sound=false
755760
self.remote=not not remote
761+
self.seed=GAME.seed
762+
self.rngPool={}
756763

757764
self.buffedKey={}
758765
self.modeData={target={},music={id='intensity'}}
759766
self.soundTimeHistory=setmetatable({},soundTimeMeta)
760767

761-
self.RND=love.math.newRandomGenerator(GAME.seed+626)
762-
763768
self.pos={
764769
x=0,y=0,k=1,a=0,
765770

assets/game/brikPlayer.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -692,15 +692,15 @@ function BP:getBrik(shapeData)
692692
shapeID=shapeData.id
693693
shapeName=shapeData.name or "?"
694694
shapeMat=TABLE.copy(shapeData.shape)
695-
shapeColor=shapeData.color or self.settings.palette[shapeID] or self:random(64)
695+
shapeColor=shapeData.color or self.settings.palette[shapeID] or self:random('color',999)
696696
else
697697
---@cast shapeData Techmino.Brik.Name | Techmino.Brik.ID
698698
local brik=Brik.get(shapeData)
699699
if not brik then errorf("invalid shapeData %s",tostring(shapeData)) end
700700
shapeID=brik.id
701701
shapeName=brik.name
702702
shapeMat=TABLE.copy(brik.shape)
703-
shapeColor=self.settings.palette[shapeID]
703+
shapeColor=self.settings.palette[shapeID] or self:random('color',999)
704704
end
705705
self.pieceCount=self.pieceCount+1
706706

@@ -843,7 +843,7 @@ function BP:parseAtkInfo(g)
843843
local count=1+max((g.fatal-50)/20,0)
844844
local splitRate=0
845845
if count~=floor(count) then
846-
if self:random()>count%1 then
846+
if self:random('atkRound')>count%1 then
847847
splitRate=count%1/2
848848
count=floor(count)
849849
else
@@ -896,7 +896,7 @@ function BP:calculateHolePos(count,splitRate,copyRate,sandwichRate)
896896
-- end
897897
-- print(str)
898898

899-
local r=sum*self:random()
899+
local r=sum*self:random('attackHoleGen')
900900
for i=1,#weights do
901901
r=r-weights[i]
902902
if r<=0 then
@@ -1308,7 +1308,7 @@ function BP:riseGarbage(holePos,count)
13081308
L[holePos[i]]=false
13091309
end
13101310
else
1311-
L[self:random(w)]=false
1311+
L[self:random('garbageHoleGen',w)]=false
13121312
end
13131313

13141314
-- Add connection

assets/game/gelaPlayer.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ end
446446
function GP:shuffleColor(n)
447447
local list=self.settings.colorSet
448448
for i=n,2,-1 do
449-
local r=self:random(i)
449+
local r=self:random('shuffleColor',i)
450450
list[i],list[r]=list[r],list[i]
451451
end
452452
end
@@ -922,7 +922,7 @@ function GP:dropGarbage(count)
922922
F:setCell({
923923
color=555,
924924
diggable=true,
925-
},table.remove(pos,self:random(w+1-n)),curGenY)
925+
},table.remove(pos,self:random('garbageGen',w+1-n)),curGenY)
926926
end
927927
end
928928
end

assets/game/mechanicLib/brik/allclearGenerator.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ local pieceShapes do
3131
}
3232
end
3333
local function generateSolidGarbage(P,field,w,growRate,splitRate)
34-
local count=math.floor(P:rand(#field<3 and 1 or 0,growRate+1)*2)
35-
local pos=P:random(1,#field+1)
34+
local count=math.floor(P:rand('acGen',#field<3 and 1 or 0,growRate+1)*2)
35+
local pos=P:random('acGen',1,#field+1)
3636
while count>0 do
3737
table.insert(field,pos,TABLE.new(true,w))
38-
if P:random()<splitRate then
39-
pos=P:random()<.5 and pos-1 or pos+2
38+
if P:random('acGen')<splitRate then
39+
pos=P:random('acGen')<.5 and pos-1 or pos+2
4040
end
4141
pos=MATH.clamp(pos,1,#field+1)
4242
count=count-1
@@ -186,7 +186,7 @@ function allclearGenerator._shuffleSeq(P,seq,holdSlot)
186186
for i=1,pickArea do pointers[i]=i end
187187

188188
for i=1,#backup do
189-
local pick=P:random(#pointers)
189+
local pick=P:random('acGen',#pointers)
190190
seq[pointers[pick]]=backup[i]
191191
pointers[pick]=i+#pointers
192192
if pointers[pick]>#backup then
@@ -256,7 +256,7 @@ function allclearGenerator._generateQuestion(P,args)
256256
piece.score=(1+highRate*.62)^piece.y
257257
totalScore=totalScore+piece.score
258258
end
259-
local r=totalScore*P:random()
259+
local r=totalScore*P:random('acGen')
260260
local piece
261261
for i=1,#pieces do
262262
r=r-pieces[i].score
@@ -335,7 +335,7 @@ function allclearGenerator._getLibQuestion(P,args)
335335
local attempts=0
336336
local r
337337
while true do
338-
r=P:random(#seqPool)
338+
r=P:random('acGen',#seqPool)
339339
attempts=attempts+1
340340
if attempts>26 then break end
341341
if not args1.avoidRepeat then break end
@@ -349,7 +349,7 @@ function allclearGenerator._getLibQuestion(P,args)
349349

350350
field=TABLE.copy(field)
351351
seq=STRING.atomize(seq)
352-
if 1 or P:roll() then
352+
if P:roll('acGen') then
353353
for i=1,#seq do
354354
seq[i]=
355355
seq[i]=='Z' and 'S' or

assets/game/mechanicLib/brik/comboGenerator.lua

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ local comboGenerator={}
44
local function newMap(P)
55
local F=P.field
66
local w=P.settings.fieldW
7-
local difficulty=MATH.clamp(P.modeData.levelsCleared+1+P:random(-1,1),1,10)
8-
local height=10+math.floor((difficulty+1)/2)+P:random(-2,2)
7+
local difficulty=MATH.clamp(P.modeData.levelsCleared+1+P:random('comboGen',-1,1),1,10)
8+
local height=10+math.floor((difficulty+1)/2)+P:random('comboGen',-2,2)
99
local wellWidth=MATH.clamp(2+math.floor(difficulty/4),2,4)
10-
local widthExpandCounter=math.floor(15-difficulty)+P:random(-2,2)
10+
local widthExpandCounter=math.floor(15-difficulty)+P:random('comboGen',-2,2)
1111
local wellL,wellR
1212

1313
if wellWidth==3 then
1414
widthExpandCounter=widthExpandCounter+3-widthExpandCounter%3
1515
end
1616

17-
wellL=P:random(1,w+1-wellWidth)
17+
wellL=P:random('comboGen',1,w+1-wellWidth)
1818
wellR=wellL+wellWidth-1
1919

2020
TABLE.clear(F._matrix)
@@ -28,36 +28,36 @@ local function newMap(P)
2828
if wellR-wellL+1<4 then
2929
widthExpandCounter=widthExpandCounter-1
3030
if widthExpandCounter==0 then
31-
if P:random()<.5 then wellL=wellL-1 else wellR=wellR+1 end
31+
if P:random('comboGen')<.5 then wellL=wellL-1 else wellR=wellR+1 end
3232
if wellL<1 then wellL,wellR=wellL+1,wellR+1 end
3333
if wellR>w then wellL,wellR=wellL-1,wellR-1 end
3434
wellL,wellR=MATH.clamp(wellL,1,10),MATH.clamp(wellR,1,10)
35-
widthExpandCounter=math.floor(12-difficulty)+P:random(-1,2)
35+
widthExpandCounter=math.floor(12-difficulty)+P:random('comboGen',-1,2)
3636
end
3737
end
3838
end
3939

4040
-- 4w base
4141
if wellWidth==4 then
42-
if P:random()<.626 then -- 6-res
42+
if P:random('comboGen')<.626 then -- 6-res
4343
for x=wellL,wellR do
4444
for y=1,2 do
4545
F._matrix[y][x]=P:newCell(777)
4646
end
4747
end
48-
F._matrix[1][P:random(wellL,wellR)]=false
49-
F._matrix[2][P:random(wellL,wellR)]=false
48+
F._matrix[1][P:random('comboGen',wellL,wellR)]=false
49+
F._matrix[2][P:random('comboGen',wellL,wellR)]=false
5050
else -- 3-res
51-
if P:random()<.626 then -- Hook pattern
52-
local L=P:random()<.5
51+
if P:random('comboGen')<.626 then -- Hook pattern
52+
local L=P:random('comboGen')<.5
5353
F._matrix[1][L and wellL or wellR ]=P:newCell(777)
5454
F._matrix[2][L and wellL or wellR ]=P:newCell(777)
5555
F._matrix[2][L and wellL+1 or wellR-1]=P:newCell(777)
5656
else -- Flat
5757
for x=wellL,wellR do
5858
F._matrix[1][x]=P:newCell(777)
5959
end
60-
F._matrix[1][P:random(wellL,wellR)]=false
60+
F._matrix[1][P:random('comboGen',wellL,wellR)]=false
6161
end
6262
end
6363
end

assets/game/mechanicLib/brik/dig.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ local garbageTypes={
55
line=function(P)
66
if not P.modeData.dig_line_init then
77
P.modeData.dig_line_init=true
8-
P.modeData.risePosition=math.floor((P.settings.fieldW+1)/2+P:random())
9-
P.modeData.riseDirection=P:random(0,1)*2-1
8+
P.modeData.risePosition=math.floor((P.settings.fieldW+1)/2+P:random('dig'))
9+
P.modeData.riseDirection=P:random('dig',0,1)*2-1
1010
end
1111
P:riseGarbage(P.modeData.risePosition)
1212
if P.modeData.risePosition<=1 or P.modeData.risePosition>=P.settings.fieldW then
@@ -26,7 +26,7 @@ local garbageTypes={
2626
for i=1,#d do
2727
sum=sum+d[i]
2828
end
29-
local r=P:random(sum)
29+
local r=P:random('dig',sum)
3030
for x=1,#d do
3131
r=r-d[x]
3232
if r<=0 then
@@ -51,7 +51,7 @@ local garbageTypes={
5151
end,
5252
shale=function(P)
5353
P:riseGarbage(P:calculateHolePos(
54-
P:random(2,3), -- count
54+
P:random('dig',2,3), -- count
5555
-.2, -- splitRate
5656
-.1, -- copyRate
5757
-1 -- sandwichRate
@@ -60,7 +60,7 @@ local garbageTypes={
6060
end,
6161
volcanics=function(P)
6262
P:riseGarbage(P:calculateHolePos(
63-
P:random(3,4), -- count
63+
P:random('dig',3,4), -- count
6464
.2, -- splitRate
6565
-.1, -- copyRate
6666
.1 -- sandwichRate

assets/game/mechanicLib/brik/marathon.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ do -- hypersonic (they are variations of marathon, aren't they?)
365365
for x=1,P.settings.fieldW do
366366
local c=P.field:getCell(x,y)
367367
if c then
368-
c.visTimer=P:random(endVisTime1,endVisTime2)
368+
c.visTimer=P:random('marathon',endVisTime1,endVisTime2)
369369
c.fadeTime=endFadeTime
370370
end
371371
end
@@ -392,10 +392,10 @@ do -- hypersonic (they are variations of marathon, aren't they?)
392392
if md.flashTimer==0 then
393393
for y=1,min(P.field:getHeight(),2*P.settings.fieldW) do
394394
for x=1,P.settings.fieldW do
395-
if P:random()<flashProbability then
395+
if P:random('marathon')<flashProbability then
396396
local c=P.field:getCell(x,y)
397397
if c then
398-
c.visTimer=max(c.visTimer or 0,P:random(flashVisTime1,flashVisTime2))
398+
c.visTimer=max(c.visTimer or 0,P:random('marathon',flashVisTime1,flashVisTime2))
399399
c.fadeTime=max(c.fadeTime or 0,flashFadeTime)
400400
end
401401
end

0 commit comments

Comments
 (0)