Skip to content

Commit c79e7fc

Browse files
evgTSVForNeVeR
authored andcommitted
Rollback changes depended on seq id gen, fix jump function
1 parent 1f5fc6a commit c79e7fc

File tree

4 files changed

+46
-30
lines changed

4 files changed

+46
-30
lines changed

O21.Game/Engine/RandomGenerator.fs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ type RandomGenerator(seed: int64) =
4444

4545
do initializeState(seed)
4646

47-
member this.Seed = seed
47+
member private _.State = randState
48+
member _.Seed = seed
4849

4950
member this.Next() : uint64 =
5051
let result = rotl (randState[0] + randState[3]) 23 + randState[0]
@@ -108,18 +109,22 @@ type RandomGenerator(seed: int64) =
108109
invalidArg (nameof(state)) "State must have exactly 4 elements"
109110
state.CopyTo randState
110111

112+
static member FromState(state: Span<uint64>) =
113+
let generator = RandomGenerator(0)
114+
generator.LoadState(state)
115+
generator
116+
111117
member private this.JumpBase(jumps: uint64[]) =
118+
let rand = RandomGenerator.FromState(randState)
112119
let newState = stackalloc<uint64> 4
113120
for i in 0 .. 3 do
114121
for b in 0 .. 63 do
115122
if (jumps[i] &&& (1UL <<< b)) <> 0UL then
116123
for j in 0 .. 3 do
117-
newState[j] <- newState[j] ^^^ randState[j]
118-
this.Next() |> ignore
124+
newState[j] <- newState[j] ^^^ rand.State[j]
125+
rand.Next() |> ignore
119126

120-
let generator = RandomGenerator(0)
121-
generator.LoadState(newState)
122-
generator
127+
RandomGenerator.FromState(newState)
123128

124129
member this.Jump() =
125130
this.JumpBase(jump)

O21.Game/Engine/ReproducibleRandom.fs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace O21.Game.Engine
66

77
open System
88
open System.Collections.Generic
9-
open O21.Game.Engine.EntityId
109
open O21.Game.U95
1110

1211
type ReproducibleRandom private (backend: RandomGenerator, idGenerator: SequentialIdGenerator) =
@@ -15,7 +14,7 @@ type ReproducibleRandom private (backend: RandomGenerator, idGenerator: Sequenti
1514
/// to have reproducible number sequence generated across all of the supported platforms.
1615
static member FromSeed(seed: int64): ReproducibleRandom =
1716
let baseGenerator = RandomGenerator(seed)
18-
ReproducibleRandom(baseGenerator, SequentialIdGenerator(baseGenerator.Jump()))
17+
ReproducibleRandom(baseGenerator, SequentialIdGenerator())
1918

2019
/// <summary>
2120
/// <para>Will choose a random seed to instantiate a new instance.</para>
@@ -32,13 +31,13 @@ type ReproducibleRandom private (backend: RandomGenerator, idGenerator: Sequenti
3231
backend.Next 100 >= 50
3332

3433
member _.NextFishId() =
35-
idGenerator.NextIdWithPrefix(FishId.prefix) |> FishId
34+
idGenerator.GetFishId()
3635

3736
member _.NextBombId() =
38-
idGenerator.NextIdWithPrefix(BombId.prefix) |> BombId
37+
idGenerator.GetBombId()
3938

4039
member _.NextBonusId() =
41-
idGenerator.NextIdWithPrefix(BonusId.prefix) |> BonusId
40+
idGenerator.GetBonusId()
4241

4342
member _.Chance(probability: float): bool =
4443
backend.NextBool(probability)

O21.Game/Engine/SequentialIdGenerator.fs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@
44

55
namespace O21.Game.Engine
66

7-
type SequentialIdGenerator(generator: RandomGenerator) =
8-
let mutable counter = 0UL
7+
open O21.Game.Engine.EntityId
8+
9+
type SequentialIdGenerator() =
10+
let mutable nextFishId = 0UL
11+
let mutable nextBombId = 0UL
12+
let mutable nextBonusId = 0UL
913

10-
member this.NextId() : uint64 =
11-
counter <- counter + 1UL
12-
13-
let randomPart = generator.Next()
14-
let combined = counter ^^^ randomPart
14+
member _.GetFishId() =
15+
let newId = nextFishId + 1UL
16+
nextFishId <- newId
17+
FishId <| newId
18+
19+
member _.GetBombId() =
20+
let newId = nextBombId + 1UL
21+
nextBombId <- newId
22+
BombId <| newId
1523

16-
if combined = 0UL then 1UL else combined
17-
18-
member this.NextIdWithPrefix (prefix: uint16) : uint64 =
19-
let id = this.NextId()
20-
let prefix = uint64 prefix <<< 48
21-
(id &&& 0x0000FFFFFFFFFFFFUL) ||| prefix
24+
member _.GetBonusId() =
25+
let newId = nextBonusId + 1UL
26+
nextBonusId <- newId
27+
BonusId <| newId

O21.Tests/EnemyTests.fs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,43 @@ let ``Basic fish get spawned on level``(): unit =
2121
|> Array.map (fun f -> { f with Id = FishId.empty }) // Ignore IDs for comparison
2222
Assert.Equivalent(
2323
[| { Id = FishId.empty
24-
TopLeft = Point (72, 60)
24+
TopLeft = Point (96, 264)
2525
Type = 4
2626
HorizontalVelocity = 3
2727
HorizontalDirection = HorizontalDirection.Right
2828
VerticalDirection = VerticalDirection.Up }
2929
{ Id = FishId.empty
30-
TopLeft = Point (108, 0)
30+
TopLeft = Point (132, 36)
31+
Type = 1
32+
HorizontalVelocity = 5
33+
HorizontalDirection = HorizontalDirection.Right
34+
VerticalDirection = VerticalDirection.Up }
35+
{ Id = FishId.empty
36+
TopLeft = Point (132, 204)
3137
Type = 4
3238
HorizontalVelocity = 5
3339
HorizontalDirection = HorizontalDirection.Right
3440
VerticalDirection = VerticalDirection.Up }
3541
{ Id = FishId.empty
36-
TopLeft = Point (264, 72)
42+
TopLeft = Point (288, 276)
3743
Type = 0
3844
HorizontalVelocity = 5
3945
HorizontalDirection = HorizontalDirection.Left
4046
VerticalDirection = VerticalDirection.Up }
4147
{ Id = FishId.empty
42-
TopLeft = Point (360, 108)
48+
TopLeft = Point (396, 12)
4349
Type = 2
4450
HorizontalVelocity = 4
4551
HorizontalDirection = HorizontalDirection.Right
4652
VerticalDirection = VerticalDirection.Up }
4753
{ Id = FishId.empty
48-
TopLeft = Point (456, 96)
54+
TopLeft = Point (492, 0)
4955
Type = 3
5056
HorizontalVelocity = 5
5157
HorizontalDirection = HorizontalDirection.Right
5258
VerticalDirection = VerticalDirection.Up }
5359
{ Id = FishId.empty
54-
TopLeft = Point (516, 216)
60+
TopLeft = Point (552, 108)
5561
Type = 0
5662
HorizontalVelocity = 3
5763
HorizontalDirection = HorizontalDirection.Left

0 commit comments

Comments
 (0)