(svn r3177) GB, CLRBIT, HASBIT, TOGGLEBIT
authortron
Mon, 14 Nov 2005 08:09:57 +0000
changeset 2635 7ed07303448d
parent 2634 0df9396b0067
child 2636 bdff7da76aa2
(svn r3177) GB, CLRBIT, HASBIT, TOGGLEBIT
ai/default/default.c
ai/trolly/trolly.c
aircraft_cmd.c
clear_cmd.c
disaster_cmd.c
gfx.c
main_gui.c
order_cmd.c
players.c
rail_gui.c
road_gui.c
ship_cmd.c
smallmap_gui.c
station.h
strings.c
town_cmd.c
train_cmd.c
tree_cmd.c
tunnelbridge_cmd.c
vehicle.c
--- a/ai/default/default.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/ai/default/default.c	Mon Nov 14 08:09:57 2005 +0000
@@ -1086,9 +1086,9 @@
 
 static void AiWantTrainRoute(Player *p)
 {
-	uint16 r;
+	uint16 r = GB(Random(), 0, 16);
+
 	p->ai.railtype_to_use = GetBestRailtype(p);
-	r = (uint16)Random();
 
 	if (r > 0xD000) {
 		AiWantLongIndustryRoute(p);
@@ -1349,7 +1349,7 @@
 
 static void AiWantRoadRoute(Player *p)
 {
-	uint16 r = (uint16)Random();
+	uint16 r = GB(Random(), 0, 16);
 
 	if (r > 0x4000) {
 		AiWantLongRoadIndustryRoute(p);
--- a/ai/trolly/trolly.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/ai/trolly/trolly.c	Mon Nov 14 08:09:57 2005 +0000
@@ -781,7 +781,8 @@
 	// To make the depot stand in the middle of the route, we start from the center..
 	// But first we walk through the route see if we can find a depot that is ours
 	//  this keeps things nice ;)
-	int g, i, j, r;
+	int g, i, r;
+	uint j;
 	TileIndex tile;
 	assert(p->ainew.state == AI_STATE_FIND_DEPOT);
 
@@ -796,7 +797,7 @@
 					// We found a depot, is it ours? (TELL ME!!!)
 					if (IsTileOwner(tile + TileOffsByDir(j), _current_player)) {
 						// Now, is it pointing to the right direction.........
-						if ((_m[tile + TileOffsByDir(j)].m5 & 3) == (j ^ 2)) {
+						if (GB(_m[tile + TileOffsByDir(j)].m5, 0, 2) == (j ^ 2)) {
 							// Yeah!!!
 							p->ainew.depot_tile = tile + TileOffsByDir(j);
 							p->ainew.depot_direction = j ^ 2; // Reverse direction
--- a/aircraft_cmd.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/aircraft_cmd.c	Mon Nov 14 08:09:57 2005 +0000
@@ -475,14 +475,14 @@
  * @param x,y unused
  * @param p1 vehicle ID of the aircraft to refit
  * @param p2 various bitstuffed elements
- * - p2 = (bit 0-7) - the new cargo type to refit to (p2 & 0xFF)
+ * - p2 = (bit 0-7) - the new cargo type to refit to
  */
 int32 CmdRefitAircraft(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
 	Vehicle *v;
 	int pass, mail;
 	int32 cost;
-	CargoID new_cid = p2 & 0xFF; //gets the cargo number
+	CargoID new_cid = GB(p2, 0, 8);
 	const AircraftVehicleInfo *avi;
 
 	if (!IsVehicleIndex(p1)) return CMD_ERROR;
--- a/clear_cmd.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/clear_cmd.c	Mon Nov 14 08:09:57 2005 +0000
@@ -772,14 +772,14 @@
 	TileIndex tile;
 
 	/* add hills */
-	i = ScaleByMapSize((Random() & 0x3FF) + 0x400);
+	i = ScaleByMapSize(GB(Random(), 0, 10) + 0x400);
 	do {
 		tile = RandomTile();
 		if (IsTileType(tile, MP_CLEAR)) SB(_m[tile].m5, 2, 2, 1);
 	} while (--i);
 
 	/* add grey squares */
-	i = ScaleByMapSize((Random() & 0x7F) + 0x80);
+	i = ScaleByMapSize(GB(Random(), 0, 7) + 0x80);
 	do {
 		uint32 r = Random();
 		tile = RandomTileSeed(r);
@@ -791,7 +791,7 @@
 				SB(_m[tile].m5, 2, 2, 2);
 				do {
 					if (--j == 0) goto get_out;
-					tile_new = tile + TileOffsByDir(Random() & 3);
+					tile_new = tile + TileOffsByDir(GB(Random(), 0, 2));
 				} while (!IsTileType(tile_new, MP_CLEAR));
 				tile = tile_new;
 			}
--- a/disaster_cmd.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/disaster_cmd.c	Mon Nov 14 08:09:57 2005 +0000
@@ -920,7 +920,7 @@
 
 				{
 					TileIndex tile = i->xy;
-					TileIndexDiff step = TileOffsByDir(Random() & 3);
+					TileIndexDiff step = TileOffsByDir(GB(Random(), 0, 2));
 
 					int count = 30;
 					do {
--- a/gfx.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/gfx.c	Mon Nov 14 08:09:57 2005 +0000
@@ -441,7 +441,7 @@
 	GetString(buffer, str);
 
 	tmp = FormatStringLinebreaks(buffer, maxw);
-	num = (uint16)tmp;
+	num = GB(tmp, 0, 16);
 
 	switch (GB(tmp, 16, 16)) {
 		case   0: mt = 10; break;
@@ -487,7 +487,7 @@
 	GetString(buffer, str);
 
 	tmp = FormatStringLinebreaks(buffer, maxw);
-	num = (uint16)tmp;
+	num = GB(tmp, 0, 16);
 
 	switch (GB(tmp, 16, 16)) {
 		case   0: mt = 10; break;
--- a/main_gui.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/main_gui.c	Mon Nov 14 08:09:57 2005 +0000
@@ -979,14 +979,14 @@
 	w = PopupMainToolbMenu(w,  43, 2, STR_02C3_GAME_OPTIONS, 13, 0);
 
 	x = (uint16)-1;
-	if (_display_opt & DO_SHOW_TOWN_NAMES) x &= ~(1<<5);
-	if (_display_opt & DO_SHOW_STATION_NAMES) x &= ~(1<<6);
-	if (_display_opt & DO_SHOW_SIGNS) x &= ~(1<<7);
-	if (_display_opt & DO_WAYPOINTS) x &= ~(1<<8);
-	if (_display_opt & DO_FULL_ANIMATION) x &= ~(1<<9);
-	if (_display_opt & DO_FULL_DETAIL) x &= ~(1<<10);
-	if (_display_opt & DO_TRANS_BUILDINGS) x &= ~(1<<11);
-	if (_display_opt & DO_TRANS_SIGNS) x &= ~(1<<12);
+	if (_display_opt & DO_SHOW_TOWN_NAMES)    CLRBIT(x,  5);
+	if (_display_opt & DO_SHOW_STATION_NAMES) CLRBIT(x,  6);
+	if (_display_opt & DO_SHOW_SIGNS)         CLRBIT(x,  7);
+	if (_display_opt & DO_WAYPOINTS)          CLRBIT(x,  8);
+	if (_display_opt & DO_FULL_ANIMATION)     CLRBIT(x,  9);
+	if (_display_opt & DO_FULL_DETAIL)        CLRBIT(x, 10);
+	if (_display_opt & DO_TRANS_BUILDINGS)    CLRBIT(x, 11);
+	if (_display_opt & DO_TRANS_SIGNS)        CLRBIT(x, 12);
 	WP(w,menu_d).checked_items = x;
 }
 
--- a/order_cmd.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/order_cmd.c	Mon Nov 14 08:09:57 2005 +0000
@@ -830,8 +830,8 @@
 int32 CmdRestoreOrderIndex(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
 	Vehicle *v;
-	OrderID cur_ord = p2 & 0xFFFF;
-	uint16 serv_int = p2 >> 16;
+	OrderID cur_ord = GB(p2,  0, 16);
+	uint16 serv_int = GB(p2, 16, 16);
 
 	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
--- a/players.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/players.c	Mon Nov 14 08:09:57 2005 +0000
@@ -155,12 +155,12 @@
 		uint val = GB(face, 20, 8);
 
 		if (!(flag&1)) {
-			DrawSprite(0x36B + ((val&3)*3>>2), x, y);
-			DrawSprite(0x36E + ((val>>2)&3), x, y);
-			DrawSprite(0x372 + ((val>>4)*6>>4), x, y);
+			DrawSprite(0x36B + (GB(val, 0, 2) * 3 >> 2), x, y);
+			DrawSprite(0x36E + (GB(val, 2, 2) * 4 >> 2), x, y);
+			DrawSprite(0x372 + (GB(val, 4, 4) * 6 >> 4), x, y);
 		} else {
-			DrawSprite(0x378 + ((val&3)*3>>2), x, y);
-			DrawSprite(0x37B + ((val>>2)&3), x, y);
+			DrawSprite(0x378 + (GB(val, 0, 2) * 3 >> 2), x, y);
+			DrawSprite(0x37B + (GB(val, 2, 2) * 4 >> 2), x, y);
 
 			val >>= 4;
 			if (val < 3) {
--- a/rail_gui.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/rail_gui.c	Mon Nov 14 08:09:57 2005 +0000
@@ -285,18 +285,16 @@
 
 static void BuildRailClick_Remove(Window *w)
 {
-	if (w->disabled_state & (1<<16))
-		return;
+	if (HASBIT(w->disabled_state, 16)) return;
 	SetWindowDirty(w);
 	SndPlayFx(SND_15_BEEP);
 
-	w->click_state ^= (1 << 16);
-	_remove_button_clicked = (w->click_state & (1 << 16)) != 0;
-	SetSelectionRed((w->click_state & (1 << 16)) != 0);
+	TOGGLEBIT(w->click_state, 16);
+	_remove_button_clicked = HASBIT(w->click_state, 16) != 0;
+	SetSelectionRed(HASBIT(w->click_state, 16) != 0);
 
 	// handle station builder
-	if( w->click_state & (1 << 16) )
-	{
+	if (HASBIT(w->click_state, 16)) {
 		if(_remove_button_clicked)
 			SetTileSelectSize(1, 1);
 		else
--- a/road_gui.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/road_gui.c	Mon Nov 14 08:09:57 2005 +0000
@@ -159,12 +159,11 @@
 
 static void BuildRoadClick_Remove(Window *w)
 {
-	if (w->disabled_state & (1<<11))
-		return;
+	if (HASBIT(w->disabled_state, 11)) return;
 	SetWindowDirty(w);
 	SndPlayFx(SND_15_BEEP);
-	w->click_state ^= (1 << 11);
-	SetSelectionRed((w->click_state & (1 << 11)) != 0);
+	TOGGLEBIT(w->click_state, 11);
+	SetSelectionRed(HASBIT(w->click_state, 11) != 0);
 }
 
 static void BuildRoadClick_Landscaping(Window *w)
--- a/ship_cmd.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/ship_cmd.c	Mon Nov 14 08:09:57 2005 +0000
@@ -356,8 +356,8 @@
 	} else {
 		return;
 	}
-	v->direction = (byte)m;
-	v->u.ship.state = (byte)(m >> 8);
+	v->direction    = GB(m, 0, 8);
+	v->u.ship.state = GB(m, 8, 8);
 	v->vehstatus &= ~VS_HIDDEN;
 
 	v->cur_speed = 0;
--- a/smallmap_gui.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/smallmap_gui.c	Mon Nov 14 08:09:57 2005 +0000
@@ -197,15 +197,15 @@
 	static inline void WRITE_PIXELS(Pixel* d, uint32 val)
 	{
 #	if defined(TTD_BIG_ENDIAN)
-		d[0] = (byte)(val >> 24);
-		d[1] = (byte)(val >> 16);
-		d[2] = (byte)(val >> 8);
-		d[3] = (byte)(val >> 0);
+		d[0] = GB(val, 24, 8);
+		d[1] = GB(val, 16, 8);
+		d[2] = GB(val,  8, 8);
+		d[3] = GB(val,  0, 8);
 #	elif defined(TTD_LITTLE_ENDIAN)
-		d[0] = (byte)(val >> 0);
-		d[1] = (byte)(val >> 8);
-		d[2] = (byte)(val >> 16);
-		d[3] = (byte)(val >> 24);
+		d[0] = GB(val,  0, 8);
+		d[1] = GB(val,  8, 8);
+		d[2] = GB(val, 16, 8);
+		d[3] = GB(val, 24, 8);
 #	endif
 	}
 
@@ -213,15 +213,15 @@
 	static inline void WRITE_PIXELS_OR(Pixel* d, uint32 val)
 	{
 #	if defined(TTD_BIG_ENDIAN)
-		d[0] |= (byte)(val >> 24);
-		d[1] |= (byte)(val >> 16);
-		d[2] |= (byte)(val >> 8);
-		d[3] |= (byte)(val >> 0);
+		d[0] |= GB(val, 24, 8);
+		d[1] |= GB(val, 16, 8);
+		d[2] |= GB(val,  8, 8);
+		d[3] |= GB(val,  0, 8);
 #	elif defined(TTD_LITTLE_ENDIAN)
-		d[0] |= (byte)(val >> 0);
-		d[1] |= (byte)(val >> 8);
-		d[2] |= (byte)(val >> 16);
-		d[3] |= (byte)(val >> 24);
+		d[0] |= GB(val,  0, 8);
+		d[1] |= GB(val,  8, 8);
+		d[2] |= GB(val, 16, 8);
+		d[3] |= GB(val, 24, 8);
 #	endif
 	}
 #else
--- a/station.h	Sun Nov 13 21:16:34 2005 +0000
+++ b/station.h	Mon Nov 14 08:09:57 2005 +0000
@@ -222,8 +222,8 @@
 	assert(IsTrainStationTile(ref));
 	return
 		IsTrainStationTile(tile) &&
-		(_m[tile].m3 & 0x0F) == (_m[ref].m3 & 0x0F) && // same rail type?
-		(_m[tile].m5 & 0x01) == (_m[ref].m5 & 0x01); // same direction?
+		GB(_m[tile].m3, 0, 4) == GB(_m[ref].m3, 0, 4) && // same rail type?
+		GB(_m[tile].m5, 0, 1) == GB(_m[ref].m5, 0, 1);   // same direction?
 }
 
 static inline bool IsRoadStationTile(TileIndex tile) {
--- a/strings.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/strings.c	Mon Nov 14 08:09:57 2005 +0000
@@ -161,9 +161,7 @@
 	uint index = GB(string,  0, 11);
 	uint tab   = GB(string, 11,  5);
 
-	if (!(string & 0xFFFF)) {
-		error("!invalid string id 0 in GetString");
-	}
+	if (GB(string, 0, 16) == 0) error("!invalid string id 0 in GetString");
 
 	switch (tab) {
 		case 4:
@@ -196,7 +194,7 @@
 			"Probably because an old version of the .lng file.\n", string
 		);
 
-	return FormatString(buffr, GetStringPtr(string&0xFFFF), argv, string >> 24);
+	return FormatString(buffr, GetStringPtr(GB(string, 0, 16)), argv, GB(string, 24, 8));
 }
 
 char *GetString(char *buffr, StringID string)
--- a/town_cmd.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/town_cmd.c	Mon Nov 14 08:09:57 2005 +0000
@@ -97,7 +97,7 @@
 	{
 		/* this "randomizes" on the (up to) 4 variants of a building */
 		byte gfx   = _m[ti->tile].m4;
-		byte stage = _m[ti->tile].m3 >> 6;
+		byte stage = GB(_m[ti->tile].m3, 6, 2);
 		uint variant;
 		variant  = ti->x >> 4;
 		variant ^= ti->x >> 6;
@@ -635,7 +635,7 @@
 
 		// Possibly extend the road in a direction.
 		// Randomize a direction and if it has a road, bail out.
-		i = (int)Random() & 3;
+		i = GB(Random(), 0, 2);
 		if (HASBIT(mask, i))
 			return;
 
--- a/train_cmd.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/train_cmd.c	Mon Nov 14 08:09:57 2005 +0000
@@ -1623,10 +1623,10 @@
  */
 int32 CmdRefitRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
+	CargoID new_cid = GB(p2, 0, 8);
 	Vehicle *v;
 	int32 cost;
 	uint num;
-	CargoID new_cid = p2 & 0xFF; //gets the cargo number
 
 	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
@@ -3173,7 +3173,7 @@
 	do {
 		//I need to buffer the train direction
 		if (!(v->u.rail.track & 0x40))
-			v->direction = (v->direction + _random_dir_change[Random()&3]) & 7;
+			v->direction = (v->direction + _random_dir_change[GB(Random(), 0, 2)]) & 7;
 		if (!(v->vehstatus & VS_HIDDEN)) {
 			BeginVehicleMove(v);
 			UpdateTrainDeltaXY(v, v->direction);
--- a/tree_cmd.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/tree_cmd.c	Mon Nov 14 08:09:57 2005 +0000
@@ -214,7 +214,7 @@
 
 						treetype = p1;
 						if (treetype == -1) {
-							treetype = GetRandomTreeType(tile, Random() >> 24);
+							treetype = GetRandomTreeType(tile, GB(Random(), 24, 8));
 							if (treetype == -1) treetype = 27;
 						}
 
@@ -488,7 +488,7 @@
 		if (_opt.landscape == LT_DESERT && _m[tile].m3 != 0x1B && GetMapExtraBits(tile) == 1) {
 			m5++; /* start destructing */
 		} else {
-			switch(Random() & 0x7) {
+			switch (GB(Random(), 0, 3)) {
 			case 0: /* start destructing */
 				m5++;
 				break;
@@ -567,7 +567,7 @@
 			(r = Random(), tile = RandomTileSeed(r), GetMapExtraBits(tile) == 2) &&
 			IsTileType(tile, MP_CLEAR) &&
 			(m = _m[tile].m5 & 0x1C, m <= 4) &&
-			(tree = GetRandomTreeType(tile, r >> 24)) >= 0) {
+			(tree = GetRandomTreeType(tile, GB(r, 24, 8))) >= 0) {
 
 		ModifyTile(tile,
 			MP_SETTYPE(MP_TREES) |
@@ -587,7 +587,7 @@
 	tile = TILE_MASK(r);
 	if (IsTileType(tile, MP_CLEAR) &&
 			(m = _m[tile].m5 & 0x1C, m == 0 || m == 4 || m == 0x10) &&
-			(tree = GetRandomTreeType(tile, r >> 24)) >= 0) {
+			(tree = GetRandomTreeType(tile, GB(r, 24, 8))) >= 0) {
 		int m2;
 
 		if (m == 0) {
--- a/tunnelbridge_cmd.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/tunnelbridge_cmd.c	Mon Nov 14 08:09:57 2005 +0000
@@ -813,7 +813,7 @@
 					static const uint16 _new_data_table[] = {0x1002, 0x1001, 0x2005, 0x200A, 0, 0, 0, 0};
 					new_data = _new_data_table[((m5 & 0x18) >> 2) | (m5&1)];
 				}	else {
-					if (!(m5 & 0x18)) goto clear_it;
+					if (GB(m5, 3, 2) == 0) goto clear_it;
 					new_data = (GetTileSlope(c, NULL) == 0) ? 0x6000 : 0x6001;
 				}
 
@@ -1398,14 +1398,14 @@
 
 	if ((m5 & 0xF0) == 0) {
 		/* This is a tunnel */
-		if (((m5 & 0xCU) >> 2) == mode) {
+		if (GB(m5, 2, 2) == mode) {
 			/* Tranport in the tunnel is compatible */
 			return m5&1 ? 0x202 : 0x101;
 		}
 	} else if (m5 & 0x80) {
 		/* This is a bridge */
 		result = 0;
-		if (((m5 & 0x6U) >> 1) == mode) {
+		if (GB(m5, 1, 2) == mode) {
 			/* Transport over the bridge is compatible */
 			result = m5&1 ? 0x202 : 0x101;
 		}
@@ -1421,7 +1421,7 @@
 						return result;
 			} else {
 				/* Transport underneath */
-				if ((m5 & 0x18U) >> 3 != mode)
+				if (GB(m5, 3, 2) != mode)
 					/* Incompatible transport underneath */
 					return result;
 			}
--- a/vehicle.c	Sun Nov 13 21:16:34 2005 +0000
+++ b/vehicle.c	Mon Nov 14 08:09:57 2005 +0000
@@ -1232,7 +1232,7 @@
 			return;
 		}
 		if (v->u.special.unk2 != 0) {
-			v->spritenum = (InteractiveRandom() & 3) + 1;
+			v->spritenum = GB(InteractiveRandom(), 0, 2) + 1;
 		} else {
 			v->spritenum = 6;
 		}