Fix "Stay Put" behaviour

This commit is contained in:
Casper Warden
2022-04-19 18:23:30 +01:00
parent 1bee36a890
commit 2efebf8816
4 changed files with 47 additions and 17 deletions

View File

@@ -1,3 +1,4 @@
import { Vector3 } from '../lib';
import { LoginResponse } from '../lib/classes/LoginResponse'; import { LoginResponse } from '../lib/classes/LoginResponse';
import { Bot } from '../lib/Bot'; import { Bot } from '../lib/Bot';
import { LoginParameters } from '../lib/classes/LoginParameters'; import { LoginParameters } from '../lib/classes/LoginParameters';
@@ -14,10 +15,12 @@ export class ExampleBot
protected isConnected = false; protected isConnected = false;
protected isConnecting = false; protected isConnecting = false;
protected loginResponse?: LoginResponse; protected loginResponse?: LoginResponse;
protected bot: Bot; protected bot: Bot;
private reconnectTimer?: Timeout; private reconnectTimer?: Timeout;
protected stayRegion?: string;
protected stayPosition?: Vector3;
constructor() constructor()
{ {
const loginParameters = new LoginParameters(); const loginParameters = new LoginParameters();
@@ -39,13 +42,6 @@ export class ExampleBot
const options = BotOptionFlags.None; const options = BotOptionFlags.None;
this.bot = new Bot(loginParameters, options); 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<void> public async run(): Promise<void>
@@ -92,6 +88,13 @@ export class ExampleBot
// Catches uncaught exceptions // Catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(this, { exit: true })); 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(); await this.login();
} }

View File

@@ -1,9 +1,15 @@
import { LureEvent, Vector3 } from '../../lib';
import { ExampleBot } from '../ExampleBot'; import { ExampleBot } from '../ExampleBot';
import { LureEvent } from '../../lib/events/LureEvent';
class Teleports extends ExampleBot 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<void>
{ {
// "OnLure" event fires when someone tries to teleport us // "OnLure" event fires when someone tries to teleport us
this.bot.clientEvents.onLure.subscribe(this.onLure.bind(this)); 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); await this.bot.clientCommands.comms.sendTeleport(this.masterAvatar);
} }
async onLure(lureEvent: LureEvent) async onLure(lureEvent: LureEvent): Promise<void>
{ {
try 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)
});

View File

@@ -107,12 +107,15 @@ export class Bot
stayPut(stay: boolean, regionName?: string, position?: Vector3): void stayPut(stay: boolean, regionName?: string, position?: Vector3): void
{ {
this.stay = stay; this.stay = stay;
if (regionName !== undefined && position !== undefined) if (regionName !== undefined)
{ {
this.stayRegion = regionName; this.stayRegion = regionName;
if (position !== undefined)
{
this.stayPosition = position; this.stayPosition = position;
} }
} }
}
getCurrentRegion(): Region getCurrentRegion(): Region
{ {
@@ -236,8 +239,16 @@ export class Bot
return this.agent.agentID; return this.agent.agentID;
} }
async connectToSim(requested: boolean = true): Promise<void> async connectToSim(requested: boolean = false): Promise<void>
{ {
if (!requested)
{
if (this.stay && this.stayRegion === '')
{
requested = true;
}
}
this.agent.setCurrentRegion(this.currentRegion); this.agent.setCurrentRegion(this.currentRegion);
const circuit = this.currentRegion.circuit; const circuit = this.currentRegion.circuit;
circuit.init(); circuit.init();
@@ -327,9 +338,13 @@ export class Bot
this.pingNumber++; this.pingNumber++;
if (this.pingNumber % 12 === 0 && this.stay) 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); 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(() => this.clientCommands.teleport.teleportTo(this.stayRegion, this.stayPosition, this.stayPosition).then(() =>
{ {
console.log('I found my way home.'); console.log('I found my way home.');

View File

@@ -1,6 +1,6 @@
{ {
"name": "@caspertech/node-metaverse", "name": "@caspertech/node-metaverse",
"version": "0.5.33", "version": "0.5.34",
"description": "A node.js interface for Second Life.", "description": "A node.js interface for Second Life.",
"main": "dist/lib/index.js", "main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts", "types": "dist/lib/index.d.ts",