summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortron <tron@openttd.org>2006-04-10 12:36:04 +0000
committertron <tron@openttd.org>2006-04-10 12:36:04 +0000
commitc5bc91eb0cf4344271e93fcc171abe62aaed67bf (patch)
tree12fb7916b2b5ea56c7e9648fb5ecfb5d29b93161
parentc7ddf7eb211057e1e5fe6d732ca06e5c24b35ba8 (diff)
downloadopenttd-c5bc91eb0cf4344271e93fcc171abe62aaed67bf.tar.xz
(svn r4344) Use tile coordinates or even TileIndices instead of virtual tile coordinates where it suffices.
-rw-r--r--landscape.c18
-rw-r--r--rail_cmd.c98
-rw-r--r--tunnelbridge_cmd.c14
3 files changed, 58 insertions, 72 deletions
diff --git a/landscape.c b/landscape.c
index e163a9abb..a86a6c463 100644
--- a/landscape.c
+++ b/landscape.c
@@ -302,19 +302,19 @@ int32 CmdClearArea(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
// make sure sx,sy are smaller than ex,ey
- ex = TileX(tile) * TILE_SIZE;
- ey = TileY(tile) * TILE_SIZE;
- sx = TileX(p1) * TILE_SIZE;
- sy = TileY(p1) * TILE_SIZE;
+ ex = TileX(tile);
+ ey = TileY(tile);
+ sx = TileX(p1);
+ sy = TileY(p1);
if (ex < sx) intswap(ex, sx);
if (ey < sy) intswap(ey, sy);
money = GetAvailableMoneyForCommand();
cost = 0;
- for (x = sx; x <= ex; x += TILE_SIZE) {
- for (y = sy; y <= ey; y += TILE_SIZE) {
- ret = DoCommand(TileVirtXY(x, y), 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR);
+ for (x = sx; x <= ex; ++x) {
+ for (y = sy; y <= ey; ++y) {
+ ret = DoCommand(TileXY(x, y), 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR);
if (CmdFailed(ret)) continue;
cost += ret;
success = true;
@@ -324,12 +324,12 @@ int32 CmdClearArea(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
_additional_cash_required = ret;
return cost - ret;
}
- DoCommand(TileVirtXY(x, y), 0, 0, flags, CMD_LANDSCAPE_CLEAR);
+ DoCommand(TileXY(x, y), 0, 0, flags, CMD_LANDSCAPE_CLEAR);
// draw explosion animation...
if ((x == sx || x == ex) && (y == sy || y == ey)) {
// big explosion in each corner, or small explosion for single tiles
- CreateEffectVehicleAbove(x + 8, y + 8, 2,
+ CreateEffectVehicleAbove(x * TILE_SIZE + 8, y * TILE_SIZE + 8, 2,
sy == ey && sx == ex ? EV_EXPLOSION_SMALL : EV_EXPLOSION_LARGE
);
}
diff --git a/rail_cmd.c b/rail_cmd.c
index f7ed7ea37..5b665812f 100644
--- a/rail_cmd.c
+++ b/rail_cmd.c
@@ -435,20 +435,23 @@ int32 CmdRemoveSingleRail(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
return cost;
}
-static const struct {
- int8 xinc[16];
- int8 yinc[16];
-} _railbit = {{
-// 0 1 2 3 4 5
- -16, 0,-16, 0, 16, 0, 0, 0,
- 16, 0, 0, 16, 0,-16, 0, 0,
-},{
- 0, 16, 0, 16, 0, 16, 0, 0,
- 0,-16,-16, 0,-16, 0, 0, 0,
-}};
-
-static int32 ValidateAutoDrag(Trackdir *trackdir, int x, int y, int ex, int ey)
+
+static const TileIndexDiffC _trackdelta[] = {
+ { -1, 0 }, { 0, 1 }, { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, 1 },
+ { 0, 0 },
+ { 0, 0 },
+ { 1, 0 }, { 0, -1 }, { 0, -1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
+ { 0, 0 },
+ { 0, 0 }
+};
+
+
+static int32 ValidateAutoDrag(Trackdir *trackdir, TileIndex start, TileIndex end)
{
+ int x = TileX(start);
+ int y = TileY(start);
+ int ex = TileX(end);
+ int ey = TileY(end);
int dx, dy, trdx, trdy;
if (!ValParamTrackOrientation(*trackdir)) return CMD_ERROR;
@@ -458,12 +461,12 @@ static int32 ValidateAutoDrag(Trackdir *trackdir, int x, int y, int ex, int ey)
dy = ey - y;
// calculate delta x,y for the first direction
- trdx = _railbit.xinc[*trackdir];
- trdy = _railbit.yinc[*trackdir];
+ trdx = _trackdelta[*trackdir].x;
+ trdy = _trackdelta[*trackdir].y;
if (!IsDiagonalTrackdir(*trackdir)) {
- trdx += _railbit.xinc[*trackdir ^ 1];
- trdy += _railbit.yinc[*trackdir ^ 1];
+ trdx += _trackdelta[*trackdir ^ 1].x;
+ trdy += _trackdelta[*trackdir ^ 1].y;
}
// validate the direction
@@ -484,8 +487,8 @@ static int32 ValidateAutoDrag(Trackdir *trackdir, int x, int y, int ex, int ey)
// (for diagonal tracks, this is already made sure of by above test), but:
// for non-diagonal tracks, check if the start and end tile are on 1 line
if (!IsDiagonalTrackdir(*trackdir)) {
- trdx = _railbit.xinc[*trackdir];
- trdy = _railbit.yinc[*trackdir];
+ trdx = _trackdelta[*trackdir].x;
+ trdy = _trackdelta[*trackdir].y;
if (abs(dx) != abs(dy) && abs(dx) + abs(trdy) != abs(dy) + abs(trdx))
return CMD_ERROR;
}
@@ -503,33 +506,26 @@ static int32 ValidateAutoDrag(Trackdir *trackdir, int x, int y, int ex, int ey)
*/
static int32 CmdRailTrackHelper(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
- int x;
- int y;
- int ex, ey;
int32 ret, total_cost = 0;
Track track = (Track)GB(p2, 4, 3);
Trackdir trackdir;
byte mode = HASBIT(p2, 7);
RailType railtype = (RailType)GB(p2, 0, 4);
+ TileIndex end_tile;
if (!ValParamRailtype(railtype) || !ValParamTrackOrientation(track)) return CMD_ERROR;
if (p1 >= MapSize()) return CMD_ERROR;
+ end_tile = p1;
trackdir = TrackToTrackdir(track);
- /* unpack end point */
- x = TileX(tile) * TILE_SIZE;
- y = TileY(tile) * TILE_SIZE;
- ex = TileX(p1) * TILE_SIZE;
- ey = TileY(p1) * TILE_SIZE;
-
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
- if (CmdFailed(ValidateAutoDrag(&trackdir, x, y, ex, ey))) return CMD_ERROR;
+ if (CmdFailed(ValidateAutoDrag(&trackdir, tile, end_tile))) return CMD_ERROR;
- if (flags & DC_EXEC) SndPlayTileFx(SND_20_SPLAT_2, TileVirtXY(x, y));
+ if (flags & DC_EXEC) SndPlayTileFx(SND_20_SPLAT_2, tile);
for (;;) {
- ret = DoCommand(TileVirtXY(x, y), railtype, TrackdirToTrack(trackdir), flags, (mode == 0) ? CMD_BUILD_SINGLE_RAIL : CMD_REMOVE_SINGLE_RAIL);
+ ret = DoCommand(tile, railtype, TrackdirToTrack(trackdir), flags, (mode == 0) ? CMD_BUILD_SINGLE_RAIL : CMD_REMOVE_SINGLE_RAIL);
if (CmdFailed(ret)) {
if ((_error_message != STR_1007_ALREADY_BUILT) && (mode == 0))
@@ -537,11 +533,9 @@ static int32 CmdRailTrackHelper(TileIndex tile, uint32 flags, uint32 p1, uint32
} else
total_cost += ret;
- if (x == ex && y == ey)
- break;
+ if (tile == end_tile) break;
- x += _railbit.xinc[trackdir];
- y += _railbit.yinc[trackdir];
+ tile += ToTileIndexDiff(_trackdelta[trackdir]);
// toggle railbit for the non-diagonal tracks
if (!IsDiagonalTrackdir(trackdir)) trackdir ^= 1;
@@ -757,12 +751,10 @@ int32 CmdBuildSingleSignal(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
*/
static int32 CmdSignalTrackHelper(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
- int x;
- int y;
- int ex, ey;
int32 ret, total_cost, signal_ctr;
byte signals;
bool error = true;
+ TileIndex end_tile;
int mode = p2 & 0x1;
Track track = GB(p2, 4, 3);
@@ -771,6 +763,7 @@ static int32 CmdSignalTrackHelper(TileIndex tile, uint32 flags, uint32 p1, uint3
byte signal_density = (p2 >> 24);
if (p1 >= MapSize()) return CMD_ERROR;
+ end_tile = p1;
if (signal_density == 0 || signal_density > 20) return CMD_ERROR;
if (!IsTileType(tile, MP_RAILWAY)) return CMD_ERROR;
@@ -782,13 +775,7 @@ static int32 CmdSignalTrackHelper(TileIndex tile, uint32 flags, uint32 p1, uint3
if (!IsDiagonalTrack(track))
signal_density *= 2;
- // unpack end tile
- x = TileX(tile) * TILE_SIZE;
- y = TileY(tile) * TILE_SIZE;
- ex = TileX(p1) * TILE_SIZE;
- ey = TileY(p1) * TILE_SIZE;
-
- if (CmdFailed(ValidateAutoDrag(&trackdir, x, y, ex, ey))) return CMD_ERROR;
+ if (CmdFailed(ValidateAutoDrag(&trackdir, tile, end_tile))) return CMD_ERROR;
track = TrackdirToTrack(trackdir); /* trackdir might have changed, keep track in sync */
@@ -814,7 +801,7 @@ static int32 CmdSignalTrackHelper(TileIndex tile, uint32 flags, uint32 p1, uint3
for (;;) {
// only build/remove signals with the specified density
if ((signal_ctr % signal_density) == 0 ) {
- ret = DoCommand(TileVirtXY(x, y), TrackdirToTrack(trackdir) | semaphores, signals, flags, (mode == 1) ? CMD_REMOVE_SIGNALS : CMD_BUILD_SIGNALS);
+ ret = DoCommand(tile, TrackdirToTrack(trackdir) | semaphores, signals, flags, (mode == 1) ? CMD_REMOVE_SIGNALS : CMD_BUILD_SIGNALS);
/* Abort placement for any other error than NOT_SUITABLE_TRACK
* This includes vehicles on track, competitor's tracks, etc. */
@@ -826,10 +813,9 @@ static int32 CmdSignalTrackHelper(TileIndex tile, uint32 flags, uint32 p1, uint3
}
}
- if (ex == x && ey == y) break; // reached end of drag
+ if (tile == end_tile) break;
- x += _railbit.xinc[trackdir];
- y += _railbit.yinc[trackdir];
+ tile += ToTileIndexDiff(_trackdelta[trackdir]);
signal_ctr++;
// toggle railbit for the non-diagonal tracks (|, -- tracks)
@@ -938,19 +924,19 @@ int32 CmdConvertRail(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
if (p1 >= MapSize()) return CMD_ERROR;
// make sure sx,sy are smaller than ex,ey
- ex = TileX(tile) * TILE_SIZE;
- ey = TileY(tile) * TILE_SIZE;
- sx = TileX(p1) * TILE_SIZE;
- sy = TileY(p1) * TILE_SIZE;
+ ex = TileX(tile);
+ ey = TileY(tile);
+ sx = TileX(p1);
+ sy = TileY(p1);
if (ex < sx) intswap(ex, sx);
if (ey < sy) intswap(ey, sy);
money = GetAvailableMoneyForCommand();
cost = 0;
- for (x = sx; x <= ex; x += TILE_SIZE) {
- for (y = sy; y <= ey; y += TILE_SIZE) {
- TileIndex tile = TileVirtXY(x, y);
+ for (x = sx; x <= ex; ++x) {
+ for (y = sy; y <= ey; ++y) {
+ TileIndex tile = TileXY(x, y);
DoConvertRailProc* proc;
switch (GetTileType(tile)) {
diff --git a/tunnelbridge_cmd.c b/tunnelbridge_cmd.c
index 712cfef6f..df7c8e594 100644
--- a/tunnelbridge_cmd.c
+++ b/tunnelbridge_cmd.c
@@ -216,10 +216,10 @@ int32 CmdBuildBridge(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2)
transport = TRANSPORT_RAIL;
}
- x = TileX(end_tile) * TILE_SIZE;
- y = TileY(end_tile) * TILE_SIZE;
- sx = TileX(p1) * TILE_SIZE;
- sy = TileY(p1) * TILE_SIZE;
+ x = TileX(end_tile);
+ y = TileY(end_tile);
+ sx = TileX(p1);
+ sy = TileY(p1);
/* check if valid, and make sure that (x,y) are smaller than (sx,sy) */
if (x == sx) {
@@ -240,12 +240,12 @@ int32 CmdBuildBridge(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2)
}
/* set and test bridge length, availability */
- bridge_len = ((sx + sy - x - y) / TILE_SIZE) - 1;
+ bridge_len = (sx + sy - x - y) - 1;
if (!CheckBridge_Stuff(bridge_type, bridge_len)) return_cmd_error(STR_5015_CAN_T_BUILD_BRIDGE_HERE);
/* retrieve landscape height and ensure it's on land */
- tile_start = TileVirtXY(x, y);
- tile_end = TileVirtXY(sx, sy);
+ tile_start = TileXY(x, y);
+ tile_end = TileXY(sx, sy);
if ((IsTileType(tile_start, MP_WATER) && _m[tile_start].m5 == 0) ||
(IsTileType(tile_end, MP_WATER) && _m[tile_end].m5 == 0)) {
return_cmd_error(STR_02A0_ENDS_OF_BRIDGE_MUST_BOTH);