static quicklist * get_island(region * root) { quicklist * ql, *result = 0; int qi = 0; fset(root, RF_MARK); ql_push(&result, root); for (ql = result, qi = 0; ql; ql_advance(&ql, &qi, 1)) { int dir; region *r = (region *)ql_get(ql, qi); region * next[MAXDIRECTIONS]; get_neighbours(r, next); for (dir = 0; dir != MAXDIRECTIONS; ++dir) { region *rn = next[dir]; if (rn != NULL && rn->land && !fval(rn, RF_MARK)) { fset(rn, RF_MARK); ql_push(&result, rn); } } } for (ql = result, qi = 0; ql; ql_advance(&ql, &qi, 1)) { region *r = (region *)ql_get(ql, qi); freset(r, RF_MARK); } return result; }
static void test_mapreduce(CuTest * tc) { int buffer; int result = 0; struct quicklist *ql = 0; ql_push(&ql, (void *)data); ql_push(&ql, (void *)data); ql_map_reduce(ql, my_map, my_reduce, &buffer, &result); CuAssertIntEquals(tc, 152, result); ql_free(ql); }
static void test_foreach(CuTest *tc) { int total = 0; struct quicklist *ql = 0; ql_push(&ql, (void *)data); ql_push(&ql, (void *)data); ql_foreach(ql, my_callback); CuAssertIntEquals(tc, 152, gtotal); ql_foreachx(ql, my_callbackx, &total); CuAssertIntEquals(tc, 152, total); ql_free(ql); }
static void test_loop(CuTest * tc) { ql_iter iter; quicklist * ql = 0; int result = 0; ql_push(&ql, (void *)data); ql_push(&ql, (void *)(data + 1)); for (iter = qli_init(&ql); qli_more(iter);) { const char * c = (const char *)qli_next(&iter); result += *c; } CuAssertIntEquals(tc, data[0] + data[1], result); }
static void test_push_doesnt_invalidate_iterator(CuTest * tc) { struct quicklist *list = NULL, *ql = NULL; int i, qi = 0; ql_push(&list, (void*)data); ql = list; for (i = 0; i != 42; ++i) { void * n; n = ql_get(ql, qi); CuAssertPtrEquals(tc, (void *)(data + i), n); ql_push(&list, (void *)(data + (i * 2 + 1))); ql_advance(&ql, &qi, 1); ql_push(&list, (void *)(data + (i * 2 + 2))); } ql_free(ql); }
void bt_register(building_type * type) { if (type->init) { type->init(type); } ql_push(&buildingtypes, (void *)type); }
region * create_region(unsigned int uid, int x, int y, const terrain * t) { region * r = 0; region **bucket = 0; while (!uid || r) { uid = (unsigned int)genrand_int31(); r = get_region(uid); }; assert(t); r = (region *)malloc(sizeof(region)); if (r) { memset(r, 0, sizeof(region)); bucket = ®ionhash[uid & RHASHMASK]; r->nexthash_ = *bucket; *bucket = r; r->uid = uid; r->x = x; r->y = y; r->terrain = t; ql_push(®ions, r); } return r; }
struct terrain *create_terrain(const char * name) { terrain * t = (terrain *)calloc(1, sizeof(terrain)); ql_push(&terrains, t); t->name = _strdup(name); return t; }
static struct quicklist * read_factions(sqlite3 * db, int game_id) { int res; quicklist *result = 0; const char * sql = "SELECT f.id, fd.code, fd.name, fd.email FROM faction f" " LEFT OUTER JOIN faction_data fd" " WHERE f.id=fd.faction_id AND f.game_id=? AND" " fd.turn=(SELECT MAX(turn) FROM faction_data fx WHERE fx.faction_id=f.id)" " ORDER BY f.id"; sqlite3_stmt *stmt = 0; sqlite3_prepare_v2(db, sql, -1, &stmt, 0); sqlite3_bind_int(stmt, 1, game_id); res = sqlite3_step(stmt); while (res == SQLITE_ROW) { const char * text; db_faction * dbf = (db_faction*)calloc(1, sizeof(db_faction)); dbf->uid = (int)sqlite3_column_int64(stmt, 0); text = (const char *)sqlite3_column_text(stmt, 1); if (text) dbf->no = atoi36(text); text = (const char *)sqlite3_column_text(stmt, 2); if (text) dbf->name = _strdup(text); text = (const char *)sqlite3_column_text(stmt, 3); if (text) dbf->email = _strdup(text); ql_push(&result, dbf); res = sqlite3_step(stmt); } sqlite3_finalize(stmt); return result; }
static void do_meffect(fighter * af, int typ, int effect, int duration) { battle *b = af->side->battle; meffect *me = (meffect *)malloc(sizeof(struct meffect)); ql_push(&b->meffects, me); me->magician = af; me->typ = typ; me->effect = effect; me->duration = duration; }
ship_type *st_get_or_create(const char * name) { ship_type * st = st_find_i(name); if (!st) { st = (ship_type *)calloc(sizeof(ship_type), 1); st->_name = _strdup(name); st->storm = 1.0; ql_push(&shiptypes, (void *)st); } return st; }
static void test_iter_delete_all(CuTest * tc) { quicklist * ql = 0; ql_iter iter; ql_push(&ql, (void *)data); iter = qli_init(&ql); qli_delete(&iter); CuAssertPtrEquals(tc, 0, ql); }
static void test_insert_delete_gives_null(CuTest * tc) { struct quicklist *ql = NULL; CuAssertIntEquals(tc, 1, ql_empty(ql)); ql_push(&ql, (void *)data); CuAssertIntEquals(tc, 0, ql_empty(ql)); ql_delete(&ql, 0); CuAssertPtrEquals(tc, 0, ql); ql_free(ql); }
static void test_replace(CuTest * tc) { struct quicklist *ql = NULL; void * a; ql_push(&ql, (void *)data); a = ql_replace(ql, 0, (void *)(data + 1)); CuAssertPtrEquals(tc, (void *)data, a); CuAssertPtrEquals(tc, (void *)(data + 1), ql_get(ql, 0)); ql_free(ql); }
static void get_island_info(region * root, int *size_p, int *inhabited_p, int *maxage_p) { int qi, size = 0, maxage = 0, inhabited = 0; quicklist *ql, *island = NULL; ql_push(&island, root); fset(root, RF_MARK); for (ql = island, qi = 0; ql; ql_advance(&ql, &qi, 1)) { int d; region *r = (region *)ql_get(ql, qi); if (r->units) { unit *u; for (u = r->units; u; u = u->next) { if (!fval(u->faction, FFL_NOIDLEOUT) && u->faction->age > maxage) { maxage = u->faction->age; } } ++inhabited; } ++size; for (d = 0; d != MAXDIRECTIONS; ++d) { region *rn = rconnect(r, d); if (rn && !fval(rn, RF_MARK) && rn->land) { ql_push(&island, rn); fset(rn, RF_MARK); } } } for (ql = island, qi = 0; ql; ql_advance(&ql, &qi, 1)) { region *r = (region *)ql_get(ql, qi); freset(r, RF_MARK); } ql_free(island); if (size_p) *size_p = size; if (inhabited_p) *inhabited_p = inhabited; if (maxage_p) *maxage_p = maxage; }
static void test_delete_edgecases(CuTest * tc) { struct quicklist *ql = NULL; ql_delete(&ql, 0); CuAssertPtrEquals(tc, 0, ql); ql_push(&ql, (void *)data); ql_delete(&ql, -1); ql_delete(&ql, 32); CuAssertIntEquals(tc, 1, ql_length(ql)); ql_free(ql); }
static void test_more(CuTest * tc) { ql_iter iter; quicklist * ql = 0; ql_push(&ql, (void *)data); iter = qli_init(&ql); CuAssertTrue(tc, qli_more(iter)); ql_delete(&iter.l, iter.i); CuAssertTrue(tc, !qli_more(iter)); }
static void test_push(CuTest * tc) { struct quicklist *result, *ql = NULL; CuAssertIntEquals(tc, 0, ql_length(ql)); result = ql_push(&ql, (void *)data); CuAssertPtrEquals(tc, result, ql); CuAssertIntEquals(tc, 1, ql_length(ql)); CuAssertPtrEquals(tc, (void *)data, ql_get(ql, 0)); ql_delete(&ql, 0); CuAssertPtrEquals(tc, 0, ql); }
static void test_find(CuTest *tc) { struct quicklist *ql = 0; struct quicklist *il; int i; ql_push(&ql, (void *)data); ql_push(&ql, (void *)(data + 1)); ql_push(&ql, (void *)data); il = ql; i = 0; CuAssertIntEquals(tc, ql_true, ql_find(&il, &i, (void *)data, 0)); CuAssertIntEquals(tc, 0, i); ql_advance(&il, &i, 1); CuAssertIntEquals(tc, ql_true, ql_find(&il, &i, (void *)data, 0)); CuAssertIntEquals(tc, 2, i); ql_advance(&il, &i, 1); CuAssertIntEquals(tc, ql_false, ql_find(&il, &i, (void *)data, 0)); ql_free(ql); }
static void test_push_returns_end(CuTest * tc) { struct quicklist *result, *ql = NULL; int i = 0, qi = 0; do { result = ql_push(&ql, 0); i++; } while (result == ql); ql_advance(&ql, &qi, i - 1); CuAssertPtrEquals(tc, result, ql); ql_free(ql); }
static void test_find_cb(CuTest *tc) { int a = 42; int b = 23; int c = 42; struct quicklist *ql = 0; struct quicklist *il; int i; ql_push(&ql, (void *)&a); ql_push(&ql, (void *)&b); ql_push(&ql, (void *)&c); il = ql; i = 0; CuAssertIntEquals(tc, ql_true, ql_find(&il, &i, (void *)&a, cb_match_int)); CuAssertIntEquals(tc, 0, i); ql_advance(&il, &i, 1); CuAssertIntEquals(tc, ql_true, ql_find(&il, &i, (void *)&a, cb_match_int)); CuAssertIntEquals(tc, 2, i); ql_advance(&il, &i, 1); CuAssertIntEquals(tc, ql_false, ql_find(&il, &i, (void *)&a, cb_match_int)); ql_free(ql); }
void spellbook_add(spellbook *sb, struct spell * sp, int level) { spellbook_entry * sbe; assert(sb && sp && level>0); #ifndef NDEBUG if (spellbook_get(sb, sp)) { log_error("duplicate spell in spellbook '%s': '%s'\n", sb->name, sp->sname); } #endif sbe = (spellbook_entry *)malloc(sizeof(spellbook_entry)); sbe->sp = sp; sbe->level = level; ql_push(&sb->spells, sbe); }
static void test_delete_rand(CuTest * tc) { struct quicklist *ql = NULL; int i; for (i = 0; i != 32; ++i) { ql_push(&ql, (void *)(data + i)); } CuAssertIntEquals(tc, 32, ql_length(ql)); ql_delete(&ql, 0); CuAssertPtrEquals(tc, (void *)(data + 1), ql_get(ql, 0)); CuAssertIntEquals(tc, 31, ql_length(ql)); ql_delete(&ql, 30); CuAssertIntEquals(tc, 30, ql_length(ql)); ql_free(ql); }
static void test_insert_many(CuTest * tc) { struct quicklist *ql = NULL; int i; for (i = 0; i != 32; ++i) { ql_push(&ql, (void *)(data + i)); } for (i = 0; i != 32; ++i) { CuAssertIntEquals(tc, 32 - i, ql_length(ql)); CuAssertPtrEquals(tc, (void *)(data + i), ql_get(ql, 0)); CuAssertPtrEquals(tc, (void *)(data + 31), ql_get(ql, ql_length(ql) - 1)); ql_delete(&ql, 0); } CuAssertPtrEquals(tc, 0, ql); ql_free(ql); }
struct quicklist *regions_in_range(struct region *start, int maxdist, bool(*allowed) (const struct region *, const struct region *)) { quicklist * rlist = NULL; node *root = new_node(start, 0, NULL); node **end = &root->next; node *n = root; while (n != NULL) { region *r = n->r; int depth = n->distance + 1; int d; if (n->distance >= maxdist) break; for (d = 0; d != MAXDIRECTIONS; ++d) { region *rn = rconnect(r, d); if (rn == NULL) continue; if (fval(rn, RF_MARK)) continue; /* already been there */ if (allowed && !allowed(r, rn)) continue; /* can't go there */ /* add the region to the list of available ones. */ ql_push(&rlist, rn); /* make sure we don't go here again, and put the region into the set for further BFS'ing */ fset(rn, RF_MARK); *end = new_node(rn, depth, n); end = &(*end)->next; } n = n->next; } free_nodes(root); return rlist; }
static void select_wormhole_regions(quicklist **rlistp, int *countp) { quicklist *rlist = 0; region *r = regions; int count = 0; while (r != NULL) { int next = rng_int() % (2 * WORMHOLE_CHANCE); while (r != NULL && (next != 0 || !good_region(r))) { if (good_region(r)) { --next; } r = r->next; } if (r == NULL) break; ql_push(&rlist, r); ++count; r = r->next; } *countp = count; *rlistp = rlist; }
void age_borders(void) { quicklist *deleted = NULL, *ql; int i; for (i = 0; i != BORDER_MAXHASH; ++i) { connection *bhash = borders[i]; for (; bhash; bhash = bhash->nexthash) { connection *b = bhash; for (; b; b = b->next) { if (b->type->age) { if (b->type->age(b) == AT_AGE_REMOVE) { ql_push(&deleted, b); } } } } } for (ql = deleted, i = 0; ql; ql_advance(&ql, &i, 1)) { connection *b = (connection *)ql_get(ql, i); erase_border(b); } ql_free(deleted); }
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); } }