-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlavat.c
More file actions
654 lines (584 loc) · 18 KB
/
lavat.c
File metadata and controls
654 lines (584 loc) · 18 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
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
654
#define TB_IMPL
#define TB_LIB_OPTS
#define TB_OPT_TRUECOLOR
#include "termbox.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <float.h>
#include <math.h>
#define MIN_NBALLS 5
#define MAX_NBALLS 20
typedef struct {
float x;
float y;
float vx;
float vy;
float heat;
int restTicks;
} Ball;
uintattr_t pallete[11];
int baseColor[3];
int baseColor2[3];
short gradient1=0;
short gradient2=0;
static char *custom = NULL;
static char *custom2 = NULL;
static short color = TB_WHITE;
static short color2 = TB_WHITE;
static short party = 0;
static int nballs = 10;
static short speedMult = 5;
static short rim = 1;
static short contained = 0;
static short gravityMode = 0;
static float gravityStrength = 0.12f;
static float buoyancyStrength = 0.22f;
static float heatGain = 0.035f;
static float heatLoss = 0.0045f;
static float bounceDamp = 0.85f;
static float radiusIn = 110;
static float radius;
static int margin;
static float sumConst;
static float sumConst2;
static int maxX, maxY;
static int speed;
static Ball balls[MAX_NBALLS] = {0};
static struct tb_event event = {0};
static short colors[]={TB_WHITE, TB_RED, TB_YELLOW, TB_BLUE, TB_GREEN, TB_MAGENTA, TB_CYAN, TB_BLACK };
void init_params();
void event_handler();
int parse_options(int argc, char *argv[]);
void print_help();
short next_color(short current);
void fix_rim_color();
void set_random_colors(short level);
void set_pallete();
uintattr_t get_color(float val);
void set_pallete2();
int set_color(short *var, int *baseColor, char *optarg, short useGradient) {
if (useGradient) {
// Parse hex color
if (sscanf(optarg, "%02x%02x%02x", &baseColor[0], &baseColor[1], &baseColor[2]) == 3) {
return 1;
} else {
printf("Invalid hex color format. Use format: RRGGBB\n");
return 0;
}
} else {
// Parse named color
if (strcmp(optarg, "red") == 0) {
*var = TB_RED;
} else if (strcmp(optarg, "yellow") == 0) {
*var = TB_YELLOW;
} else if (strcmp(optarg, "blue") == 0) {
*var = TB_BLUE;
} else if (strcmp(optarg, "green") == 0) {
*var = TB_GREEN;
} else if (strcmp(optarg, "magenta") == 0) {
*var = TB_MAGENTA;
} else if (strcmp(optarg, "cyan") == 0) {
*var = TB_CYAN;
} else if (strcmp(optarg, "black") == 0) {
*var = TB_BLACK;
} else if (strcmp(optarg, "white") == 0) {
*var = TB_WHITE;
} else {
printf("Unknown color: %s\n", optarg);
return 0;
}
return 1;
}
}
int main(int argc, char *argv[]) {
if (!parse_options(argc, argv))
return 0;
time_t t;
// Ball *balls = malloc(sizeof(Ball) * nballs);
srand((unsigned)time(&t));
tb_init();
tb_hide_cursor();
init_params();
while (1) {
// move balls
for (int i = 0; i < nballs; i++) {
if (gravityMode) {
float minY = (float)margin;
float maxYf = (float)(maxY - margin - 1);
float topZone = minY + 6.0f;
float bottomZone = maxYf - 6.0f;
if (balls[i].restTicks > 0) {
balls[i].restTicks--;
balls[i].vy = 0.0f;
balls[i].heat -= heatLoss * 2.0f;
} else {
if (balls[i].y > bottomZone) {
balls[i].heat += heatGain;
} else {
balls[i].heat -= heatLoss;
}
if (balls[i].y < topZone) {
balls[i].heat -= heatLoss * 1.5f;
}
balls[i].vy += gravityStrength - buoyancyStrength * balls[i].heat;
}
if (balls[i].heat < 0.0f)
balls[i].heat = 0.0f;
if (balls[i].heat > 1.0f)
balls[i].heat = 1.0f;
// Drag helps create the "hang time" at the top.
balls[i].vx *= 0.995f;
balls[i].vy *= 0.997f;
}
float nextX = balls[i].x + balls[i].vx;
float nextY = balls[i].y + balls[i].vy;
float minX = (float)margin;
float maxXf = (float)(maxX - margin - 1);
float minY = (float)margin;
float maxYf = (float)(maxY - margin - 1);
if (balls[i].vy > 2.5f)
balls[i].vy = 2.5f;
if (balls[i].vy < -2.5f)
balls[i].vy = -2.5f;
if (nextX > maxXf) {
balls[i].x = maxXf;
balls[i].vx = -balls[i].vx;
if (gravityMode) {
balls[i].vx *= bounceDamp;
}
} else if (nextX < minX) {
balls[i].x = minX;
balls[i].vx = -balls[i].vx;
if (gravityMode) {
balls[i].vx *= bounceDamp;
}
} else {
balls[i].x = nextX;
}
if (nextY > maxYf) {
balls[i].y = maxYf;
// With gravity mode enabled, blobs should "sit" at the bottom and
// get heated (buoyant) rather than bounce.
if (gravityMode) {
// Only enter a "rest" period on a real impact; otherwise keep
// sticking to the floor and allow heat to accumulate.
if (balls[i].restTicks == 0 && balls[i].vy > 0.4f) {
balls[i].heat = 0.0f;
balls[i].restTicks = 15 + (rand() % 45);
}
balls[i].vy = 0.0f;
} else {
balls[i].vy = -fabsf(balls[i].vy);
}
} else if (nextY < minY) {
balls[i].y = minY;
// With gravity mode enabled, blobs can linger at the top until they cool.
if (gravityMode) {
balls[i].vy = 0.0f;
} else {
balls[i].vy = fabsf(balls[i].vy);
}
} else {
balls[i].y = nextY;
}
// Keep some motion even with damping.
if (fabsf(balls[i].vx) < 0.15f) {
balls[i].vx = (balls[i].vx < 0 ? -0.15f : 0.15f);
}
}
// render
for (int i = 0; i < maxX; i++) {
for (int j = 0; j < maxY / 2; j++) {
// calculate the two halfs of the block at the same time
float sum[2] = {0};
for (int j2 = 0; j2 < (!custom ? 2 : 1); j2++) {
for (int k = 0; k < nballs; k++) {
int y = j * 2 + j2;
float dx = (float)i - balls[k].x;
float dy = (float)y - balls[k].y;
float dist_squared = dx * dx + dy * dy;
if (dist_squared == 0) {
sum[j2] += FLT_MAX;
} else {
sum[j2] += (radius * radius) / dist_squared;
}
}
}
if (!custom) {
if(gradient1){
if (sum[0] > sumConst) {
if (sum[1] > sumConst) {
tb_printf(i, j, get_color( sum[0]), get_color(sum[1]), "▀");
} else {
tb_printf(i, j, get_color( sum[0]), TB_TRUECOLOR_DEFAULT, "▀");
}
} else if (sum[1] > sumConst) {
tb_printf(i, j,get_color( sum[1]),TB_TRUECOLOR_DEFAULT, "▄");
}
}else{
if (sum[0] > sumConst) {
if (sum[1] > sumConst) {
tb_printf(i, j, color2, 0, "█");
} else {
tb_printf(i, j, color2, 0, "▀");
}
} else if (sum[1] > sumConst) {
tb_printf(i, j, color2, 0, "▄");
}
if (rim) {
if (sum[0] > sumConst2) {
if (sum[1] > sumConst2) {
tb_printf(i, j, color, 0, "█");
} else {
tb_printf(i, j, color2, color, "▄");
}
} else if (sum[1] > sumConst2) {
tb_printf(i, j, color2, color, "▀");
}
}
}
} else {
if (sum[0] > sumConst) {
tb_printf(i, j, color2, 0, custom2);
}
if (rim) {
if (sum[0] > sumConst2) {
tb_printf(i, j, color, 0, custom);
}
}
}
}
}
if (party>0){
set_random_colors(party);
}
tb_present();
usleep(speed);
tb_clear();
tb_peek_event(&event, 10);
event_handler();
}
tb_shutdown();
// free(balls);
}
void event_handler() {
if (event.type == TB_EVENT_RESIZE) {
do
tb_peek_event(&event, 10);
while (event.type == TB_EVENT_RESIZE);
init_params();
} else if (event.type == TB_EVENT_KEY) {
if (event.key == TB_KEY_CTRL_C || event.key == TB_KEY_ESC) {
tb_shutdown();
exit(0);
}
switch (event.ch) {
case '-':
case '_':
if (speedMult < 10) {
speedMult++;
speed = (((1 / (float)(maxX + maxY)) * 1000000) + 10000) * speedMult;
}
break;
case '+':
case '=':
if (speedMult > 1) {
speedMult--;
speed = (((1 / (float)(maxX + maxY)) * 1000000) + 10000) * speedMult;
}
break;
case 'm':
case 'M':
if (nballs + 1 <= MAX_NBALLS) {
nballs++;
}
break;
case 'l':
case 'L':
if (nballs - 1 >= MIN_NBALLS) {
nballs--;
}
break;
case 'i':
if (radiusIn + 5 <= 150) {
radiusIn += 5;
radius = (radiusIn * radiusIn + (float)(maxX * maxY)) / 15000;
margin = contained ? radius * 10 : 0;
}
break;
case 'd':
if (radiusIn - 5 >= 100) {
radiusIn -= 5;
radius = (radiusIn * radiusIn + (float)(maxX * maxY)) / 15000;
margin = contained ? radius * 10 : 0;
}
break;
case 'I':
if (color != TB_WHITE || custom || gradient1 )
if (rim + 1 <= 5) {
rim++;
sumConst2 = sumConst * (1 + (float)(0.25 * rim));
}
break;
case 'D':
if (color != TB_WHITE || custom || gradient1)
if (rim - 1 >= 0) {
rim--;
sumConst2 = sumConst * (1 + (float)(0.25 * rim));
}
break;
case 'c':
color = next_color(color);
fix_rim_color();
break;
case 'k':
color2 = next_color(color2);
fix_rim_color();
break;
case 'p':
party = (party+1)%4;
break;
case 'q':
case 'Q':
tb_shutdown();
exit(0);
break;
}
}
}
void init_params() {
maxX = tb_width();
maxY = tb_height() * 2;
speedMult = 11 - speedMult;
speed = (((1 / (float)(maxX + maxY)) * 1000000) + 10000) * speedMult;
radius = (radiusIn * radiusIn + (float)(maxX * maxY)) / 15000;
margin = contained ? radius * 10 : 0;
sumConst = 0.0225;
sumConst2 = sumConst * (1 + (float)(0.25 * rim));
custom2 = custom;
if (color2 == TB_WHITE || !rim)
color2 = color | TB_BOLD;
if (custom && strlen(custom) > 1 && rim) {
custom2 = custom + 1;
}
for (int i = 0; i < MAX_NBALLS; i++) {
balls[i].x = (float)(rand() % (maxX - 2 * margin) + margin);
balls[i].y = (float)(rand() % (maxY - 2 * margin) + margin);
balls[i].vx = (rand() % 2 == 0) ? -1.0f : 1.0f;
balls[i].vy = (rand() % 2 == 0) ? -1.0f : 1.0f;
balls[i].heat = (float)(rand() % 101) / 100.0f;
balls[i].restTicks = rand() % 30;
}
if(gradient1){
tb_set_output_mode(TB_OUTPUT_TRUECOLOR);
tb_set_clear_attrs(TB_TRUECOLOR_DEFAULT, TB_TRUECOLOR_DEFAULT);
if(gradient2) set_pallete2();
else set_pallete();
}
}
short next_color(short current){
for(int i = 0; i<8; i++){
if((current == colors[i])||(current == (colors[i] | TB_BOLD ))){
return colors[(i+1)%8];
}
}
return colors[0];
}
void fix_rim_color(){
if(color2 == color){
color2 = color2 | TB_BOLD;
}
}
void set_random_colors( short level){
if(level==1 || level==3) color = colors[ rand() % 7];
if(level==2 || level==3) color2 = colors[ rand() % 7];
fix_rim_color();
}
int parse_options(int argc, char *argv[]) {
if (argc == 1)
return 1;
int c;
// First pass to check for gradient mode
optind = 1; // Reset getopt
while ((c = getopt(argc, argv, ":c:k:s:r:R:b:F:Cp:hgG")) != -1) {
if (c == 'g') {
gradient1 = 1;
if (!tb_has_truecolor()) {
fprintf(stderr, "The terminal does not support truecolor\n");
return 0;
}
break;
}
}
// Reset getopt for second pass
optind = 1;
while ((c = getopt(argc, argv, ":c:k:s:r:R:b:F:Cp:hgG")) != -1) {
switch (c) {
case 'c':
if (!set_color(&color, baseColor, optarg, gradient1))
return 0;
break;
case 'k':
if (!set_color(&color2, baseColor2, optarg, gradient1))
return 0;
gradient2 = gradient1; // If we're using gradient, enable the second gradient too
break;
case 's':
speedMult = atoi(optarg);
if (speedMult > 10 || speedMult <= 0) {
printf("Invalid speed, only values between 1 and 10 are allowed\n");
return 0;
}
break;
case 'R':
rim = atoi(optarg);
if (rim > 5 || rim < 1) {
printf("Invalid rim, only values between 1 and 5 are allowed\n");
return 0;
}
break;
case 'r':
radiusIn = 100 + atoi(optarg) * 5;
if (radiusIn > 150 || radiusIn < 100) {
printf("Invalid radius, only values between 1 and 10 are allowed\n");
return 0;
}
break;
case 'b':
nballs = atoi(optarg);
if (nballs > MAX_NBALLS || nballs < MIN_NBALLS) {
printf("Invalid number of metaballs, only values between %i and %i are "
"allowed\n",
MIN_NBALLS, MAX_NBALLS);
return 0;
}
break;
case 'F':
custom = optarg;
break;
case 'C':
contained = 1;
break;
case 'G':
gravityMode = 1;
break;
case 'p':
party = atoi(optarg);
if (party < 0 || party > 3) {
printf("Invalid party mode, only values between 1 and 3 are allowed\n");
return 0;
}
break;
case 'h':
print_help();
return 0;
break;
case 'g':
// Already handled in first pass
break;
case ':':
fprintf(stderr, "Option -%c requires an operand\n", optopt);
return 0;
break;
case '?':
fprintf(stderr, "Unrecognized option: -%c\n", optopt);
return 0;
}
}
return 1;
}
void print_help() {
printf(
"Usage: lavat [OPTIONS]\n"
"OPTIONS:\n"
" -g Enable gradient mode with truecolor support.\n"
" Changes how -c and -k options work.\n"
" -G Enable gravity and buoyancy movement (balls heat up at the bottom, rise, cool at the top, then fall).\n"
" -c <COLOR> Set color. In normal mode, available colors are: red, blue, yellow, "
"green, cyan, magenta, white, and black.\n"
" In gradient mode (-g), use hex format: RRGGBB (e.g., FF0000 for red).\n"
" -k <COLOR> Set the rim color. Same format options as -c.\n"
" In gradient mode, this sets the second color for the gradient.\n"
" -s <SPEED> Set the speed, from 1 to 10. (default 5)\n"
" -r <RADIUS> Set the radius of the metaballs, from 1 to 10. "
"(default: 5)\n"
" -R <RIM> Set a rim for each metaball, sizes from 1 to 5."
"(default: none)\n"
" This option does not work with the default "
"color\n"
" If you use Kitty or Alacritty you must use it "
"with the -k option to see the rim.\n"
" -b <NBALLS> Set the number of metaballs in the simulation, "
"from %i to %i. (default: 10)\n"
" -F <CHARS> Allows for a custom set of chars to be used\n"
" Only ascii symbols are supported for now, "
"wide/unicode chars may appear broken.\n"
" -C Retain the entire lava inside the terminal.\n"
" It may not work well with a lot of balls or with"
" a bigger radius than the default one.\n"
" -p <MODE> PARTY!! THREE MODES AVAILABLE (p1, p2 and p3).\n"
" -h Print help.\n"
"RUNTIME CONTROLS:\n"
" i Increase radius of the metaballs.\n"
" d Decrease radius of the metaballs.\n"
" shift i Increase rim of the metaballs.\n"
" shift d Decrease rim of the metaballs.\n"
" m Increase the number of metaballs.\n"
" l Decrease the number metaballs.\n"
" c Change the color of the metaballs.\n"
" k Change the rim color of the metaballs.\n"
" + Increase speed.\n"
" - Decrease speed.\n"
" p TURN ON THE PARTY AND CYCLE THROUGH THE PARTY MODES "
"(it can also turns off the party).\n"
"(Tip: Zoom out in your terminal before running the program to get a "
"better resolution of the lava).\n"
"EXAMPLES:\n"
" lavat -c green -k red Use named colors in normal mode\n"
" lavat -g -c 00FF00 -k FF0000 Use hex colors in gradient mode\n"
" lavat -G Start with gravity mode enabled\n",
MIN_NBALLS, MAX_NBALLS);
}
void set_pallete(){
int avgColor= (baseColor[0] + baseColor[1] + baseColor[2])/3;
int blackfactor[5];
int whitefactor[5];
for(int i=1 ;i<6;i++){
blackfactor[i-1]=(6-i)*avgColor/5;
whitefactor[i-1]=i*(255-avgColor)/5;
}
for(int i=0 ;i<5;i++){
int r, g, b;
float factor = (1 - ((float)blackfactor[i]/(avgColor)));
r = baseColor[0]*factor;
g = baseColor[1]*factor;
b = baseColor[2]*factor;
pallete[i]= (r << 16) | (g << 8) | b;
r = baseColor[0] + (255-baseColor[0])*whitefactor[i]/(255-avgColor);
g = baseColor[1] + (255-baseColor[1])*whitefactor[i]/(255-avgColor);
b = baseColor[2] + (255-baseColor[2])*whitefactor[i]/(255-avgColor);
pallete[i+6] = (r << 16) | (g << 8) | b;
}
pallete[5]= (baseColor[0] << 16) | (baseColor[1] << 8) | baseColor[2];
}
void set_pallete2() {
for (int i = 0; i < 11; i++) {
float t = (float)i / (11 - 1);
int r = (1 - t) * baseColor[0] + t * baseColor2[0];
int g = (1 - t) * baseColor[1] + t * baseColor2[1];
int b = (1 - t) * baseColor[2] + t * baseColor2[2];
pallete[i] = (r << 16) | (g << 8) | b;
}
}
uintattr_t get_color(float val) {
val = (val - sumConst)/(0.001*(rim+1));
if (val > 10) {
return pallete[10];
} else if (val < 0) {
return pallete[0];
}
return pallete[(int)val];
}