solanapolis 1.0.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.
Files changed (202) hide show
  1. package/README.md +518 -0
  2. package/bin/solanapolis.js +197 -0
  3. package/convex/_generated/api.d.ts +175 -0
  4. package/convex/_generated/api.js +23 -0
  5. package/convex/_generated/dataModel.d.ts +60 -0
  6. package/convex/_generated/server.d.ts +143 -0
  7. package/convex/_generated/server.js +93 -0
  8. package/convex/agent/conversation.ts +352 -0
  9. package/convex/agent/embeddingsCache.ts +110 -0
  10. package/convex/agent/memory.ts +450 -0
  11. package/convex/agent/schema.ts +53 -0
  12. package/convex/aiChat.ts +54 -0
  13. package/convex/aiTown/agent.ts +382 -0
  14. package/convex/aiTown/agentDescription.ts +27 -0
  15. package/convex/aiTown/agentInputs.ts +155 -0
  16. package/convex/aiTown/agentOperations.ts +178 -0
  17. package/convex/aiTown/conversation.ts +395 -0
  18. package/convex/aiTown/conversationMembership.ts +38 -0
  19. package/convex/aiTown/game.ts +371 -0
  20. package/convex/aiTown/ids.ts +32 -0
  21. package/convex/aiTown/inputHandler.ts +9 -0
  22. package/convex/aiTown/inputs.ts +25 -0
  23. package/convex/aiTown/insertInput.ts +20 -0
  24. package/convex/aiTown/location.ts +32 -0
  25. package/convex/aiTown/main.ts +154 -0
  26. package/convex/aiTown/movement.ts +189 -0
  27. package/convex/aiTown/player.ts +310 -0
  28. package/convex/aiTown/playerDescription.ts +35 -0
  29. package/convex/aiTown/schema.ts +79 -0
  30. package/convex/aiTown/world.ts +65 -0
  31. package/convex/aiTown/worldMap.ts +74 -0
  32. package/convex/chat.ts +79 -0
  33. package/convex/constants.ts +78 -0
  34. package/convex/convex.config.ts +6 -0
  35. package/convex/crons.ts +89 -0
  36. package/convex/engine/abstractGame.ts +199 -0
  37. package/convex/engine/historicalObject.ts +355 -0
  38. package/convex/engine/schema.ts +56 -0
  39. package/convex/http.ts +36 -0
  40. package/convex/init.ts +110 -0
  41. package/convex/messages.ts +53 -0
  42. package/convex/npcCarAgents.ts +415 -0
  43. package/convex/schema.ts +61 -0
  44. package/convex/streaming.ts +23 -0
  45. package/convex/testing.ts +202 -0
  46. package/convex/tsconfig.json +18 -0
  47. package/convex/util/FastIntegerCompression.ts +221 -0
  48. package/convex/util/assertNever.ts +4 -0
  49. package/convex/util/asyncMap.ts +20 -0
  50. package/convex/util/compression.ts +71 -0
  51. package/convex/util/geometry.ts +132 -0
  52. package/convex/util/isSimpleObject.ts +11 -0
  53. package/convex/util/llm.ts +724 -0
  54. package/convex/util/minheap.ts +38 -0
  55. package/convex/util/object.ts +22 -0
  56. package/convex/util/sleep.ts +3 -0
  57. package/convex/util/types.ts +33 -0
  58. package/convex/util/xxhash.ts +228 -0
  59. package/convex/world.ts +257 -0
  60. package/data/animations/campfire.json +45 -0
  61. package/data/animations/gentlesparkle.json +37 -0
  62. package/data/animations/gentlesplash.json +61 -0
  63. package/data/animations/gentlewaterfall.json +61 -0
  64. package/data/animations/windmill.json +78 -0
  65. package/data/characters.ts +121 -0
  66. package/data/convertMap.js +74 -0
  67. package/data/gentle.js +330 -0
  68. package/data/spritesheets/f1.ts +75 -0
  69. package/data/spritesheets/f2.ts +75 -0
  70. package/data/spritesheets/f3.ts +75 -0
  71. package/data/spritesheets/f4.ts +75 -0
  72. package/data/spritesheets/f5.ts +75 -0
  73. package/data/spritesheets/f6.ts +75 -0
  74. package/data/spritesheets/f7.ts +75 -0
  75. package/data/spritesheets/f8.ts +75 -0
  76. package/data/spritesheets/p1.ts +59 -0
  77. package/data/spritesheets/p2.ts +59 -0
  78. package/data/spritesheets/p3.ts +59 -0
  79. package/data/spritesheets/player.ts +59 -0
  80. package/data/spritesheets/types.ts +26 -0
  81. package/eslint.config.mjs +37 -0
  82. package/next.config.ts +7 -0
  83. package/package.json +85 -0
  84. package/postcss.config.mjs +7 -0
  85. package/public/file.svg +1 -0
  86. package/public/globe.svg +1 -0
  87. package/public/helius-icon.svg +84 -0
  88. package/public/helius-logo.svg +85 -0
  89. package/public/next.svg +1 -0
  90. package/public/plane.glb +0 -0
  91. package/public/vercel.svg +1 -0
  92. package/public/window.svg +1 -0
  93. package/scripts/clear-city.ts +74 -0
  94. package/scripts/seed-wallets.ts +185 -0
  95. package/scripts/setup-webhook.ts +73 -0
  96. package/src/app/api/auth/callback/route.ts +6 -0
  97. package/src/app/api/auth/link-wallet/route.ts +6 -0
  98. package/src/app/api/auth/phantom/route.ts +6 -0
  99. package/src/app/api/broadcast-position/route.ts +59 -0
  100. package/src/app/api/leaderboard/route.ts +85 -0
  101. package/src/app/api/network-stats/route.ts +86 -0
  102. package/src/app/api/parcel-reward/route.ts +181 -0
  103. package/src/app/api/queue-status/route.ts +30 -0
  104. package/src/app/api/snapshots/route.ts +37 -0
  105. package/src/app/api/transactions/enhanced/route.ts +57 -0
  106. package/src/app/api/treasury/route.ts +83 -0
  107. package/src/app/api/wallet/[address]/balances/route.ts +124 -0
  108. package/src/app/api/wallet/[address]/identity/route.ts +32 -0
  109. package/src/app/api/wallet/[address]/route.ts +216 -0
  110. package/src/app/api/wallet/[address]/traded-tokens/route.ts +41 -0
  111. package/src/app/api/wallets/route.ts +68 -0
  112. package/src/app/api/webhooks/helius/route.ts +76 -0
  113. package/src/app/auth/callback/page.tsx +29 -0
  114. package/src/app/favicon.ico +0 -0
  115. package/src/app/globals.css +39 -0
  116. package/src/app/layout.tsx +43 -0
  117. package/src/app/page.tsx +16 -0
  118. package/src/components/AITownNPCs.tsx +206 -0
  119. package/src/components/ActivityFeed.tsx +189 -0
  120. package/src/components/AuthPanel.tsx +163 -0
  121. package/src/components/BeachScene.tsx +280 -0
  122. package/src/components/Building.tsx +138 -0
  123. package/src/components/CesiumFlight.tsx +1768 -0
  124. package/src/components/CesiumGlobe.tsx +616 -0
  125. package/src/components/CitizenCard.tsx +442 -0
  126. package/src/components/CitizenCardModal.tsx +153 -0
  127. package/src/components/CityGrid.tsx +313 -0
  128. package/src/components/CityLandmarks.tsx +427 -0
  129. package/src/components/CityScene.tsx +1289 -0
  130. package/src/components/CitySlotsBadge.tsx +68 -0
  131. package/src/components/CockpitHUD.tsx +460 -0
  132. package/src/components/ConvexWrapper.tsx +19 -0
  133. package/src/components/DubaiDistrict.tsx +630 -0
  134. package/src/components/FlightMiniMap.tsx +133 -0
  135. package/src/components/GameChat.tsx +383 -0
  136. package/src/components/GameHUD.tsx +393 -0
  137. package/src/components/Ground.tsx +14 -0
  138. package/src/components/HowItWorksModal.tsx +251 -0
  139. package/src/components/IngestionBanner.tsx +123 -0
  140. package/src/components/InstancedBuildings.tsx +316 -0
  141. package/src/components/InstancedCars.tsx +504 -0
  142. package/src/components/InstancedCityPlanes.tsx +259 -0
  143. package/src/components/InstancedHouses.tsx +246 -0
  144. package/src/components/InstancedLampPosts.tsx +201 -0
  145. package/src/components/InstancedResidentCars.tsx +357 -0
  146. package/src/components/InstancedRoadDashes.tsx +42 -0
  147. package/src/components/InstancedSkyscrapers.tsx +434 -0
  148. package/src/components/InstancedTrees.tsx +67 -0
  149. package/src/components/LeaderboardPanel.tsx +136 -0
  150. package/src/components/MultiplayerPlanes.tsx +128 -0
  151. package/src/components/NetworkStats.tsx +83 -0
  152. package/src/components/NewBuildingSpotlight.tsx +93 -0
  153. package/src/components/ParcelChallengeBanner.tsx +242 -0
  154. package/src/components/ParcelReward.tsx +191 -0
  155. package/src/components/Park.tsx +42 -0
  156. package/src/components/PhantomWrapper.tsx +22 -0
  157. package/src/components/PixelStreamViewer.tsx +335 -0
  158. package/src/components/PlaneMode.tsx +190 -0
  159. package/src/components/PlayerCar.tsx +211 -0
  160. package/src/components/PlayerPlane.tsx +255 -0
  161. package/src/components/ProjectileRenderer.tsx +249 -0
  162. package/src/components/QueueStatusBanner.tsx +86 -0
  163. package/src/components/RealPlayerTags.tsx +82 -0
  164. package/src/components/SceneLighting.tsx +382 -0
  165. package/src/components/SelectionBeam.tsx +59 -0
  166. package/src/components/SwapPanel.tsx +104 -0
  167. package/src/components/SwapParticles.tsx +237 -0
  168. package/src/components/TreasureGate.tsx +505 -0
  169. package/src/components/WalletPanel.tsx +421 -0
  170. package/src/components/WalletSearch.tsx +244 -0
  171. package/src/components/WelcomeOverlay.tsx +135 -0
  172. package/src/components/WindowTooltip.tsx +498 -0
  173. package/src/context/AuthContext.tsx +230 -0
  174. package/src/lib/bot-detection.ts +125 -0
  175. package/src/lib/building-math.ts +136 -0
  176. package/src/lib/building-shader.ts +253 -0
  177. package/src/lib/car-paths.ts +244 -0
  178. package/src/lib/car-system.ts +182 -0
  179. package/src/lib/city-constants.ts +29 -0
  180. package/src/lib/city-slots.ts +35 -0
  181. package/src/lib/city-zoning.ts +64 -0
  182. package/src/lib/collision-map.ts +147 -0
  183. package/src/lib/day-night.ts +252 -0
  184. package/src/lib/export-card.ts +28 -0
  185. package/src/lib/helius-webhook.ts +90 -0
  186. package/src/lib/helius.ts +74 -0
  187. package/src/lib/house-shader.ts +119 -0
  188. package/src/lib/mock-data.ts +56 -0
  189. package/src/lib/multiplayer-manager.ts +329 -0
  190. package/src/lib/plane-physics.ts +66 -0
  191. package/src/lib/player-car.ts +147 -0
  192. package/src/lib/player-plane.ts +200 -0
  193. package/src/lib/projectile-system.ts +272 -0
  194. package/src/lib/skyscraper-types.ts +52 -0
  195. package/src/lib/sound-engine.ts +464 -0
  196. package/src/lib/supabase-admin.ts +9 -0
  197. package/src/lib/supabase.ts +8 -0
  198. package/src/lib/swap-events.ts +70 -0
  199. package/src/middleware.ts +37 -0
  200. package/src/types/phantom.d.ts +16 -0
  201. package/src/types/wallet.ts +20 -0
  202. package/tsconfig.json +34 -0
@@ -0,0 +1,18 @@
1
+ {
2
+ "compilerOptions": {
3
+ "allowJs": true,
4
+ "strict": true,
5
+ "target": "ESNext",
6
+ "lib": ["ES2021", "dom"],
7
+ "forceConsistentCasingInFileNames": true,
8
+ "skipLibCheck": true,
9
+ "paths": {
10
+ "convex/_generated/*": ["./_generated/*"]
11
+ },
12
+ "moduleResolution": "Bundler",
13
+ "module": "ESNext",
14
+ "noEmit": true
15
+ },
16
+ "include": ["./**/*.ts"],
17
+ "exclude": ["./_generated"]
18
+ }
@@ -0,0 +1,221 @@
1
+ /**
2
+ * FastIntegerCompression.js : a fast integer compression library in JavaScript.
3
+ * From https://github.com/lemire/FastIntegerCompression.js/
4
+ * (c) the authors
5
+ * Licensed under the Apache License, Version 2.0.
6
+ *
7
+ *FastIntegerCompression
8
+ * Simple usage :
9
+ * // var FastIntegerCompression = require("fastintcompression");// if you use node
10
+ * var array = [10,100000,65999,10,10,0,1,1,2000];
11
+ * var buf = FastIntegerCompression.compress(array);
12
+ * var back = FastIntegerCompression.uncompress(buf); // gets back [10,100000,65999,10,10,0,1,1,2000]
13
+ *
14
+ *
15
+ * You can install the library under node with the command line
16
+ * npm install fastintcompression
17
+ */
18
+
19
+ function bytelog(val: number) {
20
+ if (val < 1 << 7) {
21
+ return 1;
22
+ } else if (val < 1 << 14) {
23
+ return 2;
24
+ } else if (val < 1 << 21) {
25
+ return 3;
26
+ } else if (val < 1 << 28) {
27
+ return 4;
28
+ }
29
+ return 5;
30
+ }
31
+
32
+ function zigzag_encode(val: number) {
33
+ return (val + val) ^ (val >> 31);
34
+ }
35
+
36
+ function zigzag_decode(val: number) {
37
+ return (val >> 1) ^ -(val & 1);
38
+ }
39
+
40
+ // Compute how many bytes an array of integers would use once compressed.
41
+ // The input is expected to be an array of non-negative integers.
42
+ export function computeCompressedSizeInBytes(input: number[]) {
43
+ var c = input.length;
44
+ var answer = 0;
45
+ for (var i = 0; i < c; i++) {
46
+ answer += bytelog(input[i]);
47
+ }
48
+ return answer;
49
+ }
50
+
51
+ // Compute how many bytes an array of integers would use once compressed.
52
+ // The input is expected to be an array of integers, some of them can be negative.
53
+ export function computeCompressedSizeInBytesSigned(input: number[]) {
54
+ var c = input.length;
55
+ var answer = 0;
56
+ for (var i = 0; i < c; i++) {
57
+ answer += bytelog(zigzag_encode(input[i]));
58
+ }
59
+ return answer;
60
+ }
61
+
62
+ // Compress an array of integers, return a compressed buffer (as an ArrayBuffer).
63
+ // It is expected that the integers are non-negative: the caller is responsible
64
+ // for making this check. Floating-point numbers are not supported.
65
+ export function compress(input: number[]) {
66
+ var c = input.length;
67
+ var buf = new ArrayBuffer(computeCompressedSizeInBytes(input));
68
+ var view = new Int8Array(buf);
69
+ var pos = 0;
70
+ for (var i = 0; i < c; i++) {
71
+ var val = input[i];
72
+ if (val < 1 << 7) {
73
+ view[pos++] = val;
74
+ } else if (val < 1 << 14) {
75
+ view[pos++] = (val & 0x7f) | 0x80;
76
+ view[pos++] = val >>> 7;
77
+ } else if (val < 1 << 21) {
78
+ view[pos++] = (val & 0x7f) | 0x80;
79
+ view[pos++] = ((val >>> 7) & 0x7f) | 0x80;
80
+ view[pos++] = val >>> 14;
81
+ } else if (val < 1 << 28) {
82
+ view[pos++] = (val & 0x7f) | 0x80;
83
+ view[pos++] = ((val >>> 7) & 0x7f) | 0x80;
84
+ view[pos++] = ((val >>> 14) & 0x7f) | 0x80;
85
+ view[pos++] = val >>> 21;
86
+ } else {
87
+ view[pos++] = (val & 0x7f) | 0x80;
88
+ view[pos++] = ((val >>> 7) & 0x7f) | 0x80;
89
+ view[pos++] = ((val >>> 14) & 0x7f) | 0x80;
90
+ view[pos++] = ((val >>> 21) & 0x7f) | 0x80;
91
+ view[pos++] = val >>> 28;
92
+ }
93
+ }
94
+ return buf;
95
+ }
96
+
97
+ // From a compressed array of integers stored ArrayBuffer,
98
+ // compute the number of compressed integers by scanning the input.
99
+ export function computeHowManyIntegers(input: ArrayBuffer) {
100
+ var view = new Uint8Array(input);
101
+ var c = view.length;
102
+ var count = 0;
103
+ for (var i = 0; i < c; i++) {
104
+ count += view[i] >>> 7;
105
+ }
106
+ return c - count;
107
+ }
108
+ // Uncompress an array of integer from an ArrayBuffer, return the array.
109
+ // It is assumed that they were compressed using the compress function, the caller
110
+ // is responsible for ensuring that it is the case.
111
+ export function uncompress(input: ArrayBuffer) {
112
+ var array = []; // The size of the output is not yet known.
113
+ var inbyte = new Int8Array(input);
114
+ var end = inbyte.length;
115
+ var pos = 0;
116
+ while (end > pos) {
117
+ var c = inbyte[pos++];
118
+ var v = c & 0x7f;
119
+ if (c >= 0) {
120
+ array.push(v);
121
+ continue;
122
+ }
123
+ c = inbyte[pos++];
124
+ v |= (c & 0x7f) << 7;
125
+ if (c >= 0) {
126
+ array.push(v);
127
+ continue;
128
+ }
129
+ c = inbyte[pos++];
130
+ v |= (c & 0x7f) << 14;
131
+ if (c >= 0) {
132
+ array.push(v);
133
+ continue;
134
+ }
135
+ c = inbyte[pos++];
136
+ v |= (c & 0x7f) << 21;
137
+ if (c >= 0) {
138
+ array.push(v);
139
+ continue;
140
+ }
141
+ c = inbyte[pos++];
142
+ v |= c << 28;
143
+ v >>>= 0; // make positive
144
+ array.push(v);
145
+ }
146
+ return array;
147
+ }
148
+
149
+ // Compress an array of integers, return a compressed buffer (as an ArrayBuffer).
150
+ // The integers can be signed (negative), but floating-point values are not supported.
151
+ export function compressSigned(input: number[]) {
152
+ var c = input.length;
153
+ var buf = new ArrayBuffer(computeCompressedSizeInBytesSigned(input));
154
+ var view = new Int8Array(buf);
155
+ var pos = 0;
156
+ for (var i = 0; i < c; i++) {
157
+ var val = zigzag_encode(input[i]);
158
+ if (val < 1 << 7) {
159
+ view[pos++] = val;
160
+ } else if (val < 1 << 14) {
161
+ view[pos++] = (val & 0x7f) | 0x80;
162
+ view[pos++] = val >>> 7;
163
+ } else if (val < 1 << 21) {
164
+ view[pos++] = (val & 0x7f) | 0x80;
165
+ view[pos++] = ((val >>> 7) & 0x7f) | 0x80;
166
+ view[pos++] = val >>> 14;
167
+ } else if (val < 1 << 28) {
168
+ view[pos++] = (val & 0x7f) | 0x80;
169
+ view[pos++] = ((val >>> 7) & 0x7f) | 0x80;
170
+ view[pos++] = ((val >>> 14) & 0x7f) | 0x80;
171
+ view[pos++] = val >>> 21;
172
+ } else {
173
+ view[pos++] = (val & 0x7f) | 0x80;
174
+ view[pos++] = ((val >>> 7) & 0x7f) | 0x80;
175
+ view[pos++] = ((val >>> 14) & 0x7f) | 0x80;
176
+ view[pos++] = ((val >>> 21) & 0x7f) | 0x80;
177
+ view[pos++] = val >>> 28;
178
+ }
179
+ }
180
+ return buf;
181
+ }
182
+
183
+ // Uncompress an array of integer from an ArrayBuffer, return the array.
184
+ // It is assumed that they were compressed using the compressSigned function, the caller
185
+ // is responsible for ensuring that it is the case.
186
+ export function uncompressSigned(input: ArrayBuffer) {
187
+ var array = []; // The size of the output is not yet known.
188
+ var inbyte = new Int8Array(input);
189
+ var end = inbyte.length;
190
+ var pos = 0;
191
+ while (end > pos) {
192
+ var c = inbyte[pos++];
193
+ var v = c & 0x7f;
194
+ if (c >= 0) {
195
+ array.push(zigzag_decode(v));
196
+ continue;
197
+ }
198
+ c = inbyte[pos++];
199
+ v |= (c & 0x7f) << 7;
200
+ if (c >= 0) {
201
+ array.push(zigzag_decode(v));
202
+ continue;
203
+ }
204
+ c = inbyte[pos++];
205
+ v |= (c & 0x7f) << 14;
206
+ if (c >= 0) {
207
+ array.push(zigzag_decode(v));
208
+ continue;
209
+ }
210
+ c = inbyte[pos++];
211
+ v |= (c & 0x7f) << 21;
212
+ if (c >= 0) {
213
+ array.push(zigzag_decode(v));
214
+ continue;
215
+ }
216
+ c = inbyte[pos++];
217
+ v |= c << 28;
218
+ array.push(zigzag_decode(v));
219
+ }
220
+ return array;
221
+ }
@@ -0,0 +1,4 @@
1
+ // From https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#union-exhaustiveness-checking
2
+ export function assertNever(x: never): never {
3
+ throw new Error(`Unexpected object: ${JSON.stringify(x)}`);
4
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * asyncMap returns the results of applying an async function over an list.
3
+ *
4
+ * @param list - Iterable object of items, e.g. an Array, Set, Object.keys
5
+ * @param asyncTransform
6
+ * @returns
7
+ */
8
+
9
+ export async function asyncMap<FromType, ToType>(
10
+ list: Iterable<FromType>,
11
+ asyncTransform: (item: FromType, index: number) => Promise<ToType>,
12
+ ): Promise<ToType[]> {
13
+ const promises: Promise<ToType>[] = [];
14
+ let idx = 0;
15
+ for (const item of list) {
16
+ promises.push(asyncTransform(item, idx));
17
+ idx += 1;
18
+ }
19
+ return Promise.all(promises);
20
+ }
@@ -0,0 +1,71 @@
1
+ export function quantize(values: number[], precision: number) {
2
+ const factor = 1 << precision;
3
+ return values.map((v) => Math.floor(v * factor));
4
+ }
5
+
6
+ export function unquantize(quantized: number[], precision: number) {
7
+ const reciprocal = 1 / (1 << precision);
8
+ return quantized.map((q) => q * reciprocal);
9
+ }
10
+
11
+ export function deltaEncode(values: number[], initialValue = 0) {
12
+ let prev = initialValue;
13
+ const deltas = [];
14
+ for (const value of values) {
15
+ deltas.push(value - prev);
16
+ prev = value;
17
+ }
18
+ return deltas;
19
+ }
20
+
21
+ export function deltaDecode(deltas: number[], initialValue = 0) {
22
+ let prev = initialValue;
23
+ const values = [];
24
+ for (const delta of deltas) {
25
+ const value = prev + delta;
26
+ values.push(value);
27
+ prev = value;
28
+ }
29
+ return values;
30
+ }
31
+
32
+ export function runLengthEncode(values: number[]) {
33
+ let hasPrevious = false;
34
+ let previous = 0;
35
+ let count = 0;
36
+ const encoded = [];
37
+ for (const value of values) {
38
+ if (!hasPrevious) {
39
+ previous = value;
40
+ count = 1;
41
+ hasPrevious = true;
42
+ continue;
43
+ }
44
+ if (previous === value) {
45
+ count += 1;
46
+ continue;
47
+ }
48
+ encoded.push(previous, count);
49
+ previous = value;
50
+ count = 1;
51
+ }
52
+ if (hasPrevious) {
53
+ encoded.push(previous, count);
54
+ }
55
+ return encoded;
56
+ }
57
+
58
+ export function runLengthDecode(encoded: number[]) {
59
+ if (encoded.length % 2 !== 0) {
60
+ throw new Error(`Invalid RLE encoded length: ${encoded.length}`);
61
+ }
62
+ const values = [];
63
+ for (let i = 0; i < encoded.length; i += 2) {
64
+ const value = encoded[i];
65
+ const count = encoded[i + 1];
66
+ for (let j = 0; j < count; j++) {
67
+ values.push(value);
68
+ }
69
+ }
70
+ return values;
71
+ }
@@ -0,0 +1,132 @@
1
+ import { Path, PathComponent, Point, Vector, packPathComponent, queryPath } from './types';
2
+
3
+ export function distance(p0: Point, p1: Point): number {
4
+ const dx = p0.x - p1.x;
5
+ const dy = p0.y - p1.y;
6
+ return Math.sqrt(dx * dx + dy * dy);
7
+ }
8
+
9
+ export function pointsEqual(p0: Point, p1: Point): boolean {
10
+ return p0.x == p1.x && p0.y == p1.y;
11
+ }
12
+
13
+ export function manhattanDistance(p0: Point, p1: Point) {
14
+ return Math.abs(p0.x - p1.x) + Math.abs(p0.y - p1.y);
15
+ }
16
+
17
+ export function pathOverlaps(path: Path, time: number): boolean {
18
+ if (path.length < 2) {
19
+ throw new Error(`Invalid path: ${JSON.stringify(path)}`);
20
+ }
21
+ const start = queryPath(path, 0);
22
+ const end = queryPath(path, path.length - 1);
23
+ return start.t <= time && time <= end.t;
24
+ }
25
+
26
+ export function pathPosition(
27
+ path: Path,
28
+ time: number,
29
+ ): { position: Point; facing: Vector; velocity: number } {
30
+ if (path.length < 2) {
31
+ throw new Error(`Invalid path: ${JSON.stringify(path)}`);
32
+ }
33
+ const first = queryPath(path, 0);
34
+ if (time < first.t) {
35
+ return { position: first.position, facing: first.facing, velocity: 0 };
36
+ }
37
+ const last = queryPath(path, path.length - 1);
38
+ if (last.t < time) {
39
+ return { position: last.position, facing: last.facing, velocity: 0 };
40
+ }
41
+ for (let i = 0; i < path.length - 1; i++) {
42
+ const segmentStart = queryPath(path, i);
43
+ const segmentEnd = queryPath(path, i + 1);
44
+ if (segmentStart.t <= time && time <= segmentEnd.t) {
45
+ const interp = (time - segmentStart.t) / (segmentEnd.t - segmentStart.t);
46
+ return {
47
+ position: {
48
+ x: segmentStart.position.x + interp * (segmentEnd.position.x - segmentStart.position.x),
49
+ y: segmentStart.position.y + interp * (segmentEnd.position.y - segmentStart.position.y),
50
+ },
51
+ facing: segmentStart.facing,
52
+ velocity:
53
+ distance(segmentStart.position, segmentEnd.position) / (segmentEnd.t - segmentStart.t),
54
+ };
55
+ }
56
+ }
57
+ throw new Error(`Timestamp checks not exhaustive?`);
58
+ }
59
+
60
+ export const EPSILON = 0.0001;
61
+
62
+ export function vector(p0: Point, p1: Point): Vector {
63
+ const dx = p1.x - p0.x;
64
+ const dy = p1.y - p0.y;
65
+ return { dx, dy };
66
+ }
67
+
68
+ export function vectorLength(vector: Vector): number {
69
+ return Math.sqrt(vector.dx * vector.dx + vector.dy * vector.dy);
70
+ }
71
+
72
+ export function normalize(vector: Vector): Vector | null {
73
+ const len = vectorLength(vector);
74
+ if (len < EPSILON) {
75
+ return null;
76
+ }
77
+ const { dx, dy } = vector;
78
+ return {
79
+ dx: dx / len,
80
+ dy: dy / len,
81
+ };
82
+ }
83
+
84
+ export function orientationDegrees(vector: Vector): number {
85
+ if (Math.sqrt(vector.dx * vector.dx + vector.dy * vector.dy) < EPSILON) {
86
+ throw new Error(`Can't compute the orientation of too small vector ${JSON.stringify(vector)}`);
87
+ }
88
+ const twoPi = 2 * Math.PI;
89
+ const radians = (Math.atan2(vector.dy, vector.dx) + twoPi) % twoPi;
90
+ return (radians / twoPi) * 360;
91
+ }
92
+
93
+ export function compressPath(densePath: PathComponent[]): Path {
94
+ const packed = densePath.map(packPathComponent);
95
+ if (densePath.length <= 2) {
96
+ return densePath.map(packPathComponent);
97
+ }
98
+ const out = [packPathComponent(densePath[0])];
99
+ let last = densePath[0];
100
+ let candidate;
101
+ for (const point of densePath.slice(1)) {
102
+ if (!candidate) {
103
+ candidate = point;
104
+ continue;
105
+ }
106
+ // We can skip `candidate` if it interpolates cleanly between
107
+ // `last` and `point`.
108
+ const { position, facing } = pathPosition(
109
+ [packPathComponent(last), packPathComponent(point)],
110
+ candidate.t,
111
+ );
112
+ const positionCloseEnough = distance(position, candidate.position) < EPSILON;
113
+ const facingDifference = {
114
+ dx: facing.dx - candidate.facing.dx,
115
+ dy: facing.dy - candidate.facing.dy,
116
+ };
117
+ const facingCloseEnough = vectorLength(facingDifference) < EPSILON;
118
+
119
+ if (positionCloseEnough && facingCloseEnough) {
120
+ candidate = point;
121
+ continue;
122
+ }
123
+
124
+ out.push(packPathComponent(candidate));
125
+ last = candidate;
126
+ candidate = point;
127
+ }
128
+ if (candidate) {
129
+ out.push(packPathComponent(candidate));
130
+ }
131
+ return out;
132
+ }
@@ -0,0 +1,11 @@
1
+ export function isSimpleObject(value: unknown) {
2
+ const isObject = typeof value === 'object';
3
+ const prototype = Object.getPrototypeOf(value);
4
+ const isSimple =
5
+ prototype === null ||
6
+ prototype === Object.prototype ||
7
+ // Objects generated from other contexts (e.g. across Node.js `vm` modules) will not satisfy the previous
8
+ // conditions but are still simple objects.
9
+ prototype?.constructor?.name === 'Object';
10
+ return isObject && isSimple;
11
+ }