]> git.lizzy.rs Git - nothing.git/blobdiff - src/game/level/goals.c
Merge pull request #595 from tsoding/594
[nothing.git] / src / game / level / goals.c
index b97383155e8e918ea78c648e13519f672774c029..47c9f4b0d45bf31a402c9e7a5971a65a54c653a4 100644 (file)
@@ -1,15 +1,15 @@
 #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"
 
 #define GOAL_RADIUS 10.0f
 #define GOAL_MAX_ID_SIZE 36
@@ -26,11 +26,9 @@ struct Goals {
     Lt *lt;
     char **ids;
     Point *points;
-    /* TODO(#493): replace Goals.regions with the Regions entity */
-    Rect *regions;
     Color *colors;
-    /* TODO(#494): it is not clear how to maintain Cue_state from the scripting language */
     Cue_state *cue_states;
+    bool *visible;
     size_t count;
     Rect player_hitbox;
     float angle;
@@ -38,7 +36,7 @@ struct Goals {
 
 Goals *create_goals_from_line_stream(LineStream *line_stream)
 {
-    assert(line_stream);
+    trace_assert(line_stream);
 
     Lt *const lt = create_lt();
     if (lt == NULL) {
@@ -47,7 +45,6 @@ Goals *create_goals_from_line_stream(LineStream *line_stream)
 
     Goals *const goals = PUSH_LT(lt, nth_alloc(sizeof(Goals)), free);
     if (goals == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
         RETURN_LT(lt, NULL);
     }
 
@@ -56,7 +53,7 @@ Goals *create_goals_from_line_stream(LineStream *line_stream)
             line_stream_next(line_stream),
             "%lu",
             &goals->count) == EOF) {
-        throw_error(ERROR_TYPE_LIBC);
+        log_fail("Could not read amount of goals\n");
         RETURN_LT(lt, NULL);
     }
 
@@ -65,38 +62,32 @@ Goals *create_goals_from_line_stream(LineStream *line_stream)
         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, 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, 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, nth_alloc(sizeof(Rect) * goals->count), free);
-    if (goals->regions == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
         RETURN_LT(lt, NULL);
     }
 
     goals->colors = PUSH_LT(lt, nth_alloc(sizeof(Color) * goals->count), free);
     if (goals->colors == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
         RETURN_LT(lt, NULL);
     }
 
     goals->cue_states = PUSH_LT(lt, nth_alloc(sizeof(int) * goals->count), free);
     if (goals->cue_states == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
+        RETURN_LT(lt, NULL);
+    }
+
+    goals->visible = PUSH_LT(lt, nth_alloc(sizeof(bool) * goals->count), free);
+    if (goals->visible == NULL) {
         RETURN_LT(lt, NULL);
     }
 
@@ -104,20 +95,17 @@ Goals *create_goals_from_line_stream(LineStream *line_stream)
     for (size_t i = 0; i < goals->count; ++i) {
         if (sscanf(
                 line_stream_next(line_stream),
-                "%" STRINGIFY(GOAL_MAX_ID_SIZE) "s%f%f%f%f%f%f%6s",
+                "%" STRINGIFY(GOAL_MAX_ID_SIZE) "s%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);
+            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;
@@ -128,7 +116,7 @@ Goals *create_goals_from_line_stream(LineStream *line_stream)
 
 void destroy_goals(Goals *goals)
 {
-    assert(goals);
+    trace_assert(goals);
     RETURN_LT0(goals->lt);
 }
 
@@ -136,8 +124,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],
@@ -165,12 +153,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)) {
@@ -178,13 +165,6 @@ int goals_render(const Goals *goals,
                 return -1;
             }
         }
-
-        if (camera_render_debug_rect(
-                camera,
-                goals->regions[i],
-                goals->colors[i]) < 0) {
-            return -1;
-        }
     }
 
     return 0;
@@ -193,8 +173,8 @@ 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);
 }
 
@@ -248,8 +228,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) {
@@ -260,21 +240,33 @@ void goals_checkpoint(const Goals *goals,
 
 void goals_hide(Goals *goals, const char *id)
 {
-    assert(goals);
-    assert(id);
-    /* TODO: goals_hide is not implemented */
+    trace_assert(goals);
+    trace_assert(id);
+
+    for (size_t i = 0; i < goals->count; ++i) {
+        if (strcmp(id, goals->ids[i]) == 0) {
+            goals->visible[i] = false;
+            return;
+        }
+    }
 }
 
 void goals_show(Goals *goals, const char *id)
 {
-    assert(goals);
-    assert(id);
-    /* TODO: goals_show is not implemented */
+    trace_assert(goals);
+    trace_assert(id);
+
+    for (size_t i = 0; i < goals->count; ++i) {
+        if (strcmp(id, goals->ids[i]) == 0) {
+            goals->visible[i] = true;
+            return;
+        }
+    }
 }
 
 /* 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];
 }