]> git.lizzy.rs Git - nothing.git/blob - src/game/level_script.c
(#503) Make hide-goal and show-goal accessible in console
[nothing.git] / src / game / level_script.c
1 #include <assert.h>
2
3 #include "ebisp/gc.h"
4 #include "ebisp/interpreter.h"
5 #include "ebisp/scope.h"
6 #include "game/level/player/rigid_rect.h"
7 #include "level.h"
8 #include "level_script.h"
9 #include "system/log.h"
10
11 struct EvalResult
12 hide_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args)
13 {
14     assert(param);
15     assert(gc);
16     assert(scope);
17
18     /* TODO: Manually parsing args of native functions is tedious. */
19     if (!list_p(args)) {
20         return wrong_argument_type(gc, "listp", args);
21     }
22
23     if (length_of_list(args) != 1) {
24         return eval_failure(
25             CONS(gc,
26                  SYMBOL(gc, "wrong-number-of-arguments"),
27                  NUMBER(gc, length_of_list(args))));
28     }
29
30     if (!string_p(CAR(args))) {
31         return wrong_argument_type(gc, "stringp", args);
32     }
33
34     const char * const goal_id = CAR(args).atom->str;
35     Level * const level = (Level*)param;
36
37     level_hide_goal(level, goal_id);
38
39     return eval_success(NIL(gc));
40 }
41
42 struct EvalResult
43 show_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args)
44 {
45     assert(param);
46     assert(gc);
47     assert(scope);
48
49     if (!list_p(args)) {
50         return wrong_argument_type(gc, "listp", args);
51     }
52
53     if (length_of_list(args) != 1) {
54         return eval_failure(
55             CONS(gc,
56                  SYMBOL(gc, "wrong-number-of-arguments"),
57                  NUMBER(gc, length_of_list(args))));
58     }
59
60     if (!string_p(CAR(args))) {
61         return wrong_argument_type(gc, "stringp", args);
62     }
63
64     const char * const goal_id = CAR(args).atom->str;
65     Level * const level = (Level*)param;
66
67     level_show_goal(level, goal_id);
68
69     return eval_success(NIL(gc));
70 }
71
72 struct EvalResult rect_apply_force(void *param, Gc *gc, struct Scope *scope, struct Expr args)
73 {
74     assert(gc);
75     assert(scope);
76     assert(param);
77
78     /* TODO(#401): rect_apply_force doesn't sanitize it's input */
79
80     Level *level = (Level*) param;
81     const char *rect_id = CAR(args).atom->str;
82     struct Expr vector_force_expr = CAR(CDR(args));
83     const float force_x = (float) CAR(vector_force_expr).atom->num;
84     const float force_y = (float) CDR(vector_force_expr).atom->num;
85
86     print_expr_as_sexpr(stdout, args); printf("\n");
87
88     Rigid_rect *rigid_rect = level_rigid_rect(level, rect_id);
89     if (rigid_rect != NULL) {
90         log_info("Found rect `%s`\n", rect_id);
91         log_info("Applying force (%f, %f)\n", force_x, force_y);
92         rigid_rect_apply_force(rigid_rect, vec(force_x, force_y));
93     } else {
94         log_fail("Couldn't find rigid_rect `%s`\n", rect_id);
95     }
96
97     return eval_success(NIL(gc));
98 }
99
100 void load_level_library(Gc *gc, struct Scope *scope, Level *level)
101 {
102     set_scope_value(
103         gc,
104         scope,
105         SYMBOL(gc, "rect-apply-force"),
106         NATIVE(gc, rect_apply_force, level));
107     set_scope_value(
108         gc,
109         scope,
110         SYMBOL(gc, "hide-goal"),
111         NATIVE(gc, hide_goal, level));
112     set_scope_value(
113         gc,
114         scope,
115         SYMBOL(gc, "show-goal"),
116         NATIVE(gc, show_goal, level));
117 }