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

View file

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

View file

@ -720,6 +720,27 @@ static void test_loot(CuTest *tc) {
test_teardown(); 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 *get_economy_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
@ -740,5 +761,6 @@ CuSuite *get_economy_suite(void)
SUITE_ADD_TEST(suite, test_maintain_buildings); SUITE_ADD_TEST(suite, test_maintain_buildings);
SUITE_ADD_TEST(suite, test_recruit); SUITE_ADD_TEST(suite, test_recruit);
SUITE_ADD_TEST(suite, test_loot); SUITE_ADD_TEST(suite, test_loot);
SUITE_ADD_TEST(suite, test_expand_production);
return suite; return suite;
} }

View file

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