summaryrefslogtreecommitdiff
path: root/demo/wsim.c
blob: 78fdf77309531ace1437ce0b6eccb7952645502e (plain)
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
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include "cbrll.h"
/* map holding the pixels for a 200x200 pixel imagebuffer, [x][y] 
 * bits are in braille order within each byte
 */
uint8_t map[100][50];
/* takes char storing the on/off positions of 8 points, in braille encoding
 * order, and prints the corresponding braille char
 */

/* global state bit switch */
uint8_t state = 3;
#define STATE_LOOP 0x01
#define STATE_DEJURE 0x02


/*node, the thing being simulated 
 * 16 bytes each
 */
struct node {
	/*culture to compare similatiry*/
	uint8_t culture;
	/*strength to spread culture*/
	uint8_t memetic;
	/*strength martial*/
	uint8_t martial;
	/*state allegiance*/
	uint8_t dejure;
	/*x and y positions*/
	uint8_t x;
	uint8_t y;
	/*connections to other nodes, 0 represents none*/
	uint8_t connections[10];
};
/*all the nodes, 0 is not used*/
struct node nodes[256];
/*makes a new random node*/
struct node makenode() {
	/*node to work on*/
	struct node rnode;
	rnode.culture = (uint8_t)rand();
	rnode.memetic = (uint8_t)rand();
	rnode.martial = (uint8_t)rand();
	rnode.dejure = (uint8_t)rand();
	rnode.x = (uint8_t)rand();
	while (rnode.x >= 200) {
		rnode.x = (uint8_t)rand();
	}
	rnode.y = (uint8_t)rand();
	while (rnode.y >= 200) {
		rnode.y = (uint8_t)rand();
	}
	memset(rnode.connections, 0, 10*sizeof(uint8_t));
	return rnode;
}
/*draws nodes based on dejure or culture*/
void drawnode(struct node drawn) {
	/*which symbol to draw*/
	uint8_t symbol;
	if (state & STATE_DEJURE) {
		symbol = drawn.dejure;
	} else {
		symbol = drawn.culture;
	}
	/* draws symbol*/
	/*layout as follows 
	 * 012
	 * 7X3
	 * 654
	 */
	setpixel(drawn.x, drawn.y, 1);
	setpixel(drawn.x-1, drawn.y-1, (symbol & (0x01 << 0)) && 1);
	setpixel(drawn.x, drawn.y-1, (symbol & (0x01 << 1)) && 1);
	setpixel(drawn.x+1, drawn.y-1, (symbol & (0x01 << 2)) && 1);
	setpixel(drawn.x+1, drawn.y, (symbol & (0x01 << 3)) && 1);
	setpixel(drawn.x+1, drawn.y+1, (symbol & (0x01 << 4)) && 1);
	setpixel(drawn.x, drawn.y+1, (symbol & (0x01 << 5)) && 1);
	setpixel(drawn.x-1, drawn.y+1, (symbol & (0x01 << 6)) && 1);
	setpixel(drawn.x-1, drawn.y, (symbol & (0x01 << 7)) && 1);
	/*draws memetic strenth*/
	/*Ma
	 *00123 Me
	 *1XXX4
	 *2XOX5
	 *3XXX6
	 *45677
	 */
	//a swich without breaks keeps executing
	switch (drawn.memetic / 32) {
		case 7:
			setpixel(drawn.x+2,drawn.y+2,1);
		case 6:
			setpixel(drawn.x+2,drawn.y+1,1);
		case 5:
			setpixel(drawn.x+2,drawn.y,1);
		case 4:
			setpixel(drawn.x+2,drawn.y-1,1);
		case 3:
			setpixel(drawn.x+2,drawn.y-2,1);
		case 2:
			setpixel(drawn.x+1,drawn.y-2,1);
		case 1:
			setpixel(drawn.x,drawn.y-2,1);
		case 0:
			setpixel(drawn.x-1,drawn.y-2,1);
	}
	//martial strenth
	switch (drawn.martial / 32) {
		case 7:
			setpixel(drawn.x+1,drawn.y+2,1);
		case 6:
			setpixel(drawn.x,drawn.y+2,1);
		case 5:
			setpixel(drawn.x-1,drawn.y+2,1);
		case 4:
			setpixel(drawn.x-2,drawn.y+2,1);
		case 3:
			setpixel(drawn.x-2,drawn.y+1,1);
		case 2:
			setpixel(drawn.x-2,drawn.y,1);
		case 1:
			setpixel(drawn.x-2,drawn.y-1,1);
		case 0:
			setpixel(drawn.x-2,drawn.y-2,1);
	}
	//draws connections
	for (uint8_t i = 0; i < 10; i++) {
		if (drawn.connections[i] != 0) {
			drawline(drawn.x, drawn.y, 
				nodes[drawn.connections[i]].x,
				nodes[drawn.connections[i]].y, 
				1);
		}
	}
}

int main (int argc, char * argv[]) {
	/*holds keypress*/
	char input;
	/*zeroing map*/
	memset(map, 0, sizeof(map[0][0]) * 100 * 50);
	/*seeds random*/
	srand(696969);
	for (int i = 0; i < 64; i++) {
		nodes[i] = makenode();
	}
	while (state & STATE_LOOP) {
		for (int i = 1; i < 64; i++) {
			drawnode(nodes[i]);
		}
		drawnode(nodes[0]);
		printmap(0, 100, 0, 50);

		
		/* checks input*/
		input = getc(stdin);
		/*haven't set the terminal to raw mode so will still need
 		 * space
		 */
		if (input == 'q') {
			state = state & (~STATE_LOOP);
		}
	}
	return 0;
}