From 2efebf88160c99c209daef0d2ff1d5b9be53bb2f Mon Sep 17 00:00:00 2001 From: Casper Warden <216465704+casperwardensl@users.noreply.github.com> Date: Tue, 19 Apr 2022 18:23:30 +0100 Subject: [PATCH] Fix "Stay Put" behaviour --- examples/ExampleBot.ts | 19 +++++++++++-------- examples/Teleports/Teleports.ts | 20 ++++++++++++++++---- lib/Bot.ts | 23 +++++++++++++++++++---- package.json | 2 +- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/examples/ExampleBot.ts b/examples/ExampleBot.ts index 5da0d65..52b2ded 100644 --- a/examples/ExampleBot.ts +++ b/examples/ExampleBot.ts @@ -1,3 +1,4 @@ +import { Vector3 } from '../lib'; import { LoginResponse } from '../lib/classes/LoginResponse'; import { Bot } from '../lib/Bot'; import { LoginParameters } from '../lib/classes/LoginParameters'; @@ -14,10 +15,12 @@ export class ExampleBot protected isConnected = false; protected isConnecting = false; protected loginResponse?: LoginResponse; - protected bot: Bot; private reconnectTimer?: Timeout; + protected stayRegion?: string; + protected stayPosition?: Vector3; + constructor() { const loginParameters = new LoginParameters(); @@ -39,13 +42,6 @@ export class ExampleBot const options = BotOptionFlags.None; this.bot = new Bot(loginParameters, options); - - // This will tell the bot to keep trying to teleport back to the 'stay' location. - // You can specify a region and position, such as: - // bot.stayPut(true, 'Izanagi', new nmv.Vector3([128, 128, 21])); - // Note that the 'stay' location will be updated if you request or accept a lure (a teleport). - // If no region is specified, it will be set to the region you log in to. - this.bot.stayPut(true); } public async run(): Promise @@ -92,6 +88,13 @@ export class ExampleBot // Catches uncaught exceptions process.on('uncaughtException', exitHandler.bind(this, { exit: true })); + // This will tell the bot to keep trying to teleport back to the 'stay' location. + // You can specify a region and position, such as: + // bot.stayPut(true, 'Izanagi', new nmv.Vector3([128, 128, 21])); + // Note that the 'stay' location will be updated if you request or accept a lure (a teleport). + // If no region is specified, it will be set to the region you log in to. + this.bot.stayPut(true, this.stayRegion, this.stayPosition); + await this.login(); } diff --git a/examples/Teleports/Teleports.ts b/examples/Teleports/Teleports.ts index 1bd89a4..0675a56 100644 --- a/examples/Teleports/Teleports.ts +++ b/examples/Teleports/Teleports.ts @@ -1,9 +1,15 @@ +import { LureEvent, Vector3 } from '../../lib'; import { ExampleBot } from '../ExampleBot'; -import { LureEvent } from '../../lib/events/LureEvent'; class Teleports extends ExampleBot { - async onConnected() + // We can make the bot always try to get to a certain region regardless of where it logged in + protected stayRegion = 'Izanagi'; + + // And we can optionally specify a position + protected stayPosition = new Vector3([122, 156, 189]); + + async onConnected(): Promise { // "OnLure" event fires when someone tries to teleport us this.bot.clientEvents.onLure.subscribe(this.onLure.bind(this)); @@ -12,7 +18,7 @@ class Teleports extends ExampleBot await this.bot.clientCommands.comms.sendTeleport(this.masterAvatar); } - async onLure(lureEvent: LureEvent) + async onLure(lureEvent: LureEvent): Promise { try { @@ -45,4 +51,10 @@ class Teleports extends ExampleBot } } -new Teleports().run().then(() => {}).catch((err) => { console.error(err) }); +new Teleports().run().then(() => +{ + +}).catch((err) => +{ + console.error(err) +}); diff --git a/lib/Bot.ts b/lib/Bot.ts index f35af23..9cbaa5d 100644 --- a/lib/Bot.ts +++ b/lib/Bot.ts @@ -107,10 +107,13 @@ export class Bot stayPut(stay: boolean, regionName?: string, position?: Vector3): void { this.stay = stay; - if (regionName !== undefined && position !== undefined) + if (regionName !== undefined) { this.stayRegion = regionName; - this.stayPosition = position; + if (position !== undefined) + { + this.stayPosition = position; + } } } @@ -236,8 +239,16 @@ export class Bot return this.agent.agentID; } - async connectToSim(requested: boolean = true): Promise + async connectToSim(requested: boolean = false): Promise { + if (!requested) + { + if (this.stay && this.stayRegion === '') + { + requested = true; + } + } + this.agent.setCurrentRegion(this.currentRegion); const circuit = this.currentRegion.circuit; circuit.init(); @@ -327,9 +338,13 @@ export class Bot this.pingNumber++; if (this.pingNumber % 12 === 0 && this.stay) { - if (this.currentRegion.regionName !== this.stayRegion) + if (this.currentRegion.regionName.toLowerCase() !== this.stayRegion.toLowerCase()) { console.log('Stay Put: Attempting to teleport to ' + this.stayRegion); + if (this.stayPosition === undefined) + { + this.stayPosition = new Vector3([128, 128, 20]); + } this.clientCommands.teleport.teleportTo(this.stayRegion, this.stayPosition, this.stayPosition).then(() => { console.log('I found my way home.'); diff --git a/package.json b/package.json index d40dcb7..93959e1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@caspertech/node-metaverse", - "version": "0.5.33", + "version": "0.5.34", "description": "A node.js interface for Second Life.", "main": "dist/lib/index.js", "types": "dist/lib/index.d.ts",