@pkmn/engine 0.1.0-dev.e5b5a998 → 0.1.0-dev.f3aaee39
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/README.md +53 -42
- package/build/pkg/addon.js +3 -2
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data/layout.json +24 -2
- package/build/pkg/gen1.js +13 -16
- package/build/pkg/gen1.js.map +1 -1
- package/build.zig +79 -54
- package/package.json +23 -14
- package/src/bin/install-pkmn-engine +10 -5
- package/src/lib/binding/node.zig +45 -140
- package/src/lib/binding/wasm.zig +4 -2
- package/src/lib/common/battle.zig +17 -0
- package/src/lib/common/data.zig +11 -11
- package/src/lib/common/debug.zig +20 -11
- package/src/lib/common/js.zig +118 -0
- package/src/lib/common/optional.zig +60 -0
- package/src/lib/common/options.zig +9 -0
- package/src/lib/common/protocol.zig +133 -76
- package/src/lib/common/rational.zig +353 -0
- package/src/lib/common/rng.zig +10 -10
- package/src/lib/common/util.zig +12 -0
- package/src/lib/gen1/README.md +254 -176
- package/src/lib/gen1/calc.zig +427 -0
- package/src/lib/gen1/chance.zig +1044 -0
- package/src/lib/gen1/data/types.zig +1 -1
- package/src/lib/gen1/data.zig +51 -29
- package/src/lib/gen1/helpers.zig +588 -17
- package/src/lib/gen1/mechanics.zig +751 -473
- package/src/lib/gen2/README.md +12 -13
- package/src/lib/gen2/data/items.zig +1 -1
- package/src/lib/gen2/data.zig +27 -16
- package/src/lib/gen2/helpers.zig +8 -10
- package/src/lib/gen2/mechanics.zig +5 -4
- package/src/lib/pkmn.zig +61 -2
|
@@ -102,7 +102,7 @@ pub const Type = enum(u4) {
|
|
|
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(
|
|
105
|
+
if (matchup.type1 == t1 and matchup.type2 == t2) return @intCast(i);
|
|
106
106
|
}
|
|
107
107
|
unreachable;
|
|
108
108
|
}
|
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;
|
|
@@ -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
|
|
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:
|
|
73
|
+
pub inline fn side(self: anytype, player: Player) PointerType(@TypeOf(self), Side) {
|
|
74
|
+
assert(@typeInfo(@TypeOf(self)).Pointer.child == Self);
|
|
65
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:
|
|
79
|
+
pub inline fn foe(self: anytype, player: Player) PointerType(@TypeOf(self), Side) {
|
|
80
|
+
assert(@typeInfo(@TypeOf(self)).Pointer.child == Self);
|
|
70
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(
|
|
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 message logs to `log` if `options.log`
|
|
80
|
-
|
|
81
|
-
|
|
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,7 +111,7 @@ 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
|
|
114
|
+
/// This function will always return choices > 0 if `pkmn.options.showdown` is true.
|
|
94
115
|
pub fn choices(self: *Self, player: Player, request: Choice.Type, out: []Choice) u8 {
|
|
95
116
|
return mechanics.choices(self, player, request, out);
|
|
96
117
|
}
|
|
@@ -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:
|
|
144
|
+
pub inline 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:
|
|
153
|
+
pub inline 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:
|
|
179
|
+
pub inline 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,7 +206,8 @@ 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:
|
|
209
|
+
pub inline 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];
|
|
@@ -203,17 +228,13 @@ pub const MoveSlot = extern struct {
|
|
|
203
228
|
|
|
204
229
|
/// Details required to detect desyncs based on the move last selected/executed by players.
|
|
205
230
|
pub const MoveDetails = packed struct {
|
|
206
|
-
/// The move slot index of the last move selected in the battle menu
|
|
207
|
-
|
|
208
|
-
/// Whether the last move executed
|
|
209
|
-
|
|
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,
|
|
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,
|
|
214
235
|
|
|
215
236
|
comptime {
|
|
216
|
-
assert(@sizeOf(MoveDetails) == if (showdown)
|
|
237
|
+
assert(@sizeOf(MoveDetails) == if (showdown) 2 else 1);
|
|
217
238
|
}
|
|
218
239
|
};
|
|
219
240
|
|
|
@@ -243,13 +264,13 @@ pub const Status = enum(u8) {
|
|
|
243
264
|
/// Whether or not the status `num` is the same as `status`.
|
|
244
265
|
pub inline fn is(num: u8, status: Status) bool {
|
|
245
266
|
if (status == .SLP) return Status.duration(num) > 0;
|
|
246
|
-
return ((num >> @intCast(
|
|
267
|
+
return ((num >> @intCast(@intFromEnum(status))) & 1) != 0;
|
|
247
268
|
}
|
|
248
269
|
|
|
249
270
|
/// Initializes a non-sleep `status`. Use `slp` or `slf` to initialize a sleep status.
|
|
250
271
|
pub inline fn init(status: Status) u8 {
|
|
251
272
|
assert(status != .SLP and status != .EXT);
|
|
252
|
-
return @as(u8, 1) << @intCast(
|
|
273
|
+
return @as(u8, 1) << @intCast(@intFromEnum(status));
|
|
253
274
|
}
|
|
254
275
|
|
|
255
276
|
/// Initializes a non self-inflicted sleep status with duration `dur`.
|
|
@@ -266,7 +287,7 @@ pub const Status = enum(u8) {
|
|
|
266
287
|
|
|
267
288
|
/// Returns the duration of a sleep status.
|
|
268
289
|
pub inline fn duration(num: u8) u3 {
|
|
269
|
-
return @intCast(
|
|
290
|
+
return @intCast(num & SLP);
|
|
270
291
|
}
|
|
271
292
|
|
|
272
293
|
/// Returns whether `num` reflects any status.
|
|
@@ -413,10 +434,11 @@ pub fn Stats(comptime T: type) type {
|
|
|
413
434
|
/// Computes the value of `stat` given a `base`, `dv`, stat `exp` and `level`.
|
|
414
435
|
pub fn calc(comptime stat: []const u8, base: T, dv: u4, exp: u16, level: u8) T {
|
|
415
436
|
assert(level > 0 and level <= 100);
|
|
416
|
-
const
|
|
437
|
+
const float: f32 = @floatFromInt(exp);
|
|
438
|
+
const evs: u16 = @min(255, @as(u16, @intFromFloat(@ceil(@sqrt(float)))));
|
|
417
439
|
const core: u32 = (2 *% (@as(u32, base) +% dv)) +% (evs / 4);
|
|
418
440
|
const factor: u32 = if (std.mem.eql(u8, stat, "hp")) level + 10 else 5;
|
|
419
|
-
return @intCast(
|
|
441
|
+
return @intCast(core *% @as(u32, level) / 100 +% factor);
|
|
420
442
|
}
|
|
421
443
|
};
|
|
422
444
|
}
|