2022-10-17 01:06:46 +00:00
|
|
|
#define TB_IMPL
|
|
|
|
|
|
|
|
#include "termbox.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2022-10-20 21:29:27 +00:00
|
|
|
|
2023-01-14 03:32:11 +00:00
|
|
|
#define MIN_NBALLS 5
|
|
|
|
#define MAX_NBALLS 20
|
|
|
|
|
2022-10-17 13:00:38 +00:00
|
|
|
typedef struct {
|
2022-10-17 01:06:46 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
int dx;
|
|
|
|
int dy;
|
|
|
|
} Ball;
|
2022-10-20 21:29:27 +00:00
|
|
|
|
2022-10-17 13:00:38 +00:00
|
|
|
static char *custom = NULL;
|
2023-01-14 04:20:46 +00:00
|
|
|
static char *custom2 = NULL;
|
2022-10-17 01:06:46 +00:00
|
|
|
static short color = TB_DEFAULT;
|
2022-11-11 02:04:58 +00:00
|
|
|
static short color2 = TB_DEFAULT;
|
2022-10-17 01:06:46 +00:00
|
|
|
static int nballs = 10;
|
2022-11-27 04:22:57 +00:00
|
|
|
static short speedMult = 5;
|
2022-10-17 01:06:46 +00:00
|
|
|
static short rim = 0;
|
2022-10-20 21:29:27 +00:00
|
|
|
static short contained = 0;
|
2023-01-14 04:20:46 +00:00
|
|
|
static float radiusIn = 100;
|
|
|
|
static float radius;
|
|
|
|
static int margin;
|
|
|
|
static float sumConst;
|
|
|
|
static float sumConst2;
|
2023-01-14 03:32:11 +00:00
|
|
|
static int maxX,maxY;
|
|
|
|
static int speed;
|
2023-01-14 04:20:46 +00:00
|
|
|
static Ball balls[MAX_NBALLS] = {0};
|
|
|
|
static struct tb_event event = {0};
|
2022-10-20 21:29:27 +00:00
|
|
|
|
2023-01-14 04:20:46 +00:00
|
|
|
void init_params();
|
|
|
|
void event_handler();
|
2022-10-17 01:06:46 +00:00
|
|
|
int parse_options(int argc, char *argv[]);
|
|
|
|
void print_help();
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
if (!parse_options(argc, argv))
|
|
|
|
return 0;
|
|
|
|
|
2022-10-21 03:48:00 +00:00
|
|
|
if (color == TB_DEFAULT && !custom)
|
2022-10-17 01:06:46 +00:00
|
|
|
rim = 0;
|
|
|
|
|
|
|
|
time_t t;
|
2023-01-14 03:32:11 +00:00
|
|
|
//Ball *balls = malloc(sizeof(Ball) * nballs);
|
2022-10-17 01:06:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
srand((unsigned)time(&t));
|
|
|
|
|
|
|
|
tb_init();
|
|
|
|
|
|
|
|
tb_hide_cursor();
|
|
|
|
|
2023-01-14 04:20:46 +00:00
|
|
|
init_params();
|
2022-10-17 01:06:46 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
|
|
|
// move balls
|
|
|
|
for (int i = 0; i < nballs; i++) {
|
|
|
|
|
2022-10-20 21:29:27 +00:00
|
|
|
if (balls[i].x + balls[i].dx >= maxX - margin ||
|
|
|
|
balls[i].x + balls[i].dx < margin)
|
2022-10-17 01:06:46 +00:00
|
|
|
balls[i].dx *= -1;
|
|
|
|
|
2022-10-20 21:29:27 +00:00
|
|
|
if (balls[i].y + balls[i].dy >= maxY - margin ||
|
|
|
|
balls[i].y + balls[i].dy < margin)
|
2022-10-17 01:06:46 +00:00
|
|
|
balls[i].dy *= -1;
|
|
|
|
|
|
|
|
balls[i].x += balls[i].dx;
|
|
|
|
balls[i].y += balls[i].dy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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};
|
|
|
|
|
2022-10-21 16:51:54 +00:00
|
|
|
for (int j2 = 0; j2 < (!custom ? 2 : 1); j2++) {
|
2022-10-17 01:06:46 +00:00
|
|
|
|
|
|
|
for (int k = 0; k < nballs; k++) {
|
|
|
|
int y = j * 2 + j2;
|
2022-10-20 21:29:27 +00:00
|
|
|
sum[j2] += (radius * radius) /
|
|
|
|
((float)((i - balls[k].x) * (i - balls[k].x) +
|
|
|
|
(y - balls[k].y) * (y - balls[k].y)));
|
2022-10-17 01:06:46 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-20 21:29:27 +00:00
|
|
|
|
2022-10-17 16:04:09 +00:00
|
|
|
if (!custom) {
|
2022-10-20 21:29:27 +00:00
|
|
|
if (sum[0] > sumConst) {
|
|
|
|
if (sum[1] > sumConst) {
|
2022-11-11 00:08:28 +00:00
|
|
|
tb_printf(i, j, color2, 0, "█");
|
2022-10-17 13:00:38 +00:00
|
|
|
} else {
|
2022-11-11 00:08:28 +00:00
|
|
|
tb_printf(i, j, color2, 0, "▀");
|
2022-10-17 13:00:38 +00:00
|
|
|
}
|
2022-10-20 21:29:27 +00:00
|
|
|
} else if (sum[1] > sumConst) {
|
2022-11-11 00:08:28 +00:00
|
|
|
tb_printf(i, j, color2, 0, "▄");
|
2022-10-17 13:00:38 +00:00
|
|
|
}
|
2022-10-17 01:06:46 +00:00
|
|
|
|
2022-10-17 13:00:38 +00:00
|
|
|
if (rim) {
|
2022-10-20 21:29:27 +00:00
|
|
|
if (sum[0] > sumConst2) {
|
|
|
|
if (sum[1] > sumConst2) {
|
2022-10-17 13:00:38 +00:00
|
|
|
tb_printf(i, j, color, 0, "█");
|
|
|
|
} else {
|
2022-11-11 00:08:28 +00:00
|
|
|
tb_printf(i, j, color2, color, "▄");
|
2022-10-17 13:00:38 +00:00
|
|
|
}
|
2022-10-20 21:29:27 +00:00
|
|
|
} else if (sum[1] > sumConst2) {
|
2022-11-11 00:08:28 +00:00
|
|
|
tb_printf(i, j, color2, color, "▀");
|
2022-10-17 13:00:38 +00:00
|
|
|
}
|
2022-10-17 01:06:46 +00:00
|
|
|
}
|
2022-10-17 13:17:02 +00:00
|
|
|
} else {
|
2022-10-20 21:29:27 +00:00
|
|
|
if (sum[0] > sumConst) {
|
2022-11-11 00:08:28 +00:00
|
|
|
tb_printf(i, j, color2, 0, custom2);
|
2022-10-17 13:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rim) {
|
2022-10-20 21:29:27 +00:00
|
|
|
if (sum[0] > sumConst2) {
|
2022-10-17 16:10:04 +00:00
|
|
|
tb_printf(i, j, color, 0, custom);
|
2022-10-17 01:06:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tb_present();
|
|
|
|
usleep(speed);
|
|
|
|
tb_clear();
|
|
|
|
|
|
|
|
tb_peek_event(&event, 10);
|
2023-01-14 03:32:11 +00:00
|
|
|
|
2023-01-14 04:20:46 +00:00
|
|
|
event_handler();
|
2023-01-14 03:32:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
tb_shutdown();
|
|
|
|
|
|
|
|
//free(balls);
|
|
|
|
}
|
|
|
|
|
2023-01-14 04:20:46 +00:00
|
|
|
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){
|
2023-01-14 04:54:38 +00:00
|
|
|
|
|
|
|
if (event.key == TB_KEY_CTRL_C || event.key == TB_KEY_ESC){
|
|
|
|
tb_shutdown();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2022-11-27 05:28:40 +00:00
|
|
|
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;
|
2023-01-14 03:32:11 +00:00
|
|
|
case 'm':
|
|
|
|
case 'M':
|
|
|
|
if(nballs+1<=MAX_NBALLS){
|
|
|
|
nballs++;
|
|
|
|
}
|
2022-10-17 01:06:46 +00:00
|
|
|
break;
|
2023-01-14 03:32:11 +00:00
|
|
|
case 'l':
|
|
|
|
case 'L':
|
|
|
|
if(nballs-1>=MIN_NBALLS){
|
|
|
|
nballs--;
|
|
|
|
}
|
|
|
|
break;
|
2023-01-14 04:54:38 +00:00
|
|
|
case 'i':
|
|
|
|
case 'I':
|
|
|
|
if(radiusIn+10<=150){
|
|
|
|
radiusIn+=10;
|
|
|
|
radius = (radiusIn * radiusIn + (float)(maxX * maxY)) / 15000;
|
|
|
|
margin = contained ? radius * 10 : 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
case 'D':
|
|
|
|
if(radiusIn-10>=50){
|
|
|
|
radiusIn-=10;
|
|
|
|
radius = (radiusIn * radiusIn + (float)(maxX * maxY)) / 15000;
|
|
|
|
margin = contained ? radius * 10 : 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
tb_shutdown();
|
|
|
|
exit(0);
|
|
|
|
break;
|
2023-01-14 03:32:11 +00:00
|
|
|
}
|
2023-01-14 04:20:46 +00:00
|
|
|
}
|
2022-10-17 01:06:46 +00:00
|
|
|
}
|
|
|
|
|
2023-01-14 03:32:11 +00:00
|
|
|
|
2023-01-14 04:20:46 +00:00
|
|
|
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_DEFAULT || !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 = rand() % (maxX - 2 * margin) + margin;
|
|
|
|
balls[i].y = rand() % (maxY - 2 * margin) + margin;
|
|
|
|
balls[i].dx = (rand() % 2 == 0) ? -1 : 1;
|
|
|
|
balls[i].dy = (rand() % 2 == 0) ? -1 : 1;
|
|
|
|
}
|
|
|
|
}
|
2023-01-14 03:32:11 +00:00
|
|
|
|
2022-11-11 00:08:28 +00:00
|
|
|
int set_color(short *var, char *optarg) {
|
|
|
|
|
|
|
|
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 {
|
|
|
|
printf("Unknown color: %s\n", optarg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-10-17 01:06:46 +00:00
|
|
|
int parse_options(int argc, char *argv[]) {
|
|
|
|
if (argc == 1)
|
|
|
|
return 1;
|
|
|
|
int c;
|
2022-11-11 00:08:28 +00:00
|
|
|
while ((c = getopt(argc, argv, ":c:k:s:r:R:b:F:Ch")) != -1) {
|
2022-10-17 01:06:46 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
2022-11-11 00:08:28 +00:00
|
|
|
if (!set_color(&color, optarg))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
if (!set_color(&color2, optarg))
|
2022-10-17 01:06:46 +00:00
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
speedMult = atoi(optarg);
|
2022-11-27 04:22:57 +00:00
|
|
|
if (speedMult > 10 || speedMult <= 0) {
|
|
|
|
printf("Invalid speed, only values between 1 and 10 are allowed\n");
|
2022-10-17 01:06:46 +00:00
|
|
|
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':
|
2023-01-14 04:20:46 +00:00
|
|
|
radiusIn = 50 + atoi(optarg) * 10;
|
|
|
|
if (radiusIn > 150 || radiusIn < 50) {
|
2022-10-17 01:06:46 +00:00
|
|
|
printf("Invalid radius, only values between 1 and 10 are allowed\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
nballs = atoi(optarg);
|
2023-01-14 03:32:11 +00:00
|
|
|
if (nballs > MAX_NBALLS || nballs < MIN_NBALLS) {
|
2022-10-17 01:06:46 +00:00
|
|
|
|
2023-01-14 03:32:11 +00:00
|
|
|
printf("Invalid number of metaballs, only values between %i and %i are"
|
|
|
|
"allowed\n", MIN_NBALLS,MAX_NBALLS);
|
2022-10-17 01:06:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2022-10-17 13:00:38 +00:00
|
|
|
case 'F':
|
|
|
|
custom = optarg;
|
|
|
|
break;
|
2022-10-20 21:29:27 +00:00
|
|
|
case 'C':
|
|
|
|
contained = 1;
|
|
|
|
break;
|
2022-10-17 01:06:46 +00:00
|
|
|
case 'h':
|
|
|
|
print_help();
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case ':': /* -f or -o without operand */
|
|
|
|
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(
|
2022-10-18 03:55:02 +00:00
|
|
|
"Usage: lavat [OPTIONS]\n"
|
2022-10-17 01:06:46 +00:00
|
|
|
"OPTIONS:\n"
|
2022-10-20 21:29:27 +00:00
|
|
|
" -c <COLOR> Set color. Available colours: red, blue, yellow, "
|
2022-10-17 01:06:46 +00:00
|
|
|
"green, cyan and magenta. \n"
|
2022-10-20 21:29:27 +00:00
|
|
|
" Besides those colors the default one is the normal"
|
|
|
|
" foreground of your terminal.\n"
|
2022-11-27 04:22:57 +00:00
|
|
|
" -s <SPEED> Set the speed, from 1 to 10. (default 5)\n"
|
2022-11-27 05:28:40 +00:00
|
|
|
" Change the speed while the program runs "
|
|
|
|
"with the - and + keys.\n"
|
2022-10-20 21:29:27 +00:00
|
|
|
" -r <RADIUS> Set the radius of the metaballs, from 1 to 10. "
|
2022-10-17 01:06:46 +00:00
|
|
|
"(default: 5)\n"
|
2023-01-14 04:54:38 +00:00
|
|
|
" Increase and decrease the radius in runtime with"
|
|
|
|
"the i and d keys.\n"
|
2022-10-20 21:29:27 +00:00
|
|
|
" -R <RIM> Set a rim for each metaball, sizes from 1 to 5."
|
2022-10-17 01:06:46 +00:00
|
|
|
"(default: none)\n"
|
2022-11-11 04:24:11 +00:00
|
|
|
" 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"
|
2022-11-11 00:08:28 +00:00
|
|
|
" -k <COLOR> Set the color of the rim if there is one."
|
|
|
|
" Available colours: red, blue, yellow, green, cyan and magenta. \n"
|
2022-10-20 21:29:27 +00:00
|
|
|
" -b <NBALLS> Set the number of metaballs in the simulation, "
|
2023-01-14 03:32:11 +00:00
|
|
|
"from %i to %i. (default: 10)\n"
|
|
|
|
" Increase or decrease the number of balls in run"
|
|
|
|
|
|
|
|
"time with the m and l keys.\n"
|
2022-10-20 21:29:27 +00:00
|
|
|
" -F <CHARS> Allows for a custom set of chars to be used\n"
|
|
|
|
" Only ascii symbols are supported for now, "
|
2022-10-18 22:45:59 +00:00
|
|
|
"wide/unicode chars may appear broken.\n"
|
2022-10-21 22:53:39 +00:00
|
|
|
" -C Retain the entire lava inside the terminal.\n"
|
|
|
|
" It may not work well with a lot of balls or with"
|
2022-10-21 22:57:10 +00:00
|
|
|
" a bigger radius than the default one.\n"
|
2022-10-21 00:48:19 +00:00
|
|
|
" -h Print help.\n"
|
|
|
|
"(Tip: Zoom out in your terminal before running the program to get a "
|
2023-01-14 03:32:11 +00:00
|
|
|
"better resolution of the lava).\n",MIN_NBALLS,MAX_NBALLS);
|
2022-10-17 01:06:46 +00:00
|
|
|
}
|