-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtcResourceBridge.js
More file actions
333 lines (281 loc) · 9.91 KB
/
tcResourceBridge.js
File metadata and controls
333 lines (281 loc) · 9.91 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
// TC-Resource Bridge
// Links Rule 110 cellular automaton to resource spawning
// Creates a living computational environment
import { CONFIG } from './config.js';
import { TcRandom } from './tcStorage.js';
/**
* Get current Rule 110 activity level (0..1)
* Returns the fraction of active cells in the automaton
*/
export function getRule110Activity(stepper) {
if (!stepper) return 0.5; // Default neutral activity
try {
const state = stepper.getState();
if (!state || state.length === 0) return 0.5;
let activeCount = 0;
for (let i = 0; i < state.length; i++) {
if (state[i] === 1) activeCount++;
}
return activeCount / state.length;
} catch (err) {
console.warn('Error reading Rule 110 activity:', err);
return 0.5;
}
}
/**
* Get local activity around a cell position
* @param {Uint8Array} state - Rule 110 state
* @param {number} center - Center cell index
* @param {number} radius - Radius to check
* @returns {number} Local activity (0..1)
*/
function getLocalActivity(state, center, radius) {
if (!state || state.length === 0) return 0.5;
let count = 0;
const localSize = 2 * radius + 1;
for (let i = -radius; i <= radius; i++) {
const idx = (center + i + state.length) % state.length; // Wrap around
if (state[idx] === 1) count++;
}
return count / localSize;
}
/**
* Get spawn location based on Rule 110 state (spatial mapping)
* @param {Object} stepper - Rule 110 stepper instance
* @param {number} canvasWidth - World width
* @param {number} canvasHeight - World height
* @returns {Object} {x, y, tcData} spawn location with TC metadata
*/
export function getRule110SpawnLocation(stepper, canvasWidth, canvasHeight) {
const config = CONFIG.tcResourceIntegration;
if (!stepper || !config.spatialMapping) {
// Fallback to random
// Note: canvasWidth/Height are already adjusted for UI panels by canvasManager
const margin = 60;
return {
x: margin + TcRandom.random() * Math.max(0, canvasWidth - 2 * margin),
y: margin + TcRandom.random() * Math.max(0, canvasHeight - 2 * margin),
tcData: { source: 'random', activity: 0 }
};
}
try {
const state = stepper.getState();
if (!state || state.length === 0) {
throw new Error('Invalid Rule 110 state');
}
// Find all active cells
const activeCells = [];
for (let i = 0; i < state.length; i++) {
if (state[i] === 1) {
activeCells.push(i);
}
}
// If no active cells, spawn at center with low randomness
if (activeCells.length === 0) {
const x = canvasWidth * (0.3 + TcRandom.random() * 0.4);
const y = canvasHeight * (0.3 + TcRandom.random() * 0.4);
return {
x, y,
tcData: { source: 'tc_empty', activity: 0, cellIndex: -1 }
};
}
// Pick a random active cell
const cellIndex = activeCells[Math.floor(TcRandom.random() * activeCells.length)];
// Map to X coordinate
const x = (cellIndex / state.length) * canvasWidth;
// Get local activity for Y positioning
const localActivity = getLocalActivity(state, cellIndex, config.localityRadius);
// Y position varies based on local activity and configured spread
const verticalSpread = config.verticalSpread || 0.3;
const yCenterBias = 0.5; // Center of screen
const yVariation = (TcRandom.random() - 0.5) * verticalSpread * localActivity;
const y = canvasHeight * (yCenterBias + yVariation);
// Clamp to margins
const margin = 60;
const clampedX = Math.max(margin, Math.min(canvasWidth - margin, x));
const clampedY = Math.max(margin, Math.min(canvasHeight - margin, y));
return {
x: clampedX,
y: clampedY,
tcData: {
source: 'tc_spatial',
activity: localActivity,
cellIndex: cellIndex,
globalActivity: getRule110Activity(stepper)
}
};
} catch (err) {
console.warn('Error in Rule 110 spatial mapping:', err);
const margin = 60;
return {
x: margin + TcRandom.random() * (canvasWidth - 2 * margin),
y: margin + TcRandom.random() * (canvasHeight - 2 * margin),
tcData: { source: 'fallback', activity: 0 }
};
}
}
/**
* Get spawn rate multiplier based on Rule 110 activity
* @param {Object} stepper - Rule 110 stepper instance
* @returns {number} Multiplier for spawn rate (0.5..1.5 typically)
*/
export function getRule110SpawnMultiplier(stepper) {
const config = CONFIG.tcResourceIntegration;
if (!stepper || !config.enabled) {
return 1.0; // No modulation
}
const activity = getRule110Activity(stepper);
const influence = config.activityInfluence || 0.5;
const minMult = config.minSpawnMultiplier || 0.5;
const maxMult = config.maxSpawnMultiplier || 1.5;
// Blend between min and max based on activity
const activityMult = minMult + activity * (maxMult - minMult);
// Blend with neutral (1.0) based on influence setting
const finalMult = 1.0 + influence * (activityMult - 1.0);
return finalMult;
}
/**
* Get integrated spawn location and rate info
* Combines spatial and activity modes
*/
export function getRule110SpawnInfo(stepper, canvasWidth, canvasHeight) {
const config = CONFIG.tcResourceIntegration;
if (!config.enabled || !stepper) {
return {
location: {
x: 60 + TcRandom.random() * (canvasWidth - 120),
y: 60 + TcRandom.random() * (canvasHeight - 120)
},
rateMultiplier: 1.0,
tcData: { source: 'disabled' }
};
}
const mode = config.mode || 'hybrid';
if (mode === 'spatial') {
const spawnLoc = getRule110SpawnLocation(stepper, canvasWidth, canvasHeight);
return {
location: { x: spawnLoc.x, y: spawnLoc.y },
rateMultiplier: 1.0,
tcData: spawnLoc.tcData
};
}
if (mode === 'activity') {
const margin = 60;
return {
location: {
x: margin + TcRandom.random() * (canvasWidth - 2 * margin),
y: margin + TcRandom.random() * (canvasHeight - 2 * margin)
},
rateMultiplier: getRule110SpawnMultiplier(stepper),
tcData: {
source: 'tc_activity',
globalActivity: getRule110Activity(stepper)
}
};
}
// Hybrid mode (default)
const spawnLoc = getRule110SpawnLocation(stepper, canvasWidth, canvasHeight);
return {
location: { x: spawnLoc.x, y: spawnLoc.y },
rateMultiplier: getRule110SpawnMultiplier(stepper),
tcData: spawnLoc.tcData
};
}
/**
* Draw Rule 110 overlay on canvas
* Shows the cellular automaton state visually
*/
export function drawRule110Overlay(ctx, stepper, canvasWidth, canvasHeight) {
const config = CONFIG.tcResourceIntegration;
// Early return checks
if (!config) {
console.warn('TC-Resource config not found');
return;
}
if (!config.showOverlay) {
return; // Silently return if overlay disabled
}
if (!stepper) {
console.warn('TC-Resource: No stepper provided to overlay');
return;
}
try {
const state = stepper.getState();
if (!state || state.length === 0) return;
const height = config.overlayHeight || 40;
const opacity = config.overlayOpacity || 0.15;
const position = config.overlayPosition || 'top';
const y = position === 'top' ? 0 : canvasHeight - height;
const cellWidth = canvasWidth / state.length;
ctx.save();
// Draw background bar
ctx.fillStyle = `rgba(0, 0, 0, ${opacity * 2})`;
ctx.fillRect(0, y, canvasWidth, height);
// Draw cells
for (let i = 0; i < state.length; i++) {
if (state[i] === 1) {
const x = i * cellWidth;
ctx.fillStyle = `rgba(0, 255, 136, ${opacity * 3})`;
ctx.fillRect(x, y, cellWidth + 1, height); // +1 to avoid gaps
}
}
// Draw activity indicator bar and label
const activity = getRule110Activity(stepper);
const labelText = `Rule 110: ${(activity * 100).toFixed(1)}% active`;
ctx.font = '11px monospace';
const barWidth = 180;
const barHeight = 10;
const padding = 10;
const labelHeight = 12; // Space for text below
// Position bar on right side, with room for label below
const barX = canvasWidth - barWidth - padding;
const barY = position === 'top' ? y + 5 : y + height - barHeight - labelHeight - 10;
// Draw bar background
ctx.fillStyle = 'rgba(0, 0, 0, 0.6)';
ctx.fillRect(barX, barY, barWidth, barHeight);
// Draw activity fill
ctx.fillStyle = `rgba(0, 255, 136, ${0.5 + activity * 0.5})`;
ctx.fillRect(barX, barY, barWidth * activity, barHeight);
// Draw label BELOW the bar (not overlapping with cells)
ctx.fillStyle = 'rgba(0, 255, 136, 0.9)';
ctx.fillText(labelText, barX, barY + barHeight + labelHeight);
ctx.restore();
} catch (err) {
console.warn('Error drawing Rule 110 overlay:', err);
}
}
/**
* Console logging helper for TC-resource events
*/
export function logTcResourceEvent(type, data) {
if (!CONFIG.tcResourceIntegration.enabled) return;
const prefix = '🔬 [TC→Resource]';
switch (type) {
case 'spawn':
console.log(`${prefix} Spawn at (${data.x.toFixed(0)}, ${data.y.toFixed(0)}) ` +
`from ${data.tcData?.source || 'unknown'} ` +
`(activity: ${(data.tcData?.activity || 0).toFixed(2)})`);
break;
case 'activity_change':
console.log(`${prefix} Activity: ${(data.activity * 100).toFixed(1)}% ` +
`(multiplier: ${data.multiplier.toFixed(2)}x)`);
break;
default:
console.log(`${prefix} ${type}:`, data);
}
}
/**
* Toggle overlay visibility (helper for console)
*/
export function toggleOverlay(enabled) {
if (enabled === undefined) {
enabled = !CONFIG.tcResourceIntegration.showOverlay;
}
CONFIG.tcResourceIntegration.showOverlay = enabled;
console.log(`🎨 TC-Resource overlay ${enabled ? 'enabled' : 'disabled'}`);
return enabled;
}
// Make available globally
if (typeof window !== 'undefined') {
window.toggleTcOverlay = toggleOverlay;
}