@casual-simulation/aux-runtime 3.5.3-alpha.15977308937 → 3.5.3

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.
@@ -12251,6 +12251,65 @@ export interface RecordPackageVersionSuccess extends CrudRecordItemSuccess {
12251
12251
  auxFileResult: RecordFileResult;
12252
12252
  }
12253
12253
 
12254
+ /**
12255
+ * The kinds of map layers that can be added to the map or miniMapPortal.
12256
+ *
12257
+ * @dochash types/os/maps
12258
+ * @docid MapLayer
12259
+ */
12260
+ export type MapLayer = GeoJSONMapLayer;
12261
+
12262
+ /**
12263
+ * Defines a base interface for map layers.
12264
+ *
12265
+ * @dochash types/os/maps
12266
+ * @docid MapLayerBase
12267
+ */
12268
+ export interface MapLayerBase {
12269
+ /**
12270
+ * Copyright information for the layer.
12271
+ */
12272
+ copyright?: string;
12273
+ }
12274
+
12275
+ /**
12276
+ * A map layer that contains GeoJSON data.
12277
+ *
12278
+ * @dochash types/os/maps
12279
+ * @docid GeoJSONMapLayer
12280
+ */
12281
+ export interface GeoJSONMapLayer extends MapLayerBase {
12282
+ type: 'geojson';
12283
+
12284
+ /**
12285
+ * The URL that contains the GeoJSON data.
12286
+ *
12287
+ * Can be a blob url, a data url, or a regular URL.
12288
+ */
12289
+ url?: string;
12290
+
12291
+ /**
12292
+ * The GeoJSON data for the layer.
12293
+ */
12294
+ data?: object;
12295
+ }
12296
+
12297
+ /**
12298
+ * The function signature of a dynamic bot listener.
12299
+ *
12300
+ * That is, a listener that is registered at runtime by a user script instead of parsed from a tag.
12301
+ *
12302
+ * @dochash types/core
12303
+ * @docgroup 01-core
12304
+ * @docname Listener
12305
+ * @docid DynamicListener
12306
+ */
12307
+ export type DynamicListener = (
12308
+ that: any,
12309
+ bot: Bot,
12310
+ tagName: string
12311
+ ) => any;
12312
+
12254
12313
  interface Ai {
12255
12314
  /**
12256
12315
  * Sends a chat message to the AI.
@@ -12758,6 +12817,34 @@ interface Ai {
12758
12817
  }
12759
12818
 
12760
12819
  interface Os {
12820
+
12821
+ /**
12822
+ * Adds the given listener to the given bot for the given tag.
12823
+ *
12824
+ * @param bot The bot that the listener should be added to.
12825
+ * @param tagName The name of the tag that the listener should be added to.
12826
+ * @param listener The listener that should be added to the bot.
12827
+ *
12828
+ * @dochash actions/os/event
12829
+ * @docgroup 02-event-actions
12830
+ * @docname os.addBotListener
12831
+ * @docid os.addBotListener
12832
+ */
12833
+ addBotListener(bot: Bot, tagName: string, listener: DynamicListener): void;
12834
+
12835
+ /**
12836
+ * Removes the given listener from a bot for a specific tag.
12837
+ * @param bot The bot that the listener should be removed from.
12838
+ * @param tagName The name of the tag that the listener should be removed from.
12839
+ * @param listener The listener that should be removed from the bot.
12840
+ *
12841
+ * @dochash actions/os/event
12842
+ * @docgroup 02-event-actions
12843
+ * @docname os.removeBotListener
12844
+ * @docid os.removeBotListener
12845
+ */
12846
+ removeBotListener(bot: Bot, tagName: string, listener: DynamicListener): void;
12847
+
12761
12848
  /**
12762
12849
  * Sleeps for time in ms.
12763
12850
  * @param time Time in ms. 1 second is 1000ms.
@@ -15213,6 +15300,68 @@ interface Os {
15213
15300
  */
15214
15301
  calculateScreenCoordinatesFromViewportCoordinates(portal: 'grid' | 'miniGrid' | 'map' | 'miniMap', coordinates: Vector2): Promise<Vector2>;
15215
15302
 
15303
+ /**
15304
+ * Calculates the screen coordinates that the given 3D position map to on the screen.
15305
+ * Returns a promise that resolves with the calculated screen coordinates.
15306
+ *
15307
+ * Screen coordinates are in pixels and are relative to the top-left corner of the screen.
15308
+ *
15309
+ * @param portal the name of the portal that should be tested.
15310
+ * @param coordinate the 3D position that should be converted to screen coordinates.
15311
+ *
15312
+ * @example Calculate the screen coordinates of the bots in the home dimension in the grid portal
15313
+ * const botPositions = getBots(inDimension('home')).map(bot => new Vector3(bot.tags.homeX, bot.tags.homeY, bot.tags.homeZ));
15314
+ * const coordinates = await os.calculateScreenCoordinatesFromPosition('grid', botPositions);
15315
+ *
15316
+ * @dochash actions/os/portals
15317
+ * @docname os.calculateScreenCoordinatesFromPosition
15318
+ * @docid os.calculateScreenCoordinatesFromPosition
15319
+ * @docgroup 10-raycast
15320
+ */
15321
+ calculateScreenCoordinatesFromPosition(
15322
+ portal: 'grid' | 'miniGrid' | 'map' | 'miniMap',
15323
+ coordinate: Point3D
15324
+ ): Promise<Vector2>;
15325
+ /**
15326
+ * Calculates the screen coordinates that the given 3D position map to on the screen.
15327
+ * Returns a promise that resolves with the calculated screen coordinates.
15328
+ *
15329
+ * Screen coordinates are in pixels and are relative to the top-left corner of the screen.
15330
+ *
15331
+ * @param portal the name of the portal that should be tested.
15332
+ * @param coordinates the 3D positions that should be converted to screen coordinates.
15333
+ *
15334
+ * @example Calculate the screen coordinates of the bots in the home dimension in the grid portal
15335
+ * const botPositions = getBots(inDimension('home')).map(bot => new Vector3(bot.tags.homeX, bot.tags.homeY, bot.tags.homeZ));
15336
+ * const coordinates = await os.calculateScreenCoordinatesFromPosition('grid', botPositions);
15337
+ *
15338
+ * @dochash actions/os/portals
15339
+ * @docname os.calculateScreenCoordinatesFromPosition
15340
+ * @docid os.calculateScreenCoordinatesFromPosition-array
15341
+ * @docgroup 10-raycast
15342
+ */
15343
+ calculateScreenCoordinatesFromPosition(
15344
+ portal: 'grid' | 'miniGrid' | 'map' | 'miniMap',
15345
+ coordinates: Point3D[]
15346
+ ): Promise<Vector2[]>;
15347
+ /**
15348
+ * Calculates the screen coordinates that the given 3D position map to on the screen.
15349
+ * Returns a promise that resolves with the calculated screen coordinates.
15350
+ *
15351
+ * Screen coordinates are in pixels and are relative to the top-left corner of the screen.
15352
+ *
15353
+ * @param portal the name of the portal that should be tested.
15354
+ * @param coordinates the 3D positions that should be converted to screen coordinates.
15355
+ *
15356
+ * @example Calculate the screen coordinates of the bots in the home dimension in the grid portal
15357
+ * const botPositions = getBots(inDimension('home')).map(bot => new Vector3(bot.tags.homeX, bot.tags.homeY, bot.tags.homeZ));
15358
+ * const coordinates = await os.calculateScreenCoordinatesFromPosition('grid', botPositions);
15359
+ */
15360
+ calculateScreenCoordinatesFromPosition(
15361
+ portal: 'grid' | 'miniGrid' | 'map' | 'miniMap',
15362
+ coordinates: Point3D | Point3D[]
15363
+ ): Promise<Vector2[] | Vector2>;
15364
+
15216
15365
  /**
15217
15366
  * Calculates the viewport coordinates that the given screen coordinates map to on the camera.
15218
15367
  * Returns a promise that resolves with the calculated viewport coordinates.
@@ -15451,6 +15600,80 @@ interface Os {
15451
15600
  */
15452
15601
  detachDebugger(debug: Debugger): Promise<void>;
15453
15602
 
15603
+ /**
15604
+ * Adds a map layer to the map or miniMap portal.
15605
+ *
15606
+ * Returns a promise that resolves with the ID of the layer that was added.
15607
+ *
15608
+ * @param portal The portal that the layer should be added to. Either 'map' or 'miniMap'.
15609
+ * @param layer The layer that should be added.
15610
+ *
15611
+ * @example Add a GeoJSON layer to the map portal
15612
+ * const layerId = await os.addMapLayer('map', {
15613
+ * type: 'geojson',
15614
+ * data: {
15615
+ * type: "FeatureCollection",
15616
+ * features: [
15617
+ * {
15618
+ * type: "Feature",
15619
+ * geometry: { type: "Point", coordinates: [102.0, 0.5] },
15620
+ * properties: { prop0: "value0" }
15621
+ * },
15622
+ * {
15623
+ * type: "Feature",
15624
+ * geometry: {
15625
+ * type: "LineString",
15626
+ * coordinates: [
15627
+ * [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
15628
+ * ]
15629
+ * },
15630
+ * properties: {
15631
+ * prop0: "value0",
15632
+ * prop1: 0.0
15633
+ * }
15634
+ * },
15635
+ * {
15636
+ * type: "Feature",
15637
+ * geometry: {
15638
+ * type: "Polygon",
15639
+ * coordinates: [
15640
+ * [[100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
15641
+ * [100.0, 1.0], [100.0, 0.0]]
15642
+ * ]
15643
+ * },
15644
+ * properties: {
15645
+ * prop0: "value0",
15646
+ * prop1: { "this": "that" }
15647
+ * }
15648
+ * }
15649
+ * ]
15650
+ * }
15651
+ * });
15652
+ *
15653
+ * @dochash action/os/maps
15654
+ * @doctitle Map Actions
15655
+ * @docsidebar Maps
15656
+ * @docdescription Actions for working with maps and map layers.
15657
+ * @docid os.addMapLayer
15658
+ */
15659
+ addMapLayer(portal: 'map' | 'miniMap', layer: MapLayer): Promise<string>;
15660
+
15661
+ /**
15662
+ * Removes a layer from the map or miniMap portal.
15663
+ *
15664
+ * Returns a promise that resolves when the layer has been removed.
15665
+ *
15666
+ * @param layerId The ID of the layer to remove.
15667
+ * @returns A promise that resolves when the layer has been removed.
15668
+ *
15669
+ * @example Remove a layer from the map portal
15670
+ * await os.removeMapLayer('my-layer-id');
15671
+ *
15672
+ * @dochash action/os/maps
15673
+ * @docid os.removeMapLayer
15674
+ */
15675
+ removeMapLayer(layerId: string): Promise<void>;
15676
+
15454
15677
  /**
15455
15678
  * The global variables that are stored in the OS.
15456
15679
  */
@@ -1,4 +1,4 @@
1
- import type { StateUpdatedEvent, Bot, BotSpace, RuntimeBot, CompiledBotListener, BotModuleResult, ResolvedBotModule, ImportMetadata } from '@casual-simulation/aux-common/bots';
1
+ import type { StateUpdatedEvent, Bot, BotSpace, RuntimeBot, BotModuleResult, ResolvedBotModule, ImportMetadata, DynamicListener } from '@casual-simulation/aux-common/bots';
2
2
  import type { Observable, SubscriptionLike } from 'rxjs';
3
3
  import type { AuxGlobalContext } from './AuxGlobalContext';
4
4
  import type { AuxLibrary } from './AuxLibrary';
@@ -90,6 +90,7 @@ export declare class AuxRuntime implements RuntimeBotInterface, RuntimeBotFactor
90
90
  * The number of times that the runtime can call onError for an error from the same script.
91
91
  */
92
92
  repeatedErrorLimit: number;
93
+ get library(): AuxLibrary;
93
94
  get context(): AuxGlobalContext;
94
95
  get currentVersion(): RuntimeStateVersion;
95
96
  get globalObject(): any;
@@ -231,11 +232,16 @@ export declare class AuxRuntime implements RuntimeBotInterface, RuntimeBotFactor
231
232
  getRawValue(bot: CompiledBot, tag: string): any;
232
233
  updateTagMask(bot: CompiledBot, tag: string, spaces: string[], value: any): RealtimeEditConfig;
233
234
  getTagMask(bot: CompiledBot, tag: string): any;
234
- getListener(bot: CompiledBot, tag: string): CompiledBotListener;
235
+ getListener(bot: CompiledBot, tag: string): DynamicListener | null;
236
+ setListener(bot: CompiledBot, tag: string, listener: DynamicListener | null): void;
237
+ getDynamicListeners(bot: CompiledBot, tag: string): DynamicListener[] | null;
238
+ addDynamicListener(bot: CompiledBot, tag: string, listener: DynamicListener): void;
239
+ removeDynamicListener(bot: CompiledBot, tag: string, listener: DynamicListener): void;
235
240
  getTagLink(bot: CompiledBot, tag: string): RuntimeBot | RuntimeBot[];
236
241
  getSignature(bot: CompiledBot, signature: string): string;
237
242
  private _compileTagOrMask;
238
243
  private _compileTag;
244
+ private _updateListenerPresense;
239
245
  private _compileTagValue;
240
246
  private _compileValue;
241
247
  private _compileTagMaskValue;
@@ -72,6 +72,9 @@ async function _importInterpreterCore() {
72
72
  * This means taking state updates events, shouts and whispers, and emitting additional events to affect the future state.
73
73
  */
74
74
  export class AuxRuntime {
75
+ get library() {
76
+ return this._library;
77
+ }
75
78
  get context() {
76
79
  return this._globalContext;
77
80
  }
@@ -1702,6 +1705,7 @@ export class AuxRuntime {
1702
1705
  newBot.masks[space] = newMasks;
1703
1706
  }
1704
1707
  }
1708
+ newBot.dynamicListeners = existing.dynamicListeners;
1705
1709
  existing.script[REPLACE_BOT_SYMBOL](newBot.script);
1706
1710
  }
1707
1711
  let precalculated = {
@@ -2152,6 +2156,8 @@ export class AuxRuntime {
2152
2156
  precalculated: true,
2153
2157
  tags: fromFactory ? bot.tags : { ...bot.tags },
2154
2158
  listeners: {},
2159
+ listenerOverrides: {},
2160
+ dynamicListeners: {},
2155
2161
  modules: {},
2156
2162
  exports: {},
2157
2163
  values: {},
@@ -2345,12 +2351,50 @@ export class AuxRuntime {
2345
2351
  return undefined;
2346
2352
  }
2347
2353
  getListener(bot, tag) {
2348
- const listener = bot.listeners[tag];
2349
- if (listener) {
2350
- return listener;
2354
+ return bot.listenerOverrides[tag] || bot.listeners[tag] || null;
2355
+ }
2356
+ setListener(bot, tag, listener) {
2357
+ if (hasValue(listener)) {
2358
+ bot.listenerOverrides[tag] = listener;
2359
+ }
2360
+ else {
2361
+ delete bot.listenerOverrides[tag];
2362
+ }
2363
+ this._updateListenerPresense(bot, tag);
2364
+ }
2365
+ getDynamicListeners(bot, tag) {
2366
+ if (bot.dynamicListeners && bot.dynamicListeners[tag]) {
2367
+ return bot.dynamicListeners[tag];
2368
+ }
2369
+ return null;
2370
+ }
2371
+ addDynamicListener(bot, tag, listener) {
2372
+ if (!bot.dynamicListeners) {
2373
+ bot.dynamicListeners = {};
2374
+ }
2375
+ if (!bot.dynamicListeners[tag]) {
2376
+ bot.dynamicListeners[tag] = [];
2377
+ }
2378
+ const listeners = bot.dynamicListeners[tag];
2379
+ if (listeners.includes(listener)) {
2380
+ // If the listener already exists, do not add it again.
2381
+ return;
2382
+ }
2383
+ listeners.push(listener);
2384
+ this._updateListenerPresense(bot, tag);
2385
+ }
2386
+ removeDynamicListener(bot, tag, listener) {
2387
+ if (bot.dynamicListeners && bot.dynamicListeners[tag]) {
2388
+ const listeners = bot.dynamicListeners[tag];
2389
+ const index = listeners.indexOf(listener);
2390
+ if (index >= 0) {
2391
+ listeners.splice(index, 1);
2392
+ if (listeners.length <= 0) {
2393
+ delete bot.dynamicListeners[tag];
2394
+ }
2395
+ this._updateListenerPresense(bot, tag);
2396
+ }
2351
2397
  }
2352
- this.getValue(bot, tag);
2353
- return bot.listeners[tag] || null;
2354
2398
  }
2355
2399
  getTagLink(bot, tag) {
2356
2400
  const tagValue = bot.values[tag];
@@ -2443,15 +2487,20 @@ export class AuxRuntime {
2443
2487
  }
2444
2488
  this._compileTagValue(bot, tag, tagValue);
2445
2489
  }
2490
+ _updateListenerPresense(bot, tag) {
2491
+ this._globalContext.recordListenerPresense(bot.id, tag, !!bot.listenerOverrides[tag] ||
2492
+ !!bot.listeners[tag] ||
2493
+ !!bot.dynamicListeners[tag]);
2494
+ }
2446
2495
  _compileTagValue(bot, tag, tagValue) {
2447
2496
  let { value, listener, module } = this._compileValue(bot, tag, tagValue);
2448
2497
  if (listener) {
2449
2498
  bot.listeners[tag] = listener;
2450
- this._globalContext.recordListenerPresense(bot.id, tag, true);
2499
+ this._updateListenerPresense(bot, tag);
2451
2500
  }
2452
2501
  else if (!!bot.listeners[tag]) {
2453
2502
  delete bot.listeners[tag];
2454
- this._globalContext.recordListenerPresense(bot.id, tag, false);
2503
+ this._updateListenerPresense(bot, tag);
2455
2504
  }
2456
2505
  if (module) {
2457
2506
  bot.modules[tag] = module;
@@ -2713,7 +2762,13 @@ export class AuxRuntime {
2713
2762
  // Default import function
2714
2763
  [`_${IMPORT_FACTORY}`]: () => (module, meta) => this._importModule(module, meta),
2715
2764
  },
2716
- arguments: [['that', 'data'], IMPORT_FACTORY, EXPORT_FACTORY],
2765
+ arguments: [
2766
+ ['that', 'data'],
2767
+ '$__bot',
2768
+ '$__tag',
2769
+ IMPORT_FACTORY,
2770
+ EXPORT_FACTORY,
2771
+ ],
2717
2772
  });
2718
2773
  if (hasValue(bot)) {
2719
2774
  this._functionMap.set(functionName, func);
@@ -2727,7 +2782,10 @@ export class AuxRuntime {
2727
2782
  };
2728
2783
  const exportFunc = (valueOrSource, exp) => exports(valueOrSource, exp, meta);
2729
2784
  return this._wrapWithCurrentPromise(() => {
2730
- let result = func(null, importFunc, exportFunc);
2785
+ // Pass null for the argument, bot, and tag
2786
+ // because module functions do not accept arguments
2787
+ // and have the bot and tag injected automatically
2788
+ let result = func(null, null, null, importFunc, exportFunc);
2731
2789
  this._scheduleJobQueueCheck();
2732
2790
  return result;
2733
2791
  });