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;
const spell *sp = co->sp;
level = co->level;
if (!sp->cast) {
log_error("spell '%s' has no function.\n", sp->sname);
}
else {
level = sp->cast(co);
if (level > 0) {
pay_spell(fig->unit, sp, level, 1);
}
level = cast_spell(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;
create_castorder_combat(&co, at.fighter, sp, level, force);
level = sp->cast(&co);
level = cast_spell(&co);
free_castorder(&co);
if (level > 0) {
pay_spell(at.fighter->unit, sp, level, 1);
@ -1840,12 +1834,7 @@ static void do_combatspell(troop at)
return;
}
if (!sp->cast) {
log_error("spell '%s' has no function.\n", sp->sname);
}
else {
level = cast_combatspell(at, sp, level, power);
}
level = cast_combatspell(at, sp, level, power);
}
/* Sonderattacken: Monster patzern nicht und zahlen auch keine
@ -1860,9 +1849,6 @@ static void do_extra_spell(troop at, const att * a)
if (!sp) {
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 {
assert(a->level > 0);
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) {
spellbook *book = unit_get_spellbook(u);
if (spellbook_get(book, sp)) {
if (!sp->cast) {
log_error("spell '%s' has no function.\n", sp->sname);
}
else {
castorder co;
create_castorder(&co, u, 0, sp, u->region, level, (double)level, 0, 0, 0);
sp->cast(&co);
free_castorder(&co);
}
castorder co;
create_castorder(&co, u, 0, sp, u->region, level, (double)level, 0, 0, 0);
cast_spell(&co);
free_castorder(&co);
}
}
}

View File

@ -595,7 +595,7 @@ static void json_spells(cJSON *json) {
continue;
}
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) {
sp->syntax = strdup(item->valuestring);

View File

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

View File

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

View File

@ -1433,7 +1433,7 @@ static void do_fumble(castorder * co)
case 5:
case 6:
/* Spruch gelingt, aber alle Magiepunkte weg */
co->level = sp->cast(co);
co->level = cast_spell(co);
set_spellpoints(u, 0);
ADDMSG(&u->faction->msgs, msg_message("patzer4", "unit region spell",
u, r, sp));
@ -1444,7 +1444,7 @@ static void do_fumble(castorder * co)
case 9:
default:
/* 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",
u, r, sp));
countspells(u, 3);
@ -2900,7 +2900,7 @@ void magic(void)
fumbled = true;
}
else {
co->level = sp->cast(co);
co->level = cast_spell(co);
if (co->level <= 0) {
/* Kosten nur für real benötige Stufe berechnen */
continue;
@ -3003,6 +3003,12 @@ spell *unit_getspell(struct unit *u, const char *name, const struct locale * lan
return 0;
}
int cast_spell(struct castorder *co)
{
const spell *sp = co->sp;
return sp->cast_fun(co);
}
static critbit_tree cb_spellbooks;
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 */
void free_castorders(struct castorder *co);
/* Speicher wieder freigeben */
int cast_spell(struct castorder *co);
/* Prüfroutinen für Zaubern */
int countspells(struct unit *u, int step);

View File

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