@pkmn/engine 0.1.0-dev.c4dec0f7 → 0.1.0-dev.c4ffaf39

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 (63) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +135 -71
  3. package/build/pkg/addon.d.ts +7 -0
  4. package/build/pkg/addon.js +111 -0
  5. package/build/pkg/addon.js.map +1 -0
  6. package/build/pkg/data/ids.json +12 -12
  7. package/build/pkg/data/layout.json +27 -5
  8. package/build/pkg/data/protocol.json +69 -12
  9. package/build/pkg/data.d.ts +5 -6
  10. package/build/pkg/data.js +3 -5
  11. package/build/pkg/data.js.map +1 -1
  12. package/build/pkg/gen1.d.ts +5 -4
  13. package/build/pkg/gen1.js +49 -33
  14. package/build/pkg/gen1.js.map +1 -1
  15. package/build/pkg/index.d.ts +17 -13
  16. package/build/pkg/index.js.map +1 -1
  17. package/build/pkg/protocol.d.ts +4 -2
  18. package/build/pkg/protocol.js +65 -44
  19. package/build/pkg/protocol.js.map +1 -1
  20. package/build/test/display.js +400 -0
  21. package/build/test/display.js.map +1 -0
  22. package/build/tools/debug.js +134 -0
  23. package/build/tools/debug.js.map +1 -0
  24. package/build.zig +161 -149
  25. package/package.json +86 -46
  26. package/src/bin/install-pkmn-engine +79 -39
  27. package/src/bin/pkmn-debug +62 -0
  28. package/src/lib/common/battle.zig +17 -0
  29. package/src/lib/common/data.zig +19 -31
  30. package/src/lib/common/debug.zig +23 -13
  31. package/src/lib/common/js.zig +118 -0
  32. package/src/lib/common/optional.zig +67 -0
  33. package/src/lib/common/options.zig +14 -3
  34. package/src/lib/common/protocol.zig +942 -403
  35. package/src/lib/common/rational.zig +357 -0
  36. package/src/lib/common/rng.zig +35 -26
  37. package/src/lib/common/util.zig +12 -0
  38. package/src/lib/gen1/calc.zig +491 -0
  39. package/src/lib/gen1/chance.zig +1063 -0
  40. package/src/lib/gen1/data/moves.zig +202 -190
  41. package/src/lib/gen1/data/species.zig +5 -5
  42. package/src/lib/gen1/data/types.zig +10 -10
  43. package/src/lib/gen1/data.zig +106 -92
  44. package/src/lib/gen1/helpers.zig +620 -35
  45. package/src/lib/gen1/mechanics.zig +1214 -826
  46. package/src/lib/gen2/calc.zig +143 -0
  47. package/src/lib/gen2/chance.zig +1315 -0
  48. package/src/lib/gen2/data/items.zig +14 -20
  49. package/src/lib/gen2/data/moves.zig +664 -570
  50. package/src/lib/gen2/data/species.zig +2 -2
  51. package/src/lib/gen2/data/types.zig +137 -34
  52. package/src/lib/gen2/data.zig +134 -96
  53. package/src/lib/gen2/generated.zig +806 -0
  54. package/src/lib/gen2/helpers.zig +50 -26
  55. package/src/lib/gen2/mechanics.zig +3591 -12
  56. package/src/lib/node.zig +131 -0
  57. package/src/lib/pkmn.zig +86 -12
  58. package/src/lib/wasm.zig +13 -0
  59. package/src/test/display/pkmn.html.tmpl +368 -0
  60. package/src/lib/binding/node.zig +0 -229
  61. package/src/lib/binding/wasm.zig +0 -10
  62. package/src/lib/gen1/README.md +0 -778
  63. package/src/lib/gen2/README.md +0 -309
@@ -1,229 +0,0 @@
1
- const std = @import("std");
2
- const pkmn = @import("../pkmn.zig");
3
-
4
- const assert = std.debug.assert;
5
-
6
- const c = @cImport({
7
- @cDefine("NAPI_VERSION", "8");
8
- @cInclude("node_api.h");
9
- });
10
-
11
- export fn napi_register_module_v1(env: c.napi_env, exports: c.napi_value) c.napi_value {
12
- const properties = [_]c.napi_property_descriptor{
13
- Property.init("options", .{ .value = options(env) }),
14
- Property.init("bindings", .{ .value = bindings(env) }),
15
- };
16
- assert(c.napi_define_properties(env, exports, properties.len, &properties) == c.napi_ok);
17
- return exports;
18
- }
19
-
20
- fn options(env: c.napi_env) c.napi_value {
21
- var object = Object.init(env);
22
- const properties = [_]c.napi_property_descriptor{
23
- Property.init("showdown", .{ .value = Boolean.init(env, pkmn.options.showdown) }),
24
- Property.init("trace", .{ .value = Boolean.init(env, pkmn.options.trace) }),
25
- };
26
- assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
27
- return object;
28
- }
29
-
30
- fn bindings(env: c.napi_env) c.napi_value {
31
- var array = Array.init(env, .{ .length = 1 });
32
- Array.set(env, array, 0, bind(env, pkmn.gen1));
33
- return array;
34
- }
35
-
36
- fn bind(env: c.napi_env, gen: anytype) c.napi_value {
37
- const options_size = @intCast(u32, gen.OPTIONS_SIZE);
38
- const logs_size = @intCast(u32, gen.LOGS_SIZE);
39
- var object = Object.init(env);
40
- const properties = [_]c.napi_property_descriptor{
41
- Property.init("OPTIONS_SIZE", .{ .value = Number.init(env, options_size) }),
42
- Property.init("LOGS_SIZE", .{ .value = Number.init(env, logs_size) }),
43
- Property.init("update", .{ .method = update(gen) }),
44
- Property.init("choices", .{ .method = choices(gen) }),
45
- };
46
- assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
47
- return object;
48
- }
49
-
50
- fn update(gen: anytype) c.napi_callback {
51
- return struct {
52
- fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
53
- var argc: usize = 4;
54
- var argv: [4]c.napi_value = undefined;
55
- assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
56
- assert(argc == 4);
57
-
58
- var data: ?*anyopaque = undefined;
59
- var len: usize = 0;
60
- assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
61
- assert(len == @sizeOf(gen.Battle(gen.PRNG)));
62
- assert(data != null);
63
-
64
- var aligned = @alignCast(@alignOf(*gen.Battle(gen.PRNG)), data.?);
65
- var battle = @ptrCast(*gen.Battle(gen.PRNG), aligned);
66
- const c1 = @bitCast(pkmn.Choice, Number.get(env, argv[1], u8));
67
- const c2 = @bitCast(pkmn.Choice, Number.get(env, argv[2], u8));
68
-
69
- var vtype: c.napi_valuetype = undefined;
70
- assert(c.napi_typeof(env, argv[3], &vtype) == c.napi_ok);
71
- const result = switch (vtype) {
72
- c.napi_undefined, c.napi_null => battle.update(c1, c2, pkmn.protocol.NULL),
73
- else => result: {
74
- assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
75
- assert(len == gen.LOGS_SIZE);
76
- assert(data != null);
77
-
78
- var buf = @ptrCast([*]u8, data.?)[0..gen.LOGS_SIZE];
79
- var stream = pkmn.protocol.ByteStream{ .buffer = buf };
80
- var log = pkmn.protocol.FixedLog{ .writer = stream.writer() };
81
- break :result battle.update(c1, c2, log);
82
- },
83
- } catch unreachable;
84
-
85
- return Number.init(env, @bitCast(u8, result));
86
- }
87
- }.call;
88
- }
89
-
90
- fn choices(gen: anytype) c.napi_callback {
91
- return struct {
92
- fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
93
- var argc: usize = 4;
94
- var argv: [4]c.napi_value = undefined;
95
- assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
96
- assert(argc == 4);
97
-
98
- var data: ?*anyopaque = undefined;
99
- var len: usize = 0;
100
- assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
101
- assert(len == @sizeOf(gen.Battle(gen.PRNG)));
102
- assert(data != null);
103
-
104
- var aligned = @alignCast(@alignOf(*gen.Battle(gen.PRNG)), data.?);
105
- var battle = @ptrCast(*gen.Battle(gen.PRNG), aligned);
106
-
107
- const player = @intToEnum(pkmn.Player, Number.get(env, argv[1], u8));
108
- const request = @intToEnum(pkmn.Choice.Type, Number.get(env, argv[2], u8));
109
-
110
- assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
111
- assert(len == gen.OPTIONS_SIZE);
112
- assert(data != null);
113
-
114
- var out = @ptrCast([*]pkmn.Choice, data.?)[0..gen.OPTIONS_SIZE];
115
- const n = battle.choices(player, request, out);
116
- return Number.init(env, @bitCast(u8, n));
117
- }
118
- }.call;
119
- }
120
-
121
- const Array = struct {
122
- fn init(env: c.napi_env, o: struct { length: ?usize }) c.napi_value {
123
- var result: c.napi_value = undefined;
124
- assert(c.napi_ok == if (o.length) |n|
125
- c.napi_create_array_with_length(env, n, &result)
126
- else
127
- c.napi_create_array(env, &result));
128
- return result;
129
- }
130
-
131
- fn set(env: c.napi_env, array: c.napi_value, index: u32, value: c.napi_value) void {
132
- assert(c.napi_set_element(env, array, index, value) == c.napi_ok);
133
- }
134
- };
135
-
136
- const Boolean = struct {
137
- fn init(env: c.napi_env, value: bool) c.napi_value {
138
- var result: c.napi_value = undefined;
139
- assert(c.napi_get_boolean(env, value, &result) == c.napi_ok);
140
- return result;
141
- }
142
- };
143
-
144
- const Number = struct {
145
- fn init(env: c.napi_env, value: anytype) c.napi_value {
146
- const T = @TypeOf(value);
147
- var result: c.napi_value = undefined;
148
- assert(c.napi_ok == switch (@typeInfo(T)) {
149
- .Int => |info| switch (info.bits) {
150
- 0...32 => switch (info.signedness) {
151
- .signed => c.napi_create_int32(env, @as(i32, value), &result),
152
- .unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
153
- },
154
- 33...52 => c.napi_create_int64(env, @as(i64, value), &result),
155
- else => @compileError("int can't be represented as JS number"),
156
- },
157
- else => @compileError("expected number, got: " ++ @typeName(T)),
158
- });
159
- return result;
160
- }
161
-
162
- fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
163
- switch (@typeInfo(T)) {
164
- .Int => |info| switch (info.bits) {
165
- 0...32 => switch (info.signedness) {
166
- .signed => {
167
- var result: i32 = undefined;
168
- assert(c.napi_get_value_int32(env, value, &result) == c.napi_ok);
169
- return if (info.bits == 32) result else @intCast(T, result);
170
- },
171
- .unsigned => {
172
- var result: u32 = undefined;
173
- assert(c.napi_get_value_uint32(env, value, &result) == c.napi_ok);
174
- return if (info.bits == 32) result else @intCast(T, result);
175
- },
176
- },
177
- 33...63 => {
178
- var result: i64 = undefined;
179
- assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
180
- return @intCast(T, result);
181
- },
182
- else => {
183
- var result: i64 = undefined;
184
- assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
185
- return switch (info.signedness) {
186
- .signed => @as(T, value),
187
- .unsigned => if (0 <= value) @intCast(T, value) else unreachable,
188
- };
189
- },
190
- },
191
- else => @compileError("expected number, got: " ++ @typeName(T)),
192
- }
193
- }
194
- };
195
-
196
- const Object = struct {
197
- fn init(env: c.napi_env) c.napi_value {
198
- var result: c.napi_value = undefined;
199
- assert(c.napi_create_object(env, &result) == c.napi_ok);
200
- return result;
201
- }
202
- };
203
-
204
- const Property = union(enum) {
205
- method: c.napi_callback,
206
- value: c.napi_value,
207
-
208
- fn init(comptime name: [:0]const u8, property: Property) c.napi_property_descriptor {
209
- return .{
210
- .utf8name = name,
211
- .name = null,
212
- .method = switch (property) {
213
- .method => |m| m,
214
- .value => null,
215
- },
216
- .getter = null,
217
- .setter = null,
218
- .value = switch (property) {
219
- .method => null,
220
- .value => |v| v,
221
- },
222
- .attributes = switch (property) {
223
- .method => c.napi_default,
224
- .value => c.napi_enumerable,
225
- },
226
- .data = null,
227
- };
228
- }
229
- };
@@ -1,10 +0,0 @@
1
- const std = @import("std");
2
- const pkmn = @import("../pkmn.zig");
3
-
4
- export const SHOWDOWN = pkmn.options.showdown;
5
- export const TRACE = pkmn.options.trace;
6
-
7
- export const GEN1_OPTIONS_SIZE =
8
- std.math.ceilPowerOfTwo(u32, @intCast(u32, pkmn.gen1.OPTIONS_SIZE)) catch unreachable;
9
- export const GEN1_LOGS_SIZE =
10
- std.math.ceilPowerOfTwo(u32, @intCast(u32, pkmn.gen1.LOGS_SIZE)) catch unreachable;