@pkmn/engine 0.1.0-dev.0cabf16e → 0.1.0-dev.0f9e9648

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 (66) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +47 -4
  3. package/build/pkg/addon/browser.d.ts +1 -1
  4. package/build/pkg/addon/browser.js +6 -2
  5. package/build/pkg/addon/browser.js.map +1 -1
  6. package/build/pkg/addon/node.js +22 -8
  7. package/build/pkg/addon/node.js.map +1 -1
  8. package/build/pkg/addon/wasm.d.ts +2 -0
  9. package/build/pkg/addon/wasm.js +53 -0
  10. package/build/pkg/addon/wasm.js.map +1 -0
  11. package/build/pkg/addon.d.ts +24 -4
  12. package/build/pkg/addon.js +8 -17
  13. package/build/pkg/addon.js.map +1 -1
  14. package/build/pkg/data/layout.json +15 -2
  15. package/build/pkg/data/protocol.json +23 -9
  16. package/build/pkg/gen1.d.ts +15 -18
  17. package/build/pkg/gen1.js +28 -19
  18. package/build/pkg/gen1.js.map +1 -1
  19. package/build/pkg/index.d.ts +37 -100
  20. package/build/pkg/index.js +23 -75
  21. package/build/pkg/index.js.map +1 -1
  22. package/build/tools/debug.js +18 -9
  23. package/build/tools/debug.js.map +1 -1
  24. package/build/tools/display/debug.jsx +18 -7
  25. package/build/tools/display/debug.jsx.map +1 -1
  26. package/build/tools/display/index.js +35 -15
  27. package/build/tools/display/index.js.map +1 -1
  28. package/build/tools/display/ui.jsx +17 -7
  29. package/build/tools/display/ui.jsx.map +1 -1
  30. package/build/tools/display/util.js +16 -1
  31. package/build/tools/display/util.js.map +1 -1
  32. package/build.zig +196 -84
  33. package/package.json +29 -127
  34. package/src/bin/install-pkmn-engine +3 -2
  35. package/src/lib/bindings/c.zig +255 -0
  36. package/src/lib/bindings/node.zig +121 -0
  37. package/src/lib/bindings/wasm.zig +59 -0
  38. package/src/lib/common/array.zig +1 -1
  39. package/src/lib/common/optional.zig +1 -1
  40. package/src/lib/common/options.zig +2 -0
  41. package/src/lib/common/protocol.zig +4 -8
  42. package/src/lib/common/rational.zig +222 -147
  43. package/src/lib/common/rng.zig +1 -3
  44. package/src/lib/common/util.zig +17 -2
  45. package/src/lib/gen1/calc.zig +87 -97
  46. package/src/lib/gen1/chance.zig +34 -18
  47. package/src/lib/gen1/data/moves.zig +2 -4
  48. package/src/lib/gen1/data/species.zig +1 -3
  49. package/src/lib/gen1/data/types.zig +1 -2
  50. package/src/lib/gen1/data.zig +7 -11
  51. package/src/lib/gen1/helpers.zig +4 -11
  52. package/src/lib/gen1/mechanics.zig +66 -50
  53. package/src/lib/gen2/calc.zig +10 -18
  54. package/src/lib/gen2/data/items.zig +1 -3
  55. package/src/lib/gen2/data/moves.zig +2 -4
  56. package/src/lib/gen2/data/species.zig +1 -3
  57. package/src/lib/gen2/data/types.zig +1 -2
  58. package/src/lib/gen2/data.zig +11 -15
  59. package/src/lib/gen2/generated.zig +18 -23
  60. package/src/lib/gen2/helpers.zig +5 -12
  61. package/src/lib/gen2/mechanics.zig +41 -38
  62. package/src/lib/node.zig +3 -116
  63. package/src/lib/pkmn.zig +7 -2
  64. package/src/lib/wasm.zig +13 -9
  65. package/src/tools/display/dom.ts +5 -8
  66. package/src/tools/display/ui.css +0 -5
package/src/lib/node.zig CHANGED
@@ -1,8 +1,6 @@
1
- const std = @import("std");
2
-
3
- const pkmn = @import("pkmn.zig");
4
-
5
1
  const js = @import("common/js.zig");
2
+ const node = @import("bindings/node.zig");
3
+ const std = @import("std");
6
4
 
7
5
  const assert = std.debug.assert;
8
6
 
@@ -13,119 +11,8 @@ const c = @cImport({
13
11
 
14
12
  export fn napi_register_module_v1(env: c.napi_env, exports: c.napi_value) c.napi_value {
15
13
  const properties = [_]c.napi_property_descriptor{
16
- js.Property.init("engine", .{ .value = register(env) }),
14
+ js.Property.init("engine", .{ .value = node.register(env) }),
17
15
  };
18
16
  assert(c.napi_define_properties(env, exports, properties.len, &properties) == c.napi_ok);
19
17
  return exports;
20
18
  }
21
-
22
- pub fn register(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("options", .{ .value = options(env) }),
26
- js.Property.init("bindings", .{ .value = bindings(env) }),
27
- };
28
- assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
29
- return object;
30
- }
31
-
32
- fn options(env: c.napi_env) c.napi_value {
33
- const object = js.Object.init(env);
34
- const properties = [_]c.napi_property_descriptor{
35
- js.Property.init("showdown", .{ .value = js.Boolean.init(env, pkmn.options.showdown) }),
36
- js.Property.init("log", .{ .value = js.Boolean.init(env, pkmn.options.log) }),
37
- js.Property.init("chance", .{ .value = js.Boolean.init(env, pkmn.options.chance) }),
38
- js.Property.init("calc", .{ .value = js.Boolean.init(env, pkmn.options.calc) }),
39
- };
40
- assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
41
- return object;
42
- }
43
-
44
- fn bindings(env: c.napi_env) c.napi_value {
45
- const array = js.Array.init(env, .{ .length = 1 });
46
- js.Array.set(env, array, 0, bind(env, pkmn.gen1));
47
- return array;
48
- }
49
-
50
- fn bind(env: c.napi_env, gen: anytype) c.napi_value {
51
- const choices_size: u32 = @intCast(gen.CHOICES_SIZE);
52
- const logs_size: u32 = @intCast(gen.LOGS_SIZE);
53
- const object = js.Object.init(env);
54
- const properties = [_]c.napi_property_descriptor{
55
- js.Property.init("CHOICES_SIZE", .{ .value = js.Number.init(env, choices_size) }),
56
- js.Property.init("LOGS_SIZE", .{ .value = js.Number.init(env, logs_size) }),
57
- js.Property.init("update", .{ .method = update(gen) }),
58
- js.Property.init("choices", .{ .method = choices(gen) }),
59
- };
60
- assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
61
- return object;
62
- }
63
-
64
- fn update(gen: anytype) c.napi_callback {
65
- return struct {
66
- fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
67
- var argc: usize = 4;
68
- var argv: [4]c.napi_value = undefined;
69
- assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
70
- assert(argc == 4);
71
-
72
- var data: ?*anyopaque = undefined;
73
- var len: usize = 0;
74
- assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
75
- assert(len == @sizeOf(gen.Battle(gen.PRNG)));
76
-
77
- var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
78
- const c1: pkmn.Choice = @bitCast(js.Number.get(env, argv[1], u8));
79
- const c2: pkmn.Choice = @bitCast(js.Number.get(env, argv[2], u8));
80
-
81
- var vtype: c.napi_valuetype = undefined;
82
- assert(c.napi_typeof(env, argv[3], &vtype) == c.napi_ok);
83
- const result = switch (vtype) {
84
- c.napi_undefined, c.napi_null => battle.update(c1, c2, &gen.NULL),
85
- else => result: {
86
- assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
87
- assert(len == gen.LOGS_SIZE);
88
-
89
- const buf = @as([*]u8, @ptrCast(data.?))[0..gen.LOGS_SIZE];
90
- var stream: pkmn.protocol.ByteStream = .{ .buffer = buf };
91
- // TODO: extract out
92
- var opts = pkmn.battle.options(
93
- pkmn.protocol.FixedLog{ .writer = stream.writer() },
94
- gen.chance.NULL,
95
- gen.calc.NULL,
96
- );
97
- break :result battle.update(c1, c2, &opts);
98
- },
99
- } catch unreachable;
100
-
101
- return js.Number.init(env, @as(u8, @bitCast(result)));
102
- }
103
- }.call;
104
- }
105
-
106
- fn choices(gen: anytype) c.napi_callback {
107
- return struct {
108
- fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
109
- var argc: usize = 4;
110
- var argv: [4]c.napi_value = undefined;
111
- assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
112
- assert(argc == 4);
113
-
114
- var data: ?*anyopaque = undefined;
115
- var len: usize = 0;
116
- assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
117
- assert(len == @sizeOf(gen.Battle(gen.PRNG)));
118
-
119
- var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
120
- const player: pkmn.Player = @enumFromInt(js.Number.get(env, argv[1], u8));
121
- const request: pkmn.Choice.Type = @enumFromInt(js.Number.get(env, argv[2], u8));
122
-
123
- assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
124
- assert(len == gen.CHOICES_SIZE);
125
-
126
- const out = @as([*]pkmn.Choice, @ptrCast(data.?))[0..gen.CHOICES_SIZE];
127
- const n = battle.choices(player, request, out);
128
- return js.Number.init(env, @as(u8, @bitCast(n)));
129
- }
130
- }.call;
131
- }
package/src/lib/pkmn.zig CHANGED
@@ -96,6 +96,10 @@ pub const gen1 = struct {
96
96
  /// though damage calculator support should additionally be turned off
97
97
  /// entirely via `options.calc`.
98
98
  pub const NULL = @import("gen1/calc.zig").NULL;
99
+ /// TODO
100
+ pub const MAX_FRONTIER = @import("gen1/calc.zig").MAX_FRONTIER;
101
+ /// TODO
102
+ pub const Rolls = @import("gen1/calc.zig").Rolls;
99
103
  };
100
104
  /// Provides helpers for initializing Generation I Pokémon battles.
101
105
  pub const helpers = @import("gen1/helpers.zig");
@@ -125,6 +129,7 @@ pub const gen2 = struct {
125
129
  // NOTE: `pub usingnamespace struct { ... }` here results in a (false?) dependency loop
126
130
  pub const js = if (options.internal) @import("common/js.zig") else {};
127
131
  pub const bindings = if (options.internal) struct {
128
- pub const node = @import("node.zig");
129
- pub const wasm = @import("wasm.zig");
132
+ pub const c = @import("bindings/c.zig");
133
+ pub const node = @import("bindings/node.zig");
134
+ pub const wasm = @import("bindings/wasm.zig");
130
135
  } else {};
package/src/lib/wasm.zig CHANGED
@@ -1,13 +1,17 @@
1
1
  const std = @import("std");
2
+ const wasm = @import("bindings/wasm.zig");
2
3
 
3
- const pkmn = @import("pkmn.zig");
4
+ export const SHOWDOWN = wasm.options.showdown;
5
+ export const LOG = wasm.options.log;
6
+ export const CHANCE = wasm.options.chance;
7
+ export const CALC = wasm.options.calc;
4
8
 
5
- export const SHOWDOWN = pkmn.options.showdown;
6
- export const LOG = pkmn.options.log;
7
- export const CHANCE = pkmn.options.chance;
8
- export const CALC = pkmn.options.calc;
9
+ export const GEN1_CHOICES_SIZE = wasm.gen(1).CHOICES_SIZE;
10
+ export const GEN1_LOGS_SIZE = wasm.gen(1).LOGS_SIZE;
9
11
 
10
- export const GEN1_CHOICES_SIZE =
11
- std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(pkmn.gen1.CHOICES_SIZE))) catch unreachable;
12
- export const GEN1_LOGS_SIZE =
13
- std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(pkmn.gen1.LOGS_SIZE))) catch unreachable;
12
+ const exportable = @hasDecl(std.zig, "Zir") and !@hasField(std.zig.Zir.Inst.Tag, "export_value");
13
+
14
+ usingnamespace if (exportable) struct {
15
+ export const GEN1_update = wasm.gen(1).update;
16
+ export const GEN1_choices = wasm.gen(1).choices;
17
+ } else wasm.exports();
@@ -1,6 +1,6 @@
1
1
  /** @license MIT modified from Vadim Demedes's https://github.com/vadimdemedes/dom-chef */
2
2
  type Attributes = JSX.IntrinsicElements['div'];
3
- type DocumentFragmentConstructor = typeof DocumentFragment;
3
+ type DocumentFragmentFunction = (props?: any) => DocumentFragment;
4
4
  type ElementFunction = (props?: any) => HTMLElement;
5
5
 
6
6
  declare global {
@@ -22,9 +22,6 @@ interface Fragment {
22
22
  // https://github.com/preactjs/preact/blob/1bbd687c/src/constants.js#L3
23
23
  const IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|itera/i;
24
24
 
25
- const isFragment = (type: DocumentFragmentConstructor | ElementFunction):
26
- type is DocumentFragmentConstructor => type === DocumentFragment;
27
-
28
25
  const setCSSProps = (element: HTMLElement, style: CSSStyleDeclaration) => {
29
26
  for (const [name, value] of Object.entries(style)) {
30
27
  if (name.startsWith('-')) {
@@ -57,12 +54,12 @@ const addChildren = (parent: Element | DocumentFragment, children: Node[]) => {
57
54
  const FALSIFIABLE_ATTRIBUTES = ['contentEditable', 'draggable', 'spellCheck', 'value'];
58
55
 
59
56
  export const h = (
60
- type: DocumentFragmentConstructor | ElementFunction | string,
57
+ type: DocumentFragmentFunction | ElementFunction | string,
61
58
  attributes?: Attributes,
62
59
  ...children: Node[]
63
60
  ) => {
64
61
  if (typeof type !== 'string') {
65
- const element = isFragment(type) ? document.createDocumentFragment() : type(attributes);
62
+ const element = type(attributes);
66
63
  addChildren(element, children);
67
64
  return element;
68
65
  }
@@ -92,5 +89,5 @@ export const h = (
92
89
  return element;
93
90
  };
94
91
 
95
- export const Fragment =
96
- (typeof DocumentFragment === 'function' ? DocumentFragment : () => {}) as Fragment;
92
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
93
+ export const Fragment = (unused: any) => document.createDocumentFragment();
@@ -250,11 +250,6 @@ th, td {
250
250
  .disabled {
251
251
  color: #AAAAAA;
252
252
  }
253
- .log {
254
- display: flex;
255
- justify-content: center;
256
- padding-bottom: 2em;
257
- }
258
253
  .position {
259
254
  position: absolute;
260
255
  right: 10px;