void u_set_building(unit * u, building * b) { assert(!u->building); /* you must leave first */ u->building = b; if (b && !b->_owner) { building_set_owner(u); } }
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 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; } }
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_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(); }