static void test_set_insert_ex(CuTest * tc) { const char * strings[] = { "aaa", "bbb", "ccc", "ddd" }; struct quicklist *ql = NULL; int qi; /* insert a string: */ CuAssertIntEquals(tc, ql_true, ql_set_insert_ex(&ql, (void *)strings[0], cmp_str)); CuAssertIntEquals(tc, 1, ql_length(ql)); /* insert a second string: */ CuAssertIntEquals(tc, ql_true, ql_set_insert_ex(&ql, (void *)strings[1], cmp_str)); CuAssertIntEquals(tc, 2, ql_length(ql)); /* same string again, no change: */ CuAssertIntEquals(tc, ql_false, ql_set_insert_ex(&ql, (void *)strings[1], cmp_str)); CuAssertIntEquals(tc, 2, ql_length(ql)); /* a third string */ CuAssertIntEquals(tc, ql_true, ql_set_insert_ex(&ql, (void *)strings[2], cmp_str)); CuAssertIntEquals(tc, 3, ql_length(ql)); /* check that they are alphabetically ordered: */ for (qi = 0; qi != 3; ++qi) { CuAssertPtrEquals(tc, (void *)strings[qi], ql_get(ql, qi)); } CuAssertIntEquals(tc, 1, ql_set_find_ex(&ql, &qi, (void *)strings[2], cmp_str)); CuAssertIntEquals(tc, 2, qi); ql_free(ql); }
static attrib *set_new_dragon_target(unit * u, region * r, int range) { int max_affinity = 0; region *max_region = NULL; quicklist *ql, *rlist = regions_in_range(r, range, allowed_dragon); int qi; for (qi = 0, ql = rlist; ql; ql_advance(&ql, &qi, 1)) { region *r2 = (region *)ql_get(ql, qi); int affinity = dragon_affinity_value(r2, u); if (affinity > max_affinity) { max_affinity = affinity; max_region = r2; } } ql_free(rlist); if (max_region && max_region != r) { attrib *a = a_find(u->attribs, &at_targetregion); if (!a) { a = a_add(&u->attribs, make_targetregion(max_region)); } else { a->data.v = max_region; } return a; } return NULL; }
static void test_set_remove(CuTest * tc) { struct quicklist *ql = NULL, *q2; int a; ql_set_insert(&ql, (void *)data); ql_set_insert(&ql, (void *)(data + 1)); ql_set_insert(&ql, (void *)(data + 2)); q2 = ql; a = ql_set_remove(&ql, (void *)(data + 1)); CuAssertIntEquals(tc, ql_true, a); CuAssertPtrEquals(tc, q2, ql); CuAssertPtrEquals(tc, (void *)data, ql_get(ql, 0)); CuAssertPtrEquals(tc, (void *)(data + 2), ql_get(ql, 1)); CuAssertIntEquals(tc, 2, ql_length(ql)); a = ql_set_remove(&ql, (void *)(data + 1)); CuAssertIntEquals(tc, ql_false, a); CuAssertPtrEquals(tc, q2, ql); a = ql_set_remove(&ql, (void *)data); CuAssertIntEquals(tc, ql_true, a); a = ql_set_remove(&ql, (void *)(data + 2)); CuAssertIntEquals(tc, ql_true, a); CuAssertPtrEquals(tc, 0, ql); ql_free(ql); }
static void test_set_find(CuTest * tc) { struct quicklist *ql = 0, *q2; int a, qi; q2 = ql; a = ql_set_find(&q2, 0, (void *)data); CuAssertIntEquals(tc, ql_false, a); CuAssertPtrEquals(tc, ql, q2); for (a = 0; a != 32; ++a) { ql_set_insert(&ql, (void *)(data + a)); } q2 = ql; a = ql_set_find(&q2, 0, (void *)(data + 31)); CuAssertIntEquals(tc, ql_true, a); CuAssertPtrEquals(tc, ql, q2); q2 = ql; a = ql_set_find(&ql, &qi, (void *)data); CuAssertIntEquals(tc, ql_true, a); CuAssertIntEquals(tc, 0, qi); CuAssertPtrEquals(tc, ql, q2); q2 = ql; a = ql_set_find(&ql, &qi, (void *)(data + 31)); CuAssertIntEquals(tc, ql_true, a); CuAssertPtrEquals(tc, (void *)(data + 31), ql_get(ql, qi)); CuAssertTrue(tc, ql != q2); ql_free(ql); }
static void test_set_insert(CuTest * tc) { struct quicklist *ql = NULL; int a, qi; a = ql_set_insert(&ql, (void *)data); CuAssertIntEquals(tc, 1, ql_length(ql)); CuAssertIntEquals(tc, ql_true, a); a = ql_set_insert(&ql, (void *)(data + 1)); CuAssertIntEquals(tc, 2, ql_length(ql)); CuAssertIntEquals(tc, ql_true, a); a = ql_set_insert(&ql, (void *)data); CuAssertIntEquals(tc, 2, ql_length(ql)); CuAssertIntEquals(tc, ql_false, a); a = ql_set_insert(&ql, (void *)(data + 2)); CuAssertIntEquals(tc, ql_true, a); CuAssertIntEquals(tc, 3, ql_length(ql)); CuAssertPtrEquals(tc, (void *)data, ql_get(ql, 0)); CuAssertPtrEquals(tc, (void *)(data + 1), ql_get(ql, 1)); CuAssertPtrEquals(tc, (void *)(data + 2), ql_get(ql, 2)); a = ql_set_find(&ql, &qi, (void *)(data + 3)); CuAssertIntEquals(tc, 0, a); a = ql_set_find(&ql, &qi, (void *)data); CuAssertIntEquals(tc, 1, a); CuAssertPtrEquals(tc, (void *)data, ql_get(ql, qi)); ql_free(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 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_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_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); }
/* Feuersturm: Betrifft sehr viele Gegner (in der Regel alle), * macht nur vergleichsweise geringen Schaden */ int sp_immolation(struct castorder * co) { fighter * fi = co->magician.fig; int level = co->level; const spell * sp = co->sp; battle *b = fi->side->battle; troop at; int force, qi, killed = 0; const char *damage; quicklist *fgs, *ql; message *m; /* 2d4 HP */ damage = spell_damage(5); /* Betrifft alle Gegner */ force = 99999; if (!count_enemies(b, fi, FIGHT_ROW, AVOID_ROW, SELECT_ADVANCE | SELECT_FIND)) { message *m = msg_message("battle::out_of_range", "mage spell", fi->unit, sp); message_all(b, m); msg_release(m); return 0; } at.fighter = fi; at.index = 0; fgs = fighters(b, fi->side, FIGHT_ROW, AVOID_ROW, FS_ENEMY); for (qi = 0, ql = fgs; ql; ql_advance(&ql, &qi, 1)) { fighter *df = (fighter *)ql_get(ql, qi); int n = df->alive - df->removed; troop dt; dt.fighter = df; while (n != 0) { dt.index = --n; killed += terminate(dt, at, AT_COMBATSPELL, damage, false); if (--force == 0) break; } if (force == 0) break; } ql_free(fgs); m = msg_message("battle::combatspell", "mage spell killed", fi->unit, sp, killed); message_all(b, m); msg_release(m); return level; }
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); }
void spellbook_clear(spellbook *sb) { quicklist *ql; int qi; assert(sb); for (qi = 0, ql = sb->spells; ql; ql_advance(&ql, &qi, 1)) { spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi); free(sbe); } ql_free(sb->spells); }
void free_region(region *r) { region **bucket; bucket = ®ionhash[r->uid & RHASHMASK]; while (*bucket!=r) { bucket = &(*bucket)->nexthash_; } *bucket = r->nexthash_; free(r->name_); while (r->units) { unit *u = r->units; r->units = u->next; free_unit(u); } ql_foreach(r->ships, (ql_cb)free_ship); ql_free(r->ships); r->ships = 0; ql_foreach(r->buildings, (ql_cb)free_building); ql_free(r->buildings); r->buildings = 0; free(r); }
static void test_insert(CuTest * tc) { struct quicklist *ql = NULL; int i; for (i = 0; i != 32; ++i) { CuAssertIntEquals(tc, i, ql_length(ql)); ql_insert(&ql, 0, (void *)(data + 31 - i)); } for (i = 0; i != 32; ++i) { CuAssertPtrEquals(tc, (void *)(data + i), ql_get(ql, i)); } ql_free(ql); }
static void test_advance(CuTest * tc) { struct quicklist *ql = NULL, *qli; int i, n = 31; for (i = 0; i != 32; ++i) { ql_insert(&ql, 0, (void *)(data + i)); } for (i = 0, qli = ql; qli; ql_advance(&qli, &i, 1), n--) { void * g = ql_get(qli, i); CuAssertPtrEquals(tc, (void *)(data + n), g); } ql_free(ql); }
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); }
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); }
int sp_healing(struct castorder * co) { fighter * fi = co->magician.fig; int level = co->level; double power = co->force; battle *b = fi->side->battle; unit *mage = fi->unit; int j = 0; int healhp = (int)power * 200; quicklist *fgs; message *msg; bool use_item = has_ao_healing(mage); /* bis zu 11 Personen pro Stufe (einen HP m�ssen sie ja noch * haben, sonst w�ren sie tot) k�nnen geheilt werden */ if (use_item) { healhp *= 2; } /* gehe alle denen wir helfen der reihe nach durch, heile verwundete, * bis zu verteilende HP aufgebraucht sind */ fgs = fighters(b, fi->side, FIGHT_ROW, AVOID_ROW, FS_HELP); scramble_fighters(fgs); j += heal_fighters(fgs, &healhp, false); j += heal_fighters(fgs, &healhp, true); ql_free(fgs); if (j <= 0) { level = j; } if (use_item) { msg = msg_message("healing_effect_1", "mage amount item", mage, j, get_resourcetype(R_AMULET_OF_HEALING)); } else { msg = msg_message("healing_effect_0", "mage amount", mage, j); } message_all(b, msg); msg_release(msg); return level; }
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; }
void wormholes_update(void) { const building_type *bt_wormhole = bt_find("wormhole"); quicklist *rlist = 0; int count = 0; region **match; if (bt_wormhole == NULL) { return; } select_wormhole_regions(&rlist, &count); if (count < 2) { return; } match = (region **)malloc(sizeof(region *) * count); sort_wormhole_regions(rlist, match, count); ql_free(rlist); make_wormholes(match, count, bt_wormhole); free(match); }
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_insert_atend(CuTest * tc) { struct quicklist *ql = NULL; ql_iter qi; int i; for (i = 0; i != QL_MAXSIZE; ++i) { ql_insert(&ql, i, (void *)(data + i)); } CuAssertIntEquals(tc, QL_MAXSIZE, ql_length(ql)); qi = qli_init(&ql); for (i = 0; qli_more(qi); ++i) { void *ptr = qli_next(&qi); CuAssertPtrEquals(tc, (void *)(data + i), ptr); if (i < QL_MAXSIZE - 1) { CuAssertPtrEquals(tc, ql, qi.l); } else { CuAssertPtrEquals(tc, 0, qi.l); } } ql_free(ql); }
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); }
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 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); } }
void free_terrains(void) { ql_foreach(terrains, (ql_cb)free_terrain); ql_free(terrains); terrains = 0; }
void free_regions(void) { ql_foreach(regions, (ql_cb)free_region); ql_free(regions); regions = 0; }
void free_buildingtypes(void) { ql_foreach(buildingtypes, free_buildingtype); ql_free(buildingtypes); buildingtypes = 0; }