(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
authortron
Mon, 03 Oct 2005 21:20:01 +0000
changeset 2484 0e45d70ae908
parent 2483 e2d5f55daf63
child 2485 3e83ba6adf27
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
aircraft_cmd.c
disaster_cmd.c
industry_cmd.c
misc.c
namegen.c
players.c
roadveh_cmd.c
station_gui.c
town_cmd.c
train_cmd.c
train_gui.c
viewport.c
--- a/aircraft_cmd.c	Mon Oct 03 21:10:51 2005 +0000
+++ b/aircraft_cmd.c	Mon Oct 03 21:20:01 2005 +0000
@@ -1239,8 +1239,7 @@
 		prob = 0x10000 / 20;
 	}
 
-	if ((uint16)Random() > prob)
-		return;
+	if (GB(Random(), 0, 16) > prob) return;
 
 	// Crash the airplane. Remove all goods stored at the station.
 	for(i=0; i!=NUM_CARGO; i++) {
--- a/disaster_cmd.c	Mon Oct 03 21:10:51 2005 +0000
+++ b/disaster_cmd.c	Mon Oct 03 21:20:01 2005 +0000
@@ -977,13 +977,13 @@
 	if (j == 0)
 		return;
 
-	_disaster_initprocs[buf[(uint16)Random() * j >> 16]]();
+	_disaster_initprocs[buf[GB(Random(), 0, 16) * j >> 16]]();
 }
 
 
 static void ResetDisasterDelay(void)
 {
-	_disaster_delay = (int)(Random() & 0x1FF) + 730;
+	_disaster_delay = GB(Random(), 0, 9) + 730;
 }
 
 void DisasterDailyLoop(void)
--- a/industry_cmd.c	Mon Oct 03 21:10:51 2005 +0000
+++ b/industry_cmd.c	Mon Oct 03 21:20:01 2005 +0000
@@ -934,7 +934,7 @@
 		if (IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES)) {
 
 			or = type;
-			if (or == 1 && (uint16)Random() <= 9362) or = 2;
+			if (or == 1 && GB(Random(), 0, 16) <= 9362) or = 2;
 
 			or <<= 2;
 			and = (byte)~0x1C;
@@ -1486,9 +1486,9 @@
 	i->town = t;
 	i->owner = owner;
 
-	r = Random() & 0xFFF;
-	i->color_map = (byte)(r >> 8);
-	i->counter = (uint16)r;
+	r = Random();
+	i->color_map = GB(r, 8, 8);
+	i->counter = GB(r, 0, 16);
 	i->cargo_waiting[0] = 0;
 	i->cargo_waiting[1] = 0;
 	i->last_mo_production[0] = 0;
@@ -1626,7 +1626,7 @@
 		return NULL;
 
 	/* pick a random layout */
-	it = spec->table[(spec->num_table * (uint16)Random()) >> 16];
+	it = spec->table[(spec->num_table * GB(Random(), 0, 16)) >> 16];
 
 	if (!CheckIfIndustryTilesAreFree(tile, it, type, t))
 		return NULL;
--- a/misc.c	Mon Oct 03 21:10:51 2005 +0000
+++ b/misc.c	Mon Oct 03 21:20:01 2005 +0000
@@ -51,12 +51,12 @@
 #if defined(RANDOM_DEBUG) && !defined(MERSENNE_TWISTER)
 uint DoRandomRange(uint max, int line, const char *file)
 {
-	return (uint16)DoRandom(line, file) * max >> 16;
+	return GB(DoRandom(line, file), 0, 16) * max >> 16;
 }
 #else
 uint RandomRange(uint max)
 {
-	return (uint16)Random() * max >> 16;
+	return GB(Random(), 0, 16) * max >> 16;
 }
 #endif
 
@@ -71,7 +71,7 @@
 
 uint InteractiveRandomRange(uint max)
 {
-	return (uint16)InteractiveRandom() * max >> 16;
+	return GB(InteractiveRandom(), 0, 16) * max >> 16;
 }
 
 void SetDate(uint date)
--- a/namegen.c	Mon Oct 03 21:10:51 2005 +0000
+++ b/namegen.c	Mon Oct 03 21:20:01 2005 +0000
@@ -3,12 +3,13 @@
 #include "stdafx.h"
 #include "openttd.h"
 #include "debug.h"
+#include "macros.h"
 #include "namegen.h"
 #include "table/namegen.h"
 
 static inline uint32 SeedChance(int shift_by, int max, uint32 seed)
 {
-	return ((uint16)(seed >> shift_by) * max) >> 16;
+	return (GB(seed, shift_by, 16) * max) >> 16;
 }
 
 static inline uint32 SeedModChance(int shift_by, int max, uint32 seed)
--- a/players.c	Mon Oct 03 21:10:51 2005 +0000
+++ b/players.c	Mon Oct 03 21:20:01 2005 +0000
@@ -1004,7 +1004,7 @@
 		memset(_highscore_table[LAST_HS_ITEM], 0, sizeof(_highscore_table[0]));
 
 		/* Copy over Top5 companies */
-		for (i = 0; i < lengthof(_highscore_table[LAST_HS_ITEM]) && i < (uint8)count; i++) {
+		for (i = 0; i < lengthof(_highscore_table[LAST_HS_ITEM]) && i < count; i++) {
 			char buf[sizeof(_highscore_table[0]->company)];
 
 			hs = &_highscore_table[LAST_HS_ITEM][i];
@@ -1019,7 +1019,7 @@
 			hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
 
 			// get the ranking of the local player
-			if ((*p_cur)->index == (int8)_local_player)
+			if ((*p_cur)->index == _local_player)
 				player = i;
 
 			p_cur++;
--- a/roadveh_cmd.c	Mon Oct 03 21:10:51 2005 +0000
+++ b/roadveh_cmd.c	Mon Oct 03 21:20:01 2005 +0000
@@ -987,7 +987,7 @@
 			num++;
 	} while (b >>= 1);
 
-	num = ((uint16)Random() * num >> 16);
+	num = GB(Random(), 0, 16) * num >> 16;
 
 	for(i=0; !((bits & 1) && ((int)--num) < 0); bits>>=1,i++);
 	return i;
@@ -1029,8 +1029,8 @@
 	{
 		uint32 r;
 		r = GetTileTrackStatus(tile, TRANSPORT_ROAD);
-		signal = (uint16)(r >> 16);
-		bitmask = (uint16)r;
+		signal  = GB(r, 16, 16);
+		bitmask = GB(r,  0, 16);
 	}
 
 	if (IsTileType(tile, MP_STREET)) {
--- a/station_gui.c	Mon Oct 03 21:10:51 2005 +0000
+++ b/station_gui.c	Mon Oct 03 21:20:01 2005 +0000
@@ -318,7 +318,7 @@
 	StringID str;
 	uint16 station_id;
 
-	station_id = (uint16)w->window_number;
+	station_id = w->window_number;
 
 	st = GetStation(w->window_number);
 
--- a/town_cmd.c	Mon Oct 03 21:10:51 2005 +0000
+++ b/town_cmd.c	Mon Oct 03 21:20:01 2005 +0000
@@ -593,9 +593,9 @@
 		// Randomize new road block numbers
 		a = block;
 		b = block ^ 2;
-		r = (uint16)Random();
+		r = GB(Random(), 0, 16);
 		if (r <= 0x4000) do {
-			a = (int)Random() & 3;
+			a = GB(Random(), 0, 2);
 		} while(a == b);
 
 		if (!IsRoadAllowedHere(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a])), a)) {
--- a/train_cmd.c	Mon Oct 03 21:10:51 2005 +0000
+++ b/train_cmd.c	Mon Oct 03 21:20:01 2005 +0000
@@ -763,23 +763,25 @@
  */
 int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
+	VehicleID s = GB(p1, 0, 16);
+	VehicleID d = GB(p1, 16, 16);
 	Vehicle *src, *dst, *src_head, *dst_head;
 	bool is_loco;
 
-	if (!IsVehicleIndex(p1 & 0xFFFF)) return CMD_ERROR;
-
-	src = GetVehicle(p1 & 0xFFFF);
+	if (!IsVehicleIndex(s)) return CMD_ERROR;
+
+	src = GetVehicle(s);
 
 	if (src->type != VEH_Train) return CMD_ERROR;
 
 	is_loco = !(RailVehInfo(src->engine_type)->flags & RVI_WAGON) && IS_FIRSTHEAD_SPRITE(src->spritenum);
 
 	// if nothing is selected as destination, try and find a matching vehicle to drag to.
-	if (((int32)p1 >> 16) == -1) {
+	if (d == INVALID_VEHICLE) {
 		dst = NULL;
 		if (!is_loco) dst = FindGoodVehiclePos(src);
 	} else {
-		dst = GetVehicle((int32)p1 >> 16);
+		dst = GetVehicle(d);
 	}
 
 	// don't move the same vehicle..
@@ -1759,14 +1761,14 @@
 
 		case 1:
 			// diesel smoke
-			if (u->cur_speed <= 40 && !IsTileDepotType(v->tile, TRANSPORT_RAIL) && !IsTunnelTile(v->tile) && (uint16)Random() <= 0x1E00) {
+			if (u->cur_speed <= 40 && !IsTileDepotType(v->tile, TRANSPORT_RAIL) && !IsTunnelTile(v->tile) && GB(Random(), 0, 16) <= 0x1E00) {
 				CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE);
 			}
 			break;
 
 		case 2:
 			// blue spark
-			if ( (v->tick_counter&0x3) == 0 && !IsTileDepotType(v->tile, TRANSPORT_RAIL) && !IsTunnelTile(v->tile) && (uint16)Random() <= 0x5B0) {
+			if (GB(v->tick_counter, 0, 2) == 0 && !IsTileDepotType(v->tile, TRANSPORT_RAIL) && !IsTunnelTile(v->tile) && GB(Random(), 0, 16) <= 0x5B0) {
 				CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK);
 			}
 			break;
@@ -3016,7 +3018,7 @@
 		CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
 	}
 
-	if (state <= 200 && (uint16)(r=Random()) <= 0x2492) {
+	if (state <= 200 && GB(r = Random(), 0, 16) <= 0x2492) {
 		index = (r * 10 >> 16);
 
 		u = v;
@@ -3082,16 +3084,17 @@
 {
 	TileIndex tile;
 	uint x,y;
+	uint16 break_speed;
 	int t;
 	uint32 ts;
 	byte trackdir;
 
-	if ((uint)(t=v->breakdown_ctr) > 1) {
+	t = v->breakdown_ctr;
+	if (t > 1) {
 		v->vehstatus |= VS_TRAIN_SLOWING;
 
-		t = _breakdown_speeds[ ((~t) >> 4) & 0xF];
-		if ((uint16)t <= v->cur_speed)
-			v->cur_speed = t;
+		break_speed = _breakdown_speeds[GB(~t, 4, 4)];
+		if (break_speed < v->cur_speed) v->cur_speed = break_speed;
 	} else {
 		v->vehstatus &= ~VS_TRAIN_SLOWING;
 	}
@@ -3160,7 +3163,7 @@
 		break;
 	}
 
-	if ( (uint16)ts != 0) {
+	if (GB(ts, 0, 16) != 0) {
 		/* If we approach a rail-piece which we can't enter, don't enter it! */
 		if (x + 4 > 15 && !CheckCompatibleRail(v, tile)) {
 			v->cur_speed = 0;
@@ -3208,10 +3211,9 @@
 
 	// slow down
 	v->vehstatus |= VS_TRAIN_SLOWING;
-	t = _breakdown_speeds[x & 0xF];
-	if (!(v->direction&1)) t>>=1;
-	if ((uint16)t < v->cur_speed)
-		v->cur_speed = t;
+	break_speed = _breakdown_speeds[x & 0xF];
+	if (!(v->direction&1)) break_speed >>= 1;
+	if (break_speed < v->cur_speed) v->cur_speed = break_speed;
 
 	return true;
 }
--- a/train_gui.c	Mon Oct 03 21:10:51 2005 +0000
+++ b/train_gui.c	Mon Oct 03 21:20:01 2005 +0000
@@ -538,7 +538,7 @@
 	return 0;
 }
 
-static void TrainDepotMoveVehicle(Vehicle *wagon, int sel, Vehicle *head)
+static void TrainDepotMoveVehicle(Vehicle *wagon, VehicleID sel, Vehicle *head)
 {
 	Vehicle *v;
 
@@ -559,13 +559,13 @@
 	if (wagon == v)
 		return;
 
-	DoCommandP(v->tile, v->index + ((wagon==NULL ? (uint32)-1 : wagon->index) << 16), _ctrl_pressed ? 1 : 0, NULL, CMD_MOVE_RAIL_VEHICLE | CMD_MSG(STR_8837_CAN_T_MOVE_VEHICLE));
+	DoCommandP(v->tile, v->index + ((wagon == NULL ? INVALID_VEHICLE : wagon->index) << 16), _ctrl_pressed ? 1 : 0, NULL, CMD_MOVE_RAIL_VEHICLE | CMD_MSG(STR_8837_CAN_T_MOVE_VEHICLE));
 }
 
 static void TrainDepotClickTrain(Window *w, int x, int y)
 {
 	GetDepotVehiclePtData gdvp;
-	int mode, sel;
+	int mode;
 	Vehicle *v;
 
 	mode = GetVehicleFromTrainDepotWndPt(w, x, y, &gdvp);
@@ -576,9 +576,10 @@
 	v = gdvp.wagon;
 
 	switch(mode) {
-	case 0: // start dragging of vehicle
-		sel = (int16)WP(w,traindepot_d).sel;
-		if (sel != -1) {
+	case 0: { // start dragging of vehicle
+		VehicleID sel = WP(w, traindepot_d).sel;
+
+		if (sel != INVALID_VEHICLE) {
 			WP(w,traindepot_d).sel = INVALID_VEHICLE;
 			TrainDepotMoveVehicle(v, sel, gdvp.head);
 		} else if (v != NULL) {
@@ -587,6 +588,7 @@
 			SetWindowDirty(w);
 		}
 		break;
+	}
 
 	case -1: // show info window
 		ShowTrainViewWindow(v);
--- a/viewport.c	Mon Oct 03 21:10:51 2005 +0000
+++ b/viewport.c	Mon Oct 03 21:20:01 2005 +0000
@@ -2229,7 +2229,7 @@
 		VpStartPreSizing();
 
 	if ( (int)icon < 0)
-		SetAnimatedMouseCursor(_animcursors[~(int32)icon]);
+		SetAnimatedMouseCursor(_animcursors[~icon]);
 	else
 		SetMouseCursor(icon);
 }