Skip to content

Commit

Permalink
LERNE AUTO fallback für Rassen, die nicht lehren dürfen (#1051)
Browse files Browse the repository at this point in the history
  • Loading branch information
ennorehling committed Mar 3, 2024
1 parent f5d3e6e commit 362fb3e
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 2 deletions.
16 changes: 15 additions & 1 deletion scripts/tests/study.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ end

function setup()
conf = [[{
"races" : { "human" : {} },
"races" : {
"human" : {},
"smurf" : { "flags" : [ "noteach" ] }
},
"terrains" : { "plain" : { "flags" : [ "land" ] } },
"keywords" : { "de" : {
"autostudy": "LERNEN AUTO",
Expand Down Expand Up @@ -140,3 +143,14 @@ function test_auto_study_expensive()
assert_equal(1, u:get_skill("tactics"))
assert_equal("@LERNEN Taktik", u:get_order())
end

function test_auto_study_noteach()
local r = region.create(0, 0, "plain")
local f = faction.create("smurf")
local u = unit.create(f, r)
u:clear_orders()
u:add_order("@LERNE AUTO Armbrust")
process_orders()
assert_equal(1, u:get_skill("crossbow"))
assert_equal("@LERNEN Armbrust", u:get_order())
end
3 changes: 2 additions & 1 deletion src/automate.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static int cmp_scholars(const void *lhs, const void *rhs) {
return (diff != 0) ? diff : b->u->number - a->u->number;
}

int autostudy_init(scholar scholars[], int max_scholars, unit **units, skill_t *o_skill)
int autostudy_init(scholar scholars[], int max_scholars, unit **units, enum skill_t *o_skill)
{
unit *unext = NULL, *u = *units;
faction *f = u->faction;
Expand All @@ -40,6 +40,7 @@ int autostudy_init(scholar scholars[], int max_scholars, unit **units, skill_t *
unext = u->next;
if (!can_teach(u)) {
cmistake(u, u->thisorder, 274, MSG_EVENT);
set_keyword(u->thisorder, K_STUDY);
fset(u, UFL_MARK);
}
else if (long_order_allowed(u, false)) {
Expand Down
21 changes: 21 additions & 0 deletions src/automate.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ static void test_autostudy_init(CuTest *tc) {
test_teardown();
}

static void test_autostudy_init_fallback(CuTest *tc) {
scholar scholar;
unit *u, *ulist;
faction *f;
region *r;
race *rc;
skill_t skill = NOSKILL;

test_setup();
rc = test_create_race("tunnelworm");
rc->flags |= RCF_NOTEACH;
r = test_create_plain(0, 0);
f = test_create_faction();
ulist = u = test_create_unit(f, r);
u_setrace(u, rc);
u->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]);
CuAssertIntEquals(tc, 0, autostudy_init(&scholar, 1, &ulist, &skill));
CuAssertIntEquals(tc, K_STUDY, getkeyword(u->thisorder));
}

/**
* Reproduce Bug 2520
*/
Expand Down Expand Up @@ -494,6 +514,7 @@ CuSuite *get_automate_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_autostudy_init);
SUITE_ADD_TEST(suite, test_autostudy_init_fallback);
SUITE_ADD_TEST(suite, test_autostudy_run);
SUITE_ADD_TEST(suite, test_do_autostudy);
SUITE_ADD_TEST(suite, test_autostudy_batches);
Expand Down
7 changes: 7 additions & 0 deletions src/kernel/order.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <string.h>

# define ORD_KEYWORD(ord) (keyword_t)((ord)->command & 0xFFFF)
# define ORD_FLAGS(ord) ((ord)->command & 0xFFFF0000)
# define OD_STRING(odata) ((odata) ? (odata)->_str : NULL)

order_data *odata_load(int id)
Expand Down Expand Up @@ -101,6 +102,12 @@ keyword_t getkeyword(const order * ord)
return ORD_KEYWORD(ord);
}

void set_keyword(order *ord, keyword_t kwd)
{
int flags = ORD_FLAGS(ord);
ord->command = flags | (int)kwd;
}

/** returns a plain-text representation of the order.
* This is the inverse function to the parse_order command. Note that
* keywords are expanded to their full length.
Expand Down
1 change: 1 addition & 0 deletions src/kernel/order.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ extern "C" {

/* access functions for orders */
keyword_t getkeyword(const order * ord);
void set_keyword(order *ord, keyword_t kwd);
void set_order(order ** destp, order * src);
char* get_command(const order *ord, const struct locale *lang,
char *buffer, size_t size);
Expand Down
34 changes: 34 additions & 0 deletions src/kernel/order.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,39 @@ static void test_parse_autostudy(CuTest *tc) {
test_teardown();
}

static void test_set_keyword(CuTest *tc) {
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, param_name(P_AUTO, NULL), "AUTO");
init_locale(lang);

ord = parse_order("STUDY AUTO Entertainment", lang);
CuAssertIntEquals(tc, K_AUTOSTUDY, getkeyword(ord));
set_keyword(ord, K_STUDY);
CuAssertIntEquals(tc, K_STUDY, getkeyword(ord));
free_order(ord);

ord = parse_order("!@STUDY AUTO Entertainment", lang);
CuAssertTrue(tc, is_persistent(ord));
CuAssertTrue(tc, is_silent(ord));
CuAssertIntEquals(tc, K_AUTOSTUDY, getkeyword(ord));
CuAssertIntEquals(tc, CMD_PERSIST|CMD_QUIET, ord->command & (CMD_PERSIST | CMD_QUIET));
set_keyword(ord, K_STUDY);
CuAssertIntEquals(tc, K_STUDY, getkeyword(ord));
CuAssertTrue(tc, is_persistent(ord));
CuAssertTrue(tc, is_silent(ord));
CuAssertIntEquals(tc, CMD_PERSIST | CMD_QUIET, ord->command & (CMD_PERSIST | CMD_QUIET));
free_order(ord);

test_teardown();
}

static void test_parse_make_temp(CuTest *tc) {
char cmd[32];
order *ord;
Expand Down Expand Up @@ -613,6 +646,7 @@ CuSuite *get_order_suite(void)
SUITE_ADD_TEST(suite, test_parse_parameters);
SUITE_ADD_TEST(suite, test_parse_make);
SUITE_ADD_TEST(suite, test_parse_autostudy);
SUITE_ADD_TEST(suite, test_set_keyword);
SUITE_ADD_TEST(suite, test_parse_make_temp);
SUITE_ADD_TEST(suite, test_parse_maketemp);
SUITE_ADD_TEST(suite, test_init_order);
Expand Down
Binary file removed tests/data/test.dat
Binary file not shown.

0 comments on commit 362fb3e

Please sign in to comment.