@pkmn/engine 0.1.0-dev.eaae4aa6 → 0.1.0-dev.f3aab252

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 (41) hide show
  1. package/LICENSE +0 -2
  2. package/README.md +15 -11
  3. package/build/pkg/addon.js +6 -7
  4. package/build/pkg/addon.js.map +1 -1
  5. package/build/pkg/data.d.ts +2 -3
  6. package/build/pkg/index.d.ts +1 -1
  7. package/build/pkg/index.js.map +1 -1
  8. package/build/pkg/protocol.js +4 -4
  9. package/build/pkg/protocol.js.map +1 -1
  10. package/build/test/display.js +8 -3
  11. package/build/test/display.js.map +1 -1
  12. package/build/tools/debug.js +2 -3
  13. package/build/tools/debug.js.map +1 -1
  14. package/build.zig +187 -72
  15. package/package.json +18 -16
  16. package/src/bin/install-pkmn-engine +17 -7
  17. package/src/lib/common/array.zig +77 -0
  18. package/src/lib/common/data.zig +8 -8
  19. package/src/lib/common/debug.zig +2 -2
  20. package/src/lib/common/js.zig +4 -2
  21. package/src/lib/common/optional.zig +13 -9
  22. package/src/lib/common/options.zig +2 -0
  23. package/src/lib/common/protocol.zig +51 -44
  24. package/src/lib/common/rational.zig +114 -24
  25. package/src/lib/common/rng.zig +10 -10
  26. package/src/lib/common/util.zig +50 -1
  27. package/src/lib/gen1/calc.zig +506 -128
  28. package/src/lib/gen1/chance.zig +383 -276
  29. package/src/lib/gen1/data/species.zig +1 -1
  30. package/src/lib/gen1/data.zig +31 -30
  31. package/src/lib/gen1/helpers.zig +95 -538
  32. package/src/lib/gen1/mechanics.zig +265 -162
  33. package/src/lib/gen2/calc.zig +34 -16
  34. package/src/lib/gen2/chance.zig +90 -502
  35. package/src/lib/gen2/data.zig +15 -14
  36. package/src/lib/gen2/helpers.zig +41 -13
  37. package/src/lib/gen2/mechanics.zig +158 -103
  38. package/src/lib/pkmn.zig +10 -16
  39. package/src/test/display/pkmn.html.tmpl +5 -5
  40. package/src/lib/gen1/README.md +0 -894
  41. package/src/lib/gen2/README.md +0 -329
@@ -1091,7 +1091,7 @@ pub const Species = enum(u8) {
1091
1091
  /// The number of Pokémon species in this generation.
1092
1092
  pub const size = 151;
1093
1093
 
1094
- /// The Pokémon's critical hit rate=io out of 256.
1094
+ /// The Pokémon's critical hit ratio out of 256.
1095
1095
  pub fn chance(id: Species) u8 {
1096
1096
  assert(id != .None);
1097
1097
  return CHANCES[@intFromEnum(id) - 1];
@@ -22,11 +22,11 @@ const expect = std.testing.expect;
22
22
 
23
23
  /// The minimum size in bytes required to hold all Generation I choice options.
24
24
  pub const MAX_CHOICES: usize = 9; // move 1..4, switch 2..6
25
- /// The optimal size in bytes required to hold all Generation I choice options.
26
- /// At least as large as MAX_CHOICES.
25
+ /// The maximum number of bytes possibly logged by a single Generation I update.
27
26
  pub const MAX_LOGS: usize = 180;
28
27
 
29
- /// The maximum number of bytes possibly logged by a single Generation I update.
28
+ /// The optimal size in bytes required to hold all Generation I choice options.
29
+ /// At least as large as MAX_CHOICES.
30
30
  pub const CHOICES_SIZE = if (builtin.mode == .ReleaseSmall)
31
31
  MAX_CHOICES
32
32
  else
@@ -47,6 +47,7 @@ const Player = data.Player;
47
47
  const Result = data.Result;
48
48
 
49
49
  const PointerType = util.PointerType;
50
+ const isPointerTo = util.isPointerTo;
50
51
 
51
52
  const showdown = pkmn.options.showdown;
52
53
 
@@ -71,13 +72,13 @@ pub fn Battle(comptime RNG: anytype) type {
71
72
 
72
73
  /// Returns the `Side` for the given `player`.
73
74
  pub fn side(self: anytype, player: Player) PointerType(@TypeOf(self), Side) {
74
- assert(@typeInfo(@TypeOf(self)).Pointer.child == Self);
75
+ assert(isPointerTo(self, Self));
75
76
  return &self.sides[@intFromEnum(player)];
76
77
  }
77
78
 
78
79
  /// Returns the `Side` of the opponent for the given `player`
79
80
  pub fn foe(self: anytype, player: Player) PointerType(@TypeOf(self), Side) {
80
- assert(@typeInfo(@TypeOf(self)).Pointer.child == Self);
81
+ assert(isPointerTo(self, Self));
81
82
  return &self.sides[@intFromEnum(player.foe())];
82
83
  }
83
84
 
@@ -91,7 +92,7 @@ pub fn Battle(comptime RNG: anytype) type {
91
92
  self: anytype,
92
93
  player: Player,
93
94
  ) PointerType(@TypeOf(self), MoveDetails) {
94
- assert(@typeInfo(@TypeOf(self)).Pointer.child == Self);
95
+ assert(isPointerTo(self, Self));
95
96
  return &self.last_moves[@intFromEnum(player)];
96
97
  }
97
98
 
@@ -142,7 +143,7 @@ pub const Side = extern struct {
142
143
 
143
144
  /// Returns the stored `Pokemon` corresponding to the one-indexed party `slot`.
144
145
  pub fn get(self: anytype, slot: u8) PointerType(@TypeOf(self), Pokemon) {
145
- assert(@typeInfo(@TypeOf(self)).Pointer.child == Side);
146
+ assert(isPointerTo(self, Side));
146
147
  assert(slot > 0 and slot <= 6);
147
148
  const id = self.order[slot - 1];
148
149
  assert(id > 0 and id <= 6);
@@ -151,7 +152,7 @@ pub const Side = extern struct {
151
152
 
152
153
  /// Returns the stored `Pokemon` corresponding to the `active` Pokémon (slot 1).
153
154
  pub fn stored(self: anytype) PointerType(@TypeOf(self), Pokemon) {
154
- assert(@typeInfo(@TypeOf(self)).Pointer.child == Side);
155
+ assert(isPointerTo(self, Side));
155
156
  return self.get(1);
156
157
  }
157
158
  };
@@ -177,7 +178,7 @@ pub const ActivePokemon = extern struct {
177
178
 
178
179
  /// Returns the active Pokémon's current move slot located at the one-indexed `mslot`.
179
180
  pub fn move(self: anytype, mslot: u8) PointerType(@TypeOf(self), MoveSlot) {
180
- assert(@typeInfo(@TypeOf(self)).Pointer.child == ActivePokemon);
181
+ assert(isPointerTo(self, ActivePokemon));
181
182
  assert(mslot > 0 and mslot <= 4);
182
183
  assert(self.moves[mslot - 1].id != .None);
183
184
  return &self.moves[mslot - 1];
@@ -207,7 +208,7 @@ pub const Pokemon = extern struct {
207
208
 
208
209
  /// Returns the Pokémon's original move slot located at the one-indexed `mslot`.
209
210
  pub fn move(self: anytype, mslot: u8) PointerType(@TypeOf(self), MoveSlot) {
210
- assert(@typeInfo(@TypeOf(self)).Pointer.child == Pokemon);
211
+ assert(isPointerTo(self, Pokemon));
211
212
  assert(mslot > 0 and mslot <= 4);
212
213
  assert(self.moves[mslot - 1].id != .None);
213
214
  return &self.moves[mslot - 1];
@@ -255,7 +256,7 @@ pub const Status = enum(u8) {
255
256
 
256
257
  pub const TOX = 0b10001000;
257
258
 
258
- const SLP = 0b111;
259
+ const SLEEP = 0b111;
259
260
 
260
261
  /// Whether or not the status `num` is the same as `status`.
261
262
  pub fn is(num: u8, status: Status) bool {
@@ -283,7 +284,7 @@ pub const Status = enum(u8) {
283
284
 
284
285
  /// Returns the duration of a sleep status.
285
286
  pub fn duration(num: u8) u3 {
286
- return @intCast(num & SLP);
287
+ return @intCast(num & SLEEP);
287
288
  }
288
289
 
289
290
  /// Returns whether `num` reflects any status.
@@ -316,12 +317,12 @@ test Status {
316
317
  try expect(Status.is(Status.slp(5), .SLP));
317
318
  try expect(!Status.is(Status.slp(5), .EXT));
318
319
  try expect(!Status.is(Status.slp(7), .PSN));
319
- try expectEqual(5, Status.duration(Status.slp(5)));
320
+ try expectEqual(@as(u3, 5), Status.duration(Status.slp(5)));
320
321
  try expect(Status.is(Status.slf(2), .SLP));
321
322
  try expect(Status.is(Status.slf(2), .EXT));
322
323
  try expect(Status.is(Status.TOX, .PSN));
323
324
  try expect(Status.is(Status.TOX, .EXT));
324
- try expectEqual(1, Status.duration(Status.slp(1)));
325
+ try expectEqual(@as(u3, 1), Status.duration(Status.slp(1)));
325
326
  }
326
327
 
327
328
  /// Bitfield representation of volatile statuses and associated data in Generation I.
@@ -405,16 +406,16 @@ test Volatiles {
405
406
  try expect(!volatiles.Transform);
406
407
  try expect(!volatiles.MultiHit);
407
408
 
408
- try expectEqual(235, volatiles.state);
409
- try expectEqual(42, volatiles.substitute);
410
- try expectEqual(2, volatiles.disable_move);
411
- try expectEqual(4, volatiles.disable_duration);
412
- try expectEqual(2, volatiles.confusion);
413
- try expectEqual(4, volatiles.toxic);
414
- try expectEqual(3, volatiles.attacks);
409
+ try expectEqual(@as(u16, 235), volatiles.state);
410
+ try expectEqual(@as(u8, 42), volatiles.substitute);
411
+ try expectEqual(@as(u4, 2), volatiles.disable_move);
412
+ try expectEqual(@as(u4, 4), volatiles.disable_duration);
413
+ try expectEqual(@as(u3, 2), volatiles.confusion);
414
+ try expectEqual(@as(u5, 4), volatiles.toxic);
415
+ try expectEqual(@as(u3, 3), volatiles.attacks);
415
416
  }
416
417
 
417
- /// Representaiton of a Pokémon's stats in Generation I.
418
+ /// Representation of a Pokémon's stats in Generation I.
418
419
  pub fn Stats(comptime T: type) type {
419
420
  return extern struct {
420
421
  hp: T = 0,
@@ -442,9 +443,9 @@ test Stats {
442
443
  try expectEqual(0, stats.def);
443
444
 
444
445
  var base = Species.get(.Gyarados).stats;
445
- try expectEqual(127, Stats(u16).calc("hp", base.hp, 0, 5120, 38));
446
+ try expectEqual(@as(u16, 127), Stats(u16).calc("hp", base.hp, 0, 5120, 38));
446
447
  base = Species.get(.Pidgeot).stats;
447
- try expectEqual(279, Stats(u16).calc("spe", base.spe, 15, 63001, 100));
448
+ try expectEqual(@as(u16, 279), Stats(u16).calc("spe", base.spe, 15, 63001, 100));
448
449
  }
449
450
 
450
451
  /// Representation of a Pokémon's boosts in Generation I.
@@ -537,7 +538,7 @@ pub const Species = species.Species;
537
538
 
538
539
  test Species {
539
540
  try expectEqual(2, @intFromEnum(Species.Ivysaur));
540
- try expectEqual(100, Species.get(.Mew).stats.def);
541
+ try expectEqual(@as(u8, 100), Species.get(.Mew).stats.def);
541
542
  }
542
543
 
543
544
  /// Representation of a Generation I type in Pokémon.
@@ -576,8 +577,8 @@ test Types {
576
577
  try expectEqual(Effectiveness.Resisted, Type.effectiveness(.Fire, .Water));
577
578
  try expectEqual(Effectiveness.Neutral, Type.effectiveness(.Normal, .Grass));
578
579
 
579
- try expectEqual(3, Type.precedence(.Water, .Water));
580
- try expectEqual(26, Type.precedence(.Bug, .Ghost));
580
+ try expectEqual(@as(u8, 3), Type.precedence(.Water, .Water));
581
+ try expectEqual(@as(u8, 26), Type.precedence(.Bug, .Ghost));
581
582
  try expect(Type.precedence(.Poison, .Bug) > Type.precedence(.Poison, .Poison));
582
583
 
583
584
  const t: Types = .{ .type1 = .Rock, .type2 = .Ground };
@@ -617,18 +618,18 @@ pub const DVs = struct {
617
618
 
618
619
  test DVs {
619
620
  var dvs: DVs = .{ .spc = 15, .spe = 15 };
620
- try expectEqual(15, dvs.hp());
621
+ try expectEqual(@as(u4, 15), dvs.hp());
621
622
  dvs = .{
622
623
  .atk = 5,
623
624
  .def = 15,
624
625
  .spe = 13,
625
626
  .spc = 13,
626
627
  };
627
- try expectEqual(15, dvs.hp());
628
+ try expectEqual(@as(u4, 15), dvs.hp());
628
629
  dvs = .{
629
630
  .def = 3,
630
631
  .spe = 10,
631
632
  .spc = 11,
632
633
  };
633
- try expectEqual(13, dvs.hp());
634
+ try expectEqual(@as(u4, 13), dvs.hp());
634
635
  }