1
0
Fork 0

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

release/0.6
skidd13 2007-12-03 09:19:19 +00:00
parent 1a43c6a6f6
commit 82913a2134
6 changed files with 38 additions and 29 deletions

View File

@ -1653,13 +1653,11 @@ clear_town_stuff:;
k = 0; k = 0;
// Build the rail // Build the rail
for (i = 0; i != 6; i++, j >>= 1) { FOR_EACH_SET_BIT(i, j) {
if (j & 1) { k = i;
k = i; ret = DoCommand(c, railtype, i, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL);
ret = DoCommand(c, railtype, i, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL); if (CmdFailed(ret)) return CMD_ERROR;
if (CmdFailed(ret)) return CMD_ERROR; total_cost.AddCost(ret);
total_cost.AddCost(ret);
}
} }
/* signals too? */ /* signals too? */
@ -2854,7 +2852,6 @@ static bool AiCheckRoadFinished(Player *p)
TileIndex tile; TileIndex tile;
DiagDirection dir = p->ai.cur_dir_a; DiagDirection dir = p->ai.cur_dir_a;
uint32 bits; uint32 bits;
int i;
are.dest = p->ai.cur_tile_b; are.dest = p->ai.cur_tile_b;
tile = TILE_MASK(p->ai.cur_tile_a + TileOffsByDiagDir(dir)); tile = TILE_MASK(p->ai.cur_tile_a + TileOffsByDiagDir(dir));
@ -2865,7 +2862,8 @@ static bool AiCheckRoadFinished(Player *p)
are.best_dist = (uint)-1; 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); 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. * Search the last set bit in a 32 bit variable.
* *
* This algorithm is a static implementation of a log * 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 * if there is a bit set search there further. And this
* way further. If no bit is set return 0. * 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 IS_CUSTOM_SPRITE(sprite) ((sprite) >= SPR_SIGNALS_BASE)
/**
#define for_each_bit(_i, _b) \ * Do an operation for each set set bit in a value.
for (_i = 0; _b != 0; _i++, _b >>= 1) \ *
if (_b & 1) * 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) static inline uint16 ReadLE16Aligned(const void* x)

View File

@ -1273,18 +1273,17 @@ do_it:;
uint best_dist = (uint)-1; uint best_dist = (uint)-1;
uint best_maxlen = (uint)-1; uint best_maxlen = (uint)-1;
uint bitmask = (uint)trackdirs; uint bitmask = (uint)trackdirs;
for (int i = 0; bitmask != 0; bitmask >>= 1, i++) { uint i;
if (HasBit(bitmask, 0)) { 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 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.maxtracklen = (uint)-1;
frd.mindist = (uint)-1; frd.mindist = (uint)-1;
FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, v->u.road.compatible_roadtypes, _road_pf_directions[i], EnumRoadTrackFindDist, NULL, &frd); 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)) { if (frd.mindist < best_dist || (frd.mindist == best_dist && frd.maxtracklen < best_maxlen)) {
best_dist = frd.mindist; best_dist = frd.mindist;
best_maxlen = frd.maxtracklen; best_maxlen = frd.maxtracklen;
best_track = (Trackdir)i; 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); ToggleBit(facilities, e->we.click.widget - STATIONLIST_WIDGET_TRAIN);
w->ToggleWidgetLoweredState(e->we.click.widget); w->ToggleWidgetLoweredState(e->we.click.widget);
} else { } else {
for (uint i = 0; facilities != 0; i++, facilities >>= 1) { uint i;
if (HasBit(facilities, 0)) w->RaiseWidget(i + STATIONLIST_WIDGET_TRAIN); FOR_EACH_SET_BIT(i, facilities) {
w->RaiseWidget(i + STATIONLIST_WIDGET_TRAIN);
} }
SetBit(facilities, e->we.click.widget - STATIONLIST_WIDGET_TRAIN); SetBit(facilities, e->we.click.widget - STATIONLIST_WIDGET_TRAIN);
w->LowerWidget(e->we.click.widget); 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) static int GetNthSetBit(uint32 bits, int n)
{ {
if (n >= 0) { if (n >= 0) {
for (uint i = 0; bits != 0; bits >>= 1, i++) { uint i;
if (bits & 1) n--; FOR_EACH_SET_BIT(i, bits) {
n--;
if (n < 0) return i; if (n < 0) return i;
} }
} }