]> git.lizzy.rs Git - nothing.git/blob - src/game/level_script.c
Merge pull request #508 from tsoding/502
[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(#507): 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 wrong_number_of_arguments(gc, length_of_list(args));
55     }
56
57     if (!string_p(CAR(args))) {
58         return wrong_argument_type(gc, "stringp", args);
59     }
60
61     const char * const goal_id = CAR(args).atom->str;
62     Level * const level = (Level*)param;
63
64     level_show_goal(level, goal_id);
65
66     return eval_success(NIL(gc));
67 }
68
69 struct EvalResult rect_apply_force(void *param, Gc *gc, struct Scope *scope, struct Expr args)
70 {
71     assert(gc);
72     assert(scope);
73     assert(param);
74
75     /* TODO(#401): rect_apply_force doesn't sanitize it's input */
76
77     Level *level = (Level*) param;
78     const char *rect_id = CAR(args).atom->str;
79     struct Expr vector_force_expr = CAR(CDR(args));
80     const float force_x = (float) CAR(vector_force_expr).atom->num;
81     const float force_y = (float) CDR(vector_force_expr).atom->num;
82
83     print_expr_as_sexpr(stdout, args); printf("\n");
84
85     Rigid_rect *rigid_rect = level_rigid_rect(level, rect_id);
86     if (rigid_rect != NULL) {
87         log_info("Found rect `%s`\n", rect_id);
88         log_info("Applying force (%f, %f)\n", force_x, force_y);
89         rigid_rect_apply_force(rigid_rect, vec(force_x, force_y));
90     } else {
91         log_fail("Couldn't find rigid_rect `%s`\n", rect_id);
92     }
93
94     return eval_success(NIL(gc));
95 }
96
97 void load_level_library(Gc *gc, struct Scope *scope, Level *level)
98 {
99     set_scope_value(
100         gc,
101         scope,
102         SYMBOL(gc, "rect-apply-force"),
103         NATIVE(gc, rect_apply_force, level));
104     set_scope_value(
105         gc,
106         scope,
107         SYMBOL(gc, "hide-goal"),
108         NATIVE(gc, hide_goal, level));
109     set_scope_value(
110         gc,
111         scope,
112         SYMBOL(gc, "show-goal"),
113         NATIVE(gc, show_goal, level));
114 }