@pkmn/engine 0.1.0-dev.f34af416 → 0.1.0-dev.f3aab252
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/build.zig +0 -2
- package/package.json +2 -1
- package/src/lib/common/array.zig +77 -0
- package/src/lib/gen1/calc.zig +0 -2
- package/src/lib/gen1/data.zig +2 -2
- package/src/lib/gen1/helpers.zig +0 -3
- package/src/lib/gen1/mechanics.zig +0 -3
- package/src/lib/gen2/chance.zig +0 -3
- package/src/lib/gen2/helpers.zig +0 -4
- package/src/lib/gen2/mechanics.zig +0 -6
package/build.zig
CHANGED
|
@@ -20,8 +20,6 @@ const ResolvedTarget =
|
|
|
20
20
|
if (@hasDecl(std.Build, "ResolvedTarget")) std.Build.ResolvedTarget else std.zig.CrossTarget;
|
|
21
21
|
|
|
22
22
|
pub fn module(b: *std.Build, options: Options) *std.Build.Module {
|
|
23
|
-
if (!has_path) @panic("Use build.zig.zon package management");
|
|
24
|
-
|
|
25
23
|
const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
|
|
26
24
|
const build_options = b.addOptions();
|
|
27
25
|
build_options.addOption(?bool, "showdown", options.showdown);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pkmn/engine",
|
|
3
|
-
"version": "0.1.0-dev.
|
|
3
|
+
"version": "0.1.0-dev.f3aab252",
|
|
4
4
|
"description": "A minimal, complete, Pokémon battle simulation engine optimized for performance",
|
|
5
5
|
"repository": "github:pkmn/engine",
|
|
6
6
|
"license": "MIT",
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"src/lib/common/data.zig",
|
|
36
36
|
"src/lib/common/debug.zig",
|
|
37
37
|
"src/lib/common/js.zig",
|
|
38
|
+
"src/lib/common/array.zig",
|
|
38
39
|
"src/lib/common/optional.zig",
|
|
39
40
|
"src/lib/common/options.zig",
|
|
40
41
|
"src/lib/common/protocol.zig",
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
|
|
3
|
+
const expectEqual = std.testing.expectEqual;
|
|
4
|
+
const assert = std.debug.assert;
|
|
5
|
+
|
|
6
|
+
const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
|
|
7
|
+
const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
|
|
8
|
+
|
|
9
|
+
/// Helpers for working with bit-packed arrays of non-powers-of-2 types.
|
|
10
|
+
/// NOTE: ziglang/zig#12547
|
|
11
|
+
pub fn Array(comptime n: comptime_int, comptime U: type) type {
|
|
12
|
+
return struct {
|
|
13
|
+
const size = @bitSizeOf(U);
|
|
14
|
+
const options = .{ .signedness = .unsigned, .bits = size * n };
|
|
15
|
+
pub const T = @Type(if (@hasField(std.builtin.Type, "int"))
|
|
16
|
+
.{ .int = options }
|
|
17
|
+
else
|
|
18
|
+
.{ .Int = options });
|
|
19
|
+
|
|
20
|
+
/// Returns the value stored at index `i` in the array `a`.
|
|
21
|
+
pub fn get(a: T, i: usize) U {
|
|
22
|
+
assert(i < n);
|
|
23
|
+
const shift = i * size;
|
|
24
|
+
const mask: T = (1 << size) - 1;
|
|
25
|
+
const result = (a >> @intCast(shift)) & mask;
|
|
26
|
+
return switch (@typeInfo(U)) {
|
|
27
|
+
Enum => @enumFromInt(result),
|
|
28
|
+
Int => @intCast(result),
|
|
29
|
+
else => unreachable,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/// Sets the value at index `i` to `val` in the array `a`.
|
|
34
|
+
pub fn set(a: T, i: usize, val: U) T {
|
|
35
|
+
assert(i < n);
|
|
36
|
+
const v: T = switch (@typeInfo(U)) {
|
|
37
|
+
Enum => @intFromEnum(val),
|
|
38
|
+
Int => @intCast(val),
|
|
39
|
+
else => unreachable,
|
|
40
|
+
};
|
|
41
|
+
const shift = i * size;
|
|
42
|
+
const mask: T = @as(T, @intCast((1 << size) - 1)) << @intCast(shift);
|
|
43
|
+
const result: T = a & ~mask;
|
|
44
|
+
return result | (v << @intCast(shift));
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
test Array {
|
|
50
|
+
const A = Array(5, u8);
|
|
51
|
+
var a: A.T = 0;
|
|
52
|
+
try expectEqual(u40, @TypeOf(a));
|
|
53
|
+
|
|
54
|
+
a = A.set(a, 4, 241);
|
|
55
|
+
try expectEqual(@as(u8, 241), A.get(a, 4));
|
|
56
|
+
a = A.set(a, 4, 1);
|
|
57
|
+
try expectEqual(@as(u8, 1), A.get(a, 4));
|
|
58
|
+
|
|
59
|
+
for (0..5) |i| a = A.set(a, i, @intCast(i));
|
|
60
|
+
for (0..5) |i| try expectEqual(@as(u8, @intCast(i)), A.get(a, i));
|
|
61
|
+
|
|
62
|
+
const Optional = @import("optional.zig").Optional;
|
|
63
|
+
|
|
64
|
+
const B = Array(4, Optional(bool));
|
|
65
|
+
var b: B.T = 0;
|
|
66
|
+
try expectEqual(u8, @TypeOf(b));
|
|
67
|
+
|
|
68
|
+
b = B.set(b, 2, .true);
|
|
69
|
+
try expectEqual(Optional(bool).None, B.get(b, 1));
|
|
70
|
+
try expectEqual(Optional(bool).true, B.get(b, 2));
|
|
71
|
+
try expectEqual(@as(u8, 0b00100000), b);
|
|
72
|
+
b = B.set(b, 2, .None);
|
|
73
|
+
b = B.set(b, 0, .false);
|
|
74
|
+
try expectEqual(Optional(bool).None, B.get(b, 2));
|
|
75
|
+
try expectEqual(Optional(bool).false, B.get(b, 0));
|
|
76
|
+
try expectEqual(@as(u8, 0b0000001), b);
|
|
77
|
+
}
|
package/src/lib/gen1/calc.zig
CHANGED
|
@@ -577,8 +577,6 @@ fn format(writer: anytype, actions: Actions, p1: u9, p2: u9) !void {
|
|
|
577
577
|
try writer.writeAll(output[0..len]);
|
|
578
578
|
}
|
|
579
579
|
|
|
580
|
-
const NONE = @intFromEnum(Optional(bool).None);
|
|
581
|
-
|
|
582
580
|
/// Helper functions that efficiently return valid ranges for various RNG events based on the
|
|
583
581
|
/// state of an `Action` and other events to be used to construct a "transitions" function.
|
|
584
582
|
pub const Rolls = struct {
|
package/src/lib/gen1/data.zig
CHANGED
|
@@ -256,7 +256,7 @@ pub const Status = enum(u8) {
|
|
|
256
256
|
|
|
257
257
|
pub const TOX = 0b10001000;
|
|
258
258
|
|
|
259
|
-
const
|
|
259
|
+
const SLEEP = 0b111;
|
|
260
260
|
|
|
261
261
|
/// Whether or not the status `num` is the same as `status`.
|
|
262
262
|
pub fn is(num: u8, status: Status) bool {
|
|
@@ -284,7 +284,7 @@ pub const Status = enum(u8) {
|
|
|
284
284
|
|
|
285
285
|
/// Returns the duration of a sleep status.
|
|
286
286
|
pub fn duration(num: u8) u3 {
|
|
287
|
-
return @intCast(num &
|
|
287
|
+
return @intCast(num & SLEEP);
|
|
288
288
|
}
|
|
289
289
|
|
|
290
290
|
/// Returns whether `num` reflects any status.
|
package/src/lib/gen1/helpers.zig
CHANGED
|
@@ -12,10 +12,8 @@ const data = @import("data.zig");
|
|
|
12
12
|
const assert = std.debug.assert;
|
|
13
13
|
|
|
14
14
|
const Choice = common.Choice;
|
|
15
|
-
const Player = common.Player;
|
|
16
15
|
|
|
17
16
|
const showdown = options.showdown;
|
|
18
|
-
const chance = options.chance;
|
|
19
17
|
|
|
20
18
|
const PSRNG = rng.PSRNG;
|
|
21
19
|
|
|
@@ -24,7 +22,6 @@ const Move = data.Move;
|
|
|
24
22
|
const MoveSlot = data.MoveSlot;
|
|
25
23
|
const Species = data.Species;
|
|
26
24
|
const Stats = data.Stats;
|
|
27
|
-
const Status = data.Status;
|
|
28
25
|
|
|
29
26
|
const endian = builtin.cpu.arch.endian();
|
|
30
27
|
|
|
@@ -4,7 +4,6 @@ const pkmn = @import("../pkmn.zig");
|
|
|
4
4
|
|
|
5
5
|
const common = @import("../common/data.zig");
|
|
6
6
|
const DEBUG = @import("../common/debug.zig").print;
|
|
7
|
-
const optional = @import("../common/optional.zig");
|
|
8
7
|
const protocol = @import("../common/protocol.zig");
|
|
9
8
|
const rng = @import("../common/rng.zig");
|
|
10
9
|
|
|
@@ -29,11 +28,9 @@ const Heal = protocol.Heal;
|
|
|
29
28
|
|
|
30
29
|
const Gen12 = rng.Gen12;
|
|
31
30
|
|
|
32
|
-
const ActivePokemon = data.ActivePokemon;
|
|
33
31
|
const Effectiveness = data.Effectiveness;
|
|
34
32
|
const Move = data.Move;
|
|
35
33
|
const MoveSlot = data.MoveSlot;
|
|
36
|
-
const Pokemon = data.Pokemon;
|
|
37
34
|
const Side = data.Side;
|
|
38
35
|
const Species = data.Species;
|
|
39
36
|
const Stats = data.Stats;
|
package/src/lib/gen2/chance.zig
CHANGED
|
@@ -8,7 +8,6 @@ const expectEqual = std.testing.expectEqual;
|
|
|
8
8
|
|
|
9
9
|
const options = @import("../common/options.zig");
|
|
10
10
|
const rational = @import("../common/rational.zig");
|
|
11
|
-
const rng = @import("../common/rng.zig");
|
|
12
11
|
const util = @import("../common/util.zig");
|
|
13
12
|
|
|
14
13
|
const Array = @import("../common/array.zig").Array;
|
|
@@ -20,8 +19,6 @@ const data = @import("data.zig");
|
|
|
20
19
|
const enabled = options.chance;
|
|
21
20
|
const showdown = options.showdown;
|
|
22
21
|
|
|
23
|
-
const Gen12 = rng.Gen12;
|
|
24
|
-
|
|
25
22
|
const PointerType = util.PointerType;
|
|
26
23
|
const isPointerTo = util.isPointerTo;
|
|
27
24
|
|
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,6 @@ const Move = data.Move;
|
|
|
25
22
|
const MoveSlot = data.MoveSlot;
|
|
26
23
|
const Species = data.Species;
|
|
27
24
|
const Stats = data.Stats;
|
|
28
|
-
const Status = data.Status;
|
|
29
25
|
|
|
30
26
|
const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
|
|
31
27
|
|
|
@@ -6,7 +6,6 @@ const common = @import("../common/data.zig");
|
|
|
6
6
|
const DEBUG = @import("../common/debug.zig").print;
|
|
7
7
|
const protocol = @import("../common/protocol.zig");
|
|
8
8
|
const rng = @import("../common/rng.zig");
|
|
9
|
-
const util = @import("../common/util.zig");
|
|
10
9
|
|
|
11
10
|
const data = @import("data.zig");
|
|
12
11
|
const generated = @import("generated.zig");
|
|
@@ -34,17 +33,12 @@ const Start = protocol.Start;
|
|
|
34
33
|
|
|
35
34
|
const Gen12 = rng.Gen12;
|
|
36
35
|
|
|
37
|
-
const PointerType = util.PointerType;
|
|
38
|
-
|
|
39
36
|
const ActivePokemon = data.ActivePokemon;
|
|
40
37
|
const Boosts = data.Boosts;
|
|
41
38
|
const Effectiveness = data.Effectiveness;
|
|
42
39
|
const Item = data.Item;
|
|
43
40
|
const Move = data.Move;
|
|
44
|
-
const MoveSlot = data.MoveSlot;
|
|
45
|
-
const Pokemon = data.Pokemon;
|
|
46
41
|
const Side = data.Side;
|
|
47
|
-
const Species = data.Species;
|
|
48
42
|
const Stats = data.Stats;
|
|
49
43
|
const Status = data.Status;
|
|
50
44
|
const TriAttack = data.TriAttack;
|