@pkmn/engine 0.1.0-dev.c544e10e → 0.1.0-dev.cb3978dd

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 (79) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +41 -14
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +44 -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 +97 -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 +21 -0
  11. package/build/pkg/addon/wasm.js.map +1 -0
  12. package/build/pkg/addon.d.ts +22 -0
  13. package/build/pkg/addon.js +35 -58
  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 +10 -13
  20. package/build/pkg/gen1.js +1 -1
  21. package/build/pkg/gen1.js.map +1 -1
  22. package/build/pkg/index.d.ts +55 -13
  23. package/build/pkg/index.js +15 -10
  24. package/build/pkg/index.js.map +1 -1
  25. package/build/pkg/protocol.d.ts +20 -1
  26. package/build/pkg/protocol.js +109 -107
  27. package/build/pkg/protocol.js.map +1 -1
  28. package/build/tools/debug.js +36 -70
  29. package/build/tools/debug.js.map +1 -1
  30. package/build/tools/display/debug.jsx +141 -0
  31. package/build/tools/display/debug.jsx.map +1 -0
  32. package/build/tools/display/index.js +141 -0
  33. package/build/tools/display/index.js.map +1 -0
  34. package/build/tools/display/ui.jsx +349 -0
  35. package/build/tools/display/ui.jsx.map +1 -0
  36. package/build/tools/display/util.js +18 -0
  37. package/build/tools/display/util.js.map +1 -0
  38. package/build.zig +253 -89
  39. package/package.json +58 -30
  40. package/src/bin/install-pkmn-engine +29 -19
  41. package/src/bin/pkmn-debug +1 -1
  42. package/src/lib/common/array.zig +77 -0
  43. package/src/lib/common/data.zig +4 -4
  44. package/src/lib/common/debug.zig +39 -8
  45. package/src/lib/common/js.zig +4 -2
  46. package/src/lib/common/optional.zig +14 -10
  47. package/src/lib/common/options.zig +6 -0
  48. package/src/lib/common/protocol.zig +75 -79
  49. package/src/lib/common/rational.zig +128 -32
  50. package/src/lib/common/rng.zig +7 -9
  51. package/src/lib/common/util.zig +50 -1
  52. package/src/lib/gen1/calc.zig +686 -159
  53. package/src/lib/gen1/chance.zig +405 -280
  54. package/src/lib/gen1/data/moves.zig +2 -4
  55. package/src/lib/gen1/data/species.zig +2 -4
  56. package/src/lib/gen1/data/types.zig +1 -2
  57. package/src/lib/gen1/data.zig +24 -27
  58. package/src/lib/gen1/helpers.zig +92 -548
  59. package/src/lib/gen1/mechanics.zig +373 -257
  60. package/src/lib/gen2/calc.zig +37 -27
  61. package/src/lib/gen2/chance.zig +84 -496
  62. package/src/lib/gen2/data/items.zig +1 -3
  63. package/src/lib/gen2/data/moves.zig +2 -4
  64. package/src/lib/gen2/data/species.zig +1 -3
  65. package/src/lib/gen2/data/types.zig +1 -2
  66. package/src/lib/gen2/data.zig +19 -22
  67. package/src/lib/gen2/generated.zig +19 -26
  68. package/src/lib/gen2/helpers.zig +46 -25
  69. package/src/lib/gen2/mechanics.zig +200 -157
  70. package/src/lib/{binding/node.zig → node.zig} +8 -13
  71. package/src/lib/pkmn.zig +36 -15
  72. package/src/lib/{binding/wasm.zig → wasm.zig} +1 -1
  73. package/src/tools/display/dom.ts +96 -0
  74. package/src/tools/display/ui.css +354 -0
  75. package/build/test/display.js +0 -400
  76. package/build/test/display.js.map +0 -1
  77. package/src/lib/gen1/README.md +0 -894
  78. package/src/lib/gen2/README.md +0 -329
  79. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -1,18 +1,21 @@
1
- const std = @import("std");
2
1
  const builtin = @import("builtin");
3
-
2
+ const data = @import("data.zig");
3
+ const options = @import("options.zig");
4
4
  const pkmn = @import("../pkmn.zig");
5
-
6
- const data = @import("./data.zig");
7
- const options = @import("./options.zig");
5
+ const std = @import("std");
8
6
 
9
7
  const assert = std.debug.assert;
8
+ const enabled = options.log;
9
+ const ID = data.ID;
10
+ const Player = data.Player;
10
11
  const print = std.debug.print;
11
12
 
12
- const enabled = options.log;
13
+ const endian = builtin.cpu.arch.endian();
14
+ const Endian = std.builtin.Endian;
15
+ const big = if (@hasField(Endian, "big")) Endian.big else Endian.Big;
16
+ const little = if (@hasField(Endian, "little")) Endian.little else Endian.Little;
13
17
 
14
- const Player = data.Player;
15
- const ID = data.ID;
18
+ const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
16
19
 
17
20
  pub const ArgType = enum(u8) {
18
21
  None,
@@ -71,7 +74,7 @@ pub const ArgType = enum(u8) {
71
74
  EndAbility,
72
75
  ClearNegativeBoost,
73
76
  FormeChange,
74
- NoTarget,
77
+ LastNoTarget, // FIXME
75
78
 
76
79
  // Gen 4
77
80
  SwapBoost,
@@ -218,6 +221,7 @@ pub const Activate = enum(u8) {
218
221
  BeatUp, // FIXME of
219
222
 
220
223
  Magnitude,
224
+ Spite,
221
225
  };
222
226
 
223
227
  pub const Start = enum(u8) {
@@ -359,8 +363,8 @@ pub fn Log(comptime Writer: type) type {
359
363
  } else {
360
364
  try self.writer.writeAll(&.{ @intFromEnum(args[1].species), args[1].level });
361
365
  }
362
- try self.writer.writeIntNative(u16, args[1].hp);
363
- try self.writer.writeIntNative(u16, args[1].stats.hp);
366
+ try self.writer.writeInt(u16, args[1].hp, endian);
367
+ try self.writer.writeInt(u16, args[1].stats.hp, endian);
364
368
  try self.writer.writeAll(&.{args[1].status});
365
369
  }
366
370
 
@@ -391,7 +395,7 @@ pub fn Log(comptime Writer: type) type {
391
395
  if (!enabled) return;
392
396
 
393
397
  try self.writer.writeByte(@intFromEnum(ArgType.Turn));
394
- try self.writer.writeIntNative(u16, args[0]);
398
+ try self.writer.writeInt(u16, args[0], endian);
395
399
  try self.writer.writeByte(@intFromEnum(ArgType.None));
396
400
  }
397
401
 
@@ -423,8 +427,8 @@ pub fn Log(comptime Writer: type) type {
423
427
  @intFromEnum(ArgType.Damage),
424
428
  @as(u8, @bitCast(args[0])),
425
429
  });
426
- try self.writer.writeIntNative(u16, args[1].hp);
427
- try self.writer.writeIntNative(u16, args[1].stats.hp);
430
+ try self.writer.writeInt(u16, args[1].hp, endian);
431
+ try self.writer.writeInt(u16, args[1].stats.hp, endian);
428
432
  try self.writer.writeAll(&.{ args[1].status, @intFromEnum(@as(Damage, args[2])) });
429
433
  if (args.len == 4) try self.writer.writeByte(@as(u8, @bitCast(args[3])));
430
434
  }
@@ -436,8 +440,8 @@ pub fn Log(comptime Writer: type) type {
436
440
 
437
441
  assert(args[2] != .Drain or args.len == 4);
438
442
  try self.writer.writeAll(&.{ @intFromEnum(ArgType.Heal), @as(u8, @bitCast(args[0])) });
439
- try self.writer.writeIntNative(u16, args[1].hp);
440
- try self.writer.writeIntNative(u16, args[1].stats.hp);
443
+ try self.writer.writeInt(u16, args[1].hp, endian);
444
+ try self.writer.writeInt(u16, args[1].stats.hp, endian);
441
445
  try self.writer.writeAll(&.{ args[1].status, @intFromEnum(@as(Heal, args[2])) });
442
446
  if (args.len == 4) try self.writer.writeByte(@as(u8, @bitCast(args[3])));
443
447
  }
@@ -542,6 +546,7 @@ pub fn Log(comptime Writer: type) type {
542
546
  // ident: ID, reason: Activate
543
547
  // ident: ID, reason: .SubstituteBlock, m: <Move>
544
548
  // ident: ID, reason: .Magnitude, num: u8
549
+ // ident: ID, reason: .Spite, m: <Move>, num: u8
545
550
  pub fn activate(self: Self, args: anytype) Error!void {
546
551
  if (!enabled) return;
547
552
 
@@ -559,6 +564,9 @@ pub fn Log(comptime Writer: type) type {
559
564
  assert(reason == .Magnitude);
560
565
  try self.writer.writeByte(args[2]);
561
566
  }
567
+ } else if (args.len == 4) {
568
+ assert(reason == .Spite);
569
+ try self.writer.writeAll(&.{ @intFromEnum(args[2]), args[3] });
562
570
  }
563
571
  }
564
572
 
@@ -706,8 +714,8 @@ pub fn Log(comptime Writer: type) type {
706
714
  if (!enabled) return;
707
715
 
708
716
  try self.writer.writeAll(&.{ @intFromEnum(ArgType.SetHP), @as(u8, @bitCast(args[0])) });
709
- try self.writer.writeIntNative(u16, args[1].hp);
710
- try self.writer.writeIntNative(u16, args[1].stats.hp);
717
+ try self.writer.writeInt(u16, args[1].hp, endian);
718
+ try self.writer.writeInt(u16, args[1].stats.hp, endian);
711
719
  try self.writer.writeAll(&.{args[1].status});
712
720
  try self.writer.writeByte(@intFromEnum(@as(SetHP, args[2])));
713
721
  }
@@ -831,10 +839,10 @@ pub const ByteStream = struct {
831
839
  try self.stream.writeByte(byte);
832
840
  }
833
841
 
834
- pub fn writeIntNative(self: Writer, comptime T: type, value: T) Error!void {
842
+ pub fn writeInt(self: Writer, comptime T: type, v: T, end: std.builtin.Endian) Error!void {
835
843
  // TODO: rework this to write directly to the buffer?
836
- var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
837
- std.mem.writeIntNative(T, &bytes, value);
844
+ var bytes: [@divExact(@field(@typeInfo(T), @tagName(Int)).bits, 8)]u8 = undefined;
845
+ std.mem.writeInt(std.math.ByteAlignedInt(@TypeOf(v)), &bytes, v, end);
838
846
  return self.writeAll(&bytes);
839
847
  }
840
848
  };
@@ -857,7 +865,7 @@ pub const ByteStream = struct {
857
865
  pub const Kind = enum { Move, Species, Type, Status, Item, Weather };
858
866
 
859
867
  pub fn format(
860
- comptime gen: anytype,
868
+ gen: anytype,
861
869
  a: []const u8,
862
870
  b: ?[]const u8,
863
871
  color: bool,
@@ -943,19 +951,19 @@ pub fn format(
943
951
  if (a[i] == N(gen.Gender.Unknown)) {
944
952
  i += 1;
945
953
  } else {
946
- var gender = if (a[i] == N(gen.Gender.Male)) "M" else "F";
954
+ const gender = if (a[i] == N(gen.Gender.Male)) "M" else "F";
947
955
  printc(", {s}", .{gender}, a, b, &i, 1, color);
948
956
  }
949
957
  }
950
958
  printc(" L{d}", .{a[i]}, a, b, &i, 1, color);
951
959
  switch (endian) {
952
- .Big => {
960
+ big => {
953
961
  var hp = @as(u16, a[i]) << 8 | @as(u16, a[i + 1]);
954
962
  printc(" {d}", .{hp}, a, b, &i, 2, color);
955
963
  hp = @as(u16, a[i]) << 8 | @as(u16, a[i + 1]);
956
964
  printc("/{d}", .{hp}, a, b, &i, 2, color);
957
965
  },
958
- .Little => {
966
+ little => {
959
967
  var hp = @as(u16, a[i + 1]) << 8 | @as(u16, a[i]);
960
968
  printc(" {d}", .{hp}, a, b, &i, 2, color);
961
969
  hp = @as(u16, a[i + 1]) << 8 | @as(u16, a[i]);
@@ -986,8 +994,8 @@ pub fn format(
986
994
  },
987
995
  .Turn => {
988
996
  const turn = switch (endian) {
989
- .Big => @as(u16, a[i]) << 8 | @as(u16, a[i + 1]),
990
- .Little => @as(u16, a[i + 1]) << 8 | @as(u16, a[i]),
997
+ big => @as(u16, a[i]) << 8 | @as(u16, a[i + 1]),
998
+ little => @as(u16, a[i + 1]) << 8 | @as(u16, a[i]),
991
999
  };
992
1000
  printc(" {d}", .{turn}, a, b, &i, 2, color);
993
1001
  },
@@ -998,13 +1006,13 @@ pub fn format(
998
1006
  var id = ID.from(@intCast(a[i]));
999
1007
  printc(" {s}({d})", .{ @tagName(id.player), id.id }, a, b, &i, 1, color);
1000
1008
  switch (endian) {
1001
- .Big => {
1009
+ big => {
1002
1010
  var hp = @as(u16, a[i]) << 8 | @as(u16, a[i + 1]);
1003
1011
  printc(" {d}", .{hp}, a, b, &i, 2, color);
1004
1012
  hp = @as(u16, a[i]) << 8 | @as(u16, a[i + 1]);
1005
1013
  printc("/{d}", .{hp}, a, b, &i, 2, color);
1006
1014
  },
1007
- .Little => {
1015
+ little => {
1008
1016
  var hp = @as(u16, a[i + 1]) << 8 | @as(u16, a[i]);
1009
1017
  printc(" {d}", .{hp}, a, b, &i, 2, color);
1010
1018
  hp = @as(u16, a[i + 1]) << 8 | @as(u16, a[i]);
@@ -1126,13 +1134,13 @@ pub fn format(
1126
1134
  const id = ID.from(@intCast(a[i]));
1127
1135
  printc(" {s}({d})", .{ @tagName(id.player), id.id }, a, b, &i, 1, color);
1128
1136
  switch (endian) {
1129
- .Big => {
1137
+ big => {
1130
1138
  var hp = @as(u16, a[i]) << 8 | @as(u16, a[i + 1]);
1131
1139
  printc(" {d}", .{hp}, a, b, &i, 2, color);
1132
1140
  hp = @as(u16, a[i]) << 8 | @as(u16, a[i + 1]);
1133
1141
  printc("/{d}", .{hp}, a, b, &i, 2, color);
1134
1142
  },
1135
- .Little => {
1143
+ little => {
1136
1144
  var hp = @as(u16, a[i + 1]) << 8 | @as(u16, a[i]);
1137
1145
  printc(" {d}", .{hp}, a, b, &i, 2, color);
1138
1146
  hp = @as(u16, a[i + 1]) << 8 | @as(u16, a[i]);
@@ -1200,14 +1208,11 @@ pub fn expectLog(
1200
1208
  ) !void {
1201
1209
  if (!enabled) return;
1202
1210
 
1203
- const color = color: {
1204
- if (std.process.hasEnvVarConstant("ZIG_DEBUG_COLOR")) {
1205
- break :color true;
1206
- } else if (std.process.hasEnvVarConstant("NO_COLOR")) {
1207
- break :color false;
1208
- } else {
1209
- break :color std.io.getStdErr().supportsAnsiEscapeCodes();
1210
- }
1211
+ const color = if (std.process.hasEnvVarConstant("ZIG_DEBUG_COLOR"))
1212
+ true
1213
+ else switch (std.io.tty.detectConfig(std.io.getStdErr())) {
1214
+ .escape_codes => true,
1215
+ else => false,
1211
1216
  };
1212
1217
 
1213
1218
  expectEqualBytes(expected, actual, offset) catch |err| switch (err) {
@@ -1239,8 +1244,6 @@ fn expectEqualBytes(expected: []const u8, actual: []const u8, offset: usize) !vo
1239
1244
  }
1240
1245
  }
1241
1246
 
1242
- const endian = builtin.target.cpu.arch.endian();
1243
-
1244
1247
  fn N(e: anytype) u8 {
1245
1248
  return @intFromEnum(e);
1246
1249
  }
@@ -1248,20 +1251,13 @@ fn N(e: anytype) u8 {
1248
1251
  const p1 = Player.P1;
1249
1252
  const p2 = Player.P2;
1250
1253
 
1251
- const gen1 = struct {
1252
- pub usingnamespace @import("../gen1/data.zig");
1253
- pub const helpers = @import("../gen1/helpers.zig");
1254
- };
1255
-
1256
- const gen2 = struct {
1257
- pub usingnamespace @import("../gen2/data.zig");
1258
- pub const helpers = @import("../gen2/helpers.zig");
1259
- };
1260
-
1261
1254
  var buf: [pkmn.LOGS_SIZE]u8 = undefined;
1262
1255
  var stream: ByteStream = .{ .buffer = &buf };
1263
1256
  var log: FixedLog = .{ .writer = stream.writer() };
1264
1257
 
1258
+ const gen1 = pkmn.gen1;
1259
+ const gen2 = pkmn.gen2;
1260
+
1265
1261
  const M1 = gen1.Move;
1266
1262
  const S1 = gen1.Species;
1267
1263
 
@@ -1350,8 +1346,8 @@ test "|switch|" {
1350
1346
  try log.switched(.{ p2.ident(3), &snorlax });
1351
1347
  const par = 0b1000000;
1352
1348
  var expected: []const u8 = switch (endian) {
1353
- .Big => &.{ N(ArgType.Switch), 0b1011, N(S1.Snorlax), 91, 0, 200, 1, 144, par },
1354
- .Little => &.{ N(ArgType.Switch), 0b1011, N(S1.Snorlax), 91, 200, 0, 144, 1, par },
1349
+ big => &.{ N(ArgType.Switch), 0b1011, N(S1.Snorlax), 91, 0, 200, 1, 144, par },
1350
+ little => &.{ N(ArgType.Switch), 0b1011, N(S1.Snorlax), 91, 200, 0, 144, 1, par },
1355
1351
  };
1356
1352
  try expectLog1(expected, buf[0..9]);
1357
1353
  stream.reset();
@@ -1361,8 +1357,8 @@ test "|switch|" {
1361
1357
  snorlax.status = 0;
1362
1358
  try log.switched(.{ p2.ident(3), &snorlax });
1363
1359
  expected = switch (endian) {
1364
- .Big => &.{ N(ArgType.Switch), 0b1011, N(S1.Snorlax), 100, 0, 0, 1, 144, 0 },
1365
- .Little => &.{ N(ArgType.Switch), 0b1011, N(S1.Snorlax), 100, 0, 0, 144, 1, 0 },
1360
+ big => &.{ N(ArgType.Switch), 0b1011, N(S1.Snorlax), 100, 0, 0, 1, 144, 0 },
1361
+ little => &.{ N(ArgType.Switch), 0b1011, N(S1.Snorlax), 100, 0, 0, 144, 1, 0 },
1366
1362
  };
1367
1363
  try expectLog1(expected, buf[0..9]);
1368
1364
  stream.reset();
@@ -1370,8 +1366,8 @@ test "|switch|" {
1370
1366
  snorlax.hp = 400;
1371
1367
  try log.switched(.{ p2.ident(3), &snorlax });
1372
1368
  expected = switch (endian) {
1373
- .Big => &.{ N(ArgType.Switch), 0b1011, N(S1.Snorlax), 100, 1, 144, 1, 144, 0 },
1374
- .Little => &.{ N(ArgType.Switch), 0b1011, N(S1.Snorlax), 100, 144, 1, 144, 1, 0 },
1369
+ big => &.{ N(ArgType.Switch), 0b1011, N(S1.Snorlax), 100, 1, 144, 1, 144, 0 },
1370
+ little => &.{ N(ArgType.Switch), 0b1011, N(S1.Snorlax), 100, 144, 1, 144, 1, 0 },
1375
1371
  };
1376
1372
  try expectLog1(expected, buf[0..9]);
1377
1373
  stream.reset();
@@ -1389,8 +1385,8 @@ test "|switch|" {
1389
1385
  N(gen2.Gender.Female),
1390
1386
  91,
1391
1387
  } ++ switch (endian) {
1392
- .Big => .{ 0, 200, 1, 144, par },
1393
- .Little => .{ 200, 0, 144, 1, par },
1388
+ big => .{ 0, 200, 1, 144, par },
1389
+ little => .{ 200, 0, 144, 1, par },
1394
1390
  });
1395
1391
  try expectLog2(expected, buf[0..10]);
1396
1392
  stream.reset();
@@ -1418,9 +1414,9 @@ test "|faint|" {
1418
1414
 
1419
1415
  test "|turn|" {
1420
1416
  try log.turn(.{42});
1421
- var expected = switch (endian) {
1422
- .Big => &.{ N(ArgType.Turn), 0, 42, N(ArgType.None) },
1423
- .Little => &.{ N(ArgType.Turn), 42, 0, N(ArgType.None) },
1417
+ const expected = switch (endian) {
1418
+ big => &.{ N(ArgType.Turn), 0, 42, N(ArgType.None) },
1419
+ little => &.{ N(ArgType.Turn), 42, 0, N(ArgType.None) },
1424
1420
  };
1425
1421
  try expectLog1(expected, buf[0..4]);
1426
1422
  stream.reset();
@@ -1444,8 +1440,8 @@ test "|-damage|" {
1444
1440
  chansey.status = gen1.Status.slp(1);
1445
1441
  try log.damage(.{ p2.ident(2), &chansey, .None });
1446
1442
  var expected: []const u8 = switch (endian) {
1447
- .Big => &.{ N(ArgType.Damage), 0b1010, 2, 100, 2, 191, 1, N(Damage.None) },
1448
- .Little => &.{ N(ArgType.Damage), 0b1010, 100, 2, 191, 2, 1, N(Damage.None) },
1443
+ big => &.{ N(ArgType.Damage), 0b1010, 2, 100, 2, 191, 1, N(Damage.None) },
1444
+ little => &.{ N(ArgType.Damage), 0b1010, 100, 2, 191, 2, 1, N(Damage.None) },
1449
1445
  };
1450
1446
  try expectLog1(expected, buf[0..8]);
1451
1447
  stream.reset();
@@ -1455,8 +1451,8 @@ test "|-damage|" {
1455
1451
  chansey.status = 0;
1456
1452
  try log.damage(.{ p2.ident(2), &chansey, .Confusion });
1457
1453
  expected = switch (endian) {
1458
- .Big => &.{ N(ArgType.Damage), 0b1010, 0, 100, 1, 0, 0, N(Damage.Confusion) },
1459
- .Little => &.{ N(ArgType.Damage), 0b1010, 100, 0, 0, 1, 0, N(Damage.Confusion) },
1454
+ big => &.{ N(ArgType.Damage), 0b1010, 0, 100, 1, 0, 0, N(Damage.Confusion) },
1455
+ little => &.{ N(ArgType.Damage), 0b1010, 100, 0, 0, 1, 0, N(Damage.Confusion) },
1460
1456
  };
1461
1457
  try expectLog1(expected, buf[0..8]);
1462
1458
  stream.reset();
@@ -1464,8 +1460,8 @@ test "|-damage|" {
1464
1460
  chansey.status = gen1.Status.init(.PSN);
1465
1461
  try log.damage(.{ p2.ident(2), &chansey, .RecoilOf, p1.ident(1) });
1466
1462
  expected = switch (endian) {
1467
- .Big => &.{ N(ArgType.Damage), 0b1010, 0, 100, 1, 0, 0b1000, N(Damage.RecoilOf), 1 },
1468
- .Little => &.{ N(ArgType.Damage), 0b1010, 100, 0, 0, 1, 0b1000, N(Damage.RecoilOf), 1 },
1463
+ big => &.{ N(ArgType.Damage), 0b1010, 0, 100, 1, 0, 0b1000, N(Damage.RecoilOf), 1 },
1464
+ little => &.{ N(ArgType.Damage), 0b1010, 100, 0, 0, 1, 0b1000, N(Damage.RecoilOf), 1 },
1469
1465
  };
1470
1466
  try expectLog1(expected, buf[0..9]);
1471
1467
  stream.reset();
@@ -1477,8 +1473,8 @@ test "|-heal|" {
1477
1473
  chansey.status = gen1.Status.slp(1);
1478
1474
  try log.heal(.{ p2.ident(2), &chansey, .None });
1479
1475
  var expected: []const u8 = switch (endian) {
1480
- .Big => &.{ N(ArgType.Heal), 0b1010, 2, 100, 2, 191, 1, N(Heal.None) },
1481
- .Little => &.{ N(ArgType.Heal), 0b1010, 100, 2, 191, 2, 1, N(Heal.None) },
1476
+ big => &.{ N(ArgType.Heal), 0b1010, 2, 100, 2, 191, 1, N(Heal.None) },
1477
+ little => &.{ N(ArgType.Heal), 0b1010, 100, 2, 191, 2, 1, N(Heal.None) },
1482
1478
  };
1483
1479
  try expectLog1(expected, buf[0..8]);
1484
1480
  stream.reset();
@@ -1488,16 +1484,16 @@ test "|-heal|" {
1488
1484
  chansey.status = 0;
1489
1485
  try log.heal(.{ p2.ident(2), &chansey, .Silent });
1490
1486
  expected = switch (endian) {
1491
- .Big => &.{ N(ArgType.Heal), 0b1010, 0, 100, 1, 0, 0, N(Heal.Silent) },
1492
- .Little => &.{ N(ArgType.Heal), 0b1010, 100, 0, 0, 1, 0, N(Heal.Silent) },
1487
+ big => &.{ N(ArgType.Heal), 0b1010, 0, 100, 1, 0, 0, N(Heal.Silent) },
1488
+ little => &.{ N(ArgType.Heal), 0b1010, 100, 0, 0, 1, 0, N(Heal.Silent) },
1493
1489
  };
1494
1490
  try expectLog1(expected, buf[0..8]);
1495
1491
  stream.reset();
1496
1492
 
1497
1493
  try log.heal(.{ p2.ident(2), &chansey, .Drain, p1.ident(1) });
1498
1494
  expected = switch (endian) {
1499
- .Big => &.{ N(ArgType.Heal), 0b1010, 0, 100, 1, 0, 0, N(Heal.Drain), 1 },
1500
- .Little => &.{ N(ArgType.Heal), 0b1010, 100, 0, 0, 1, 0, N(Heal.Drain), 1 },
1495
+ big => &.{ N(ArgType.Heal), 0b1010, 0, 100, 1, 0, 0, N(Heal.Drain), 1 },
1496
+ little => &.{ N(ArgType.Heal), 0b1010, 100, 0, 0, 1, 0, N(Heal.Drain), 1 },
1501
1497
  };
1502
1498
  try expectLog1(expected, buf[0..9]);
1503
1499
  stream.reset();
@@ -1740,8 +1736,8 @@ test "|drag|" {
1740
1736
  N(gen2.Gender.Female),
1741
1737
  91,
1742
1738
  } ++ switch (endian) {
1743
- .Big => .{ 0, 200, 1, 144, par },
1744
- .Little => .{ 200, 0, 144, 1, par },
1739
+ big => .{ 0, 200, 1, 144, par },
1740
+ little => .{ 200, 0, 144, 1, par },
1745
1741
  });
1746
1742
  try expectLog2(expected, buf[0..10]);
1747
1743
  stream.reset();
@@ -1773,9 +1769,9 @@ test "|-sethp|" {
1773
1769
  var blissey = gen2.helpers.Pokemon.init(.{ .species = .Blissey, .moves = &.{.PainSplit} });
1774
1770
  blissey.hp = 612;
1775
1771
  try log.sethp(.{ p2.ident(2), &blissey, .Silent });
1776
- var expected: []const u8 = switch (endian) {
1777
- .Big => &.{ N(ArgType.SetHP), 0b1010, 2, 100, 2, 201, 0, N(SetHP.Silent) },
1778
- .Little => &.{ N(ArgType.SetHP), 0b1010, 100, 2, 201, 2, 0, N(SetHP.Silent) },
1772
+ const expected: []const u8 = switch (endian) {
1773
+ big => &.{ N(ArgType.SetHP), 0b1010, 2, 100, 2, 201, 0, N(SetHP.Silent) },
1774
+ little => &.{ N(ArgType.SetHP), 0b1010, 100, 2, 201, 2, 0, N(SetHP.Silent) },
1779
1775
  };
1780
1776
  try expectLog2(expected, buf[0..8]);
1781
1777
  stream.reset();
@@ -6,9 +6,18 @@ const expect = std.testing.expect;
6
6
  const expectEqual = std.testing.expectEqual;
7
7
  const expectError = std.testing.expectError;
8
8
 
9
+ const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
10
+ const Float = if (@hasField(std.builtin.Type, "float")) .float else .Float;
11
+ const ComptimeInt =
12
+ if (@hasField(std.builtin.Type, "comptime_int")) .comptime_int else .ComptimeInt;
13
+ const ComptimeFloat =
14
+ if (@hasField(std.builtin.Type, "comptime_float")) .comptime_float else .ComptimeFloat;
15
+
9
16
  /// Specialization of a rational number used by the engine to compute probabilties.
10
17
  /// For performance reasons the rational is only reduced lazily and thus `reduce` must be
11
- /// invoked explicitly before reading.
18
+ /// invoked explicitly before reading. Note that this laziness means that this implementation
19
+ /// sometimes overflows in places where an eager reduced implementation would not - to achieve
20
+ /// the same behavior as an eager implementation simply call `reduce` after each operation.
12
21
  pub fn Rational(comptime T: type) type {
13
22
  return extern struct {
14
23
  const Self = @This();
@@ -18,19 +27,19 @@ pub fn Rational(comptime T: type) type {
18
27
  /// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
19
28
  q: T = 1,
20
29
 
21
- // With floats we can't rely on an overflow bit to let us know when to reduce, so we instead
22
- // start reducing when we get sufficiently close to the limit of the mantissa (in our domain
23
- // we expect updates to involve numbers < 2**10, so we should be safe not reducing before we
24
- // are 2**10 away from "overflowing" the mantissa)
25
- const REDUCE = if (@typeInfo(T) == .Float)
30
+ // With floats we can't rely on an overflow bit to let us know when to reduce, so we
31
+ // instead start reducing when we get sufficiently close to the limit of the mantissa
32
+ // (in our domain we expect updates to involve numbers < 2**10, so we should be safe
33
+ // not reducing before we are 2**10 away from "overflowing" the mantissa)
34
+ const REDUCE = if (@typeInfo(T) == Float)
26
35
  std.math.pow(T, 2, std.math.floatMantissaBits(T) - 10)
27
36
  else
28
37
  0;
29
38
 
30
39
  /// Possible error returned by operations on the Rational.
31
40
  pub const Error = switch (@typeInfo(T)) {
32
- .Int => error{Overflow},
33
- .Float => error{},
41
+ Int => error{Overflow},
42
+ Float => error{},
34
43
  else => unreachable,
35
44
  };
36
45
 
@@ -43,20 +52,19 @@ pub fn Rational(comptime T: type) type {
43
52
  /// Update the rational by multiplying its numerator by p and its denominator by q.
44
53
  /// Both p and q must be >= 1, and if computable at comptime must have no common factors.
45
54
  pub fn update(r: *Self, p: anytype, q: anytype) Error!void {
46
- // FIXME
47
- // std.debug.print("({d}/{d}) * ", .{ p, q });
55
+ // std.debug.print("({d}/{d}) * ", .{ p, q }); // DEBUG
48
56
  assert(p >= 1);
49
57
  assert(q >= 1);
50
58
 
51
59
  // If our parameters are not fully reduced they may prematurely
52
60
  // cause overflow/loss of precision after the multiplication below
53
61
  assert(switch (@typeInfo(@TypeOf(p, q))) {
54
- .ComptimeInt, .ComptimeFloat => comptime gcd(p, q),
62
+ ComptimeInt, ComptimeFloat => comptime gcd(p, q),
55
63
  else => 1,
56
64
  } == 1);
57
65
 
58
66
  switch (@typeInfo(T)) {
59
- .Int => {
67
+ Int => {
60
68
  // Greedily attempt to multiply and if it fails, reduce and try again
61
69
  r.multiplication(p, q) catch |err| switch (err) {
62
70
  error.Overflow => {
@@ -66,16 +74,16 @@ pub fn Rational(comptime T: type) type {
66
74
  else => unreachable,
67
75
  };
68
76
  },
69
- .Float => {
77
+ Float => {
70
78
  // Reduce in situations where we're likely to start losing precision
71
79
  if (r.q > REDUCE or r.p > REDUCE) r.reduce();
72
80
 
73
81
  r.p *= switch (@typeInfo(@TypeOf(p))) {
74
- .Float, .ComptimeFloat => p,
82
+ Float, ComptimeFloat => p,
75
83
  else => @floatFromInt(p),
76
84
  };
77
85
  r.q *= switch (@typeInfo(@TypeOf(q))) {
78
- .Float, .ComptimeFloat => q,
86
+ Float, ComptimeFloat => q,
79
87
  else => @floatFromInt(q),
80
88
  };
81
89
 
@@ -90,7 +98,7 @@ pub fn Rational(comptime T: type) type {
90
98
  /// Add two rationals using the identity (a/b) + (c/d) = (ad+bc)/(bd).
91
99
  pub fn add(r: *Self, s: *Self) Error!void {
92
100
  switch (@typeInfo(T)) {
93
- .Int => {
101
+ Int => {
94
102
  if (r.q == s.q) {
95
103
  r.p = std.math.add(T, r.p, s.p) catch |err| switch (err) {
96
104
  error.Overflow => val: {
@@ -111,7 +119,7 @@ pub fn Rational(comptime T: type) type {
111
119
  };
112
120
  }
113
121
  },
114
- .Float => {
122
+ Float => {
115
123
  if (r.q == s.q) {
116
124
  if (r.p > REDUCE) r.reduce();
117
125
  if (s.p > REDUCE) s.reduce();
@@ -136,7 +144,7 @@ pub fn Rational(comptime T: type) type {
136
144
  /// Multiplies two rationals.
137
145
  pub fn mul(r: *Self, s: *Self) Error!void {
138
146
  switch (@typeInfo(T)) {
139
- .Int => {
147
+ Int => {
140
148
  r.multiplication(s.p, s.q) catch |err| switch (err) {
141
149
  error.Overflow => {
142
150
  r.reduce();
@@ -146,7 +154,7 @@ pub fn Rational(comptime T: type) type {
146
154
  else => unreachable,
147
155
  };
148
156
  },
149
- .Float => {
157
+ Float => {
150
158
  if (r.q > REDUCE or r.p > REDUCE) r.reduce();
151
159
  if (s.q > REDUCE or s.p > REDUCE) s.reduce();
152
160
 
@@ -192,9 +200,9 @@ pub fn Rational(comptime T: type) type {
192
200
 
193
201
  fn addition(r: *Self, p: anytype, q: anytype) !void {
194
202
  // (a/b) + (c/d) = (ad+bc)/(bd)
195
- var d = try std.math.mul(T, r.q, q);
196
- var n1 = try std.math.mul(T, r.p, q);
197
- var n2 = try std.math.mul(T, r.q, p);
203
+ const d = try std.math.mul(T, r.q, q);
204
+ const n1 = try std.math.mul(T, r.p, q);
205
+ const n2 = try std.math.mul(T, r.q, p);
198
206
 
199
207
  r.p = try std.math.add(T, n1, n2);
200
208
  r.q = d;
@@ -202,23 +210,74 @@ pub fn Rational(comptime T: type) type {
202
210
  };
203
211
  }
204
212
 
205
- // https://en.wikipedia.org/wiki/Euclidean_algorithm
206
213
  fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
207
214
  assert(p >= 1);
208
215
  assert(q >= 1);
209
216
 
210
- var a = p;
211
- var b = q;
212
- var c: @TypeOf(p, q) = undefined;
217
+ // convert comptime_int to a sized integer within this function so that @ctz will work
218
+ const T = switch (@TypeOf(p, q)) {
219
+ comptime_int => std.math.IntFittingRange(@min(p, q), @max(p, q)),
220
+ else => |U| U,
221
+ };
222
+
223
+ switch (@typeInfo(T)) {
224
+ Int => {
225
+ // std.math.gcd but without some checks because we have a stricter range
226
+ var x: T = @intCast(p);
227
+ var y: T = @intCast(q);
228
+
229
+ const xz = @ctz(x);
230
+ const yz = @ctz(y);
231
+ const shift = @min(xz, yz);
232
+ x >>= @intCast(xz);
233
+ y >>= @intCast(yz);
234
+
235
+ var diff = y -% x;
236
+ while (diff != 0) : (diff = y -% x) {
237
+ const zeros = @ctz(diff);
238
+ if (x > y) diff = -%diff;
239
+ y = @min(x, y);
240
+ x = diff >> @intCast(zeros);
241
+ }
242
+
243
+ const result = y << @intCast(shift);
244
+ assert(result > 0);
245
+ return result;
246
+ },
247
+ else => {
248
+ var a = p;
249
+ var b = q;
250
+ var c: T = undefined;
251
+
252
+ while (b != 0) {
253
+ c = b;
254
+ b = @mod(a, b);
255
+ a = c;
256
+ }
257
+
258
+ assert(a > 0);
259
+ return a;
260
+ },
261
+ }
262
+ }
263
+
264
+ test gcd {
265
+ const seed = if (@hasDecl(std.testing, "random_seed")) std.testing.random_seed else 0x12345678;
266
+ var prng = (if (@hasDecl(std, "Random")) std.Random else std.rand).DefaultPrng.init(seed);
267
+ var random = prng.random();
268
+
269
+ for (0..1000) |_| {
270
+ const a = random.int(u32);
271
+ const b = random.int(u32);
272
+ if (a == 0 or b == 0) continue;
213
273
 
214
- while (b != 0) {
215
- c = b;
216
- b = @mod(a, b);
217
- a = c;
274
+ try expectEqual(
275
+ gcd(a, b),
276
+ @as(u32, @intFromFloat(gcd(@as(f64, @floatFromInt(a)), @as(f64, @floatFromInt(b))))),
277
+ );
218
278
  }
219
279
 
220
- assert(a > 0);
221
- return a;
280
+ try expectEqual(gcd(300_000, @as(u32, 2_300_000)), 100_000); // NB: @ctz requires an @intCast
222
281
  }
223
282
 
224
283
  fn doTurn(r: anytype) !void {
@@ -234,6 +293,8 @@ test Rational {
234
293
  var r: Rational(t) = .{};
235
294
 
236
295
  var c: t = 128;
296
+ _ = &c;
297
+
237
298
  try r.update(c, 256);
238
299
  try doTurn(&r);
239
300
 
@@ -320,6 +381,8 @@ test BigRational {
320
381
  try r.reset();
321
382
 
322
383
  var c: u8 = 128;
384
+ _ = &c;
385
+
323
386
  try r.update(c, 256);
324
387
  try doTurn(&r);
325
388
 
@@ -351,3 +414,36 @@ test BigRational {
351
414
  try s.setRatio(71, 78);
352
415
  try expect((try s.order(r.val)) == .eq);
353
416
  }
417
+
418
+ test "minimum" {
419
+ // TODO: ziglang/zig#21069
420
+ if (builtin.mode == .ReleaseFast or builtin.mode == .ReleaseSmall) {
421
+ return error.SkipZigTest;
422
+ }
423
+ var r = try BigRational.init(std.testing.allocator);
424
+ defer r.deinit();
425
+ var s = try std.math.big.Rational.init(std.testing.allocator);
426
+ defer s.deinit();
427
+
428
+ try r.reset();
429
+
430
+ try r.update(1, 2); // Speed Tie
431
+ for (0..2) |_| {
432
+ try r.update(1, 2); // Confusion
433
+ try r.update(1, 2); // Infatuation
434
+ try r.update(3, 4); // Paralysis
435
+ // Population Bomb
436
+ for (0..10) |_| {
437
+ try r.update(9, 10); // Accuracy
438
+ try r.update(1, 15); // Damage roll
439
+ try r.update(1, 24); // Critical Hit
440
+ try r.update(9, 10); // King's Rock non-proc
441
+ }
442
+ }
443
+
444
+ const ZEROS = std.math.pow(u310, 10, 31);
445
+ const n = 36472996377170786403;
446
+ const d: u310 = 146267071761884981524886149560653378220687632500762654519853056 * ZEROS;
447
+ try s.setRatio(n, d);
448
+ try expect((try s.order(r.val)) == .eq);
449
+ }