@pkmn/engine 0.1.0-dev.d56bdadd → 0.1.0-dev.d73cd76c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +66 -32
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +47 -0
  5. package/build/pkg/addon/browser.js.map +1 -0
  6. package/build/pkg/addon/node.d.ts +3 -0
  7. package/build/pkg/addon/node.js +110 -0
  8. package/build/pkg/addon/node.js.map +1 -0
  9. package/build/pkg/addon/wasm.d.ts +2 -0
  10. package/build/pkg/addon/wasm.js +53 -0
  11. package/build/pkg/addon/wasm.js.map +1 -0
  12. package/build/pkg/addon.d.ts +26 -4
  13. package/build/pkg/addon.js +39 -71
  14. package/build/pkg/addon.js.map +1 -1
  15. package/build/pkg/data/layout.json +15 -2
  16. package/build/pkg/data/protocol.json +23 -9
  17. package/build/pkg/data.d.ts +27 -5
  18. package/build/pkg/data.js.map +1 -1
  19. package/build/pkg/gen1.d.ts +15 -18
  20. package/build/pkg/gen1.js +28 -19
  21. package/build/pkg/gen1.js.map +1 -1
  22. package/build/pkg/index.d.ts +42 -91
  23. package/build/pkg/index.js +23 -70
  24. package/build/pkg/index.js.map +1 -1
  25. package/build/pkg/protocol.d.ts +29 -1
  26. package/build/pkg/protocol.js +109 -107
  27. package/build/pkg/protocol.js.map +1 -1
  28. package/build/tools/debug.js +84 -73
  29. package/build/tools/debug.js.map +1 -1
  30. package/build/tools/display/debug.jsx +66 -0
  31. package/build/tools/display/debug.jsx.map +1 -0
  32. package/build/tools/display/index.js +152 -0
  33. package/build/tools/display/index.js.map +1 -0
  34. package/build/tools/display/ui.jsx +360 -0
  35. package/build/tools/display/ui.jsx.map +1 -0
  36. package/build/tools/display/util.js +157 -0
  37. package/build/tools/display/util.js.map +1 -0
  38. package/build.zig +357 -135
  39. package/package.json +61 -132
  40. package/src/bin/install-pkmn-engine +47 -21
  41. package/src/bin/pkmn-debug +1 -1
  42. package/src/lib/bindings/c.zig +201 -0
  43. package/src/lib/{binding → bindings}/node.zig +15 -32
  44. package/src/lib/bindings/wasm.zig +51 -0
  45. package/src/lib/common/array.zig +70 -0
  46. package/src/lib/common/data.zig +4 -4
  47. package/src/lib/common/debug.zig +45 -29
  48. package/src/lib/common/js.zig +3 -7
  49. package/src/lib/common/optional.zig +27 -23
  50. package/src/lib/common/options.zig +6 -0
  51. package/src/lib/common/protocol.zig +565 -552
  52. package/src/lib/common/rational.zig +340 -253
  53. package/src/lib/common/rng.zig +7 -9
  54. package/src/lib/common/util.zig +53 -1
  55. package/src/lib/gen1/calc.zig +724 -175
  56. package/src/lib/gen1/chance.zig +486 -300
  57. package/src/lib/gen1/data/moves.zig +2 -4
  58. package/src/lib/gen1/data/species.zig +2 -4
  59. package/src/lib/gen1/data/types.zig +3 -4
  60. package/src/lib/gen1/data.zig +29 -32
  61. package/src/lib/gen1/helpers.zig +113 -561
  62. package/src/lib/gen1/mechanics.zig +611 -488
  63. package/src/lib/gen2/calc.zig +35 -28
  64. package/src/lib/gen2/chance.zig +111 -511
  65. package/src/lib/gen2/data/items.zig +1 -3
  66. package/src/lib/gen2/data/moves.zig +2 -4
  67. package/src/lib/gen2/data/species.zig +1 -3
  68. package/src/lib/gen2/data/types.zig +7 -5
  69. package/src/lib/gen2/data.zig +27 -29
  70. package/src/lib/gen2/generated.zig +22 -29
  71. package/src/lib/gen2/helpers.zig +68 -39
  72. package/src/lib/gen2/mechanics.zig +504 -430
  73. package/src/lib/napi.h +2 -0
  74. package/src/lib/node.zig +14 -0
  75. package/src/lib/pkmn.zig +125 -29
  76. package/src/lib/wasm.zig +13 -0
  77. package/src/tools/display/dom.ts +108 -0
  78. package/src/tools/display/ui.css +354 -0
  79. package/build/test/display.js +0 -400
  80. package/build/test/display.js.map +0 -1
  81. package/src/lib/binding/wasm.zig +0 -12
  82. package/src/lib/gen1/README.md +0 -894
  83. package/src/lib/gen2/README.md +0 -329
  84. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -1,39 +1,23 @@
1
- const std = @import("std");
2
-
1
+ const builtin = @import("builtin");
3
2
  const common = @import("../common/data.zig");
4
- const optional = @import("../common/optional.zig");
3
+ const data = @import("data.zig");
5
4
  const options = @import("../common/options.zig");
6
5
  const protocol = @import("../common/protocol.zig");
7
- const rational = @import("../common/rational.zig");
8
6
  const rng = @import("../common/rng.zig");
9
-
10
- const calc = @import("calc.zig");
11
- const chance = @import("chance.zig");
12
- const data = @import("data.zig");
7
+ const std = @import("std");
8
+ const util = @import("../common/util.zig");
13
9
 
14
10
  const assert = std.debug.assert;
15
-
16
- const expectEqual = std.testing.expectEqual;
17
- const expectEqualSlices = std.testing.expectEqualSlices;
18
-
19
11
  const Choice = common.Choice;
20
- const Player = common.Player;
21
-
22
- const Optional = optional.Optional;
23
-
24
- const showdown = options.showdown;
25
-
26
- const PSRNG = rng.PSRNG;
27
-
28
12
  const DVs = data.DVs;
29
13
  const Move = data.Move;
30
14
  const MoveSlot = data.MoveSlot;
15
+ const PSRNG = rng.PSRNG;
16
+ const showdown = options.showdown;
31
17
  const Species = data.Species;
32
18
  const Stats = data.Stats;
33
- const Status = data.Status;
34
19
 
35
- const Action = chance.Action;
36
- const Actions = chance.Actions;
20
+ const endian = builtin.cpu.arch.endian();
37
21
 
38
22
  /// Configuration options controlling the domain of valid Pokémon that can be generated by the
39
23
  /// random generator helpers.
@@ -43,6 +27,8 @@ pub const Options = struct {
43
27
  /// Whether to generate Pokémon with moves that contain bugs on Pokémon Showdown that are
44
28
  /// unimplementable in the engine.
45
29
  block: bool = showdown,
30
+ // FIXME: remove once durations work with -Dchance
31
+ durations: bool = false,
46
32
  };
47
33
 
48
34
  /// Helpers to simplify initialization of a Generation I battle.
@@ -86,18 +72,38 @@ pub const Battle = struct {
86
72
  };
87
73
 
88
74
  fn prng(rand: *PSRNG) data.PRNG {
89
- // GLITCH: initial bytes in seed can only range from 0-252, not 0-255
90
- const max: u8 = 253;
91
75
  return .{
92
76
  .src = .{
93
77
  .seed = if (showdown)
94
78
  rand.newSeed()
95
- else
96
- .{
97
- rand.range(u8, 0, max), rand.range(u8, 0, max), rand.range(u8, 0, max),
98
- rand.range(u8, 0, max), rand.range(u8, 0, max), rand.range(u8, 0, max),
99
- rand.range(u8, 0, max), rand.range(u8, 0, max), rand.range(u8, 0, max),
100
- },
79
+ else seed: {
80
+ // GLITCH: The initial RNG seed in Gen I link battles is heavily constrained
81
+ // https://www.smogon.com/forums/threads/rng-in-rby-link-battles.3746779
82
+ var seed = [_]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0 };
83
+
84
+ // Bytes in seed can only range from 0-252, not 0-255
85
+ const max: u8 = 253;
86
+ // Instructions take multiples of 4 cycles to execute
87
+ var timer: u16 = 4 * rand.range(u16, 0, 16384);
88
+ var add: u8 = rand.range(u8, 0, 256);
89
+ var carry: u8 = 0;
90
+
91
+ var i: u8 = 0;
92
+ while (i < seed.len) {
93
+ const div: u8 = @intCast(timer >> 8);
94
+ add +%= div +% carry;
95
+ if (add < max) {
96
+ seed[i] = add;
97
+ i += 1;
98
+ timer +%= 472;
99
+ carry = 1;
100
+ } else {
101
+ timer +%= 452;
102
+ carry = 0;
103
+ }
104
+ }
105
+ break :seed seed;
106
+ },
101
107
  },
102
108
  };
103
109
  }
@@ -155,15 +161,20 @@ pub const Pokemon = struct {
155
161
  var pokemon = data.Pokemon{};
156
162
  pokemon.species = p.species;
157
163
  const species = Species.get(p.species);
158
- inline for (@typeInfo(@TypeOf(pokemon.stats)).Struct.fields) |field| {
159
- const hp = comptime std.mem.eql(u8, field.name, "hp");
164
+ inline for (if (@hasField(@TypeOf(@typeInfo(@TypeOf(pokemon.stats)).@"struct"), "fields"))
165
+ @typeInfo(@TypeOf(pokemon.stats)).@"struct".fields
166
+ else
167
+ @typeInfo(@TypeOf(pokemon.stats)).@"struct".field_names) |field|
168
+ {
169
+ const field_name = if (@hasField(@TypeOf(field), "name")) field.name else field;
170
+ const hp = comptime std.mem.eql(u8, field_name, "hp");
160
171
  const spc =
161
- comptime std.mem.eql(u8, field.name, "spa") or std.mem.eql(u8, field.name, "spd");
162
- @field(pokemon.stats, field.name) = Stats(u16).calc(
163
- field.name,
164
- @field(species.stats, field.name),
165
- if (hp) p.dvs.hp() else if (spc) p.dvs.spc else @field(p.dvs, field.name),
166
- @field(p.stats, field.name),
172
+ comptime std.mem.eql(u8, field_name, "spa") or std.mem.eql(u8, field_name, "spd");
173
+ @field(pokemon.stats, field_name) = Stats(u16).calc(
174
+ field_name,
175
+ @field(species.stats, field_name),
176
+ if (hp) p.dvs.hp() else if (spc) p.dvs.spc else @field(p.dvs, field_name),
177
+ @field(p.stats, field_name),
167
178
  p.level,
168
179
  );
169
180
  }
@@ -191,25 +202,31 @@ pub const Pokemon = struct {
191
202
  const lvl = if (rand.chance(u8, 1, 20)) rand.range(u8, 1, 99 + 1) else 100;
192
203
  var stats: Stats(u16) = .{};
193
204
  const dvs = DVs.random(rand);
194
- inline for (@typeInfo(@TypeOf(stats)).Struct.fields) |field| {
195
- @field(stats, field.name) = Stats(u16).calc(
196
- field.name,
197
- @field(species.stats, field.name),
198
- if (comptime std.mem.eql(u8, field.name, "hp"))
205
+ inline for (if (@hasField(@TypeOf(@typeInfo(@TypeOf(stats)).@"struct"), "fields"))
206
+ @typeInfo(@TypeOf(stats)).@"struct".fields
207
+ else
208
+ @typeInfo(@TypeOf(stats)).@"struct".field_names) |field|
209
+ {
210
+ const field_name = if (@hasField(@TypeOf(field), "name")) field.name else field;
211
+ @field(stats, field_name) = Stats(u16).calc(
212
+ field_name,
213
+ @field(species.stats, field_name),
214
+ if (comptime std.mem.eql(u8, field_name, "hp"))
199
215
  dvs.hp()
200
216
  else
201
- @field(dvs, field.name),
217
+ @field(dvs, field_name),
202
218
  if (rand.chance(u8, 1, 20)) rand.range(u16, 0, EXP + 1) else EXP,
203
219
  lvl,
204
220
  );
205
221
  }
206
222
 
207
- var ms = [_]MoveSlot{.{}} ** 4;
223
+ var ms: [4]MoveSlot = @splat(.{});
208
224
  const n = if (rand.chance(u8, 1, 100)) rand.range(u4, 1, 3 + 1) else 4;
209
225
  for (0..n) |i| {
210
226
  var m: Move = .None;
211
227
  sample: while (true) {
212
228
  m = @enumFromInt(rand.range(u8, 1, Move.size - 1 + 1));
229
+ if (opt.durations and durations(m)) continue :sample;
213
230
  if (opt.block and blocked(m)) continue :sample;
214
231
  for (0..i) |j| if (ms[j].id == m) continue :sample;
215
232
  break;
@@ -231,6 +248,7 @@ pub const Pokemon = struct {
231
248
  .stats = stats,
232
249
  .hp = if (opt.cleric) stats.hp else rand.range(u16, 0, stats.hp + 1),
233
250
  .status = if (!opt.cleric and rand.chance(u8, 1, 6 + 1))
251
+ // NB: This will only assign sleep as 2 or 4 when rolled
234
252
  0 | (@as(u8, 1) << rand.range(u3, 1, 6 + 1))
235
253
  else
236
254
  0,
@@ -247,6 +265,16 @@ fn blocked(m: Move) bool {
247
265
  };
248
266
  }
249
267
 
268
+ fn durations(m: Move) bool {
269
+ // Any moves that have effects that result in duration tracking with -Dchance
270
+ return switch (Move.get(m).effect) {
271
+ .Haze,
272
+ .Metronome,
273
+ => true,
274
+ else => false,
275
+ };
276
+ }
277
+
250
278
  /// Convenience helper to create a move-type choice for the provided move slot.
251
279
  pub fn move(slot: u4) Choice {
252
280
  return .{ .type = .Move, .data = slot };
@@ -257,523 +285,47 @@ pub fn swtch(slot: u4) Choice {
257
285
  return .{ .type = .Switch, .data = slot };
258
286
  }
259
287
 
260
- const NONE = @intFromEnum(Optional(bool).None);
261
-
262
- /// Helper functions that efficiently return valid ranges for various RNG events based on the
263
- /// state of an `Action` and other events to be used to construct a "transitions" function.
264
- pub const Rolls = struct {
265
- const PLAYER_NONE = [_]Optional(Player){.None};
266
- const PLAYERS = [_]Optional(Player){ .P1, .P2 };
267
-
268
- /// Returns a slice with the correct range of values for speed ties given the `action` state.
269
- pub fn speedTie(action: Action) []const Optional(Player) {
270
- return if (@field(action, "speed_tie") == .None) &PLAYER_NONE else &PLAYERS;
271
- }
272
-
273
- const BOOL_NONE = [_]Optional(bool){.None};
274
- const BOOLS = [_]Optional(bool){ .false, .true };
275
-
276
- /// Returns a slice with the correct range of values for hits given the `action` state
277
- /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
278
- pub fn hit(action: Action, parent: Optional(bool)) []const Optional(bool) {
279
- if (parent == .true) return &BOOL_NONE;
280
- return if (@field(action, "hit") == .None) &BOOL_NONE else &BOOLS;
281
- }
282
-
283
- /// Returns a slice with the correct range of values for critical hits given the `action` state
284
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
285
- pub fn criticalHit(
286
- action: Action,
287
- parent: Optional(bool),
288
- ) []const Optional(bool) {
289
- if (parent == .false) return &BOOL_NONE;
290
- return if (@field(action, "critical_hit") == .None) &BOOL_NONE else &BOOLS;
291
- }
292
-
293
- /// Returns a slice with the correct range of values for secondary chances hits given the
294
- /// `action` state and the state of the `parent` (whether the player's Pokémon's move hit).
295
- pub fn secondaryChance(
296
- action: Action,
297
- parent: Optional(bool),
298
- ) []const Optional(bool) {
299
- if (parent == .false) return &BOOL_NONE;
300
- return if (@field(action, "secondary_chance") == .None) &BOOL_NONE else &BOOLS;
301
- }
302
-
303
- /// The min and max bounds on iteration over damage rolls.
304
- pub const Range = struct { min: u9, max: u9 };
305
-
306
- /// Returns the range bounding damage rolls given the `action` state and the state of
307
- /// the `parent` (whether the player's Pokémon's move hit).
308
- pub fn damage(action: Action, parent: Optional(bool)) Range {
309
- return if (parent == .false or @field(action, "damage") == 0)
310
- .{ .min = 0, .max = 1 }
311
- else
312
- .{ .min = 217, .max = 256 };
313
- }
314
-
315
- /// Returns the max damage roll which will produce the same damage as `roll`
316
- /// given the base damage in `summaries`.
317
- pub fn coalesce(player: Player, roll: u8, summaries: *calc.Summaries, cap: bool) !u8 {
318
- if (roll == 0) return roll;
319
-
320
- const dmg = summaries.get(player.foe()).damage;
321
- if (dmg.base == 0 or (cap and dmg.capped)) return 255;
322
-
323
- // Closed form solution for max damage roll provided by Orion Taylor (taylorott)
324
- return @min(255, roll + ((254 - ((@as(u32, dmg.base) * roll) % 255)) / dmg.base));
325
- }
326
-
327
- /// Returns a slice with the correct range of values for confused given the `action` state
328
- /// and the state of the `parent` (the player's Pokémon's remaining confusion turns).
329
- pub fn confused(action: Action, parent: u4) []const Optional(bool) {
330
- return if (parent == 0 or @field(action, "confused") == .None) &BOOL_NONE else &BOOLS;
331
- }
332
-
333
- /// Returns a slice with the correct range of values for paralysis given the `action` state
334
- /// and the state of the `parent` (whether the player's Pokémon was confused).
335
- pub fn paralyzed(action: Action, parent: Optional(bool)) []const Optional(bool) {
336
- if (parent == .true) return &BOOL_NONE;
337
- return if (@field(action, "paralyzed") == .None) &BOOL_NONE else &BOOLS;
338
- }
339
-
340
- const DURATION_NONE = [_]u1{0};
341
- const DURATION = [_]u1{1};
342
-
343
- /// Returns a slice with a range of values for duration given the `action` state
344
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
345
- pub fn duration(action: Action, parent: Optional(bool)) []const u1 {
346
- if (parent == .false) return &DURATION_NONE;
347
- return if (@field(action, "duration") == 0) &DURATION_NONE else &DURATION;
348
- }
349
-
350
- const U3_NONE = [_]u3{0};
351
- const U3_FORCED = [_]u3{1};
352
-
353
- /// Returns a slice with a range of values for sleep given the `action` and `origin` states.
354
- pub inline fn sleep(action: Action, origin: Action) []const u3 {
355
- const a = @field(action.durations, "sleep");
356
- const o = @field(origin.durations, "sleep");
357
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
358
- assert(a == 0 or a == o + 1);
359
- return if (o >= 7) &U3_NONE else &[_]u3{ 0, o + 1 };
360
- }
361
-
362
- const DISABLE_NONE = [_]u4{0};
363
-
364
- /// Returns a slice with a range of values for disable given the `action` and `origin` states
365
- /// and the state of the `parent` (the player's Pokémon's remaining sleep turns).
366
- pub inline fn disable(action: Action, origin: Action, parent: u4) []const u4 {
367
- if (parent > 0) return &DISABLE_NONE;
368
- const a = @field(action.durations, "disable");
369
- const o = @field(origin.durations, "disable");
370
- if (o == a) return if (o == 0) &DISABLE_NONE else &[_]u4{o};
371
- assert(a == 0 or a == o + 1);
372
- return if (o >= 8) &DISABLE_NONE else &[_]u4{ 0, o + 1 };
373
- }
374
-
375
- /// Returns a slice with a range of values for confusion given the `action` and `origin` states
376
- /// and the state of the `parent` (the player's remaining sleep turns).
377
- pub inline fn confusion(action: Action, origin: Action, parent: u4) []const u3 {
378
- if (parent > 0) return &U3_NONE;
379
- const a = @field(action.durations, "confusion");
380
- const o = @field(origin.durations, "confusion");
381
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
382
- assert(a == 0 or a == o + 1);
383
- return if (o >= 5) &U3_NONE else if (o < 2)
384
- if (a == 0) &U3_NONE else &U3_FORCED
385
- else
386
- &[_]u3{ 0, o + 1 };
387
- }
388
-
389
- /// Returns a slice with a range of values for attacking given the `action` and `origin` states
390
- /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
391
- pub inline fn attacking(action: Action, origin: Action, parent: Optional(bool)) []const u3 {
392
- if (parent == .true) return &U3_NONE;
393
- const a = @field(action.durations, "attacking");
394
- const o = @field(origin.durations, "attacking");
395
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
396
- assert(a == 0 or a == o + 1);
397
- return if (o >= 3) &U3_NONE else if (o < 2)
398
- if (a == 0) &U3_NONE else &U3_FORCED
399
- else
400
- &[_]u3{ 0, o + 1 };
401
- }
402
-
403
- /// Returns a slice with a range of values for binding given the `action` and `origin` states
404
- /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
405
- pub inline fn binding(action: Action, origin: Action, parent: Optional(bool)) []const u3 {
406
- if (parent == .true) return &U3_NONE;
407
- const a = @field(action.durations, "binding");
408
- const o = @field(origin.durations, "binding");
409
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
410
- assert(a == 0 or a == o + 1);
411
- return if (o >= 4) &U3_NONE else &[_]u3{ 0, o + 1 };
412
- }
413
-
414
- const SLOT_NONE = [_]u4{0};
415
- const SLOT = [_]u4{ 1, 2, 3, 4 };
416
-
417
- /// Returns a slice with a range of values for move slots given the `action` state
418
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
419
- ///
420
- /// These slots may or **may not be valid** as slots may be unset / have 0 PP.
421
- pub fn moveSlot(action: Action, parent: Optional(bool)) []const u4 {
422
- if (parent == .false) return &SLOT_NONE;
423
- return if (@field(action, "move_slot") == 0) &SLOT_NONE else &SLOT;
424
- }
425
-
426
- const MULTI_NONE = [_]u4{0};
427
- const MULTI = [_]u4{ 2, 3, 4, 5 };
428
-
429
- /// Returns a slice with the correct range of values for multi hit given the `action` state
430
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
431
- pub fn multiHit(action: Action, parent: Optional(bool)) []const u4 {
432
- if (parent == .false) return &MULTI_NONE;
433
- return if (@field(action, "multi_hit") == 0) &MULTI_NONE else &MULTI;
434
- }
435
-
436
- const PSYWAVE_NONE = [_]u8{0};
437
- const PSYWAVE = init: {
438
- var rolls: [150]u8 = undefined;
439
- for (0..150) |i| rolls[i] = i + 1;
440
- break :init rolls;
441
- };
442
-
443
- /// Returns a slice with the correct range of values for psywave given the `action` state,
444
- /// the `side`, and the state of the `parent` (whether the player's Pokémon's move hit).
445
- pub fn psywave(
446
- action: Action,
447
- side: *data.Side,
448
- parent: Optional(bool),
449
- ) []const u8 {
450
- if (parent == .false) return &PSYWAVE_NONE;
451
- return if (@field(action, "psywave") == 0)
452
- &PSYWAVE_NONE
453
- else
454
- PSYWAVE[0 .. @as(u16, side.stored().level) * 3 / 2];
455
- }
456
-
457
- const MOVE_NONE = [_]Move{.None};
458
-
459
- /// Returns a slice with the correct range of values for metronome given the `action` state.
460
- pub fn metronome(action: Action) []const Move {
461
- return if (@field(action, "metronome") == .None) &MOVE_NONE else &Move.METRONOME;
288
+ /// Empirically determined maximum number of bytes possibly required to store the diff between two
289
+ /// Generation I battles.
290
+ pub const MAX_DIFFS: usize = 120;
291
+
292
+ /// The optimal size in bytes required to hold the diff between two Generation I battles.
293
+ /// At least as large as MAX_DIFFS.
294
+ pub const DIFFS_SIZE = if (builtin.mode == .ReleaseSmall)
295
+ MAX_DIFFS
296
+ else
297
+ std.math.ceilPowerOfTwo(usize, MAX_DIFFS) catch unreachable;
298
+
299
+ /// TODO
300
+ pub fn diff(
301
+ a: *const data.Battle(data.PRNG),
302
+ b: *const data.Battle(data.PRNG),
303
+ w: *protocol.Writer,
304
+ ) !usize {
305
+ const pos = w.pos;
306
+ const size = @sizeOf(data.Battle(data.PRNG)) - @sizeOf(data.PRNG);
307
+ assert(size % 2 == 0);
308
+ assert(size / 2 < 255);
309
+
310
+ var i: usize = 0;
311
+ while (i < size) : (i += 2) {
312
+ const val = std.mem.bytesAsValue(u16, std.mem.asBytes(a)[i .. i + 2]).*;
313
+ if (val != std.mem.bytesAsValue(u16, std.mem.asBytes(b)[i .. i + 2]).*) {
314
+ try w.writeByte(@intCast(i / 2));
315
+ try w.writeInt(u16, val, endian);
316
+ }
462
317
  }
463
- };
464
318
 
465
- test "Rolls.speedTie" {
466
- const actions: Actions = .{ .p1 = .{ .speed_tie = .P2 } };
467
- try expectEqualSlices(Optional(Player), &.{ .P1, .P2 }, Rolls.speedTie(actions.p1));
468
- try expectEqualSlices(Optional(Player), &.{.None}, Rolls.speedTie(actions.p2));
319
+ return w.pos - pos;
469
320
  }
470
321
 
471
- test "Rolls.damage" {
472
- const actions: Actions = .{ .p2 = .{ .damage = 221 } };
473
- try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p1, .None));
474
- try expectEqual(Rolls.Range{ .min = 217, .max = 256 }, Rolls.damage(actions.p2, .None));
475
- try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p2, .false));
476
- }
477
-
478
- test "Rolls.coalesce" {
479
- var summaries =
480
- calc.Summaries{ .p1 = .{ .damage = .{ .base = 74, .final = 69, .capped = true } } };
481
- try expectEqual(@as(u8, 0), try Rolls.coalesce(.P2, 0, &summaries, false));
482
- try expectEqual(@as(u8, 241), try Rolls.coalesce(.P2, 238, &summaries, false));
483
- try expectEqual(@as(u8, 255), try Rolls.coalesce(.P2, 238, &summaries, true));
484
- summaries.p1.damage.final = 74;
485
- try expectEqual(@as(u8, 217), try Rolls.coalesce(.P2, 217, &summaries, false));
486
- try expectEqual(@as(u8, 255), try Rolls.coalesce(.P2, 217, &summaries, true));
487
- }
488
-
489
- test "Rolls.hit" {
490
- const actions: Actions = .{ .p2 = .{ .hit = .true } };
491
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p1, .None));
492
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .None));
493
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p2, .true));
494
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .false));
495
- }
496
-
497
- test "Rolls.secondaryChance" {
498
- const actions: Actions = .{ .p1 = .{ .secondary_chance = .true } };
499
- try expectEqualSlices(
500
- Optional(bool),
501
- &.{ .false, .true },
502
- Rolls.secondaryChance(actions.p1, .None),
503
- );
504
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p1, .false));
505
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p2, .None));
506
- }
507
-
508
- test "Rolls.criticalHit" {
509
- const actions: Actions = .{ .p1 = .{ .critical_hit = .true } };
510
- try expectEqualSlices(
511
- Optional(bool),
512
- &.{ .false, .true },
513
- Rolls.criticalHit(actions.p1, .None),
514
- );
515
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p1, .false));
516
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p2, .None));
517
- }
518
-
519
- test "Rolls.confused" {
520
- const actions: Actions = .{ .p2 = .{ .confused = .true } };
521
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p1, 1));
522
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.confused(actions.p2, 1));
523
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p2, 0));
524
- }
525
-
526
- test "Rolls.paralyzed" {
527
- const actions: Actions = .{ .p2 = .{ .paralyzed = .true } };
528
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p1, .None));
529
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .None));
530
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .false));
531
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p2, .true));
532
- }
533
-
534
- test "Rolls.duration" {
535
- const actions: Actions = .{ .p2 = .{ .duration = 3 } };
536
- try expectEqualSlices(u1, &.{0}, Rolls.duration(actions.p1, .None));
537
- try expectEqualSlices(u1, &.{1}, Rolls.duration(actions.p2, .None));
538
- try expectEqualSlices(u1, &.{0}, Rolls.duration(actions.p2, .false));
539
- }
540
-
541
- test "Rolls.sleep" {
542
- try expectEqualSlices(u3, &.{0}, Rolls.sleep(
543
- .{ .durations = .{ .sleep = 0 } },
544
- .{ .durations = .{ .sleep = 0 } },
545
- ));
546
- try expectEqualSlices(u3, &.{ 0, 1 }, Rolls.sleep(
547
- .{ .durations = .{ .sleep = 1 } },
548
- .{ .durations = .{ .sleep = 0 } },
549
- ));
550
- try expectEqualSlices(u3, &.{ 0, 2 }, Rolls.sleep(
551
- .{ .durations = .{ .sleep = 0 } },
552
- .{ .durations = .{ .sleep = 1 } },
553
- ));
554
- try expectEqualSlices(u3, &.{1}, Rolls.sleep(
555
- .{ .durations = .{ .sleep = 1 } },
556
- .{ .durations = .{ .sleep = 1 } },
557
- ));
558
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.sleep(
559
- .{ .durations = .{ .sleep = 0 } },
560
- .{ .durations = .{ .sleep = 2 } },
561
- ));
562
- try expectEqualSlices(u3, &.{ 0, 7 }, Rolls.sleep(
563
- .{ .durations = .{ .sleep = 7 } },
564
- .{ .durations = .{ .sleep = 6 } },
565
- ));
566
- try expectEqualSlices(u3, &.{0}, Rolls.sleep(
567
- .{ .durations = .{ .sleep = 0 } },
568
- .{ .durations = .{ .sleep = 7 } },
569
- ));
570
- }
571
-
572
- test "Rolls.disable" {
573
- try expectEqualSlices(u4, &.{0}, Rolls.disable(
574
- .{ .durations = .{ .disable = 0 } },
575
- .{ .durations = .{ .disable = 0 } },
576
- 0,
577
- ));
578
- try expectEqualSlices(u4, &.{ 0, 1 }, Rolls.disable(
579
- .{ .durations = .{ .disable = 1 } },
580
- .{ .durations = .{ .disable = 0 } },
581
- 0,
582
- ));
583
- try expectEqualSlices(u4, &.{ 0, 2 }, Rolls.disable(
584
- .{ .durations = .{ .disable = 0 } },
585
- .{ .durations = .{ .disable = 1 } },
586
- 0,
587
- ));
588
- try expectEqualSlices(u4, &.{1}, Rolls.disable(
589
- .{ .durations = .{ .disable = 1 } },
590
- .{ .durations = .{ .disable = 1 } },
591
- 0,
592
- ));
593
- try expectEqualSlices(u4, &.{ 0, 3 }, Rolls.disable(
594
- .{ .durations = .{ .disable = 0 } },
595
- .{ .durations = .{ .disable = 2 } },
596
- 0,
597
- ));
598
- try expectEqualSlices(u4, &.{0}, Rolls.disable(
599
- .{ .durations = .{ .disable = 0 } },
600
- .{ .durations = .{ .disable = 2 } },
601
- 1,
602
- ));
603
- try expectEqualSlices(u4, &.{ 0, 8 }, Rolls.disable(
604
- .{ .durations = .{ .disable = 8 } },
605
- .{ .durations = .{ .disable = 7 } },
606
- 0,
607
- ));
608
- try expectEqualSlices(u4, &.{0}, Rolls.disable(
609
- .{ .durations = .{ .disable = 0 } },
610
- .{ .durations = .{ .disable = 8 } },
611
- 0,
612
- ));
613
- }
614
-
615
- test "Rolls.confusion" {
616
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(
617
- .{ .durations = .{ .confusion = 0 } },
618
- .{ .durations = .{ .confusion = 0 } },
619
- 0,
620
- ));
621
- try expectEqualSlices(u3, &.{1}, Rolls.confusion(
622
- .{ .durations = .{ .confusion = 1 } },
623
- .{ .durations = .{ .confusion = 0 } },
624
- 0,
625
- ));
626
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(
627
- .{ .durations = .{ .confusion = 0 } },
628
- .{ .durations = .{ .confusion = 1 } },
629
- 0,
630
- ));
631
- try expectEqualSlices(u3, &.{1}, Rolls.confusion(
632
- .{ .durations = .{ .confusion = 1 } },
633
- .{ .durations = .{ .confusion = 1 } },
634
- 0,
635
- ));
636
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.confusion(
637
- .{ .durations = .{ .confusion = 0 } },
638
- .{ .durations = .{ .confusion = 2 } },
639
- 0,
640
- ));
641
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(
642
- .{ .durations = .{ .confusion = 0 } },
643
- .{ .durations = .{ .confusion = 2 } },
644
- 1,
645
- ));
646
- try expectEqualSlices(u3, &.{ 0, 5 }, Rolls.confusion(
647
- .{ .durations = .{ .confusion = 5 } },
648
- .{ .durations = .{ .confusion = 4 } },
649
- 0,
650
- ));
651
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(
652
- .{ .durations = .{ .confusion = 0 } },
653
- .{ .durations = .{ .confusion = 5 } },
654
- 0,
655
- ));
656
- }
657
-
658
- test "Rolls.attacking" {
659
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
660
- .{ .durations = .{ .attacking = 0 } },
661
- .{ .durations = .{ .attacking = 0 } },
662
- .false,
663
- ));
664
- try expectEqualSlices(u3, &.{1}, Rolls.attacking(
665
- .{ .durations = .{ .attacking = 1 } },
666
- .{ .durations = .{ .attacking = 0 } },
667
- .false,
668
- ));
669
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
670
- .{ .durations = .{ .attacking = 0 } },
671
- .{ .durations = .{ .attacking = 1 } },
672
- .false,
673
- ));
674
- try expectEqualSlices(u3, &.{1}, Rolls.attacking(
675
- .{ .durations = .{ .attacking = 1 } },
676
- .{ .durations = .{ .attacking = 1 } },
677
- .false,
678
- ));
679
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.attacking(
680
- .{ .durations = .{ .attacking = 0 } },
681
- .{ .durations = .{ .attacking = 2 } },
682
- .false,
683
- ));
684
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
685
- .{ .durations = .{ .attacking = 0 } },
686
- .{ .durations = .{ .attacking = 2 } },
687
- .true,
688
- ));
689
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.attacking(
690
- .{ .durations = .{ .attacking = 3 } },
691
- .{ .durations = .{ .attacking = 2 } },
692
- .false,
693
- ));
694
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
695
- .{ .durations = .{ .attacking = 0 } },
696
- .{ .durations = .{ .attacking = 3 } },
697
- .false,
698
- ));
699
- }
700
-
701
- test "Rolls.binding" {
702
- try expectEqualSlices(u3, &.{0}, Rolls.binding(
703
- .{ .durations = .{ .binding = 0 } },
704
- .{ .durations = .{ .binding = 0 } },
705
- .false,
706
- ));
707
- try expectEqualSlices(u3, &.{ 0, 1 }, Rolls.binding(
708
- .{ .durations = .{ .binding = 1 } },
709
- .{ .durations = .{ .binding = 0 } },
710
- .false,
711
- ));
712
- try expectEqualSlices(u3, &.{ 0, 2 }, Rolls.binding(
713
- .{ .durations = .{ .binding = 0 } },
714
- .{ .durations = .{ .binding = 1 } },
715
- .false,
716
- ));
717
- try expectEqualSlices(u3, &.{1}, Rolls.binding(
718
- .{ .durations = .{ .binding = 1 } },
719
- .{ .durations = .{ .binding = 1 } },
720
- .false,
721
- ));
722
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.binding(
723
- .{ .durations = .{ .binding = 0 } },
724
- .{ .durations = .{ .binding = 2 } },
725
- .false,
726
- ));
727
- try expectEqualSlices(u3, &.{0}, Rolls.binding(
728
- .{ .durations = .{ .binding = 0 } },
729
- .{ .durations = .{ .binding = 2 } },
730
- .true,
731
- ));
732
- try expectEqualSlices(u3, &.{ 0, 4 }, Rolls.binding(
733
- .{ .durations = .{ .binding = 4 } },
734
- .{ .durations = .{ .binding = 3 } },
735
- .false,
736
- ));
737
- try expectEqualSlices(u3, &.{0}, Rolls.binding(
738
- .{ .durations = .{ .binding = 0 } },
739
- .{ .durations = .{ .binding = 4 } },
740
- .false,
741
- ));
742
- }
743
-
744
- test "Rolls.moveSlot" {
745
- const actions: Actions = .{ .p2 = .{ .move_slot = 3 } };
746
- try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p1, .None));
747
- try expectEqualSlices(u4, &.{ 1, 2, 3, 4 }, Rolls.moveSlot(actions.p2, .None));
748
- try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p2, .false));
749
- }
750
-
751
- test "Rolls.multiHit" {
752
- const actions: Actions = .{ .p2 = .{ .multi_hit = 3 } };
753
- try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p1, .None));
754
- try expectEqualSlices(u4, &.{ 2, 3, 4, 5 }, Rolls.multiHit(actions.p2, .None));
755
- try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p2, .false));
756
- }
757
-
758
- test "Rolls.metronome" {
759
- const actions: Actions = .{ .p2 = .{ .metronome = .Surf } };
760
- try expectEqualSlices(Move, &.{.None}, Rolls.metronome(actions.p1));
761
- try expectEqual(@as(Move, @enumFromInt(24)), Rolls.metronome(actions.p2)[23]);
762
- }
763
-
764
- test "Rolls.psywave" {
765
- const actions: Actions = .{ .p2 = .{ .psywave = 79 } };
766
- var side = Side.init(&[_]Pokemon{.{
767
- .species = .Bulbasaur,
768
- .level = 100,
769
- .moves = &[_]Move{.Tackle},
770
- }});
771
-
772
- try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p1, &side, .None));
773
- var rolls = Rolls.psywave(actions.p2, &side, .None);
774
- try expectEqual(@as(u8, 150), rolls[rolls.len - 1]);
775
- side.stored().level = 81;
776
- rolls = Rolls.psywave(actions.p2, &side, .None);
777
- try expectEqual(@as(u8, 121), rolls[rolls.len - 1]);
778
- try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p2, &side, .false));
322
+ /// TODO
323
+ pub fn patch(battle: *data.Battle(data.PRNG), buf: []u8) void {
324
+ var i: usize = 0;
325
+ while (i < buf.len) : (i += 3) {
326
+ const off = @as(u16, @intCast(buf[i])) * 2;
327
+ const val = std.mem.readInt(u16, @as(*const [2]u8, @ptrCast(buf[i + 1 .. i + 3])), endian);
328
+ const bytes: *[2]u8 = @ptrCast(std.mem.asBytes(battle)[off .. off + 2]);
329
+ std.mem.writeInt(u16, bytes, val, endian);
330
+ }
779
331
  }