From ac66e3e28f35c6939d3af68d1f0e26eb9b34e377 Mon Sep 17 00:00:00 2001 From: tron Date: Wed, 20 Jul 2005 15:29:28 +0000 Subject: [PATCH] (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read --- ai_old.c | 9 +++++---- aircraft_cmd.c | 8 ++++---- clear_cmd.c | 8 ++++---- disaster_cmd.c | 22 +++++++++++----------- engine.c | 14 +++++++------- industry_cmd.c | 4 ++-- landscape.c | 12 ++++++------ music_gui.c | 4 ++-- network_data.c | 28 ++++++++++++++-------------- oldloader.c | 4 ++-- order.h | 6 +++--- order_cmd.c | 36 ++++++++++++++++++------------------ players.c | 20 ++++++++++---------- rail_cmd.c | 4 ++-- road_cmd.c | 16 ++++++++-------- settings_gui.c | 8 ++++---- station_cmd.c | 8 ++++---- strings.c | 4 ++-- town_cmd.c | 7 ++++--- train_cmd.c | 6 +++--- tunnelbridge_cmd.c | 18 +++++++++--------- vehicle.c | 8 ++++---- win32.c | 4 ++-- 23 files changed, 130 insertions(+), 128 deletions(-) diff --git a/ai_old.c b/ai_old.c index 41cffcd971..f6cd06bede 100644 --- a/ai_old.c +++ b/ai_old.c @@ -1534,15 +1534,16 @@ static void AiStateWantNewRoute(Player *p) static bool AiCheckTrackResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo) { uint values[NUM_CARGO]; - int w,h; int rad; for(;p->mode != 4;p++) if (p->mode == 1) { TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs)); + uint w; + uint h; - w = ((p->attr>>1) & 7); - h = ((p->attr>>4) & 7); - if (p->attr&1) intswap(w, h); + w = GB(p->attr, 1, 3); + h = GB(p->attr, 4, 3); + if (p->attr & 1) uintswap(w, h); if (_patches.modified_catchment) { diff --git a/aircraft_cmd.c b/aircraft_cmd.c index e67e21ad00..d5f9d061be 100644 --- a/aircraft_cmd.c +++ b/aircraft_cmd.c @@ -1045,13 +1045,13 @@ static void HandleCrashedAircraft(Vehicle *v) if (v->u.air.crashed_counter < 650) { if (CHANCE16R(1,32,r)) { - v->direction = (v->direction+_crashed_aircraft_moddir[(r >> 16)&3]) & 7; + v->direction = (v->direction + _crashed_aircraft_moddir[GB(r, 16, 2)]) & 7; SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos); r = Random(); CreateEffectVehicleRel(v, - 4 + (r&0xF), - 4 + ((r>>4)&0xF), - ((r>>8)&0xF), + GB(r, 0, 4) + 4, + GB(r, 4, 4) + 4, + GB(r, 8, 4), EV_EXPLOSION_SMALL); } } else if (v->u.air.crashed_counter >= 10000) { diff --git a/clear_cmd.c b/clear_cmd.c index 773d058674..38f1ccd686 100644 --- a/clear_cmd.c +++ b/clear_cmd.c @@ -470,7 +470,7 @@ void DrawHillyLandTile(TileInfo *ti) if (ti->tileh != 0) { DrawGroundSprite(0xFA0 + _tileh_to_sprite[ti->tileh]); } else { - DrawGroundSprite(_landscape_clear_sprites[((ti->x^ti->y) >> 4) & 0x7]); + DrawGroundSprite(_landscape_clear_sprites[GB(ti->x ^ ti->y, 4, 3)]); } } @@ -485,11 +485,11 @@ void DrawClearLandFence(TileInfo *ti, byte img) } if (img & 0x38) { - DrawGroundSpriteAt(_clear_land_fence_sprites_1[((img >> 3) & 7) - 1] + _fence_mod_by_tileh[ti->tileh], ti->x, ti->y, z); + DrawGroundSpriteAt(_clear_land_fence_sprites_1[GB(img, 3, 3) - 1] + _fence_mod_by_tileh[ti->tileh], ti->x, ti->y, z); } if (img & 0x7) { - DrawGroundSpriteAt(_clear_land_fence_sprites_1[(img & 7) - 1] + _fence_mod_by_tileh_2[ti->tileh], ti->x, ti->y, z); + DrawGroundSpriteAt(_clear_land_fence_sprites_1[GB(img, 0, 3) - 1] + _fence_mod_by_tileh_2[ti->tileh], ti->x, ti->y, z); } } @@ -785,7 +785,7 @@ void GenerateClearTile(void) r = Random(); tile = RandomTileSeed(r); if (IsTileType(tile, MP_CLEAR)) { - j = ((r >> 16) & 0xF) + 5; + j = GB(r, 16, 4) + 5; for(;;) { TileIndex tile_new; diff --git a/disaster_cmd.c b/disaster_cmd.c index 90e005df5d..a0e22b7f2c 100644 --- a/disaster_cmd.c +++ b/disaster_cmd.c @@ -226,9 +226,9 @@ static void DisasterTick_Zeppeliner(Vehicle *v) uint32 r = Random(); CreateEffectVehicleRel(v, - -7 + (r&0xF), - -7 + (r>>4&0xF), - 5 + (r>>8&0x7), + GB(r, 0, 4) - 7, + GB(r, 4, 4) - 7, + GB(r, 8, 3) + 5, EV_EXPLOSION_SMALL); } } else if (v->age == 350) { @@ -364,9 +364,9 @@ static void DisasterTick_2(Vehicle *v) uint32 r = Random(); CreateEffectVehicleAbove( - x + (r & 0x3F), - y + (r >> 6 & 0x3F), - (r >> 12 & 0xF), + GB(r, 0, 6) + x, + GB(r, 6, 6) + y, + GB(r, 12, 4), EV_EXPLOSION_SMALL); if (++v->age >= 55) @@ -435,9 +435,9 @@ static void DisasterTick_3(Vehicle *v) uint32 r = Random(); CreateEffectVehicleAbove( - x + (r & 0x3F), - y + (r >> 6 & 0x3F), - (r >> 12 & 0xF), + GB(r, 0, 6) + x, + GB(r, 6, 6) + y, + GB(r, 12, 4), EV_EXPLOSION_SMALL); if (++v->age >= 55) @@ -620,8 +620,8 @@ static void DisasterTick_4b(Vehicle *v) for(i=0; i!=80; i++) { uint32 r = Random(); CreateEffectVehicleAbove( - v->x_pos-32+(r&0x3F), - v->y_pos-32+(r>>5&0x3F), + GB(r, 0, 6) + v->x_pos - 32, + GB(r, 5, 6) + v->y_pos - 32, 0, EV_EXPLOSION_SMALL); } diff --git a/engine.c b/engine.c index 65f016eda7..45fbdb2f6e 100644 --- a/engine.c +++ b/engine.c @@ -196,22 +196,22 @@ void StartupEngines(void) e->player_avail = 0; r = Random(); - e->intro_date = (uint16)((r & 0x1FF) + ei->base_intro); + e->intro_date = GB(r, 0, 9) + ei->base_intro; if (e->intro_date <= _date) { e->age = (_date - e->intro_date) >> 5; e->player_avail = (byte)-1; e->flags |= ENGINE_AVAILABLE; } - e->reliability_start = (uint16)(((r >> 16) & 0x3fff) + 0x7AE0); + e->reliability_start = GB(r, 16, 14) + 0x7AE0; r = Random(); - e->reliability_max = (uint16)((r & 0x3fff) + 0xbfff); - e->reliability_final = (uint16)(((r>>16) & 0x3fff) + 0x3fff); + e->reliability_max = GB(r, 0, 14) + 0xBFFF; + e->reliability_final = GB(r, 16, 14) + 0x3FFF; r = Random(); - e->duration_phase_1 = (uint16)((r & 0x1F) + 7); - e->duration_phase_2 = (uint16)(((r >> 5) & 0xF) + ei->base_life * 12 - 96); - e->duration_phase_3 = (uint16)(((r >> 9) & 0x7F) + 120); + e->duration_phase_1 = GB(r, 0, 5) + 7; + e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96; + e->duration_phase_3 = GB(r, 9, 7) + 120; e->reliability_spd_dec = (ei->unk2&0x7F) << 2; diff --git a/industry_cmd.c b/industry_cmd.c index d421617e6d..d7eaf5840e 100644 --- a/industry_cmd.c +++ b/industry_cmd.c @@ -315,7 +315,7 @@ static void DrawCoalPlantSparkles(TileInfo *ti) { int image = _m[ti->tile].owner; if (image & 0x80) { - image = (image >> 2) & 0x1F; + image = GB(image, 2, 5); if (image != 0 && image < 7) { AddChildSpriteScreen(image + 0x806, _coal_plant_sparkles_x[image-1], @@ -1802,7 +1802,7 @@ static void MaybeNewIndustry(uint32 r) int j; Industry *i; - type = _new_industry_rand[_opt.landscape][(r >> 16) & 0x1F]; + type = _new_industry_rand[_opt.landscape][GB(r, 16, 5)]; if (type == IT_OIL_WELL && _date > 10958) return; diff --git a/landscape.c b/landscape.c index bd708aea45..2d6b772e83 100644 --- a/landscape.c +++ b/landscape.c @@ -352,7 +352,7 @@ void CDECL ModifyTile(TileIndex tile, uint flags, ...) va_start(va, flags); - if ((i = (flags >> 8) & 0xF) != 0) { + if ((i = GB(flags, 8, 4)) != 0) { SetTileType(tile, i - 1); } @@ -485,7 +485,7 @@ static void GenerateTerrain(int type, int flag) if (x < 2 || y < 2) return; - direction = (r >> 22) & 3; + direction = GB(r, 22, 2); if (direction & 1) { w = template->height; h = template->width; @@ -632,16 +632,16 @@ void GenerateLandscape(void) GenerateTerrain(2, 0); r = Random(); - flag = (r & 3) | 4; - for (i = ScaleByMapSize(((r >> 16) & 0x7F) + 450); i != 0; --i) + flag = GB(r, 0, 2) | 4; + for (i = ScaleByMapSize(GB(r, 16, 7) + 450); i != 0; --i) GenerateTerrain(4, flag); } else if (_opt.landscape == LT_DESERT) { for (i = ScaleByMapSize((Random()&0x7F) + 170); i != 0; --i) GenerateTerrain(0, 0); r = Random(); - flag = (r & 3) | 4; - for (i = ScaleByMapSize(((r >> 16) & 0xFF) + 1700); i != 0; --i) + flag = GB(r, 0, 2) | 4; + for (i = ScaleByMapSize(GB(r, 16, 8) + 1700); i != 0; --i) GenerateTerrain(0, flag); flag ^= 2; diff --git a/music_gui.c b/music_gui.c index 4cf254b431..81302355c8 100644 --- a/music_gui.c +++ b/music_gui.c @@ -132,8 +132,8 @@ static void SelectSongToPlay(void) i = 500; do { uint32 r = InteractiveRandom(); - byte *a = &_cur_playlist[r & 0x1F]; - byte *b = &_cur_playlist[(r >> 8)&0x1F]; + byte *a = &_cur_playlist[GB(r, 0, 5)]; + byte *b = &_cur_playlist[GB(r, 8, 5)]; if (*a != 0 && *b != 0) { byte t = *a; diff --git a/network_data.c b/network_data.c index cbb0b6f04d..18b179fd0a 100644 --- a/network_data.c +++ b/network_data.c @@ -44,30 +44,30 @@ void NetworkSend_uint8(Packet *packet, uint8 data) void NetworkSend_uint16(Packet *packet, uint16 data) { assert(packet->size < sizeof(packet->buffer) - sizeof(data)); - packet->buffer[packet->size++] = data & 0xFF; - packet->buffer[packet->size++] = (data >> 8) & 0xFF; + packet->buffer[packet->size++] = GB(data, 0, 8); + packet->buffer[packet->size++] = GB(data, 8, 8); } void NetworkSend_uint32(Packet *packet, uint32 data) { assert(packet->size < sizeof(packet->buffer) - sizeof(data)); - packet->buffer[packet->size++] = data & 0xFF; - packet->buffer[packet->size++] = (data >>= 8) & 0xFF; - packet->buffer[packet->size++] = (data >>= 8) & 0xFF; - packet->buffer[packet->size++] = (data >> 8) & 0xFF; + packet->buffer[packet->size++] = GB(data, 0, 8); + packet->buffer[packet->size++] = GB(data, 8, 8); + packet->buffer[packet->size++] = GB(data, 16, 8); + packet->buffer[packet->size++] = GB(data, 24, 8); } void NetworkSend_uint64(Packet *packet, uint64 data) { assert(packet->size < sizeof(packet->buffer) - sizeof(data)); - packet->buffer[packet->size++] = data & 0xFF; - packet->buffer[packet->size++] = (data >>= 8) & 0xFF; - packet->buffer[packet->size++] = (data >>= 8) & 0xFF; - packet->buffer[packet->size++] = (data >>= 8) & 0xFF; - packet->buffer[packet->size++] = (data >>= 8) & 0xFF; - packet->buffer[packet->size++] = (data >>= 8) & 0xFF; - packet->buffer[packet->size++] = (data >>= 8) & 0xFF; - packet->buffer[packet->size++] = (data >> 8) & 0xFF; + packet->buffer[packet->size++] = GB(data, 0, 8); + packet->buffer[packet->size++] = GB(data, 8, 8); + packet->buffer[packet->size++] = GB(data, 16, 8); + packet->buffer[packet->size++] = GB(data, 24, 8); + packet->buffer[packet->size++] = GB(data, 32, 8); + packet->buffer[packet->size++] = GB(data, 40, 8); + packet->buffer[packet->size++] = GB(data, 48, 8); + packet->buffer[packet->size++] = GB(data, 56, 8); } // Sends a string over the network. It sends out diff --git a/oldloader.c b/oldloader.c index 26f5813b00..56ef7c51c5 100644 --- a/oldloader.c +++ b/oldloader.c @@ -722,8 +722,8 @@ static bool LoadOldStation(LoadgameState *ls, int num) if (st->xy != 0) { if (st->train_tile) { /* Calculate the trainst_w and trainst_h */ - int w = (_old_platforms >> 3) & 0x7; - int h = (_old_platforms & 0x7); + uint w = GB(_old_platforms, 3, 3); + uint h = GB(_old_platforms, 0, 3); st->trainst_w = w; st->trainst_h = h; } diff --git a/order.h b/order.h index 79a66cf6ef..1b0bdd18ad 100644 --- a/order.h +++ b/order.h @@ -135,9 +135,9 @@ static inline uint32 PackOrder(const Order *order) static inline Order UnpackOrder(uint32 packed) { Order order; - order.type = (packed & 0x000000FF); - order.flags = (packed & 0x0000FF00) >> 8; - order.station = (packed & 0xFFFF0000) >> 16; + order.type = GB(packed, 0, 8); + order.flags = GB(packed, 8, 8); + order.station = GB(packed, 16, 16); order.next = NULL; order.index = 0; // avoid compiler warning return order; diff --git a/order_cmd.c b/order_cmd.c index 2f0e43b39b..8ec6afb08d 100644 --- a/order_cmd.c +++ b/order_cmd.c @@ -40,9 +40,9 @@ MemoryPool _order_pool = { "Orders", ORDER_POOL_MAX_BLOCKS, ORDER_POOL_BLOCK_SIZ Order UnpackOldOrder(uint16 packed) { Order order; - order.type = (packed & 0x000F); - order.flags = (packed & 0x00F0) >> 4; - order.station = (packed & 0xFF00) >> 8; + order.type = GB(packed, 0, 4); + order.flags = GB(packed, 4, 4); + order.station = GB(packed, 8, 8); order.next = NULL; // Sanity check @@ -63,9 +63,9 @@ Order UnpackOldOrder(uint16 packed) Order UnpackVersion4Order(uint16 packed) { Order order; - order.type = (packed & 0x000F); - order.flags = (packed & 0x00F0) >> 4; - order.station = (packed & 0xFF00) >> 8; + order.type = GB(packed, 0, 4); + order.flags = GB(packed, 4, 4); + order.station = GB(packed, 8, 8); order.next = NULL; order.index = 0; // avoid compiler warning return order; @@ -144,17 +144,17 @@ void AssignOrder(Order *order, Order data) /** Add an order to the orderlist of a vehicle. * @param x,y unused * @param p1 various bitstuffed elements - * - p1 = (bit 0 - 15) - ID of the vehicle (p1 & 0xFFFF) + * - p1 = (bit 0 - 15) - ID of the vehicle * - p1 = (bit 16 - 31) - the selected order (if any). If the last order is given, - * the order will be inserted before that one (p1 & 0xFFFF0000)>>16 - * only the first 8 bytes used currently (bit 16 - 23) (max 255) + * the order will be inserted before that one + * only the first 8 bits used currently (bit 16 - 23) (max 255) * @param p2 packed order to insert */ int32 CmdInsertOrder(int x, int y, uint32 flags, uint32 p1, uint32 p2) { Vehicle *v; - VehicleID veh = p1 & 0xFFFF; - OrderID sel_ord = p1 >> 16; + VehicleID veh = GB(p1, 0, 16); + OrderID sel_ord = GB(p1, 16, 16); Order new_order = UnpackOrder(p2); if (!IsVehicleIndex(veh)) return CMD_ERROR; @@ -509,18 +509,18 @@ int32 CmdSkipOrder(int x, int y, uint32 flags, uint32 p1, uint32 p2) /** Modify an order in the orderlist of a vehicle. * @param x,y unused * @param p1 various bitstuffed elements - * - p1 = (bit 0 - 15) - ID of the vehicle (p1 & 0xFFFF) + * - p1 = (bit 0 - 15) - ID of the vehicle * - p1 = (bit 16 - 31) - the selected order (if any). If the last order is given, - * the order will be inserted before that one (p1 & 0xFFFF0000)>>16 - * only the first 8 bytes used currently (bit 16 - 23) (max 255) + * the order will be inserted before that one + * only the first 8 bits used currently (bit 16 - 23) (max 255) * @param p2 mode to change the order to (always set) */ int32 CmdModifyOrder(int x, int y, uint32 flags, uint32 p1, uint32 p2) { Vehicle *v; Order *order; - OrderID sel_ord = p1 >> 16; // XXX - automatically truncated to 8 bits. - VehicleID veh = p1 & 0xFFFF; + OrderID sel_ord = GB(p1, 16, 16); // XXX - automatically truncated to 8 bits. + VehicleID veh = GB(p1, 0, 16); if (!IsVehicleIndex(veh)) return CMD_ERROR; if (p2 != OFB_FULL_LOAD && p2 != OFB_UNLOAD && p2 != OFB_NON_STOP && p2 != OFB_TRANSFER) return CMD_ERROR; @@ -583,8 +583,8 @@ int32 CmdModifyOrder(int x, int y, uint32 flags, uint32 p1, uint32 p2) int32 CmdCloneOrder(int x, int y, uint32 flags, uint32 p1, uint32 p2) { Vehicle *dst; - VehicleID veh_src = (p1 >> 16) & 0xFFFF; - VehicleID veh_dst = p1 & 0xFFFF; + VehicleID veh_src = GB(p1, 16, 16); + VehicleID veh_dst = GB(p1, 0, 16); if (!IsVehicleIndex(veh_dst)) return CMD_ERROR; diff --git a/players.c b/players.c index b8cfc53c91..0ee4060184 100644 --- a/players.c +++ b/players.c @@ -45,7 +45,7 @@ void DrawPlayerFace(uint32 face, int color, int x, int y) /* draw the chin */ /* FIXME: real code uses -2 in zoomlevel 1 */ { - uint val = (face >> 4) & 3; + uint val = GB(face, 4, 2); if (!(flag & 2)) { DrawSprite(0x327 + (flag&1?0:val), x, y); } else { @@ -54,8 +54,8 @@ void DrawPlayerFace(uint32 face, int color, int x, int y) } /* draw the eyes */ { - uint val1 = (face >> 6)&15; - uint val2 = (face >> 20)&7; + uint val1 = GB(face, 6, 4); + uint val2 = GB(face, 20, 3); uint32 high = 0x314<<16; if (val2 >= 6) { @@ -81,7 +81,7 @@ void DrawPlayerFace(uint32 face, int color, int x, int y) /* draw the mouth */ { - uint val = (face >> 10) & 63; + uint val = GB(face, 10, 6); uint val2; if (!(flag&1)) { @@ -127,7 +127,7 @@ void DrawPlayerFace(uint32 face, int color, int x, int y) /* draw the hair */ { - uint val = (face >> 16) & 15; + uint val = GB(face, 16, 4); if (!(flag&2)) { if (!(flag&1)) { DrawSprite(0x382 + (val*9>>4), x, y); @@ -145,7 +145,7 @@ void DrawPlayerFace(uint32 face, int color, int x, int y) /* draw the tie */ { - uint val = (face >> 20) & 0xFF; + uint val = GB(face, 20, 8); if (!(flag&1)) { DrawSprite(0x36B + ((val&3)*3>>2), x, y); @@ -164,7 +164,7 @@ void DrawPlayerFace(uint32 face, int color, int x, int y) /* draw the glasses */ { - uint val = (face >> 28) & 7; + uint val = GB(face, 28, 3); if (!(flag&2)) { if (val<=1) { @@ -371,7 +371,7 @@ static byte GeneratePlayerColor(void) n = 100; do { r = Random(); - COLOR_SWAP(r & 0xF, (r >> 4) & 0xF); + COLOR_SWAP(GB(r, 0, 4), GB(r, 4, 4)); } while (--n); // Bubble sort it according to the values in table 1 @@ -754,8 +754,8 @@ int32 CmdPlayerCtrl(int x, int y, uint32 flags, uint32 p1, uint32 p2) } break; case 3: { /* Merge a company (#1) into another company (#2), elimination company #1 */ - PlayerID pid_old = p2 & 0xFFFF; - PlayerID pid_new = (p2 >> 16) & 0xFFFF; + PlayerID pid_old = GB(p2, 0, 16); + PlayerID pid_new = GB(p2, 16, 16); if (pid_old >= MAX_PLAYERS || pid_new >= MAX_PLAYERS) return CMD_ERROR; diff --git a/rail_cmd.c b/rail_cmd.c index f665dbdc09..33e9255cd6 100644 --- a/rail_cmd.c +++ b/rail_cmd.c @@ -577,7 +577,7 @@ static int32 CmdRailTrackHelper(int x, int y, uint32 flags, uint32 p1, uint32 p2 { int ex, ey; int32 ret, total_cost = 0; - Track track = (Track)((p2 >> 4) & 7); + Track track = (Track)GB(p2, 4, 3); Trackdir trackdir; byte mode = HASBIT(p2, 7); @@ -845,7 +845,7 @@ static int32 CmdSignalTrackHelper(int x, int y, uint32 flags, uint32 p1, uint32 bool error = true; int mode = p2 & 0x1; - Track track = (p2 >> 4) & 7; + Track track = GB(p2, 4, 3); Trackdir trackdir = TrackToTrackdir(track); byte semaphores = (HASBIT(p2, 3)) ? 8 : 0; byte signal_density = (p2 >> 24); diff --git a/road_cmd.c b/road_cmd.c index cf1d96cbdc..470b9f22d9 100644 --- a/road_cmd.c +++ b/road_cmd.c @@ -789,7 +789,7 @@ static void DrawTile_Road(TileInfo *ti) image = _road_tile_sprites_1[ti->map5 & 0xF]; } - m2 = (_m[ti->tile].m4 & 0x70) >> 4; + m2 = GB(_m[ti->tile].m4, 4, 3); if (m2 == 0) image |= 0x3178000; @@ -835,7 +835,7 @@ static void DrawTile_Road(TileInfo *ti) if ( _m[ti->tile].m4 & 0x80) { image += 8; } else { - m2 = (_m[ti->tile].m4 & 0x70) >> 4; + m2 = GB(_m[ti->tile].m4, 4, 3); if (m2 == 0) image |= 0x3178000; if (m2 > 1) image += 4; } @@ -1006,7 +1006,7 @@ static void TileLoop_Road(TileIndex tile) if (_m[tile].m5 & 0xE0) return; - if (((_m[tile].m4 & 0x70) >> 4) < 6) { + if (GB(_m[tile].m4, 4, 3) < 6) { t = ClosestTownFromTile(tile, (uint)-1); grp = 0; @@ -1018,7 +1018,7 @@ static void TileLoop_Road(TileIndex tile) !(DistanceManhattan(t->xy, tile) >= 8 && grp == 0) && (_m[tile].m5==5 || _m[tile].m5==10)) { if (GetTileSlope(tile, NULL) == 0 && EnsureNoVehicle(tile) && CHANCE16(1,20)) { - _m[tile].m4 |= ((((_m[tile].m4 & 0x70) >> 4 ) <= 2) ? 7 : 6) << 4; + _m[tile].m4 |= (GB(_m[tile].m4, 4, 3) <= 2 ? 7 : 6) << 4; SndPlayTileFx(SND_21_JACKHAMMER, tile); CreateEffectVehicleAbove( @@ -1034,7 +1034,7 @@ static void TileLoop_Road(TileIndex tile) { const byte *p = (_opt.landscape == LT_CANDY) ? _town_road_types_2[grp] : _town_road_types[grp]; - byte b = (_m[tile].m4 & 0x70) >> 4; + byte b = GB(_m[tile].m4, 4, 3); if (b == p[0]) return; @@ -1062,7 +1062,7 @@ static void TileLoop_Road(TileIndex tile) return; } //roadworks finished - _m[tile].m4 = ((((b& 0x70) >> 4)== 6) ? 1 : 2) << 4; + _m[tile].m4 = (GB(b, 4, 3) == 6 ? 1 : 2) << 4; MarkTileDirtyByTile(tile); } } @@ -1090,7 +1090,7 @@ static uint32 GetTileTrackStatus_Road(TileIndex tile, TransportType mode) byte b = _m[tile].m5; if ((b & 0xF0) == 0) { /* Ordinary road */ - if (!_road_special_gettrackstatus && ((_m[tile].m4&0x70) >> 4) >= 6) + if (!_road_special_gettrackstatus && GB(_m[tile].m4, 4, 3) >= 6) return 0; return _road_trackbits[b&0xF] * 0x101; } else if (IsLevelCrossing(tile)) { @@ -1125,7 +1125,7 @@ static void GetTileDesc_Road(TileIndex tile, TileDesc *td) { int i = (_m[tile].m5 >> 4); if (i == 0) - i = ((_m[tile].m4 & 0x70) >> 4) + 3; + i = GB(_m[tile].m4, 4, 3) + 3; td->str = _road_tile_strings[i - 1]; td->owner = GetTileOwner(tile); } diff --git a/settings_gui.c b/settings_gui.c index fc6c00462f..a2ef471cf4 100644 --- a/settings_gui.c +++ b/settings_gui.c @@ -1019,16 +1019,16 @@ static void PatchesSelectionWndProc(Window *w, WindowEvent *e) /** Network-safe changing of patch-settings. * @param p1 various bitstuffed elements - * - p1 = (bit 0- 7) - the patches type (page) that is being changed (construction, network, ai) (p1 & 0xFF) - * - p2 = (bit 8-15) - the actual patch (entry) being set inside the category ((p1>>8) & 0xFF) + * - p1 = (bit 0- 7) - the patches type (page) that is being changed (construction, network, ai) + * - p2 = (bit 8-15) - the actual patch (entry) being set inside the category * @param p2 the new value for the patch * @todo check that the new value is a valid one. Awful lot of work, but since only * the server is allowed to do this, we trust it on this one :) */ int32 CmdChangePatchSetting(int x, int y, uint32 flags, uint32 p1, uint32 p2) { - byte pcat = p1 & 0xFF; - byte pel = (p1 >> 8) & 0xFF; + byte pcat = GB(p1, 0, 8); + byte pel = GB(p1, 8, 8); if (pcat >= lengthof(_patches_page)) return CMD_ERROR; if (pel >= _patches_page[pcat].num) return CMD_ERROR; diff --git a/station_cmd.c b/station_cmd.c index 1e0d2af44c..c76965c510 100644 --- a/station_cmd.c +++ b/station_cmd.c @@ -933,8 +933,8 @@ static void GetStationLayout(byte *layout, int numtracks, int plat_len, StationS * @param x,y starting position of station dragging/placement * @param p1 various bitstuffed elements * - p1 = (bit 0) - orientation (p1 & 1) - * - p1 = (bit 8-15) - number of tracks (p1 >> 8) & 0xFF) - * - p1 = (bit 16-23) - platform length (p1 >> 16) & 0xFF) + * - p1 = (bit 8-15) - number of tracks + * - p1 = (bit 16-23) - platform length * @param p2 various bitstuffed elements * - p2 = (bit 0- 3) - railtype (p2 & 0xF) * - p2 = (bit 4) - set for custom station (p2 & 0x10) @@ -961,8 +961,8 @@ int32 CmdBuildRailroadStation(int x, int y, uint32 flags, uint32 p1, uint32 p2) /* unpack parameters */ direction = p1 & 1; - numtracks = (p1 >> 8) & 0xFF; - plat_len = (p1 >> 16) & 0xFF; + numtracks = GB(p1, 8, 8); + plat_len = GB(p1, 16, 8); /* w = length, h = num_tracks */ if (direction) { h_org = plat_len; diff --git a/strings.c b/strings.c index e73969332b..7562909402 100644 --- a/strings.c +++ b/strings.c @@ -179,8 +179,8 @@ static const char *GetStringPtr(StringID string) // should set those bits. char *GetStringWithArgs(char *buffr, uint string, const int32 *argv) { - uint index = string & 0x7FF; - uint tab = (string >> 11) & 0x1F; + uint index = GB(string, 0, 11); + uint tab = GB(string, 11, 5); if (!(string & 0xFFFF)) { error("!invalid string id 0 in GetString"); diff --git a/town_cmd.c b/town_cmd.c index 56413df667..1e1daf6fec 100644 --- a/town_cmd.c +++ b/town_cmd.c @@ -769,9 +769,10 @@ static int GrowTownAtRoad(Town *t, TileIndex tile) static int GenRandomRoadBits(void) { uint32 r = Random(); - int a = r&3, b = (r >> 8) & 3; + uint a = GB(r, 0, 2); + uint b = GB(r, 8, 2); if (a == b) b ^= 2; - return (1<> 16) & 0x3F; + m5 = GB(r, 16, 6); } assert(IsTileType(tile, MP_CLEAR)); diff --git a/train_cmd.c b/train_cmd.c index 5a5eb70fff..35a3c82502 100644 --- a/train_cmd.c +++ b/train_cmd.c @@ -3013,9 +3013,9 @@ static void HandleCrashedTrain(Vehicle *v) r = Random(); CreateEffectVehicleRel(u, - 2 + ((r>>8)&7), - 2 + ((r>>16)&7), - 5 + (r&7), + GB(r, 8, 3) + 2, + GB(r, 16, 3) + 2, + GB(r, 0, 3) + 5, EV_EXPLOSION_SMALL); break; } diff --git a/tunnelbridge_cmd.c b/tunnelbridge_cmd.c index 8da2c779c2..e4c59dc864 100644 --- a/tunnelbridge_cmd.c +++ b/tunnelbridge_cmd.c @@ -184,8 +184,8 @@ int32 CmdBuildBridge(int x, int y, uint32 flags, uint32 p1, uint32 p2) SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); /* unpack parameters */ - bridge_type = p2 & 0xFF; - railtype = (byte)(p2 >> 8); + bridge_type = GB(p2, 0, 8); + railtype = GB(p2, 8, 8); if (p1 > MapSize()) return CMD_ERROR; @@ -1023,8 +1023,8 @@ static void DrawTile_TunnelBridge(TileInfo *ti) if (ice) image += 32; - image += _draw_tunnel_table_1[(ti->map5 >> 2) & 0x3]; - image += (ti->map5 & 3) << 1; + image += _draw_tunnel_table_1[GB(ti->map5, 2, 2)]; + image += GB(ti->map5, 0, 2) * 2; DrawGroundSprite(image); AddSortableSpriteToDraw(image+1, ti->x + 15, ti->y + 15, 1, 1, 8, (byte)ti->z); @@ -1055,7 +1055,7 @@ static void DrawTile_TunnelBridge(TileInfo *ti) } // bridge ending. - b = _bridge_sprite_table[(_m[ti->tile].m2 >> 4) & 0xF][6]; + b = _bridge_sprite_table[GB(_m[ti->tile].m2, 4, 4)][6]; b += (tmp&(3<<1))*4; /* actually ((tmp>>2)&3)*8 */ b += (tmp&1); // direction if (ti->tileh == 0) b += 4; // sloped "entrance" ? @@ -1077,7 +1077,7 @@ static void DrawTile_TunnelBridge(TileInfo *ti) uint z; int x,y; - image = (ti->map5 >> 3) & 3; // type of stuff under bridge (only defined for 0,1) + image = GB(ti->map5, 3, 2); // type of stuff under bridge (only defined for 0,1) assert(image <= 1); if (!(ti->map5 & 0x20)) { @@ -1115,7 +1115,7 @@ static void DrawTile_TunnelBridge(TileInfo *ti) DrawGroundSprite(image); } // get bridge sprites - b = _bridge_sprite_table[(_m[ti->tile].m2 >> 4) & 0xF][_m[ti->tile].m2&0xF] + tmp * 4; + b = _bridge_sprite_table[GB(_m[ti->tile].m2, 4, 4)][GB(_m[ti->tile].m2, 0, 4)] + tmp * 4; z = GetBridgeHeight(ti) + 5; @@ -1285,9 +1285,9 @@ static const StringID _bridge_tile_str[(MAX_BRIDGES + 3) + (MAX_BRIDGES + 3)] = static void GetTileDesc_TunnelBridge(TileIndex tile, TileDesc *td) { if ((_m[tile].m5 & 0x80) == 0) { - td->str = STR_5017_RAILROAD_TUNNEL + ((_m[tile].m5 >> 2) & 3); + td->str = STR_5017_RAILROAD_TUNNEL + GB(_m[tile].m5, 2, 2); } else { - td->str = _bridge_tile_str[ (_m[tile].m2 >> 4) + (((_m[tile].m5>>1)&3)<<4) ]; + td->str = _bridge_tile_str[GB(_m[tile].m5, 1, 2) << 4 | GB(_m[tile].m2, 4, 4)]; /* scan to the end of the bridge, that's where the owner is stored */ if (_m[tile].m5 & 0x40) { diff --git a/vehicle.c b/vehicle.c index d12ba127f0..7d1734f702 100644 --- a/vehicle.c +++ b/vehicle.c @@ -612,8 +612,8 @@ void ViewportAddVehicles(DrawPixelInfo *dpi) static void ChimneySmokeInit(Vehicle *v) { uint32 r = Random(); - v->cur_image = SPR_CHIMNEY_SMOKE_0 + (r & 7); - v->progress = (r >> 16) & 7; + v->cur_image = SPR_CHIMNEY_SMOKE_0 + GB(r, 0, 3); + v->progress = GB(r, 16, 3); } static void ChimneySmokeTick(Vehicle *v) @@ -1299,8 +1299,8 @@ void CheckVehicleBreakdown(Vehicle *v) /* check if to break down */ if (_breakdown_chance[(uint)min(rel, 0xffff) >> 10] <= v->breakdown_chance) { - v->breakdown_ctr = (byte)(((r >> 16) & 0x3F) + 0x3F); - v->breakdown_delay = (byte)(((r >> 24) & 0x7F) | 0x80); + v->breakdown_ctr = GB(r, 16, 6) + 0x3F; + v->breakdown_delay = GB(r, 24, 7) + 0x80; v->breakdown_chance = 0; } } diff --git a/win32.c b/win32.c index 09c9946718..d3dfab0ebc 100644 --- a/win32.c +++ b/win32.c @@ -2142,8 +2142,8 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, #if defined(_MSC_VER) { uint64 seed = rdtsc(); - _random_seeds[0][0] = seed & 0xffffffff; - _random_seeds[0][1] = seed >> 32; + _random_seeds[0][0] = GB(seed, 0, 32); + _random_seeds[0][1] = GB(seed, 32, 32); } #else _random_seeds[0][0] = GetTickCount();