@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.
@@ -3,8 +3,10 @@ const pkmn = @import("../pkmn.zig");
3
3
 
4
4
  export const SHOWDOWN = pkmn.options.showdown;
5
5
  export const LOG = pkmn.options.log;
6
+ export const CHANCE = pkmn.options.chance;
7
+ export const CALC = pkmn.options.calc;
6
8
 
7
9
  export const GEN1_CHOICES_SIZE =
8
- std.math.ceilPowerOfTwo(u32, @intCast(u32, pkmn.gen1.CHOICES_SIZE)) catch unreachable;
10
+ std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(pkmn.gen1.CHOICES_SIZE))) catch unreachable;
9
11
  export const GEN1_LOGS_SIZE =
10
- std.math.ceilPowerOfTwo(u32, @intCast(u32, pkmn.gen1.LOGS_SIZE)) catch unreachable;
12
+ std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(pkmn.gen1.LOGS_SIZE))) catch unreachable;
@@ -0,0 +1,17 @@
1
+ /// Stores state required by optional features to the engine that can be passed to a generation's
2
+ /// `Battle.update` function. All generations generically support protocol logging (enabled via
3
+ /// `-Dlog`), chance actions and probability tracking (enabled via `-Dchance`), and damage
4
+ /// computation and chance overrides (`-Dcalc`), though the concrete types for these features are
5
+ /// generation dependent.
6
+ pub fn Options(comptime Log: type, comptime Chance: type, comptime Calc: type) type {
7
+ return struct { log: Log, chance: Chance, calc: Calc };
8
+ }
9
+
10
+ /// Helper to create a battle `Options` object with the provided `log`, `chance`, and `calc`.
11
+ pub fn options(
12
+ log: anytype,
13
+ chance: anytype,
14
+ calc: anytype,
15
+ ) Options(@TypeOf(log), @TypeOf(chance), @TypeOf(calc)) {
16
+ return .{ .log = log, .chance = chance, .calc = calc };
17
+ }
@@ -10,7 +10,7 @@ pub const Player = enum(u1) {
10
10
 
11
11
  /// Returns a player's opponent.
12
12
  pub inline fn foe(self: Player) Player {
13
- return @enumFromInt(Player, ~@intFromEnum(self));
13
+ return @enumFromInt(~@intFromEnum(self));
14
14
  }
15
15
 
16
16
  /// Return's an identifier for the player's Pokémon at the one-indexed `id`.
@@ -22,8 +22,8 @@ pub const Player = enum(u1) {
22
22
 
23
23
  test Player {
24
24
  try expectEqual(Player.P2, Player.P1.foe());
25
- try expectEqual(@as(u8, 0b0001), @bitCast(u8, Player.P1.ident(1)));
26
- try expectEqual(@as(u8, 0b1101), @bitCast(u8, Player.P2.ident(5)));
25
+ try expectEqual(@as(u8, 0b0001), @as(u8, @bitCast(Player.P1.ident(1))));
26
+ try expectEqual(@as(u8, 0b1101), @as(u8, @bitCast(Player.P2.ident(5))));
27
27
  }
28
28
 
29
29
  /// An identifier for a specific Pokémon in battle.
@@ -40,18 +40,18 @@ pub const ID = packed struct {
40
40
 
41
41
  /// Converts the identifier into a number.
42
42
  pub inline fn int(self: ID) u4 {
43
- return @intCast(u4, @bitCast(u8, self));
43
+ return @intCast(@as(u8, @bitCast(self)));
44
44
  }
45
45
 
46
46
  /// Decodes the identifier from a number.
47
47
  pub inline fn from(id: u4) ID {
48
- return @bitCast(ID, @as(u8, id));
48
+ return @bitCast(@as(u8, id));
49
49
  }
50
50
  };
51
51
 
52
52
  test ID {
53
- try expectEqual(@as(u8, 0b0001), @bitCast(u8, ID{ .player = .P1, .id = 1 }));
54
- try expectEqual(@as(u8, 0b1101), @bitCast(u8, ID{ .player = .P2, .id = 5 }));
53
+ try expectEqual(@as(u8, 0b0001), @as(u8, @bitCast(ID{ .player = .P1, .id = 1 })));
54
+ try expectEqual(@as(u8, 0b1101), @as(u8, @bitCast(ID{ .player = .P2, .id = 5 })));
55
55
  const id = ID{ .player = .P2, .id = 4 };
56
56
  try expectEqual(id, ID.from(id.int()));
57
57
  }
@@ -84,8 +84,8 @@ test Choice {
84
84
  const p2: Choice = .{ .type = .Switch, .data = 5 };
85
85
  try expectEqual(5, p2.data);
86
86
  try expectEqual(Choice.Type.Move, p1.type);
87
- try expectEqual(0b0001_0001, @bitCast(u8, p1));
88
- try expectEqual(0b0001_0110, @bitCast(u8, p2));
87
+ try expectEqual(0b0001_0001, @as(u8, @bitCast(p1)));
88
+ try expectEqual(0b0001_0110, @as(u8, @bitCast(p2)));
89
89
  }
90
90
 
91
91
  /// The result of the battle - all results other than 'None' should be considered terminal.
@@ -124,6 +124,6 @@ pub const Result = packed struct {
124
124
  };
125
125
 
126
126
  test Result {
127
- try expectEqual(0b0101_0000, @bitCast(u8, Result.Default));
128
- try expectEqual(0b1000_0000, @bitCast(u8, Result{ .p2 = .Switch }));
127
+ try expectEqual(0b0101_0000, @as(u8, @bitCast(Result.Default)));
128
+ try expectEqual(0b1000_0000, @as(u8, @bitCast(Result{ .p2 = .Switch })));
129
129
  }
@@ -10,18 +10,27 @@ pub fn print(value: anytype) void {
10
10
 
11
11
  nosuspend {
12
12
  stderr.writeAll("\x1b[41m") catch return;
13
- switch (@typeInfo(@TypeOf(value))) {
14
- .Struct => |info| {
15
- if (info.is_tuple) {
16
- inline for (info.fields, 0..) |f, i| {
17
- inspect(@field(value, f.name));
18
- if (i < info.fields.len - 1) stderr.writeAll(" ") catch return;
13
+ if (@TypeOf(@src()) == @TypeOf(value)) {
14
+ stderr.print("{s} ({s}:{d}:{d})", .{
15
+ value.fn_name,
16
+ value.file,
17
+ value.line,
18
+ value.column,
19
+ }) catch return;
20
+ } else {
21
+ switch (@typeInfo(@TypeOf(value))) {
22
+ .Struct => |info| {
23
+ if (info.is_tuple) {
24
+ inline for (info.fields, 0..) |f, i| {
25
+ inspect(@field(value, f.name));
26
+ if (i < info.fields.len - 1) stderr.writeAll(" ") catch return;
27
+ }
28
+ } else {
29
+ inspect(value);
19
30
  }
20
- } else {
21
- inspect(value);
22
- }
23
- },
24
- else => inspect(value),
31
+ },
32
+ else => inspect(value),
33
+ }
25
34
  }
26
35
  stderr.writeAll("\x1b[K\x1b[0m\n") catch return;
27
36
  }
@@ -0,0 +1,118 @@
1
+ const std = @import("std");
2
+
3
+ const assert = std.debug.assert;
4
+
5
+ const c = @cImport({
6
+ @cDefine("NAPI_VERSION", "8");
7
+ @cInclude("node_api.h");
8
+ });
9
+
10
+ pub const Array = struct {
11
+ pub fn init(env: c.napi_env, o: struct { length: ?usize }) c.napi_value {
12
+ var result: c.napi_value = undefined;
13
+ assert(c.napi_ok == if (o.length) |n|
14
+ c.napi_create_array_with_length(env, n, &result)
15
+ else
16
+ c.napi_create_array(env, &result));
17
+ return result;
18
+ }
19
+
20
+ pub fn set(env: c.napi_env, array: c.napi_value, index: u32, value: c.napi_value) void {
21
+ assert(c.napi_set_element(env, array, index, value) == c.napi_ok);
22
+ }
23
+ };
24
+
25
+ pub const Boolean = struct {
26
+ pub fn init(env: c.napi_env, value: bool) c.napi_value {
27
+ var result: c.napi_value = undefined;
28
+ assert(c.napi_get_boolean(env, value, &result) == c.napi_ok);
29
+ return result;
30
+ }
31
+ };
32
+
33
+ pub const Number = struct {
34
+ pub fn init(env: c.napi_env, value: anytype) c.napi_value {
35
+ const T = @TypeOf(value);
36
+ var result: c.napi_value = undefined;
37
+ assert(c.napi_ok == switch (@typeInfo(T)) {
38
+ .Int => |info| switch (info.bits) {
39
+ 0...32 => switch (info.signedness) {
40
+ .signed => c.napi_create_int32(env, @as(i32, value), &result),
41
+ .unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
42
+ },
43
+ 33...52 => c.napi_create_int64(env, @as(i64, value), &result),
44
+ else => @compileError("int can't be represented as JS number"),
45
+ },
46
+ else => @compileError("expected number, got: " ++ @typeName(T)),
47
+ });
48
+ return result;
49
+ }
50
+
51
+ pub fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
52
+ switch (@typeInfo(T)) {
53
+ .Int => |info| switch (info.bits) {
54
+ 0...32 => switch (info.signedness) {
55
+ .signed => {
56
+ var result: i32 = undefined;
57
+ assert(c.napi_get_value_int32(env, value, &result) == c.napi_ok);
58
+ return if (info.bits == 32) result else @intCast(result);
59
+ },
60
+ .unsigned => {
61
+ var result: u32 = undefined;
62
+ assert(c.napi_get_value_uint32(env, value, &result) == c.napi_ok);
63
+ return if (info.bits == 32) result else @intCast(result);
64
+ },
65
+ },
66
+ 33...63 => {
67
+ var result: i64 = undefined;
68
+ assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
69
+ return @intCast(result);
70
+ },
71
+ else => {
72
+ var result: i64 = undefined;
73
+ assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
74
+ return switch (info.signedness) {
75
+ .signed => value,
76
+ .unsigned => if (0 <= value) @intCast(value) else unreachable,
77
+ };
78
+ },
79
+ },
80
+ else => @compileError("expected number, got: " ++ @typeName(T)),
81
+ }
82
+ }
83
+ };
84
+
85
+ pub const Object = struct {
86
+ pub fn init(env: c.napi_env) c.napi_value {
87
+ var result: c.napi_value = undefined;
88
+ assert(c.napi_create_object(env, &result) == c.napi_ok);
89
+ return result;
90
+ }
91
+ };
92
+
93
+ pub const Property = union(enum) {
94
+ method: c.napi_callback,
95
+ value: c.napi_value,
96
+
97
+ pub fn init(comptime name: [:0]const u8, property: Property) c.napi_property_descriptor {
98
+ return .{
99
+ .utf8name = name,
100
+ .name = null,
101
+ .method = switch (property) {
102
+ .method => |m| m,
103
+ .value => null,
104
+ },
105
+ .getter = null,
106
+ .setter = null,
107
+ .value = switch (property) {
108
+ .method => null,
109
+ .value => |v| v,
110
+ },
111
+ .attributes = switch (property) {
112
+ .method => c.napi_default,
113
+ .value => c.napi_enumerable,
114
+ },
115
+ .data = null,
116
+ };
117
+ }
118
+ };
@@ -0,0 +1,60 @@
1
+ const std = @import("std");
2
+
3
+ const expect = std.testing.expect;
4
+ const assert = std.debug.assert;
5
+
6
+ // TODO: ziglang/zig#104
7
+ pub fn Optional(comptime T: type) type {
8
+ const fields = std.meta.fields(switch (@typeInfo(T)) {
9
+ .Bool => enum { false, true },
10
+ else => T,
11
+ });
12
+
13
+ var enumFields: [fields.len + 1]std.builtin.Type.EnumField = undefined;
14
+ var decls = [_]std.builtin.Type.Declaration{};
15
+
16
+ enumFields[0] = .{ .name = "None", .value = 0 };
17
+ inline for (fields, 1..) |field, i| {
18
+ assert(!std.mem.eql(u8, field.name, "None"));
19
+ enumFields[i] = .{
20
+ .name = field.name,
21
+ .value = i,
22
+ };
23
+ }
24
+
25
+ return @Type(.{
26
+ .Enum = .{
27
+ .tag_type = std.math.IntFittingRange(0, fields.len + 1),
28
+ .fields = &enumFields,
29
+ .decls = &decls,
30
+ .is_exhaustive = true,
31
+ },
32
+ });
33
+ }
34
+
35
+ test Optional {
36
+ try expect(@bitSizeOf(Optional(bool)) == 2);
37
+
38
+ const a: Optional(bool) = .true;
39
+ try expect(a != .None);
40
+ try expect(a == .true);
41
+ try expect(a != .false);
42
+
43
+ const b: Optional(bool) = .None;
44
+ try expect(b == .None);
45
+ try expect(b != .true);
46
+ try expect(b != .false);
47
+
48
+ const Player = @import("./data.zig").Player;
49
+ try expect(@bitSizeOf(Optional(Player)) == 2);
50
+
51
+ const p: Optional(Player) = .P2;
52
+ try expect(p != .None);
53
+ try expect(p != .P1);
54
+ try expect(p == .P2);
55
+
56
+ const q: Optional(Player) = .None;
57
+ try expect(q == .None);
58
+ try expect(q != .P1);
59
+ try expect(q != .P2);
60
+ }
@@ -1,11 +1,15 @@
1
1
  const build_options = @import("build_options");
2
2
  const root = @import("root");
3
3
 
4
+ /// Configures the behavior of the pkmn package.
4
5
  pub const Options = struct {
5
6
  showdown: ?bool = null,
6
7
  log: ?bool = null,
8
+ chance: ?bool = null,
9
+ calc: ?bool = null,
7
10
  advance: ?bool = null,
8
11
  ebc: ?bool = null,
12
+ key: ?bool = null,
9
13
  internal: ?bool = null,
10
14
  };
11
15
 
@@ -13,9 +17,14 @@ pub const Options = struct {
13
17
  pub const showdown = get("showdown", false);
14
18
  /// Whether or not protocol message logging is enabled.
15
19
  pub const log = get("log", false);
20
+ /// Whether or not update probability tracking is enabled.
21
+ pub const chance = get("chance", false);
22
+ /// Whether or not damage calculator support is enabled.
23
+ pub const calc = get("calc", false);
16
24
 
17
25
  pub const advance = get("advance", true);
18
26
  pub const ebc = get("ebc", true);
27
+ pub const key = get("key", false);
19
28
  pub const internal = get("internal", false);
20
29
 
21
30
  fn get(comptime name: []const u8, default: bool) bool {