static void test_buildingowner_goes_to_other_when_empty(CuTest * tc) { struct region *r; struct building *bld; struct unit *u, *u2; struct faction *f; test_setup(); f = test_create_faction(NULL); r = test_create_plain(0, 0); bld = test_create_building(r, NULL); CuAssertPtrNotNull(tc, bld); u2 = test_create_unit(f, r); u = test_create_unit(f, r); CuAssertPtrNotNull(tc, u); u_set_building(u, bld); CuAssertPtrEquals(tc, u, building_owner(bld)); u_set_building(u2, bld); CuAssertPtrEquals(tc, u, building_owner(bld)); u->number = 0; CuAssertPtrEquals(tc, u2, building_owner(bld)); test_teardown(); }
void test_buildingowner_goes_to_empty_unit_after_leave(CuTest * tc) { struct region *r; struct building *bld; struct unit *u1, *u2, *u3; struct faction *f1; test_setup(); f1 = test_create_faction(NULL); r = test_create_plain(0, 0); bld = test_create_building(r, NULL); CuAssertPtrNotNull(tc, bld); u1 = test_create_unit(f1, r); u2 = test_create_unit(f1, r); u3 = test_create_unit(f1, r); u_set_building(u1, bld); u_set_building(u2, bld); u_set_building(u3, bld); CuAssertPtrEquals(tc, u1, building_owner(bld)); u2->number = 0; leave_building(u1); CuAssertPtrEquals(tc, u3, building_owner(bld)); leave_building(u3); CuAssertPtrEquals(tc, NULL, building_owner(bld)); u2->number = 1; CuAssertPtrEquals(tc, u2, building_owner(bld)); test_teardown(); }
static void test_buildingowner_goes_to_same_faction_after_leave(CuTest * tc) { struct region *r; struct building *bld; struct unit *u, *u2, *u3; struct faction *f1, *f2; test_setup(); f1 = test_create_faction(NULL); f2 = test_create_faction(NULL); r = test_create_plain(0, 0); bld = test_create_building(r, NULL); CuAssertPtrNotNull(tc, bld); u2 = test_create_unit(f2, r); u3 = test_create_unit(f1, r); u = test_create_unit(f1, r); CuAssertPtrNotNull(tc, u); u_set_building(u, bld); u_set_building(u2, bld); u_set_building(u3, bld); CuAssertPtrEquals(tc, u, building_owner(bld)); leave_building(u); CuAssertPtrEquals(tc, u3, building_owner(bld)); leave_building(u3); CuAssertPtrEquals(tc, u2, building_owner(bld)); leave_building(u2); CuAssertPtrEquals(tc, NULL, building_owner(bld)); test_teardown(); }
static void test_cmp_current_owner(CuTest *tc) { region *r; building *b1, *b2; building_type *btype; unit *u1, *u2; test_setup(); config_set("rules.region_owners", "1"); r = test_create_region(0, 0, NULL); btype = test_create_buildingtype("watch"); btype->stages->construction->maxsize = 1; btype->taxes = 200; b1 = test_create_building(r, btype); btype = test_create_buildingtype("castle"); btype->stages->construction->maxsize = 1; btype->taxes = 100; b2 = test_create_building(r, btype); b1->size = 1; CuAssertIntEquals(tc, 1, buildingeffsize(b1, false)); b2->size = 1; CuAssertIntEquals(tc, 1, buildingeffsize(b2, false)); u1 = test_create_unit(test_create_faction(NULL), r); u_set_building(u1, b1); u2 = test_create_unit(test_create_faction(NULL), r); u_set_building(u2, b2); region_set_owner(r, u1->faction, turn); CuAssertPtrEquals(tc, b1, largestbuilding(r, cmp_current_owner, false)); CuAssertTrue(tc, cmp_current_owner(b2, b1) < 0); CuAssertTrue(tc, cmp_current_owner(b1, b2) > 0); CuAssertTrue(tc, cmp_current_owner(b1, b1) == 0); test_teardown(); }
static void test_active_building(CuTest *tc) { building *b; region *r; unit *u; building_type *btype; test_setup(); btype = test_create_buildingtype("castle"); assert(btype && btype->maxsize == -1); b = test_create_building(r = test_create_region(0, 0, NULL), btype); u = test_create_unit(test_create_faction(NULL), r); CuAssertIntEquals(tc, false, building_is_active(b)); CuAssertPtrEquals(tc, NULL, active_building(u, btype)); b->flags |= BLD_MAINTAINED; CuAssertIntEquals(tc, true, building_is_active(b)); CuAssertPtrEquals(tc, NULL, active_building(u, btype)); u_set_building(u, b); CuAssertIntEquals(tc, true, building_is_active(b)); CuAssertPtrNotNull(tc, active_building(u, btype) ); btype->maxsize = 10; b->size = btype->maxsize; CuAssertIntEquals(tc, true, building_is_active(b)); CuAssertPtrNotNull(tc, active_building(u, btype) ); b->size = 9; CuAssertIntEquals(tc, false, building_is_active(b)); CuAssertPtrEquals(tc, NULL, active_building(u, btype)); btype->maxsize = -1; b->flags &= ~BLD_MAINTAINED; CuAssertIntEquals(tc, false, building_is_active(b)); CuAssertPtrEquals(tc, NULL, active_building(u, btype)); test_teardown(); }
static void test_rename_building_twice(CuTest * tc) { region *r; building *b; unit *u; faction *f; building_type *btype; test_cleanup(); test_create_world(); btype = bt_get_or_create("castle"); r = findregion(-1, 0); b = new_building(btype, r, default_locale); f = test_create_faction(rc_find("human")); u = test_create_unit(f, r); u_set_building(u, b); rename_building(u, NULL, b, "Villa Nagel"); CuAssertStrEquals(tc, "Villa Nagel", b->name); rename_building(u, NULL, b, "Villa Kunterbunt"); CuAssertStrEquals(tc, "Villa Kunterbunt", b->name); }
static int tolua_building_set_owner(lua_State * L) { building *b = (building *)tolua_tousertype(L, 1, 0); unit *u = (unit *)tolua_tousertype(L, 2, 0); if (b != u->building) { u_set_building(u, b); } building_set_owner(u); return 0; }
static void test_reorder_units(CuTest * tc) { region *r; building *b; ship * s; unit *u0, *u1, *u2, *u3, *u4; struct faction * f; const building_type *btype; const ship_type *stype; test_cleanup(); test_create_world(); btype = bt_find("castle"); stype = st_find("boat"); r = findregion(-1, 0); b = test_create_building(r, btype); s = test_create_ship(r, stype); f = test_create_faction(0); u0 = test_create_unit(f, r); u_set_ship(u0, s); u1 = test_create_unit(f, r); u_set_ship(u1, s); ship_set_owner(u1); u2 = test_create_unit(f, r); u3 = test_create_unit(f, r); u_set_building(u3, b); u4 = test_create_unit(f, r); u_set_building(u4, b); building_set_owner(u4); reorder_units(r); CuAssertPtrEquals(tc, u4, r->units); CuAssertPtrEquals(tc, u3, u4->next); CuAssertPtrEquals(tc, u2, u3->next); CuAssertPtrEquals(tc, u1, u2->next); CuAssertPtrEquals(tc, u0, u1->next); CuAssertPtrEquals(tc, 0, u0->next); }
static void test_give_control_building(CuTest * tc) { unit *u1, *u2; building *b; struct faction *f; region *r; test_cleanup(); f = test_create_faction(0); r = test_create_region(0, 0, 0); b = test_create_building(r, 0); u1 = test_create_unit(f, r); u_set_building(u1, b); u2 = test_create_unit(f, r); u_set_building(u2, b); CuAssertPtrEquals(tc, u1, building_owner(b)); give_control(u1, u2); CuAssertPtrEquals(tc, u2, building_owner(b)); test_cleanup(); }
static void test_cmp_castle_size(CuTest *tc) { region *r; building *b1, *b2; unit *u1, *u2; test_setup(); r = test_create_region(0, 0, NULL); b1 = test_create_building(r, NULL); b2 = test_create_building(r, NULL); u1 = test_create_unit(test_create_faction(NULL), r); u_set_building(u1, b1); u2 = test_create_unit(test_create_faction(NULL), r); u_set_building(u2, b2); b1->size = 5; b2->size = 10; CuAssertTrue(tc, cmp_castle_size(b1, b2) < 0); CuAssertTrue(tc, cmp_castle_size(b2, b1) > 0); CuAssertTrue(tc, cmp_castle_size(b1, b1) == 0); test_teardown(); }
static void test_cmp_taxes(CuTest *tc) { region *r; building *b1, *b2; building_type *btype; unit *u1, *u2; test_setup(); btype = test_create_buildingtype("castle"); btype->taxes = 100; r = test_create_region(0, 0, NULL); b1 = test_create_building(r, btype); b2 = test_create_building(r, btype); b1->size = 5; b2->size = 10; u1 = test_create_unit(test_create_faction(NULL), r); u_set_building(u1, b1); u2 = test_create_unit(test_create_faction(NULL), r); u_set_building(u2, b2); CuAssertPtrEquals(tc, b2, largestbuilding(r, cmp_taxes, false)); CuAssertTrue(tc, cmp_taxes(b1, b2) < 0); CuAssertTrue(tc, cmp_taxes(b2, b1) > 0); CuAssertTrue(tc, cmp_taxes(b1, b1) == 0); test_teardown(); }
static void test_building_set_owner(CuTest * tc) { struct region *r; struct building *bld; struct unit *u1, *u2; struct faction *f; test_setup(); f = test_create_faction(NULL); r = test_create_region(0, 0, NULL); bld = test_create_building(r, NULL); u1 = test_create_unit(f, r); u_set_building(u1, bld); CuAssertPtrEquals(tc, u1, building_owner(bld)); u2 = test_create_unit(f, r); u_set_building(u2, bld); CuAssertPtrEquals(tc, u1, building_owner(bld)); building_set_owner(u2); CuAssertPtrEquals(tc, u2, building_owner(bld)); test_teardown(); }
static void equip_newunits(const struct equipment *eq, struct unit *u) { struct region *r = u->region; const struct resource_type *rtype; switch (old_race(u_race(u))) { case RC_ELF: rtype = rt_find("fairyboot"); set_show_item(u->faction, rtype->itype); break; case RC_GOBLIN: rtype = rt_find("roi"); set_show_item(u->faction, rtype->itype); set_number(u, 10); break; case RC_HUMAN: if (u->building == NULL) { const building_type *btype = bt_find("castle"); if (btype != NULL) { building *b = new_building(btype, r, u->faction->locale); b->size = 10; u_set_building(u, b); building_set_owner(u); } } break; case RC_CAT: rtype = rt_find("roi"); set_show_item(u->faction, rtype->itype); break; case RC_AQUARIAN: { ship *sh = new_ship(st_find("boat"), r, u->faction->locale); sh->size = sh->type->construction->maxsize; u_set_ship(u, sh); } break; case RC_CENTAUR: rsethorses(r, 250 + rng_int() % 51 + rng_int() % 51); break; default: break; } }
int build_building(unit * u, const building_type * btype, int id, int want, order * ord) { region *r = u->region; int n = want, built = 0; building *b = NULL; /* einmalige Korrektur */ const char *btname; order *new_order = NULL; const struct locale *lang = u->faction->locale; static int rule_other = -1; assert(u->number); assert(btype->construction); if (eff_skill(u, SK_BUILDING, r) == 0) { cmistake(u, ord, 101, MSG_PRODUCE); return 0; } /* Falls eine Nummer angegeben worden ist, und ein Gebaeude mit der * betreffenden Nummer existiert, ist b nun gueltig. Wenn keine Burg * gefunden wurde, dann wird nicht einfach eine neue erbaut. Ansonsten * baut man an der eigenen burg weiter. */ /* Wenn die angegebene Nummer falsch ist, KEINE Burg bauen! */ if (id > 0) { /* eine Nummer angegeben, keine neue Burg bauen */ b = findbuilding(id); if (!b || b->region != u->region) { /* eine Burg mit dieser Nummer gibt es hier nicht */ /* vieleicht Tippfehler und die eigene Burg ist gemeint? */ if (u->building && u->building->type == btype) { b = u->building; } else { /* keine neue Burg anfangen wenn eine Nummer angegeben war */ cmistake(u, ord, 6, MSG_PRODUCE); return 0; } } } else if (u->building && u->building->type == btype) { b = u->building; } if (b) btype = b->type; if (fval(btype, BTF_UNIQUE) && buildingtype_exists(r, btype, false)) { /* only one of these per region */ cmistake(u, ord, 93, MSG_PRODUCE); return 0; } if (besieged(u)) { /* units under siege can not build */ cmistake(u, ord, 60, MSG_PRODUCE); return 0; } if (btype->flags & BTF_NOBUILD) { /* special building, cannot be built */ cmistake(u, ord, 221, MSG_PRODUCE); return 0; } if ((r->terrain->flags & LAND_REGION) == 0) { /* special terrain, cannot build */ cmistake(u, ord, 221, MSG_PRODUCE); return 0; } if (btype->flags & BTF_ONEPERTURN) { if (b && fval(b, BLD_EXPANDED)) { cmistake(u, ord, 318, MSG_PRODUCE); return 0; } n = 1; } if (b) { if (rule_other < 0) { rule_other = get_param_int(global.parameters, "rules.build.other_buildings", 1); } if (!rule_other) { unit *owner = building_owner(b); if (!owner || owner->faction != u->faction) { cmistake(u, ord, 1222, MSG_PRODUCE); return 0; } } } if (b) built = b->size; if (n <= 0 || n == INT_MAX) { if (b == NULL) { if (btype->maxsize > 0) { n = btype->maxsize - built; } else { n = INT_MAX; } } else { if (b->type->maxsize > 0) { n = b->type->maxsize - built; } else { n = INT_MAX; } } } built = build(u, btype->construction, built, n); switch (built) { case ECOMPLETE: /* the building is already complete */ cmistake(u, ord, 4, MSG_PRODUCE); break; case ENOMATERIALS: ADDMSG(&u->faction->msgs, msg_materials_required(u, ord, btype->construction, want)); break; case ELOWSKILL: case ENEEDSKILL: /* no skill, or not enough skill points to build */ cmistake(u, ord, 50, MSG_PRODUCE); break; } if (built <= 0) { return built; } /* at this point, the building size is increased. */ if (b == NULL) { /* build a new building */ b = new_building(btype, r, lang); b->type = btype; fset(b, BLD_MAINTAINED | BLD_WORKING); /* Die Einheit befindet sich automatisch im Inneren der neuen Burg. */ if (u->number && leave(u, false)) { u_set_building(u, b); } } btname = LOC(lang, btype->_name); if (want - built <= 0) { /* gebäude fertig */ new_order = default_order(lang); } else if (want != INT_MAX) { /* reduzierte restgröße */ const char *hasspace = strchr(btname, ' '); if (hasspace) { new_order = create_order(K_MAKE, lang, "%d \"%s\" %i", n - built, btname, b->no); } else { new_order = create_order(K_MAKE, lang, "%d %s %i", n - built, btname, b->no); } } else if (btname) { /* Neues Haus, Befehl mit Gebäudename */ const char *hasspace = strchr(btname, ' '); if (hasspace) { new_order = create_order(K_MAKE, lang, "\"%s\" %i", btname, b->no); } else { new_order = create_order(K_MAKE, lang, "%s %i", btname, b->no); } } if (new_order) { replace_order(&u->orders, ord, new_order); free_order(new_order); } b->size += built; fset(b, BLD_EXPANDED); update_lighthouse(b); ADDMSG(&u->faction->msgs, msg_message("buildbuilding", "building unit size", b, u, built)); return built; }
static void move_iceberg(region * r) { attrib *a; direction_t dir; region *rc; a = a_find(r->attribs, &at_iceberg); if (!a) { dir = (direction_t) (rng_int() % MAXDIRECTIONS); a = a_add(&r->attribs, make_iceberg(dir)); } else { if (rng_int() % 100 < 20) { dir = (direction_t) (rng_int() % MAXDIRECTIONS); a->data.i = dir; } else { dir = (direction_t) a->data.i; } } rc = rconnect(r, dir); if (rc && !fval(rc->terrain, ARCTIC_REGION)) { if (fval(rc->terrain, SEA_REGION)) { /* Eisberg treibt */ ship *sh, *shn; unit *u; int x, y; 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_drift", "region dir", r, dir)); } x = r->x; y = r->y; runhash(r); runhash(rc); r->x = rc->x; r->y = rc->y; rc->x = x; rc->y = y; rhash(rc); rhash(r); /* rc ist der Ozean (Ex-Eisberg), r der Eisberg (Ex-Ozean) */ /* Schiffe aus dem Zielozean werden in den Eisberg transferiert * und nehmen Schaden. */ for (sh = r->ships; sh; sh = sh->next) freset(sh, SF_SELECT); for (sh = r->ships; sh; sh = sh->next) { /* Meldung an Kapitän */ float dmg = get_param_flt(global.parameters, "rules.ship.damage.intoiceberg", 0.10F); damage_ship(sh, dmg); fset(sh, SF_SELECT); } /* Personen, Schiffe und Gebäude verschieben */ while (rc->buildings) { rc->buildings->region = r; translist(&rc->buildings, &r->buildings, rc->buildings); } while (rc->ships) { float dmg = get_param_flt(global.parameters, "rules.ship.damage.withiceberg", 0.10F); fset(rc->ships, SF_SELECT); damage_ship(rc->ships, dmg); move_ship(rc->ships, rc, r, NULL); } while (rc->units) { building *b = rc->units->building; u = rc->units; u->building = 0; /* prevent leaving in move_unit */ move_unit(rc->units, r, NULL); u_set_building(u, b); /* undo leave-prevention */ } /* Beschädigte Schiffe können sinken */ for (sh = r->ships; sh;) { shn = sh->next; if (fval(sh, SF_SELECT)) { u = ship_owner(sh); if (sh->damage >= sh->size * DAMAGE_SCALE) { if (u != NULL) { ADDMSG(&u->faction->msgs, msg_message("overrun_by_iceberg_des", "ship", sh)); } remove_ship(&sh->region->ships, sh); } else if (u != NULL) { ADDMSG(&u->faction->msgs, msg_message("overrun_by_iceberg", "ship", sh)); } } sh = shn; } } else if (rng_int() % 100 < 20) { /* Eisberg bleibt als Gletscher liegen */ unit *u; rsetterrain(r, T_GLACIER); a_remove(&r->attribs, a); 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_land", "region", r)); } } } }
/** * see https://bugs.eressea.de/view.php?id=2234 */ static void test_maintain_buildings(CuTest *tc) { region *r; building *b; building_type *btype; unit *u; faction *f; maintenance *req; item_type *itype; test_cleanup(); btype = test_create_buildingtype("Hort"); btype->maxsize = 10; r = test_create_region(0, 0, 0); f = test_create_faction(0); u = test_create_unit(f, r); b = test_create_building(r, btype); itype = test_create_itemtype("money"); b->size = btype->maxsize; u_set_building(u, b); // this building has no upkeep, it just works: b->flags = 0; maintain_buildings(r); CuAssertIntEquals(tc, BLD_MAINTAINED, fval(b, BLD_MAINTAINED)); CuAssertPtrEquals(tc, 0, f->msgs); CuAssertPtrEquals(tc, 0, r->msgs); req = calloc(2, sizeof(maintenance)); req[0].number = 100; req[0].rtype = itype->rtype; btype->maintenance = req; // we cannot afford to pay: b->flags = 0; maintain_buildings(r); CuAssertIntEquals(tc, 0, fval(b, BLD_MAINTAINED)); CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "maintenancefail")); CuAssertPtrNotNull(tc, test_find_messagetype(r->msgs, "maintenance_nowork")); test_clear_messagelist(&f->msgs); test_clear_messagelist(&r->msgs); // we can afford to pay: i_change(&u->items, itype, 100); b->flags = 0; maintain_buildings(r); CuAssertIntEquals(tc, BLD_MAINTAINED, fval(b, BLD_MAINTAINED)); CuAssertIntEquals(tc, 0, i_get(u->items, itype)); CuAssertPtrEquals(tc, 0, r->msgs); CuAssertPtrEquals(tc, 0, test_find_messagetype(f->msgs, "maintenance_nowork")); CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "maintenance")); test_clear_messagelist(&f->msgs); // this building has no owner, it doesn't work: u_set_building(u, NULL); b->flags = 0; maintain_buildings(r); CuAssertIntEquals(tc, 0, fval(b, BLD_MAINTAINED)); CuAssertPtrEquals(tc, 0, f->msgs); CuAssertPtrNotNull(tc, test_find_messagetype(r->msgs, "maintenance_noowner")); test_clear_messagelist(&r->msgs); test_cleanup(); }
/** creates a new unit. * * @param dname: name, set to NULL to get a default. * @param creator: unit to inherit stealth, group, building, ship, etc. from */ unit *create_unit(region * r, faction * f, int number, const struct race *urace, int id, const char *dname, unit * creator) { unit *u = (unit *)calloc(1, sizeof(unit)); assert(urace); if (f) { assert(f->alive); u_setfaction(u, f); if (f->locale) { order *deford = default_order(f->locale); if (deford) { set_order(&u->thisorder, NULL); addlist(&u->orders, deford); } } } u_seteffstealth(u, -1); u_setrace(u, urace); u->irace = NULL; set_number(u, number); /* die nummer der neuen einheit muss vor name_unit generiert werden, * da der default name immer noch 'Nummer u->no' ist */ createunitid(u, id); /* zuerst in die Region setzen, da zb Drachennamen den Regionsnamen * enthalten */ if (r) move_unit(u, r, NULL); /* u->race muss bereits gesetzt sein, wird für default-hp gebraucht */ /* u->region auch */ u->hp = unit_max_hp(u) * number; if (!dname) { name_unit(u); } else { u->name = _strdup(dname); } if (creator) { attrib *a; /* erbt Kampfstatus */ setstatus(u, creator->status); /* erbt Gebäude/Schiff */ if (creator->region == r) { if (creator->building) { u_set_building(u, creator->building); } if (creator->ship && fval(u_race(u), RCF_CANSAIL)) { u_set_ship(u, creator->ship); } } /* Tarnlimit wird vererbt */ if (fval(creator, UFL_STEALTH)) { attrib *a = a_find(creator->attribs, &at_stealth); if (a) { int stealth = a->data.i; a = a_add(&u->attribs, a_new(&at_stealth)); a->data.i = stealth; } } /* Temps von parteigetarnten Einheiten sind wieder parteigetarnt */ if (fval(creator, UFL_ANON_FACTION)) { fset(u, UFL_ANON_FACTION); } /* Daemonentarnung */ set_racename(&u->attribs, get_racename(creator->attribs)); if (fval(u_race(u), RCF_SHAPESHIFT) && fval(u_race(creator), RCF_SHAPESHIFT)) { u->irace = creator->irace; } /* Gruppen */ if (creator->faction == f && fval(creator, UFL_GROUP)) { a = a_find(creator->attribs, &at_group); if (a) { group *g = (group *) a->data.v; set_group(u, g); } } a = a_find(creator->attribs, &at_otherfaction); if (a) { a_add(&u->attribs, make_otherfaction(get_otherfaction(a))); } a = a_add(&u->attribs, a_new(&at_creator)); a->data.v = creator; } return u; }