(svn r2660) Get rid of some more shifting/anding/casting
authortron
Thu, 21 Jul 2005 06:31:02 +0000
changeset 2150 010d923a81a9
parent 2149 e5ae969b26b9
child 2151 10e1a9e63531
(svn r2660) Get rid of some more shifting/anding/casting
clear_cmd.c
economy.c
industry_cmd.c
landscape.c
main_gui.c
misc_cmd.c
news_gui.c
players.c
roadveh_cmd.c
saveload.c
ship_cmd.c
strings.c
town_cmd.c
train_cmd.c
tunnelbridge_cmd.c
unmovable_cmd.c
--- a/clear_cmd.c	Thu Jul 21 06:13:17 2005 +0000
+++ b/clear_cmd.c	Thu Jul 21 06:31:02 2005 +0000
@@ -742,7 +742,7 @@
 			}
 			/* did overflow, so continue */
 		} else {
-			m5 = ((byte)Random() > 21) ? (2) : (6);
+			m5 = (GB(Random(), 0, 8) > 21) ? 2 : 6;
 		}
 		m5++;
 	} else if (_game_mode != GM_EDITOR) {
@@ -767,29 +767,27 @@
 
 void GenerateClearTile(void)
 {
-	int i,j;
+	uint i;
 	TileIndex tile;
-	uint32 r;
 
 	/* add hills */
 	i = ScaleByMapSize((Random() & 0x3FF) + 0x400);
 	do {
 		tile = RandomTile();
-		if (IsTileType(tile, MP_CLEAR))
-			_m[tile].m5 = (byte)((_m[tile].m5 & ~(3<<2)) | (1<<2));
+		if (IsTileType(tile, MP_CLEAR)) SB(_m[tile].m5, 2, 2, 1);
 	} while (--i);
 
 	/* add grey squares */
 	i = ScaleByMapSize((Random() & 0x7F) + 0x80);
 	do {
-		r = Random();
+		uint32 r = Random();
 		tile = RandomTileSeed(r);
 		if (IsTileType(tile, MP_CLEAR)) {
-			j = GB(r, 16, 4) + 5;
+			uint j = GB(r, 16, 4) + 5;
 			for(;;) {
 				TileIndex tile_new;
 
-				_m[tile].m5 = (byte)((_m[tile].m5 & ~(3<<2)) | (2<<2));
+				SB(_m[tile].m5, 2, 2, 2);
 				do {
 					if (--j == 0) goto get_out;
 					tile_new = tile + TileOffsByDir(Random() & 3);
--- a/economy.c	Thu Jul 21 06:13:17 2005 +0000
+++ b/economy.c	Thu Jul 21 06:31:02 2005 +0000
@@ -787,7 +787,7 @@
 	_economy.infl_amount = _opt.diff.initial_interest;
 	_economy.infl_amount_pr = max(0, _opt.diff.initial_interest - 1);
 	_economy.max_loan_unround = _economy.max_loan = _opt.diff.max_loan * 1000;
-	_economy.fluct = (byte)(Random()) + 168;
+	_economy.fluct = GB(Random(), 0, 8) + 168;
 }
 
 Pair SetupSubsidyDecodeParam(Subsidy *s, bool mode)
--- a/industry_cmd.c	Thu Jul 21 06:13:17 2005 +0000
+++ b/industry_cmd.c	Thu Jul 21 06:31:02 2005 +0000
@@ -960,8 +960,8 @@
 	/* determine field size */
 	r = (Random() & 0x303) + 0x404;
 	if (_opt.landscape == LT_HILLY) r += 0x404;
-	size_x = (byte)r;
-	size_y = r >> 8;
+	size_x = GB(r, 0, 8);
+	size_y = GB(r, 8, 8);
 
 	/* offset tile to match size */
 	tile -= TileDiffXY(size_x / 2, size_y / 2);
@@ -977,7 +977,7 @@
 	/* determine type of field */
 	r = Random();
 	type = ((r & 0xE0) | 0xF);
-	type2 = ((byte)(r >> 8) * 9) >> 8;
+	type2 = GB(r, 8, 8) * 9 >> 8;
 
 	/* make field */
 	BEGIN_TILE_LOOP(cur_tile, size_x, size_y, tile)
--- a/landscape.c	Thu Jul 21 06:13:17 2005 +0000
+++ b/landscape.c	Thu Jul 21 06:31:02 2005 +0000
@@ -685,8 +685,8 @@
 	uint32 r = Random();
 
 	return TILE_MASK(TileXY(
-		TileX(a) + ((byte)r * rn * 2 >> 8) - rn,
-		TileY(a) + ((byte)(r >> 8) * rn * 2 >> 8) - rn
+		TileX(a) + (GB(r, 0, 8) * rn * 2 >> 8) - rn,
+		TileY(a) + (GB(r, 8, 8) * rn * 2 >> 8) - rn
 	));
 }
 
--- a/main_gui.c	Thu Jul 21 06:13:17 2005 +0000
+++ b/main_gui.c	Thu Jul 21 06:31:02 2005 +0000
@@ -502,7 +502,7 @@
 
 		do {
 			if (sel== 0) GfxFillRect(x, y, x + eo, y+9, 0);
-			DrawString(x + 2, y, (StringID)(string + (chk&1)), (byte)(sel==0?(byte)0xC:(byte)0x10));
+			DrawString(x + 2, y, string + (chk & 1), sel == 0 ? 0xC : 0x10);
 			y += 10;
 			string += inc;
 			chk >>= 1;
@@ -642,7 +642,7 @@
 				SetDParam(1, p->name_2);
 				SetDParam(2, GetPlayerNameString(p->index, 3));
 
-				color = (byte)((p->index==sel) ? 0xC : 0x10);
+				color = (p->index == sel) ? 0xC : 0x10;
 				if (chk&1) color = 14;
 				DrawString(x + 19, y, STR_7021, color);
 
--- a/misc_cmd.c	Thu Jul 21 06:13:17 2005 +0000
+++ b/misc_cmd.c	Thu Jul 21 06:31:02 2005 +0000
@@ -32,9 +32,10 @@
 int32 CmdSetPlayerColor(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
 	Player *p, *pp;
-	byte colour = (byte)p2;
+	byte colour;
 
 	if (p2 >= 16) return CMD_ERROR; // max 16 colours
+	colour = p2;
 
 	p = GetPlayer(_current_player);
 
--- a/news_gui.c	Thu Jul 21 06:13:17 2005 +0000
+++ b/news_gui.c	Thu Jul 21 06:31:02 2005 +0000
@@ -579,7 +579,7 @@
 	/* Copy the just gotten string to another buffer to remove any formatting
 	 * from it such as big fonts, etc. */
 	for (ptr = buffer, dest = buffer2; *ptr != '\0'; ptr++) {
-		if ((byte)*ptr == '\r') {
+		if (*ptr == '\r') {
 			dest[0] = dest[1] = dest[2] = dest[3] = ' ';
 			dest += 4;
 		} else if ((byte)*ptr >= ' ' && ((byte)*ptr < 0x88 || (byte)*ptr >= 0x99)) {
--- a/players.c	Thu Jul 21 06:13:17 2005 +0000
+++ b/players.c	Thu Jul 21 06:31:02 2005 +0000
@@ -180,10 +180,9 @@
 
 void InvalidatePlayerWindows(Player *p)
 {
-	uint pid = p->index;
-	if ( (byte)pid == _local_player)
-		InvalidateWindow(WC_STATUS_BAR, 0);
+	PlayerID pid = p->index;
 
+	if (pid == _local_player) InvalidateWindow(WC_STATUS_BAR, 0);
 	InvalidateWindow(WC_FINANCES, pid);
 }
 
--- a/roadveh_cmd.c	Thu Jul 21 06:13:17 2005 +0000
+++ b/roadveh_cmd.c	Thu Jul 21 06:31:02 2005 +0000
@@ -449,10 +449,10 @@
 	};
 #undef MKIT
 	uint32 x = _delta_xy_table[v->direction];
-	v->x_offs = (byte)x;
-	v->y_offs = (byte)(x>>=8);
-	v->sprite_width = (byte)(x>>=8);
-	v->sprite_height = (byte)(x>>=8);
+	v->x_offs        = GB(x,  0, 8);
+	v->y_offs        = GB(x,  8, 8);
+	v->sprite_width  = GB(x, 16, 8);
+	v->sprite_height = GB(x, 24, 8);
 }
 
 static void ClearCrashedStation(Vehicle *v)
@@ -927,7 +927,7 @@
 	if (v->u.road.state >= 32 || (v->u.road.state&7) > 1 )
 		return;
 
-	tt = (byte)(GetTileTrackStatus(v->tile, TRANSPORT_ROAD) & 0x3F);
+	tt = GetTileTrackStatus(v->tile, TRANSPORT_ROAD) & 0x3F;
 	if ((tt & 3) == 0)
 		return;
 	if ((tt & 0x3C) != 0)
--- a/saveload.c	Thu Jul 21 06:13:17 2005 +0000
+++ b/saveload.c	Thu Jul 21 06:31:02 2005 +0000
@@ -123,14 +123,14 @@
 
 static inline void SlWriteUint16(uint16 v)
 {
-	SlWriteByte((byte)(v >> 8));
-	SlWriteByte((byte)v);
+	SlWriteByte(GB(v, 8, 8));
+	SlWriteByte(GB(v, 0, 8));
 }
 
 static inline void SlWriteUint32(uint32 v)
 {
-	SlWriteUint16((uint16)(v >> 16));
-	SlWriteUint16((uint16)v);
+	SlWriteUint16(GB(v, 16, 16));
+	SlWriteUint16(GB(v,  0, 16));
 }
 
 static inline void SlWriteUint64(uint64 x)
--- a/ship_cmd.c	Thu Jul 21 06:13:17 2005 +0000
+++ b/ship_cmd.c	Thu Jul 21 06:31:02 2005 +0000
@@ -312,10 +312,10 @@
 	};
 #undef MKIT
 	uint32 x = _delta_xy_table[dir];
-	v->x_offs = (byte)x;
-	v->y_offs = (byte)(x>>=8);
-	v->sprite_width = (byte)(x>>=8);
-	v->sprite_height = (byte)(x>>=8);
+	v->x_offs        = GB(x,  0, 8);
+	v->y_offs        = GB(x,  8, 8);
+	v->sprite_width  = GB(x, 16, 8);
+	v->sprite_height = GB(x, 24, 8);
 }
 
 static void RecalcShipStuff(Vehicle *v)
@@ -532,7 +532,7 @@
 
 			/* if we reach this position, there's two paths of equal value so far.
 			 * pick one randomly. */
-			r = (byte)Random();
+			r = GB(Random(), 0, 8);
 			if (_pick_shiptrack_table[i] == ship_dir) r += 80;
 			if (_pick_shiptrack_table[best_track] == ship_dir) r -= 80;
 			if (r <= 127) goto bad;
--- a/strings.c	Thu Jul 21 06:13:17 2005 +0000
+++ b/strings.c	Thu Jul 21 06:31:02 2005 +0000
@@ -893,12 +893,12 @@
 {
 	uint i, base, num;
 
-	buff[0] = _initial_name_letters[(sizeof(_initial_name_letters) * (byte)x) >> 8];
+	buff[0] = _initial_name_letters[sizeof(_initial_name_letters) * GB(x, 0, 8) >> 8];
 	buff[1] = '.';
 	buff[2] = ' '; // Insert a space after initial and period "I. Firstname" instead of "I.Firstname"
 	buff += 3;
 
-	i = ((sizeof(_initial_name_letters) + 35) * (byte)(x >> 8)) >> 8;
+	i = (sizeof(_initial_name_letters) + 35) * GB(x, 8, 8) >> 8;
 	if (i < sizeof(_initial_name_letters)) {
 		buff[0] = _initial_name_letters[i];
 		buff[1] = '.';
--- a/town_cmd.c	Thu Jul 21 06:13:17 2005 +0000
+++ b/town_cmd.c	Thu Jul 21 06:31:02 2005 +0000
@@ -92,7 +92,7 @@
 	/* Retrieve pointer to the draw town tile struct */
 	{
 		/* this "randomizes" on the (up to) 4 variants of a building */
-		byte gfx   = (byte)_m[ti->tile].m4;
+		byte gfx   = _m[ti->tile].m4;
 		byte stage = _m[ti->tile].m3 >> 6;
 		uint variant;
 		variant  = ti->x >> 4;
@@ -304,16 +304,20 @@
 
 	r = Random();
 
-	if ( (byte)r < _housetype_population[house] ) {
-		uint amt = ((byte)r >> 3) + 1, moved;
+	if (GB(r, 0, 8) < _housetype_population[house]) {
+		uint amt = GB(r, 0, 8) / 8 + 1;
+		uint moved;
+
 		if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
 		t->new_max_pass += amt;
 		moved = MoveGoodsToStation(tile, 1, 1, CT_PASSENGERS, amt);
 		t->new_act_pass += moved;
 	}
 
-	if ( (byte)(r>>8) < _housetype_mailamount[house] ) {
-		uint amt = ((byte)(r>>8) >> 3) + 1, moved;
+	if (GB(r, 8, 8) < _housetype_mailamount[house] ) {
+		uint amt = GB(r, 8, 8) / 8 + 1;
+		uint moved;
+
 		if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
 		t->new_max_mail += amt;
 		moved = MoveGoodsToStation(tile, 1, 1, CT_MAIL, amt);
@@ -321,17 +325,14 @@
 	}
 
 	if (_house_more_flags[house]&8 && (t->flags12&1) && --t->time_until_rebuild == 0) {
-		r>>=16;
-		t->time_until_rebuild = (r & 63) + 130;
+		t->time_until_rebuild = GB(r, 16, 6) + 130;
 
 		_current_player = OWNER_TOWN;
 
 		ClearTownHouse(t, tile);
 
 		// rebuild with another house?
-		if ( (byte) (r >> 8) >= 12) {
-			DoBuildTownHouse(t, tile);
-		}
+		if (GB(r, 24, 8) >= 12) DoBuildTownHouse(t, tile);
 
 		_current_player = OWNER_NONE;
 	}
@@ -1303,7 +1304,7 @@
 
 			// Value for map3lo
 			m3lo = 0xC0;
-			if ((byte)r >= 220) m3lo &= (r>>8);
+			if (GB(r, 0, 8) >= 220) m3lo &= (r>>8);
 
 			if (m3lo == 0xC0)
 				ChangePopulation(t, _housetype_population[house]);
--- a/train_cmd.c	Thu Jul 21 06:13:17 2005 +0000
+++ b/train_cmd.c	Thu Jul 21 06:31:02 2005 +0000
@@ -398,7 +398,6 @@
 	int32 value;
 	Vehicle *v;
 	const RailVehicleInfo *rvi;
-	int dir;
 	const Engine *e;
 	int x,y;
 
@@ -417,6 +416,7 @@
 		if (flags & DC_EXEC) {
 			byte img = rvi->image_index;
 			Vehicle *u, *w;
+			uint dir;
 
 			v->spritenum = img;
 
@@ -432,9 +432,9 @@
 
 			v->engine_type = engine;
 
-			dir = _m[tile].m5 & 3;
-
-			v->direction = (byte)(dir*2+1);
+			dir = GB(_m[tile].m5, 0, 2);
+
+			v->direction = dir * 2 + 1;
 			v->tile = tile;
 
 			x = TileX(tile) * TILE_SIZE | _vehicle_initial_x_fract[dir];
@@ -570,7 +570,7 @@
 int32 CmdBuildRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
 	const RailVehicleInfo *rvi;
-	int value,dir;
+	int value;
 	Vehicle *v, *u;
 	UnitID unit_num;
 	Engine *e;
@@ -606,11 +606,13 @@
 			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
 
 		if (flags & DC_EXEC) {
+			uint dir;
+
 			v->unitnumber = unit_num;
 
-			dir = _m[tile].m5 & 3;
-
-			v->direction = (byte)(dir*2+1);
+			dir = GB(_m[tile].m5, 0, 2);
+
+			v->direction = dir * 2 + 1;
 			v->tile = tile;
 			v->owner = _current_player;
 			v->x_pos = (x |= _vehicle_initial_x_fract[dir]);
@@ -1126,10 +1128,10 @@
 
 	uint32 x = _delta_xy_table[direction];
 
-	v->x_offs = (byte)x;
-	v->y_offs = (byte)(x>>=8);
-	v->sprite_width = (byte)(x>>=8);
-	v->sprite_height = (byte)(x>>=8);
+	v->x_offs        = GB(x,  0, 8);
+	v->y_offs        = GB(x,  8, 8);
+	v->sprite_width  = GB(x, 16, 8);
+	v->sprite_height = GB(x, 24, 8);
 }
 
 static void UpdateVarsAfterSwap(Vehicle *v)
@@ -2116,7 +2118,7 @@
 
 				/* if we reach this position, there's two paths of equal value so far.
 				 * pick one randomly. */
-				r = (byte)Random();
+				r = GB(Random(), 0, 8);
 				if (_pick_track_table[i] == (v->direction & 3)) r += 80;
 				if (_pick_track_table[best_track] == (v->direction & 3)) r -= 80;
 				if (r <= 127) goto bad;
@@ -3095,8 +3097,8 @@
 	tile = v->tile;
 
 	// tunnel entrance?
-	if (IsTunnelTile(tile) && (byte)((_m[tile].m5 & 3)*2+1) == v->direction)
-				return true;
+	if (IsTunnelTile(tile) && GB(_m[tile].m5, 0, 2) * 2 + 1 == v->direction)
+		return true;
 
 	// depot?
 	/* XXX -- When enabled, this makes it possible to crash trains of others
--- a/tunnelbridge_cmd.c	Thu Jul 21 06:13:17 2005 +0000
+++ b/tunnelbridge_cmd.c	Thu Jul 21 06:31:02 2005 +0000
@@ -377,8 +377,7 @@
 			}
 
 			_m[ti.tile].m2 = (bridge_type << 4) | m5;
-			_m[ti.tile].m3 &= 0xF;
-			_m[ti.tile].m3 |= (byte)(railtype << 4);
+			SB(_m[ti.tile].m3, 4, 4, railtype);
 
 			MarkTileDirtyByTile(ti.tile);
 		}
@@ -549,8 +548,8 @@
 
 	if (p1 != 0x200 && !ValParamRailtype(p1)) return CMD_ERROR;
 
-	_build_tunnel_railtype = (byte)(p1 & 0xFF);
-	_build_tunnel_bh = (byte)(p1 >> 8);
+	_build_tunnel_railtype = GB(p1, 0, 8);
+	_build_tunnel_bh       = GB(p1, 8, 8);
 
 	_build_tunnel_endtile = 0;
 	excavated_tile = 0;
@@ -602,7 +601,7 @@
 	} while (
 		!IsTileType(tile, MP_TUNNELBRIDGE) ||
 		(_m[tile].m5 & 0xF0) != 0 ||
-		(byte)(_m[tile].m5 ^ 2) != m5 ||
+		(_m[tile].m5 ^ 2) != m5 ||
 		GetTileZ(tile) != z
 	);
 
@@ -1015,9 +1014,9 @@
 	bool ice = _m[ti->tile].m4 & 0x80;
 
 	// draw tunnel?
-	if ( (byte)(ti->map5&0xF0) == 0) {
+	if ((ti->map5 & 0xF0) == 0) {
 		/* railway type */
-		image = (_m[ti->tile].m3 & 0xF) * 8;
+		image = GB(_m[ti->tile].m3, 0, 4) * 8;
 
 		/* ice? */
 		if (ice)
--- a/unmovable_cmd.c	Thu Jul 21 06:13:17 2005 +0000
+++ b/unmovable_cmd.c	Thu Jul 21 06:31:02 2005 +0000
@@ -267,7 +267,7 @@
 static void TileLoop_Unmovable(TileIndex tile)
 {
 	byte m5 = _m[tile].m5;
-	byte level; // HQ level (depends on company performance) in the range 1..5.
+	uint level; // HQ level (depends on company performance) in the range 1..5.
 	uint32 r;
 
 	if (!(m5 & 0x80)) {
@@ -278,23 +278,22 @@
 	/* HQ accepts passenger and mail; but we have to divide the values
 	 * between 4 tiles it occupies! */
 
-	level = (m5 & ~0x80) / 4 + 1;
+	level = GB(m5, 0, 7) / 4 + 1;
 	assert(level < 6);
 
 	r = Random();
 	// Top town buildings generate 250, so the top HQ type makes 256.
-	if ((byte) r < (256 / 4 /  (6 - level))) {
-		uint amt = ((byte) r >> 3) / 4 + 1;
+	if (GB(r, 0, 8) < (256 / 4 / (6 - level))) {
+		uint amt = GB(r, 0, 8) / 8 / 4 + 1;
 		if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
 		MoveGoodsToStation(tile, 2, 2, CT_PASSENGERS, amt);
 	}
 
-	r >>= 8;
 	// Top town building generates 90, HQ can make up to 196. The
 	// proportion passengers:mail is about the same as in the acceptance
 	// equations.
-	if ((byte) r < (196 / 4 / (6 - level))) {
-		uint amt = ((byte) r >> 3) / 4 + 1;
+	if (GB(r, 8, 8) < (196 / 4 / (6 - level))) {
+		uint amt = GB(r, 8, 8) / 8 / 4 + 1;
 		if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
 		MoveGoodsToStation(tile, 2, 2, CT_MAIL, amt);
 	}