]> git.lizzy.rs Git - nothing.git/blob - src/game/level_script.c
(#507) Move unpack_args to interpreter unit
[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     Level * const level = (Level*)param;
19     const char * const goal_id = NULL;
20
21     struct EvalResult result = unpack_args(gc, "s", args, &goal_id);
22     if (result.is_error) {
23         return result;
24     }
25
26     level_hide_goal(level, goal_id);
27
28     return eval_success(NIL(gc));
29 }
30
31 struct EvalResult
32 show_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args)
33 {
34     assert(param);
35     assert(gc);
36     assert(scope);
37
38     if (!list_p(args)) {
39         return wrong_argument_type(gc, "listp", args);
40     }
41
42     if (length_of_list(args) != 1) {
43         return wrong_number_of_arguments(gc, length_of_list(args));
44     }
45
46     if (!string_p(CAR(args))) {
47         return wrong_argument_type(gc, "stringp", args);
48     }
49
50     const char * const goal_id = CAR(args).atom->str;
51     Level * const level = (Level*)param;
52
53     level_show_goal(level, goal_id);
54
55     return eval_success(NIL(gc));
56 }
57
58 struct EvalResult rect_apply_force(void *param, Gc *gc, struct Scope *scope, struct Expr args)
59 {
60     assert(gc);
61     assert(scope);
62     assert(param);
63
64     /* TODO(#401): rect_apply_force doesn't sanitize it's input */
65
66     Level *level = (Level*) param;
67     const char *rect_id = CAR(args).atom->str;
68     struct Expr vector_force_expr = CAR(CDR(args));
69     const float force_x = (float) CAR(vector_force_expr).atom->num;
70     const float force_y = (float) CDR(vector_force_expr).atom->num;
71
72     print_expr_as_sexpr(stdout, args); printf("\n");
73
74     Rigid_rect *rigid_rect = level_rigid_rect(level, rect_id);
75     if (rigid_rect != NULL) {
76         log_info("Found rect `%s`\n", rect_id);
77         log_info("Applying force (%f, %f)\n", force_x, force_y);
78         rigid_rect_apply_force(rigid_rect, vec(force_x, force_y));
79     } else {
80         log_fail("Couldn't find rigid_rect `%s`\n", rect_id);
81     }
82
83     return eval_success(NIL(gc));
84 }
85
86 void load_level_library(Gc *gc, struct Scope *scope, Level *level)
87 {
88     set_scope_value(
89         gc,
90         scope,
91         SYMBOL(gc, "rect-apply-force"),
92         NATIVE(gc, rect_apply_force, level));
93     set_scope_value(
94         gc,
95         scope,
96         SYMBOL(gc, "hide-goal"),
97         NATIVE(gc, hide_goal, level));
98     set_scope_value(
99         gc,
100         scope,
101         SYMBOL(gc, "show-goal"),
102         NATIVE(gc, show_goal, level));
103 }