mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-02-07 06:46:43 +00:00
(svn r8276) -Fix
Change the signature of Swap() to be less error prone, i.e. pass the variables to be swapped by reference instead of passing pointers to the variables. Just do Swap(x, y) instead of Swap(&x, &y). This prevents accidents when the variables are pointers.
This commit is contained in:
parent
7af3094bc3
commit
b2def96248
@ -2291,10 +2291,10 @@ static void AiStateBuildRail(Player *p)
|
|||||||
AiBuildRail(p);
|
AiBuildRail(p);
|
||||||
|
|
||||||
// Alternate between edges
|
// Alternate between edges
|
||||||
SwapT(&p->ai.start_tile_a, &p->ai.start_tile_b);
|
Swap(p->ai.start_tile_a, p->ai.start_tile_b);
|
||||||
SwapT(&p->ai.cur_tile_a, &p->ai.cur_tile_b);
|
Swap(p->ai.cur_tile_a, p->ai.cur_tile_b);
|
||||||
SwapT(&p->ai.start_dir_a, &p->ai.start_dir_b);
|
Swap(p->ai.start_dir_a, p->ai.start_dir_b);
|
||||||
SwapT(&p->ai.cur_dir_a, &p->ai.cur_dir_b);
|
Swap(p->ai.cur_dir_a, p->ai.cur_dir_b);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3112,10 +3112,10 @@ static void AiStateBuildRoad(Player *p)
|
|||||||
AiBuildRoad(p);
|
AiBuildRoad(p);
|
||||||
|
|
||||||
// Alternate between edges
|
// Alternate between edges
|
||||||
SwapT(&p->ai.start_tile_a, &p->ai.start_tile_b);
|
Swap(p->ai.start_tile_a, p->ai.start_tile_b);
|
||||||
SwapT(&p->ai.cur_tile_a, &p->ai.cur_tile_b);
|
Swap(p->ai.cur_tile_a, p->ai.cur_tile_b);
|
||||||
SwapT(&p->ai.start_dir_a, &p->ai.start_dir_b);
|
Swap(p->ai.start_dir_a, p->ai.start_dir_b);
|
||||||
SwapT(&p->ai.cur_dir_a, &p->ai.cur_dir_b);
|
Swap(p->ai.cur_dir_a, p->ai.cur_dir_b);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -30,14 +30,13 @@ template <typename T> FORCEINLINE T* ReallocT(T* t_ptr, size_t num_elements)
|
|||||||
return t_ptr;
|
return t_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** type safe swap operation */
|
|
||||||
template <typename T> void SwapT(T *a, T *b);
|
|
||||||
|
|
||||||
template <typename T> FORCEINLINE void SwapT(T *a, T *b)
|
/** type safe swap operation */
|
||||||
|
template<typename T> void Swap(T& a, T& b)
|
||||||
{
|
{
|
||||||
T t = *a;
|
T t = a;
|
||||||
*a = *b;
|
a = b;
|
||||||
*b = t;
|
b = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -141,11 +140,6 @@ template <typename Tenum_t> struct TinyEnumT
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Tenum_t> FORCEINLINE void SwapT(TinyEnumT<Tenum_t> *a, TinyEnumT<Tenum_t> *b)
|
|
||||||
{
|
|
||||||
SwapT(&a->m_val, &b->m_val);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> FORCEINLINE T ClrBitT(T t, int bit_index)
|
template <typename T> FORCEINLINE T ClrBitT(T t, int bit_index)
|
||||||
{
|
{
|
||||||
int val = t;
|
int val = t;
|
||||||
|
@ -310,7 +310,7 @@ static void FixOldStations(void)
|
|||||||
FOR_ALL_STATIONS(st) {
|
FOR_ALL_STATIONS(st) {
|
||||||
/* Check if we need to swap width and height for the station */
|
/* Check if we need to swap width and height for the station */
|
||||||
if (st->train_tile != 0 && GetRailStationAxis(st->train_tile) != AXIS_X) {
|
if (st->train_tile != 0 && GetRailStationAxis(st->train_tile) != AXIS_X) {
|
||||||
SwapT(&st->trainst_w, &st->trainst_h);
|
Swap(st->trainst_w, st->trainst_h);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if there is a bus or truck station, and convert to new format */
|
/* Check if there is a bus or truck station, and convert to new format */
|
||||||
|
@ -932,11 +932,11 @@ start_at:
|
|||||||
assert(tpf->nstack == 1 || tpf->nstack == 2 || tpf->nstack == 3);
|
assert(tpf->nstack == 1 || tpf->nstack == 2 || tpf->nstack == 3);
|
||||||
if (tpf->nstack != 1) {
|
if (tpf->nstack != 1) {
|
||||||
uint32 r = Random();
|
uint32 r = Random();
|
||||||
if (r&1) SwapT(&tpf->stack[0].track, &tpf->stack[1].track);
|
if (r & 1) Swap(tpf->stack[0].track, tpf->stack[1].track);
|
||||||
if (tpf->nstack != 2) {
|
if (tpf->nstack != 2) {
|
||||||
TrackdirByte t = tpf->stack[2].track;
|
TrackdirByte t = tpf->stack[2].track;
|
||||||
if (r&2) SwapT(&tpf->stack[0].track, &t);
|
if (r & 2) Swap(tpf->stack[0].track, t);
|
||||||
if (r&4) SwapT(&tpf->stack[1].track, &t);
|
if (r & 4) Swap(tpf->stack[1].track, t);
|
||||||
tpf->stack[2].first_track = tpf->stack[2].track = t;
|
tpf->stack[2].first_track = tpf->stack[2].track = t;
|
||||||
}
|
}
|
||||||
tpf->stack[0].first_track = tpf->stack[0].track;
|
tpf->stack[0].first_track = tpf->stack[0].track;
|
||||||
|
@ -1594,19 +1594,17 @@ static void ReverseTrainSwapVeh(Vehicle *v, int l, int r)
|
|||||||
a->vehstatus = tmp;
|
a->vehstatus = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* swap variables */
|
Swap(a->u.rail.track, b->u.rail.track);
|
||||||
SwapT(&a->u.rail.track, &b->u.rail.track);
|
Swap(a->direction, b->direction);
|
||||||
SwapT(&a->direction, &b->direction);
|
|
||||||
|
|
||||||
/* toggle direction */
|
/* toggle direction */
|
||||||
if (!(a->u.rail.track & 0x80)) a->direction = ReverseDir(a->direction);
|
if (!(a->u.rail.track & 0x80)) a->direction = ReverseDir(a->direction);
|
||||||
if (!(b->u.rail.track & 0x80)) b->direction = ReverseDir(b->direction);
|
if (!(b->u.rail.track & 0x80)) b->direction = ReverseDir(b->direction);
|
||||||
|
|
||||||
/* swap more variables */
|
Swap(a->x_pos, b->x_pos);
|
||||||
SwapT(&a->x_pos, &b->x_pos);
|
Swap(a->y_pos, b->y_pos);
|
||||||
SwapT(&a->y_pos, &b->y_pos);
|
Swap(a->tile, b->tile);
|
||||||
SwapT(&a->tile, &b->tile);
|
Swap(a->z_pos, b->z_pos);
|
||||||
SwapT(&a->z_pos, &b->z_pos);
|
|
||||||
|
|
||||||
SwapTrainFlags(&a->u.rail.flags, &b->u.rail.flags);
|
SwapTrainFlags(&a->u.rail.flags, &b->u.rail.flags);
|
||||||
|
|
||||||
|
@ -2065,7 +2065,7 @@ static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_t
|
|||||||
uint h0, h1, ht; // start heigth, end height, and temp variable
|
uint h0, h1, ht; // start heigth, end height, and temp variable
|
||||||
|
|
||||||
if (start_tile == end_tile) return 0;
|
if (start_tile == end_tile) return 0;
|
||||||
if (swap) SwapT(&start_tile, &end_tile);
|
if (swap) Swap(start_tile, end_tile);
|
||||||
|
|
||||||
switch (style & HT_DRAG_MASK) {
|
switch (style & HT_DRAG_MASK) {
|
||||||
case HT_RECT: {
|
case HT_RECT: {
|
||||||
@ -2125,7 +2125,7 @@ static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_t
|
|||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (swap) SwapT(&h0, &h1);
|
if (swap) Swap(h0, h1);
|
||||||
/* Minimap shows height in intervals of 50 meters, let's do the same */
|
/* Minimap shows height in intervals of 50 meters, let's do the same */
|
||||||
return (int)(h1 - h0) * 50;
|
return (int)(h1 - h0) * 50;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user