]> git.lizzy.rs Git - nothing.git/commitdiff
(#503) Make hide-goal and show-goal accessible in console
authorrexim <reximkut@gmail.com>
Sat, 17 Nov 2018 17:03:58 +0000 (00:03 +0700)
committerrexim <reximkut@gmail.com>
Sat, 17 Nov 2018 17:03:58 +0000 (00:03 +0700)
CMakeLists.txt
src/game/level/script.c
src/game/level_script.c [new file with mode: 0644]
src/game/level_script.h [new file with mode: 0644]
src/ui/console.c
src/ui/console.h

index 12f6fa6cdd12128ed61afa58e9256bfc31c2174e..7ae77574ee5db7a47c5a495f94fb0521d5d83f88 100644 (file)
@@ -107,6 +107,8 @@ add_executable(nothing
   src/system/log.c
   src/system/nth_alloc.h
   src/system/nth_alloc.c
+  src/game/level_script.c
+  src/game/level_script.h
 )
 
 add_executable(repl
index faf86f792c24607a1bae9971be53de1b651e681a..bdba3c69ea5b21da3e73485c58d74c928a182ad1 100644 (file)
@@ -5,6 +5,7 @@
 #include "ebisp/parser.h"
 #include "ebisp/scope.h"
 #include "game/level.h"
+#include "game/level_script.h"
 #include "script.h"
 #include "str.h"
 #include "system/line_stream.h"
@@ -20,66 +21,6 @@ struct Script
     struct Scope scope;
 };
 
-static struct EvalResult
-hide_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args)
-{
-    assert(param);
-    assert(gc);
-    assert(scope);
-
-    if (!list_p(args)) {
-        return wrong_argument_type(gc, "listp", args);
-    }
-
-    if (length_of_list(args) != 1) {
-        return eval_failure(
-            CONS(gc,
-                 SYMBOL(gc, "wrong-number-of-arguments"),
-                 NUMBER(gc, length_of_list(args))));
-    }
-
-    if (!string_p(CAR(args))) {
-        return wrong_argument_type(gc, "stringp", args);
-    }
-
-    const char * const goal_id = CAR(args).atom->str;
-    Level * const level = (Level*)param;
-
-    level_hide_goal(level, goal_id);
-
-    return eval_success(NIL(gc));
-}
-
-static struct EvalResult
-show_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args)
-{
-    assert(param);
-    assert(gc);
-    assert(scope);
-
-    if (!list_p(args)) {
-        return wrong_argument_type(gc, "listp", args);
-    }
-
-    if (length_of_list(args) != 1) {
-        return eval_failure(
-            CONS(gc,
-                 SYMBOL(gc, "wrong-number-of-arguments"),
-                 NUMBER(gc, length_of_list(args))));
-    }
-
-    if (!string_p(CAR(args))) {
-        return wrong_argument_type(gc, "stringp", args);
-    }
-
-    const char * const goal_id = CAR(args).atom->str;
-    Level * const level = (Level*)param;
-
-    level_show_goal(level, goal_id);
-
-    return eval_success(NIL(gc));
-}
-
 Script *create_script_from_line_stream(LineStream *line_stream, Level *level)
 {
     assert(line_stream);
@@ -114,21 +55,7 @@ Script *create_script_from_line_stream(LineStream *line_stream, Level *level)
     }
     PUSH_LT(lt, source_code, free);
 
-    set_scope_value(
-        script->gc,
-        &script->scope,
-        SYMBOL(script->gc, "rect-apply-force"),
-        NATIVE(script->gc, rect_apply_force, level));
-    set_scope_value(
-        script->gc,
-        &script->scope,
-        SYMBOL(script->gc, "hide-goal"),
-        NATIVE(script->gc, hide_goal, level));
-    set_scope_value(
-        script->gc,
-        &script->scope,
-        SYMBOL(script->gc, "show-goal"),
-        NATIVE(script->gc, show_goal, level));
+    load_level_library(script->gc, &script->scope, level);
 
     struct ParseResult parse_result =
         read_all_exprs_from_string(
diff --git a/src/game/level_script.c b/src/game/level_script.c
new file mode 100644 (file)
index 0000000..91d7ca4
--- /dev/null
@@ -0,0 +1,117 @@
+#include <assert.h>
+
+#include "ebisp/gc.h"
+#include "ebisp/interpreter.h"
+#include "ebisp/scope.h"
+#include "game/level/player/rigid_rect.h"
+#include "level.h"
+#include "level_script.h"
+#include "system/log.h"
+
+struct EvalResult
+hide_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args)
+{
+    assert(param);
+    assert(gc);
+    assert(scope);
+
+    /* TODO: Manually parsing args of native functions is tedious. */
+    if (!list_p(args)) {
+        return wrong_argument_type(gc, "listp", args);
+    }
+
+    if (length_of_list(args) != 1) {
+        return eval_failure(
+            CONS(gc,
+                 SYMBOL(gc, "wrong-number-of-arguments"),
+                 NUMBER(gc, length_of_list(args))));
+    }
+
+    if (!string_p(CAR(args))) {
+        return wrong_argument_type(gc, "stringp", args);
+    }
+
+    const char * const goal_id = CAR(args).atom->str;
+    Level * const level = (Level*)param;
+
+    level_hide_goal(level, goal_id);
+
+    return eval_success(NIL(gc));
+}
+
+struct EvalResult
+show_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args)
+{
+    assert(param);
+    assert(gc);
+    assert(scope);
+
+    if (!list_p(args)) {
+        return wrong_argument_type(gc, "listp", args);
+    }
+
+    if (length_of_list(args) != 1) {
+        return eval_failure(
+            CONS(gc,
+                 SYMBOL(gc, "wrong-number-of-arguments"),
+                 NUMBER(gc, length_of_list(args))));
+    }
+
+    if (!string_p(CAR(args))) {
+        return wrong_argument_type(gc, "stringp", args);
+    }
+
+    const char * const goal_id = CAR(args).atom->str;
+    Level * const level = (Level*)param;
+
+    level_show_goal(level, goal_id);
+
+    return eval_success(NIL(gc));
+}
+
+struct EvalResult rect_apply_force(void *param, Gc *gc, struct Scope *scope, struct Expr args)
+{
+    assert(gc);
+    assert(scope);
+    assert(param);
+
+    /* TODO(#401): rect_apply_force doesn't sanitize it's input */
+
+    Level *level = (Level*) param;
+    const char *rect_id = CAR(args).atom->str;
+    struct Expr vector_force_expr = CAR(CDR(args));
+    const float force_x = (float) CAR(vector_force_expr).atom->num;
+    const float force_y = (float) CDR(vector_force_expr).atom->num;
+
+    print_expr_as_sexpr(stdout, args); printf("\n");
+
+    Rigid_rect *rigid_rect = level_rigid_rect(level, rect_id);
+    if (rigid_rect != NULL) {
+        log_info("Found rect `%s`\n", rect_id);
+        log_info("Applying force (%f, %f)\n", force_x, force_y);
+        rigid_rect_apply_force(rigid_rect, vec(force_x, force_y));
+    } else {
+        log_fail("Couldn't find rigid_rect `%s`\n", rect_id);
+    }
+
+    return eval_success(NIL(gc));
+}
+
+void load_level_library(Gc *gc, struct Scope *scope, Level *level)
+{
+    set_scope_value(
+        gc,
+        scope,
+        SYMBOL(gc, "rect-apply-force"),
+        NATIVE(gc, rect_apply_force, level));
+    set_scope_value(
+        gc,
+        scope,
+        SYMBOL(gc, "hide-goal"),
+        NATIVE(gc, hide_goal, level));
+    set_scope_value(
+        gc,
+        scope,
+        SYMBOL(gc, "show-goal"),
+        NATIVE(gc, show_goal, level));
+}
diff --git a/src/game/level_script.h b/src/game/level_script.h
new file mode 100644 (file)
index 0000000..9a223f3
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef LEVEL_SCRIPT_H_
+#define LEVEL_SCRIPT_H_
+
+#include "ebisp/expr.h"
+
+typedef struct Gc Gc;
+struct Scope;
+typedef struct Level Level;
+
+struct EvalResult
+rect_apply_force(void *param, Gc *gc, struct Scope *scope, struct Expr args);
+
+struct EvalResult
+hide_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args);
+
+struct EvalResult
+show_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args);
+
+void load_level_library(Gc *gc, struct Scope *scope, Level *level);
+
+#endif  // LEVEL_SCRIPT_H_
index 0e6a8f799c6d6e2aaf065b3e2ff584c102632bea..d737428f63871f3d266c6e528a1e6f7cc2133d1f 100644 (file)
@@ -6,6 +6,7 @@
 #include "ebisp/scope.h"
 #include "game/level.h"
 #include "game/level/player/rigid_rect.h"
+#include "game/level_script.h"
 #include "sdl/renderer.h"
 #include "system/log.h"
 #include "system/lt.h"
@@ -53,34 +54,6 @@ struct Console
 /* TODO(#358): Console does not support copy, cut, paste operations */
 /* TODO(#503): hide-goal and show-goal are not accessible in Console */
 
-struct EvalResult rect_apply_force(void *param, Gc *gc, struct Scope *scope, struct Expr args)
-{
-    assert(gc);
-    assert(scope);
-    assert(param);
-
-    /* TODO(#401): rect_apply_force doesn't sanitize it's input */
-
-    Level *level = (Level*) param;
-    const char *rect_id = CAR(args).atom->str;
-    struct Expr vector_force_expr = CAR(CDR(args));
-    const float force_x = (float) CAR(vector_force_expr).atom->num;
-    const float force_y = (float) CDR(vector_force_expr).atom->num;
-
-    print_expr_as_sexpr(stdout, args); printf("\n");
-
-    Rigid_rect *rigid_rect = level_rigid_rect(level, rect_id);
-    if (rigid_rect != NULL) {
-        log_info("Found rect `%s`\n", rect_id);
-        log_info("Applying force (%f, %f)\n", force_x, force_y);
-        rigid_rect_apply_force(rigid_rect, vec(force_x, force_y));
-    } else {
-        log_fail("Couldn't find rigid_rect `%s`\n", rect_id);
-    }
-
-    return eval_success(NIL(gc));
-}
-
 Console *create_console(Level *level,
                         const Sprite_font *font)
 {
@@ -104,11 +77,8 @@ Console *create_console(Level *level,
     console->scope.expr = CONS(console->gc,
                                NIL(console->gc),
                                NIL(console->gc));
-    set_scope_value(
-        console->gc,
-        &console->scope,
-        SYMBOL(console->gc, "rect-apply-force"),
-        NATIVE(console->gc, rect_apply_force, level));
+
+    load_level_library(console->gc, &console->scope, level);
 
     console->edit_field = PUSH_LT(
         lt,
index 2105ba357126d2ae7fef4f9abd4967346667073b..1cc66491e86908d52225b312398c6713c4b54499 100644 (file)
@@ -24,6 +24,4 @@ int console_update(Console *console,
 
 void console_slide_down(Console *console);
 
-struct EvalResult rect_apply_force(void *param, Gc *gc, struct Scope *scope, struct Expr args);
-
 #endif  // CONSOLE_H_