From 06e0d2b3b6c43e620e5a32ecf4479cf5ef9605f6 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 9 May 2012 14:16:41 -0700 Subject: [PATCH] cleaning up castorder, unifying access to target region and caster. --- src/bindings/helpers.c | 11 ++++++----- src/kernel/magic.c | 30 ++++++++++++++++++------------ src/kernel/magic.h | 7 +++++-- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/bindings/helpers.c b/src/bindings/helpers.c index 7e8f5d5ba..c17ffeb55 100644 --- a/src/bindings/helpers.c +++ b/src/bindings/helpers.c @@ -190,7 +190,8 @@ static int lua_callspell(castorder * co) { lua_State *L = (lua_State *) global.vm_state; const char *fname = co->sp->sname; - unit *mage = co->familiar ? co->familiar : co->magician.u; + unit *caster = co_get_caster(co); + region * r = co_get_region(co); int result = -1; const char *hashpos = strchr(fname, '#'); char fbuf[64]; @@ -207,8 +208,8 @@ static int lua_callspell(castorder * co) lua_rawget(L, LUA_GLOBALSINDEX); if (lua_isfunction(L, 1)) { int nparam = 4; - tolua_pushusertype(L, co->rt, TOLUA_CAST "region"); - tolua_pushusertype(L, mage, TOLUA_CAST "unit"); + tolua_pushusertype(L, r, TOLUA_CAST "region"); + tolua_pushusertype(L, caster, TOLUA_CAST "unit"); tolua_pushnumber(L, (lua_Number) co->level); tolua_pushnumber(L, (lua_Number) co->force); if (co->sp->parameter && co->par->length) { @@ -232,7 +233,7 @@ static int lua_callspell(castorder * co) if (lua_pcall(L, nparam, 1, 0) != 0) { const char *error = lua_tostring(L, -1); log_error(("spell(%s) calling '%s': %s.\n", - unitname(mage), fname, error)); + unitname(caster), fname, error)); lua_pop(L, 1); } else { result = (int)lua_tonumber(L, -1); @@ -240,7 +241,7 @@ static int lua_callspell(castorder * co) } } else { log_error(("spell(%s) calling '%s': not a function.\n", - unitname(mage), fname)); + unitname(caster), fname)); lua_pop(L, 1); } diff --git a/src/kernel/magic.c b/src/kernel/magic.c index bc4225a6c..a422522ed 100644 --- a/src/kernel/magic.c +++ b/src/kernel/magic.c @@ -1365,7 +1365,7 @@ static void patzer(castorder * co) static void do_fumble(castorder * co) { curse *c; - region *r = co->rt; + region *r = co_get_region(co); unit *u = co->magician.u; const spell *sp = co->sp; int level = co->level; @@ -1664,7 +1664,7 @@ verify_targets(castorder * co, int *invalid, int *resist, int *success) { unit *mage = co->magician.u; const spell *sp = co->sp; - region *target_r = co->rt; + region *target_r = co_get_region(co); spellparameter *sa = co->par; int i; @@ -2040,7 +2040,7 @@ static spellparameter *add_spellparameter(region * target_r, unit * u, pword = findparam(param[i++], u->faction->locale); switch (pword) { case P_REGION: - spobj = malloc(sizeof(spllprm)); + spobj = (spllprm *)malloc(sizeof(spllprm)); spobj->flag = 0; spobj->typ = SPP_REGION; spobj->data.r = u->region; @@ -2095,20 +2095,26 @@ static spellparameter *add_spellparameter(region * target_r, unit * u, return par; } -/* ------------------------------------------------------------- */ +struct unit * co_get_caster(struct castorder * co) { + return co->_familiar ? co->_familiar : co->magician.u; +} -castorder *new_castorder(void *u, unit * u2, const spell * sp, region * r, +struct region * co_get_region(struct castorder * co) { + return co->_rtarget; +} + +castorder *new_castorder(void *caster, unit * familiar, const spell * sp, region * r, int lev, double force, int range, struct order * ord, spellparameter * p) { - castorder *corder; + castorder *corder = (castorder*)calloc(1, sizeof(castorder)); + unit * u = (unit *)caster; - corder = (castorder*)calloc(1, sizeof(castorder)); corder->magician.u = u; - corder->familiar = u2; + corder->_familiar = familiar; corder->sp = sp; corder->level = lev; corder->force = force; - corder->rt = r; + corder->_rtarget = r ? r : (familiar ? familiar->region : (u ? u->region : 0)); corder->distance = range; corder->order = copy_order(ord); corder->par = p; @@ -2122,11 +2128,11 @@ castorder *create_castorder(castorder * co, unit *caster, unit * familiar, const if (!co) co = (castorder*)calloc(1, sizeof(castorder)); co->magician.u = caster; - co->familiar = familiar; + co->_familiar = familiar; co->sp = sp; co->level = lev; co->force = force; - co->rt = r; + co->_rtarget = r ? r : (familiar ? familiar->region : (caster ? caster->region : 0)); co->distance = range; co->order = copy_order(ord); co->par = p; @@ -2834,7 +2840,7 @@ void magic(void) boolean fumbled = false; unit *u = co->magician.u; const spell *sp = co->sp; - region *target_r = co->rt; + region *target_r = co_get_region(co); /* reichen die Komponenten nicht, wird der Level reduziert. */ co->level = eff_spelllevel(u, sp, cast_level, co->distance); diff --git a/src/kernel/magic.h b/src/kernel/magic.h index d48926499..8679c0b63 100644 --- a/src/kernel/magic.h +++ b/src/kernel/magic.h @@ -130,17 +130,20 @@ typedef struct sc_mage { struct unit *u; struct fighter *fig; } magician; /* Magier (kann vom Typ struct unit oder fighter sein) */ - struct unit *familiar; /* Vertrauter, gesetzt, wenn der Spruch durch + struct unit *_familiar; /* Vertrauter, gesetzt, wenn der Spruch durch den Vertrauten gezaubert wird */ const struct spell *sp; /* Spruch */ int level; /* gewünschte Stufe oder Stufe des Magiers */ double force; /* Stärke des Zaubers */ - struct region *rt; /* Zielregion des Spruchs */ + struct region *_rtarget; /* Zielregion des Spruchs */ int distance; /* Entfernung zur Zielregion */ struct order *order; /* Befehl */ struct spellparameter *par; /* für weitere Parameter */ } castorder; + struct unit * co_get_caster(struct castorder * co); + struct region * co_get_region(struct castorder * co); + /* irgendwelche zauber: */ typedef void (*spell_f) (void *); /* normale zauber: */