aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrumeet <yuuta@yuuta.moe>2021-11-21 18:27:38 -0800
committerTrumeet <yuuta@yuuta.moe>2021-11-21 18:27:38 -0800
commitfd8942c64e2e3085f510fbeecd5a671820fbb256 (patch)
tree88f6ff1c5ff885db04746ce1e737a1951e15d722
parent7b0205a20a6bfcae8ae9090b3a544bdd2c691a05 (diff)
downloadmcal-fd8942c64e2e3085f510fbeecd5a671820fbb256.tar
mcal-fd8942c64e2e3085f510fbeecd5a671820fbb256.tar.gz
mcal-fd8942c64e2e3085f510fbeecd5a671820fbb256.tar.bz2
mcal-fd8942c64e2e3085f510fbeecd5a671820fbb256.zip
refactor(expvc/common): support more data types using inheritance
-rw-r--r--common.h79
-rw-r--r--expvc.c13
2 files changed, 67 insertions, 25 deletions
diff --git a/common.h b/common.h
index 30620fd..ead08c2 100644
--- a/common.h
+++ b/common.h
@@ -6,38 +6,79 @@
#include <assert.h>
#include <math.h>
+enum val_type { VEC3D, BOX };
+
+struct val {
+ enum val_type type;
+};
+
struct vec3d {
+ enum val_type type;
double x;
double y;
double z;
};
-static int vec3d_parse(char *in, struct vec3d *out)
+struct box {
+ enum val_type type;
+ double min_x;
+ double min_y;
+ double min_z;
+ double max_x;
+ double max_y;
+ double max_z;
+};
+
+static inline double *val_get_index(struct val *val, int index)
+{
+ switch (val->type)
+ {
+ case VEC3D:
+ struct vec3d *d_v3d = (struct vec3d *)val;
+ switch (index)
+ {
+ case 0: return &d_v3d->x;
+ case 1: return &d_v3d->y;
+ case 2: return &d_v3d->z;
+ default: assert(0);
+ }
+ case BOX:
+ struct box *v_box = (struct box *)val;
+ switch (index)
+ {
+ case 0: return &v_box->min_x;
+ case 1: return &v_box->min_y;
+ case 2: return &v_box->min_z;
+ case 3: return &v_box->max_x;
+ case 4: return &v_box->max_y;
+ case 5: return &v_box->max_z;
+ default: assert(0);
+ }
+ default:
+ assert(0);
+ }
+}
+
+static int val_parse(char *in, struct val *o)
{
int i = 0;
char *p = strtok(in, ",");
+ int required_vars = 0;
+ switch (o->type)
+ {
+ case VEC3D:
+ required_vars = 3;
+ break;
+ case BOX:
+ required_vars = 6;
+ break;
+ }
while (p != NULL)
{
- double *ptr;
- switch (i ++)
- {
- case 0:
- ptr = &out->x;
- break;
- case 1:
- ptr = &out->y;
- break;
- case 2:
- ptr = &out->z;
- break;
- default:
- assert(0);
- break;
- }
- sscanf(p, "%lf", ptr);
+ sscanf(p, "%lf", val_get_index(o, i++));
p = strtok(NULL, ",");
}
- if (i != 3)
+ if (i != required_vars)
{
fprintf(stderr, "Invalid Vec3D: %s. Example: -1,1,1.\n", in);
return 64;
diff --git a/expvc.c b/expvc.c
index dc7637f..b3aa27b 100644
--- a/expvc.c
+++ b/expvc.c
@@ -8,9 +8,9 @@
int main(int argc, char **argv)
{
- struct vec3d tnt = { 0.0, 0.0, 0.0 };
- struct vec3d entity = { 0.0, 0.0, 0.0 };
- struct vec3d velocity = { 0.0, 0.0, 0.0 };
+ struct vec3d tnt = { VEC3D, 0.0, 0.0, 0.0 };
+ struct vec3d entity = { VEC3D, 0.0, 0.0, 0.0 };
+ struct vec3d velocity = { VEC3D, 0.0, 0.0, 0.0 };
double entity_eye_y = 0.0;
int is_tnt = 1;
double power = 4.0;
@@ -44,9 +44,9 @@ usage:
goto usage;
}
char *val = argv[++i];
- if (!strcmp("-t", arg)) { if ((r = vec3d_parse(val, &tnt))) return r; }
- else if (!strcmp("-e", arg)) { if ((r = vec3d_parse(val, &entity))) return r; }
- else if (!strcmp("-v", arg)) { if ((r = vec3d_parse(val, &velocity))) return r; }
+ if (!strcmp("-t", arg)) { if ((r = val_parse(val, (struct val *)&tnt))) return r; }
+ else if (!strcmp("-e", arg)) { if ((r = val_parse(val, (struct val *)&entity))) return r; }
+ else if (!strcmp("-v", arg)) { if ((r = val_parse(val, (struct val *)&velocity))) return r; }
else if (!strcmp("-ey", arg))
{
sscanf(val, "%lf", &entity_eye_y);
@@ -107,6 +107,7 @@ usage:
printf("DAMAGE=%d\n", damage);
fprintf(stderr, "WARNING: Explosion protection is ignored. In reality, if the entity has that protection, the final acceleration may be altered.\n");
struct vec3d delta_v = {
+ VEC3D,
xx * accel,
yy * accel,
zz * accel