refactor econ_request union for readability.

This commit is contained in:
Enno Rehling 2018-02-03 20:24:16 +01:00
parent 613c0edf2f
commit e1d33e55a4
4 changed files with 41 additions and 13 deletions

View file

@ -88,7 +88,7 @@ static econ_request entertainers[1024];
static econ_request *nextentertainer;
static int entertaining;
static econ_request *g_requests;
static econ_request *g_requests; /* TODO: no need for this to be module-global */
#define RECRUIT_MERGE 1
static int rules_recruit = -1;
@ -181,7 +181,9 @@ unsigned int expand_production(region * r, econ_request * requests, econ_request
}
while (requests) {
econ_request *req = requests->next;
free_order(requests->ord);
if (requests->ord) {
free_order(requests->ord);
}
free(requests);
requests = req;
}
@ -1484,7 +1486,7 @@ static void expandbuying(region * r, econ_request * buyorders)
for (j = 0; j != norders; j++) {
int price, multi;
ltype = g_requests[j].type.ltype;
ltype = g_requests[j].type.trade.ltype;
trade = trades;
while (trade->type && trade->type != ltype)
++trade;
@ -1654,7 +1656,7 @@ static void buy(unit * u, econ_request ** buyorders, struct order *ord)
return;
}
o = (econ_request *)calloc(1, sizeof(econ_request));
o->type.ltype = ltype; /* sollte immer gleich sein */
o->type.trade.ltype = ltype; /* sollte immer gleich sein */
o->unit = u;
o->qty = n;
@ -1758,7 +1760,7 @@ static void expandselling(region * r, econ_request * sellorders, int limit)
for (j = 0; j != norders; j++) {
const luxury_type *search = NULL;
const luxury_type *ltype = g_requests[j].type.ltype;
const luxury_type *ltype = g_requests[j].type.trade.ltype;
int multi = r_demand(r, ltype);
int i;
int use = 0;
@ -1970,7 +1972,7 @@ static bool sell(unit * u, econ_request ** sellorders, struct order *ord)
/* Wenn andere Einheiten das selbe verkaufen, mu<6D> ihr Zeug abgezogen
* werden damit es nicht zweimal verkauft wird: */
for (o = *sellorders; o; o = o->next) {
if (o->type.ltype == ltype && o->unit->faction == u->faction) {
if (o->type.trade.ltype == ltype && o->unit->faction == u->faction) {
int fpool =
o->qty - get_pooled(o->unit, itype->rtype, GET_RESERVE, INT_MAX);
if (fpool < 0) fpool = 0;
@ -2010,7 +2012,7 @@ static bool sell(unit * u, econ_request ** sellorders, struct order *ord)
o = (econ_request *)calloc(1, sizeof(econ_request));
o->unit = u;
o->qty = n;
o->type.ltype = ltype;
o->type.trade.ltype = ltype;
addlist(sellorders, o);
return unlimited;

View file

@ -51,10 +51,14 @@ extern "C" {
struct unit *unit;
struct order *ord;
int qty;
int no;
union {
bool goblin; /* stealing */
const struct luxury_type *ltype; /* trading */
struct {
int no;
bool goblin; /* stealing */
} steal;
struct {
const struct luxury_type *ltype; /* trading */
} trade;
} type;
} econ_request;

View file

@ -720,6 +720,27 @@ static void test_loot(CuTest *tc) {
test_teardown();
}
static void test_expand_production(CuTest *tc) {
econ_request *orders;
econ_request *results = NULL;
region *r;
unit *u;
test_setup();
orders = calloc(1, sizeof(econ_request));
orders->qty = 2;
orders->unit = u = test_create_unit(test_create_faction(NULL), r = test_create_region(0, 0, NULL));
orders->next = NULL;
u->n = 1; /* will be overwritten */
CuAssertIntEquals(tc, 2, expand_production(r, orders, &results));
CuAssertPtrNotNull(tc, results);
CuAssertPtrEquals(tc, u, results[0].unit);
CuAssertPtrEquals(tc, u, results[1].unit);
CuAssertIntEquals(tc, 0, u->n);
test_teardown();
}
CuSuite *get_economy_suite(void)
{
CuSuite *suite = CuSuiteNew();
@ -740,5 +761,6 @@ CuSuite *get_economy_suite(void)
SUITE_ADD_TEST(suite, test_maintain_buildings);
SUITE_ADD_TEST(suite, test_recruit);
SUITE_ADD_TEST(suite, test_loot);
SUITE_ADD_TEST(suite, test_expand_production);
return suite;
}

View file

@ -72,7 +72,7 @@ void expandstealing(region * r, econ_request * stealorders)
break;
}
u = findunitg(requests[j].no, r);
u = findunitg(requests[j].type.steal.no, r);
if (u && u->region == r) {
n = get_pooled(u, rsilver, GET_ALL, INT_MAX);
@ -237,8 +237,8 @@ void steal_cmd(unit * u, struct order *ord, econ_request ** stealorders)
o = (econ_request *)calloc(1, sizeof(econ_request));
o->unit = u;
o->qty = 1; /* Betrag steht in u->wants */
o->no = u2->no;
o->type.goblin = goblin; /* Merken, wenn Goblin-Spezialklau */
o->type.steal.no = u2->no;
o->type.steal.goblin = goblin; /* Merken, wenn Goblin-Spezialklau */
o->next = *stealorders;
*stealorders = o;