@korial29/tiny-fps 0.11.0 → 0.13.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/README.md +27 -38
- package/dist/index.js +32 -18
- package/dist/local.js +1 -1
- package/dist/server.js +394 -2299
- package/dist/types/client/audio.d.ts +3 -16
- package/dist/types/client/effects.d.ts +5 -171
- package/dist/types/client/hud.d.ts +1 -25
- package/dist/types/client/input.d.ts +2 -2
- package/dist/types/client/lights.d.ts +7 -23
- package/dist/types/client/mesh.d.ts +13 -22
- package/dist/types/client/predict.d.ts +3 -27
- package/dist/types/client/render.d.ts +45 -21
- package/dist/types/client/sfx.d.ts +27 -20
- package/dist/types/client/soldier.d.ts +21 -0
- package/dist/types/shared/constants.d.ts +2 -49
- package/dist/types/shared/map.d.ts +14 -5
- package/dist/types/shared/physics.d.ts +11 -25
- package/dist/types/shared/protocol.d.ts +2 -22
- package/dist/types/shared/room.d.ts +14 -54
- package/dist/types/shared/types.d.ts +2 -197
- package/dist/types/shared/world.d.ts +59 -0
- package/package.json +2 -2
- package/dist/types/client/gun.d.ts +0 -24
- package/dist/types/client/items.d.ts +0 -19
- package/dist/types/client/monsters.d.ts +0 -60
- package/dist/types/client/progress.d.ts +0 -56
- package/dist/types/shared/level.d.ts +0 -15
- package/dist/types/shared/levels.d.ts +0 -2
- package/dist/types/shared/monsters.d.ts +0 -23
- package/dist/types/shared/shoot.d.ts +0 -33
package/README.md
CHANGED
|
@@ -10,15 +10,15 @@
|
|
|
10
10
|
|
|
11
11
|
**▶ Play it now: https://korial29.github.io/tiny-fps/**
|
|
12
12
|
|
|
13
|
-
A tiny
|
|
13
|
+
A tiny multiplayer first-person game for the browser, written in TypeScript with raw WebGL2, no game library and no runtime dependency. It is becoming an open-air capture-the-flag between two teams of players on rooftops, inspired by Mirror's Edge: parkour movement, no weapons, no CPU enemies. The game is being rebuilt step by step toward that ([`docs/PLAN.md`](docs/PLAN.md)): today you run and jump around one empty rooftop with the other players in the room. Everything is generated in code: geometry, textures, lights. There are no images, fonts or assets to serve.
|
|
14
14
|
|
|
15
|
-
It
|
|
15
|
+
It started as a co-op shooter inspired by carlini's [Yet Another Doom Clone](https://github.com/carlini/js13k2019-yet-another-doom-clone) (js13k 2019). Only the ideas were reused, not the code. The renderer, the netcode and the sound come from that first game; the screenshots below are from it.
|
|
16
16
|
|
|
17
|
-

|
|
18
18
|
|
|
19
|
-

|
|
20
20
|
|
|
21
|
-

|
|
22
22
|
|
|
23
23
|
## Framework-agnostic, light to add
|
|
24
24
|
|
|
@@ -26,18 +26,18 @@ It is inspired by carlini's [Yet Another Doom Clone](https://github.com/carlini/
|
|
|
26
26
|
|
|
27
27
|
| Entry | What it is | Size (gzip) |
|
|
28
28
|
| --------------------------- | ---------------------------------------------------------------------- | ----------- |
|
|
29
|
-
| `@korial29/tiny-fps` | The game client: rendering, lights and shadows, input, prediction, HUD | ~
|
|
30
|
-
| `@korial29/tiny-fps/local` | Same-browser multiplayer, no server: the host runs in a tab | ~
|
|
29
|
+
| `@korial29/tiny-fps` | The game client: rendering, lights and shadows, input, prediction, HUD | ~15 KB |
|
|
30
|
+
| `@korial29/tiny-fps/local` | Same-browser multiplayer, no server: the host runs in a tab | ~4.3 KB |
|
|
31
31
|
| `@korial29/tiny-fps/server` | Node WebSocket server (needs `ws`) | Node only |
|
|
32
32
|
|
|
33
|
-
CI checks the budgets (
|
|
33
|
+
CI checks the budgets (40 KB and 16 KB) on every pull request.
|
|
34
34
|
|
|
35
35
|
## Live demo
|
|
36
36
|
|
|
37
37
|
https://korial29.github.io/tiny-fps/
|
|
38
38
|
|
|
39
|
-
1. Open the page, pick a name and play: you
|
|
40
|
-
2.
|
|
39
|
+
1. Open the page, pick a name and play: you have the rooftop to yourself.
|
|
40
|
+
2. To play with someone on one machine, open it in a second tab of the same browser and join with another name: the tabs play together, with no server.
|
|
41
41
|
3. To play across devices, pick the server mode and give it the URL of a running `tiny-fps-server`.
|
|
42
42
|
|
|
43
43
|
---
|
|
@@ -171,7 +171,6 @@ The subset of `WebSocket` the client uses: `binaryType`, `onopen`, `onclose`, `o
|
|
|
171
171
|
| Look | Mouse (click the game to lock the pointer, Esc to release it) |
|
|
172
172
|
| Move | WASD, or the arrow keys |
|
|
173
173
|
| Jump | Space |
|
|
174
|
-
| Fire | Left mouse button, held |
|
|
175
174
|
| Mute | M (remembered) |
|
|
176
175
|
|
|
177
176
|
Keys are read by their position (`KeyboardEvent.code`), so on an AZERTY keyboard WASD is ZQSD with nothing to set.
|
|
@@ -180,47 +179,37 @@ Keys are read by their position (`KeyboardEvent.code`), so on an AZERTY keyboard
|
|
|
180
179
|
|
|
181
180
|
## How a game plays
|
|
182
181
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
- **
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
- **
|
|
190
|
-
- **Keys and gates**: red, blue and yellow keys open the barred gates of their colour, for the whole team.
|
|
191
|
-
- **The way out**: the start room locks behind you. Touch the goal to open it, then the level is won once every living player is back inside.
|
|
192
|
-
- **Five levels**: Warm-up, Foundry, Cistern, Loop and Citadel. Then the run starts over in hard mode, with faster monsters that strike and fire more often. The end screen shows the time and each player's kills and deaths.
|
|
193
|
-
- **Sound**: every sound is synthesised in code and placed left or right by where it comes from; a dark music loops under it. The sound starts with the click that locks the pointer.
|
|
194
|
-
- **Dark levels**: your flashlight casts shadows, and so do muzzle flashes. Explosions, hits, bullets, the goal and the open exit light up their surroundings.
|
|
182
|
+
For now the game is the movement and the room; the rules come with the next steps of [`docs/PLAN.md`](docs/PLAN.md).
|
|
183
|
+
|
|
184
|
+
- **The rooftop**: one 40 × 40 m deck under an open sky, with a parapet round the edge, a crate you can jump onto, a block too tall to climb, and a platform with four steps up to it. Up to 16 players share it, each with a name tag and a colour of their own.
|
|
185
|
+
- **Moving**: run, strafe and jump. The host decides where everyone is. Your own moves show at once (the client predicts them) and are corrected to the host's state, to the bit.
|
|
186
|
+
- **The others**: drawn 100 ms in the past, interpolated between snapshots, as soldiers with swinging arms and legs. Each carries a flashlight that casts shadows.
|
|
187
|
+
- **Sound**: a synthesiser and a mixer that places sounds left or right by where they come from, and a dark music loop. The sound starts with the click that locks the pointer. There are no sound effects yet.
|
|
188
|
+
- **Coming**: parkour moves (sprint, double jump, wall-run, ledge grab, slide), a Mirror's Edge look, two teams, flags, a score, and a tackle that makes a carrier drop the flag.
|
|
195
189
|
|
|
196
190
|
---
|
|
197
191
|
|
|
198
192
|
## Features
|
|
199
193
|
|
|
200
|
-
Parity with the js13k game is tracked in [`docs/PARITY.md`](docs/PARITY.md).
|
|
201
|
-
|
|
202
194
|
- [x] Multiplayer over WebSocket, or between tabs of one browser with no server
|
|
203
|
-
- [x]
|
|
195
|
+
- [x] A brush world under an open sky: roofs at any height, gaps, floating platforms and overhangs, steps, falls back to a spawn; eight procedural materials (brick, tile, lava, stone, metal, wood, moss, grating) with bump mapping and per-material highlights; a sky with a blooming sun, a sun shadow that follows you, daylight from the sky
|
|
196
|
+
- [x] The rooftop map: a deck, a parapet, a crate, a block and a platform with steps
|
|
204
197
|
- [x] Client-side prediction, exact to the bit, and the other players interpolated
|
|
205
|
-
- [x]
|
|
206
|
-
- [x]
|
|
207
|
-
- [x]
|
|
208
|
-
- [x]
|
|
209
|
-
- [x]
|
|
210
|
-
- [x]
|
|
211
|
-
- [x] Post-processing: bloom on flashes, lava and glows, tone mapping, vignette, film grain, 4x antialiasing
|
|
212
|
-
- [x] Models and particles: hunched walkers with jointed limbs, flapping flyers, soldiers for the other players, a detailed chaingun, ejected brass casings, streaking sparks, gibs that leave blood splats
|
|
213
|
-
- [x] Sound: a jsfxr-style synthesiser (shot, boom, hit, collect, clock, burst), stereo by position, a looped music; M mutes
|
|
214
|
-
- [x] HUD and screens: health bar, damage vignette and pickup fades, low-health pulse and heartbeat, title and pause screen, death countdown, end screen with each player's kills and deaths
|
|
198
|
+
- [x] A per-tick history of the players on the host, for lag compensation
|
|
199
|
+
- [x] Flashlights with shadow maps, a light colour per room, a fog colour per map, flickering lava light, dust in the flashlight beam
|
|
200
|
+
- [x] Post-processing: bloom, tone mapping, vignette, film grain, 4x antialiasing
|
|
201
|
+
- [x] A model for the other players: jointed legs and arms that follow their stride
|
|
202
|
+
- [x] Sound: a jsfxr-style synthesiser and a looped music; M mutes
|
|
203
|
+
- [x] A title screen with the controls, a muted mark, a player count and name tags
|
|
215
204
|
- [x] Shadow DOM, zero runtime dependencies, size-gated in CI
|
|
216
205
|
|
|
217
206
|
---
|
|
218
207
|
|
|
219
208
|
## How it works
|
|
220
209
|
|
|
221
|
-
- **The host is authoritative**: the Node server, or the host tab in local mode. It runs the players
|
|
210
|
+
- **The host is authoritative**: the Node server, or the host tab in local mode. It runs the players on the map.
|
|
222
211
|
- **Inputs, not positions**: each client sends one 11-byte input per step at 60 Hz, holding its keys, view angles and the moment it sees the others at. It predicts its own player at once and replays what the host has not acknowledged yet.
|
|
223
|
-
- **Snapshots** go out at 30 Hz:
|
|
212
|
+
- **Snapshots** go out at 30 Hz: a 4-byte head and 24 bytes per player, in a compact binary format. The other players are drawn 100 ms in the past, interpolated between snapshots.
|
|
224
213
|
- **Rendering**: one WebGL2 shader program and one vertex buffer, up to 16 lights, with up to 4 shadow maps drawn by the same program.
|
|
225
214
|
|
|
226
215
|
---
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
var e=.4,t=1.8,n=1.6,r=1.55,i=.5,a=.02,o=1.8,s=.45,c=.9,l=.15,u={FORWARD:1,BACK:2,LEFT:4,RIGHT:8,JUMP:16,FIRE:32},d={WALKER:0,FLYER:1},f={DIE:0,HURT:1,BOOM:2,SWING:3},p={HEALTH:0,KEY_RED:1,KEY_BLUE:2,KEY_YELLOW:3},m={HUNT:0,RETURN:1},h={BRICK:0,TILE:1,LAVA:2,STONE:3,METAL:4,WOOD:5,MOSS:6,GRATE:7},g=e=>{let t=(e,t,n,r)=>`${e},${t},${n},${r}`,n=/* @__PURE__ */ new Map;e.sectors.forEach(({pts:e},r)=>e.forEach(([i,a],o)=>n.set(t(i,a,...e[(o+1)%e.length]),r)));let r={map:e,edges:e.sectors.flatMap(({pts:e},r)=>e.map(([i,a],o)=>{let[s,c]=e[(o+1)%e.length];return{ax:i,az:a,bx:s,bz:c,front:r,back:n.get(t(s,c,i,a))??-1}})),barred:/* @__PURE__ */ new Set};return _(r,0,m.HUNT),r},_=({map:e,barred:t},n,r)=>{t.clear(),e.gates?.forEach(e=>n&1<<e.key||t.add(e.sector)),e.exit!==void 0&&r===m.HUNT&&t.add(e.exit)},v=({pts:e},t,n)=>e.every(([r,i],a)=>{let[o,s]=e[(a+1)%e.length];return(o-r)*(n-i)-(s-i)*(t-r)>=0}),y=({map:e},t,n)=>e.sectors.findIndex(e=>v(e,t,n)),b=({map:e,edges:t},[n,r,i],[a,o,s])=>{let[c,l]=[a-n,s-i];return t.some(t=>{let[a,s]=[t.bx-t.ax,t.bz-t.az],u=c*s-l*a;if(!u)return!1;let d=((t.ax-n)*s-(t.az-i)*a)/u,f=((t.ax-n)*l-(t.az-i)*c)/u;if(d<=0||d>=1||f<0||f>1)return!1;if(t.back<0)return!0;let[p,m]=[e.sectors[t.front],e.sectors[t.back]],h=r+d*(o-r);return h<Math.max(p.floor,m.floor)||h>Math.min(p.ceil,m.ceil)})},x=(e,...t)=>t.map(([t,n,r,i,a,o,s,c,l])=>[t,n,r,i,a,o,s,c,l,e]),S=(e,[t,n,r,i],a,o,s,c,l,u)=>Array.from({length:s},(d,f)=>{let[p,m]=e?[n,i]:[t,r],[h,g,_]=[p+f*(m-p)/s,p+(f+1)*(m-p)/s,+(a+f*o).toFixed(1)];return e?[t,h,r,g,_,c,l,u]:[h,n,g,i,_,c,l,u]}),[C,w,T,E]=[p.HEALTH,p.KEY_RED,p.KEY_BLUE,p.KEY_YELLOW],D=Math.PI/4,O=[1,.82,.6],ee=[.6,.75,1],k=(e,t,[n,r,i],a,o,s,c,l)=>{let u=t.map(([e,t,n,r])=>[[e,t],[n,t],[n,r],[e,r]]),f=[...new Map(u.flat().map(e=>[`${e}`,e])).values()];return{name:e,sectors:t.map(([,,,,e,t,n=`103`,r,,i],a)=>({pts:u[a].flatMap((e,t,n)=>{let r=n[(t+1)%4],i=t=>(t[0]-e[0])*(r[0]-e[0])+(t[1]-e[1])*(r[1]-e[1]);return[e,...f.filter(t=>(r[0]-e[0])*(t[1]-e[1])===(r[1]-e[1])*(t[0]-e[0])&&i(t)>0&&i(t)<(r[0]-e[0])**2+(r[1]-e[1])**2).sort((e,t)=>i(e)-i(t))]}),floor:e,ceil:t,ft:+n[0],wt:+n[1],ct:+n[2],light:r,tint:i})),spawns:Array.from({length:16},(e,t)=>[n+t%4*2,r+(t>>2)*2,i*D]),monsters:[...a.map(([e,t,n])=>[e,t,n*D,d.WALKER]),...o.map(([e,t,n])=>[e,t,n*D,d.FLYER])],items:s,gates:t.flatMap(([,,,,,,,,e],t)=>e?[{sector:t,key:e}]:[]),goal:c,exit:0,fog:l}},A=[k(`Warm-up`,[[0,0,10,10,0,4,`103`],[10,4,22,6,0,2.8,`333`,.6],...x([1,.85,.65],[22,0,38,14,0,6,`553`],...S(0,[38,4,42,10],.4,.4,4,6,`553`)),[42,2,52,12,2,6,`103`,.9],...S(1,[46,12,48,22],1.6,-.4,4,4.8,`744`,.7),[38,22,56,36,0,6,`113`]],[2,2,6],[[28,4,3],[34,10,4],[48,6,4],[44,30,0]],[[30,8,0],[52,28,2]],[[26,12,C],[50,10,C],[40,34,C]],[54,34]),k(`Foundry`,[...x([1,.9,.8],[0,14,10,26,0,4,`443`],[10,22,20,24,0,2.8,`744`,.6]),...x([1,.5,.25],[24,8,28,16,0,9,`744`],[20,16,28,30,0,9,`443`],...S(1,[20,8,24,16],2,-.4,5,9,`744`),[28,8,42,18,-1.6,9,`233`,.9],...S(1,[28,18,42,21],-1.2,.4,3,9,`333`),[28,21,50,30,0,9,`744`],[42,8,50,21,0,9,`744`],[20,0,50,8,2.4,9,`744`,.8]),[50,14,51,17,0,3.5,`444`,1,w],...x([.85,.65,.5],[51,8,59,26,0,7,`443`,.6],[59,8,67,26,.4,7,`143`,.8],[57,26,59,36,0,4.8,`744`,.7]),...x([1,.6,.45],[50,36,68,50,0,7,`443`])],[2,16,6],[[46,14,2],[36,26,0],[38,4,2],[56,12,4],[62,20,2],[60,44,0]],[[35,13,0],[60,14,4],[58,42,6]],[[24,26,C],[24,4,C],[64,10,C],[52,46,C],[46,4,w]],[66,48],[.07,.05,.04,.026]),k(`Cistern`,[...x([.45,.9,1],[0,20,10,32,0,4,`363`],[10,26,20,28,0,2.8,`363`,.6],[20,14,56,26,0,11,`363`,.9],[20,26,34,32,0,11,`363`,.9]),...x([.9,.6,.45],[34,26,42,32,-.4,11,`263`]),...x([.45,.9,1],[42,26,56,32,0,11,`363`,.9],[20,32,56,44,0,11,`363`,.9],[36,44,38,54,0,1.9,`000`,.35],[30,54,46,68,0,5,`003`,.7]),[56,28,57,31,0,4,`444`,1,w],...x([.55,.85,1],...S(0,[57,26,65,32],.4,.4,8,10,`333`,.8)),...x([.7,1,.85],[65,22,81,36,3.6,11,`563`]),[71,36,74,37,3.6,7,`444`,1,T],...x([.45,.9,1],[71,37,74,48,3.6,6.4,`363`,.6]),...x([.9,.6,.45],[62,48,82,64,3.6,10,`263`])],[2,22,6],[[30,18,4],[50,20,4],[30,40,0],[50,40,0],[36,60,2],[42,64,0],[76,28,2],[70,58,0]],[[26,24,0],[48,22,0],[36,40,2],[72,56,4]],[[24,40,C],[52,42,C],[44,57,C],[68,34,C],[64,62,C],[38,66,w],[79,24,T]],[80,62],[.04,.11,.1,.034]),k(`Loop`,[...x(O,[0,44,10,56,0,4,`153`],[10,49,20,51,0,2.8,`553`,.6]),[20,30,52,40,0,9,`113`],[20,48,52,58,0,9,`113`,.9],[20,40,32,48,0,9,`113`],[40,40,52,48,0,9,`113`],[20,22,49,30,3.2,9,`743`,.8],...x(O,[34,58,36,66,0,2.8,`553`,.4],[26,66,46,84,0,5,`353`,.5]),[52,42,53,45,0,4,`444`,1,w],...x(O,[53,36,64,54,0,6,`153`,.8],[64,36,70,54,.4,6,`253`,.8]),[60,35,63,36,0,4,`444`,1,T],...x(O,[57,12,77,35,0,8,`053`,.9]),[56,24,57,27,0,4,`444`,1,E],...S(0,[49,22,56,30],2.8,-.4,7,9,`744`,.8),...x(O,[12,24,20,28,3.2,6,`744`,.6],[0,10,12,34,3.2,9,`553`])],[2,46,6],[[30,36,4],[44,36,2],[40,52,0],[40,26,4],[30,74,6],[42,78,2],[56,40,6],[64,50,2],[62,18,4],[72,28,2],[6,22,0]],[[28,48,0],[46,44,4],[36,72,0],[60,45,6],[68,22,4]],[[24,54,C],[24,26,C],[44,80,C],[68,38,C],[74,14,C],[36,82,w],[68,52,T],[74,32,E]],[6,13],[.06,.045,.04,.02]),k(`Citadel`,[...x(ee,[0,60,10,72,0,4,`303`],[10,65,24,67,0,2.8,`363`,.6],[24,48,56,84,0,12,`363`],[27,40,31,48,0,3.4,`363`,.4]),...x([.85,.7,.65],[24,16,34,40,0,12,`443`,.9],...S(0,[34,16,40,26],-.4,-.4,3,12,`243`),[34,26,40,30,0,12,`744`],...S(0,[34,30,40,40],-.4,-.4,3,12,`243`),[40,16,60,40,0,12,`443`,.9]),[60,27,61,30,0,4,`444`,1,E],...x([.55,.3,1],[61,10,101,16,0,14,`443`,1],[61,40,101,46,0,14,`443`,1],[61,16,69,40,0,14,`443`,1],[93,16,101,40,0,14,`443`,1],[69,16,93,20,.4,14,`243`,1],[69,36,93,40,.4,14,`243`,1],[69,20,73,36,.4,14,`243`,1],[89,20,93,36,.4,14,`243`,1],[73,20,89,36,.8,14,`744`,1]),[101,26,111,28,0,3.2,`744`,.6],[111,16,131,40,0,8,`163`],[56,66,57,69,0,4,`444`,1,w],...x([1,.5,.45],[57,56,75,80,0,12,`443`,.7],...S(0,[75,60,85,72],.4,.4,5,12,`744`,.8),[85,52,101,80,2.4,12,`543`]),[38,84,42,85,0,4,`444`,1,T],...x([.6,.85,.85],[38,85,42,95,0,1.9,`363`,.35],[28,95,56,115,0,6,`663`,.6])],[2,62,6],[[34,56,4],[50,56,2],[36,76,0],[46,20,4],[54,34,2],[66,60,2],[66,76,0],[92,60,2],[34,100,4],[50,108,2],[70,13,4],[92,13,2],[70,43,0],[92,43,6]],[[30,70,0],[46,68,0],[50,28,0],[76,26,0],[86,30,0],[81,22,0]],[[30,80,C],[56,36,C],[60,78,C],[50,112,C],[64,13,C],[98,43,C],[114,38,C],[52,22,w],[95,66,T],[42,110,E]],[127,36],[.045,.05,.075,.02])],j=65536/(2*Math.PI),M=e=>Math.round(e*j)&65535,N=e=>Math.max(-32768,Math.min(32767,Math.round(e))),P=e=>N(e*1e4),F=e=>e/1e4,I=e=>e/j,L=(e=0)=>Math.round(e*256)&16777215,te=e=>({...e,yaw:I(M(e.yaw)),pitch:F(P(e.pitch)),...e.view!==void 0&&{view:L(e.view)/256}}),R=e=>{let t=/* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(11));t.setUint8(0,1),t.setUint16(1,e.seq&65535),t.setUint8(3,e.keys&255),t.setUint16(4,M(e.yaw)),t.setInt16(6,P(e.pitch));let n=L(e.view);return t.setUint16(8,n>>8),t.setUint8(10,n&255),t.buffer},ne=e=>{if(e.byteLength<7)return null;let t=new DataView(e),n=t.getUint8(3);if(t.getUint8(0)!==2)return null;let r=7+25*n,[i,a,o,s,c]=[14,16,13,14,14].map(n=>{if(e.byteLength<=r)return null;let i=t.getUint8(r),a=r+1;return r=a+n*i,[a,i]});if(!i||!a||!o||!s||!c||e.byteLength!==r)return null;let l=([e,t],n,r)=>Array.from({length:t},(t,i)=>r(e+i*n)),u=[];for(let e=0;e<n;e++){let n=7+e*25;u.push({id:t.getUint8(n),x:t.getFloat32(n+1),y:t.getFloat32(n+5),z:t.getFloat32(n+9),vy:t.getFloat32(n+13),yaw:I(t.getUint16(n+17)),pitch:F(t.getInt16(n+19)),ack:t.getUint16(n+21),cool:t.getUint8(n+23)>>1,ground:(t.getUint8(n+23)&1)==1,hp:t.getUint8(n+24)})}return{tick:t.getUint16(1),players:u,shots:l(i,14,e=>({id:t.getUint8(e),x:t.getFloat32(e+1),y:t.getFloat32(e+5),z:t.getFloat32(e+9),hit:t.getUint8(e+13)===255?-1:t.getUint8(e+13)})),monsters:l(a,16,e=>({id:t.getUint8(e),kind:t.getUint8(e+1)&1,chase:!!(t.getUint8(e+1)&2),x:t.getFloat32(e+2),y:t.getFloat32(e+6),z:t.getFloat32(e+10),yaw:I(t.getUint16(e+14))})),bullets:l(o,13,e=>({id:t.getUint8(e),x:t.getFloat32(e+1),y:t.getFloat32(e+5),z:t.getFloat32(e+9)})),events:l(s,14,e=>({type:t.getUint8(e),id:t.getUint8(e+1),x:t.getFloat32(e+2),y:t.getFloat32(e+6),z:t.getFloat32(e+10)})),level:t.getUint8(4),hard:(t.getUint8(5)&1)==1,phase:t.getUint8(5)>>1&1,keys:t.getUint8(6),items:l(c,14,e=>({id:t.getUint8(e),kind:t.getUint8(e+1),x:t.getFloat32(e+2),y:t.getFloat32(e+6),z:t.getFloat32(e+10)}))}},re=e=>{try{let t=JSON.parse(e);return t&&typeof t.t==`string`?t:null}catch{return null}},z=44100,B={SHOT:0,BOOM:1,HIT:2,COLLECT:3,CLOCK:4,BURST:5,HEART:6},V=([e,t,n,r,i,a,o,s,c,l,u],d)=>{let f=new Float32Array(Math.ceil((r+i+a)*z)),p=0,m=0,h=0,g=()=>{d=d+1831565813|0;let e=Math.imul(d^d>>>15,1|d);return e=e+Math.imul(e^e>>>7,61|e)^e,((e^e>>>14)>>>0)/2**31-1};for(let d=0;d<f.length;d++){let _=d/z;p+=t*2**(n*_)*(_>c?l:1)*(1+o*Math.sin(_*44))/z,p>=1&&(p%=1,h=g());let v=e==3?h:e==2?Math.sin(p*2*Math.PI):e?1-2*p:p<.5?1:-1;m+=(v-m)*s;let y=_<r?_/r:_<r+i?1:((r+i+a-_)/a)**2;f[d]=m*y*u}return f},ie=[[[3,7e3,-.5,.002,0,.06,0,.6,0,1,.8],[0,220,-5,.002,0,.075,0,.3,0,1,.5]],[[3,500,-1.5,.004,.05,.8,0,.1,0,1,2.2],[2,110,-1.5,.004,.05,.6,0,1,0,1,.8]],[[1,160,-1.5,.003,.04,.22,.2,.5,0,1,.6],[0,83,-1,.003,.04,.22,0,.3,0,1,.4],[3,3e3,0,.002,0,.12,0,.4,0,1,.5]],[[2,880,.3,.005,.06,.4,0,1,.09,1.5,.8],[2,1760,.3,.005,.06,.35,0,1,.09,1.5,.3]],[[3,4e3,0,.001,0,.025,0,.5,0,1,.7],[2,1300,-1,.001,0,.04,0,1,0,1,.5]],[[3,1200,-1,.002,0,.14,0,.45,0,1,.7],[0,3200,-1,.002,0,.05,0,1,.025,.7,.2]],[[2,55,-1,.01,.01,.12,0,1,0,1,.9],[2,110,-1,.01,0,.08,0,1,0,1,.3],[2,48,-1,.01,.01,.14,0,1,0,1,.6,.22],[2,96,-1,.01,0,.09,0,1,0,1,.2,.22]]],ae=e=>{let t=ie[e].map((t,n)=>[V(t,e*9+n),Math.round((t[11]??0)*z)]);return Float32Array.from({length:Math.max(...t.map(([e,t])=>t+e.length))},(e,n)=>Math.tanh(t.reduce((e,[t,r])=>e+(t[n-r]??0),0)))},oe=z*60/108,se=oe/4,ce=4,le=[2,150,-6,.002,0,.25,0,1,0,1,1],ue=[3,9e3,0,.002,0,.15,0,.5,0,1,.5],de=[3,3e4,0,.001,0,.04,0,.7,0,1,.15],fe=()=>{let e=new Float32Array(oe*4*ce),t={},n=(n,r)=>(t[String(r)]??=V(r,1)).forEach((t,r)=>e[(n*se+r)%e.length]+=t),r=e=>55*2**(e/12),i=[0,0,-4,-5],a=[0,0,12,0,0,7,12,7];for(let e=0;e<64;e++){let t=e%16;if(`x.......x.x.....`[t]==`x`&&n(e,le),`....x.......x...`[t]==`x`&&n(e,ue),t%2==0){n(e,de);let o=i[e>>4]+a[t>>1];n(e,[1,r(o),0,.005,.08,.18,0,.15,0,1,.6])}}[15,12,10,19,17,15].forEach((e,t)=>n([18,24,28,50,54,60][t],[2,r(e+24),0,.02,.1,.6,.01,1,0,1,.5]));let o=e.reduce((e,t)=>Math.max(e,Math.abs(t)),0);return e.map(e=>e/o*.35)},H=40,pe=24,me=.5,he=`tiny-fps:muted`,ge=()=>{let e=!1;return{unlock:()=>{},listen:()=>{},play:()=>{},mute:t=>{e=t},get muted(){return e},destroy:()=>{}}},_e=()=>{let e;try{e=new AudioContext}catch{return ge()}let t=(t,n=1)=>{let r=e.createGain();return r.gain.value=n,r.connect(t),r},n=t(e.destination),r=t(n,me),i=[],a=/* @__PURE__ */ new Set,o=[0,0,0],s=0,c=!1,l=t=>{let n=e.createBuffer(1,t.length,z);return n.getChannelData(0).set(t),n},u=t=>{let n=e.createBufferSource();return n.buffer=t,n};return{unlock(){if(e.resume().catch(()=>{}),!i.length){for(let e of Object.values(B))i[e]=l(ae(e));let e=u(l(fe()));e.loop=!0,e.connect(r),e.start()}},listen(e,t){[o,s]=[e,t]},play(r,c,l=1){let d=0,f=l;if(c){let[e,t,n]=c.map((e,t)=>e-o[t]),r=Math.hypot(e,t,n);f=Math.max(0,1-r/H)**2*l,d=(e*Math.cos(s)-n*Math.sin(s))/Math.max(r,1)}let p=i[r];if(!p||!f)return;if(a.size>=pe){let e=[...a].reduce((e,t)=>t.vol<e.vol?t:e);if(e.vol>=f)return;e.src.stop(),a.delete(e)}let m=u(p),h=e.createStereoPanner();h.pan.value=d,m.connect(h).connect(t(n,f));let g={vol:f,src:m};a.add(g),m.onended=()=>a.delete(g),m.start()},mute(t){c=t,n.gain.setTargetAtTime(+!t,e.currentTime,.05)},get muted(){return c},destroy:()=>void e.close().catch(()=>{})}},ve=(e,{type:t,id:n,x:r,y:i,z:a},o)=>{let s=[r,i,a];t==f.DIE?e.play(B.BOOM,s):t==f.BOOM?e.play(B.BURST,s):t==f.HURT&&e.play(B.HIT,n==o?void 0:s)},ye=e=>{let t=0;return(n,r)=>{n&&r>=t&&(t=r+1e3,e.play(B.CLOCK))}},be=e=>{let t=-1/0;return(n,r)=>{if(n<1||n>3)return t=-1/0,0;let i=n>1?800:500;return r-t>=i&&(t=r,e.play(B.HEART,void 0,n>1?.6:1)),1-(r-t)/i}},xe=()=>{try{return localStorage.getItem(he)==`1`}catch{return!1}},Se=e=>{e.mute(!e.muted);try{localStorage.setItem(he,e.muted?`1`:`0`)}catch{}return e.muted},Ce=1/60,U=Math.PI*2,we=1e-6,Te=.01,Ee=e=>(e%U+U)%U,De=e=>[-Math.sin(e),-Math.cos(e)],Oe=Math.fround,ke=(e,t,n)=>Math.min(Math.max(e,t),n),Ae=({map:e,barred:t},n,r,i,a)=>{if(n.back<0||t.has(n.back))return!0;let o=e.sectors[n.front],s=e.sectors[n.back],c=Math.min(o.ceil,s.ceil);return s.floor>r+a+Te||c-Math.max(o.floor,s.floor)<i-Te||c<r+i-Te},je=(e,t,n,r,i)=>{let a=(n-we)**2;for(let o=0;o<4;o++){let o=0,s=0,c=0;for(let l of t.edges){let u=l.bx-l.ax,d=l.bz-l.az,f=e.x-l.ax,p=e.z-l.az;if(u*p-d*f<0)continue;let m=ke((f*u+p*d)/(u*u+d*d),0,1),h=f-m*u,g=p-m*d,_=h*h+g*g;if(_>=a)continue;let v=Math.sqrt(_);if(n-v<=o||!Ae(t,l,e.y,r,i))continue;o=n-v;let y=v||Math.hypot(u,d);s=(v?h:-d)/y*o,c=(v?g:u)/y*o}if(!o)return;e.x+=s,e.z+=c}},Me=(e,t,n,r,i,a,o)=>{let{map:s,edges:c}=e,l=Math.max(y(e,t,n),0),{floor:u,ceil:d}=s.sectors[l],f=(i-we)**2;for(let i of c){if(i.front!==l||i.back<0)continue;let c=i.bx-i.ax,p=i.bz-i.az,m=t-i.ax,h=n-i.az,g=ke((m*c+h*p)/(c*c+p*p),0,1);if((m-g*c)**2+(h-g*p)**2<f&&!Ae(e,i,r,a,o)){let e=s.sectors[i.back];u=Math.max(u,e.floor),d=Math.min(d,e.ceil)}}return[u,d]},Ne=(n,r,a,o,s)=>{n.x+=r,n.z+=a,je(n,s,e,t,i);let[c,l]=Me(s,n.x,n.z,n.y,e,t,i);n.ground&&o&&(n.vy=7);let u=n.vy-20*Ce;n.y+=(n.vy+u)/2*Ce,n.vy=u,n.ground=!1,n.y+1.8>l&&(n.y=l-t,n.vy=Math.min(n.vy,0)),(n.vy<=0?n.y<=c+Te:n.y<c)&&(n.y=c,n.vy<=0&&(n.vy=0,n.ground=!0)),n.x=Oe(n.x),n.y=Oe(n.y),n.z=Oe(n.z),n.vy=Oe(n.vy)},Pe=(e,t,n)=>{let i=(t.keys&u.FIRE)!==0;e.cool=Math.max(0,e.cool-1),e.yaw=Ee(t.yaw),e.pitch=Math.max(-1.55,Math.min(r,t.pitch));let a=(t.keys&u.FORWARD?1:0)-(t.keys&u.BACK?1:0),o=(t.keys&u.RIGHT?1:0)-(t.keys&u.LEFT?1:0),s=6*Ce*(i?.5:1)/(Math.hypot(a,o)||1),[c,l]=De(e.yaw);return Ne(e,(c*a-l*o)*s,(l*a+c*o)*s,(t.keys&u.JUMP)!==0,n),i&&!e.cool?(e.cool=5,!0):!1},Fe=-.5,Ie=(e,t,n=0)=>{let[r,i,a,o,s,c]=[e,t,n].flatMap(e=>[Math.sin(e),Math.cos(e)]),l=[i,0,-r],u=[r*a,o,i*a];return[l.map((e,t)=>e*c-u[t]*s),u.map((e,t)=>e*c+l[t]*s),[-r*o,a,-i*o]]},Le=[[-1,-1],[1,-1],[1,1],[-1,-1],[1,1],[-1,1]],W=(e,[t,n,r],[i,a,o],s,c,l=-1)=>{let[u,d]=[Math.sin(s),Math.cos(s)],f=(e,t)=>[e*d+t*u,-e*u+t*d];for(let s=0;s<3;s++)for(let u of[-1,1]){let[d,p]=[(s+1)%3,(s+2)%3],m=[0,0,0];m[s]=u;let[h,g]=f(m[0],m[2]);for(let[_,v]of Le){let y=[0,0,0];[y[s],y[d],y[p]]=[u,_,v];let[b,x]=f(y[0]*i,y[2]*o);e.push(t+b,n+y[1]*a,r+x,h,m[1],g,c[0],c[1],l,c[2])}}},Re=(e,[t,n,r],i=-1)=>Array.from({length:e.length*(10/6)},(a,o)=>{let[s,c]=[Math.floor(o/10),o%10];return c<6?e[s*6+c]:[t,n,i,r][c-6]}),ze=(e,t)=>[e[1]*t[2]-e[2]*t[1],e[2]*t[0]-e[0]*t[2],e[0]*t[1]-e[1]*t[0]],Be=e=>{let t=ze(Math.abs(e[1])<.9?[0,1,0]:[1,0,0],e),n=t.map(e=>e/Math.hypot(...t));return[e,n,ze(e,n)]},Ve=(e,t,n,r,i,a=-1,o=i)=>{for(let s=0;s<3;s++)for(let c of[-1,1]){let[l,u]=[(s+1)%3,(s+2)%3];for(let[d,f]of Le){let p=[0,0,0];[p[s],p[l],p[u]]=[c,d,f];let m=p[0]>0?o:i,h=[0,1,2].map(e=>t[e]+n[0][e]*p[0]*r[0]+n[1][e]*p[1]*r[1]+n[2][e]*p[2]*r[2]);e.push(...h,...n[s].map(e=>e*c),m[0],m[1],a,m[2])}}},He=(e,t,n,[r,i,a],o,s,c=0,l=[1,1,1])=>{let[u,d,f]=typeof s==`number`?[s,s,s]:s,[p,m]=[Math.sin(c),Math.cos(c)],[h,g,_,v,y,b,x,S,C]=[...r,...i,...a],w=n[0]+h*o[0]+v*o[1]+x*o[2],T=n[1]+g*o[0]+y*o[1]+S*o[2],E=n[2]+_*o[0]+b*o[1]+C*o[2];for(let n=0;n<t.length;n+=10){let r=t[n]*u,i=t[n+1]*d,a=t[n+2]*f,o=r*m-i*p,s=r*p+i*m,c=t[n+3]*m-t[n+4]*p,D=t[n+3]*p+t[n+4]*m,O=t[n+5];e.push(w+h*o+v*s-x*a,T+g*o+y*s-S*a,E+_*o+b*s-C*a,h*c+v*D-x*O,g*c+y*D-S*O,_*c+b*D-C*O,t[n+6]*l[0],t[n+7]*l[1],t[n+8],t[n+9]*l[2])}},Ue=(e,t)=>{let n=e[0]*t[0]+e[1]*t[1]+e[2]*t[2],r=t.map((t,r)=>t-e[r]*n),i=Math.hypot(...r)||1,[a,o,s]=r.map(e=>e/i);return[[a,o,s],[o*e[2]-s*e[1],s*e[0]-a*e[2],a*e[1]-o*e[0]],e]},We=(e,t,n,r,i,a,o=-1)=>{let[s,c]=[n,i].map(e=>e.map((e,n)=>e-t[n])),l=[s[1]*c[2]-s[2]*c[1],s[2]*c[0]-s[0]*c[2],s[0]*c[1]-s[1]*c[0]],u=Math.hypot(...l)||1;for(let s of[1,-1])for(let c of s>0?[t,n,r,t,r,i]:[t,r,n,t,i,r])e.push(...c,...l.map(e=>e*s/u),a[0],a[1],o,a[2])},Ge=.55,Ke=6,G=(e,t,n=[0,0,0])=>{let r=[];for(let i=1;i<e.length;i++){let[[a,o],[s,c]]=[e[i-1],e[i]],l=Math.hypot(s-a,c-o);if(!l)continue;let[u,d]=[(c-o)/l,-(s-a)/l],f=(e,i,a)=>{let[o,s]=[Math.cos(e*U/t),Math.sin(e*U/t)];r.push(n[0]+i*o,n[1]+i*s,n[2]-a,u*o,u*s,-d)};for(let e=0;e<t;e++)for(let[t,n,r]of[[e,a,o],[e+1,a,o],[e+1,s,c],[e,a,o],[e+1,s,c],[e,s,c]])f(t,n,r)}return r},K=(e,t,n=0)=>{let[r,i]=[Math.sin(n),Math.cos(n)],a=[];return Ve(a,e,[[i,r,0],[-r,i,0],[0,0,1]],t,[0,0,0]),a.filter((e,t)=>t%10<6)},qe=(e,t,n,r)=>K([Math.cos(e)*t,Math.sin(e)*t,-n],r,e+Math.PI/4),Je=(e,t,n,r,i)=>Le.flatMap(([a,o])=>{let[s,c]=[Math.cos(e),Math.sin(e)];return[s*t-c*r*a,c*t+s*r*a,-n-i*o,s,c,0]}),Ye=e=>{let t=e>6,n=[],[r,i,a,o]=[[],[],[],[]];for(let t=0;t<Ke;t++){let r=t*U/Ke;n.push(...G([[0,0],[.02,0],[.02,.45],[.014,.45],[.014,.55],[0,.55]],e,[Math.cos(r)*.05,Math.sin(r)*.05,0]))}let s=G([[0,-.3],[.08,-.3],[.09,-.26],[.09,0],[.075,0],[.075,.1],[.03,.1],[.03,.4],[0,.4]],e);if(t){for(let[t,n]of[[.1,4],[.44,4]]){r.push(...G([[0,t],[.086,t],[.086,t+.06],[0,t+.06]],e));for(let e=0;e<n;e++)r.push(...qe(e*U/n,.088,t+.03,[.008,.008,.012]))}s.push(...G([[.078,.16],[.078,.3]],e));let t=t=>(t+.5)*U/e,n=t=>t*Math.cos(Math.PI/e)+.002;for(let r=0;r<e;r++)a.push(...Je(t(r),n(.078),r%2?.21:.25,.012,.02));a.push(...Je(t(0),n(.075),.05,.016,.03)),i.push(...G([[.088,.3],[.088,.33],[.07,.33]],e),...G([[0,-.325],[.03,-.325],[.03,-.3]],e),...G([[.062,-.3],[.062,-.31],[.07,-.31],[.07,-.3]],e));for(let e=0;e<6;e++)i.push(...qe(e*U/6,.046,-.306,[.007,.007,.006]));s.push(...K([-.135,-.03,.12],[.045,.055,.09]));for(let e of[-.055,-.005])i.push(...K([-.18,e,.12],[.004,.006,.075]));i.push(...K([-.135,.03,.12],[.048,.007,.094]),...K([-.135,.042,.12],[.02,.006,.012]),...K([-.1,.02,0],[.02,.025,.045]),...K([0,.118,.12],[.012,.009,.075]),...K([0,.098,.055],[.009,.02,.009]),...K([0,.098,.185],[.009,.02,.009]));for(let e=0;e<4;e++)o.push(...K([-.1,.052,.03-e*.026],[.007,.007,.011]))}return{barrels:n,clamps:r,body:s,trim:i,holes:a,brass:o}},Xe=.3,Ze=3.6,q={body:[.27,.22,.25],arm:[.4,.26,.22],leg:[.18,.17,.22],head:[.45,.33,.3],shell:[.36,.2,.32],fin:[.5,.25,.25],gore:[.6,.08,.07],calm:[.6,.05,.04],chase:[1,.3,.15],blob:[1,.3,.05],core:[1,.8,.4]},Qe=[[q.body,q.arm,q.head,q.gore,q.gore,q.chase],[q.shell,q.fin,q.shell,q.gore,q.chase]],$e=e=>e?q.chase:q.calm,J=({x:e,y:t,z:n,yaw:r},i,a,o)=>{let[s,c]=[Math.sin(r),Math.cos(r)];return[e+i*c-o*s,t+a,n-i*s-o*c]},et=(e,t,n,r,i)=>{let a=e.get(t)??{x:n,z:r,walked:0,vx:0,vz:0},o=Math.hypot(n-a.x,r-a.z),s=i>0&&o<1?1-Math.exp(-i*8):0;return a.vx+=((n-a.x)/(i||1)-a.vx)*s,a.vz+=((r-a.z)/(i||1)-a.vz)*s,a.walked+=o<1?o:0,[a.x,a.z]=[n,r],e.set(t,a),a},tt=e=>Math.min(1,Math.hypot(e.vx,e.vz)/2),nt=(e,t)=>Re(G(e,t),[1,1,1]),rt=nt([[0,0],[1,.05],[.65,1],[0,1]],5),it=nt([[0,0],[.8,.05],[.55,.7],[1,.8],[.5,1],[0,1]],5),at=nt([[0,0],[.15,0],[.13,.25],[.22,.7],[.2,.95],[0,1]],6),ot=nt([[0,0],[.1,.06],[.155,.4],[.13,.8],[0,1]],8),st=nt([[0,0],[1,0],[0,1]],5),ct=(e,t,n,r,i,a,o,s=1)=>{let c=i.map((e,t)=>e-r[t]),l=Math.hypot(...c);He(e,t,r,Ue(c.map(e=>e/l),[Math.cos(n),0,-Math.sin(n)]),[0,0,0],[a*s,a,l],0,o)},Y=(e,t,n,r,i,a,o,s=1)=>ct(e,t,n.yaw,J(n,...r),J(n,...i),a,o,s),lt=([e,t,n],r,i,a,o)=>{let[s,c]=[t-a*Math.cos(r),n+a*Math.sin(r)];return[[e,s,c],[e,s-o*Math.cos(i),c+o*Math.sin(i)]]},ut=(e,t,n)=>{let r=.55*Math.sin(t)*n;return lt(e,r,r-.9*Math.max(0,-Math.sin(t+.5))*n,.43,.45)},dt=.88,ft=.66,pt=.4,mt=(o-dt-ft*Math.cos(.25)-.065)/(.8*Math.cos(pt)),ht=e=>e<.4?2.6*Math.sin(e/.4*Math.PI/2):e<.7?2.6-2.3*((e-.4)/.3)**2:.3*(1-(e-.7)/.3),gt=(e,t,n,r,i=1)=>{let a=n*Ze,o=r>0?ht(r):0,s=dt-.1*Math.sin(a)**2*i,c=.25-.12*o,[l,u]=[[0,s,-.02],[0,s+ft*Math.cos(c),ft*Math.sin(c)-.02]];Y(e,at,t,l,u,1.1,q.body,1.3),Y(e,ot,t,u,[0,u[1]+mt*Math.cos(pt),u[2]+mt*Math.sin(pt)],1.25,q.head);let d=t.chase?.06:.045;for(let n of[-1,1]){let[r,l]=ut([n*.14,s,-.02],a+(n>0?Math.PI:0),i);Y(e,rt,t,[n*.14,s,-.02],r,.105,q.leg),Y(e,rt,t,r,l,.085,q.leg),W(e,J(t,l[0],l[1]+.03,l[2]+.06),[.06,.03,.1],t.yaw,q.leg);let f=[n*.28,s+.55*Math.cos(c),.55*Math.sin(c)-.02],p=.45*Math.sin(a+(n<0?Math.PI:0))*i+(n>0?o:0),[m,h]=lt(f,p,p+.3,.38,.42);Y(e,rt,t,f,m,.095,q.arm),Y(e,it,t,m,h,.085,q.arm),Y(e,st,t,f,[f[0]+n*.18,f[1]+.2,f[2]-.02],.08,q.fin),W(e,J(t,n*.075,u[1]+mt*.45*Math.cos(pt)-.18*Math.sin(pt),u[2]+mt*.45*Math.sin(pt)+.18*Math.cos(pt)),[d,d*.7,.02],t.yaw,$e(t.chase),-2)}},_t=(e,t,n,r,i)=>{let a=t.map((t,n)=>t-e[n]),o=Math.hypot(...a),s=a.map(e=>e/o),c=Math.min(o,n+r-.01),l=(n*n-r*r+c*c)/(2*c),u=X(i,[s,-(s[0]*i[0]+s[1]*i[1]+s[2]*i[2])]);return X(e,[s,l],[u,Math.sqrt(Math.max(0,n*n-l*l))/Math.hypot(...u)])},vt=(e,n,r,i,a,o)=>{let s=i*Ze,c=dt-.08*Math.sin(s)**2*a,[l,u]=[r.map(e=>e*.45),[.1,.1,.12]],[d,f]=[[0,c,0],[0,c+.55,.02]];Y(e,at,n,d,f,1,r,1.25),Y(e,ot,n,f,[0,t,.02],1.1,l);let p=r.map(e=>.35+.65*e);W(e,J(n,0,f[1]+.17,.17),[.11,.035,.03],n.yaw,p,-2),W(e,J(n,0,c+.4,-.2),[.15,.2,.07],n.yaw,l);let[m,h,g]=Ie(n.yaw,n.pitch),_=J(n,0,1.3,0),v=X(_,[m,.2],[h,-.1],[g,.3]);for(let t of o)He(e,t,_,[m,h,g],[.2,-.1,.3],.8);for(let t of[-1,1]){let i=[t*.12,c,0],[o,d]=ut(i,s+(t>0?Math.PI:0),a);Y(e,rt,n,i,o,.1,l),Y(e,rt,n,o,d,.08,l),W(e,J(n,d[0],d[1]+.03,d[2]+.05),[.06,.03,.1],n.yaw,u);let f=X(v,[h,-.09],[g,t>0?.02:.26]),p=J(n,t*.27,c+.5,.02),_=_t(p,f,.34,.36,X([0,-1,0],[m,t*.5]));ct(e,rt,n.yaw,p,_,.07,r),ct(e,it,n.yaw,_,f,.06,l)}},yt=Re(G([[0,0],[.16,.04],[.33,.15],[.43,.3],[s,.45],[.4,.62],[.27,.76],[0,c]],10),q.shell),X=(e,...t)=>t.reduce((e,[t,n])=>e.map((e,r)=>e+t[r]*n),e),bt=.09,xt=(e,t,n,r=0,i=0)=>{let[a,o]=[Math.sin(t.yaw),Math.cos(t.yaw)],[s,l]=[-r*a-i*o,r*o-i*a],u=[(-a*s+o*l)*bt,1,(-o*s-a*l)*bt],d=u.map(e=>e/Math.hypot(...u)),f=X([t.x,t.y+Math.sin(n*2+t.id)*.06+c/2,t.z],[d,-c/2]),[p,m]=Ue(d,[o,0,-a]),h=m.map(e=>-e);He(e,yt,f,[p,m,d],[0,0,0],1),He(e,st,f,Ue(d.map(e=>-e),[o,0,-a]),[0,0,.06],[.09,.09,.3],0,q.fin);let g=t.chase?1.3:1;W(e,X(f,[d,.4],[h,.4]),[.12*g,.08*g,.05],t.yaw,$e(t.chase),-2);for(let r=0;r<4;r++){let i=Math.PI/4+r*Math.PI/2,[a,o]=[X([0,0,0],[p,Math.cos(i)],[h,Math.sin(i)]),X([0,0,0],[p,-Math.sin(i)],[h,Math.cos(i)])],s=n*8+t.id,c=e=>X([0,0,0],[a,Math.cos(e)],[d,Math.sin(e)]),l=X(f,[d,.4],[a,.42]),u=X(l,[c(.15+.55*Math.sin(s)),.32]),m=X(u,[c(.15+.55*Math.sin(s-1.2)),.3]),[g,_,v,y]=[X(l,[o,-.08]),X(l,[o,.08]),X(u,[o,.17]),X(u,[o,-.17])];We(e,g,_,v,y,q.fin),We(e,y,v,X(m,[o,.04]),X(m,[o,-.04]),q.fin)}},St=(e,t,n)=>{let r=[t.x,t.y,t.z];if(W(e,r,[l*.8,l*.8,l*.8],0,q.blob,-2),W(e,r,[l*.5,l*.5,l*.5],Math.PI/4,q.core,-2),n)for(let t of[1,2]){let i=l*(1-t*.3);W(e,r.map((e,r)=>e-n[r]*t*.2),[i,i,i],0,q.blob,-2)}},Ct=.05,wt=.3,Tt=96,Et=64,Dt=32,Ot=8,kt=24,At=12,jt=40,Mt=[5,1.6,5],Nt=Math.cos(.436),Pt=6,Ft=.3,It=.45,Lt=.4,Rt=.5,zt=.9,Bt=.3,Vt=14,Ht=.04,Ut=[.3,.03,.03],Wt=[[1,1,1],[1.6,.35,1.3],[.35,.35,2],[.5,.5,.5]],Gt=4.4,Kt=.035,qt=.03,Jt=30,Yt=({r:e,t})=>{let n=t/Ft;return n<1?e*Math.sqrt(n)*(1-n*n*n):0},Xt=({r:e,t,life:n})=>{let r=t/n;return e*(.3+.7*Math.min(1,r*2))*Math.min(1,(1-r)/.3)},Zt=({t:e,life:t})=>{let n=e/t;return .85*Math.min(1,n*10)*(1-n)},Qt=(e,t,n)=>{let r=[];for(let i=0;i<e.motes.length&&!e.dead;i+=3){let a=[0,1,2].map(n=>e.motes[i+n]-t[n]),o=Math.hypot(...a),s=(a[0]*n[0]+a[1]*n[1]+a[2]*n[2])/o;o<Pt&&s>Nt&&r.push(...a.map((e,n)=>t[n]+e),(s-Nt)/(1-Nt)*(1-o/Pt))}return r},$t=(e=64)=>{let t=[];return{clear(){t=[]},push(n,r){t.push(...n.map(e=>({t:r,v:e}))),t.splice(0,t.length-e)},due(e){let n=t.findIndex(({t})=>t+100>e);return t.splice(0,n<0?t.length:n).map(({v:e})=>e)}}},en=(e=Math.random,t)=>{let r=t,i=0,a=0,o=0,s=0,c=0,l=0,d=!0,p=/* @__PURE__ */ new Float32Array(384),m,h=()=>e()*2-1,g=/* @__PURE__ */ new Map,_=(e,t,n,r)=>{C.sparks.length<Tt&&C.sparks.push({p:e,v:t,life:n,c:r})},v=(t,n,r,i)=>{for(let a=0;a<n;a++){let[n,a]=[e()*U,h()],o=Math.sqrt(1-a*a)*r*(.5+e()*.5);_(t,[Math.cos(n)*o,a*r,Math.sin(n)*o],wt*(.6+e()*.4),i)}},x=(t,n,r)=>{for(let i=0;i<n;i++)C.smoke.push({p:t.map(e=>e+h()*.3),v:[h()*.5,.5+e()*.7,h()*.5],r:r*(.7+e()*.6),yaw:e()*U,t:0,life:1.5+e()});C.smoke.splice(0,C.smoke.length-jt)},S=(e,t)=>{C.blasts.push({p:e,r:t,t:0}),C.blasts.splice(0,C.blasts.length-At)},C={bob:0,roll:0,shakeYaw:0,shakePitch:0,kick:0,spin:0,flash:0,flashes:/* @__PURE__ */ new Map,sparks:[],gibs:[],casings:Array.from({length:Dt},()=>({p:[0,0,0],v:[0,0,0],life:0,rest:!0,own:!1})),splats:[],blasts:[],smoke:[],motes:/* @__PURE__ */ new Float32Array(384),hurt:0,gain:0,gainC:[1,1,1],dead:0,swings:/* @__PURE__ */ new Map,herd:/* @__PURE__ */ new Map,aim:/* @__PURE__ */ new Map,update(e,t,f,g=!1){let _=Math.min(1,f*10),v=e&&m?.ground&&e.ground?Math.hypot(e.x-m.x,e.z-m.z):0;v<1&&(i+=v*Gt),a+=(+(v>.5*f&&v<1)-a)*_,m=e,C.dead=Math.max(0,Math.min(1,C.dead+(g?f:-f)/Rt));let b=C.dead*C.dead*(3-2*C.dead);C.bob=Math.sin(i)*Kt*a-b*(n-Bt);let x=+!!(t&u.RIGHT)-!!(t&u.LEFT);s+=(x*qt-s)*Math.min(1,f*8),C.roll=s+b*zt,o*=Math.exp(-f*12),C.shakeYaw=h()*o,C.shakePitch=h()*o,C.kick*=Math.exp(-f*18),C.flash=Math.max(0,C.flash-f);for(let[e,t]of C.flashes)t>f?C.flashes.set(e,t-f):C.flashes.delete(e);c=Math.max(0,Math.min(Jt,c+(t&u.FIRE?60:-25)*f)),C.spin=(C.spin+c*f)%U;for(let e of C.sparks)e.v[1]-=9.8*f,e.p=e.p.map((t,n)=>t+e.v[n]*f),e.life-=f;C.sparks=C.sparks.filter(e=>e.life>0),C.hurt=Math.max(0,C.hurt-f),C.gain=Math.max(0,C.gain-f);for(let[e,t]of C.swings)t>f?C.swings.set(e,t-f):C.swings.delete(e);for(let e of C.blasts)e.t+=f;C.blasts=C.blasts.filter(e=>e.t<Ft);for(let e of C.gibs)e.life-=f,!e.rest&&T(e,f,Vt,3,.3,.5)&&e.gore&&(e.gore=!1,E(e.p[0],e.floor,e.p[2]));C.gibs=C.gibs.filter(e=>e.life>0);for(let e of C.casings)e.life-=f,e.life>0&&!e.rest&&(e.tilt+=e.spin*f,T(e,f,9.8,1.2,.4,.6)&&(e.spin*=.5),e.tilt*=+!e.rest);for(let e of C.splats)e.life-=f;C.splats=C.splats.filter(e=>e.life>0);for(let{p:e,v:t}of C.smoke)e.forEach((n,r)=>e[r]=n+t[r]*f),e[1]=Math.min(e[1],r?.map.sectors[y(r,e[0],e[2])]?.ceil??1/0);for(let e of C.smoke)e.t+=f;C.smoke=C.smoke.filter(e=>e.t<e.life),e&&(l+=f,C.motes.forEach((t,r)=>{let[i,a]=[r%3,Mt[r%3]],o=i==1?e.y+n:i?e.z:e.x;p[r]=d?h()*.1:p[r];let s=d?o+h()*a:t+(p[r]+Math.sin(l*.8+r)*.05)*f;C.motes[r]=o+((s-o+a)%(2*a)+2*a)%(2*a)-a}),d=!1)},hit(){C.hurt=It},pickup(e){[C.gain,C.gainC]=[Lt,e]},reset(e){r=e,C.sparks=[],C.gibs=[],C.splats=[],C.casings.forEach(e=>e.life=0),C.blasts=[],C.smoke=[],d=!0,C.flashes.clear(),C.swings.clear(),C.herd.clear(),C.aim.clear(),g.clear(),C.hurt=C.gain=C.flash=0},shot(t,n,r,i=!0){r?(C.kick=1,C.flash=Ct,o=.012):C.flashes.set(t.id,Ct);let a=[t.x,t.y,t.z];D(n,a,r);let s=n.map((e,t)=>e-a[t]),c=Math.hypot(...s);if(i&&c>0&&c<99.99)for(let t=0;t<4;t++){let t=s.map(t=>t/c*(2+e()*2)+h());t[1]+=1,_(a,t,wt*(.6+e()*.4))}},track(e,t){for(let{id:t,kind:n,x:r,z:i}of e){let e=C.herd.get(t),a=e?Math.hypot(r-e.x,i-e.z):0;C.herd.set(t,{x:r,z:i,kind:n,walked:(e?.walked??0)+(a<1?a:0)})}let n=/* @__PURE__ */ new Map;for(let{id:e,x:r,y:i,z:a}of t){let[t,o]=[g.get(e),[r,i,a]],s=t?Math.hypot(o[0]-t[0],o[1]-t[1],o[2]-t[2]):0;t&&s>1e-4&&C.aim.set(e,o.map((e,n)=>(e-t[n])/s)),n.set(e,o)}g=n;for(let e of C.aim.keys())n.has(e)||C.aim.delete(e)},event({type:t,id:n,x:i,y:a,z:o},s){let c=[i,a,o];if(t==f.DIE){let t=Qe[C.herd.get(n)?.kind??0]??Qe[0],s=10+Math.floor(e()*7);for(let n=0;n<s;n++){let n=e()*U,s=1.5+e()*3.5,[c,l]=[.05+e()*.08,Wt[Math.floor(e()*Wt.length)]],u=e()<.25;C.gibs.push({p:[i,a+h()*.3,o],v:[Math.cos(n)*s,2+e()*4,Math.sin(n)*s],yaw:e()*U,half:l.map(e=>e*c),size:l[1]*c,c:u?Ut:t[Math.floor(e()*t.length)],gore:u,life:2+e(),floor:r?.map.sectors[y(r,i,o)]?.floor??a,rest:!1})}C.gibs.splice(0,C.gibs.length-Et),S(c,.6),v(c,14,5),x(c,6+Math.floor(e()*3),.3)}else t==f.BOOM?(S(c,.25),v(c,8,3),x(c,3,.15)):t==f.HURT?n!=s&&v(c,8,3,[1,.12,.08]):t==f.SWING&&C.swings.set(n,Xe)}},w=[0,0,0],T=(e,t,n,i,a,o)=>{e.v[1]-=n*t,e.p.forEach((n,r)=>w[r]=n+e.v[r]*t),r&&b(r,e.p,w)&&(e.v[0]*=-.3,e.v[2]*=-.3,[w[0],w[2]]=[e.p[0],e.p[2]]);let s=r?.map.sectors[y(r,w[0],w[2])];e.floor=s?.floor??e.floor,s&&w[1]>s.ceil-e.size&&(w[1]=s.ceil-e.size,e.v[1]=Math.min(e.v[1],0)*.3);let c=w[1]<=e.floor+e.size;return c&&(w[1]=e.floor+e.size,e.v[1]<-i?[e.v[0],e.v[1],e.v[2]]=[e.v[0]*o,-e.v[1]*a,e.v[2]*o]:(e.v.fill(0),e.rest=!0)),w.forEach((t,n)=>e.p[n]=t),c},E=(t,n,r)=>{C.splats.push({p:[t,n+.004,r],r:.1+e()*.12,yaw:e()*U,life:8}),C.splats.splice(0,C.splats.length-kt)},D=(t,n,i)=>{let[a,o]=[C.casings[0],0];for(let e of C.casings)o+=+(e.life>0&&!e.own),a=e.life<a.life?e:a;if(!i&&(a.life>0||o>=Ot))return;let s=Math.hypot(...n.map((e,n)=>e-t[n]))||1,c=n.map((e,n)=>(e-t[n])/s),l=Math.hypot(c[0],c[2])||1,u=[-c[2]/l,0,c[0]/l],d=ze(u,c),f=i?[.32,-.22,.62]:[.3,-.45,.3],[p,m,h]=[.9+e()*.8,1+e(),e()*.6-.3];for(let e=0;e<3;e++)a.p[e]=t[e]+u[e]*f[0]+d[e]*f[1]+c[e]*f[2],a.v[e]=u[e]*p+d[e]*m+c[e]*h;r&&b(r,t,a.p)&&t.forEach((e,t)=>a.p[t]=e),Object.assign(a,{yaw:e()*U,tilt:e()*U,spin:(e()<.5?-1:1)*(15+e()*20),size:i?.01:.006,life:3+e(),floor:r?.map.sectors[y(r,a.p[0],a.p[2])]?.floor??t[1]-1.6,rest:!1,own:i})};return C},tn=[[1,1,1],[.95,.15,.1],[.2,.4,1],[1,.85,.15]],nn=(e,t=1)=>`rgba(${e.map(e=>Math.round(e*255))},${t})`,rn=.3,an=[.25,1,.45],on=[.85,.1,.1],sn=[.05,.05,.06],cn=(e,[t,n,r],[i,a,o],s,c,l,u=-1)=>{let[d,f]=[Math.sin(c),Math.cos(c)];W(e,[t+i*f+o*d,n+a,r-i*d+o*f],s,c,l,u)},ln=(e,{id:t,kind:n,x:r,y:i,z:a},o)=>{let s=o*2+t,c=[r,i+(n?.55:.4)+Math.sin(o*2.5+t)*.07,a],l=tn[n];if(n==p.HEALTH){W(e,c,[.2,.2,.2],s,l);for(let t of[[.21,.06,.21],[.06,.21,.21],[.21,.21,.06]])W(e,c,t,s,on)}else cn(e,c,[0,.17,0],[.1,.1,.025],s,l,-2),cn(e,c,[0,.17,0],[.04,.04,.035],s,sn),cn(e,c,[0,-.1,0],[.025,.17,.025],s,l),cn(e,c,[.06,-.24,0],[.05,.03,.025],s,l),cn(e,c,[.045,-.15,0],[.035,.025,.025],s,l)},un=({map:e},{front:t,back:n})=>{let[r,i]=[e.sectors[t],e.sectors[n]];return[Math.max(r.floor,i.floor),Math.min(r.ceil,i.ceil)]},dn=(e,t,n,r,i,a,o=-1)=>{let[s,c]=[t.bx-t.ax,t.bz-t.az];W(e,[(t.ax+t.bx)/2,n,(t.az+t.bz)/2],[Math.hypot(s,c)/2,r,i],Math.atan2(-c,s),a,o)},fn=(e,t,n)=>{let{gates:r=[],exit:i}=t.map;for(let{sector:n,key:i}of r){if(!t.barred.has(n))continue;let r=tn[i];for(let i of t.edges.filter(e=>e.front==n&&e.back>=0)){let[n,a]=un(t,i),o=Math.ceil(Math.hypot(i.bx-i.ax,i.bz-i.az)/rn);for(let t=0;t<o;t++){let s=(t+.5)/o;W(e,[i.ax+(i.bx-i.ax)*s,(n+a)/2,i.az+(i.bz-i.az)*s],[.03,(a-n)/2,.03],0,r)}dn(e,i,n+.1,.04,.04,r),dn(e,i,a-.1,.04,.04,r)}}if(i!==void 0&&!t.barred.has(i)){let r=an.map(e=>e*(.75+.25*Math.sin(n*4)));for(let n of t.edges.filter(e=>e.front==i&&e.back>=0)){let[i,a]=un(t,n);for(let t of[i+.04,a-.04])dn(e,n,t,.04,.04,r,-2);for(let[t,o]of[[n.ax,n.az],[n.bx,n.bz]])W(e,[t,(i+a)/2,o],[.05,(a-i)/2,.05],0,r,-2)}}},pn=(e,t,n)=>{let r=t.map.goal,i=r&&t.map.sectors[y(t,r[0],r[1])]?.floor;if(!r||i===void 0)return;let a=Math.sin(n*4),[o,s]=[1*(.7+a*.07),[1,.85,.25].map(e=>e*(.75+a*.25))];for(let t of[0,1])W(e,[r[0],i+.03+t*.005,r[1]],[o,.03,o],n/2+t*Math.PI/4,s,-2);W(e,[r[0],i+.8+Math.sin(n*2)*.1,r[1]],[.15,.15,.15],n*2,[1,.95,.5],-2)},mn=(e,t,n,r)=>{for(let t of n)ln(e,t,r);fn(e,t,r),pn(e,t,r)},hn={TITLE:0,NOTE:1},gn=[2,3,8],_n=2,vn=.35,yn=`h1,h2,h4,h5,ul,ol,aside,section,s{position:absolute;margin:0;padding:0;pointer-events:none;user-select:none;white-space:pre-line}h1,h2,h4{left:0;right:0;text-align:center}h1{top:14%;font-size:20px;line-height:1.4}h1::first-line{font-size:44px}h2{top:32%;font-size:22px}h5{top:8px;right:12px;font-size:12px}h4{top:28%;font-size:22px;line-height:1.5;color:#fcc;text-shadow:0 2px 8px #800}h4::first-line{font-size:52px;color:#f33}s{inset:0;opacity:0}s:first-of-type{background:radial-gradient(#d000 45%,#d00 115%)}ul{--f:0;--s:0;--d:0;left:12px;bottom:12px;width:min(40vw,140px);height:22px;border-radius:4px;overflow:hidden;background:#0009;box-shadow:0 0 0 1px #0008,0 0 0 calc(var(--d)*3px) #f22;transform:translateX(calc(var(--s)*1px));filter:brightness(calc(1 + var(--f)*2))}ul li{position:absolute;top:0;bottom:0;left:0;transition:width .15s}ul li+li{right:0;width:auto;text-align:center;line-height:22px;font-size:14px}ol{left:calc(22px + min(40vw,140px));bottom:12px;display:flex;gap:4px;list-style:none}ol li{width:16px;height:22px;border-radius:3px;box-shadow:0 0 0 1px #0008}aside,section{inset:0;z-index:1;display:flex;flex-direction:column;align-items:center;gap:14px;padding:0 12px;text-align:center;background:#000b}aside{justify-content:center}section{padding-top:10%;font-size:20px}:is(aside,section):not([hidden])~:is(h1,h2){visibility:hidden}h6{margin:0;font-size:inherit;text-shadow:none}aside h6{font:900 clamp(36px,13vw,84px)/1 system-ui,sans-serif;letter-spacing:.04em;transform:skewX(-8deg);background:linear-gradient(#fe6,#f92 45%,#d21);-webkit-background-clip:text;background-clip:text;color:transparent;filter:drop-shadow(0 3px 0 #500) drop-shadow(0 0 14px #f604)}section h6{font-size:20px;line-height:1.5}section h6::first-line{font-size:48px}strong{font-size:22px}small{font-size:16px;opacity:.8}table{border-collapse:collapse;font-size:16px}td,th{padding:2px 10px}th{opacity:.6}td+td,th+th{text-align:left}td+td+td,th+th+th{text-align:right}aside td:first-child{text-align:right;opacity:.7}`,bn=e=>{e.insertAdjacentHTML(`beforeend`,`<s></s><s></s><ul><li></li><li></li></ul><ol>${`<li hidden></li>`.repeat(3)}</ol><aside hidden><h6>TINY FPS</h6><span></span><table><tr><td>Mouse<td>look<tr><td>WASD / ZQSD<td>move<tr><td>Space<td>jump<tr><td>Left click<td>fire<tr><td>M<td>mute</table><strong></strong></aside><section hidden><h6></h6><table><thead><tr><th><th>Player<th>Kills<th>Deaths</thead><tbody></tbody></table><small></small></section><h1 hidden></h1><h2 hidden></h2><h4 hidden></h4><h5 hidden>Muted (M)</h5>`);let t=t=>e.querySelector(t),[n,r]=[...e.querySelectorAll(`s`)],[i,a,o]=[t(`ul`),t(`ul li`),t(`ul li+li`)],s=[...e.querySelectorAll(`ol li`)],[c,l,u,d]=[`h4`,`h5`,`section`,`aside`].map(e=>t(e)),[f,p,m]=[`section h6`,`section tbody`,`small`].map(e=>t(e)),[h,g]=[`aside span`,`strong`].map(e=>t(e)),_=[t(`h1`),t(`h2`),u];s.forEach((e,t)=>e.style.background=nn(tn[t+1]));let v=gn.map(()=>0),y=0,[b,x]=[0,0],S=(e,t)=>e.textContent!=t&&(e.textContent=t);return{hp(e){y&&(b=e>y?vn:b,x=e<y?vn:x),y=e,a.style.width=`${e/9*100}%`,a.style.background=`hsl(${(Math.max(e,1)-1)/8*110},75%,42%)`,S(o,String(e))},keys:e=>s.forEach((t,n)=>t.hidden=!(e>>n+1&1)),dead(e){c.hidden=!e,e&&S(c,`You died\nRespawn in ${e}`)},fade(e,t,i,a){n.style.opacity=String(Math.max(e*.9,t&&.2+t*.4)),r.style.opacity=String(i*.32),r.style.background=nn(a)},title(e,t){d.hidden=!e,S(h,t),S(g,e)},end(e,t,n){S(f,e),S(m,n),p.replaceChildren(...t.map(([e,t,n,r])=>{let i=document.createElement(`tr`);return[`●`,e,n,r].forEach((e,n)=>{let r=i.insertCell();r.textContent=String(e),r.style.color=n?``:t}),i})),_[_n].hidden=!1,v[_n]=gn[_n]},mute:e=>l.hidden=!e,say(e,t){_[e].textContent=t,_[e].hidden=!1,v[e]=gn[e]},update(e){v.forEach((t,n)=>{t>0&&(v[n]=t-e)<=0&&(_[n].hidden=!0)}),b=Math.max(0,b-e),x=Math.max(0,x-e);for(let[e,t]of[[`--f`,b/vn],[`--d`,x/vn],[`--s`,Math.sin(x*90)*8*x/vn]])i.style.setProperty(e,String(t))}}},xn=.0025,Sn=Math.PI*2,Cn={KeyW:u.FORWARD,ArrowUp:u.FORWARD,KeyS:u.BACK,ArrowDown:u.BACK,KeyA:u.LEFT,ArrowLeft:u.LEFT,KeyD:u.RIGHT,ArrowRight:u.RIGHT,Space:u.JUMP},wn=e=>Cn[e]??0,Tn=(e,t=()=>{})=>{let n=new AbortController,i=(e,t,r)=>e.addEventListener(t,r,{signal:n.signal}),a={yaw:0,pitch:0,keys:0,locked:()=>e.getRootNode().pointerLockElement==e,destroy:()=>n.abort()};i(e,`click`,()=>{Promise.resolve(e.requestPointerLock()).catch(()=>{})}),i(document,`mousemove`,e=>{a.locked()&&(a.yaw=((a.yaw-e.movementX*xn)%Sn+Sn)%Sn,a.pitch=Math.max(-1.55,Math.min(r,a.pitch-e.movementY*xn)))}),i(document,`keydown`,e=>{let n=wn(e.code);e.code==`KeyM`&&!e.repeat&&a.locked()&&t(),n&&a.locked()&&(a.keys|=n,e.preventDefault())}),i(document,`keyup`,e=>{a.keys&=~wn(e.code)}),i(document,`mousedown`,e=>{e.button==0&&a.locked()&&(a.keys|=u.FIRE)}),i(document,`mouseup`,e=>{e.button==0&&(a.keys&=~u.FIRE)});let o=()=>{a.keys=0};return i(window,`blur`,o),i(document,`pointerlockchange`,()=>a.locked()||o()),a},En=e=>{for(let t=0;t<2;t++)e=Math.imul(e^e>>>16,73244475);return(e^e>>>16)>>>0},Dn=(e,t,n)=>{let[r,i,o,s]=[Math.sin(e),Math.cos(e),Math.sin(t),Math.cos(t)],c=En(n+1),l=(c&65535)/65536*U,u=(c>>>16)/65535,[d,f]=[Math.cos(l)*u*a,Math.sin(l)*u*a],p=[-r*s+i*d+r*o*f,o+s*f,-i*s-r*d+i*o*f],m=Math.hypot(...p);return[p[0]/m,p[1]/m,p[2]/m]},On=(e,t,n,r)=>{let{map:i,edges:a}=e,o=Math.max(y(e,t[0],t[2]),0),s=0;for(let e=0;e<=i.sectors.length;e++){let{floor:e,ceil:c}=i.sectors[o],l=1/0,u;for(let e of a){if(e.front!==o)continue;let[r,i]=[e.bx-e.ax,e.bz-e.az],a=n[2]*r-n[0]*i;if(a>=0)continue;let c=Math.max(((t[2]-e.az)*r-(t[0]-e.ax)*i)/-a,s),d=((t[0]+c*n[0]-e.ax)*r+(t[2]+c*n[2]-e.az)*i)/(r*r+i*i);d>-1e-9&&d<1+1e-9&&(c<l-1e-9||c<l+1e-9&&e.back<0)&&([l,u]=[c,e])}let d=n[1]<0?(e-t[1])/n[1]:n[1]>0?(c-t[1])/n[1]:1/0;if(d<=l||!u)return Math.min(Math.max(d,s),r);if(l>=r)return r;let f=t[1]+l*n[1],p=u.back<0?void 0:i.sectors[u.back];if(!p||f<Math.max(e,p.floor)||f>Math.min(c,p.ceil))return l;[o,s]=[u.back,l]}return s},kn=(e,t,n)=>{let[r,i]=[e[0]-n.x,e[2]-n.z],a=t[0]*t[0]+t[2]*t[2],o=r*t[0]+i*t[2],s=r*r+i*i-n.r*n.r,[c,l]=[0,1/0];if(a>1e-12){let e=o*o-a*s;if(e<0)return 1/0;[c,l]=[Math.max(0,(-o-Math.sqrt(e))/a),(-o+Math.sqrt(e))/a]}else if(s>0)return 1/0;let[u,d]=[n.y-e[1],n.y+n.h-e[1]];if(t[1]){let[e,n]=[u/t[1],d/t[1]];c=Math.max(c,Math.min(e,n)),l=Math.min(l,Math.max(e,n))}else if(u>0||d<0)return 1/0;return c<=l?c:1/0},An=({id:n,x:r,y:i,z:a})=>({id:n,x:r,y:i,z:a,r:e,h:t}),jn=({id:e,kind:t,x:n,y:r,z:i})=>{let a=t===d.WALKER;return{id:e,x:n,y:r,z:i,r:a?.4:s,h:a?o:c}},Mn=(e,t,r,i)=>{let a=[t.x,t.y+n,t.z],o=Dn(t.yaw,t.pitch,r),s=On(e,a,o,100),c=-1;for(let e of i){let n=e.id===t.id?1/0:kn(a,o,e);n<s&&([s,c]=[n,e.id])}return{id:t.id,x:a[0]+o[0]*s,y:a[1]+o[1]*s,z:a[2]+o[2]*s,hit:c}},Nn=120,Pn=16,Fn=({id:e,x:t,y:n,z:r,vy:i,yaw:a,pitch:o,ground:s,cool:c})=>({id:e,x:t,y:n,z:r,vy:i,yaw:a,pitch:o,ground:s,cool:c}),In=(e,t,n)=>({x:e.x+(t.x-e.x)*n,y:e.y+(t.y-e.y)*n,z:e.z+(t.z-e.z)*n}),Ln=(e,t,n)=>Ee(e+(((t-e+Math.PI)%U+U)%U-Math.PI)*n),Rn=(e,t)=>(t-e&65535)<32768,zn=(e,t,n)=>({...t,...In(e,t,n),yaw:Ln(e.yaw,t.yaw,n),pitch:e.pitch+(t.pitch-e.pitch)*n}),Bn=e=>{let t=e,n,r=0,i=[],a,o,s,c=!0,l=/* @__PURE__ */ new Map,u=[],d=e=>{let t=e-100,n=0;for(;n+1<u.length&&u[n+1].t<=t;)n++;let r=u[n],i=u[n+1]??r;return{a:r,b:i,u:i.t>r.t?Math.max(0,Math.min(1,(t-r.t)/(i.t-r.t))):0}},f=e=>{if(!u.length)return;let{a:t,b:n,u:r}=d(e);return(t.s.tick+(n.s.tick-t.s.tick&65535)*r)%65536},p=e=>{let t=u.at(-1);if(!t)return[];let{a:r,b:i,u:a}=d(e);return t.s.players.filter(e=>e.id!==n).map(e=>{let t=r.s.players.find(t=>t.id===e.id),n=i.s.players.find(t=>t.id===e.id);return t&&n?zn(Fn(t),Fn(n),a):Fn(n??t??e)})},m=(e,t,n)=>{if(!u.length)return[];let{a:r,b:i,u:a}=d(e),o=t(r.s);return t(i.s).map(e=>{let t=o.find(t=>t.id===e.id);return t?n(t,e,a):e})},h=e=>m(e,e=>e.monsters,(e,t,n)=>({...t,...In(e,t,n),yaw:Ln(e.yaw,t.yaw,n)}));return{you:e=>{n=e},step:(e,n)=>{let d=te({...e,seq:r,view:n===void 0?void 0:f(n)});if(s=void 0,r=r+1&65535,i.push(d),i.length>Nn&&i.shift(),a&&(o=Fn(a),c&&Pe(a,d,t))){let e=n??1/0,r=h(e).filter(e=>!l.has(e.id)),i=u.at(-1)?.s.players??[],o=p(e).filter(e=>i.find(t=>t.id===e.id)?.hp);s=Mn(t,a,d.seq,[...o.map(An),...r.map(jn)]),s.hit>=16&&l.set(s.hit,d.seq)}return d},snapshot:(e,r)=>{u.push({t:r,s:e}),u.length>Pn&&u.shift(),_(t,e.keys,e.phase);let s=e.players.find(e=>e.id===n);s&&(c=s.hp>0,i=i.filter(e=>!Rn(e.seq,s.ack)),l.forEach((t,n)=>{let r=e=>e.monsters.some(e=>e.id===n);Rn(t,s.ack)&&(r(e)||!u.some(e=>r(e.s)))&&l.delete(n)}),a=Fn(s),o=Fn(a),i.forEach((e,n)=>{n===i.length-1&&(o=Fn(a)),c&&Pe(a,e,t)}))},monsters:h,bullets:e=>m(e,e=>e.bullets,(e,t,n)=>({...t,...In(e,t,n)})),level:e=>{t=e,u.length=0,l.clear(),a=o=s=void 0},shot:()=>s,me:e=>a&&o?zn(o,a,Math.max(0,Math.min(1,e))):void 0,others:p}},Vn={SPOT:0,POINT:1},Hn=[1.2,1.1,.9],Un=[3,1.4,.4],Wn=Math.cos(.436),Gn=.5,Kn=2,qn=4,Jn=(e,t,n)=>({kind:Vn.POINT,p:e,d:[0,0,0],cone:0,range:t,color:n,shadow:!1}),Yn=(e,t,n,r,i,a=!1)=>({...Jn(e,r,i),kind:Vn.SPOT,d:t,cone:n,shadow:a}),Xn=(e,...t)=>e.map((e,n)=>e+t.reduce((e,[t,r])=>e+t[n]*r,0)),Zn=(e,t)=>e.map(e=>e*t),Qn=4,$n=/* @__PURE__ */ new WeakMap,er=e=>{let t=$n.get(e);return t||(t=e.map.sectors.filter(e=>e.ft==h.LAVA).map(({pts:e,floor:t})=>{let[n,r]=[0,1].map(t=>e.map(e=>e[t])),[i,a]=[Math.max(...n)-Math.min(...n),Math.max(...r)-Math.min(...r)];return{p:[(Math.max(...n)+Math.min(...n))/2,t+2,(Math.max(...r)+Math.min(...r))/2],range:4+Math.sqrt(i*a)*.6}}),$n.set(e,t)),t},tr=(e,t)=>{let n=e[0]+e[2];return Zn([1.4,.45,.12],1+.18*Math.sin(t*1.3+n)+.1*Math.sin(t*11+n*3)+.06*Math.sin(t*23+n))},nr=({eye:e,yaw:t,pitch:r,dead:i,others:a,fx:o,bullets:s,lvl:c,t:l})=>{let[u,d,f]=Ie(t,r),p=[];i||(p.push(Yn(Xn(e,[u,.2],[d,-.2]),f,Wn,25,Hn,!0)),o.flash>0&&p.push(Yn(Xn(e,[u,.26],[d,-.24],[f,.6+Ge*.8]),f,Gn,8,Un,!0)));let m=t=>Math.hypot(t[0]-e[0],t[1]-e[1],t[2]-e[2]),h=[];a.map(e=>({p:e,e:[e.x,e.y+n,e.z]})).sort((e,t)=>m(e.e)-m(t.e)).forEach(({p:e,e:t},n)=>{let[r,i,a]=Ie(e.yaw,e.pitch);h.push(Yn(Xn(t,[a,.35]),a,Wn,25,Hn,n<Kn)),o.flashes.has(e.id)&&h.push(Jn(Xn([e.x,e.y+1.25,e.z],[r,.36],[i,-.12],[a,.8]),6,Un))});for(let e of o.blasts){let t=Yt(e);t>0&&h.push(Jn(e.p,6,Zn(Un,t*6)))}o.hurt>0&&h.push(Jn(e,4,Zn([2,.15,.1],Math.min(1,o.hurt*10)))),s.map(e=>Jn([e.x,e.y,e.z],3,[1.2,.4,.15])).sort((e,t)=>m(e.p)-m(t.p)).slice(0,qn).forEach(e=>h.push(e)),er(c).filter(e=>m(e.p)<e.range*2).map(({p:e,range:t})=>Jn(e,t,tr(e,l))).sort((e,t)=>m(e.p)-m(t.p)).slice(0,Qn).forEach(e=>h.push(e));let{exit:g,goal:_}=c.map;if(g!==void 0){let e=1+.3*Math.sin(l*4);if(c.barred.has(g)){if(_){let t=c.map.sectors[y(c,_[0],_[1])]?.floor??0;h.push(Jn([_[0],t+1,_[1]],8,Zn([.2,1.2,1.5],e)))}}else{let{floor:t}=c.map.sectors[g];for(let{ax:n,az:r,bx:i,bz:a,front:o,back:s}of c.edges)if(o==g&&s>=0){let[o,s]=[i-n,a-r],c=.5/Math.hypot(o,s);h.push(Jn([(n+i)/2-s*c,t+1.2,(r+a)/2+o*c],8,Zn([.2,1.5,.3],e)))}}}return p.concat(h.sort((e,t)=>m(e.p)-m(t.p)).slice(0,16-p.length))},rr=1.5,ir=(e,t)=>{let n=A[e]?.name;return`Level ${e+1}${n?`: ${n}`:``}${t?` (hard mode)`:``}`},ar=e=>{let t=Math.floor(e*10/60);return`${Math.floor(t/600)}:${String(Math.floor(t/10)%60).padStart(2,`0`)}.${t%10}`},or=({spawns:e},t,n)=>e.reduce((e,r)=>Math.hypot(r[0]-t,r[1]-n)<Math.hypot(e[0]-t,e[1]-n)?r:e)[2],sr=({lvl:e,you:t,roster:n,predictor:r,rd:i,delays:a,fx:o,hud:s,audio:c})=>{let l=-1,u=-1,d=0,f=/* @__PURE__ */ new Map,p={lvl:e,items:[],dead:!1,hp:0,name:ir(0,!1),returning:!1,alive:e=>f.get(e)!==0,snapshot(e){let n=t(),h=e.level*2+ +e.hard;if(h!=l){let t=A[e.level];if(!t)return;l=h,p.lvl=g(t),p.items=[],f=/* @__PURE__ */ new Map,u=-1,r.level(p.lvl),i?.level(p.lvl),o.reset(p.lvl),a.forEach(e=>e.clear()),p.name=ir(e.level,e.hard),s.say(hn.TITLE,`Level ${e.level+1}${e.hard?`
|
|
2
|
-
Hard mode`:``}`)}let _=e.players.find(e=>e.id==n),v=f.get(n);_&&v!==void 0&&_.hp<v&&o.hit(),f=new Map(e.players.map(e=>[e.id,e.hp]));for(let t of p.items)_&&!e.items.some(e=>e.id==t.id)&&Math.hypot(t.x-_.x,t.z-_.z)<rr&&(o.pickup(tn[t.kind]),c?.play(B.COLLECT));p.items=e.items,e.phase!=u&&(u=e.phase,p.returning=u==m.RETURN,s.say(hn.NOTE,u==m.RETURN?`Back to the start!`:`Find the exit`)),_?.hp===0&&!p.dead&&(d=e.tick),p.dead=_?.hp===0,p.hp=_?.hp??0,s.hp(p.hp),s.keys(e.keys);let y=180-(e.tick-d&65535);s.dead(p.dead?Math.max(1,Math.ceil(y/60)):0)},end(e,t,r){let i=new Map(n().map(e=>[e.id,e])),a=r.filter(e=>i.has(e.id)).sort((e,t)=>t.kills-e.kills||e.deaths-t.deaths||e.id-t.id).map(({id:e,kills:t,deaths:n})=>[i.get(e).name,i.get(e).color,t,n]);s.end(`Run complete\n${ar(e)}${t?` (hard)`:``}`,a,`Next run: Hard mode`)}};return p},cr=Object.keys(h).length,lr=(e,t,n)=>{let r=Math.imul(e,374761393)+Math.imul(t,668265263)+Math.imul(n,144665)|0;return r=Math.imul(r^r>>>13,1274126177),((r^r>>>16)>>>0)/4294967296},ur=(e,t,n,r)=>{let[i,a]=[Math.floor(e),Math.floor(t)],[o,s]=[e-i,t-a].map(e=>e*e*(3-2*e)),c=(e,t)=>lr((i+e)%n,(a+t)%n,r);return(c(0,0)*(1-o)+c(1,0)*o)*(1-s)+(c(0,1)*(1-o)+c(1,1)*o)*s},dr=(e,t,n,r,i)=>{let[a,o]=[0,.5];for(let s=0;s<r;s++,o/=2)a+=o*ur(e*(1<<s),t*(1<<s),n<<s,i+s);return a/(1-o*2)},fr=(e,t,n)=>e.map((e,r)=>e+(t[r]-e)*n),Z=(e,t)=>e.map(e=>e*t),Q=e=>Math.min(Math.max(e,0),1),pr=Math.PI*2,$=[];$[h.BRICK]=(e,t)=>{let n=Math.floor(t*8),r=e*4+(n&1)*.5,[i,a,o]=[Math.floor(r)%4,r%1,t*8%1],s=ur(e*16,t*16,16,5),c=.8+.4*s;return a<.05||o<.1?[Z([165,160,150],.7+.3*c),.1+.1*s]:[Z([170,78,56],(.8+.4*lr(i,n,1))*c*(.85+.15*o)),.7+.2*s+.1*Q(Math.min(a-.05,o-.1)*12)]},$[h.TILE]=(e,t)=>{let[n,r]=[Math.floor(e*4),Math.floor(t*4)],[i,a]=[e*4%1,t*4%1],o=ur(e*16,t*16,16,7),s=.9+.2*o;return i<.05||a<.05?[Z([85,85,80],s),.1]:[Z([196,190,172],s*(.88+.08*(n+r&1)+.1*lr(n,r,2))),.8+.1*o+.1*Q((Math.min(i,a)-.05)*14)]},$[h.LAVA]=(e,t)=>{let n=dr(e*4,t*4,4,4,11),r=Math.max(0,1-Math.abs(n-.5)*7)**2;return[fr(fr([55,8,4],[230,70,8],n),[255,215,90],r),.5]},$[h.STONE]=(e,t)=>{let n=dr(e*8,t*8,8,4,3);return[Z([128,124,120],.45+.7*(n*1.3+.1*lr(Math.floor(e*128),Math.floor(t*128),4))),Q(n*1.4-.2)]},$[h.METAL]=(e,t)=>{let[n,r,i,a]=[Math.floor(e*2),Math.floor(t*2),e*2%1,t*2%1],o=Q((ur(e*8,t*8,8,13)-.68)*6),s=e=>Math.min(Math.max(Math.round((e-.1)/.4),0),2)*.4+.1,[c,l]=[s(i),s(a)],u=c==l&&c==.5?1:Math.hypot(i-c,a-l),d=Q(1-u/.07),f=Z([98,108,124],(.9+.2*ur(e*32,t*32,32,9)+.12*lr(n,r,6))*(1+.3*d)*(.65+.35*Q((u-.06)/.04)));return Math.min(i,a,1-i,1-a)<.03?[Z([40,44,50],1+o),.05]:[fr(f,[105,68,45],o*.7),.5+.1*(1-o)+.4*d]},$[h.WOOD]=(e,t)=>{let n=Math.floor(t*8),r=e*2+lr(n,0,8)*2,[i,a,o]=[Math.floor(r)%2,r%1,t*8%1],s=lr(n,i,3),c=.5+.5*Math.sin(pr*(t*40+2.5*ur(e*4,t*4,4,21)+s*7));return o<.08||a<.015?[[28,18,10],.05]:[fr([92,58,32],[158,106,62],c*.6+s*.35),.7+.15*c+.15*Q((o-.08)*15)]},$[h.MOSS]=(e,t)=>{let n=Math.floor(t*4),r=e*2+(n&1)*.5,[i,a,o]=[Math.floor(r)%2,r%1,t*4%1],s=Math.min(a,1-a,(o<.5?o:1-o)*.5),c=a<.04||o<.06,l=Q((.3*dr(e*12,t*12,12,3,17)-.03-s)*12),u=ur(e*64,t*64,64,19),d=Z([128,126,118],(.7+.4*lr(i,n,5))*(.7+.5*ur(e*16,t*16,16,15)));return[fr(c?Z(d,.45):d,fr([52,100,34],[96,148,56],u),l),c?.1+.6*l*u:.6+.2*Q(s*30)+.2*l*u]},$[h.GRATE]=(e,t)=>{let[n,r]=[e*8%1,t*8%1],i=Math.max(Math.abs(n-.5),Math.abs(r-.5)),a=Math.min(Math.abs(n-.5),Math.abs(r-.5))>.43,o=ur(e*16,t*16,16,23);return i<.36?[[9,9,12],0]:[Z(fr([104,108,116],[96,80,68],Q((o-.55)*4)),(a?1.25:1)-(.5-i)*2.2+.2*o),a?1:.6+.4*Q((i-.36)*12)]};var mr=()=>{let e=new Uint8ClampedArray(cr*128*128*4);return $.forEach((t,n)=>{for(let r=0;r<16384;r++){let[i,a]=t(r%128/128,Math.floor(r/128)/128);e.set([...i,a*255],(n*128*128+r)*4)}}),e},hr=[.05,.045,.05,.02],gr=1<<18,_r=2,vr=512,yr=.3,br=.55,xr=4,Sr=`#version 300 es
|
|
1
|
+
var e=.4,t=1.8,n=1.55,r={FORWARD:1,BACK:2,LEFT:4,RIGHT:8,JUMP:16},i={BRICK:0,TILE:1,LAVA:2,STONE:3,METAL:4,WOOD:5,MOSS:6,GRATE:7},a=-30,o=(e,t,n,r,o)=>({min:[e,a,t],max:[n,o,r],top:i.TILE,side:i.BRICK}),s=([e,t,n],[r,i,a],o,s=o,c)=>({min:[e,t,n],max:[r,i,a],top:o,side:s,tint:c}),c={name:`Rooftops`,brushes:[o(4,56,24,76,10),o(4,36,24,53.5,10),o(4,20,24,36,12.4),o(4,0,24,12,12.4),o(27,20,47,50,8),o(27,60,47,76,8),s([9,12.2,39],[19,12.6,47],i.METAL,i.METAL),...((e,t)=>[1,2,3,4,5,6].map(n=>s([12,t,e-n],[14,t+.4*n,e-n+1],i.STONE,i.STONE)))(53,10),s([5,10,42],[5.5,14,42.5],i.METAL),s([5,10,50],[5.5,14,50.5],i.METAL),s([8,12.4,24],[20,12.85,24.45],i.METAL),s([36,8,22],[37,14.5,48],i.STONE,i.STONE,[1,.75,.62]),s([30,8,28],[34,10,32],i.STONE,i.BRICK),s([40,8,30],[44,10.2,34],i.STONE,i.BRICK),s([28,9.2,44],[35,12,46],i.METAL,i.GRATE,[.8,.9,1]),s([41.5,7.6,50],[42,8,60],i.METAL),s([30,8,64],[34,9.6,68],i.STONE,i.BRICK)],spawns:[[6,10,72,0],[10,10,72,0],[14,10,72,0],[18,10,72,0],[22,10,72,0],[8,10,66,0],[14,10,66,0],[20,10,66,0]],floor:-25,sun:{dir:[.6,.64,.48],color:[3,2.7,2.2]},sky:[[.12,.3,.75],[.7,.82,.95]],fog:[.7,.82,.95,.008]},l=65536/(2*Math.PI),u=e=>Math.round(e*l)&65535,d=e=>Math.max(-32768,Math.min(32767,Math.round(e))),f=e=>d(e*1e4),p=e=>e/1e4,m=e=>e/l,h=(e=0)=>Math.round(e*256)&16777215,g=e=>({...e,yaw:m(u(e.yaw)),pitch:p(f(e.pitch)),...e.view!==void 0&&{view:h(e.view)/256}}),_=e=>{let t=/* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(11));t.setUint8(0,1),t.setUint16(1,e.seq&65535),t.setUint8(3,e.keys&255),t.setUint16(4,u(e.yaw)),t.setInt16(6,f(e.pitch));let n=h(e.view);return t.setUint16(8,n>>8),t.setUint8(10,n&255),t.buffer},v=e=>{if(e.byteLength<4)return null;let t=new DataView(e),n=t.getUint8(3);if(t.getUint8(0)!==2||e.byteLength!==4+24*n)return null;let r=[];for(let e=0;e<n;e++){let n=4+e*24;r.push({id:t.getUint8(n),x:t.getFloat32(n+1),y:t.getFloat32(n+5),z:t.getFloat32(n+9),vy:t.getFloat32(n+13),yaw:m(t.getUint16(n+17)),pitch:p(t.getInt16(n+19)),ack:t.getUint16(n+21),ground:(t.getUint8(n+23)&1)==1})}return{tick:t.getUint16(1),players:r}},y=e=>{try{let t=JSON.parse(e);return t&&typeof t.t==`string`?t:null}catch{return null}},b=4,x=1024,S=(e,t)=>(e+x)*(2*x)+t+x,C=(e,t)=>Math.min(Math.max(Math.floor(e/t),-1024),1023),w=(e,t=b)=>{let n=/* @__PURE__ */ new Map;return e.brushes.forEach(({min:e,max:r},i)=>{for(let a=C(e[0],t);a<=C(r[0],t);a++)for(let o=C(e[2],t);o<=C(r[2],t);o++){let e=S(a,o),t=n.get(e);t?t.push(i):n.set(e,[i])}}),{def:e,cell:t,grid:n}},T=(e,t,n,r)=>{let i=[];for(let a=C(t-r,e.cell);a<=C(t+r,e.cell);a++)for(let t=C(n-r,e.cell);t<=C(n+r,e.cell);t++)for(let n of e.grid.get(S(a,t))??[])i.includes(n)||i.push(n);return i},E=(e,t,n,r)=>{let{cell:i,def:a}=e,o=r,s=C(t[0],i),c=C(t[2],i),l=Math.sign(n[0]),u=Math.sign(n[2]),d=l?((s+ +(l>0))*i-t[0])/n[0]:1/0,f=u?((c+ +(u>0))*i-t[2])/n[2]:1/0,p=d,m=f,h=l?i/Math.abs(n[0]):1/0,g=u?i/Math.abs(n[2]):1/0;for(;;){for(let r of e.grid.get(S(s,c))??[]){let e=a.brushes[r],[i,s]=[0,o];for(let r=0;r<3&&i<=s;r++){if(!n[r]){(t[r]<e.min[r]||t[r]>e.max[r])&&(s=-1);continue}let a=(e.min[r]-t[r])/n[r],o=(e.max[r]-t[r])/n[r];i=Math.max(i,Math.min(a,o)),s=Math.min(s,Math.max(a,o))}i<=s&&s>0&&i<o&&(o=i)}if(Math.min(p,m)>=o||Math.abs(s)>x||Math.abs(c)>x)return o;p<m?(s+=l,p+=h):(c+=u,m+=g)}},D=(e,t,n)=>{let[r,i,a]=[n[0]-t[0],n[1]-t[1],n[2]-t[2]],o=Math.hypot(r,i,a);return o>0&&E(e,t,[r/o,i/o,a/o],o)<o-1e-4},O=44100,k=([e,t,n,r,i,a,o,s,c,l,u],d)=>{let f=new Float32Array(Math.ceil((r+i+a)*O)),p=0,m=0,h=0,g=()=>{d=d+1831565813|0;let e=Math.imul(d^d>>>15,1|d);return e=e+Math.imul(e^e>>>7,61|e)^e,((e^e>>>14)>>>0)/2**31-1};for(let d=0;d<f.length;d++){let _=d/O;p+=t*2**(n*_)*(_>c?l:1)*(1+o*Math.sin(_*44))/O,p>=1&&(p%=1,h=g());let v=e==3?h:e==2?Math.sin(p*2*Math.PI):e?1-2*p:p<.5?1:-1;m+=(v-m)*s;let y=_<r?_/r:_<r+i?1:((r+i+a-_)/a)**2;f[d]=m*y*u}return f},A=[],j=(e,t=A)=>{let n=t[e].map((t,n)=>[k(t,e*9+n),Math.round((t[11]??0)*O)]);return Float32Array.from({length:Math.max(...n.map(([e,t])=>t+e.length))},(e,t)=>Math.tanh(n.reduce((e,[n,r])=>e+(n[t-r]??0),0)))},M=O*60/108,N=M/4,P=4,F=[2,150,-6,.002,0,.25,0,1,0,1,1],I=[3,9e3,0,.002,0,.15,0,.5,0,1,.5],L=[3,3e4,0,.001,0,.04,0,.7,0,1,.15],R=()=>{let e=new Float32Array(M*4*P),t={},n=(n,r)=>(t[String(r)]??=k(r,1)).forEach((t,r)=>e[(n*N+r)%e.length]+=t),r=e=>55*2**(e/12),i=[0,0,-4,-5],a=[0,0,12,0,0,7,12,7];for(let e=0;e<64;e++){let t=e%16;if(`x.......x.x.....`[t]==`x`&&n(e,F),`....x.......x...`[t]==`x`&&n(e,I),t%2==0){n(e,L);let o=i[e>>4]+a[t>>1];n(e,[1,r(o),0,.005,.08,.18,0,.15,0,1,.6])}}[15,12,10,19,17,15].forEach((e,t)=>n([18,24,28,50,54,60][t],[2,r(e+24),0,.02,.1,.6,.01,1,0,1,.5]));let o=e.reduce((e,t)=>Math.max(e,Math.abs(t)),0);return e.map(e=>e/o*.35)},ee=40,te=24,ne=.5,re=`tiny-fps:muted`,ie=()=>{let e=!1;return{unlock:()=>{},listen:()=>{},play:()=>{},mute:t=>{e=t},get muted(){return e},destroy:()=>{}}},ae=(e=A)=>{let t;try{t=new AudioContext}catch{return ie()}let n=(e,n=1)=>{let r=t.createGain();return r.gain.value=n,r.connect(e),r},r=n(t.destination),i=n(r,ne),a=[],o=!1,s=/* @__PURE__ */ new Set,c=[0,0,0],l=0,u=!1,d=e=>{let n=t.createBuffer(1,e.length,O);return n.getChannelData(0).set(e),n},f=e=>{let n=t.createBufferSource();return n.buffer=e,n};return{unlock(){if(t.resume().catch(()=>{}),!o){o=!0,e.forEach((t,n)=>a[n]=d(j(n,e)));let t=f(d(R()));t.loop=!0,t.connect(i),t.start()}},listen(e,t){[c,l]=[e,t]},play(e,i,o=1){let u=0,d=o;if(i){let[e,t,n]=i.map((e,t)=>e-c[t]),r=Math.hypot(e,t,n);d=Math.max(0,1-r/ee)**2*o,u=(e*Math.cos(l)-n*Math.sin(l))/Math.max(r,1)}let p=a[e];if(!p||!d)return;if(s.size>=te){let e=[...s].reduce((e,t)=>t.vol<e.vol?t:e);if(e.vol>=d)return;e.src.stop(),s.delete(e)}let m=f(p),h=t.createStereoPanner();h.pan.value=u,m.connect(h).connect(n(r,d));let g={vol:d,src:m};s.add(g),m.onended=()=>s.delete(g),m.start()},mute(e){u=e,r.gain.setTargetAtTime(+!e,t.currentTime,.05)},get muted(){return u},destroy:()=>void t.close().catch(()=>{})}},oe=()=>{try{return localStorage.getItem(re)==`1`}catch{return!1}},se=e=>{e.mute(!e.muted);try{localStorage.setItem(re,e.muted?`1`:`0`)}catch{}return e.muted},ce=4.4,le=.035,ue=.03,de=()=>{let e=0,t=0,n=0,i,a={bob:0,roll:0,update(o,s,c){let l=Math.min(1,c*10),u=o&&i?.ground&&o.ground?Math.hypot(o.x-i.x,o.z-i.z):0;u<1&&(e+=u*ce),t+=(+(u>.5*c&&u<1)-t)*l,i=o,a.bob=Math.sin(e)*le*t;let d=+!!(s&r.RIGHT)-!!(s&r.LEFT);n+=(d*ue-n)*Math.min(1,c*8),a.roll=n}};return a},fe=`h5,aside{position:absolute;margin:0;padding:0;pointer-events:none;user-select:none;white-space:pre-line}h5{top:8px;right:12px;font-size:12px}aside{inset:0;z-index:1;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:14px;padding:0 12px;text-align:center;background:#000b}h6{margin:0;font-size:inherit;text-shadow:none}aside h6{font:900 clamp(36px,13vw,84px)/1 system-ui,sans-serif;letter-spacing:.04em;transform:skewX(-8deg);background:linear-gradient(#fe6,#f92 45%,#d21);-webkit-background-clip:text;background-clip:text;color:transparent;filter:drop-shadow(0 3px 0 #500) drop-shadow(0 0 14px #f604)}strong{font-size:22px}table{border-collapse:collapse;font-size:16px}td{padding:2px 10px}td+td{text-align:left}td:first-child{text-align:right;opacity:.7}`,pe=e=>{e.insertAdjacentHTML(`beforeend`,`<aside hidden><h6>TINY FPS</h6><span></span><table><tr><td>Mouse<td>look<tr><td>WASD / ZQSD<td>move<tr><td>Space<td>jump<tr><td>M<td>mute</table><strong></strong></aside><h5 hidden>Muted (M)</h5>`);let t=t=>e.querySelector(t),[n,r]=[`h5`,`aside`].map(e=>t(e)),[i,a]=[`aside span`,`strong`].map(e=>t(e)),o=(e,t)=>e.textContent!=t&&(e.textContent=t);return{title(e,t){r.hidden=!e,o(i,t),o(a,e)},mute:e=>n.hidden=!e}},me=.0025,he=Math.PI*2,ge={KeyW:r.FORWARD,ArrowUp:r.FORWARD,KeyS:r.BACK,ArrowDown:r.BACK,KeyA:r.LEFT,ArrowLeft:r.LEFT,KeyD:r.RIGHT,ArrowRight:r.RIGHT,Space:r.JUMP},_e=e=>ge[e]??0,ve=(e,t=()=>{})=>{let r=new AbortController,i=(e,t,n)=>e.addEventListener(t,n,{signal:r.signal}),a={yaw:0,pitch:0,keys:0,locked:()=>e.getRootNode().pointerLockElement==e,destroy:()=>r.abort()};i(e,`click`,()=>{Promise.resolve(e.requestPointerLock()).catch(()=>{})}),i(document,`mousemove`,e=>{a.locked()&&(a.yaw=((a.yaw-e.movementX*me)%he+he)%he,a.pitch=Math.max(-1.55,Math.min(n,a.pitch-e.movementY*me)))}),i(document,`keydown`,e=>{let n=_e(e.code);e.code==`KeyM`&&!e.repeat&&a.locked()&&t(),n&&a.locked()&&(a.keys|=n,e.preventDefault())}),i(document,`keyup`,e=>{a.keys&=~_e(e.code)});let o=()=>{a.keys=0};return i(window,`blur`,o),i(document,`pointerlockchange`,()=>a.locked()||o()),a},ye=1/60,z=Math.PI*2,be=1e-6,B=.01,xe=e=>(e%z+z)%z,Se=e=>[-Math.sin(e),-Math.cos(e)],V=Math.fround,Ce=(e,t,n)=>Math.min(Math.max(e,t),n),we=(e,t,n)=>{let{spawns:r}=t.def,[i,a,o,s]=r[(n%r.length+r.length)%r.length];return{id:e,x:V(i),y:V(a),z:V(o),vy:0,yaw:xe(s),pitch:0,ground:!0}},Te=(e,t)=>e.max[1]>t+.5+B&&e.min[1]<t+1.8-B,Ee=(e,t,n)=>[t-Ce(t,e.min[0],e.max[0]),n-Ce(n,e.min[2],e.max[2])],De=(t,n)=>{let{brushes:r}=n.def,i=T(n,t.x,t.z,2*e),a=(e-be)**2;for(let n=0;n<4;n++){let n=0,o=0,s=0;for(let c of i){let i=r[c];if(!Te(i,t.y))continue;let[l,u]=Ee(i,t.x,t.z),d=l*l+u*u;if(d>=a)continue;if(d){let t=Math.sqrt(d);.4-t>n&&(n=e-t,o=l/t*n,s=u/t*n);continue}let[f,p,m]=[[t.x-i.min[0],-1,0],[i.max[0]-t.x,1,0],[t.z-i.min[2],0,-1],[i.max[2]-t.z,0,1]].reduce((e,t)=>t[0]<e[0]?t:e);f+.4>n&&(n=f+e,[o,s]=[p*n,m*n])}if(!n)return;t.x+=o,t.z+=s}},Oe=(t,n)=>{let{brushes:r}=n.def,i=(e-be)**2,a=-1/0,o=1/0;for(let s of T(n,t.x,t.z,e)){let e=r[s],[n,c]=Ee(e,t.x,t.z);n*n+c*c>=i||(e.max[1]<=t.y+.5+B?a=Math.max(a,e.max[1]):e.min[1]>=t.y+1.8-B&&(o=Math.min(o,e.min[1])))}return[a,o]},ke=(e,n,r,i,a)=>{e.x+=n,e.z+=r,De(e,a);let[o,s]=Oe(e,a);e.ground&&i&&(e.vy=7);let c=e.vy-20*ye;if(e.y+=(e.vy+c)/2*ye,e.vy=c,e.ground=!1,e.y+1.8>s&&(e.y=s-t,e.vy=Math.min(e.vy,0)),(e.vy<=0?e.y<=o+B:e.y<o)&&(e.y=o,e.vy<=0&&(e.vy=0,e.ground=!0)),e.y<a.def.floor){let{x:t,y:n,z:r}=we(e.id,a,e.id);Object.assign(e,{x:t,y:n,z:r,vy:0,ground:!0})}e.x=V(e.x),e.y=V(e.y),e.z=V(e.z),e.vy=V(e.vy)},Ae=(e,t,i)=>{e.yaw=xe(t.yaw),e.pitch=Math.max(-1.55,Math.min(n,t.pitch));let a=(t.keys&r.FORWARD?1:0)-(t.keys&r.BACK?1:0),o=(t.keys&r.RIGHT?1:0)-(t.keys&r.LEFT?1:0),s=6*ye/(Math.hypot(a,o)||1),[c,l]=Se(e.yaw);ke(e,(c*a-l*o)*s,(l*a+c*o)*s,(t.keys&r.JUMP)!==0,i)},je=120,Me=16,H=({id:e,x:t,y:n,z:r,vy:i,yaw:a,pitch:o,ground:s})=>({id:e,x:t,y:n,z:r,vy:i,yaw:a,pitch:o,ground:s}),Ne=(e,t,n)=>({x:e.x+(t.x-e.x)*n,y:e.y+(t.y-e.y)*n,z:e.z+(t.z-e.z)*n}),Pe=(e,t,n)=>xe(e+(((t-e+Math.PI)%z+z)%z-Math.PI)*n),Fe=(e,t)=>(t-e&65535)<32768,Ie=(e,t,n)=>({...t,...Ne(e,t,n),yaw:Pe(e.yaw,t.yaw,n),pitch:e.pitch+(t.pitch-e.pitch)*n}),Le=e=>{let t,n=0,r=[],i,a,o=[],s=e=>{let t=e-100,n=0;for(;n+1<o.length&&o[n+1].t<=t;)n++;let r=o[n],i=o[n+1]??r;return{a:r,b:i,u:i.t>r.t?Math.max(0,Math.min(1,(t-r.t)/(i.t-r.t))):0}},c=e=>{if(!o.length)return;let{a:t,b:n,u:r}=s(e);return(t.s.tick+(n.s.tick-t.s.tick&65535)*r)%65536};return{you:e=>{t=e},step:(t,o)=>{let s=g({...t,seq:n,view:o===void 0?void 0:c(o)});return n=n+1&65535,r.push(s),r.length>je&&r.shift(),i&&(a=H(i),Ae(i,s,e)),s},snapshot:(n,s)=>{o.push({t:s,s:n}),o.length>Me&&o.shift();let c=n.players.find(e=>e.id===t);c&&(r=r.filter(e=>!Fe(e.seq,c.ack)),i=H(c),a=H(i),r.forEach((t,n)=>{n===r.length-1&&(a=H(i)),Ae(i,t,e)}))},me:e=>i&&a?Ie(a,i,Math.max(0,Math.min(1,e))):void 0,others:e=>{let n=o.at(-1);if(!n)return[];let{a:r,b:i,u:a}=s(e);return n.s.players.filter(e=>e.id!==t).map(e=>{let t=r.s.players.find(t=>t.id===e.id),n=i.s.players.find(t=>t.id===e.id);return t&&n?Ie(H(t),H(n),a):H(n??t??e)})}}},Re={SPOT:0,POINT:1},ze=e=>[],Be=(e,t,n=0)=>{let[r,i,a,o,s,c]=[e,t,n].flatMap(e=>[Math.sin(e),Math.cos(e)]),l=[i,0,-r],u=[r*a,o,i*a];return[l.map((e,t)=>e*c-u[t]*s),u.map((e,t)=>e*c+l[t]*s),[-r*o,a,-i*o]]},Ve=[[-1,-1],[1,-1],[1,1],[-1,-1],[1,1],[-1,1]],He=(e,[t,n,r],[i,a,o],s,c,l=-1)=>{let[u,d]=[Math.sin(s),Math.cos(s)],f=(e,t)=>[e*d+t*u,-e*u+t*d];for(let s=0;s<3;s++)for(let u of[-1,1]){let[d,p]=[(s+1)%3,(s+2)%3],m=[0,0,0];m[s]=u;let[h,g]=f(m[0],m[2]);for(let[_,v]of Ve){let y=[0,0,0];[y[s],y[d],y[p]]=[u,_,v];let[b,x]=f(y[0]*i,y[2]*o);e.push(t+b,n+y[1]*a,r+x,h,m[1],g,c[0],c[1],l,c[2])}}},Ue=(e,t,n=[0,0,0])=>{let r=[];for(let i=1;i<e.length;i++){let[[a,o],[s,c]]=[e[i-1],e[i]],l=Math.hypot(s-a,c-o);if(!l)continue;let[u,d]=[(c-o)/l,-(s-a)/l],f=(e,i,a)=>{let[o,s]=[Math.cos(e*z/t),Math.sin(e*z/t)];r.push(n[0]+i*o,n[1]+i*s,n[2]-a,u*o,u*s,-d)};for(let e=0;e<t;e++)for(let[t,n,r]of[[e,a,o],[e+1,a,o],[e+1,s,c],[e,a,o],[e+1,s,c],[e,s,c]])f(t,n,r)}return r},We=(e,[t,n,r])=>Array.from({length:e.length*(10/6)},(i,a)=>{let[o,s]=[Math.floor(a/10),a%10];return s<6?e[o*6+s]:[t,n,-1,r][s-6]}),Ge=(e,t,n,[r,i,a],o,s,c=[1,1,1])=>{let[l,u,d]=typeof s==`number`?[s,s,s]:s,[f,p,m,h,g,_,v,y,b]=[...r,...i,...a],x=n[0]+f*o[0]+h*o[1]+v*o[2],S=n[1]+p*o[0]+g*o[1]+y*o[2],C=n[2]+m*o[0]+_*o[1]+b*o[2];for(let n=0;n<t.length;n+=10){let[r,i,a]=[t[n]*l,t[n+1]*u,t[n+2]*d],[o,s,w]=[t[n+3],t[n+4],t[n+5]];e.push(x+f*r+h*i-v*a,S+p*r+g*i-y*a,C+m*r+_*i-b*a,f*o+h*s-v*w,p*o+g*s-y*w,m*o+_*s-b*w,t[n+6]*c[0],t[n+7]*c[1],t[n+8],t[n+9]*c[2])}},Ke=(e,t)=>{let n=e[0]*t[0]+e[1]*t[1]+e[2]*t[2],r=t.map((t,r)=>t-e[r]*n),i=Math.hypot(...r)||1,[a,o,s]=r.map(e=>e/i);return[[a,o,s],[o*e[2]-s*e[1],s*e[0]-a*e[2],a*e[1]-o*e[0]],e]},qe=3.6,U=({x:e,y:t,z:n,yaw:r},i,a,o)=>{let[s,c]=[Math.sin(r),Math.cos(r)];return[e+i*c-o*s,t+a,n-i*s-o*c]},Je=(e,t,n,r,i)=>{let a=e.get(t)??{x:n,z:r,walked:0,vx:0,vz:0},o=Math.hypot(n-a.x,r-a.z),s=i>0&&o<1?1-Math.exp(-i*8):0;return a.vx+=((n-a.x)/(i||1)-a.vx)*s,a.vz+=((r-a.z)/(i||1)-a.vz)*s,a.walked+=o<1?o:0,[a.x,a.z]=[n,r],e.set(t,a),a},Ye=e=>Math.min(1,Math.hypot(e.vx,e.vz)/2),W=(e,t)=>We(Ue(e,t),[1,1,1]),Xe=W([[0,0],[1,.05],[.65,1],[0,1]],5),Ze=W([[0,0],[.8,.05],[.55,.7],[1,.8],[.5,1],[0,1]],5),Qe=W([[0,0],[.15,0],[.13,.25],[.22,.7],[.2,.95],[0,1]],6),$e=W([[0,0],[.1,.06],[.155,.4],[.13,.8],[0,1]],8),et=(e,t,n,r,i,a,o,s=1)=>{let c=i.map((e,t)=>e-r[t]),l=Math.hypot(...c);Ge(e,t,r,Ke(c.map(e=>e/l),[Math.cos(n),0,-Math.sin(n)]),[0,0,0],[a*s,a,l],o)},G=(e,t,n,r,i,a,o,s=1)=>et(e,t,n.yaw,U(n,...r),U(n,...i),a,o,s),tt=([e,t,n],r,i,a,o)=>{let[s,c]=[t-a*Math.cos(r),n+a*Math.sin(r)];return[[e,s,c],[e,s-o*Math.cos(i),c+o*Math.sin(i)]]},nt=(e,t,n)=>{let r=.55*Math.sin(t)*n;return tt(e,r,r-.9*Math.max(0,-Math.sin(t+.5))*n,.43,.45)},rt=.88,it=(e,n,r,i,a)=>{let o=i*qe,s=rt-.08*Math.sin(o)**2*a,[c,l]=[r.map(e=>e*.45),[.1,.1,.12]],[u,d]=[[0,s,0],[0,s+.55,.02]];G(e,Qe,n,u,d,1,r,1.25),G(e,$e,n,d,[0,t,.02],1.1,c);let f=r.map(e=>.35+.65*e);He(e,U(n,0,d[1]+.17,.17),[.11,.035,.03],n.yaw,f,-2),He(e,U(n,0,s+.4,-.2),[.15,.2,.07],n.yaw,c);for(let t of[-1,1]){let i=[t*.12,s,0],[u,d]=nt(i,o+(t>0?Math.PI:0),a);G(e,Xe,n,i,u,.1,c),G(e,Xe,n,u,d,.08,c),He(e,U(n,d[0],d[1]+.03,d[2]+.05),[.06,.03,.1],n.yaw,l);let f=[t*.27,s+.5,.02],p=.45*Math.sin(o+(t<0?Math.PI:0))*a,[m,h]=tt(f,p,p+.3,.34,.36);G(e,Xe,n,f,m,.07,r),G(e,Ze,n,m,h,.06,c)}},at=Object.keys(i).length,K=(e,t,n)=>{let r=Math.imul(e,374761393)+Math.imul(t,668265263)+Math.imul(n,144665)|0;return r=Math.imul(r^r>>>13,1274126177),((r^r>>>16)>>>0)/4294967296},q=(e,t,n,r)=>{let[i,a]=[Math.floor(e),Math.floor(t)],[o,s]=[e-i,t-a].map(e=>e*e*(3-2*e)),c=(e,t)=>K((i+e)%n,(a+t)%n,r);return(c(0,0)*(1-o)+c(1,0)*o)*(1-s)+(c(0,1)*(1-o)+c(1,1)*o)*s},ot=(e,t,n,r,i)=>{let[a,o]=[0,.5];for(let s=0;s<r;s++,o/=2)a+=o*q(e*(1<<s),t*(1<<s),n<<s,i+s);return a/(1-o*2)},J=(e,t,n)=>e.map((e,r)=>e+(t[r]-e)*n),Y=(e,t)=>e.map(e=>e*t),X=e=>Math.min(Math.max(e,0),1),st=Math.PI*2,Z=[];Z[i.BRICK]=(e,t)=>{let n=Math.floor(t*8),r=e*4+(n&1)*.5,[i,a,o]=[Math.floor(r)%4,r%1,t*8%1],s=q(e*16,t*16,16,5),c=.8+.4*s;return a<.05||o<.1?[Y([165,160,150],.7+.3*c),.1+.1*s]:[Y([170,78,56],(.8+.4*K(i,n,1))*c*(.85+.15*o)),.7+.2*s+.1*X(Math.min(a-.05,o-.1)*12)]},Z[i.TILE]=(e,t)=>{let[n,r]=[Math.floor(e*4),Math.floor(t*4)],[i,a]=[e*4%1,t*4%1],o=q(e*16,t*16,16,7),s=.9+.2*o;return i<.05||a<.05?[Y([85,85,80],s),.1]:[Y([196,190,172],s*(.88+.08*(n+r&1)+.1*K(n,r,2))),.8+.1*o+.1*X((Math.min(i,a)-.05)*14)]},Z[i.LAVA]=(e,t)=>{let n=ot(e*4,t*4,4,4,11),r=Math.max(0,1-Math.abs(n-.5)*7)**2;return[J(J([55,8,4],[230,70,8],n),[255,215,90],r),.5]},Z[i.STONE]=(e,t)=>{let n=ot(e*8,t*8,8,4,3);return[Y([128,124,120],.45+.7*(n*1.3+.1*K(Math.floor(e*128),Math.floor(t*128),4))),X(n*1.4-.2)]},Z[i.METAL]=(e,t)=>{let[n,r,i,a]=[Math.floor(e*2),Math.floor(t*2),e*2%1,t*2%1],o=X((q(e*8,t*8,8,13)-.68)*6),s=e=>Math.min(Math.max(Math.round((e-.1)/.4),0),2)*.4+.1,[c,l]=[s(i),s(a)],u=c==l&&c==.5?1:Math.hypot(i-c,a-l),d=X(1-u/.07),f=Y([98,108,124],(.9+.2*q(e*32,t*32,32,9)+.12*K(n,r,6))*(1+.3*d)*(.65+.35*X((u-.06)/.04)));return Math.min(i,a,1-i,1-a)<.03?[Y([40,44,50],1+o),.05]:[J(f,[105,68,45],o*.7),.5+.1*(1-o)+.4*d]},Z[i.WOOD]=(e,t)=>{let n=Math.floor(t*8),r=e*2+K(n,0,8)*2,[i,a,o]=[Math.floor(r)%2,r%1,t*8%1],s=K(n,i,3),c=.5+.5*Math.sin(st*(t*40+2.5*q(e*4,t*4,4,21)+s*7));return o<.08||a<.015?[[28,18,10],.05]:[J([92,58,32],[158,106,62],c*.6+s*.35),.7+.15*c+.15*X((o-.08)*15)]},Z[i.MOSS]=(e,t)=>{let n=Math.floor(t*4),r=e*2+(n&1)*.5,[i,a,o]=[Math.floor(r)%2,r%1,t*4%1],s=Math.min(a,1-a,(o<.5?o:1-o)*.5),c=a<.04||o<.06,l=X((.3*ot(e*12,t*12,12,3,17)-.03-s)*12),u=q(e*64,t*64,64,19),d=Y([128,126,118],(.7+.4*K(i,n,5))*(.7+.5*q(e*16,t*16,16,15)));return[J(c?Y(d,.45):d,J([52,100,34],[96,148,56],u),l),c?.1+.6*l*u:.6+.2*X(s*30)+.2*l*u]},Z[i.GRATE]=(e,t)=>{let[n,r]=[e*8%1,t*8%1],i=Math.max(Math.abs(n-.5),Math.abs(r-.5)),a=Math.min(Math.abs(n-.5),Math.abs(r-.5))>.43,o=q(e*16,t*16,16,23);return i<.36?[[9,9,12],0]:[Y(J([104,108,116],[96,80,68],X((o-.55)*4)),(a?1.25:1)-(.5-i)*2.2+.2*o),a?1:.6+.4*X((i-.36)*12)]};var ct=()=>{let e=new Uint8ClampedArray(at*128*128*4);return Z.forEach((t,n)=>{for(let r=0;r<16384;r++){let[i,a]=t(r%128/128,Math.floor(r/128)/128);e.set([...i,a*255],(n*128*128+r)*4)}}),e},lt=1<<18,ut=2,Q=1024,dt=.6,ft=Math.tan(70*Math.PI/360),pt=4,mt=`#version 300 es
|
|
3
2
|
layout(location=0)in vec3 p;layout(location=1)in vec3 n;layout(location=2)in vec4 c;
|
|
4
|
-
uniform mat4 m;out vec3 P,N;out vec4 C;
|
|
5
|
-
void main(){
|
|
6
|
-
|
|
3
|
+
uniform mat4 m;uniform float Y;uniform vec3 J[3];out vec3 P,N;out vec4 C;
|
|
4
|
+
void main(){
|
|
5
|
+
if(Y>.5){vec2 q=vec2(gl_VertexID&1,gl_VertexID>>1)*4.-1.;gl_Position=vec4(q,1,1);P=J[0]*q.x+J[1]*q.y+J[2];N=vec3(0);C=vec4(0);return;}
|
|
6
|
+
P=p;N=n;C=c;gl_Position=m*vec4(p,1);}`,ht=`#version 300 es
|
|
7
|
+
out vec2 U;void main(){U=vec2(gl_VertexID&1,gl_VertexID>>1)*2.;gl_Position=vec4(U*2.-1.,0,1);}`,gt=`#version 300 es
|
|
7
8
|
precision highp float;uniform sampler2D S[4];uniform vec4 Y;in vec2 U;out vec4 o;
|
|
8
|
-
vec3 b(vec2 x){vec3 c=texture(S[0],x).rgb;return c*smoothstep(.
|
|
9
|
+
vec3 b(vec2 x){vec3 c=texture(S[0],x).rgb;return c*smoothstep(.95,2.2,max(c.r,max(c.g,c.b)));}
|
|
9
10
|
#define T(k)texture(S[0],U+Y.xy*(k)).rgb
|
|
10
11
|
void main(){
|
|
11
12
|
vec3 c;
|
|
@@ -17,24 +18,37 @@ vec2 q=U-.5;
|
|
|
17
18
|
c=min(c,.6)+.4*(1.-exp(-max(c-.6,0.)/.4));
|
|
18
19
|
c*=1.-.7*dot(q,q);
|
|
19
20
|
c=pow(c,vec3(.92))+(fract(sin(dot(gl_FragCoord.xy+Y.w,vec2(12.9898,78.233)))*43758.5453)-.5)*.03;}
|
|
20
|
-
o=vec4(c,1);}`,
|
|
21
|
+
o=vec4(c,1);}`,_t=[[.04,8],[.25,30],[0,1],[.08,10],[.8,48],[.06,12],[.3,20],[.7,40],[.1,24]],vt=`#version 300 es
|
|
21
22
|
precision highp float;precision highp sampler2DArray;precision highp sampler2DArrayShadow;
|
|
22
|
-
in vec3 P,N;in vec4 C;uniform vec3 E;uniform float T,Z;uniform int Q;
|
|
23
|
-
uniform vec4 A[16],B[16],K[16],F;uniform vec3 G[16];uniform mat4 M[4];
|
|
23
|
+
in vec3 P,N;in vec4 C;uniform vec3 E;uniform float T,Z,Y;uniform int Q;
|
|
24
|
+
uniform vec4 A[16],B[16],K[16],F;uniform vec3 G[16],H[5],O,X;uniform mat4 M[4],W;
|
|
24
25
|
uniform sampler2DArray S;uniform sampler2DArrayShadow D;out vec4 o;
|
|
25
|
-
const vec2 SP[${
|
|
26
|
+
const vec2 SP[${_t.length}]=vec2[](${_t.map(([e,t])=>`vec2(${e},${t})`)});
|
|
26
27
|
vec3 e(vec3 x){return 2.*(1.-exp(-.5*x));}
|
|
27
28
|
void main(){
|
|
28
29
|
if(Z<0.)return;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
vec3 c=
|
|
30
|
+
if(Y>.5){
|
|
31
|
+
vec3 d=normalize(P);float h=d.y,k=max(dot(d,O),0.);
|
|
32
|
+
vec3 c=mix(H[4],H[3],pow(clamp(h,0.,1.),.5));
|
|
33
|
+
c=mix(c,H[4]*1.08,exp(-abs(h)*7.));
|
|
34
|
+
c+=X*(.2*pow(k,24.)+.05*pow(k,4.)+2.*smoothstep(.9993,.9997,k));
|
|
35
|
+
o=vec4(c,1);return;}
|
|
36
|
+
float L=C.z,s=step(0.,L),I=floor(C.w*.5),g=step(abs(L-${i.LAVA}.),.5)+step(L,-1.5);
|
|
37
|
+
vec4 t=texture(S,vec3(C.xy/${ut}.+vec2(g*T*.01,0),max(L,0.)));
|
|
38
|
+
vec3 c=mix(vec3(C.xy,C.w),t.rgb,s);
|
|
32
39
|
vec3 n=normalize(N),dx=dFdx(P),dy=dFdy(P),a=cross(dy,n),b=cross(n,dx);
|
|
33
40
|
float h=t.a*s*0.06,det=dot(dx,a);
|
|
34
|
-
float f=1.-smoothstep(.6,2.5,${128/
|
|
41
|
+
float f=1.-smoothstep(.6,2.5,${128/ut}.*max(length(dFdx(C.xy)),length(dFdy(C.xy))));
|
|
35
42
|
vec3 m=normalize(abs(det)*n-sign(det)*f*(dFdx(h)*a+dFdy(h)*b)+n*1e-9);
|
|
36
|
-
vec2 R=SP[int(s>0.?L+.5:${
|
|
37
|
-
vec3
|
|
43
|
+
vec2 R=SP[int(s>0.?L+.5:${at}.)];
|
|
44
|
+
vec3 y=mix(vec3(1),G[int(min(I,15.))]*(C.w-2.*I),s),V=normalize(E-P),w=normalize(n+m);
|
|
45
|
+
float sd=max(dot(m,O),0.)*smoothstep(0.,.1,dot(n,O)),sv=1.;
|
|
46
|
+
if(sd>0.&&g<1.){
|
|
47
|
+
vec4 q=W*vec4(P+n*.08,1);q.xyz=q.xyz*.5+.5;
|
|
48
|
+
float v=0.;
|
|
49
|
+
for(int j=0;j<4;j++)v+=texture(D,vec4(q.xy+(vec2(j&1,j>>1)*2.-1.)*${.75/Q},0,q.z-.0004));
|
|
50
|
+
sv=mix(v/4.,1.,smoothstep(.8,1.,max(abs(q.x-.5),abs(q.y-.5))*2.));}
|
|
51
|
+
vec3 l=y*(mix(mix(H[1],H[0],max(w.y,0.)),H[2],max(-w.y,0.))+X*sd*sv),sp=X*sv*sd*pow(max(dot(m,normalize(O+V)),0.),R.y);
|
|
38
52
|
for(int i=0;i<16;i++){
|
|
39
53
|
if(i>=Q||g>0.)break;
|
|
40
54
|
vec3 d=A[i].xyz-P;float r=length(d),k=K[i].w,u=B[i].w;
|
|
@@ -44,9 +58,9 @@ w*=w*smoothstep(u,u+(1.-u)*.4,-dot(d,B[i].xyz))*max(dot(m,d),0.);
|
|
|
44
58
|
if(w>0.&&k>=0.){
|
|
45
59
|
vec4 q=M[int(k)]*vec4(P+n*.03,1);q.xyz=q.xyz/q.w*.5+.5;
|
|
46
60
|
float v=0.;
|
|
47
|
-
for(int j=0;j<4;j++)v+=texture(D,vec4(q.xy+(vec2(j&1,j>>1)*2.-1.)*${.75/
|
|
61
|
+
for(int j=0;j<4;j++)v+=texture(D,vec4(q.xy+(vec2(j&1,j>>1)*2.-1.)*${.75/Q},k+1.,q.z-.0005));
|
|
48
62
|
w*=v/4.;}
|
|
49
63
|
l+=K[i].rgb*w;
|
|
50
64
|
sp+=K[i].rgb*w*pow(max(dot(m,normalize(d+V)),0.),R.y);}
|
|
51
65
|
c=c*mix(e(l),vec3(1.4),g)+e(sp*R.x*(.4+.6*mix(1.,t.a,s)));
|
|
52
|
-
o=vec4(mix(c,F.rgb,1.-exp(-F.a*length(P-E))),y>0.?C.w*(1.-smoothstep(.2,1.,length(C.xy))):1.);}`,Dr=(e,t,n,r,i=0,a=Math.tan(70*Math.PI/360),[o,s]=[.1,200])=>{let[c,l,u]=Ie(t,n,i),d=(s+o)/(s-o),f=[c.map(e=>e/(a*r)),l.map(e=>e/a),u.map(e=>e*d),u].map(t=>[...t,-t[0]*e[0]-t[1]*e[1]-t[2]*e[2]]);return f[2][3]-=2*s*o/(s-o),Float32Array.from({length:16},(e,t)=>f[t&3][t>>2])},Or=(e,t,n,r)=>{let i=e[3]*t+e[7]*n+e[11]*r+e[15];if(i<=0)return;let a=e[0]*t+e[4]*n+e[8]*r+e[12],o=e[1]*t+e[5]*n+e[9]*r+e[13];return[.5+a/(2*i),.5-o/(2*i)]},kr=({p:e,d:t,cone:n,range:r})=>Dr(e,Math.atan2(-t[0],-t[2]),Math.atan2(t[1],Math.hypot(t[0],t[2])),1,0,Math.min(Math.tan(Math.acos(n)),5.7),[.3,r]),Ar=e=>{let[t,n,r]=[0,0,0].map(()=>/* @__PURE__ */ new Float32Array(64)),i=/* @__PURE__ */ new Float32Array(64),a=0,o=e.slice(0,16);return o.forEach((e,o)=>{let s=e.shadow&&e.kind==Vn.SPOT&&a<4?a++:-1;t.set([...e.p,e.range],o*4),n.set([...e.d,e.kind==Vn.POINT?-9:e.cone],o*4),r.set([...e.color,s],o*4),s>=0&&i.set(kr(e),s*16)}),{n:o.length,maps:a,a:t,b:n,k:r,m:i}},jr=({map:e},{front:t,back:n})=>{let r=e.sectors[t];if(n<0)return[[r.floor,r.ceil]];let i=e.sectors[n];return[[r.floor,i.floor],[i.ceil,r.ceil]].filter(([e,t])=>t>e)},Mr=({map:e})=>[...new Map([[`1,1,1`,[1,1,1]],...e.sectors.flatMap(({tint:e})=>e?[[`${e}`,e]]:[])]).values()].slice(0,16),Nr=e=>{let t=[],n=Mr(e).map(String),r=({light:e=1,tint:t})=>e+2*Math.max(0,n.indexOf(`${t}`));for(let n of e.map.sectors){let{pts:e,floor:i,ceil:a,ft:o,ct:s}=n,c=r(n);for(let[n,r,l]of[[i,1,o],[a,-1,s]])for(let i=1;i<e.length-1;i++)for(let[a,o]of[e[0],e[i],e[i+1]])t.push(a,n,o,0,r,0,a,o,l,c)}for(let n of e.edges){let{ax:i,az:a,bx:o,bz:s,front:c}=n,{wt:l}=e.map.sectors[c],u=r(e.map.sectors[c]),d=Math.hypot(o-i,s-a),[f,p]=[(o-i)/d,(s-a)/d];for(let[r,c]of jr(e,n))for(let[e,n,d]of[[i,a,r],[o,s,r],[o,s,c],[i,a,r],[o,s,c],[i,a,c]])t.push(e,d,n,-p,0,f,e*f+n*p,d,l,u)}return t},Pr=2073600,Fr=(e,t,n)=>{let r=Math.min(n,2,Math.sqrt(Pr/(e*t||1)));return[Math.round(e*r),Math.round(t*r)]},Ir=(e,t)=>{let n=t,r=/* @__PURE__ */ new Map,i=performance.now()/1e3,a=e.getContext(`webgl2`,{antialias:!1,depth:!1});if(!a)throw Error(`WebGL2 is not available`);let o=document.createElement(`canvas`).getContext(`2d`),s=/* @__PURE__ */ new Map,c=e=>{let t=s.get(e);if(!t){o.fillStyle=`#888`,o.fillStyle=e;let n=parseInt(o.fillStyle.slice(1),16),r=n>=0?n:8947848;s.set(e,t=[r>>16,r>>8&255,r&255].map(e=>e/255))}return t},l=()=>Fr(e.clientWidth,e.clientHeight,devicePixelRatio),u=Nr(n),f=mr(),p=u.length*4,m=p+gr,[h,g,_,v,y]=[[.55,.55,.6],[.2,.2,.23],[.38,.38,.42],[.03,.03,.04],[.75,.5,.15]],b=e=>{let t=Ye(e),n=(e,n,r=Fe)=>Re(t[e],n,r);return{spun:[n(`barrels`,h),n(`clamps`,_)].flat(),still:[n(`body`,g),n(`trim`,_),n(`holes`,v,-1),n(`brass`,y)].flat()}},[x,S]=[b(10),b(5)],C=[],w=[];for(let e of[[.1,.01,.03],[.01,.1,.03],[.03,.03,.05]])W(C,[0,0,-Ge-.04],e,0,[1,.85,.4],-2),W(w,[0,0,-Ge-.12],e.map(e=>e*3),0,[1,.85,.4],-2);let T={},E,D,O,ee,k={},A,j,M,N,P=[0,0],F=[],I=!1,L=/* @__PURE__ */ new Float32Array(16),te=()=>{a.bufferData(a.ARRAY_BUFFER,m,a.DYNAMIC_DRAW),a.bufferSubData(a.ARRAY_BUFFER,0,new Float32Array(u))},R=(e,t,n)=>{let r=a.createProgram();for(let[n,i]of[[a.VERTEX_SHADER,e],[a.FRAGMENT_SHADER,t]]){let e=a.createShader(n);a.shaderSource(e,i),a.compileShader(e),a.attachShader(r,e)}return a.linkProgram(r),a.useProgram(r),[r,Object.fromEntries(n.map(e=>[e,a.getUniformLocation(r,e)]))]},ne=(e,t)=>{let n=a.createTexture();a.bindTexture(a.TEXTURE_2D,n),a.texStorage2D(a.TEXTURE_2D,1,A,e,t),a.texParameteri(a.TEXTURE_2D,a.TEXTURE_MIN_FILTER,a.LINEAR),a.texParameteri(a.TEXTURE_2D,a.TEXTURE_MAG_FILTER,a.LINEAR),a.texParameteri(a.TEXTURE_2D,a.TEXTURE_WRAP_S,a.CLAMP_TO_EDGE),a.texParameteri(a.TEXTURE_2D,a.TEXTURE_WRAP_T,a.CLAMP_TO_EDGE);let r=a.createFramebuffer();return a.bindFramebuffer(a.FRAMEBUFFER,r),a.framebufferTexture2D(a.FRAMEBUFFER,a.COLOR_ATTACHMENT0,a.TEXTURE_2D,n,0),F.push(()=>a.deleteTexture(n),()=>a.deleteFramebuffer(r)),{t:n,f:r,w:e,h:t}},re=(e,t)=>{F.splice(0).forEach(e=>e()),P=[e,t],M=ne(e,t),N=[1,2,3].map(n=>[0,1].map(()=>ne(e>>n||1,t>>n||1)));let n=n=>{let r=a.createRenderbuffer();return a.bindRenderbuffer(a.RENDERBUFFER,r),a.renderbufferStorageMultisample(a.RENDERBUFFER,xr,n,e,t),F.push(()=>a.deleteRenderbuffer(r)),r};a.bindFramebuffer(a.FRAMEBUFFER,j=a.createFramebuffer()),a.framebufferRenderbuffer(a.FRAMEBUFFER,a.COLOR_ATTACHMENT0,a.RENDERBUFFER,n(A)),a.framebufferRenderbuffer(a.FRAMEBUFFER,a.DEPTH_ATTACHMENT,a.RENDERBUFFER,n(a.DEPTH_COMPONENT24)),F.push(()=>a.deleteFramebuffer(j))},z=(e,t,n,r,...i)=>{a.bindFramebuffer(a.FRAMEBUFFER,e?.f??null),a.viewport(0,0,e?.w??P[0],e?.h??P[1]),i.forEach((e,t)=>{a.activeTexture(a.TEXTURE0+t),a.bindTexture(a.TEXTURE_2D,e.t)}),a.uniform4f(k.Y,n,r,t,performance.now()%1e4),a.drawArrays(a.TRIANGLES,0,3)},B=()=>{A=a.getExtension(`EXT_color_buffer_float`)?a.RGBA16F:a.RGBA8,F.length=0,P=[0,0],[ee,k]=R(Cr,wr,[`S`,`Y`]),a.uniform1iv(k.S,[0,1,2,3]),[O,T]=R(Sr,Er,[`m`,`E`,`T`,`Z`,`Q`,`A`,`B`,`K`,`M`,`D`,`F`,`G`]),a.uniform1i(T.D,1),a.bindBuffer(a.ARRAY_BUFFER,a.createBuffer()),te(),[3,3,4].reduce((e,t,n)=>(a.enableVertexAttribArray(n),a.vertexAttribPointer(n,t,a.FLOAT,!1,40,e*4),e+t),0),a.activeTexture(a.TEXTURE1),a.bindTexture(a.TEXTURE_2D_ARRAY,E=a.createTexture()),a.texStorage3D(a.TEXTURE_2D_ARRAY,1,a.DEPTH_COMPONENT24,vr,vr,4);for(let[e,t]of[[a.TEXTURE_MIN_FILTER,a.LINEAR],[a.TEXTURE_MAG_FILTER,a.LINEAR],[a.TEXTURE_WRAP_S,a.CLAMP_TO_EDGE],[a.TEXTURE_WRAP_T,a.CLAMP_TO_EDGE],[a.TEXTURE_COMPARE_MODE,a.COMPARE_REF_TO_TEXTURE]])a.texParameteri(a.TEXTURE_2D_ARRAY,e,t);a.bindFramebuffer(a.FRAMEBUFFER,D=a.createFramebuffer()),a.drawBuffers([a.NONE]),a.bindFramebuffer(a.FRAMEBUFFER,null),a.polygonOffset(2,4),a.activeTexture(a.TEXTURE0),a.bindTexture(a.TEXTURE_2D_ARRAY,a.createTexture()),a.texImage3D(a.TEXTURE_2D_ARRAY,0,a.RGBA8,128,128,cr,0,a.RGBA,a.UNSIGNED_BYTE,f),a.generateMipmap(a.TEXTURE_2D_ARRAY),a.texParameteri(a.TEXTURE_2D_ARRAY,a.TEXTURE_MIN_FILTER,a.LINEAR_MIPMAP_LINEAR);let e=a.getExtension(`EXT_texture_filter_anisotropic`);e&&a.texParameterf(a.TEXTURE_2D_ARRAY,e.TEXTURE_MAX_ANISOTROPY_EXT,8),a.enable(a.DEPTH_TEST),a.blendFunc(a.SRC_ALPHA,a.ONE_MINUS_SRC_ALPHA),V();let[t,n]=l();t&&n&&re(t,n)},V=()=>{let e=n.map.fog??hr,t=Mr(n);a.uniform4fv(T.F,e),a.uniform3fv(T.G,Float32Array.from({length:48},(e,n)=>t[n/3|0]?.[n%3]??1)),a.clearColor(e[0],e[1],e[2],1)};B();let ie=e=>{e.preventDefault(),I=!0},ae=()=>{B(),I=!1};return e.addEventListener(`webglcontextlost`,ie),e.addEventListener(`webglcontextrestored`,ae),{level(e){n=e,r.clear(),u=Nr(n),p=u.length*4,m=p+gr,I||(te(),V())},frame(t,o,s,f,h,g,_,v,b,k){let[A,F]=l();if(I||!A||!F)return;(e.width!=A||e.height!=F)&&(e.width=A,e.height=F),(P[0]!=A||P[1]!=F)&&re(A,F),L=Dr(t,o,s,A/F,g.roll);let R=[],ne=performance.now()/1e3,B=Math.min(.1,ne-i);i=ne;for(let e of f){let t=et(r,e.id,e.x,e.z,B),n=[S.spun,S.still,g.flashes.has(e.id)?w:[]];vt(R,e,c(h.get(e.id)??`#888`),t.walked,tt(t),n)}for(let e of _){let t=et(r,e.id,e.x,e.z,B);if(e.kind==d.FLYER)xt(R,e,ne,t.vx,t.vz);else{let n=g.swings.get(e.id);gt(R,e,t.walked,n?1-n/Xe:0,tt(t))}}mn(R,n,b,ne);for(let{p:e,yaw:t,half:n,c:r,life:i}of g.gibs){let a=Math.min(1,i*2);W(R,e,n.map(e=>e*a),t,r)}let V=R.length;for(let e of v)St(R,e,g.aim.get(e.id));let ie=Ie(o,s,g.roll),[ae,oe,se]=ie,ce=Qt(g,t,se);for(let e=0;e<ce.length;e+=4){let[t,n]=[Math.sqrt(ce[e+3]),.01+.025*ce[e+3]];W(R,ce.slice(e,e+3),[n,n,n],0,[t,t*.9,t*.7],-2)}for(let{p:e,v:t,c:n=[1,.8,.3]}of g.sparks){let[r,i]=[Math.hypot(...t),Math.max(Math.hypot(...t)*Ht,.04)/2],a=r?t.map(e=>e/r):[0,1,0];Ve(R,e.map((e,t)=>e-a[t]*i),Be(a),[i,.008,.008],n.map(e=>e*.25),-2,n)}for(let{p:e,yaw:t,tilt:n,size:r,life:i}of g.casings)if(i>0){let[a,o]=[r*Math.min(1,i*2),Math.cos(n)];Ve(R,e,Be([o*Math.sin(t),Math.sin(n),o*Math.cos(t)]),[a*2.8,a,a],y,Fe)}for(let{p:e,r:t,yaw:n,life:r}of g.splats){let i=t*Math.min(1,r);for(let t of[n,n+Math.PI/4])W(R,e,[i,.002,i*.8],t,Ut)}for(let e of g.blasts){let[t,n]=[Yt(e),e.t/Ft],r=[1,.9-n*.7,.5-n*.45];for(let n of[0,Math.PI/4])W(R,e.p,[t,t,t],n,r,-2)}let le=R.length,ue=e=>Math.hypot(e.p[0]-t[0],e.p[1]-t[1],e.p[2]-t[2]);for(let e of[...g.smoke].sort((e,t)=>ue(t)-ue(e))){let[t,n]=[Xt(e)*1.6,Zt(e)];for(let[r,i]of Le)R.push(...[0,1,2].map(n=>e.p[n]+(ae[n]*r+oe[n]*i)*t),...se.map(e=>-e),r,i,-3,n)}let de=R.length,fe=[.26,-.24+g.kick*.01,.6-g.kick*.06];g.dead||(He(R,x.still,t,ie,fe,.8),He(R,x.spun,t,ie,fe,.8,g.spin),g.flash>0&&He(R,C,t,ie,fe,.8,g.spin*3)),p+R.length*4>m&&(m=p+R.length*8,te()),a.bufferSubData(a.ARRAY_BUFFER,p,new Float32Array(R));let H=Ar(k),pe=u.length/10;if(H.maps){a.uniform1f(T.Z,-1),a.activeTexture(a.TEXTURE1),a.bindTexture(a.TEXTURE_2D_ARRAY,null),a.bindFramebuffer(a.FRAMEBUFFER,D),a.viewport(0,0,vr,vr),a.enable(a.POLYGON_OFFSET_FILL);for(let e=0;e<H.maps;e++)a.framebufferTextureLayer(a.FRAMEBUFFER,a.DEPTH_ATTACHMENT,E,0,e),a.uniformMatrix4fv(T.m,!1,H.m.subarray(e*16,e*16+16)),a.clear(a.DEPTH_BUFFER_BIT),a.drawArrays(a.TRIANGLES,0,pe+V/10);a.disable(a.POLYGON_OFFSET_FILL),a.bindTexture(a.TEXTURE_2D_ARRAY,E),a.activeTexture(a.TEXTURE0)}a.bindFramebuffer(a.FRAMEBUFFER,j),a.viewport(0,0,A,F),a.uniformMatrix4fv(T.m,!1,L),a.uniform3fv(T.E,t),a.uniform1f(T.T,performance.now()/1e3),a.uniform1f(T.Z,yr),a.uniform1i(T.Q,H.n),a.uniform4fv(T.A,H.a),a.uniform4fv(T.B,H.b),a.uniform4fv(T.K,H.k),a.uniformMatrix4fv(T.M,!1,H.m),a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT),a.drawArrays(a.TRIANGLES,0,pe+le/10),de>le&&(a.enable(a.BLEND),a.depthMask(!1),a.drawArrays(a.TRIANGLES,pe+le/10,(de-le)/10),a.depthMask(!0),a.disable(a.BLEND)),a.clear(a.DEPTH_BUFFER_BIT),a.uniform1f(T.Z,br),a.drawArrays(a.TRIANGLES,pe+de/10,(R.length-de)/10),a.bindFramebuffer(a.READ_FRAMEBUFFER,j),a.bindFramebuffer(a.DRAW_FRAMEBUFFER,M.f),a.blitFramebuffer(0,0,A,F,0,0,A,F,a.COLOR_BUFFER_BIT,a.NEAREST),a.useProgram(ee),z(N[0][0],0,1/A,1/F,M),N.forEach(([e,t],n)=>{z(t,1,1/t.w,0,N[n&&n-1][0]),z(e,1,0,1/e.h,t)}),z(void 0,2,0,0,M,...N.map(e=>e[0]));for(let e=3;e>0;e--)a.activeTexture(a.TEXTURE0+e),a.bindTexture(a.TEXTURE_2D,null);a.activeTexture(a.TEXTURE0),a.useProgram(O)},project:(e,t,n)=>Or(L,e,t,n),destroy(){e.removeEventListener(`webglcontextlost`,ie),e.removeEventListener(`webglcontextrestored`,ae),a.getExtension(`WEBGL_lose_context`)?.loseContext()}}},Lr=1/60,Rr=5,zr=(e,i)=>{let a=document.createElement(`div`),o=a.attachShadow({mode:`open`});o.innerHTML=`<style>:host{display:block;width:100%;height:100%}div{position:relative;width:100%;height:100%;overflow:hidden;background:#000;font:600 14px system-ui,sans-serif;color:#fff;text-shadow:0 1px 2px #000}canvas{display:block;width:100%;height:100%;cursor:pointer}i,p,b{position:absolute;pointer-events:none;user-select:none;margin:0}`+yn+`i{left:50%;top:50%;width:6px;height:6px;margin:-3px;border-radius:50%;background:#fff;box-shadow:0 0 0 1px #0008}p{top:8px;left:12px}b{transform:translate(-50%,-100%);font-size:12px;white-space:nowrap}[hidden]{display:none}</style><div><canvas></canvas><i></i><p></p></div>`,e.append(a);let s=o.querySelector(`canvas`),c=o.querySelector(`p`),l=o.querySelector(`div`),u=bn(l),d={roster:[],open:[],close:[]},f=e=>d[e].forEach(e=>e()),p=g(A[0]),m=Bn(p),h=_e();h.mute(xe()),u.mute(h.muted);let _=ye(h),v=be(h),y=Tn(s,()=>u.mute(Se(h)));s.addEventListener(`click`,()=>h.unlock());let x=en(Math.random,p),S=$t(),C=$t(),w=-1,T;try{T=Ir(s,p)}catch{}let E=sr({lvl:p,you:()=>w,roster:()=>D,predictor:m,rd:T,delays:[S,C],fx:x,hud:u,audio:h}),D=[],O=/* @__PURE__ */ new Map,ee=/* @__PURE__ */ new Map,k=/* @__PURE__ */ new Map,j=!1,M=T?``:`WebGL2 is not available`,N=!1,P=-1,F=!1,I=performance.now(),L=0,te=0,z=!1,V=i.socket??new WebSocket(i.url);V.binaryType=`arraybuffer`,V.onopen=()=>{j=!0,V.send(JSON.stringify({t:`join`,name:i.name})),f(`open`)},V.onclose=()=>{j=!1,M||=`Connection closed`,f(`close`)},V.onmessage=({data:e})=>{if(typeof e!=`string`){let t=ne(e);if(t){let e=performance.now();E.snapshot(t),m.snapshot(t,e);let n=t.players.find(e=>e.id==w),r=t.level*2+ +t.hard;n?.hp&&(r!=P||!F)&&([y.yaw,y.pitch]=[or(E.lvl.map,n.x,n.z),0]),[P,F]=[r,!!n?.hp],S.push(t.shots.filter(e=>e.id!=w),e),C.push(t.events,e)}return}let t=re(e);t?.t==`roster`?(D=t.players,O=new Map(D.map(e=>[e.id,e.color])),ee=new Map(D.map(e=>[e.id,e.name])),m.you(w=t.you),c.textContent=`Players: ${D.length}`,f(`roster`)):t?.t==`full`?M=`Room is full`:t?.t==`end`&&E.end(t.ticks,t.hard,t.stats)};let ie=e=>{let t=k.get(e);return t||(k.set(e,t=document.createElement(`b`)),l.append(t)),t},ae=()=>{te=requestAnimationFrame(ae);let e=performance.now(),i=I;if(L=Math.min(L+(e-I)/1e3,Rr*Lr),I=e,!N){let e=m.me(0);e&&([y.yaw,y.pitch,N]=[e.yaw,e.pitch,!0]),L=0}for(;N&&L>=Lr;L-=Lr){let t=E.dead?0:y.keys,r=m.step({keys:t,yaw:y.yaw,pitch:y.pitch},e);j&&V.send(R(r));let i=m.shot(),a=i&&m.me(1);i&&a&&(x.shot(i,[a.x,a.y+n,a.z],!0),h.play(B.SHOT))}let a=m.me(L/Lr),o=m.others(e).filter(e=>E.alive(e.id)),s=m.monsters(e),c=m.bullets(e),l=a?[a.x,a.y+n,a.z]:[0,0,0];x.track(s,c),h.listen(l,y.yaw);for(let t of S.due(e)){let e=o.find(e=>e.id==t.id);if(e){let r=[e.x,e.y+n,e.z];x.shot(t,r,!1,t.hit!=w),h.play(B.SHOT,r)}}for(let t of C.due(e))x.event(t,w),ve(h,t,w);_(E.returning,e);let d=Math.min((e-i)/1e3,.1);if(x.update(a,y.keys,d,E.dead),u.update(d),a&&T){let t=Math.max(-1.55,Math.min(r,y.pitch+x.shakePitch));T.frame([l[0],l[1]+x.bob,l[2]],y.yaw+x.shakeYaw,t,o,O,x,s,c,E.items,nr({eye:[l[0],l[1]+x.bob,l[2]],yaw:y.yaw,pitch:y.pitch,dead:E.dead,others:o,fx:x,bullets:c,lvl:E.lvl,t:e/1e3}))}let f=/* @__PURE__ */ new Set;for(let e of o){let n=[e.x,e.y+t+.3,e.z],r=a&&!b(E.lvl,l,n)?T?.project(...n):void 0,i=ie(e.id);f.add(e.id),i.hidden=!r,r&&(i.style.left=r[0]*100+`%`,i.style.top=r[1]*100+`%`);let o=ee.get(e.id)??``;i.textContent!=o&&(i.textContent=o)}for(let[e,t]of k)f.has(e)||(t.remove(),k.delete(e));u.fade(x.hurt/It,v(E.hp,e),x.gain/Lt,x.gainC),u.title(M||(j?N?y.locked()?``:`Click to play`:`Joining…`:`Connecting…`),E.name)};return te=requestAnimationFrame(ae),{on:(e,t)=>void d[e].push(t),players:()=>D,destroy(){z||(z=!0,cancelAnimationFrame(te),y.destroy(),h.destroy(),T?.destroy(),V.onopen=V.onclose=V.onmessage=null,V.close(),document.exitPointerLock(),a.remove())}}};export{zr as tinyFps};
|
|
66
|
+
o=vec4(mix(c,F.rgb,1.-exp(-F.a*length(P-E))),1.);}`,yt=(e,t,n,r,i=0,a=ft,[o,s]=[.1,200])=>{let[c,l,u]=Be(t,n,i),d=(s+o)/(s-o),f=[c.map(e=>e/(a*r)),l.map(e=>e/a),u.map(e=>e*d),u].map(t=>[...t,-t[0]*e[0]-t[1]*e[1]-t[2]*e[2]]);return f[2][3]-=2*s*o/(s-o),Float32Array.from({length:16},(e,t)=>f[t&3][t>>2])},bt=(e,t,n,r)=>{let i=e[3]*t+e[7]*n+e[11]*r+e[15];if(i<=0)return;let a=e[0]*t+e[4]*n+e[8]*r+e[12],o=e[1]*t+e[5]*n+e[9]*r+e[13];return[.5+a/(2*i),.5-o/(2*i)]},xt=({p:e,d:t,cone:n,range:r})=>yt(e,Math.atan2(-t[0],-t[2]),Math.atan2(t[1],Math.hypot(t[0],t[2])),1,0,Math.min(Math.tan(Math.acos(n)),5.7),[.3,r]),St=e=>{let[t,n,r]=[0,0,0].map(()=>/* @__PURE__ */ new Float32Array(64)),i=/* @__PURE__ */ new Float32Array(64),a=0,o=e.slice(0,16);return o.forEach((e,o)=>{let s=e.shadow&&e.kind==Re.SPOT&&a<4?a++:-1;t.set([...e.p,e.range],o*4),n.set([...e.d,e.kind==Re.POINT?-9:e.cone],o*4),r.set([...e.color,s],o*4),s>=0&&i.set(xt(e),s*16)}),{n:o.length,maps:a,a:t,b:n,k:r,m:i}},Ct=(e,t,n=64,r=100)=>{let i=e.map(e=>-e),a=Math.hypot(i[0],i[2]),o=a>.001?[-i[2]/a,0,i[0]/a]:[1,0,0],s=[o[1]*i[2]-o[2]*i[1],o[2]*i[0]-o[0]*i[2],o[0]*i[1]-o[1]*i[0]],c=(e,t)=>e[0]*t[0]+e[1]*t[1]+e[2]*t[2],[l,u]=[n/2,n/Q],d=[o,s].map(e=>[...e.map(e=>e/l),-(Math.round(c(e,t)/u)*u)/l]);return d.push([...i.map(e=>e/r),-c(i,t)/r],[0,0,0,1]),Float32Array.from({length:16},(e,t)=>d[t&3][t>>2])},wt=({def:e})=>[...new Map([[`1,1,1`,[1,1,1]],...e.brushes.flatMap(({tint:e})=>e?[[`${e}`,e]]:[])]).values()].slice(0,16),Tt=[[0,0],[1,0],[1,1],[0,0],[1,1],[0,1]],Et=e=>{let{brushes:t,floor:n}=e.def,r=[],i=wt(e).map(String);return t.forEach((e,a)=>{let o=1+2*Math.max(0,i.indexOf(`${e.tint}`));for(let i=0;i<3;i++){let[s,c]=[(i+1)%3,(i+2)%3],[l,u]=i==0?[2,1]:i==1?[0,2]:[0,1];for(let d of[0,1]){let f=d?e.max[i]:e.min[i];if(t.some((t,n)=>n!=a&&(d?t.min[i]:t.max[i])==f&&t.min[s]<=e.min[s]&&t.max[s]>=e.max[s]&&t.min[c]<=e.min[c]&&t.max[c]>=e.max[c])||i==1&&!d&&e.min[1]<=n)continue;let p=[0,0,0];p[i]=d?1:-1;for(let[t,n]of Tt){let a=[0,0,0];[a[i],a[s],a[c]]=[f,(t?e.max:e.min)[s],(n?e.max:e.min)[c]],r.push(...a,...p,a[l],a[u],i==1&&d?e.top:e.side,o)}}}}),r},Dt=({sky:[e,t]})=>[e.map(e=>(e*.55+.45)*.62),t.map(e=>(e*.7+.3)*.58),t.map((e,t)=>e*[1,.92,.8][t]*.34),e,t],Ot=({fog:e,sky:t})=>e?[...[0,1,2].map(n=>e[n]+(t[1][n]-e[n])*dt),e[3]]:[...t[1],0],kt=2073600,At=(e,t,n)=>{let r=Math.min(n,2,Math.sqrt(kt/(e*t||1)));return[Math.round(e*r),Math.round(t*r)]},jt=(e,t)=>{let n=/* @__PURE__ */ new Map,r=performance.now()/1e3,i=e.getContext(`webgl2`,{antialias:!1,depth:!1});if(!i)throw Error(`WebGL2 is not available`);let a=document.createElement(`canvas`).getContext(`2d`),o=/* @__PURE__ */ new Map,s=e=>{let t=o.get(e);if(!t){a.fillStyle=`#888`,a.fillStyle=e;let n=parseInt(a.fillStyle.slice(1),16),r=n>=0?n:8947848;o.set(e,t=[r>>16,r>>8&255,r&255].map(e=>e/255))}return t},c=()=>At(e.clientWidth,e.clientHeight,devicePixelRatio),l=Et(t),{sun:u,sky:d}=t.def,f=u.dir.map(e=>e/Math.hypot(...u.dir)),p=ct(),m=l.length*4,h=m+lt,g={},_,v,y,b,x={},S,C,w,T,E=[0,0],D=[],O=!1,k=/* @__PURE__ */ new Float32Array(16),A=()=>{i.bufferData(i.ARRAY_BUFFER,h,i.DYNAMIC_DRAW),i.bufferSubData(i.ARRAY_BUFFER,0,new Float32Array(l))},j=(e,t,n)=>{let r=i.createProgram();for(let[n,a]of[[i.VERTEX_SHADER,e],[i.FRAGMENT_SHADER,t]]){let e=i.createShader(n);i.shaderSource(e,a),i.compileShader(e),i.attachShader(r,e)}return i.linkProgram(r),i.useProgram(r),[r,Object.fromEntries(n.map(e=>[e,i.getUniformLocation(r,e)]))]},M=(e,t)=>{let n=i.createTexture();i.bindTexture(i.TEXTURE_2D,n),i.texStorage2D(i.TEXTURE_2D,1,S,e,t),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MIN_FILTER,i.LINEAR),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MAG_FILTER,i.LINEAR),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_S,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_T,i.CLAMP_TO_EDGE);let r=i.createFramebuffer();return i.bindFramebuffer(i.FRAMEBUFFER,r),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,n,0),D.push(()=>i.deleteTexture(n),()=>i.deleteFramebuffer(r)),{t:n,f:r,w:e,h:t}},N=(e,t)=>{D.splice(0).forEach(e=>e()),E=[e,t],w=M(e,t),T=[1,2,3].map(n=>[0,1].map(()=>M(e>>n||1,t>>n||1)));let n=n=>{let r=i.createRenderbuffer();return i.bindRenderbuffer(i.RENDERBUFFER,r),i.renderbufferStorageMultisample(i.RENDERBUFFER,pt,n,e,t),D.push(()=>i.deleteRenderbuffer(r)),r};i.bindFramebuffer(i.FRAMEBUFFER,C=i.createFramebuffer()),i.framebufferRenderbuffer(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.RENDERBUFFER,n(S)),i.framebufferRenderbuffer(i.FRAMEBUFFER,i.DEPTH_ATTACHMENT,i.RENDERBUFFER,n(i.DEPTH_COMPONENT24)),D.push(()=>i.deleteFramebuffer(C))},P=(e,t,n,r,...a)=>{i.bindFramebuffer(i.FRAMEBUFFER,e?.f??null),i.viewport(0,0,e?.w??E[0],e?.h??E[1]),a.forEach((e,t)=>{i.activeTexture(i.TEXTURE0+t),i.bindTexture(i.TEXTURE_2D,e.t)}),i.uniform4f(x.Y,n,r,t,performance.now()%1e4),i.drawArrays(i.TRIANGLES,0,3)},F=()=>{S=i.getExtension(`EXT_color_buffer_float`)?i.RGBA16F:i.RGBA8,D.length=0,E=[0,0],[b,x]=j(ht,gt,[`S`,`Y`]),i.uniform1iv(x.S,[0,1,2,3]),[y,g]=j(mt,vt,[`m`,`E`,`T`,`Z`,`Q`,`A`,`B`,`K`,`M`,`D`,`F`,`G`,`W`,`H`,`O`,`X`,`Y`,`J`]),i.uniform1i(g.D,1),i.bindBuffer(i.ARRAY_BUFFER,i.createBuffer()),A(),[3,3,4].reduce((e,t,n)=>(i.enableVertexAttribArray(n),i.vertexAttribPointer(n,t,i.FLOAT,!1,40,e*4),e+t),0),i.activeTexture(i.TEXTURE1),i.bindTexture(i.TEXTURE_2D_ARRAY,_=i.createTexture()),i.texStorage3D(i.TEXTURE_2D_ARRAY,1,i.DEPTH_COMPONENT24,Q,Q,5);for(let[e,t]of[[i.TEXTURE_MIN_FILTER,i.LINEAR],[i.TEXTURE_MAG_FILTER,i.LINEAR],[i.TEXTURE_WRAP_S,i.CLAMP_TO_EDGE],[i.TEXTURE_WRAP_T,i.CLAMP_TO_EDGE],[i.TEXTURE_COMPARE_MODE,i.COMPARE_REF_TO_TEXTURE]])i.texParameteri(i.TEXTURE_2D_ARRAY,e,t);i.bindFramebuffer(i.FRAMEBUFFER,v=i.createFramebuffer()),i.drawBuffers([i.NONE]),i.bindFramebuffer(i.FRAMEBUFFER,null),i.polygonOffset(2,4),i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D_ARRAY,i.createTexture()),i.texImage3D(i.TEXTURE_2D_ARRAY,0,i.RGBA8,128,128,at,0,i.RGBA,i.UNSIGNED_BYTE,p),i.generateMipmap(i.TEXTURE_2D_ARRAY),i.texParameteri(i.TEXTURE_2D_ARRAY,i.TEXTURE_MIN_FILTER,i.LINEAR_MIPMAP_LINEAR);let e=i.getExtension(`EXT_texture_filter_anisotropic`);e&&i.texParameterf(i.TEXTURE_2D_ARRAY,e.TEXTURE_MAX_ANISOTROPY_EXT,8),i.enable(i.DEPTH_TEST),i.depthFunc(i.LEQUAL),I();let[t,n]=c();t&&n&&N(t,n)},I=()=>{let e=wt(t);i.uniform4fv(g.F,Ot(t.def)),i.uniform3fv(g.H,Dt(t.def).flat()),i.uniform3fv(g.O,f),i.uniform3fv(g.X,u.color),i.uniform3fv(g.G,Float32Array.from({length:48},(t,n)=>e[n/3|0]?.[n%3]??1)),i.clearColor(...d[1],1)};F();let L=e=>{e.preventDefault(),O=!0},R=()=>{F(),O=!1};return e.addEventListener(`webglcontextlost`,L),e.addEventListener(`webglcontextrestored`,R),{frame(t,a,o,u,d,p,x){let[S,D]=c();if(O||!S||!D)return;(e.width!=S||e.height!=D)&&(e.width=S,e.height=D),(E[0]!=S||E[1]!=D)&&N(S,D),k=yt(t,a,o,S/D,p.roll);let j=[],M=performance.now()/1e3,F=Math.min(.1,M-r);r=M;for(let e of u){let t=Je(n,e.id,e.x,e.z,F);it(j,e,s(d.get(e.id)??`#888`),t.walked,Ye(t))}m+j.length*4>h&&(h=m+j.length*8,A()),i.bufferSubData(i.ARRAY_BUFFER,m,new Float32Array(j));let I=St(x),L=l.length/10,R=Ct(f,t);i.uniform1f(g.Z,-1),i.activeTexture(i.TEXTURE1),i.bindTexture(i.TEXTURE_2D_ARRAY,null),i.bindFramebuffer(i.FRAMEBUFFER,v),i.viewport(0,0,Q,Q),i.enable(i.POLYGON_OFFSET_FILL);for(let e=0;e<=I.maps;e++)i.framebufferTextureLayer(i.FRAMEBUFFER,i.DEPTH_ATTACHMENT,_,0,e),i.uniformMatrix4fv(g.m,!1,e?I.m.subarray(e*16-16,e*16):R),i.clear(i.DEPTH_BUFFER_BIT),i.drawArrays(i.TRIANGLES,0,L+j.length/10);i.disable(i.POLYGON_OFFSET_FILL),i.bindTexture(i.TEXTURE_2D_ARRAY,_),i.activeTexture(i.TEXTURE0),i.bindFramebuffer(i.FRAMEBUFFER,C),i.viewport(0,0,S,D),i.uniformMatrix4fv(g.m,!1,k),i.uniformMatrix4fv(g.W,!1,R),i.uniform3fv(g.E,t),i.uniform1f(g.T,performance.now()/1e3),i.uniform1f(g.Z,1),i.uniform1i(g.Q,I.n),i.uniform4fv(g.A,I.a),i.uniform4fv(g.B,I.b),i.uniform4fv(g.K,I.k),i.uniformMatrix4fv(g.M,!1,I.m),i.clear(i.COLOR_BUFFER_BIT|i.DEPTH_BUFFER_BIT);let[ee,te,ne]=Be(a,o,p.roll);i.uniform3fv(g.J,[ee.map(e=>e*ft*(S/D)),te.map(e=>e*ft),ne].flat()),i.uniform1f(g.Y,1),i.drawArrays(i.TRIANGLES,0,3),i.uniform1f(g.Y,0),i.drawArrays(i.TRIANGLES,0,L+j.length/10),i.bindFramebuffer(i.READ_FRAMEBUFFER,C),i.bindFramebuffer(i.DRAW_FRAMEBUFFER,w.f),i.blitFramebuffer(0,0,S,D,0,0,S,D,i.COLOR_BUFFER_BIT,i.NEAREST),i.useProgram(b),P(T[0][0],0,1/S,1/D,w),T.forEach(([e,t],n)=>{P(t,1,1/t.w,0,T[n&&n-1][0]),P(e,1,0,1/e.h,t)}),P(void 0,2,0,0,w,...T.map(e=>e[0]));for(let e=3;e>0;e--)i.activeTexture(i.TEXTURE0+e),i.bindTexture(i.TEXTURE_2D,null);i.activeTexture(i.TEXTURE0),i.useProgram(y)},project:(e,t,n)=>bt(k,e,t,n),destroy(){e.removeEventListener(`webglcontextlost`,L),e.removeEventListener(`webglcontextrestored`,R),i.getExtension(`WEBGL_lose_context`)?.loseContext()}}},$=1/60,Mt=5,Nt=(e,n)=>{let r=document.createElement(`div`),i=r.attachShadow({mode:`open`});i.innerHTML=`<style>:host{display:block;width:100%;height:100%}div{position:relative;width:100%;height:100%;overflow:hidden;background:#000;font:600 14px system-ui,sans-serif;color:#fff;text-shadow:0 1px 2px #000}canvas{display:block;width:100%;height:100%;cursor:pointer}i,p,b{position:absolute;pointer-events:none;user-select:none;margin:0}`+fe+`i{left:50%;top:50%;width:6px;height:6px;margin:-3px;border-radius:50%;background:#fff;box-shadow:0 0 0 1px #0008}p{top:8px;left:12px}b{transform:translate(-50%,-100%);font-size:12px;white-space:nowrap}[hidden]{display:none}</style><div><canvas></canvas><i></i><p></p></div>`,e.append(r);let a=i.querySelector(`canvas`),o=i.querySelector(`p`),s=i.querySelector(`div`),l=pe(s),u={roster:[],open:[],close:[]},d=e=>u[e].forEach(e=>e()),f=w(c),p=Le(f),m=ae();m.mute(oe()),l.mute(m.muted);let h=ve(a,()=>l.mute(se(m)));a.addEventListener(`click`,()=>m.unlock());let g=de(),b;try{b=jt(a,f)}catch{}let x=[],S=/* @__PURE__ */ new Map,C=/* @__PURE__ */ new Map,T=/* @__PURE__ */ new Map,E=!1,O=b?``:`WebGL2 is not available`,k=!1,A=performance.now(),j=0,M=0,N=!1,P=n.socket??new WebSocket(n.url);P.binaryType=`arraybuffer`,P.onopen=()=>{E=!0,P.send(JSON.stringify({t:`join`,name:n.name})),d(`open`)},P.onclose=()=>{E=!1,O||=`Connection closed`,d(`close`)},P.onmessage=({data:e})=>{if(typeof e!=`string`){let t=v(e);t&&p.snapshot(t,performance.now());return}let t=y(e);t?.t==`roster`?(x=t.players,S=new Map(x.map(e=>[e.id,e.color])),C=new Map(x.map(e=>[e.id,e.name])),p.you(t.you),o.textContent=`Players: ${x.length}`,d(`roster`)):t?.t==`full`&&(O=`Room is full`)};let F=e=>{let t=T.get(e);return t||(T.set(e,t=document.createElement(`b`)),s.append(t)),t},I=()=>{M=requestAnimationFrame(I);let e=performance.now(),n=A;if(j=Math.min(j+(e-A)/1e3,Mt*$),A=e,!k){let e=p.me(0);e&&([h.yaw,h.pitch,k]=[e.yaw,e.pitch,!0]),j=0}for(;k&&j>=$;j-=$){let t=p.step({keys:h.keys,yaw:h.yaw,pitch:h.pitch},e);E&&P.send(_(t))}let r=p.me(j/$),i=p.others(e),a=r?[r.x,r.y+1.6,r.z]:[0,0,0];m.listen(a,h.yaw);let o=Math.min((e-n)/1e3,.1);g.update(r,h.keys,o),r&&b&&b.frame([a[0],a[1]+g.bob,a[2]],h.yaw,h.pitch,i,S,g,ze({eye:[a[0],a[1]+g.bob,a[2]],yaw:h.yaw,pitch:h.pitch,others:i,t:e/1e3}));let s=/* @__PURE__ */ new Set;for(let e of i){let n=[e.x,e.y+t+.3,e.z],i=r&&!D(f,a,n)?b?.project(...n):void 0,o=F(e.id);s.add(e.id),o.hidden=!i,i&&(o.style.left=i[0]*100+`%`,o.style.top=i[1]*100+`%`);let c=C.get(e.id)??``;o.textContent!=c&&(o.textContent=c)}for(let[e,t]of T)s.has(e)||(t.remove(),T.delete(e));l.title(O||(E?k?h.locked()?``:`Click to play`:`Joining…`:`Connecting…`),f.def.name??``)};return M=requestAnimationFrame(I),{on:(e,t)=>void u[e].push(t),players:()=>x,destroy(){N||(N=!0,cancelAnimationFrame(M),h.destroy(),m.destroy(),b?.destroy(),P.onopen=P.onclose=P.onmessage=null,P.close(),document.exitPointerLock(),r.remove())}}};export{Nt as tinyFps};
|
package/dist/local.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e=65536/(2*Math.PI),t=t=>Math.round(t*e)&65535,n=e=>Math.max(-32768,Math.min(32767,Math.round(e))),r=e=>n(e*1e4),i=e=>e/1e4,a=t=>t/e,o=e=>{if(e.byteLength!==11)return null;let t=new DataView(e);return t.getUint8(0)===1?{seq:t.getUint16(1),keys:t.getUint8(3),yaw:a(t.getUint16(4)),pitch:i(t.getInt16(6)),view:(t.getUint16(8)*256+t.getUint8(10))/256}:null},s=e=>{let n=7+25*e.players.length,i=n+1+14*e.shots.length,a=i+1+16*e.monsters.length,o=a+1+13*e.bullets.length,s=o+1+14*e.events.length,c=new DataView(new ArrayBuffer(s+1+14*e.items.length));return c.setUint8(0,2),c.setUint16(1,e.tick&65535),c.setUint8(3,e.players.length),c.setUint8(4,e.level),c.setUint8(5,+e.hard|(e.phase&1)<<1),c.setUint8(6,e.keys),e.players.forEach((e,n)=>{let i=7+n*25;c.setUint8(i,e.id),c.setFloat32(i+1,e.x),c.setFloat32(i+5,e.y),c.setFloat32(i+9,e.z),c.setFloat32(i+13,e.vy),c.setUint16(i+17,t(e.yaw)),c.setInt16(i+19,r(e.pitch)),c.setUint16(i+21,e.ack&65535),c.setUint8(i+23,+e.ground|e.cool<<1),c.setUint8(i+24,e.hp)}),c.setUint8(n,e.shots.length),e.shots.forEach((e,t)=>{let r=n+1+t*14;c.setUint8(r,e.id),c.setFloat32(r+1,e.x),c.setFloat32(r+5,e.y),c.setFloat32(r+9,e.z),c.setUint8(r+13,e.hit&255)}),c.setUint8(i,e.monsters.length),e.monsters.forEach((e,n)=>{let r=i+1+n*16;c.setUint8(r,e.id),c.setUint8(r+1,e.kind&1|e.chase<<1),c.setFloat32(r+2,e.x),c.setFloat32(r+6,e.y),c.setFloat32(r+10,e.z),c.setUint16(r+14,t(e.yaw))}),c.setUint8(a,e.bullets.length),e.bullets.forEach((e,t)=>{let n=a+1+t*13;c.setUint8(n,e.id),c.setFloat32(n+1,e.x),c.setFloat32(n+5,e.y),c.setFloat32(n+9,e.z)}),c.setUint8(o,e.events.length),e.events.forEach((e,t)=>{let n=o+1+t*14;c.setUint8(n,e.type),c.setUint8(n+1,e.id),c.setFloat32(n+2,e.x),c.setFloat32(n+6,e.y),c.setFloat32(n+10,e.z)}),c.setUint8(s,e.items.length),e.items.forEach((e,t)=>{let n=s+1+t*14;c.setUint8(n,e.id),c.setUint8(n+1,e.kind),c.setFloat32(n+2,e.x),c.setFloat32(n+6,e.y),c.setFloat32(n+10,e.z)}),c.buffer},c=e=>{try{let t=JSON.parse(e);return t&&typeof t.t==`string`?t:null}catch{return null}},l=.4,u=1.8,d=1.6,f=1.55,p=.5,m=.5,h=.02,g=.4,_=1.8,v=2.5,y=.45,b=.9,x=1.2,S=3.5,ee=Math.cos(Math.PI/6),C=.15,te=1.3,ne=.6,w={maxPlayers:16},T={FORWARD:1,BACK:2,LEFT:4,RIGHT:8,JUMP:16,FIRE:32},E={WALKER:0,FLYER:1},D={DIE:0,HURT:1,BOOM:2,SWING:3},O={HEALTH:0,KEY_RED:1,KEY_BLUE:2,KEY_YELLOW:3},k={HUNT:0,RETURN:1},A=e=>{let t=(e,t,n,r)=>`${e},${t},${n},${r}`,n=/* @__PURE__ */ new Map;e.sectors.forEach(({pts:e},r)=>e.forEach(([i,a],o)=>n.set(t(i,a,...e[(o+1)%e.length]),r)));let r={map:e,edges:e.sectors.flatMap(({pts:e},r)=>e.map(([i,a],o)=>{let[s,c]=e[(o+1)%e.length];return{ax:i,az:a,bx:s,bz:c,front:r,back:n.get(t(s,c,i,a))??-1}})),barred:/* @__PURE__ */ new Set};return j(r,0,k.HUNT),r},j=({map:e,barred:t},n,r)=>{t.clear(),e.gates?.forEach(e=>n&1<<e.key||t.add(e.sector)),e.exit!==void 0&&r===k.HUNT&&t.add(e.exit)},M=({pts:e},t,n)=>e.every(([r,i],a)=>{let[o,s]=e[(a+1)%e.length];return(o-r)*(n-i)-(s-i)*(t-r)>=0}),N=({map:e},t,n)=>e.sectors.findIndex(e=>M(e,t,n)),P=({map:e,edges:t},[n,r,i],[a,o,s])=>{let[c,l]=[a-n,s-i];return t.some(t=>{let[a,s]=[t.bx-t.ax,t.bz-t.az],u=c*s-l*a;if(!u)return!1;let d=((t.ax-n)*s-(t.az-i)*a)/u,f=((t.ax-n)*l-(t.az-i)*c)/u;if(d<=0||d>=1||f<0||f>1)return!1;if(t.back<0)return!0;let[p,m]=[e.sectors[t.front],e.sectors[t.back]],h=r+d*(o-r);return h<Math.max(p.floor,m.floor)||h>Math.min(p.ceil,m.ceil)})},F=(e,...t)=>t.map(([t,n,r,i,a,o,s,c,l])=>[t,n,r,i,a,o,s,c,l,e]),I=(e,[t,n,r,i],a,o,s,c,l,u)=>Array.from({length:s},(d,f)=>{let[p,m]=e?[n,i]:[t,r],[h,g,_]=[p+f*(m-p)/s,p+(f+1)*(m-p)/s,+(a+f*o).toFixed(1)];return e?[t,h,r,g,_,c,l,u]:[h,n,g,i,_,c,l,u]}),[L,R,z,B]=[O.HEALTH,O.KEY_RED,O.KEY_BLUE,O.KEY_YELLOW],V=Math.PI/4,H=[1,.82,.6],re=[.6,.75,1],U=(e,t,[n,r,i],a,o,s,c,l)=>{let u=t.map(([e,t,n,r])=>[[e,t],[n,t],[n,r],[e,r]]),d=[...new Map(u.flat().map(e=>[`${e}`,e])).values()];return{name:e,sectors:t.map(([,,,,e,t,n=`103`,r,,i],a)=>({pts:u[a].flatMap((e,t,n)=>{let r=n[(t+1)%4],i=t=>(t[0]-e[0])*(r[0]-e[0])+(t[1]-e[1])*(r[1]-e[1]);return[e,...d.filter(t=>(r[0]-e[0])*(t[1]-e[1])===(r[1]-e[1])*(t[0]-e[0])&&i(t)>0&&i(t)<(r[0]-e[0])**2+(r[1]-e[1])**2).sort((e,t)=>i(e)-i(t))]}),floor:e,ceil:t,ft:+n[0],wt:+n[1],ct:+n[2],light:r,tint:i})),spawns:Array.from({length:16},(e,t)=>[n+t%4*2,r+(t>>2)*2,i*V]),monsters:[...a.map(([e,t,n])=>[e,t,n*V,E.WALKER]),...o.map(([e,t,n])=>[e,t,n*V,E.FLYER])],items:s,gates:t.flatMap(([,,,,,,,,e],t)=>e?[{sector:t,key:e}]:[]),goal:c,exit:0,fog:l}},ie=[U(`Warm-up`,[[0,0,10,10,0,4,`103`],[10,4,22,6,0,2.8,`333`,.6],...F([1,.85,.65],[22,0,38,14,0,6,`553`],...I(0,[38,4,42,10],.4,.4,4,6,`553`)),[42,2,52,12,2,6,`103`,.9],...I(1,[46,12,48,22],1.6,-.4,4,4.8,`744`,.7),[38,22,56,36,0,6,`113`]],[2,2,6],[[28,4,3],[34,10,4],[48,6,4],[44,30,0]],[[30,8,0],[52,28,2]],[[26,12,L],[50,10,L],[40,34,L]],[54,34]),U(`Foundry`,[...F([1,.9,.8],[0,14,10,26,0,4,`443`],[10,22,20,24,0,2.8,`744`,.6]),...F([1,.5,.25],[24,8,28,16,0,9,`744`],[20,16,28,30,0,9,`443`],...I(1,[20,8,24,16],2,-.4,5,9,`744`),[28,8,42,18,-1.6,9,`233`,.9],...I(1,[28,18,42,21],-1.2,.4,3,9,`333`),[28,21,50,30,0,9,`744`],[42,8,50,21,0,9,`744`],[20,0,50,8,2.4,9,`744`,.8]),[50,14,51,17,0,3.5,`444`,1,R],...F([.85,.65,.5],[51,8,59,26,0,7,`443`,.6],[59,8,67,26,.4,7,`143`,.8],[57,26,59,36,0,4.8,`744`,.7]),...F([1,.6,.45],[50,36,68,50,0,7,`443`])],[2,16,6],[[46,14,2],[36,26,0],[38,4,2],[56,12,4],[62,20,2],[60,44,0]],[[35,13,0],[60,14,4],[58,42,6]],[[24,26,L],[24,4,L],[64,10,L],[52,46,L],[46,4,R]],[66,48],[.07,.05,.04,.026]),U(`Cistern`,[...F([.45,.9,1],[0,20,10,32,0,4,`363`],[10,26,20,28,0,2.8,`363`,.6],[20,14,56,26,0,11,`363`,.9],[20,26,34,32,0,11,`363`,.9]),...F([.9,.6,.45],[34,26,42,32,-.4,11,`263`]),...F([.45,.9,1],[42,26,56,32,0,11,`363`,.9],[20,32,56,44,0,11,`363`,.9],[36,44,38,54,0,1.9,`000`,.35],[30,54,46,68,0,5,`003`,.7]),[56,28,57,31,0,4,`444`,1,R],...F([.55,.85,1],...I(0,[57,26,65,32],.4,.4,8,10,`333`,.8)),...F([.7,1,.85],[65,22,81,36,3.6,11,`563`]),[71,36,74,37,3.6,7,`444`,1,z],...F([.45,.9,1],[71,37,74,48,3.6,6.4,`363`,.6]),...F([.9,.6,.45],[62,48,82,64,3.6,10,`263`])],[2,22,6],[[30,18,4],[50,20,4],[30,40,0],[50,40,0],[36,60,2],[42,64,0],[76,28,2],[70,58,0]],[[26,24,0],[48,22,0],[36,40,2],[72,56,4]],[[24,40,L],[52,42,L],[44,57,L],[68,34,L],[64,62,L],[38,66,R],[79,24,z]],[80,62],[.04,.11,.1,.034]),U(`Loop`,[...F(H,[0,44,10,56,0,4,`153`],[10,49,20,51,0,2.8,`553`,.6]),[20,30,52,40,0,9,`113`],[20,48,52,58,0,9,`113`,.9],[20,40,32,48,0,9,`113`],[40,40,52,48,0,9,`113`],[20,22,49,30,3.2,9,`743`,.8],...F(H,[34,58,36,66,0,2.8,`553`,.4],[26,66,46,84,0,5,`353`,.5]),[52,42,53,45,0,4,`444`,1,R],...F(H,[53,36,64,54,0,6,`153`,.8],[64,36,70,54,.4,6,`253`,.8]),[60,35,63,36,0,4,`444`,1,z],...F(H,[57,12,77,35,0,8,`053`,.9]),[56,24,57,27,0,4,`444`,1,B],...I(0,[49,22,56,30],2.8,-.4,7,9,`744`,.8),...F(H,[12,24,20,28,3.2,6,`744`,.6],[0,10,12,34,3.2,9,`553`])],[2,46,6],[[30,36,4],[44,36,2],[40,52,0],[40,26,4],[30,74,6],[42,78,2],[56,40,6],[64,50,2],[62,18,4],[72,28,2],[6,22,0]],[[28,48,0],[46,44,4],[36,72,0],[60,45,6],[68,22,4]],[[24,54,L],[24,26,L],[44,80,L],[68,38,L],[74,14,L],[36,82,R],[68,52,z],[74,32,B]],[6,13],[.06,.045,.04,.02]),U(`Citadel`,[...F(re,[0,60,10,72,0,4,`303`],[10,65,24,67,0,2.8,`363`,.6],[24,48,56,84,0,12,`363`],[27,40,31,48,0,3.4,`363`,.4]),...F([.85,.7,.65],[24,16,34,40,0,12,`443`,.9],...I(0,[34,16,40,26],-.4,-.4,3,12,`243`),[34,26,40,30,0,12,`744`],...I(0,[34,30,40,40],-.4,-.4,3,12,`243`),[40,16,60,40,0,12,`443`,.9]),[60,27,61,30,0,4,`444`,1,B],...F([.55,.3,1],[61,10,101,16,0,14,`443`,1],[61,40,101,46,0,14,`443`,1],[61,16,69,40,0,14,`443`,1],[93,16,101,40,0,14,`443`,1],[69,16,93,20,.4,14,`243`,1],[69,36,93,40,.4,14,`243`,1],[69,20,73,36,.4,14,`243`,1],[89,20,93,36,.4,14,`243`,1],[73,20,89,36,.8,14,`744`,1]),[101,26,111,28,0,3.2,`744`,.6],[111,16,131,40,0,8,`163`],[56,66,57,69,0,4,`444`,1,R],...F([1,.5,.45],[57,56,75,80,0,12,`443`,.7],...I(0,[75,60,85,72],.4,.4,5,12,`744`,.8),[85,52,101,80,2.4,12,`543`]),[38,84,42,85,0,4,`444`,1,z],...F([.6,.85,.85],[38,85,42,95,0,1.9,`363`,.35],[28,95,56,115,0,6,`663`,.6])],[2,62,6],[[34,56,4],[50,56,2],[36,76,0],[46,20,4],[54,34,2],[66,60,2],[66,76,0],[92,60,2],[34,100,4],[50,108,2],[70,13,4],[92,13,2],[70,43,0],[92,43,6]],[[30,70,0],[46,68,0],[50,28,0],[76,26,0],[86,30,0],[81,22,0]],[[30,80,L],[56,36,L],[60,78,L],[50,112,L],[64,13,L],[98,43,L],[114,38,L],[52,22,R],[95,66,z],[42,110,B]],[127,36],[.045,.05,.075,.02])],W=1/60,G=Math.PI*2,ae=1e-6,K=.01,q=e=>(e%G+G)%G,J=e=>[-Math.sin(e),-Math.cos(e)],Y=Math.fround,oe=(e,t,n)=>Math.min(Math.max(e,t),n),se=({map:e,barred:t},n,r,i,a)=>{if(n.back<0||t.has(n.back))return!0;let o=e.sectors[n.front],s=e.sectors[n.back],c=Math.min(o.ceil,s.ceil);return s.floor>r+a+K||c-Math.max(o.floor,s.floor)<i-K||c<r+i-K},ce=(e,t,n,r,i)=>{let a=(n-ae)**2;for(let o=0;o<4;o++){let o=0,s=0,c=0;for(let l of t.edges){let u=l.bx-l.ax,d=l.bz-l.az,f=e.x-l.ax,p=e.z-l.az;if(u*p-d*f<0)continue;let m=oe((f*u+p*d)/(u*u+d*d),0,1),h=f-m*u,g=p-m*d,_=h*h+g*g;if(_>=a)continue;let v=Math.sqrt(_);if(n-v<=o||!se(t,l,e.y,r,i))continue;o=n-v;let y=v||Math.hypot(u,d);s=(v?h:-d)/y*o,c=(v?g:u)/y*o}if(!o)return;e.x+=s,e.z+=c}},le=(e,t,n,r,i,a,o)=>{let{map:s,edges:c}=e,l=Math.max(N(e,t,n),0),{floor:u,ceil:d}=s.sectors[l],f=(i-ae)**2;for(let i of c){if(i.front!==l||i.back<0)continue;let c=i.bx-i.ax,p=i.bz-i.az,m=t-i.ax,h=n-i.az,g=oe((m*c+h*p)/(c*c+p*p),0,1);if((m-g*c)**2+(h-g*p)**2<f&&!se(e,i,r,a,o)){let e=s.sectors[i.back];u=Math.max(u,e.floor),d=Math.min(d,e.ceil)}}return[u,d]},X=(e,t,n,r,i)=>{e.x+=t,e.z+=n,ce(e,i,l,u,p);let[a,o]=le(i,e.x,e.z,e.y,l,u,p);e.ground&&r&&(e.vy=7);let s=e.vy-20*W;e.y+=(e.vy+s)/2*W,e.vy=s,e.ground=!1,e.y+1.8>o&&(e.y=o-u,e.vy=Math.min(e.vy,0)),(e.vy<=0?e.y<=a+K:e.y<a)&&(e.y=a,e.vy<=0&&(e.vy=0,e.ground=!0)),e.x=Y(e.x),e.y=Y(e.y),e.z=Y(e.z),e.vy=Y(e.vy)},ue=(e,t,n,r,i)=>{e.x+=t,e.z+=r,ce(e,i,y,b,0);let[a,o]=le(i,e.x,e.z,e.y,y,b,0),s=Math.min(a+x,o-b),c=Math.max(a,Math.min(s,e.y+S*W));e.y=Math.min(Math.max(e.y+n,c),o-b),e.x=Y(e.x),e.y=Y(e.y),e.z=Y(e.z)},de=(e,t,n)=>{let r=(t.keys&T.FIRE)!==0;e.cool=Math.max(0,e.cool-1),e.yaw=q(t.yaw),e.pitch=Math.max(-f,Math.min(f,t.pitch));let i=(t.keys&T.FORWARD?1:0)-(t.keys&T.BACK?1:0),a=(t.keys&T.RIGHT?1:0)-(t.keys&T.LEFT?1:0),o=6*W*(r?m:1)/(Math.hypot(i,a)||1),[s,c]=J(e.yaw);return X(e,(s*i-c*a)*o,(c*i+s*a)*o,(t.keys&T.JUMP)!==0,n),r&&!e.cool?(e.cool=5,!0):!1},fe=(e,t,n)=>{let{spawns:r,sectors:i}=t.map,[a,o,s]=r[(n%r.length+r.length)%r.length],c=i[Math.max(N(t,a,o),0)].floor;return{id:e,x:Y(a),y:Y(c),z:Y(o),vy:0,yaw:q(s),pitch:0,ground:!0,cool:0}},pe=e=>{for(let t=0;t<2;t++)e=Math.imul(e^e>>>16,73244475);return(e^e>>>16)>>>0},me=(e,t,n)=>{let[r,i,a,o]=[Math.sin(e),Math.cos(e),Math.sin(t),Math.cos(t)],s=pe(n+1),c=(s&65535)/65536*G,l=(s>>>16)/65535,[u,d]=[Math.cos(c)*l*h,Math.sin(c)*l*h],f=[-r*o+i*u+r*a*d,a+o*d,-i*o-r*u+i*a*d],p=Math.hypot(...f);return[f[0]/p,f[1]/p,f[2]/p]},he=(e,t,n,r)=>{let{map:i,edges:a}=e,o=Math.max(N(e,t[0],t[2]),0),s=0;for(let e=0;e<=i.sectors.length;e++){let{floor:e,ceil:c}=i.sectors[o],l=1/0,u;for(let e of a){if(e.front!==o)continue;let[r,i]=[e.bx-e.ax,e.bz-e.az],a=n[2]*r-n[0]*i;if(a>=0)continue;let c=Math.max(((t[2]-e.az)*r-(t[0]-e.ax)*i)/-a,s),d=((t[0]+c*n[0]-e.ax)*r+(t[2]+c*n[2]-e.az)*i)/(r*r+i*i);d>-1e-9&&d<1+1e-9&&(c<l-1e-9||c<l+1e-9&&e.back<0)&&([l,u]=[c,e])}let d=n[1]<0?(e-t[1])/n[1]:n[1]>0?(c-t[1])/n[1]:1/0;if(d<=l||!u)return Math.min(Math.max(d,s),r);if(l>=r)return r;let f=t[1]+l*n[1],p=u.back<0?void 0:i.sectors[u.back];if(!p||f<Math.max(e,p.floor)||f>Math.min(c,p.ceil))return l;[o,s]=[u.back,l]}return s},ge=(e,t,n)=>{let[r,i]=[e[0]-n.x,e[2]-n.z],a=t[0]*t[0]+t[2]*t[2],o=r*t[0]+i*t[2],s=r*r+i*i-n.r*n.r,[c,l]=[0,1/0];if(a>1e-12){let e=o*o-a*s;if(e<0)return 1/0;[c,l]=[Math.max(0,(-o-Math.sqrt(e))/a),(-o+Math.sqrt(e))/a]}else if(s>0)return 1/0;let[u,d]=[n.y-e[1],n.y+n.h-e[1]];if(t[1]){let[e,n]=[u/t[1],d/t[1]];c=Math.max(c,Math.min(e,n)),l=Math.min(l,Math.max(e,n))}else if(u>0||d<0)return 1/0;return c<=l?c:1/0},_e=({id:e,x:t,y:n,z:r})=>({id:e,x:t,y:n,z:r,r:l,h:u}),Z=({id:e,kind:t,x:n,y:r,z:i})=>{let a=t===E.WALKER;return{id:e,x:n,y:r,z:i,r:a?g:y,h:a?_:b}},ve=(e,t,n,r)=>{let i=[t.x,t.y+d,t.z],a=me(t.yaw,t.pitch,n),o=he(e,i,a,100),s=-1;for(let e of r){let n=e.id===t.id?1/0:ge(i,a,e);n<o&&([o,s]=[n,e.id])}return{id:t.id,x:i[0]+a[0]*o,y:i[1]+a[1]*o,z:i[2]+a[2]*o,hit:s}},Q=1/60,ye=.05,be=.4,$=1,xe=15,Se=1.2,Ce=255,we=e=>{let t=e|0;return()=>{t=t+1831565813|0;let e=Math.imul(t^t>>>15,1|t);return e=e+Math.imul(e^e>>>7,61|e)^e,((e^e>>>14)>>>0)/4294967296}},Te=(e,t,n=!1)=>{let r=(e.map.monsters??[]).slice(0,64),[i,a]=n?[te,ne]:[1,1],[o,s]=[v*i,S*i],[c,f]=[90,45].map(e=>Math.round(e*a)),m=[],h=[],g=0,_=0,y=r.map(([n,r,i,a],o)=>{let{floor:s,ceil:l}=e.map.sectors[Math.max(N(e,n,r),0)],u=a===E.FLYER;return{id:16+o,kind:a,x:Math.fround(n),y:Math.fround(u?Math.min(s+x,l-b):s),z:Math.fround(r),yaw:q(i),chase:!1,vy:0,ground:!0,target:-1,turn:Math.floor(t()*240),melee:0,swing:0,shoot:Math.floor(t()*c)}}),w=e=>e.y+Z(e).h/2,T=e=>[e.x,e.kind===E.WALKER?e.y+d:w(e),e.z],O=e=>[e.x,e.y+d,e.z],k=(e,t)=>{e.target=t,e.chase=t>=0},A=(t,n)=>{for(let r of y)r!==t&&!r.chase&&Math.hypot(r.x-t.x,r.z-t.z)<12&&!P(e,T(t),T(r))&&k(r,n)},j=(t,n)=>{let[r,i]=J(t.yaw),a,o=1/0;for(let s of n){let[n,c]=[s.x-t.x,s.z-t.z],l=Math.hypot(n,c);l<=30&&l<o&&n*r+c*i>=ee*l&&!P(e,T(t),O(s))&&([a,o]=[s,l])}if(a){let e=!t.chase;k(t,a.id),e&&A(t,a.id)}},M=(e,t)=>{let n=((t-e.yaw+Math.PI)%G+G)%G-Math.PI;e.yaw=q(e.yaw+Math.max(-.05,Math.min(ye,n)))},F=(t,n)=>e.map.sectors[Math.max(N(e,t.x,t.z),0)].floor<n-p,I=n=>{let r=(n.kind===E.WALKER?o:s)*be*Q,[i,a]=J(n.yaw),c={x:n.x,y:n.y,z:n.z,vy:n.vy,ground:n.ground};n.kind===E.WALKER?X(n,i*r,a*r,!1,e):ue(n,i*r,-r,a*r,e);let l=n.kind===E.WALKER&&F(n,c.y);l||Math.hypot(n.x-c.x,n.z-c.z)<r/2?(l&&Object.assign(n,c),n.yaw=q(n.yaw+t()*G)):--n.turn<=0&&(n.turn=120+Math.floor(t()*240),n.yaw=q(n.yaw+(t()-.5)*Math.PI))},L=(n,r)=>{let[i,a,o]=T(n);if(m.length>=Ce||P(e,[i,a,o],O(r))){n.shoot=0;return}let s=[r.x-i,r.y+Se-a,r.z-o],l=12*Q/Math.hypot(...s);m.push({id:_++&255,x:i,y:a,z:o,vx:s[0]*l,vy:s[1]*l,vz:s[2]*l,life:300}),n.shoot=c+Math.floor((t()-.5)*20)},R=(t,n)=>{let[r,i]=[n.x-t.x,n.z-t.z],a=Math.hypot(r,i);if(a>1e-6&&M(t,Math.atan2(-r,-i)),t.kind===E.WALKER){let[n,r]=J(t.yaw),i=a>$?o*Q:0;X(t,n*i,r*i,!1,e)}else{let o=n.y+Se-b/2-t.y,c=Math.min(1,s*Q/(Math.hypot(a,o)||1));ue(t,a>$?r*c:0,o*c,a>$?i*c:0,e)}let c=Math.hypot(n.x-t.x,n.z-t.z)<=1.4&&t.y<n.y+1.8&&n.y<t.y+Z(t).h;if(t.melee=Math.max(0,t.melee-1),t.swing&&!--t.swing&&c){let[e,r]=J(t.yaw);(n.x-t.x)*e+(n.z-t.z)*r>0&&h.push({type:D.HURT,id:n.id,x:n.x,y:n.y+Se,z:n.z})}c&&!t.melee&&(h.push({type:D.SWING,id:t.id,x:t.x,y:w(t),z:t.z}),[t.melee,t.swing]=[f,xe]),t.kind===E.WALKER&&t.shoot--<=0&&a<=30&&L(t,n)},z=(t,n)=>{let r=Math.hypot(t.vx,t.vy,t.vz),i=[t.x,t.y,t.z],a=[t.vx/r,t.vy/r,t.vz/r],o=he(e,i,a,r),s=-1;for(let e of n){let t=ge(i,a,{id:e.id,x:e.x,y:e.y-C,z:e.z,r:l+C,h:u+2*C});t<o&&([o,s]=[t,e.id])}return[t.x,t.y,t.z]=[i[0]+a[0]*o,i[1]+a[1]*o,i[2]+a[2]*o],s>=0?h.push({type:D.HURT,id:s,x:t.x,y:t.y,z:t.z}):o<r&&h.push({type:D.BOOM,id:t.id,x:t.x,y:t.y,z:t.z}),s<0&&o>=r&&--t.life>0};return{monsters:()=>y.map(({id:e,kind:t,x:n,y:r,z:i,yaw:a,chase:o})=>({id:e,kind:t,x:n,y:r,z:i,yaw:a,chase:o})),bullets:()=>m.map(({id:e,x:t,y:n,z:r})=>({id:e,x:t,y:n,z:r})),events:h,step:e=>{g++;for(let t of y){(g+t.id)%60==0&&j(t,e);let n=e.find(e=>e.id===t.target);t.chase&&!n&&k(t,-1),n?R(t,n):I(t)}m=m.filter(t=>z(t,e))},kill:(e,t)=>{let n=y.findIndex(t=>t.id===e);if(n<0)return;let[r]=y.splice(n,1);return h.push({type:D.DIE,id:e,x:r.x,y:w(r),z:r.z}),A(r,t),r}}},Ee=20,De=.5,Oe=2,ke=[`#e6194b`,`#3cb44b`,`#ffe119`,`#4363d8`,`#f58231`,`#911eb4`,`#42d4f4`,`#f032e6`,`#bfef45`,`#fabed4`,`#469990`,`#dcbeff`,`#9a6324`,`#800000`,`#aaffc3`,`#808000`],Ae=(e,t)=>Array.from(e).slice(0,t).join(``).trim(),je=e=>Ae((typeof e==`string`?e:``).replace(/\s+/g,` `).replace(/\p{Cc}/gu,``).trim(),16),Me=(e,t)=>{let n=e;for(let r=2;t.includes(n);r++)n=`${Ae(e,15-`${r}`.length)} ${r}`;return n},Ne=(e,t,n,r,i)=>Math.hypot(e.x-t,e.z-r)<=i&&e.y<n+De&&n<e.y+1.8,Pe=(e,t)=>{let n=setInterval(e,t);return()=>clearInterval(n)},Fe=class{peers=/* @__PURE__ */ new Map;ticks=0;history=[];maps;lvl;horde;rand;level=0;hard=!1;phase=k.HUNT;keys=0;items=[];nextItem=0;kills=0;runStart=0;shots=[];halt;rules;rr=0;constructor(e={},t=Date.now(),n=ie){this.rules={...w,...e},this.rand=we(t),this.maps=n.map(A),this.load(0)}get size(){return this.players().length}connect(e){this.peers.set(e,null)}receive(e,t){let n=this.peers.get(e);if(typeof t==`string`){let r=c(t);n===null&&r?.t===`join`&&this.join(e,r.name);return}let r=n&&o(t);if(r&&n.bank>0){if(n.bank--,n.hp&&de(n.state,r,this.lvl)){let e=ve(this.lvl,n.state,r.seq,this.rewind(n,r.view));if(this.shots.push(e),e.hit>=16){let t=this.horde.kill(e.hit,n.state.id);t&&n.kills++,t&&++this.kills%4==0&&this.drop(O.HEALTH,t.x,t.z)}}n.ack=r.seq}}disconnect(e){let t=this.peers.get(e);this.peers.delete(e),t&&this.roster()}tick(){let e=this.players();if(!e.length)return;let t=++this.ticks;for(let n of e)n.bank=Math.min(10,n.bank+1),!n.hp&&t-n.hurt>=180&&(n.hp=9,n.state=this.place(n.info.id,e.filter(e=>e.hp&&e!==n).map(e=>e.state)));let{events:n}=this.horde,r=n.length;if(this.horde.step(e.filter(e=>e.hp).map(e=>e.state)),n.push(...n.splice(r).filter(e=>e.type!==D.HURT||this.wound(e.id,t))),this.progress(e.filter(e=>e.hp)),t%2==0){let n=this.horde.monsters();for(this.history.push({tick:t,players:e.map(e=>({...e.state})),monsters:n});this.history[0].tick<t-30;)this.history.shift();let r=s({tick:t,players:e.map(e=>({...e.state,ack:e.ack,hp:e.hp})),shots:this.shots,monsters:n,bullets:this.horde.bullets(),events:this.horde.events.splice(0,255),level:this.level,hard:this.hard,phase:this.phase,keys:this.keys,items:this.items});this.shots=[],e.forEach(e=>e.conn.send(r))}}start(e=Pe){if(this.halt)return;let t=1e3/60,n=performance.now(),r=0;this.halt=e(()=>{let e=performance.now();r+=e-n,n=e;for(let e=0;r>=t&&e<Ee;e++)this.tick(),r-=t;r%=t},t)}stop(){this.halt?.(),this.halt=void 0}rewind(e,t=0){let n=this.players().filter(t=>t!==e&&t.hp).map(e=>e.state),r=this.horde.monsters(),[i,a]=[this.history[0],this.history.at(-1)];if(!i||!a)return[...n.map(_e),...r.map(Z)];let o=((this.ticks-t+32768)%65536+65536)%65536-32768,s=Math.min(Math.max(this.ticks-o,i.tick),a.tick),c=0;for(;c+1<this.history.length&&this.history[c+1].tick<=s;)c++;let[l,u]=[this.history[c],this.history[c+1]??this.history[c]],d=u.tick>l.tick?(s-l.tick)/(u.tick-l.tick):0,f=(e,t)=>e+(t-e)*d,p=(e,t)=>e.map(e=>{let n=t(l).find(t=>t.id===e.id),r=t(u).find(t=>t.id===e.id);return n&&r?{...r,x:f(n.x,r.x),y:f(n.y,r.y),z:f(n.z,r.z)}:r??n??e});return[...p(n,e=>e.players).map(_e),...p(r,e=>e.monsters).map(Z)]}load(e){this.level=e,this.lvl=this.maps[e],this.keys=0,this.phase=k.HUNT,j(this.lvl,0,k.HUNT),this.horde=Te(this.lvl,this.rand,this.hard),this.items=[],this.nextItem=0,this.kills=0,this.lvl.map.items?.forEach(([e,t,n])=>this.drop(n,e,t)),this.shots=[],this.history=[];let t=[];for(let e of this.players())e.hp=9,e.state=this.place(e.info.id,t),t.push(e.state)}place(e,t){let n=this.lvl.map.spawns.findIndex((n,r)=>{let{x:i,z:a}=fe(e,this.lvl,r);return!t.some(e=>Math.hypot(e.x-i,e.z-a)<Oe)});return fe(e,this.lvl,n<0?this.rr++:n)}floor(e,t){return this.lvl.map.sectors[Math.max(N(this.lvl,e,t),0)].floor}drop(e,t,n){let r=Math.fround;this.items.push({id:this.nextItem++,kind:e,x:r(t),y:r(this.floor(t,n)),z:r(n)})}wound(e,t){let n=this.players().find(t=>t.info.id===e);return!n?.hp||t-n.hurt<60?!1:([n.hp,n.hurt]=[n.hp-1,t],n.deaths+=+!n.hp,!0)}progress(e){let{sectors:t,goal:n,exit:r}=this.lvl.map,i=!1;if(this.items=this.items.filter(t=>{let n=e.find(e=>Ne(e.state,t.x,t.y,t.z,.8)&&(t.kind!==O.HEALTH||e.hp<9));return n&&t.kind===O.HEALTH?n.hp=Math.min(9,n.hp+3):n&&(this.keys|=1<<t.kind,i=!0),!n}),this.phase===k.HUNT)n&&e.some(e=>Ne(e.state,n[0],this.floor(...n),n[1],1))&&(this.phase=k.RETURN,i=!0);else if(r!==void 0&&e.length&&e.every(e=>M(t[r],e.state.x,e.state.z)))return this.win();i&&j(this.lvl,this.keys,this.phase)}win(){if(this.level+1<this.maps.length){this.load(this.level+1);return}let e=JSON.stringify({t:`end`,ticks:this.ticks-this.runStart,hard:this.hard,stats:this.players().map(e=>({id:e.info.id,kills:e.kills,deaths:e.deaths}))});this.players().forEach(t=>t.conn.send(e)),this.players().forEach(e=>e.kills=e.deaths=0),[this.hard,this.runStart]=[!0,this.ticks],this.load(0)}players(){return[...this.peers.values()].filter(e=>e!==null)}join(e,t){let n=this.players();if(n.length>=this.rules.maxPlayers){e.send(JSON.stringify({t:`full`}));return}let r=0;for(;n.some(e=>e.info.id===r);)r++;let i=ke.find(e=>!n.some(t=>t.info.color===e))??ke[r%ke.length],a={conn:e,info:{id:r,name:Me(je(t)||`Player ${r}`,n.map(e=>e.info.name)),color:i},state:this.place(r,n.filter(e=>e.hp).map(e=>e.state)),bank:10,ack:65535,hp:9,hurt:-60,kills:0,deaths:0};this.peers.set(e,a),this.roster()}roster(){let e=this.players(),t=e.map(e=>e.info).sort((e,t)=>e.id-t.id);e.forEach(e=>e.conn.send(JSON.stringify({t:`roster`,you:e.info.id,players:t})))}},Ie=(e,t)=>{let n=()=>{},r=()=>{n();let r=setInterval(e,t);n=()=>clearInterval(r)};try{let i=URL.createObjectURL(new Blob([`setInterval(()=>postMessage(0),${t})`])),a=new Worker(i);a.onmessage=e,a.onerror=r,n=()=>{a.terminate(),URL.revokeObjectURL(i)}}catch{r()}return()=>n()},Le=(e,t)=>{let n=`tiny-fps:`+e,r=Math.random().toString(36).slice(2),i=new BroadcastChannel(n),a=new AbortController,o=globalThis.navigator?.locks,s,l,u,d,f=[],p,m=!1,h=e=>i.postMessage({from:r,...e}),g=e=>!m&&S.onmessage?.({data:e}),_={send:e=>queueMicrotask(()=>g(e))},v=()=>{let e=f.length?f:p?[p]:[];return f=[],e},y=e=>{if(m)return e();u=e,d=r,l=/* @__PURE__ */ new Map,s=new Fe(t),s.start(Ie),s.connect(_),v().forEach(e=>s.receive(_,e)),h({kind:`h`})};i.onmessage=({data:e})=>{if(!m){if(s&&l){let{from:t,kind:n}=e,r=l.get(t);n===`q`?h({to:t,kind:`h`}):n===`m`&&!e.to?(r||(r={send:e=>!m&&h({to:t,kind:`m`,data:e})},l.set(t,r),s.connect(r)),s.receive(r,e.data)):n===`b`&&r&&(l.delete(t),s.disconnect(r))}else e.to!==r&&e.to||(e.kind===`m`&&e.to?g(e.data):e.kind===`h`&&e.from!==d&&(d=e.from,v().forEach(e=>h({kind:`m`,data:e}))))}};let b=e=>{m||(typeof e==`string`&&c(e)?.t===`join`&&(p=e),s?s.receive(_,e):d?h({kind:`m`,data:e}):f.push(e))},x=()=>{m||(m=!0,globalThis.removeEventListener?.(`pagehide`,x),s?(s.stop(),u()):(h({kind:`b`}),a.abort()),i.close(),setTimeout(()=>S.onclose?.()))},S={binaryType:`arraybuffer`,onopen:null,onclose:null,onmessage:null,send:b,close:x};return o?o.request(n,{signal:a.signal},()=>new Promise(y)).catch(()=>{}):y(()=>{}),h({kind:`q`}),globalThis.addEventListener?.(`pagehide`,x),setTimeout(()=>!m&&S.onopen?.()),S};export{Le as localSocket};
|
|
1
|
+
var e=65536/(2*Math.PI),t=t=>Math.round(t*e)&65535,n=e=>Math.max(-32768,Math.min(32767,Math.round(e))),r=e=>n(e*1e4),i=e=>e/1e4,a=t=>t/e,o=e=>{if(e.byteLength!==11)return null;let t=new DataView(e);return t.getUint8(0)===1?{seq:t.getUint16(1),keys:t.getUint8(3),yaw:a(t.getUint16(4)),pitch:i(t.getInt16(6)),view:(t.getUint16(8)*256+t.getUint8(10))/256}:null},s=e=>{let n=/* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(4+24*e.players.length));return n.setUint8(0,2),n.setUint16(1,e.tick&65535),n.setUint8(3,e.players.length),e.players.forEach((e,i)=>{let a=4+i*24;n.setUint8(a,e.id),n.setFloat32(a+1,e.x),n.setFloat32(a+5,e.y),n.setFloat32(a+9,e.z),n.setFloat32(a+13,e.vy),n.setUint16(a+17,t(e.yaw)),n.setInt16(a+19,r(e.pitch)),n.setUint16(a+21,e.ack&65535),n.setUint8(a+23,+e.ground)}),n.buffer},c=e=>{try{let t=JSON.parse(e);return t&&typeof t.t==`string`?t:null}catch{return null}},l=.4,u=1.8,d=1.55,f={maxPlayers:16},p={FORWARD:1,BACK:2,LEFT:4,RIGHT:8,JUMP:16},m={BRICK:0,TILE:1,LAVA:2,STONE:3,METAL:4,WOOD:5,MOSS:6,GRATE:7},h=-30,g=(e,t,n,r,i)=>({min:[e,h,t],max:[n,i,r],top:m.TILE,side:m.BRICK}),_=([e,t,n],[r,i,a],o,s=o,c)=>({min:[e,t,n],max:[r,i,a],top:o,side:s,tint:c}),v={name:`Rooftops`,brushes:[g(4,56,24,76,10),g(4,36,24,53.5,10),g(4,20,24,36,12.4),g(4,0,24,12,12.4),g(27,20,47,50,8),g(27,60,47,76,8),_([9,12.2,39],[19,12.6,47],m.METAL,m.METAL),...((e,t)=>[1,2,3,4,5,6].map(n=>_([12,t,e-n],[14,t+.4*n,e-n+1],m.STONE,m.STONE)))(53,10),_([5,10,42],[5.5,14,42.5],m.METAL),_([5,10,50],[5.5,14,50.5],m.METAL),_([8,12.4,24],[20,12.85,24.45],m.METAL),_([36,8,22],[37,14.5,48],m.STONE,m.STONE,[1,.75,.62]),_([30,8,28],[34,10,32],m.STONE,m.BRICK),_([40,8,30],[44,10.2,34],m.STONE,m.BRICK),_([28,9.2,44],[35,12,46],m.METAL,m.GRATE,[.8,.9,1]),_([41.5,7.6,50],[42,8,60],m.METAL),_([30,8,64],[34,9.6,68],m.STONE,m.BRICK)],spawns:[[6,10,72,0],[10,10,72,0],[14,10,72,0],[18,10,72,0],[22,10,72,0],[8,10,66,0],[14,10,66,0],[20,10,66,0]],floor:-25,sun:{dir:[.6,.64,.48],color:[3,2.7,2.2]},sky:[[.12,.3,.75],[.7,.82,.95]],fog:[.7,.82,.95,.008]},y=4,b=1024,x=(e,t)=>(e+b)*(2*b)+t+b,S=(e,t)=>Math.min(Math.max(Math.floor(e/t),-1024),1023),C=(e,t=y)=>{let n=/* @__PURE__ */ new Map;return e.brushes.forEach(({min:e,max:r},i)=>{for(let a=S(e[0],t);a<=S(r[0],t);a++)for(let o=S(e[2],t);o<=S(r[2],t);o++){let e=x(a,o),t=n.get(e);t?t.push(i):n.set(e,[i])}}),{def:e,cell:t,grid:n}},w=(e,t,n,r)=>{let i=[];for(let a=S(t-r,e.cell);a<=S(t+r,e.cell);a++)for(let t=S(n-r,e.cell);t<=S(n+r,e.cell);t++)for(let n of e.grid.get(x(a,t))??[])i.includes(n)||i.push(n);return i},T=1/60,E=Math.PI*2,D=1e-6,O=.01,k=e=>(e%E+E)%E,A=e=>[-Math.sin(e),-Math.cos(e)],j=Math.fround,M=(e,t,n)=>Math.min(Math.max(e,t),n),N=(e,t,n)=>{let{spawns:r}=t.def,[i,a,o,s]=r[(n%r.length+r.length)%r.length];return{id:e,x:j(i),y:j(a),z:j(o),vy:0,yaw:k(s),pitch:0,ground:!0}},P=(e,t)=>e.max[1]>t+.5+O&&e.min[1]<t+1.8-O,F=(e,t,n)=>[t-M(t,e.min[0],e.max[0]),n-M(n,e.min[2],e.max[2])],I=(e,t)=>{let{brushes:n}=t.def,r=w(t,e.x,e.z,2*l),i=(l-D)**2;for(let t=0;t<4;t++){let t=0,a=0,o=0;for(let s of r){let r=n[s];if(!P(r,e.y))continue;let[c,u]=F(r,e.x,e.z),d=c*c+u*u;if(d>=i)continue;if(d){let e=Math.sqrt(d);.4-e>t&&(t=l-e,a=c/e*t,o=u/e*t);continue}let[f,p,m]=[[e.x-r.min[0],-1,0],[r.max[0]-e.x,1,0],[e.z-r.min[2],0,-1],[r.max[2]-e.z,0,1]].reduce((e,t)=>t[0]<e[0]?t:e);f+.4>t&&(t=f+l,[a,o]=[p*t,m*t])}if(!t)return;e.x+=a,e.z+=o}},L=(e,t)=>{let{brushes:n}=t.def,r=(l-D)**2,i=-1/0,a=1/0;for(let o of w(t,e.x,e.z,l)){let t=n[o],[s,c]=F(t,e.x,e.z);s*s+c*c>=r||(t.max[1]<=e.y+.5+O?i=Math.max(i,t.max[1]):t.min[1]>=e.y+1.8-O&&(a=Math.min(a,t.min[1])))}return[i,a]},R=(e,t,n,r,i)=>{e.x+=t,e.z+=n,I(e,i);let[a,o]=L(e,i);e.ground&&r&&(e.vy=7);let s=e.vy-20*T;if(e.y+=(e.vy+s)/2*T,e.vy=s,e.ground=!1,e.y+1.8>o&&(e.y=o-u,e.vy=Math.min(e.vy,0)),(e.vy<=0?e.y<=a+O:e.y<a)&&(e.y=a,e.vy<=0&&(e.vy=0,e.ground=!0)),e.y<i.def.floor){let{x:t,y:n,z:r}=N(e.id,i,e.id);Object.assign(e,{x:t,y:n,z:r,vy:0,ground:!0})}e.x=j(e.x),e.y=j(e.y),e.z=j(e.z),e.vy=j(e.vy)},z=(e,t,n)=>{e.yaw=k(t.yaw),e.pitch=Math.max(-d,Math.min(d,t.pitch));let r=(t.keys&p.FORWARD?1:0)-(t.keys&p.BACK?1:0),i=(t.keys&p.RIGHT?1:0)-(t.keys&p.LEFT?1:0),a=6*T/(Math.hypot(r,i)||1),[o,s]=A(e.yaw);R(e,(o*r-s*i)*a,(s*r+o*i)*a,(t.keys&p.JUMP)!==0,n)},B=20,V=2,H=[`#e6194b`,`#3cb44b`,`#ffe119`,`#4363d8`,`#f58231`,`#911eb4`,`#42d4f4`,`#f032e6`,`#bfef45`,`#fabed4`,`#469990`,`#dcbeff`,`#9a6324`,`#800000`,`#aaffc3`,`#808000`],U=(e,t)=>Array.from(e).slice(0,t).join(``).trim(),W=e=>U((typeof e==`string`?e:``).replace(/\s+/g,` `).replace(/\p{Cc}/gu,``).trim(),16),G=(e,t)=>{let n=e;for(let r=2;t.includes(n);r++)n=`${U(e,15-`${r}`.length)} ${r}`;return n},K=(e,t)=>{let n=setInterval(e,t);return()=>clearInterval(n)},q=class{peers=/* @__PURE__ */ new Map;ticks=0;history=[];world;halt;rules;rr=0;constructor(e={},t=v){this.rules={...f,...e},this.world=C(t)}get size(){return this.players().length}connect(e){this.peers.set(e,null)}receive(e,t){let n=this.peers.get(e);if(typeof t==`string`){let r=c(t);n===null&&r?.t===`join`&&this.join(e,r.name);return}let r=n&&o(t);r&&n.bank>0&&(n.bank--,z(n.state,r,this.world),n.ack=r.seq)}disconnect(e){let t=this.peers.get(e);this.peers.delete(e),t&&this.roster()}tick(){let e=this.players();if(!e.length)return;let t=++this.ticks;for(let t of e)t.bank=Math.min(10,t.bank+1);if(t%2==0){for(this.history.push({tick:t,players:e.map(e=>({...e.state}))});this.history[0].tick<t-30;)this.history.shift();let n=s({tick:t,players:e.map(e=>({...e.state,ack:e.ack}))});e.forEach(e=>e.conn.send(n))}}start(e=K){if(this.halt)return;let t=1e3/60,n=performance.now(),r=0;this.halt=e(()=>{let e=performance.now();r+=e-n,n=e;for(let e=0;r>=t&&e<B;e++)this.tick(),r-=t;r%=t},t)}stop(){this.halt?.(),this.halt=void 0}rewind(e,t=0){let n=this.players().filter(t=>t.info.id!==e).map(e=>e.state),[r,i]=[this.history[0],this.history.at(-1)];if(!r||!i)return n;let a=((this.ticks-t+32768)%65536+65536)%65536-32768,o=Math.min(Math.max(this.ticks-a,r.tick),i.tick),s=0;for(;s+1<this.history.length&&this.history[s+1].tick<=o;)s++;let[c,l]=[this.history[s],this.history[s+1]??this.history[s]],u=l.tick>c.tick?(o-c.tick)/(l.tick-c.tick):0,d=(e,t)=>e+(t-e)*u;return n.map(e=>{let t=c.players.find(t=>t.id===e.id),n=l.players.find(t=>t.id===e.id);return t&&n?{...n,x:d(t.x,n.x),y:d(t.y,n.y),z:d(t.z,n.z)}:n??t??e})}place(e,t){let n=this.world.def.spawns.findIndex((n,r)=>{let{x:i,z:a}=N(e,this.world,r);return!t.some(e=>Math.hypot(e.x-i,e.z-a)<V)});return N(e,this.world,n<0?this.rr++:n)}players(){return[...this.peers.values()].filter(e=>e!==null)}join(e,t){let n=this.players();if(n.length>=this.rules.maxPlayers){e.send(JSON.stringify({t:`full`}));return}let r=0;for(;n.some(e=>e.info.id===r);)r++;let i=H.find(e=>!n.some(t=>t.info.color===e))??H[r%H.length],a={conn:e,info:{id:r,name:G(W(t)||`Player ${r}`,n.map(e=>e.info.name)),color:i},state:this.place(r,n.map(e=>e.state)),bank:10,ack:65535};this.peers.set(e,a),this.roster()}roster(){let e=this.players(),t=e.map(e=>e.info).sort((e,t)=>e.id-t.id);e.forEach(e=>e.conn.send(JSON.stringify({t:`roster`,you:e.info.id,players:t})))}},J=(e,t)=>{let n=()=>{},r=()=>{n();let r=setInterval(e,t);n=()=>clearInterval(r)};try{let i=URL.createObjectURL(new Blob([`setInterval(()=>postMessage(0),${t})`])),a=new Worker(i);a.onmessage=e,a.onerror=r,n=()=>{a.terminate(),URL.revokeObjectURL(i)}}catch{r()}return()=>n()},Y=(e,t)=>{let n=`tiny-fps:`+e,r=Math.random().toString(36).slice(2),i=new BroadcastChannel(n),a=new AbortController,o=globalThis.navigator?.locks,s,l,u,d,f=[],p,m=!1,h=e=>i.postMessage({from:r,...e}),g=e=>!m&&S.onmessage?.({data:e}),_={send:e=>queueMicrotask(()=>g(e))},v=()=>{let e=f.length?f:p?[p]:[];return f=[],e},y=e=>{if(m)return e();u=e,d=r,l=/* @__PURE__ */ new Map,s=new q(t),s.start(J),s.connect(_),v().forEach(e=>s.receive(_,e)),h({kind:`h`})};i.onmessage=({data:e})=>{if(!m){if(s&&l){let{from:t,kind:n}=e,r=l.get(t);n===`q`?h({to:t,kind:`h`}):n===`m`&&!e.to?(r||(r={send:e=>!m&&h({to:t,kind:`m`,data:e})},l.set(t,r),s.connect(r)),s.receive(r,e.data)):n===`b`&&r&&(l.delete(t),s.disconnect(r))}else e.to!==r&&e.to||(e.kind===`m`&&e.to?g(e.data):e.kind===`h`&&e.from!==d&&(d=e.from,v().forEach(e=>h({kind:`m`,data:e}))))}};let b=e=>{m||(typeof e==`string`&&c(e)?.t===`join`&&(p=e),s?s.receive(_,e):d?h({kind:`m`,data:e}):f.push(e))},x=()=>{m||(m=!0,globalThis.removeEventListener?.(`pagehide`,x),s?(s.stop(),u()):(h({kind:`b`}),a.abort()),i.close(),setTimeout(()=>S.onclose?.()))},S={binaryType:`arraybuffer`,onopen:null,onclose:null,onmessage:null,send:b,close:x};return o?o.request(n,{signal:a.signal},()=>new Promise(y)).catch(()=>{}):y(()=>{}),h({kind:`q`}),globalThis.addEventListener?.(`pagehide`,x),setTimeout(()=>!m&&S.onopen?.()),S};export{Y as localSocket};
|