|
| 1 | +#include <time.h> |
1 | 2 | #include <stdio.h> |
2 | 3 | #include <stdlib.h> |
3 | 4 | #include <string.h> |
4 | | -#include <sys/ioctl.h> |
5 | | -#include <sys/time.h> |
6 | | -#include <termios.h> |
7 | 5 | #include <unistd.h> |
| 6 | +#include <termios.h> |
| 7 | +#include <sys/ioctl.h> |
8 | 8 | int t() |
9 | 9 | { |
10 | | - struct timeval t; |
11 | | - gettimeofday(&t, 0); |
12 | | - return t.tv_usec / 1000 + t.tv_sec * 1000; |
| 10 | + struct timespec now; |
| 11 | + timespec_get(&now, TIME_UTC); |
| 12 | + return now.tv_sec * 1000 + now.tv_nsec / 1000000; |
13 | 13 | } |
14 | 14 |
|
15 | 15 | #define g(x, y, u, v, s) \ |
16 | | - for (int j = 0, X = x, Y = y; j < v && Y + j < h - X / w && Y >= 0 && X >= 0; ++j) \ |
17 | | - memcpy(&f[Y + j][X], &s[j * u], u) |
| 16 | + for (int j = 0, X = x, Y = y; j < v && Y + j < h - X / w && Y >= 0 && X >= 0; ++j) \ |
| 17 | + memcpy(&f[Y + j][X], &s[j * u], u) |
18 | 18 | #define l(x, y, w, h, a, b, c, d) \ |
19 | | - !((x - a > c && x >= a) || (a - x > w && a >= x) || (y - b > d && y >= b) || (b - y > h && b >= y)) |
| 19 | + !((x - a > c && x >= a) || (a - x > w && a >= x) || (y - b > d && y >= b) || (b - y > h && b >= y)) |
20 | 20 | #define f(x, y, z) \ |
21 | | - y += z * d; \ |
22 | | - strcpy(b, x); \ |
23 | | - break |
| 21 | + y += z * d; \ |
| 22 | + strcpy(b, x); \ |
| 23 | + break |
24 | 24 |
|
25 | 25 | int main() |
26 | 26 | { |
27 | | - struct termios z, o; |
28 | | - tcgetattr(0, &z); |
29 | | - o = z; |
30 | | - z.c_lflag &= ~ICANON & ~ECHO; |
31 | | - z.c_cc[VMIN] = 0; |
32 | | - tcsetattr(0, TCSANOW, &z); |
33 | | - struct winsize v; |
34 | | - ioctl(STDOUT_FILENO, TIOCGWINSZ, &v); |
35 | | - int h = v.ws_row, w = v.ws_col, A = w * h / 100, l = t(), g = 1, i, c = 0, L; |
36 | | - struct V |
37 | | - { |
38 | | - float x, y; |
39 | | - } p = {w / 2, h / 2}, a[A], m[A]; |
40 | | - char u = 0, f[h + 1][w], b[13] = " /\\ / \\ vv "; |
41 | | - while (1) |
42 | | - { |
43 | | - float d = (t() - l) * .001; |
44 | | - read(0, &u, 1); |
45 | | - l = t(); |
46 | | - c += 15; |
47 | | - i = h * w - 1; |
48 | | - K: |
49 | | - L = c * d; |
50 | | - L = abs(((i / w) - L) * (i % w) + h * w); |
51 | | - (*f)[i] = L % 3 + L % 5 + L % 7 + L % 11 + L % 13 + L % 17 + L % 19 > 14 ? 32 : 46; |
52 | | - if (i--) |
53 | | - goto K; |
54 | | - switch (u) // could be minified... |
55 | | - { |
56 | | - case 65: |
57 | | - f(" /\\ / \\ vv ", p.y, -20); |
58 | | - case 66: |
59 | | - f(" ^^ \\ / \\/ ", p.y, 20); |
60 | | - case 67: |
61 | | - f("<\\ < ></ ", p.x, 40); |
62 | | - case 68: |
63 | | - f(" />< > \\>", p.x, -40); |
64 | | - } |
65 | | - p.x = p.x < 4 ? 4 : p.x >= w - 4 ? w - 4 |
66 | | - : p.x; |
67 | | - p.y = p.y < 1 ? 1 : p.y >= h - 3 ? h - 3 |
68 | | - : p.y; |
69 | | - i = A - 1; |
70 | | - L: |
71 | | - *(f[h]) = 0; |
72 | | - struct V *e = &a[i], *z = &m[i]; |
73 | | - e->x += d * z->x; |
74 | | - e->y += d * z->y; |
75 | | - e->x < 0 - 3 || e->x >= w + 3 || e->y >= h + 2 || g ? e->y = -rand() % h * (1 + g), |
76 | | - e->x = rand() % w, |
77 | | - z->x = -8 + rand() % 15, |
78 | | - z->y = 10 + rand() % 5 : 0; |
79 | | - if (l(p.x, p.y, 4, 3, e->x, e->y, 3, 2)) |
80 | | - { |
81 | | - tcsetattr(0, TCSADRAIN, &o); |
82 | | - exit(0); |
83 | | - }; |
84 | | - g(e->x, e->y, 3, 2, "OOOOOO"); |
85 | | - if (i--) |
86 | | - goto L; |
87 | | - g(p.x, p.y, 4, 3, b); |
88 | | - printf("\33[0;4H%s", &f[0][4]); |
89 | | - while (t() - l < 9) |
90 | | - ; |
91 | | - g = 0; |
92 | | - } |
| 27 | + struct termios z, o; |
| 28 | + tcgetattr(0, &z); |
| 29 | + o = z; |
| 30 | + z.c_lflag &= ~ICANON & ~ECHO; |
| 31 | + z.c_cc[VMIN] = 0; |
| 32 | + tcsetattr(0, TCSANOW, &z); |
| 33 | + struct winsize v; |
| 34 | + ioctl(1, TIOCGWINSZ, &v); |
| 35 | + int h = v.ws_row, w = v.ws_col, A = w * h / 100, l = t(), g = 1, i, c = 0, L; |
| 36 | + struct V |
| 37 | + { |
| 38 | + float x, y; |
| 39 | + } p = {w / 2, h / 2}, a[A], m[A]; |
| 40 | + char u = 0, f[h + 1][w], b[13] = " /\\ / \\ vv "; |
| 41 | + while (1) |
| 42 | + { |
| 43 | + float d = (t() - l) * .001; |
| 44 | + read(0, &u, 1); |
| 45 | + l = t(); |
| 46 | + c += 15; |
| 47 | + i = h * w - 1; |
| 48 | + K: |
| 49 | + L = c * d; |
| 50 | + L = abs(((i / w) - L) * (i % w) + h * w); |
| 51 | + (*f)[i] = L % 3 + L % 5 + L % 7 + L % 11 + L % 13 + L % 17 + L % 19 > 14 ? 32 : 46; |
| 52 | + if (i--) |
| 53 | + goto K; |
| 54 | + switch (u) // could be minified... |
| 55 | + { |
| 56 | + case 65: |
| 57 | + f(" /\\ / \\ vv ", p.y, -20); |
| 58 | + case 66: |
| 59 | + f(" ^^ \\ / \\/ ", p.y, 20); |
| 60 | + case 67: |
| 61 | + f("<\\ < ></ ", p.x, 40); |
| 62 | + case 68: |
| 63 | + f(" />< > \\>", p.x, -40); |
| 64 | + } |
| 65 | + p.x = p.x < 4 ? 4 : p.x >= w - 4 ? w - 4 |
| 66 | + : p.x; |
| 67 | + p.y = p.y < 1 ? 1 : p.y >= h - 3 ? h - 3 |
| 68 | + : p.y; |
| 69 | + i = A - 1; |
| 70 | + L: |
| 71 | + *(f[h]) = 0; |
| 72 | + struct V *e = &a[i], *z = &m[i]; |
| 73 | + e->x += d * z->x; |
| 74 | + e->y += d * z->y; |
| 75 | + e->x < 0 - 3 || e->x >= w + 3 || e->y >= h + 2 || g ? e->y = -rand() % h * (1 + g), |
| 76 | + e->x = rand() % w, |
| 77 | + z->x = -8 + rand() % 15, |
| 78 | + z->y = 10 + rand() % 5 : 0; |
| 79 | + if (l(p.x, p.y, 4, 3, e->x, e->y, 3, 2)) |
| 80 | + { |
| 81 | + tcsetattr(0, TCSADRAIN, &o); |
| 82 | + exit(0); |
| 83 | + }; |
| 84 | + g(e->x, e->y, 3, 2, "OOOOOO"); |
| 85 | + if (i--) |
| 86 | + goto L; |
| 87 | + g(p.x, p.y, 4, 3, b); |
| 88 | + printf("\33[0;4H%s", &f[0][4]); |
| 89 | + while (t() - l < 9) |
| 90 | + ; |
| 91 | + g = 0; |
| 92 | + } |
93 | 93 | } |
0 commit comments