route all casting through a single cast_spell function.

This commit is contained in:
Enno Rehling 2017-04-30 10:00:29 +02:00
parent 1885831204
commit 294b7bf01e
8 changed files with 23 additions and 35 deletions

View File

@ -1749,15 +1749,9 @@ void do_combatmagic(battle * b, combatmagic_t was)
fighter *fig = co->magician.fig; fighter *fig = co->magician.fig;
const spell *sp = co->sp; const spell *sp = co->sp;
level = co->level; level = cast_spell(co);
if (!sp->cast) { if (level > 0) {
log_error("spell '%s' has no function.\n", sp->sname); pay_spell(fig->unit, sp, level, 1);
}
else {
level = sp->cast(co);
if (level > 0) {
pay_spell(fig->unit, sp, level, 1);
}
} }
} }
} }
@ -1771,7 +1765,7 @@ static int cast_combatspell(troop at, const spell * sp, int level, double force)
castorder co; castorder co;
create_castorder_combat(&co, at.fighter, sp, level, force); create_castorder_combat(&co, at.fighter, sp, level, force);
level = sp->cast(&co); level = cast_spell(&co);
free_castorder(&co); free_castorder(&co);
if (level > 0) { if (level > 0) {
pay_spell(at.fighter->unit, sp, level, 1); pay_spell(at.fighter->unit, sp, level, 1);
@ -1840,12 +1834,7 @@ static void do_combatspell(troop at)
return; return;
} }
if (!sp->cast) { level = cast_combatspell(at, sp, level, power);
log_error("spell '%s' has no function.\n", sp->sname);
}
else {
level = cast_combatspell(at, sp, level, power);
}
} }
/* Sonderattacken: Monster patzern nicht und zahlen auch keine /* Sonderattacken: Monster patzern nicht und zahlen auch keine
@ -1860,9 +1849,6 @@ static void do_extra_spell(troop at, const att * a)
if (!sp) { if (!sp) {
log_error("no such spell: '%s'", a->data.sp->name); log_error("no such spell: '%s'", a->data.sp->name);
} }
else if (sp->cast == NULL) {
log_error("spell '%s' has no function.", sp->sname);
}
else { else {
assert(a->level > 0); assert(a->level > 0);
cast_combatspell(at, sp, a->level, a->level); cast_combatspell(at, sp, a->level, a->level);

View File

@ -512,15 +512,10 @@ static void unit_castspell(unit * u, const char *name, int level)
if (sp) { if (sp) {
spellbook *book = unit_get_spellbook(u); spellbook *book = unit_get_spellbook(u);
if (spellbook_get(book, sp)) { if (spellbook_get(book, sp)) {
if (!sp->cast) { castorder co;
log_error("spell '%s' has no function.\n", sp->sname); create_castorder(&co, u, 0, sp, u->region, level, (double)level, 0, 0, 0);
} cast_spell(&co);
else { free_castorder(&co);
castorder co;
create_castorder(&co, u, 0, sp, u->region, level, (double)level, 0, 0, 0);
sp->cast(&co);
free_castorder(&co);
}
} }
} }
} }

View File

@ -595,7 +595,7 @@ static void json_spells(cJSON *json) {
continue; continue;
} }
else if (strcmp(item->string, "cast") == 0) { else if (strcmp(item->string, "cast") == 0) {
sp->cast = (spell_f)get_function(item->valuestring); sp->cast_fun = (spell_f)get_function(item->valuestring);
} }
else if (strcmp(item->string, "syntax") == 0) { else if (strcmp(item->string, "syntax") == 0) {
sp->syntax = strdup(item->valuestring); sp->syntax = strdup(item->valuestring);

View File

@ -41,7 +41,7 @@ extern "C" {
int sptyp; int sptyp;
int rank; /* Reihenfolge der Zauber */ int rank; /* Reihenfolge der Zauber */
struct spell_component *components; struct spell_component *components;
spell_f cast; spell_f cast_fun;
} spell; } spell;
typedef struct spellref { typedef struct spellref {

View File

@ -1423,7 +1423,7 @@ static int parse_spells(xmlDocPtr doc)
xmlFree(propValue); xmlFree(propValue);
} }
} }
sp->cast = (spell_f)cast; sp->cast_fun = (spell_f)cast;
xmlXPathFreeObject(result); xmlXPathFreeObject(result);
/* reading eressea/spells/spell/resource */ /* reading eressea/spells/spell/resource */

View File

@ -1433,7 +1433,7 @@ static void do_fumble(castorder * co)
case 5: case 5:
case 6: case 6:
/* Spruch gelingt, aber alle Magiepunkte weg */ /* Spruch gelingt, aber alle Magiepunkte weg */
co->level = sp->cast(co); co->level = cast_spell(co);
set_spellpoints(u, 0); set_spellpoints(u, 0);
ADDMSG(&u->faction->msgs, msg_message("patzer4", "unit region spell", ADDMSG(&u->faction->msgs, msg_message("patzer4", "unit region spell",
u, r, sp)); u, r, sp));
@ -1444,7 +1444,7 @@ static void do_fumble(castorder * co)
case 9: case 9:
default: default:
/* Spruch gelingt, alle nachfolgenden Sprüche werden 2^4 so teuer */ /* Spruch gelingt, alle nachfolgenden Sprüche werden 2^4 so teuer */
co->level = sp->cast(co); co->level = cast_spell(co);
ADDMSG(&u->faction->msgs, msg_message("patzer5", "unit region spell", ADDMSG(&u->faction->msgs, msg_message("patzer5", "unit region spell",
u, r, sp)); u, r, sp));
countspells(u, 3); countspells(u, 3);
@ -2900,7 +2900,7 @@ void magic(void)
fumbled = true; fumbled = true;
} }
else { else {
co->level = sp->cast(co); co->level = cast_spell(co);
if (co->level <= 0) { if (co->level <= 0) {
/* Kosten nur für real benötige Stufe berechnen */ /* Kosten nur für real benötige Stufe berechnen */
continue; continue;
@ -3003,6 +3003,12 @@ spell *unit_getspell(struct unit *u, const char *name, const struct locale * lan
return 0; return 0;
} }
int cast_spell(struct castorder *co)
{
const spell *sp = co->sp;
return sp->cast_fun(co);
}
static critbit_tree cb_spellbooks; static critbit_tree cb_spellbooks;
spellbook * get_spellbook(const char * name) spellbook * get_spellbook(const char * name)

View File

@ -286,6 +286,7 @@ extern "C" {
/* Hänge c-order co an die letze c-order von cll an */ /* Hänge c-order co an die letze c-order von cll an */
void free_castorders(struct castorder *co); void free_castorders(struct castorder *co);
/* Speicher wieder freigeben */ /* Speicher wieder freigeben */
int cast_spell(struct castorder *co);
/* Prüfroutinen für Zaubern */ /* Prüfroutinen für Zaubern */
int countspells(struct unit *u, int step); int countspells(struct unit *u, int step);

View File

@ -378,7 +378,7 @@ void test_multi_cast(CuTest *tc) {
test_setup(); test_setup();
sp = create_spell("fireball", 0); sp = create_spell("fireball", 0);
sp->cast = cast_fireball; sp->cast_fun = cast_fireball;
CuAssertPtrEquals(tc, sp, find_spell("fireball")); CuAssertPtrEquals(tc, sp, find_spell("fireball"));
lang = test_create_locale(); lang = test_create_locale();