@pkmn/engine 0.1.0-dev.d716e940 → 0.1.0-dev.da4a0515

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 (57) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +90 -56
  3. package/build/pkg/addon.js +9 -9
  4. package/build/pkg/addon.js.map +1 -1
  5. package/build/pkg/data/ids.json +12 -12
  6. package/build/pkg/data/layout.json +26 -4
  7. package/build/pkg/data/protocol.json +69 -12
  8. package/build/pkg/data.d.ts +4 -4
  9. package/build/pkg/data.js.map +1 -1
  10. package/build/pkg/gen1.js +15 -18
  11. package/build/pkg/gen1.js.map +1 -1
  12. package/build/pkg/index.d.ts +2 -1
  13. package/build/pkg/index.js.map +1 -1
  14. package/build/pkg/protocol.js +61 -42
  15. package/build/pkg/protocol.js.map +1 -1
  16. package/build/test/display.js +2 -2
  17. package/build/test/display.js.map +1 -1
  18. package/build/tools/debug.js +5 -4
  19. package/build/tools/debug.js.map +1 -1
  20. package/build.zig +154 -116
  21. package/package.json +53 -24
  22. package/src/bin/install-pkmn-engine +35 -22
  23. package/src/lib/common/battle.zig +17 -0
  24. package/src/lib/common/data.zig +19 -31
  25. package/src/lib/common/debug.zig +20 -11
  26. package/src/lib/common/js.zig +118 -0
  27. package/src/lib/common/optional.zig +67 -0
  28. package/src/lib/common/options.zig +9 -0
  29. package/src/lib/common/protocol.zig +906 -367
  30. package/src/lib/common/rational.zig +357 -0
  31. package/src/lib/common/rng.zig +35 -26
  32. package/src/lib/common/util.zig +12 -0
  33. package/src/lib/gen1/calc.zig +426 -0
  34. package/src/lib/gen1/chance.zig +1061 -0
  35. package/src/lib/gen1/data/moves.zig +202 -190
  36. package/src/lib/gen1/data/species.zig +5 -5
  37. package/src/lib/gen1/data/types.zig +10 -10
  38. package/src/lib/gen1/data.zig +99 -87
  39. package/src/lib/gen1/helpers.zig +599 -23
  40. package/src/lib/gen1/mechanics.zig +978 -710
  41. package/src/lib/gen2/calc.zig +143 -0
  42. package/src/lib/gen2/chance.zig +1315 -0
  43. package/src/lib/gen2/data/items.zig +14 -20
  44. package/src/lib/gen2/data/moves.zig +664 -570
  45. package/src/lib/gen2/data/species.zig +2 -2
  46. package/src/lib/gen2/data/types.zig +137 -34
  47. package/src/lib/gen2/data.zig +130 -92
  48. package/src/lib/gen2/generated.zig +806 -0
  49. package/src/lib/gen2/helpers.zig +16 -22
  50. package/src/lib/gen2/mechanics.zig +3591 -12
  51. package/src/lib/node.zig +131 -0
  52. package/src/lib/pkmn.zig +84 -2
  53. package/src/lib/wasm.zig +13 -0
  54. package/src/lib/binding/node.zig +0 -229
  55. package/src/lib/binding/wasm.zig +0 -10
  56. package/src/lib/gen1/README.md +0 -812
  57. package/src/lib/gen2/README.md +0 -309
@@ -1,4 +1,4 @@
1
- //! Code generated by `tools/generate` - manual edits will be overwritten
1
+ //! Code generated by `tools/generate` - manual edits will be overwritten.
2
2
 
3
3
  const std = @import("std");
4
4
 
@@ -1091,15 +1091,15 @@ 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.
1095
- pub inline fn chance(id: Species) u8 {
1094
+ /// The Pokémon's critical hit ratio out of 256.
1095
+ pub 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
  };
@@ -1,4 +1,4 @@
1
- //! Code generated by `tools/generate` - manual edits will be overwritten
1
+ //! Code generated by `tools/generate` - manual edits will be overwritten.
2
2
 
3
3
  const std = @import("std");
4
4
 
@@ -30,7 +30,7 @@ pub const Type = enum(u4) {
30
30
  Ice,
31
31
  Dragon,
32
32
 
33
- const CHART = [15][15]Effectiveness{
33
+ const CHART: [15][15]Effectiveness = .{
34
34
  [_]Effectiveness{ N, N, N, N, N, R, N, I, N, N, N, N, N, N, N }, // Normal
35
35
  [_]Effectiveness{ S, N, R, R, N, S, R, I, N, N, N, N, R, S, N }, // Fighting
36
36
  [_]Effectiveness{ N, S, N, N, N, R, S, N, N, N, S, R, N, N, N }, // Flying
@@ -90,19 +90,19 @@ pub const Type = enum(u4) {
90
90
  pub const size = 15;
91
91
 
92
92
  /// Whether or not this type is considered to be special as opposed to physical.
93
- pub inline fn special(self: Type) bool {
94
- return @enumToInt(self) >= @enumToInt(Type.Fire);
93
+ pub fn special(self: Type) bool {
94
+ return @intFromEnum(self) >= @intFromEnum(Type.Fire);
95
95
  }
96
96
 
97
97
  /// The `Effectiveness` of type `t2` vs. type `t1`.
98
- pub inline fn effectiveness(t1: Type, t2: Type) Effectiveness {
99
- return CHART[@enumToInt(t1)][@enumToInt(t2)];
98
+ pub fn effectiveness(t1: Type, t2: Type) Effectiveness {
99
+ return CHART[@intFromEnum(t1)][@intFromEnum(t2)];
100
100
  }
101
101
 
102
- /// The precedence order of type `t2` vs. type `t1`.
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
  }
@@ -120,12 +120,12 @@ pub const Types = packed struct {
120
120
  }
121
121
 
122
122
  /// Whether this typing is immune to type `t`.
123
- pub inline fn immune(self: Types, t: Type) bool {
123
+ pub fn immune(self: Types, t: Type) bool {
124
124
  return t.effectiveness(self.type1) == I or t.effectiveness(self.type2) == I;
125
125
  }
126
126
 
127
127
  /// Whether this typing includes type `t`.
128
- pub inline fn includes(self: Types, t: Type) bool {
128
+ pub fn includes(self: Types, t: Type) bool {
129
129
  return self.type1 == t or self.type2 == t;
130
130
  }
131
131
  };
@@ -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;
@@ -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);
@@ -56,29 +65,41 @@ pub fn Battle(comptime RNG: anytype) type {
56
65
  /// The last damage dealt by either side.
57
66
  last_damage: u16 = 0,
58
67
  /// Details about the last move selected/used by each player required to detect desyncs.
59
- last_moves: MoveDetails = .{},
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 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 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 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 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 message logs to `log` if `options.log` is enabled.
80
- pub fn update(self: *Self, c1: Choice, c2: Choice, log: anytype) !Result {
81
- return mechanics.update(self, c1, c2, log);
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);
82
103
  }
83
104
 
84
105
  /// Fills in at most `out.len` possible choices for the `player` given the previous `result`
@@ -90,8 +111,8 @@ pub fn Battle(comptime RNG: anytype) type {
90
111
  /// interacts with Disable, in which case there are no possible choices for the player to
91
112
  /// make (i.e. on the cartridge a soft-lock occurs).
92
113
  ///
93
- /// This function will always return a number of choices > 0 if options.showdown is true.
94
- pub fn choices(self: *Self, player: Player, request: Choice.Type, out: []Choice) u8 {
114
+ /// This function will always return choices > 0 if `pkmn.options.showdown` is true.
115
+ pub fn choices(self: *const Self, player: Player, request: Choice.Type, out: []Choice) u8 {
95
116
  return mechanics.choices(self, player, request, out);
96
117
  }
97
118
  };
@@ -120,7 +141,8 @@ pub const Side = extern struct {
120
141
  }
121
142
 
122
143
  /// Returns the stored `Pokemon` corresponding to the one-indexed party `slot`.
123
- pub inline fn get(self: *Side, slot: u8) *Pokemon {
144
+ pub fn get(self: anytype, slot: u8) PointerType(@TypeOf(self), Pokemon) {
145
+ assert(@typeInfo(@TypeOf(self)).Pointer.child == Side);
124
146
  assert(slot > 0 and slot <= 6);
125
147
  const id = self.order[slot - 1];
126
148
  assert(id > 0 and id <= 6);
@@ -128,7 +150,8 @@ pub const Side = extern struct {
128
150
  }
129
151
 
130
152
  /// Returns the stored `Pokemon` corresponding to the `active` Pokémon (slot 1).
131
- pub inline fn stored(self: *Side) *Pokemon {
153
+ pub fn stored(self: anytype) PointerType(@TypeOf(self), Pokemon) {
154
+ assert(@typeInfo(@TypeOf(self)).Pointer.child == Side);
132
155
  return self.get(1);
133
156
  }
134
157
  };
@@ -153,7 +176,8 @@ pub const ActivePokemon = extern struct {
153
176
  }
154
177
 
155
178
  /// Returns the active Pokémon's current move slot located at the one-indexed `mslot`.
156
- pub inline fn move(self: *ActivePokemon, mslot: u8) *MoveSlot {
179
+ pub fn move(self: anytype, mslot: u8) PointerType(@TypeOf(self), MoveSlot) {
180
+ assert(@typeInfo(@TypeOf(self)).Pointer.child == ActivePokemon);
157
181
  assert(mslot > 0 and mslot <= 4);
158
182
  assert(self.moves[mslot - 1].id != .None);
159
183
  return &self.moves[mslot - 1];
@@ -182,14 +206,15 @@ pub const Pokemon = extern struct {
182
206
  }
183
207
 
184
208
  /// Returns the Pokémon's original move slot located at the one-indexed `mslot`.
185
- pub inline fn move(self: *Pokemon, mslot: u8) *MoveSlot {
209
+ pub fn move(self: anytype, mslot: u8) PointerType(@TypeOf(self), MoveSlot) {
210
+ assert(@typeInfo(@TypeOf(self)).Pointer.child == Pokemon);
186
211
  assert(mslot > 0 and mslot <= 4);
187
212
  assert(self.moves[mslot - 1].id != .None);
188
213
  return &self.moves[mslot - 1];
189
214
  }
190
215
  };
191
216
 
192
- /// Representation of a Generation I & II Pokémon's move slot in a battle.
217
+ /// Representation of a Generation I Pokémon's move slot in a battle.
193
218
  pub const MoveSlot = extern struct {
194
219
  /// The identifier for the move.
195
220
  id: Move = .None,
@@ -197,24 +222,16 @@ pub const MoveSlot = extern struct {
197
222
  pp: u8 = 0,
198
223
 
199
224
  comptime {
200
- assert(@sizeOf(MoveSlot) == @sizeOf(u16));
225
+ assert(@sizeOf(MoveSlot) == 2);
201
226
  }
202
227
  };
203
228
 
204
229
  /// Details required to detect desyncs based on the move last selected/executed by players.
205
- pub const MoveDetails = packed struct {
206
- /// The move slot index of the last move selected in the battle menu by Player 1
207
- p1_index: if (showdown) u8 else u3 = 0,
208
- /// Whether the last move executed by Player 1 was counterable.
209
- p1_counterable: if (showdown) u8 else u1 = 0,
210
- /// The move slot index of the last move selected in the battle menu by Player 2
211
- p2_index: if (showdown) u8 else u3 = 0,
212
- /// Whether the last move executed by Player 2 was counterable.
213
- p2_counterable: if (showdown) u8 else u1 = 0,
214
-
215
- comptime {
216
- assert(@sizeOf(MoveDetails) == if (showdown) 4 else 1);
217
- }
230
+ pub const MoveDetails = packed struct(if (showdown) u16 else u8) {
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,
218
235
  };
219
236
 
220
237
  /// Bitfield representation of a Generation I & II Pokémon's major status condition.
@@ -241,36 +258,36 @@ pub const Status = enum(u8) {
241
258
  const SLP = 0b111;
242
259
 
243
260
  /// Whether or not the status `num` is the same as `status`.
244
- pub inline fn is(num: u8, status: Status) bool {
261
+ pub fn is(num: u8, status: Status) bool {
245
262
  if (status == .SLP) return Status.duration(num) > 0;
246
- return ((num >> @intCast(u3, @enumToInt(status))) & 1) != 0;
263
+ return ((num >> @intCast(@intFromEnum(status))) & 1) != 0;
247
264
  }
248
265
 
249
266
  /// Initializes a non-sleep `status`. Use `slp` or `slf` to initialize a sleep status.
250
- pub inline fn init(status: Status) u8 {
267
+ pub fn init(status: Status) u8 {
251
268
  assert(status != .SLP and status != .EXT);
252
- return @as(u8, 1) << @intCast(u3, @enumToInt(status));
269
+ return @as(u8, 1) << @intCast(@intFromEnum(status));
253
270
  }
254
271
 
255
272
  /// Initializes a non self-inflicted sleep status with duration `dur`.
256
- pub inline fn slp(dur: u3) u8 {
273
+ pub fn slp(dur: u3) u8 {
257
274
  assert(dur > 0);
258
275
  return @as(u8, dur);
259
276
  }
260
277
 
261
278
  /// Initializes a self-inflicted sleep status with duration `dur`.
262
- pub inline fn slf(dur: u3) u8 {
279
+ pub fn slf(dur: u3) u8 {
263
280
  assert(dur > 0);
264
281
  return 0x80 | slp(dur);
265
282
  }
266
283
 
267
284
  /// Returns the duration of a sleep status.
268
- pub inline fn duration(num: u8) u3 {
269
- return @intCast(u3, num & SLP);
285
+ pub fn duration(num: u8) u3 {
286
+ return @intCast(num & SLP);
270
287
  }
271
288
 
272
289
  /// Returns whether `num` reflects any status.
273
- pub inline fn any(num: u8) bool {
290
+ pub fn any(num: u8) bool {
274
291
  return num > 0;
275
292
  }
276
293
 
@@ -299,16 +316,16 @@ test Status {
299
316
  try expect(Status.is(Status.slp(5), .SLP));
300
317
  try expect(!Status.is(Status.slp(5), .EXT));
301
318
  try expect(!Status.is(Status.slp(7), .PSN));
302
- try expectEqual(@as(u3, 5), Status.duration(Status.slp(5)));
319
+ try expectEqual(5, Status.duration(Status.slp(5)));
303
320
  try expect(Status.is(Status.slf(2), .SLP));
304
321
  try expect(Status.is(Status.slf(2), .EXT));
305
322
  try expect(Status.is(Status.TOX, .PSN));
306
323
  try expect(Status.is(Status.TOX, .EXT));
307
- try expectEqual(@as(u3, 1), Status.duration(Status.slp(1)));
324
+ try expectEqual(1, Status.duration(Status.slp(1)));
308
325
  }
309
326
 
310
327
  /// Bitfield representation of volatile statuses and associated data in Generation I.
311
- pub const Volatiles = packed struct {
328
+ pub const Volatiles = packed struct(u64) {
312
329
  /// Whether the "Bide" volatile status is present.
313
330
  Bide: bool = false,
314
331
  /// Whether the "Thrashing" volatile status is present.
@@ -361,19 +378,15 @@ pub const Volatiles = packed struct {
361
378
  /// The identity of whom the active Pokémon is transformed into.
362
379
  transform: u4 = 0,
363
380
  /// The remaining turns the move is disabled.
364
- disabled_duration: u4 = 0,
381
+ disable_duration: u4 = 0,
365
382
  /// The move slot (1-4) that is disabled.
366
- disabled_move: u3 = 0,
383
+ disable_move: u3 = 0,
367
384
  /// The number of turns toxic damage has been accumulating.
368
385
  toxic: u5 = 0,
369
-
370
- comptime {
371
- assert(@sizeOf(Volatiles) == 8);
372
- }
373
386
  };
374
387
 
375
388
  test Volatiles {
376
- var volatiles = Volatiles{};
389
+ var volatiles: Volatiles = .{};
377
390
  volatiles.Confusion = true;
378
391
  volatiles.confusion = 2;
379
392
  volatiles.Thrashing = true;
@@ -382,8 +395,8 @@ test Volatiles {
382
395
  volatiles.Substitute = true;
383
396
  volatiles.substitute = 42;
384
397
  volatiles.toxic = 4;
385
- volatiles.disabled_move = 2;
386
- volatiles.disabled_duration = 4;
398
+ volatiles.disable_move = 2;
399
+ volatiles.disable_duration = 4;
387
400
 
388
401
  try expect(volatiles.Confusion);
389
402
  try expect(volatiles.Thrashing);
@@ -392,13 +405,13 @@ test Volatiles {
392
405
  try expect(!volatiles.Transform);
393
406
  try expect(!volatiles.MultiHit);
394
407
 
395
- try expectEqual(@as(u16, 235), volatiles.state);
396
- try expectEqual(@as(u8, 42), volatiles.substitute);
397
- try expectEqual(@as(u4, 2), volatiles.disabled_move);
398
- try expectEqual(@as(u4, 4), volatiles.disabled_duration);
399
- try expectEqual(@as(u4, 2), volatiles.confusion);
400
- try expectEqual(@as(u5, 4), volatiles.toxic);
401
- try expectEqual(@as(u4, 3), volatiles.attacks);
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);
402
415
  }
403
416
 
404
417
  /// Representaiton of a Pokémon's stats in Generation I.
@@ -413,10 +426,11 @@ pub fn Stats(comptime T: type) type {
413
426
  /// Computes the value of `stat` given a `base`, `dv`, stat `exp` and `level`.
414
427
  pub fn calc(comptime stat: []const u8, base: T, dv: u4, exp: u16, level: u8) T {
415
428
  assert(level > 0 and level <= 100);
416
- const evs = @min(255, @floatToInt(u16, @ceil(@sqrt(@intToFloat(f32, exp)))));
429
+ const float: f32 = @floatFromInt(exp);
430
+ const evs: u16 = @min(255, @as(u16, @intFromFloat(@ceil(@sqrt(float)))));
417
431
  const core: u32 = (2 *% (@as(u32, base) +% dv)) +% (evs / 4);
418
432
  const factor: u32 = if (std.mem.eql(u8, stat, "hp")) level + 10 else 5;
419
- return @intCast(T, core *% @as(u32, level) / 100 +% factor);
433
+ return @intCast(core *% @as(u32, level) / 100 +% factor);
420
434
  }
421
435
  };
422
436
  }
@@ -428,13 +442,13 @@ test Stats {
428
442
  try expectEqual(0, stats.def);
429
443
 
430
444
  var base = Species.get(.Gyarados).stats;
431
- try expectEqual(@as(u16, 127), Stats(u16).calc("hp", base.hp, 0, 5120, 38));
445
+ try expectEqual(127, Stats(u16).calc("hp", base.hp, 0, 5120, 38));
432
446
  base = Species.get(.Pidgeot).stats;
433
- try expectEqual(@as(u16, 279), Stats(u16).calc("spe", base.spe, 15, 63001, 100));
447
+ try expectEqual(279, Stats(u16).calc("spe", base.spe, 15, 63001, 100));
434
448
  }
435
449
 
436
450
  /// Representation of a Pokémon's boosts in Generation I.
437
- pub const Boosts = packed struct {
451
+ pub const Boosts = packed struct(u32) {
438
452
  /// A Pokémon's Attack boosts.
439
453
  atk: i4 = 0,
440
454
  /// A Pokémon's Defense boosts.
@@ -449,14 +463,10 @@ pub const Boosts = packed struct {
449
463
  evasion: i4 = 0,
450
464
 
451
465
  _: u8 = 0,
452
-
453
- comptime {
454
- assert(@sizeOf(Boosts) == 4);
455
- }
456
466
  };
457
467
 
458
468
  test Boosts {
459
- const boosts = Boosts{ .spc = -6 };
469
+ const boosts: Boosts = .{ .spc = -6 };
460
470
  try expectEqual(0, boosts.atk);
461
471
  try expectEqual(-6, boosts.spc);
462
472
  }
@@ -465,10 +475,10 @@ test Boosts {
465
475
  pub const Move = moves.Move;
466
476
 
467
477
  test Move {
468
- try expectEqual(2, @enumToInt(Move.KarateChop));
478
+ try expectEqual(2, @intFromEnum(Move.KarateChop));
469
479
  const move = Move.get(.Fissure);
470
480
  try expectEqual(Move.Effect.OHKO, move.effect);
471
- try expectEqual(@as(u8, 30), move.accuracy);
481
+ try expectEqual(rng.Gen12.percent(30), move.accuracy);
472
482
  try expectEqual(Type.Ground, move.type);
473
483
 
474
484
  try expect(!Move.Effect.onBegin(.None));
@@ -526,8 +536,8 @@ test Move {
526
536
  pub const Species = species.Species;
527
537
 
528
538
  test Species {
529
- try expectEqual(2, @enumToInt(Species.Ivysaur));
530
- try expectEqual(@as(u8, 100), Species.get(.Mew).stats.def);
539
+ try expectEqual(2, @intFromEnum(Species.Ivysaur));
540
+ try expectEqual(100, Species.get(.Mew).stats.def);
531
541
  }
532
542
 
533
543
  /// Representation of a Generation I type in Pokémon.
@@ -543,9 +553,11 @@ pub const Effectiveness = enum(u8) {
543
553
  Super = 20,
544
554
 
545
555
  /// Used to determine the effectiness of a neutral damage hit.
546
- pub const neutral: u16 = @enumToInt(Effectiveness.Neutral) * @enumToInt(Effectiveness.Neutral);
556
+ pub const neutral: u16 =
557
+ @intFromEnum(Effectiveness.Neutral) * @intFromEnum(Effectiveness.Neutral);
547
558
  /// Used to detect whether there is a mismatch in type effectivness (relevant for precedence).
548
- pub const mismatch: u16 = @enumToInt(Effectiveness.Resisted) + @enumToInt(Effectiveness.Super);
559
+ pub const mismatch: u16 =
560
+ @intFromEnum(Effectiveness.Resisted) + @intFromEnum(Effectiveness.Super);
549
561
 
550
562
  comptime {
551
563
  assert(@sizeOf(Effectiveness) == 1);
@@ -553,8 +565,8 @@ pub const Effectiveness = enum(u8) {
553
565
  };
554
566
 
555
567
  test Types {
556
- try expectEqual(14, @enumToInt(Type.Dragon));
557
- try expectEqual(20, @enumToInt(Effectiveness.Super));
568
+ try expectEqual(14, @intFromEnum(Type.Dragon));
569
+ try expectEqual(20, @intFromEnum(Effectiveness.Super));
558
570
 
559
571
  try expect(!Type.Ghost.special());
560
572
  try expect(Type.Dragon.special());
@@ -564,8 +576,8 @@ test Types {
564
576
  try expectEqual(Effectiveness.Resisted, Type.effectiveness(.Fire, .Water));
565
577
  try expectEqual(Effectiveness.Neutral, Type.effectiveness(.Normal, .Grass));
566
578
 
567
- try expectEqual(@as(u8, 3), Type.precedence(.Water, .Water));
568
- try expectEqual(@as(u8, 26), Type.precedence(.Bug, .Ghost));
579
+ try expectEqual(3, Type.precedence(.Water, .Water));
580
+ try expectEqual(26, Type.precedence(.Bug, .Ghost));
569
581
  try expect(Type.precedence(.Poison, .Bug) > Type.precedence(.Poison, .Poison));
570
582
 
571
583
  const t: Types = .{ .type1 = .Rock, .type2 = .Ground };
@@ -604,19 +616,19 @@ pub const DVs = struct {
604
616
  };
605
617
 
606
618
  test DVs {
607
- var dvs = DVs{ .spc = 15, .spe = 15 };
608
- try expectEqual(@as(u4, 15), dvs.hp());
609
- dvs = DVs{
619
+ var dvs: DVs = .{ .spc = 15, .spe = 15 };
620
+ try expectEqual(15, dvs.hp());
621
+ dvs = .{
610
622
  .atk = 5,
611
623
  .def = 15,
612
624
  .spe = 13,
613
625
  .spc = 13,
614
626
  };
615
- try expectEqual(@as(u4, 15), dvs.hp());
616
- dvs = DVs{
627
+ try expectEqual(15, dvs.hp());
628
+ dvs = .{
617
629
  .def = 3,
618
630
  .spe = 10,
619
631
  .spc = 11,
620
632
  };
621
- try expectEqual(@as(u4, 13), dvs.hp());
633
+ try expectEqual(13, dvs.hp());
622
634
  }