diff --git a/src/common/gamecode/laws.c b/src/common/gamecode/laws.c index 637a473f5..3becc13e3 100644 --- a/src/common/gamecode/laws.c +++ b/src/common/gamecode/laws.c @@ -3354,54 +3354,47 @@ ageing(void) } static int -maxunits(faction *f) +maxunits(const faction *f) { - if(global.unitsperalliance == true) { - faction *f2; - float mult = 1.0; + if (global.unitsperalliance == true) { + faction *f2; + float mult = 1.0; - for(f2 = factions; f2; f2 = f2->next) { - if(f2->alliance == f->alliance) { - mult += 0.4f * fspecial(f2, FS_ADMINISTRATOR); - } - } - return (int) (global.maxunits * mult); - } + for (f2 = factions; f2; f2 = f2->next) { + if (f2->alliance == f->alliance) { + mult += 0.4f * fspecial(f2, FS_ADMINISTRATOR); + } + } + return (int) (global.maxunits * mult); + } - return (int) (global.maxunits * - (1 + 0.4 * fspecial(f, FS_ADMINISTRATOR))); + return (int) (global.maxunits * (1 + 0.4 * fspecial(f, FS_ADMINISTRATOR))); } static boolean -checkunitnumber(faction *f) +checkunitnumber(const faction *f, int add) { - if(global.unitsperalliance == true) { - faction *f2; - int unitsinalliance = 0; - - for(f2 = factions; f2; f2 = f2->next) { - if(f->alliance == f2->alliance) { - unitsinalliance += f2->no_units; - } - } + if (global.maxunits==0) return true; + if (global.unitsperalliance == true) { + /* if unitsperalliance is true, maxunits returns the + number of units allowed in an alliance */ + faction *f2; + int unitsinalliance = add; + int maxu = maxunits(f); - /* if unitsperalliance is true, maxunits returns the - number of units allowed in an alliance */ - if(unitsinalliance >= maxunits(f)) { - return false; - } else { - return true; - } - } + for (f2 = factions; f2; f2 = f2->next) { + if (f->alliance == f2->alliance) { + unitsinalliance += f2->no_units; + } + if (unitsinalliance > maxu) return false; + } - if(f->no_units >= maxunits(f)) { - return false; - } + return true; + } - return true; + return (f->no_units + add < maxunits(f)); } - static void new_units (void) { @@ -3425,7 +3418,7 @@ new_units (void) int g, alias; order ** newordersp; - if (checkunitnumber(u->faction) == false) { + if (checkunitnumber(u->faction, 1) == false) { if (global.unitsperalliance == false) { ADDMSG(&u->faction->msgs, msg_message("too_many_units_in_faction", "command unit region allowed", diff --git a/src/common/kernel/xmlreader.c b/src/common/kernel/xmlreader.c index 5735f019b..ef9450e0e 100644 --- a/src/common/kernel/xmlreader.c +++ b/src/common/kernel/xmlreader.c @@ -39,6 +39,7 @@ without prior permission by the authors of Eressea. /* libc includes */ #include #include +#include #include static boolean gamecode_enabled = false; @@ -1179,12 +1180,7 @@ parse_main(xmlDocPtr doc) } global.unitsperalliance = xml_bvalue(node, "unitsperalliance", false); - - property = xmlGetProp(node, BAD_CAST "units"); - if (property!=NULL) { - global.maxunits = atoi((const char*)property); - xmlFree(property); - } + global.maxunits = xml_ivalue(node, "units", INT_MAX); property = xmlGetProp(node, BAD_CAST "name"); if (property!=NULL) {