Merge branch 'develop' of github.com:eressea/server into develop

This commit is contained in:
Enno Rehling 2020-03-08 10:15:41 +01:00
commit 42280e5ff1
7 changed files with 128 additions and 266 deletions

View File

@ -37,8 +37,13 @@ int u_geteffstealth(const unit *u)
if (skill_enabled(SK_STEALTH)) {
if (u->flags & UFL_STEALTH) {
attrib *a = a_find(u->attribs, &at_stealth);
if (a != NULL)
if (a != NULL) {
int eff = effskill(u, SK_STEALTH, u->region);
if (eff < a->data.i) {
return eff;
}
return a->data.i;
}
}
}
return -1;

View File

@ -20,7 +20,7 @@ static void test_stealth(CuTest *tc) {
CuAssertIntEquals(tc, -1, u_geteffstealth(u));
CuAssertIntEquals(tc, 2, eff_stealth(u, u->region));
u_seteffstealth(u, 3);
CuAssertIntEquals(tc, 3, u_geteffstealth(u));
CuAssertIntEquals(tc, 2, u_geteffstealth(u));
CuAssertIntEquals(tc, 2, eff_stealth(u, u->region));
u_seteffstealth(u, 1);
CuAssertIntEquals(tc, 1, u_geteffstealth(u));

View File

@ -20,7 +20,8 @@
static int cmp_scholars(const void *lhs, const void *rhs) {
const scholar *a = (const scholar *)lhs;
const scholar *b = (const scholar *)rhs;
return b->level - a->level;
int diff = b->level - a->level;
return (diff != 0) ? diff : b->u->number - a->u->number;
}
int autostudy_init(scholar scholars[], int max_scholars, unit **units, skill_t *o_skill)
@ -77,6 +78,7 @@ int autostudy_init(scholar scholars[], int max_scholars, unit **units, skill_t *
static void teaching(scholar *s, int n) {
assert(n <= s->u->number);
// doppelter Effekt mit Lehrer:
s->learn += n;
s->u->flags |= UFL_LONGACTION;
}
@ -91,27 +93,31 @@ void autostudy_run(scholar scholars[], int nscholars)
{
int ti = 0;
while (ti != nscholars) {
int t, se, ts = 0, tt = 0, si = ti;
for (se = ti; se != nscholars; ++se) {
int mint;
ts += scholars[se].u->number; /* count total scholars */
int t, ts = 0, tt = 0, si = ti, mint = 0, ns = 0;
for (t = ti; t != nscholars; ++t) {
ts += scholars[t].u->number; /* count total scholars */
mint = (ts + 10) / 11; /* need a minimum of ceil(ts/11) teachers */
for (; mint > tt && si != nscholars - 1; ++si) {
for (; mint > tt && si != nscholars; ++si) {
tt += scholars[si].u->number;
}
}
if (mint < tt) {
/* die Einheit si-1 hat einen Mix aus Lehrer und Schüler */
--si;
ns = tt - mint;
}
/* now si splits the teachers and students 1:10 */
/* first student must be 2 levels below first teacher: */
for (; si != se; ++si) {
for (; si != nscholars; ++si) {
if (scholars[si].level + TEACHDIFFERENCE <= scholars[ti].level) {
break;
}
tt += scholars[si].u->number;
ns = 0;
}
/* now si is the first unit we can teach, if we can teach any */
if (si == se) {
if (si == nscholars) {
/* there are no students, so standard learning for everyone */
for (t = ti; t != se; ++t) {
for (t = ti; t != nscholars; ++t) {
learning(scholars + t, scholars[t].u->number);
}
}
@ -119,16 +125,17 @@ void autostudy_run(scholar scholars[], int nscholars)
/* invariant: unit ti can still teach i students */
int i = scholars[ti].u->number * STUDENTS_PER_TEACHER;
/* invariant: unit si has n students that can still be taught */
int s, n = scholars[si].u->number;
for (t = ti, s = si; t != si && s != se; ) {
int s, n = (ns > 0) ? ns : scholars[si].u->number;
for (t = ti, s = si; t != si && s != nscholars; ) {
if (i >= n) {
/* t has more than enough teaching capacity for s */
i -= n;
teaching(scholars + s, n);
learning(scholars + s, scholars[s].u->number);
/* next student, please: */
if (++s == se) {
continue;
if (++s == nscholars) {
/* no more students */
break;
}
n = scholars[s].u->number;
}
@ -147,7 +154,7 @@ void autostudy_run(scholar scholars[], int nscholars)
do {
/* remaining students learn without a teacher: */
learning(scholars + s, n);
if (++s == se) {
if (++s == nscholars) {
break;
}
n = scholars[s].u->number;
@ -163,10 +170,15 @@ void autostudy_run(scholar scholars[], int nscholars)
}
++t;
for (; t < si; ++t) {
/* teachers that did not teach */
learning(scholars + t, scholars[t].u->number);
}
for (; s < nscholars; ++s) {
/* students that were not taught */
learning(scholars + s, scholars[s].u->number);
}
}
ti = se;
ti = nscholars;
}
}

View File

@ -139,7 +139,78 @@ static void test_autostudy_run_bigunit(CuTest *tc) {
autostudy_run(scholars, nscholars);
CuAssertIntEquals(tc, SK_ENTERTAINMENT, skill);
CuAssertIntEquals(tc, 0, scholars[0].learn);
CuAssertIntEquals(tc, 200, scholars[1].learn);
CuAssertIntEquals(tc, 1200, scholars[1].learn);
test_teardown();
}
static void test_autostudy_run_few_teachers(CuTest *tc) {
scholar scholars[4];
int nscholars;
unit *u1, *u2, *u3, *ulist;
faction *f;
region *r;
skill_t skill;
test_setup();
r = test_create_plain(0, 0);
f = test_create_faction(NULL);
u1 = test_create_unit(f, r);
set_number(u1, 20);
set_level(u1, SK_ENTERTAINMENT, 16);
u1->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]);
u2 = test_create_unit(f, r);
set_number(u2, 500);
set_level(u2, SK_ENTERTAINMENT, 10);
u2->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]);
u3 = test_create_unit(f, r);
set_number(u3, 100);
set_level(u3, SK_ENTERTAINMENT, 9);
u3->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]);
ulist = r->units;
CuAssertIntEquals(tc, 3, nscholars = autostudy_init(scholars, 4, &ulist, &skill));
CuAssertPtrEquals(tc, NULL, ulist);
autostudy_run(scholars, nscholars);
CuAssertIntEquals(tc, SK_ENTERTAINMENT, skill);
CuAssertIntEquals(tc, 0, scholars[0].learn);
CuAssertIntEquals(tc, 700, scholars[1].learn);
CuAssertIntEquals(tc, 100, scholars[2].learn);
test_teardown();
}
static void test_autostudy_run_few_teachers_reverse(CuTest *tc) {
scholar scholars[4];
int nscholars;
unit *u1, *u2, *u3, *ulist;
faction *f;
region *r;
skill_t skill;
test_setup();
r = test_create_plain(0, 0);
f = test_create_faction(NULL);
u1 = test_create_unit(f, r);
set_number(u1, 20);
set_level(u1, SK_ENTERTAINMENT, 16);
u1->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]);
u2 = test_create_unit(f, r);
set_number(u2, 100);
set_level(u2, SK_ENTERTAINMENT, 10);
u2->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]);
u3 = test_create_unit(f, r);
set_number(u3, 500);
set_level(u3, SK_ENTERTAINMENT, 9);
u3->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]);
ulist = r->units;
CuAssertIntEquals(tc, 3, nscholars = autostudy_init(scholars, 4, &ulist, &skill));
CuAssertPtrEquals(tc, NULL, ulist);
autostudy_run(scholars, nscholars);
CuAssertIntEquals(tc, SK_ENTERTAINMENT, skill);
CuAssertIntEquals(tc, 0, scholars[0].learn);
CuAssertIntEquals(tc, 800, scholars[1].learn + scholars[2].learn);
test_teardown();
}
@ -360,6 +431,8 @@ CuSuite *get_automate_suite(void)
SUITE_ADD_TEST(suite, test_autostudy_run_teachers_learn);
SUITE_ADD_TEST(suite, test_autostudy_run_twoteachers);
SUITE_ADD_TEST(suite, test_autostudy_run_bigunit);
SUITE_ADD_TEST(suite, test_autostudy_run_few_teachers);
SUITE_ADD_TEST(suite, test_autostudy_run_few_teachers_reverse);
SUITE_ADD_TEST(suite, test_autostudy_run_skilldiff);
return suite;
}

View File

@ -48,182 +48,6 @@ static void begin_orders(unit *u) {
u->orders = NULL;
}
static void unitorders(input *in, faction *f)
{
int i;
unit *u;
assert(f);
i = getid();
u = findunit(i);
if (u && u->faction == f) {
order **ordp;
begin_orders(u);
ordp = &u->orders;
for (;;) {
const char *s;
/* Erst wenn wir sicher sind, dass kein Befehl
* eingegeben wurde, checken wir, ob nun eine neue
* Einheit oder ein neuer Spieler drankommt */
s = in->getbuf(in->data);
if (s == NULL)
break;
if (s[0]) {
if (s[0] != '@') {
char token[64];
const char *stok = s;
stok = parse_token(&stok, token, sizeof(token));
if (stok) {
bool quit = false;
param_t param = findparam(stok, u->faction->locale);
switch (param) {
case P_UNIT:
case P_REGION:
quit = true;
break;
case P_FACTION:
case P_NEXT:
case P_GAMENAME:
/* these terminate the orders, so we apply extra checking */
if (strlen(stok) >= 3) {
quit = true;
break;
}
else {
quit = false;
}
break;
default:
break;
}
if (quit) {
break;
}
}
}
/* Nun wird der Befehl erzeut und eingehaengt */
*ordp = parse_order(s, u->faction->locale);
if (*ordp) {
ordp = &(*ordp)->next;
}
else {
ADDMSG(&f->msgs, msg_message("parse_error", "unit command", u, s));
}
}
}
}
}
static faction *factionorders(void)
{
int fid = getid();
faction *f = findfaction(fid);
if (f != NULL && (f->flags & FFL_NPC) == 0) {
char token[PASSWORD_MAXSIZE];
const char *pass = gettoken(token, sizeof(token));
if (!checkpasswd(f, (const char *)pass)) {
log_debug("Invalid password for faction %s", itoa36(fid));
ADDMSG(&f->msgs, msg_message("wrongpasswd", "password", pass));
return 0;
}
/* Die Partei hat sich zumindest gemeldet, so dass sie noch
* nicht als untaetig gilt */
f->lastorders = turn;
}
else {
log_debug("orders for invalid faction %s", itoa36(fid));
}
return f;
}
int read_orders(input *in)
{
const char *b;
int nfactions = 0;
struct faction *f = NULL;
const struct locale *lang = default_locale;
/* TODO: recognize UTF8 BOM */
b = in->getbuf(in->data);
/* Auffinden der ersten Partei, und danach abarbeiten bis zur letzten
* Partei */
while (b) {
char token[128];
param_t p;
const char *s;
init_tokens_str(b);
s = gettoken(token, sizeof(token));
p = findparam_block(s, lang, true);
switch (p) {
case P_GAMENAME:
case P_FACTION:
f = factionorders();
if (f) {
lang = f->locale;
++nfactions;
}
b = in->getbuf(in->data);
break;
/* in factionorders wird nur eine zeile gelesen:
* diejenige mit dem passwort. Die befehle der units
* werden geloescht, und die Partei wird als aktiv
* vermerkt. */
case P_UNIT:
if (f) {
unitorders(in, f);
do {
b = in->getbuf(in->data);
if (!b) {
break;
}
init_tokens_str(b);
s = gettoken(token, sizeof(token));
p = (s && s[0] != '@') ? findparam(s, lang) : NOPARAM;
} while ((p != P_UNIT || !f) && p != P_FACTION && p != P_NEXT
&& p != P_GAMENAME);
}
break;
/* Falls in unitorders() abgebrochen wird, steht dort entweder eine neue
* Partei, eine neue Einheit oder das File-Ende. Das switch() wird erneut
* durchlaufen, und die entsprechende Funktion aufgerufen. Man darf buf
* auf alle Faelle nicht ueberschreiben! Bei allen anderen Eintraegen hier
* muss buf erneut gefaellt werden, da die betreffende Information in nur
* einer Zeile steht, und nun die naechste gelesen werden muss.
*/
case P_NEXT:
f = NULL;
lang = default_locale;
b = in->getbuf(in->data);
break;
default:
b = in->getbuf(in->data);
break;
}
}
log_info("done reading orders for %d factions", nfactions);
return 0;
}
typedef struct parser_state {
unit *u;
faction *f;

View File

@ -12,7 +12,6 @@ extern "C" {
void *data;
} input;
int read_orders(struct input *in);
int parseorders(FILE *F);
#ifdef __cplusplus

View File

@ -21,82 +21,36 @@
#include <CuTest.h>
#include <tests.h>
static const char *getbuf_null(void *data)
{
return NULL;
}
static void test_read_orders(CuTest *tc) {
input in;
test_setup();
in.getbuf = getbuf_null;
in.data = NULL;
CuAssertIntEquals(tc, 0, read_orders(&in));
test_teardown();
}
static const char *getbuf_strings(void *data)
{
strlist **listp = (strlist **)data;
if (listp && *listp) {
strlist *list = *listp;
*listp = list->next;
return list->s;
}
return NULL;
}
static void test_unit_orders(CuTest *tc) {
input in;
unit *u;
faction *f;
strlist *orders = NULL, *backup;
char buf[64];
FILE *F = tmpfile();
test_setup();
u = test_create_unit(f = test_create_faction(NULL), test_create_plain(0, 0));
f->locale = test_create_locale();
u->orders = create_order(K_ENTERTAIN, f->locale, NULL);
faction_setpassword(f, password_hash("password", PASSWORD_DEFAULT));
snprintf(buf, sizeof(buf), "%s %s %s",
fprintf(F, "%s %s %s\n",
LOC(f->locale, parameters[P_FACTION]), itoa36(f->no), "password");
addstrlist(&orders, buf);
snprintf(buf, sizeof(buf), "%s %s",
fprintf(F, "%s %s\n",
LOC(f->locale, parameters[P_UNIT]), itoa36(u->no));
addstrlist(&orders, buf);
snprintf(buf, sizeof(buf), "%s %s", keyword_name(K_MOVE, f->locale),
fprintf(F, "%s %s\n", keyword_name(K_MOVE, f->locale),
LOC(f->locale, shortdirections[D_WEST]));
backup = orders;
addstrlist(&orders, buf);
in.data = &orders;
in.getbuf = getbuf_strings;
CuAssertIntEquals(tc, 0, read_orders(&in));
rewind(F);
CuAssertIntEquals(tc, 0, parseorders(F));
CuAssertPtrNotNull(tc, u->old_orders);
CuAssertPtrNotNull(tc, u->orders);
CuAssertPtrEquals(tc, NULL, orders);
CuAssertIntEquals(tc, K_MOVE, getkeyword(u->orders));
CuAssertIntEquals(tc, K_ENTERTAIN, getkeyword(u->old_orders));
CuAssertIntEquals(tc, UFL_ORDERS, u->flags & UFL_ORDERS);
freestrlist(backup);
fclose(F);
test_teardown();
}
typedef struct order_list {
const char **orders;
int next;
} order_list;
static const char *getbuf_list(void *data)
{
order_list * olist = (order_list *)data;
return olist->orders[olist->next++];
}
static void test_faction_password_okay(CuTest *tc) {
input in;
faction *f;
order_list olist;
const char *orders[] = { "ERESSEA 1 password", NULL };
FILE *F;
test_setup();
f = test_create_faction(NULL);
@ -104,21 +58,18 @@ static void test_faction_password_okay(CuTest *tc) {
CuAssertIntEquals(tc, 1, f->no);
faction_setpassword(f, "password");
f->lastorders = turn - 1;
olist.orders = orders;
olist.next = 0;
in.getbuf = getbuf_list;
in.data = &olist;
CuAssertIntEquals(tc, 0, read_orders(&in));
CuAssertIntEquals(tc, 2, olist.next);
F = tmpfile();
fprintf(F, "ERESSEA 1 password\n");
rewind(F);
CuAssertIntEquals(tc, 0, parseorders(F));
CuAssertIntEquals(tc, turn, f->lastorders);
fclose(F);
test_teardown();
}
static void test_faction_password_bad(CuTest *tc) {
input in;
faction *f;
order_list olist;
const char *orders[] = { "ERESSEA 1 password", NULL };
FILE *F;
test_setup();
mt_create_va(mt_new("wrongpasswd", NULL), "password:string", MT_NEW_END);
@ -128,20 +79,18 @@ static void test_faction_password_bad(CuTest *tc) {
CuAssertIntEquals(tc, 1, f->no);
faction_setpassword(f, "patzword");
f->lastorders = turn - 1;
olist.orders = orders;
olist.next = 0;
in.getbuf = getbuf_list;
in.data = &olist;
CuAssertIntEquals(tc, 0, read_orders(&in));
CuAssertIntEquals(tc, 2, olist.next);
F = tmpfile();
fprintf(F, "ERESSEA 1 password\n");
rewind(F);
CuAssertIntEquals(tc, 0, parseorders(F));
CuAssertIntEquals(tc, turn - 1, f->lastorders);
fclose(F);
test_teardown();
}
CuSuite *get_orderfile_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_read_orders);
SUITE_ADD_TEST(suite, test_unit_orders);
SUITE_ADD_TEST(suite, test_faction_password_okay);
SUITE_ADD_TEST(suite, test_faction_password_bad);