static int tolua_region_create(lua_State * L) { int x = (int)tolua_tonumber(L, 1, 0); int y = (int)tolua_tonumber(L, 2, 0); const char *tname = tolua_tostring(L, 3, 0); if (tname) { plane *pl = findplane(x, y); const terrain_type *terrain = get_terrain(tname); region *r, *result; if (!terrain) { return 0; } assert(!pnormalize(&x, &y, pl)); r = result = findregion(x, y); if (r != NULL && r->units != NULL) { /* TODO: error message */ result = NULL; } else if (r == NULL) { result = new_region(x, y, pl, 0); } if (result) { terraform_region(result, terrain); if (result->land) { fix_demand(result); } } tolua_pushusertype(L, result, TOLUA_CAST "region"); return 1; } return 0; }
static void prepare_starting_region(region * r) { int n, t; double p; assert(r->land); /* population between 30% and 60% of max */ p = rng_double(); n = (int)(r->terrain->size * (0.3 + p * 0.3)); rsetpeasants(r, n); /* trees: don't squash the peasants, and at least 5% should be forrest */ t = (rtrees(r, 2) + rtrees(r, 1) / 2) * TREESIZE; if (t < r->terrain->size / 20 || t + n > r->terrain->size) { double p2 = 0.05 + rng_double() * (1.0 - p - 0.05); int maxtrees = (int)(r->terrain->size / 1.25 / TREESIZE); /* 1.25 = each young tree will take 1/2 the space of old trees */ int trees = (int)(p2 * maxtrees); rsettrees(r, 2, trees); rsettrees(r, 1, trees / 2); rsettrees(r, 0, trees / 4); } /* horses: between 1% and 2% */ p = rng_double(); rsethorses(r, (int)(r->terrain->size * (0.01 + p * 0.01))); if (!markets_module()) { fix_demand(r); } }
int fix_all_demand(region *rd) { region_list *rl, *rlist = NULL; recurse_regions(rd, &rlist, f_nolux); for (rl = rlist; rl; rl = rl->next) { region *r = rl->data; freset(r, RF_MARK); /* undo recursive marker */ if (!fix_demand(r)) { return -1; } } return 0; }
const item_type *r_luxury(region * r) { struct demand *dmd; if (r->land) { if (!r->land->demands) { fix_demand(r); } for (dmd = r->land->demands; dmd; dmd = dmd->next) { if (dmd->value == 0) return dmd->type->itype; } } return NULL; }
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(); }