cleaning up castorder, unifying access to target region and caster.

This commit is contained in:
Enno Rehling 2012-05-09 14:16:41 -07:00
parent 02559f6869
commit 06e0d2b3b6
3 changed files with 29 additions and 19 deletions

View File

@ -190,7 +190,8 @@ static int lua_callspell(castorder * co)
{ {
lua_State *L = (lua_State *) global.vm_state; lua_State *L = (lua_State *) global.vm_state;
const char *fname = co->sp->sname; 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; int result = -1;
const char *hashpos = strchr(fname, '#'); const char *hashpos = strchr(fname, '#');
char fbuf[64]; char fbuf[64];
@ -207,8 +208,8 @@ static int lua_callspell(castorder * co)
lua_rawget(L, LUA_GLOBALSINDEX); lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, 1)) { if (lua_isfunction(L, 1)) {
int nparam = 4; int nparam = 4;
tolua_pushusertype(L, co->rt, TOLUA_CAST "region"); tolua_pushusertype(L, r, TOLUA_CAST "region");
tolua_pushusertype(L, mage, TOLUA_CAST "unit"); tolua_pushusertype(L, caster, TOLUA_CAST "unit");
tolua_pushnumber(L, (lua_Number) co->level); tolua_pushnumber(L, (lua_Number) co->level);
tolua_pushnumber(L, (lua_Number) co->force); tolua_pushnumber(L, (lua_Number) co->force);
if (co->sp->parameter && co->par->length) { 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) { if (lua_pcall(L, nparam, 1, 0) != 0) {
const char *error = lua_tostring(L, -1); const char *error = lua_tostring(L, -1);
log_error(("spell(%s) calling '%s': %s.\n", log_error(("spell(%s) calling '%s': %s.\n",
unitname(mage), fname, error)); unitname(caster), fname, error));
lua_pop(L, 1); lua_pop(L, 1);
} else { } else {
result = (int)lua_tonumber(L, -1); result = (int)lua_tonumber(L, -1);
@ -240,7 +241,7 @@ static int lua_callspell(castorder * co)
} }
} else { } else {
log_error(("spell(%s) calling '%s': not a function.\n", log_error(("spell(%s) calling '%s': not a function.\n",
unitname(mage), fname)); unitname(caster), fname));
lua_pop(L, 1); lua_pop(L, 1);
} }

View File

@ -1365,7 +1365,7 @@ static void patzer(castorder * co)
static void do_fumble(castorder * co) static void do_fumble(castorder * co)
{ {
curse *c; curse *c;
region *r = co->rt; region *r = co_get_region(co);
unit *u = co->magician.u; unit *u = co->magician.u;
const spell *sp = co->sp; const spell *sp = co->sp;
int level = co->level; int level = co->level;
@ -1664,7 +1664,7 @@ verify_targets(castorder * co, int *invalid, int *resist, int *success)
{ {
unit *mage = co->magician.u; unit *mage = co->magician.u;
const spell *sp = co->sp; const spell *sp = co->sp;
region *target_r = co->rt; region *target_r = co_get_region(co);
spellparameter *sa = co->par; spellparameter *sa = co->par;
int i; int i;
@ -2040,7 +2040,7 @@ static spellparameter *add_spellparameter(region * target_r, unit * u,
pword = findparam(param[i++], u->faction->locale); pword = findparam(param[i++], u->faction->locale);
switch (pword) { switch (pword) {
case P_REGION: case P_REGION:
spobj = malloc(sizeof(spllprm)); spobj = (spllprm *)malloc(sizeof(spllprm));
spobj->flag = 0; spobj->flag = 0;
spobj->typ = SPP_REGION; spobj->typ = SPP_REGION;
spobj->data.r = u->region; spobj->data.r = u->region;
@ -2095,20 +2095,26 @@ static spellparameter *add_spellparameter(region * target_r, unit * u,
return par; 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) 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->magician.u = u;
corder->familiar = u2; corder->_familiar = familiar;
corder->sp = sp; corder->sp = sp;
corder->level = lev; corder->level = lev;
corder->force = force; corder->force = force;
corder->rt = r; corder->_rtarget = r ? r : (familiar ? familiar->region : (u ? u->region : 0));
corder->distance = range; corder->distance = range;
corder->order = copy_order(ord); corder->order = copy_order(ord);
corder->par = p; 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)); if (!co) co = (castorder*)calloc(1, sizeof(castorder));
co->magician.u = caster; co->magician.u = caster;
co->familiar = familiar; co->_familiar = familiar;
co->sp = sp; co->sp = sp;
co->level = lev; co->level = lev;
co->force = force; co->force = force;
co->rt = r; co->_rtarget = r ? r : (familiar ? familiar->region : (caster ? caster->region : 0));
co->distance = range; co->distance = range;
co->order = copy_order(ord); co->order = copy_order(ord);
co->par = p; co->par = p;
@ -2834,7 +2840,7 @@ void magic(void)
boolean fumbled = false; boolean fumbled = false;
unit *u = co->magician.u; unit *u = co->magician.u;
const spell *sp = co->sp; 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. */ /* reichen die Komponenten nicht, wird der Level reduziert. */
co->level = eff_spelllevel(u, sp, cast_level, co->distance); co->level = eff_spelllevel(u, sp, cast_level, co->distance);

View File

@ -130,17 +130,20 @@ typedef struct sc_mage {
struct unit *u; struct unit *u;
struct fighter *fig; struct fighter *fig;
} magician; /* Magier (kann vom Typ struct unit oder fighter sein) */ } 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 */ den Vertrauten gezaubert wird */
const struct spell *sp; /* Spruch */ const struct spell *sp; /* Spruch */
int level; /* gewünschte Stufe oder Stufe des Magiers */ int level; /* gewünschte Stufe oder Stufe des Magiers */
double force; /* Stärke des Zaubers */ double force; /* Stärke des Zaubers */
struct region *rt; /* Zielregion des Spruchs */ struct region *_rtarget; /* Zielregion des Spruchs */
int distance; /* Entfernung zur Zielregion */ int distance; /* Entfernung zur Zielregion */
struct order *order; /* Befehl */ struct order *order; /* Befehl */
struct spellparameter *par; /* für weitere Parameter */ struct spellparameter *par; /* für weitere Parameter */
} castorder; } castorder;
struct unit * co_get_caster(struct castorder * co);
struct region * co_get_region(struct castorder * co);
/* irgendwelche zauber: */ /* irgendwelche zauber: */
typedef void (*spell_f) (void *); typedef void (*spell_f) (void *);
/* normale zauber: */ /* normale zauber: */