Codechange: Introduce an enum to remove some magic numbers from the visual effect code. diff -r d1c7bd5fafc6 src/train_cmd.cpp --- a/src/train_cmd.cpp Tue Nov 09 21:24:28 2010 +0100 +++ b/src/train_cmd.cpp Wed Nov 10 16:49:04 2010 +0100 @@ -144,7 +144,8 @@ */ void Train::UpdateVisualEffect(bool allow_power_change) { - byte powered_before = this->vcache.cached_vis_effect & 0x80; + bool powered_before = HasBit(this->vcache.cached_vis_effect, VE_DISABLE_WAGON_POWER); + this->vcache.cached_vis_effect = 0; const Engine *e = Engine::Get(this->engine_type); if (e->u.rail.visual_effect != 0) { @@ -152,13 +153,13 @@ } else { if (this->IsWagon() || this->IsArticulatedPart()) { /* Wagons and articulated parts have no effect by default */ - this->vcache.cached_vis_effect = 0x40; + SetBit(this->vcache.cached_vis_effect, VE_DISABLE_EFFECT); } else if (e->u.rail.engclass == 0) { /* Steam is offset by -4 units */ - this->vcache.cached_vis_effect = 4; + SB(this->vcache.cached_vis_effect, VE_OFFSET_START, VE_OFFSET_COUNT, VE_OFFSET_CENTRE - 4); } else { /* Diesel fumes and sparks come from the centre */ - this->vcache.cached_vis_effect = 8; + SB(this->vcache.cached_vis_effect, VE_OFFSET_START, VE_OFFSET_COUNT, VE_OFFSET_CENTRE); } } @@ -169,8 +170,8 @@ if (callback != CALLBACK_FAILED) this->vcache.cached_vis_effect = GB(callback, 0, 8); } - if (!allow_power_change && powered_before != (this->vcache.cached_vis_effect & 0x80)) { - this->vcache.cached_vis_effect ^= 0x80; + if (!allow_power_change && powered_before != HasBit(this->vcache.cached_vis_effect, VE_DISABLE_WAGON_POWER)) { + ToggleBit(this->vcache.cached_vis_effect, VE_DISABLE_WAGON_POWER); ShowNewGrfVehicleError(this->engine_type, STR_NEWGRF_BROKEN, STR_NEWGRF_BROKEN_POWERED_WAGON, GBUG_VEH_POWERED_WAGON, false); } } @@ -235,7 +236,7 @@ u->UpdateVisualEffect(true); if (rvi_v->pow_wag_power != 0 && rvi_u->railveh_type == RAILVEH_WAGON && - UsesWagonOverride(u) && !HasBit(u->vcache.cached_vis_effect, 7)) { + UsesWagonOverride(u) && !HasBit(u->vcache.cached_vis_effect, VE_DISABLE_WAGON_POWER)) { /* wagon is powered */ SetBit(u->flags, VRF_POWEREDWAGON); // cache 'powered' status } else { @@ -1945,9 +1946,9 @@ do { const RailVehicleInfo *rvi = RailVehInfo(v->engine_type); - int effect_offset = GB(v->vcache.cached_vis_effect, 0, 4) - 8; - byte effect_type = GB(v->vcache.cached_vis_effect, 4, 2); - bool disable_effect = HasBit(v->vcache.cached_vis_effect, 6); + int effect_offset = GB(v->vcache.cached_vis_effect, VE_OFFSET_START, VE_OFFSET_COUNT) - VE_OFFSET_CENTRE; + byte effect_type = GB(v->vcache.cached_vis_effect, VE_TYPE_START, VE_TYPE_COUNT); + bool disable_effect = HasBit(v->vcache.cached_vis_effect, VE_DISABLE_EFFECT); /* no smoke? */ if ((rvi->railveh_type == RAILVEH_WAGON && effect_type == 0) || @@ -1962,11 +1963,9 @@ /* No sparks for electric vehicles on non-electrified tracks. */ if (!HasPowerOnRail(v->railtype, GetTileRailType(v->tile))) continue; - if (effect_type == 0) { + if (effect_type == VE_TYPE_DEFAULT) { /* Use default effect type for engine class. */ - effect_type = rvi->engclass; - } else { - effect_type--; + effect_type = rvi->engclass + 1; } int x = _vehicle_smoke_pos[v->direction] * effect_offset; @@ -1978,7 +1977,7 @@ } switch (effect_type) { - case 0: + case VE_TYPE_STEAM: /* Steam smoke - amount is gradually falling until train reaches its maximum speed, after that it's normal. * Details: while train's current speed is gradually increasing, steam plumes' density decreases by one third each * third of its maximum speed spectrum. Steam emission finally normalises at very close to train's maximum speed. @@ -1990,7 +1989,7 @@ } break; - case 1: + case VE_TYPE_DIESEL: /* Diesel smoke - thicker when train is starting, gradually subsiding till locomotive reaches its maximum speed * when it stops. * Details: Train's (max.) speed spectrum is divided into 32 parts. When max. speed is reached, chance for smoke @@ -2009,7 +2008,7 @@ } break; - case 2: + case VE_TYPE_ELECTRIC: /* Electric train's spark - more often occurs when train is departing (more load) * Details: Electric locomotives are usually at least twice as powerful as their diesel counterparts, so spark * emissions are kept simple. Only when starting, creating huge force are sparks more likely to happen, but when diff -r d1c7bd5fafc6 src/vehicle_base.h --- a/src/vehicle_base.h Tue Nov 09 21:24:28 2010 +0100 +++ b/src/vehicle_base.h Wed Nov 10 16:49:04 2010 +0100 @@ -64,17 +64,28 @@ uint8 cache_valid; ///< Bitset that indicates which cache values are valid. }; +/** Meaning of the various bits of the visual effect. */ +enum VisualEffect { + VE_OFFSET_START = 0, ///< First bit that contains the offset (0 = front, 8 = centre, 15 = rear) + VE_OFFSET_COUNT = 4, ///< Number of bits used for the offset + VE_OFFSET_CENTRE = 8, ///< Value of offset corresponding to a position above the centre of the vehicle + + VE_TYPE_START = 4, ///< First bit used for the type of effect + VE_TYPE_COUNT = 2, ///< Number of bits used for the effect type + VE_TYPE_DEFAULT = 0, ///< Use default from engine class + VE_TYPE_STEAM = 1, ///< Steam plumes + VE_TYPE_DIESEL = 2, ///< Diesel fumes + VE_TYPE_ELECTRIC = 3, ///< Electric sparks + + VE_DISABLE_EFFECT = 6, ///< Flag to disable visual effect + VE_DISABLE_WAGON_POWER = 7, ///< Flag to disable wagon power +}; + /** Cached often queried values common to all vehicles. */ struct VehicleCache { uint16 cached_max_speed; ///< Maximum speed of the consist (minimum of the max speed of all vehicles in the consist). - /** - * Position/type of visual effect. - * bit 0 - 3 = position of effect relative to vehicle. (0 = front, 8 = centre, 15 = rear) - * bit 4 - 5 = type of effect. (0 = default for engine class, 1 = steam, 2 = diesel, 3 = electric) - * bit 6 = disable visual effect. - * bit 7 = disable powered wagons (trains only). - */ - byte cached_vis_effect; + + byte cached_vis_effect; ///< Visual effect to show (see #VisualEffect) }; /** A vehicle pool for a little over 1 million vehicles. */