implement fixed wage functions, select by constant value

This commit is contained in:
Enno Rehling 2017-03-16 16:07:52 +01:00
parent 67a566d728
commit ac12cb966d
7 changed files with 13 additions and 57 deletions

View File

@ -56,6 +56,7 @@
"seed.population.max": 8,
"rules.reserve.twophase": true,
"rules.owners.force_leave": false,
"rules.wage.function": 2,
"rules.monsters.attack_chance": 0.1,
"rules.transfermen": false,
"stealth.faction.other": false,

View File

@ -28,7 +28,4 @@
<xi:include href="config://default/names-zombies.xml"/>
<xi:include href="config://default/names-ghouls.xml"/>
<xi:include href="config://default/names-dragons.xml"/>
<rules>
<function name="wage" value="minimum_wage"/>
</rules>
</eressea>

View File

@ -811,10 +811,18 @@ minimum_wage(const region * r, const faction * f, const race * rc, int in_turn)
* die Bauern wenn f == NULL. */
int wage(const region * r, const faction * f, const race * rc, int in_turn)
{
if (global.wage) {
return global.wage(r, f, rc, in_turn);
static int config;
static int rule_wage;
if (config_changed(&config)) {
rule_wage = config_get_int("rules.wage.function", 1);
}
if (rule_wage==0) {
return 0;
}
if (rule_wage==1) {
return default_wage(r, f, rc, in_turn);
}
return minimum_wage(r, f, rc, in_turn);
}
int cmp_wage(const struct building *b, const building * a)
@ -913,7 +921,6 @@ int cmp_current_owner(const building * b, const building * a)
void register_buildings(void)
{
register_function((pf_generic)minimum_wage, "minimum_wage");
register_function((pf_generic)init_smithy, "init_smithy");
register_function((pf_generic)castle_name, "castle_name");
register_function((pf_generic)castle_name_2, "castle_name_2");

View File

@ -796,7 +796,6 @@ bool config_token(const char *key, const char *tok) {
}
void free_config(void) {
global.wage = NULL;
free_params(&configuration);
++config_cache_key;
}

View File

@ -110,8 +110,6 @@ extern "C" {
typedef struct settings {
struct attrib *attribs;
void *vm_state;
int(*wage) (const struct region * r, const struct faction * f,
const struct race * rc, int in_turn);
} settings;
void set_param(struct param **p, const char *key, const char *value);

View File

@ -883,44 +883,6 @@ static item_type *xml_readitem(xmlXPathContextPtr xpath, resource_type * rtype)
return itype;
}
static int parse_rules(xmlDocPtr doc)
{
xmlXPathContextPtr xpath = xmlXPathNewContext(doc);
xmlXPathObjectPtr functions;
xmlNodeSetPtr nodes;
int i;
/* reading eressea/resources/resource */
functions = xmlXPathEvalExpression(BAD_CAST "/eressea/rules/function", xpath);
nodes = functions->nodesetval;
for (i = 0; i != nodes->nodeNr; ++i) {
xmlNodePtr node = nodes->nodeTab[i];
xmlChar *propValue;
pf_generic fun;
parse_function(node, &fun, &propValue);
if (fun == NULL) {
log_error("unknown function for rule '%s' %s\n", (const char *)propValue);
xmlFree(propValue);
continue;
}
assert(propValue != NULL);
if (strcmp((const char *)propValue, "wage") == 0) {
global.wage =
(int(*)(const struct region *, const struct faction *,
const struct race *, int))fun;
}
else {
log_error("unknown function for rule '%s'\n", (const char *)propValue);
}
xmlFree(propValue);
}
xmlXPathFreeObject(functions);
xmlXPathFreeContext(xpath);
return 0;
}
static int parse_resources(xmlDocPtr doc)
{
xmlXPathContextPtr xpath = xmlXPathNewContext(doc);
@ -1960,8 +1922,6 @@ static int parse_strings(xmlDocPtr doc)
void register_xmlreader(void)
{
xml_register_callback(parse_rules);
xml_register_callback(parse_races);
xml_register_callback(parse_calendar);
xml_register_callback(parse_resources);

View File

@ -1444,15 +1444,10 @@ static void test_show_race(CuTest *tc) {
test_cleanup();
}
static int low_wage(const region * r, const faction * f, const race * rc, int in_turn) {
return 1;
}
static void test_immigration(CuTest * tc)
{
region *r;
double inject[] = { 1 };
int (*old_wage)(const region*, const faction*, const race*, int) = global.wage;
test_setup();
r = test_create_region(0, 0, 0);
@ -1472,10 +1467,9 @@ static void test_immigration(CuTest * tc)
random_source_inject_array(inject, 2);
global.wage = low_wage;
config_set("rules.wage.function", "0");
immigration();
CuAssertIntEquals(tc, 2, rpeasants(r));
global.wage = old_wage;
test_cleanup();
}