@primitiv/core 0.19.0
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.
- package/LICENSE.txt +190 -0
- package/README.md +9 -0
- package/dist/index.cjs +4 -0
- package/dist/index.d.ts +2421 -0
- package/dist/index.mjs +4 -0
- package/package.json +64 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding any notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2026 Thomas Piquet (THP Software)
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# @primitiv/core
|
|
2
|
+
|
|
3
|
+
> **Internal package** - This is an internal module of the [Primitiv Engine](https://primitivengine.com/).
|
|
4
|
+
|
|
5
|
+
For documentation, examples, and guides, visit **[primitivengine.com](https://primitivengine.com/)**.
|
|
6
|
+
|
|
7
|
+
## 📄 License
|
|
8
|
+
|
|
9
|
+
Apache-2.0 - See [LICENSE.txt](./LICENSE.txt).
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
"use strict";var Be=Object.defineProperty;var tt=Object.getOwnPropertyDescriptor;var rt=Object.getOwnPropertyNames;var it=Object.prototype.hasOwnProperty;var nt=(l,e)=>{for(var t in e)Be(l,t,{get:e[t],enumerable:!0})},st=(l,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of rt(e))!it.call(l,i)&&i!==t&&Be(l,i,{get:()=>e[i],enumerable:!(r=tt(e,i))||r.enumerable});return l};var ot=l=>st(Be({},"__esModule",{value:!0}),l);var At={};nt(At,{BinaryReader:()=>H,COLOR_SKIP:()=>f,CellStruct:()=>oe,Display:()=>le,DisplayCompositor:()=>Le,Engine:()=>De,EngineStats:()=>ge,EventEmitter:()=>ze,FontType:()=>Ye,Grid:()=>z,ImageFont:()=>be,ImageFontRegistry:()=>he,InputBindingRegistry:()=>X,LAYER_SIZE:()=>Pt,Layer:()=>de,LayerRasterizer:()=>ae,MacroRegistry:()=>pe,OrderBuilder:()=>Ve,OrderDecoder:()=>ie,OrderEncoder:()=>_,SoundRegistry:()=>me,SpriteRegistry:()=>ce,TickPacketDecoder:()=>Se,TickPacketEncoder:()=>Y,UNICODE_TO_CP437:()=>Fe,User:()=>ue,UserUpdateBuilder:()=>ke,UserUpdateParser:()=>Ae,cp437ToUnicode:()=>mt,cp437ToUnicodeCodepoint:()=>$e,decodeCompressedInput:()=>ct,encodeCompressedInput:()=>ut,encodeCp437String:()=>ft,getAtlasColumns:()=>gt,getAtlasDimensions:()=>Pe,getAtlasLayout:()=>K,getAtlasRows:()=>yt,getMaxCharCode:()=>Ze,hasCP437Mapping:()=>pt,nextPow2:()=>G,stringToCp437:()=>ht,unicodeToCp437:()=>D});module.exports=ot(At);var f=255,Pt=256;var z=class l{width;height;size;data;constructor(e,t){this.width=e,this.height=t,this.size=e*t,this.data=new Uint32Array(this.size)}clear(){this.data.fill(255<<24|255<<16|0)}fill(e,t,r){let i=r<<24|t<<16|e;this.data.fill(i)}setSafe(e,t,r,i,n){e<0||e>=this.width||t<0||t>=this.height||(this.data[t*this.width+e]=n<<24|i<<16|r)}set(e,t,r,i){this.data[e]=i<<24|r<<16|t}setPacked(e,t){this.data[e]=t}get(e){return this.data[e]}unpack(e){let t=this.data[e];return{char:t&65535,fg:t>>>16&255,bg:t>>>24&255}}clone(){let e=new l(this.width,this.height);return e.data.set(this.data),e}};var oe=class{static pack(e,t,r){return r<<24|t<<16|e}static getChar(e){return e&65535}static getFg(e){return e>>>16&255}static getBg(e){return e>>>24&255}};var S=require("@primitiv/types"),ae=class{rasterize(e,t,r){for(let i of t)this.executeOrder(e,i,r)}executeOrder(e,t,r){switch(t.type){case S.OrderType.Char:this.drawChar(e,t);break;case S.OrderType.Text:this.drawText(e,t);break;case S.OrderType.TextMultiline:this.drawTextMultiline(e,t);break;case S.OrderType.SubFrame:this.drawSubFrame(e,t);break;case S.OrderType.SubFrameMulti:this.drawSubFrameMulti(e,t);break;case S.OrderType.FullFrame:this.drawFullFrame(e,t);break;case S.OrderType.FullFrameMulti:this.drawFullFrameMulti(e,t);break;case S.OrderType.ColorMap:this.drawColorMap(e,t);break;case S.OrderType.DotCloud:this.drawDotCloud(e,t);break;case S.OrderType.DotCloudMulti:this.drawDotCloudMulti(e,t);break;case S.OrderType.SpriteCloud:this.drawSpriteCloud(e,t,r);break;case S.OrderType.SpriteCloudMulti:this.drawSpriteCloudMulti(e,t,r);break;case S.OrderType.SpriteCloudVaried:this.drawSpriteCloudVaried(e,t,r);break;case S.OrderType.SpriteCloudVariedMulti:this.drawSpriteCloudVariedMulti(e,t,r);break;case S.OrderType.Bitmask:this.drawBitmask(e,t);break;case S.OrderType.Bitmask4:this.drawBitmask4(e,t);break;case S.OrderType.Bitmask16:this.drawBitmask16(e,t);break;case S.OrderType.Polyline:this.drawPolyline(e,t);break;case S.OrderType.Fill:this.drawFill(e,t);break;case S.OrderType.FillChar:this.drawFillChar(e,t);break;case S.OrderType.FillSprite:this.drawFillSprite(e,t,r);break;case S.OrderType.FillSpriteMulti:this.drawFillSpriteMulti(e,t,r);break;case S.OrderType.Sprite:this.drawSprite(e,t,r);break;case S.OrderType.SpriteMulti:this.drawSpriteMulti(e,t,r);break;case S.OrderType.Shape:this.drawShape(e,t);break}}drawChar(e,t){let{x:r,y:i,char:n,fg:s,bg:o}=t;e.setSafe(r,i,n,s,o)}drawText(e,t){let{x:r,y:i,text:n,fg:s,bg:o}=t;if(typeof n=="string")for(let d=0;d<n.length;d++){let u=n.charCodeAt(d);e.setSafe(r+d,i,u,s,o)}}drawTextMultiline(e,t){let{x:r,y:i,text:n,fg:s,bg:o}=t;if(typeof n!="string")return;let d=n.split(`
|
|
2
|
+
`);for(let u=0;u<d.length;u++){let a=d[u];for(let c=0;c<a.length;c++){let b=a.charCodeAt(c);e.setSafe(r+c,i+u,b,s,o)}}}drawSubFrame(e,t){let{x:r,y:i,width:n,height:s,frame:o,fg:d,bg:u}=t;for(let a=0;a<s;a++)for(let c=0;c<n;c++){let b=a*n+c;b<o.length&&e.setSafe(r+c,i+a,o[b],d,u)}}drawSubFrameMulti(e,t){let{x:r,y:i,width:n,height:s,frame:o}=t;for(let d=0;d<s;d++)for(let u=0;u<n;u++){let a=d*n+u;if(a<o.length){let c=o[a];e.setSafe(r+u,i+d,c.char,c.fg,c.bg)}}}drawFullFrame(e,t){let{frame:r,fg:i,bg:n}=t,s=e.width,o=e.height,d=0;for(let u=0;u<o;u++)for(let a=0;a<s;a++){if(d>=r.length)return;e.setSafe(a,u,r[d],i,n),d++}}drawFullFrameMulti(e,t){let{frame:r}=t,i=e.width,n=e.height,s=0;for(let o=0;o<n;o++)for(let d=0;d<i;d++){if(s>=r.length)return;let u=r[s];e.setSafe(d,o,u.char,u.fg,u.bg),s++}}drawColorMap(e,t){let{x:r,y:i,width:n,height:s,data:o}=t;for(let d=0;d<s;d++)for(let u=0;u<n;u++){let a=d*n+u;if(a<o.length){let c=o[a];if(r+u>=0&&r+u<e.width&&i+d>=0&&i+d<e.height){let b=(i+d)*e.width+(r+u),m=e.data[b]&65535;e.data[b]=c.bg<<24|c.fg<<16|m}}}}drawFill(e,t){let{char:r,fg:i,bg:n,x:s,y:o,width:d,height:u}=t,a=s??0,c=o??0,b=d??e.width,h=u??e.height;if(a===0&&c===0&&b===e.width&&h===e.height){let m=n<<24|i<<16|r&65535;e.data.fill(m);return}for(let m=c;m<c+h;m++)for(let g=a;g<a+b;g++)e.setSafe(g,m,r,i,n)}drawFillChar(e,t){let{patternWidth:r,patternHeight:i,pattern:n,fg:s,bg:o}=t;if(!(r<=0||i<=0))for(let d=0;d<e.height;d++)for(let u=0;u<e.width;u++){let a=u%r,c=d%i,b=n[c*r+a];e.setSafe(u,d,b,s,o)}}drawFillSprite(e,t,r){if(!r)return;let i=r.getUnicolorSprite(t.spriteId);if(i)for(let n=0;n<e.height;n+=i.height)for(let s=0;s<e.width;s+=i.width)for(let o=0;o<i.height;o++)for(let d=0;d<i.width;d++){let u=i.data[o*i.width+d];e.setSafe(s+d,n+o,u,t.fg,t.bg)}}drawFillSpriteMulti(e,t,r){if(!r)return;let i=r.getMulticolorSprite(t.spriteId);if(i)for(let n=0;n<e.height;n+=i.height)for(let s=0;s<e.width;s+=i.width)for(let o=0;o<i.height;o++)for(let d=0;d<i.width;d++){let u=i.data[o*i.width+d],a=s+d,c=n+o;a>=0&&a<e.width&&c>=0&&c<e.height&&(e.data[c*e.width+a]=u)}}drawSprite(e,t,r){if(!r)return;let{x:i,y:n,spriteId:s,fg:o,bg:d}=t,u=r.getUnicolorSprite(s);if(u)for(let a=0;a<u.height;a++)for(let c=0;c<u.width;c++){let b=u.data[a*u.width+c];e.setSafe(i+c,n+a,b,o,d)}}drawSpriteMulti(e,t,r){if(!r)return;let{x:i,y:n,spriteId:s}=t,o=r.getMulticolorSprite(s);if(o)for(let d=0;d<o.height;d++)for(let u=0;u<o.width;u++){let a=o.data[d*o.width+u],c=i+u,b=n+d;c>=0&&c<e.width&&b>=0&&b<e.height&&(e.data[b*e.width+c]=a)}}drawDotCloud(e,t){let{positions:r,char:i,fg:n,bg:s}=t;for(let o of r)e.setSafe(o.x,o.y,i,n,s)}drawDotCloudMulti(e,t){for(let r of t.dots)e.setSafe(r.x,r.y,r.char,r.fg,r.bg)}drawSpriteCloud(e,t,r){if(!r)return;let i=r.getUnicolorSprite(t.spriteId);if(i)for(let n of t.positions)for(let s=0;s<i.height;s++)for(let o=0;o<i.width;o++){let d=i.data[s*i.width+o];e.setSafe(n.x+o,n.y+s,d,t.fg,t.bg)}}drawSpriteCloudMulti(e,t,r){if(!r)return;let i=r.getMulticolorSprite(t.spriteId);if(i)for(let n of t.positions)for(let s=0;s<i.height;s++)for(let o=0;o<i.width;o++){let d=i.data[s*i.width+o],u=n.x+o,a=n.y+s;u>=0&&u<e.width&&a>=0&&a<e.height&&(e.data[a*e.width+u]=d)}}drawSpriteCloudVaried(e,t,r){if(r)for(let i of t.sprites){let n=r.getUnicolorSprite(i.spriteId);if(n)for(let s=0;s<n.height;s++)for(let o=0;o<n.width;o++){let d=n.data[s*n.width+o];e.setSafe(i.x+o,i.y+s,d,i.fg,i.bg)}}}drawSpriteCloudVariedMulti(e,t,r){if(r)for(let i of t.sprites){let n=r.getMulticolorSprite(i.spriteId);if(n)for(let s=0;s<n.height;s++)for(let o=0;o<n.width;o++){let d=n.data[s*n.width+o],u=i.x+o,a=i.y+s;u>=0&&u<e.width&&a>=0&&a<e.height&&(e.data[a*e.width+u]=d)}}}drawBitmask(e,t){let{x:r,y:i,width:n,height:s,mask:o,char:d,fg:u,bg:a,override:c}=t;for(let b=0;b<s;b++)for(let h=0;h<n;h++){let m=b*n+h,g=m>>3,p=m&7;(o[g]&1<<p)!==0?e.setSafe(r+h,i+b,d,u,a):c&&e.setSafe(r+h,i+b,0,255,255)}}drawBitmask4(e,t){let{x:r,y:i,width:n,height:s,mask:o,variants:d,override:u}=t;for(let a=0;a<s;a++)for(let c=0;c<n;c++){let b=a*n+c,h=b>>2,g=(b&3)*2,p=o[h]>>g&3;if(p>0){let y=d[p-1];y&&e.setSafe(r+c,i+a,y.char,y.fg,y.bg)}else u&&e.setSafe(r+c,i+a,0,255,255)}}drawBitmask16(e,t){let{x:r,y:i,width:n,height:s,mask:o,variants:d,override:u}=t;for(let a=0;a<s;a++)for(let c=0;c<n;c++){let b=a*n+c,h=b>>1,g=(b&1)*4,p=o[h]>>g&15;if(p>0){let y=d[p-1];y&&e.setSafe(r+c,i+a,y.char,y.fg,y.bg)}else u&&e.setSafe(r+c,i+a,0,255,255)}}drawPolyline(e,t){let{points:r,char:i,fg:n,bg:s}=t;if(!(r.length<2))for(let o=0;o<r.length-1;o++){let d=r[o],u=r[o+1];this.drawLineBresenham(e,d.x,d.y,u.x,u.y,i,n,s)}}drawLineBresenham(e,t,r,i,n,s,o,d){let u=Math.abs(i-t),a=Math.abs(n-r),c=t<i?1:-1,b=r<n?1:-1,h=u-a;for(;e.setSafe(t,r,s,o,d),!(t===i&&r===n);){let m=2*h;m>-a&&(h-=a,t+=c),m<u&&(h+=u,r+=b)}}drawShape(e,t){switch(t.shapeType){case S.ShapeType.Rectangle:this.drawRectangle(e,t);break;case S.ShapeType.Circle:this.drawCircle(e,t);break;case S.ShapeType.Line:t.x1!==void 0&&t.y1!==void 0&&t.x2!==void 0&&t.y2!==void 0&&this.drawLineBresenham(e,t.x1,t.y1,t.x2,t.y2,t.char,t.fg,t.bg);break;case S.ShapeType.Ellipse:this.drawEllipse(e,t);break;case S.ShapeType.Triangle:this.drawTriangle(e,t);break}}drawRectangle(e,t){let{char:r,fg:i,bg:n,filled:s}=t,o=t.x??0,d=t.y??0,u=t.width??0,a=t.height??0;if(s)for(let c=d;c<d+a;c++)for(let b=o;b<o+u;b++)e.setSafe(b,c,r,i,n);else{for(let c=o;c<o+u;c++)e.setSafe(c,d,r,i,n),e.setSafe(c,d+a-1,r,i,n);for(let c=d+1;c<d+a-1;c++)e.setSafe(o,c,r,i,n),e.setSafe(o+u-1,c,r,i,n)}}drawCircle(e,t){let{char:r,fg:i,bg:n,filled:s}=t,o=t.x??0,d=t.y??0,u=t.radius??0;if(u<=0)return;let a=u,c=0,b=0;for(;a>=c;){if(s){for(let h=o-a;h<=o+a;h++)e.setSafe(h,d+c,r,i,n);for(let h=o-a;h<=o+a;h++)e.setSafe(h,d-c,r,i,n);for(let h=o-c;h<=o+c;h++)e.setSafe(h,d+a,r,i,n);for(let h=o-c;h<=o+c;h++)e.setSafe(h,d-a,r,i,n)}else e.setSafe(o+a,d+c,r,i,n),e.setSafe(o+c,d+a,r,i,n),e.setSafe(o-c,d+a,r,i,n),e.setSafe(o-a,d+c,r,i,n),e.setSafe(o-a,d-c,r,i,n),e.setSafe(o-c,d-a,r,i,n),e.setSafe(o+c,d-a,r,i,n),e.setSafe(o+a,d-c,r,i,n);b<=0&&(c+=1,b+=2*c+1),b>0&&(a-=1,b-=2*a+1)}}drawEllipse(e,t){let{char:r,fg:i,bg:n,filled:s}=t,o=t.x??0,d=t.y??0,u=t.radiusX??t.radius??0,a=t.radiusY??t.radius??0;if(u<=0||a<=0)return;let c=0,b=a,h=a*a-u*u*a+.25*u*u,m=2*a*a*c,g=2*u*u*b;for(;m<g;){if(s)for(let y=o-c;y<=o+c;y++)e.setSafe(y,d+b,r,i,n),e.setSafe(y,d-b,r,i,n);else e.setSafe(o+c,d+b,r,i,n),e.setSafe(o-c,d+b,r,i,n),e.setSafe(o+c,d-b,r,i,n),e.setSafe(o-c,d-b,r,i,n);h<0?(c++,m+=2*a*a,h+=m+a*a):(c++,b--,m+=2*a*a,g-=2*u*u,h+=m-g+a*a)}let p=a*a*((c+.5)*(c+.5))+u*u*((b-1)*(b-1))-u*u*a*a;for(;b>=0;){if(s)for(let y=o-c;y<=o+c;y++)e.setSafe(y,d+b,r,i,n),e.setSafe(y,d-b,r,i,n);else e.setSafe(o+c,d+b,r,i,n),e.setSafe(o-c,d+b,r,i,n),e.setSafe(o+c,d-b,r,i,n),e.setSafe(o-c,d-b,r,i,n);p>0?(b--,g-=2*u*u,p+=u*u-g):(b--,c++,m+=2*a*a,g-=2*u*u,p+=m-g+u*u)}}drawTriangle(e,t){let{char:r,fg:i,bg:n,filled:s}=t,o=t.x1??0,d=t.y1??0,u=t.x2??0,a=t.y2??0,c=t.x3??0,b=t.y3??0;if(!s){this.drawLineBresenham(e,o,d,u,a,r,i,n),this.drawLineBresenham(e,u,a,c,b,r,i,n),this.drawLineBresenham(e,c,b,o,d,r,i,n);return}let h=o,m=d,g=u,p=a,y=c,w=b;if(m>p){let v=h;h=g,g=v,v=m,m=p,p=v}if(m>w){let v=h;h=y,y=v,v=m,m=w,w=v}if(p>w){let v=g;g=y,y=v,v=p,p=w,w=v}let A=w-m;if(A!==0)for(let v=0;v<A;v++){let L=v>p-m||p===m,Z=L?w-p:p-m,N=v/A,E=(v-(L?p-m:0))/Z,R=Math.floor(h+(y-h)*N),F;if(L?F=Math.floor(g+(y-g)*E):F=Math.floor(h+(g-h)*E),R>F){let V=R;R=F,F=V}for(let V=R;V<=F;V++)e.setSafe(V,m+v,r,i,n)}}};var de=class l{id=0;grid;x=0;y=0;origin;zIndex=0;visible=!0;name;isMacroLayer;width;height;mustBeReliable=!1;spriteRegistry;charCodeMode;is16bit;pendingOrders=[];ordersUpdatedThisTick=!1;enabled=!0;needsCommit=!1;static rasterizer=new ae;constructor(e,t,r,i,n){r<1||r>256||i<1||i>256;let s={};typeof n=="boolean"?s={mustBeReliable:n}:n&&(s=n),this.origin=e,this.x=e.x,this.y=e.y,this.zIndex=t,this.name=s.name,this.width=r,this.height=i,this.mustBeReliable=s.mustBeReliable??!1,this.isMacroLayer=s.isMacroLayer??!1,this.charCodeMode=s.charCodeMode??"8bit",this.is16bit=this.charCodeMode==="16bit",this.grid=new z(r,i),this.grid.clear()}setId(e){this.id=e}setSpriteRegistry(e){this.spriteRegistry=e}resize(e,t){this.grid=new z(e,t),this.width=e,this.height=t}getMustBeReliable(){return this.mustBeReliable}getName(){return this.name}getIsMacroLayer(){return this.isMacroLayer}getWidth(){return this.width}getHeight(){return this.height}getOrigin(){return this.origin}setOrigin(e){this.origin=e,this.x=e.x,this.y=e.y,this.needsCommit=!0}setName(e){let t=typeof e=="string"?e.trim():void 0;this.name=t&&t.length>0?t:void 0}setEnabled(e){this.enabled=e,this.visible=e,this.needsCommit=!0}setZIndex(e){this.zIndex=e,this.needsCommit=!0}setMustBeReliable(e){this.mustBeReliable=e}getCellAt(e,t){return e<0||e>=this.width||t<0||t>=this.height?null:this.grid.unpack(t*this.width+e)}setOrders(e){this.pendingOrders=e,this.ordersUpdatedThisTick=!0,this.grid.clear(),l.rasterizer.rasterize(this.grid,e,this.spriteRegistry),this.needsCommit=!0}getPendingOrders(){return this.pendingOrders}clearPendingOrders(){this.pendingOrders.length=0,this.ordersUpdatedThisTick=!1}getOrdersUpdatedThisTick(){return this.ordersUpdatedThisTick}commit(){this.needsCommit=!0}getNeedsCommit(){return this.needsCommit}clearCommit(){this.needsCommit=!1}isEnabled(){return this.enabled}getDebugInfo(){return{id:this.id,name:this.name,zIndex:this.zIndex,origin:{x:this.x,y:this.y},width:this.width,height:this.height,visible:this.visible,enabled:this.enabled,mustBeReliable:this.mustBeReliable,isMacroLayer:this.isMacroLayer,charCodeMode:this.charCodeMode,needsCommit:this.needsCommit,ordersCount:this.pendingOrders.length}}};var Ce=require("@primitiv/types"),le=class{id;x=0;y=0;width;height;cellWidth=8;cellHeight=8;pixelWidth=0;pixelHeight=0;postProcess={};scalingMode=Ce.ScalingMode.None;activePaletteSlot=0;renderPasses;constructor(e,t,r){this.id=e,this.width=t,this.height=r}teleport(e,t){this.x=e,this.y=t}resize(e,t){this.width=e,this.height=t}setSize(e){this.width=e.x,this.height=e.y}setPixelViewport(e,t){this.pixelWidth=e,this.pixelHeight=t}calculateMaxCells(e,t){if(this.pixelWidth<=0||this.pixelHeight<=0)return null;let r=e??this.cellWidth,i=t??this.cellHeight;return r<=0||i<=0?null:{cols:Math.min(256,Math.max(1,Math.floor(this.pixelWidth/r))),rows:Math.min(256,Math.max(1,Math.floor(this.pixelHeight/i)))}}setCellSize(e,t){this.cellWidth=Math.max(1,Math.min(255,Math.round(e))),this.cellHeight=Math.max(1,Math.min(255,Math.round(t)))}getCellSize(){return{cellWidth:this.cellWidth,cellHeight:this.cellHeight}}setScalingMode(e){this.scalingMode=e}setAmbientEffect(e){this.postProcess.ambientEffect||(this.postProcess.ambientEffect={enabled:!1,blur:30,scale:1.3}),typeof e=="boolean"?this.postProcess.ambientEffect.enabled=e:this.postProcess.ambientEffect={...this.postProcess.ambientEffect,...e,enabled:e.enabled??!0}}setGrid(e){this.postProcess.grid||(this.postProcess.grid={enabled:!1}),typeof e=="boolean"?this.postProcess.grid.enabled=e:this.postProcess.grid={...this.postProcess.grid,...e,enabled:e.enabled??!0}}moveOrigin(e,t){this.x+=e,this.y+=t}setScanlines(e){this.postProcess.scanlines||(this.postProcess.scanlines={enabled:!1,opacity:.15,pattern:"horizontal"}),typeof e=="boolean"?this.postProcess.scanlines.enabled=e:this.postProcess.scanlines={...this.postProcess.scanlines,...e,enabled:e.enabled??!0}}setScanlinesEnabled(e){this.postProcess.scanlines?this.postProcess.scanlines.enabled=e:this.postProcess.scanlines={enabled:e,opacity:.15,pattern:"horizontal"}}setScanlinesOpacity(e){this.postProcess.scanlines?this.postProcess.scanlines.opacity=e:this.postProcess.scanlines={enabled:!0,opacity:e,pattern:"horizontal"}}setGridEnabled(e){this.postProcess.grid?this.postProcess.grid.enabled=e:this.postProcess.grid={enabled:e}}setAmbientEffectEnabled(e){this.postProcess.ambientEffect?this.postProcess.ambientEffect.enabled=e:this.postProcess.ambientEffect={enabled:e,blur:30,scale:1.3}}setPostProcess(e){if(e===null){this.postProcess={};return}this.postProcess={...this.postProcess,...e}}setOrigin(e){this.x=e.x,this.y=e.y}getOrigin(){return new Ce.Vector2(this.x,this.y)}getSize(){return{x:this.width,y:this.height,width:this.width,height:this.height}}switchPalette(e){this.activePaletteSlot=e}setRenderPasses(e){if(!e||e.length===0){this.renderPasses=void 0;return}if(e.length>4)throw new Error(`Display.setRenderPasses: max 4 passes, got ${e.length}`);this.renderPasses=e.map(t=>this.normalizePass(t))}getRenderPasses(){return this.renderPasses}normalizePass(e){let t=Math.max(0,Math.min(255,e.zMin)),r=Math.max(0,Math.min(255,e.zMax));if(t>r){let i=t;t=r,r=i}return{id:e.id,zMin:t,zMax:r,enabled:e.enabled!==!1}}getDebugInfo(){return{id:this.id,origin:{x:this.x,y:this.y},size:{width:this.width,height:this.height},pixelViewport:{pixelWidth:this.pixelWidth,pixelHeight:this.pixelHeight},cellSize:{cellWidth:this.cellWidth,cellHeight:this.cellHeight},scalingMode:this.scalingMode,activePaletteSlot:this.activePaletteSlot,postProcess:this.postProcess,renderPasses:this.renderPasses}}};var B=require("@primitiv/types");var X=class{axes=new Map;buttons=new Map;touchZones=new Map;axisNameToId=new Map;buttonNameToId=new Map;touchZoneNameToId=new Map;version=1;s=null;o=null;d=null;defineAxis(e,t,r=[],i=-1,n=1,s=0){if(e<0||e>255)throw new Error(`Axis bindingId must be between 0 and 255, got ${e}`);if(this.axes.has(e))throw new Error(`Axis bindingId ${e} is already defined`);if(this.axisNameToId.has(t))throw new Error(`Axis name "${t}" is already defined`);let o={bindingId:e,name:t,min:i,max:n,defaultValue:s,sources:r};this.axes.set(e,o),this.axisNameToId.set(t,e),this.s=null,this.version++}defineButton(e,t,r=[],i=!1){if(e<0||e>255)throw new Error(`Button bindingId must be between 0 and 255, got ${e}`);if(this.buttons.has(e))throw new Error(`Button bindingId ${e} is already defined`);if(this.buttonNameToId.has(t))throw new Error(`Button name "${t}" is already defined`);let n={bindingId:e,name:t,defaultValue:i,sources:r};this.buttons.set(e,n),this.buttonNameToId.set(t,e),this.o=null,this.version++}defineTouchZone(e,t,r,i,n,s){if(e<0||e>31)throw new Error(`Touch zone zoneId must be between 0 and 31, got ${e}`);if(this.touchZones.has(e))throw new Error(`Touch zone zoneId ${e} is already defined`);if(this.touchZoneNameToId.has(t))throw new Error(`Touch zone name "${t}" is already defined`);let o={zoneId:e,name:t,x:r,y:i,width:n,height:s};this.touchZones.set(e,o),this.touchZoneNameToId.set(t,e),this.d=null,this.version++}evaluateAxis(e,t){let r=this.axes.get(e);if(!r)return 0;let i=0;for(let n of r.sources){let o=t.get(n.sourceId)??0,d=n.deadzone??0;Math.abs(o)<d?o=0:o=(o>0?1:-1)*((Math.abs(o)-d)/(1-d)),o*=(n.scale??1)*(n.sensitivity??1),n.invert&&(o=-o),i+=o}return Math.max(r.min,Math.min(r.max,i))}evaluateButton(e,t){let r=this.buttons.get(e);if(!r)return!1;for(let i of r.sources)if(t.get(i.sourceId)===!0)return!0;return r.defaultValue}toLoadPacket(){return{type:"input-binding",version:this.version,axes:Array.from(this.axes.values()),buttons:Array.from(this.buttons.values()),touchZones:this.touchZones.size>0?Array.from(this.touchZones.values()):void 0}}loadFromPacket(e){this.clear();for(let t of e.axes)this.defineAxis(t.bindingId,t.name,t.sources,t.min,t.max,t.defaultValue);for(let t of e.buttons)this.defineButton(t.bindingId,t.name,t.sources,t.defaultValue);if(e.touchZones)for(let t of e.touchZones)this.defineTouchZone(t.zoneId,t.name,t.x,t.y,t.width,t.height)}getAllAxes(){return this.s||(this.s=Array.from(this.axes.values()).sort((e,t)=>e.bindingId-t.bindingId)),this.s}getAllButtons(){return this.o||(this.o=Array.from(this.buttons.values()).sort((e,t)=>e.bindingId-t.bindingId)),this.o}getAllTouchZones(){return this.d||(this.d=Array.from(this.touchZones.values()).sort((e,t)=>e.zoneId-t.zoneId)),this.d}getAxisCount(){return this.axes.size}getButtonCount(){return this.buttons.size}getAxisId(e){return this.axisNameToId.get(e)??null}getButtonId(e){return this.buttonNameToId.get(e)??null}clear(){this.axes.clear(),this.buttons.clear(),this.touchZones.clear(),this.axisNameToId.clear(),this.buttonNameToId.clear(),this.touchZoneNameToId.clear(),this.s=null,this.o=null,this.d=null,this.version++}};var ue=class{id;name;data={};bridgeInbox=[];p=new Map;c=new Map;t=new Map;isTabHidden=!1;layers=new Map;displays=new Map;nextLayerId=1;i=null;n=null;spriteRegistry;macroRegistry=null;commandQueue=[];C=[];nextSoundInstanceId=1;axes=new Map;buttons=new Map;justPressed=new Set;justReleased=new Set;mouseX=0;mouseY=0;activeDisplay=0;mouseOver=!1;constructor(e,t){this.id=e,this.name=t}setSpriteRegistry(e){this.spriteRegistry=e;for(let t of this.layers.values())t.setSpriteRegistry(e)}addLayer(e,t){let r=this.nextLayerId++;e.setId(r),this.layers.set(e.id,e),this.i=null,this.spriteRegistry&&e.setSpriteRegistry(this.spriteRegistry)}addDisplay(e){this.displays.set(e.id,e),this.n=null}getMouseDisplayInfo(){return this.mouseOver?{localX:this.mouseX,localY:this.mouseY,displayId:this.activeDisplay}:null}getTouchDisplayInfo(e){return!this.mouseOver||e!==void 0&&this.activeDisplay!==e?null:this.getMouseDisplayInfo()}getDisplayViewport(e){let t=this.displays.get(e);return t?{id:t.id,x:t.x,y:t.y,width:t.width,height:t.height,pixelWidth:t.pixelWidth,pixelHeight:t.pixelHeight,origin:{x:t.x,y:t.y},size:{x:t.width,y:t.height,width:t.width,height:t.height}}:null}calculateMaxCells(e,t,r){let i=this.displays.get(e);return i?i.calculateMaxCells(t,r):null}inputRegistry=new X;getInputBindingRegistry(){return this.inputRegistry}applyInput(e,t){this.mouseX=e.mouseX,this.mouseY=e.mouseY,this.mouseOver=e.mouseOverDisplay,this.activeDisplay=e.displayId;for(let r of t.getAllAxes()){let i=e.axes.get(r.bindingId)??r.defaultValue;this.axes.set(r.name,i)}for(let r of t.getAllButtons()){let i=e.buttons.get(r.bindingId)??r.defaultValue,n=this.buttons.get(r.name)??!1;i&&!n?this.justPressed.add(r.name):!i&&n&&this.justReleased.add(r.name),this.buttons.set(r.name,i)}}poll(){this.justPressed.clear(),this.justReleased.clear(),this.commandQueue.length=0}playSound(e,t){let r=this.nextSoundInstanceId++,i={type:B.OrderType.Audio,sound:e,instanceId:r,...t};return this.commandQueue.push(i),r}stopSound(e){let t={type:B.OrderType.Audio,sound:e,stop:!0};this.commandQueue.push(t)}stopAllSounds(){let e={type:B.OrderType.Audio,sound:"all",stop:!0};this.commandQueue.push(e)}fadeOutSound(e,t){let r={type:B.OrderType.Audio,sound:e,fadeOut:t};this.commandQueue.push(r)}fadeOutAllSounds(e){let t={type:B.OrderType.Audio,sound:"all",fadeOut:e};this.commandQueue.push(t)}pauseSound(e){let t={type:B.OrderType.Audio,sound:e,pause:!0};this.commandQueue.push(t)}pauseAllSounds(){let e={type:B.OrderType.Audio,sound:"all",pause:!0};this.commandQueue.push(e)}resumeSound(e){let t={type:B.OrderType.Audio,sound:e,resume:!0};this.commandQueue.push(t)}resumeAllSounds(){let e={type:B.OrderType.Audio,sound:"all",resume:!0};this.commandQueue.push(e)}setSoundEffects(e,t){let r={type:B.OrderType.Audio,sound:e,...t};this.commandQueue.push(r)}setListenerPosition(e,t){let r={type:B.OrderType.Audio,sound:"__listener_position__",x:e,y:t};this.commandQueue.push(r)}configureSpatialAudio(e){let t={type:B.OrderType.Audio,sound:"__configure_spatial__",...e};this.commandQueue.push(t)}sendSounds(){this.M=!0}M=!1;vibrate(e){let t={type:B.OrderType.Vibration,pattern:e};this.commandQueue.push(t)}vibrateGamepad(e){let t={type:B.OrderType.Vibration,target:"gamepad",gamepadIndex:e.gamepadIndex??0,duration:e.duration,strongMagnitude:e.strongMagnitude??1,weakMagnitude:e.weakMagnitude??1,startDelay:e.startDelay??0};this.commandQueue.push(t)}setPostProcess(e,t){let r=this.displays.get(e);r&&r.setPostProcess(t)}setAmbientEffect(e,t){let r=this.displays.get(e);r&&r.setAmbientEffect(t)}setGrid(e,t){let r=this.displays.get(e);r&&r.setGrid(t)}setScalingMode(e,t){let r=this.displays.get(e);r&&r.setScalingMode(t)}switchPalette(e,t){let r=this.displays.get(e);r&&r.switchPalette(t)}setOrigin(e,t){let r=this.displays.get(e);r&&r.setOrigin(t)}flushCommands(){let e=this.commandQueue;return this.commandQueue=this.C,this.C=e,this.commandQueue.length=0,e}getAxis(e){return this.axes.get(e)??0}getButton(e){return this.buttons.get(e)??!1}isJustPressed(e){return this.justPressed.has(e)}isJustReleased(e){return this.justReleased.has(e)}createLayer(e,t,r){if(this.layers.has(e))throw new Error(`Layer ${e} already exists`);let i=new de(new B.Vector2(0,0),0,t,r);return i.setId(e),this.layers.set(e,i),this.i=null,i}getLayer(e){return this.layers.get(e)}removeLayer(e){let t=this.layers.delete(e);return t&&(this.i=null),t}getLayers(){return this.i===null&&(this.i=Array.from(this.layers.values())),this.i}createDisplay(e,t,r){if(this.displays.has(e))throw new Error(`Display ${e} already exists`);let i=new le(e,t,r);return this.displays.set(e,i),this.n=null,i}getDisplay(e){return this.displays.get(e)}removeDisplay(e){let t=this.displays.delete(e);return t&&(this.n=null),t}getDisplays(){return this.n===null&&(this.n=Array.from(this.displays.values())),this.n}defineButton(e,t,r,i=!1){this.inputRegistry.defineButton(e,t,r,i)}defineAxis(e,t,r,i=-1,n=1,s=0){this.inputRegistry.defineAxis(e,t,r,i,n,s)}handleAudioAck(e){switch(e.type){case"sound-loaded":this.p.set(e.soundId,{name:e.name,loadedAt:Date.now()}),this.c.delete(e.soundId);break;case"sound-error":this.c.set(e.soundId,{name:e.name,error:e.error,at:Date.now()});break;case"playback-started":this.t.set(e.instanceId,{soundId:e.soundId,startedAt:Date.now()});break;case"playback-ended":this.t.delete(e.instanceId);break;case"playback-error":e.instanceId!=null&&this.t.delete(e.instanceId);break}}isSoundLoaded(e){return this.p.has(e)}getLoadedSounds(){return this.p}getSoundLoadError(e){return this.c.get(e)?.error}getSoundLoadErrors(){return this.c}isSoundPlaying(e){return this.t.has(e)}getPlayingSounds(){return this.t}getPlayingSoundCount(){return this.t.size}setMacroRegistry(e){this.macroRegistry=e}getMacroRegistry(){return this.macroRegistry}loadMacro(e,t){if(!this.macroRegistry)throw new Error("MacroRegistry not set");return this.macroRegistry.registerTemplate(e,t)}createMacroInstance(e){if(!this.macroRegistry)throw new Error("MacroRegistry not set");return this.macroRegistry.createInstance(e)}updateMacroInstance(e,t){this.macroRegistry?.updateInstance(e,t)}removeMacroInstance(e){this.macroRegistry?.removeInstance(e)}pauseMacroInstance(e){this.macroRegistry?.pauseInstance(e)}resumeMacroInstance(e){this.macroRegistry?.resumeInstance(e)}onMacroEvent(e,t,r){this.macroRegistry?.onEvent(e,t,r)}handleMacroFeedback(e){this.macroRegistry?.handleFeedback(e)}};var Ue=require("@primitiv/types"),He=require("@primitiv/logger"),at=He.Logger.create("core:update-builder"),ke=class{buffer;view;offset=0;textEncoder=new TextEncoder;constructor(e=1024){this.buffer=new Uint8Array(e),this.view=new DataView(this.buffer.buffer)}serialize(e){this.offset=0;let t=e.flushCommands();this.writeUint8(Math.min(t.length,255));for(let r of t)this.writeOrder(r);return this.buffer.slice(0,this.offset)}writeOrder(e){switch(this.writeUint8(e.type),e.type){case Ue.OrderType.Audio:this.writeAudioOrder(e);break;case Ue.OrderType.Vibration:this.writeVibrationOrder(e);break;case Ue.OrderType.PostProcess:this.writePostProcessOrder(e);break;default:at.warn(`Unsupported order type ${e.type}`);break}}writeAudioOrder(e){typeof e.sound=="number"?(this.writeUint8(0),this.writeUint16(e.sound)):(this.writeUint8(1),this.writeString(e.sound)),this.writeUint16(e.instanceId??0);let t=0;e.stop&&(t|=1),e.pause&&(t|=2),e.resume&&(t|=4),e.loop&&(t|=8),this.writeUint8(t),this.writeUint8(Math.floor((e.volume??1)*255)),this.writeUint8(Math.floor((e.pitch??1)*100)),this.writeInt16(e.x??0),this.writeInt16(e.y??0)}writeVibrationOrder(e){if("target"in e&&e.target==="gamepad"){this.writeUint8(0);return}let t=Array.isArray(e.pattern)?e.pattern:[e.pattern];this.writeUint8(Math.min(t.length,255));for(let r of t)this.writeUint16(r)}writePostProcessOrder(e){this.writeUint8(e.displayId),this.writeUint8(e.enabled?1:0),this.writeUint8(Math.min(255,Math.floor((e.bloom??0)*100))),this.writeUint8(Math.min(255,Math.floor((e.chromaticAberration??0)*100))),this.writeUint8(Math.min(255,Math.floor((e.scanlineOpacity??0)*255)))}ensureCapacity(e){if(this.offset+e>this.buffer.length){let t=Math.max(this.buffer.length*2,this.offset+e),r=new Uint8Array(t);r.set(this.buffer),this.buffer=r,this.view=new DataView(this.buffer.buffer)}}writeUint8(e){this.ensureCapacity(1),this.buffer[this.offset++]=e}writeUint16(e){this.ensureCapacity(2),this.view.setUint16(this.offset,e,!1),this.offset+=2}writeInt16(e){this.ensureCapacity(2),this.view.setInt16(this.offset,e,!1),this.offset+=2}writeString(e){let t=this.textEncoder.encode(e);this.writeUint8(Math.min(t.length,255)),this.ensureCapacity(t.length),this.buffer.set(t,this.offset),this.offset+=t.length}};var $=require("@primitiv/types"),Ne=require("@primitiv/logger"),dt=Ne.Logger.create("core:update-parser"),Ae=class{textDecoder=new TextDecoder;parse(e){let t=new DataView(e.buffer,e.byteOffset,e.byteLength),r=0,i=[];if(r>=e.byteLength)return i;let n=e[r++];for(let s=0;s<n&&!(r>=e.byteLength);s++){let o=this.parseOrder(e,t,r);if(o)i.push(o.order),r=o.newOffset;else break}return i}parseOrder(e,t,r){let i=e[r++];switch(i){case $.OrderType.Audio:return this.parseAudioOrder(e,t,r);case $.OrderType.Vibration:return this.parseVibrationOrder(e,t,r);case $.OrderType.PostProcess:return this.parsePostProcessOrder(e,t,r);default:return dt.warn(`Unsupported order type ${i}`),null}}parseAudioOrder(e,t,r){let i=e[r++],n;if(i===0)n=t.getUint16(r,!1),r+=2;else{let y=e[r++];n=this.textDecoder.decode(e.slice(r,r+y)),r+=y}let s=t.getUint16(r,!1);r+=2;let o=e[r++],d=(o&1)!==0,u=(o&2)!==0,a=(o&4)!==0,c=(o&8)!==0,b=e[r++]/255,h=e[r++]/100,m=t.getInt16(r,!1);r+=2;let g=t.getInt16(r,!1);return r+=2,{order:{type:$.OrderType.Audio,sound:n,instanceId:s,volume:b,pitch:h,loop:c,stop:d,pause:u,resume:a,x:m,y:g},newOffset:r}}parseVibrationOrder(e,t,r){let i=e[r++],n=[];for(let o=0;o<i;o++)n.push(t.getUint16(r,!1)),r+=2;return{order:{type:$.OrderType.Vibration,pattern:n},newOffset:r}}parsePostProcessOrder(e,t,r){let i=e[r++],n=e[r++]!==0,s=e[r++]/100,o=e[r++]/100,d=e[r++]/255;return{order:{type:$.OrderType.PostProcess,displayId:i,enabled:n,bloom:s,chromaticAberration:o,scanlineOpacity:d},newOffset:r}}};var W=require("@primitiv/types"),lt=new TextDecoder;function ut(l){let e=Array.from(l.axes.entries()).sort((p,y)=>p[0]-y[0]),t=Array.from(l.buttons.entries()).sort((p,y)=>p[0]-y[0]),r=(0,W.getButtonByteCount)(t.length),i=new TextEncoder,n=l.textInputs.map(p=>i.encode(p)),s=1+n.reduce((p,y)=>p+1+y.length,0),o=1+l.displayViewports.length*5,d=1+l.touchPositions.length*4,u=e.length*3,a=t.length*2+r,c=2+u+a+4+s+o+d,b=new Uint8Array(c),h=new DataView(b.buffer),m=0;b[m++]=e.length&255,b[m++]=t.length&255;for(let[p,y]of e)h.setUint16(m,p,!1),m+=2,b[m++]=(0,W.encodeAxisToInt8)(y)+256&255;for(let[p]of t)h.setUint16(m,p,!1),m+=2;for(let p=0;p<r;p++){let y=0;for(let w=0;w<8;w++){let A=p*8+w;A<t.length&&t[A][1]&&(y|=1<<w)}b[m++]=y}b[m++]=l.displayId&255,b[m++]=l.mouseX&255,b[m++]=l.mouseY&255;let g=0;l.mouseOverDisplay&&(g|=1),l.isTabHidden&&(g|=2),b[m++]=g,b[m++]=Math.min(n.length,255);for(let p of n){let y=Math.min(p.length,255);b[m++]=y,b.set(p.slice(0,y),m),m+=y}b[m++]=Math.min(l.displayViewports.length,255);for(let p of l.displayViewports)b[m++]=p.displayId&255,h.setUint16(m,p.pixelWidth,!1),m+=2,h.setUint16(m,p.pixelHeight,!1),m+=2;b[m++]=Math.min(l.touchPositions.length,255);for(let p of l.touchPositions)b[m++]=p.id&15,b[m++]=p.x&255,b[m++]=p.y&255,b[m++]=p.over?1:0;return b}function ct(l,e){let t=new DataView(l.buffer,l.byteOffset,l.byteLength),r=0,i=(0,W.createEmptyCompressedInputPacket)(),n=l[r++],s=l[r++],o=(0,W.getButtonByteCount)(s);for(let a=0;a<n;a++){let c=t.getUint16(r,!1);r+=2;let b=l[r++],h=b>127?b-256:b;i.axes.set(c,(0,W.decodeInt8ToAxis)(h))}let d=[];for(let a=0;a<s;a++)d.push(t.getUint16(r,!1)),r+=2;for(let a=0;a<o;a++){let c=l[r++];for(let b=0;b<8;b++){let h=a*8+b;h<s&&i.buttons.set(d[h],(c&1<<b)!==0)}}i.displayId=l[r++],i.mouseX=l[r++],i.mouseY=l[r++];let u=l[r++];if(i.mouseOverDisplay=(u&1)!==0,i.isTabHidden=(u&2)!==0,r<l.length){let a=l[r++];for(let c=0;c<a;c++){let b=l[r++];i.textInputs.push(lt.decode(l.subarray(r,r+b))),r+=b}}if(r<l.length){let a=l[r++];for(let c=0;c<a;c++){let b=l[r++],h=t.getUint16(r,!1);r+=2;let m=t.getUint16(r,!1);r+=2,i.displayViewports.push({displayId:b,pixelWidth:h,pixelHeight:m})}}if(r<l.length){let a=l[r++];for(let c=0;c<a;c++){let b=l[r++]&15,h=l[r++],m=l[r++],g=(l[r++]&1)!==0;i.touchPositions.push({id:b,x:h,y:m,over:g})}}return i}var Xe=[[0,0],[9786,1],[9787,2],[9829,3],[9830,4],[9827,5],[9824,6],[8226,7],[9688,8],[9675,9],[9689,10],[9794,11],[9792,12],[9834,13],[9835,14],[9788,15],[9658,16],[9668,17],[8597,18],[8252,19],[182,20],[167,21],[9644,22],[8616,23],[8593,24],[8595,25],[8594,26],[8592,27],[8735,28],[8596,29],[9650,30],[9660,31],[8962,127],[199,128],[252,129],[233,130],[226,131],[228,132],[224,133],[229,134],[231,135],[234,136],[235,137],[232,138],[239,139],[238,140],[236,141],[196,142],[197,143],[201,144],[230,145],[198,146],[244,147],[246,148],[242,149],[251,150],[249,151],[255,152],[214,153],[220,154],[162,155],[163,156],[165,157],[8359,158],[402,159],[225,160],[237,161],[243,162],[250,163],[241,164],[209,165],[170,166],[186,167],[191,168],[8976,169],[172,170],[189,171],[188,172],[161,173],[171,174],[187,175],[9617,176],[9618,177],[9619,178],[9474,179],[9508,180],[9569,181],[9570,182],[9558,183],[9557,184],[9571,185],[9553,186],[9559,187],[9565,188],[9564,189],[9563,190],[9488,191],[9492,192],[9524,193],[9516,194],[9500,195],[9472,196],[9532,197],[9566,198],[9567,199],[9562,200],[9556,201],[9577,202],[9574,203],[9568,204],[9552,205],[9580,206],[9575,207],[9576,208],[9572,209],[9573,210],[9561,211],[9560,212],[9554,213],[9555,214],[9579,215],[9578,216],[9496,217],[9484,218],[9608,219],[9604,220],[9612,221],[9616,222],[9600,223],[945,224],[223,225],[915,226],[960,227],[931,228],[963,229],[181,230],[964,231],[934,232],[920,233],[937,234],[948,235],[8734,236],[966,237],[949,238],[8745,239],[8801,240],[177,241],[8805,242],[8804,243],[8992,244],[8993,245],[247,246],[8776,247],[176,248],[8729,249],[183,250],[8730,251],[8319,252],[178,253],[9632,254],[160,255]],Fe=new Map(Xe),bt=new Map(Xe.map(([l,e])=>[e,l]));function D(l){if(l<128)return l;let e=Fe.get(l);return e!==void 0?e:l&255}function ht(l){let e=new Uint8Array(l.length);for(let t=0;t<l.length;t++)e[t]=D(l.charCodeAt(t));return e}function $e(l){let e=l&255;return e<128?e:bt.get(e)??e}function mt(l){return String.fromCharCode($e(l))}function pt(l){return l<128?!0:Fe.has(l)}function ft(l){let e="";for(let t=0;t<l.length;t++)e+=String.fromCharCode(D(l.charCodeAt(t)));return e}var ce=class{unicolorSprites=new Map;multicolorSprites=new Map;loadUnicolorSprites(e){for(let t of e){let{spriteId:r,width:i,height:n,data:s}=t,o=new Uint16Array(i*n);if(typeof s=="string"){let u=s.replace(/\n/g,"");for(let a=0;a<Math.min(u.length,i*n);a++)o[a]=D(u.charCodeAt(a))}else if(Array.isArray(s)&&s.length>0&&typeof s[0]=="string"){let u=s;for(let a=0;a<Math.min(u.length,n);a++){let c=u[a];if(c)for(let b=0;b<Math.min(c.length,i);b++)o[a*i+b]=D(c.charCodeAt(b))}}else{let u=s;for(let a=0;a<Math.min(u.length,i*n);a++)o[a]=u[a]}let d={id:r,width:i,height:n,data:o};this.unicolorSprites.set(d.id,d)}}loadMulticolorSprites(e){for(let t of e){let{spriteId:r,width:i,height:n,data:s}=t,o=new Uint32Array(i*n);for(let u=0;u<Math.min(s.length,i*n);u++){let a=s[u],c=0;typeof a.charCode=="string"?c=D(a.charCode.charCodeAt(0)):c=a.charCode||0;let b=a.fgColorId||0,h=a.bgColorId||0;o[u]=oe.pack(c,b,h)}let d={id:r,width:i,height:n,data:o};this.multicolorSprites.set(d.id,d)}}getUnicolorSprite(e){return this.unicolorSprites.get(e)}getMulticolorSprite(e){return this.multicolorSprites.get(e)}hasUnicolorSprite(e){return this.unicolorSprites.has(e)}hasMulticolorSprite(e){return this.multicolorSprites.has(e)}clearAll(){this.unicolorSprites.clear(),this.multicolorSprites.clear()}};var Ye=(t=>(t.Bitmap="bitmap",t.Image="image",t))(Ye||{});function G(l){return l<=1?1:1<<Math.ceil(Math.log2(l))}function K(l){let e=G(Math.ceil(Math.sqrt(l))),t=G(Math.ceil(l/e));return{cols:e*16,rows:t*16,blocksPerRow:e,blocksPerCol:t}}function gt(l){return K(l).cols}function yt(l){return K(l).rows}function Ze(l){return l*256-1}function Pe(l,e,t){let r=K(l),i=G(e),n=G(t??e);return{width:r.cols*i,height:r.rows*n}}var be=class{constructor(e,t){this.fontId=e;this.config=t;let r=K(t.atlasBlocks);this.atlasColumns=r.cols,this.atlasRows=r.rows,this.maxCharCode=Ze(t.atlasBlocks)}atlasColumns;atlasRows;maxCharCode;blocks=new Map;addBlock(e,t){if(e<0||e>=this.config.atlasBlocks)throw new Error(`Invalid block index ${e} for font with ${this.config.atlasBlocks} blocks`);this.blocks.set(e,t)}getBlock(e){return this.blocks.get(e)}getFontId(){return this.fontId}getConfig(){return{glyphWidth:this.config.glyphWidth,glyphHeight:this.config.glyphHeight,cellWidth:this.config.cellWidth,cellHeight:this.config.cellHeight,atlasBlocks:this.config.atlasBlocks}}getGlyphWidth(){return this.config.glyphWidth}getGlyphHeight(){return this.config.glyphHeight}getCellWidth(){return this.config.cellWidth??this.config.glyphWidth}getCellHeight(){return this.config.cellHeight??this.config.glyphHeight}getAtlasBlocks(){return this.config.atlasBlocks}getAtlasColumns(){return this.atlasColumns}getMaxCharCode(){return this.maxCharCode}getAtlasRows(){return this.atlasRows}getAtlasDimensions(){let e=G(this.config.glyphWidth),t=G(this.config.glyphHeight);return{width:this.atlasColumns*e,height:this.atlasRows*t}}getCharUV(e){if(e<0||e>this.maxCharCode)return null;let{blocksPerRow:t}=K(this.config.atlasBlocks),r=Math.floor(e/256),i=e%256,n=r%t,s=Math.floor(r/t),o=i%16,d=Math.floor(i/16),u=G(this.config.glyphWidth),a=G(this.config.glyphHeight),c=this.atlasColumns*u,b=this.atlasRows*a,h=n*16*u+o*this.config.glyphWidth,m=s*16*a+d*this.config.glyphHeight,g=h/c,p=m/b,y=(h+this.config.glyphWidth)/c,w=(m+this.config.glyphHeight)/b;return{u1:g,v1:p,u2:y,v2:w}}isValidCharCode(e){return e>=0&&e<=this.maxCharCode}};var he=class{fonts=new Map;nameToId=new Map;nextId=0;allocateId(e){if(this.nextId>255)throw new Error(`Cannot register font "${e}": maximum 256 fonts reached`);return this.nextId++}registerFont(e,t){if(this.nameToId.has(e))throw new Error(`ImageFont with name "${e}" already exists`);let r=this.allocateId(e),i={glyphWidth:t.glyphWidth,glyphHeight:t.glyphHeight,cellWidth:t.cellWidth,cellHeight:t.cellHeight,atlasBlocks:t.atlasBlocks??1},n=new be(r,i);return this.fonts.set(r,n),this.nameToId.set(e,r),r}unregisterFont(e){let t=this.nameToId.get(e);t!==void 0&&(this.fonts.delete(t),this.nameToId.delete(e))}hasFont(e){return this.nameToId.has(e)}addBlock(e,t,r){let i=this.fonts.get(e);if(!i)throw new Error(`ImageFont with ID ${e} not found`);i.addBlock(t,r)}getFont(e){return this.fonts.get(e)}getFontByName(e){let t=this.nameToId.get(e);if(t!==void 0)return this.fonts.get(t)}getFontId(e){return this.nameToId.get(e)}getAllFonts(){return Array.from(this.fonts.values())}getFontName(e){for(let[t,r]of this.nameToId)if(r===e)return t}};var me=class{sounds=new Map;nameToId=new Map;nextId=0;registerFile(e,t,r){let i=this.allocateId(e),n={soundId:i,name:e,loadType:"file",format:t,data:r};return this.addEntry(n),i}registerExternal(e,t,r,i,n){let s=this.allocateId(e),o={soundId:s,name:e,loadType:"external",format:t,url:r,size:i,checksum:n};return this.addEntry(o),s}get(e){if(typeof e=="number")return this.sounds.get(e);let t=this.nameToId.get(e);return t!==void 0?this.sounds.get(t):void 0}has(e){return this.get(e)!==void 0}toLoadPackets(){let e=[],t=Array.from(this.sounds.values()),r=t.length,i=t.filter(s=>s.loadType==="file");i.length>0&&e.push({type:"sound",mode:"file",sounds:i.map(s=>({soundId:s.soundId,name:s.name,format:s.format,data:s.data})),totalSounds:r});let n=t.filter(s=>s.loadType==="external");return n.length>0&&e.push({type:"sound",mode:"external",sounds:n.map(s=>({soundId:s.soundId,name:s.name,format:s.format,url:s.url,size:s.size,checksum:s.checksum})),totalSounds:r}),e}allocateId(e){if(this.nameToId.has(e))throw new Error(`Sound with name "${e}" already exists`);for(;this.sounds.has(this.nextId);)if(this.nextId++,this.nextId>255)throw new Error("Maximum number of sounds (256) reached");return this.nextId++}addEntry(e){this.sounds.set(e.soundId,e),this.nameToId.set(e.name,e.soundId)}};var Qe=require("@primitiv/logger"),je=Qe.Logger.create("core:macro-registry"),pe=class{templates=new Map;templateNameToId=new Map;nextTemplateId=0;instances=new Map;instanceNameToId=new Map;nextInstanceId=1;pendingDefines=[];pendingOrders=[];feedbackHandlers=new Map;registerTemplate(e,t){if(this.templateNameToId.has(e))return this.templateNameToId.get(e);let r=this.nextTemplateId++;if(r>65535)throw new Error("Max 65535 macro templates");return this.templates.set(r,t),this.templateNameToId.set(e,r),this.pendingDefines.push({templateId:r,template:t}),je.debug(`Template registered: "${e}" -> id=${r}`),r}getTemplate(e){if(typeof e=="string"){let t=this.templateNameToId.get(e);return t!==void 0?this.templates.get(t):void 0}return this.templates.get(e)}getTemplateId(e){return this.templateNameToId.get(e)}createInstance(e){let t=this.templateNameToId.get(e.macro);if(t===void 0)throw new Error(`Unknown macro template: "${e.macro}"`);let r=this.nextInstanceId++;if(r>65535)throw new Error("Max 65535 macro instances");let i={instanceId:r,templateId:t,layerId:e.layer,x:e.x,y:e.y,zIndex:e.zIndex??0,params:e.params??{},state:"running"};return this.instances.set(r,i),e.name&&this.instanceNameToId.set(e.name,r),this.pendingOrders.push({cmd:1,instanceId:r,templateId:t,layerId:e.layer,x:e.x,y:e.y,zIndex:e.zIndex??0,params:e.params??{}}),je.debug(`Instance created: id=${r} macro="${e.macro}" layer=${e.layer}`),r}updateInstance(e,t){let r=this.resolveInstanceId(e);if(r===void 0)return;let i=this.instances.get(r);i&&(Object.assign(i.params,t),this.pendingOrders.push({cmd:2,instanceId:r,params:t}))}removeInstance(e){let t=this.resolveInstanceId(e);if(t!==void 0){this.instances.delete(t);for(let[r,i]of this.instanceNameToId)if(i===t){this.instanceNameToId.delete(r);break}this.feedbackHandlers.delete(t),this.pendingOrders.push({cmd:3,instanceId:t})}}pauseInstance(e){let t=this.resolveInstanceId(e);if(t===void 0)return;let r=this.instances.get(t);r&&(r.state="paused"),this.pendingOrders.push({cmd:4,instanceId:t})}resumeInstance(e){let t=this.resolveInstanceId(e);if(t===void 0)return;let r=this.instances.get(t);r&&(r.state="running"),this.pendingOrders.push({cmd:5,instanceId:t})}getPendingDefines(){return this.pendingDefines}getPendingOrders(){return this.pendingOrders}clearPendingDefines(){this.pendingDefines.length=0}clearPendingOrders(){this.pendingOrders.length=0}getAllDefines(){let e=[];for(let[t,r]of this.templates)e.push({templateId:t,template:r});return e}getActiveInstanceOrders(){let e=[];for(let t of this.instances.values())e.push({cmd:1,instanceId:t.instanceId,templateId:t.templateId,layerId:t.layerId,x:t.x,y:t.y,zIndex:t.zIndex,params:t.params});return e}onEvent(e,t,r){let i=this.resolveInstanceId(e);if(i===void 0)return;let n=this.feedbackHandlers.get(i);n||(n=new Map,this.feedbackHandlers.set(i,n));let s=n.get(t);s||(s=[],n.set(t,s)),s.push(r)}handleFeedback(e){let t=this.feedbackHandlers.get(e.instanceId);if(!t)return;let r,i;switch(e.cmd){case 1:r="click",i=void 0;break;case 2:r="change",i=e.value;break;case 3:r="submit",i=e.text;break;case 4:r="select",i=e.index;break;case 5:r="custom",i=e.data;break;default:return}let n=t.get(r);if(n)for(let s of n)s(i)}resolveInstanceId(e){return typeof e=="number"?e:this.instanceNameToId.get(e)}};var fe=require("@primitiv/types");var x=require("@primitiv/types");var q=class{buffer;view;offset=0;constructor(e=4096){this.buffer=new Uint8Array(e),this.view=new DataView(this.buffer.buffer)}reset(){this.offset=0}ensure(e){if(this.offset+e>this.buffer.length){let t=this.buffer.length*2;for(;t<this.offset+e;)t*=2;let r=new Uint8Array(t);r.set(this.buffer),this.buffer=r,this.view=new DataView(this.buffer.buffer)}}ensureAndGetBuffer(e){return this.ensure(e),this.buffer}writeU8(e){this.ensure(1),this.buffer[this.offset++]=e&255}writeI8(e){this.ensure(1),this.view.setInt8(this.offset,e),this.offset+=1}writeU16BE(e){this.ensure(2),this.view.setUint16(this.offset,e,!1),this.offset+=2}writeI16BE(e){this.ensure(2),this.view.setInt16(this.offset,e,!1),this.offset+=2}writeU16LE(e){this.ensure(2),this.view.setUint16(this.offset,e,!0),this.offset+=2}writeBytes(e){this.ensure(e.length),this.buffer.set(e,this.offset),this.offset+=e.length}toUint8Array(){return this.buffer.slice(0,this.offset)}getView(){return this.buffer.subarray(0,this.offset)}};var Je=new TextEncoder;function O(l,e,t){t?l.writeU16LE(e):l.writeU8(e&255)}var _=class l{static v=new q(4096);static encode(e,t=!1){let r=l.v;r.reset();for(let i of e)l.encodeOne(r,i,t);return r.toUint8Array()}static encodeInto(e,t,r=!1){for(let i of t)l.encodeOne(e,i,r)}static encodeOne(e,t,r){switch(t.type){case x.OrderType.Char:l.encodeChar(e,t,r);break;case x.OrderType.Text:l.encodeText(e,t);break;case x.OrderType.TextMultiline:l.encodeTextMultiline(e,t);break;case x.OrderType.SubFrame:l.encodeSubFrame(e,t,r);break;case x.OrderType.SubFrameMulti:l.encodeSubFrameMulti(e,t,r);break;case x.OrderType.FullFrame:l.encodeFullFrame(e,t,r);break;case x.OrderType.FullFrameMulti:l.encodeFullFrameMulti(e,t,r);break;case x.OrderType.Sprite:l.encodeSprite(e,t);break;case x.OrderType.SpriteMulti:l.encodeSpriteMulti(e,t);break;case x.OrderType.ColorMap:l.encodeColorMap(e,t);break;case x.OrderType.Shape:l.encodeShape(e,t,r);break;case x.OrderType.Polyline:l.encodePolyline(e,t,r);break;case x.OrderType.DotCloud:l.encodeDotCloud(e,t,r);break;case x.OrderType.DotCloudMulti:l.encodeDotCloudMulti(e,t,r);break;case x.OrderType.SpriteCloud:l.encodeSpriteCloud(e,t);break;case x.OrderType.SpriteCloudMulti:l.encodeSpriteCloudMulti(e,t);break;case x.OrderType.SpriteCloudVaried:l.encodeSpriteCloudVaried(e,t);break;case x.OrderType.SpriteCloudVariedMulti:l.encodeSpriteCloudVariedMulti(e,t);break;case x.OrderType.Bitmask:l.encodeBitmask(e,t,r);break;case x.OrderType.Bitmask4:l.encodeBitmask4(e,t,r);break;case x.OrderType.Bitmask16:l.encodeBitmask16(e,t,r);break;case x.OrderType.Fill:l.encodeFill(e,t,r);break;case x.OrderType.FillChar:l.encodeFillChar(e,t,r);break;case x.OrderType.FillSprite:l.encodeFillSprite(e,t);break;case x.OrderType.FillSpriteMulti:l.encodeFillSpriteMulti(e,t);break;default:break}}static encodeChar(e,t,r){e.writeU8(x.OrderType.Char),e.writeU8(t.x),e.writeU8(t.y),O(e,t.char,r),e.writeU8(t.bg),e.writeU8(t.fg)}static encodeText(e,t){e.writeU8(x.OrderType.Text),e.writeU8(t.x),e.writeU8(t.y);let r=t.text.length*3,i=e.ensureAndGetBuffer(1+r),n=e.offset;e.offset++;let{written:s}=Je.encodeInto(t.text,i.subarray(e.offset,e.offset+r));i[n]=s,e.offset+=s,e.writeU8(t.bg),e.writeU8(t.fg)}static encodeTextMultiline(e,t){e.writeU8(x.OrderType.TextMultiline),e.writeU8(t.x),e.writeU8(t.y);let r=t.text.length*3,i=e.ensureAndGetBuffer(1+r),n=e.offset;e.offset++;let{written:s}=Je.encodeInto(t.text,i.subarray(e.offset,e.offset+r));i[n]=s,e.offset+=s,e.writeU8(t.bg),e.writeU8(t.fg)}static encodeSubFrame(e,t,r){e.writeU8(x.OrderType.SubFrame),e.writeU8(t.x),e.writeU8(t.y),e.writeU8(t.width-1),e.writeU8(t.height-1),e.writeU8(t.bg),e.writeU8(t.fg);for(let i of t.frame)O(e,i,r)}static encodeSubFrameMulti(e,t,r){e.writeU8(x.OrderType.SubFrameMulti),e.writeU8(t.x),e.writeU8(t.y),e.writeU8(t.width-1),e.writeU8(t.height-1);for(let i of t.frame)O(e,i.char,r),e.writeU8(i.bg),e.writeU8(i.fg)}static encodeFullFrame(e,t,r){e.writeU8(x.OrderType.FullFrame),e.writeU8(t.bg),e.writeU8(t.fg);for(let i of t.frame)O(e,i,r)}static encodeFullFrameMulti(e,t,r){e.writeU8(x.OrderType.FullFrameMulti);for(let i of t.frame)O(e,i.char,r),e.writeU8(i.bg),e.writeU8(i.fg)}static encodeSprite(e,t){e.writeU8(x.OrderType.Sprite),e.writeU8(t.x),e.writeU8(t.y),e.writeU8(t.spriteId),e.writeU8(t.bg),e.writeU8(t.fg)}static encodeSpriteMulti(e,t){e.writeU8(x.OrderType.SpriteMulti),e.writeU8(t.x),e.writeU8(t.y),e.writeU8(t.spriteId)}static encodeColorMap(e,t){e.writeU8(x.OrderType.ColorMap),e.writeU8(t.x),e.writeU8(t.y),e.writeU8(t.width-1),e.writeU8(t.height-1);for(let r of t.data)e.writeU8(r.bg),e.writeU8(r.fg)}static encodeShape(e,t,r){switch(e.writeU8(x.OrderType.Shape),e.writeU8(t.shapeType),t.shapeType){case x.ShapeType.Rectangle:e.writeU8(t.x??0),e.writeU8(t.y??0),e.writeU8((t.width??1)-1),e.writeU8((t.height??1)-1),e.writeU8(t.filled?1:0),O(e,t.char,r),e.writeU8(t.bg),e.writeU8(t.fg);break;case x.ShapeType.Circle:e.writeU8(t.x??0),e.writeU8(t.y??0),e.writeU8((t.radius??1)-1),e.writeU8(t.filled?1:0),O(e,t.char,r),e.writeU8(t.bg),e.writeU8(t.fg);break;case x.ShapeType.Line:e.writeU8(t.x1??0),e.writeU8(t.y1??0),e.writeU8(t.x2??0),e.writeU8(t.y2??0),O(e,t.char,r),e.writeU8(t.bg),e.writeU8(t.fg);break;case x.ShapeType.Ellipse:e.writeU8(t.x??0),e.writeU8(t.y??0),e.writeU8((t.radiusX??1)-1),e.writeU8((t.radiusY??1)-1),e.writeU8(t.filled?1:0),O(e,t.char,r),e.writeU8(t.bg),e.writeU8(t.fg);break;case x.ShapeType.Triangle:e.writeU8(t.x1??0),e.writeU8(t.y1??0),e.writeU8(t.x2??0),e.writeU8(t.y2??0),e.writeU8(t.x3??0),e.writeU8(t.y3??0),e.writeU8(t.filled?1:0),O(e,t.char,r),e.writeU8(t.bg),e.writeU8(t.fg);break}}static encodePolyline(e,t,r){e.writeU8(x.OrderType.Polyline),O(e,t.char,r),e.writeU8(t.fg),e.writeU8(t.bg),e.writeU8(t.points.length);for(let i of t.points)e.writeU8(i.x),e.writeU8(i.y)}static encodeDotCloud(e,t,r){e.writeU8(x.OrderType.DotCloud),O(e,t.char,r),e.writeU8(t.bg),e.writeU8(t.fg),e.writeU16BE(t.positions.length);for(let i of t.positions)e.writeU8(i.x),e.writeU8(i.y)}static encodeDotCloudMulti(e,t,r){e.writeU8(x.OrderType.DotCloudMulti),e.writeU16BE(t.dots.length);for(let i of t.dots)O(e,i.char,r),e.writeU8(i.bg),e.writeU8(i.fg),e.writeU8(i.x),e.writeU8(i.y)}static encodeSpriteCloud(e,t){e.writeU8(x.OrderType.SpriteCloud),e.writeU8(t.spriteId),e.writeU8(t.bg),e.writeU8(t.fg),e.writeU16BE(t.positions.length);for(let r of t.positions)e.writeU8(r.x),e.writeU8(r.y)}static encodeSpriteCloudMulti(e,t){e.writeU8(x.OrderType.SpriteCloudMulti),e.writeU8(t.spriteId),e.writeU16BE(t.positions.length);for(let r of t.positions)e.writeU8(r.x),e.writeU8(r.y)}static encodeSpriteCloudVaried(e,t){e.writeU8(x.OrderType.SpriteCloudVaried),e.writeU16BE(t.sprites.length);for(let r of t.sprites)e.writeU8(r.spriteId),e.writeU8(r.bg),e.writeU8(r.fg),e.writeU8(r.x),e.writeU8(r.y)}static encodeSpriteCloudVariedMulti(e,t){e.writeU8(x.OrderType.SpriteCloudVariedMulti),e.writeU16BE(t.sprites.length);for(let r of t.sprites)e.writeU8(r.spriteId),e.writeU8(r.x),e.writeU8(r.y)}static encodeBitmask(e,t,r){e.writeU8(x.OrderType.Bitmask),e.writeU8(t.x),e.writeU8(t.y),e.writeU8(t.width-1),e.writeU8(t.height-1),O(e,t.char,r),e.writeU8(t.bg),e.writeU8(t.fg),e.writeU8(t.override?1:0),e.writeBytes(t.mask)}static encodeBitmask4(e,t,r){e.writeU8(x.OrderType.Bitmask4),e.writeU8(t.x),e.writeU8(t.y),e.writeU8(t.width-1),e.writeU8(t.height-1),e.writeU8(t.override?1:0);for(let i of t.variants)O(e,i.char,r),e.writeU8(i.bg),e.writeU8(i.fg);e.writeBytes(t.mask)}static encodeBitmask16(e,t,r){e.writeU8(x.OrderType.Bitmask16),e.writeU8(t.x),e.writeU8(t.y),e.writeU8(t.width-1),e.writeU8(t.height-1),e.writeU8(t.override?1:0),e.writeU8(t.variants.length);for(let i of t.variants)O(e,i.char,r),e.writeU8(i.bg),e.writeU8(i.fg);e.writeBytes(t.mask)}static encodeFill(e,t,r){e.writeU8(x.OrderType.Fill),O(e,t.char,r),e.writeU8(t.bg),e.writeU8(t.fg)}static encodeFillChar(e,t,r){e.writeU8(x.OrderType.FillChar),e.writeU8(t.patternWidth-1),e.writeU8(t.patternHeight-1),e.writeU8(t.bg),e.writeU8(t.fg);for(let i of t.pattern)O(e,i,r)}static encodeFillSprite(e,t){e.writeU8(x.OrderType.FillSprite),e.writeU8(t.spriteId),e.writeU8(t.bg),e.writeU8(t.fg)}static encodeFillSpriteMulti(e,t){e.writeU8(x.OrderType.FillSpriteMulti),e.writeU8(t.spriteId)}};var ee=new TextEncoder,xt=1,Ct=16,Ut=32,St=64,Te=[],Ee=[],te=[],re=[],Mt=[],vt=[],wt=new q(8192),Re={reliable:new Uint8Array(0),volatile:new Uint8Array(0)},Y=class l{static encode(e){let t=e.getDisplays(),r=e.getLayers(),i=e.flushCommands();Te.length=0,Ee.length=0;for(let a of r)!a.getNeedsCommit()&&!a.getIsMacroLayer()||(a.getMustBeReliable()?Te.push(a):Ee.push(a));te.length=0,re.length=0;for(let a of i)switch(a.type){case fe.OrderType.Audio:te.push(a);break;case fe.OrderType.Vibration:re.push(a);break}let n=e.getMacroRegistry(),s=n?.getPendingOrders()??[],o=n?.getPendingDefines()??[],d=l.encodePacket(t,Te,te,re,void 0,s,o,!1),u=l.encodePacket(t,Ee,Mt,vt,void 0,void 0,void 0,!1);return n?.clearPendingOrders(),n?.clearPendingDefines(),Re.reliable=d,Re.volatile=u,Re}static encodeFull(e,t){let r=e.getDisplays(),i=e.getLayers(),n=e.flushCommands();te.length=0,re.length=0;for(let c of n)switch(c.type){case fe.OrderType.Audio:te.push(c);break;case fe.OrderType.Vibration:re.push(c);break}let s;if(t){s=new Map;for(let c of r){let b=c.activePaletteSlot;t.has(b)&&!s.has(b)&&s.set(b,t.get(b))}}let o=e.getMacroRegistry(),d=o?.getAllDefines()??[],u=o?.getActiveInstanceOrders()??[],a=e.getInputBindingRegistry().toLoadPacket();return l.encodePacket(r,i,te,re,s,u,d,!0,a)}static encodePacket(e,t,r,i,n,s,o,d=!1,u){let a=wt;a.reset(),a.writeU8(e.length);for(let h of e){a.writeU8(h.id),a.writeI16BE(h.x),a.writeI16BE(h.y),a.writeU8(h.width-1),a.writeU8(h.height-1),a.writeU8(h.activePaletteSlot),a.writeU8(h.scalingMode),a.writeU8(h.cellWidth),a.writeU8(h.cellHeight);let m=h.renderPasses;if(m&&m.length>0){a.writeU8(m.length);for(let g of m)a.writeU8(g.id),a.writeU8(Math.max(0,Math.min(255,g.zMin))),a.writeU8(Math.max(0,Math.min(255,g.zMax))),a.writeU8(g.enabled!==!1?1:0)}else a.writeU8(0);l.encodePostProcess(a,h.postProcess)}if(n&&n.size>0){a.writeU8(n.size);for(let[h,m]of n){a.writeU8(h);for(let g=0;g<256;g++){let p=m[g];p?(a.writeU8(p.r??0),a.writeU8(p.g??0),a.writeU8(p.b??0),a.writeU8(p.a??0)):(a.writeU8(0),a.writeU8(0),a.writeU8(0),a.writeU8(0))}}}else a.writeU8(0);a.writeU16BE(t.length);for(let h of t){a.writeU16BE(h.id);let m=h.getPendingOrders(),g=Math.min(m.length,255),p=0;(d||h.getOrdersUpdatedThisTick())&&(p|=xt),h.isEnabled()&&(p|=Ct),h.getIsMacroLayer()&&(p|=Ut);let y=h.is16bit;y&&(p|=St),a.writeU8(p),a.writeI8(h.zIndex);let w=h.getOrigin();if(a.writeI16BE(w.x),a.writeI16BE(w.y),a.writeU8(h.getWidth()-1),a.writeU8(h.getHeight()-1),m.length>255&&console.warn(`[TickPacketEncoder] Layer ${h.id} has ${m.length} orders, truncating to 255. Consider splitting into multiple layers or reducing draw calls.`),a.writeU8(g),g>0){let A=m.length>255?m.slice(0,255):m;_.encodeInto(a,A,y)}}a.writeU8(Math.min(r.length,255));for(let h=0;h<Math.min(r.length,255);h++)l.encodeAudioOrder(a,r[h]);a.writeU8(Math.min(i.length,255));for(let h=0;h<Math.min(i.length,255);h++)l.encodeVibrationOrder(a,i[h]);let c=o??[];a.writeU16BE(c.length);for(let h of c)l.encodeMacroDefine(a,h);let b=s??[];a.writeU16BE(b.length);for(let h of b)l.encodeMacroOrder(a,h);if(a.writeU8(0),u){let h=ee.encode(JSON.stringify(u));a.writeU16BE(h.length),a.writeBytes(h)}else a.writeU16BE(0);return a.toUint8Array()}static encodeAudioOrder(e,t){if(t.stop){e.writeU8(2),l.encodeSoundTarget(e,t.sound);return}if(t.fadeOut!==void 0){e.writeU8(3),l.encodeSoundTarget(e,t.sound),e.writeU16BE(Math.round(t.fadeOut*1e3));return}if(t.pause){e.writeU8(4),l.encodeSoundTarget(e,t.sound);return}if(t.resume){e.writeU8(5),l.encodeSoundTarget(e,t.sound);return}if(t.sound==="__listener_position__"){e.writeU8(7),e.writeI16BE(t.x??0),e.writeI16BE(t.y??0);return}if(t.sound==="__configure_spatial__"){e.writeU8(8),e.writeU16BE(t.maxDistance??100),e.writeU16BE(t.referenceDistance??1),e.writeU8(t.rolloffFactor??1),e.writeU8(t.panSpread??0);return}if(t.instanceId===void 0&&typeof t.sound=="number"&&(t.lowpass!==void 0||t.highpass!==void 0||t.reverb!==void 0||t.pitch!==void 0||t.volume!==void 0)){e.writeU8(6),e.writeU16BE(t.sound);let i=0;t.lowpass!==void 0&&(i|=1),t.highpass!==void 0&&(i|=2),t.reverb!==void 0&&(i|=4),t.pitch!==void 0&&(i|=8),t.volume!==void 0&&(i|=16),e.writeU8(i),t.lowpass!==void 0&&e.writeU16BE(Math.round(t.lowpass)),t.highpass!==void 0&&e.writeU16BE(Math.round(t.highpass)),t.reverb!==void 0&&e.writeU8(Math.round(t.reverb*255)),t.pitch!==void 0&&e.writeU8(Math.round(t.pitch*100)),t.volume!==void 0&&e.writeU8(Math.round(t.volume*255));return}e.writeU8(1),l.encodeSoundTarget(e,t.sound),e.writeU16BE(t.instanceId??0);let r=0;t.volume!==void 0&&(r|=1),t.pitch!==void 0&&(r|=2),t.fadeIn!==void 0&&(r|=4),t.x!==void 0&&t.y!==void 0&&(r|=8),t.lowpass!==void 0&&(r|=16),t.highpass!==void 0&&(r|=32),t.reverb!==void 0&&(r|=64),t.loop&&(r|=128),e.writeU8(r),t.volume!==void 0&&e.writeU8(Math.round(t.volume*255)),t.pitch!==void 0&&e.writeU8(Math.round(t.pitch*100)),t.fadeIn!==void 0&&e.writeU16BE(Math.round(t.fadeIn*1e3)),t.x!==void 0&&t.y!==void 0&&(e.writeI16BE(t.x),e.writeI16BE(t.y)),t.lowpass!==void 0&&e.writeU16BE(Math.round(t.lowpass)),t.highpass!==void 0&&e.writeU16BE(Math.round(t.highpass)),t.reverb!==void 0&&e.writeU8(Math.round(t.reverb*255))}static encodeSoundTarget(e,t){if(typeof t=="number")e.writeU8(0),e.writeU16BE(t);else if(t==="all")e.writeU8(1);else{e.writeU8(2);let r=ee.encode(t);e.writeU8(r.length),e.writeBytes(r)}}static encodeVibrationOrder(e,t){if("target"in t&&t.target==="gamepad")e.writeU8(1),e.writeU8(t.gamepadIndex),e.writeU16BE(t.duration),e.writeU8(Math.round(t.strongMagnitude*255)),e.writeU8(Math.round(t.weakMagnitude*255)),e.writeU16BE(t.startDelay);else{e.writeU8(0);let r=t.pattern;if(typeof r=="number")e.writeU8(1),e.writeU16BE(r);else{e.writeU8(r.length);for(let i of r)e.writeU16BE(i)}}}static encodeMacroDefine(e,t){e.writeU16BE(t.templateId);let r=ee.encode(JSON.stringify(t.template));e.writeU16BE(r.length),e.writeBytes(r)}static encodeMacroOrder(e,t){switch(e.writeU8(t.cmd),e.writeU16BE(t.instanceId),t.cmd){case 1:{let r=t;if(e.writeU16BE(r.templateId),e.writeU16BE(r.layerId),e.writeI16BE(r.x),e.writeI16BE(r.y),e.writeU8(r.zIndex),r.params!==void 0){let i=ee.encode(JSON.stringify(r.params));e.writeU16BE(i.length),e.writeBytes(i)}else e.writeU16BE(0);break}case 2:{let r=t,i=ee.encode(JSON.stringify(r.params));e.writeU16BE(i.length),e.writeBytes(i);break}default:break}}static encodePostProcess(e,t){let r=0;if(t.ambientEffect&&(r|=1),t.scanlines&&(r|=2),t.grid&&(r|=4),e.writeU8(r),t.ambientEffect){let i=t.ambientEffect;e.writeU8(i.enabled?1:0),e.writeU8(Math.min(255,i.blur??30)),e.writeU8(Math.min(255,Math.round((i.scale??1.3)*10))),e.writeU8(Math.min(255,Math.round((i.opacity??.7)*100)))}if(t.scanlines){let i=t.scanlines;e.writeU8(i.enabled?1:0),e.writeU8(Math.min(255,Math.round((i.opacity??.15)*100)));let n={horizontal:0,vertical:1,grid:2};e.writeU8(n[i.pattern??"horizontal"]??0),e.writeU8(Math.min(255,i.spacing??2)),e.writeU8(Math.min(255,i.thickness??1)),e.writeU8(i.color?.r??0),e.writeU8(i.color?.g??0),e.writeU8(i.color?.b??0)}if(t.grid){let i=t.grid;e.writeU8(i.enabled?1:0),e.writeU8(Math.min(255,Math.round((i.lineWidth??1)*10)));let n=ee.encode(i.color??"");e.writeU8(Math.min(255,n.length)),e.writeBytes(n.subarray(0,255))}}};var ge=class{samples;head=0;count=0;windowSize;totalTicks=0;U=0;constructor(e=120){this.windowSize=e,this.samples=new Array(e);for(let t=0;t<e;t++)this.samples[t]={durationMs:0,bytes:0,layers:0,orders:0,cells:0,displays:0}}beginTick(){this.U=performance.now()}endTick(e){let t=performance.now()-this.U;this.totalTicks++;let r=this.samples[this.head];r.durationMs=t,r.bytes=e.bytes,r.layers=e.layers,r.orders=e.orders,r.cells=e.cells,r.displays=e.displays,this.head=(this.head+1)%this.windowSize,this.count<this.windowSize&&this.count++}getSnapshot(){if(this.count===0)return{totalTicks:0,lastTickDurationMs:0,avgTickDurationMs:0,peakTickDurationMs:0,lastTickBytes:0,avgTickBytes:0,lastLayerCount:0,lastOrderCount:0,lastCellCount:0,lastDisplayCount:0};let e=(this.head-1+this.windowSize)%this.windowSize,t=this.samples[e],r=0,i=0,n=0,s=this.count<this.windowSize?0:this.head;for(let o=0;o<this.count;o++){let d=this.samples[(s+o)%this.windowSize];r+=d.durationMs,n+=d.bytes,d.durationMs>i&&(i=d.durationMs)}return{totalTicks:this.totalTicks,lastTickDurationMs:t.durationMs,avgTickDurationMs:r/this.count,peakTickDurationMs:i,lastTickBytes:t.bytes,avgTickBytes:n/this.count,lastLayerCount:t.layers,lastOrderCount:t.orders,lastCellCount:t.cells,lastDisplayCount:t.displays}}reset(){this.head=0,this.count=0,this.totalTicks=0;for(let e=0;e<this.windowSize;e++){let t=this.samples[e];t.durationMs=0,t.bytes=0,t.layers=0,t.orders=0,t.cells=0,t.displays=0}}};var De=class l{static VGA_COLORS=[{colorId:0,r:0,g:0,b:0,a:255},{colorId:1,r:170,g:0,b:0,a:255},{colorId:2,r:0,g:170,b:0,a:255},{colorId:3,r:170,g:85,b:0,a:255},{colorId:4,r:0,g:0,b:170,a:255},{colorId:5,r:170,g:0,b:170,a:255},{colorId:6,r:0,g:170,b:170,a:255},{colorId:7,r:170,g:170,b:170,a:255},{colorId:8,r:85,g:85,b:85,a:255},{colorId:9,r:255,g:85,b:85,a:255},{colorId:10,r:85,g:255,b:85,a:255},{colorId:11,r:255,g:255,b:85,a:255},{colorId:12,r:85,g:85,b:255,a:255},{colorId:13,r:255,g:85,b:255,a:255},{colorId:14,r:85,g:255,b:255,a:255},{colorId:15,r:255,g:255,b:255,a:255}];static SYSTEM_COLORS=[{colorId:240,r:0,g:0,b:0,a:255},{colorId:241,r:60,g:60,b:75,a:255},{colorId:242,r:160,g:160,b:180,a:255},{colorId:243,r:255,g:255,b:255,a:255},{colorId:244,r:230,g:50,b:80,a:255},{colorId:245,r:255,g:120,b:50,a:255},{colorId:246,r:255,g:210,b:60,a:255},{colorId:247,r:130,g:220,b:50,a:255},{colorId:248,r:35,g:100,b:55,a:255},{colorId:249,r:40,g:200,b:220,a:255},{colorId:250,r:60,g:120,b:240,a:255},{colorId:251,r:35,g:55,b:125,a:255},{colorId:252,r:150,g:70,b:210,a:255},{colorId:253,r:130,g:80,b:50,a:255},{colorId:254,r:240,g:210,b:170,a:255}];mode;sessions=new Map;spriteRegistry=new ce;fontRegistry=new he;soundRegistry=new me;inputBindings=new X;macroRegistry=new pe;stats=new ge;getSpriteRegistry(){return this.spriteRegistry}resourceLoader=null;onFontAllocatedCallback=null;onFontBlockAddedCallback=null;constructor(e){this.mode=e.mode,this.initializeDefaultPalette(),this.loadFont(8,8,1,8,8)}setResourceLoader(e){this.resourceLoader=e}createUser(e,t){let r=String(e),i=t??`Player ${e}`,n=new ue(r,i);return this.spriteRegistry&&n.setSpriteRegistry(this.spriteRegistry),n.setMacroRegistry(this.macroRegistry),this.sessions.set(r,n),n}removeUser(e){return this.sessions.delete(String(e))}getUser(e){return this.sessions.get(String(e))}endTick(e){this.stats.beginTick();let t=Y.encode(e),r=e.getLayers(),i=0,n=0;for(let o of r)n+=o.getWidth()*o.getHeight(),o.getNeedsCommit()&&(i+=o.getPendingOrders().length,o.clearPendingOrders(),o.clearCommit());let s=e.getDisplays();return this.stats.endTick({bytes:t.reliable.byteLength+t.volatile.byteLength,layers:r.length,orders:i,cells:n,displays:s.length}),t}getEngineStats(){return this.stats.getSnapshot()}encodeInitialState(e){return Y.encodeFull(e,this.palettes)}onFontAllocated(e){this.onFontAllocatedCallback=e}onFontBlockAdded(e){this.onFontBlockAddedCallback=e}loadFont(e,t,r,i,n){let{width:o,height:d}=Pe(r,e,t);if(o>2048||d>2048)throw new Error(`Atlas texture ${o}\xD7${d} exceeds the maximum allowed size of 2048\xD72048. Reduce atlasBlocks (${r}) or glyph size (${e}\xD7${t}).`);let u="__default__";this.fontRegistry.getFontId(u)!==void 0&&this.fontRegistry.unregisterFont(u),this.fontRegistry.registerFont(u,{glyphWidth:e,glyphHeight:t,cellWidth:i,cellHeight:n,atlasBlocks:r}),this.onFontAllocatedCallback&&this.onFontAllocatedCallback()}async loadFontBlock(e,t){let r;if(typeof t=="string"){if(!this.resourceLoader)throw new Error("[Engine] No resource loader set. The runtime must call engine.setResourceLoader() before loading font blocks.");r=await this.resourceLoader.load(t)}else r=t;let i=this.fontRegistry.getFontId("__default__");if(i===void 0)throw new Error("[Engine] No font allocated. Call loadFont() first.");this.fontRegistry.addBlock(i,e,r),this.onFontBlockAddedCallback&&this.onFontBlockAddedCallback(e)}async loadFontBlocks(e){await Promise.all(Object.entries(e).map(([t,r])=>this.loadFontBlock(Number(t),r)))}getFontConfig(){return this.fontRegistry.getFontByName("__default__")?.getConfig()}getFontBlock(e){return this.fontRegistry.getFontByName("__default__")?.getBlock(e)}palettes=new Map;detectSoundFormat(e){let t=e.split(".").pop()?.toLowerCase();return{mp3:"mp3",wav:"wav",ogg:"ogg",webm:"webm",aac:"aac",m4a:"aac"}[t||""]||"mp3"}async loadSound(e,t){if(!this.resourceLoader)throw new Error("[Engine] No resource loader set. The runtime must call engine.setResourceLoader() before loading sounds.");let r=this.detectSoundFormat(t),i=await this.resourceLoader.load(t);return this.soundRegistry.registerFile(e,r,i)}async loadSounds(e){let t=Object.entries(e),r=await Promise.all(t.map(([n,s])=>this.loadSound(n,s))),i={};return t.forEach(([n],s)=>{i[n]=r[s]}),i}initializeDefaultPalette(){let e=new Array(256);for(let t=0;t<256;t++)e[t]={colorId:t,r:0,g:0,b:0,a:0};for(let t of l.VGA_COLORS)e[t.colorId]={...t};for(let t of l.SYSTEM_COLORS)e[t.colorId]={...t};e[255]={colorId:255,r:0,g:0,b:0,a:0},this.palettes.set(0,e)}loadPaletteToSlot(e,t){let r=new Array(256);for(let i=0;i<256;i++)r[i]={colorId:i,r:0,g:0,b:0,a:0};for(let i of t){let n=i.colorId;n>=0&&n<240&&(r[n]={colorId:n,r:i.r??0,g:i.g??0,b:i.b??0,a:i.a??255})}for(let i of l.SYSTEM_COLORS)r[i.colorId]={...i};r[255]={colorId:255,r:0,g:0,b:0,a:0},this.palettes.set(e,r)}getPalette(e){return this.palettes.get(e)}};var Le=class{h=new Uint8Array(0);e=[];m=[];S=[];f=void 0;ensureMask(e){return this.h.length<e?this.h=new Uint8Array(e):this.h.fill(0,0,e),this.h}ensurePassGrids(e,t,r){for(;this.e.length<e;)this.e.push(new z(t,r)),this.m.push(new Uint8Array(t*r));for(let i=0;i<e;i++){let n=this.e[i];(n.width!==t||n.height!==r)&&(this.e[i]=new z(t,r),this.m[i]=new Uint8Array(t*r)),this.e[i].clear(),this.m[i].fill(0,0,t*r)}}composite(e,t,r){let i=e.renderPasses;if(!i||i.length===0){this.compositeSinglePass(e,t,r),this.f=void 0;return}else{let n=this.compositeMultiPass(e,t,i,r);return this.f=n,n}}getLastPasses(){return this.f}compositeSinglePass(e,t,r){let i=this.S;i.length=0;for(let s=0;s<t.length;s++)i.push(t[s]);i.sort((s,o)=>o.zIndex-s.zIndex),r.clear();let n=this.ensureMask(r.size);for(let s=0;s<i.length;s++){let o=i[s];o.visible&&this.compositeLayer(e,o,r,n)}}compositeMultiPass(e,t,r,i){let n=0;for(let u=0;u<r.length;u++)r[u].enabled!==!1&&n++;if(n===0){this.compositeSinglePass(e,t,i);return}let s=this.S;s.length=0;for(let u=0;u<t.length;u++)t[u].visible&&s.push(t[u]);s.sort((u,a)=>a.zIndex-u.zIndex),this.ensurePassGrids(n,e.width,e.height);let o=[],d=0;for(let u=0;u<r.length;u++){let a=r[u];if(a.enabled===!1)continue;let c=this.e[d],b=this.m[d];for(let h=0;h<s.length;h++){let m=s[h];m.zIndex>=a.zMin&&m.zIndex<=a.zMax&&this.compositeLayer(e,m,c,b)}o.push({id:a.id,zMin:a.zMin,zMax:a.zMax,gridData:c.data}),d++}i.clear();for(let u=0;u<o.length;u++)this.overlayPass(this.e[u],i);return o}overlayPass(e,t){let r=e.size;for(let i=0;i<r;i++){let n=e.data[i],s=n>>>24&255,o=n>>>16&255,d=n&65535;if(s!==255){t.data[i]=n;continue}if(d===0&&o===255)continue;let u=t.data[i],a=u>>>24&255,c=u>>>16&255,b=u&65535;d!==0&&d!==32&&(b=d),o!==255&&(c=o),t.data[i]=a<<24|c<<16|b}}compositeLayer(e,t,r,i){let n=Math.max(e.x,t.x),s=Math.min(e.x+e.width,t.x+t.grid.width),o=Math.max(e.y,t.y),d=Math.min(e.y+e.height,t.y+t.grid.height);if(n>=s||o>=d)return;let u=s-n,a=d-o,c=e.width,b=t.grid.width,h=(o-e.y)*c+(n-e.x),m=(o-t.y)*b+(n-t.x);for(let g=0;g<a;g++){let p=h,y=m;for(let w=0;w<u;w++){if(!i[p]){let A=t.grid.data[y],v=A>>>24&255,L=A>>>16&255,Z=A&65535,N=r.data[p],E=N>>>24&255,R=N>>>16&255,F=N&65535;Z!==0&&(F===0||F===32)&&(F=Z),L!==255&&R===255&&(R=L),v!==255&&(E===255&&(E=v),i[p]=1),r.data[p]=E<<24|R<<16|F}p++,y++}h+=c,m+=b}}};var U=require("@primitiv/types");var Ve=class l{orders=[];static toCharCode(e){return typeof e=="number"?e&65535:e.length===0?32:D(e.charCodeAt(0))}static dedent(e){let t=e.split(`
|
|
3
|
+
`);t.length>0&&t[0].trim()===""&&t.shift();let r=1/0;for(let i of t){if(i.trim().length===0)continue;let n=i.match(/^(\s*)/);n&&(r=Math.min(r,n[1].length))}return r===1/0?e.trim():t.map(i=>i.slice(r)).join(`
|
|
4
|
+
`).trim()}static encodeString(e){let t="";for(let r=0;r<e.length;r++){let i=e.charCodeAt(r),n=D(i);t+=String.fromCharCode(n)}return t}getOrders(){return this.orders}clear(){return this.orders=[],this}char(e,t,r,i=15,n=255){return this.orders.push(l.char(e,t,r,i,n)),this}static char(e,t,r,i=15,n=255){return{type:U.OrderType.Char,x:e,y:t,char:l.toCharCode(r),fg:i,bg:n}}text(e,t,r,i=15,n=255){return this.orders.push(l.text(e,t,r,i,n)),this}static text(e,t,r,i=15,n=255){return{type:U.OrderType.Text,x:e,y:t,text:l.encodeString(r),fg:i,bg:n}}textMultiline(e,t,r,i=15,n=255){return this.orders.push(l.textMultiline(e,t,r,i,n)),this}static textMultiline(e,t,r,i=15,n=255){return{type:U.OrderType.TextMultiline,x:e,y:t,text:l.encodeString(l.dedent(r)),fg:i,bg:n}}subFrame(e,t,r,i,n,s=15,o=255){let d={type:U.OrderType.SubFrame,x:e,y:t,width:r,height:i,frame:n.map(u=>l.toCharCode(u)),fg:s,bg:o};return this.orders.push(d),this}static subFrame(e,t,r,i,n,s=15,o=255){return{type:U.OrderType.SubFrame,x:e,y:t,width:r,height:i,frame:n.map(d=>l.toCharCode(d)),fg:s,bg:o}}subFrameMulti(e,t,r,i,n){return this.orders.push(l.subFrameMulti(e,t,r,i,n)),this}static subFrameMulti(e,t,r,i,n){return{type:U.OrderType.SubFrameMulti,x:e,y:t,width:r,height:i,frame:n.map(s=>({char:l.toCharCode(s.charCode??s.char??0),fg:s.fgColorCode??s.fgColor??s.fg??15,bg:s.bgColorCode??s.bgColor??s.bg??255}))}}fullFrame(e,t=15,r=255){return this.orders.push(l.fullFrame(e,t,r)),this}static fullFrame(e,t=15,r=255){return{type:U.OrderType.FullFrame,frame:e.map(i=>l.toCharCode(i)),fg:t,bg:r}}fullFrameMulti(e){return this.orders.push(l.fullFrameMulti(e)),this}static fullFrameMulti(e){return{type:U.OrderType.FullFrameMulti,frame:e.map(t=>({char:l.toCharCode(t.charCode??t.char??0),fg:t.fgColorCode??t.fgColor??t.fg??15,bg:t.bgColorCode??t.bgColor??t.bg??255}))}}sprite(e,t,r,i=15,n=255){return this.orders.push(l.sprite(e,t,r,i,n)),this}static sprite(e,t,r,i=15,n=255){return{type:U.OrderType.Sprite,x:e,y:t,spriteId:r,fg:i,bg:n}}spriteMulti(e,t,r){return this.orders.push(l.spriteMulti(e,t,r)),this}static spriteMulti(e,t,r){return{type:U.OrderType.SpriteMulti,x:e,y:t,spriteId:r}}colorMap(e,t,r,i,n){let s={type:U.OrderType.ColorMap,x:e,y:t,width:r,height:i,data:n};return this.orders.push(s),this}rect(e,t,r,i,n="\u2588",s=15,o=255,d=!0){return this.orders.push(l.rect(e,t,r,i,n,s,o,d)),this}static rect(e,t,r,i,n="\u2588",s=15,o=255,d=!0){return{type:U.OrderType.Shape,shapeType:U.ShapeType.Rectangle,x:e,y:t,width:r,height:i,filled:d,char:l.toCharCode(n),fg:s,bg:o}}circle(e,t,r,i,n=15,s=255,o=!0){let d={type:U.OrderType.Shape,shapeType:U.ShapeType.Circle,x:e,y:t,radius:r,filled:o,char:l.toCharCode(i),fg:n,bg:s};return this.orders.push(d),this}static circle(e,t,r,i="\u2588",n=15,s=255,o=!0){let d,u=n,a=s,c=o;return typeof i=="object"?(d=l.toCharCode(i.charCode??"\u2588"),u=i.fgColor??15,a=i.bgColor??255,c=i.filled??!0):d=l.toCharCode(i),{type:U.OrderType.Shape,shapeType:U.ShapeType.Circle,x:e,y:t,radius:r,filled:c,char:d,fg:u,bg:a}}line(e,t,r,i,n,s=15,o=255){return this.orders.push(l.line(e,t,r,i,n,s,o)),this}static line(e,t,r,i,n,s=15,o=255){let d,u=s,a=o;return typeof n=="object"?(d=l.toCharCode(n.charCode??"#"),u=n.fgColor??15,a=n.bgColor??255):d=l.toCharCode(n),{type:U.OrderType.Shape,shapeType:U.ShapeType.Line,x1:e,y1:t,x2:r,y2:i,filled:!1,char:d,fg:u,bg:a}}triangle(e,t,r,i,n,s,o,d=15,u=255,a=!0){let c={type:U.OrderType.Shape,shapeType:U.ShapeType.Triangle,x1:e,y1:t,x2:r,y2:i,x3:n,y3:s,filled:a,char:l.toCharCode(o),fg:d,bg:u};return this.orders.push(c),this}static triangle(e,t,r,i,n,s,o="\u2588",d=15,u=255,a=!0){let c,b=d,h=u,m=a;return typeof o=="object"?(c=l.toCharCode(o.charCode??"\u2588"),b=o.fgColor??15,h=o.bgColor??255,m=o.filled??!0):c=l.toCharCode(o),{type:U.OrderType.Shape,shapeType:U.ShapeType.Triangle,x1:e,y1:t,x2:r,y2:i,x3:n,y3:s,filled:m,char:c,fg:b,bg:h}}ellipse(e,t,r,i,n,s=15,o=255,d=!0){let u={type:U.OrderType.Shape,shapeType:U.ShapeType.Ellipse,x:e,y:t,radiusX:r,radiusY:i,filled:d,char:l.toCharCode(n),fg:s,bg:o};return this.orders.push(u),this}static ellipse(e,t,r,i,n="\u2588",s=15,o=255,d=!0){let u,a=s,c=o,b=d;return typeof n=="object"?(u=l.toCharCode(n.charCode??"\u2588"),a=n.fgColor??15,c=n.bgColor??255,b=n.filled??!0):u=l.toCharCode(n),{type:U.OrderType.Shape,shapeType:U.ShapeType.Ellipse,x:e,y:t,radiusX:r,radiusY:i,filled:b,char:u,fg:a,bg:c}}polyline(e,t,r=15,i=255){return this.orders.push(l.polyline(e,t,r,i)),this}static polyline(e,t,r=15,i=255){return{type:U.OrderType.Polyline,points:e,char:l.toCharCode(t),fg:r,bg:i}}polygon(e,t,r=15,i=255){return this.orders.push(l.polygon(e,t,r,i)),this}static polygon(e,t,r=15,i=255){let n=[...e];return n.length>0&&n.push(n[0]),{type:U.OrderType.Polyline,points:n,char:l.toCharCode(t),fg:r,bg:i}}dotCloud(e,t,r=15,i=255){return this.orders.push(l.dotCloud(e,t,r,i)),this}static dotCloud(e,t,r=15,i=255){return{type:U.OrderType.DotCloud,positions:e.map(n=>({x:n.posX??n.x??0,y:n.posY??n.y??0})),char:l.toCharCode(t),fg:r,bg:i}}dotCloudMulti(e){let t={type:U.OrderType.DotCloudMulti,dots:e.map(r=>({...r,char:l.toCharCode(r.char)}))};return this.orders.push(t),this}static dotCloudMulti(e){return{type:U.OrderType.DotCloudMulti,dots:e.map(t=>({x:t.posX??t.x??0,y:t.posY??t.y??0,char:l.toCharCode(t.charCode??t.char??0),fg:t.fgColorCode??t.fgColor??t.fg??15,bg:t.bgColorCode??t.bgColor??t.bg??255}))}}spriteCloud(e,t,r=15,i=255){let n={type:U.OrderType.SpriteCloud,positions:e,spriteId:t,fg:r,bg:i};return this.orders.push(n),this}static spriteCloud(e,t,r=15,i=255){return{type:U.OrderType.SpriteCloud,positions:t.map(n=>({x:n.posX??n.x??0,y:n.posY??n.y??0})),spriteId:e,fg:r,bg:i}}spriteCloudMulti(e,t){let r={type:U.OrderType.SpriteCloudMulti,positions:e,spriteId:t};return this.orders.push(r),this}static spriteCloudMulti(e,t){return{type:U.OrderType.SpriteCloudMulti,positions:t.map(r=>({x:r.posX??r.x??0,y:r.posY??r.y??0})),spriteId:e}}spriteCloudVaried(e){let t={type:U.OrderType.SpriteCloudVaried,sprites:e};return this.orders.push(t),this}static spriteCloudVaried(e){return{type:U.OrderType.SpriteCloudVaried,sprites:e.map(t=>({x:t.posX??t.x??0,y:t.posY??t.y??0,spriteId:t.spriteIndex??t.spriteId??0,fg:t.fgColorCode??t.fgColor??t.fg??15,bg:t.bgColorCode??t.bgColor??t.bg??255}))}}spriteCloudVariedMulti(e){let t={type:U.OrderType.SpriteCloudVariedMulti,sprites:e};return this.orders.push(t),this}static spriteCloudVariedMulti(e){return{type:U.OrderType.SpriteCloudVariedMulti,sprites:e.map(t=>({x:t.posX??t.x??0,y:t.posY??t.y??0,spriteId:t.spriteIndex??t.spriteId??0}))}}bitmask(e,t,r,i,n,s,o=15,d=255,u=!1){let a=r*i,c=Math.ceil(a/8),b=new Uint8Array(c);for(let m=0;m<a;m++)if(n[m]&1){let g=Math.floor(m/8),p=m%8;b[g]|=1<<p}let h={type:U.OrderType.Bitmask,x:e,y:t,width:r,height:i,mask:b,char:l.toCharCode(s),fg:o,bg:d,override:u};return this.orders.push(h),this}static bitmask(e,t,r,i,n,s,o=15,d=255,u=!1){let a=r*i,c=Math.ceil(a/8),b=new Uint8Array(c);for(let h=0;h<a;h++)if(n[h]){let m=Math.floor(h/8),g=h%8;b[m]|=1<<g}return{type:U.OrderType.Bitmask,x:e,y:t,width:r,height:i,mask:b,char:l.toCharCode(s),fg:o,bg:d,override:u}}bitmask4(e,t,r,i,n,s,o=!1){return this.orders.push(l.bitmask4(e,t,r,i,n,s,o)),this}static bitmask4(e,t,r,i,n,s,o=!1){if(s.length!==3)throw new Error("Bitmask4 requires exactly 3 variants");let d=r*i,u=Math.ceil(d/4),a=new Uint8Array(u);for(let b=0;b<d;b++){let h=n[b]&3;if(h>0){let m=Math.floor(b/4),p=b%4*2;a[m]|=h<<p}}let c=b=>({char:l.toCharCode(b.charCode??b.char),fg:b.fg??b.fgColor??15,bg:b.bg??b.bgColor??0});return{type:U.OrderType.Bitmask4,x:e,y:t,width:r,height:i,mask:a,variants:[c(s[0]),c(s[1]),c(s[2])],override:o}}bitmask16(e,t,r,i,n,s,o=!1){if(s.length>15)throw new Error("Bitmask16 supports max 15 variants");let d=r*i,u=Math.ceil(d/2),a=new Uint8Array(u);for(let h=0;h<d;h++){let m=n[h]&15;if(m>0){let g=Math.floor(h/2),y=h%2*4;a[g]|=m<<y}}let c=h=>({char:l.toCharCode(h.charCode??h.char),fg:h.fg??h.fgColor??15,bg:h.bg??h.bgColor??0}),b={type:U.OrderType.Bitmask16,x:e,y:t,width:r,height:i,mask:a,variants:s.map(c),override:o};return this.orders.push(b),this}static bitmask16(e,t,r,i,n,s,o=!1){if(s.length>15)throw new Error("Bitmask16 supports max 15 variants");let d=r*i,u=Math.ceil(d/2),a=new Uint8Array(u);for(let b=0;b<d;b++){let h=n[b]&15;if(h>0){let m=Math.floor(b/2),p=b%2*4;a[m]|=h<<p}}let c=b=>({char:l.toCharCode(b.charCode??b.char),fg:b.fg??b.fgColor??15,bg:b.bg??b.bgColor??0});return{type:U.OrderType.Bitmask16,x:e,y:t,width:r,height:i,mask:a,variants:s.map(c),override:o}}fill(e,t=15,r=255){return this.orders.push(l.fill(e,t,r)),this}static fill(e,t=15,r=255){return{type:U.OrderType.Fill,char:l.toCharCode(e),fg:t,bg:r}}fillChar(e,t,r,i=15,n=255){return this.orders.push(l.fillChar(e,t,r,i,n)),this}static fillChar(e,t,r,i=15,n=255){return{type:U.OrderType.FillChar,patternWidth:e,patternHeight:t,pattern:r.map(s=>l.toCharCode(s)),fg:i,bg:n}}fillSprite(e,t=15,r=255){return this.orders.push(l.fillSprite(e,t,r)),this}static fillSprite(e,t=15,r=255){return{type:U.OrderType.FillSprite,spriteId:e,fg:t,bg:r}}fillSpriteMulti(e){return this.orders.push(l.fillSpriteMulti(e)),this}static fillSpriteMulti(e){return{type:U.OrderType.FillSpriteMulti,spriteId:e}}boxWithBorder(e,t,r,i,n,s){let o=l.boxWithBorder(e,t,r,i,n,s);return this.orders.push(...o),this}static boxWithBorder(e,t,r,i,n={},s={}){return[l.rect(e+1,t+1,r-2,i-2,s.charCode??"\u2588",s.fgColor??8,s.bgColor??0,!0),l.rect(e,t,r,i,n.charCode??"\u2588",n.fgColor??15,n.bgColor??0,!1)]}grid(e,t,r,i,n,s,o="+",d=15,u=255){return this.orders.push(l.grid(e,t,r,i,n,s,o,d,u)),this}static grid(e,t,r,i,n,s,o="+",d=15,u=255){let a=[];for(let c=0;c<=n;c++)for(let b=0;b<=s;b++)a.push({x:e+b*r,y:t+c*i});return l.dotCloud(a,o,d,u)}};var H=class{l;u;offset;constructor(e,t=0){this.l=e,this.u=new DataView(e.buffer,e.byteOffset,e.byteLength),this.offset=t}get remaining(){return this.l.length-this.offset}get rawBuffer(){return this.l}readU8(){return this.l[this.offset++]}readI8(){let e=this.u.getInt8(this.offset);return this.offset+=1,e}readU16BE(){let e=this.u.getUint16(this.offset,!1);return this.offset+=2,e}readI16BE(){let e=this.u.getInt16(this.offset,!1);return this.offset+=2,e}readU16LE(){let e=this.u.getUint16(this.offset,!0);return this.offset+=2,e}readBytes(e){let t=this.l.slice(this.offset,this.offset+e);return this.offset+=e,t}};var C=require("@primitiv/types");var Ke=new TextDecoder;function I(l,e){return e?l.readU16LE():l.readU8()}var ie=class l{static decode(e,t=!1,r=0,i=0){let n=new H(e),s=[];for(;n.remaining>0;){let o=n.readU8(),d=l.decodeOne(n,o,t,r,i);d&&s.push(d)}return s}static decodeN(e,t,r,i,n){let s=new Array(t);for(let o=0;o<t;o++){let d=e.readU8(),u=l.decodeOne(e,d,r,i,n);s[o]=u}return s}static decodeOne(e,t,r,i,n){switch(t){case C.OrderType.Char:return l.decodeChar(e,r);case C.OrderType.Text:return l.decodeText(e);case C.OrderType.TextMultiline:return l.decodeTextMultiline(e);case C.OrderType.SubFrame:return l.decodeSubFrame(e,r);case C.OrderType.SubFrameMulti:return l.decodeSubFrameMulti(e,r);case C.OrderType.FullFrame:return l.decodeFullFrame(e,r,i,n);case C.OrderType.FullFrameMulti:return l.decodeFullFrameMulti(e,r,i,n);case C.OrderType.Sprite:return l.decodeSprite(e);case C.OrderType.SpriteMulti:return l.decodeSpriteMulti(e);case C.OrderType.ColorMap:return l.decodeColorMap(e);case C.OrderType.Shape:return l.decodeShape(e,r);case C.OrderType.Polyline:return l.decodePolyline(e,r);case C.OrderType.DotCloud:return l.decodeDotCloud(e,r);case C.OrderType.DotCloudMulti:return l.decodeDotCloudMulti(e,r);case C.OrderType.SpriteCloud:return l.decodeSpriteCloud(e);case C.OrderType.SpriteCloudMulti:return l.decodeSpriteCloudMulti(e);case C.OrderType.SpriteCloudVaried:return l.decodeSpriteCloudVaried(e);case C.OrderType.SpriteCloudVariedMulti:return l.decodeSpriteCloudVariedMulti(e);case C.OrderType.Bitmask:return l.decodeBitmask(e,r);case C.OrderType.Bitmask4:return l.decodeBitmask4(e,r);case C.OrderType.Bitmask16:return l.decodeBitmask16(e,r);case C.OrderType.Fill:return l.decodeFill(e,r);case C.OrderType.FillChar:return l.decodeFillChar(e,r);case C.OrderType.FillSprite:return l.decodeFillSprite(e);case C.OrderType.FillSpriteMulti:return l.decodeFillSpriteMulti(e);default:return null}}static decodeChar(e,t){let r=e.readU8(),i=e.readU8(),n=I(e,t),s=e.readU8(),o=e.readU8();return{type:C.OrderType.Char,x:r,y:i,char:n,fg:o,bg:s}}static decodeText(e){let t=e.readU8(),r=e.readU8(),i=e.readU8(),n=e.readBytes(i),s=Ke.decode(n),o=e.readU8(),d=e.readU8();return{type:C.OrderType.Text,x:t,y:r,text:s,fg:d,bg:o}}static decodeTextMultiline(e){let t=e.readU8(),r=e.readU8(),i=e.readU8(),n=e.readBytes(i),s=Ke.decode(n),o=e.readU8(),d=e.readU8();return{type:C.OrderType.TextMultiline,x:t,y:r,text:s,fg:d,bg:o}}static decodeSubFrame(e,t){let r=e.readU8(),i=e.readU8(),n=e.readU8()+1,s=e.readU8()+1,o=e.readU8(),d=e.readU8(),u=n*s,a=new Array(u);for(let c=0;c<u;c++)a[c]=I(e,t);return{type:C.OrderType.SubFrame,x:r,y:i,width:n,height:s,frame:a,fg:d,bg:o}}static decodeSubFrameMulti(e,t){let r=e.readU8(),i=e.readU8(),n=e.readU8()+1,s=e.readU8()+1,o=n*s,d=new Array(o);for(let u=0;u<o;u++){let a=I(e,t),c=e.readU8(),b=e.readU8();d[u]={char:a,fg:b,bg:c}}return{type:C.OrderType.SubFrameMulti,x:r,y:i,width:n,height:s,frame:d}}static decodeFullFrame(e,t,r,i){let n=e.readU8(),s=e.readU8(),o=r*i,d=new Array(o);for(let u=0;u<o;u++)d[u]=I(e,t);return{type:C.OrderType.FullFrame,frame:d,fg:s,bg:n}}static decodeFullFrameMulti(e,t,r,i){let n=r*i,s=new Array(n);for(let o=0;o<n;o++){let d=I(e,t),u=e.readU8(),a=e.readU8();s[o]={char:d,fg:a,bg:u}}return{type:C.OrderType.FullFrameMulti,frame:s}}static decodeSprite(e){let t=e.readU8(),r=e.readU8(),i=e.readU8(),n=e.readU8(),s=e.readU8();return{type:C.OrderType.Sprite,x:t,y:r,spriteId:i,fg:s,bg:n}}static decodeSpriteMulti(e){let t=e.readU8(),r=e.readU8(),i=e.readU8();return{type:C.OrderType.SpriteMulti,x:t,y:r,spriteId:i}}static decodeColorMap(e){let t=e.readU8(),r=e.readU8(),i=e.readU8()+1,n=e.readU8()+1,s=i*n,o=new Array(s);for(let d=0;d<s;d++){let u=e.readU8(),a=e.readU8();o[d]={fg:a,bg:u}}return{type:C.OrderType.ColorMap,x:t,y:r,width:i,height:n,data:o}}static decodeShape(e,t){let r=e.readU8(),i={type:C.OrderType.Shape,shapeType:r,filled:!1,char:0,fg:0,bg:0};switch(r){case C.ShapeType.Rectangle:{let n=e.readU8(),s=e.readU8(),o=e.readU8()+1,d=e.readU8()+1,u=e.readU8()!==0,a=I(e,t),c=e.readU8(),b=e.readU8();return{...i,x:n,y:s,width:o,height:d,filled:u,char:a,fg:b,bg:c}}case C.ShapeType.Circle:{let n=e.readU8(),s=e.readU8(),o=e.readU8()+1,d=e.readU8()!==0,u=I(e,t),a=e.readU8(),c=e.readU8();return{...i,x:n,y:s,radius:o,filled:d,char:u,fg:c,bg:a}}case C.ShapeType.Line:{let n=e.readU8(),s=e.readU8(),o=e.readU8(),d=e.readU8(),u=I(e,t),a=e.readU8(),c=e.readU8();return{...i,x1:n,y1:s,x2:o,y2:d,filled:!1,char:u,fg:c,bg:a}}case C.ShapeType.Ellipse:{let n=e.readU8(),s=e.readU8(),o=e.readU8()+1,d=e.readU8()+1,u=e.readU8()!==0,a=I(e,t),c=e.readU8(),b=e.readU8();return{...i,x:n,y:s,radiusX:o,radiusY:d,filled:u,char:a,fg:b,bg:c}}case C.ShapeType.Triangle:{let n=e.readU8(),s=e.readU8(),o=e.readU8(),d=e.readU8(),u=e.readU8(),a=e.readU8(),c=e.readU8()!==0,b=I(e,t),h=e.readU8(),m=e.readU8();return{...i,x1:n,y1:s,x2:o,y2:d,x3:u,y3:a,filled:c,char:b,fg:m,bg:h}}default:return i}}static decodePolyline(e,t){let r=I(e,t),i=e.readU8(),n=e.readU8(),s=e.readU8(),o=new Array(s);for(let d=0;d<s;d++)o[d]={x:e.readU8(),y:e.readU8()};return{type:C.OrderType.Polyline,points:o,char:r,fg:i,bg:n}}static decodeDotCloud(e,t){let r=I(e,t),i=e.readU8(),n=e.readU8(),s=e.readU16BE(),o=new Array(s);for(let d=0;d<s;d++)o[d]={x:e.readU8(),y:e.readU8()};return{type:C.OrderType.DotCloud,positions:o,char:r,fg:n,bg:i}}static decodeDotCloudMulti(e,t){let r=e.readU16BE(),i=new Array(r);for(let n=0;n<r;n++){let s=I(e,t),o=e.readU8(),d=e.readU8(),u=e.readU8(),a=e.readU8();i[n]={x:u,y:a,char:s,fg:d,bg:o}}return{type:C.OrderType.DotCloudMulti,dots:i}}static decodeSpriteCloud(e){let t=e.readU8(),r=e.readU8(),i=e.readU8(),n=e.readU16BE(),s=new Array(n);for(let o=0;o<n;o++)s[o]={x:e.readU8(),y:e.readU8()};return{type:C.OrderType.SpriteCloud,positions:s,spriteId:t,fg:i,bg:r}}static decodeSpriteCloudMulti(e){let t=e.readU8(),r=e.readU16BE(),i=new Array(r);for(let n=0;n<r;n++)i[n]={x:e.readU8(),y:e.readU8()};return{type:C.OrderType.SpriteCloudMulti,positions:i,spriteId:t}}static decodeSpriteCloudVaried(e){let t=e.readU16BE(),r=new Array(t);for(let i=0;i<t;i++){let n=e.readU8(),s=e.readU8(),o=e.readU8(),d=e.readU8(),u=e.readU8();r[i]={x:d,y:u,spriteId:n,fg:o,bg:s}}return{type:C.OrderType.SpriteCloudVaried,sprites:r}}static decodeSpriteCloudVariedMulti(e){let t=e.readU16BE(),r=new Array(t);for(let i=0;i<t;i++){let n=e.readU8(),s=e.readU8(),o=e.readU8();r[i]={x:s,y:o,spriteId:n}}return{type:C.OrderType.SpriteCloudVariedMulti,sprites:r}}static decodeBitmask(e,t){let r=e.readU8(),i=e.readU8(),n=e.readU8()+1,s=e.readU8()+1,o=I(e,t),d=e.readU8(),u=e.readU8(),a=e.readU8()!==0,c=Math.ceil(n*s/8),b=e.readBytes(c);return{type:C.OrderType.Bitmask,x:r,y:i,width:n,height:s,mask:b,char:o,fg:u,bg:d,override:a}}static decodeBitmask4(e,t){let r=e.readU8(),i=e.readU8(),n=e.readU8()+1,s=e.readU8()+1,o=e.readU8()!==0,d=[{char:0,fg:0,bg:0},{char:0,fg:0,bg:0},{char:0,fg:0,bg:0}];for(let c=0;c<3;c++)d[c].char=I(e,t),d[c].bg=e.readU8(),d[c].fg=e.readU8();let u=Math.ceil(n*s*2/8),a=e.readBytes(u);return{type:C.OrderType.Bitmask4,x:r,y:i,width:n,height:s,mask:a,variants:d,override:o}}static decodeBitmask16(e,t){let r=e.readU8(),i=e.readU8(),n=e.readU8()+1,s=e.readU8()+1,o=e.readU8()!==0,d=e.readU8(),u=new Array(d);for(let b=0;b<d;b++){let h=I(e,t),m=e.readU8(),g=e.readU8();u[b]={char:h,fg:g,bg:m}}let a=Math.ceil(n*s*4/8),c=e.readBytes(a);return{type:C.OrderType.Bitmask16,x:r,y:i,width:n,height:s,mask:c,variants:u,override:o}}static decodeFill(e,t){let r=I(e,t),i=e.readU8(),n=e.readU8();return{type:C.OrderType.Fill,char:r,fg:n,bg:i}}static decodeFillChar(e,t){let r=e.readU8()+1,i=e.readU8()+1,n=e.readU8(),s=e.readU8(),o=r*i,d=new Array(o);for(let u=0;u<o;u++)d[u]=I(e,t);return{type:C.OrderType.FillChar,patternWidth:r,patternHeight:i,pattern:d,fg:s,bg:n}}static decodeFillSprite(e){let t=e.readU8(),r=e.readU8(),i=e.readU8();return{type:C.OrderType.FillSprite,spriteId:t,fg:i,bg:r}}static decodeFillSpriteMulti(e){let t=e.readU8();return{type:C.OrderType.FillSpriteMulti,spriteId:t}}};var P=require("@primitiv/types");var Ot=1,It=16,Bt=32,kt=64,ne=new TextDecoder,Se=class l{static decode(e){let t=new H(e),r=t.offset,i=t.readU8(),n=new Array(i);for(let M=0;M<i;M++){let k=t.readU8(),j=t.readI16BE(),T=t.readI16BE(),Me=t.readU8()+1,ve=t.readU8()+1,we=t.readU8(),ye=t.readU8(),Oe=t.readU8(),Ie=t.readU8(),se=t.readU8(),Q;if(se>0){Q=new Array(se);for(let J=0;J<se;J++)Q[J]={id:t.readU8(),zMin:t.readU8(),zMax:t.readU8(),enabled:(t.readU8()&1)!==0}}let xe=l.decodePostProcess(t);n[M]={id:k,originX:j,originY:T,width:Me,height:ve,paletteSlot:we,scalingMode:ye,cellWidth:Oe,cellHeight:Ie,renderPasses:Q,postProcess:xe}}let s=t.offset-r,o=t.offset,d=t.readU8(),u=new Array(d);for(let M=0;M<d;M++){let k=t.readU8(),j=new Array(256);for(let T=0;T<256;T++)j[T]={colorId:T,r:t.readU8(),g:t.readU8(),b:t.readU8(),a:t.readU8()};u[M]={slot:k,colors:j}}let a=t.offset-o,c=t.offset,b=t.readU16BE(),h=new Array(b);for(let M=0;M<b;M++){let k=t.offset,j=t.readU16BE(),T=t.readU8(),Me=(T&Ot)!==0,ve=(T&It)!==0,we=(T&Bt)!==0,ye=(T&kt)!==0,Oe=t.readI8(),Ie=t.readI16BE(),se=t.readI16BE(),Q=t.readU8()+1,xe=t.readU8()+1,J=t.readU8(),Ge=[];J>0&&(Ge=l.decodeOrders(t,J,ye,Q,xe));let et=t.offset-k;h[M]={id:j,enabled:ve,isMacro:we,is16bit:ye,hasOrders:Me,zIndex:Oe,originX:Ie,originY:se,width:Q,height:xe,orders:Ge,byteSize:et}}let m=t.offset-c,g=t.offset,p=t.readU8(),y=new Array(p);for(let M=0;M<p;M++)y[M]=l.decodeAudioOrder(t);let w=t.offset-g,A=t.offset,v=t.readU8(),L=new Array(v);for(let M=0;M<v;M++)L[M]=l.decodeVibrationOrder(t);let Z=t.offset-A,N=t.offset,E=[];if(t.remaining>=2){let M=t.readU16BE();E=new Array(M);for(let k=0;k<M;k++)E[k]=l.decodeMacroDefine(t)}let R=t.offset-N,F=t.offset,V=[];if(t.remaining>=2){let M=t.readU16BE();V=new Array(M);for(let k=0;k<M;k++)V[k]=l.decodeMacroOrder(t)}let qe=t.offset-F;t.remaining>0&&t.readU8();let We;if(t.remaining>=2){let M=t.readU16BE();if(M>0){let k=t.readBytes(M);We=JSON.parse(ne.decode(k))}}let _e={displays:s,palettes:a,layers:m,audio:w,vibration:Z,macroDefines:R,macroOrders:qe,total:e.byteLength};return{displays:n,palettes:u,layers:h,audioOrders:y,vibrationOrders:L,macroDefines:E,macroOrders:V,inputBindings:We,sectionSizes:_e}}static decodeOrders(e,t,r,i,n){return ie.decodeN(e,t,r,i,n)}static decodeAudioOrder(e){switch(e.readU8()){case 1:{let r=l.decodeSoundTarget(e),i=e.readU16BE(),n=e.readU8(),s={type:P.OrderType.Audio,sound:r,instanceId:i};return n&1&&(s.volume=e.readU8()/255),n&2&&(s.pitch=e.readU8()/100),n&4&&(s.fadeIn=e.readU16BE()/1e3),n&8&&(s.x=e.readI16BE(),s.y=e.readI16BE()),n&16&&(s.lowpass=e.readU16BE()),n&32&&(s.highpass=e.readU16BE()),n&64&&(s.reverb=e.readU8()/255),n&128&&(s.loop=!0),s}case 2:{let r=l.decodeSoundTarget(e);return{type:P.OrderType.Audio,sound:r,stop:!0}}case 3:{let r=l.decodeSoundTarget(e),i=e.readU16BE();return{type:P.OrderType.Audio,sound:r,fadeOut:i/1e3}}case 4:{let r=l.decodeSoundTarget(e);return{type:P.OrderType.Audio,sound:r,pause:!0}}case 5:{let r=l.decodeSoundTarget(e);return{type:P.OrderType.Audio,sound:r,resume:!0}}case 6:{let r=e.readU16BE(),i=e.readU8(),n={type:P.OrderType.Audio,sound:r};return i&1&&(n.lowpass=e.readU16BE()),i&2&&(n.highpass=e.readU16BE()),i&4&&(n.reverb=e.readU8()/255),i&8&&(n.pitch=e.readU8()/100),i&16&&(n.volume=e.readU8()/255),n}case 7:{let r=e.readI16BE(),i=e.readI16BE();return{type:P.OrderType.Audio,sound:"__listener_position__",x:r,y:i}}case 8:{let r=e.readU16BE(),i=e.readU16BE(),n=e.readU8(),s=e.readU8();return{type:P.OrderType.Audio,sound:"__configure_spatial__",maxDistance:r,referenceDistance:i,rolloffFactor:n,panSpread:s}}default:return{type:P.OrderType.Audio,sound:0}}}static decodeSoundTarget(e){switch(e.readU8()){case 0:return e.readU16BE();case 1:return"all";case 2:{let r=e.readU8(),i=e.readBytes(r);return ne.decode(i)}default:return 0}}static decodeVibrationOrder(e){if(e.readU8()===1){let n=e.readU8(),s=e.readU16BE(),o=e.readU8()/255,d=e.readU8()/255,u=e.readU16BE();return{type:P.OrderType.Vibration,target:"gamepad",gamepadIndex:n,duration:s,strongMagnitude:o,weakMagnitude:d,startDelay:u}}let r=e.readU8();if(r===1)return{type:P.OrderType.Vibration,pattern:e.readU16BE()};let i=new Array(r);for(let n=0;n<r;n++)i[n]=e.readU16BE();return{type:P.OrderType.Vibration,pattern:i}}static decodeMacroDefine(e){let t=e.readU16BE(),r=e.readU16BE(),i=e.readBytes(r),n=JSON.parse(ne.decode(i));return{templateId:t,template:n}}static decodeMacroOrder(e){let t=e.readU8(),r=e.readU16BE();switch(t){case 1:{let i=e.readU16BE(),n=e.readU16BE(),s=e.readI16BE(),o=e.readI16BE(),d=e.readU8(),u=e.readU16BE(),a;if(u>0){let c=e.readBytes(u);a=JSON.parse(ne.decode(c))}return{cmd:t,instanceId:r,templateId:i,layerId:n,x:s,y:o,zIndex:d,params:a??{}}}case 2:{let i=e.readU16BE(),n=e.readBytes(i),s=JSON.parse(ne.decode(n));return{cmd:t,instanceId:r,params:s}}case 3:return{cmd:t,instanceId:r};case 4:return{cmd:t,instanceId:r};case 5:return{cmd:t,instanceId:r};default:return{cmd:t,instanceId:r}}}static decodePostProcess(e){let t={},r=e.readU8();if(r&1){let i=e.readU8()!==0,n=e.readU8(),s=e.readU8()/10,o=e.readU8()/100;t.ambientEffect={enabled:i,blur:n,scale:s,opacity:o}}if(r&2){let i=e.readU8()!==0,n=e.readU8()/100,s=e.readU8(),o=e.readU8(),d=e.readU8(),u=e.readU8(),a=e.readU8(),c=e.readU8(),b=["horizontal","vertical","grid"];t.scanlines={enabled:i,opacity:n,pattern:b[s]??"horizontal",spacing:o,thickness:d,color:{r:u,g:a,b:c}}}if(r&4){let i=e.readU8()!==0,n=e.readU8()/10,s=e.readU8(),o=s>0?ne.decode(e.readBytes(s)):void 0;t.grid={enabled:i,lineWidth:n,color:o}}return t}};var ze=class{listeners=new Map;on(e,t){this.listeners.has(e)||this.listeners.set(e,[]),this.listeners.get(e).push(t)}off(e,t){let r=this.listeners.get(e);if(r){let i=r.indexOf(t);i!==-1&&r.splice(i,1)}}emit(e,t){let r=this.listeners.get(e);r&&r.forEach(i=>i(t))}removeAllListeners(){this.listeners.clear()}};
|