@pkmn/engine 0.1.0-dev.6c5fdce2 → 0.1.0-dev.6d566713
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 +41 -14
- package/build/pkg/addon.js +6 -7
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data.d.ts +4 -5
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +1 -1
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.js +4 -4
- package/build/pkg/protocol.js.map +1 -1
- package/build/test/display.js +8 -3
- package/build/test/display.js.map +1 -1
- package/build/tools/debug.js +2 -3
- package/build/tools/debug.js.map +1 -1
- package/build.zig +203 -89
- package/package.json +32 -22
- package/src/bin/install-pkmn-engine +30 -20
- package/src/lib/common/array.zig +77 -0
- package/src/lib/common/data.zig +4 -4
- package/src/lib/common/debug.zig +2 -2
- package/src/lib/common/js.zig +4 -2
- package/src/lib/common/optional.zig +14 -10
- package/src/lib/common/options.zig +2 -0
- package/src/lib/common/protocol.zig +57 -57
- package/src/lib/common/rational.zig +114 -24
- package/src/lib/common/rng.zig +7 -7
- package/src/lib/common/util.zig +50 -1
- package/src/lib/gen1/calc.zig +506 -128
- package/src/lib/gen1/chance.zig +374 -267
- package/src/lib/gen1/data/species.zig +1 -1
- package/src/lib/gen1/data.zig +17 -16
- package/src/lib/gen1/helpers.zig +95 -538
- package/src/lib/gen1/mechanics.zig +268 -174
- package/src/lib/gen2/calc.zig +34 -16
- package/src/lib/gen2/chance.zig +84 -496
- package/src/lib/gen2/data.zig +7 -6
- package/src/lib/gen2/generated.zig +0 -2
- package/src/lib/gen2/helpers.zig +41 -13
- package/src/lib/gen2/mechanics.zig +158 -108
- package/src/lib/{binding/node.zig → node.zig} +3 -6
- package/src/lib/pkmn.zig +32 -15
- package/src/lib/{binding/wasm.zig → wasm.zig} +2 -1
- package/src/test/display/pkmn.html.tmpl +5 -5
- package/src/lib/gen1/README.md +0 -894
- package/src/lib/gen2/README.md +0 -329
package/src/lib/gen2/data.zig
CHANGED
|
@@ -45,6 +45,7 @@ const Player = data.Player;
|
|
|
45
45
|
const Result = data.Result;
|
|
46
46
|
|
|
47
47
|
const PointerType = util.PointerType;
|
|
48
|
+
const isPointerTo = util.isPointerTo;
|
|
48
49
|
|
|
49
50
|
const showdown = pkmn.options.showdown;
|
|
50
51
|
|
|
@@ -63,12 +64,12 @@ pub fn Battle(comptime RNG: anytype) type {
|
|
|
63
64
|
_: [padding]u8 = .{0} ** padding,
|
|
64
65
|
|
|
65
66
|
pub fn side(self: anytype, player: Player) PointerType(@TypeOf(self), Side) {
|
|
66
|
-
assert(
|
|
67
|
+
assert(isPointerTo(self, Self));
|
|
67
68
|
return &self.sides[@intFromEnum(player)];
|
|
68
69
|
}
|
|
69
70
|
|
|
70
71
|
pub fn foe(self: anytype, player: Player) PointerType(@TypeOf(self), Side) {
|
|
71
|
-
assert(
|
|
72
|
+
assert(isPointerTo(self, Self));
|
|
72
73
|
return &self.sides[@intFromEnum(player.foe())];
|
|
73
74
|
}
|
|
74
75
|
|
|
@@ -134,7 +135,7 @@ pub const Side = extern struct {
|
|
|
134
135
|
}
|
|
135
136
|
|
|
136
137
|
pub fn get(self: anytype, slot: u8) PointerType(@TypeOf(self), Pokemon) {
|
|
137
|
-
assert(
|
|
138
|
+
assert(isPointerTo(self, Side));
|
|
138
139
|
assert(slot > 0 and slot <= 6);
|
|
139
140
|
const id = self.pokemon[slot - 1].position;
|
|
140
141
|
assert(id > 0 and id <= 6);
|
|
@@ -142,7 +143,7 @@ pub const Side = extern struct {
|
|
|
142
143
|
}
|
|
143
144
|
|
|
144
145
|
pub fn stored(self: anytype) PointerType(@TypeOf(self), Pokemon) {
|
|
145
|
-
assert(
|
|
146
|
+
assert(isPointerTo(self, Side));
|
|
146
147
|
return self.get(1);
|
|
147
148
|
}
|
|
148
149
|
|
|
@@ -176,7 +177,7 @@ pub const ActivePokemon = extern struct {
|
|
|
176
177
|
}
|
|
177
178
|
|
|
178
179
|
pub fn move(self: anytype, mslot: u8) PointerType(@TypeOf(self), MoveSlot) {
|
|
179
|
-
assert(
|
|
180
|
+
assert(isPointerTo(self, Pokemon));
|
|
180
181
|
assert(mslot > 0 and mslot <= 4);
|
|
181
182
|
assert(self.moves[mslot - 1].id != .None);
|
|
182
183
|
return &self.moves[mslot - 1];
|
|
@@ -201,7 +202,7 @@ pub const Pokemon = extern struct {
|
|
|
201
202
|
}
|
|
202
203
|
|
|
203
204
|
pub fn move(self: anytype, mslot: u8) PointerType(@TypeOf(self), MoveSlot) {
|
|
204
|
-
assert(
|
|
205
|
+
assert(isPointerTo(self, Pokemon));
|
|
205
206
|
assert(mslot > 0 and mslot <= 4);
|
|
206
207
|
assert(self.moves[mslot - 1].id != .None);
|
|
207
208
|
return &self.moves[mslot - 1];
|
package/src/lib/gen2/helpers.zig
CHANGED
|
@@ -2,7 +2,6 @@ const std = @import("std");
|
|
|
2
2
|
|
|
3
3
|
const common = @import("../common/data.zig");
|
|
4
4
|
const options = @import("../common/options.zig");
|
|
5
|
-
const protocol = @import("../common/protocol.zig");
|
|
6
5
|
const rng = @import("../common/rng.zig");
|
|
7
6
|
|
|
8
7
|
const gen1 = @import("../gen1/data.zig");
|
|
@@ -11,8 +10,6 @@ const data = @import("data.zig");
|
|
|
11
10
|
|
|
12
11
|
const assert = std.debug.assert;
|
|
13
12
|
|
|
14
|
-
const expectEqual = std.testing.expectEqual;
|
|
15
|
-
|
|
16
13
|
const Choice = common.Choice;
|
|
17
14
|
|
|
18
15
|
const showdown = options.showdown;
|
|
@@ -25,7 +22,8 @@ const Move = data.Move;
|
|
|
25
22
|
const MoveSlot = data.MoveSlot;
|
|
26
23
|
const Species = data.Species;
|
|
27
24
|
const Stats = data.Stats;
|
|
28
|
-
|
|
25
|
+
|
|
26
|
+
const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
|
|
29
27
|
|
|
30
28
|
pub const Options = struct {
|
|
31
29
|
cleric: bool = showdown,
|
|
@@ -65,17 +63,47 @@ pub const Battle = struct {
|
|
|
65
63
|
};
|
|
66
64
|
|
|
67
65
|
pub fn prng(rand: *PSRNG) data.PRNG {
|
|
68
|
-
// FIXME: what is max range in gen2?
|
|
69
66
|
return .{
|
|
70
67
|
.src = .{
|
|
71
68
|
.seed = if (showdown)
|
|
72
69
|
rand.newSeed()
|
|
73
|
-
else
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
70
|
+
else seed: {
|
|
71
|
+
// GLITCH: The initial RNG seed in Gen II link battles is heavily constrained
|
|
72
|
+
// https://www.smogon.com/forums/threads/rng-in-rby-link-battles.3746779
|
|
73
|
+
var seed = [_]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
74
|
+
|
|
75
|
+
// Bytes in seed can only range from 0-252, not 0-255
|
|
76
|
+
const max: u8 = 253;
|
|
77
|
+
// Instructions take multiples of 4 cycles to execute
|
|
78
|
+
var timer: u16 = 4 * rand.range(u16, 0, 16384);
|
|
79
|
+
var add: u8 = rand.range(u8, 0, 256);
|
|
80
|
+
var sub: u8 = rand.range(u8, 0, 256);
|
|
81
|
+
var carry: u8 = 0;
|
|
82
|
+
var div: u8 = 0;
|
|
83
|
+
|
|
84
|
+
var i: u8 = 0;
|
|
85
|
+
while (i < seed.len) {
|
|
86
|
+
div = @intCast(timer >> 8);
|
|
87
|
+
// This is effectively @addWithOverflow but with three operands
|
|
88
|
+
const result = @as(u16, add) + @as(u16, div) + @as(u16, carry);
|
|
89
|
+
add = @intCast(result & 255);
|
|
90
|
+
carry = @intFromBool(result > 255);
|
|
91
|
+
timer +%= 44;
|
|
92
|
+
div = @intCast(timer >> 8);
|
|
93
|
+
sub -%= div +% carry;
|
|
94
|
+
|
|
95
|
+
if (sub < max) {
|
|
96
|
+
seed[i] = sub;
|
|
97
|
+
i += 1;
|
|
98
|
+
timer +%= 152;
|
|
99
|
+
carry = 1;
|
|
100
|
+
} else {
|
|
101
|
+
timer +%= 132;
|
|
102
|
+
carry = 0;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
break :seed seed;
|
|
106
|
+
},
|
|
79
107
|
},
|
|
80
108
|
};
|
|
81
109
|
}
|
|
@@ -121,7 +149,7 @@ pub const Pokemon = struct {
|
|
|
121
149
|
var pokemon = data.Pokemon{};
|
|
122
150
|
pokemon.species = p.species;
|
|
123
151
|
const species = Species.get(p.species);
|
|
124
|
-
inline for (@typeInfo(@TypeOf(pokemon.stats))
|
|
152
|
+
inline for (@field(@typeInfo(@TypeOf(pokemon.stats)), @tagName(Struct)).fields) |field| {
|
|
125
153
|
const hp = comptime std.mem.eql(u8, field.name, "hp");
|
|
126
154
|
const spc =
|
|
127
155
|
comptime std.mem.eql(u8, field.name, "spa") or std.mem.eql(u8, field.name, "spd");
|
|
@@ -162,7 +190,7 @@ pub const Pokemon = struct {
|
|
|
162
190
|
|
|
163
191
|
var stats: Stats(u16) = .{};
|
|
164
192
|
const dvs = gen1.DVs.random(rand);
|
|
165
|
-
inline for (@typeInfo(@TypeOf(stats))
|
|
193
|
+
inline for (@field(@typeInfo(@TypeOf(stats)), @tagName(Struct)).fields) |field| {
|
|
166
194
|
const hp = std.mem.eql(u8, field.name, "hp");
|
|
167
195
|
const spc = std.mem.eql(u8, field.name, "spa") or std.mem.eql(u8, field.name, "spd");
|
|
168
196
|
@field(stats, field.name) = Stats(u16).calc(
|