/
no8_20070909_2344_cool_settings.c
653 lines (565 loc) · 14.9 KB
/
no8_20070909_2344_cool_settings.c
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/* no8.c - created 2007 by inhaesio zha (zha@inhesion.com) */
/* gcc -ansi -O3 -L/usr/X11R6/lib -lX11 -o no8 no8.c */
/* + use graphviz to generate graphs of network connectivity through time */
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>
#define SEED 0
#define NODES 768
/* TODO: there's a range-oriented bug that's tickled if you set this too
high */
#define TIME 768
#define XWINDOW_WIDTH NODES
#define XWINDOW_HEIGHT TIME
#define SLEEP 1024
#define MAX_CONNECTIVITY 3 * NODES
struct node_t {
unsigned int value;
unsigned int link_count;
struct node_t *link_0;
struct node_t *link_1;
struct node_t *link_2;
unsigned int display_index;
};
typedef struct node_t node_t;
struct network_t {
node_t *nodes[NODES];
node_t *nodes_display_order[NODES];
};
typedef struct network_t network_t;
static void connect_nodes(node_t *this_node, node_t *node_a, node_t *node_b);
static network_t *create_network();
static void destroy_network(network_t *network);
static void disconnect_nodes(node_t *node_a, node_t *node_b);
static int get_action_2(node_t *node);
static int get_action_3(node_t *node);
static void invert_value(node_t *node);
static unsigned int inverted_value(node_t *node);
static void iterate_network(network_t *network);
static void iterate_node(node_t *node);
static void iterate_node_2(node_t *node);
static void iterate_node_3(node_t *node);
static node_t *link_not_me(node_t *node_to_search, node_t *me);
static unsigned int node_can_connect(node_t *node);
static unsigned int node_can_disconnect(node_t *node);
static unsigned int node_is_connected_to(node_t *node_from, node_t *node_to);
static void nothing();
static void redirect_link(node_t *node_to_change, node_t *old_link,
node_t *new_link);
static void shuffle_down_links(node_t *node);
static void swap_nodes(node_t *node_a, node_t *node_b);
static void swap_values(node_t *node_a, node_t *node_b);
network_t *network;
void connect_nodes(node_t *this_node, node_t *node_a, node_t *node_b)
{
node_t *node_a_link_not_me;
node_t *node_b_link_not_me;
if (!node_a || !node_b) {
return;
}
/* TODO: take a deeper look at why this condition happens */
if ((this_node == node_a) || (this_node == node_b) || (node_a == node_b)) {
return;
}
if (node_can_connect(node_a)) {
if (!node_is_connected_to(node_a, node_b)) {
node_a->link_2 = node_b;
node_a->link_count = 3;
}
}
else if (node_can_connect(node_b)) {
if (!node_is_connected_to(node_b, node_a)) {
node_b->link_2 = node_a;
node_b->link_count = 3;
}
}
else {
node_a_link_not_me = link_not_me(node_a, this_node);
if (node_a_link_not_me) {
redirect_link(node_a, node_a_link_not_me, node_b);
redirect_link(this_node, node_a, node_a_link_not_me);
}
node_b_link_not_me = link_not_me(node_b, this_node);
if (node_b_link_not_me) {
redirect_link(node_b, node_b_link_not_me, node_a);
redirect_link(this_node, node_b, node_b_link_not_me);
}
}
}
network_t *create_network()
{
network_t *network;
unsigned int each_node;
node_t *node;
network = malloc(sizeof(network_t));
for (each_node = 0; each_node < NODES; each_node++) {
node = malloc(sizeof(node_t));
node->value = random() % 2;
node->link_count = 2;
node->link_2 = 0;
node->display_index = each_node;
network->nodes[each_node] = node;
network->nodes_display_order[each_node] = node;
}
for (each_node = 1; each_node < (NODES - 1); each_node++) {
node = network->nodes[each_node];
node->link_0 = network->nodes[each_node - 1];
node->link_1 = network->nodes[each_node + 1];
}
node = network->nodes[0];
node->link_0 = network->nodes[NODES - 1];
node->link_1 = network->nodes[1];
node = network->nodes[NODES - 1];
node->link_0 = network->nodes[NODES - 2];
node->link_1 = network->nodes[0];
return network;
}
void destroy_network(network_t *network)
{
unsigned int each_node;
for (each_node = 0; each_node < NODES; each_node++) {
free(network->nodes[each_node]);
}
}
void disconnect_nodes(node_t *node_a, node_t *node_b)
{
if (node_a) {
if (node_can_disconnect(node_a)) {
if (node_b == node_a->link_0) {
node_a->link_0 = 0;
shuffle_down_links(node_a);
}
else if (node_b == node_a->link_1) {
node_a->link_1 = 0;
shuffle_down_links(node_a);
}
else if (node_a->link_2) {
if (node_b == node_a->link_2) {
node_a->link_2 = 0;
}
}
else {
exit(23);
}
node_a->link_count = 2;
}
}
if (node_b) {
if (node_can_disconnect(node_b)) {
if (node_a == node_b->link_0) {
node_b->link_0 = 0;
shuffle_down_links(node_b);
}
else if (node_a == node_b->link_1) {
node_b->link_1 = 0;
shuffle_down_links(node_b);
}
else if (node_b->link_2) {
if (node_a == node_b->link_2) {
node_b->link_2 = 0;
}
}
else {
exit(24);
}
node_b->link_count = 2;
}
}
}
int get_action_2(node_t *node)
{
int action;
unsigned int action_index;
action_index = (1 * node->value) + (2 * node->link_0->value)
+ (4 * node->link_1->value);
return action_index;
}
int get_action_3(node_t *node)
{
int action;
unsigned int action_index;
action_index = (1 * node->value) + (2 * node->link_0->value)
+ (4 * node->link_1->value) + (8 * node->link_2->value);
return action_index;
}
void invert_value(node_t *node)
{
node->value = inverted_value(node);
}
unsigned int inverted_value(node_t *node)
{
unsigned int inverted_value;
if (0 == node->value) {
inverted_value = 1;
}
else {
inverted_value = 0;
}
return inverted_value;
}
void iterate_network(network_t *network)
{
unsigned int each_node;
for (each_node = 0; each_node < NODES; each_node++) {
iterate_node(network->nodes[each_node]);
}
}
void iterate_node(node_t *node)
{
if (2 == node->link_count) {
iterate_node_2(node);
}
else {
iterate_node_3(node);
}
}
void iterate_node_2(node_t *node)
{
int action;
action = get_action_2(node);
switch (action) {
case 0:
node->link_0->value = node->value;
case 1:
connect_nodes(node, node->link_0, node->link_1);
swap_nodes(node, node->link_0);
break;
case 2:
node->link_1->value = node->value;
case 3:
connect_nodes(node, node->link_1, node->link_0);
swap_nodes(node, node->link_1);
break;
case 4:
node->link_0->value = inverted_value(node);
break;
case 5:
node->link_0->value = inverted_value(node);
swap_nodes(node, node->link_1);
break;
case 6:
node->link_1->value = inverted_value(node);
break;
case 7:
node->link_1->value = inverted_value(node);
swap_nodes(node, node->link_0);
break;
}
/*
node->value = 0;
node->value = 1;
invert_value(node);
connect_nodes(node, node->link_0, node->link_1);
swap_values(node, node->link_0);
swap_values(node, node->link_1);
node->link_0->value = node->value;
node->link_0->value = 0;
node->link_0->value = 1;
node->link_0->value = inverted_value(node);
node->link_1->value = node->value;
node->link_1->value = 0;
node->link_1->value = 1;
node->link_1->value = inverted_value(node);
swap_nodes(node, node->link_0);
swap_nodes(node, node->link_1);
*/
}
void iterate_node_3(node_t *node)
{
int action;
action = get_action_3(node);
switch (action) {
case 0:
node->link_1->value = node->value;
case 9:
connect_nodes(node, node->link_0, node->link_1);
swap_nodes(node, node->link_2);
break;
case 1:
node->link_2->value = node->value;
case 10:
connect_nodes(node, node->link_1, node->link_2);
swap_nodes(node, node->link_0);
break;
case 2:
node->link_0->value = node->value;
case 11:
connect_nodes(node, node->link_2, node->link_0);
swap_nodes(node, node->link_1);
break;
case 3:
disconnect_nodes(node, node->link_0);
case 12:
swap_nodes(node, node->link_0);
break;
case 4:
disconnect_nodes(node, node->link_1);
case 13:
swap_nodes(node, node->link_1);
break;
case 5:
disconnect_nodes(node, node->link_2);
case 14:
swap_nodes(node, node->link_2);
break;
case 6:
case 15:
swap_values(node, node->link_0);
break;
case 7:
swap_values(node, node->link_1);
break;
case 8:
swap_values(node, node->link_2);
break;
}
/*
node->value = 0;
node->value = 1;
invert_value(node);
disconnect_nodes(node, node->link_0);
disconnect_nodes(node, node->link_1);
disconnect_nodes(node, node->link_2);
connect_nodes(node, node->link_0, node->link_1);
connect_nodes(node, node->link_1, node->link_2);
connect_nodes(node, node->link_2, node->link_0);
swap_values(node, node->link_0);
swap_values(node, node->link_1);
swap_values(node, node->link_2);
node->link_0->value = node->value;
node->link_0->value = 0;
node->link_0->value = 1;
node->link_0->value = inverted_value(node);
node->link_1->value = node->value;
node->link_1->value = 0;
node->link_1->value = 1;
node->link_1->value = inverted_value(node);
node->link_2->value = node->value;
node->link_2->value = 0;
node->link_2->value = 1;
node->link_2->value = inverted_value(node);
swap_nodes(node, node->link_0);
swap_nodes(node, node->link_1);
swap_nodes(node, node->link_2);
*/
}
node_t *link_not_me(node_t *node_to_search, node_t *me)
{
node_t *link = 0;
if (!node_is_connected_to(node_to_search, me)) {
if (node_to_search->link_0 != me) {
link = node_to_search->link_0;
}
else if (node_to_search->link_1 != me) {
link = node_to_search->link_1;
}
else if (node_to_search->link_2) {
if (node_to_search->link_2 != me) {
link = node_to_search->link_2;
}
}
}
return link;
}
unsigned int node_can_connect(node_t *node)
{
unsigned int can_connect;
if (2 == node->link_count) {
can_connect = 1;
}
else {
can_connect = 0;
}
return can_connect;
}
unsigned int node_can_disconnect(node_t *node)
{
unsigned int can_disconnect;
if (3 == node->link_count) {
can_disconnect = 1;
}
else {
can_disconnect = 0;
}
return can_disconnect;
}
unsigned int node_is_connected_to(node_t *node_from, node_t *node_to)
{
unsigned int connected;
if ((node_from->link_0 == node_to) || (node_from->link_1 == node_to)
|| (node_from->link_2 == node_to)) {
connected = 1;
}
else {
connected = 0;
}
return connected;
}
void nothing()
{
return;
}
void redirect_link(node_t *node_to_change, node_t *old_link, node_t *new_link)
{
if (node_to_change != new_link) {
if (node_to_change->link_0 == old_link) {
node_to_change->link_0 = new_link;
}
else if (node_to_change->link_1 == old_link) {
node_to_change->link_1 = new_link;
}
else if (node_to_change->link_2 == old_link) {
node_to_change->link_2 = new_link;
}
}
}
void shuffle_down_links(node_t *node)
{
if (!node->link_0) {
node->link_0 = node->link_2;
node->link_2 = 0;
}
else if (!node->link_1) {
node->link_1 = node->link_2;
node->link_2 = 0;
}
}
void swap_nodes(node_t *node_a, node_t *node_b)
{
unsigned int temporary_display_index;
if (node_a && node_b) {
redirect_link(node_a->link_0, node_a, node_b);
redirect_link(node_a->link_1, node_a, node_b);
if (node_a->link_2) {
redirect_link(node_a->link_2, node_a, node_b);
}
redirect_link(node_b->link_0, node_b, node_a);
redirect_link(node_b->link_1, node_b, node_a);
if (node_b->link_2) {
redirect_link(node_b->link_2, node_b, node_a);
}
network->nodes_display_order[node_a->display_index] = node_b;
network->nodes_display_order[node_b->display_index] = node_a;
temporary_display_index = node_a->display_index;
node_a->display_index = node_b->display_index;
node_b->display_index = temporary_display_index;
}
}
void swap_values(node_t *node_a, node_t *node_b)
{
unsigned int temporary_value;
if (!node_a || !node_b) {
return;
}
temporary_value = node_a->value;
node_a->value = node_b->value;
node_b->value = temporary_value;
}
int main(int argc, char *argv[])
{
unsigned int moment;
unsigned int node_index;
unsigned int moment_index;
node_t *node;
unsigned int total_connectivity;
Display *display;
GC gc;
int screen_number;
int window_x;
int window_y;
unsigned int each_x;
unsigned int each_y;
unsigned int window_border_width;
unsigned int window_height;
unsigned int window_width;
unsigned long gc_value_mask;
unsigned long window_background_color;
unsigned long window_border_color;
Window root_window;
Window window;
XGCValues gc_values;
Visual* default_visual;
Colormap colormap;
XColor system_color;
XColor exact_color;
unsigned int window_display_x_modulus;
unsigned int window_display_y_modulus;
unsigned int window_x_pixel;
unsigned int window_y_pixel;
display = XOpenDisplay(NULL);
screen_number = DefaultScreen(display);
root_window = RootWindow(display, screen_number);
window_x = 0;
window_y = 0;
window_width = XWINDOW_WIDTH;
window_height = XWINDOW_HEIGHT;
window_border_width = 0;
window_border_color = BlackPixel(display, screen_number);
window_background_color = WhitePixel(display, screen_number);
window = XCreateSimpleWindow(display, root_window, window_x, window_y,
window_width, window_height, window_border_width,
window_border_color, window_background_color);
XMapWindow(display, window);
XFlush(display);
gc_value_mask = 0;
gc = XCreateGC(display, window, gc_value_mask, &gc_values);
default_visual = DefaultVisual(display, DefaultScreen(display));
colormap = XCreateColormap(display, window, default_visual, AllocNone);
XSync(display, False);
window_display_x_modulus = NODES / XWINDOW_WIDTH;
window_display_y_modulus = TIME / XWINDOW_HEIGHT;
srandom(SEED);
network = create_network();
window_y_pixel = 0;
for (moment = 0; moment < TIME; moment++) {
total_connectivity = 0;
if (0 == (moment % window_display_y_modulus)) {
for (node_index = 0; node_index < NODES; node_index++) {
node = network->nodes_display_order[node_index];
total_connectivity += node->link_count;
}
window_x_pixel = 0;
for (node_index = 0; node_index < NODES; node_index++) {
if (0 != (node_index % window_display_x_modulus)) {
continue;
}
node = network->nodes_display_order[node_index];
system_color.red = (total_connectivity / MAX_CONNECTIVITY)
% (USHRT_MAX / 2);
if (0 == node->value) {
system_color.green = USHRT_MAX / 4;
system_color.blue = USHRT_MAX;
}
else if (1 == node->value) {
system_color.green = USHRT_MAX;
system_color.blue = USHRT_MAX / 4;
}
else {
exit(100);
}
if (2 == node->link_count) {
system_color.green /= 2;
system_color.blue /= 2;
}
XAllocColor(display, colormap, &system_color);
XSetForeground(display, gc, system_color.pixel);
XDrawPoint(display, window, gc, window_x_pixel,
window_y_pixel);
window_x_pixel++;
}
XFlush(display);
window_y_pixel++;
}
iterate_network(network);
}
while (1) {
usleep(SLEEP);
}
destroy_network(network);
XUnmapWindow(display, window);
XDestroyWindow(display, window);
XCloseDisplay(display);
return 0;
}