1
0
Fork 0

(svn r4344) Use tile coordinates or even TileIndices instead of virtual tile coordinates where it suffices.

release/0.5
tron 2006-04-10 12:36:04 +00:00
parent 4fcd2f0643
commit 7a3345f4df
3 changed files with 57 additions and 71 deletions

View File

@ -302,19 +302,19 @@ int32 CmdClearArea(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
// make sure sx,sy are smaller than ex,ey // make sure sx,sy are smaller than ex,ey
ex = TileX(tile) * TILE_SIZE; ex = TileX(tile);
ey = TileY(tile) * TILE_SIZE; ey = TileY(tile);
sx = TileX(p1) * TILE_SIZE; sx = TileX(p1);
sy = TileY(p1) * TILE_SIZE; sy = TileY(p1);
if (ex < sx) intswap(ex, sx); if (ex < sx) intswap(ex, sx);
if (ey < sy) intswap(ey, sy); if (ey < sy) intswap(ey, sy);
money = GetAvailableMoneyForCommand(); money = GetAvailableMoneyForCommand();
cost = 0; cost = 0;
for (x = sx; x <= ex; x += TILE_SIZE) { for (x = sx; x <= ex; ++x) {
for (y = sy; y <= ey; y += TILE_SIZE) { for (y = sy; y <= ey; ++y) {
ret = DoCommand(TileVirtXY(x, y), 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR); ret = DoCommand(TileXY(x, y), 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR);
if (CmdFailed(ret)) continue; if (CmdFailed(ret)) continue;
cost += ret; cost += ret;
success = true; success = true;
@ -324,12 +324,12 @@ int32 CmdClearArea(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
_additional_cash_required = ret; _additional_cash_required = ret;
return cost - 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... // draw explosion animation...
if ((x == sx || x == ex) && (y == sy || y == ey)) { if ((x == sx || x == ex) && (y == sy || y == ey)) {
// big explosion in each corner, or small explosion for single tiles // 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 sy == ey && sx == ex ? EV_EXPLOSION_SMALL : EV_EXPLOSION_LARGE
); );
} }

View File

@ -435,20 +435,23 @@ int32 CmdRemoveSingleRail(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
return cost; 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; int dx, dy, trdx, trdy;
if (!ValParamTrackOrientation(*trackdir)) return CMD_ERROR; 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; dy = ey - y;
// calculate delta x,y for the first direction // calculate delta x,y for the first direction
trdx = _railbit.xinc[*trackdir]; trdx = _trackdelta[*trackdir].x;
trdy = _railbit.yinc[*trackdir]; trdy = _trackdelta[*trackdir].y;
if (!IsDiagonalTrackdir(*trackdir)) { if (!IsDiagonalTrackdir(*trackdir)) {
trdx += _railbit.xinc[*trackdir ^ 1]; trdx += _trackdelta[*trackdir ^ 1].x;
trdy += _railbit.yinc[*trackdir ^ 1]; trdy += _trackdelta[*trackdir ^ 1].y;
} }
// validate the direction // 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 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 // for non-diagonal tracks, check if the start and end tile are on 1 line
if (!IsDiagonalTrackdir(*trackdir)) { if (!IsDiagonalTrackdir(*trackdir)) {
trdx = _railbit.xinc[*trackdir]; trdx = _trackdelta[*trackdir].x;
trdy = _railbit.yinc[*trackdir]; trdy = _trackdelta[*trackdir].y;
if (abs(dx) != abs(dy) && abs(dx) + abs(trdy) != abs(dy) + abs(trdx)) if (abs(dx) != abs(dy) && abs(dx) + abs(trdy) != abs(dy) + abs(trdx))
return CMD_ERROR; 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) static int32 CmdRailTrackHelper(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{ {
int x;
int y;
int ex, ey;
int32 ret, total_cost = 0; int32 ret, total_cost = 0;
Track track = (Track)GB(p2, 4, 3); Track track = (Track)GB(p2, 4, 3);
Trackdir trackdir; Trackdir trackdir;
byte mode = HASBIT(p2, 7); byte mode = HASBIT(p2, 7);
RailType railtype = (RailType)GB(p2, 0, 4); RailType railtype = (RailType)GB(p2, 0, 4);
TileIndex end_tile;
if (!ValParamRailtype(railtype) || !ValParamTrackOrientation(track)) return CMD_ERROR; if (!ValParamRailtype(railtype) || !ValParamTrackOrientation(track)) return CMD_ERROR;
if (p1 >= MapSize()) return CMD_ERROR; if (p1 >= MapSize()) return CMD_ERROR;
end_tile = p1;
trackdir = TrackToTrackdir(track); 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); 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 (;;) { 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 (CmdFailed(ret)) {
if ((_error_message != STR_1007_ALREADY_BUILT) && (mode == 0)) if ((_error_message != STR_1007_ALREADY_BUILT) && (mode == 0))
@ -537,11 +533,9 @@ static int32 CmdRailTrackHelper(TileIndex tile, uint32 flags, uint32 p1, uint32
} else } else
total_cost += ret; total_cost += ret;
if (x == ex && y == ey) if (tile == end_tile) break;
break;
x += _railbit.xinc[trackdir]; tile += ToTileIndexDiff(_trackdelta[trackdir]);
y += _railbit.yinc[trackdir];
// toggle railbit for the non-diagonal tracks // toggle railbit for the non-diagonal tracks
if (!IsDiagonalTrackdir(trackdir)) trackdir ^= 1; 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) static int32 CmdSignalTrackHelper(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{ {
int x;
int y;
int ex, ey;
int32 ret, total_cost, signal_ctr; int32 ret, total_cost, signal_ctr;
byte signals; byte signals;
bool error = true; bool error = true;
TileIndex end_tile;
int mode = p2 & 0x1; int mode = p2 & 0x1;
Track track = GB(p2, 4, 3); 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); byte signal_density = (p2 >> 24);
if (p1 >= MapSize()) return CMD_ERROR; if (p1 >= MapSize()) return CMD_ERROR;
end_tile = p1;
if (signal_density == 0 || signal_density > 20) return CMD_ERROR; if (signal_density == 0 || signal_density > 20) return CMD_ERROR;
if (!IsTileType(tile, MP_RAILWAY)) 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)) if (!IsDiagonalTrack(track))
signal_density *= 2; signal_density *= 2;
// unpack end tile if (CmdFailed(ValidateAutoDrag(&trackdir, tile, end_tile))) return CMD_ERROR;
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;
track = TrackdirToTrack(trackdir); /* trackdir might have changed, keep track in sync */ 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 (;;) { for (;;) {
// only build/remove signals with the specified density // only build/remove signals with the specified density
if ((signal_ctr % signal_density) == 0 ) { 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 /* Abort placement for any other error than NOT_SUITABLE_TRACK
* This includes vehicles on track, competitor's tracks, etc. */ * 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]; tile += ToTileIndexDiff(_trackdelta[trackdir]);
y += _railbit.yinc[trackdir];
signal_ctr++; signal_ctr++;
// toggle railbit for the non-diagonal tracks (|, -- tracks) // 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; if (p1 >= MapSize()) return CMD_ERROR;
// make sure sx,sy are smaller than ex,ey // make sure sx,sy are smaller than ex,ey
ex = TileX(tile) * TILE_SIZE; ex = TileX(tile);
ey = TileY(tile) * TILE_SIZE; ey = TileY(tile);
sx = TileX(p1) * TILE_SIZE; sx = TileX(p1);
sy = TileY(p1) * TILE_SIZE; sy = TileY(p1);
if (ex < sx) intswap(ex, sx); if (ex < sx) intswap(ex, sx);
if (ey < sy) intswap(ey, sy); if (ey < sy) intswap(ey, sy);
money = GetAvailableMoneyForCommand(); money = GetAvailableMoneyForCommand();
cost = 0; cost = 0;
for (x = sx; x <= ex; x += TILE_SIZE) { for (x = sx; x <= ex; ++x) {
for (y = sy; y <= ey; y += TILE_SIZE) { for (y = sy; y <= ey; ++y) {
TileIndex tile = TileVirtXY(x, y); TileIndex tile = TileXY(x, y);
DoConvertRailProc* proc; DoConvertRailProc* proc;
switch (GetTileType(tile)) { switch (GetTileType(tile)) {

View File

@ -216,10 +216,10 @@ int32 CmdBuildBridge(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2)
transport = TRANSPORT_RAIL; transport = TRANSPORT_RAIL;
} }
x = TileX(end_tile) * TILE_SIZE; x = TileX(end_tile);
y = TileY(end_tile) * TILE_SIZE; y = TileY(end_tile);
sx = TileX(p1) * TILE_SIZE; sx = TileX(p1);
sy = TileY(p1) * TILE_SIZE; sy = TileY(p1);
/* check if valid, and make sure that (x,y) are smaller than (sx,sy) */ /* check if valid, and make sure that (x,y) are smaller than (sx,sy) */
if (x == sx) { if (x == sx) {
@ -240,12 +240,12 @@ int32 CmdBuildBridge(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2)
} }
/* set and test bridge length, availability */ /* 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); 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 */ /* retrieve landscape height and ensure it's on land */
tile_start = TileVirtXY(x, y); tile_start = TileXY(x, y);
tile_end = TileVirtXY(sx, sy); tile_end = TileXY(sx, sy);
if ((IsTileType(tile_start, MP_WATER) && _m[tile_start].m5 == 0) || if ((IsTileType(tile_start, MP_WATER) && _m[tile_start].m5 == 0) ||
(IsTileType(tile_end, MP_WATER) && _m[tile_end].m5 == 0)) { (IsTileType(tile_end, MP_WATER) && _m[tile_end].m5 == 0)) {
return_cmd_error(STR_02A0_ENDS_OF_BRIDGE_MUST_BOTH); return_cmd_error(STR_02A0_ENDS_OF_BRIDGE_MUST_BOTH);