Merge pull request #584 from ennorehling/develop

some spell-related refactorings
This commit is contained in:
Enno Rehling 2016-09-24 19:55:59 +02:00 committed by GitHub
commit c2f0c82a70
14 changed files with 134 additions and 62 deletions

View file

@ -133,19 +133,21 @@ attrib_type at_npcfaction = {
*/
int HelpMask(void)
{
const char *str = config_get("rules.help.mask");
int rule = 0;
if (str != NULL) {
char *sstr = _strdup(str);
char *tok = strtok(sstr, " ");
while (tok) {
rule |= ally_flag(tok, -1);
tok = strtok(NULL, " ");
static int config, rule = 0;
if (config_changed(&config)) {
const char *str = config_get("rules.help.mask");
if (str != NULL) {
char *sstr = _strdup(str);
char *tok = strtok(sstr, " ");
while (tok) {
rule |= ally_flag(tok, -1);
tok = strtok(NULL, " ");
}
free(sstr);
}
else {
rule = HELP_ALL;
}
free(sstr);
}
else {
rule = HELP_ALL;
}
return rule;
}

View file

@ -731,7 +731,7 @@ int create_directories(void) {
double get_param_flt(const struct param *p, const char *key, double def)
{
const char *str = get_param(p, key);
const char *str = p ? get_param(p, key) : NULL;
return str ? atof(str) : def;
}
@ -862,7 +862,10 @@ int cmp_current_owner(const building * b, const building * a)
bool rule_stealth_other(void)
{
int rule = config_get_int("stealth.faction.other", 1);
static int rule, config;
if (config_changed(&config)) {
rule = config_get_int("stealth.faction.other", 1);
}
return rule != 0;
}

View file

@ -25,7 +25,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "unit.h"
#include "faction.h"
#include "race.h"
#include "spellbook.h"
#include "spell.h"
/* util includes */
#include <quicklist.h>
@ -86,13 +86,20 @@ void equipment_setskill(equipment * eq, skill_t sk, const char *value)
}
}
void equipment_addspell(equipment * eq, struct spell * sp, int level)
typedef struct lazy_spell {
char *name;
struct spell *sp;
int level;
} lazy_spell;
void equipment_addspell(equipment * eq, const char * name, int level)
{
if (eq) {
if (!eq->spellbook) {
eq->spellbook = create_spellbook(0);
}
spellbook_add(eq->spellbook, sp, level);
lazy_spell *ls = malloc(sizeof(lazy_spell));
ls->sp = NULL;
ls->level = level;
ls->name = _strdup(name);
ql_push(&eq->spells, ls);
}
}
@ -148,13 +155,18 @@ void equip_unit_mask(struct unit *u, const struct equipment *eq, int mask)
}
if (mask & EQUIP_SPELLS) {
if (eq->spellbook) {
quicklist * ql = eq->spellbook->spells;
if (eq->spells) {
quicklist * ql = eq->spells;
int qi;
sc_mage * mage = get_mage(u);
for (qi = 0; ql; ql_advance(&ql, &qi, 1)) {
spellbook_entry *sbe = (spellbook_entry *)ql_get(ql, qi);
lazy_spell *sbe = (lazy_spell *)ql_get(ql, qi);
if (!sbe->sp) {
sbe->sp = find_spell(sbe->name);
free(sbe->name);
sbe->name = NULL;
}
unit_add_spell(u, mage, sbe->sp, sbe->level);
}
}
@ -224,6 +236,12 @@ void equip_items(struct item **items, const struct equipment *eq)
}
}
void free_ls(void *arg) {
lazy_spell *ls = (lazy_spell*)arg;
free(ls->name);
free(ls);
}
void equipment_done(void) {
equipment **eqp = &equipment_sets;
while (*eqp) {
@ -231,9 +249,9 @@ void equipment_done(void) {
equipment *eq = *eqp;
*eqp = eq->next;
free(eq->name);
if (eq->spellbook) {
spellbook_clear(eq->spellbook);
free(eq->spellbook);
if (eq->spells) {
ql_foreach(eq->spells, free_ls);
ql_free(eq->spells);
}
while (eq->items) {
itemdata *next = eq->items->next;

View file

@ -48,7 +48,7 @@ extern "C" {
char *name;
struct itemdata *items;
char *skills[MAXSKILLS];
struct spellbook *spellbook;
struct quicklist *spells;
struct subset *subsets;
struct equipment *next;
void(*callback) (const struct equipment *, struct unit *);
@ -63,7 +63,7 @@ extern "C" {
const struct item_type *itype, const char *value);
void equipment_setskill(struct equipment *eq, skill_t sk,
const char *value);
void equipment_addspell(struct equipment *eq, struct spell *sp, int level);
void equipment_addspell(struct equipment *eq, const char *name, int level);
void equipment_setcallback(struct equipment *eq,
void(*callback) (const struct equipment *, struct unit *));

View file

@ -34,7 +34,7 @@ void test_equipment(CuTest * tc)
equipment_setitem(eq, it_horses, "1");
equipment_setskill(eq, SK_MAGIC, "5");
equipment_addspell(eq, sp, 1);
equipment_addspell(eq, sp->sname, 1);
u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0));
equip_unit_mask(u, eq, EQUIP_ALL);

View file

@ -796,14 +796,26 @@ attrib_type at_maxmagicians = {
int max_magicians(const faction * f)
{
int m = config_get_int("rules.maxskills.magic", MAXMAGICIANS);
attrib *a;
static int rule, config, rc_cache;
static const race *rc_elf;
int m;
if ((a = a_find(f->attribs, &at_maxmagicians)) != NULL) {
m = a->data.i;
if (config_changed(&config)) {
rule = config_get_int("rules.maxskills.magic", MAXMAGICIANS);
}
if (f->race == get_race(RC_ELF))
m = rule;
if (f->attribs) {
attrib *a = a_find(f->attribs, &at_maxmagicians);
if (a) {
m = a->data.i;
}
}
if (rc_changed(&rc_cache)) {
rc_elf = get_race(RC_ELF);
}
if (f->race == rc_elf) {
++m;
}
return m;
}

View file

@ -184,9 +184,13 @@ void sk_set(skill * sv, int level)
sv->level = level;
}
static int rule_random_progress(void)
static bool rule_random_progress(void)
{
return config_get_int("study.random_progress", 1);
static int rule, config;
if (config_changed(&config)) {
rule = config_get_int("study.random_progress", 1);
}
return rule != 0;
}
int skill_weeks(int level)

View file

@ -883,15 +883,16 @@ void leave_building(unit * u)
bool can_leave(unit * u)
{
int rule_leave;
static int config;
static bool rule_leave;
if (!u->building) {
return true;
}
rule_leave = config_get_int("rules.move.owner_leave", 0);
if (rule_leave != 0 && u->building && u == building_owner(u->building)) {
if (config_changed(&config)) {
rule_leave = config_get_int("rules.move.owner_leave", 0) != 0;
}
if (rule_leave && u->building && u == building_owner(u->building)) {
return false;
}
return true;
@ -1721,9 +1722,13 @@ int unit_max_hp(const unit * u)
{
int h;
double p;
int rule_stamina = config_get_int("rules.stamina", STAMINA_AFFECTS_HP);
static int config;
static int rule_stamina;
h = u_race(u)->hitpoints;
if (config_changed(&config)) {
rule_stamina = config_get_int("rules.stamina", STAMINA_AFFECTS_HP);
}
if (rule_stamina & 1) {
p = pow(effskill(u, SK_STAMINA, u->region) / 2.0, 1.5) * 0.2;
h += (int)(h * p + 0.5);

View file

@ -1191,22 +1191,18 @@ static void add_spells(equipment * eq, xmlNodeSetPtr nsetItems)
for (i = 0; i != nsetItems->nodeNr; ++i) {
xmlNodePtr node = nsetItems->nodeTab[i];
xmlChar *propValue;
struct spell *sp;
int level;
const char *name;
propValue = xmlGetProp(node, BAD_CAST "name");
assert(propValue != NULL);
sp = find_spell((const char *)propValue);
if (!sp) {
log_error("no spell '%s' for equipment-set '%s'\n", (const char *)propValue, eq->name);
name = (const char *)propValue;
level = xml_ivalue(node, "level", 0);
if (level > 0) {
equipment_addspell(eq, name, level);
}
else {
int level = xml_ivalue(node, "level", 0);
if (level > 0) {
equipment_addspell(eq, sp, level);
}
else {
log_error("spell '%s' for equipment-set '%s' has no level\n", sp->sname, eq->name);
}
log_error("spell '%s' for equipment-set '%s' has no level\n", name, eq->name);
}
xmlFree(propValue);
}
@ -1331,7 +1327,7 @@ static int parse_equipment(xmlDocPtr doc)
xmlXPathFreeObject(xpathResult);
xpathResult = xmlXPathEvalExpression(BAD_CAST "spell", xpath);
assert(!eq->spellbook);
assert(!eq->spells);
add_spells(eq, xpathResult->nodesetval);
xmlXPathFreeObject(xpathResult);

View file

@ -225,7 +225,11 @@ static void free_mage(attrib * a)
bool FactionSpells(void)
{
return config_get_int("rules.magic.factionlist", 0) != 0;
static int config, rule;
if (config_changed(&config)) {
rule = config_get_int("rules.magic.factionlist", 0);
}
return rule != 0;
}
void read_spells(struct quicklist **slistp, magic_t mtype,

View file

@ -84,7 +84,12 @@ static void give_peasants(unit *u, const item_type *itype, int reduce) {
}
static double random_move_chance(void) {
return config_get_flt("rules.monsters.random_move_chance", MOVECHANCE);
static double rule;
static int config;
if (config_changed(&config)) {
rule = config_get_flt("rules.monsters.random_move_chance", MOVECHANCE);
}
return rule;
}
static void reduce_weight(unit * u)

View file

@ -1308,6 +1308,17 @@ static void cb_add_seen(region *r, unit *u, void *cbdata) {
}
}
static bool unit_cansee(const unit *u)
{
const race *rc = u_race(u);
static const race *rc_spell;
static int rc_cache;
if (rc_changed(&rc_cache)) {
rc_spell = get_race(RC_SPELL);
}
return (rc!=rc_spell || u->number == RS_FARVISION);
}
/** set region.seen based on visibility by one faction.
*
* this function may also update ctx->last and ctx->first for potential
@ -1344,7 +1355,7 @@ void prepare_report(report_context *ctx, faction *f)
u = building_owner(b);
if (u && u->faction==f) {
prepare_lighthouse(b, ctx);
if (u_race(u) != get_race(RC_SPELL) || u->number == RS_FARVISION) {
if (unit_cansee(u)) {
add_seen_nb(f, r, seen_unit);
}
}
@ -1355,7 +1366,7 @@ void prepare_report(report_context *ctx, faction *f)
}
for (u = r->units; u; u = u->next) {
if (u->faction==f) {
if (u_race(u) != get_race(RC_SPELL) || u->number == RS_FARVISION) {
if (unit_cansee(u)) {
add_seen_nb(f, r, seen_unit);
}
if (fval(r, RF_LIGHTHOUSE)) {

View file

@ -800,7 +800,11 @@ int study_cmd(unit * u, order * ord)
}
static int produceexp_days(void) {
return config_get_int("study.produceexp", 10);
static int config, rule;
if (config_changed(&config)) {
rule = config_get_int("study.produceexp", 10);
}
return rule;
}
void produceexp_ex(struct unit *u, skill_t sk, int n, learn_fun learn)
@ -863,7 +867,11 @@ void demon_skillchange(unit *u)
if (fval(u, UFL_HUNGER)) {
/* hungry demons only go down, never up in skill */
int rule_hunger = config_get_int("hunger.demon.skill", 0) != 0;
static int config;
static bool rule_hunger;
if (config_changed(&config)) {
rule_hunger = config_get_int("hunger.demon.skill", 0) != 0;
}
if (rule_hunger) {
upchance = 0;
downchance = 15;

View file

@ -176,8 +176,12 @@ bool is_astral(const region * r)
plane *get_astralplane(void)
{
plane *astralspace = 0;
int rule_astralplane = config_get_int("modules.astralspace", 1);
static int config;
static bool rule_astralplane;
if (config_changed(&config)) {
rule_astralplane = config_get_int("modules.astralspace", 1) != 0;
}
if (!rule_astralplane) {
return NULL;
}