optimization: read configuration once before all battles, not before or during each individual one.

This commit is contained in:
Enno Rehling 2015-11-22 15:29:43 +01:00
parent 7bbf11c6b6
commit 3bd458b5e8
6 changed files with 49 additions and 59 deletions

View File

@ -64,7 +64,7 @@
"rules.combat.herospeed": 3, "rules.combat.herospeed": 3,
"rules.combat.demon_vampire": 5, "rules.combat.demon_vampire": 5,
"rules.combat.skill_bonus": 0, "rules.combat.skill_bonus": 0,
"rules.combat.nat_armor": true, "rules.combat.nat_armor": 1,
"rules.items.loot_divisor": 2, "rules.items.loot_divisor": 2,
"rules.items.give_divisor": 2, "rules.items.give_divisor": 2,
"rules.move.owner_leave": true, "rules.move.owner_leave": true,

View File

@ -114,11 +114,6 @@ static message *msg_separator;
const troop no_troop = { 0, 0 }; const troop no_troop = { 0, 0 };
static int max_turns = 0;
static int damage_rules = 0;
static int loot_rules = 0;
static int skill_formula = 0;
#define FORMULA_ORIG 0 #define FORMULA_ORIG 0
#define FORMULA_NEW 1 #define FORMULA_NEW 1
@ -131,20 +126,44 @@ static int skill_formula = 0;
#define DAMAGE_MELEE_BONUS (1<<1) #define DAMAGE_MELEE_BONUS (1<<1)
#define DAMAGE_MISSILE_BONUS (1<<2) #define DAMAGE_MISSILE_BONUS (1<<2)
#define DAMAGE_SKILL_BONUS (1<<4) #define DAMAGE_SKILL_BONUS (1<<4)
static int max_turns;
static int damage_rules;
static int loot_rules;
static int skill_formula;
static int rule_cavalry_skill;
static int rule_population_damage;
static int rule_hero_speed;
static bool rule_anon_battle;
static int rule_goblin_bonus;
static int rule_tactics_formula;
static int rule_nat_armor;
static int rule_cavalry_mode;
static const curse_type *peace_ct, *slave_ct, *calm_ct;
/** initialize rules from configuration. /** initialize rules from configuration.
*/ */
static void static_rules(void) static void init_rules(void)
{ {
loot_rules = peace_ct = ct_find("peacezone");
config_get_int("rules.combat.loot", slave_ct = ct_find("slavery");
calm_ct = ct_find("calmmonster");
rule_nat_armor = config_get_int("rules.combat.nat_armor", 0);
rule_tactics_formula = config_get_int("rules.tactics.formula", 0);
rule_goblin_bonus = config_get_int("rules.combat.goblinbonus", 10);
rule_hero_speed = config_get_int("rules.combat.herospeed", 10);
rule_population_damage = config_get_int("rules.combat.populationdamage", 20);
rule_anon_battle = config_get_int("rules.stealth.anon_battle", 1) != 0;
rule_cavalry_mode = config_get_int("rules.cavalry.mode", 1);
rule_cavalry_skill = config_get_int("rules.cavalry.skill", 2);
loot_rules = config_get_int("rules.combat.loot",
LOOT_MONSTERS | LOOT_OTHERS | LOOT_KEEPLOOT); LOOT_MONSTERS | LOOT_OTHERS | LOOT_KEEPLOOT);
/* new formula to calculate to-hit-chance */ /* new formula to calculate to-hit-chance */
skill_formula = skill_formula = config_get_int("rules.combat.skill_formula",
config_get_int("rules.combat.skill_formula",
FORMULA_ORIG); FORMULA_ORIG);
/* maximum number of combat turns */ /* maximum number of combat turns */
max_turns = max_turns = config_get_int("rules.combat.turns", COMBAT_TURNS);
config_get_int("rules.combat.turns", COMBAT_TURNS);
/* damage calculation */ /* damage calculation */
if (config_get_int("rules.combat.critical", 1)) { if (config_get_int("rules.combat.critical", 1)) {
damage_rules |= DAMAGE_CRITICAL; damage_rules |= DAMAGE_CRITICAL;
@ -664,15 +683,14 @@ weapon_skill(const weapon_type * wtype, const unit * u, bool attacking)
static int CavalrySkill(void) static int CavalrySkill(void)
{ {
return config_get_int("rules.cavalry.skill", 2); return rule_cavalry_skill;
} }
#define BONUS_SKILL 1 #define BONUS_SKILL 1
#define BONUS_DAMAGE 2 #define BONUS_DAMAGE 2
static int CavalryBonus(const unit * u, troop enemy, int type) static int CavalryBonus(const unit * u, troop enemy, int type)
{ {
int mode = config_get_int("rules.cavalry.mode", 1); if (rule_cavalry_mode == 0) {
if (mode == 0) {
/* old rule, Eressea 1.0 compat */ /* old rule, Eressea 1.0 compat */
return (type == BONUS_SKILL) ? 2 : 0; return (type == BONUS_SKILL) ? 2 : 0;
} }
@ -1095,7 +1113,7 @@ int calculate_armor(troop dt, const weapon_type *dwtype, const weapon_type *awty
/* Momentan nur Trollgürtel und Werwolf-Eigenschaft */ /* Momentan nur Trollgürtel und Werwolf-Eigenschaft */
am = select_magicarmor(dt); am = select_magicarmor(dt);
if (config_get_int("rules.combat.nat_armor", 0) == 0) { if (rule_nat_armor == 0) {
/* natürliche Rüstung ist halbkumulativ */ /* natürliche Rüstung ist halbkumulativ */
if (ar > 0) { if (ar > 0) {
ar += an / 2; ar += an / 2;
@ -1597,13 +1615,7 @@ static troop select_opponent(battle * b, troop at, int mindist, int maxdist)
} }
if (b->turn == 0 && dt.fighter) { if (b->turn == 0 && dt.fighter) {
int tactics_formula = -1; if (rule_tactics_formula == 1) {
if (tactics_formula < 0) {
tactics_formula =
config_get_int("rules.tactics.formula", 0);
}
if (tactics_formula == 1) {
int tactics = get_tactics(at.fighter->side, dt.fighter->side); int tactics = get_tactics(at.fighter->side, dt.fighter->side);
/* percentage chance to get this attack */ /* percentage chance to get this attack */
@ -1924,8 +1936,7 @@ int skilldiff(troop at, troop dt, int dist)
} }
if (u_race(au) == get_race(RC_GOBLIN)) { if (u_race(au) == get_race(RC_GOBLIN)) {
int goblin_bonus = config_get_int("rules.combat.goblinbonus", 10); if (af->side->size[SUM_ROW] >= df->side->size[SUM_ROW] * rule_goblin_bonus) {
if (af->side->size[SUM_ROW] >= df->side->size[SUM_ROW] * goblin_bonus) {
skdiff += 1; skdiff += 1;
} }
} }
@ -2122,7 +2133,6 @@ static int attacks_per_round(troop t)
static void make_heroes(battle * b) static void make_heroes(battle * b)
{ {
side *s; side *s;
int hero_speed = config_get_int("rules.combat.herospeed", 10);
for (s = b->sides; s != b->sides + b->nsides; ++s) { for (s = b->sides; s != b->sides + b->nsides; ++s) {
fighter *fig; fighter *fig;
for (fig = s->fighters; fig; fig = fig->next) { for (fig = s->fighters; fig; fig = fig->next) {
@ -2133,7 +2143,7 @@ static void make_heroes(battle * b)
log_error("Hero %s is a %s.\n", unitname(u), u_race(u)->_name); log_error("Hero %s is a %s.\n", unitname(u), u_race(u)->_name);
} }
for (i = 0; i != u->number; ++i) { for (i = 0; i != u->number; ++i) {
fig->person[i].speed += (hero_speed - 1); fig->person[i].speed += (rule_hero_speed - 1);
} }
} }
} }
@ -2630,14 +2640,7 @@ static bool seematrix(const faction * f, const side * s)
static double PopulationDamage(void) static double PopulationDamage(void)
{ {
static double value = -1.0; return rule_population_damage / 100.0;
if (value < 0) {
int damage =
config_get_int("rules.combat.populationdamage",
BATTLE_KILLS_PEASANTS);
value = damage / 100.0;
}
return value;
} }
static void battle_effects(battle * b, int dead_players) static void battle_effects(battle * b, int dead_players)
@ -3192,7 +3195,6 @@ side * get_side(battle * b, const struct unit * u)
side * find_side(battle * b, const faction * f, const group * g, unsigned int flags, const faction * stealthfaction) side * find_side(battle * b, const faction * f, const group * g, unsigned int flags, const faction * stealthfaction)
{ {
side * s; side * s;
bool rule_anon_battle = config_get_int("rules.stealth.anon_battle", 1) != 0;
for (s = b->sides; s != b->sides + b->nsides; ++s) { for (s = b->sides; s != b->sides + b->nsides; ++s) {
if (s->faction == f && s->group == g) { if (s->faction == f && s->group == g) {
unsigned int s1flags = flags | SIDE_HASGUARDS; unsigned int s1flags = flags | SIDE_HASGUARDS;
@ -3935,15 +3937,6 @@ static bool start_battle(region * r, battle ** bp)
order *ord; order *ord;
for (ord = u->orders; ord; ord = ord->next) { for (ord = u->orders; ord; ord = ord->next) {
static bool init = false;
static const curse_type *peace_ct, *slave_ct, *calm_ct;
if (!init) {
init = true;
peace_ct = ct_find("peacezone");
slave_ct = ct_find("slavery");
calm_ct = ct_find("calmmonster");
}
if (getkeyword(ord) == K_ATTACK) { if (getkeyword(ord) == K_ATTACK) {
unit *u2; unit *u2;
fighter *c1, *c2; fighter *c1, *c2;
@ -4267,12 +4260,6 @@ void do_battle(region * r)
battle *b = NULL; battle *b = NULL;
bool fighting = false; bool fighting = false;
ship *sh; ship *sh;
static int init_rules = 0;
if (!init_rules) {
static_rules();
init_rules = 1;
}
if (msg_separator == NULL) { if (msg_separator == NULL) {
msg_separator = msg_message("battle::section", ""); msg_separator = msg_message("battle::section", "");
} }
@ -4340,3 +4327,10 @@ void do_battle(region * r)
} }
} }
void do_battles(void) {
region *r;
init_rules();
for (r = regions; r; r = r->next) {
do_battle(r);
}
}

View File

@ -230,7 +230,7 @@ extern "C" {
fighter * get_fighter(battle * b, const struct unit * u); fighter * get_fighter(battle * b, const struct unit * u);
/* END battle interface */ /* END battle interface */
extern void do_battle(struct region *r); extern void do_battles(void);
/* for combat spells and special attacks */ /* for combat spells and special attacks */
enum { SELECT_ADVANCE = 0x1, SELECT_DISTANCE = 0x2, SELECT_FIND = 0x4 }; enum { SELECT_ADVANCE = 0x1, SELECT_DISTANCE = 0x2, SELECT_FIND = 0x4 };

View File

@ -70,10 +70,7 @@ void process_produce(void) {
} }
void process_battle(void) { void process_battle(void) {
struct region *r; do_battles();
for (r = regions; r; r = r->next) {
do_battle(r);
}
} }
void process_siege(void) { void process_siege(void) {

View File

@ -4352,7 +4352,7 @@ void init_processor(void)
add_proc_region(p, enter_1, "Betreten (2. Versuch)"); /* to allow a buildingowner to enter the castle pre combat */ add_proc_region(p, enter_1, "Betreten (2. Versuch)"); /* to allow a buildingowner to enter the castle pre combat */
p += 10; p += 10;
add_proc_region(p, do_battle, "Attackieren"); add_proc_global(p, do_battles, "Attackieren");
if (!keyword_disabled(K_BESIEGE)) { if (!keyword_disabled(K_BESIEGE)) {
p += 10; p += 10;

View File

@ -26,7 +26,6 @@
/* Vermehrungsrate Bauern in 1/10000. /* Vermehrungsrate Bauern in 1/10000.
* TODO: Evt. Berechnungsfehler, reale Vermehrungsraten scheinen höher. */ * TODO: Evt. Berechnungsfehler, reale Vermehrungsraten scheinen höher. */
#define PEASANTGROWTH 10 #define PEASANTGROWTH 10
#define BATTLE_KILLS_PEASANTS 20
#define PEASANTLUCK 10 #define PEASANTLUCK 10
#define ROW_FACTOR 3 /* factor for combat row advancement rule */ #define ROW_FACTOR 3 /* factor for combat row advancement rule */