-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnpc-psychology.js
More file actions
executable file
·325 lines (287 loc) · 9.58 KB
/
Copy pathnpc-psychology.js
File metadata and controls
executable file
·325 lines (287 loc) · 9.58 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// npc-psychology.js - NPC psychology engine
// NPCs with emotional swings, grudges, tilt, traps, bluffs
// Core idea: poker is human vs human, not human vs calculator
/**
* NPC psychology state machine
* Each NPC maintains independent psychological state affecting decisions
*/
class NPCPsychology {
constructor() {
this.states = new Map(); // npcId → psychState
}
getOrCreate(npcId, profile) {
if (!this.states.has(npcId)) {
this.states.set(npcId, {
// ── Mood system ──
mood: 'normal', // normal, tilted, confident, scared, vengeful, bored
moodIntensity: 0, // 0~1, mood intensity
moodDecay: 0, // decay per hand
// ── Grudge system ──
nemesis: null, // player who recently won big pot from me
nemesisGrudge: 0, // 0~1, grudge level
// ── Self-awareness ──
recentResults: [], // last 5 results [{won, amount, hand}]
winStreak: 0,
loseStreak: 0,
tableImage: 'unknown', // tight, loose, aggressive, passive, wild, unknown
caughtBluffing: 0, // times caught bluffing
successfulBluffs: 0, // successful bluff count
// ── Gear shifting ──
gearLevel: 0, // -2(very tight) ~ +2(very loose), 0=normal
trapReady: false, // ready to set trap (been tight for a while)
lastBigAction: null, // last big action {action, hand, result}
// ── Game awareness ──
handsAtTable: 0,
totalChipsWon: 0,
totalChipsLost: 0,
bigPotThreshold: 0, // dynamic: what counts as "big pot"
});
}
return this.states.get(npcId);
}
/**
* Update psychology after each hand
*/
updateAfterHand(npcId, result, profile) {
const ps = this.getOrCreate(npcId, profile);
ps.handsAtTable++;
ps.bigPotThreshold = 200; // can be adjusted dynamically
// Record result
ps.recentResults.push(result);
if (ps.recentResults.length > 8) ps.recentResults.shift();
if (result.won) {
ps.winStreak++;
ps.loseStreak = 0;
ps.totalChipsWon += result.amount;
} else if (result.lost) {
ps.loseStreak++;
ps.winStreak = 0;
ps.totalChipsLost += result.amount;
}
// ── Mood transitions ──
// Consecutive losses trigger tilt
if (ps.loseStreak >= 3 || (result.lost && result.amount > ps.bigPotThreshold)) {
const tiltChance =
profile.style === 'aggressive'
? 0.5
: profile.style === 'maniac'
? 0.7
: profile.style === 'tight'
? 0.15
: profile.style === 'rock'
? 0.05
: 0.3;
if (Math.random() < tiltChance) {
ps.mood = 'tilted';
ps.moodIntensity = Math.min(1, 0.5 + ps.loseStreak * 0.15);
ps.moodDecay = 3 + Math.floor(Math.random() * 3); // recover after 3-5 hands
}
}
// Consecutive wins → confident/cocky
if (ps.winStreak >= 3) {
ps.mood = 'confident';
ps.moodIntensity = Math.min(1, 0.4 + ps.winStreak * 0.1);
ps.moodDecay = 4;
}
// Lost big pot → grudge
if (result.lost && result.amount > ps.bigPotThreshold && result.taker) {
ps.nemesis = result.taker;
ps.nemesisGrudge = Math.min(1, 0.6 + result.amount / 1000);
ps.mood = 'vengeful';
ps.moodIntensity = 0.7;
ps.moodDecay = 5;
}
// Caught bluffing → temporarily tighten
if (result.caughtBluffing) {
ps.caughtBluffing++;
if (ps.caughtBluffing >= 2) {
ps.mood = 'scared';
ps.moodIntensity = 0.6;
ps.moodDecay = 4;
ps.gearLevel = Math.max(-2, ps.gearLevel - 1);
}
}
// into bluff→below time can can again
if (result.successfulBluff) {
ps.successfulBluffs++;
}
// mooddecay
if (ps.moodDecay > 0) {
ps.moodDecay--;
} else if (ps.mood !== 'normal') {
ps.mood = 'normal';
ps.moodIntensity = 0;
}
// ── strategy ──
// too →standardset trap(switch to )
if (ps.gearLevel <= -1 && ps.handsAtTable > 10 && Math.random() < 0.15) {
ps.trapReady = true;
ps.gearLevel = 1; // to
}
// too →tighten
if (ps.gearLevel >= 1 && Math.random() < 0.1) {
ps.gearLevel = Math.max(-1, ps.gearLevel - 1);
ps.trapReady = false;
}
// new
ps.tableImage = this.calculateTableImage(ps);
}
/**
* calc(affectopponentif what see NPC)
*/
calculateTableImage(ps) {
if (ps.handsAtTable < 5) return 'unknown';
const recent = ps.recentResults.slice(-5);
const aggressiveActions = recent.filter((r) => r.wasAggressive).length;
const bluffs = ps.caughtBluffing + ps.successfulBluffs;
if (bluffs >= 3) return 'wild';
if (aggressiveActions >= 4) return 'aggressive';
if (aggressiveActions <= 1) return 'tight';
return 'balanced';
}
/**
* get psychstatedecisionadjust
* return back groupmultiplynumber and shiftamount, NPCdecisionenginethis adjustbehavior
*/
getDecisionModifiers(npcId, opponentId, profile) {
const ps = this.getOrCreate(npcId, profile);
const mods = {
// adjust
tightnessShift: 0, // =, =
aggressionShift: 0, // =, =aggressive
bluffFreqShift: 0, // bluff frequencyshift
callDownShift: 0, // =call down (bluff)
betSizeMult: 1.0, // below betbig small multiplynumber
// behaviorsend
shouldTrap: false, // is set trap(strongcheck-callnon-raise)
shouldOverbet: false, // is below bet(intimidation)
targetPlayer: null, // player
shouldSpeech: null, // NPC()
// info
mood: ps.mood,
moodIntensity: ps.moodIntensity,
};
// ── moodbehaviorshift ──
switch (ps.mood) {
case 'tilted':
// tilted: , , below betbig , bluff
mods.tightnessShift = -0.15 * ps.moodIntensity;
mods.aggressionShift = 0.2 * ps.moodIntensity;
mods.bluffFreqShift = 0.15 * ps.moodIntensity;
mods.betSizeMult = 1.0 + 0.5 * ps.moodIntensity;
if (Math.random() < 0.3) {
mods.shouldOverbet = true;
}
break;
case 'confident':
// confident: , valuebelow bet and bluff
mods.tightnessShift = -0.08 * ps.moodIntensity;
mods.aggressionShift = 0.1 * ps.moodIntensity;
mods.bluffFreqShift = 0.08 * ps.moodIntensity;
break;
case 'scared':
// : tight, not bluff, only in strongout
mods.tightnessShift = 0.15 * ps.moodIntensity;
mods.aggressionShift = -0.1 * ps.moodIntensity;
mods.bluffFreqShift = -0.15 * ps.moodIntensity;
// but this is set trapgood when ——, bluff
if (ps.trapReady && Math.random() < 0.25) {
mods.shouldTrap = true;
mods.bluffFreqShift = 0.3; // big bluff
}
break;
case 'vengeful':
// grudge: opponent, call/raisethey
if (opponentId === ps.nemesis) {
mods.callDownShift = 0.15 * ps.nemesisGrudge;
mods.aggressionShift = 0.12 * ps.nemesisGrudge;
mods.targetPlayer = ps.nemesis;
mods.tightnessShift = -0.1;
}
break;
}
// ── ──
mods.tightnessShift += ps.gearLevel * -0.08; // receiveaffect
// ── trapmode ──
if (ps.trapReady && ps.mood !== 'tilted') {
mods.shouldTrap = true;
}
// ── behavior ──
if (profile.style === 'tricky') {
// : , opponent
if (ps.handsAtTable % 7 === 0) {
mods.tightnessShift += (Math.random() - 0.5) * 0.2;
mods.aggressionShift += (Math.random() - 0.5) * 0.2;
}
// set trap
if (Math.random() < 0.2) mods.shouldTrap = true;
}
if (profile.style === 'maniac') {
// player: randombelow bet
if (Math.random() < 0.15) {
mods.shouldOverbet = true;
mods.betSizeMult = 1.5 + Math.random();
}
}
return mods;
}
/**
* NPC (mood and hand)
*/
generateChat(npcId, profile, situation) {
const ps = this.getOrCreate(npcId, profile);
if (Math.random() > 0.15) return null; // 85%when not
const lines = {
tilted: [
'No. Not like this.',
'These cards again...',
'Unbelievable.',
'I refuse this outcome.',
'Enough.',
],
confident: [
'This spot is mine.',
'I know what I am doing.',
'Go on. Add more.',
'You really want to test me?',
],
scared: ['...', 'Let me think.', 'The numbers feel wrong.'],
vengeful: [
'That is not settled.',
'I remember what happened.',
'I will get those chips back.',
'This hand is personal now.',
],
normal_bluff: [
'This bet speaks for itself.',
'You can still fold.',
'Go ahead. Test me.',
'You do not win every hand.',
],
normal_strong: ['I like where this is going.', 'Yes. This will do.'],
normal_win: ['Good.', 'That worked nicely.', 'As expected.', 'We continue.'],
normal_fold: [
'Not this one.',
'Too expensive for this hand.',
'I can wait for a better spot.',
'You may have this pot.',
],
};
let pool;
if (ps.mood !== 'normal' && lines[ps.mood]) {
pool = lines[ps.mood];
} else if (situation === 'bluffing') {
pool = lines.normal_bluff;
} else if (situation === 'strong') {
pool = lines.normal_strong;
} else if (situation === 'won') {
pool = lines.normal_win;
} else if (situation === 'folded') {
pool = lines.normal_fold;
} else {
return null;
}
return pool[Math.floor(Math.random() * pool.length)];
}
}
module.exports = { NPCPsychology };