diff --git a/src/economy.c b/src/economy.c index 7c4b0efd3..3781e0e8e 100644 --- a/src/economy.c +++ b/src/economy.c @@ -1446,24 +1446,19 @@ static void expandbuying(region * r, request * buyorders) const resource_type *rsilver = get_resourcetype(R_SILVER); int max_products; unit *u; - static struct trade { + struct trade { const luxury_type *type; int number; int multi; } trades[MAXLUXURIES], *trade; - static int ntrades = 0; - int i; + int ntrades = 0; const luxury_type *ltype; - if (ntrades == 0) { - for (ntrades = 0, ltype = luxurytypes; ltype; ltype = ltype->next) { - assert(ntrades < MAXLUXURIES); - trades[ntrades++].type = ltype; - } - } - for (i = 0; i != ntrades; ++i) { - trades[i].number = 0; - trades[i].multi = 1; + for (ntrades = 0, ltype = luxurytypes; ltype; ltype = ltype->next) { + assert(ntrades < MAXLUXURIES); + trades[ntrades].number = 0; + trades[ntrades].multi = 1; + trades[ntrades++].type = ltype; } if (!buyorders) @@ -1490,7 +1485,7 @@ static void expandbuying(region * r, request * buyorders) int price, multi; ltype = g_requests[j].type.ltype; trade = trades; - while (trade->type != ltype) + while (trade->type && trade->type != ltype) ++trade; multi = trade->multi; price = ltype->price * multi; diff --git a/src/economy.test.c b/src/economy.test.c index c7083ea4c..5f3ac3828 100644 --- a/src/economy.test.c +++ b/src/economy.test.c @@ -170,6 +170,60 @@ static void test_normals_recruit(CuTest * tc) { test_cleanup(); } +static region *setup_trade_region(CuTest *tc, const struct terrain_type *terrain) { + region *r; + item_type *it_luxury; + struct locale * lang = default_locale; + + new_luxurytype(it_luxury = test_create_itemtype("jewel"), 5); + locale_setstring(lang, it_luxury->rtype->_name, it_luxury->rtype->_name); + CuAssertStrEquals(tc, it_luxury->rtype->_name, LOC(lang, resourcename(it_luxury->rtype, 0))); + + new_luxurytype(it_luxury = test_create_itemtype("balm"), 5); + locale_setstring(lang, it_luxury->rtype->_name, it_luxury->rtype->_name); + CuAssertStrEquals(tc, it_luxury->rtype->_name, LOC(lang, resourcename(it_luxury->rtype, 0))); + + r = test_create_region(0, 0, terrain); + return r; +} + +static unit *setup_trade_unit(CuTest *tc, region *r, const struct race *rc) { + unit *u; + + UNUSED_ARG(tc); + u = test_create_unit(test_create_faction(rc), r); + set_level(u, SK_TRADE, 2); + return u; +} + +static void test_trade_insect(CuTest *tc) { + /* Insekten können in Wüsten und Sümpfen auch ohne Burgen handeln. */ + unit *u; + region *r; + const item_type *it_luxury; + const item_type *it_silver; + struct locale *lang; + + test_setup(); + init_resources(); + + lang = test_create_locale(); + it_silver = get_resourcetype(R_SILVER)->itype; + + r = setup_trade_region(tc, get_terrain("swamp")); + it_luxury = r_luxury(r); + CuAssertPtrNotNull(tc, it_luxury); + + u = setup_trade_unit(tc, r, test_create_race("insect")); + unit_addorder(u, create_order(K_BUY, u->faction->locale, "1 %s", + LOC(u->faction->locale, resourcename(it_luxury->rtype, 0)))); + + set_item(u, it_silver, 10); + produce(u->region); + CuAssertIntEquals(tc, 1, get_item(u, it_luxury)); + CuAssertIntEquals(tc, 5, get_item(u, it_silver)); +} + static void test_buy_cmd(CuTest *tc) { region * r; unit *u; @@ -622,6 +676,7 @@ CuSuite *get_economy_suite(void) 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_trade_insect); SUITE_ADD_TEST(suite, test_maintain_buildings); SUITE_ADD_TEST(suite, test_recruit); return suite; diff --git a/src/util/language.c b/src/util/language.c index b3cce61af..a1cda4d2f 100644 --- a/src/util/language.c +++ b/src/util/language.c @@ -45,9 +45,6 @@ typedef struct locale { struct locale_str *strings[SMAXHASH]; } locale; -extern locale *default_locale; -extern locale *locales; - locale *default_locale; locale *locales;