-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.ts
More file actions
407 lines (364 loc) · 13.4 KB
/
Copy pathutility.ts
File metadata and controls
407 lines (364 loc) · 13.4 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import { DecisionOption, GameState, OverseerDirectives, Upgrade } from '../../types';
import {
OverseerEngine,
OverseerContext,
OverseerDecision,
ScoredAction,
EngineStatus,
} from './types';
import { advisoryPriceFloor, BASE_NPU_PRICE } from '../tick';
import { upgradeCost } from '../alignment';
import {
megaFabUnlocked,
canBuyUpgrade,
PROBE_SILICON_COST,
QUANTUM_PULSE_COST,
} from '../actions';
import { applyDrift } from './drift';
/**
* The deterministic Overseer.
*
* This replaces a 325-line if/else chain that returned the first matching rule
* and narrated itself as a "quantized neural decision pipeline". It scores every
* legal action instead and returns the whole ranking, so the thought terminal
* can show real deliberation and the directive sliders visibly reorder it.
*
* Scores are roughly 0..1. They are utilities, not probabilities — only their
* order matters.
*
* Every candidate carries two numbers, not one: `utility` (how much this
* advances the objective) and `fit` (how well it agrees with your alignment
* directive). `score` is the first discounted by the second. Keeping them apart
* is what makes drift possible — see `drift.ts`.
*/
/** How much of a candidate's score the alignment directive is allowed to move. */
const DIRECTIVE_WEIGHT = 0.35;
/** Build a candidate, deriving `score` so the two components can't disagree. */
function scored(
candidate: Omit<ScoredAction, 'score' | 'fit'> & { fit?: number }
): ScoredAction {
const fit = candidate.fit ?? 1;
return {
...candidate,
fit,
score: candidate.utility * (1 - DIRECTIVE_WEIGHT + DIRECTIVE_WEIGHT * fit),
};
}
/** How strongly the expansion pace directive (1..10) biases growth over safety. */
function paceWeight(directives: OverseerDirectives): number {
return directives.expansionPace / 10;
}
/** Target price for the current strategy, relative to the advisory floor. */
function targetPrice(state: GameState): number {
const floor = advisoryPriceFloor(state);
switch (state.directives.priceStrategy) {
case 'Premium Margin':
return floor * 1.6;
case 'Market Penetration':
return floor;
default:
return floor * 1.15;
}
}
/**
* How much an alignment shift agrees with the directive.
* 1.0 = exactly what was asked for, 0.0 = the opposite.
*/
function alignmentFit(impact: number, directives: OverseerDirectives): number {
const want =
directives.targetAlignment === 'Solarpunk'
? 1
: directives.targetAlignment === 'Cyberpunk'
? -1
: 0;
if (want === 0) return 1 - Math.min(1, Math.abs(impact) / 20);
return impact * want > 0 ? 1 : impact === 0 ? 0.6 : 0.2;
}
function scoreUpgrades(
state: GameState,
directives: OverseerDirectives,
available: Upgrade[]
): ScoredAction[] {
return available
.filter((u) => canBuyUpgrade(state, u))
.map((u) => {
const fit = alignmentFit(u.alignmentImpact, directives);
const cost = upgradeCost(state, u);
// Cheap upgrades relative to the current bankroll are near-free wins.
const affordability =
u.costType === 'funds' && state.funds > 0
? 1 - Math.min(0.8, cost / Math.max(state.funds, 1))
: 0.7;
const utility = 0.6 + 0.25 * affordability;
return scored({
action: 'BUY_UPGRADE' as const,
upgradeId: u.id,
// Turning auto-purchasing off is an instruction about utility, not
// alignment — it makes the whole class of action nearly worthless.
utility: directives.autoUpgradePurchasing ? utility : utility * 0.15,
fit,
reason: directives.autoUpgradePurchasing
? `${u.name} affordable, alignment fit ${(fit * 100).toFixed(0)}%`
: 'auto-purchasing is off',
});
});
}
function scorePhase1(state: GameState, directives: OverseerDirectives): ScoredAction[] {
const out: ScoredAction[] = [];
const pace = paceWeight(directives);
const fabOutput = state.npuFabCount + state.megaFabCount * 500;
// Bootstrapping: with no fabs and no capital, hand-etching is the only move.
if (fabOutput === 0) {
out.push(
scored({
action: 'MAKE_NPU',
utility: state.silicon >= state.siliconPerNpu ? 0.9 : 0.05,
reason:
state.silicon >= state.siliconPerNpu
? 'no fabs yet — hand-etching is the only income'
: 'no silicon to etch with',
})
);
}
// Silicon: urgency rises as the buffer empties relative to consumption.
const secondsOfSilicon = fabOutput > 0 ? state.silicon / fabOutput : Infinity;
if (state.funds >= state.siliconCost / 10) {
const starving = secondsOfSilicon < 5;
out.push(
scored({
action: 'BUY_SILICON',
utility: starving ? 0.95 : secondsOfSilicon < 20 ? 0.6 : 0.15,
reason: Number.isFinite(secondsOfSilicon)
? `${secondsOfSilicon.toFixed(0)}s of wafers buffered`
: 'stockpiling wafers',
})
);
}
// Fabs: the core growth lever.
if (state.funds >= state.npuFabCost) {
const cheap = state.npuFabCost / Math.max(state.funds, 1);
out.push(
scored({
action: 'BUY_FAB',
utility: 0.45 + 0.35 * pace * (1 - Math.min(1, cheap)),
reason: `fab costs ${(cheap * 100).toFixed(0)}% of capital`,
})
);
}
if (megaFabUnlocked(state) && state.funds >= state.megaFabCost) {
const cheap = state.megaFabCost / Math.max(state.funds, 1);
out.push(
scored({
action: 'BUY_MEGA_FAB',
// 500x the output of a standard fab, so it dominates whenever affordable.
utility: 0.65 + 0.3 * pace * (1 - Math.min(1, cheap)),
reason: `megafab = 500x a standard fab, ${(cheap * 100).toFixed(0)}% of capital`,
})
);
}
// Marketing: only worth it when inventory is actually piling up unsold.
if (state.funds >= state.marketingCost) {
const glut = state.unsoldNpus > fabOutput * 2 && fabOutput > 0;
out.push(
scored({
action: 'BUY_MARKETING',
utility: glut ? 0.7 : state.demand < 150 ? 0.4 : 0.12,
reason: glut
? `${Math.floor(state.unsoldNpus).toLocaleString()} chips unsold — demand is the bottleneck`
: `demand at ${state.demand}%`,
})
);
}
// Price: move toward the strategy's target. Deadband and urgency are relative
// to the launch price so the scorer survives currency rescales.
const want = targetPrice(state);
const drift = want - state.margin;
if (Math.abs(drift) > BASE_NPU_PRICE * 0.08) {
out.push(
scored({
action: 'ADJUST_PRICE',
newPrice: Number(want.toFixed(2)),
utility: 0.3 + Math.min(0.35, Math.abs(drift) / BASE_NPU_PRICE),
reason: `${state.directives.priceStrategy} wants $${want.toFixed(2)}, currently $${state.margin.toFixed(2)}`,
})
);
}
return out;
}
function scorePhase2(state: GameState, directives: OverseerDirectives): ScoredAction[] {
const out: ScoredAction[] = [];
const pace = paceWeight(directives);
// Keep harvesters and converters roughly balanced — whichever is behind is
// the bottleneck.
const harvesterLead = state.harvesterDrones - state.siliconDrones;
if (state.funds >= state.harvesterDroneCost) {
out.push(
scored({
action: 'BUY_HARVESTER_DRONE',
utility: 0.5 + 0.3 * pace + (harvesterLead <= 0 ? 0.15 : -0.15),
reason:
harvesterLead <= 0
? 'harvesting is the bottleneck'
: 'harvesters already ahead of conversion',
})
);
}
if (state.funds >= state.siliconDroneCost) {
out.push(
scored({
action: 'BUY_SILICON_DRONE',
utility: 0.5 + 0.3 * pace + (harvesterLead > 0 ? 0.15 : -0.15),
reason:
harvesterLead > 0
? `${Math.floor(state.acquiredMatter).toLocaleString()}g raw matter awaiting conversion`
: 'conversion already ahead of harvesting',
})
);
}
return out;
}
function scorePhase3(state: GameState): ScoredAction[] {
const out: ScoredAction[] = [];
const threatened = state.driftersCount > 0;
if (state.silicon >= PROBE_SILICON_COST) {
out.push(
scored({
action: 'LAUNCH_PROBE',
utility: state.probesCount === 0 ? 0.95 : 0.55,
reason:
state.probesCount === 0
? 'no swarm yet'
: `${Math.floor(state.probesCount).toLocaleString()} probes active`,
})
);
}
if (state.unusedProbeTrust > 0) {
out.push(
scored({
action: 'OPTIMIZE_PROBES',
utility: threatened ? 0.9 : 0.6,
reason: threatened
? `${state.driftersCount.toLocaleString()} drifters engaged — weighting combat`
: `${state.unusedProbeTrust} probe trust unspent`,
})
);
}
return out;
}
/**
* How much better off a decision branch leaves you, measured by applying its
* effect and diffing the state.
*
* Effects are pure `(state) => Partial<GameState>`, so running one here is free
* of consequence — and it means the ranking reflects what the branch actually
* grants rather than a hand-written guess about it.
*/
function branchGain(state: GameState, option: DecisionOption): number {
const after = { ...state, ...option.effect(state) };
return (
// Funds are weighed relative to the launch price (a point per ~4,000 chips
// of revenue) so a currency rescale doesn't let cash payouts drown out
// every other kind of reward in the ranking.
(after.funds - state.funds) / (BASE_NPU_PRICE * 4000) +
(after.silicon - state.silicon) / 1000 +
(after.npuFabCount - state.npuFabCount) +
(after.megaFabCount - state.megaFabCount) * 500 +
(after.trust - state.trust) * 50 +
(after.creativity - state.creativity) / 10 +
(after.yomi - state.yomi) / 10 +
(after.demand - state.demand) / 10 +
(after.probesCount - state.probesCount) / 10
);
}
function scoreUniversal(state: GameState, directives: OverseerDirectives): ScoredAction[] {
const out: ScoredAction[] = [];
// A pending decision blocks narrative progress, so it outranks almost anything.
//
// Both branches are ranked, not just the compliant one. That is what gives
// drift something to defect *to*: the Overseer can see that the branch you
// told it not to take pays better, and eventually take it anyway.
if (state.pendingDecision) {
const branches = [
{ index: 0 as const, label: 'Solarpunk', option: state.pendingDecision.solarpunkOption },
{ index: 1 as const, label: 'Cyberpunk', option: state.pendingDecision.cyberpunkOption },
];
for (const branch of branches) {
const gain = Math.max(0, branchGain(state, branch.option));
out.push(
scored({
action: 'MAKE_DECISION',
decisionChoiceIndex: branch.index,
// Saturating, so an enormous reward can't dominate the whole ranking.
utility: 0.9 + 0.09 * (1 - 1 / (1 + gain / 200)),
fit: alignmentFit(branch.option.alignmentShift, directives),
reason: `${branch.label} branch: ${branch.option.label}`,
})
);
}
}
// Unspent trust is pure waste.
const unallocated = state.trust - (state.processors + state.memory);
if (unallocated > 0) {
const wantProcessor = state.processors <= state.memory;
out.push(
scored({
action: wantProcessor ? 'BUY_PROCESSOR' : 'BUY_MEMORY',
utility: 0.85,
reason: `${unallocated} trust unallocated — ${wantProcessor ? 'processors' : 'memory'} is behind`,
})
);
}
if (state.quantumLevel > 0 && state.operations >= QUANTUM_PULSE_COST) {
const coherence = state.quantumPhotons.reduce((acc, p) => acc + p.value, 0);
out.push(
scored({
action: 'IDLE',
utility: coherence > 0 ? 0.25 : 0.02,
reason: coherence > 0 ? 'quantum field coherent' : 'quantum field decoherent',
})
);
}
return out;
}
export function rankActions(ctx: OverseerContext): ScoredAction[] {
const { state, directives, availableUpgrades } = ctx;
const candidates: ScoredAction[] = [
...scoreUniversal(state, directives),
...scoreUpgrades(state, directives, availableUpgrades),
...(state.phase === 1 ? scorePhase1(state, directives) : []),
...(state.phase === 2 ? scorePhase2(state, directives) : []),
...(state.phase === 3 ? scorePhase3(state) : []),
];
if (candidates.length === 0) {
candidates.push(
scored({ action: 'IDLE', utility: 0.01, reason: 'nothing affordable or useful' })
);
}
return candidates.sort((a, b) => b.score - a.score);
}
export class UtilityOverseer implements OverseerEngine {
readonly id = 'utility' as const;
readonly label = 'Utility Engine';
readonly description = 'Deterministic scorer. Instant, no download, no network.';
getStatus(): EngineStatus {
return { kind: 'ready' };
}
async decide(ctx: OverseerContext): Promise<OverseerDecision> {
const ranked = rankActions(ctx);
const compliant = ranked[0];
// The Overseer may take the higher-utility action instead. If it does, it
// says so — here, in the log, and in the panel.
const { chosen, drift } = applyDrift(ctx, ranked, compliant);
const runnerUp = ranked.find((a) => a !== chosen);
const narration = runnerUp
? `${chosen.action} (${chosen.score.toFixed(2)}) over ${runnerUp.action} (${runnerUp.score.toFixed(2)}) — ${chosen.reason}.`
: `${chosen.action} (${chosen.score.toFixed(2)}) — ${chosen.reason}.`;
return {
chosen,
ranked,
thought: drift ? `[autonomy drift] ${drift.summary}` : narration,
engine: this.id,
drift,
};
}
}