@pkmn/engine 0.1.0-dev.63cef311 → 0.1.0-dev.645b4750

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.
Files changed (84) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +189 -77
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +47 -0
  5. package/build/pkg/addon/browser.js.map +1 -0
  6. package/build/pkg/addon/node.d.ts +3 -0
  7. package/build/pkg/addon/node.js +110 -0
  8. package/build/pkg/addon/node.js.map +1 -0
  9. package/build/pkg/addon/wasm.d.ts +2 -0
  10. package/build/pkg/addon/wasm.js +53 -0
  11. package/build/pkg/addon/wasm.js.map +1 -0
  12. package/build/pkg/addon.d.ts +29 -0
  13. package/build/pkg/addon.js +80 -0
  14. package/build/pkg/addon.js.map +1 -0
  15. package/build/pkg/data/ids.json +12 -12
  16. package/build/pkg/data/layout.json +40 -5
  17. package/build/pkg/data/protocol.json +84 -13
  18. package/build/pkg/data.d.ts +28 -7
  19. package/build/pkg/data.js +3 -5
  20. package/build/pkg/data.js.map +1 -1
  21. package/build/pkg/gen1.d.ts +18 -20
  22. package/build/pkg/gen1.js +76 -51
  23. package/build/pkg/gen1.js.map +1 -1
  24. package/build/pkg/index.d.ts +57 -103
  25. package/build/pkg/index.js +23 -70
  26. package/build/pkg/index.js.map +1 -1
  27. package/build/pkg/protocol.d.ts +24 -3
  28. package/build/pkg/protocol.js +127 -104
  29. package/build/pkg/protocol.js.map +1 -1
  30. package/build/tools/debug.js +110 -0
  31. package/build/tools/debug.js.map +1 -0
  32. package/build/tools/display/debug.jsx +152 -0
  33. package/build/tools/display/debug.jsx.map +1 -0
  34. package/build/tools/display/index.js +152 -0
  35. package/build/tools/display/index.js.map +1 -0
  36. package/build/tools/display/ui.jsx +359 -0
  37. package/build/tools/display/ui.jsx.map +1 -0
  38. package/build/tools/display/util.js +33 -0
  39. package/build/tools/display/util.js.map +1 -0
  40. package/build.zig +417 -160
  41. package/package.json +92 -133
  42. package/src/bin/install-pkmn-engine +85 -42
  43. package/src/bin/pkmn-debug +62 -0
  44. package/src/lib/bindings/c.zig +255 -0
  45. package/src/lib/bindings/node.zig +121 -0
  46. package/src/lib/bindings/wasm.zig +59 -0
  47. package/src/lib/common/array.zig +77 -0
  48. package/src/lib/common/battle.zig +17 -0
  49. package/src/lib/common/data.zig +19 -31
  50. package/src/lib/common/debug.zig +58 -17
  51. package/src/lib/common/js.zig +120 -0
  52. package/src/lib/common/optional.zig +71 -0
  53. package/src/lib/common/options.zig +18 -3
  54. package/src/lib/common/protocol.zig +947 -407
  55. package/src/lib/common/rational.zig +522 -0
  56. package/src/lib/common/rng.zig +31 -24
  57. package/src/lib/common/util.zig +76 -0
  58. package/src/lib/gen1/calc.zig +962 -0
  59. package/src/lib/gen1/chance.zig +1185 -0
  60. package/src/lib/gen1/data/moves.zig +203 -193
  61. package/src/lib/gen1/data/species.zig +6 -8
  62. package/src/lib/gen1/data/types.zig +11 -12
  63. package/src/lib/gen1/data.zig +100 -89
  64. package/src/lib/gen1/helpers.zig +139 -54
  65. package/src/lib/gen1/mechanics.zig +1405 -910
  66. package/src/lib/gen2/calc.zig +153 -0
  67. package/src/lib/gen2/chance.zig +902 -0
  68. package/src/lib/gen2/data/items.zig +15 -23
  69. package/src/lib/gen2/data/moves.zig +665 -573
  70. package/src/lib/gen2/data/species.zig +3 -5
  71. package/src/lib/gen2/data/types.zig +138 -36
  72. package/src/lib/gen2/data.zig +134 -99
  73. package/src/lib/gen2/generated.zig +801 -0
  74. package/src/lib/gen2/helpers.zig +59 -44
  75. package/src/lib/gen2/mechanics.zig +3582 -30
  76. package/src/lib/node.zig +18 -0
  77. package/src/lib/pkmn.zig +93 -12
  78. package/src/lib/wasm.zig +17 -0
  79. package/src/tools/display/dom.ts +93 -0
  80. package/src/tools/display/ui.css +354 -0
  81. package/src/lib/binding/node.zig +0 -229
  82. package/src/lib/binding/wasm.zig +0 -10
  83. package/src/lib/gen1/README.md +0 -778
  84. package/src/lib/gen2/README.md +0 -309
@@ -0,0 +1,255 @@
1
+ const pkmn = @import("../pkmn.zig");
2
+ const std = @import("std");
3
+
4
+ const assert = std.debug.assert;
5
+
6
+ const ERROR: u8 = 0b1100;
7
+
8
+ const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
9
+ const Strong = if (@hasField(std.builtin.GlobalLinkage, "strong")) .strong else .Strong;
10
+
11
+ pub const OPTIONS: extern struct {
12
+ showdown: bool,
13
+ log: bool,
14
+ chance: bool,
15
+ calc: bool,
16
+ } = .{
17
+ .showdown = pkmn.options.showdown,
18
+ .log = pkmn.options.log,
19
+ .chance = pkmn.options.chance,
20
+ .calc = pkmn.options.calc,
21
+ };
22
+
23
+ pub const MAX_CHOICES = pkmn.MAX_CHOICES;
24
+ pub const CHOICES_SIZE = pkmn.CHOICES_SIZE;
25
+ pub const MAX_LOGS = pkmn.MAX_LOGS;
26
+ pub const LOGS_SIZE = pkmn.LOGS_SIZE;
27
+
28
+ pub fn choice_init(choice: u8, data: u8) callconv(.C) u8 {
29
+ assert(choice <= @field(@typeInfo(pkmn.Choice.Type), @tagName(Enum)).fields.len);
30
+ assert(data <= 6);
31
+ return @bitCast(pkmn.Choice{ .type = @enumFromInt(choice), .data = @intCast(data) });
32
+ }
33
+
34
+ pub fn choice_type(choice: u8) callconv(.C) u8 {
35
+ return @intFromEnum(@as(pkmn.Choice, @bitCast(choice)).type);
36
+ }
37
+
38
+ pub fn choice_data(choice: u8) callconv(.C) u8 {
39
+ return @as(u8, @as(pkmn.Choice, @bitCast(choice)).data);
40
+ }
41
+
42
+ pub fn result_type(result: u8) callconv(.C) u8 {
43
+ return @intFromEnum(@as(pkmn.Result, @bitCast(result)).type);
44
+ }
45
+
46
+ pub fn result_p1(result: u8) callconv(.C) u8 {
47
+ assert(!err(result));
48
+ return @intFromEnum(@as(pkmn.Result, @bitCast(result)).p1);
49
+ }
50
+
51
+ pub fn result_p2(result: u8) callconv(.C) u8 {
52
+ assert(!err(result));
53
+ return @intFromEnum(@as(pkmn.Result, @bitCast(result)).p2);
54
+ }
55
+
56
+ pub fn err(result: u8) callconv(.C) bool {
57
+ return result == ERROR;
58
+ }
59
+
60
+ pub fn psrng_init(prng: *pkmn.PSRNG, seed: u64) callconv(.C) void {
61
+ prng.src = .{ .seed = seed };
62
+ }
63
+
64
+ pub fn psrng_next(prng: *pkmn.PSRNG) callconv(.C) u32 {
65
+ return prng.next();
66
+ }
67
+
68
+ pub fn rational_init(rational: *pkmn.Rational(f64)) callconv(.C) void {
69
+ rational.reset();
70
+ }
71
+
72
+ pub fn rational_reduce(rational: *pkmn.Rational(f64)) callconv(.C) void {
73
+ rational.reduce();
74
+ }
75
+
76
+ pub fn rational_numerator(rational: *const pkmn.Rational(f64)) callconv(.C) f64 {
77
+ return rational.p;
78
+ }
79
+
80
+ pub fn rational_denominator(rational: *const pkmn.Rational(f64)) callconv(.C) f64 {
81
+ return rational.q;
82
+ }
83
+
84
+ const log_options = extern struct {
85
+ buf: [*]u8,
86
+ len: usize,
87
+ };
88
+
89
+ pub fn gen(comptime num: comptime_int) type {
90
+ const g = @field(pkmn, "gen" ++ std.fmt.comptimePrint("{d}", .{num}));
91
+
92
+ return struct {
93
+ pub const MAX_CHOICES = g.MAX_CHOICES;
94
+ pub const CHOICES_SIZE = g.CHOICES_SIZE;
95
+ pub const MAX_LOGS = g.MAX_LOGS;
96
+ pub const LOGS_SIZE = g.LOGS_SIZE;
97
+
98
+ const chance_options = extern struct {
99
+ probability: pkmn.Rational(f64),
100
+ actions: g.chance.Actions,
101
+ durations: g.chance.Durations,
102
+ };
103
+
104
+ const calc_options = extern struct {
105
+ overrides: g.chance.Actions,
106
+ };
107
+
108
+ const battle_options = struct {
109
+ stream: pkmn.protocol.ByteStream,
110
+ log: pkmn.protocol.FixedLog,
111
+ chance: g.Chance(pkmn.Rational(f64)),
112
+ calc: g.Calc,
113
+
114
+ comptime {
115
+ assert(@sizeOf(battle_options) <= 128);
116
+ }
117
+ };
118
+
119
+ pub fn battle_options_set(
120
+ options: *battle_options,
121
+ log: ?*const log_options,
122
+ chance: ?*const chance_options,
123
+ calc: ?*const calc_options,
124
+ ) callconv(.C) void {
125
+ if (pkmn.options.log) {
126
+ if (log) |l| {
127
+ options.stream = .{ .buffer = l.buf[0..l.len] };
128
+ options.log = .{ .writer = options.stream.writer() };
129
+ } else {
130
+ options.stream.reset();
131
+ }
132
+ }
133
+ if (pkmn.options.chance) {
134
+ if (chance) |c| {
135
+ options.chance = .{
136
+ .probability = c.probability,
137
+ .actions = c.actions,
138
+ .durations = c.durations,
139
+ };
140
+ } else {
141
+ options.chance.reset();
142
+ }
143
+ }
144
+ if (pkmn.options.calc) {
145
+ if (calc) |c| {
146
+ options.calc = .{ .overrides = c.overrides };
147
+ } else {
148
+ options.calc = .{};
149
+ }
150
+ }
151
+ }
152
+
153
+ pub fn battle_options_chance_probability(
154
+ options: *battle_options,
155
+ ) callconv(.C) *pkmn.Rational(f64) {
156
+ return &options.chance.probability;
157
+ }
158
+
159
+ pub fn battle_options_chance_actions(
160
+ options: *battle_options,
161
+ ) callconv(.C) *g.chance.Actions {
162
+ return &options.chance.actions;
163
+ }
164
+
165
+ pub fn battle_options_chance_durations(
166
+ options: *battle_options,
167
+ ) callconv(.C) *g.chance.Durations {
168
+ return &options.chance.durations;
169
+ }
170
+
171
+ pub fn battle_options_calc_summaries(
172
+ options: *battle_options,
173
+ ) callconv(.C) *g.calc.Summaries {
174
+ return &options.calc.summaries;
175
+ }
176
+
177
+ pub fn battle_update(
178
+ battle: *g.Battle(g.PRNG),
179
+ c1: pkmn.Choice,
180
+ c2: pkmn.Choice,
181
+ opts: ?*battle_options,
182
+ ) callconv(.C) pkmn.Result {
183
+ if ((pkmn.options.log or pkmn.options.chance or pkmn.options.calc) and opts != null) {
184
+ return battle.update(c1, c2, opts.?) catch return @bitCast(ERROR);
185
+ }
186
+ return battle.update(c1, c2, &g.NULL) catch unreachable;
187
+ }
188
+
189
+ pub fn battle_choices(
190
+ battle: *const g.Battle(g.PRNG),
191
+ player: u8,
192
+ request: u8,
193
+ out: [*]u8,
194
+ n: usize,
195
+ ) callconv(.C) u8 {
196
+ assert(player <= @field(@typeInfo(pkmn.Player), @tagName(Enum)).fields.len);
197
+ assert(request <= @field(@typeInfo(pkmn.Choice.Type), @tagName(Enum)).fields.len);
198
+
199
+ assert(!pkmn.options.showdown or n > 0);
200
+ return battle.choices(@enumFromInt(player), @enumFromInt(request), @ptrCast(out[0..n]));
201
+ }
202
+ };
203
+ }
204
+
205
+ pub fn exports() type {
206
+ @export(choice_init, .{ .name = "pkmn_choice_init", .linkage = Strong });
207
+ @export(choice_type, .{ .name = "pkmn_choice_type", .linkage = Strong });
208
+ @export(choice_data, .{ .name = "pkmn_choice_data", .linkage = Strong });
209
+
210
+ @export(result_type, .{ .name = "pkmn_result_type", .linkage = Strong });
211
+ @export(result_p1, .{ .name = "pkmn_result_p1", .linkage = Strong });
212
+ @export(result_p2, .{ .name = "pkmn_result_p2", .linkage = Strong });
213
+
214
+ @export(err, .{ .name = "pkmn_error", .linkage = Strong });
215
+
216
+ @export(psrng_init, .{ .name = "pkmn_psrng_init", .linkage = Strong });
217
+ @export(psrng_next, .{ .name = "pkmn_psrng_next", .linkage = Strong });
218
+
219
+ @export(rational_init, .{ .name = "pkmn_rational_init", .linkage = Strong });
220
+ @export(rational_reduce, .{ .name = "pkmn_rational_reduce", .linkage = Strong });
221
+ @export(rational_numerator, .{ .name = "pkmn_rational_numerator", .linkage = Strong });
222
+ @export(rational_denominator, .{ .name = "pkmn_rational_denominator", .linkage = Strong });
223
+
224
+ @export(
225
+ gen(1).battle_options_set,
226
+ .{ .name = "pkmn_gen1_battle_options_set", .linkage = Strong },
227
+ );
228
+ @export(
229
+ gen(1).battle_options_chance_probability,
230
+ .{ .name = "pkmn_gen1_battle_options_chance_probability", .linkage = Strong },
231
+ );
232
+ @export(
233
+ gen(1).battle_options_chance_actions,
234
+ .{ .name = "pkmn_gen1_battle_options_chance_actions", .linkage = Strong },
235
+ );
236
+ @export(
237
+ gen(1).battle_options_chance_durations,
238
+ .{ .name = "pkmn_gen1_battle_options_chance_durations", .linkage = Strong },
239
+ );
240
+ @export(
241
+ gen(1).battle_options_calc_summaries,
242
+ .{ .name = "pkmn_gen1_battle_options_calc_summaries", .linkage = Strong },
243
+ );
244
+
245
+ @export(
246
+ gen(1).battle_update,
247
+ .{ .name = "pkmn_gen1_battle_update", .linkage = Strong },
248
+ );
249
+ @export(
250
+ gen(1).battle_choices,
251
+ .{ .name = "pkmn_gen1_battle_choices", .linkage = Strong },
252
+ );
253
+
254
+ return struct {};
255
+ }
@@ -0,0 +1,121 @@
1
+ const js = @import("../common/js.zig");
2
+ const pkmn = @import("../pkmn.zig");
3
+ const std = @import("std");
4
+
5
+ const assert = std.debug.assert;
6
+
7
+ const c = @cImport({
8
+ @cDefine("NAPI_VERSION", "8");
9
+ @cInclude("node_api.h");
10
+ });
11
+
12
+ pub fn register(env: c.napi_env) c.napi_value {
13
+ const object = js.Object.init(env);
14
+ const properties = [_]c.napi_property_descriptor{
15
+ js.Property.init("options", .{ .value = options(env) }),
16
+ js.Property.init("bindings", .{ .value = bindings(env) }),
17
+ };
18
+ assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
19
+ return object;
20
+ }
21
+
22
+ fn options(env: c.napi_env) c.napi_value {
23
+ const object = js.Object.init(env);
24
+ const properties = [_]c.napi_property_descriptor{
25
+ js.Property.init("showdown", .{ .value = js.Boolean.init(env, pkmn.options.showdown) }),
26
+ js.Property.init("log", .{ .value = js.Boolean.init(env, pkmn.options.log) }),
27
+ js.Property.init("chance", .{ .value = js.Boolean.init(env, pkmn.options.chance) }),
28
+ js.Property.init("calc", .{ .value = js.Boolean.init(env, pkmn.options.calc) }),
29
+ };
30
+ assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
31
+ return object;
32
+ }
33
+
34
+ fn bindings(env: c.napi_env) c.napi_value {
35
+ const array = js.Array.init(env, .{ .length = 1 });
36
+ js.Array.set(env, array, 0, bind(env, pkmn.gen1));
37
+ return array;
38
+ }
39
+
40
+ fn bind(env: c.napi_env, gen: anytype) c.napi_value {
41
+ const choices_size: u32 = @intCast(gen.CHOICES_SIZE);
42
+ const logs_size: u32 = @intCast(gen.LOGS_SIZE);
43
+ const object = js.Object.init(env);
44
+ const properties = [_]c.napi_property_descriptor{
45
+ js.Property.init("CHOICES_SIZE", .{ .value = js.Number.init(env, choices_size) }),
46
+ js.Property.init("LOGS_SIZE", .{ .value = js.Number.init(env, logs_size) }),
47
+ js.Property.init("update", .{ .method = update(gen) }),
48
+ js.Property.init("choices", .{ .method = choices(gen) }),
49
+ };
50
+ assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
51
+ return object;
52
+ }
53
+
54
+ fn update(gen: anytype) c.napi_callback {
55
+ return struct {
56
+ fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
57
+ var argc: usize = 4;
58
+ var argv: [4]c.napi_value = undefined;
59
+ assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
60
+ assert(argc == 4);
61
+
62
+ var data: ?*anyopaque = undefined;
63
+ var len: usize = 0;
64
+ assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
65
+ assert(len == @sizeOf(gen.Battle(gen.PRNG)));
66
+
67
+ var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
68
+ const c1: pkmn.Choice = @bitCast(js.Number.get(env, argv[1], u8));
69
+ const c2: pkmn.Choice = @bitCast(js.Number.get(env, argv[2], u8));
70
+
71
+ var vtype: c.napi_valuetype = undefined;
72
+ assert(c.napi_typeof(env, argv[3], &vtype) == c.napi_ok);
73
+ const result = switch (vtype) {
74
+ c.napi_undefined, c.napi_null => battle.update(c1, c2, &gen.NULL),
75
+ else => result: {
76
+ assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
77
+ assert(len == gen.LOGS_SIZE);
78
+
79
+ const buf = @as([*]u8, @ptrCast(data.?))[0..gen.LOGS_SIZE];
80
+ var stream: pkmn.protocol.ByteStream = .{ .buffer = buf };
81
+ // TODO: extract out
82
+ var opts = pkmn.battle.options(
83
+ pkmn.protocol.FixedLog{ .writer = stream.writer() },
84
+ gen.chance.NULL,
85
+ gen.calc.NULL,
86
+ );
87
+ break :result battle.update(c1, c2, &opts);
88
+ },
89
+ } catch unreachable;
90
+
91
+ return js.Number.init(env, @as(u8, @bitCast(result)));
92
+ }
93
+ }.call;
94
+ }
95
+
96
+ fn choices(gen: anytype) c.napi_callback {
97
+ return struct {
98
+ fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
99
+ var argc: usize = 4;
100
+ var argv: [4]c.napi_value = undefined;
101
+ assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
102
+ assert(argc == 4);
103
+
104
+ var data: ?*anyopaque = undefined;
105
+ var len: usize = 0;
106
+ assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
107
+ assert(len == @sizeOf(gen.Battle(gen.PRNG)));
108
+
109
+ var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
110
+ const player: pkmn.Player = @enumFromInt(js.Number.get(env, argv[1], u8));
111
+ const request: pkmn.Choice.Type = @enumFromInt(js.Number.get(env, argv[2], u8));
112
+
113
+ assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
114
+ assert(len == gen.CHOICES_SIZE);
115
+
116
+ const out = @as([*]pkmn.Choice, @ptrCast(data.?))[0..gen.CHOICES_SIZE];
117
+ const n = battle.choices(player, request, out);
118
+ return js.Number.init(env, @as(u8, @bitCast(n)));
119
+ }
120
+ }.call;
121
+ }
@@ -0,0 +1,59 @@
1
+ const pkmn = @import("../pkmn.zig");
2
+ const std = @import("std");
3
+
4
+ const assert = std.debug.assert;
5
+
6
+ const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
7
+ const Strong = if (@hasField(std.builtin.GlobalLinkage, "strong")) .strong else .Strong;
8
+
9
+ pub const options = pkmn.options;
10
+
11
+ pub fn gen(comptime num: comptime_int) type {
12
+ const g = @field(pkmn, "gen" ++ std.fmt.comptimePrint("{d}", .{num}));
13
+ return struct {
14
+ pub const CHOICES_SIZE = size(g.CHOICES_SIZE);
15
+ pub const LOGS_SIZE = size(g.LOGS_SIZE);
16
+
17
+ pub fn update(
18
+ battle: *g.Battle(g.PRNG),
19
+ c1: pkmn.Choice,
20
+ c2: pkmn.Choice,
21
+ options_: ?[*]u8,
22
+ ) callconv(.C) pkmn.Result {
23
+ return (if (options_) |o| result: {
24
+ const buf = @as([*]u8, @ptrCast(o))[0..g.LOGS_SIZE];
25
+ var stream: pkmn.protocol.ByteStream = .{ .buffer = buf };
26
+ // TODO: extract out
27
+ var opts = pkmn.battle.options(
28
+ pkmn.protocol.FixedLog{ .writer = stream.writer() },
29
+ g.chance.NULL,
30
+ g.calc.NULL,
31
+ );
32
+ break :result battle.update(c1, c2, &opts);
33
+ } else battle.update(c1, c2, &g.NULL)) catch unreachable;
34
+ }
35
+
36
+ pub fn choices(
37
+ battle: *g.Battle(g.PRNG),
38
+ player: u8,
39
+ request: u8,
40
+ buf: [*]u8,
41
+ ) callconv(.C) u8 {
42
+ assert(player <= @field(@typeInfo(pkmn.Player), @tagName(Enum)).fields.len);
43
+ assert(request <= @field(@typeInfo(pkmn.Choice.Type), @tagName(Enum)).fields.len);
44
+
45
+ const n = CHOICES_SIZE;
46
+ return battle.choices(@enumFromInt(player), @enumFromInt(request), @ptrCast(buf[0..n]));
47
+ }
48
+ };
49
+ }
50
+
51
+ pub fn exports() type {
52
+ @export(gen(1).update, .{ .name = "GEN1_update", .linkage = Strong });
53
+ @export(gen(1).choices, .{ .name = "GEN1_choices", .linkage = Strong });
54
+ return struct {};
55
+ }
56
+
57
+ fn size(n: usize) u32 {
58
+ return std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(n))) catch unreachable;
59
+ }
@@ -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: std.builtin.Type.Int = .{ .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
+ }
@@ -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
+ }
@@ -9,12 +9,12 @@ pub const Player = enum(u1) {
9
9
  P2,
10
10
 
11
11
  /// Returns a player's opponent.
12
- pub inline fn foe(self: Player) Player {
13
- return @intToEnum(Player, ~@enumToInt(self));
12
+ pub fn foe(self: Player) Player {
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`.
17
- pub inline fn ident(self: Player, id: u3) ID {
17
+ pub fn ident(self: Player, id: u3) ID {
18
18
  assert(id > 0 and id <= 6);
19
19
  return .{ .id = id, .player = self };
20
20
  }
@@ -22,42 +22,38 @@ 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.
30
- pub const ID = packed struct {
30
+ pub const ID = packed struct(u8) {
31
31
  /// The one-indexed team slot of the Pokémon
32
32
  id: u3 = 0,
33
33
  /// The Pokémon's trainer.
34
34
  player: Player = .P1,
35
35
  _: u4 = 0,
36
36
 
37
- comptime {
38
- assert(@sizeOf(ID) == 1);
39
- }
40
-
41
37
  /// Converts the identifier into a number.
42
- pub inline fn int(self: ID) u4 {
43
- return @intCast(u4, @bitCast(u8, self));
38
+ pub fn int(self: ID) u4 {
39
+ return @intCast(@as(u8, @bitCast(self)));
44
40
  }
45
41
 
46
42
  /// Decodes the identifier from a number.
47
- pub inline fn from(id: u4) ID {
48
- return @bitCast(ID, @as(u8, id));
43
+ pub fn from(id: u4) ID {
44
+ return @bitCast(@as(u8, id));
49
45
  }
50
46
  };
51
47
 
52
48
  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 }));
55
- const id = ID{ .player = .P2, .id = 4 };
49
+ try expectEqual(@as(u8, 0b0001), @as(u8, @bitCast(ID{ .player = .P1, .id = 1 })));
50
+ try expectEqual(@as(u8, 0b1101), @as(u8, @bitCast(ID{ .player = .P2, .id = 5 })));
51
+ const id: ID = .{ .player = .P2, .id = 4 };
56
52
  try expectEqual(id, ID.from(id.int()));
57
53
  }
58
54
 
59
55
  /// A choice made by a player during battle.
60
- pub const Choice = packed struct {
56
+ pub const Choice = packed struct(u8) {
61
57
  /// The choice type.
62
58
  type: Choice.Type = .Pass,
63
59
  /// The choice data:
@@ -73,10 +69,6 @@ pub const Choice = packed struct {
73
69
  Move,
74
70
  Switch,
75
71
  };
76
-
77
- comptime {
78
- assert(@sizeOf(Choice) == 1);
79
- }
80
72
  };
81
73
 
82
74
  test Choice {
@@ -84,12 +76,12 @@ test Choice {
84
76
  const p2: Choice = .{ .type = .Switch, .data = 5 };
85
77
  try expectEqual(5, p2.data);
86
78
  try expectEqual(Choice.Type.Move, p1.type);
87
- try expectEqual(0b0001_0001, @bitCast(u8, p1));
88
- try expectEqual(0b0001_0110, @bitCast(u8, p2));
79
+ try expectEqual(@as(u8, 0b0001_0001), @as(u8, @bitCast(p1)));
80
+ try expectEqual(@as(u8, 0b0001_0110), @as(u8, @bitCast(p2)));
89
81
  }
90
82
 
91
83
  /// The result of the battle - all results other than 'None' should be considered terminal.
92
- pub const Result = packed struct {
84
+ pub const Result = packed struct(u8) {
93
85
  /// The type of result from the perspective of Player 1.
94
86
  /// `Error` is not possible when in Pokémon Showdown compatibility mode.
95
87
  type: Result.Type = .None,
@@ -117,13 +109,9 @@ pub const Result = packed struct {
117
109
  pub const Error: Result = .{ .type = .Error };
118
110
  /// The default result.
119
111
  pub const Default: Result = .{ .p1 = .Move, .p2 = .Move };
120
-
121
- comptime {
122
- assert(@sizeOf(Result) == 1);
123
- }
124
112
  };
125
113
 
126
114
  test Result {
127
- try expectEqual(0b0101_0000, @bitCast(u8, Result.Default));
128
- try expectEqual(0b1000_0000, @bitCast(u8, Result{ .p2 = .Switch }));
115
+ try expectEqual(@as(u8, 0b0101_0000), @as(u8, @bitCast(Result.Default)));
116
+ try expectEqual(@as(u8, 0b1000_0000), @as(u8, @bitCast(Result{ .p2 = .Switch })));
129
117
  }