(svn r11564) -Codechange: Increase the usage of the for_each_bit macro and rename it fitting to the naming style

This commit is contained in:
skidd13 2007-12-03 09:19:19 +00:00
parent 2b372e7e7b
commit 5d74f2ced4
6 changed files with 38 additions and 29 deletions

View File

@ -1653,13 +1653,11 @@ clear_town_stuff:;
k = 0;
// Build the rail
for (i = 0; i != 6; i++, j >>= 1) {
if (j & 1) {
k = i;
ret = DoCommand(c, railtype, i, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL);
if (CmdFailed(ret)) return CMD_ERROR;
total_cost.AddCost(ret);
}
FOR_EACH_SET_BIT(i, j) {
k = i;
ret = DoCommand(c, railtype, i, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL);
if (CmdFailed(ret)) return CMD_ERROR;
total_cost.AddCost(ret);
}
/* signals too? */
@ -2854,7 +2852,6 @@ static bool AiCheckRoadFinished(Player *p)
TileIndex tile;
DiagDirection dir = p->ai.cur_dir_a;
uint32 bits;
int i;
are.dest = p->ai.cur_tile_b;
tile = TILE_MASK(p->ai.cur_tile_a + TileOffsByDiagDir(dir));
@ -2865,7 +2862,8 @@ static bool AiCheckRoadFinished(Player *p)
are.best_dist = (uint)-1;
for_each_bit(i, bits) {
uint i;
FOR_EACH_SET_BIT(i, bits) {
FollowTrack(tile, 0x3000 | TRANSPORT_ROAD, ROADTYPES_ROAD, (DiagDirection)_dir_by_track[i], (TPFEnumProc*)AiEnumFollowRoad, NULL, &are);
}

View File

@ -48,7 +48,7 @@ uint8 FindFirstBit(uint32 x)
* Search the last set bit in a 32 bit variable.
*
* This algorithm is a static implementation of a log
* conguence search algorithm. It checks the first half
* conguence search algorithm. It checks the second half
* if there is a bit set search there further. And this
* way further. If no bit is set return 0.
*

View File

@ -20,10 +20,20 @@
*/
#define IS_CUSTOM_SPRITE(sprite) ((sprite) >= SPR_SIGNALS_BASE)
#define for_each_bit(_i, _b) \
for (_i = 0; _b != 0; _i++, _b >>= 1) \
if (_b & 1)
/**
* Do an operation for each set set bit in a value.
*
* This macros is used to do an operation for each set
* bit in a variable. The first variable can be reused
* in the operation due to it's the bit position counter.
* The second variable will be cleared during the usage
*
* @param i The position counter
* @param b The value which we check for set bits
*/
#define FOR_EACH_SET_BIT(i, b) \
for (i = 0; b != 0; i++, b >>= 1) \
if (b & 1)
static inline uint16 ReadLE16Aligned(const void* x)

View File

@ -1273,18 +1273,17 @@ do_it:;
uint best_dist = (uint)-1;
uint best_maxlen = (uint)-1;
uint bitmask = (uint)trackdirs;
for (int i = 0; bitmask != 0; bitmask >>= 1, i++) {
if (HasBit(bitmask, 0)) {
if (best_track == INVALID_TRACKDIR) best_track = (Trackdir)i; // in case we don't find the path, just pick a track
frd.maxtracklen = (uint)-1;
frd.mindist = (uint)-1;
FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, v->u.road.compatible_roadtypes, _road_pf_directions[i], EnumRoadTrackFindDist, NULL, &frd);
uint i;
FOR_EACH_SET_BIT(i, bitmask) {
if (best_track == INVALID_TRACKDIR) best_track = (Trackdir)i; // in case we don't find the path, just pick a track
frd.maxtracklen = (uint)-1;
frd.mindist = (uint)-1;
FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, v->u.road.compatible_roadtypes, _road_pf_directions[i], EnumRoadTrackFindDist, NULL, &frd);
if (frd.mindist < best_dist || (frd.mindist == best_dist && frd.maxtracklen < best_maxlen)) {
best_dist = frd.mindist;
best_maxlen = frd.maxtracklen;
best_track = (Trackdir)i;
}
if (frd.mindist < best_dist || (frd.mindist == best_dist && frd.maxtracklen < best_maxlen)) {
best_dist = frd.mindist;
best_maxlen = frd.maxtracklen;
best_track = (Trackdir)i;
}
}
}

View File

@ -403,8 +403,9 @@ static void PlayerStationsWndProc(Window *w, WindowEvent *e)
ToggleBit(facilities, e->we.click.widget - STATIONLIST_WIDGET_TRAIN);
w->ToggleWidgetLoweredState(e->we.click.widget);
} else {
for (uint i = 0; facilities != 0; i++, facilities >>= 1) {
if (HasBit(facilities, 0)) w->RaiseWidget(i + STATIONLIST_WIDGET_TRAIN);
uint i;
FOR_EACH_SET_BIT(i, facilities) {
w->RaiseWidget(i + STATIONLIST_WIDGET_TRAIN);
}
SetBit(facilities, e->we.click.widget - STATIONLIST_WIDGET_TRAIN);
w->LowerWidget(e->we.click.widget);

View File

@ -121,8 +121,9 @@ uint GetMaskOfTownActions(int *nump, PlayerID pid, const Town *t)
static int GetNthSetBit(uint32 bits, int n)
{
if (n >= 0) {
for (uint i = 0; bits != 0; bits >>= 1, i++) {
if (bits & 1) n--;
uint i;
FOR_EACH_SET_BIT(i, bits) {
n--;
if (n < 0) return i;
}
}