finish parser for K_AUTOSTUDY and P_AUTO.

This commit is contained in:
Enno Rehling 2018-07-07 17:29:22 +02:00
parent 26e06804d6
commit a93d8fd56b
8 changed files with 97 additions and 20 deletions

View file

@ -188,6 +188,7 @@ set(TESTS_SRC
tests.c tests.c
academy.test.c academy.test.c
alchemy.test.c alchemy.test.c
automate.test.c
battle.test.c battle.test.c
creport.test.c creport.test.c
direction.test.c direction.test.c

View file

@ -13,14 +13,6 @@
#include <stdlib.h> #include <stdlib.h>
typedef struct student {
unit *u;
skill_t sk;
int level;
} student;
#define MAXSTUDENTS 128
int cmp_students(const void *lhs, const void *rhs) { int cmp_students(const void *lhs, const void *rhs) {
const student *a = (const student *)lhs; const student *a = (const student *)lhs;
const student *b = (const student *)rhs; const student *b = (const student *)rhs;
@ -32,16 +24,16 @@ int cmp_students(const void *lhs, const void *rhs) {
return (int)a->sk - (int)b->sk; return (int)a->sk - (int)b->sk;
} }
void do_autostudy(region *r) { int autostudy_init(student students[], int max_students, region *r)
{
unit *u; unit *u;
int nstudents = 0; int nstudents = 0;
student students[MAXSTUDENTS];
for (u = r->units; u; u = u->next) { for (u = r->units; u; u = u->next) {
keyword_t kwd = getkeyword(u->thisorder); keyword_t kwd = getkeyword(u->thisorder);
if (kwd == K_AUTOSTUDY) { if (kwd == K_AUTOSTUDY) {
student * st = students + nstudents; student * st = students + nstudents;
if (++nstudents == MAXSTUDENTS) { if (++nstudents == max_students) {
log_fatal("you must increase MAXSTUDENTS"); log_fatal("you must increase MAXSTUDENTS");
} }
st->u = u; st->u = u;
@ -50,10 +42,18 @@ void do_autostudy(region *r) {
st->level = effskill_study(u, st->sk); st->level = effskill_study(u, st->sk);
} }
} }
return nstudents;
}
#define MAXSTUDENTS 128
void do_autostudy(region *r) {
student students[MAXSTUDENTS];
int nstudents = autostudy_init(students, MAXSTUDENTS, r);
if (nstudents > 0) { if (nstudents > 0) {
int i, taught = 0; int i;
skill_t sk = NOSKILL; skill_t sk = NOSKILL;
student *teacher = NULL, *student = NULL;
qsort(students, nstudents, sizeof(student), cmp_students); qsort(students, nstudents, sizeof(student), cmp_students);
for (i = 0; i != nstudents; ++i) { for (i = 0; i != nstudents; ++i) {

View file

@ -21,8 +21,20 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef H_GC_AUTOMATE #ifndef H_GC_AUTOMATE
#define H_GC_AUTOMATE #define H_GC_AUTOMATE
#include "skill.h"
struct region; struct region;
struct unit;
typedef struct student {
struct unit *u;
skill_t sk;
int level;
int learn;
} student;
void do_autostudy(struct region *r); void do_autostudy(struct region *r);
int autostudy_init(student students[], int max_students, struct region *r);
#endif #endif

38
src/automate.test.c Normal file
View file

@ -0,0 +1,38 @@
#ifdef _MSC_VER
#include <platform.h>
#endif
#include "automate.h"
#include "kernel/faction.h"
#include "kernel/order.h"
#include "kernel/region.h"
#include "kernel/unit.h"
#include "tests.h"
#include <CuTest.h>
static void test_autostudy(CuTest *tc) {
student students[4];
unit *u1, *u2;
faction *f;
region *r;
test_setup();
r = test_create_plain(0, 0);
f = test_create_faction(NULL);
u1 = test_create_unit(f, r);
u1->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]);
u2 = test_create_unit(f, r);
u2->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]);
CuAssertIntEquals(tc, 2, autostudy_init(students, 4, r));
test_teardown();
}
CuSuite *get_automate_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_autostudy);
return suite;
}

View file

@ -84,7 +84,7 @@ char* get_command(const order *ord, const struct locale *lang, char *sbuffer, si
sbs_strcat(&sbs, str); sbs_strcat(&sbs, str);
if (ord->id < 0) { if (ord->id < 0) {
skill_t sk = (skill_t)(100+ord->id); skill_t sk = (skill_t)(100+ord->id);
assert(kwd == K_STUDY && sk != SK_MAGIC && sk < MAXSKILLS); assert((kwd == K_STUDY || kwd == K_AUTOSTUDY) && sk != SK_MAGIC && sk < MAXSKILLS);
str = skillname(sk, lang); str = skillname(sk, lang);
if (str) { if (str) {
if (strchr(str, ' ') == NULL) { if (strchr(str, ' ') == NULL) {

View file

@ -113,6 +113,30 @@ static void test_parse_make(CuTest *tc) {
test_teardown(); test_teardown();
} }
static void test_parse_autostudy(CuTest *tc) {
char cmd[32];
order *ord;
struct locale * lang;
test_setup();
lang = get_or_create_locale("en");
locale_setstring(lang, mkname("skill", skillnames[SK_ENTERTAINMENT]), "Entertainment");
locale_setstring(lang, keyword(K_STUDY), "STUDY");
locale_setstring(lang, keyword(K_AUTOSTUDY), "AUTOSTUDY");
locale_setstring(lang, parameters[P_AUTO], "AUTO");
init_locale(lang);
ord = parse_order("STUDY AUTO Entertainment", lang);
CuAssertPtrNotNull(tc, ord);
CuAssertIntEquals(tc, K_AUTOSTUDY, getkeyword(ord));
CuAssertStrEquals(tc, "AUTOSTUDY Entertainment", get_command(ord, lang, cmd, sizeof(cmd)));
CuAssertIntEquals(tc, K_AUTOSTUDY, init_order(ord, lang));
CuAssertStrEquals(tc, "Entertainment", getstrtoken());
free_order(ord);
test_teardown();
}
static void test_parse_make_temp(CuTest *tc) { static void test_parse_make_temp(CuTest *tc) {
char cmd[32]; char cmd[32];
order *ord; order *ord;
@ -130,7 +154,7 @@ static void test_parse_make_temp(CuTest *tc) {
CuAssertIntEquals(tc, K_MAKETEMP, getkeyword(ord)); CuAssertIntEquals(tc, K_MAKETEMP, getkeyword(ord));
CuAssertStrEquals(tc, "MAKETEMP herp", get_command(ord, lang, cmd, sizeof(cmd))); CuAssertStrEquals(tc, "MAKETEMP herp", get_command(ord, lang, cmd, sizeof(cmd)));
CuAssertIntEquals(tc, K_MAKETEMP, init_order_depr(ord)); CuAssertIntEquals(tc, K_MAKETEMP, init_order(ord, lang));
CuAssertStrEquals(tc, "herp", getstrtoken()); CuAssertStrEquals(tc, "herp", getstrtoken());
free_order(ord); free_order(ord);
test_teardown(); test_teardown();
@ -153,7 +177,7 @@ static void test_parse_maketemp(CuTest *tc) {
CuAssertPtrNotNull(tc, ord); CuAssertPtrNotNull(tc, ord);
CuAssertStrEquals(tc, "MAKETEMP herp", get_command(ord, lang, cmd, sizeof(cmd))); CuAssertStrEquals(tc, "MAKETEMP herp", get_command(ord, lang, cmd, sizeof(cmd)));
CuAssertIntEquals(tc, K_MAKETEMP, getkeyword(ord)); CuAssertIntEquals(tc, K_MAKETEMP, getkeyword(ord));
CuAssertIntEquals(tc, K_MAKETEMP, init_order_depr(ord)); CuAssertIntEquals(tc, K_MAKETEMP, init_order(ord, lang));
CuAssertStrEquals(tc, "herp", getstrtoken()); CuAssertStrEquals(tc, "herp", getstrtoken());
free_order(ord); free_order(ord);
test_teardown(); test_teardown();
@ -167,7 +191,7 @@ static void test_init_order(CuTest *tc) {
lang = get_or_create_locale("en"); lang = get_or_create_locale("en");
ord = create_order(K_MAKETEMP, lang, "hurr durr"); ord = create_order(K_MAKETEMP, lang, "hurr durr");
CuAssertIntEquals(tc, K_MAKETEMP, init_order_depr(ord)); CuAssertIntEquals(tc, K_MAKETEMP, init_order(ord, lang));
CuAssertStrEquals(tc, "hurr", getstrtoken()); CuAssertStrEquals(tc, "hurr", getstrtoken());
CuAssertStrEquals(tc, "durr", getstrtoken()); CuAssertStrEquals(tc, "durr", getstrtoken());
free_order(ord); free_order(ord);
@ -548,6 +572,7 @@ CuSuite *get_order_suite(void)
SUITE_ADD_TEST(suite, test_study_order_quoted); SUITE_ADD_TEST(suite, test_study_order_quoted);
SUITE_ADD_TEST(suite, test_parse_order); SUITE_ADD_TEST(suite, test_parse_order);
SUITE_ADD_TEST(suite, test_parse_make); SUITE_ADD_TEST(suite, test_parse_make);
SUITE_ADD_TEST(suite, test_parse_autostudy);
SUITE_ADD_TEST(suite, test_parse_make_temp); SUITE_ADD_TEST(suite, test_parse_make_temp);
SUITE_ADD_TEST(suite, test_parse_maketemp); SUITE_ADD_TEST(suite, test_parse_maketemp);
SUITE_ADD_TEST(suite, test_init_order); SUITE_ADD_TEST(suite, test_init_order);

View file

@ -343,7 +343,7 @@ int teach_cmd(unit * teacher, struct order *ord)
init_order(student->thisorder, student->faction->locale); init_order(student->thisorder, student->faction->locale);
sk = getskill(student->faction->locale); sk = getskill(student->faction->locale);
if (sk != NOSKILL if (sk != NOSKILL
&& effskill_study(teacher, sk) - TEACHDIFFERENCE >= effskill(student, sk)) { && effskill_study(teacher, sk) - TEACHDIFFERENCE >= effskill(student, sk, NULL)) {
teaching -= teach_unit(teacher, student, teaching, sk, true, &academy_students); teaching -= teach_unit(teacher, student, teaching, sk, true, &academy_students);
} }
} }
@ -432,7 +432,7 @@ int teach_cmd(unit * teacher, struct order *ord)
continue; continue;
} }
if (effskill_study(student, sk, NULL) > effskill_study(teacher, sk) if (effskill_study(student, sk) > effskill_study(teacher, sk)
- TEACHDIFFERENCE) { - TEACHDIFFERENCE) {
if (feedback) { if (feedback) {
ADDMSG(&teacher->faction->msgs, msg_feedback(teacher, ord, "teach_asgood", ADDMSG(&teacher->faction->msgs, msg_feedback(teacher, ord, "teach_asgood",

View file

@ -93,7 +93,6 @@ int RunAllTests(int argc, char *argv[])
ADD_SUITE(xerewards); ADD_SUITE(xerewards);
/* kernel */ /* kernel */
ADD_SUITE(academy); ADD_SUITE(academy);
ADD_SUITE(alchemy);
ADD_SUITE(alliance); ADD_SUITE(alliance);
ADD_SUITE(ally); ADD_SUITE(ally);
ADD_SUITE(building); ADD_SUITE(building);
@ -121,6 +120,8 @@ int RunAllTests(int argc, char *argv[])
ADD_SUITE(spells); ADD_SUITE(spells);
ADD_SUITE(unit); ADD_SUITE(unit);
/* gamecode */ /* gamecode */
ADD_SUITE(alchemy);
ADD_SUITE(automate);
ADD_SUITE(battle); ADD_SUITE(battle);
ADD_SUITE(calendar); ADD_SUITE(calendar);
ADD_SUITE(creport); ADD_SUITE(creport);