faction *gm_addquest(const char *email, const char *name, int radius, unsigned int flags) { plane *pl; watcher *w = calloc(sizeof(watcher), 1); region *center; bool invalid = false; int minx, miny, maxx, maxy, cx, cy; int x; faction *f; /* GM playfield */ do { minx = ((rng_int() % (2 * EXTENSION)) - EXTENSION); miny = ((rng_int() % (2 * EXTENSION)) - EXTENSION); for (x = 0; !invalid && x <= radius * 2; ++x) { int y; for (y = 0; !invalid && y <= radius * 2; ++y) { region *r = findregion(minx + x, miny + y); if (r) invalid = true; } } } while (invalid); maxx = minx + 2 * radius; cx = minx + radius; maxy = miny + 2 * radius; cy = miny + radius; pl = create_new_plane(rng_int(), name, minx, maxx, miny, maxy, flags); center = new_region(cx, cy, pl, 0); for (x = 0; x <= 2 * radius; ++x) { int y; for (y = 0; y <= 2 * radius; ++y) { region *r = findregion(minx + x, miny + y); if (!r) { r = new_region(minx + x, miny + y, pl, 0); } freset(r, RF_ENCOUNTER); if (distance(r, center) == radius) { terraform_region(r, newterrain(T_FIREWALL)); } else if (r == center) { terraform_region(r, newterrain(T_PLAIN)); } else { terraform_region(r, newterrain(T_OCEAN)); } } } /* watcher: */ f = gm_addfaction(email, pl, center); w->faction = f; w->mode = see_unit; w->next = pl->watchers; pl->watchers = w; return f; }
static void frame_regions(int age, const terrain_type * terrain) { plane *hplane = get_homeplane(); region *r = regions; for (r = regions; r; r = r->next) { plane *pl = rplane(r); direction_t d; if (r->age < age) continue; if (pl != hplane) continue; /* only do this on the main world */ if (r->terrain == terrain) continue; for (d = 0; d != MAXDIRECTIONS; ++d) { region *rn = rconnect(r, d); if (rn == NULL) { int x = r->x + delta_x[d]; int y = r->y + delta_y[d]; pnormalize(&x, &y, pl); rn = new_region(x, y, pl, 0); terraform_region(rn, terrain); rn->age = r->age; } } } }
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; }
/** ** GM: TERRAFORM <x> <y> <terrain> ** requires: permission-key "gmterf" **/ static void gm_terraform(const void *tnext, struct unit *u, struct order *ord) { const struct plane *p = rplane(u->region); int x = rel_to_abs(p, u->faction, getint(), 0); int y = rel_to_abs(p, u->faction, getint(), 1); const char *c = getstrtoken(); variant token; void **tokens = get_translations(u->faction->locale, UT_TERRAINS); region *r; pnormalize(&x, &y, p); r = findregion(x, y); if (r == NULL || p != rplane(r)) { mistake(u, ord, "region is in another plane."); return; } else { /* checking permissions */ attrib *permissions = a_find(u->faction->attribs, &at_permissions); if (!permissions || !has_permission(permissions, atoi36("gmterf"))) return; } if (findtoken(*tokens, c, &token) != E_TOK_NOMATCH) { const terrain_type *terrain = (const terrain_type *)token.v; terraform_region(r, terrain); } }
static void melt_iceberg(region * r) { attrib *a; unit *u; for (u = r->units; u; u = u->next) freset(u->faction, FFL_SELECT); for (u = r->units; u; u = u->next) if (!fval(u->faction, FFL_SELECT)) { fset(u->faction, FFL_SELECT); ADDMSG(&u->faction->msgs, msg_message("iceberg_melt", "region", r)); } /* driftrichtung löschen */ a = a_find(r->attribs, &at_iceberg); if (a) a_remove(&r->attribs, a); /* Gebäude löschen */ while (r->buildings) { remove_building(&r->buildings, r->buildings); } /* in Ozean wandeln */ terraform_region(r, newterrain(T_OCEAN)); /* Einheiten, die nicht schwimmen können oder in Schiffen sind, * ertrinken */ drown(r); }
plane *gm_addplane(int radius, unsigned int flags, const char *name) { region *center; plane *pl; bool invalid = false; int minx, miny, maxx, maxy, cx, cy; int x; /* GM playfield */ do { minx = (rng_int() % (2 * EXTENSION)) - EXTENSION; miny = (rng_int() % (2 * EXTENSION)) - EXTENSION; for (x = 0; !invalid && x <= radius * 2; ++x) { int y; for (y = 0; !invalid && y <= radius * 2; ++y) { region *r = findregion(minx + x, miny + y); if (r) invalid = true; } } } while (invalid); maxx = minx + 2 * radius; cx = minx + radius; maxy = miny + 2 * radius; cy = miny + radius; pl = create_new_plane(rng_int(), name, minx, maxx, miny, maxy, flags); center = new_region(cx, cy, pl, 0); for (x = 0; x <= 2 * radius; ++x) { int y; for (y = 0; y <= 2 * radius; ++y) { region *r = findregion(minx + x, miny + y); if (!r) r = new_region(minx + x, miny + y, pl, 0); freset(r, RF_ENCOUNTER); if (distance(r, center) == radius) { terraform_region(r, newterrain(T_FIREWALL)); } else if (r == center) { terraform_region(r, newterrain(T_PLAIN)); } else { terraform_region(r, newterrain(T_OCEAN)); } } } return pl; }
struct region *test_create_region(int x, int y, const terrain_type *terrain) { region *r = new_region(x, y, NULL, 0); terraform_region(r, terrain); rsettrees(r, 0, 0); rsettrees(r, 1, 0); rsettrees(r, 2, 0); rsethorses(r, 0); rsetpeasants(r, terrain->size); return r; }
static int tolua_region_set_terrain(lua_State * L) { region *r = (region *)tolua_tousertype(L, 1, 0); const char *tname = tolua_tostring(L, 2, 0); if (tname) { const terrain_type *terrain = get_terrain(tname); if (terrain) { terraform_region(r, terrain); } } return 0; }
static void oceans_around(region * r, region * rn[]) { int n; for (n = 0; n != MAXDIRECTIONS; ++n) { region *rx = rn[n]; if (rx == NULL) { plane *pl = rplane(r); int x = r->x + delta_x[n]; int y = r->y + delta_y[n]; pnormalize(&x, &y, pl); rx = new_region(x, y, pl, 0); terraform_region(rx, newterrain(T_OCEAN)); rn[n] = rx; } } }
static void starting_region(newfaction ** players, region * r, region * rn[]) { int n; oceans_around(r, rn); freset(r, RF_MARK); for (n = 0; n != MAXDIRECTIONS; ++n) { freset(rn[n], RF_MARK); } terraform_region(r, newterrain(T_PLAIN)); prepare_starting_region(r); if (players && *players) { newfaction *nf = *players; const struct race *rc = nf->race ? nf->race : races; const struct locale *lang = nf->lang ? nf->lang : default_locale; const char * passwd = nf->password ? nf->password : itoa36(rng_int()); addplayer(r, addfaction(nf->email, passwd, rc, lang, 0)); *players = nf->next; free_newfaction(nf); } }
int json_import(struct stream * out) { cJSON *json, *child; char buffer[1024], *data = 0; size_t sz = 0; assert(out && out->api); while (!out->api->readln(out->handle, buffer, sizeof(buffer))) { size_t len = strlen(buffer); data = (char *)realloc(data, sz + len + 1); memcpy(data + sz, buffer, len); sz += len; data[sz] = 0; } json = cJSON_Parse(data); child = cJSON_GetObjectItem(json, "regions"); if (child && child->type == cJSON_Object) { cJSON *j; for (j = child->child; j; j = j->next) { cJSON *attr; unsigned int id = 0; int x = 0, y = 0; region * r; id = (unsigned int)atol(j->string); if ((attr = cJSON_GetObjectItem(j, "x")) != 0 && attr->type == cJSON_Number) x = attr->valueint; if ((attr = cJSON_GetObjectItem(j, "y")) != 0 && attr->type == cJSON_Number) y = attr->valueint; r = new_region(x, y, 0, id); if ((attr = cJSON_GetObjectItem(j, "type")) != 0 && attr->type == cJSON_String) { const terrain_type *terrain = get_terrain(attr->valuestring); terraform_region(r, terrain); } if ((attr = cJSON_GetObjectItem(j, "name")) != 0 && attr->type == cJSON_String) { region_setname(r, attr->valuestring); } } } cJSON_Delete(json); 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(); }
int random_neighbours(region * r, region_list ** rlist, const terrain_type * (*terraformer) (direction_t)) { int nsize = 0; direction_t dir; for (dir = 0; dir != MAXDIRECTIONS; ++dir) { region *rn = rconnect(r, dir); if (rn == NULL) { const terrain_type *terrain = terraformer(dir); plane *pl = rplane(r); int x = r->x + delta_x[dir]; int y = r->y + delta_y[dir]; pnormalize(&x, &y, pl); rn = new_region(x, y, pl, 0); terraform_region(rn, terrain); regionqueue_push(rlist, rn); if (rn->land) { ++nsize; } } } return nsize; }
/* E3A island generation */ int build_island_e3(newfaction ** players, int x, int y, int numfactions, int minsize) { #define MIN_QUALITY 1000 int nfactions = 0; region_list *rlist = NULL; region_list *island = NULL; plane *pl = findplane(x, y); region *r = findregion(x, y); int nsize = 1; int q, maxq = INT_MIN, minq = INT_MAX; if (!r) r = new_region(x, y, pl, 0); assert(!r->units); do { terraform_region(r, random_terrain_e3(NODIRECTION)); } while (!r->land); while (r) { fset(r, RF_MARK); if (r->land) { if (nsize < minsize) { nsize += random_neighbours(r, &rlist, &random_terrain_e3); } else { nsize += random_neighbours(r, &rlist, &get_ocean); } } regionqueue_push(&island, r); r = regionqueue_pop(&rlist); } smooth_island(island); if (nsize > minsize / 2) { for (rlist = island; rlist; rlist = rlist->next) { r = rlist->data; if (r->land && fval(r, RF_MARK)) { region *rn[MAXDIRECTIONS]; get_neighbours(r, rn); q = region_quality(r, rn); if (q >= MIN_QUALITY && nfactions < numfactions && *players) { starting_region(players, r, rn); minq = _min(minq, q); maxq = _max(maxq, q); ++nfactions; } } } for (rlist = island; rlist && nfactions < numfactions; rlist = rlist->next) { r = rlist->data; if (!r->land && fval(r, RF_MARK)) { region *rn[MAXDIRECTIONS]; get_neighbours(r, rn); q = region_quality(r, rn); if (q >= MIN_QUALITY * 4 / 3 && nfactions < numfactions && *players) { starting_region(players, r, rn); minq = _min(minq, q); maxq = _max(maxq, q); ++nfactions; } } } } for (rlist = island; rlist; rlist = rlist->next) { r = rlist->data; if (r->units) { region *rn[MAXDIRECTIONS]; get_neighbours(r, rn); q = region_quality(r, rn); if (q - minq > (maxq - minq) * 2 / 3) { terraform_region(r, newterrain(T_HIGHLAND)); prepare_starting_region(r); } r->land->money = 50000; /* 2% = 1000 silver */ } else if (r->land) { r->land->money *= 4; } } return nfactions; }
void chaos(region * r) { if (rng_int() % 100 < 8) { switch (rng_int() % 3) { case 0: /* Untote */ if (!fval(r->terrain, SEA_REGION)) { unit *u = random_unit(r); if (u && playerrace(u_race(u))) { ADDMSG(&u->faction->msgs, msg_message("chaos_disease", "unit", u)); u_setfaction(u, get_monsters()); u_setrace(u, get_race(RC_GHOUL)); } } break; case 1: /* Drachen */ if (random_unit(r)) { int mfac = 0; unit *u; switch (rng_int() % 3) { case 0: mfac = 100; u = createunit(r, get_monsters(), rng_int() % 8 + 1, get_race(RC_FIREDRAGON)); break; case 1: mfac = 500; u = createunit(r, get_monsters(), rng_int() % 4 + 1, get_race(RC_DRAGON)); break; default: mfac = 1000; u = createunit(r, get_monsters(), rng_int() % 2 + 1, get_race(RC_WYRM)); break; } if (mfac) set_money(u, u->number * (rng_int() % mfac)); fset(u, UFL_ISNEW | UFL_MOVED); } case 2: /* Terrainveränderung */ if (!fval(r->terrain, FORBIDDEN_REGION)) { if (!fval(r->terrain, SEA_REGION)) { direction_t dir; for (dir = 0; dir != MAXDIRECTIONS; ++dir) { region *rn = rconnect(r, dir); if (rn && fval(rn->terrain, SEA_REGION)) break; } if (dir != MAXDIRECTIONS) { ship *sh = r->ships; unit **up; while (sh) { ship *nsh = sh->next; float dmg = get_param_flt(global.parameters, "rules.ship.damage.atlantis", 0.50); damage_ship(sh, dmg); if (sh->damage >= sh->size * DAMAGE_SCALE) { remove_ship(&sh->region->ships, sh); } sh = nsh; } for (up = &r->units; *up;) { unit *u = *up; if (u_race(u) != get_race(RC_SPELL) && u->ship == 0 && !canfly(u)) { ADDMSG(&u->faction->msgs, msg_message("tidalwave_kill", "region unit", r, u)); remove_unit(up, u); } if (*up == u) up = &u->next; } ADDMSG(&r->msgs, msg_message("tidalwave", "region", r)); while (r->buildings) { remove_building(&r->buildings, r->buildings); } terraform_region(r, newterrain(T_OCEAN)); } } else { direction_t dir; for (dir = 0; dir != MAXDIRECTIONS; ++dir) { region *rn = rconnect(r, dir); if (rn && fval(rn->terrain, SEA_REGION)) break; } if (dir != MAXDIRECTIONS) { terraform_region(r, chaosterrain()); } } } } } }
void create_museum(void) { #if 0 /* TODO: move this to Lua. It should be possible. */ unsigned int museum_id = hashstring("museum"); plane *museum = getplanebyid(museum_id); region *r; building *b; const terrain_type *terrain_hall = get_terrain("hall1"); const terrain_type *terrain_corridor = get_terrain("corridor1"); assert(terrain_corridor && terrain_hall); if (!museum) { museum = create_new_plane(museum_id, "Museum", 9500, 9550, 9500, 9550, PFL_MUSEUM); } if (findregion(9525, 9525) == NULL) { /* Eingangshalle */ r = new_region(9525, 9525, 0); terraform_region(r, terrain_hall); r->planep = museum; rsetname(r, "Eingangshalle"); rsethorses(r, 0); rsetmoney(r, 0); rsetpeasants(r, 0); set_string(&r->display, "Die Eingangshalle des Großen Museum der 1. Welt ist bereits jetzt ein beeindruckender Anblick. Obwohl das Museum noch nicht eröffnet ist, vermittelt sie bereits einen Flair exotischer Welten. In den Boden ist ein großer Kompass eingelassen, der den Besuchern bei Orientierung helfen soll."); } r = findregion(9526, 9525); if (!r) { /* Lounge */ r = new_region(9526, 9525, 0); terraform_region(r, terrain_hall); r->planep = museum; rsetname(r, "Lounge"); rsethorses(r, 0); rsetmoney(r, 0); rsetpeasants(r, 0); set_string(&r->display, "Die Lounge des großen Museums ist ein Platz, in dem sich die Besucher treffen, um die Eindrücke, die sie gewonnen haben, zu verarbeiten. Gemütliche Sitzgruppen laden zum Verweilen ein."); } r = findregion(9526, 9525); if (!r->buildings) { const building_type *bt_generic = bt_find("generic"); b = new_building(bt_generic, r, NULL); set_string(&b->name, "Séparée im dämonischen Stil"); set_string(&b->display, "Diese ganz im dämonischen Stil gehaltene Sitzgruppe ist ganz in dunklen Schwarztönen gehalten. Muster fremdartiger Runen bedecken das merkwürdig geformte Mobiliar, das unangenehm lebendig wirkt."); b = new_building(bt_generic, r, NULL); set_string(&b->name, "Séparée im elfischen Stil"); set_string(&b->display, "Ganz in Grün- und Brauntönen gehalten wirkt die Sitzgruppe fast lebendig. Bei näherer Betrachtung erschließt sich dem Betrachter, daß sie tatsächlich aus lebenden Pflanzen erstellt ist. So ist der Tisch aus einem eizigen Baum gewachsen, und die Polster bestehen aus weichen Grassoden. Ein wunderschön gemusterter Webteppich mit tausenden naturgetreu eingestickter Blumensarten bedeckt den Boden."); b = new_building(bt_generic, r, NULL); set_string(&b->name, "Séparée im halblingschen Stil"); set_string(&b->display, "Dieses rustikale Mobiliar ist aus einem einzigen, gewaltigen Baum hergestellt worden. Den Stamm haben fleißige Halblinge der Länge nach gevierteilt und aus den vier langen Viertelstämmen die Sitzbänke geschnitzt, während der verbleibende Stumpf als Tisch dient. Schon von weitem steigen dem Besucher die Gerüche der Köstlichkeiten entgegen, die auf dem Tisch stapeln."); b = new_building(bt_generic, r, NULL); set_string(&b->name, "Séparée im orkischen Stil"); set_string(&b->display, "Grobgeschreinerte, elfenhautbespannte Stühle und ein Tisch aus Knochen, über deren Herkunft man sich lieber keine Gedanken macht, bilden die Sitzgruppe im orkischen Stil. Überall haben Orks ihre Namen, und anderes wenig zitierenswertes in das Holz und Gebein geritzt."); b = new_building(bt_generic, r, NULL); set_string(&b->name, "Séparée im Meermenschenstil"); set_string(&b->display, "Ganz in Blau- und Grüntönen gehalten, mit Algen und Muscheln verziert wirken die aus altem Meerholz geschnitzten Stühle immer ein wenig feucht. Seltsammerweise hat der schwere aus alten Planken gezimmerte Tisch einen Mast mit kompletten Segel in der Mitte."); b = new_building(bt_generic, r, NULL); set_string(&b->name, "Séparée im Katzenstil"); set_string(&b->display, "Die Wände dieses Séparée sind aus dunklem Holz. Was aus der Ferne wie ein chaotisch durchbrochenes Flechtwerk wirkt, entpuppt sich bei näherer Betrachtung als eine bis in winzige Details gestaltete dschungelartige Landschaft, in die eine Vielzahl von kleinen Bildergeschichten eingewoben sind. Wie es scheint hat sich der Künstler Mühe gegeben wirklich jedes Katzenvolk Eresseas zu porträtieren. Das schummrige Innere wird von einem Kamin dominiert, vor dem einige Sessel und weiche Kissen zu einem gemütlichen Nickerchen einladen. Feiner Anduner Sisal bezieht die Lehnen der Sessel und verlockt dazu, seine Krallen hinein zu versenken. Auf einem kleinen Ecktisch steht ein großer Korb mit roten Wollknäulen und grauen und braunen Spielmäusen."); } else { for (b = r->buildings; b; b = b->next) { b->size = b->type->maxsize; } } r = findregion(9524, 9526); if (!r) { r = new_region(9524, 9526, 0); terraform_region(r, terrain_corridor); r->planep = museum; rsetname(r, "Nördliche Promenade"); rsethorses(r, 0); rsetmoney(r, 0); rsetpeasants(r, 0); set_string(&r->display, "Die Nördliche Promenade führt direkt in den naturgeschichtlichen Teil des Museums."); } r = findregion(9525, 9524); if (!r) { r = new_region(9525, 9524, 0); terraform_region(r, terrain_corridor); r->planep = museum; rsetname(r, "Südliche Promenade"); rsethorses(r, 0); rsetmoney(r, 0); rsetpeasants(r, 0); set_string(&r->display, "Die Südliche Promenade führt den Besucher in den kulturgeschichtlichen Teil des Museums."); } #endif }
/** create new island with up to nsize players * returns the number of players placed on the new island. */ int autoseed(newfaction ** players, int nsize, int max_agediff) { region *r = NULL; region_list *rlist = NULL; int rsize = 0, tsize = 0; int isize = REGIONS_PER_FACTION; /* target size for the island */ int psize = 0; /* players on this island */ const terrain_type *volcano_terrain = get_terrain("volcano"); static int nterrains = -1; static const terrain_type **terrainarr = 0; static int *distribution; assert(players); if (nterrains < 0) { int n = 0; const terrain_type *terrain = terrains(); for (nterrains = 0; terrain; terrain = terrain->next) { if (terrain->distribution) { ++nterrains; } } terrainarr = malloc(sizeof(terrain_type *) * nterrains); distribution = malloc(sizeof(int) * nterrains); for (terrain = terrains(); terrain; terrain = terrain->next) { if (terrain->distribution) { terrainarr[n] = terrain; distribution[n++] = terrain->distribution; } } } frame_regions(16, newterrain(T_FIREWALL)); if (listlen(*players) < MINFACTIONS) return 0; if (max_agediff > 0) { region *rmin = NULL; plane *hplane = get_homeplane(); /* find a spot that's adjacent to the previous island, but virgin. * like the last land virgin ocean region adjacent to land. */ for (r = regions; r; r = r->next) { struct plane *pl = rplane(r); if (r->age <= max_agediff && r->terrain == newterrain(T_OCEAN) && pl == hplane && virgin_region(r)) { direction_t d; for (d = 0; d != MAXDIRECTIONS; ++d) { region *rn = rconnect(r, d); if (rn && rn->land && rn->age <= max_agediff && virgin_region(rn)) { /* only expand islands that aren't single-islands and not too big already */ int size, inhabitants, maxage; get_island_info(rn, &size, &inhabitants, &maxage); if (maxage <= max_agediff && size >= 2 && size < MAXISLANDSIZE) { rmin = rn; break; } } } } } if (rmin != NULL) { faction *f; quicklist *ql, *rlist = get_island(rmin); int qi; for (ql = rlist, qi = 0; ql; ql_advance(&ql, &qi, 1)) { region *r = (region *)ql_get(ql, qi); unit *u; for (u = r->units; u; u = u->next) { f = u->faction; if (!fval(f, FFL_MARK)) { ++psize; fset(f, FFL_MARK); } } } ql_free(rlist); if (psize > 0) { for (f = factions; f; f = f->next) { freset(f, FFL_MARK); } } if (psize < PLAYERS_PER_ISLAND) { r = rmin; } } } if (r == NULL) { region *rmin = NULL; direction_t dmin = MAXDIRECTIONS; plane *hplane = get_homeplane(); /* find an empty spot. * rmin = the youngest ocean region that has a missing neighbour * dmin = direction in which it's empty */ for (r = regions; r; r = r->next) { struct plane *pl = rplane(r); if (r->terrain == newterrain(T_OCEAN) && pl == hplane && (rmin == NULL || r->age <= max_agediff)) { direction_t d; for (d = 0; d != MAXDIRECTIONS; ++d) { region *rn = rconnect(r, d); if (rn == NULL) break; } if (d != MAXDIRECTIONS) { rmin = r; dmin = d; } } } /* create a new region where we found the empty spot, and make it the first * in our island. island regions are kept in rlist, so only new regions can * get populated, and old regions are not overwritten */ if (rmin != NULL) { plane *pl = rplane(rmin); int x = rmin->x + delta_x[dmin]; int y = rmin->y + delta_y[dmin]; pnormalize(&x, &y, pl); assert(virgin_region(rconnect(rmin, dmin))); r = new_region(x, y, pl, 0); terraform_region(r, newterrain(T_OCEAN)); } } if (r != NULL) { add_regionlist(&rlist, r); fset(r, RF_MARK); rsize = 1; } while (rsize && (nsize || isize >= REGIONS_PER_FACTION)) { int i = rng_int() % rsize; region_list **rnext = &rlist; region_list *rfind; direction_t d; while (i--) rnext = &(*rnext)->next; rfind = *rnext; r = rfind->data; freset(r, RF_MARK); *rnext = rfind->next; free(rfind); --rsize; for (d = 0; d != MAXDIRECTIONS; ++d) { region *rn = rconnect(r, d); if (rn && fval(rn, RF_MARK)) continue; if (rn == NULL) { plane *pl = rplane(r); int x = r->x + delta_x[d]; int y = r->y + delta_y[d]; pnormalize(&x, &y, pl); rn = new_region(x, y, pl, 0); terraform_region(rn, newterrain(T_OCEAN)); } if (virgin_region(rn)) { add_regionlist(&rlist, rn); fset(rn, RF_MARK); ++rsize; } } if (volcano_terrain != NULL && (rng_int() % VOLCANO_CHANCE == 0)) { terraform_region(r, volcano_terrain); } else if (nsize && (rng_int() % isize == 0 || rsize == 0)) { newfaction **nfp, *nextf = *players; faction *f; unit *u; isize += REGIONS_PER_FACTION; terraform_region(r, preferred_terrain(nextf->race)); prepare_starting_region(r); ++tsize; assert(r->land && r->units == 0); u = addplayer(r, addfaction(nextf->email, nextf->password, nextf->race, nextf->lang, nextf->subscription)); f = u->faction; fset(f, FFL_ISNEW); f->alliance = nextf->allies; /* remove duplicate email addresses */ nfp = &nextf->next; while (*nfp) { newfaction *nf = *nfp; if (strcmp(nextf->email, nf->email) == 0) { *nfp = nf->next; free_newfaction(nf); } else nfp = &nf->next; } *players = nextf->next; free_newfaction(nextf); ++psize; --nsize; --isize; if (psize >= PLAYERS_PER_ISLAND) break; } else { terraform_region(r, random_terrain(terrainarr, distribution, nterrains)); --isize; } } if (nsize != 0) { log_error( ("Could not place all factions on the same island as requested\n")); } if (rlist) { #define MINOCEANDIST 3 #define MAXOCEANDIST 6 #define MAXFILLDIST 10 #define SPECIALCHANCE 80 region_list **rbegin = &rlist; int special = 1; int oceandist = MINOCEANDIST + (rng_int() % (MAXOCEANDIST - MINOCEANDIST)); while (oceandist--) { region_list **rend = rbegin; while (*rend) rend = &(*rend)->next; while (rbegin != rend) { direction_t d; region *r = (*rbegin)->data; rbegin = &(*rbegin)->next; for (d = 0; d != MAXDIRECTIONS; ++d) { region *rn = rconnect(r, d); if (rn == NULL) { const struct terrain_type *terrain = newterrain(T_OCEAN); plane *pl = rplane(r); int x = r->x + delta_x[d]; int y = r->y + delta_y[d]; pnormalize(&x, &y, pl); rn = new_region(x, y, pl, 0); if (rng_int() % SPECIALCHANCE < special) { terrain = random_terrain(terrainarr, distribution, nterrains); special = SPECIALCHANCE / 3; /* 33% chance auf noch eines */ } else { special = 1; } terraform_region(rn, terrain); /* the new region has an extra 20% chance to have mallorn */ if (rng_int() % 100 < 20) fset(r, RF_MALLORN); add_regionlist(rend, rn); } } } } while (*rbegin) { region *r = (*rbegin)->data; plane *pl = rplane(r); direction_t d; rbegin = &(*rbegin)->next; for (d = 0; d != MAXDIRECTIONS; ++d) if (rconnect(r, d) == NULL) { int i; for (i = 1; i != MAXFILLDIST; ++i) { int x = r->x + delta_x[d] * i; int y = r->y + delta_y[d] * i; pnormalize(&x, &y, pl); if (findregion(x, y)) { break; } } if (i != MAXFILLDIST) { while (--i) { region *rn; int x = r->x + delta_x[d] * i; int y = r->y + delta_y[d] * i; pnormalize(&x, &y, pl); rn = new_region(x, y, pl, 0); terraform_region(rn, newterrain(T_OCEAN)); } } } } while (rlist) { region_list *self = rlist; rlist = rlist->next; freset(self->data, RF_MARK); free(self); } } return tsize; }