Merge pull request #72 from kolbytn/load-npc

always load npc
This commit is contained in:
Kolby Nottingham 2024-05-14 20:23:24 -07:00 committed by GitHub
commit 821b44c389
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 7 deletions

View file

@ -229,8 +229,8 @@ export const actionsList = [
'quantity': '(number) The quantity of the goal to set. Default is 1.'
},
perform: async function (agent, name=null, quantity=1) {
if (!agent.npc.data) return 'NPC module is not loaded.';
await agent.npc.setGoal(name, quantity);
agent.bot.emit('idle'); // to trigger the goal
return 'Set goal: ' + agent.npc.data.curr_goal.name;
}
}

View file

@ -39,8 +39,6 @@ export class NPCContoller {
}
init() {
if (this.data === null) return;
for (let file of readdirSync('src/agent/npc/construction')) {
if (file.endsWith('.json')) {
try {
@ -68,6 +66,7 @@ export class NPCContoller {
}
this.agent.bot.on('idle', async () => {
if (this.data.goals.length === 0 && !this.data.curr_goal) return;
// Wait a while for inputs before acting independently
await new Promise((resolve) => setTimeout(resolve, 5000));
if (!this.agent.isIdle()) return;
@ -81,12 +80,15 @@ export class NPCContoller {
}
async setGoal(name=null, quantity=1) {
this.data.curr_goal = null;
this.last_goals = {};
if (name) {
this.data.curr_goal = {name: name, quantity: quantity};
return;
}
if (!this.data.do_set_goal) return;
let past_goals = {...this.last_goals};
for (let goal in this.data.goals) {
if (past_goals[goal.name] === undefined) past_goals[goal.name] = true;

View file

@ -4,8 +4,8 @@ export class NPCData {
this.curr_goal = null;
this.built = {};
this.home = null;
this.do_routine = true;
this.do_set_goal = true;
this.do_routine = false;
this.do_set_goal = false;
}
toObject() {
@ -24,8 +24,8 @@ export class NPCData {
}
static fromObject(obj) {
if (!obj) return null;
let npc = new NPCData();
if (!obj) return npc;
if (obj.goals) {
npc.goals = [];
for (let goal of obj.goals) {