server/src/market.c

176 lines
4.7 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
/* vi: set ts=2:
+-------------------+ Christian Schlittchen <corwin@amber.kn-bremen.de>
| | Enno Rehling <enno@eressea.de>
| Eressea PBEM host | Katja Zedel <katze@felidae.kn-bremen.de>
| (c) 1998 - 2003 | Henning Peters <faroul@beyond.kn-bremen.de>
| | Ingo Wilken <Ingo.Wilken@informatik.uni-oldenburg.de>
+-------------------+ Stefan Reich <reich@halbling.de>
This program may not be used, modified or distributed
without prior permission by the authors of Eressea.
*/
#include <platform.h>
#include <kernel/config.h>
#include "market.h"
#include <assert.h>
#include <util/attrib.h>
#include <quicklist.h>
2010-08-08 10:06:34 +02:00
#include <util/rng.h>
#include <kernel/building.h>
#include <kernel/faction.h>
#include <kernel/item.h>
#include <kernel/messages.h>
2010-08-08 10:06:34 +02:00
#include <kernel/race.h>
#include <kernel/region.h>
#include <kernel/unit.h>
2011-03-07 08:02:35 +01:00
static unsigned int get_markets(region * r, unit ** results, size_t size)
2010-08-08 10:06:34 +02:00
{
unsigned int n = 0;
2011-03-07 08:02:35 +01:00
building *b;
const building_type *btype = bt_find("market");
2011-03-07 08:02:35 +01:00
if (!btype)
return 0;
2011-03-07 08:02:35 +01:00
for (b = r->buildings; n < size && b; b = b->next) {
if (b->type == btype && (b->flags & BLD_WORKING)
&& b->size >= b->type->maxsize) {
unit *u = building_owner(b);
2010-08-08 10:06:34 +02:00
unsigned int i;
2011-03-07 08:02:35 +01:00
for (i = 0; u && i != n; ++i) {
2010-08-08 10:06:34 +02:00
/* only one market per faction */
2011-03-07 08:02:35 +01:00
if (results[i]->faction == u->faction)
u = NULL;
2010-08-08 10:06:34 +02:00
}
if (u) {
results[n++] = u;
}
}
}
return n;
}
2011-03-07 08:02:35 +01:00
static void free_market(attrib * a)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
item *items = (item *) a->data.v;
2010-08-08 10:06:34 +02:00
i_freeall(&items);
a->data.v = 0;
}
attrib_type at_market = {
"script",
NULL, free_market, NULL,
NULL, NULL, ATF_UNIQUE
};
2011-03-07 08:02:35 +01:00
static int rc_luxury_trade(const struct race *rc)
2010-08-08 10:06:34 +02:00
{
if (rc) {
return get_param_int(rc->parameters, "luxury_trade", 1000);
}
return 1000;
}
2011-03-07 08:02:35 +01:00
static int rc_herb_trade(const struct race *rc)
2010-08-08 10:06:34 +02:00
{
if (rc) {
return get_param_int(rc->parameters, "herb_trade", 500);
}
return 500;
}
#define MAX_MARKETS 128
2011-03-07 08:02:35 +01:00
#define MIN_PEASANTS 50 /* if there are at least this many peasants, you will get 1 good */
2010-08-08 10:06:34 +02:00
void do_markets(void)
{
2011-03-07 08:02:35 +01:00
quicklist *traders = 0;
unit *markets[MAX_MARKETS];
region *r;
for (r = regions; r; r = r->next) {
2010-08-08 10:06:34 +02:00
if (r->land) {
2011-03-07 08:02:35 +01:00
faction *f = region_get_owner(r);
const struct race *rc = f ? f->race : NULL;
2010-08-08 10:06:34 +02:00
int p = rpeasants(r);
int numlux = rc_luxury_trade(rc), numherbs = rc_herb_trade(rc);
2011-03-07 08:02:35 +01:00
numlux = (p + numlux - MIN_PEASANTS) / numlux;
numherbs = (p + numherbs - MIN_PEASANTS) / numherbs;
if (numlux > 0 || numherbs > 0) {
2010-08-08 10:06:34 +02:00
int d, nmarkets = 0;
2011-03-07 08:02:35 +01:00
const item_type *lux = r_luxury(r);
const item_type *herb = r->land->herbtype;
nmarkets += get_markets(r, markets + nmarkets, MAX_MARKETS - nmarkets);
for (d = 0; d != MAXDIRECTIONS; ++d) {
region *r2 = rconnect(r, d);
2010-08-08 10:06:34 +02:00
if (r2 && r2->buildings) {
2011-03-07 08:02:35 +01:00
nmarkets +=
get_markets(r2, markets + nmarkets, MAX_MARKETS - nmarkets);
2010-08-08 10:06:34 +02:00
}
}
if (nmarkets) {
while (lux && numlux--) {
int n = rng_int() % nmarkets;
2011-03-07 08:02:35 +01:00
unit *u = markets[n];
item *items;
attrib *a = a_find(u->attribs, &at_market);
if (a == NULL) {
2010-08-08 10:06:34 +02:00
a = a_add(&u->attribs, a_new(&at_market));
ql_push(&traders, u);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
items = (item *) a->data.v;
2010-08-08 10:06:34 +02:00
i_change(&items, lux, 1);
a->data.v = items;
/* give 1 luxury */
}
while (herb && numherbs--) {
int n = rng_int() % nmarkets;
2011-03-07 08:02:35 +01:00
unit *u = markets[n];
item *items;
attrib *a = a_find(u->attribs, &at_market);
if (a == NULL) {
2010-08-08 10:06:34 +02:00
a = a_add(&u->attribs, a_new(&at_market));
ql_push(&traders, u);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
items = (item *) a->data.v;
2010-08-08 10:06:34 +02:00
i_change(&items, herb, 1);
a->data.v = items;
/* give 1 herb */
}
}
}
}
}
if (traders) {
2011-03-07 08:02:35 +01:00
quicklist *qliter = traders;
int qli = 0;
2011-03-07 08:02:35 +01:00
for (qli = 0; qliter; ql_advance(&qliter, &qli, 1)) {
unit *u = (unit *) ql_get(qliter, qli);
attrib *a = a_find(u->attribs, &at_market);
item *items = (item *) a->data.v;
a->data.v = NULL;
while (items) {
2011-03-07 08:02:35 +01:00
item *itm = items;
items = itm->next;
if (itm->number) {
ADDMSG(&u->faction->msgs, msg_message("buyamount",
2011-03-07 08:02:35 +01:00
"unit amount resource", u, itm->number, itm->type->rtype));
itm->next = NULL;
i_add(&u->items, itm);
} else {
i_free(itm);
}
2010-08-08 10:06:34 +02:00
}
a_remove(&u->attribs, a);
}
ql_free(traders);
2010-08-08 10:06:34 +02:00
}
}