diff options
Diffstat (limited to 'src/map/magic-interpreter-base.cpp')
-rw-r--r-- | src/map/magic-interpreter-base.cpp | 229 |
1 files changed, 111 insertions, 118 deletions
diff --git a/src/map/magic-interpreter-base.cpp b/src/map/magic-interpreter-base.cpp index d86f595..1ac391a 100644 --- a/src/map/magic-interpreter-base.cpp +++ b/src/map/magic-interpreter-base.cpp @@ -19,73 +19,67 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. -#include "magic-interpreter-aux.hpp" -#include "magic-interpreter.hpp" +#include <algorithm> #include "../strings/astring.hpp" #include "../strings/xstring.hpp" #include "../io/cxxstdio.hpp" +#include "../io/cxxstdio_enums.hpp" -#include "../mmo/timer.hpp" +#include "../net/timer.hpp" +#include "magic.hpp" #include "magic-expr.hpp" - +#include "magic-interpreter.hpp" #include "pc.hpp" #include "../poison.hpp" + +namespace tmwa +{ +namespace magic +{ static -void set_int_p(val_t *v, int i, TYPE t) +void set_int(val_t *v, int i) { - v->ty = t; - v->v.v_int = i; + *v = ValInt{i}; } -#warning "This code should die" -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-macros" - -#define set_int(v, i) set_int_p(v, i, TYPE::INT) -#define set_dir(v, i) set_int_p(v, i, TYPE::DIR) +static __attribute__((unused)) +void set_dir(val_t *v, DIR d) +{ + *v = ValDir{d}; +} -#define SETTER(tty, dyn_ty, field) (val_t *v, tty x) { v->ty = dyn_ty; v->v.field = x; } static -void set_string SETTER(dumb_string, TYPE::STRING, v_string) +void set_string(val_t *v, dumb_string x) +{ + *v = ValString{x}; +} static void set_entity(val_t *v, dumb_ptr<block_list> e) { - v->ty = TYPE::ENTITY; - v->v.v_int = e->bl_id; + *v = ValEntityInt{e->bl_id}; } static void set_invocation(val_t *v, dumb_ptr<invocation> i) { - v->ty = TYPE::INVOCATION; - v->v.v_int = i->bl_id; + *v = ValInvocationInt{i->bl_id}; } static -void set_spell SETTER(dumb_ptr<spell_t>, TYPE::SPELL, v_spell) - -#define setenv(f, v, x) f(&(env->varu[v]), x) - -#define set_env_int(v, x) setenv(set_int, v, x) -#define set_env_dir(v, x) setenv(set_dir, v, x) -#define set_env_string(v, x) setenv(set_string, v, x) -#define set_env_entity(v, x) setenv(set_entity, v, x) -#define set_env_location(v, x) setenv(set_location, v, x) -#define set_env_area(v, x) setenv(set_area, v, x) -#define set_env_invocation(v, x) setenv(set_invocation, v, x) -#define set_env_spell(v, x) setenv(set_spell, v, x) - -#pragma GCC diagnostic pop +void set_spell(val_t *v, dumb_ptr<spell_t> x) +{ + *v = ValSpell{x}; +} magic_conf_t magic_conf; /* Global magic conf */ -env_t magic_default_env = { &magic_conf, NULL }; +env_t magic_default_env = { &magic_conf, nullptr }; AString magic_find_invocation(XString spellname) { @@ -102,7 +96,7 @@ dumb_ptr<spell_t> magic_find_spell(XString invocation) if (it != magic_conf.spells_by_invocation.end()) return it->second; - return NULL; + return nullptr; } /* -------------------------------------------------------------------------------- */ @@ -125,7 +119,7 @@ dumb_ptr<teleport_anchor_t> magic_find_anchor(XString name) if (it != magic_conf.anchors_by_invocation.end()) return it->second; - return NULL; + return nullptr; } /* -------------------------------------------------------------------------------- */ @@ -170,7 +164,7 @@ dumb_ptr<env_t> spell_create_env(magic_conf_t *conf, dumb_ptr<spell_t> spell, { case SPELLARG::STRING: - set_env_string(spell->arg, dumb_string::copys(param)); + set_string(&env->varu[spell->arg], dumb_string::copys(param)); break; case SPELLARG::PC: @@ -179,7 +173,7 @@ dumb_ptr<env_t> spell_create_env(magic_conf_t *conf, dumb_ptr<spell_t> spell, dumb_ptr<map_session_data> subject = map_nick2sd(name); if (!subject) subject = caster; - set_env_entity(spell->arg, subject); + set_entity(&env->varu[spell->arg], subject); break; } @@ -187,13 +181,13 @@ dumb_ptr<env_t> spell_create_env(magic_conf_t *conf, dumb_ptr<spell_t> spell, break; default: - FPRINTF(stderr, "Unexpected spellarg type %d\n", - spell->spellarg_ty); + FPRINTF(stderr, "Unexpected spellarg type %d\n"_fmt, + spell->spellarg_ty); } - set_env_entity(VAR_CASTER, caster); - set_env_int(VAR_SPELLPOWER, spellpower); - set_env_spell(VAR_SPELL, spell); + set_entity(&env->varu[VAR_CASTER], caster); + set_int(&env->varu[VAR_SPELLPOWER], spellpower); + set_spell(&env->varu[VAR_SPELL], spell); return env; } @@ -201,22 +195,22 @@ dumb_ptr<env_t> spell_create_env(magic_conf_t *conf, dumb_ptr<spell_t> spell, static void free_components(dumb_ptr<component_t> *component_holder) { - if (*component_holder == NULL) + if (*component_holder == nullptr) return; free_components(&(*component_holder)->next); (*component_holder).delete_(); - *component_holder = NULL; + *component_holder = nullptr; } -void magic_add_component(dumb_ptr<component_t> *component_holder, int id, int count) +void magic_add_component(dumb_ptr<component_t> *component_holder, ItemNameId id, int count) { if (count <= 0) return; - if (*component_holder == NULL) + if (*component_holder == nullptr) { auto component = dumb_ptr<component_t>::make(); - component->next = NULL; + component->next = nullptr; component->item_id = id; component->count = count; *component_holder = component; @@ -238,7 +232,7 @@ void magic_add_component(dumb_ptr<component_t> *component_holder, int id, int co static void copy_components(dumb_ptr<component_t> *component_holder, dumb_ptr<component_t> component) { - if (component == NULL) + if (component == nullptr) return; magic_add_component(component_holder, component->item_id, component->count); @@ -295,8 +289,10 @@ int spellguard_can_satisfy(spellguard_check_t *check, dumb_ptr<map_session_data> { interval_t casttime = check->casttime; - if (env->VAR(VAR_MIN_CASTTIME).ty == TYPE::INT) - casttime = max(casttime, static_cast<interval_t>(env->VAR(VAR_MIN_CASTTIME).v.v_int)); + if (ValInt *v = env->VAR(VAR_MIN_CASTTIME).get_if<ValInt>()) + { + casttime = std::max(casttime, static_cast<interval_t>(v->v_int)); + } caster->cast_tick = tick + casttime; /* Make sure not to cast too frequently */ @@ -308,37 +304,37 @@ int spellguard_can_satisfy(spellguard_check_t *check, dumb_ptr<map_session_data> } static -effect_set_t *spellguard_check_sub(spellguard_check_t *check, +const effect_set_t *spellguard_check_sub(spellguard_check_t *check, dumb_ptr<spellguard_t> guard, dumb_ptr<map_session_data> caster, dumb_ptr<env_t> env, int *near_miss) { - if (guard == NULL) - return NULL; + if (guard == nullptr) + return nullptr; - switch (guard->ty) + MATCH (*guard) { - case SPELLGUARD::CONDITION: - if (!magic_eval_int(env, guard->s.s_condition)) - return NULL; - break; - - case SPELLGUARD::COMPONENTS: - copy_components(&check->components, guard->s.s_components); - break; - - case SPELLGUARD::CATALYSTS: - copy_components(&check->catalysts, guard->s.s_catalysts); - break; - - case SPELLGUARD::CHOICE: + CASE (const GuardCondition&, s) + { + if (!magic_eval_int(env, s.s_condition)) + return nullptr; + } + CASE (const GuardComponents&, s) + { + copy_components(&check->components, s.s_components); + } + CASE (const GuardCatalysts&, s) + { + copy_components(&check->catalysts, s.s_catalysts); + } + CASE (const GuardChoice&, s) { spellguard_check_t altcheck = *check; - effect_set_t *retval; + const effect_set_t *retval; - altcheck.components = NULL; - altcheck.catalysts = NULL; + altcheck.components = nullptr; + altcheck.catalysts = nullptr; copy_components(&altcheck.catalysts, check->catalysts); copy_components(&altcheck.components, check->components); @@ -351,42 +347,38 @@ effect_set_t *spellguard_check_sub(spellguard_check_t *check, if (retval) return retval; else - return spellguard_check_sub(check, guard->s.s_alt, caster, + return spellguard_check_sub(check, s.s_alt, caster, env, near_miss); } - - case SPELLGUARD::MANA: - check->mana += magic_eval_int(env, guard->s.s_mana); - break; - - case SPELLGUARD::CASTTIME: - check->casttime += static_cast<interval_t>(magic_eval_int(env, guard->s.s_mana)); - break; - - case SPELLGUARD::EFFECT: + CASE (const GuardMana&, s) + { + check->mana += magic_eval_int(env, s.s_mana); + } + CASE (const GuardCastTime&, s) + { + check->casttime += static_cast<interval_t>(magic_eval_int(env, s.s_casttime)); + } + CASE (const effect_set_t&, s_effect) + { if (spellguard_can_satisfy(check, caster, env, near_miss)) - return &guard->s.s_effect; + return &s_effect; else - return NULL; - - default: - FPRINTF(stderr, "Unexpected spellguard type %d\n", - guard->ty); - return NULL; + return nullptr; + } } return spellguard_check_sub(check, guard->next, caster, env, near_miss); } static -effect_set_t *check_spellguard(dumb_ptr<spellguard_t> guard, +const effect_set_t *check_spellguard(dumb_ptr<spellguard_t> guard, dumb_ptr<map_session_data> caster, dumb_ptr<env_t> env, int *near_miss) { spellguard_check_t check; - effect_set_t *retval; - check.catalysts = NULL; - check.components = NULL; + const effect_set_t *retval; + check.catalysts = nullptr; + check.components = nullptr; check.mana = 0; check.casttime = interval_t::zero(); @@ -402,7 +394,7 @@ effect_set_t *check_spellguard(dumb_ptr<spellguard_t> guard, /* Public API */ /* -------------------------------------------------------------------------------- */ -effect_set_t *spell_trigger(dumb_ptr<spell_t> spell, dumb_ptr<map_session_data> caster, +const effect_set_t *spell_trigger(dumb_ptr<spell_t> spell, dumb_ptr<map_session_data> caster, dumb_ptr<env_t> env, int *near_miss) { dumb_ptr<spellguard_t> guard = spell->spellguard; @@ -420,10 +412,11 @@ static void spell_set_location(dumb_ptr<invocation> invocation, dumb_ptr<block_list> entity) { magic_clear_var(&invocation->env->varu[VAR_LOCATION]); - invocation->env->varu[VAR_LOCATION].ty = TYPE::LOCATION; - invocation->env->varu[VAR_LOCATION].v.v_location.m = entity->bl_m; - invocation->env->varu[VAR_LOCATION].v.v_location.x = entity->bl_x; - invocation->env->varu[VAR_LOCATION].v.v_location.y = entity->bl_y; + ValLocation v; + v.v_location.m = entity->bl_m; + v.v_location.x = entity->bl_x; + v.v_location.y = entity->bl_y; + invocation->env->varu[VAR_LOCATION] = v; } void spell_update_location(dumb_ptr<invocation> invocation) @@ -441,7 +434,7 @@ void spell_update_location(dumb_ptr<invocation> invocation) } } -dumb_ptr<invocation> spell_instantiate(effect_set_t *effect_set, dumb_ptr<env_t> env) +dumb_ptr<invocation> spell_instantiate(const effect_set_t *effect_set, dumb_ptr<env_t> env) { dumb_ptr<invocation> retval; retval.new_(); @@ -449,9 +442,8 @@ dumb_ptr<invocation> spell_instantiate(effect_set_t *effect_set, dumb_ptr<env_t> retval->env = env; - retval->caster = env->VAR(VAR_CASTER).v.v_int; - retval->spell = env->VAR(VAR_SPELL).v.v_spell; - retval->stack_size = 0; + retval->caster = env->VAR(VAR_CASTER).get_if<ValEntityInt>()->v_eid; + retval->spell = env->VAR(VAR_SPELL).get_if<ValSpell>()->v_spell; retval->current_effect = effect_set->effect; retval->trigger_effect = effect_set->at_trigger; retval->end_effect = effect_set->at_end; @@ -464,7 +456,7 @@ dumb_ptr<invocation> spell_instantiate(effect_set_t *effect_set, dumb_ptr<env_t> retval->bl_y = caster->bl_y; map_addblock(retval); - set_env_invocation(VAR_INVOCATION, retval); + set_invocation(&env->varu[VAR_INVOCATION], retval); return retval; } @@ -478,32 +470,31 @@ dumb_ptr<invocation> spell_clone_effect(dumb_ptr<invocation> base) // since this is the only call site, it is expanded here //*retval = *base; - retval->next_invocation = NULL; + retval->next_invocation = nullptr; retval->flags = INVOCATION_FLAG::ZERO; dumb_ptr<env_t> env = retval->env = clone_env(base->env); retval->spell = base->spell; retval->caster = base->caster; - retval->subject = 0; + retval->subject = BlockId(); // retval->timer = 0; - retval->stack_size = 0; // retval->stack = undef; retval->script_pos = 0; // huh? retval->current_effect = base->trigger_effect; retval->trigger_effect = base->trigger_effect; - retval->end_effect = NULL; - // retval->status_change_refs = NULL; + retval->end_effect = nullptr; + // retval->status_change_refs = nullptr; - retval->bl_id = 0; - retval->bl_prev = NULL; - retval->bl_next = NULL; + retval->bl_id = BlockId(); + retval->bl_prev = nullptr; + retval->bl_next = nullptr; retval->bl_m = base->bl_m; retval->bl_x = base->bl_x; retval->bl_y = base->bl_y; retval->bl_type = base->bl_type; retval->bl_id = map_addobject(retval); - set_env_invocation(VAR_INVOCATION, retval); + set_invocation(&env->varu[VAR_INVOCATION], retval); return retval; } @@ -517,10 +508,10 @@ void spell_bind(dumb_ptr<map_session_data> subject, dumb_ptr<invocation> invocat if (bool(invocation->flags & INVOCATION_FLAG::BOUND) || invocation->subject || invocation->next_invocation) { - int *i = NULL; + int *i = nullptr; FPRINTF(stderr, - "[magic] INTERNAL ERROR: Attempt to re-bind spell invocation `%s'\n", - invocation->spell->name); + "[magic] INTERNAL ERROR: Attempt to re-bind spell invocation `%s'\n"_fmt, + invocation->spell->name); *i = 1; return; } @@ -545,8 +536,8 @@ int spell_unbind(dumb_ptr<map_session_data> subject, dumb_ptr<invocation> invoca *seeker = invocation_->next_invocation; invocation_->flags &= ~INVOCATION_FLAG::BOUND; - invocation_->next_invocation = NULL; - invocation_->subject = 0; + invocation_->next_invocation = nullptr; + invocation_->subject = BlockId(); return 0; } @@ -555,3 +546,5 @@ int spell_unbind(dumb_ptr<map_session_data> subject, dumb_ptr<invocation> invoca return 1; } +} // namespace magic +} // namespace tmwa |