@pkmn/engine 0.1.0-dev.5ea92492 → 0.1.0-dev.6158d5ad

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 (48) hide show
  1. package/README.md +71 -60
  2. package/build/pkg/addon.d.ts +2 -2
  3. package/build/pkg/addon.js +8 -7
  4. package/build/pkg/addon.js.map +1 -1
  5. package/build/pkg/data/layout.json +25 -3
  6. package/build/pkg/gen1.d.ts +5 -4
  7. package/build/pkg/gen1.js +47 -31
  8. package/build/pkg/gen1.js.map +1 -1
  9. package/build/pkg/index.d.ts +15 -12
  10. package/build/pkg/index.js.map +1 -1
  11. package/build/pkg/protocol.d.ts +4 -2
  12. package/build/pkg/protocol.js +4 -2
  13. package/build/pkg/protocol.js.map +1 -1
  14. package/build/test/display.js +31 -13
  15. package/build/test/display.js.map +1 -1
  16. package/build.zig +94 -77
  17. package/package.json +26 -16
  18. package/src/bin/install-pkmn-engine +11 -6
  19. package/src/lib/binding/node.zig +46 -141
  20. package/src/lib/binding/wasm.zig +6 -4
  21. package/src/lib/common/battle.zig +17 -0
  22. package/src/lib/common/data.zig +11 -11
  23. package/src/lib/common/debug.zig +21 -11
  24. package/src/lib/common/js.zig +118 -0
  25. package/src/lib/common/optional.zig +60 -0
  26. package/src/lib/common/options.zig +12 -3
  27. package/src/lib/common/protocol.zig +242 -178
  28. package/src/lib/common/rational.zig +353 -0
  29. package/src/lib/common/rng.zig +10 -10
  30. package/src/lib/common/util.zig +12 -0
  31. package/src/lib/gen1/README.md +255 -154
  32. package/src/lib/gen1/calc.zig +427 -0
  33. package/src/lib/gen1/chance.zig +1044 -0
  34. package/src/lib/gen1/data/moves.zig +10 -10
  35. package/src/lib/gen1/data/species.zig +2 -2
  36. package/src/lib/gen1/data/types.zig +3 -3
  37. package/src/lib/gen1/data.zig +69 -44
  38. package/src/lib/gen1/helpers.zig +592 -32
  39. package/src/lib/gen1/mechanics.zig +2599 -2312
  40. package/src/lib/gen2/README.md +12 -13
  41. package/src/lib/gen2/data/items.zig +3 -3
  42. package/src/lib/gen2/data/moves.zig +1 -1
  43. package/src/lib/gen2/data/species.zig +1 -1
  44. package/src/lib/gen2/data/types.zig +3 -3
  45. package/src/lib/gen2/data.zig +37 -26
  46. package/src/lib/gen2/helpers.zig +9 -11
  47. package/src/lib/gen2/mechanics.zig +5 -4
  48. package/src/lib/pkmn.zig +68 -9
@@ -1602,44 +1602,44 @@ pub const Move = enum(u8) {
1602
1602
 
1603
1603
  /// Whether this effect activates during the "begin" step of move execution.
1604
1604
  pub inline fn onBegin(effect: Effect) bool {
1605
- return @enumToInt(effect) > 0 and @enumToInt(effect) <= 16;
1605
+ return @intFromEnum(effect) > 0 and @intFromEnum(effect) <= 16;
1606
1606
  }
1607
1607
 
1608
1608
  /// Whether this effect lowers stats.
1609
1609
  pub inline fn isStatDown(effect: Effect) bool {
1610
- return @enumToInt(effect) > 16 and @enumToInt(effect) <= 21;
1610
+ return @intFromEnum(effect) > 16 and @intFromEnum(effect) <= 21;
1611
1611
  }
1612
1612
 
1613
1613
  /// Whether this effect activates during the "end" step of move execution.
1614
1614
  pub inline fn onEnd(effect: Effect) bool {
1615
- return @enumToInt(effect) > 16 and @enumToInt(effect) <= 31;
1615
+ return @intFromEnum(effect) > 16 and @intFromEnum(effect) <= 31;
1616
1616
  }
1617
1617
 
1618
1618
  /// Whether this effect is considered to "always happen".
1619
1619
  pub inline fn alwaysHappens(effect: Effect) bool {
1620
- return @enumToInt(effect) > 31 and @enumToInt(effect) <= 38;
1620
+ return @intFromEnum(effect) > 31 and @intFromEnum(effect) <= 38;
1621
1621
  }
1622
1622
 
1623
1623
  /// Whether this effect is handled specially by the engine.
1624
1624
  pub inline fn isSpecial(effect: Effect) bool {
1625
1625
  // NB: isSpecial includes isMulti up to Twineedle
1626
- return @enumToInt(effect) > 31 and @enumToInt(effect) <= 46;
1626
+ return @intFromEnum(effect) > 31 and @intFromEnum(effect) <= 46;
1627
1627
  }
1628
1628
 
1629
1629
  /// Whether this effect is a multi-hit effect.
1630
1630
  pub inline fn isMulti(effect: Effect) bool {
1631
- return @enumToInt(effect) > 44 and @enumToInt(effect) <= 47;
1631
+ return @intFromEnum(effect) > 44 and @intFromEnum(effect) <= 47;
1632
1632
  }
1633
1633
 
1634
1634
  /// Whether this effect is has chance of lowering stats.
1635
1635
  pub inline fn isStatDownChance(effect: Effect) bool {
1636
- return @enumToInt(effect) > 47 and @enumToInt(effect) <= 51;
1636
+ return @intFromEnum(effect) > 47 and @intFromEnum(effect) <= 51;
1637
1637
  }
1638
1638
 
1639
1639
  /// Whether this effect has a secondary chance.
1640
1640
  pub inline fn isSecondaryChance(effect: Effect) bool {
1641
1641
  // NB: isSecondaryChance includes isStatDownChance as well as Twineedle
1642
- return (@enumToInt(effect) > 46 and @enumToInt(effect) <= 61);
1642
+ return (@intFromEnum(effect) > 46 and @intFromEnum(effect) <= 61);
1643
1643
  }
1644
1644
  };
1645
1645
 
@@ -1843,12 +1843,12 @@ pub const Move = enum(u8) {
1843
1843
  /// Returns the `Data` corresponding to the move.
1844
1844
  pub inline fn get(id: Move) Data {
1845
1845
  assert(id != .None and id != .SKIP_TURN);
1846
- return DATA[@enumToInt(id) - 1];
1846
+ return DATA[@intFromEnum(id) - 1];
1847
1847
  }
1848
1848
 
1849
1849
  /// Returns the base PP of the move.
1850
1850
  pub fn pp(id: Move) u8 {
1851
1851
  assert(id != .None);
1852
- return PP[@enumToInt(id) - 1];
1852
+ return PP[@intFromEnum(id) - 1];
1853
1853
  }
1854
1854
  };
@@ -1094,12 +1094,12 @@ pub const Species = enum(u8) {
1094
1094
  /// The Pokémon's critical hit rate=io out of 256.
1095
1095
  pub inline fn chance(id: Species) u8 {
1096
1096
  assert(id != .None);
1097
- return CHANCES[@enumToInt(id) - 1];
1097
+ return CHANCES[@intFromEnum(id) - 1];
1098
1098
  }
1099
1099
 
1100
1100
  /// Returns the `Data` corresponding to the species.
1101
1101
  pub fn get(id: Species) Data {
1102
1102
  assert(id != .None);
1103
- return DATA[@enumToInt(id) - 1];
1103
+ return DATA[@intFromEnum(id) - 1];
1104
1104
  }
1105
1105
  };
@@ -91,18 +91,18 @@ pub const Type = enum(u4) {
91
91
 
92
92
  /// Whether or not this type is considered to be special as opposed to physical.
93
93
  pub inline fn special(self: Type) bool {
94
- return @enumToInt(self) >= @enumToInt(Type.Fire);
94
+ return @intFromEnum(self) >= @intFromEnum(Type.Fire);
95
95
  }
96
96
 
97
97
  /// The `Effectiveness` of type `t2` vs. type `t1`.
98
98
  pub inline fn effectiveness(t1: Type, t2: Type) Effectiveness {
99
- return CHART[@enumToInt(t1)][@enumToInt(t2)];
99
+ return CHART[@intFromEnum(t1)][@intFromEnum(t2)];
100
100
  }
101
101
 
102
102
  /// The precedence order of type `t2` vs. type `t1`.
103
103
  pub fn precedence(t1: Type, t2: Type) u8 {
104
104
  for (PRECEDENCE, 0..) |matchup, i| {
105
- if (matchup.type1 == t1 and matchup.type2 == t2) return @intCast(u8, i);
105
+ if (matchup.type1 == t1 and matchup.type2 == t2) return @intCast(i);
106
106
  }
107
107
  unreachable;
108
108
  }
@@ -1,15 +1,19 @@
1
1
  const std = @import("std");
2
2
  const builtin = @import("builtin");
3
3
 
4
+ const pkmn = @import("../pkmn.zig");
5
+
4
6
  const data = @import("../common/data.zig");
5
- const options = @import("../common/options.zig");
6
7
  const protocol = @import("../common/protocol.zig");
7
8
  const rng = @import("../common/rng.zig");
9
+ const util = @import("../common/util.zig");
8
10
 
9
11
  const moves = @import("data/moves.zig");
10
12
  const species = @import("data/species.zig");
11
13
  const types = @import("data/types.zig");
12
14
 
15
+ const calc = @import("calc.zig");
16
+ const chance = @import("chance.zig");
13
17
  const mechanics = @import("mechanics.zig");
14
18
 
15
19
  const assert = std.debug.assert;
@@ -17,16 +21,16 @@ const expectEqual = std.testing.expectEqual;
17
21
  const expect = std.testing.expect;
18
22
 
19
23
  /// The minimum size in bytes required to hold all Generation I choice options.
20
- pub const MAX_OPTIONS: usize = 9; // move 1..4, switch 2..6
24
+ pub const MAX_CHOICES: usize = 9; // move 1..4, switch 2..6
21
25
  /// The optimal size in bytes required to hold all Generation I choice options.
22
- /// At least as large as MAX_OPTIONS.
26
+ /// At least as large as MAX_CHOICES.
23
27
  pub const MAX_LOGS: usize = 180;
24
28
 
25
29
  /// The maximum number of bytes possibly logged by a single Generation I update.
26
- pub const OPTIONS_SIZE = if (builtin.mode == .ReleaseSmall)
27
- MAX_OPTIONS
30
+ pub const CHOICES_SIZE = if (builtin.mode == .ReleaseSmall)
31
+ MAX_CHOICES
28
32
  else
29
- std.math.ceilPowerOfTwo(usize, MAX_OPTIONS) catch unreachable;
33
+ std.math.ceilPowerOfTwo(usize, MAX_CHOICES) catch unreachable;
30
34
  /// The optimal size in bytes required to hold the largest amount of log data possible from a
31
35
  /// single Generation I update. At least as large as MAX_LOGS.
32
36
  pub const LOGS_SIZE = if (builtin.mode == .ReleaseSmall)
@@ -34,12 +38,17 @@ pub const LOGS_SIZE = if (builtin.mode == .ReleaseSmall)
34
38
  else
35
39
  std.math.ceilPowerOfTwo(usize, MAX_LOGS) catch unreachable;
36
40
 
41
+ /// Null object pattern implementation of pkmn.battle.Options for Generation I.
42
+ pub const NULL = pkmn.battle.options(protocol.NULL, chance.NULL, calc.NULL);
43
+
37
44
  const Choice = data.Choice;
38
45
  const ID = data.ID;
39
46
  const Player = data.Player;
40
47
  const Result = data.Result;
41
48
 
42
- const showdown = options.showdown;
49
+ const PointerType = util.PointerType;
50
+
51
+ const showdown = pkmn.options.showdown;
43
52
 
44
53
  /// The pseudo random number generator used by Generation I.
45
54
  pub const PRNG = rng.PRNG(1);
@@ -55,31 +64,42 @@ pub fn Battle(comptime RNG: anytype) type {
55
64
  turn: u16 = 0,
56
65
  /// The last damage dealt by either side.
57
66
  last_damage: u16 = 0,
58
- /// The slot index of the last selected moves for each side
59
- last_selected_indexes: MoveIndexes = .{},
67
+ /// Details about the last move selected/used by each player required to detect desyncs.
68
+ last_moves: [2]MoveDetails = [_]MoveDetails{.{}} ** 2,
60
69
  /// The pseudo random number generator.
61
70
  rng: RNG,
62
71
 
63
72
  /// Returns the `Side` for the given `player`.
64
- pub inline fn side(self: *Self, player: Player) *Side {
65
- return &self.sides[@enumToInt(player)];
73
+ pub inline fn side(self: anytype, player: Player) PointerType(@TypeOf(self), Side) {
74
+ assert(@typeInfo(@TypeOf(self)).Pointer.child == Self);
75
+ return &self.sides[@intFromEnum(player)];
66
76
  }
67
77
 
68
78
  /// Returns the `Side` of the opponent for the given `player`
69
- pub inline fn foe(self: *Self, player: Player) *Side {
70
- return &self.sides[@enumToInt(player.foe())];
79
+ pub inline fn foe(self: anytype, player: Player) PointerType(@TypeOf(self), Side) {
80
+ assert(@typeInfo(@TypeOf(self)).Pointer.child == Self);
81
+ return &self.sides[@intFromEnum(player.foe())];
71
82
  }
72
83
 
73
84
  /// Returns an identifier for the active Pokémon of `player`.
74
- pub inline fn active(self: *Self, player: Player) ID {
75
- return player.ident(@intCast(u3, self.side(player).order[0]));
85
+ pub inline fn active(self: *const Self, player: Player) ID {
86
+ return player.ident(@intCast(self.side(player).order[0]));
87
+ }
88
+
89
+ /// Returns the `MoveDetails` for the given `player`.
90
+ pub inline fn lastMove(
91
+ self: anytype,
92
+ player: Player,
93
+ ) PointerType(@TypeOf(self), MoveDetails) {
94
+ assert(@typeInfo(@TypeOf(self)).Pointer.child == Self);
95
+ return &self.last_moves[@intFromEnum(player)];
76
96
  }
77
97
 
78
98
  /// Returns the result of applying Player 1's choice `c1` and Player 2's choice `c2` to the
79
- /// battle, optionally writing protocol logs to `log` if `options.trace` is enabled.
80
- pub fn update(self: *Self, c1: Choice, c2: Choice, log: anytype) !Result {
81
- const m = mechanics.Mechanics(*Self, @TypeOf(log)){ .battle = self, .log = log };
82
- return m.update(c1, c2);
99
+ /// battle, optionally writing protocol message logs to `options.log` if `pkmn.options.log`
100
+ /// is enabled.
101
+ pub fn update(self: *Self, c1: Choice, c2: Choice, options: anytype) !Result {
102
+ return mechanics.update(self, c1, c2, options);
83
103
  }
84
104
 
85
105
  /// Fills in at most `out.len` possible choices for the `player` given the previous `result`
@@ -91,7 +111,7 @@ pub fn Battle(comptime RNG: anytype) type {
91
111
  /// interacts with Disable, in which case there are no possible choices for the player to
92
112
  /// make (i.e. on the cartridge a soft-lock occurs).
93
113
  ///
94
- /// This function will always return a number of choices > 0 if options.showdown is true.
114
+ /// This function will always return choices > 0 if `pkmn.options.showdown` is true.
95
115
  pub fn choices(self: *Self, player: Player, request: Choice.Type, out: []Choice) u8 {
96
116
  return mechanics.choices(self, player, request, out);
97
117
  }
@@ -121,7 +141,8 @@ pub const Side = extern struct {
121
141
  }
122
142
 
123
143
  /// Returns the stored `Pokemon` corresponding to the one-indexed party `slot`.
124
- pub inline fn get(self: *Side, slot: u8) *Pokemon {
144
+ pub inline fn get(self: anytype, slot: u8) PointerType(@TypeOf(self), Pokemon) {
145
+ assert(@typeInfo(@TypeOf(self)).Pointer.child == Side);
125
146
  assert(slot > 0 and slot <= 6);
126
147
  const id = self.order[slot - 1];
127
148
  assert(id > 0 and id <= 6);
@@ -129,7 +150,8 @@ pub const Side = extern struct {
129
150
  }
130
151
 
131
152
  /// Returns the stored `Pokemon` corresponding to the `active` Pokémon (slot 1).
132
- pub inline fn stored(self: *Side) *Pokemon {
153
+ pub inline fn stored(self: anytype) PointerType(@TypeOf(self), Pokemon) {
154
+ assert(@typeInfo(@TypeOf(self)).Pointer.child == Side);
133
155
  return self.get(1);
134
156
  }
135
157
  };
@@ -154,7 +176,8 @@ pub const ActivePokemon = extern struct {
154
176
  }
155
177
 
156
178
  /// Returns the active Pokémon's current move slot located at the one-indexed `mslot`.
157
- pub inline fn move(self: *ActivePokemon, mslot: u8) *MoveSlot {
179
+ pub inline fn move(self: anytype, mslot: u8) PointerType(@TypeOf(self), MoveSlot) {
180
+ assert(@typeInfo(@TypeOf(self)).Pointer.child == ActivePokemon);
158
181
  assert(mslot > 0 and mslot <= 4);
159
182
  assert(self.moves[mslot - 1].id != .None);
160
183
  return &self.moves[mslot - 1];
@@ -183,7 +206,8 @@ pub const Pokemon = extern struct {
183
206
  }
184
207
 
185
208
  /// Returns the Pokémon's original move slot located at the one-indexed `mslot`.
186
- pub inline fn move(self: *Pokemon, mslot: u8) *MoveSlot {
209
+ pub inline fn move(self: anytype, mslot: u8) PointerType(@TypeOf(self), MoveSlot) {
210
+ assert(@typeInfo(@TypeOf(self)).Pointer.child == Pokemon);
187
211
  assert(mslot > 0 and mslot <= 4);
188
212
  assert(self.moves[mslot - 1].id != .None);
189
213
  return &self.moves[mslot - 1];
@@ -202,17 +226,15 @@ pub const MoveSlot = extern struct {
202
226
  }
203
227
  };
204
228
 
205
- const uindex = if (showdown) u16 else u4;
206
-
207
- /// Representation of the move slot indexes for each side in a Generation I Pokémon battle.
208
- pub const MoveIndexes = packed struct {
209
- /// Player 1's move index.
210
- p1: uindex = 0,
211
- /// Player 1's move index.
212
- p2: uindex = 0,
229
+ /// Details required to detect desyncs based on the move last selected/executed by players.
230
+ pub const MoveDetails = packed struct {
231
+ /// The move slot index of the last move selected in the battle menu.
232
+ index: if (showdown) u8 else u4 = 0,
233
+ /// Whether the last move executed was counterable.
234
+ counterable: if (showdown) u8 else u4 = 0,
213
235
 
214
236
  comptime {
215
- assert(@sizeOf(MoveIndexes) == if (showdown) 4 else 1);
237
+ assert(@sizeOf(MoveDetails) == if (showdown) 2 else 1);
216
238
  }
217
239
  };
218
240
 
@@ -242,13 +264,13 @@ pub const Status = enum(u8) {
242
264
  /// Whether or not the status `num` is the same as `status`.
243
265
  pub inline fn is(num: u8, status: Status) bool {
244
266
  if (status == .SLP) return Status.duration(num) > 0;
245
- return ((num >> @intCast(u3, @enumToInt(status))) & 1) != 0;
267
+ return ((num >> @intCast(@intFromEnum(status))) & 1) != 0;
246
268
  }
247
269
 
248
270
  /// Initializes a non-sleep `status`. Use `slp` or `slf` to initialize a sleep status.
249
271
  pub inline fn init(status: Status) u8 {
250
272
  assert(status != .SLP and status != .EXT);
251
- return @as(u8, 1) << @intCast(u3, @enumToInt(status));
273
+ return @as(u8, 1) << @intCast(@intFromEnum(status));
252
274
  }
253
275
 
254
276
  /// Initializes a non self-inflicted sleep status with duration `dur`.
@@ -265,7 +287,7 @@ pub const Status = enum(u8) {
265
287
 
266
288
  /// Returns the duration of a sleep status.
267
289
  pub inline fn duration(num: u8) u3 {
268
- return @intCast(u3, num & SLP);
290
+ return @intCast(num & SLP);
269
291
  }
270
292
 
271
293
  /// Returns whether `num` reflects any status.
@@ -412,10 +434,11 @@ pub fn Stats(comptime T: type) type {
412
434
  /// Computes the value of `stat` given a `base`, `dv`, stat `exp` and `level`.
413
435
  pub fn calc(comptime stat: []const u8, base: T, dv: u4, exp: u16, level: u8) T {
414
436
  assert(level > 0 and level <= 100);
415
- const evs = @min(255, @floatToInt(u16, @ceil(@sqrt(@intToFloat(f32, exp)))));
437
+ const float: f32 = @floatFromInt(exp);
438
+ const evs: u16 = @min(255, @as(u16, @intFromFloat(@ceil(@sqrt(float)))));
416
439
  const core: u32 = (2 *% (@as(u32, base) +% dv)) +% (evs / 4);
417
440
  const factor: u32 = if (std.mem.eql(u8, stat, "hp")) level + 10 else 5;
418
- return @intCast(T, core *% @as(u32, level) / 100 +% factor);
441
+ return @intCast(core *% @as(u32, level) / 100 +% factor);
419
442
  }
420
443
  };
421
444
  }
@@ -464,7 +487,7 @@ test Boosts {
464
487
  pub const Move = moves.Move;
465
488
 
466
489
  test Move {
467
- try expectEqual(2, @enumToInt(Move.KarateChop));
490
+ try expectEqual(2, @intFromEnum(Move.KarateChop));
468
491
  const move = Move.get(.Fissure);
469
492
  try expectEqual(Move.Effect.OHKO, move.effect);
470
493
  try expectEqual(@as(u8, 30), move.accuracy);
@@ -525,7 +548,7 @@ test Move {
525
548
  pub const Species = species.Species;
526
549
 
527
550
  test Species {
528
- try expectEqual(2, @enumToInt(Species.Ivysaur));
551
+ try expectEqual(2, @intFromEnum(Species.Ivysaur));
529
552
  try expectEqual(@as(u8, 100), Species.get(.Mew).stats.def);
530
553
  }
531
554
 
@@ -542,9 +565,11 @@ pub const Effectiveness = enum(u8) {
542
565
  Super = 20,
543
566
 
544
567
  /// Used to determine the effectiness of a neutral damage hit.
545
- pub const neutral: u16 = @enumToInt(Effectiveness.Neutral) * @enumToInt(Effectiveness.Neutral);
568
+ pub const neutral: u16 =
569
+ @intFromEnum(Effectiveness.Neutral) * @intFromEnum(Effectiveness.Neutral);
546
570
  /// Used to detect whether there is a mismatch in type effectivness (relevant for precedence).
547
- pub const mismatch: u16 = @enumToInt(Effectiveness.Resisted) + @enumToInt(Effectiveness.Super);
571
+ pub const mismatch: u16 =
572
+ @intFromEnum(Effectiveness.Resisted) + @intFromEnum(Effectiveness.Super);
548
573
 
549
574
  comptime {
550
575
  assert(@sizeOf(Effectiveness) == 1);
@@ -552,8 +577,8 @@ pub const Effectiveness = enum(u8) {
552
577
  };
553
578
 
554
579
  test Types {
555
- try expectEqual(14, @enumToInt(Type.Dragon));
556
- try expectEqual(20, @enumToInt(Effectiveness.Super));
580
+ try expectEqual(14, @intFromEnum(Type.Dragon));
581
+ try expectEqual(20, @intFromEnum(Effectiveness.Super));
557
582
 
558
583
  try expect(!Type.Ghost.special());
559
584
  try expect(Type.Dragon.special());