@pkmn/engine 0.1.0-dev.83de29ee → 0.1.0-dev.8b83af6b

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.
@@ -219,18 +219,18 @@ pub const Item = enum(u8) {
219
219
  pub inline fn boost(item: Item) ?Type {
220
220
  assert(item != .None);
221
221
  if (item == .PolkadotBow) return .Normal;
222
- return if (@enumToInt(item) <= 18) @intToEnum(Type, @enumToInt(item) - 1) else null;
222
+ return if (@intFromEnum(item) <= 18) @enumFromInt(@intFromEnum(item) - 1) else null;
223
223
  }
224
224
 
225
225
  /// Whether or not this item is a Berry.
226
226
  pub inline fn berry(item: Item) bool {
227
227
  assert(item != .None);
228
- return @enumToInt(item) > 30 and @enumToInt(item) <= 40;
228
+ return @intFromEnum(item) > 30 and @intFromEnum(item) <= 40;
229
229
  }
230
230
 
231
231
  /// Whether or not this item is Mail.
232
232
  pub inline fn mail(item: Item) bool {
233
233
  assert(item != .None);
234
- return @enumToInt(item) > 60 and @enumToInt(item) <= 70;
234
+ return @intFromEnum(item) > 60 and @intFromEnum(item) <= 70;
235
235
  }
236
236
  };
@@ -2779,6 +2779,6 @@ pub const Move = enum(u8) {
2779
2779
  /// Returns the `Data` corresponding to the move.
2780
2780
  pub inline fn get(id: Move) Data {
2781
2781
  assert(id != .None);
2782
- return DATA[@enumToInt(id) - 1];
2782
+ return DATA[@intFromEnum(id) - 1];
2783
2783
  }
2784
2784
  };
@@ -1793,6 +1793,6 @@ pub const Species = enum(u8) {
1793
1793
  /// Returns the `Data` corresponding to the species.
1794
1794
  pub fn get(id: Species) Data {
1795
1795
  assert(id != .None);
1796
- return DATA[@enumToInt(id) - 1];
1796
+ return DATA[@intFromEnum(id) - 1];
1797
1797
  }
1798
1798
  };
@@ -86,17 +86,17 @@ pub const Type = enum(u8) {
86
86
 
87
87
  /// Whether or not this type is considered to be special as opposed to physical.
88
88
  pub inline fn special(self: Type) bool {
89
- return @enumToInt(self) >= @enumToInt(Type.Fire);
89
+ return @intFromEnum(self) >= @intFromEnum(Type.Fire);
90
90
  }
91
91
 
92
92
  /// The `Effectiveness` of type `t2` vs. type `t1`.
93
93
  pub inline fn effectiveness(t1: Type, t2: Type) Effectiveness {
94
- return CHART[@enumToInt(t1)][@enumToInt(t2)];
94
+ return CHART[@intFromEnum(t1)][@intFromEnum(t2)];
95
95
  }
96
96
 
97
97
  /// The precedence order of type type.
98
98
  pub inline fn precedence(self: Type) u8 {
99
- return PRECEDENCE[@enumToInt(self)];
99
+ return PRECEDENCE[@intFromEnum(self)];
100
100
  }
101
101
  };
102
102
 
@@ -50,15 +50,15 @@ pub fn Battle(comptime RNG: anytype) type {
50
50
  field: Field = .{},
51
51
 
52
52
  pub inline fn side(self: *Self, player: Player) *Side {
53
- return &self.sides[@enumToInt(player)];
53
+ return &self.sides[@intFromEnum(player)];
54
54
  }
55
55
 
56
56
  pub inline fn foe(self: *Self, player: Player) *Side {
57
- return &self.sides[@enumToInt(player.foe())];
57
+ return &self.sides[@intFromEnum(player.foe())];
58
58
  }
59
59
 
60
60
  pub inline fn active(self: *Self, player: Player) ID {
61
- return player.ident(@intCast(u3, self.side(player).pokemon[0].position));
61
+ return player.ident(@intCast(self.side(player).pokemon[0].position));
62
62
  }
63
63
 
64
64
  pub fn update(self: *Self, c1: Choice, c2: Choice, log: anytype) !Result {
@@ -205,11 +205,11 @@ const DVs = packed struct {
205
205
  0xFF => .Unknown,
206
206
  else => if ((@as(u8, dvs.atk) << 4 | dvs.spe) < ratio) Gender.Female else Gender.Male,
207
207
  };
208
- const p = @intCast(u6, 1 + ((5 * @as(u8, (dvs.atk & 0b1000) | ((dvs.def & 0b1000)) >> 1 |
208
+ const p: u6 = @intCast(1 + ((5 * @as(u8, (dvs.atk & 0b1000) | ((dvs.def & 0b1000)) >> 1 |
209
209
  ((dvs.spe & 0b1000)) >> 2 | ((dvs.spc & 0b1000)) >> 3) + (dvs.spc & 0b0011)) >> 1));
210
210
  var t = @as(u8, dvs.def & 0b0011) + (@as(u8, dvs.atk & 0b0011) << 2) + 1;
211
- if (t >= @enumToInt(Type.@"???")) t = t + 1;
212
- return DVs{ .gender = g, .pow = p, .type = @intToEnum(Type, t) };
211
+ if (t >= @intFromEnum(Type.@"???")) t = t + 1;
212
+ return DVs{ .gender = g, .pow = p, .type = @enumFromInt(t) };
213
213
  }
214
214
 
215
215
  comptime {
@@ -327,10 +327,11 @@ pub fn Stats(comptime T: type) type {
327
327
 
328
328
  pub fn calc(comptime stat: []const u8, base: T, dv: u4, exp: u16, level: u8) T {
329
329
  assert(level > 0 and level <= 100);
330
- const evs = @min(255, @floatToInt(u16, @ceil(@sqrt(@intToFloat(f32, exp)))));
330
+ const float: f32 = @floatFromInt(exp);
331
+ const evs = @min(255, @as(u16, @intFromFloat(@ceil(@sqrt(float)))));
331
332
  const core: u32 = (2 *% (@as(u32, base) +% dv)) +% (evs / 4);
332
333
  const factor: u32 = if (std.mem.eql(u8, stat, "hp")) level + 10 else 5;
333
- return @intCast(T, core *% @as(u32, level) / 100 +% factor);
334
+ return @intCast(core *% @as(u32, level) / 100 +% factor);
334
335
  }
335
336
  };
336
337
  }
@@ -390,7 +391,7 @@ test Item {
390
391
  pub const Move = moves.Move;
391
392
 
392
393
  test Move {
393
- try expectEqual(251, @enumToInt(Move.BeatUp));
394
+ try expectEqual(251, @intFromEnum(Move.BeatUp));
394
395
  const move = Move.get(.DynamicPunch);
395
396
  try expectEqual(Move.Effect.ConfusionChance, move.effect);
396
397
  try expectEqual(@as(u8, 50), move.accuracy);
@@ -400,7 +401,7 @@ test Move {
400
401
  pub const Species = species.Species;
401
402
 
402
403
  test Species {
403
- try expectEqual(152, @enumToInt(Species.Chikorita));
404
+ try expectEqual(152, @intFromEnum(Species.Chikorita));
404
405
  try expectEqual(@as(u8, 100), Species.get(.Celebi).stats.spd);
405
406
  }
406
407
 
@@ -409,7 +410,7 @@ pub const Types = types.Types;
409
410
  pub const Effectiveness = gen1.Effectiveness;
410
411
 
411
412
  test Types {
412
- try expectEqual(13, @enumToInt(Type.Electric));
413
+ try expectEqual(13, @intFromEnum(Type.Electric));
413
414
 
414
415
  try expect(!Type.Steel.special());
415
416
  try expect(Type.Dark.special());
@@ -141,7 +141,7 @@ pub const Pokemon = struct {
141
141
  .id = m,
142
142
  .pp_ups = 3,
143
143
  // NB: PP can be at most 61 legally (though can overflow to 63)
144
- .pp = @intCast(u8, @min(Move.pp(m) / 5 * 8, 61)),
144
+ .pp = @intCast(@min(Move.pp(m) / 5 * 8, 61)),
145
145
  };
146
146
  }
147
147
  pokemon.item = p.item;
@@ -158,11 +158,11 @@ pub const Pokemon = struct {
158
158
  }
159
159
 
160
160
  pub fn random(rand: *PSRNG, opt: Options) data.Pokemon {
161
- const s = @intToEnum(Species, rand.range(u8, 1, Species.size + 1));
161
+ const s: Species = @enumFromInt(rand.range(u8, 1, Species.size + 1));
162
162
  const species = Species.get(s);
163
163
  const lvl = if (rand.chance(u8, 1, 20)) rand.range(u8, 1, 99 + 1) else 100;
164
164
  // Pokémon Showdown does not support most items without in-battle held item effects
165
- const max_item = @enumToInt(if (showdown) Item.FlowerMail else Item.TM50);
165
+ const max_item = @intFromEnum(if (showdown) Item.FlowerMail else Item.TM50);
166
166
  const item = if (rand.chance(u8, 1, 10)) 0 else rand.range(u8, 1, max_item + 1);
167
167
 
168
168
  var stats: Stats(u16) = .{};
@@ -184,14 +184,14 @@ pub const Pokemon = struct {
184
184
  for (0..n) |i| {
185
185
  var m: Move = .None;
186
186
  sample: while (true) {
187
- m = @intToEnum(Move, rand.range(u8, 1, 164 + 1));
187
+ m = @enumFromInt(rand.range(u8, 1, 164 + 1));
188
188
  for (0..i) |j| if (ms[j].id == m) continue :sample;
189
189
  break;
190
190
  }
191
- const pp_ups =
191
+ const pp_ups: u8 =
192
192
  if (!opt.cleric and rand.chance(u8, 1, 10)) rand.range(u2, 0, 2 + 1) else 3;
193
193
  // NB: PP can be at most 61 legally (though can overflow to 63)
194
- const max_pp = @intCast(u8, Move.pp(m) + @as(u8, pp_ups) * @min(Move.pp(m) / 5, 7));
194
+ const max_pp: u8 = @intCast(Move.pp(m) + pp_ups * @min(Move.pp(m) / 5, 7));
195
195
  ms[i] = .{
196
196
  .id = m,
197
197
  .pp_ups = pp_ups,