@pkmn/engine 0.1.0-dev.63cef311 → 0.1.0-dev.6be9a775
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.
- package/LICENSE +1 -3
- package/README.md +139 -73
- package/build/pkg/addon.d.ts +7 -0
- package/build/pkg/addon.js +111 -0
- package/build/pkg/addon.js.map +1 -0
- package/build/pkg/data/ids.json +12 -12
- package/build/pkg/data/layout.json +27 -5
- package/build/pkg/data/protocol.json +69 -12
- package/build/pkg/data.d.ts +5 -6
- package/build/pkg/data.js +3 -5
- package/build/pkg/data.js.map +1 -1
- package/build/pkg/gen1.d.ts +5 -4
- package/build/pkg/gen1.js +49 -33
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +18 -14
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.d.ts +4 -2
- package/build/pkg/protocol.js +65 -44
- package/build/pkg/protocol.js.map +1 -1
- package/build/test/display.js +405 -0
- package/build/test/display.js.map +1 -0
- package/build/tools/debug.js +134 -0
- package/build/tools/debug.js.map +1 -0
- package/build.zig +299 -154
- package/package.json +89 -46
- package/src/bin/install-pkmn-engine +83 -41
- package/src/bin/pkmn-debug +62 -0
- package/src/lib/common/array.zig +77 -0
- package/src/lib/common/battle.zig +17 -0
- package/src/lib/common/data.zig +19 -31
- package/src/lib/common/debug.zig +23 -13
- package/src/lib/common/js.zig +120 -0
- package/src/lib/common/optional.zig +71 -0
- package/src/lib/common/options.zig +14 -3
- package/src/lib/common/protocol.zig +947 -403
- package/src/lib/common/rational.zig +447 -0
- package/src/lib/common/rng.zig +31 -22
- package/src/lib/common/util.zig +61 -0
- package/src/lib/gen1/calc.zig +806 -0
- package/src/lib/gen1/chance.zig +1167 -0
- package/src/lib/gen1/data/moves.zig +202 -190
- package/src/lib/gen1/data/species.zig +5 -5
- package/src/lib/gen1/data/types.zig +10 -10
- package/src/lib/gen1/data.zig +98 -83
- package/src/lib/gen1/helpers.zig +143 -42
- package/src/lib/gen1/mechanics.zig +1341 -864
- package/src/lib/gen2/calc.zig +161 -0
- package/src/lib/gen2/chance.zig +905 -0
- package/src/lib/gen2/data/items.zig +14 -20
- package/src/lib/gen2/data/moves.zig +664 -570
- package/src/lib/gen2/data/species.zig +2 -2
- package/src/lib/gen2/data/types.zig +137 -34
- package/src/lib/gen2/data.zig +129 -90
- package/src/lib/gen2/generated.zig +806 -0
- package/src/lib/gen2/helpers.zig +54 -28
- package/src/lib/gen2/mechanics.zig +3567 -12
- package/src/lib/node.zig +131 -0
- package/src/lib/pkmn.zig +88 -12
- package/src/lib/wasm.zig +13 -0
- package/src/test/display/pkmn.html.tmpl +368 -0
- package/src/lib/binding/node.zig +0 -229
- package/src/lib/binding/wasm.zig +0 -10
- package/src/lib/gen1/README.md +0 -778
- 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
|
|
1095
|
-
pub
|
|
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[@
|
|
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[@
|
|
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
|
|
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
|
|
94
|
-
return @
|
|
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
|
|
99
|
-
return CHART[@
|
|
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
|
|
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(
|
|
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
|
|
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
|
|
128
|
+
pub fn includes(self: Types, t: Type) bool {
|
|
129
129
|
return self.type1 == t or self.type2 == t;
|
|
130
130
|
}
|
|
131
131
|
};
|
package/src/lib/gen1/data.zig
CHANGED
|
@@ -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
|
|
21
|
-
/// The
|
|
22
|
-
/// At least as large as MAX_OPTIONS.
|
|
24
|
+
pub const MAX_CHOICES: usize = 9; // move 1..4, switch 2..6
|
|
25
|
+
/// The maximum number of bytes possibly logged by a single Generation I update.
|
|
23
26
|
pub const MAX_LOGS: usize = 180;
|
|
24
27
|
|
|
25
|
-
/// The
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
/// The optimal size in bytes required to hold all Generation I choice options.
|
|
29
|
+
/// At least as large as MAX_CHOICES.
|
|
30
|
+
pub const CHOICES_SIZE = if (builtin.mode == .ReleaseSmall)
|
|
31
|
+
MAX_CHOICES
|
|
28
32
|
else
|
|
29
|
-
std.math.ceilPowerOfTwo(usize,
|
|
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,18 @@ 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
|
|
49
|
+
const PointerType = util.PointerType;
|
|
50
|
+
const isPointerTo = util.isPointerTo;
|
|
51
|
+
|
|
52
|
+
const showdown = pkmn.options.showdown;
|
|
43
53
|
|
|
44
54
|
/// The pseudo random number generator used by Generation I.
|
|
45
55
|
pub const PRNG = rng.PRNG(1);
|
|
@@ -55,30 +65,42 @@ pub fn Battle(comptime RNG: anytype) type {
|
|
|
55
65
|
turn: u16 = 0,
|
|
56
66
|
/// The last damage dealt by either side.
|
|
57
67
|
last_damage: u16 = 0,
|
|
58
|
-
///
|
|
59
|
-
|
|
68
|
+
/// Details about the last move selected/used by each player required to detect desyncs.
|
|
69
|
+
last_moves: [2]MoveDetails = [_]MoveDetails{.{}} ** 2,
|
|
60
70
|
/// The pseudo random number generator.
|
|
61
71
|
rng: RNG,
|
|
62
72
|
|
|
63
73
|
/// Returns the `Side` for the given `player`.
|
|
64
|
-
pub
|
|
65
|
-
|
|
74
|
+
pub fn side(self: anytype, player: Player) PointerType(@TypeOf(self), Side) {
|
|
75
|
+
assert(isPointerTo(self, Self));
|
|
76
|
+
return &self.sides[@intFromEnum(player)];
|
|
66
77
|
}
|
|
67
78
|
|
|
68
79
|
/// Returns the `Side` of the opponent for the given `player`
|
|
69
|
-
pub
|
|
70
|
-
|
|
80
|
+
pub fn foe(self: anytype, player: Player) PointerType(@TypeOf(self), Side) {
|
|
81
|
+
assert(isPointerTo(self, Self));
|
|
82
|
+
return &self.sides[@intFromEnum(player.foe())];
|
|
71
83
|
}
|
|
72
84
|
|
|
73
85
|
/// Returns an identifier for the active Pokémon of `player`.
|
|
74
|
-
pub
|
|
75
|
-
return player.ident(@intCast(
|
|
86
|
+
pub fn active(self: *const Self, player: Player) ID {
|
|
87
|
+
return player.ident(@intCast(self.side(player).order[0]));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// Returns the `MoveDetails` for the given `player`.
|
|
91
|
+
pub fn lastMove(
|
|
92
|
+
self: anytype,
|
|
93
|
+
player: Player,
|
|
94
|
+
) PointerType(@TypeOf(self), MoveDetails) {
|
|
95
|
+
assert(isPointerTo(self, Self));
|
|
96
|
+
return &self.last_moves[@intFromEnum(player)];
|
|
76
97
|
}
|
|
77
98
|
|
|
78
99
|
/// 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.
|
|
80
|
-
|
|
81
|
-
|
|
100
|
+
/// battle, optionally writing protocol message logs to `options.log` if `pkmn.options.log`
|
|
101
|
+
/// is enabled.
|
|
102
|
+
pub fn update(self: *Self, c1: Choice, c2: Choice, options: anytype) !Result {
|
|
103
|
+
return mechanics.update(self, c1, c2, options);
|
|
82
104
|
}
|
|
83
105
|
|
|
84
106
|
/// Fills in at most `out.len` possible choices for the `player` given the previous `result`
|
|
@@ -90,8 +112,8 @@ pub fn Battle(comptime RNG: anytype) type {
|
|
|
90
112
|
/// interacts with Disable, in which case there are no possible choices for the player to
|
|
91
113
|
/// make (i.e. on the cartridge a soft-lock occurs).
|
|
92
114
|
///
|
|
93
|
-
/// This function will always return
|
|
94
|
-
pub fn choices(self: *Self, player: Player, request: Choice.Type, out: []Choice) u8 {
|
|
115
|
+
/// This function will always return choices > 0 if `pkmn.options.showdown` is true.
|
|
116
|
+
pub fn choices(self: *const Self, player: Player, request: Choice.Type, out: []Choice) u8 {
|
|
95
117
|
return mechanics.choices(self, player, request, out);
|
|
96
118
|
}
|
|
97
119
|
};
|
|
@@ -120,7 +142,8 @@ pub const Side = extern struct {
|
|
|
120
142
|
}
|
|
121
143
|
|
|
122
144
|
/// Returns the stored `Pokemon` corresponding to the one-indexed party `slot`.
|
|
123
|
-
pub
|
|
145
|
+
pub fn get(self: anytype, slot: u8) PointerType(@TypeOf(self), Pokemon) {
|
|
146
|
+
assert(isPointerTo(self, Side));
|
|
124
147
|
assert(slot > 0 and slot <= 6);
|
|
125
148
|
const id = self.order[slot - 1];
|
|
126
149
|
assert(id > 0 and id <= 6);
|
|
@@ -128,7 +151,8 @@ pub const Side = extern struct {
|
|
|
128
151
|
}
|
|
129
152
|
|
|
130
153
|
/// Returns the stored `Pokemon` corresponding to the `active` Pokémon (slot 1).
|
|
131
|
-
pub
|
|
154
|
+
pub fn stored(self: anytype) PointerType(@TypeOf(self), Pokemon) {
|
|
155
|
+
assert(isPointerTo(self, Side));
|
|
132
156
|
return self.get(1);
|
|
133
157
|
}
|
|
134
158
|
};
|
|
@@ -153,7 +177,8 @@ pub const ActivePokemon = extern struct {
|
|
|
153
177
|
}
|
|
154
178
|
|
|
155
179
|
/// Returns the active Pokémon's current move slot located at the one-indexed `mslot`.
|
|
156
|
-
pub
|
|
180
|
+
pub fn move(self: anytype, mslot: u8) PointerType(@TypeOf(self), MoveSlot) {
|
|
181
|
+
assert(isPointerTo(self, ActivePokemon));
|
|
157
182
|
assert(mslot > 0 and mslot <= 4);
|
|
158
183
|
assert(self.moves[mslot - 1].id != .None);
|
|
159
184
|
return &self.moves[mslot - 1];
|
|
@@ -182,14 +207,15 @@ pub const Pokemon = extern struct {
|
|
|
182
207
|
}
|
|
183
208
|
|
|
184
209
|
/// Returns the Pokémon's original move slot located at the one-indexed `mslot`.
|
|
185
|
-
pub
|
|
210
|
+
pub fn move(self: anytype, mslot: u8) PointerType(@TypeOf(self), MoveSlot) {
|
|
211
|
+
assert(isPointerTo(self, Pokemon));
|
|
186
212
|
assert(mslot > 0 and mslot <= 4);
|
|
187
213
|
assert(self.moves[mslot - 1].id != .None);
|
|
188
214
|
return &self.moves[mslot - 1];
|
|
189
215
|
}
|
|
190
216
|
};
|
|
191
217
|
|
|
192
|
-
/// Representation of a Generation I
|
|
218
|
+
/// Representation of a Generation I Pokémon's move slot in a battle.
|
|
193
219
|
pub const MoveSlot = extern struct {
|
|
194
220
|
/// The identifier for the move.
|
|
195
221
|
id: Move = .None,
|
|
@@ -197,22 +223,16 @@ pub const MoveSlot = extern struct {
|
|
|
197
223
|
pp: u8 = 0,
|
|
198
224
|
|
|
199
225
|
comptime {
|
|
200
|
-
assert(@sizeOf(MoveSlot) ==
|
|
226
|
+
assert(@sizeOf(MoveSlot) == 2);
|
|
201
227
|
}
|
|
202
228
|
};
|
|
203
229
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
///
|
|
207
|
-
|
|
208
|
-
///
|
|
209
|
-
|
|
210
|
-
/// Player 1's move index.
|
|
211
|
-
p2: uindex = 0,
|
|
212
|
-
|
|
213
|
-
comptime {
|
|
214
|
-
assert(@sizeOf(MoveIndexes) == if (showdown) 4 else 1);
|
|
215
|
-
}
|
|
230
|
+
/// Details required to detect desyncs based on the move last selected/executed by players.
|
|
231
|
+
pub const MoveDetails = packed struct(if (showdown) u16 else u8) {
|
|
232
|
+
/// The move slot index of the last move selected in the battle menu.
|
|
233
|
+
index: if (showdown) u8 else u4 = 0,
|
|
234
|
+
/// Whether the last move executed was counterable.
|
|
235
|
+
counterable: if (showdown) u8 else u4 = 0,
|
|
216
236
|
};
|
|
217
237
|
|
|
218
238
|
/// Bitfield representation of a Generation I & II Pokémon's major status condition.
|
|
@@ -236,39 +256,39 @@ pub const Status = enum(u8) {
|
|
|
236
256
|
|
|
237
257
|
pub const TOX = 0b10001000;
|
|
238
258
|
|
|
239
|
-
const
|
|
259
|
+
const SLEEP = 0b111;
|
|
240
260
|
|
|
241
261
|
/// Whether or not the status `num` is the same as `status`.
|
|
242
|
-
pub
|
|
262
|
+
pub fn is(num: u8, status: Status) bool {
|
|
243
263
|
if (status == .SLP) return Status.duration(num) > 0;
|
|
244
|
-
return ((num >> @intCast(
|
|
264
|
+
return ((num >> @intCast(@intFromEnum(status))) & 1) != 0;
|
|
245
265
|
}
|
|
246
266
|
|
|
247
267
|
/// Initializes a non-sleep `status`. Use `slp` or `slf` to initialize a sleep status.
|
|
248
|
-
pub
|
|
268
|
+
pub fn init(status: Status) u8 {
|
|
249
269
|
assert(status != .SLP and status != .EXT);
|
|
250
|
-
return @as(u8, 1) << @intCast(
|
|
270
|
+
return @as(u8, 1) << @intCast(@intFromEnum(status));
|
|
251
271
|
}
|
|
252
272
|
|
|
253
273
|
/// Initializes a non self-inflicted sleep status with duration `dur`.
|
|
254
|
-
pub
|
|
274
|
+
pub fn slp(dur: u3) u8 {
|
|
255
275
|
assert(dur > 0);
|
|
256
276
|
return @as(u8, dur);
|
|
257
277
|
}
|
|
258
278
|
|
|
259
279
|
/// Initializes a self-inflicted sleep status with duration `dur`.
|
|
260
|
-
pub
|
|
280
|
+
pub fn slf(dur: u3) u8 {
|
|
261
281
|
assert(dur > 0);
|
|
262
282
|
return 0x80 | slp(dur);
|
|
263
283
|
}
|
|
264
284
|
|
|
265
285
|
/// Returns the duration of a sleep status.
|
|
266
|
-
pub
|
|
267
|
-
return @intCast(
|
|
286
|
+
pub fn duration(num: u8) u3 {
|
|
287
|
+
return @intCast(num & SLEEP);
|
|
268
288
|
}
|
|
269
289
|
|
|
270
290
|
/// Returns whether `num` reflects any status.
|
|
271
|
-
pub
|
|
291
|
+
pub fn any(num: u8) bool {
|
|
272
292
|
return num > 0;
|
|
273
293
|
}
|
|
274
294
|
|
|
@@ -306,7 +326,7 @@ test Status {
|
|
|
306
326
|
}
|
|
307
327
|
|
|
308
328
|
/// Bitfield representation of volatile statuses and associated data in Generation I.
|
|
309
|
-
pub const Volatiles = packed struct {
|
|
329
|
+
pub const Volatiles = packed struct(u64) {
|
|
310
330
|
/// Whether the "Bide" volatile status is present.
|
|
311
331
|
Bide: bool = false,
|
|
312
332
|
/// Whether the "Thrashing" volatile status is present.
|
|
@@ -359,19 +379,15 @@ pub const Volatiles = packed struct {
|
|
|
359
379
|
/// The identity of whom the active Pokémon is transformed into.
|
|
360
380
|
transform: u4 = 0,
|
|
361
381
|
/// The remaining turns the move is disabled.
|
|
362
|
-
|
|
382
|
+
disable_duration: u4 = 0,
|
|
363
383
|
/// The move slot (1-4) that is disabled.
|
|
364
|
-
|
|
384
|
+
disable_move: u3 = 0,
|
|
365
385
|
/// The number of turns toxic damage has been accumulating.
|
|
366
386
|
toxic: u5 = 0,
|
|
367
|
-
|
|
368
|
-
comptime {
|
|
369
|
-
assert(@sizeOf(Volatiles) == 8);
|
|
370
|
-
}
|
|
371
387
|
};
|
|
372
388
|
|
|
373
389
|
test Volatiles {
|
|
374
|
-
var volatiles =
|
|
390
|
+
var volatiles: Volatiles = .{};
|
|
375
391
|
volatiles.Confusion = true;
|
|
376
392
|
volatiles.confusion = 2;
|
|
377
393
|
volatiles.Thrashing = true;
|
|
@@ -380,8 +396,8 @@ test Volatiles {
|
|
|
380
396
|
volatiles.Substitute = true;
|
|
381
397
|
volatiles.substitute = 42;
|
|
382
398
|
volatiles.toxic = 4;
|
|
383
|
-
volatiles.
|
|
384
|
-
volatiles.
|
|
399
|
+
volatiles.disable_move = 2;
|
|
400
|
+
volatiles.disable_duration = 4;
|
|
385
401
|
|
|
386
402
|
try expect(volatiles.Confusion);
|
|
387
403
|
try expect(volatiles.Thrashing);
|
|
@@ -392,14 +408,14 @@ test Volatiles {
|
|
|
392
408
|
|
|
393
409
|
try expectEqual(@as(u16, 235), volatiles.state);
|
|
394
410
|
try expectEqual(@as(u8, 42), volatiles.substitute);
|
|
395
|
-
try expectEqual(@as(u4, 2), volatiles.
|
|
396
|
-
try expectEqual(@as(u4, 4), volatiles.
|
|
397
|
-
try expectEqual(@as(
|
|
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);
|
|
398
414
|
try expectEqual(@as(u5, 4), volatiles.toxic);
|
|
399
|
-
try expectEqual(@as(
|
|
415
|
+
try expectEqual(@as(u3, 3), volatiles.attacks);
|
|
400
416
|
}
|
|
401
417
|
|
|
402
|
-
///
|
|
418
|
+
/// Representation of a Pokémon's stats in Generation I.
|
|
403
419
|
pub fn Stats(comptime T: type) type {
|
|
404
420
|
return extern struct {
|
|
405
421
|
hp: T = 0,
|
|
@@ -411,10 +427,11 @@ pub fn Stats(comptime T: type) type {
|
|
|
411
427
|
/// Computes the value of `stat` given a `base`, `dv`, stat `exp` and `level`.
|
|
412
428
|
pub fn calc(comptime stat: []const u8, base: T, dv: u4, exp: u16, level: u8) T {
|
|
413
429
|
assert(level > 0 and level <= 100);
|
|
414
|
-
const
|
|
430
|
+
const float: f32 = @floatFromInt(exp);
|
|
431
|
+
const evs: u16 = @min(255, @as(u16, @intFromFloat(@ceil(@sqrt(float)))));
|
|
415
432
|
const core: u32 = (2 *% (@as(u32, base) +% dv)) +% (evs / 4);
|
|
416
433
|
const factor: u32 = if (std.mem.eql(u8, stat, "hp")) level + 10 else 5;
|
|
417
|
-
return @intCast(
|
|
434
|
+
return @intCast(core *% @as(u32, level) / 100 +% factor);
|
|
418
435
|
}
|
|
419
436
|
};
|
|
420
437
|
}
|
|
@@ -432,7 +449,7 @@ test Stats {
|
|
|
432
449
|
}
|
|
433
450
|
|
|
434
451
|
/// Representation of a Pokémon's boosts in Generation I.
|
|
435
|
-
pub const Boosts = packed struct {
|
|
452
|
+
pub const Boosts = packed struct(u32) {
|
|
436
453
|
/// A Pokémon's Attack boosts.
|
|
437
454
|
atk: i4 = 0,
|
|
438
455
|
/// A Pokémon's Defense boosts.
|
|
@@ -447,14 +464,10 @@ pub const Boosts = packed struct {
|
|
|
447
464
|
evasion: i4 = 0,
|
|
448
465
|
|
|
449
466
|
_: u8 = 0,
|
|
450
|
-
|
|
451
|
-
comptime {
|
|
452
|
-
assert(@sizeOf(Boosts) == 4);
|
|
453
|
-
}
|
|
454
467
|
};
|
|
455
468
|
|
|
456
469
|
test Boosts {
|
|
457
|
-
const boosts =
|
|
470
|
+
const boosts: Boosts = .{ .spc = -6 };
|
|
458
471
|
try expectEqual(0, boosts.atk);
|
|
459
472
|
try expectEqual(-6, boosts.spc);
|
|
460
473
|
}
|
|
@@ -463,10 +476,10 @@ test Boosts {
|
|
|
463
476
|
pub const Move = moves.Move;
|
|
464
477
|
|
|
465
478
|
test Move {
|
|
466
|
-
try expectEqual(2, @
|
|
479
|
+
try expectEqual(2, @intFromEnum(Move.KarateChop));
|
|
467
480
|
const move = Move.get(.Fissure);
|
|
468
481
|
try expectEqual(Move.Effect.OHKO, move.effect);
|
|
469
|
-
try expectEqual(
|
|
482
|
+
try expectEqual(rng.Gen12.percent(30), move.accuracy);
|
|
470
483
|
try expectEqual(Type.Ground, move.type);
|
|
471
484
|
|
|
472
485
|
try expect(!Move.Effect.onBegin(.None));
|
|
@@ -524,7 +537,7 @@ test Move {
|
|
|
524
537
|
pub const Species = species.Species;
|
|
525
538
|
|
|
526
539
|
test Species {
|
|
527
|
-
try expectEqual(2, @
|
|
540
|
+
try expectEqual(2, @intFromEnum(Species.Ivysaur));
|
|
528
541
|
try expectEqual(@as(u8, 100), Species.get(.Mew).stats.def);
|
|
529
542
|
}
|
|
530
543
|
|
|
@@ -541,9 +554,11 @@ pub const Effectiveness = enum(u8) {
|
|
|
541
554
|
Super = 20,
|
|
542
555
|
|
|
543
556
|
/// Used to determine the effectiness of a neutral damage hit.
|
|
544
|
-
pub const neutral: u16 =
|
|
557
|
+
pub const neutral: u16 =
|
|
558
|
+
@intFromEnum(Effectiveness.Neutral) * @intFromEnum(Effectiveness.Neutral);
|
|
545
559
|
/// Used to detect whether there is a mismatch in type effectivness (relevant for precedence).
|
|
546
|
-
pub const mismatch: u16 =
|
|
560
|
+
pub const mismatch: u16 =
|
|
561
|
+
@intFromEnum(Effectiveness.Resisted) + @intFromEnum(Effectiveness.Super);
|
|
547
562
|
|
|
548
563
|
comptime {
|
|
549
564
|
assert(@sizeOf(Effectiveness) == 1);
|
|
@@ -551,8 +566,8 @@ pub const Effectiveness = enum(u8) {
|
|
|
551
566
|
};
|
|
552
567
|
|
|
553
568
|
test Types {
|
|
554
|
-
try expectEqual(14, @
|
|
555
|
-
try expectEqual(20, @
|
|
569
|
+
try expectEqual(14, @intFromEnum(Type.Dragon));
|
|
570
|
+
try expectEqual(20, @intFromEnum(Effectiveness.Super));
|
|
556
571
|
|
|
557
572
|
try expect(!Type.Ghost.special());
|
|
558
573
|
try expect(Type.Dragon.special());
|
|
@@ -602,16 +617,16 @@ pub const DVs = struct {
|
|
|
602
617
|
};
|
|
603
618
|
|
|
604
619
|
test DVs {
|
|
605
|
-
var dvs =
|
|
620
|
+
var dvs: DVs = .{ .spc = 15, .spe = 15 };
|
|
606
621
|
try expectEqual(@as(u4, 15), dvs.hp());
|
|
607
|
-
dvs =
|
|
622
|
+
dvs = .{
|
|
608
623
|
.atk = 5,
|
|
609
624
|
.def = 15,
|
|
610
625
|
.spe = 13,
|
|
611
626
|
.spc = 13,
|
|
612
627
|
};
|
|
613
628
|
try expectEqual(@as(u4, 15), dvs.hp());
|
|
614
|
-
dvs =
|
|
629
|
+
dvs = .{
|
|
615
630
|
.def = 3,
|
|
616
631
|
.spe = 10,
|
|
617
632
|
.spc = 11,
|