@pkmn/engine 0.1.0-dev.ce4c0b2a → 0.1.0-dev.d127cb34

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 (73) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +23 -15
  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 +25 -5
  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/data.js.map +1 -1
  17. package/build/pkg/gen1.d.ts +15 -18
  18. package/build/pkg/gen1.js +28 -19
  19. package/build/pkg/gen1.js.map +1 -1
  20. package/build/pkg/index.d.ts +37 -100
  21. package/build/pkg/index.js +23 -75
  22. package/build/pkg/index.js.map +1 -1
  23. package/build/pkg/protocol.d.ts +11 -2
  24. package/build/pkg/protocol.js.map +1 -1
  25. package/build/tools/debug.js +56 -13
  26. package/build/tools/debug.js.map +1 -1
  27. package/build/tools/display/debug.jsx +19 -81
  28. package/build/tools/display/debug.jsx.map +1 -1
  29. package/build/tools/display/index.js +35 -15
  30. package/build/tools/display/index.js.map +1 -1
  31. package/build/tools/display/ui.jsx +27 -16
  32. package/build/tools/display/ui.jsx.map +1 -1
  33. package/build/tools/display/util.js +141 -2
  34. package/build/tools/display/util.js.map +1 -1
  35. package/build.zig +330 -245
  36. package/package.json +36 -128
  37. package/src/bin/install-pkmn-engine +25 -9
  38. package/src/lib/bindings/c.zig +201 -0
  39. package/src/lib/bindings/node.zig +117 -0
  40. package/src/lib/bindings/wasm.zig +51 -0
  41. package/src/lib/common/array.zig +5 -12
  42. package/src/lib/common/debug.zig +45 -34
  43. package/src/lib/common/js.zig +3 -9
  44. package/src/lib/common/optional.zig +26 -26
  45. package/src/lib/common/options.zig +2 -0
  46. package/src/lib/common/protocol.zig +228 -197
  47. package/src/lib/common/rational.zig +278 -285
  48. package/src/lib/common/rng.zig +1 -3
  49. package/src/lib/common/util.zig +34 -31
  50. package/src/lib/gen1/calc.zig +220 -195
  51. package/src/lib/gen1/chance.zig +184 -108
  52. package/src/lib/gen1/data/moves.zig +2 -4
  53. package/src/lib/gen1/data/species.zig +1 -3
  54. package/src/lib/gen1/data/types.zig +3 -4
  55. package/src/lib/gen1/data.zig +12 -16
  56. package/src/lib/gen1/helpers.zig +35 -40
  57. package/src/lib/gen1/mechanics.zig +147 -127
  58. package/src/lib/gen2/calc.zig +12 -23
  59. package/src/lib/gen2/chance.zig +38 -26
  60. package/src/lib/gen2/data/items.zig +1 -3
  61. package/src/lib/gen2/data/moves.zig +2 -4
  62. package/src/lib/gen2/data/species.zig +1 -3
  63. package/src/lib/gen2/data/types.zig +7 -5
  64. package/src/lib/gen2/data.zig +19 -22
  65. package/src/lib/gen2/generated.zig +18 -23
  66. package/src/lib/gen2/helpers.zig +32 -31
  67. package/src/lib/gen2/mechanics.zig +58 -55
  68. package/src/lib/napi.h +2 -0
  69. package/src/lib/node.zig +4 -121
  70. package/src/lib/pkmn.zig +98 -19
  71. package/src/lib/wasm.zig +9 -9
  72. package/src/tools/display/dom.ts +43 -31
  73. package/src/tools/display/ui.css +12 -9
@@ -1,30 +1,22 @@
1
1
  /** @license MIT modified from Vadim Demedes's https://github.com/vadimdemedes/dom-chef */
2
- type Attributes = JSX.IntrinsicElements['div'];
3
- type DocumentFragmentConstructor = typeof DocumentFragment;
4
- type ElementFunction = (props?: any) => HTMLElement;
5
2
 
6
3
  declare global {
7
4
  namespace JSX {
8
- interface Element extends HTMLElement, DocumentFragment {
9
- addEventListener: HTMLElement['addEventListener'];
10
- removeEventListener: HTMLElement['removeEventListener'];
11
- className: HTMLElement['className'];
5
+ type Element = HTMLElement | DocumentFragment;
6
+ interface IntrinsicElements {
7
+ [elemName: string]: Record<string, unknown>;
12
8
  }
13
9
  }
14
10
  }
15
11
 
16
- interface JSXElementClassDocumentFragment extends DocumentFragment, JSX.ElementClass { }
17
- interface Fragment {
18
- prototype: JSXElementClassDocumentFragment;
19
- new(): JSXElementClassDocumentFragment;
20
- }
12
+ type Child = Node | string | number | boolean | null | undefined | Child[];
13
+
14
+ type DocumentFragmentFunction = (props?: any) => DocumentFragment;
15
+ type ElementFunction = (props?: any) => HTMLElement;
21
16
 
22
17
  // https://github.com/preactjs/preact/blob/1bbd687c/src/constants.js#L3
23
18
  const IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|itera/i;
24
19
 
25
- const isFragment = (type: DocumentFragmentConstructor | ElementFunction):
26
- type is DocumentFragmentConstructor => type === DocumentFragment;
27
-
28
20
  const setCSSProps = (element: HTMLElement, style: CSSStyleDeclaration) => {
29
21
  for (const [name, value] of Object.entries(style)) {
30
22
  if (name.startsWith('-')) {
@@ -41,14 +33,14 @@ const setAttribute = (element: HTMLElement, name: string, value: string) => {
41
33
  if (value !== undefined && value !== null) element.setAttribute(name, value);
42
34
  };
43
35
 
44
- const addChildren = (parent: Element | DocumentFragment, children: Node[]) => {
36
+ const addChildren = (parent: Node, children: Child[]) => {
45
37
  for (const child of children) {
46
38
  if (child instanceof Node) {
47
39
  parent.appendChild(child);
48
40
  } else if (Array.isArray(child)) {
49
41
  addChildren(parent, child);
50
42
  } else if (typeof child !== 'boolean' && typeof child !== 'undefined' && child !== null) {
51
- parent.appendChild(document.createTextNode(child));
43
+ parent.appendChild(document.createTextNode(String(child)));
52
44
  }
53
45
  }
54
46
  };
@@ -56,13 +48,33 @@ const addChildren = (parent: Element | DocumentFragment, children: Node[]) => {
56
48
  // https://github.com/facebook/react/blob/3f899089/packages/react-dom/src/shared/DOMProperty.js#L288-L322
57
49
  const FALSIFIABLE_ATTRIBUTES = ['contentEditable', 'draggable', 'spellCheck', 'value'];
58
50
 
59
- export const h = (
60
- type: DocumentFragmentConstructor | ElementFunction | string,
61
- attributes?: Attributes,
62
- ...children: Node[]
63
- ) => {
51
+ export function h<K extends keyof HTMLElementTagNameMap>(
52
+ type: K,
53
+ attributes?: any,
54
+ ...children: Child[]
55
+ ): HTMLElementTagNameMap[K];
56
+ export function h(
57
+ type: DocumentFragmentFunction,
58
+ attributes?: any,
59
+ ...children: Child[]
60
+ ): DocumentFragment;
61
+ export function h<T extends HTMLElement>(
62
+ type: (props: any) => T,
63
+ attributes?: any,
64
+ ...children: Child[]
65
+ ): T;
66
+ export function h(
67
+ type: string,
68
+ attributes?: any,
69
+ ...children: Child[]
70
+ ): HTMLElement;
71
+ export function h(
72
+ type: DocumentFragmentFunction | ElementFunction | string,
73
+ attributes?: any,
74
+ ...children: Child[]
75
+ ): JSX.Element {
64
76
  if (typeof type !== 'string') {
65
- const element = isFragment(type) ? document.createDocumentFragment() : type(attributes);
77
+ const element = type(attributes);
66
78
  addChildren(element, children);
67
79
  return element;
68
80
  }
@@ -78,19 +90,19 @@ export const h = (
78
90
  const existingClassname = element.getAttribute('class') ?? '';
79
91
  setAttribute(element, 'class', (existingClassname + ' ' + String(value)).trim());
80
92
  } else if (name === 'style') {
81
- setCSSProps(element, value);
93
+ setCSSProps(element, value as CSSStyleDeclaration);
82
94
  } else if (name.startsWith('on')) {
83
95
  const eventName = name.slice(2).toLowerCase().replace(/^-/, '');
84
- element.addEventListener(eventName, value);
85
- } else if (name === 'dangerouslySetInnerHTML' && '__html' in value) {
86
- element.innerHTML = value.__html;
96
+ element.addEventListener(eventName, value as EventListenerOrEventListenerObject);
97
+ } else if (name === 'dangerouslySetInnerHTML' && value && (value as any).__html) {
98
+ element.innerHTML = (value as any).__html;
87
99
  } else if (name !== 'key' && (FALSIFIABLE_ATTRIBUTES.includes(name) || value !== false)) {
88
- setAttribute(element, name, value === true ? '' : value);
100
+ setAttribute(element, name, value === true ? '' : String(value));
89
101
  }
90
102
  }
91
103
 
92
104
  return element;
93
- };
105
+ }
94
106
 
95
- export const Fragment =
96
- (typeof DocumentFragment === 'function' ? DocumentFragment : () => {}) as Fragment;
107
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
108
+ export const Fragment = (unused?: any) => document.createDocumentFragment();
@@ -1,3 +1,11 @@
1
+ body {
2
+ background: white;
3
+ color: black;
4
+ }
5
+ code {
6
+ background: white;
7
+ color: black;
8
+ }
1
9
  #content {
2
10
  font-family: "Roboto", "Helvetica Neue", "Helvetica", "Arial", sans-serif;
3
11
  margin: 4em auto;
@@ -233,17 +241,15 @@ th, td {
233
241
  text-align: center;
234
242
  width: 100%;
235
243
  }
244
+ .boosts {
245
+ margin: 0 5px;
246
+ }
236
247
  .types {
237
248
  display: inherit;
238
249
  }
239
250
  .disabled {
240
251
  color: #AAAAAA;
241
252
  }
242
- .log {
243
- display: flex;
244
- justify-content: center;
245
- padding-bottom: 2em;
246
- }
247
253
  .position {
248
254
  position: absolute;
249
255
  right: 10px;
@@ -278,9 +284,6 @@ th, td {
278
284
  justify-content: center;
279
285
  padding: 0;
280
286
  }
281
- .boosts {
282
- margin-left: 5px;
283
- }
284
287
  .moves ul {
285
288
  grid-template-columns: repeat(2, auto);
286
289
  }
@@ -320,7 +323,7 @@ th, td {
320
323
  .boosts {
321
324
  display: flex;
322
325
  justify-content: space-between;
323
- margin-top: 0.5em;
326
+ margin: 0.5em 0 0 0;
324
327
  }
325
328
  }
326
329
  @media(max-width: 550px) {