Skip to content

Commit

Permalink
pay local taxes on combined sales, not each individual item.
Browse files Browse the repository at this point in the history
  • Loading branch information
ennorehling committed Mar 9, 2024
1 parent b63535a commit 7d00025
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 31 deletions.
36 changes: 19 additions & 17 deletions src/economy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1573,22 +1573,6 @@ static void expandselling(region * r, econ_request * sellorders, int limit)
price = ltype->price * multi;

if (money >= price) {
if (hafenowner) {
if (hafenowner->faction != u->faction) {
int abgezogenhafen = price / 10;
hafencollected += abgezogenhafen;
price -= abgezogenhafen;
money -= abgezogenhafen;
}
}
if (maxb) {
if (maxowner->faction != u->faction) {
int abgezogensteuer = price * tax_per_size[maxeffsize] / 100;
taxcollected += abgezogensteuer;
price -= abgezogensteuer;
money -= abgezogensteuer;
}
}
if (t) {
++t->trades;
i_change(&t->items, ltype->itype, 1);
Expand Down Expand Up @@ -1617,6 +1601,22 @@ static void expandselling(region * r, econ_request * sellorders, int limit)
}
}
if (income > 0) {
if (hafenowner) {
if (hafenowner->faction != u->faction) {
int abgezogenhafen = income / 10;
hafencollected += abgezogenhafen;
income -= abgezogenhafen;
money -= abgezogenhafen;
}
}
if (maxb) {
if (maxowner->faction != u->faction) {
int abgezogensteuer = income * tax_per_size[maxeffsize] / 100;
taxcollected += abgezogensteuer;
income -= abgezogensteuer;
money -= abgezogensteuer;
}
}
change_money(u, income);
fset(u, UFL_LONGACTION | UFL_NOTMOVING);
}
Expand Down Expand Up @@ -1646,14 +1646,16 @@ static void expandselling(region * r, econ_request * sellorders, int limit)
if (a) {
item* itm;
struct trade* t = NULL;
int income;
t = (struct trade*)a->data.v;
for (itm = t->items; itm; itm = itm->next) {
if (itm->number) {
ADDMSG(&u->faction->msgs, msg_message("sellamount",
"unit amount resource", u, itm->number, itm->type->rtype));
}
}
add_income(u, IC_TRADE, t->price, t->price);
income = t->price - hafencollected - taxcollected;
add_income(u, IC_TRADE, income, income);
}
}
}
Expand Down
69 changes: 55 additions & 14 deletions src/economy.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ static void test_sell_over_demand(CuTest* tc) {
rsetpeasants(r, TRADE_FRACTION * 10);
max_products = rpeasants(r) / TRADE_FRACTION;
r_setdemand(r, ltype, 2);
b = test_create_building(r, test_create_buildingtype("castle"));
b = test_create_building(r, test_create_castle());
b->size = 2;
u = test_create_unit(test_create_faction(), r);
set_level(u, SK_TRADE, 10);
Expand All @@ -368,6 +368,7 @@ static void test_sell_all(CuTest* tc) {
const item_type* it_luxury;
const luxury_type* ltype;
int max_products;
message *m;

test_setup();
setup_production();
Expand All @@ -377,7 +378,7 @@ static void test_sell_all(CuTest* tc) {
rsetpeasants(r, TRADE_FRACTION * 10);
max_products = rpeasants(r) / TRADE_FRACTION;
r_setdemand(r, ltype, 2);
b = test_create_building(r, test_create_buildingtype("castle"));
b = test_create_building(r, test_create_castle());
b->size = 2;
u = test_create_unit(test_create_faction(), r);
set_level(u, SK_TRADE, 10);
Expand All @@ -389,13 +390,53 @@ static void test_sell_all(CuTest* tc) {
CuAssertIntEquals(tc, 0, i_get(u->items, it_luxury));
CuAssertIntEquals(tc, 2, r_demand(r, ltype));
CuAssertIntEquals(tc, max_products * 2 * ltype->price, i_get(u->items, it_find("money")));
CuAssertPtrNotNull(tc, m = test_find_faction_message(u->faction, "income"));
CuAssertIntEquals(tc, max_products * 2 * ltype->price, m->parameters[2].i);
test_teardown();
}

static void test_sales_taxes(CuTest *tc) {
region *r;
unit *u, *ub;
building *b;
const item_type *it_luxury, *it_money;
const luxury_type *ltype;
int max_products, revenue;
message *m;

test_setup();
setup_production();
r = setup_trade_region(tc, NULL);
it_luxury = r_luxury(r);
it_money = it_find("money");
ltype = it_luxury->rtype->ltype;
rsetpeasants(r, TRADE_FRACTION * 100);
max_products = rpeasants(r) / TRADE_FRACTION;
r_setdemand(r, ltype, 2);
b = test_create_building(r, test_create_castle());
b->size = 10; // 12% sales tax
CuAssertIntEquals(tc, 2, bt_effsize(b->type, b, b->size));

ub = test_create_unit(test_create_faction(), r);
u_set_building(ub, b);

u = test_create_unit(test_create_faction(), r);
set_level(u, SK_TRADE, 10);
i_change(&u->items, it_luxury, max_products);
unit_addorder(u, create_order(K_SELL, u->faction->locale, "%d %s",
max_products,
LOC(u->faction->locale, resourcename(it_luxury->rtype, 0))));

produce(r);
CuAssertIntEquals(tc, 0, i_get(u->items, it_luxury));
CuAssertIntEquals(tc, 2, r_demand(r, ltype));
revenue = max_products * 2 * ltype->price;
CuAssertIntEquals(tc, revenue * 88 / 100, i_get(u->items, it_money));
CuAssertPtrNotNull(tc, m = test_find_faction_message(u->faction, "income"));
CuAssertIntEquals(tc, revenue * 88 / 100, m->parameters[2].i);
CuAssertIntEquals(tc, revenue * 12 / 100, i_get(ub->items, it_money));
CuAssertPtrNotNull(tc, m = test_find_faction_message(ub->faction, "income"));
CuAssertIntEquals(tc, revenue * 12 / 100, m->parameters[2].i);
test_teardown();
}

Expand All @@ -410,7 +451,7 @@ static void test_sell_nothing_message(CuTest *tc) {
r = setup_trade_region(tc, NULL);
it_luxury = r_luxury(r);
rsetpeasants(r, TRADE_FRACTION * 10);
b = test_create_building(r, test_create_buildingtype("castle"));
b = test_create_building(r, test_create_castle());
b->size = 2;
u = test_create_unit(test_create_faction(), r);
u2 = test_create_unit(test_create_faction(), r);
Expand Down Expand Up @@ -442,7 +483,7 @@ static void test_trade_limits(CuTest *tc) {
test_setup();
setup_production();
r = setup_trade_region(tc, NULL);
b = test_create_building(r, test_create_buildingtype("castle"));
b = test_create_building(r, test_create_castle());
b->size = 2;
rsetpeasants(r, TRADE_FRACTION * 20);
it_jewel = it_find("jewel");
Expand Down Expand Up @@ -472,7 +513,7 @@ static void test_trade_produceexp(CuTest *tc) {
setup_production();
config_set_int("study.produceexp", STUDYDAYS);
r = setup_trade_region(tc, NULL);
b = test_create_building(r, test_create_buildingtype("castle"));
b = test_create_building(r, test_create_castle());
b->size = 2;
rsetpeasants(r, TRADE_FRACTION * 20);
it_jewel = it_find("jewel");
Expand Down Expand Up @@ -501,7 +542,7 @@ static void test_buy_limits(CuTest *tc) {
test_setup();
setup_production();
r = setup_trade_region(tc, NULL);
b = test_create_building(r, test_create_buildingtype("castle"));
b = test_create_building(r, test_create_castle());
b->size = 2;
rsetpeasants(r, TRADE_FRACTION * 20);
it_jewel = it_find("jewel");
Expand Down Expand Up @@ -552,7 +593,7 @@ static void test_trade_needs_castle(CuTest *tc) {

test_clear_messages(u->faction);
freset(u, UFL_LONGACTION);
b = test_create_building(r, test_create_buildingtype("castle"));
b = test_create_building(r, test_create_castle());
b->size = 1;
produce(r);
CuAssertIntEquals(tc, 2, test_count_messagetype(u->faction->msgs, "error119"));
Expand Down Expand Up @@ -620,7 +661,7 @@ static void test_buy_prices(CuTest* tc) {
CuAssertPtrNotNull(tc, rt_silver);
CuAssertPtrNotNull(tc, rt_silver->itype);

b = test_create_building(r, test_create_buildingtype("castle"));
b = test_create_building(r, test_create_castle());
b->size = 2;
sold = max_luxuries_sold(r);
costs = sold * it_luxury->rtype->ltype->price;
Expand Down Expand Up @@ -655,7 +696,7 @@ static void test_buy_prices_split(CuTest* tc) {
CuAssertPtrNotNull(tc, rt_silver);
CuAssertPtrNotNull(tc, rt_silver->itype);

b = test_create_building(r, test_create_buildingtype("castle"));
b = test_create_building(r, test_create_castle());
b->size = 2;
sold = max_luxuries_sold(r);
costs = sold * it_luxury->rtype->ltype->price;
Expand Down Expand Up @@ -695,7 +736,7 @@ static void test_buy_prices_rising(CuTest* tc) {
CuAssertPtrNotNull(tc, rt_silver);
CuAssertPtrNotNull(tc, rt_silver->itype);

b = test_create_building(r, test_create_buildingtype("castle"));
b = test_create_building(r, test_create_castle());
b->size = 2;
u = test_create_unit(test_create_faction(), r);
sold = max_luxuries_sold(r);
Expand All @@ -720,7 +761,7 @@ static void test_trade_is_long_action(CuTest *tc) {
test_setup();
setup_production();
r = setup_trade_region(tc, NULL);
test_create_building(r, test_create_buildingtype("castle"))->size = 2;
test_create_building(r, test_create_castle())->size = 2;
rt_silver = get_resourcetype(R_SILVER);
CuAssertPtrNotNull(tc, rt_silver);
CuAssertPtrNotNull(tc, rt_silver->itype);
Expand Down Expand Up @@ -787,7 +828,7 @@ static void test_buy_cmd(CuTest *tc) {
test_clear_messages(u->faction);
freset(u, UFL_LONGACTION);

b = test_create_building(r, test_create_buildingtype("castle"));
b = test_create_building(r, test_create_castle());
produce(r);
CuAssertPtrNotNullMsg(tc, "castle must have size >=2", test_find_messagetype(u->faction->msgs, "error119"));
test_clear_messages(u->faction);
Expand Down Expand Up @@ -840,7 +881,7 @@ static void test_buy_before_sell(CuTest* tc) {
set_level(u, SK_TRADE, 4);
test_set_item(u, rt_silver->itype, 5000);
test_set_item(u, it_other, 1000);
b = test_create_building(r, test_create_buildingtype("castle"));
b = test_create_building(r, test_create_castle());
b->size = 2;
produce(r);
CuAssertIntEquals(tc, 80, get_item(u, it_luxury));
Expand Down Expand Up @@ -874,7 +915,7 @@ static void test_buy_twice(CuTest *tc) {
unit_addorder(u, create_order(K_BUY, u->faction->locale, "%d %s", sold, LOC(u->faction->locale, resourcename(it_luxury->rtype, 0))));
set_level(u, SK_TRADE, (1 + sold) / 5);
test_set_item(u, rt_silver->itype, 1000);
b = test_create_building(r, test_create_buildingtype("castle"));
b = test_create_building(r, test_create_castle());
b->size = 2;
produce(r);
CuAssertIntEquals(tc, 2 * sold, get_item(u, it_luxury));
Expand Down

0 comments on commit 7d00025

Please sign in to comment.