diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2013-06-11 21:55:13 -0700 |
---|---|---|
committer | Ben Longbons <b.r.longbons@gmail.com> | 2013-06-11 23:27:33 -0700 |
commit | 8b5370313dcc00a45ea5c3e8b4c497bc00fd8e13 (patch) | |
tree | 15e8a4841af992e17794f26fc7991ed40c35bd51 /src/map/magic-expr.cpp | |
parent | 8c6072df499ef9068346fbe8313b63dbba1e4e82 (diff) | |
download | tmwa-8b5370313dcc00a45ea5c3e8b4c497bc00fd8e13.tar.gz tmwa-8b5370313dcc00a45ea5c3e8b4c497bc00fd8e13.tar.bz2 tmwa-8b5370313dcc00a45ea5c3e8b4c497bc00fd8e13.tar.xz tmwa-8b5370313dcc00a45ea5c3e8b4c497bc00fd8e13.zip |
Allegedly remove all manual memory management
Diffstat (limited to 'src/map/magic-expr.cpp')
-rw-r--r-- | src/map/magic-expr.cpp | 327 |
1 files changed, 164 insertions, 163 deletions
diff --git a/src/map/magic-expr.cpp b/src/map/magic-expr.cpp index c0eaf9f..7739847 100644 --- a/src/map/magic-expr.cpp +++ b/src/map/magic-expr.cpp @@ -16,7 +16,7 @@ #include "../poison.hpp" static -void free_area(area_t *area) +void free_area(dumb_ptr<area_t> area) { if (!area) return; @@ -31,13 +31,13 @@ void free_area(area_t *area) break; } - free(area); + area.delete_(); } static -area_t *dup_area(area_t *area) +dumb_ptr<area_t> dup_area(dumb_ptr<area_t> area) { - area_t *retval = (area_t *)malloc(sizeof(area_t)); + dumb_ptr<area_t> retval = dumb_ptr<area_t>::make(); *retval = *area; switch (area->ty) @@ -60,7 +60,7 @@ void magic_copy_var(val_t *dest, val_t *src) switch (dest->ty) { case TYPE::STRING: - dest->v.v_string = strdup(dest->v.v_string); + dest->v.v_string = dest->v.v_string.dup(); break; case TYPE::AREA: dest->v.v_area = dup_area(dest->v.v_area); @@ -76,7 +76,7 @@ void magic_clear_var(val_t *v) switch (v->ty) { case TYPE::STRING: - free(v->v.v_string); + v->v.v_string.delete_(); break; case TYPE::AREA: free_area(v->v.v_area); @@ -139,12 +139,12 @@ void stringify(val_t *v, int within_op) break; case TYPE::ENTITY: - buf = show_entity(dumb_ptr<block_list>(v->v.v_entity)); + buf = show_entity(v->v.v_entity); break; case TYPE::LOCATION: buf = STRPRINTF("<\"%s\", %d, %d>", - map[v->v.v_location.m].name, + v->v.v_location.m->name, v->v.v_location.x, v->v.v_location.y); break; @@ -161,7 +161,7 @@ void stringify(val_t *v, int within_op) case TYPE::INVOCATION: { dumb_ptr<invocation> invocation_ = within_op - ? dumb_ptr<invocation>(v->v.v_invocation) + ? v->v.v_invocation : map_id2bl(v->v.v_int)->as_spell(); buf = invocation_->spell->name; } @@ -173,7 +173,7 @@ void stringify(val_t *v, int within_op) return; } - v->v.v_string = strdup(buf.c_str()); + v->v.v_string = dumb_string::copys(buf); v->ty = TYPE::STRING; } @@ -189,18 +189,17 @@ void intify(val_t *v) } static -area_t *area_new(AREA ty) +dumb_ptr<area_t> area_new(AREA ty) { - area_t *retval; - CREATE(retval, area_t, 1); + auto retval = dumb_ptr<area_t>::make(); retval->ty = ty; return retval; } static -area_t *area_union(area_t *area, area_t *other_area) +dumb_ptr<area_t> area_union(dumb_ptr<area_t> area, dumb_ptr<area_t> other_area) { - area_t *retval = area_new(AREA::UNION); + dumb_ptr<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 */ @@ -215,7 +214,7 @@ void make_area(val_t *v) { if (v->ty == TYPE::LOCATION) { - area_t *a = (area_t *)malloc(sizeof(area_t)); + auto a = dumb_ptr<area_t>::make(); v->ty = TYPE::AREA; a->ty = AREA::LOCATION; a->a.a_loc = v->v.v_location; @@ -240,7 +239,7 @@ void make_spell(val_t *v) { if (v->ty == TYPE::INVOCATION) { - dumb_ptr<invocation> invoc = dumb_ptr<invocation>(v->v.v_invocation); + dumb_ptr<invocation> invoc = v->v.v_invocation; //invoc = (dumb_ptr<invocation>) map_id2bl(v->v.v_int); if (!invoc) v->ty = TYPE::FAIL; @@ -253,7 +252,7 @@ void make_spell(val_t *v) } static -int fun_add(env_t *, int, val_t *result, val_t *args) +int fun_add(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { if (ARG_TYPE(0) == TYPE::INT && ARG_TYPE(1) == TYPE::INT) { @@ -277,31 +276,29 @@ int fun_add(env_t *, int, val_t *result, val_t *args) 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)); + // ugh + RESULTSTR = dumb_string::copys(ARGSTR(0).str() + ARGSTR(1).str()); result->ty = TYPE::STRING; } return 0; } static -int fun_sub(env_t *, int, val_t *result, val_t *args) +int fun_sub(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = ARGINT(0) - ARGINT(1); return 0; } static -int fun_mul(env_t *, int, val_t *result, val_t *args) +int fun_mul(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = ARGINT(0) * ARGINT(1); return 0; } static -int fun_div(env_t *, int, val_t *result, val_t *args) +int fun_div(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { if (!ARGINT(1)) return 1; /* division by zero */ @@ -310,7 +307,7 @@ int fun_div(env_t *, int, val_t *result, val_t *args) } static -int fun_mod(env_t *, int, val_t *result, val_t *args) +int fun_mod(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { if (!ARGINT(1)) return 1; /* division by zero */ @@ -319,41 +316,42 @@ int fun_mod(env_t *, int, val_t *result, val_t *args) } static -int fun_or(env_t *, int, val_t *result, val_t *args) +int fun_or(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = ARGINT(0) || ARGINT(1); return 0; } static -int fun_and(env_t *, int, val_t *result, val_t *args) +int fun_and(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = ARGINT(0) && ARGINT(1); return 0; } static -int fun_not(env_t *, int, val_t *result, val_t *args) +int fun_not(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = !ARGINT(0); return 0; } static -int fun_neg(env_t *, int, val_t *result, val_t *args) +int fun_neg(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = ~ARGINT(0); return 0; } static -int fun_gte(env_t *, int, val_t *result, val_t *args) +int fun_gte(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { 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; + using namespace operators; + RESULTINT = ARGSTR(0) >= ARGSTR(1); } else { @@ -365,13 +363,14 @@ int fun_gte(env_t *, int, val_t *result, val_t *args) } static -int fun_gt(env_t *, int, val_t *result, val_t *args) +int fun_gt(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { 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; + using namespace operators; + RESULTINT = ARGSTR(0) > ARGSTR(1); } else { @@ -383,13 +382,14 @@ int fun_gt(env_t *, int, val_t *result, val_t *args) } static -int fun_eq(env_t *, int, val_t *result, val_t *args) +int fun_eq(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { 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; + using namespace operators; + RESULTINT = ARGSTR(0) == ARGSTR(1); } else if (ARG_TYPE(0) == TYPE::DIR && ARG_TYPE(1) == TYPE::DIR) RESULTINT = ARGDIR(0) == ARGDIR(1); @@ -415,56 +415,56 @@ int fun_eq(env_t *, int, val_t *result, val_t *args) } static -int fun_bitand(env_t *, int, val_t *result, val_t *args) +int fun_bitand(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = ARGINT(0) & ARGINT(1); return 0; } static -int fun_bitor(env_t *, int, val_t *result, val_t *args) +int fun_bitor(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = ARGINT(0) | ARGINT(1); return 0; } static -int fun_bitxor(env_t *, int, val_t *result, val_t *args) +int fun_bitxor(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = ARGINT(0) ^ ARGINT(1); return 0; } static -int fun_bitshl(env_t *, int, val_t *result, val_t *args) +int fun_bitshl(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = ARGINT(0) << ARGINT(1); return 0; } static -int fun_bitshr(env_t *, int, val_t *result, val_t *args) +int fun_bitshr(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = ARGINT(0) >> ARGINT(1); return 0; } static -int fun_max(env_t *, int, val_t *result, val_t *args) +int fun_max(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = max(ARGINT(0), ARGINT(1)); return 0; } static -int fun_min(env_t *, int, val_t *result, val_t *args) +int fun_min(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = min(ARGINT(0), ARGINT(1)); return 0; } static -int fun_if_then_else(env_t *, int, val_t *result, val_t *args) +int fun_if_then_else(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { if (ARGINT(0)) magic_copy_var(result, &args[1]); @@ -473,9 +473,10 @@ int fun_if_then_else(env_t *, int, val_t *result, val_t *args) return 0; } -void magic_area_rect(int *m, int *x, int *y, int *width, int *height, - area_t *area) +void magic_area_rect(map_local **m, int *x, int *y, int *width, int *height, + area_t& area_) { + area_t *area = &area_; // diff hack switch (area->ty) { case AREA::UNION: @@ -547,7 +548,7 @@ void 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(map_local *m, int x, int y, dumb_ptr<area_t> area) { switch (area->ty) { @@ -558,9 +559,9 @@ int magic_location_in_area(int m, int x, int y, area_t *area) case AREA::RECT: case AREA::BAR: { - int am; + map_local *am; int ax, ay, awidth, aheight; - magic_area_rect(&am, &ax, &ay, &awidth, &aheight, area); + magic_area_rect(&am, &ax, &ay, &awidth, &aheight, *area); return (am == m && (x >= ax) && (y >= ay) && (x < ax + awidth) && (y < ay + aheight)); @@ -572,7 +573,7 @@ int magic_location_in_area(int m, int x, int y, area_t *area) } static -int fun_is_in(env_t *, int, val_t *result, val_t *args) +int fun_is_in(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = magic_location_in_area(ARGLOCATION(0).m, ARGLOCATION(0).x, @@ -581,7 +582,7 @@ int fun_is_in(env_t *, int, val_t *result, val_t *args) } static -int fun_skill(env_t *, int, val_t *result, val_t *args) +int fun_skill(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { if (ENTITY_TYPE(0) != BL::PC // don't convert to enum until after the range check @@ -599,7 +600,7 @@ int fun_skill(env_t *, int, val_t *result, val_t *args) } static -int fun_has_shroud(env_t *, int, val_t *result, val_t *args) +int fun_has_shroud(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = (ENTITY_TYPE(0) == BL::PC && ARGPC(0)->state.shroud_active); return 0; @@ -607,7 +608,7 @@ int fun_has_shroud(env_t *, int, val_t *result, val_t *args) #define BATTLE_GETTER(name) \ static \ -int fun_get_##name(env_t *, int, val_t *result, val_t *args) \ +int fun_get_##name(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) \ { \ RESULTINT = battle_get_##name(ARGENTITY(0)); \ return 0; \ @@ -625,7 +626,7 @@ BATTLE_GETTER(mdef) BATTLE_GETTER(def) BATTLE_GETTER(max_hp) static -int fun_get_dir(env_t *, int, val_t *result, val_t *args) +int fun_get_dir(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTDIR = battle_get_dir(ARGENTITY(0)); return 0; @@ -633,7 +634,7 @@ int fun_get_dir(env_t *, int, val_t *result, val_t *args) #define MMO_GETTER(name) \ static \ -int fun_get_##name(env_t *, int, val_t *result, val_t *args) \ +int fun_get_##name(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) \ { \ if (ENTITY_TYPE(0) == BL::PC) \ RESULTINT = ARGPC(0)->status.name; \ @@ -646,21 +647,21 @@ MMO_GETTER(sp) MMO_GETTER(max_sp) static -int fun_name_of(env_t *, int, val_t *result, val_t *args) +int fun_name_of(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { if (ARG_TYPE(0) == TYPE::ENTITY) { - RESULTSTR = strdup(show_entity(ARGENTITY(0))); + RESULTSTR = dumb_string::copy(show_entity(ARGENTITY(0))); return 0; } else if (ARG_TYPE(0) == TYPE::SPELL) { - RESULTSTR = strdup(ARGSPELL(0)->name); + RESULTSTR = dumb_string::copys(ARGSPELL(0)->name); return 0; } else if (ARG_TYPE(0) == TYPE::INVOCATION) { - RESULTSTR = strdup(ARGINVOCATION(0)->spell->name); + RESULTSTR = dumb_string::copys(ARGINVOCATION(0)->spell->name); return 0; } return 1; @@ -668,7 +669,7 @@ int fun_name_of(env_t *, int, val_t *result, val_t *args) /* [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 *, int, val_t *result, val_t *args) +int fun_mob_id(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { if (ENTITY_TYPE(0) != BL::MOB) return 1; @@ -693,14 +694,14 @@ void COPY_LOCATION(location_t& dest, block_list& src) } static -int fun_location(env_t *, int, val_t *result, val_t *args) +int fun_location(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { COPY_LOCATION(RESULTLOCATION, *(ARGENTITY(0))); return 0; } static -int fun_random(env_t *, int, val_t *result, val_t *args) +int fun_random(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { int delta = ARGINT(0); if (delta < 0) @@ -718,7 +719,7 @@ int fun_random(env_t *, int, val_t *result, val_t *args) } static -int fun_random_dir(env_t *, int, val_t *result, val_t *args) +int fun_random_dir(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { if (ARGINT(0)) RESULTDIR = random_::choice({DIR::S, DIR::SW, DIR::W, DIR::NW, DIR::N, DIR::NE, DIR::E, DIR::SE}); @@ -728,14 +729,14 @@ int fun_random_dir(env_t *, int, val_t *result, val_t *args) } static -int fun_hash_entity(env_t *, int, val_t *result, val_t *args) +int fun_hash_entity(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = ARGENTITY(0)->bl_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) +magic_find_item(const_array<val_t> args, int index, struct item *item, int *stackable) { struct item_data *item_data; int must_add_sequentially; @@ -743,7 +744,7 @@ magic_find_item(val_t *args, int index, struct item *item, int *stackable) 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)); + item_data = itemdb_searchname(ARGSTR(index).c_str()); else return -1; @@ -768,7 +769,7 @@ magic_find_item(val_t *args, int index, struct item *item, int *stackable) } static -int fun_count_item(env_t *, int, val_t *result, val_t *args) +int fun_count_item(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { dumb_ptr<map_session_data> chr = (ENTITY_TYPE(0) == BL::PC) ? ARGPC(0) : NULL; int stackable; @@ -784,7 +785,7 @@ int fun_count_item(env_t *, int, val_t *result, val_t *args) } static -int fun_is_equipped(env_t *, int, val_t *result, val_t *args) +int fun_is_equipped(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { dumb_ptr<map_session_data> chr = (ENTITY_TYPE(0) == BL::PC) ? ARGPC(0) : NULL; int stackable; @@ -810,33 +811,33 @@ int fun_is_equipped(env_t *, int, val_t *result, val_t *args) } static -int fun_is_married(env_t *, int, val_t *result, val_t *args) +int fun_is_married(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = (ENTITY_TYPE(0) == BL::PC && ARGPC(0)->status.partner_id); return 0; } static -int fun_is_dead(env_t *, int, val_t *result, val_t *args) +int fun_is_dead(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = (ENTITY_TYPE(0) == BL::PC && pc_isdead(ARGPC(0))); return 0; } static -int fun_is_pc(env_t *, int, val_t *result, val_t *args) +int fun_is_pc(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = (ENTITY_TYPE(0) == BL::PC); return 0; } static -int fun_partner(env_t *, int, val_t *result, val_t *args) +int fun_partner(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { if (ENTITY_TYPE(0) == BL::PC && ARGPC(0)->status.partner_id) { RESULTENTITY = - map_nick2sd(map_charid2nick(ARGPC(0)->status.partner_id)).operator->(); + map_nick2sd(map_charid2nick(ARGPC(0)->status.partner_id)); return 0; } else @@ -844,14 +845,14 @@ int fun_partner(env_t *, int, val_t *result, val_t *args) } static -int fun_awayfrom(env_t *, int, val_t *result, val_t *args) +int fun_awayfrom(dumb_ptr<env_t>, val_t *result, const_array<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) + && !bool(read_gatp(loc->m, loc->x + dx, loc->y + dy) & MapCell::UNWALKABLE)) { loc->x += dx; @@ -863,28 +864,28 @@ int fun_awayfrom(env_t *, int, val_t *result, val_t *args) } static -int fun_failed(env_t *, int, val_t *result, val_t *args) +int fun_failed(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = ARG_TYPE(0) == TYPE::FAIL; return 0; } static -int fun_npc(env_t *, int, val_t *result, val_t *args) +int fun_npc(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { - RESULTENTITY = npc_name2id(ARGSTR(0)).operator->(); + RESULTENTITY = npc_name2id(ARGSTR(0).c_str()); return RESULTENTITY == NULL; } static -int fun_pc(env_t *, int, val_t *result, val_t *args) +int fun_pc(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { - RESULTENTITY = map_nick2sd(ARGSTR(0)).operator->(); + RESULTENTITY = map_nick2sd(ARGSTR(0).c_str()); return RESULTENTITY == NULL; } static -int fun_distance(env_t *, int, val_t *result, val_t *args) +int fun_distance(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { if (ARGLOCATION(0).m != ARGLOCATION(1).m) RESULTINT = 0x7fffffff; @@ -895,7 +896,7 @@ int fun_distance(env_t *, int, val_t *result, val_t *args) } static -int fun_rdistance(env_t *, int, val_t *result, val_t *args) +int fun_rdistance(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { if (ARGLOCATION(0).m != ARGLOCATION(1).m) RESULTINT = 0x7fffffff; @@ -903,15 +904,15 @@ int fun_rdistance(env_t *, int, val_t *result, val_t *args) { int dx = ARGLOCATION(0).x - ARGLOCATION(1).x; int dy = ARGLOCATION(0).y - ARGLOCATION(1).y; - RESULTINT = (int)(sqrt((dx * dx) + (dy * dy))); + RESULTINT = static_cast<int>(sqrt((dx * dx) + (dy * dy))); } return 0; } static -int fun_anchor(env_t *env, int, val_t *result, val_t *args) +int fun_anchor(dumb_ptr<env_t> env, val_t *result, const_array<val_t> args) { - teleport_anchor_t *anchor = magic_find_anchor(ARGSTR(0)); + dumb_ptr<teleport_anchor_t> anchor = magic_find_anchor(ARGSTR(0).str()); if (!anchor) return 1; @@ -929,7 +930,7 @@ int fun_anchor(env_t *env, int, val_t *result, val_t *args) } static -int fun_line_of_sight(env_t *, int, val_t *result, val_t *args) +int fun_line_of_sight(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { block_list e1, e2; @@ -941,7 +942,7 @@ int fun_line_of_sight(env_t *, int, val_t *result, val_t *args) return 0; } -void magic_random_location(location_t *dest, area_t *area) +void magic_random_location(location_t *dest, dumb_ptr<area_t> area) { switch (area->ty) { @@ -958,8 +959,9 @@ void magic_random_location(location_t *dest, area_t *area) case AREA::RECT: case AREA::BAR: { - int m, x, y, w, h; - magic_area_rect(&m, &x, &y, &w, &h, area); + map_local *m; + int x, y, w, h; + magic_area_rect(&m, &x, &y, &w, &h, *area); if (w <= 1) w = 1; @@ -984,27 +986,27 @@ void magic_random_location(location_t *dest, area_t *area) } static -int fun_pick_location(env_t *, int, val_t *result, val_t *args) +int fun_pick_location(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { magic_random_location(&result->v.v_location, ARGAREA(0)); return 0; } static -int fun_read_script_int(env_t *, int, val_t *result, val_t *args) +int fun_read_script_int(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { dumb_ptr<block_list> subject_p = ARGENTITY(0); - char *var_name = ARGSTR(1); + dumb_string var_name = ARGSTR(1); if (subject_p->bl_type != BL::PC) return 1; - RESULTINT = pc_readglobalreg(subject_p->as_player(), var_name); + RESULTINT = pc_readglobalreg(subject_p->as_player(), var_name.c_str()); return 0; } static -int fun_rbox(env_t *, int, val_t *result, val_t *args) +int fun_rbox(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { location_t loc = ARGLOCATION(0); int radius = ARGINT(1); @@ -1020,7 +1022,7 @@ int fun_rbox(env_t *, int, val_t *result, val_t *args) } static -int fun_running_status_update(env_t *, int, val_t *result, val_t *args) +int fun_running_status_update(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { if (ENTITY_TYPE(0) != BL::PC && ENTITY_TYPE(0) != BL::MOB) return 1; @@ -1031,66 +1033,67 @@ int fun_running_status_update(env_t *, int, val_t *result, val_t *args) } static -int fun_status_option(env_t *, int, val_t *result, val_t *args) +int fun_status_option(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = (bool((ARGPC(0))->status.option & static_cast<Option>(ARGINT(0)))); return 0; } static -int fun_element(env_t *, int, val_t *result, val_t *args) +int fun_element(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = static_cast<int>(battle_get_element(ARGENTITY(0)).element); return 0; } static -int fun_element_level(env_t *, int, val_t *result, val_t *args) +int fun_element_level(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { RESULTINT = battle_get_element(ARGENTITY(0)).level; return 0; } static -int fun_index(env_t *, int, val_t *result, val_t *args) +int fun_index(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { - RESULTINT = ARGSPELL(0)->index; + RESULTINT = ARGSPELL(0)->index_; return 0; } static -int fun_is_exterior(env_t *, int, val_t *result, val_t *args) +int fun_is_exterior(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { - RESULTINT = map[ARGLOCATION(0).m].name[4] == '1'; +#warning "Evil assumptions!" + RESULTINT = ARGLOCATION(0).m->name[4] == '1'; return 0; } static -int fun_contains_string(env_t *, int, val_t *result, val_t *args) +int fun_contains_string(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { - RESULTINT = NULL != strstr(ARGSTR(0), ARGSTR(1)); + RESULTINT = NULL != strstr(ARGSTR(0).c_str(), ARGSTR(1).c_str()); return 0; } static -int fun_strstr(env_t *, int, val_t *result, val_t *args) +int fun_strstr(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { - char *offset = strstr(ARGSTR(0), ARGSTR(1)); - RESULTINT = offset - ARGSTR(0); + const char *offset = strstr(ARGSTR(0).c_str(), ARGSTR(1).c_str()); + RESULTINT = offset - ARGSTR(0).c_str(); return offset == NULL; } static -int fun_strlen(env_t *, int, val_t *result, val_t *args) +int fun_strlen(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { - RESULTINT = strlen(ARGSTR(0)); + RESULTINT = strlen(ARGSTR(0).c_str()); return 0; } static -int fun_substr(env_t *, int, val_t *result, val_t *args) +int fun_substr(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { - const char *src = ARGSTR(0); + const char *src = ARGSTR(0).c_str(); const int slen = strlen(src); int offset = ARGINT(1); int len = ARGINT(2); @@ -1106,30 +1109,33 @@ int fun_substr(env_t *, int, val_t *result, val_t *args) if (offset + len > slen) len = slen - offset; - RESULTSTR = (char *) calloc(1, 1 + len); - memcpy(RESULTSTR, src + offset, len); + const char *begin = src + offset; + const char *end = begin + len; + RESULTSTR = dumb_string::copy(begin, end); return 0; } static -int fun_sqrt(env_t *, int, val_t *result, val_t *args) +int fun_sqrt(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { - RESULTINT = (int) sqrt(ARGINT(0)); + RESULTINT = static_cast<int>(sqrt(ARGINT(0))); return 0; } static -int fun_map_level(env_t *, int, val_t *result, val_t *args) +int fun_map_level(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { - RESULTINT = map[ARGLOCATION(0).m].name[4] - '0'; +#warning "Evil assumptions!" + RESULTINT = ARGLOCATION(0).m->name[4] - '0'; return 0; } static -int fun_map_nr(env_t *, int, val_t *result, val_t *args) +int fun_map_nr(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { - const char *mapname = map[ARGLOCATION(0).m].name; +#warning "Evil assumptions!" + const char *mapname = ARGLOCATION(0).m->name; RESULTINT = ((mapname[0] - '0') * 100) + ((mapname[1] - '0') * 10) + ((mapname[2] - '0')); @@ -1137,7 +1143,7 @@ int fun_map_nr(env_t *, int, val_t *result, val_t *args) } static -int fun_dir_towards(env_t *, int, val_t *result, val_t *args) +int fun_dir_towards(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { int dx; int dy; @@ -1203,7 +1209,7 @@ int fun_dir_towards(env_t *, int, val_t *result, val_t *args) } static -int fun_extract_healer_xp(env_t *, int, val_t *result, val_t *args) +int fun_extract_healer_xp(dumb_ptr<env_t>, val_t *result, const_array<val_t> args) { dumb_ptr<map_session_data> sd = (ENTITY_TYPE(0) == BL::PC) ? ARGPC(0) : NULL; @@ -1215,7 +1221,8 @@ int fun_extract_healer_xp(env_t *, int, val_t *result, val_t *args) } static -fun_t functions[] = { +fun_t functions[] = +{ {"+", "..", '.', fun_add}, {"-", "ii", 'i', fun_sub}, {"*", "ii", 'i', fun_mul}, @@ -1297,16 +1304,16 @@ fun_t functions[] = { static int functions_are_sorted = 0; -static +static __attribute__((deprecated)) int compare_fun(const void *lhs, const void *rhs) { - return strcmp(((const fun_t *) lhs)->name, ((const fun_t *) rhs)->name); + return strcmp(static_cast<const fun_t *>(lhs)->name, static_cast<const fun_t *>(rhs)->name); } -fun_t *magic_get_fun(const char *name, int *index) +fun_t *magic_get_fun(const std::string& name, int *index) { static -int functions_nr; + int functions_nr; fun_t *result; fun_t key; @@ -1322,9 +1329,9 @@ int functions_nr; functions_are_sorted = 1; } - key.name = name; - result = (fun_t *) bsearch(&key, functions, functions_nr, sizeof(fun_t), - compare_fun); + key.name = name.c_str(); + result = static_cast<fun_t *>(bsearch(&key, functions, functions_nr, sizeof(fun_t), + compare_fun)); if (result && index) *index = result - functions; @@ -1332,9 +1339,9 @@ int functions_nr; return result; } +// 1 on failure static -int // 1 on failure -eval_location(env_t *env, location_t *dest, e_location_t *expr) +int eval_location(dumb_ptr<env_t> env, location_t *dest, e_location_t *expr) { val_t m, x, y; magic_eval(env, &m, expr->m); @@ -1344,9 +1351,9 @@ eval_location(env_t *env, location_t *dest, e_location_t *expr) 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); + map_local *map_id = map_mapname2mapid(m.v.v_string.c_str()); magic_clear_var(&m); - if (map_id < 0) + if (!map_id) return 1; dest->m = map_id; dest->x = x.v.v_int; @@ -1363,9 +1370,10 @@ eval_location(env_t *env, location_t *dest, e_location_t *expr) } static -area_t *eval_area(env_t *env, e_area_t *expr) +dumb_ptr<area_t> eval_area(dumb_ptr<env_t> env, e_area_t& expr_) { - area_t *area = (area_t *)malloc(sizeof(area_t)); + e_area_t *expr = &expr_; // temporary hack to reduce diff + auto area = dumb_ptr<area_t>::make(); area->ty = expr->ty; switch (expr->ty) @@ -1374,7 +1382,7 @@ area_t *eval_area(env_t *env, e_area_t *expr) area->size = 1; if (eval_location(env, &area->a.a_loc, &expr->a.a_loc)) { - free(area); + area.delete_(); return NULL; } else @@ -1385,7 +1393,7 @@ area_t *eval_area(env_t *env, e_area_t *expr) 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; } @@ -1397,7 +1405,7 @@ area_t *eval_area(env_t *env, e_area_t *expr) if (area->a.a_union[i]) free_area(area->a.a_union[i]); } - free(area); + area.delete_(); return NULL; } area->size = area->a.a_union[0]->size + area->a.a_union[1]->size; @@ -1425,7 +1433,7 @@ area_t *eval_area(env_t *env, e_area_t *expr) } else { - free(area); + area.delete_(); magic_clear_var(&width); magic_clear_var(&height); return NULL; @@ -1458,7 +1466,7 @@ area_t *eval_area(env_t *env, e_area_t *expr) } else { - free(area); + area.delete_(); magic_clear_var(&width); magic_clear_var(&depth); magic_clear_var(&dir); @@ -1469,7 +1477,7 @@ area_t *eval_area(env_t *env, e_area_t *expr) default: FPRINTF(stderr, "INTERNAL ERROR: Unknown area type %d\n", area->ty); - free(area); + area.delete_(); return NULL; } } @@ -1514,13 +1522,13 @@ int magic_signature_check(const char *opname, const char *funname, const char *s if (ty == TYPE::ENTITY) { /* Dereference entities in preparation for calling function */ - arg->v.v_entity = map_id2bl(arg->v.v_int).operator->(); + arg->v.v_entity = map_id2bl(arg->v.v_int); if (!arg->v.v_entity) ty = arg->ty = TYPE::FAIL; } else if (ty == TYPE::INVOCATION) { - arg->v.v_invocation = map_id2bl(arg->v.v_int)->as_spell().operator->(); + arg->v.v_invocation = map_id2bl(arg->v.v_int)->as_spell(); if (!arg->v.v_entity) ty = arg->ty = TYPE::FAIL; } @@ -1584,11 +1592,8 @@ int magic_signature_check(const char *opname, const char *funname, const char *s return 0; } -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wshadow" -void magic_eval(env_t *env, val_t *dest, expr_t *expr) +void magic_eval(dumb_ptr<env_t> env, val_t *dest, dumb_ptr<expr_t> expr) { -#pragma GCC diagnostic pop switch (expr->ty) { case EXPR::VAL: @@ -1603,7 +1608,7 @@ void magic_eval(env_t *env, val_t *dest, expr_t *expr) break; case EXPR::AREA: - if ((dest->v.v_area = eval_area(env, &expr->e.e_area))) + if ((dest->v.v_area = eval_area(env, expr->e.e_area))) dest->ty = TYPE::AREA; else dest->ty = TYPE::FAIL; @@ -1619,8 +1624,8 @@ void magic_eval(env_t *env, val_t *dest, expr_t *expr) 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, - expr->e.e_funapp.line_nr, expr->e.e_funapp.column) - || f->fun(env, args_nr, dest, arguments)) + expr->e.e_funapp.line_nr, expr->e.e_funapp.column) + || f->fun(env, dest, const_array<val_t>(arguments, args_nr))) dest->ty = TYPE::FAIL; else { @@ -1645,7 +1650,7 @@ void magic_eval(env_t *env, val_t *dest, expr_t *expr) case EXPR::ID: { - val_t v = VAR(expr->e.e_id); + val_t v = env->VAR(expr->e.e_id); magic_copy_var(dest, &v); break; } @@ -1664,11 +1669,7 @@ void magic_eval(env_t *env, val_t *dest, expr_t *expr) dest->ty = TYPE::UNDEF; else { -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wshadow" - env_t *env = t->env; -#pragma GCC diagnostic pop - val_t val = VAR(id); + val_t val = t->env->VAR(id); magic_copy_var(dest, &val); } } @@ -1676,7 +1677,7 @@ void magic_eval(env_t *env, val_t *dest, expr_t *expr) { FPRINTF(stderr, "[magic] Attempt to access field %s on non-spell\n", - env->base_env->var_name[id]); + env->base_env->varv[id].name); dest->ty = TYPE::FAIL; } break; @@ -1690,7 +1691,7 @@ void magic_eval(env_t *env, val_t *dest, expr_t *expr) } } -int magic_eval_int(env_t *env, expr_t *expr) +int magic_eval_int(dumb_ptr<env_t> env, dumb_ptr<expr_t> expr) { val_t result; magic_eval(env, &result, expr); @@ -1703,22 +1704,22 @@ int magic_eval_int(env_t *env, expr_t *expr) return result.v.v_int; } -char *magic_eval_str(env_t *env, expr_t *expr) +std::string magic_eval_str(dumb_ptr<env_t> env, dumb_ptr<expr_t> expr) { val_t result; magic_eval(env, &result, expr); if (result.ty == TYPE::FAIL || result.ty == TYPE::UNDEF) - return strdup("?"); + return "?"; stringify(&result, 0); - return result.v.v_string; + return result.v.v_string.str(); } -expr_t *magic_new_expr(EXPR ty) +dumb_ptr<expr_t> magic_new_expr(EXPR ty) { - expr_t *expr = (expr_t *) malloc(sizeof(expr_t)); + auto expr = dumb_ptr<expr_t>::make(); expr->ty = ty; return expr; } |