summaryrefslogtreecommitdiff
path: root/src/map/magic-expr.hpp
blob: 419c833bcf695fe716520229decf21c175c43d12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef MAGIC_EXPR_HPP
#define MAGIC_EXPR_HPP

#include "magic-interpreter.hpp"

/*
 * Argument types:
 *  i : int
 *  d : dir
 *  s : string
 *  e : entity
 *  l : location
 *  a : area
 *  S : spell
 *  I : invocation
 *  . : any, except for fail/undef
 *  _ : any, including fail, but not undef
 */
typedef struct fun
{
    const char *name;
    const char *signature;
    char ret_ty;
    int(*fun)(env_t *env, int args_nr, val_t *result, val_t *args);
} fun_t;

typedef struct op
{
    const char *name;
    const char *signature;
    int(*op)(env_t *env, int args_nr, val_t *args);
} op_t;

/**
 * Retrieves a function by name
 * @param name The name to look up
 * @return A function of that name, or NULL, and a function index
 */
fun_t *magic_get_fun(const char *name, int *index);

/**
 * Retrieves an operation by name
 * @param name The name to look up
 * @return An operation of that name, or NULL, and a function index
 */
op_t *magic_get_op(char *name, int *index);

/**
 * Evaluates an expression and stores the result in `dest'
 */
void magic_eval(env_t *env, val_t *dest, expr_t *expr);

/**
 * Evaluates an expression and coerces the result into an integer
 */
int magic_eval_int(env_t *env, expr_t *expr);

/**
 * Evaluates an expression and coerces the result into a string
 */
char *magic_eval_str(env_t *env, expr_t *expr);

expr_t *magic_new_expr(EXPR ty);

void magic_clear_var(val_t *v);

void magic_copy_var(val_t *dest, val_t *src);

void magic_random_location(location_t *dest, area_t *area);

// ret -1: not a string, ret 1: no such item, ret 0: OK
int magic_find_item(val_t *args, int index, struct item *item, int *stackable);

#define GET_ARG_ITEM(index, dest, stackable)                    \
     switch (magic_find_item(args, index, &dest, &stackable))   \
    {                                                           \
        case -1: return 1;                                      \
        case 1: return 0;                                       \
        default: break;                                         \
    }

int magic_location_in_area(int m, int x, int y, area_t *area);

#endif // MAGIC_EXPR_HPP