remove a lot of struct copying in the stupid expand function.

This commit is contained in:
Enno Rehling 2018-02-03 20:45:19 +01:00
parent e1d33e55a4
commit 6f7c8743b3
4 changed files with 149 additions and 140 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; /* TODO: no need for this to be module-global */ 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;
@ -141,7 +141,7 @@ static void scramble(void *data, unsigned int n, size_t width)
} }
} }
unsigned int expand_production(region * r, econ_request * requests, econ_request **results) unsigned int expand_production(region * r, econ_request * requests, econ_request ***results)
{ {
unit *u; unit *u;
econ_request *o; econ_request *o;
@ -161,24 +161,28 @@ unsigned int expand_production(region * r, econ_request * requests, econ_request
if (norders > 0) { if (norders > 0) {
int i = 0; int i = 0;
econ_request *split; econ_request **split;
split = (econ_request *)calloc(norders, sizeof(econ_request)); split = calloc(norders, sizeof(econ_request *));
for (o = requests; o; o = o->next) { for (o = requests; o; o = o->next) {
if (o->qty > 0) { if (o->qty > 0) {
unsigned int j; unsigned int j;
for (j = o->qty; j; j--) { for (j = o->qty; j; j--) {
split[i] = *o; split[i] = o;
split[i].unit->n = 0; o->unit->n = 0;
i++; i++;
} }
} }
} }
scramble(split, norders, sizeof(econ_request)); scramble(split, norders, sizeof(econ_request *));
*results = split; *results = split;
} }
else { else {
*results = NULL; *results = NULL;
} }
return norders;
}
static void free_requests(econ_request *requests) {
while (requests) { while (requests) {
econ_request *req = requests->next; econ_request *req = requests->next;
if (requests->ord) { if (requests->ord) {
@ -187,7 +191,6 @@ unsigned int expand_production(region * r, econ_request * requests, econ_request
free(requests); free(requests);
requests = req; requests = req;
} }
return norders;
} }
static unsigned int expandorders(region * r, econ_request * requests) { static unsigned int expandorders(region * r, econ_request * requests) {
@ -1479,21 +1482,20 @@ static void expandbuying(region * r, econ_request * buyorders)
* G<EFBFBD>ter pro Monat ist. j sind die Befehle, i der Index des * G<EFBFBD>ter pro Monat ist. j sind die Befehle, i der Index des
* gehandelten Produktes. */ * gehandelten Produktes. */
if (max_products > 0) { if (max_products > 0) {
unsigned int j;
unsigned int norders = expandorders(r, buyorders); unsigned int norders = expandorders(r, buyorders);
if (!norders)
return;
if (norders) {
unsigned int j;
for (j = 0; j != norders; j++) { for (j = 0; j != norders; j++) {
int price, multi; int price, multi;
ltype = g_requests[j].type.trade.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;
multi = trade->multi; multi = trade->multi;
price = ltype->price * multi; price = ltype->price * multi;
if (get_pooled(g_requests[j].unit, rsilver, GET_DEFAULT, if (get_pooled(g_requests[j]->unit, rsilver, GET_DEFAULT,
price) >= price) { price) >= price) {
item *items; item *items;
/* litems z<>hlt die G<>ter, die verkauft wurden, u->n das Geld, das /* litems z<>hlt die G<>ter, die verkauft wurden, u->n das Geld, das
@ -1502,7 +1504,7 @@ static void expandbuying(region * r, econ_request * buyorders)
* merken mu<EFBFBD>. */ * merken mu<EFBFBD>. */
attrib *a; attrib *a;
u = g_requests[j].unit; u = g_requests[j]->unit;
a = a_find(u->attribs, &at_luxuries); a = a_find(u->attribs, &at_luxuries);
if (a == NULL) { if (a == NULL) {
a = a_add(&u->attribs, a_new(&at_luxuries)); a = a_add(&u->attribs, a_new(&at_luxuries));
@ -1511,7 +1513,7 @@ static void expandbuying(region * r, econ_request * buyorders)
i_change(&items, ltype->itype, 1); i_change(&items, ltype->itype, 1);
a->data.v = items; a->data.v = items;
i_change(&g_requests[j].unit->items, ltype->itype, 1); i_change(&g_requests[j]->unit->items, ltype->itype, 1);
use_pooled(u, rsilver, GET_DEFAULT, price); use_pooled(u, rsilver, GET_DEFAULT, price);
if (u->n < 0) if (u->n < 0)
u->n = 0; u->n = 0;
@ -1529,6 +1531,7 @@ static void expandbuying(region * r, econ_request * buyorders)
fset(u, UFL_LONGACTION | UFL_NOTMOVING); fset(u, UFL_LONGACTION | UFL_NOTMOVING);
} }
} }
}
free(g_requests); free(g_requests);
/* Ausgabe an Einheiten */ /* Ausgabe an Einheiten */
@ -1754,13 +1757,10 @@ static void expandselling(region * r, econ_request * sellorders, int limit)
* Runde erweitert werden kann. */ * Runde erweitert werden kann. */
norders = expandorders(r, sellorders); norders = expandorders(r, sellorders);
if (norders == 0) { if (norders > 0) {
return;
}
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.trade.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;
@ -1777,7 +1777,7 @@ static void expandselling(region * r, econ_request * sellorders, int limit)
if (money >= price) { if (money >= price) {
item *itm; item *itm;
attrib *a; attrib *a;
u = g_requests[j].unit; u = g_requests[j]->unit;
a = a_find(u->attribs, &at_luxuries); a = a_find(u->attribs, &at_luxuries);
if (!a) { if (!a) {
a = a_add(&u->attribs, a_new(&at_luxuries)); a = a_add(&u->attribs, a_new(&at_luxuries));
@ -1826,7 +1826,8 @@ static void expandselling(region * r, econ_request * sellorders, int limit)
} }
} }
if (use > 0) { if (use > 0) {
use_pooled(g_requests[j].unit, ltype->itype->rtype, GET_DEFAULT, use); use_pooled(g_requests[j]->unit, ltype->itype->rtype, GET_DEFAULT, use);
}
} }
} }
free(g_requests); free(g_requests);
@ -2520,8 +2521,8 @@ static void expandloot(region * r, econ_request * lootorders)
int startmoney = rmoney(r); int startmoney = rmoney(r);
for (i = 0; i != norders && startmoney > looted + TAXFRACTION * 2; i++) { for (i = 0; i != norders && startmoney > looted + TAXFRACTION * 2; i++) {
change_money(g_requests[i].unit, TAXFRACTION); change_money(g_requests[i]->unit, TAXFRACTION);
g_requests[i].unit->n += TAXFRACTION; g_requests[i]->unit->n += TAXFRACTION;
/*Looting destroys double the money*/ /*Looting destroys double the money*/
looted += TAXFRACTION * 2; looted += TAXFRACTION * 2;
} }
@ -2554,8 +2555,8 @@ void expandtax(region * r, econ_request * taxorders)
if (norders > 0) { if (norders > 0) {
unsigned int i; unsigned int i;
for (i = 0; i != norders && rmoney(r) > TAXFRACTION; i++) { for (i = 0; i != norders && rmoney(r) > TAXFRACTION; i++) {
change_money(g_requests[i].unit, TAXFRACTION); change_money(g_requests[i]->unit, TAXFRACTION);
g_requests[i].unit->n += TAXFRACTION; g_requests[i]->unit->n += TAXFRACTION;
rsetmoney(r, rmoney(r) - TAXFRACTION); rsetmoney(r, rmoney(r) - TAXFRACTION);
} }
free(g_requests); free(g_requests);
@ -2938,18 +2939,23 @@ void produce(struct region *r)
if (!rule_autowork()) { if (!rule_autowork()) {
expandwork(r, workers, nextworker, region_maxworkers(r)); expandwork(r, workers, nextworker, region_maxworkers(r));
} }
if (taxorders) if (taxorders) {
expandtax(r, taxorders); expandtax(r, taxorders);
free_requests(taxorders);
}
if (lootorders) if (lootorders) {
expandloot(r, lootorders); expandloot(r, lootorders);
free_requests(lootorders);
}
/* An erster Stelle Kaufen (expandbuying), die Bauern so Geld bekommen, um /* An erster Stelle Kaufen (expandbuying), die Bauern so Geld bekommen, um
* nachher zu beim Verkaufen (expandselling) den Spielern abkaufen zu * nachher zu beim Verkaufen (expandselling) den Spielern abkaufen zu
* k<EFBFBD>nnen. */ * k<EFBFBD>nnen. */
if (buyorders) if (buyorders) {
expandbuying(r, buyorders); expandbuying(r, buyorders);
free_requests(buyorders);
}
if (sellorders) { if (sellorders) {
int limit = rpeasants(r) / TRADE_FRACTION; int limit = rpeasants(r) / TRADE_FRACTION;
@ -2957,13 +2963,16 @@ void produce(struct region *r)
&& buildingtype_exists(r, caravan_bt, true)) && buildingtype_exists(r, caravan_bt, true))
limit *= 2; limit *= 2;
expandselling(r, sellorders, limited ? limit : INT_MAX); expandselling(r, sellorders, limited ? limit : INT_MAX);
free_requests(sellorders);
} }
/* Die Spieler sollen alles Geld verdienen, bevor sie beklaut werden /* Die Spieler sollen alles Geld verdienen, bevor sie beklaut werden
* (expandstealing). */ * (expandstealing). */
if (stealorders) if (stealorders) {
expandstealing(r, stealorders); expandstealing(r, stealorders);
free_requests(stealorders);
}
assert(rmoney(r) >= 0); assert(rmoney(r) >= 0);
assert(rpeasants(r) >= 0); assert(rpeasants(r) >= 0);

View file

@ -69,7 +69,7 @@ extern "C" {
void produce(struct region *r); void produce(struct region *r);
void auto_work(struct region *r); void auto_work(struct region *r);
unsigned int expand_production(struct region * r, struct econ_request * requests, struct econ_request **results); unsigned int expand_production(struct region * r, struct econ_request * requests, struct econ_request ***results);
typedef enum income_t { IC_WORK, IC_ENTERTAIN, IC_TAX, IC_TRADE, IC_TRADETAX, IC_STEAL, IC_MAGIC, IC_LOOT } income_t; typedef enum income_t { IC_WORK, IC_ENTERTAIN, IC_TAX, IC_TRADE, IC_TRADETAX, IC_STEAL, IC_MAGIC, IC_LOOT } income_t;
void add_income(struct unit * u, income_t type, int want, int qty); void add_income(struct unit * u, income_t type, int want, int qty);

View file

@ -722,7 +722,7 @@ static void test_loot(CuTest *tc) {
static void test_expand_production(CuTest *tc) { static void test_expand_production(CuTest *tc) {
econ_request *orders; econ_request *orders;
econ_request *results = NULL; econ_request **results = NULL;
region *r; region *r;
unit *u; unit *u;
@ -735,8 +735,8 @@ static void test_expand_production(CuTest *tc) {
u->n = 1; /* will be overwritten */ u->n = 1; /* will be overwritten */
CuAssertIntEquals(tc, 2, expand_production(r, orders, &results)); CuAssertIntEquals(tc, 2, expand_production(r, orders, &results));
CuAssertPtrNotNull(tc, results); CuAssertPtrNotNull(tc, results);
CuAssertPtrEquals(tc, u, results[0].unit); CuAssertPtrEquals(tc, u, results[0]->unit);
CuAssertPtrEquals(tc, u, results[1].unit); CuAssertPtrEquals(tc, u, results[1]->unit);
CuAssertIntEquals(tc, 0, u->n); CuAssertIntEquals(tc, 0, u->n);
test_teardown(); test_teardown();
} }

View file

@ -51,7 +51,7 @@ void expandstealing(region * r, econ_request * stealorders)
const resource_type *rsilver = get_resourcetype(R_SILVER); const resource_type *rsilver = get_resourcetype(R_SILVER);
unsigned int j; unsigned int j;
unsigned int norders; unsigned int norders;
econ_request *requests; econ_request **requests;
assert(rsilver); assert(rsilver);
@ -68,11 +68,11 @@ void expandstealing(region * r, econ_request * stealorders)
unit *u; unit *u;
int n = 0; int n = 0;
if (requests[j].unit->n > requests[j].unit->wants) { if (requests[j]->unit->n > requests[j]->unit->wants) {
break; break;
} }
u = findunitg(requests[j].type.steal.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);
@ -82,16 +82,16 @@ void expandstealing(region * r, econ_request * stealorders)
n = 10; n = 10;
} }
if (n > 0) { if (n > 0) {
int w = requests[j].unit->wants; int w = requests[j]->unit->wants;
if (n > w) n = w; if (n > w) n = w;
use_pooled(u, rsilver, GET_ALL, n); use_pooled(u, rsilver, GET_ALL, n);
requests[j].unit->n = n; requests[j]->unit->n = n;
change_money(requests[j].unit, n); change_money(requests[j]->unit, n);
ADDMSG(&u->faction->msgs, msg_message("stealeffect", "unit region amount", ADDMSG(&u->faction->msgs, msg_message("stealeffect", "unit region amount",
u, u->region, n)); u, u->region, n));
} }
add_income(requests[j].unit, IC_STEAL, requests[j].unit->wants, requests[j].unit->n); add_income(requests[j]->unit, IC_STEAL, requests[j]->unit->wants, requests[j]->unit->n);
fset(requests[j].unit, UFL_LONGACTION | UFL_NOTMOVING); fset(requests[j]->unit, UFL_LONGACTION | UFL_NOTMOVING);
} }
free(requests); free(requests);
} }