(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them

This commit is contained in:
tron 2005-10-03 21:20:01 +00:00
parent b0a365ee67
commit db3b1228bf
12 changed files with 48 additions and 44 deletions

View File

@ -1239,8 +1239,7 @@ static void MaybeCrashAirplane(Vehicle *v)
prob = 0x10000 / 20; prob = 0x10000 / 20;
} }
if ((uint16)Random() > prob) if (GB(Random(), 0, 16) > prob) return;
return;
// Crash the airplane. Remove all goods stored at the station. // Crash the airplane. Remove all goods stored at the station.
for(i=0; i!=NUM_CARGO; i++) { for(i=0; i!=NUM_CARGO; i++) {

View File

@ -977,13 +977,13 @@ static void DoDisaster(void)
if (j == 0) if (j == 0)
return; return;
_disaster_initprocs[buf[(uint16)Random() * j >> 16]](); _disaster_initprocs[buf[GB(Random(), 0, 16) * j >> 16]]();
} }
static void ResetDisasterDelay(void) static void ResetDisasterDelay(void)
{ {
_disaster_delay = (int)(Random() & 0x1FF) + 730; _disaster_delay = GB(Random(), 0, 9) + 730;
} }
void DisasterDailyLoop(void) void DisasterDailyLoop(void)

View File

@ -934,7 +934,7 @@ static void SetupFarmFieldFence(TileIndex tile, int size, byte type, int directi
if (IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES)) { if (IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES)) {
or = type; or = type;
if (or == 1 && (uint16)Random() <= 9362) or = 2; if (or == 1 && GB(Random(), 0, 16) <= 9362) or = 2;
or <<= 2; or <<= 2;
and = (byte)~0x1C; and = (byte)~0x1C;
@ -1486,9 +1486,9 @@ static void DoCreateNewIndustry(Industry *i, TileIndex tile, int type, const Ind
i->town = t; i->town = t;
i->owner = owner; i->owner = owner;
r = Random() & 0xFFF; r = Random();
i->color_map = (byte)(r >> 8); i->color_map = GB(r, 8, 8);
i->counter = (uint16)r; i->counter = GB(r, 0, 16);
i->cargo_waiting[0] = 0; i->cargo_waiting[0] = 0;
i->cargo_waiting[1] = 0; i->cargo_waiting[1] = 0;
i->last_mo_production[0] = 0; i->last_mo_production[0] = 0;
@ -1626,7 +1626,7 @@ Industry *CreateNewIndustry(TileIndex tile, int type)
return NULL; return NULL;
/* pick a random layout */ /* 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)) if (!CheckIfIndustryTilesAreFree(tile, it, type, t))
return NULL; return NULL;

6
misc.c
View File

@ -51,12 +51,12 @@ uint32 t;
#if defined(RANDOM_DEBUG) && !defined(MERSENNE_TWISTER) #if defined(RANDOM_DEBUG) && !defined(MERSENNE_TWISTER)
uint DoRandomRange(uint max, int line, const char *file) 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 #else
uint RandomRange(uint max) uint RandomRange(uint max)
{ {
return (uint16)Random() * max >> 16; return GB(Random(), 0, 16) * max >> 16;
} }
#endif #endif
@ -71,7 +71,7 @@ uint32 InteractiveRandom(void)
uint InteractiveRandomRange(uint max) uint InteractiveRandomRange(uint max)
{ {
return (uint16)InteractiveRandom() * max >> 16; return GB(InteractiveRandom(), 0, 16) * max >> 16;
} }
void SetDate(uint date) void SetDate(uint date)

View File

@ -3,12 +3,13 @@
#include "stdafx.h" #include "stdafx.h"
#include "openttd.h" #include "openttd.h"
#include "debug.h" #include "debug.h"
#include "macros.h"
#include "namegen.h" #include "namegen.h"
#include "table/namegen.h" #include "table/namegen.h"
static inline uint32 SeedChance(int shift_by, int max, uint32 seed) 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) static inline uint32 SeedModChance(int shift_by, int max, uint32 seed)

View File

@ -1004,7 +1004,7 @@ int8 SaveHighScoreValueNetwork(void)
memset(_highscore_table[LAST_HS_ITEM], 0, sizeof(_highscore_table[0])); memset(_highscore_table[LAST_HS_ITEM], 0, sizeof(_highscore_table[0]));
/* Copy over Top5 companies */ /* 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)]; char buf[sizeof(_highscore_table[0]->company)];
hs = &_highscore_table[LAST_HS_ITEM][i]; hs = &_highscore_table[LAST_HS_ITEM][i];
@ -1019,7 +1019,7 @@ int8 SaveHighScoreValueNetwork(void)
hs->title = EndGameGetPerformanceTitleFromValue(hs->score); hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
// get the ranking of the local player // get the ranking of the local player
if ((*p_cur)->index == (int8)_local_player) if ((*p_cur)->index == _local_player)
player = i; player = i;
p_cur++; p_cur++;

View File

@ -987,7 +987,7 @@ static int PickRandomBit(uint bits)
num++; num++;
} while (b >>= 1); } 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++); for(i=0; !((bits & 1) && ((int)--num) < 0); bits>>=1,i++);
return i; return i;
@ -1029,8 +1029,8 @@ static int RoadFindPathToDest(Vehicle *v, TileIndex tile, int enterdir)
{ {
uint32 r; uint32 r;
r = GetTileTrackStatus(tile, TRANSPORT_ROAD); r = GetTileTrackStatus(tile, TRANSPORT_ROAD);
signal = (uint16)(r >> 16); signal = GB(r, 16, 16);
bitmask = (uint16)r; bitmask = GB(r, 0, 16);
} }
if (IsTileType(tile, MP_STREET)) { if (IsTileType(tile, MP_STREET)) {

View File

@ -318,7 +318,7 @@ static void DrawStationViewWindow(Window *w)
StringID str; StringID str;
uint16 station_id; uint16 station_id;
station_id = (uint16)w->window_number; station_id = w->window_number;
st = GetStation(w->window_number); st = GetStation(w->window_number);

View File

@ -593,9 +593,9 @@ static void GrowTownInTile(TileIndex *tile_ptr, uint mask, int block, Town *t1)
// Randomize new road block numbers // Randomize new road block numbers
a = block; a = block;
b = block ^ 2; b = block ^ 2;
r = (uint16)Random(); r = GB(Random(), 0, 16);
if (r <= 0x4000) do { if (r <= 0x4000) do {
a = (int)Random() & 3; a = GB(Random(), 0, 2);
} while(a == b); } while(a == b);
if (!IsRoadAllowedHere(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a])), a)) { if (!IsRoadAllowedHere(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a])), a)) {

View File

@ -763,23 +763,25 @@ static Vehicle *FindGoodVehiclePos(const Vehicle *src)
*/ */
int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) 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; Vehicle *src, *dst, *src_head, *dst_head;
bool is_loco; bool is_loco;
if (!IsVehicleIndex(p1 & 0xFFFF)) return CMD_ERROR; if (!IsVehicleIndex(s)) return CMD_ERROR;
src = GetVehicle(p1 & 0xFFFF); src = GetVehicle(s);
if (src->type != VEH_Train) return CMD_ERROR; if (src->type != VEH_Train) return CMD_ERROR;
is_loco = !(RailVehInfo(src->engine_type)->flags & RVI_WAGON) && IS_FIRSTHEAD_SPRITE(src->spritenum); 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 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; dst = NULL;
if (!is_loco) dst = FindGoodVehiclePos(src); if (!is_loco) dst = FindGoodVehiclePos(src);
} else { } else {
dst = GetVehicle((int32)p1 >> 16); dst = GetVehicle(d);
} }
// don't move the same vehicle.. // don't move the same vehicle..
@ -1759,14 +1761,14 @@ static void HandleLocomotiveSmokeCloud(Vehicle *v)
case 1: case 1:
// diesel smoke // 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); CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE);
} }
break; break;
case 2: case 2:
// blue spark // 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); CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK);
} }
break; break;
@ -3016,7 +3018,7 @@ static void HandleCrashedTrain(Vehicle *v)
CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE); 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); index = (r * 10 >> 16);
u = v; u = v;
@ -3082,16 +3084,17 @@ static bool TrainCheckIfLineEnds(Vehicle *v)
{ {
TileIndex tile; TileIndex tile;
uint x,y; uint x,y;
uint16 break_speed;
int t; int t;
uint32 ts; uint32 ts;
byte trackdir; byte trackdir;
if ((uint)(t=v->breakdown_ctr) > 1) { t = v->breakdown_ctr;
if (t > 1) {
v->vehstatus |= VS_TRAIN_SLOWING; v->vehstatus |= VS_TRAIN_SLOWING;
t = _breakdown_speeds[ ((~t) >> 4) & 0xF]; break_speed = _breakdown_speeds[GB(~t, 4, 4)];
if ((uint16)t <= v->cur_speed) if (break_speed < v->cur_speed) v->cur_speed = break_speed;
v->cur_speed = t;
} else { } else {
v->vehstatus &= ~VS_TRAIN_SLOWING; v->vehstatus &= ~VS_TRAIN_SLOWING;
} }
@ -3160,7 +3163,7 @@ static bool TrainCheckIfLineEnds(Vehicle *v)
break; 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 we approach a rail-piece which we can't enter, don't enter it! */
if (x + 4 > 15 && !CheckCompatibleRail(v, tile)) { if (x + 4 > 15 && !CheckCompatibleRail(v, tile)) {
v->cur_speed = 0; v->cur_speed = 0;
@ -3208,10 +3211,9 @@ static bool TrainCheckIfLineEnds(Vehicle *v)
// slow down // slow down
v->vehstatus |= VS_TRAIN_SLOWING; v->vehstatus |= VS_TRAIN_SLOWING;
t = _breakdown_speeds[x & 0xF]; break_speed = _breakdown_speeds[x & 0xF];
if (!(v->direction&1)) t>>=1; if (!(v->direction&1)) break_speed >>= 1;
if ((uint16)t < v->cur_speed) if (break_speed < v->cur_speed) v->cur_speed = break_speed;
v->cur_speed = t;
return true; return true;
} }

View File

@ -538,7 +538,7 @@ found_it:
return 0; return 0;
} }
static void TrainDepotMoveVehicle(Vehicle *wagon, int sel, Vehicle *head) static void TrainDepotMoveVehicle(Vehicle *wagon, VehicleID sel, Vehicle *head)
{ {
Vehicle *v; Vehicle *v;
@ -559,13 +559,13 @@ static void TrainDepotMoveVehicle(Vehicle *wagon, int sel, Vehicle *head)
if (wagon == v) if (wagon == v)
return; 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) static void TrainDepotClickTrain(Window *w, int x, int y)
{ {
GetDepotVehiclePtData gdvp; GetDepotVehiclePtData gdvp;
int mode, sel; int mode;
Vehicle *v; Vehicle *v;
mode = GetVehicleFromTrainDepotWndPt(w, x, y, &gdvp); mode = GetVehicleFromTrainDepotWndPt(w, x, y, &gdvp);
@ -576,9 +576,10 @@ static void TrainDepotClickTrain(Window *w, int x, int y)
v = gdvp.wagon; v = gdvp.wagon;
switch(mode) { switch(mode) {
case 0: // start dragging of vehicle case 0: { // start dragging of vehicle
sel = (int16)WP(w,traindepot_d).sel; VehicleID sel = WP(w, traindepot_d).sel;
if (sel != -1) {
if (sel != INVALID_VEHICLE) {
WP(w,traindepot_d).sel = INVALID_VEHICLE; WP(w,traindepot_d).sel = INVALID_VEHICLE;
TrainDepotMoveVehicle(v, sel, gdvp.head); TrainDepotMoveVehicle(v, sel, gdvp.head);
} else if (v != NULL) { } else if (v != NULL) {
@ -587,6 +588,7 @@ static void TrainDepotClickTrain(Window *w, int x, int y)
SetWindowDirty(w); SetWindowDirty(w);
} }
break; break;
}
case -1: // show info window case -1: // show info window
ShowTrainViewWindow(v); ShowTrainViewWindow(v);

View File

@ -2229,7 +2229,7 @@ void SetObjectToPlace(CursorID icon, byte mode, WindowClass window_class, Window
VpStartPreSizing(); VpStartPreSizing();
if ( (int)icon < 0) if ( (int)icon < 0)
SetAnimatedMouseCursor(_animcursors[~(int32)icon]); SetAnimatedMouseCursor(_animcursors[~icon]);
else else
SetMouseCursor(icon); SetMouseCursor(icon);
} }