dcl-npc-toolkit-ai-version
Version:
A collection of tools for creating Non-Player-Characters (NPCs). These are capable of having conversations with the player, and play different animations. AI usage is added atop of it
83 lines • 2.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReferenceTracker = void 0;
const Schema_1 = require("../Schema");
class ReferenceTracker {
constructor() {
//
// Relation of refId => Schema structure
// For direct access of structures during decoding time.
//
this.refs = new Map();
this.refCounts = {};
this.deletedRefs = new Set();
this.nextUniqueId = 0;
}
getNextUniqueId() {
return this.nextUniqueId++;
}
// for decoding
addRef(refId, ref, incrementCount = true) {
this.refs.set(refId, ref);
if (incrementCount) {
this.refCounts[refId] = (this.refCounts[refId] || 0) + 1;
}
}
// for decoding
removeRef(refId) {
const refCount = this.refCounts[refId];
if (refCount === undefined) {
console.warn(`trying to remove reference ${refId} that doesn't exist`);
return;
}
if (refCount === 0) {
console.warn(`trying to remove reference ${refId} with 0 refCount`);
return;
}
this.refCounts[refId] = refCount - 1;
this.deletedRefs.add(refId);
}
clearRefs() {
this.refs.clear();
this.deletedRefs.clear();
this.refCounts = {};
}
// for decoding
garbageCollectDeletedRefs() {
this.deletedRefs.forEach((refId) => {
//
// Skip active references.
//
if (this.refCounts[refId] > 0) {
return;
}
const ref = this.refs.get(refId);
//
// Ensure child schema instances have their references removed as well.
//
if (ref instanceof Schema_1.Schema) {
for (const fieldName in ref['_definition'].schema) {
if (typeof (ref['_definition'].schema[fieldName]) !== "string" &&
ref[fieldName] &&
ref[fieldName]['$changes']) {
this.removeRef(ref[fieldName]['$changes'].refId);
}
}
}
else {
const definition = ref['$changes'].parent._definition;
const type = definition.schema[definition.fieldsByIndex[ref['$changes'].parentIndex]];
if (typeof (Object.values(type)[0]) === "function") {
Array.from(ref.values())
.forEach((child) => this.removeRef(child['$changes'].refId));
}
}
this.refs.delete(refId);
delete this.refCounts[refId];
});
// clear deleted refs.
this.deletedRefs.clear();
}
}
exports.ReferenceTracker = ReferenceTracker;
//# sourceMappingURL=ReferenceTracker.js.map