static int tolua_region_get_luxury(lua_State * L) { region *r = (region *)tolua_tousertype(L, 1, 0); if (r->land) { const item_type *lux = r_luxury(r); if (lux) { const char *name = lux->rtype->_name; tolua_pushstring(L, name); return 1; } } return 0; }
static int tolua_region_set_luxury(lua_State * L) { region *r = (region *)tolua_tousertype(L, 1, 0); const char *name = tolua_tostring(L, 2, 0); if (r->land && name) { const item_type *lux = r_luxury(r); const item_type *itype = it_find(name); if (lux && itype && lux != itype) { r_setdemand(r, lux->rtype->ltype, 1); r_setdemand(r, itype->rtype->ltype, 0); } } return 0; }
static void test_fix_demand(CuTest *tc) { region *r; terrain_type *tplain; item_type *ltype; test_cleanup(); ltype = test_create_itemtype("balm"); ltype->rtype->flags |= (RTF_ITEM | RTF_POOLED); new_luxurytype(ltype, 0); ltype = test_create_itemtype("oint"); ltype->rtype->flags |= (RTF_ITEM | RTF_POOLED); new_luxurytype(ltype, 0); tplain = test_create_terrain("plain", LAND_REGION); r = new_region(0, 0, NULL, 0); CuAssertPtrNotNull(tc, r); terraform_region(r, tplain); CuAssertPtrNotNull(tc, r->land); CuAssertIntEquals(tc, 0, fix_demand(r)); CuAssertPtrNotNull(tc, r->land->demands); CuAssertPtrNotNull(tc, r->land->demands->next); CuAssertPtrNotNull(tc, r_luxury(r)); test_cleanup(); }
void do_markets(void) { quicklist *traders = 0; unit *markets[MAX_MARKETS]; region *r; for (r = regions; r; r = r->next) { if (r->land) { faction *f = region_get_owner(r); const struct race *rc = f ? f->race : NULL; int p = rpeasants(r); int numlux = rc_luxury_trade(rc), numherbs = rc_herb_trade(rc); numlux = (p + numlux - MIN_PEASANTS) / numlux; numherbs = (p + numherbs - MIN_PEASANTS) / numherbs; if (numlux > 0 || numherbs > 0) { int d, nmarkets = 0; 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); if (r2 && r2->buildings) { nmarkets += get_markets(r2, markets + nmarkets, MAX_MARKETS - nmarkets); } } if (nmarkets) { while (lux && numlux--) { int n = rng_int() % nmarkets; unit *u = markets[n]; item *items; attrib *a = a_find(u->attribs, &at_market); if (a == NULL) { a = a_add(&u->attribs, a_new(&at_market)); ql_push(&traders, u); } items = (item *)a->data.v; i_change(&items, lux, 1); a->data.v = items; /* give 1 luxury */ } while (herb && numherbs--) { int n = rng_int() % nmarkets; unit *u = markets[n]; item *items; attrib *a = a_find(u->attribs, &at_market); if (a == NULL) { a = a_add(&u->attribs, a_new(&at_market)); ql_push(&traders, u); } items = (item *)a->data.v; i_change(&items, herb, 1); a->data.v = items; /* give 1 herb */ } } } } } if (traders) { quicklist *qliter = traders; int qli = 0; 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) { item *itm = items; items = itm->next; if (itm->number) { ADDMSG(&u->faction->msgs, msg_message("buyamount", "unit amount resource", u, itm->number, itm->type->rtype)); itm->next = NULL; i_add(&u->items, itm); } else { i_free(itm); } } a_remove(&u->attribs, a); } ql_free(traders); } }