/
NPC.js
176 lines (141 loc) · 5.68 KB
/
NPC.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { promptFunctions } from '../openai.js';
import Entity from './Entity.js';
import npcFunctions from '../npcFunctions.json' assert { type: 'json' };
const NPCfunctionsMap = Object.fromEntries(npcFunctions.map(f => [f.name, f]));
export default class NPC extends Entity {
static memorySize = 5;
constructor(world, label, string, x, y) {
super(world, label, string, x, y);
}
async preTick() {
this.metadata.speechPrev = this.metadata.speechCurr;
this.metadata.speechCurr = null;
}
async tick() {
const formattedCoordinates = ``;
let formattedMemory = '';
if (this.metadata.memory?.length) {
formattedMemory = 'Your memory contains the following facts:\n';
for (const fact of this.metadata.memory)
formattedMemory += ` - ${fact}\n`;
formattedMemory += '\n';
}
const surroundingEntities = [
this.world.cells[this.y - 1][this.x],
this.world.cells[this.y + 1][this.x],
this.world.cells[this.y][this.x - 1],
this.world.cells[this.y][this.x + 1],
];
const surroundings = surroundingEntities.map(cell => cell ? cell.toString() : 'nothing');
const maySpeak = !this.metadata.speechPrev && surroundingEntities.some(cell => cell && cell.constructor.name === 'NPC');
let formattedSurroundings = `To your front is ${surroundings[0]}.\nTo your back is ${surroundings[1]}.\nTo your left is ${surroundings[2]}.\nTo your right is ${surroundings[3]}.\n`;
const otherNPCs = this.world.cells.flat().filter(cell => cell && cell.constructor.name === 'NPC' && cell !== this);
for (const npc of otherNPCs) {
formattedSurroundings += `\n${npc.label} is ${Math.abs(npc.x - this.x)} cells to your ${npc.x < this.x ? 'left' : 'right'} and ${Math.abs(npc.y - this.y)} cells to your ${npc.y < this.y ? 'front' : 'back'}.`
}
let formattedEvents = '';
if (this.world.prevEvents?.length) {
formattedEvents = 'In the previous round:\n';
for (const event of this.world.prevEvents)
formattedEvents += ` - ${event}\n`;
formattedEvents += '\n';
}
const surroundingEntitiesThatHaveSpoken = surroundingEntities.filter(cell => cell && cell.constructor.name === 'NPC' && cell.metadata.speechPrev);
let formattedSpeech = '';
if (surroundingEntitiesThatHaveSpoken.length) {
formattedSpeech = 'In the previous round:\n';
for (const entity of surroundingEntitiesThatHaveSpoken)
formattedSpeech += ` - ${entity.label} said "${entity.metadata.speechPrev}"\n`;
formattedSpeech += '\n';
}
const m = NPCfunctionsMap;
const capabilities = [
m.doNothing,
//m.remember,
];
if (surroundingEntities[0] == null)
capabilities.push(m.stepForward);
if (surroundingEntities[1] == null)
capabilities.push(m.stepBackward);
if (surroundingEntities[2] == null)
capabilities.push(m.stepLeft);
if (surroundingEntities[3] == null)
capabilities.push(m.stepRight);
if (maySpeak)
capabilities.push(m.say);
let functionCall;
do {
functionCall = await promptFunctions(
`Your name is ${this.label}. You live on a 32x32 grid. Your current coordinates are (${this.x}, ${this.y}).
${formattedMemory}Your goal is: "Walk to someone and have interesting conversations"
You're currently carrying nothing.
${formattedSurroundings}
${formattedEvents}${formattedSpeech}You MUST perform a single function in response to the above information.`,// Your memory is precious, so only remember important things others tell you.`,
capabilities);
if (!(functionCall?.name in NPCfunctionsMap))
break;
this[functionCall.name](...Object.values(functionCall.arguments || []));
// if (functionCall.name === 'say')
// return `${this.label} said: "${functionCall.arguments.text}"`;
//this.move(['up', 'down', 'left', 'right'][Math.floor(Math.random() * 4)]);
} while (functionCall.name === 'remember');
}
doNothing() {
console.log('doing nothing');
}
remember(fact) {
console.log(this.label, 'remembering', fact);
this.metadata.memory = this.metadata.memory || [];
this.metadata.memory.push(fact);
if (this.metadata.memory.length > NPC.memorySize)
this.metadata.memory.shift();
}
say(text) {
this.metadata.speechCurr = text;
// colour output
console.log('\x1b[33m%s\x1b[0m', this.label + ':', text);
// console.log(this.label, 'saying', text);
}
stepForward() {
console.log(this.label, 'stepping forward');
if (this.world.cells[this.y - 1][this.x] === null)
this.setPosition(this.x, this.y - 1);
}
stepBackward() {
console.log(this.label, 'stepping backward');
if (this.world.cells[this.y + 1][this.x] === null)
this.setPosition(this.x, this.y + 1);
}
stepLeft() {
console.log(this.label, 'stepping left');
if (this.world.cells[this.y][this.x - 1] === null)
this.setPosition(this.x - 1, this.y);
}
stepRight() {
console.log(this.label, 'stepping right');
if (this.world.cells[this.y][this.x + 1] === null)
this.setPosition(this.x + 1, this.y);
}
move(direction) {
switch (direction) {
case 'up':
if (this.world.cells[this.y - 1][this.x] === null)
this.setPosition(this.x, this.y - 1);
break;
case 'down':
if (this.world.cells[this.y + 1][this.x] === null)
this.setPosition(this.x, this.y + 1);
break;
case 'left':
if (this.world.cells[this.y][this.x - 1] === null)
this.setPosition(this.x - 1, this.y);
break;
case 'right':
if (this.world.cells[this.y][this.x + 1] === null)
this.setPosition(this.x + 1, this.y);
break;
default:
break;
}
}
}