unit *addplayer(region * r, faction * f) { unit *u; const char * name; assert(r->land); if (rpeasants(r) < PEASANT_MIN) { rsetpeasants(r, PEASANT_MIN + rng_int() % (PEASANT_MAX - PEASANT_MIN)); } assert(f->units == NULL); faction_setorigin(f, 0, r->x, r->y); u = create_unit(r, f, 1, f->race, 0, NULL, NULL); u->thisorder = default_order(f->locale); unit_addorder(u, copy_order(u->thisorder)); name = config_get("rules.equip_first"); if (!equip_unit(u, name ? name : "first_unit")) { /* give every unit enough money to survive the first turn */ i_change(&u->items, get_resourcetype(R_SILVER)->itype, maintenance_cost(u)); } u->hp = unit_max_hp(u) * u->number; fset(u, UFL_ISNEW); if (f->race == get_race(RC_DAEMON)) { race_t urc; const race *rc; do { urc = (race_t)(rng_int() % MAXRACES); rc = get_race(urc); } while (rc == NULL || urc == RC_DAEMON || !playerrace(rc)); u->irace = rc; } f->lastorders = 0; return u; }
static void test_set_origin(CuTest *tc) { faction *f; int x = 0, y = 0; plane *pl; test_cleanup(); test_create_world(); pl = create_new_plane(0, "", 0, 19, 0, 19, 0); f = test_create_faction(0); CuAssertPtrEquals(tc, 0, f->ursprung); faction_setorigin(f, 0, 1, 1); CuAssertIntEquals(tc, 0, f->ursprung->id); CuAssertIntEquals(tc, 1, f->ursprung->x); CuAssertIntEquals(tc, 1, f->ursprung->y); faction_getorigin(f, 0, &x, &y); CuAssertIntEquals(tc, 1, x); CuAssertIntEquals(tc, 1, y); adjust_coordinates(f, &x, &y, pl); CuAssertIntEquals(tc, -9, x); CuAssertIntEquals(tc, -9, y); adjust_coordinates(f, &x, &y, 0); CuAssertIntEquals(tc, -10, x); CuAssertIntEquals(tc, -10, y); test_cleanup(); }
static void test_set_origin_bug(CuTest *tc) { faction *f; plane *pl; int x = 17, y = 10; test_cleanup(); test_create_world(); pl = create_new_plane(0, "", 0, 19, 0, 19, 0); f = test_create_faction(0); faction_setorigin(f, 0, -10, 3); faction_setorigin(f, 0, -13, -4); adjust_coordinates(f, &x, &y, pl); CuAssertIntEquals(tc, 0, f->ursprung->id); CuAssertIntEquals(tc, -9, x); CuAssertIntEquals(tc, 2, y); test_cleanup(); }
static void test_origin(CuTest *tc) { struct faction *f; int x, y; test_setup(); f = test_create_faction(NULL); x = 0; y = 0; adjust_coordinates(f, &x, &y, 0); CuAssertIntEquals(tc, 0, x); CuAssertIntEquals(tc, 0, y); faction_setorigin(f, 0, 10, 20); adjust_coordinates(f, &x, &y, 0); CuAssertIntEquals(tc, -10, x); CuAssertIntEquals(tc, -20, y); test_teardown(); }