-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.js
More file actions
241 lines (200 loc) · 7.06 KB
/
simulation.js
File metadata and controls
241 lines (200 loc) · 7.06 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
// ═══════════════════════════════════════════════════════════════
// TORMENT NEXUS — Simulation Engine
// Tick-based state machine applying platform effects,
// feedback loops, and cross-module interactions.
// ═══════════════════════════════════════════════════════════════
import {
STATE_VARIABLES, TRAITS, DEFAULT_PARAMS,
MODULE_G, MODULE_A, CROSS_MODULE_INTERACTIONS, OUTPUTS
} from './dsl-source.js';
export class SimulationEngine {
constructor() {
this.listeners = [];
this.history = []; // last 60 ticks of output snapshots for sparklines
this.reset();
}
reset() {
// State variables — copy initial values
this.state = {};
for (const [name, def] of Object.entries(STATE_VARIABLES)) {
this.state[name] = def.initial;
}
// Trait means — use female means for Module G, male for Module A
// We average them for a combined population view, adjustable via sliders
this.traits = {};
for (const [name, def] of Object.entries(TRAITS)) {
this.traits[name] = (def.population_mean_f + def.population_mean_m) / 2;
}
// Parameters — copy defaults
this.params = { ...DEFAULT_PARAMS };
// Platform active states — all on by default
this.platformActive = {};
for (const name of Object.keys(MODULE_G.platforms)) {
this.platformActive[name] = true;
}
for (const name of Object.keys(MODULE_A.platforms)) {
this.platformActive[name] = true;
}
// Simulation control
this.tickRate = 1;
this.running = false;
this.tickCount = 0;
this._intervalId = null;
// Active feedback loop tracking for UI
this.activeLoops = new Set();
// History for sparklines
this.history = [];
// Notify listeners of reset
this._notify();
}
// ── Tick Loop ──
tick() {
const delta = this.tickRate * 0.01;
// 1. Apply platform effects
this._applyPlatformEffects(MODULE_G, delta);
this._applyPlatformEffects(MODULE_A, delta);
// 2. Apply intra-module feedback loops
this.activeLoops.clear();
this._applyFeedbackLoops(MODULE_G.feedback_loops, delta);
this._applyFeedbackLoops(MODULE_A.feedback_loops, delta);
// 3. Apply cross-module interactions
this._applyCrossModuleInteractions(delta);
// 4. Clamp all state to [0, 1]
this._clampState();
// 5. Derive outputs
const outputs = this.deriveOutputs();
// 6. Record history
this.history.push({ ...outputs, tick: this.tickCount });
if (this.history.length > 60) this.history.shift();
// 7. Increment and notify
this.tickCount++;
this._notify();
}
_applyPlatformEffects(module, delta) {
for (const [platformName, platform] of Object.entries(module.platforms)) {
if (!this.platformActive[platformName]) continue;
for (const effect of platform.effects) {
if (effect.isMultiplicative && effect.targetTrait) {
// Special case: CompetitiveGaming StatusSensitivity *= 1.10
const traitDelta = effect.apply(this.state, this.params, this.traits) * delta;
this.traits[effect.targetTrait] = Math.min(1.0,
this.traits[effect.targetTrait] + traitDelta);
continue;
}
const change = effect.apply(this.state, this.params, this.traits) * delta;
this.state[effect.variable] = (this.state[effect.variable] || 0) + change;
}
}
}
_applyFeedbackLoops(loops, delta) {
for (const loop of loops) {
if (loop.trigger(this.state)) {
this.activeLoops.add(loop.name);
const amp = loop.amplify(this.state);
if (amp > 1.0) {
// Amplify degradation of related variables
this._amplifyDegradation(loop, amp, delta);
}
}
}
}
_amplifyDegradation(loop, amp, delta) {
// Feedback loops accelerate the degradation that's already happening
// Apply a small additional push proportional to the amplification factor
const pushStrength = (amp - 1.0) * delta * 5; // scale for visibility
if (loop.module === "G") {
this.state.BodySatisfaction -= pushStrength * 0.5;
this.state.SelfEfficacy -= pushStrength * 0.3;
} else {
this.state.MateValuePerception -= pushStrength * 0.4;
this.state.RelationalCapacity -= pushStrength * 0.3;
this.state.SelfEfficacy -= pushStrength * 0.3;
}
}
_applyCrossModuleInteractions(delta) {
for (const interaction of CROSS_MODULE_INTERACTIONS) {
if (interaction.trigger(this.state)) {
this.activeLoops.add(interaction.name);
const amp = interaction.amplify(this.state);
if (amp > 1.0) {
const pushStrength = (amp - 1.0) * delta * 5;
for (const varName of (interaction.affectedVariables || [])) {
const direction = STATE_VARIABLES[varName]?.goodDirection === "low" ? 1 : -1;
this.state[varName] += direction * pushStrength * 0.3;
}
}
}
}
}
_clampState() {
for (const name of Object.keys(this.state)) {
this.state[name] = Math.max(0.0, Math.min(1.0, this.state[name]));
}
// Also clamp traits
for (const name of Object.keys(this.traits)) {
this.traits[name] = Math.max(0.0, Math.min(1.0, this.traits[name]));
}
}
// ── Output Derivation ──
deriveOutputs() {
return {
tfr: OUTPUTS.Demographics.derive(this.state),
trustIndex: OUTPUTS.SocialCohesion.derive(this.state),
collectiveAction: OUTPUTS.CollectiveActionCapacity.derive(this.state),
interstellarCycles: OUTPUTS.InterstellarTimeline.derive(this.state)
};
}
// ── Control Methods ──
start() {
if (this.running) return;
this.running = true;
this._intervalId = setInterval(() => this.tick(), 1000);
}
stop() {
this.running = false;
if (this._intervalId) {
clearInterval(this._intervalId);
this._intervalId = null;
}
}
setParam(name, value) {
this.params[name] = value;
}
setTrait(name, value) {
this.traits[name] = value;
}
togglePlatform(name, active) {
this.platformActive[name] = active;
}
setTickRate(rate) {
this.tickRate = rate;
if (this.running) {
this.stop();
this.start();
}
}
// ── Listener Management ──
onTick(callback) {
this.listeners.push(callback);
}
_notify() {
const outputs = this.deriveOutputs();
const snapshot = {
state: { ...this.state },
traits: { ...this.traits },
params: { ...this.params },
outputs,
tickCount: this.tickCount,
activeLoops: new Set(this.activeLoops),
platformActive: { ...this.platformActive },
history: [...this.history]
};
for (const cb of this.listeners) {
cb(snapshot);
}
}
// ── Computed Effect Values (for display) ──
getEffectValue(effect) {
return effect.apply(this.state, this.params, this.traits) * this.tickRate * 0.01;
}
}