-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
210 lines (174 loc) · 4.1 KB
/
main.cpp
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
#include <curses.h>
#include <ncurses.h>
#include <thread>
#include <mutex>
#include <map>
#include <iostream>
#include <stdlib.h>
#include <deque>
#include <unistd.h>
#include <time.h>
#include <vector>
#include <math.h>
enum Atom{
Hydrogen,
Oxygen,
Null
};
struct Particle{
double y;
double x;
double yv;
double xv;
Particle(double y, double x, double yv, double xv) : y(y), x(x), yv(yv), xv(xv){}
};
std::vector<struct Particle> particles;
std::mutex mP;
std::map<char, int> barrier; //tracks how many atoms are at the barrier.
std::mutex mB; //protect the barrier
//std::condition_variable cv; //wait on this condition.
std::map<char, std::deque<int>> tunnel; //what atoms are in the tunnel.
std::mutex mT; //protect the tunnel
int pipeLength = 50; //length of the tunnel..
void keyboardMonitor(WINDOW * win){
char c = '0';
while(1){
c = getch();
if(c == 'H' || c == 'O'){
std::string msg = "Generating atom " + std::string(1, c) + "\n";
waddstr(win, msg.c_str());
wrefresh(win);
mT.lock();
tunnel[c].push_front(2);
mT.unlock();
}
else{
continue;
}
}
}
/*
void pushMonitor(){ //move the tunnel forward
while(1){
m.lock();
tunnel.push_front(' ');
m.unlock();
usleep(1000 * 100);
}
}
*/
//void animateScreen(
void barrierMonitor(WINDOW* win){
std::string text = "SCIENCE TERMINAL\n";
waddstr(win, text.c_str());
wrefresh(win);
while(1){
mB.lock();
if(barrier['O'] >= 1 && barrier['H'] >= 2){
barrier['O']--;
barrier['H']-=2;
std::string w = "WATER!!!\n";
waddstr(win, w.c_str());
wrefresh(win);
struct Particle p(2, 2, 0, 1);
mP.lock();
particles.emplace_back(p);
mP.unlock();
}
mB.unlock();
}
}
void poolMonitor(WINDOW*win){
//box(win,0,0);
while(1){
mP.lock();
for(struct Particle & p : particles){ //erase the particles
wmove(win, std::round(p.y), std::round(p.x)); //clear position!
waddch(win, ' ');
}
for(struct Particle & p : particles){ //move the particles
p.x += p.xv;
p.y += p.yv;
}
for(int i = 0; i < particles.size(); i++){ //collision
struct Particle &p = particles[i];
if(p.y > 50 || p.y < 0){
p.y = p.y - p.yv;
p.yv = -1 * p.yv;
}
if(p.x > 50 || p.x < 0){
p.x = p.x - p.xv;
p.xv = -1 * p.xv;
}
for(int j = 0; j < particles.size(); j++){
if(j != i){
struct Particle &q = particles[j];
if(q.x == p.x && q.y == q.y){ //elastic collision
std::swap(q.xv,p.xv);
std::swap(q.yv,p.yv);
//advance them early to avoid double counting the collision
//q.x += q.xv;
//q.y += q.yv;
//p.x += p.xv;
//p.y += p.yv;
}
}
}
}
for(struct Particle & p : particles){ //draw the particles
wmove(win, std::round(p.y), std::round(p.x)); //clear position!
waddch(win, '*');
}
for(struct Particle & p : particles){ //gravity acceleration
p.yv += 0.98;
}
mP.unlock();
wrefresh(win);
usleep(1000 * 100);
}
}
void cursor_set_color_string(const char * color){
printf("\e]12;%s\a", color);
fflush(stdout);
}
int main(){
std::system("clear");
cursor_set_color_string("black");
initscr(); //initialize curses screen.
cbreak();
noecho();
keypad(stdscr, TRUE);
nodelay(stdscr, TRUE);
WINDOW * term = newwin(50,20, 10, 50); //embedded terminal!
WINDOW * pool = newwin(50,100, 10, 5);
std::thread keym(keyboardMonitor, term);
std::thread barrierm(barrierMonitor, term);
std::thread poolm(poolMonitor, pool);
//std::thread pm(pushMonitor);
while(1){ //console screen
waddch(stdscr, '>'); //ejector...
for(std::pair<const char, std::deque<int>>& p : tunnel){
for(int i = 0; i < p.second.size(); i++){
int x = p.second[i];
int ch = p.first;
wmove(stdscr, 0, x - 1); //clear previous position!
waddch(stdscr, ' ');
wmove(stdscr, 0, x);
waddch(stdscr, ch);
p.second[i] = p.second[i] + 1;
if(p.second[i] > pipeLength){
p.second.pop_back();
wmove(stdscr, 0, x);
waddch(stdscr, ' ');
mB.lock();
barrier[ch]++;
mB.unlock();
}
}
}
wmove(stdscr, 0, 1);
usleep(1000 * 100);
}
endwin(); //end curses screen
return 0;
}