]> git.lizzy.rs Git - nothing.git/blobdiff - src/game/level/goals.c
(#639) Introduce rigid_bodies_collide_with_itself
[nothing.git] / src / game / level / goals.c
index 89165d8554bdd0738e01a9b6382e96577a02e959..075d59d79b9bec98e5290603e9a81b827b4a07b3 100644 (file)
@@ -1,16 +1,20 @@
 #include <SDL2/SDL.h>
-#include <assert.h>
+#include "system/stacktrace.h"
 #include <math.h>
 
 #include "goals.h"
 #include "math/pi.h"
 #include "math/triangle.h"
 #include "str.h"
-#include "system/error.h"
+#include "system/line_stream.h"
 #include "system/lt.h"
+#include "system/nth_alloc.h"
+#include "system/log.h"
+#include "ebisp/interpreter.h"
+#include "broadcast.h"
 
 #define GOAL_RADIUS 10.0f
-#define MAX_ID_SIZE 36
+#define GOAL_MAX_ID_SIZE 36
 
 static int goals_is_goal_hidden(const Goals *goals, size_t i);
 
@@ -24,91 +28,86 @@ struct Goals {
     Lt *lt;
     char **ids;
     Point *points;
-    Rect *regions;
     Color *colors;
     Cue_state *cue_states;
+    bool *visible;
     size_t count;
     Rect player_hitbox;
     float angle;
 };
 
-Goals *create_goals_from_stream(FILE *stream)
+Goals *create_goals_from_line_stream(LineStream *line_stream)
 {
-    assert(stream);
+    trace_assert(line_stream);
 
     Lt *const lt = create_lt();
     if (lt == NULL) {
         return NULL;
     }
 
-    Goals *const goals = PUSH_LT(lt, malloc(sizeof(Goals)), free);
+    Goals *const goals = PUSH_LT(lt, nth_alloc(sizeof(Goals)), free);
     if (goals == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
         RETURN_LT(lt, NULL);
     }
 
     goals->count = 0;
-    if (fscanf(stream, "%lu", &goals->count) == EOF) {
-        throw_error(ERROR_TYPE_LIBC);
+    if (sscanf(
+            line_stream_next(line_stream),
+            "%lu",
+            &goals->count) == EOF) {
+        log_fail("Could not read amount of goals\n");
         RETURN_LT(lt, NULL);
     }
 
     goals->ids = PUSH_LT(
         lt,
-        malloc(sizeof(char*) * goals->count),
+        nth_alloc(sizeof(char*) * goals->count),
         free);
     if (goals->ids == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
         RETURN_LT(lt, NULL);
     }
     for (size_t i = 0; i < goals->count; ++i) {
-        goals->ids[i] = PUSH_LT(lt, malloc(sizeof(char) * MAX_ID_SIZE), free);
+        goals->ids[i] = PUSH_LT(lt, nth_alloc(sizeof(char) * GOAL_MAX_ID_SIZE), free);
         if (goals->ids[i] == NULL) {
-            throw_error(ERROR_TYPE_LIBC);
             RETURN_LT(lt, NULL);
         }
     }
 
-    goals->points = PUSH_LT(lt, malloc(sizeof(Point) * goals->count), free);
+    goals->points = PUSH_LT(lt, nth_alloc(sizeof(Point) * goals->count), free);
     if (goals->points == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
         RETURN_LT(lt, NULL);
     }
 
-    goals->regions = PUSH_LT(lt, malloc(sizeof(Rect) * goals->count), free);
-    if (goals->regions == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
+    goals->colors = PUSH_LT(lt, nth_alloc(sizeof(Color) * goals->count), free);
+    if (goals->colors == NULL) {
         RETURN_LT(lt, NULL);
     }
 
-    goals->colors = PUSH_LT(lt, malloc(sizeof(Color) * goals->count), free);
-    if (goals->colors == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
+    goals->cue_states = PUSH_LT(lt, nth_alloc(sizeof(int) * goals->count), free);
+    if (goals->cue_states == NULL) {
         RETURN_LT(lt, NULL);
     }
 
-    goals->cue_states = PUSH_LT(lt, malloc(sizeof(int) * goals->count), free);
-    if (goals->cue_states == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
+    goals->visible = PUSH_LT(lt, nth_alloc(sizeof(bool) * goals->count), free);
+    if (goals->visible == NULL) {
         RETURN_LT(lt, NULL);
     }
 
     char color[7];
     for (size_t i = 0; i < goals->count; ++i) {
-        if (fscanf(stream, "%" STRINGIFY(MAX_ID_SIZE) "s%f%f%f%f%f%f%6s",
-                   goals->ids[i],
-                   &goals->points[i].x,
-                   &goals->points[i].y,
-                   &goals->regions[i].x,
-                   &goals->regions[i].y,
-                   &goals->regions[i].w,
-                   &goals->regions[i].h,
-                   color) < 0) {
-            throw_error(ERROR_TYPE_LIBC);
+        if (sscanf(
+                line_stream_next(line_stream),
+                "%" STRINGIFY(GOAL_MAX_ID_SIZE) "s%f%f%6s",
+                goals->ids[i],
+                &goals->points[i].x,
+                &goals->points[i].y,
+                color) < 0) {
+            log_fail("Could not read %dth goal\n", i);
             RETURN_LT(lt, NULL);
         }
-        goals->colors[i] = color_from_hexstr(color);
+        goals->colors[i] = hexstr(color);
         goals->cue_states[i] = CUE_STATE_VIRGIN;
+        goals->visible[i] = true;
     }
 
     goals->lt = lt;
@@ -119,7 +118,7 @@ Goals *create_goals_from_stream(FILE *stream)
 
 void destroy_goals(Goals *goals)
 {
-    assert(goals);
+    trace_assert(goals);
     RETURN_LT0(goals->lt);
 }
 
@@ -127,8 +126,8 @@ static int goals_render_core(const Goals *goals,
                              size_t goal_index,
                              Camera *camera)
 {
-    assert(goals);
-    assert(camera);
+    trace_assert(goals);
+    trace_assert(camera);
 
     const Point position = vec_sum(
         goals->points[goal_index],
@@ -156,12 +155,11 @@ static int goals_render_core(const Goals *goals,
     return 0;
 }
 
-/* TODO(#448): goals do not render their ids in debug mode */
 int goals_render(const Goals *goals,
                  Camera *camera)
 {
-    assert(goals);
-    assert(camera);
+    trace_assert(goals);
+    trace_assert(camera);
 
     for (size_t i = 0; i < goals->count; ++i) {
         if (!goals_is_goal_hidden(goals, i)) {
@@ -177,13 +175,13 @@ int goals_render(const Goals *goals,
 void goals_update(Goals *goals,
                   float delta_time)
 {
-    assert(goals);
-    assert(delta_time > 0.0f);
+    trace_assert(goals);
+    trace_assert(delta_time > 0.0f);
     goals->angle = fmodf(goals->angle + 2.0f * delta_time, 2.0f * PI);
 }
 
-void goals_hide(Goals *goals,
-                Rect player_hitbox)
+void goals_hide_from_player(Goals *goals,
+                            Rect player_hitbox)
 {
     goals->player_hitbox = player_hitbox;
 
@@ -232,8 +230,8 @@ void goals_cue(Goals *goals,
 void goals_checkpoint(const Goals *goals,
                       Player *player)
 {
-    assert(goals);
-    assert(player);
+    trace_assert(goals);
+    trace_assert(player);
 
     for (size_t i = 0; i < goals->count; ++i) {
         if (goals->cue_states[i] == CUE_STATE_HIT_NOTHING) {
@@ -242,9 +240,56 @@ void goals_checkpoint(const Goals *goals,
     }
 }
 
+static struct EvalResult
+goals_action(Goals *goals, size_t index, Gc *gc, struct Scope *scope, struct Expr path)
+{
+    trace_assert(goals);
+    trace_assert(gc);
+    trace_assert(scope);
+
+    const char *target = NULL;
+    struct EvalResult res = match_list(gc, "q*", path, &target, NULL);
+    if (res.is_error) {
+        return res;
+    }
+
+    if (strcmp(target, "show") == 0) {
+        goals->visible[index] = true;
+        return eval_success(NIL(gc));
+    } else if (strcmp(target, "hide") == 0) {
+        goals->visible[index] = false;
+        return eval_success(NIL(gc));
+    }
+
+    return unknown_target(gc, goals->ids[index], target);
+}
+
+struct EvalResult
+goals_send(Goals *goals, Gc *gc, struct Scope *scope, struct Expr path)
+{
+    trace_assert(goals);
+    trace_assert(gc);
+    trace_assert(scope);
+
+    const char *target = NULL;
+    struct Expr rest = void_expr();
+    struct EvalResult res = match_list(gc, "s*", path, &target, &rest);
+    if (res.is_error) {
+        return res;
+    }
+
+    for (size_t i = 0; i < goals->count; ++i) {
+        if (strcmp(target, goals->ids[i]) == 0) {
+            return goals_action(goals, i, gc, scope, rest);
+        }
+    }
+
+    return unknown_target(gc, "goals", target);
+}
+
 /* Private Functions */
 
 static int goals_is_goal_hidden(const Goals *goals, size_t i)
 {
-    return rects_overlap(goals->regions[i], goals->player_hitbox);
+    return !goals->visible[i];
 }