optimization: select_fighters.

use select_fighters with callback to reduce the number of selected units for (most) combat spells.
This commit is contained in:
Enno Rehling 2017-05-21 12:26:04 +02:00
parent 58d9aa58a0
commit 88626fb163
3 changed files with 171 additions and 128 deletions

View File

@ -439,7 +439,7 @@ static int get_row(const side * s, int row, const side * vs)
return result;
}
static int get_unitrow(const fighter * af, const side * vs)
int get_unitrow(const fighter * af, const side * vs)
{
int row = statusrow(af->status);
if (vs == NULL) {
@ -1589,8 +1589,7 @@ static troop select_opponent(battle * b, troop at, int mindist, int maxdist)
return dt;
}
selist *fighters(battle * b, const side * vs, int minrow, int maxrow,
int mask)
selist *select_fighters(battle * b, const side * vs, int mask, select_fun cb, void *cbdata)
{
side *s;
selist *fightervp = 0;
@ -1613,8 +1612,7 @@ selist *fighters(battle * b, const side * vs, int minrow, int maxrow,
assert(mask == (FS_HELP | FS_ENEMY) || !"invalid alliance state");
}
for (fig = s->fighters; fig; fig = fig->next) {
int row = get_unitrow(fig, vs);
if (row >= minrow && row <= maxrow) {
if (cb(vs, fig, cbdata)) {
selist_push(&fightervp, fig);
}
}
@ -1623,6 +1621,26 @@ selist *fighters(battle * b, const side * vs, int minrow, int maxrow,
return fightervp;
}
struct selector {
int minrow;
int maxrow;
};
static bool select_row(const side *vs, const fighter *fig, void *cbdata)
{
struct selector *sel = (struct selector *)cbdata;
int row = get_unitrow(fig, vs);
return (row >= sel->minrow && row <= sel->maxrow);
}
selist *fighters(battle * b, const side * vs, int minrow, int maxrow, int mask)
{
struct selector sel;
sel.maxrow = maxrow;
sel.minrow = minrow;
return select_fighters(b, vs, mask, select_row, &sel);
}
static void report_failed_spell(struct battle * b, struct unit * mage, const struct spell *sp)
{
message *m = msg_message("battle::spell_failed", "unit spell", mage, sp);

View File

@ -229,15 +229,17 @@ extern "C" {
fighter * get_fighter(battle * b, const struct unit * u);
/* END battle interface */
extern void do_battles(void);
void do_battles(void);
/* for combat spells and special attacks */
enum { SELECT_ADVANCE = 0x1, SELECT_DISTANCE = 0x2, SELECT_FIND = 0x4 };
enum { ALLY_SELF, ALLY_ANY };
extern troop select_enemy(struct fighter *af, int minrow, int maxrow,
int get_unitrow(const fighter * af, const side * vs);
troop select_enemy(struct fighter *af, int minrow, int maxrow,
int select);
extern troop select_ally(struct fighter *af, int minrow, int maxrow,
troop select_ally(struct fighter *af, int minrow, int maxrow,
int allytype);
int count_enemies(struct battle *b, const struct fighter *af,
@ -246,21 +248,25 @@ extern "C" {
int calculate_armor(troop dt, const struct weapon_type *dwtype, const struct weapon_type *awtype, union variant *magres);
bool terminate(troop dt, troop at, int type, const char *damage,
bool missile);
extern void message_all(battle * b, struct message *m);
extern int hits(troop at, troop dt, weapon * awp);
extern void damage_building(struct battle *b, struct building *bldg,
void message_all(battle * b, struct message *m);
int hits(troop at, troop dt, weapon * awp);
void damage_building(struct battle *b, struct building *bldg,
int damage_abs);
typedef bool(*select_fun)(const struct side *vs, const struct fighter *fig, void *cbdata);
struct selist *select_fighters(struct battle *b, const struct side *vs, int mask, select_fun cb, void *cbdata);
struct selist *fighters(struct battle *b, const struct side *vs,
int minrow, int maxrow, int mask);
int count_allies(const struct side *as, int minrow, int maxrow,
int select, int allytype);
extern bool helping(const struct side *as, const struct side *ds);
extern void rmfighter(fighter * df, int i);
extern struct fighter *select_corpse(struct battle *b, struct fighter *af);
extern int statusrow(int status);
extern void drain_exp(struct unit *u, int d);
extern void kill_troop(troop dt);
extern void remove_troop(troop dt); /* not the same as the badly named rmtroop */
bool helping(const struct side *as, const struct side *ds);
void rmfighter(fighter * df, int i);
struct fighter *select_corpse(struct battle *b, struct fighter *af);
int statusrow(int status);
void drain_exp(struct unit *u, int d);
void kill_troop(troop dt);
void remove_troop(troop dt); /* not the same as the badly named rmtroop */
bool is_attacker(const fighter * fig);
struct battle *make_battle(struct region * r);

View File

@ -256,6 +256,17 @@ static void scramble_fighters(selist * ql)
}
}
static bool select_armed(const side *vs, const fighter *fig, void *cbdata)
{
int row = get_unitrow(fig, vs);
UNUSED_ARG(cbdata);
if (row >= FIGHT_ROW && row < BEHIND_ROW) {
return fig->alive > 0 && fig->weapons;
}
return false;
}
/* Rosthauch */
int sp_combatrosthauch(struct castorder * co)
{
@ -275,22 +286,13 @@ int sp_combatrosthauch(struct castorder * co)
return 0;
}
fgs = fighters(b, fi->side, FIGHT_ROW, BEHIND_ROW - 1, FS_ENEMY);
fgs = select_fighters(b, fi->side, FS_ENEMY, select_armed, NULL);
scramble_fighters(fgs);
for (qi = 0, ql = fgs; ql; selist_advance(&ql, &qi, 1)) {
for (qi = 0, ql = fgs; force>0 && ql; selist_advance(&ql, &qi, 1)) {
fighter *df = (fighter *)selist_get(ql, qi);
if (df->alive == 0)
continue;
if (force <= 0)
break;
/* da n MIN(force, x), sollte force maximal auf 0 sinken */
assert(force >= 0);
if (df->weapons) {
int w;
for (w = 0; df->weapons[w].type != NULL; ++w) {
weapon *wp = df->weapons;
int n = MIN(force, wp->used);
@ -326,7 +328,6 @@ int sp_combatrosthauch(struct castorder * co)
}
}
}
}
selist_free(fgs);
if (k == 0) {
@ -889,6 +890,13 @@ int sp_strong_wall(struct castorder * co)
return level;
}
static bool select_alive(const side *vs, const fighter *fig, void *cbdata)
{
UNUSED_ARG(vs);
UNUSED_ARG(cbdata);
return fig->alive > 0;
}
/** Spells: chaosrow / song of confusion.
* German Title: 'Gesang der Verwirrung'
*/
@ -915,7 +923,7 @@ int sp_chaosrow(struct castorder * co)
power = chaosrow ? (power * 40) : get_force(power, 5);
fgs = fighters(b, fi->side, FIGHT_ROW, NUMROWS, FS_ENEMY);
fgs = select_fighters(b, fi->side, FS_ENEMY, select_alive, NULL);
scramble_fighters(fgs);
for (qi = 0, ql = fgs; ql; selist_advance(&ql, &qi, 1)) {
@ -977,9 +985,18 @@ int sp_chaosrow(struct castorder * co)
return level;
}
static bool select_afraid(const side *vs, const fighter *fig, void *cbdata)
{
int row = get_unitrow(fig, vs);
UNUSED_ARG(cbdata);
if (row >= FIGHT_ROW && row <= AVOID_ROW) {
return fig->alive + fig->run.number < fig->unit->number;
}
return false;
}
/* Gesang der Furcht (Kampfzauber) */
/* Panik (Pr<50>kampfzauber) */
int flee_spell(struct castorder * co, int strength)
{
fighter * fi = co->magician.fig;
@ -994,23 +1011,20 @@ int flee_spell(struct castorder * co, int strength)
int force;
force = (int)get_force(power, strength);
if (!count_enemies(b, fi, FIGHT_ROW, AVOID_ROW, SELECT_ADVANCE | SELECT_FIND)) {
if (force<=0 || !count_enemies(b, fi, FIGHT_ROW, AVOID_ROW, SELECT_ADVANCE | SELECT_FIND)) {
msg = msg_message("sp_flee_effect_0", "mage spell", mage, sp);
message_all(b, msg);
msg_release(msg);
return 0;
}
fgs = fighters(b, fi->side, FIGHT_ROW, AVOID_ROW, FS_ENEMY);
fgs = select_fighters(b, fi->side, FS_ENEMY, select_afraid, NULL);
scramble_fighters(fgs);
for (qi = 0, ql = fgs; ql; selist_advance(&ql, &qi, 1)) {
for (qi = 0, ql = fgs; force > 0 && ql; selist_advance(&ql, &qi, 1)) {
fighter *df = (fighter *)selist_get(ql, qi);
for (n = 0; n != df->alive; ++n) {
if (force < 0)
break;
for (n = 0; force > 0 && n != df->alive; ++n) {
if (df->person[n].flags & FL_PANICED) { /* bei SPL_SONG_OF_FEAR m<>glich */
df->person[n].attack -= 1;
--force;
@ -1556,8 +1570,8 @@ int sp_healing(struct castorder * co)
message *msg;
bool use_item = has_ao_healing(mage);
/* bis zu 11 Personen pro Stufe (einen HP m<EFBFBD>ssen sie ja noch
* haben, sonst w<EFBFBD>ren sie tot) k<EFBFBD>nnen geheilt werden */
/* bis zu 11 Personen pro Stufe (einen HP muessen sie ja noch
* haben, sonst waeren sie tot) koennen geheilt werden */
if (use_item) {
healhp *= 2;
@ -1589,6 +1603,19 @@ int sp_healing(struct castorder * co)
return level;
}
static bool select_hero(const side *vs, const fighter *fig, void *cbdata)
{
UNUSED_ARG(cbdata);
if (playerrace(u_race(fig->unit))) {
int row = get_unitrow(fig, vs);
if (row >= FIGHT_ROW && row <= AVOID_ROW) {
return fig->alive + fig->run.number < fig->unit->number;
}
}
return false;
}
int sp_undeadhero(struct castorder * co)
{
fighter * fi = co->magician.fig;
@ -1603,30 +1630,20 @@ int sp_undeadhero(struct castorder * co)
int force = (int)get_force(power, 0);
double c = 0.50 + 0.02 * power;
/* Liste aus allen K<EFBFBD>mpfern */
fgs = fighters(b, fi->side, FIGHT_ROW, AVOID_ROW, FS_ENEMY | FS_HELP);
/* Liste aus allen Kaempfern */
fgs = select_fighters(b, fi->side, FS_ENEMY | FS_HELP, select_hero, NULL);
scramble_fighters(fgs);
for (qi = 0, ql = fgs; ql; selist_advance(&ql, &qi, 1)) {
for (qi = 0, ql = fgs; ql && force>0; selist_advance(&ql, &qi, 1)) {
fighter *df = (fighter *)selist_get(ql, qi);
unit *du = df->unit;
if (force <= 0)
break;
/* keine Monster */
if (!playerrace(u_race(du)))
continue;
if (df->alive + df->run.number < du->number) {
int j = 0;
/* Wieviele Untote k<>nnen wir aus dieser Einheit wecken? */
for (n = df->alive + df->run.number; n != du->number; n++) {
/* Wieviele Untote koennen wir aus dieser Einheit wecken? */
for (n = df->alive + df->run.number; force>0 && n != du->number; n++) {
if (chance(c)) {
++j;
if (--force <= 0)
break;
--force;
}
}
@ -1668,14 +1685,16 @@ int sp_undeadhero(struct castorder * co)
}
/* transfer dead people to new unit, set hitpoints to those of old unit */
clone_men(du, u, j); /* for reporting reasons, do not remove the dead */
transfermen(du, u, j);
u->hp = u->number * unit_max_hp(du);
assert(j <= df->side->casualties);
df->side->casualties -= j;
df->side->dead -= j;
/* counting total number of undead */
undead += j;
}
}
}
selist_free(fgs);
level = MIN(level, undead);