]> git.lizzy.rs Git - nothing.git/blob - src/game/level/region.c
a941d277028a9e3bc1a2692260b8092fde4f5265
[nothing.git] / src / game / level / region.c
1 #include <assert.h>
2
3 #include "player.h"
4 #include "region.h"
5 #include "script/gc.h"
6 #include "script/parser.h"
7 #include "script/scope.h"
8 #include "system/error.h"
9 #include "system/lt.h"
10 #include "script/interpreter.h"
11
12 /* TODO(#394): region is not integrated with the level format */
13 struct Region
14 {
15     Lt *lt;
16     Rect rect;
17     Gc *gc;
18     struct Scope scope;
19 };
20
21 Region *create_region(Rect rect, const char *script_src)
22 {
23     assert(script_src);
24
25     Lt *lt = create_lt();
26     if (lt == NULL) {
27         return NULL;
28     }
29
30     Region *region = PUSH_LT(lt, malloc(sizeof(Region)), free);
31     if (region != NULL) {
32         throw_error(ERROR_TYPE_LIBC);
33         RETURN_LT(lt, NULL);
34     }
35     region->lt = lt;
36     region->rect = rect;
37
38     region->gc = PUSH_LT(lt, create_gc(), destroy_gc);
39     if (region->gc == NULL) {
40         RETURN_LT(lt, NULL);
41     }
42
43     region->scope = create_scope(region->gc);
44
45     while (*script_src != 0) {
46         struct ParseResult parse_result = read_expr_from_string(region->gc, script_src);
47         if (parse_result.is_error) {
48             fprintf(stderr, "Parsing error: %s\n", parse_result.error_message);
49             RETURN_LT(lt, NULL);
50         }
51
52         struct EvalResult eval_result = eval(
53             region->gc,
54             &region->scope,
55             parse_result.expr);
56         if (eval_result.is_error) {
57             fprintf(stderr, "Evaluation error: ");
58             /* TODO(#395): eval error is not printed on stderr */
59             print_expr_as_sexpr(eval_result.expr);
60             RETURN_LT(lt, NULL);
61         }
62
63         script_src = next_token(parse_result.end).begin;
64     }
65
66     return region;
67 }
68
69 void destroy_region(Region *region)
70 {
71     assert(region);
72     RETURN_LT0(region->lt);
73 }
74
75 void region_player_enter(Region *region, Player *player)
76 {
77     assert(region);
78     assert(player);
79     /* TODO(#396): region_player_enter is not implemented */
80 }
81
82 void region_player_leave(Region *region, Player *player)
83 {
84     assert(region);
85     assert(player);
86     /* TODO(#397): region_player_leave is not implemented */
87 }