diff options
Diffstat (limited to 'src/map/magic-expr.cpp')
-rw-r--r-- | src/map/magic-expr.cpp | 1310 |
1 files changed, 681 insertions, 629 deletions
diff --git a/src/map/magic-expr.cpp b/src/map/magic-expr.cpp index 4b4e3a3..4a72a38 100644 --- a/src/map/magic-expr.cpp +++ b/src/map/magic-expr.cpp @@ -1,47 +1,49 @@ -#include "magic-expr.hpp" #include "magic-expr-eval.hpp" -#include "itemdb.hpp" -#include <math.h> +#include "magic-expr.hpp" +#include "magic-interpreter-aux.hpp" -#include "../common/mt_rand.hpp" +#include <cmath> -#define IS_SOLID(c) ((c) == 1 || (c) == 5) +#include "../common/cxxstdio.hpp" +#include "../common/random.hpp" -int map_is_solid (int m, int x, int y) -{ - return (IS_SOLID (map_getcell (m, x, y))); -} +#include "battle.hpp" +#include "npc.hpp" +#include "pc.hpp" +#include "itemdb.hpp" -#undef IS_SOLID +#include "../poison.hpp" -static void free_area (area_t * area) +static +void free_area(area_t *area) { if (!area) return; switch (area->ty) { - case AREA_UNION: - free_area (area->a.a_union[0]); - free_area (area->a.a_union[1]); + case AREA::UNION: + free_area(area->a.a_union[0]); + free_area(area->a.a_union[1]); break; default: break; } - free (area); + free(area); } -static area_t *dup_area (area_t * area) +static +area_t *dup_area(area_t *area) { - area_t *retval = (area_t *)malloc (sizeof (area_t)); + area_t *retval = (area_t *)malloc(sizeof(area_t)); *retval = *area; switch (area->ty) { - case AREA_UNION: - retval->a.a_union[0] = dup_area (retval->a.a_union[0]); - retval->a.a_union[1] = dup_area (retval->a.a_union[1]); + case AREA::UNION: + retval->a.a_union[0] = dup_area(retval->a.a_union[0]); + retval->a.a_union[1] = dup_area(retval->a.a_union[1]); break; default: break; @@ -50,17 +52,17 @@ static area_t *dup_area (area_t * area) return retval; } -void magic_copy_var (val_t * dest, val_t * src) +void magic_copy_var(val_t *dest, val_t *src) { *dest = *src; switch (dest->ty) { - case TY_STRING: - dest->v.v_string = strdup (dest->v.v_string); + case TYPE::STRING: + dest->v.v_string = strdup(dest->v.v_string); break; - case TY_AREA: - dest->v.v_area = dup_area (dest->v.v_area); + case TYPE::AREA: + dest->v.v_area = dup_area(dest->v.v_area); break; default: break; @@ -68,15 +70,15 @@ void magic_copy_var (val_t * dest, val_t * src) } -void magic_clear_var (val_t * v) +void magic_clear_var(val_t *v) { switch (v->ty) { - case TY_STRING: - free (v->v.v_string); + case TYPE::STRING: + free(v->v.v_string); break; - case TY_AREA: - free_area (v->v.v_area); + case TYPE::AREA: + free_area(v->v.v_area); break; default: break; @@ -84,116 +86,120 @@ void magic_clear_var (val_t * v) } static -const char *show_entity (entity_t * entity) +const char *show_entity(entity_t *entity) { switch (entity->type) { - case BL_PC: + case BL::PC: return ((struct map_session_data *) entity)->status.name; - case BL_NPC: + case BL::NPC: return ((struct npc_data *) entity)->name; - case BL_MOB: + case BL::MOB: return ((struct mob_data *) entity)->name; - case BL_ITEM: + case BL::ITEM: /* Sorry about this one... */ return ((struct item_data *) (&((struct flooritem_data *) entity)-> item_data))->name; - case BL_SKILL: - return "%skill"; - case BL_SPELL: + case BL::SPELL: return "%invocation(ERROR:this-should-not-be-an-entity)"; default: return "%unknown-entity"; } } -static void stringify (val_t * v, int within_op) +static +void stringify(val_t *v, int within_op) { static const char *dirs[8] = - { "south", "south-west", "west", "north-west", "north", "north-east", + { + "south", "south-west", + "west", "north-west", + "north", "north-east", "east", "south-east" }; - char *buf; + std::string buf; switch (v->ty) { - case TY_UNDEF: - buf = strdup ("UNDEF"); + case TYPE::UNDEF: + buf = "UNDEF"; break; - case TY_INT: - buf = (char *)malloc (32); - sprintf (buf, "%i", v->v.v_int); + case TYPE::INT: + buf = STRPRINTF("%i", v->v.v_int); break; - case TY_STRING: + case TYPE::STRING: return; - case TY_DIR: - buf = strdup (dirs[v->v.v_int]); + case TYPE::DIR: + buf = dirs[v->v.v_int]; break; - case TY_ENTITY: - buf = strdup (show_entity (v->v.v_entity)); + case TYPE::ENTITY: + buf = show_entity(v->v.v_entity); break; - case TY_LOCATION: - buf = (char *) malloc (128); - sprintf (buf, "<\"%s\", %d, %d>", map[v->v.v_location.m].name, - v->v.v_location.x, v->v.v_location.y); + case TYPE::LOCATION: + buf = STRPRINTF("<\"%s\", %d, %d>", + map[v->v.v_location.m].name, + v->v.v_location.x, + v->v.v_location.y); break; - case TY_AREA: - buf = strdup ("%area"); - free_area (v->v.v_area); + case TYPE::AREA: + buf = "%area"; + free_area(v->v.v_area); break; - case TY_SPELL: - buf = strdup (v->v.v_spell->name); + case TYPE::SPELL: + buf = v->v.v_spell->name; break; - case TY_INVOCATION: + case TYPE::INVOCATION: { invocation_t *invocation = within_op - ? v->v.v_invocation : (invocation_t *) map_id2bl (v->v.v_int); - buf = strdup (invocation->spell->name); + ? v->v.v_invocation + : (invocation_t *) map_id2bl(v->v.v_int); + buf = invocation->spell->name; } break; default: - fprintf (stderr, "[magic] INTERNAL ERROR: Cannot stringify %d\n", - v->ty); + FPRINTF(stderr, "[magic] INTERNAL ERROR: Cannot stringify %d\n", + v->ty); return; } - v->v.v_string = buf; - v->ty = TY_STRING; + v->v.v_string = strdup(buf.c_str()); + v->ty = TYPE::STRING; } -static void intify (val_t * v) +static +void intify(val_t *v) { - if (v->ty == TY_INT) + if (v->ty == TYPE::INT) return; - magic_clear_var (v); - v->ty = TY_INT; + magic_clear_var(v); + v->ty = TYPE::INT; v->v.v_int = 1; } static -area_t *area_new (int ty) +area_t *area_new(AREA ty) { area_t *retval; - CREATE (retval, area_t, 1); + CREATE(retval, area_t, 1); retval->ty = ty; return retval; } static -area_t *area_union (area_t * area, area_t * other_area) +area_t *area_union(area_t *area, area_t *other_area) { - area_t *retval = area_new (AREA_UNION); + area_t *retval = area_new(AREA::UNION); retval->a.a_union[0] = area; retval->a.a_union[1] = other_area; retval->size = area->size + other_area->size; /* Assume no overlap */ @@ -203,256 +209,277 @@ area_t *area_union (area_t * area, area_t * other_area) /** * Turns location into area, leaves other types untouched */ -static void make_area (val_t * v) +static +void make_area(val_t *v) { - if (v->ty == TY_LOCATION) + if (v->ty == TYPE::LOCATION) { - area_t *a = (area_t *)malloc (sizeof (area_t)); - v->ty = TY_AREA; - a->ty = AREA_LOCATION; + area_t *a = (area_t *)malloc(sizeof(area_t)); + v->ty = TYPE::AREA; + a->ty = AREA::LOCATION; a->a.a_loc = v->v.v_location; v->v.v_area = a; } } -static void make_location (val_t * v) +static +void make_location(val_t *v) { - if (v->ty == TY_AREA && v->v.v_area->ty == AREA_LOCATION) + if (v->ty == TYPE::AREA && v->v.v_area->ty == AREA::LOCATION) { location_t location = v->v.v_area->a.a_loc; - free_area (v->v.v_area); - v->ty = TY_LOCATION; + free_area(v->v.v_area); + v->ty = TYPE::LOCATION; v->v.v_location = location; } } -static void make_spell (val_t * v) +static +void make_spell(val_t *v) { - if (v->ty == TY_INVOCATION) + if (v->ty == TYPE::INVOCATION) { invocation_t *invoc = v->v.v_invocation; //(invocation_t *) map_id2bl(v->v.v_int); if (!invoc) - v->ty = TY_FAIL; + v->ty = TYPE::FAIL; else { - v->ty = TY_SPELL; + v->ty = TYPE::SPELL; v->v.v_spell = invoc->spell; } } } -static int fun_add (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_add(env_t *, int, val_t *result, val_t *args) { - if (TY (0) == TY_INT && TY (1) == TY_INT) + if (ARG_TYPE(0) == TYPE::INT && ARG_TYPE(1) == TYPE::INT) { /* Integer addition */ - RESULTINT = ARGINT (0) + ARGINT (1); - result->ty = TY_INT; + RESULTINT = ARGINT(0) + ARGINT(1); + result->ty = TYPE::INT; } - else if (ARG_MAY_BE_AREA (0) && ARG_MAY_BE_AREA (1)) + else if (ARG_MAY_BE_AREA(0) && ARG_MAY_BE_AREA(1)) { /* Area union */ - make_area (&args[0]); - make_area (&args[1]); - RESULTAREA = area_union (ARGAREA (0), ARGAREA (1)); - ARGAREA (0) = NULL; - ARGAREA (1) = NULL; - result->ty = TY_AREA; + make_area(&args[0]); + make_area(&args[1]); + RESULTAREA = area_union(ARGAREA(0), ARGAREA(1)); + ARGAREA(0) = NULL; + ARGAREA(1) = NULL; + result->ty = TYPE::AREA; } else { /* Anything else -> string concatenation */ - stringify (&args[0], 1); - stringify (&args[1], 1); + stringify(&args[0], 1); + stringify(&args[1], 1); /* Yes, we could speed this up. */ RESULTSTR = - (char *) malloc (1 + strlen (ARGSTR (0)) + strlen (ARGSTR (1))); - strcpy (RESULTSTR, ARGSTR (0)); - strcat (RESULTSTR, ARGSTR (1)); - result->ty = TY_STRING; + (char *) malloc(1 + strlen(ARGSTR(0)) + strlen(ARGSTR(1))); + strcpy(RESULTSTR, ARGSTR(0)); + strcat(RESULTSTR, ARGSTR(1)); + result->ty = TYPE::STRING; } return 0; } -static int fun_sub (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_sub(env_t *, int, val_t *result, val_t *args) { - RESULTINT = ARGINT (0) - ARGINT (1); + RESULTINT = ARGINT(0) - ARGINT(1); return 0; } -static int fun_mul (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_mul(env_t *, int, val_t *result, val_t *args) { - RESULTINT = ARGINT (0) * ARGINT (1); + RESULTINT = ARGINT(0) * ARGINT(1); return 0; } -static int fun_div (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_div(env_t *, int, val_t *result, val_t *args) { - if (!ARGINT (1)) + if (!ARGINT(1)) return 1; /* division by zero */ - RESULTINT = ARGINT (0) / ARGINT (1); + RESULTINT = ARGINT(0) / ARGINT(1); return 0; } -static int fun_mod (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_mod(env_t *, int, val_t *result, val_t *args) { - if (!ARGINT (1)) + if (!ARGINT(1)) return 1; /* division by zero */ - RESULTINT = ARGINT (0) % ARGINT (1); + RESULTINT = ARGINT(0) % ARGINT(1); return 0; } -static int fun_or (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_or(env_t *, int, val_t *result, val_t *args) { - RESULTINT = ARGINT (0) || ARGINT (1); + RESULTINT = ARGINT(0) || ARGINT(1); return 0; } -static int fun_and (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_and(env_t *, int, val_t *result, val_t *args) { - RESULTINT = ARGINT (0) && ARGINT (1); + RESULTINT = ARGINT(0) && ARGINT(1); return 0; } -static int fun_not (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_not(env_t *, int, val_t *result, val_t *args) { - RESULTINT = !ARGINT (0); + RESULTINT = !ARGINT(0); return 0; } -static int fun_neg (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_neg(env_t *, int, val_t *result, val_t *args) { - RESULTINT = ~ARGINT (0); + RESULTINT = ~ARGINT(0); return 0; } -static int fun_gte (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_gte(env_t *, int, val_t *result, val_t *args) { - if (TY (0) == TY_STRING || TY (1) == TY_STRING) + if (ARG_TYPE(0) == TYPE::STRING || ARG_TYPE(1) == TYPE::STRING) { - stringify (&args[0], 1); - stringify (&args[1], 1); - RESULTINT = strcmp (ARGSTR (0), ARGSTR (1)) >= 0; + stringify(&args[0], 1); + stringify(&args[1], 1); + RESULTINT = strcmp(ARGSTR(0), ARGSTR(1)) >= 0; } else { - intify (&args[0]); - intify (&args[1]); - RESULTINT = ARGINT (0) >= ARGINT (1); + intify(&args[0]); + intify(&args[1]); + RESULTINT = ARGINT(0) >= ARGINT(1); } return 0; } -static int fun_gt (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_gt(env_t *, int, val_t *result, val_t *args) { - if (TY (0) == TY_STRING || TY (1) == TY_STRING) + if (ARG_TYPE(0) == TYPE::STRING || ARG_TYPE(1) == TYPE::STRING) { - stringify (&args[0], 1); - stringify (&args[1], 1); - RESULTINT = strcmp (ARGSTR (0), ARGSTR (1)) > 0; + stringify(&args[0], 1); + stringify(&args[1], 1); + RESULTINT = strcmp(ARGSTR(0), ARGSTR(1)) > 0; } else { - intify (&args[0]); - intify (&args[1]); - RESULTINT = ARGINT (0) > ARGINT (1); + intify(&args[0]); + intify(&args[1]); + RESULTINT = ARGINT(0) > ARGINT(1); } return 0; } -static int fun_eq (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_eq(env_t *, int, val_t *result, val_t *args) { - if (TY (0) == TY_STRING || TY (1) == TY_STRING) + if (ARG_TYPE(0) == TYPE::STRING || ARG_TYPE(1) == TYPE::STRING) { - stringify (&args[0], 1); - stringify (&args[1], 1); - RESULTINT = strcmp (ARGSTR (0), ARGSTR (1)) == 0; + stringify(&args[0], 1); + stringify(&args[1], 1); + RESULTINT = strcmp(ARGSTR(0), ARGSTR(1)) == 0; } - else if (TY (0) == TY_DIR && TY (1) == TY_DIR) - RESULTINT = ARGDIR (0) == ARGDIR (1); - else if (TY (0) == TY_ENTITY && TY (1) == TY_ENTITY) - RESULTINT = ARGENTITY (0) == ARGENTITY (1); - else if (TY (0) == TY_LOCATION && TY (1) == TY_LOCATION) - RESULTINT = (ARGLOCATION (0).x == ARGLOCATION (1).x - && ARGLOCATION (0).y == ARGLOCATION (1).y - && ARGLOCATION (0).m == ARGLOCATION (1).m); - else if (TY (0) == TY_AREA && TY (1) == TY_AREA) - RESULTINT = ARGAREA (0) == ARGAREA (1); /* Probably not that great an idea... */ - else if (TY (0) == TY_SPELL && TY (1) == TY_SPELL) - RESULTINT = ARGSPELL (0) == ARGSPELL (1); - else if (TY (0) == TY_INVOCATION && TY (1) == TY_INVOCATION) - RESULTINT = ARGINVOCATION (0) == ARGINVOCATION (1); + else if (ARG_TYPE(0) == TYPE::DIR && ARG_TYPE(1) == TYPE::DIR) + RESULTINT = ARGDIR(0) == ARGDIR(1); + else if (ARG_TYPE(0) == TYPE::ENTITY && ARG_TYPE(1) == TYPE::ENTITY) + RESULTINT = ARGENTITY(0) == ARGENTITY(1); + else if (ARG_TYPE(0) == TYPE::LOCATION && ARG_TYPE(1) == TYPE::LOCATION) + RESULTINT = (ARGLOCATION(0).x == ARGLOCATION(1).x + && ARGLOCATION(0).y == ARGLOCATION(1).y + && ARGLOCATION(0).m == ARGLOCATION(1).m); + else if (ARG_TYPE(0) == TYPE::AREA && ARG_TYPE(1) == TYPE::AREA) + RESULTINT = ARGAREA(0) == ARGAREA(1); /* Probably not that great an idea... */ + else if (ARG_TYPE(0) == TYPE::SPELL && ARG_TYPE(1) == TYPE::SPELL) + RESULTINT = ARGSPELL(0) == ARGSPELL(1); + else if (ARG_TYPE(0) == TYPE::INVOCATION && ARG_TYPE(1) == TYPE::INVOCATION) + RESULTINT = ARGINVOCATION(0) == ARGINVOCATION(1); else { - intify (&args[0]); - intify (&args[1]); - RESULTINT = ARGINT (0) == ARGINT (1); + intify(&args[0]); + intify(&args[1]); + RESULTINT = ARGINT(0) == ARGINT(1); } return 0; } -static int fun_bitand (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_bitand(env_t *, int, val_t *result, val_t *args) { - RESULTINT = ARGINT (0) & ARGINT (1); + RESULTINT = ARGINT(0) & ARGINT(1); return 0; } -static int fun_bitor (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_bitor(env_t *, int, val_t *result, val_t *args) { - RESULTINT = ARGINT (0) | ARGINT (1); + RESULTINT = ARGINT(0) | ARGINT(1); return 0; } -static int fun_bitxor (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_bitxor(env_t *, int, val_t *result, val_t *args) { - RESULTINT = ARGINT (0) ^ ARGINT (1); + RESULTINT = ARGINT(0) ^ ARGINT(1); return 0; } -static int fun_bitshl (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_bitshl(env_t *, int, val_t *result, val_t *args) { - RESULTINT = ARGINT (0) << ARGINT (1); + RESULTINT = ARGINT(0) << ARGINT(1); return 0; } -static int fun_bitshr (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_bitshr(env_t *, int, val_t *result, val_t *args) { - RESULTINT = ARGINT (0) >> ARGINT (1); + RESULTINT = ARGINT(0) >> ARGINT(1); return 0; } -static int fun_max (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_max(env_t *, int, val_t *result, val_t *args) { - RESULTINT = MAX (ARGINT (0), ARGINT (1)); + RESULTINT = max(ARGINT(0), ARGINT(1)); return 0; } -static int fun_min (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_min(env_t *, int, val_t *result, val_t *args) { - RESULTINT = MIN (ARGINT (0), ARGINT (1)); + RESULTINT = min(ARGINT(0), ARGINT(1)); return 0; } -static int -fun_if_then_else (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_if_then_else(env_t *, int, val_t *result, val_t *args) { - if (ARGINT (0)) - magic_copy_var (result, &args[1]); + if (ARGINT(0)) + magic_copy_var(result, &args[1]); else - magic_copy_var (result, &args[2]); + magic_copy_var(result, &args[2]); return 0; } -void -magic_area_rect (int *m, int *x, int *y, int *width, int *height, - area_t * area) +void magic_area_rect(int *m, int *x, int *y, int *width, int *height, + area_t *area) { switch (area->ty) { - case AREA_UNION: + case AREA::UNION: break; - case AREA_LOCATION: + case AREA::LOCATION: *m = area->a.a_loc.m; *x = area->a.a_loc.x; *y = area->a.a_loc.y; @@ -460,7 +487,7 @@ magic_area_rect (int *m, int *x, int *y, int *width, int *height, *height = 1; break; - case AREA_RECT: + case AREA::RECT: *m = area->a.a_rect.loc.m; *x = area->a.a_rect.loc.x; *y = area->a.a_rect.loc.y; @@ -468,38 +495,38 @@ magic_area_rect (int *m, int *x, int *y, int *width, int *height, *height = area->a.a_rect.height; break; - case AREA_BAR: + case AREA::BAR: { - int tx = area->a.a_bar.loc.x; - int ty = area->a.a_bar.loc.y; - int twidth = area->a.a_bar.width; - int tdepth = area->a.a_bar.width; + int tx = area->a.a_bar.loc.x; + int ty = area->a.a_bar.loc.y; + int twidth = area->a.a_bar.width; + int tdepth = area->a.a_bar.width; *m = area->a.a_bar.loc.m; switch (area->a.a_bar.dir) { - case DIR_S: + case DIR::S: *x = tx - twidth; *y = ty; *width = twidth * 2 + 1; *height = tdepth; break; - case DIR_W: + case DIR::W: *x = tx - tdepth; *y = ty - twidth; *width = tdepth; *height = twidth * 2 + 1; break; - case DIR_N: + case DIR::N: *x = tx - twidth; *y = ty - tdepth; *width = twidth * 2 + 1; *height = tdepth; break; - case DIR_E: + case DIR::E: *x = tx; *y = ty - twidth; *width = tdepth; @@ -507,7 +534,7 @@ magic_area_rect (int *m, int *x, int *y, int *width, int *height, break; default: - fprintf (stderr, + FPRINTF(stderr, "Error: Trying to compute area of NE/SE/NW/SW-facing bar"); *x = tx; *y = ty; @@ -518,124 +545,162 @@ magic_area_rect (int *m, int *x, int *y, int *width, int *height, } } -int magic_location_in_area (int m, int x, int y, area_t * area) +int magic_location_in_area(int m, int x, int y, area_t *area) { switch (area->ty) { - case AREA_UNION: - return magic_location_in_area (m, x, y, area->a.a_union[0]) - || magic_location_in_area (m, x, y, area->a.a_union[1]); - case AREA_LOCATION: - case AREA_RECT: - case AREA_BAR: + case AREA::UNION: + return magic_location_in_area(m, x, y, area->a.a_union[0]) + || magic_location_in_area(m, x, y, area->a.a_union[1]); + case AREA::LOCATION: + case AREA::RECT: + case AREA::BAR: { - int am; - int ax, ay, awidth, aheight; - magic_area_rect (&am, &ax, &ay, &awidth, &aheight, area); + int am; + int ax, ay, awidth, aheight; + magic_area_rect(&am, &ax, &ay, &awidth, &aheight, area); return (am == m && (x >= ax) && (y >= ay) && (x < ax + awidth) && (y < ay + aheight)); } default: - fprintf (stderr, "INTERNAL ERROR: Invalid area\n"); + FPRINTF(stderr, "INTERNAL ERROR: Invalid area\n"); return 0; } } -static int fun_is_in (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_is_in(env_t *, int, val_t *result, val_t *args) { - RESULTINT = magic_location_in_area (ARGLOCATION (0).m, - ARGLOCATION (0).x, - ARGLOCATION (0).y, ARGAREA (1)); + RESULTINT = magic_location_in_area(ARGLOCATION(0).m, + ARGLOCATION(0).x, + ARGLOCATION(0).y, ARGAREA(1)); return 0; } -static int fun_skill (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_skill(env_t *, int, val_t *result, val_t *args) { - if (ETY (0) != BL_PC - || ARGINT (1) < 0 - || ARGINT (1) >= MAX_SKILL - || ARGPC (0)->status.skill[ARGINT (1)].id != ARGINT (1)) + if (ENTITY_TYPE(0) != BL::PC + // don't convert to enum until after the range check + || ARGINT(1) < 0 + || ARGINT(1) >= uint16_t(MAX_SKILL)) + { RESULTINT = 0; + } else - RESULTINT = ARGPC (0)->status.skill[ARGINT (1)].lv; + { + SkillID id = static_cast<SkillID>(ARGINT(1)); + RESULTINT = ARGPC(0)->status.skill[id].lv; + } return 0; } -static int -fun_has_shroud (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_has_shroud(env_t *, int, val_t *result, val_t *args) { - RESULTINT = (ETY (0) == BL_PC && ARGPC (0)->state.shroud_active); + RESULTINT = (ENTITY_TYPE(0) == BL::PC && ARGPC(0)->state.shroud_active); return 0; } -#define BATTLE_GETTER(name) static int fun_get_##name(env_t *env, int args_nr, val_t *result, val_t *args) { RESULTINT = battle_get_##name(ARGENTITY(0)); return 0; } - -BATTLE_GETTER (str); -BATTLE_GETTER (agi); -BATTLE_GETTER (vit); -BATTLE_GETTER (dex); -BATTLE_GETTER (luk); -BATTLE_GETTER (int); -BATTLE_GETTER (lv); -BATTLE_GETTER (hp); -BATTLE_GETTER (mdef); -BATTLE_GETTER (def); -BATTLE_GETTER (max_hp); -BATTLE_GETTER (dir); +#define BATTLE_GETTER(name) \ +static \ +int fun_get_##name(env_t *, int, val_t *result, val_t *args) \ +{ \ + RESULTINT = battle_get_##name(ARGENTITY(0)); \ + return 0; \ +} + +BATTLE_GETTER(str) +BATTLE_GETTER(agi) +BATTLE_GETTER(vit) +BATTLE_GETTER(dex) +BATTLE_GETTER(luk) +BATTLE_GETTER(int) +BATTLE_GETTER(lv) +BATTLE_GETTER(hp) +BATTLE_GETTER(mdef) +BATTLE_GETTER(def) +BATTLE_GETTER(max_hp) +static +int fun_get_dir(env_t *, int, val_t *result, val_t *args) +{ + RESULTDIR = battle_get_dir(ARGENTITY(0)); + return 0; +} -#define MMO_GETTER(name) static int fun_get_##name(env_t *env, int args_nr, val_t *result, val_t *args) { \ - if (ETY(0) == BL_PC) \ - RESULTINT = ARGPC(0)->status.name; \ - else \ - RESULTINT = 0; \ - return 0; } +#define MMO_GETTER(name) \ +static \ +int fun_get_##name(env_t *, int, val_t *result, val_t *args) \ +{ \ + if (ENTITY_TYPE(0) == BL::PC) \ + RESULTINT = ARGPC(0)->status.name; \ + else \ + RESULTINT = 0; \ + return 0; \ +} -MMO_GETTER (sp); -MMO_GETTER (max_sp); +MMO_GETTER(sp) +MMO_GETTER(max_sp) -static int -fun_name_of (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_name_of(env_t *, int, val_t *result, val_t *args) { - if (TY (0) == TY_ENTITY) + if (ARG_TYPE(0) == TYPE::ENTITY) { - RESULTSTR = strdup (show_entity (ARGENTITY (0))); + RESULTSTR = strdup(show_entity(ARGENTITY(0))); return 0; } - else if (TY (0) == TY_SPELL) + else if (ARG_TYPE(0) == TYPE::SPELL) { - RESULTSTR = strdup (ARGSPELL (0)->name); + RESULTSTR = strdup(ARGSPELL(0)->name); return 0; } - else if (TY (0) == TY_INVOCATION) + else if (ARG_TYPE(0) == TYPE::INVOCATION) { - RESULTSTR = strdup (ARGINVOCATION (0)->spell->name); + RESULTSTR = strdup(ARGINVOCATION(0)->spell->name); return 0; } return 1; } /* [Freeyorp] I'm putting this one in as name_of seems to have issues with summoned or spawned mobs. */ -static int -fun_mob_id (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_mob_id(env_t *, int, val_t *result, val_t *args) { - if (ETY (0) != BL_MOB) return 1; - RESULTINT = ((struct mob_data *) (ARGENTITY(0)))->mob_class; + if (ENTITY_TYPE(0) != BL::MOB) + return 1; + RESULTINT = ((struct mob_data *)(ARGENTITY(0)))->mob_class; return 0; } -#define COPY_LOCATION(dest, src) (dest).x = (src).x; (dest).y = (src).y; (dest).m = (src).m; +inline +void COPY_LOCATION(entity_t& dest, location_t& src) +{ + dest.x = src.x; + dest.y = src.y; + dest.m = src.m; +} + +inline +void COPY_LOCATION(location_t& dest, entity_t& src) +{ + dest.x = src.x; + dest.y = src.y; + dest.m = src.m; +} -static int -fun_location (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_location(env_t *, int, val_t *result, val_t *args) { - COPY_LOCATION (RESULTLOCATION, *(ARGENTITY (0))); + COPY_LOCATION(RESULTLOCATION, *(ARGENTITY(0))); return 0; } -static int fun_random (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_random(env_t *, int, val_t *result, val_t *args) { - int delta = ARGINT (0); + int delta = ARGINT(0); if (delta < 0) delta = -delta; if (delta == 0) @@ -643,94 +708,98 @@ static int fun_random (env_t * env, int args_nr, val_t * result, val_t * args) RESULTINT = 0; return 0; } - RESULTINT = MRAND (delta); + RESULTINT = random_::to(delta); - if (ARGINT (0) < 0) + if (ARGINT(0) < 0) RESULTINT = -RESULTINT; return 0; } -static int -fun_random_dir (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_random_dir(env_t *, int, val_t *result, val_t *args) { - if (ARGINT (0)) - RESULTDIR = mt_random () & 0x7; + if (ARGINT(0)) + RESULTDIR = random_::choice({DIR::S, DIR::SW, DIR::W, DIR::NW, DIR::N, DIR::NE, DIR::E, DIR::SE}); else - RESULTDIR = (mt_random () & 0x3) * 2; + RESULTDIR = random_::choice({DIR::S, DIR::W, DIR::N, DIR::E}); return 0; } -static int -fun_hash_entity (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_hash_entity(env_t *, int, val_t *result, val_t *args) { - RESULTINT = ARGENTITY (0)->id; + RESULTINT = ARGENTITY(0)->id; return 0; } -int // ret -1: not a string, ret 1: no such item, ret 0: OK -magic_find_item (val_t * args, int index, struct item *item, int *stackable) +int // ret -1: not a string, ret 1: no such item, ret 0: OK +magic_find_item(val_t *args, int index, struct item *item, int *stackable) { struct item_data *item_data; - int must_add_sequentially; + int must_add_sequentially; - if (TY (index) == TY_INT) - item_data = itemdb_exists (ARGINT (index)); - else if (TY (index) == TY_STRING) - item_data = itemdb_searchname (ARGSTR (index)); + if (ARG_TYPE(index) == TYPE::INT) + item_data = itemdb_exists(ARGINT(index)); + else if (ARG_TYPE(index) == TYPE::STRING) + item_data = itemdb_searchname(ARGSTR(index)); else return -1; if (!item_data) return 1; - must_add_sequentially = (item_data->type == 4 || item_data->type == 5 || item_data->type == 7 || item_data->type == 8); /* Very elegant. */ + // Very elegant. + must_add_sequentially = ( + item_data->type == ItemType::WEAPON + || item_data->type == ItemType::ARMOR + || item_data->type == ItemType::_7 + || item_data->type == ItemType::_8); if (stackable) *stackable = !must_add_sequentially; - memset (item, 0, sizeof (struct item)); + memset(item, 0, sizeof(struct item)); item->nameid = item_data->nameid; item->identify = 1; return 0; } -static int -fun_count_item (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_count_item(env_t *, int, val_t *result, val_t *args) { - character_t *chr = (ETY (0) == BL_PC) ? ARGPC (0) : NULL; - int stackable; + character_t *chr = (ENTITY_TYPE(0) == BL::PC) ? ARGPC(0) : NULL; + int stackable; struct item item; - GET_ARG_ITEM (1, item, stackable); + GET_ARG_ITEM(1, item, stackable); if (!chr) return 1; - RESULTINT = pc_count_all_items (chr, item.nameid); + RESULTINT = pc_count_all_items(chr, item.nameid); return 0; } -static int -fun_is_equipped (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_is_equipped(env_t *, int, val_t *result, val_t *args) { - character_t *chr = (ETY (0) == BL_PC) ? ARGPC (0) : NULL; - int stackable; + character_t *chr = (ENTITY_TYPE(0) == BL::PC) ? ARGPC(0) : NULL; + int stackable; struct item item; - int i; - int retval = 0; + bool retval = false; - GET_ARG_ITEM (1, item, stackable); + GET_ARG_ITEM(1, item, stackable); if (!chr) return 1; - for (i = 0; i < 11; i++) + for (EQUIP i : EQUIPs) if (chr->equip_index[i] >= 0 && chr->status.inventory[chr->equip_index[i]].nameid == item.nameid) { - retval = i + 1; + retval = true; break; } @@ -738,48 +807,51 @@ fun_is_equipped (env_t * env, int args_nr, val_t * result, val_t * args) return 0; } -static int -fun_is_married (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_is_married(env_t *, int, val_t *result, val_t *args) { - RESULTINT = (ETY (0) == BL_PC && ARGPC (0)->status.partner_id); + RESULTINT = (ENTITY_TYPE(0) == BL::PC && ARGPC(0)->status.partner_id); return 0; } -static int -fun_is_dead (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_is_dead(env_t *, int, val_t *result, val_t *args) { - RESULTINT = (ETY (0) == BL_PC && pc_isdead (ARGPC (0))); + RESULTINT = (ENTITY_TYPE(0) == BL::PC && pc_isdead(ARGPC(0))); return 0; } -static int fun_is_pc (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_is_pc(env_t *, int, val_t *result, val_t *args) { - RESULTINT = (ETY (0) == BL_PC); + RESULTINT = (ENTITY_TYPE(0) == BL::PC); return 0; } -static int -fun_partner (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_partner(env_t *, int, val_t *result, val_t *args) { - if (ETY (0) == BL_PC && ARGPC (0)->status.partner_id) + if (ENTITY_TYPE(0) == BL::PC && ARGPC(0)->status.partner_id) { RESULTENTITY = (entity_t *) - map_nick2sd (map_charid2nick (ARGPC (0)->status.partner_id)); + map_nick2sd(map_charid2nick(ARGPC(0)->status.partner_id)); return 0; } else return 1; } -static int -fun_awayfrom (env_t * env, int args_nr, val_t * result, val_t * args) -{ - location_t *loc = &ARGLOCATION (0); - int dx = heading_x[ARGDIR (1)]; - int dy = heading_y[ARGDIR (1)]; - int distance = ARGINT (2); - while (distance-- && !map_is_solid (loc->m, loc->x + dx, loc->y + dy)) +static +int fun_awayfrom(env_t *, int, val_t *result, val_t *args) +{ + location_t *loc = &ARGLOCATION(0); + int dx = dirx[ARGDIR(1)]; + int dy = diry[ARGDIR(1)]; + int distance = ARGINT(2); + while (distance-- + && !bool(read_gat(loc->m, loc->x + dx, loc->y + dy) + & MapCell::UNWALKABLE)) { loc->x += dx; loc->y += dy; @@ -789,101 +861,104 @@ fun_awayfrom (env_t * env, int args_nr, val_t * result, val_t * args) return 0; } -static int fun_failed (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_failed(env_t *, int, val_t *result, val_t *args) { - RESULTINT = TY (0) == TY_FAIL; + RESULTINT = ARG_TYPE(0) == TYPE::FAIL; return 0; } -static int fun_npc (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_npc(env_t *, int, val_t *result, val_t *args) { - RESULTENTITY = (entity_t *) npc_name2id (ARGSTR (0)); + RESULTENTITY = (entity_t *) npc_name2id(ARGSTR(0)); return RESULTENTITY == NULL; } -static int fun_pc (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_pc(env_t *, int, val_t *result, val_t *args) { - RESULTENTITY = (entity_t *) map_nick2sd (ARGSTR (0)); + RESULTENTITY = (entity_t *) map_nick2sd(ARGSTR(0)); return RESULTENTITY == NULL; } -static int -fun_distance (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_distance(env_t *, int, val_t *result, val_t *args) { - if (ARGLOCATION (0).m != ARGLOCATION (1).m) - RESULTINT = INT_MAX; + if (ARGLOCATION(0).m != ARGLOCATION(1).m) + RESULTINT = 0x7fffffff; else - RESULTINT = MAX (abs (ARGLOCATION (0).x - ARGLOCATION (1).x), - abs (ARGLOCATION (0).y - ARGLOCATION (1).y)); + RESULTINT = max(abs(ARGLOCATION(0).x - ARGLOCATION(1).x), + abs(ARGLOCATION(0).y - ARGLOCATION(1).y)); return 0; } -static int -fun_rdistance (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_rdistance(env_t *, int, val_t *result, val_t *args) { - if (ARGLOCATION (0).m != ARGLOCATION (1).m) - RESULTINT = INT_MAX; + if (ARGLOCATION(0).m != ARGLOCATION(1).m) + RESULTINT = 0x7fffffff; else { - int dx = ARGLOCATION (0).x - ARGLOCATION (1).x; - int dy = ARGLOCATION (0).y - ARGLOCATION (1).y; - RESULTINT = (int) (sqrt ((dx * dx) + (dy * dy))); + int dx = ARGLOCATION(0).x - ARGLOCATION(1).x; + int dy = ARGLOCATION(0).y - ARGLOCATION(1).y; + RESULTINT = (int)(sqrt((dx * dx) + (dy * dy))); } return 0; } -static int fun_anchor (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_anchor(env_t *env, int, val_t *result, val_t *args) { - teleport_anchor_t *anchor = magic_find_anchor (ARGSTR (0)); + teleport_anchor_t *anchor = magic_find_anchor(ARGSTR(0)); if (!anchor) return 1; - magic_eval (env, result, anchor->location); + magic_eval(env, result, anchor->location); - make_area (result); - if (result->ty != TY_AREA) + make_area(result); + if (result->ty != TYPE::AREA) { - magic_clear_var (result); + magic_clear_var(result); return 1; } return 0; } -static int -fun_line_of_sight (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_line_of_sight(env_t *, int, val_t *result, val_t *args) { entity_t e1, e2; - COPY_LOCATION (e1, ARGLOCATION (0)); - COPY_LOCATION (e2, ARGLOCATION (1)); + COPY_LOCATION(e1, ARGLOCATION(0)); + COPY_LOCATION(e2, ARGLOCATION(1)); - RESULTINT = battle_check_range (&e1, &e2, 0); + RESULTINT = battle_check_range(&e1, &e2, 0); return 0; } -void magic_random_location (location_t * dest, area_t * area) +void magic_random_location(location_t *dest, area_t *area) { switch (area->ty) { - case AREA_UNION: + case AREA::UNION: { - int rv = MRAND (area->size); - if (rv < area->a.a_union[0]->size) - magic_random_location (dest, area->a.a_union[0]); + if (random_::chance({area->a.a_union[0]->size, area->size})) + magic_random_location(dest, area->a.a_union[0]); else - magic_random_location (dest, area->a.a_union[1]); + magic_random_location(dest, area->a.a_union[1]); break; } - case AREA_LOCATION: - case AREA_RECT: - case AREA_BAR: + case AREA::LOCATION: + case AREA::RECT: + case AREA::BAR: { - int m, x, y, w, h; - magic_area_rect (&m, &x, &y, &w, &h, area); + int m, x, y, w, h; + magic_area_rect(&m, &x, &y, &w, &h, area); if (w <= 1) w = 1; @@ -891,73 +966,49 @@ void magic_random_location (location_t * dest, area_t * area) if (h <= 1) h = 1; - x += MRAND (w); - y += MRAND (h); - - if (!map_is_solid (m, x, y)) - { - int start_x = x; - int start_y = y; - int i; - int initial_dir = mt_random () & 0x7; - int dir = initial_dir; - - /* try all directions, up to a distance to 10, for a free slot */ - do - { - x = start_x; - y = start_y; - - for (i = 0; i < 10 && map_is_solid (m, x, y); i++) - { - x += heading_x[dir]; - y += heading_y[dir]; - } - - dir = (dir + 1) & 0x7; - } - while (map_is_solid (m, x, y) && dir != initial_dir); - - } - /* We've tried our best. If the map is still solid, the engine will automatically randomise the target location if we try to warp. */ + // This is not exactly the same as the old logic, + // but it's better. + auto pair = map_randfreecell(m, x, y, w, h); dest->m = m; - dest->x = x; - dest->y = y; + dest->x = pair.first; + dest->y = pair.second; break; } default: - fprintf (stderr, "Unknown area type %d\n", area->ty); + FPRINTF(stderr, "Unknown area type %d\n", + area->ty); } } -static int -fun_pick_location (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_pick_location(env_t *, int, val_t *result, val_t *args) { - magic_random_location (&result->v.v_location, ARGAREA (0)); + magic_random_location(&result->v.v_location, ARGAREA(0)); return 0; } -static int -fun_read_script_int (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_read_script_int(env_t *, int, val_t *result, val_t *args) { - entity_t *subject_p = ARGENTITY (0); - char *var_name = ARGSTR (1); + entity_t *subject_p = ARGENTITY(0); + char *var_name = ARGSTR(1); - if (subject_p->type != BL_PC) + if (subject_p->type != BL::PC) return 1; - RESULTINT = pc_readglobalreg ((character_t *) subject_p, var_name); + RESULTINT = pc_readglobalreg((character_t *) subject_p, var_name); return 0; } -static int fun_rbox (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_rbox(env_t *, int, val_t *result, val_t *args) { - location_t loc = ARGLOCATION (0); - int radius = ARGINT (1); + location_t loc = ARGLOCATION(0); + int radius = ARGINT(1); - RESULTAREA = area_new (AREA_RECT); + RESULTAREA = area_new(AREA::RECT); RESULTAREA->a.a_rect.loc.m = loc.m; RESULTAREA->a.a_rect.loc.x = loc.x - radius; RESULTAREA->a.a_rect.loc.y = loc.y - radius; @@ -967,79 +1018,83 @@ static int fun_rbox (env_t * env, int args_nr, val_t * result, val_t * args) return 0; } -static int -fun_running_status_update (env_t * env, int args_nr, val_t * result, - val_t * args) +static +int fun_running_status_update(env_t *, int, val_t *result, val_t *args) { - if (ETY (0) != BL_PC && ETY (0) != BL_MOB) + if (ENTITY_TYPE(0) != BL::PC && ENTITY_TYPE(0) != BL::MOB) return 1; - RESULTINT = battle_get_sc_data (ARGENTITY (0))[ARGINT (1)].timer != -1; + StatusChange sc = StatusChange(ARGINT(1)); + RESULTINT = bool(battle_get_sc_data(ARGENTITY(0))[sc].timer); return 0; } -static int -fun_status_option (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_status_option(env_t *, int, val_t *result, val_t *args) { RESULTINT = - ((((struct map_session_data *) ARGENTITY (0))-> - status.option & ARGINT (0)) != 0); + (bool(((struct map_session_data *) ARGENTITY(0))-> + status.option & Option(ARGINT(0)))); return 0; } -static int -fun_element (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_element(env_t *, int, val_t *result, val_t *args) { - RESULTINT = battle_get_element (ARGENTITY (0)) % 10; + RESULTINT = static_cast<int>(battle_get_element(ARGENTITY(0)).element); return 0; } -static int -fun_element_level (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_element_level(env_t *, int, val_t *result, val_t *args) { - RESULTINT = battle_get_element (ARGENTITY (0)) / 10; + RESULTINT = battle_get_element(ARGENTITY(0)).level; return 0; } -static int fun_index (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_index(env_t *, int, val_t *result, val_t *args) { - RESULTINT = ARGSPELL (0)->index; + RESULTINT = ARGSPELL(0)->index; return 0; } -static int -fun_is_exterior (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_is_exterior(env_t *, int, val_t *result, val_t *args) { - RESULTINT = map[ARGLOCATION (0).m].name[4] == '1'; + RESULTINT = map[ARGLOCATION(0).m].name[4] == '1'; return 0; } -static int -fun_contains_string (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_contains_string(env_t *, int, val_t *result, val_t *args) { - RESULTINT = NULL != strstr (ARGSTR (0), ARGSTR (1)); + RESULTINT = NULL != strstr(ARGSTR(0), ARGSTR(1)); return 0; } -static int fun_strstr (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_strstr(env_t *, int, val_t *result, val_t *args) { - char *offset = strstr (ARGSTR (0), ARGSTR (1)); - RESULTINT = offset - ARGSTR (0); + char *offset = strstr(ARGSTR(0), ARGSTR(1)); + RESULTINT = offset - ARGSTR(0); return offset == NULL; } -static int fun_strlen (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_strlen(env_t *, int, val_t *result, val_t *args) { - RESULTINT = strlen (ARGSTR (0)); + RESULTINT = strlen(ARGSTR(0)); return 0; } -static int fun_substr (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_substr(env_t *, int, val_t *result, val_t *args) { - const char *src = ARGSTR (0); - const int slen = strlen (src); - int offset = ARGINT (1); - int len = ARGINT (2); + const char *src = ARGSTR(0); + const int slen = strlen(src); + int offset = ARGINT(1); + int len = ARGINT(2); if (len < 0) len = 0; @@ -1052,57 +1107,59 @@ static int fun_substr (env_t * env, int args_nr, val_t * result, val_t * args) if (offset + len > slen) len = slen - offset; - RESULTSTR = (char *) calloc (1, 1 + len); - memcpy (RESULTSTR, src + offset, len); + RESULTSTR = (char *) calloc(1, 1 + len); + memcpy(RESULTSTR, src + offset, len); return 0; } -static int fun_sqrt (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_sqrt(env_t *, int, val_t *result, val_t *args) { - RESULTINT = (int) sqrt (ARGINT (0)); + RESULTINT = (int) sqrt(ARGINT(0)); return 0; } -static int -fun_map_level (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_map_level(env_t *, int, val_t *result, val_t *args) { - RESULTINT = map[ARGLOCATION (0).m].name[4] - '0'; + RESULTINT = map[ARGLOCATION(0).m].name[4] - '0'; return 0; } -static int fun_map_nr (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_map_nr(env_t *, int, val_t *result, val_t *args) { - const char *mapname = map[ARGLOCATION (0).m].name; + const char *mapname = map[ARGLOCATION(0).m].name; RESULTINT = ((mapname[0] - '0') * 100) + ((mapname[1] - '0') * 10) + ((mapname[2] - '0')); return 0; } -static int -fun_dir_towards (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_dir_towards(env_t *, int, val_t *result, val_t *args) { - int dx; - int dy; + int dx; + int dy; - if (ARGLOCATION (0).m != ARGLOCATION (1).m) + if (ARGLOCATION(0).m != ARGLOCATION(1).m) return 1; - dx = ARGLOCATION (1).x - ARGLOCATION (0).x; - dy = ARGLOCATION (1).y - ARGLOCATION (0).y; + dx = ARGLOCATION(1).x - ARGLOCATION(0).x; + dy = ARGLOCATION(1).y - ARGLOCATION(0).y; - if (ARGINT (1)) + if (ARGINT(1)) { /* 8-direction mode */ - if (abs (dx) > abs (dy) * 2) + if (abs(dx) > abs(dy) * 2) { /* east or west */ if (dx < 0) RESULTINT = 2 /* west */ ; else RESULTINT = 6 /* east */ ; } - else if (abs (dy) > abs (dx) * 2) + else if (abs(dy) > abs(dx) * 2) { /* north or south */ if (dy > 0) RESULTINT = 0 /* south */ ; @@ -1127,7 +1184,7 @@ fun_dir_towards (env_t * env, int args_nr, val_t * result, val_t * args) else { /* 4-direction mode */ - if (abs (dx) > abs (dy)) + if (abs(dx) > abs(dy)) { /* east or west */ if (dx < 0) RESULTINT = 2 /* west */ ; @@ -1146,21 +1203,20 @@ fun_dir_towards (env_t * env, int args_nr, val_t * result, val_t * args) return 0; } -static int -fun_extract_healer_xp (env_t * env, int args_nr, val_t * result, val_t * args) +static +int fun_extract_healer_xp(env_t *, int, val_t *result, val_t *args) { - character_t *sd = (ETY (0) == BL_PC) ? ARGPC (0) : NULL; + character_t *sd = (ENTITY_TYPE(0) == BL::PC) ? ARGPC(0) : NULL; if (!sd) RESULTINT = 0; else - RESULTINT = pc_extract_healer_exp (sd, ARGINT (1)); + RESULTINT = pc_extract_healer_exp(sd, ARGINT(1)); return 0; } -#define BATTLE_RECORD2(sname, name) { sname, "e", 'i', fun_get_##name } -#define BATTLE_RECORD(name) BATTLE_RECORD2(#name, name) -static fun_t functions[] = { +static +fun_t functions[] = { {"+", "..", '.', fun_add}, {"-", "ii", 'i', fun_sub}, {"*", "ii", 'i', fun_mul}, @@ -1183,19 +1239,19 @@ static fun_t functions[] = { {"is_in", "la", 'i', fun_is_in}, {"if_then_else", "i__", '_', fun_if_then_else}, {"skill", "ei", 'i', fun_skill}, - BATTLE_RECORD (str), - BATTLE_RECORD (agi), - BATTLE_RECORD (vit), - BATTLE_RECORD (dex), - BATTLE_RECORD (luk), - BATTLE_RECORD (int), - BATTLE_RECORD2 ("level", lv), - BATTLE_RECORD (mdef), - BATTLE_RECORD (def), - BATTLE_RECORD (hp), - BATTLE_RECORD (max_hp), - BATTLE_RECORD (sp), - BATTLE_RECORD (max_sp), + {"str", "e", 'i', fun_get_str}, + {"agi", "e", 'i', fun_get_agi}, + {"vit", "e", 'i', fun_get_vit}, + {"dex", "e", 'i', fun_get_dex}, + {"luk", "e", 'i', fun_get_luk}, + {"int", "e", 'i', fun_get_int}, + {"level", "e", 'i', fun_get_lv}, + {"mdef", "e", 'i', fun_get_mdef}, + {"def", "e", 'i', fun_get_def}, + {"hp", "e", 'i', fun_get_hp}, + {"max_hp", "e", 'i', fun_get_max_hp}, + {"sp", "e", 'i', fun_get_sp}, + {"max_sp", "e", 'i', fun_get_max_sp}, {"dir", "e", 'd', fun_get_dir}, {"name_of", ".", 's', fun_name_of}, {"mob_id", "e", 'i', fun_mob_id}, @@ -1239,17 +1295,19 @@ static fun_t functions[] = { {NULL, NULL, '.', NULL} }; -static int functions_are_sorted = 0; +static +int functions_are_sorted = 0; static -int compare_fun (const void *lhs, const void *rhs) +int compare_fun(const void *lhs, const void *rhs) { - return strcmp (((fun_t *) lhs)->name, ((fun_t *) rhs)->name); + return strcmp(((const fun_t *) lhs)->name, ((const fun_t *) rhs)->name); } -fun_t *magic_get_fun (const char *name, int *index) +fun_t *magic_get_fun(const char *name, int *index) { - static int functions_nr; + static +int functions_nr; fun_t *result; fun_t key; @@ -1261,12 +1319,12 @@ fun_t *magic_get_fun (const char *name, int *index) ++it; functions_nr = it - functions; - qsort (functions, functions_nr, sizeof (fun_t), compare_fun); + qsort(functions, functions_nr, sizeof(fun_t), compare_fun); functions_are_sorted = 1; } key.name = name; - result = (fun_t *) bsearch (&key, functions, functions_nr, sizeof (fun_t), + result = (fun_t *) bsearch(&key, functions, functions_nr, sizeof(fun_t), compare_fun); if (result && index) @@ -1275,19 +1333,20 @@ fun_t *magic_get_fun (const char *name, int *index) return result; } -static int // 1 on failure -eval_location (env_t * env, location_t * dest, e_location_t * expr) +static +int // 1 on failure +eval_location(env_t *env, location_t *dest, e_location_t *expr) { val_t m, x, y; - magic_eval (env, &m, expr->m); - magic_eval (env, &x, expr->x); - magic_eval (env, &y, expr->y); + magic_eval(env, &m, expr->m); + magic_eval(env, &x, expr->x); + magic_eval(env, &y, expr->y); - if (CHECK_TYPE (&m, TY_STRING) - && CHECK_TYPE (&x, TY_INT) && CHECK_TYPE (&y, TY_INT)) + if (CHECK_TYPE(&m, TYPE::STRING) + && CHECK_TYPE(&x, TYPE::INT) && CHECK_TYPE(&y, TYPE::INT)) { - int map_id = map_mapname2mapid (m.v.v_string); - magic_clear_var (&m); + int map_id = map_mapname2mapid(m.v.v_string); + magic_clear_var(&m); if (map_id < 0) return 1; dest->m = map_id; @@ -1297,36 +1356,37 @@ eval_location (env_t * env, location_t * dest, e_location_t * expr) } else { - magic_clear_var (&m); - magic_clear_var (&x); - magic_clear_var (&y); + magic_clear_var(&m); + magic_clear_var(&x); + magic_clear_var(&y); return 1; } } -static area_t *eval_area (env_t * env, e_area_t * expr) +static +area_t *eval_area(env_t *env, e_area_t *expr) { - area_t *area = (area_t *)malloc (sizeof (area_t)); + area_t *area = (area_t *)malloc(sizeof(area_t)); area->ty = expr->ty; switch (expr->ty) { - case AREA_LOCATION: + case AREA::LOCATION: area->size = 1; - if (eval_location (env, &area->a.a_loc, &expr->a.a_loc)) + if (eval_location(env, &area->a.a_loc, &expr->a.a_loc)) { - free (area); + free(area); return NULL; } else return area; - case AREA_UNION: + case AREA::UNION: { - int i, fail = 0; + int i, fail = 0; for (i = 0; i < 2; i++) { - area->a.a_union[i] = eval_area (env, expr->a.a_union[i]); + area->a.a_union[i] = eval_area(env, expr->a.a_union[i]); if (!area->a.a_union[i]) fail = 1; } @@ -1336,152 +1396,153 @@ static area_t *eval_area (env_t * env, e_area_t * expr) for (i = 0; i < 2; i++) { if (area->a.a_union[i]) - free_area (area->a.a_union[i]); + free_area(area->a.a_union[i]); } - free (area); + free(area); return NULL; } area->size = area->a.a_union[0]->size + area->a.a_union[1]->size; return area; } - case AREA_RECT: + case AREA::RECT: { val_t width, height; - magic_eval (env, &width, expr->a.a_rect.width); - magic_eval (env, &height, expr->a.a_rect.height); + magic_eval(env, &width, expr->a.a_rect.width); + magic_eval(env, &height, expr->a.a_rect.height); area->a.a_rect.width = width.v.v_int; area->a.a_rect.height = height.v.v_int; - if (CHECK_TYPE (&width, TY_INT) - && CHECK_TYPE (&height, TY_INT) - && !eval_location (env, &(area->a.a_rect.loc), + if (CHECK_TYPE(&width, TYPE::INT) + && CHECK_TYPE(&height, TYPE::INT) + && !eval_location(env, &(area->a.a_rect.loc), &expr->a.a_rect.loc)) { area->size = area->a.a_rect.width * area->a.a_rect.height; - magic_clear_var (&width); - magic_clear_var (&height); + magic_clear_var(&width); + magic_clear_var(&height); return area; } else { - free (area); - magic_clear_var (&width); - magic_clear_var (&height); + free(area); + magic_clear_var(&width); + magic_clear_var(&height); return NULL; } } - case AREA_BAR: + case AREA::BAR: { val_t width, depth, dir; - magic_eval (env, &width, expr->a.a_bar.width); - magic_eval (env, &depth, expr->a.a_bar.depth); - magic_eval (env, &dir, expr->a.a_bar.dir); + magic_eval(env, &width, expr->a.a_bar.width); + magic_eval(env, &depth, expr->a.a_bar.depth); + magic_eval(env, &dir, expr->a.a_bar.dir); area->a.a_bar.width = width.v.v_int; area->a.a_bar.depth = depth.v.v_int; - area->a.a_bar.dir = dir.v.v_int; + area->a.a_bar.dir = dir.v.v_dir; - if (CHECK_TYPE (&width, TY_INT) - && CHECK_TYPE (&depth, TY_INT) - && CHECK_TYPE (&dir, TY_DIR) - && !eval_location (env, &area->a.a_bar.loc, + if (CHECK_TYPE(&width, TYPE::INT) + && CHECK_TYPE(&depth, TYPE::INT) + && CHECK_TYPE(&dir, TYPE::DIR) + && !eval_location(env, &area->a.a_bar.loc, &expr->a.a_bar.loc)) { area->size = (area->a.a_bar.width * 2 + 1) * area->a.a_bar.depth; - magic_clear_var (&width); - magic_clear_var (&depth); - magic_clear_var (&dir); + magic_clear_var(&width); + magic_clear_var(&depth); + magic_clear_var(&dir); return area; } else { - free (area); - magic_clear_var (&width); - magic_clear_var (&depth); - magic_clear_var (&dir); + free(area); + magic_clear_var(&width); + magic_clear_var(&depth); + magic_clear_var(&dir); return NULL; } } default: - fprintf (stderr, "INTERNAL ERROR: Unknown area type %d\n", - area->ty); - free (area); + FPRINTF(stderr, "INTERNAL ERROR: Unknown area type %d\n", + area->ty); + free(area); return NULL; } } -static int type_key (char ty_key) +static +TYPE type_key(char ty_key) { switch (ty_key) { case 'i': - return TY_INT; + return TYPE::INT; case 'd': - return TY_DIR; + return TYPE::DIR; case 's': - return TY_STRING; + return TYPE::STRING; case 'e': - return TY_ENTITY; + return TYPE::ENTITY; case 'l': - return TY_LOCATION; + return TYPE::LOCATION; case 'a': - return TY_AREA; + return TYPE::AREA; case 'S': - return TY_SPELL; + return TYPE::SPELL; case 'I': - return TY_INVOCATION; + return TYPE::INVOCATION; default: - return -1; + return TYPE::NEGATIVE_1; } } -int magic_signature_check (const char *opname, const char *funname, const char *signature, - int args_nr, val_t * args, int line, int column) +int magic_signature_check(const char *opname, const char *funname, const char *signature, + int args_nr, val_t *args, int line, int column) { - int i; + int i; for (i = 0; i < args_nr; i++) { val_t *arg = &args[i]; char ty_key = signature[i]; - int ty = arg->ty; - int desired_ty = type_key (ty_key); + TYPE ty = arg->ty; + TYPE desired_ty = type_key(ty_key); - if (ty == TY_ENTITY) + if (ty == TYPE::ENTITY) { /* Dereference entities in preparation for calling function */ - arg->v.v_entity = map_id2bl (arg->v.v_int); + arg->v.v_entity = map_id2bl(arg->v.v_int); if (!arg->v.v_entity) - ty = arg->ty = TY_FAIL; + ty = arg->ty = TYPE::FAIL; } - else if (ty == TY_INVOCATION) + else if (ty == TYPE::INVOCATION) { - arg->v.v_invocation = (invocation_t *) map_id2bl (arg->v.v_int); + arg->v.v_invocation = (invocation_t *) map_id2bl(arg->v.v_int); if (!arg->v.v_entity) - ty = arg->ty = TY_FAIL; + ty = arg->ty = TYPE::FAIL; } if (!ty_key) { - fprintf (stderr, + FPRINTF(stderr, "[magic-eval]: L%d:%d: Too many arguments (%d) to %s `%s'\n", line, column, args_nr, opname, funname); return 1; } - if (ty == TY_FAIL && ty_key != '_') + if (ty == TYPE::FAIL && ty_key != '_') return 1; /* Fail `in a sane way': This is a perfectly permissible error */ - if (ty == desired_ty || desired_ty < 0 /* `dontcare' */ ) + if (ty == desired_ty || desired_ty == TYPE::NEGATIVE_1) continue; - if (ty == TY_UNDEF) + if (ty == TYPE::UNDEF) { - fprintf (stderr, + FPRINTF(stderr, "[magic-eval]: L%d:%d: Argument #%d to %s `%s' undefined\n", line, column, i + 1, opname, funname); return 1; @@ -1490,20 +1551,20 @@ int magic_signature_check (const char *opname, const char *funname, const char * /* If we are here, we have a type mismatch but no failure _yet_. Try to coerce. */ switch (desired_ty) { - case TY_INT: - intify (arg); + case TYPE::INT: + intify(arg); break; /* 100% success rate */ - case TY_STRING: - stringify (arg, 1); + case TYPE::STRING: + stringify(arg, 1); break; /* 100% success rate */ - case TY_AREA: - make_area (arg); + case TYPE::AREA: + make_area(arg); break; /* Only works for locations */ - case TY_LOCATION: - make_location (arg); + case TYPE::LOCATION: + make_location(arg); break; /* Only works for some areas */ - case TY_SPELL: - make_spell (arg); + case TYPE::SPELL: + make_spell(arg); break; /* Only works for still-active invocatoins */ default: break; /* We'll fail right below */ @@ -1512,10 +1573,11 @@ int magic_signature_check (const char *opname, const char *funname, const char * ty = arg->ty; if (ty != desired_ty) { /* Coercion failed? */ - if (ty != TY_FAIL) - fprintf (stderr, + if (ty != TYPE::FAIL) + FPRINTF(stderr, "[magic-eval]: L%d:%d: Argument #%d to %s `%s' of incorrect type (%d)\n", - line, column, i + 1, opname, funname, ty); + line, column, i + 1, opname, funname, + ty); return 1; } } @@ -1525,149 +1587,139 @@ int magic_signature_check (const char *opname, const char *funname, const char * #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wshadow" -void magic_eval (env_t * env, val_t * dest, expr_t * expr) +void magic_eval(env_t *env, val_t *dest, expr_t *expr) { -#ifdef RECENT_GCC #pragma GCC diagnostic pop -#endif switch (expr->ty) { - case EXPR_VAL: - magic_copy_var (dest, &expr->e.e_val); + case EXPR::VAL: + magic_copy_var(dest, &expr->e.e_val); break; - case EXPR_LOCATION: - if (eval_location (env, &dest->v.v_location, &expr->e.e_location)) - dest->ty = TY_FAIL; + case EXPR::LOCATION: + if (eval_location(env, &dest->v.v_location, &expr->e.e_location)) + dest->ty = TYPE::FAIL; else - dest->ty = TY_LOCATION; + dest->ty = TYPE::LOCATION; break; - case EXPR_AREA: - if ((dest->v.v_area = eval_area (env, &expr->e.e_area))) - dest->ty = TY_AREA; + case EXPR::AREA: + if ((dest->v.v_area = eval_area(env, &expr->e.e_area))) + dest->ty = TYPE::AREA; else - dest->ty = TY_FAIL; + dest->ty = TYPE::FAIL; break; - case EXPR_FUNAPP: + case EXPR::FUNAPP: { val_t arguments[MAX_ARGS]; - int args_nr = expr->e.e_funapp.args_nr; - int i; + int args_nr = expr->e.e_funapp.args_nr; + int i; fun_t *f = functions + expr->e.e_funapp.id; for (i = 0; i < args_nr; ++i) - magic_eval (env, &arguments[i], expr->e.e_funapp.args[i]); - if (magic_signature_check - ("function", f->name, f->signature, args_nr, arguments, + magic_eval(env, &arguments[i], expr->e.e_funapp.args[i]); + if (magic_signature_check("function", f->name, f->signature, args_nr, arguments, expr->e.e_funapp.line_nr, expr->e.e_funapp.column) - || f->fun (env, args_nr, dest, arguments)) - dest->ty = TY_FAIL; + || f->fun(env, args_nr, dest, arguments)) + dest->ty = TYPE::FAIL; else { - int dest_ty = type_key (f->ret_ty); - if (dest_ty != -1) + TYPE dest_ty = type_key(f->ret_ty); + if (dest_ty != TYPE::NEGATIVE_1) dest->ty = dest_ty; /* translate entity back into persistent int */ - if (dest->ty == TY_ENTITY) + if (dest->ty == TYPE::ENTITY) { if (dest->v.v_entity) dest->v.v_int = dest->v.v_entity->id; else - dest->ty = TY_FAIL; + dest->ty = TYPE::FAIL; } } for (i = 0; i < args_nr; ++i) - magic_clear_var (&arguments[i]); + magic_clear_var(&arguments[i]); break; } - case EXPR_ID: + case EXPR::ID: { - val_t v = VAR (expr->e.e_id); - magic_copy_var (dest, &v); + val_t v = VAR(expr->e.e_id); + magic_copy_var(dest, &v); break; } - case EXPR_SPELLFIELD: + case EXPR::SPELLFIELD: { val_t v; - int id = expr->e.e_field.id; - magic_eval (env, &v, expr->e.e_field.expr); + int id = expr->e.e_field.id; + magic_eval(env, &v, expr->e.e_field.expr); - if (v.ty == TY_INVOCATION) + if (v.ty == TYPE::INVOCATION) { - invocation_t *t = (invocation_t *) map_id2bl (v.v.v_int); + invocation_t *t = (invocation_t *) map_id2bl(v.v.v_int); if (!t) - dest->ty = TY_UNDEF; + dest->ty = TYPE::UNDEF; else { -#ifdef RECENT_GCC #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wshadow" -#endif env_t *env = t->env; -#ifdef RECENT_GCC #pragma GCC diagnostic pop -#endif - val_t val = VAR (id); - magic_copy_var (dest, &val); + val_t val = VAR(id); + magic_copy_var(dest, &val); } } else { - fprintf (stderr, + FPRINTF(stderr, "[magic] Attempt to access field %s on non-spell\n", env->base_env->var_name[id]); - dest->ty = TY_FAIL; + dest->ty = TYPE::FAIL; } break; } default: - fprintf (stderr, + FPRINTF(stderr, "[magic] INTERNAL ERROR: Unknown expression type %d\n", expr->ty); break; } } -#ifndef RECENT_GCC -#pragma GCC diagnostic pop -#endif -int magic_eval_int (env_t * env, expr_t * expr) +int magic_eval_int(env_t *env, expr_t *expr) { val_t result; - magic_eval (env, &result, expr); + magic_eval(env, &result, expr); - if (result.ty == TY_FAIL || result.ty == TY_UNDEF) + if (result.ty == TYPE::FAIL || result.ty == TYPE::UNDEF) return 0; - intify (&result); + intify(&result); return result.v.v_int; } -char *magic_eval_str (env_t * env, expr_t * expr) +char *magic_eval_str(env_t *env, expr_t *expr) { val_t result; - magic_eval (env, &result, expr); + magic_eval(env, &result, expr); - if (result.ty == TY_FAIL || result.ty == TY_UNDEF) - return strdup ("?"); + if (result.ty == TYPE::FAIL || result.ty == TYPE::UNDEF) + return strdup("?"); - stringify (&result, 0); + stringify(&result, 0); return result.v.v_string; } -expr_t *magic_new_expr (int ty) +expr_t *magic_new_expr(EXPR ty) { - expr_t *expr = (expr_t *) malloc (sizeof (expr_t)); + expr_t *expr = (expr_t *) malloc(sizeof(expr_t)); expr->ty = ty; return expr; } |