-
Notifications
You must be signed in to change notification settings - Fork 6
/
determinism.rs
367 lines (283 loc) · 10 KB
/
determinism.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#![allow(clippy::type_complexity)]
use bevy::prelude::*;
use bevy_turborand::prelude::*;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
#[derive(Debug, Component, Default)]
struct HitPoints {
total: u32,
max: u32,
}
#[derive(Debug, Component, Default)]
struct Attack {
min: u32,
max: u32,
hit: f64,
}
#[derive(Debug, Component, Default)]
struct Buff {
min: u32,
max: u32,
chance: f64,
}
#[derive(Debug, Component, Default)]
struct Player;
#[derive(Debug, Component, Default)]
struct Enemy;
fn setup_player(mut commands: Commands, mut global: ResMut<GlobalRng>) {
commands.spawn((Player, RngComponent::from(&mut global)));
}
fn setup_enemies(mut commands: Commands, mut global: ResMut<GlobalRng>) {
for _ in 0..2 {
commands.spawn((Enemy, RngComponent::from(&mut global)));
}
}
#[cfg(feature = "chacha")]
fn setup_secure_player(mut commands: Commands, mut global: ResMut<GlobalChaChaRng>) {
commands.spawn((Player, ChaChaRngComponent::from(&mut global)));
}
#[cfg(feature = "chacha")]
fn setup_secure_enemies(mut commands: Commands, mut global: ResMut<GlobalChaChaRng>) {
for _ in 0..2 {
commands.spawn((Enemy, ChaChaRngComponent::from(&mut global)));
}
}
/// A system for enemies attacking the player, applying randomised damage if they are able to land a hit.
fn attack_player(
mut q_player: Query<&mut HitPoints, (With<Player>, Without<Enemy>)>,
mut q_enemies: Query<(&Attack, &mut RngComponent), (With<Enemy>, Without<Player>)>,
) {
let mut player = q_player.single_mut();
for (attack, mut rng) in q_enemies.iter_mut() {
if rng.chance(attack.hit) {
player.total = player
.total
.saturating_sub(rng.u32(attack.min..=attack.max));
}
}
}
/// A system for seeing if the player will apply an attack on a random enemy or miss if unlucky!
fn attack_random_enemy(
mut q_enemies: Query<&mut HitPoints, (With<Enemy>, Without<Player>)>,
mut q_player: Query<(&Attack, &mut RngComponent), (With<Player>, Without<Enemy>)>,
) {
let (attack, mut rng) = q_player.single_mut();
for mut enemy in q_enemies.iter_mut() {
if rng.chance(attack.hit) {
enemy.total = enemy.total.saturating_sub(rng.u32(attack.min..=attack.max));
break;
}
}
}
/// A system to randomly apply a healing effect on the player.
fn buff_player(mut q_player: Query<(&mut HitPoints, &mut RngComponent, &Buff), With<Player>>) {
let (mut player, mut rng, buff) = q_player.single_mut();
if rng.chance(buff.chance) {
player.total = player
.total
.saturating_add(rng.u32(buff.min..=buff.max))
.clamp(0, player.max);
}
}
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn deterministic_play_through() {
// Set up the game App and World
let mut app = App::new();
let world = app.world_mut();
// Initialise our global Rng resource
let mut global_rng = GlobalRng::with_seed(12345);
// Spawn the player
let player = world
.spawn((
Player,
HitPoints {
total: 100,
max: 100,
},
Attack {
min: 5,
max: 10,
hit: 0.6,
},
Buff {
min: 2,
max: 6,
chance: 0.10,
},
RngComponent::from(&mut global_rng),
))
.id();
// Spawn some enemies for the player to fight with
let enemy_1 = world
.spawn((
Enemy,
HitPoints { total: 20, max: 20 },
Attack {
min: 3,
max: 6,
hit: 0.5,
},
RngComponent::from(&mut global_rng),
))
.id();
let enemy_2 = world
.spawn((
Enemy,
HitPoints { total: 20, max: 20 },
Attack {
min: 3,
max: 6,
hit: 0.5,
},
RngComponent::from(&mut global_rng),
))
.id();
// Add the systems to our App. Order the necessary systems in order
// to ensure deterministic behaviour.
app.add_systems(
Update,
((attack_random_enemy, buff_player).chain(), attack_player),
);
// Run the game once!
app.update();
// Check to see the health of our combatants
assert_eq!(app.world().get::<HitPoints>(player).unwrap().total, 100);
assert_eq!(app.world().get::<HitPoints>(enemy_1).unwrap().total, 20);
assert_eq!(app.world().get::<HitPoints>(enemy_2).unwrap().total, 11);
// Again!
app.update();
// Player OP. Enemy 2 is in trouble
assert_eq!(app.world().get::<HitPoints>(player).unwrap().total, 90);
assert_eq!(app.world().get::<HitPoints>(enemy_1).unwrap().total, 20);
assert_eq!(app.world().get::<HitPoints>(enemy_2).unwrap().total, 3);
// And again!
app.update();
// Enemy 2 is now deceased
assert_eq!(app.world().get::<HitPoints>(player).unwrap().total, 88);
assert_eq!(app.world().get::<HitPoints>(enemy_1).unwrap().total, 20);
assert_eq!(app.world().get::<HitPoints>(enemy_2).unwrap().total, 0);
}
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn deterministic_setup() {
let mut app = App::new();
app.insert_resource(GlobalRng::with_seed(23456));
app.add_systems(Startup, (setup_player, setup_enemies).chain());
app.update();
let mut q_player = app
.world_mut()
.query_filtered::<&mut RngComponent, With<Player>>();
let mut player = q_player.single_mut(app.world_mut());
assert_eq!(player.u32(..=10), 10);
let mut q_enemies = app
.world_mut()
.query_filtered::<&mut RngComponent, With<Enemy>>();
let mut enemies = q_enemies.iter_mut(app.world_mut());
let mut enemy_1 = enemies.next().unwrap();
assert_eq!(enemy_1.u32(..=10), 1);
let mut enemy_2 = enemies.next().unwrap();
assert_eq!(enemy_2.u32(..=10), 7);
}
#[cfg(feature = "chacha")]
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn deterministic_secure_setup() {
let mut app = App::new();
app.insert_resource(GlobalChaChaRng::with_seed([1; 40]));
app.add_systems(Startup, (setup_secure_player, setup_secure_enemies).chain());
app.update();
let mut q_player = app
.world_mut()
.query_filtered::<&mut ChaChaRngComponent, With<Player>>();
let mut player = q_player.single_mut(app.world_mut());
assert_eq!(player.u32(..=10), 0);
let mut q_enemies = app
.world_mut()
.query_filtered::<&mut ChaChaRngComponent, With<Enemy>>();
let mut enemies = q_enemies.iter_mut(app.world_mut());
let mut enemy_1 = enemies.next().unwrap();
assert_eq!(enemy_1.u32(..=10), 6);
let mut enemy_2 = enemies.next().unwrap();
assert_eq!(enemy_2.u32(..=10), 3);
}
#[cfg(feature = "serialize")]
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn load_rng_setup() {
let payload = "(((state:(24691))))";
let mut rng: RngComponent = ron::from_str(payload).unwrap();
assert_eq!(rng.u32(..10), 4);
}
#[cfg(all(feature = "serialize", feature = "chacha"))]
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn load_chacha_rng_setup() {
let payload = "(((state:(1634760805,857760878,2036477234,1797285236,117901063,117901063,117901063,117901063,117901063,117901063,117901063,117901063,0,0,117901063,117901063),cache:(0,0,0,0,0,0,0,0,64))))";
let mut rng: ChaChaRngComponent = ron::from_str(payload).unwrap();
assert_eq!(rng.u32(..10), 0);
}
#[cfg(feature = "serialize")]
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn rng_reflection() {
use bevy::reflect::{
serde::{ReflectDeserializer, ReflectSerializer},
TypeRegistry,
};
use ron::ser::to_string;
use serde::de::DeserializeSeed;
let mut registry = TypeRegistry::default();
registry.register::<RngComponent>();
let mut val = RngComponent::with_seed(7);
let ser = ReflectSerializer::new(&val, ®istry);
let serialized = to_string(&ser).unwrap();
assert_eq!(
&serialized,
"{\"bevy_turborand::component::rng::RngComponent\":(((state:(15))))}"
);
let mut deserializer = ron::Deserializer::from_str(&serialized).unwrap();
let de = ReflectDeserializer::new(®istry);
let value = de.deserialize(&mut deserializer).unwrap();
let mut dynamic = value.take::<RngComponent>().unwrap();
assert_eq!(val.get_mut(), dynamic.get_mut());
dynamic.u64(..);
assert_ne!(val.get_mut(), dynamic.get_mut());
}
#[cfg(all(feature = "serialize", feature = "chacha"))]
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn chacha_rng_reflection() {
use bevy::reflect::{
serde::{ReflectDeserializer, ReflectSerializer},
TypeRegistry,
};
use serde::de::DeserializeSeed;
let mut registry = TypeRegistry::default();
registry.register::<ChaChaRngComponent>();
let mut val = ChaChaRngComponent::with_seed([7; 40]);
let ser = ReflectSerializer::new(&val, ®istry);
let serialized = ron::ser::to_string_pretty(
&ser,
ron::ser::PrettyConfig::new().new_line(String::from("\n")),
)
.unwrap();
assert_eq!(
&serialized,
r#"{
"bevy_turborand::component::chacha::ChaChaRngComponent": (((
state: (1634760805, 857760878, 2036477234, 1797285236, 117901063, 117901063, 117901063, 117901063, 117901063, 117901063, 117901063, 117901063, 0, 0, 117901063, 117901063),
cache: (0, 0, 0, 0, 0, 0, 0, 0, 64),
))),
}"#
);
let mut deserializer = ron::Deserializer::from_str(&serialized).unwrap();
let de = ReflectDeserializer::new(®istry);
let value = de.deserialize(&mut deserializer).unwrap();
let mut dynamic = value.take::<ChaChaRngComponent>().unwrap();
assert_eq!(val.get_mut(), dynamic.get_mut());
dynamic.u64(..);
assert_ne!(val.get_mut(), dynamic.get_mut());
}