as much as possible, replace the UFL_OWNER flag with checking the owner the canonical way.

This commit is contained in:
Enno Rehling 2012-05-17 16:40:50 -07:00
parent 95d1203594
commit 775e8edae1
10 changed files with 49 additions and 132 deletions

View File

@ -170,13 +170,7 @@ 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);
unit *o = b ? building_owner(b) : NULL;
if (o && o != u) {
freset(o, UFL_OWNER);
}
if (u) {
fset(u, UFL_OWNER);
}
building_set_owner(b, u);
return 0;
}

View File

@ -1351,27 +1351,29 @@ static void cr_output_region(FILE * F, report_context * ctx, seen_region * sr)
ru && ru->type == &at_travelunit; ru = ru->next) {
unit *u = (unit *) ru->data.v;
if (cansee_durchgezogen(f, r, u, 0) && r != u->region) {
if (!u->ship || !fval(u, UFL_OWNER))
continue;
if (!seeships)
if (u->ship && ship_owner(u->ship)==u) {
if (!seeships) {
fprintf(F, "DURCHSCHIFFUNG\n");
}
seeships = true;
fprintf(F, "\"%s\"\n", shipname(u->ship));
}
}
}
for (ru = a_find(r->attribs, &at_travelunit);
ru && ru->type == &at_travelunit; ru = ru->next) {
unit *u = (unit *) ru->data.v;
if (cansee_durchgezogen(f, r, u, 0) && r != u->region) {
if (u->ship)
continue;
if (!seeunits)
if (!u->ship) {
if (!seeunits) {
fprintf(F, "DURCHREISE\n");
}
seeunits = true;
fprintf(F, "\"%s\"\n", unitname(u));
}
}
}
}
cr_output_messages(F, r->msgs, f);
{
message_list *mlist = r_getmessages(r, f);

View File

@ -1105,51 +1105,6 @@ static boolean maintain(building * b, boolean first)
return true;
}
#ifdef COLLAPSE_CHANCE
static void gebaeude_stuerzt_ein(region * r, building * b)
{
unit *u;
int n, i;
int opfer = 0;
int road = 0;
struct message *msg;
for (u = r->units; u; u = u->next) {
if (u->building == b) {
int loss = 0;
fset(u->faction, FFL_MARK);
freset(u, UFL_OWNER);
leave(r, u);
n = u->number;
#ifdef COLLAPSE_SURVIVAL
for (i = 0; i < n; i++) {
if (rng_double() >= COLLAPSE_SURVIVAL) {
++loss;
}
}
#endif
scale_number(u, u->number - loss);
opfer += loss;
}
}
msg =
msg_message("buildingcrash", "region building opfer road", r, b, opfer,
road);
add_message(&r->msgs, msg);
for (u = r->units; u; u = u->next) {
faction *f = u->faction;
if (fval(f, FFL_MARK)) {
freset(u->faction, FFL_MARK);
add_message(&f->msgs, msg);
}
}
msg_release(msg);
remove_building(&r->buildings, b);
}
#endif
void maintain_buildings(region * r, boolean crash)
{
building **bp = &r->buildings;
@ -1159,12 +1114,6 @@ void maintain_buildings(region * r, boolean crash)
/* the second time, send a message */
if (crash) {
#ifdef COLLAPSE_CHANCE
if (!maintained && (rng_double() < COLLAPSE_CHANCE)) {
gebaeude_stuerzt_ein(r, b);
continue;
}
#endif
if (!fval(b, BLD_WORKING)) {
unit *u = building_owner(b);
const char *msgtype =

View File

@ -1203,7 +1203,7 @@ static void nmr_death(faction * f)
if (rule) {
unit *u;
for (u = f->units; u; u = u->nextF) {
if (u->building && fval(u, UFL_OWNER)) {
if (u->building && building_owner(u->building)==u) {
remove_building(&u->region->buildings, u->building);
}
}
@ -1526,7 +1526,6 @@ static cmp_building_cb get_cmp_region_owner(void)
static int display_cmd(unit * u, struct order *ord)
{
building *b = u->building;
char **s = NULL;
const char *str;
region *r = u->region;
@ -1538,19 +1537,19 @@ static int display_cmd(unit * u, struct order *ord)
switch (findparam_ex(str, u->faction->locale)) {
case P_BUILDING:
case P_GEBAEUDE:
if (!b) {
if (!u->building) {
cmistake(u, ord, 145, MSG_PRODUCE);
break;
}
if (!fval(u, UFL_OWNER)) {
if (building_owner(u->building)!=u) {
cmistake(u, ord, 5, MSG_PRODUCE);
break;
}
if (!fval(b->type, BTF_NAMECHANGE) && b->display && b->display[0] != 0) {
if (!fval(u->building->type, BTF_NAMECHANGE) && u->building->display && u->building->display[0]) {
cmistake(u, ord, 278, MSG_EVENT);
break;
}
s = &b->display;
s = &u->building->display;
break;
case P_SHIP:
@ -1558,7 +1557,7 @@ static int display_cmd(unit * u, struct order *ord)
cmistake(u, ord, 144, MSG_PRODUCE);
break;
}
if (!fval(u, UFL_OWNER)) {
if (ship_owner(u->ship)!=u) {
cmistake(u, ord, 12, MSG_PRODUCE);
break;
}
@ -1581,15 +1580,15 @@ static int display_cmd(unit * u, struct order *ord)
break;
case P_REGION:
if (!b) {
if (!u->building) {
cmistake(u, ord, 145, MSG_EVENT);
break;
}
if (!fval(u, UFL_OWNER)) {
if (building_owner(u->building)!=u) {
cmistake(u, ord, 148, MSG_EVENT);
break;
}
if (b != largestbuilding(r, get_cmp_region_owner(), false)) {
if (u->building != largestbuilding(r, get_cmp_region_owner(), false)) {
cmistake(u, ord, 147, MSG_EVENT);
break;
}
@ -1682,7 +1681,7 @@ rename_building(unit * u, order * ord, building * b, const char *name)
}
}
} else {
if (!fval(u, UFL_OWNER)) {
if (owner!=u) {
cmistake(u, ord, 148, MSG_PRODUCE);
return -1;
}
@ -1814,7 +1813,7 @@ static int name_cmd(unit * u, struct order *ord)
cmistake(u, ord, 144, MSG_PRODUCE);
break;
}
if (!fval(u, UFL_OWNER)) {
if (ship_owner(u->ship)!=u) {
cmistake(u, ord, 12, MSG_PRODUCE);
break;
}
@ -1857,7 +1856,7 @@ static int name_cmd(unit * u, struct order *ord)
cmistake(u, ord, 145, MSG_EVENT);
break;
}
if (!fval(u, UFL_OWNER)) {
if (building_owner(b)!=u) {
cmistake(u, ord, 148, MSG_EVENT);
break;
}
@ -2881,7 +2880,9 @@ static void reorder(void)
cmistake(u, ord, 258, MSG_EVENT);
} else if (v->building != u->building || v->ship != u->ship) {
cmistake(u, ord, 259, MSG_EVENT);
} else if (fval(u, UFL_OWNER)) {
} else if (u->building && building_owner(u->building)==u) {
cmistake(u, ord, 260, MSG_EVENT);
} else if (u->ship && ship_owner(u->ship)==u) {
cmistake(u, ord, 260, MSG_EVENT);
} else if (v == u) {
cmistake(u, ord, 10, MSG_EVENT);
@ -2895,7 +2896,9 @@ static void reorder(void)
sorted = true;
break;
case P_BEFORE:
if (fval(v, UFL_OWNER)) {
if (v->ship && ship_owner(v->ship)==v) {
cmistake(v, ord, 261, MSG_EVENT);
} else if (v->building && building_owner(v->building)==v) {
cmistake(v, ord, 261, MSG_EVENT);
} else {
unit **vp = &r->units;
@ -2990,7 +2993,7 @@ static int renumber_cmd(unit * u, order * ord)
cmistake(u, ord, 116, MSG_EVENT);
break;
}
if (!fval(u, UFL_OWNER)) {
if (ship_owner(u->ship)!=u) {
cmistake(u, ord, 146, MSG_EVENT);
break;
}
@ -3018,7 +3021,7 @@ static int renumber_cmd(unit * u, order * ord)
cmistake(u, ord, 145, MSG_EVENT);
break;
}
if (!fval(u, UFL_OWNER)) {
if (building_owner(u->building)!=u) {
cmistake(u, ord, 148, MSG_EVENT);
break;
}
@ -4068,12 +4071,6 @@ static void maintain_buildings_1(region * r)
maintain_buildings(r, false);
}
#ifdef COLLAPSE_CHANCE
static void maintain_buildings_2(region * r)
{
maintain_buildings(r, true);
}
#endif
static void reset_moved(unit * u)
{
freset(u, UFL_MOVED);
@ -4246,11 +4243,6 @@ void init_processor(void)
}
add_proc_global(p, &demographics, "Nahrung, Seuchen, Wachstum, Wanderung");
#ifdef COLLAPSE_CHANCE
p += 10;
add_proc_region(p, &maintain_buildings_2, "Gebaeudeunterhalt (2. Versuch)");
#endif
if (!global.disabled[K_SORT]) {
p += 10;
add_proc_global(p, &reorder, "Einheiten sortieren");

View File

@ -54,7 +54,6 @@ static void test_rename_building(CuTest * tc)
f = test_create_faction(rc_find("human"));
u = test_create_unit(f, r);
u_set_building(u, b);
fset(u, UFL_OWNER);
rename_building(u, NULL, b, "Villa Nagel");
CuAssertStrEquals(tc, "Villa Nagel", b->name);

View File

@ -1382,7 +1382,7 @@ static void durchreisende(FILE * F, const region * r, const faction * f)
for (a = abegin; a && a->type == &at_travelunit; a = a->next) {
unit *u = (unit *) a->data.v;
if (r != u->region && (u->ship == NULL || fval(u, UFL_OWNER))) {
if (r != u->region && (!u->ship || ship_owner(u->ship)==u)) {
if (cansee_durchgezogen(f, r, u, 0)) {
++maxtravel;
}
@ -1399,7 +1399,7 @@ static void durchreisende(FILE * F, const region * r, const faction * f)
for (a = abegin; a && a->type == &at_travelunit; a = a->next) {
unit *u = (unit *) a->data.v;
if (r != u->region && (u->ship == NULL || fval(u, UFL_OWNER))) {
if (r != u->region && (!u->ship || ship_owner(u->ship)==u)) {
if (cansee_durchgezogen(f, r, u, 0)) {
++counter;
if (u->ship != NULL) {
@ -1565,7 +1565,7 @@ report_template(const char *filename, report_context * ctx, const char *charset)
unitid(u), u->name, u->number, get_money(u));
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (u->building != NULL && fval(u, UFL_OWNER)) {
if (u->building && building_owner(u->building)==u) {
building *b = u->building;
int cost = buildingmaintenance(b, r_silver);
@ -1578,7 +1578,7 @@ report_template(const char *filename, report_context * ctx, const char *charset)
WARN_STATIC_BUFFER();
}
} else if (u->ship) {
if (fval(u, UFL_OWNER)) {
if (ship_owner(u->ship)==u) {
bytes = (int)strlcpy(bufp, ",S", size);
} else {
bytes = (int)strlcpy(bufp, ",s", size);

View File

@ -376,7 +376,6 @@ int destroy_cmd(unit * u, struct order *ord)
/* all units leave the building */
for (u2 = r->units; u2; u2 = u2->next) {
if (u2->building == b) {
freset(u2, UFL_OWNER);
leave_building(u2);
}
}

View File

@ -68,13 +68,6 @@ extern "C" {
/* Chance of a monster attack */
#define MONSTERATTACK 0.4F
/** Chance of an unmaintained building crashing */
/* #define COLLAPSE_CHANCE 0.4F */
#undef COLLAPSE_CHANCE
/** chance to survive the crash of a building */
/* #define COLLAPSE_SURVIVAL 0.5F */
#undef COLLAPSE_SURVIVAL
/* Bewegungsweiten: */
#define BP_WALKING 4
#define BP_RIDING 6

View File

@ -2597,13 +2597,13 @@ void movement(void)
set_order(&u->thisorder, NULL);
} else {
if (ships) {
if (u->ship && fval(u, UFL_OWNER)) {
if (u->ship && ship_owner(u->ship)==u) {
init_tokens(u->thisorder);
skip_token();
move(u, false);
}
} else {
if (u->ship == NULL || !fval(u, UFL_OWNER)) {
if (!u->ship || ship_owner(u->ship)!=u) {
init_tokens(u->thisorder);
skip_token();
move(u, false);

View File

@ -795,32 +795,21 @@ void u_set_ship(unit * u, ship * sh)
void leave_ship(unit * u)
{
struct ship *sh = u->ship;
if (!sh) return;
u->ship = 0;
if (sh->_owner==u) {
sh->_owner = ship_owner(sh);
}
set_leftship(u, sh);
}
void leave_building(unit * u)
{
struct building *b = u->building;
building * b = u->building;
u->building = NULL;
if (b && fval(u, UFL_OWNER)) {
unit *u2, *owner = NULL;
freset(u, UFL_OWNER);
for (u2 = u->region->units; u2; u2 = u2->next) {
if (u2->building == b) {
if (u2->faction == u->faction) {
owner = u2;
break;
} else if (owner == NULL)
owner = u2;
}
}
if (owner != NULL) {
fset(owner, UFL_OWNER);
}
u->building = 0;
if (b->_owner==u) {
b->_owner = building_owner(b);
}
}