extract long_order_allowed for use in autostudy.

This commit is contained in:
Enno Rehling 2018-07-14 15:56:44 +02:00 committed by Enno Rehling
parent 2bb3e7601d
commit 36b7104ce3
6 changed files with 81 additions and 29 deletions

View File

@ -1,6 +1,7 @@
#include <platform.h>
#include "kernel/faction.h"
#include "kernel/messages.h"
#include "kernel/order.h"
#include "kernel/region.h"
#include "kernel/unit.h"
@ -9,6 +10,7 @@
#include "automate.h"
#include "keyword.h"
#include "laws.h"
#include "study.h"
#include <stdlib.h>
@ -34,6 +36,7 @@ int autostudy_init(scholar scholars[], int max_scholars, region *r)
for (u = r->units; u; u = u->next) {
keyword_t kwd = getkeyword(u->thisorder);
if (kwd == K_AUTOSTUDY) {
if (long_order_allowed(u) && unit_can_study(u)) {
scholar * st = scholars + nscholars;
if (++nscholars == max_scholars) {
log_fatal("you must increase MAXSCHOLARS");
@ -44,6 +47,11 @@ int autostudy_init(scholar scholars[], int max_scholars, region *r)
st->level = effskill_study(u, st->sk);
st->learn = 0;
}
else {
ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder, "error_race_nolearn", "race",
u_race(u)));
}
}
}
if (nscholars > 0) {
qsort(scholars, nscholars, sizeof(scholar), cmp_scholars);
@ -54,11 +62,13 @@ int autostudy_init(scholar scholars[], int max_scholars, region *r)
static void teaching(scholar *s, int n) {
assert(n <= s->u->number);
s->learn += n;
fset(s->u, UFL_LONGACTION);
}
static void learning(scholar *s, int n) {
assert(n <= s->u->number);
s->learn += n;
fset(s->u, UFL_LONGACTION);
}
void autostudy_run(scholar scholars[], int nscholars)

View File

@ -73,6 +73,7 @@ static void test_autostudy_run(CuTest *tc) {
CuAssertIntEquals(tc, 0, scholars[0].learn);
CuAssertIntEquals(tc, 20, scholars[1].learn);
CuAssertIntEquals(tc, 15, scholars[2].learn);
test_teardown();
}
static void test_autostudy_run_noteachers(CuTest *tc) {
@ -100,6 +101,7 @@ static void test_autostudy_run_noteachers(CuTest *tc) {
CuAssertIntEquals(tc, 2, scholars[0].learn);
CuAssertIntEquals(tc, 10, scholars[1].learn);
CuAssertIntEquals(tc, 15, scholars[2].learn);
test_teardown();
}
CuSuite *get_automate_suite(void)

View File

@ -3637,6 +3637,24 @@ void add_proc_unit(int priority, void(*process) (unit *), const char *name)
}
}
bool long_order_allowed(const unit *u)
{
const region *r = u->region;
if (fval(u, UFL_LONGACTION)) {
/* this message was already given in laws.update_long_order
cmistake(u, ord, 52, MSG_PRODUCE);
*/
return false;
}
else if (fval(r->terrain, SEA_REGION)
&& u_race(u) != get_race(RC_AQUARIAN)
&& !(u_race(u)->flags & RCF_SWIM)) {
/* error message disabled by popular demand */
return false;
}
return true;
}
/* per priority, execute processors in order from PR_GLOBAL down to PR_ORDER */
void process(void)
{
@ -3706,16 +3724,7 @@ void process(void)
cmistake(u, ord, 224, MSG_MAGIC);
ord = NULL;
}
else if (fval(u, UFL_LONGACTION)) {
/* this message was already given in laws.update_long_order
cmistake(u, ord, 52, MSG_PRODUCE);
*/
ord = NULL;
}
else if (fval(r->terrain, SEA_REGION)
&& u_race(u) != get_race(RC_AQUARIAN)
&& !(u_race(u)->flags & RCF_SWIM)) {
/* error message disabled by popular demand */
else if (!long_order_allowed(u)) {
ord = NULL;
}
}
@ -4157,7 +4166,7 @@ void update_subscriptions(void)
/** determine if unit can be seen by faction
* @param f -- the observiong faction
* @param u -- the unit that is observed
* @param r -- the region that u is obesrved in (see below)
* @param r -- the region that u is obesrved from (see below)
* @param m -- terrain modifier to stealth
*
* r kann != u->region sein, wenn es um Durchreisen geht,

View File

@ -66,6 +66,7 @@ extern "C" {
void update_long_order(struct unit *u);
void sinkships(struct region * r);
void do_enter(struct region *r, bool is_final_attempt);
bool long_order_allowed(const struct unit *u);
int password_cmd(struct unit *u, struct order *ord);
int banner_cmd(struct unit *u, struct order *ord);

View File

@ -1759,6 +1759,34 @@ static void test_nmr_timeout(CuTest *tc) {
test_teardown();
}
static void test_long_orders(CuTest *tc) {
unit *u;
test_setup();
u = test_create_unit(test_create_faction(NULL), test_create_plain(0, 0));
CuAssertTrue(tc, long_order_allowed(u));
u->flags |= UFL_LONGACTION;
CuAssertTrue(tc, !long_order_allowed(u));
test_teardown();
}
static void test_long_order_on_ocean(CuTest *tc) {
unit *u;
race * rc;
test_setup();
rc = test_create_race("pikachu");
u = test_create_unit(test_create_faction(rc), test_create_ocean(0, 0));
CuAssertTrue(tc, !long_order_allowed(u));
rc->flags |= RCF_SWIM;
CuAssertTrue(tc, long_order_allowed(u));
rc = test_create_race("aquarian");
u = test_create_unit(test_create_faction(rc), u->region);
CuAssertTrue(tc, long_order_allowed(u));
test_teardown();
}
CuSuite *get_laws_suite(void)
{
CuSuite *suite = CuSuiteNew();
@ -1831,6 +1859,8 @@ CuSuite *get_laws_suite(void)
SUITE_ADD_TEST(suite, test_cansee_ring);
SUITE_ADD_TEST(suite, test_cansee_sphere);
SUITE_ADD_TEST(suite, test_nmr_timeout);
SUITE_ADD_TEST(suite, test_long_orders);
SUITE_ADD_TEST(suite, test_long_order_on_ocean);
return suite;
}

View File

@ -89,15 +89,15 @@ static void setup_teacher(study_fixture *fix, skill_t sk) {
setup_locale(lang);
fix->u = test_create_unit(f, r);
assert(fix->u);
fix->u->thisorder = create_order(K_STUDY, f->locale, "%s", skillnames[sk]);
fix->u->thisorder = create_order(K_STUDY, f->locale, skillnames[sk]);
fix->teachers[0] = test_create_unit(f, r);
assert(fix->teachers[0]);
fix->teachers[0]->thisorder = create_order(K_TEACH, f->locale, "%s", itoa36(fix->u->no));
fix->teachers[0]->thisorder = create_order(K_TEACH, f->locale, itoa36(fix->u->no));
fix->teachers[1] = test_create_unit(f, r);
assert(fix->teachers[1]);
fix->teachers[1]->thisorder = create_order(K_TEACH, f->locale, "%s", itoa36(fix->u->no));
fix->teachers[1]->thisorder = create_order(K_TEACH, f->locale, itoa36(fix->u->no));
test_clear_messages(f);
}
@ -110,7 +110,7 @@ static void test_study_no_teacher(CuTest *tc) {
CuAssertPtrNotNull(tc, sv = unit_skill(fix.u, SK_CROSSBOW));
CuAssertIntEquals(tc, 1, sv->level);
CuAssertIntEquals(tc, 2, sv->weeks);
CuAssertPtrEquals(tc, 0, test_get_last_message(fix.u->faction->msgs));
CuAssertPtrEquals(tc, NULL, test_get_last_message(fix.u->faction->msgs));
test_teardown();
}
@ -121,7 +121,7 @@ static void test_study_with_teacher(CuTest *tc) {
setup_teacher(&fix, SK_CROSSBOW);
set_level(fix.teachers[0], SK_CROSSBOW, TEACHDIFFERENCE);
teach_cmd(fix.teachers[0], fix.teachers[0]->thisorder);
CuAssertPtrEquals(tc, 0, test_get_last_message(fix.u->faction->msgs));
CuAssertPtrEquals(tc, NULL, test_get_last_message(fix.u->faction->msgs));
study_cmd(fix.u, fix.u->thisorder);
CuAssertPtrNotNull(tc, sv = unit_skill(fix.u, SK_CROSSBOW));
CuAssertIntEquals(tc, 1, sv->level);
@ -288,7 +288,7 @@ static void test_academy_bonus(CuTest *tc) {
u1 = test_create_unit(u->faction, u->region);
u3 = test_create_unit(u->faction, u->region);
u0->thisorder = create_order(K_TEACH, loc, "%s %s", itoa36(u3->no), itoa36(u1->no));
u->thisorder = create_order(K_TEACH, loc, "%s", itoa36(u1->no));
u->thisorder = create_order(K_TEACH, loc, itoa36(u1->no));
u1->thisorder = create_order(K_STUDY, loc, skillnames[SK_CROSSBOW]);
u3->thisorder = create_order(K_STUDY, loc, skillnames[SK_CROSSBOW]);
@ -405,7 +405,7 @@ static void test_study_magic(CuTest *tc) {
f = test_create_faction(NULL);
lang = f->locale;
u = test_create_unit(f, test_create_region(0, 0, NULL));
u->thisorder = create_order(K_STUDY, lang, "%s", skillnames[SK_MAGIC]);
u->thisorder = create_order(K_STUDY, lang, skillnames[SK_MAGIC]);
itype = test_create_silver();
CuAssertIntEquals(tc, -1, study_cmd(u, u->thisorder));
@ -423,7 +423,7 @@ static void test_study_magic(CuTest *tc) {
CuAssertIntEquals(tc, M_GWYRRD, f->magiegebiet);
CuAssertIntEquals(tc, 0, i_get(u->items, itype));
CuAssertPtrNotNull(tc, get_mage_depr(u));
CuAssertPtrEquals(tc, 0, test_find_messagetype(f->msgs, "error65"));
CuAssertPtrEquals(tc, NULL, test_find_messagetype(f->msgs, "error65"));
CuAssertIntEquals(tc, M_GWYRRD, get_mage_depr(u)->magietyp);
test_teardown();
@ -491,12 +491,12 @@ static void test_teach_magic(CuTest *tc) {
f = test_create_faction(NULL);
f->magiegebiet = M_GWYRRD;
u = test_create_unit(f, test_create_region(0, 0, NULL));
u->thisorder = create_order(K_STUDY, f->locale, "%s", skillnames[SK_MAGIC]);
u->thisorder = create_order(K_STUDY, f->locale, skillnames[SK_MAGIC]);
i_change(&u->items, itype, study_cost(u, SK_MAGIC));
ut = test_create_unit(f, u->region);
set_level(ut, SK_MAGIC, TEACHDIFFERENCE);
create_mage(ut, M_GWYRRD);
ut->thisorder = create_order(K_TEACH, f->locale, "%s", itoa36(u->no));
ut->thisorder = create_order(K_TEACH, f->locale, itoa36(u->no));
learn_inject();
teach_cmd(ut, ut->thisorder);
study_cmd(u, u->thisorder);