diff --git a/src/economy.test.c b/src/economy.test.c index 83c103f7f..c7083ea4c 100644 --- a/src/economy.test.c +++ b/src/economy.test.c @@ -170,6 +170,59 @@ static void test_normals_recruit(CuTest * tc) { test_cleanup(); } +static void test_buy_cmd(CuTest *tc) { + region * r; + unit *u; + building *b; + const resource_type *rt_silver; + const resource_type *rt_luxury; + test_setup(); + + init_resources(); + rt_luxury = get_resourcetype(R_HORSE); + CuAssertPtrNotNull(tc, rt_luxury); + CuAssertPtrNotNull(tc, rt_luxury->itype); + new_luxurytype(rt_luxury->itype, 5); + CuAssertPtrNotNull(tc, rt_luxury->ltype); + rt_silver = get_resourcetype(R_SILVER); + CuAssertPtrNotNull(tc, rt_silver); + CuAssertPtrNotNull(tc, rt_silver->itype); + + r = test_create_region(0, 0, NULL); + fix_demand(r); + CuAssertPtrEquals(tc, rt_luxury->itype, (void *)r_luxury(r)); + u = test_create_unit(test_create_faction(NULL), r); + unit_addorder(u, create_order(K_BUY, u->faction->locale, "1 %s", LOC(u->faction->locale, resourcename(rt_luxury, 0)))); + set_item(u, rt_silver->itype, 1000); + + produce(r); + CuAssertPtrNotNullMsg(tc, "trading requires a castle", test_find_messagetype(u->faction->msgs, "error119")); + test_clear_messages(u->faction); + freset(u, UFL_LONGACTION); + + b = test_create_building(r, test_create_buildingtype("castle")); + produce(r); + CuAssertPtrNotNullMsg(tc, "castle must have size >=2", test_find_messagetype(u->faction->msgs, "error119")); + test_clear_messages(u->faction); + freset(u, UFL_LONGACTION); + + b->size = 2; + produce(r); + CuAssertPtrEquals(tc, NULL, test_find_messagetype(u->faction->msgs, "error119")); + CuAssertPtrNotNullMsg(tc, "traders need SK_TRADE skill", test_find_messagetype(u->faction->msgs, "error102")); + test_clear_messages(u->faction); + freset(u, UFL_LONGACTION); + + /* at last, the happy case: */ + set_level(u, SK_TRADE, 1); + produce(r); + CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "buy")); + CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "buyamount")); + CuAssertIntEquals(tc, 1, get_item(u, rt_luxury->itype)); + CuAssertIntEquals(tc, 995, get_item(u, rt_silver->itype)); + test_cleanup(); +} + typedef struct request { struct request *next; struct unit *unit; @@ -568,6 +621,7 @@ CuSuite *get_economy_suite(void) SUITE_ADD_TEST(suite, test_normals_recruit); SUITE_ADD_TEST(suite, test_heroes_dont_recruit); SUITE_ADD_TEST(suite, test_tax_cmd); + SUITE_ADD_TEST(suite, test_buy_cmd); SUITE_ADD_TEST(suite, test_maintain_buildings); SUITE_ADD_TEST(suite, test_recruit); return suite; diff --git a/src/kernel/item.h b/src/kernel/item.h index 51ebcd2a1..6477a4923 100644 --- a/src/kernel/item.h +++ b/src/kernel/item.h @@ -224,7 +224,7 @@ extern "C" { /* convenience: */ item *i_change(item ** items, const item_type * it, int delta); - int i_get(const item * i, const item_type * it); + int i_get(const item * items, const item_type * it); /* creation */ resource_type *rt_get_or_create(const char *name); @@ -300,7 +300,6 @@ extern "C" { void register_resources(void); void init_resources(void); - void init_itemtypes(void); void register_item_give(int(*foo) (struct unit *, struct unit *, const struct item_type *, int, struct order *), const char *name); diff --git a/src/kernel/region.c b/src/kernel/region.c index c7e3841fb..3dad8e3ab 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -711,7 +711,7 @@ const item_type *r_luxury(const region * r) { struct demand *dmd; if (r->land) { - assert(r->land->demands || !"need to call fix_demands on a region"); + assert(r->land->demands || !"need to call fix_demand on a region"); for (dmd = r->land->demands; dmd; dmd = dmd->next) { if (dmd->value == 0) return dmd->type->itype;