truelight@0: #include "stdafx.h" truelight@0: #include "ttd.h" truelight@0: #include "vehicle.h" truelight@0: #include "viewport.h" truelight@0: #include "command.h" truelight@0: #include "pathfind.h" truelight@0: #include "town.h" truelight@0: truelight@0: void ShowTrainDepotWindow(uint tile); truelight@0: truelight@0: enum { truelight@0: RAIL_TYPE_NORMAL = 0, truelight@0: RAIL_TYPE_SIGNALS = 0x40, truelight@0: RAIL_TYPE_SPECIAL = 0x80, // If this bit is set, then it's not a regular track. truelight@0: RAIL_TYPE_DEPOT = 0xC0, truelight@0: RAIL_TYPE_MASK = 0xC0, truelight@0: truelight@0: RAIL_BIT_DIAG1 = 1, // 0 truelight@0: RAIL_BIT_DIAG2 = 2, // 1 truelight@0: RAIL_BIT_UPPER = 4, // 2 truelight@0: RAIL_BIT_LOWER = 8, // 3 truelight@0: RAIL_BIT_LEFT = 16, // 4 truelight@0: RAIL_BIT_RIGHT = 32, // 5 truelight@0: RAIL_BIT_MASK = 0x3F, truelight@0: truelight@0: RAIL_DEPOT_TRACK_MASK = 1, truelight@0: RAIL_DEPOT_DIR = 3, truelight@0: RAIL_DEPOT_UNUSED_BITS = 0x3C, truelight@0: truelight@0: RAIL_TYPE_CHECKPOINT = 0xC4, truelight@0: RAIL_CHECKPOINT_TRACK_MASK = 1, truelight@0: RAIL_CHECKPOINT_UNUSED_BITS = 0x3E, truelight@0: }; truelight@0: truelight@0: #define IS_RAIL_DEPOT(x) (((x) & (RAIL_TYPE_DEPOT|RAIL_DEPOT_UNUSED_BITS)) == RAIL_TYPE_DEPOT) truelight@0: #define IS_RAIL_CHECKPOINT(x) (((x) & (RAIL_TYPE_CHECKPOINT|RAIL_CHECKPOINT_UNUSED_BITS)) == RAIL_TYPE_CHECKPOINT) truelight@0: truelight@0: /* Format of rail map5 byte. truelight@0: * 00 abcdef => Normal rail truelight@0: * 01 abcdef => Rail with signals truelight@0: * 10 ?????? => Unused truelight@0: * 11 ????dd => Depot truelight@0: */ truelight@0: truelight@0: /* 4 truelight@0: * --------- truelight@0: * |\ /| truelight@0: * | \ 1/ | truelight@0: * | \ / | truelight@0: * | \ / | truelight@0: * 16| \ |32 truelight@0: * | / \2 | truelight@0: * | / \ | truelight@0: * | / \ | truelight@0: * |/ \| truelight@0: * --------- truelight@0: * 8 truelight@0: */ truelight@0: truelight@0: truelight@0: // Constants for lower part of Map2 byte. truelight@0: enum RailMap2Lower4 { truelight@0: RAIL_MAP2LO_GROUND_MASK = 0xF, truelight@0: RAIL_GROUND_BROWN = 0, truelight@0: RAIL_GROUND_GREEN = 1, truelight@0: RAIL_GROUND_FENCE_NW = 2, truelight@0: RAIL_GROUND_FENCE_SE = 3, truelight@0: RAIL_GROUND_FENCE_SENW = 4, truelight@0: RAIL_GROUND_FENCE_NE = 5, truelight@0: RAIL_GROUND_FENCE_SW = 6, truelight@0: RAIL_GROUND_FENCE_NESW = 7, truelight@0: RAIL_GROUND_FENCE_VERT1 = 8, truelight@0: RAIL_GROUND_FENCE_VERT2 = 9, truelight@0: RAIL_GROUND_FENCE_HORIZ1 = 10, truelight@0: RAIL_GROUND_FENCE_HORIZ2 = 11, truelight@0: RAIL_GROUND_ICE_DESERT = 12, truelight@0: }; truelight@0: truelight@0: truelight@0: /* MAP2 byte: abcd???? => Signal On? truelight@0: * MAP3LO byte: abcd???? => Signal Exists? truelight@0: * MAP2 byte: ????abcd => Type of ground. truelight@0: * MAP3LO byte: ????abcd => Type of rail. truelight@0: * MAP5: 00abcdef => rail truelight@0: * 01abcdef => rail w/ signals truelight@0: * 10uuuuuu => unused truelight@0: * 11uuuudd => rail depot truelight@0: */ truelight@0: truelight@0: static bool CheckTrackCombination(byte map5, byte trackbits, byte flags) truelight@0: { truelight@0: _error_message = STR_1001_IMPOSSIBLE_TRACK_COMBINATION; truelight@0: truelight@0: if ((map5&RAIL_TYPE_MASK) == RAIL_TYPE_SIGNALS) { truelight@0: truelight@0: if (map5 & trackbits) { truelight@0: _error_message = STR_1007_ALREADY_BUILT; truelight@0: return false; truelight@0: } truelight@0: truelight@0: map5 |= trackbits; truelight@0: return (map5 == (RAIL_TYPE_SIGNALS|RAIL_BIT_UPPER|RAIL_BIT_LOWER) || map5 == (RAIL_TYPE_SIGNALS|RAIL_BIT_LEFT|RAIL_BIT_RIGHT)); truelight@0: truelight@0: } else if ((map5&RAIL_TYPE_MASK) == RAIL_TYPE_NORMAL) { truelight@0: _error_message = STR_1007_ALREADY_BUILT; truelight@0: if (map5 & trackbits) truelight@0: return false; truelight@0: truelight@0: // Computer players are not allowed to intersect pieces of rail. truelight@0: if (!(flags&DC_NO_RAIL_OVERLAP)) truelight@0: return true; truelight@0: truelight@0: map5 |= trackbits; truelight@0: return (map5 == (RAIL_BIT_UPPER|RAIL_BIT_LOWER) || map5 == (RAIL_BIT_LEFT|RAIL_BIT_RIGHT)); truelight@0: } else { truelight@0: return false; truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: static const byte _valid_tileh_slopes[3][15] = { truelight@0: truelight@0: // set of normal ones truelight@0: { truelight@0: RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT, truelight@0: RAIL_BIT_RIGHT, truelight@0: RAIL_BIT_UPPER, truelight@0: RAIL_BIT_DIAG1, truelight@0: truelight@0: RAIL_BIT_LEFT, truelight@0: 0, truelight@0: RAIL_BIT_DIAG2, truelight@0: RAIL_BIT_LOWER, truelight@0: truelight@0: RAIL_BIT_LOWER, truelight@0: RAIL_BIT_DIAG2, truelight@0: 0, truelight@0: RAIL_BIT_LEFT, truelight@0: truelight@0: RAIL_BIT_DIAG1, truelight@0: RAIL_BIT_UPPER, truelight@0: RAIL_BIT_RIGHT, truelight@0: }, truelight@0: truelight@0: // allowed rail for an evenly raised platform truelight@0: { truelight@0: 0, truelight@0: RAIL_BIT_LEFT, truelight@0: RAIL_BIT_LOWER, truelight@0: RAIL_BIT_DIAG2 | RAIL_BIT_LOWER | RAIL_BIT_LEFT, truelight@0: truelight@0: RAIL_BIT_RIGHT, truelight@0: RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT, truelight@0: RAIL_BIT_DIAG1 | RAIL_BIT_LOWER | RAIL_BIT_RIGHT, truelight@0: RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT, truelight@0: truelight@0: RAIL_BIT_UPPER, truelight@0: RAIL_BIT_DIAG1 | RAIL_BIT_UPPER | RAIL_BIT_LEFT, truelight@0: RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT, truelight@0: RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT, truelight@0: truelight@0: RAIL_BIT_DIAG2 | RAIL_BIT_UPPER | RAIL_BIT_RIGHT, truelight@0: RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT, truelight@0: RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT, truelight@0: }, truelight@0: truelight@0: // allowed rail on coast tile truelight@0: { truelight@0: 0, truelight@0: RAIL_BIT_LEFT, truelight@0: RAIL_BIT_LOWER, truelight@0: RAIL_BIT_DIAG2|RAIL_BIT_LEFT|RAIL_BIT_LOWER, truelight@0: truelight@0: RAIL_BIT_RIGHT, truelight@0: RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT, truelight@0: RAIL_BIT_DIAG1|RAIL_BIT_RIGHT|RAIL_BIT_LOWER, truelight@0: RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT, truelight@0: truelight@0: RAIL_BIT_UPPER, truelight@0: RAIL_BIT_DIAG1|RAIL_BIT_LEFT|RAIL_BIT_UPPER, truelight@0: RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT, truelight@0: RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT, truelight@0: truelight@0: RAIL_BIT_DIAG2|RAIL_BIT_RIGHT|RAIL_BIT_UPPER, truelight@0: RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT, truelight@0: RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT, truelight@0: } truelight@0: }; truelight@0: truelight@0: static uint GetRailFoundation(uint tileh, uint bits) truelight@0: { truelight@0: int i; truelight@0: truelight@0: if ((~_valid_tileh_slopes[0][tileh] & bits) == 0) truelight@0: return 0; truelight@0: truelight@0: if ((~_valid_tileh_slopes[1][tileh] & bits) == 0) truelight@0: return tileh; truelight@0: truelight@0: if ( ((i=0, tileh == 1) || (i+=2, tileh == 2) || (i+=2, tileh == 4) || (i+=2, tileh == 8)) && (bits == RAIL_BIT_DIAG1 || (i++, bits == RAIL_BIT_DIAG2))) truelight@0: return i + 15; truelight@0: truelight@0: return 0; truelight@0: } truelight@0: truelight@0: // truelight@0: static uint32 CheckRailSlope(int tileh, uint rail_bits, uint existing, uint tile) truelight@0: { truelight@0: // never allow building on top of steep tiles truelight@0: if (!(tileh & 0x10)) { truelight@0: rail_bits |= existing; truelight@0: truelight@0: // don't allow building on the lower side of a coast truelight@0: if (IS_TILETYPE(tile, MP_WATER) && ~_valid_tileh_slopes[2][tileh] & rail_bits) { truelight@0: return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER); truelight@0: } truelight@0: truelight@0: // no special foundation truelight@0: if ((~_valid_tileh_slopes[0][tileh] & rail_bits) == 0) truelight@0: return 0; truelight@0: truelight@0: if (((~_valid_tileh_slopes[1][tileh] & rail_bits) == 0) || // whole tile is leveled up truelight@0: ((rail_bits == RAIL_BIT_DIAG1 || rail_bits == RAIL_BIT_DIAG2) && (tileh == 1 || tileh == 2 || tileh == 4 || tileh == 8))) { // partly up truelight@0: return existing ? 0 : _price.terraform; truelight@0: } truelight@0: } truelight@0: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); truelight@0: } truelight@0: truelight@0: /* Build a single track. truelight@0: * p1 - railroad type normal/maglev truelight@0: * p2 - tile direction truelight@0: */ truelight@0: truelight@0: int32 CmdBuildSingleRail(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: TileInfo ti; truelight@0: int32 ret, cost = 0; truelight@0: byte rail_bit = 1 << p2; truelight@0: byte rail_type = (byte)(p1 & 0xF); truelight@0: uint tile; truelight@0: byte existing = 0; truelight@0: bool need_clear = false; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: truelight@0: _error_message = STR_1007_ALREADY_BUILT; truelight@0: truelight@0: FindLandscapeHeight(&ti, x, y); truelight@0: tile = ti.tile; truelight@0: truelight@0: // allow building rail under bridge truelight@0: if (ti.type != MP_TUNNELBRIDGE && !EnsureNoVehicle(tile)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (ti.type == MP_TUNNELBRIDGE) { truelight@0: /* BUILD ON BRIDGE CODE */ truelight@0: if (!EnsureNoVehicleZ(tile, GET_TILEHEIGHT(tile))) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if ((ti.map5 & 0xF8) == 0xC0) { truelight@0: if (ti.tileh & 0x10 || rail_bit != (byte)((ti.map5 & 1) ? 1 : 2)) goto need_clear; truelight@0: truelight@0: if (!(flags & DC_EXEC)) truelight@0: return _price.build_rail; truelight@0: truelight@0: _map5[tile] = (ti.map5 & 0xC7) | 0x20; truelight@0: goto set_ownership; truelight@0: } else if ((ti.map5 & 0xF8) == 0xE0) { truelight@0: if ((_map3_lo[tile] & 0xF) != (int)p1) goto need_clear; truelight@0: if (rail_bit != (byte)((ti.map5 & 1) ? 1 : 2)) goto need_clear; truelight@0: return CMD_ERROR; truelight@0: } else truelight@0: goto need_clear; truelight@0: } else if (ti.type == MP_STREET) { truelight@0: byte m5; truelight@0: /* BUILD ON STREET CODE */ truelight@0: if (ti.tileh != 0) goto need_clear; truelight@0: truelight@0: if (!(ti.map5 & 0xF0)) { truelight@0: if ((ti.map5 & 0x0F) == 0xA) { truelight@0: if (rail_bit != 2) goto need_clear; truelight@0: m5 = 0x10; truelight@0: } else if ((ti.map5 & 0x0F) == 0x5) { truelight@0: if (rail_bit != 1) goto need_clear; truelight@0: m5 = 0x18; truelight@0: } else truelight@0: goto need_clear; truelight@0: truelight@0: if (!(flags & DC_EXEC)) truelight@0: return _price.build_rail; truelight@0: truelight@0: ModifyTile(tile, truelight@0: MP_SETTYPE(MP_STREET) | truelight@0: MP_MAP3LO | MP_MAP3HI | MP_MAPOWNER_CURRENT | MP_MAP5, truelight@0: _map_owner[tile], /* map3_lo */ truelight@0: p1, /* map3_hi */ truelight@0: m5 /* map5 */ truelight@0: ); truelight@0: goto fix_signals; truelight@0: } else if (!(ti.map5 & 0xE0)) { truelight@0: if (rail_bit != (byte)((ti.map5 & 8) ? 1 : 2)) goto need_clear; truelight@0: return CMD_ERROR; truelight@0: } else truelight@0: goto need_clear; truelight@0: } else if (ti.type == MP_RAILWAY) { truelight@0: truelight@0: /* BUILD ON RAILWAY CODE */ truelight@0: if (_map_owner[tile] != _current_player || (byte)(_map3_lo[tile]&0xF) != rail_type) truelight@0: goto need_clear; truelight@0: if (!CheckTrackCombination(ti.map5, rail_bit, (byte)flags)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: existing = ti.map5 & 0x3F; truelight@0: } else { truelight@0: truelight@0: /* DEFAULT BUILD ON CODE */ truelight@0: need_clear:; truelight@0: /* isnot_railway */ truelight@0: if (!(flags & DC_EXEC)) { truelight@0: ret = DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); truelight@0: if (ret == CMD_ERROR) return CMD_ERROR; truelight@0: cost += ret; truelight@0: } truelight@0: need_clear = true; truelight@0: } truelight@0: truelight@0: ret = CheckRailSlope(ti.tileh, rail_bit, existing, tile); truelight@0: if (ret & 0x80000000) truelight@0: return ret; truelight@0: cost += ret; truelight@0: truelight@0: // the AI is not allowed to used foundationed tiles. truelight@0: if (ret && (_is_ai_player || !_patches.build_on_slopes)) truelight@0: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); truelight@0: truelight@0: if (flags & DC_EXEC && need_clear) { truelight@0: ret = DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); truelight@0: if (ret == CMD_ERROR) return CMD_ERROR; truelight@0: cost += ret; truelight@0: } truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: _map_type_and_height[tile] &= 0xF; truelight@0: _map_type_and_height[tile] |= MP_RAILWAY << 4; truelight@0: _map5[tile] |= rail_bit; truelight@0: _map2[tile] &= ~RAIL_MAP2LO_GROUND_MASK; truelight@0: truelight@0: // In case it's a tile without signals, clear the signal bits. Why? truelight@0: if ((_map5[tile] & RAIL_TYPE_MASK) != RAIL_TYPE_SIGNALS) truelight@0: _map2[tile] &= ~0xF0; truelight@0: truelight@0: set_ownership: truelight@0: _map_owner[tile] = _current_player; truelight@0: truelight@0: _map3_lo[tile] &= ~0xF; truelight@0: _map3_lo[tile] |= rail_type; truelight@0: truelight@0: MarkTileDirtyByTile(tile); truelight@0: truelight@0: fix_signals: truelight@0: SetSignalsOnBothDir(tile, (byte)p2); truelight@0: } truelight@0: truelight@0: return cost + _price.build_rail; truelight@0: } truelight@0: truelight@0: static const byte _signals_table[] = { truelight@0: 0x40, 0x40, 0x40, 0x10, 0x80, 0x20, 0, 0, // direction 1 truelight@0: 0x80, 0x80, 0x80, 0x20, 0x40, 0x10, 0, 0 // direction 2 truelight@0: }; truelight@0: truelight@0: static const byte _signals_table_other[] = { truelight@0: 0x80, 0x80, 0x80, 0x20, 0x40, 0x10, 0, 0, // direction 1 truelight@0: 0x40, 0x40, 0x40, 0x10, 0x80, 0x20, 0, 0 // direction 2 truelight@0: }; truelight@0: truelight@0: static const byte _signals_table_both[] = { truelight@0: 0xC0, 0xC0, 0xC0, 0x30, 0xC0, 0x30, 0, 0, // both directions combined truelight@0: 0xC0, 0xC0, 0xC0, 0x30, 0xC0, 0x30, 0, 0 truelight@0: }; truelight@0: truelight@0: truelight@0: /* Remove a single track. truelight@0: * p1 - unused truelight@0: * p2 - tile direction truelight@0: */ truelight@0: truelight@0: int32 CmdRemoveSingleRail(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: TileInfo ti; truelight@0: byte rail_bit = 1 << p2; truelight@0: byte m5; truelight@0: uint tile; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: truelight@0: FindLandscapeHeight(&ti, x, y); truelight@0: truelight@0: tile = ti.tile; truelight@0: truelight@0: if (!((1< sx truelight@0: 10, 9, 0,11, // y > sy truelight@0: 13, 9, 8,12, // x > sx && y > sy truelight@0: }}; truelight@0: truelight@0: truelight@0: /* Build either NE or NW sequence of tracks. truelight@0: * p1 0:15 - start pt X truelight@0: * p1 16:31 - start pt y truelight@0: * truelight@0: * p2 0:3 - rail type truelight@0: * p2 4:7 - rail direction truelight@0: */ truelight@0: truelight@0: truelight@0: int32 CmdBuildRailroadTrack(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: int sx, sy; truelight@0: int32 ret, total_cost = 0; truelight@0: int railbit; truelight@0: truelight@0: if (flags & DC_EXEC) truelight@0: SndPlayTileFx(0x1E, TILE_FROM_XY(x,y)); truelight@0: truelight@0: /* unpack start point */ truelight@0: sx = (p1 & 0xFFFF) & ~0xF; truelight@0: sy = (p1 >> 16) & ~0xF; truelight@0: truelight@0: railbit = _railbit.initial[(p2 >> 4) + (x > sx ? 4 : 0) + (y > sy ? 8 : 0)]; truelight@0: truelight@0: for(;;) { truelight@0: ret = DoCommand(x, y, p2&0xF, railbit&7, flags, CMD_BUILD_SINGLE_RAIL); truelight@0: truelight@0: if (ret == CMD_ERROR) { truelight@0: if (_error_message != STR_1007_ALREADY_BUILT) truelight@0: break; truelight@0: } else truelight@0: total_cost += ret; truelight@0: truelight@0: if (x == sx && y == sy) truelight@0: break; truelight@0: truelight@0: x += _railbit.xinc[railbit]; truelight@0: y += _railbit.yinc[railbit]; truelight@0: truelight@0: // toggle railbit for the diagonal tiles truelight@0: if (railbit & 0x6) railbit ^= 1; truelight@0: } truelight@0: truelight@0: if (total_cost == 0) truelight@0: return CMD_ERROR; truelight@0: truelight@0: return total_cost; truelight@0: } truelight@0: truelight@0: truelight@0: /* Remove either NE or NW sequence of tracks. truelight@0: * p1 0:15 - start pt X truelight@0: * p1 16:31 - start pt y truelight@0: * truelight@0: * p2 0:3 - rail type truelight@0: * p2 4:7 - rail direction truelight@0: */ truelight@0: truelight@0: truelight@0: int32 CmdRemoveRailroadTrack(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: int sx, sy; truelight@0: int32 ret, total_cost = 0; truelight@0: int railbit; truelight@0: truelight@0: if (flags & DC_EXEC) truelight@0: SndPlayTileFx(0x1E, TILE_FROM_XY(x,y)); truelight@0: truelight@0: /* unpack start point */ truelight@0: sx = (p1 & 0xFFFF) & ~0xF; truelight@0: sy = (p1 >> 16) & ~0xF; truelight@0: truelight@0: railbit = _railbit.initial[(p2 >> 4) + (x > sx ? 4 : 0) + (y > sy ? 8 : 0)]; truelight@0: truelight@0: for(;;) { truelight@0: ret = DoCommand(x, y, p2&0xF, railbit&7, flags, CMD_REMOVE_SINGLE_RAIL); truelight@0: if (ret != CMD_ERROR) total_cost += ret; truelight@0: if (x == sx && y == sy) truelight@0: break; truelight@0: x += _railbit.xinc[railbit]; truelight@0: y += _railbit.yinc[railbit]; truelight@0: // toggle railbit for the diagonal tiles truelight@0: if (railbit & 0x6) railbit ^= 1; truelight@0: } truelight@0: truelight@0: if (total_cost == 0) truelight@0: return CMD_ERROR; truelight@0: truelight@0: return total_cost; truelight@0: } truelight@0: truelight@0: /* Build a train depot truelight@0: * p1 = rail type truelight@0: * p2 = depot direction truelight@0: */ truelight@0: int32 CmdBuildTrainDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: uint tile = TILE_FROM_XY(x,y); truelight@0: int32 cost, ret; truelight@0: Depot *dep; truelight@0: uint tileh; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: truelight@0: if (!EnsureNoVehicle(tile)) return CMD_ERROR; truelight@0: truelight@0: tileh = GetTileSlope(tile, NULL); truelight@0: if (tileh != 0) { truelight@0: if (_is_ai_player || !_patches.build_on_slopes || (tileh & 0x10 || !((0x4C >> p2) & tileh) )) truelight@0: return_cmd_error(STR_0007_FLAT_LAND_REQUIRED); truelight@0: } truelight@0: truelight@0: ret = DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); truelight@0: if (ret == CMD_ERROR) return CMD_ERROR; truelight@0: cost = ret; truelight@0: truelight@0: dep = AllocateDepot(); truelight@0: if (dep == NULL) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: if (_current_player == _local_player) truelight@0: _last_built_train_depot_tile = (TileIndex)tile; truelight@0: truelight@0: ModifyTile(tile, truelight@0: MP_SETTYPE(MP_RAILWAY) | truelight@0: MP_MAP3LO | MP_MAPOWNER_CURRENT | MP_MAP5, truelight@0: p1, /* map3_lo */ truelight@0: p2 | RAIL_TYPE_DEPOT /* map5 */ truelight@0: ); truelight@0: truelight@0: dep->xy = tile; truelight@0: dep->town_index = ClosestTownFromTile(tile, (uint)-1)->index; truelight@0: truelight@0: SetSignalsOnBothDir(tile, (p2&1) ? 2 : 1); truelight@0: truelight@0: } truelight@0: truelight@0: return cost + _price.build_train_depot; truelight@0: } truelight@0: truelight@0: static void MakeDefaultCheckpointName(Checkpoint *cp) truelight@0: { truelight@0: int townidx = ClosestTownFromTile(cp->xy, (uint)-1)->index; truelight@0: Checkpoint *cc; truelight@0: bool used_checkpoint[64]; truelight@0: int i; truelight@0: truelight@0: memset(used_checkpoint, 0, sizeof(used_checkpoint)); truelight@0: truelight@0: // find an unused checkpoint number belonging to this town truelight@0: for(cc = _checkpoints; cc != endof(_checkpoints); cc++) { truelight@0: if (cc->xy && cc->town_or_string & 0xC000 && (cc->town_or_string & 0xFF) == townidx) truelight@0: used_checkpoint[(cc->town_or_string >> 8) & 0x3F] = true; truelight@0: } truelight@0: truelight@0: for(i=0; used_checkpoint[i] && i!=lengthof(used_checkpoint)-1; i++) {} truelight@0: cp->town_or_string = 0xC000 + (i << 8) + townidx; truelight@0: } truelight@0: truelight@0: // find a deleted checkpoint close to a tile. truelight@0: static Checkpoint *FindDeletedCheckpointCloseTo(uint tile) truelight@0: { truelight@0: Checkpoint *cp,*best = NULL; truelight@0: uint thres = 8, cur_dist; truelight@0: truelight@0: for(cp = _checkpoints; cp != endof(_checkpoints); cp++) { truelight@0: if (cp->deleted && cp->xy) { truelight@0: cur_dist = GetTileDist(tile, cp->xy); truelight@0: if (cur_dist < thres) { truelight@0: thres = cur_dist; truelight@0: best = cp; truelight@0: } truelight@0: } truelight@0: } truelight@0: return best; truelight@0: } truelight@0: truelight@0: /* Convert existing rail to checkpoint */ truelight@0: truelight@0: int32 CmdBuildTrainCheckpoint(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: uint tile = TILE_FROM_XY(x,y); truelight@0: Checkpoint *cp; truelight@0: uint tileh; truelight@0: uint dir; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: truelight@0: if (!IS_TILETYPE(tile, MP_RAILWAY) || ((dir=0, _map5[tile] != 1) && (dir=1, _map5[tile] != 2))) truelight@0: return_cmd_error(STR_1005_NO_SUITABLE_RAILROAD_TRACK); truelight@0: truelight@0: if (!CheckTileOwnership(tile)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (!EnsureNoVehicle(tile)) return CMD_ERROR; truelight@0: truelight@0: tileh = GetTileSlope(tile, NULL); truelight@0: if (tileh != 0) { truelight@0: if (!_patches.build_on_slopes || tileh & 0x10 || !(tileh & (0x3 << dir)) || !(tileh & ~(0x3 << dir))) truelight@0: return_cmd_error(STR_0007_FLAT_LAND_REQUIRED); truelight@0: } truelight@0: truelight@0: // check if there is an already existing, deleted, checkpoint close to us that we can reuse. truelight@0: cp = FindDeletedCheckpointCloseTo(tile); truelight@0: if (cp == NULL) { truelight@0: cp = AllocateCheckpoint(); truelight@0: if (cp == NULL) return CMD_ERROR; truelight@0: cp->town_or_string = 0; truelight@0: } truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: ModifyTile(tile, MP_MAP5, RAIL_TYPE_CHECKPOINT | dir); truelight@0: truelight@0: cp->deleted = 0; truelight@0: cp->xy = tile; truelight@0: truelight@0: if (cp->town_or_string == 0) MakeDefaultCheckpointName(cp); else RedrawCheckpointSign(cp); truelight@0: UpdateCheckpointSign(cp); truelight@0: RedrawCheckpointSign(cp); truelight@0: SetSignalsOnBothDir(tile, dir ? 2 : 1); truelight@0: } truelight@0: truelight@0: return _price.build_train_depot; truelight@0: } truelight@0: truelight@0: static void DoDeleteCheckpoint(Checkpoint *cp) truelight@0: { truelight@0: cp->xy = 0; truelight@0: DeleteCommandFromVehicleSchedule(((cp-_checkpoints) << 8) + OT_GOTO_CHECKPOINT); truelight@0: if (~cp->town_or_string & 0xC000) DeleteName(cp->town_or_string); truelight@0: RedrawCheckpointSign(cp); truelight@0: } truelight@0: truelight@0: // delete checkpoints after a while truelight@0: void CheckpointsDailyLoop() truelight@0: { truelight@0: Checkpoint *cp; truelight@0: for(cp = _checkpoints; cp != endof(_checkpoints); cp++) { truelight@0: if (cp->deleted && !--cp->deleted) { truelight@0: DoDeleteCheckpoint(cp); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static int32 RemoveTrainCheckpoint(uint tile, uint32 flags, bool justremove) truelight@0: { truelight@0: Checkpoint *cp; truelight@0: truelight@0: // make sure it's a checkpoint truelight@0: if (!IS_TILETYPE(tile, MP_RAILWAY) || !IS_RAIL_CHECKPOINT(_map5[tile])) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (!CheckTileOwnership(tile) && !(_current_player==17)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (!EnsureNoVehicle(tile)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: int direction = _map5[tile] & RAIL_CHECKPOINT_TRACK_MASK; truelight@0: truelight@0: // mark the checkpoint deleted truelight@0: for(cp=_checkpoints; cp->xy != (TileIndex)tile; cp++) {} truelight@0: cp->deleted = 30; // let it live for this many days before we do the actual deletion. truelight@0: RedrawCheckpointSign(cp); truelight@0: truelight@0: if (justremove) { truelight@0: ModifyTile(tile, MP_MAP5, 1<town_or_string & 0xC000) DeleteName(cp->town_or_string); truelight@0: cp->town_or_string = str; truelight@0: UpdateCheckpointSign(cp); truelight@0: MarkWholeScreenDirty(); truelight@0: } else { truelight@0: DeleteName(str); truelight@0: } truelight@0: } else { truelight@0: if (flags & DC_EXEC) { truelight@0: cp = &_checkpoints[p1]; truelight@0: if (~cp->town_or_string & 0xC000) DeleteName(cp->town_or_string); truelight@0: MakeDefaultCheckpointName(cp); truelight@0: UpdateCheckpointSign(cp); truelight@0: MarkWholeScreenDirty(); truelight@0: } truelight@0: } truelight@0: return 0; truelight@0: } truelight@0: truelight@0: truelight@0: /* build signals, p1 = track */ truelight@0: int32 CmdBuildSignals(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: uint tile; truelight@0: byte m5; truelight@0: int32 cost; truelight@0: int track = p1 & 0x7; truelight@0: truelight@0: assert(track >= 0 && track < 6); truelight@0: assert(p2 == 0); truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: truelight@0: tile = TILE_FROM_XY(x,y); truelight@0: truelight@0: if (!EnsureNoVehicle(tile)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: _error_message = STR_1005_NO_SUITABLE_RAILROAD_TRACK; truelight@0: truelight@0: // must be railway, and not a depot, and it must have a track in the suggested position. truelight@0: if (!IS_TILETYPE(tile, MP_RAILWAY) || (m5=_map5[tile], m5&0x80) || !HASBIT(m5, track)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: // check rail combination truelight@0: { truelight@0: byte m = m5 & RAIL_BIT_MASK; truelight@0: if (m != RAIL_BIT_DIAG1 && m != RAIL_BIT_DIAG2 && m != RAIL_BIT_UPPER && m != RAIL_BIT_LOWER && truelight@0: m != RAIL_BIT_LEFT && m != RAIL_BIT_RIGHT && m != (RAIL_BIT_UPPER|RAIL_BIT_LOWER) && m != (RAIL_BIT_LEFT|RAIL_BIT_RIGHT)) truelight@0: return CMD_ERROR; truelight@0: } truelight@0: truelight@0: // check ownership truelight@0: if (!CheckTileOwnership(tile)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: // If it had signals previously it's no cost to build. truelight@0: cost = 0; truelight@0: if (!(m5 & RAIL_TYPE_SIGNALS)) { truelight@0: cost = _price.build_signals; truelight@0: } truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: truelight@0: truelight@0: if (!(m5 & RAIL_TYPE_SIGNALS)) { truelight@0: _map5[tile] |= RAIL_TYPE_SIGNALS; // change into signals truelight@0: _map2[tile] |= 0xF0; // all signals are on truelight@0: _map3_lo[tile] &= ~0xF0; // no signals built by default truelight@0: _map3_hi[tile] = (p1 & 8) ? 4 : 0;// initial presignal state, semaphores depend on ctrl key truelight@0: goto ignore_presig; truelight@0: } truelight@0: truelight@0: if (!(p1 & 8)) { truelight@0: byte a,b,c,d; truelight@0: ignore_presig: truelight@0: a = _signals_table[track]; // signal for this track in one direction truelight@0: b = _signals_table[track + 8]; // signal for this track in the other direction truelight@0: c = a | b; truelight@0: d = _map3_lo[tile] & c; // mask of built signals. it only affects &0xF0 truelight@0: truelight@0: // Alternate between a|b, b, a truelight@0: if ( d != 0 && d != a) { truelight@0: c = (d==c)?b:a; truelight@0: } truelight@0: truelight@0: _map3_lo[tile] = (_map3_lo[tile]&~(a|b)) | c; truelight@0: } else { truelight@0: // toggle between the signal types. Using low 2 bits of map3_hi. truelight@0: _map3_hi[tile] = (_map3_hi[tile] & ~3) | ((_map3_hi[tile] + 1) & 3); truelight@0: } truelight@0: truelight@0: MarkTileDirtyByTile(tile); truelight@0: SetSignalsOnBothDir(tile, track); truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: /* Remove signals truelight@0: * p1 = unused truelight@0: * p2 = unused truelight@0: */ truelight@0: truelight@0: int32 CmdRemoveSignals(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: TileInfo ti; truelight@0: uint tile; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: truelight@0: FindLandscapeHeight(&ti, x, y); truelight@0: tile = ti.tile; truelight@0: truelight@0: /* No vehicle behind. */ truelight@0: if (!EnsureNoVehicle(tile)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: /* Signals can only exist on railways */ truelight@0: if (ti.type != MP_RAILWAY) truelight@0: return CMD_ERROR; truelight@0: truelight@0: /* Map5 mode is 0x40 when there's signals */ truelight@0: if ((ti.map5 & RAIL_TYPE_MASK) != RAIL_TYPE_SIGNALS) truelight@0: return CMD_ERROR; truelight@0: truelight@0: /* Who owns the tile? */ truelight@0: if (_current_player != OWNER_WATER && !CheckTileOwnership(tile)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: /* Do it? */ truelight@0: if (flags & DC_EXEC) { truelight@0: byte bits = _map5[tile]; truelight@0: _map5[tile] &= ~RAIL_TYPE_SIGNALS; truelight@0: _map2[tile] &= ~0xF0; truelight@0: truelight@0: /* TTDBUG: this code contains a bug, if a tile contains 2 signals truelight@0: * on separate tracks, it won't work properly for the 2nd track */ truelight@0: SetSignalsOnBothDir(tile, FIND_FIRST_BIT(bits & RAIL_BIT_MASK)); truelight@0: truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: truelight@0: return _price.remove_signals; truelight@0: } truelight@0: truelight@0: typedef int32 DoConvertRailProc(uint tile, uint totype, bool exec); truelight@0: truelight@0: static int32 DoConvertRail(uint tile, uint totype, bool exec) truelight@0: { truelight@0: if (!CheckTileOwnership(tile) || !EnsureNoVehicle(tile)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: // tile is already of requested type? truelight@0: if ( (uint)(_map3_lo[tile] & 0xF) == totype) truelight@0: return CMD_ERROR; truelight@0: truelight@0: // change type. truelight@0: if (exec) { truelight@0: _map3_lo[tile] = (_map3_lo[tile] & 0xF0) + totype; truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: truelight@0: return _price.build_rail >> 1; truelight@0: } truelight@0: truelight@0: extern int32 DoConvertStationRail(uint tile, uint totype, bool exec); truelight@0: extern int32 DoConvertStreetRail(uint tile, uint totype, bool exec); truelight@0: extern int32 DoConvertTunnelBridgeRail(uint tile, uint totype, bool exec); truelight@0: truelight@0: // p1 = start tile truelight@0: // p2 = new railtype truelight@0: int32 CmdConvertRail(int ex, int ey, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: int32 ret, cost, money; truelight@0: int sx,sy,x,y; truelight@0: truelight@0: // make sure sx,sy are smaller than ex,ey truelight@0: sx = GET_TILE_X(p1)*16; truelight@0: sy = GET_TILE_Y(p1)*16; truelight@0: if (ex < sx) intswap(ex, sx); truelight@0: if (ey < sy) intswap(ey, sy); truelight@0: truelight@0: money = GetAvailableMoneyForCommand(); truelight@0: ret = false; truelight@0: cost = 0; truelight@0: for(x=sx; x<=ex; x+=16) { truelight@0: for(y=sy; y<=ey; y+=16) { truelight@0: uint tile = TILE_FROM_XY(x,y); truelight@0: DoConvertRailProc *p; truelight@0: truelight@0: if (IS_TILETYPE(tile, MP_RAILWAY)) p = DoConvertRail; truelight@0: else if (IS_TILETYPE(tile, MP_STATION)) p = DoConvertStationRail; truelight@0: else if (IS_TILETYPE(tile, MP_STREET)) p = DoConvertStreetRail; truelight@0: else if (IS_TILETYPE(tile, MP_TUNNELBRIDGE)) p = DoConvertTunnelBridgeRail; truelight@0: else continue; truelight@0: truelight@0: ret = p(tile, p2, false); truelight@0: if (ret == CMD_ERROR) continue; truelight@0: cost += ret; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: if ( (money -= ret) < 0) { _additional_cash_required = ret; return cost - ret; } truelight@0: p(tile, p2, true); truelight@0: } truelight@0: } truelight@0: } truelight@0: if (cost == 0) cost = CMD_ERROR; truelight@0: return cost; truelight@0: } truelight@0: truelight@0: static int32 RemoveTrainDepot(uint tile, uint32 flags) truelight@0: { truelight@0: if (!CheckTileOwnership(tile) && !(_current_player==17)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (!EnsureNoVehicle(tile)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: int track = _map5[tile] & RAIL_DEPOT_TRACK_MASK; truelight@0: truelight@0: DoDeleteDepot(tile); truelight@0: SetSignalsOnBothDir(tile, track); truelight@0: } truelight@0: truelight@0: return _price.remove_train_depot; truelight@0: } truelight@0: truelight@0: static int32 ClearTile_Track(uint tile, byte flags) { truelight@0: int32 cost,ret; truelight@0: int i; truelight@0: byte m5; truelight@0: truelight@0: m5 = _map5[tile]; truelight@0: truelight@0: if (flags & DC_AUTO) { truelight@0: if (m5 & RAIL_TYPE_SPECIAL) truelight@0: return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED); truelight@0: truelight@0: if (_map_owner[tile] != _current_player) truelight@0: return_cmd_error(STR_1024_AREA_IS_OWNED_BY_ANOTHER); truelight@0: truelight@0: return_cmd_error(STR_1008_MUST_REMOVE_RAILROAD_TRACK); truelight@0: } truelight@0: truelight@0: cost = 0; truelight@0: truelight@0: truelight@0: if ( (m5 & RAIL_TYPE_MASK) == RAIL_TYPE_NORMAL) { truelight@0: regular_track:; truelight@0: for(i=0; m5; i++, m5>>=1) { truelight@0: if (m5 & 1) { truelight@0: ret = DoCommandByTile(tile, 0, i, flags, CMD_REMOVE_SINGLE_RAIL); truelight@0: if (ret == CMD_ERROR) truelight@0: return CMD_ERROR; truelight@0: cost += ret; truelight@0: } truelight@0: } truelight@0: return cost; truelight@0: truelight@0: } else if ((m5 & RAIL_TYPE_MASK)==RAIL_TYPE_SIGNALS) { truelight@0: cost = DoCommandByTile(tile, 0, 0, flags, CMD_REMOVE_SIGNALS); truelight@0: if (cost == CMD_ERROR) truelight@0: return CMD_ERROR; truelight@0: m5 &= RAIL_BIT_MASK; truelight@0: if (flags & DC_EXEC) truelight@0: goto regular_track; truelight@0: return cost + _price.remove_rail; truelight@0: } else if ( (m5 & (RAIL_TYPE_MASK|RAIL_DEPOT_UNUSED_BITS)) == RAIL_TYPE_DEPOT) { truelight@0: return RemoveTrainDepot(tile, flags); truelight@0: } else if ( (m5 & (RAIL_TYPE_MASK|RAIL_CHECKPOINT_UNUSED_BITS)) == RAIL_TYPE_CHECKPOINT) { truelight@0: return RemoveTrainCheckpoint(tile, flags, false); truelight@0: } else truelight@0: return CMD_ERROR; truelight@0: } truelight@0: truelight@0: truelight@0: truelight@0: typedef struct DrawTrackSeqStruct { truelight@0: uint16 image; truelight@0: byte subcoord_x; truelight@0: byte subcoord_y; truelight@0: byte width; truelight@0: byte height; truelight@0: } DrawTrackSeqStruct; truelight@0: truelight@0: #include "table/track_land.h" truelight@0: truelight@0: // used for presignals truelight@0: static const SpriteID _signal_base_sprites[16] = { truelight@0: 0x4FB, truelight@0: 0x1323, truelight@0: 0x1333, truelight@0: 0x1343, truelight@0: truelight@0: // use semaphores instead of signals? truelight@0: 0x1353, truelight@0: 0x1363, truelight@0: 0x1373, truelight@0: 0x1383, truelight@0: truelight@0: // mirrored versions truelight@0: 0x4FB, truelight@0: 0x1323, truelight@0: 0x1333, truelight@0: 0x1343, truelight@0: truelight@0: 0x13C6, truelight@0: 0x13D6, truelight@0: 0x13E6, truelight@0: 0x13F6, truelight@0: }; truelight@0: truelight@0: // used to determine the side of the road for the signal truelight@0: static const byte _signal_position[24] = { truelight@0: /* original: left side position */ truelight@0: 0x58,0x1E,0xE1,0xB9,0x01,0xA3,0x4B,0xEE,0x3B,0xD4,0x43,0xBD, truelight@0: /* patch: ride side position */ truelight@0: 0x1E,0xAC,0x64,0xE1,0x4A,0x10,0xEE,0xC5,0xDB,0x34,0x4D,0xB3 truelight@0: }; truelight@0: truelight@0: static void DrawSignalHelper(TileInfo *ti, byte condition, uint32 image_and_pos) truelight@0: { truelight@0: bool otherside = _opt.road_side & _patches.signal_side; truelight@0: truelight@0: uint v = _signal_position[(image_and_pos & 0xF) + (otherside ? 12 : 0)]; truelight@0: uint x = ti->x | (v&0xF); truelight@0: uint y = ti->y | (v>>4); truelight@0: uint sprite = _signal_base_sprites[(_map3_hi[ti->tile] & 7) + (otherside ? 8 : 0)] + (image_and_pos>>4) + ((condition != 0) ? 1 : 0); truelight@0: AddSortableSpriteToDraw(sprite, x, y, 1, 1, 10, GetSlopeZ(x,y)); truelight@0: } truelight@0: truelight@0: static uint32 _drawtile_track_palette; truelight@0: truelight@0: truelight@0: static void DrawTrackFence_NW(TileInfo *ti) truelight@0: { truelight@0: uint32 image = 0x515; truelight@0: if (ti->tileh != 0) { truelight@0: image = 0x519; truelight@0: if (!(ti->tileh & 2)) { truelight@0: image = 0x51B; truelight@0: } truelight@0: } truelight@0: AddSortableSpriteToDraw(image | _drawtile_track_palette, truelight@0: ti->x, ti->y+1, 16, 1, 4, ti->z); truelight@0: } truelight@0: truelight@0: static void DrawTrackFence_SE(TileInfo *ti) truelight@0: { truelight@0: uint32 image = 0x515; truelight@0: if (ti->tileh != 0) { truelight@0: image = 0x519; truelight@0: if (!(ti->tileh & 2)) { truelight@0: image = 0x51B; truelight@0: } truelight@0: } truelight@0: AddSortableSpriteToDraw(image | _drawtile_track_palette, truelight@0: ti->x, ti->y+15, 16, 1, 4, ti->z); truelight@0: } truelight@0: truelight@0: static void DrawTrackFence_NW_SE(TileInfo *ti) truelight@0: { truelight@0: DrawTrackFence_NW(ti); truelight@0: DrawTrackFence_SE(ti); truelight@0: } truelight@0: truelight@0: static void DrawTrackFence_NE(TileInfo *ti) truelight@0: { truelight@0: uint32 image = 0x516; truelight@0: if (ti->tileh != 0) { truelight@0: image = 0x51A; truelight@0: if (!(ti->tileh & 2)) { truelight@0: image = 0x51C; truelight@0: } truelight@0: } truelight@0: AddSortableSpriteToDraw(image | _drawtile_track_palette, truelight@0: ti->x+1, ti->y, 1, 16, 4, ti->z); truelight@0: } truelight@0: truelight@0: static void DrawTrackFence_SW(TileInfo *ti) truelight@0: { truelight@0: uint32 image = 0x516; truelight@0: if (ti->tileh != 0) { truelight@0: image = 0x51A; truelight@0: if (!(ti->tileh & 2)) { truelight@0: image = 0x51C; truelight@0: } truelight@0: } truelight@0: AddSortableSpriteToDraw(image | _drawtile_track_palette, truelight@0: ti->x+15, ti->y, 1, 16, 4, ti->z); truelight@0: } truelight@0: truelight@0: static void DrawTrackFence_NE_SW(TileInfo *ti) truelight@0: { truelight@0: DrawTrackFence_NE(ti); truelight@0: DrawTrackFence_SW(ti); truelight@0: } truelight@0: truelight@0: static void DrawTrackFence_NS_1(TileInfo *ti) truelight@0: { truelight@0: int z = ti->z; truelight@0: if (ti->tileh & 1) truelight@0: z += 8; truelight@0: AddSortableSpriteToDraw(0x517 | _drawtile_track_palette, truelight@0: ti->x + 8, ti->y + 8, 1, 1, 4, z); truelight@0: } truelight@0: truelight@0: static void DrawTrackFence_NS_2(TileInfo *ti) truelight@0: { truelight@0: int z = ti->z; truelight@0: if (ti->tileh & 4) truelight@0: z += 8; truelight@0: AddSortableSpriteToDraw(0x517 | _drawtile_track_palette, truelight@0: ti->x + 8, ti->y + 8, 1, 1, 4, z); truelight@0: } truelight@0: truelight@0: static void DrawTrackFence_WE_1(TileInfo *ti) truelight@0: { truelight@0: int z = ti->z; truelight@0: if (ti->tileh & 8) truelight@0: z += 8; truelight@0: AddSortableSpriteToDraw(0x518 | _drawtile_track_palette, truelight@0: ti->x + 8, ti->y + 8, 1, 1, 4, z); truelight@0: } truelight@0: truelight@0: static void DrawTrackFence_WE_2(TileInfo *ti) truelight@0: { truelight@0: int z = ti->z; truelight@0: if (ti->tileh & 2) truelight@0: z += 8; truelight@0: AddSortableSpriteToDraw(0x518 | _drawtile_track_palette, truelight@0: ti->x + 8, ti->y + 8, 1, 1, 4, z); truelight@0: } truelight@0: truelight@0: static void DetTrackDrawProc_Null(TileInfo *ti) truelight@0: { truelight@0: /* nothing should be here */ truelight@0: } truelight@0: truelight@0: typedef void DetailedTrackProc(TileInfo *ti); truelight@0: DetailedTrackProc * const _detailed_track_proc[16] = { truelight@0: DetTrackDrawProc_Null, truelight@0: DetTrackDrawProc_Null, truelight@0: truelight@0: DrawTrackFence_NW, truelight@0: DrawTrackFence_SE, truelight@0: DrawTrackFence_NW_SE, truelight@0: truelight@0: DrawTrackFence_NE, truelight@0: DrawTrackFence_SW, truelight@0: DrawTrackFence_NE_SW, truelight@0: truelight@0: DrawTrackFence_NS_1, truelight@0: DrawTrackFence_NS_2, truelight@0: truelight@0: DrawTrackFence_WE_1, truelight@0: DrawTrackFence_WE_2, truelight@0: truelight@0: DetTrackDrawProc_Null, truelight@0: DetTrackDrawProc_Null, truelight@0: DetTrackDrawProc_Null, truelight@0: DetTrackDrawProc_Null, truelight@0: }; truelight@0: truelight@0: static void DrawTile_Track(TileInfo *ti) truelight@0: { truelight@0: uint32 tracktype_offs, image; truelight@0: byte m5; truelight@0: truelight@0: _drawtile_track_palette = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_map_owner[ti->tile])); truelight@0: truelight@0: tracktype_offs = (_map3_lo[ti->tile] & 0xF) * TRACKTYPE_SPRITE_PITCH; truelight@0: truelight@0: m5 = (byte)ti->map5; truelight@0: if (!(m5 & RAIL_TYPE_SPECIAL)) { truelight@0: bool special; truelight@0: truelight@0: m5 &= RAIL_BIT_MASK; truelight@0: truelight@0: special = false; truelight@0: truelight@0: // select the sprite to use based on the map5 byte. truelight@0: (image = 0x3F3, m5 == RAIL_BIT_DIAG2) || truelight@0: (image++, m5 == RAIL_BIT_DIAG1) || truelight@0: (image++, m5 == RAIL_BIT_UPPER) || truelight@0: (image++, m5 == RAIL_BIT_LOWER) || truelight@0: (image++, m5 == RAIL_BIT_RIGHT) || truelight@0: (image++, m5 == RAIL_BIT_LEFT) || truelight@0: (image++, m5 == (RAIL_BIT_DIAG1|RAIL_BIT_DIAG2)) || truelight@0: truelight@0: (image = 0x40B, m5 == (RAIL_BIT_UPPER|RAIL_BIT_LOWER)) || truelight@0: (image++, m5 == (RAIL_BIT_LEFT|RAIL_BIT_RIGHT)) || truelight@0: truelight@0: (special=true, false) || truelight@0: truelight@0: (image = 0x3FA, !(m5 & (RAIL_BIT_RIGHT|RAIL_BIT_UPPER|RAIL_BIT_DIAG1))) || truelight@0: (image++, !(m5 & (RAIL_BIT_LEFT|RAIL_BIT_LOWER|RAIL_BIT_DIAG1))) || truelight@0: (image++, !(m5 & (RAIL_BIT_LEFT|RAIL_BIT_UPPER|RAIL_BIT_DIAG2))) || truelight@0: (image++, !(m5 & (RAIL_BIT_RIGHT|RAIL_BIT_LOWER|RAIL_BIT_DIAG2))) || truelight@0: (image++, true); truelight@0: truelight@0: if (ti->tileh != 0) { truelight@0: int f = GetRailFoundation(ti->tileh, ti->map5 & 0x3F); truelight@0: if (f) DrawFoundation(ti, f); truelight@0: truelight@0: // default sloped sprites.. truelight@0: if (ti->tileh != 0) image = _track_sloped_sprites[ti->tileh - 1] + 0x3F3; truelight@0: } truelight@0: truelight@0: if ((_map2[ti->tile] & RAIL_MAP2LO_GROUND_MASK)==RAIL_GROUND_BROWN) truelight@0: image = (image & 0xFFFF) | 0x3178000; // use a brown palette truelight@0: else if ((_map2[ti->tile] & RAIL_MAP2LO_GROUND_MASK)==RAIL_GROUND_ICE_DESERT) truelight@0: image += 26; truelight@0: truelight@0: DrawGroundSprite(image + tracktype_offs); truelight@0: truelight@0: if (special) { truelight@0: if (m5 & RAIL_BIT_DIAG1) DrawGroundSprite(0x3ED + tracktype_offs); truelight@0: if (m5 & RAIL_BIT_DIAG2) DrawGroundSprite(0x3EE + tracktype_offs); truelight@0: if (m5 & RAIL_BIT_UPPER) DrawGroundSprite(0x3EF + tracktype_offs); truelight@0: if (m5 & RAIL_BIT_LOWER) DrawGroundSprite(0x3F0 + tracktype_offs); truelight@0: if (m5 & RAIL_BIT_LEFT) DrawGroundSprite(0x3F2 + tracktype_offs); truelight@0: if (m5 & RAIL_BIT_RIGHT) DrawGroundSprite(0x3F1 + tracktype_offs); truelight@0: } truelight@0: truelight@0: if (_display_opt & DO_FULL_DETAIL) { truelight@0: _detailed_track_proc[_map2[ti->tile] & RAIL_MAP2LO_GROUND_MASK](ti); truelight@0: } truelight@0: truelight@0: /* draw signals also? */ truelight@0: if (!(ti->map5 & RAIL_TYPE_SIGNALS)) truelight@0: return; truelight@0: truelight@0: { truelight@0: byte m23; truelight@0: truelight@0: m23 = (_map3_lo[ti->tile] >> 4) | (_map2[ti->tile] & 0xF0); truelight@0: truelight@0: #define HAS_SIGNAL(x) (m23 & (byte)(0x1 << (x))) truelight@0: #define ISON_SIGNAL(x) (m23 & (byte)(0x10 << (x))) truelight@0: #define MAYBE_DRAW_SIGNAL(x,y,z) if (HAS_SIGNAL(x)) DrawSignalHelper(ti, ISON_SIGNAL(x), ((y-0x4FB) << 4)|(z)) truelight@0: truelight@0: if (!(m5 & RAIL_BIT_DIAG2)) { truelight@0: if (!(m5 & RAIL_BIT_DIAG1)) { truelight@0: if (m5 & RAIL_BIT_LEFT) { truelight@0: MAYBE_DRAW_SIGNAL(2, 0x509, 0); truelight@0: MAYBE_DRAW_SIGNAL(3, 0x507, 1); truelight@0: } truelight@0: if (m5 & RAIL_BIT_RIGHT) { truelight@0: MAYBE_DRAW_SIGNAL(0, 0x509, 2); truelight@0: MAYBE_DRAW_SIGNAL(1, 0x507, 3); truelight@0: } truelight@0: if (m5 & RAIL_BIT_UPPER) { truelight@0: MAYBE_DRAW_SIGNAL(3, 0x505, 4); truelight@0: MAYBE_DRAW_SIGNAL(2, 0x503, 5); truelight@0: } truelight@0: if (m5 & RAIL_BIT_LOWER) { truelight@0: MAYBE_DRAW_SIGNAL(1, 0x505, 6); truelight@0: MAYBE_DRAW_SIGNAL(0, 0x503, 7); truelight@0: } truelight@0: } else { truelight@0: MAYBE_DRAW_SIGNAL(3, 0x4FB, 8); truelight@0: MAYBE_DRAW_SIGNAL(2, 0x4FD, 9); truelight@0: } truelight@0: } else { truelight@0: MAYBE_DRAW_SIGNAL(3, 0x4FF, 10); truelight@0: MAYBE_DRAW_SIGNAL(2, 0x501, 11); truelight@0: } truelight@0: } truelight@0: } else { truelight@0: const byte *s; truelight@0: const DrawTrackSeqStruct *drss; truelight@0: truelight@0: if (!(m5 & (RAIL_TYPE_MASK&~RAIL_TYPE_SPECIAL))) truelight@0: return; truelight@0: truelight@0: if (ti->tileh != 0) { DrawFoundation(ti, ti->tileh); } truelight@0: truelight@0: s = _track_depot_layout_table[m5 & 0x3F]; truelight@0: truelight@0: image = *(uint16*)s; truelight@0: if (image & 0x8000) image = (image & 0x7FFF) + tracktype_offs; truelight@0: truelight@0: // adjust ground tile for desert truelight@0: // (don't adjust for arctic, because snow in depots looks weird) truelight@0: if ((_map2[ti->tile] & RAIL_MAP2LO_GROUND_MASK)==RAIL_GROUND_ICE_DESERT && _opt.landscape == LT_DESERT) truelight@0: { truelight@0: if(image!=3981) truelight@0: image += 26; // tile with tracks truelight@0: else truelight@0: image = 4550; // flat ground truelight@0: } truelight@0: truelight@0: DrawGroundSprite(image); truelight@0: truelight@0: drss = (const DrawTrackSeqStruct*)(s + sizeof(uint16)); truelight@0: truelight@0: while ((image=drss->image) != 0) { truelight@0: if (image & 0x8000) truelight@0: image |= _drawtile_track_palette; truelight@0: image += tracktype_offs; truelight@0: if (!(_display_opt & DO_TRANS_BUILDINGS)) truelight@0: image = (image & 0x3FFF) | 0x3224000; truelight@0: AddSortableSpriteToDraw(image, ti->x | drss->subcoord_x, truelight@0: ti->y | drss->subcoord_y, drss->width, drss->height, 0x17, ti->z); truelight@0: drss++; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: void DrawTrainDepotSprite(int x, int y, int image, int railtype) truelight@0: { truelight@0: uint32 ormod, img; truelight@0: const DrawTrackSeqStruct *dtss; truelight@0: const byte *t; truelight@0: truelight@0: /* baseimage */ truelight@0: railtype *= TRACKTYPE_SPRITE_PITCH; truelight@0: truelight@0: ormod = PLAYER_SPRITE_COLOR(_local_player); truelight@0: truelight@0: t = _track_depot_layout_table[image]; truelight@0: truelight@0: x+=33; truelight@0: y+=17; truelight@0: truelight@0: img = *(uint16*)t; truelight@0: if (img & 0x8000) img = (img & 0x7FFF) + railtype; truelight@0: DrawSprite(img, x, y); truelight@0: truelight@0: for(dtss = (DrawTrackSeqStruct *)(t + sizeof(uint16)); dtss->image != 0; dtss++) { truelight@0: Point pt = RemapCoords(dtss->subcoord_x, dtss->subcoord_y, 0); truelight@0: image = dtss->image; truelight@0: if (image & 0x8000) image |= ormod; truelight@0: DrawSprite(image + railtype, x + pt.x, y + pt.y); truelight@0: } truelight@0: } truelight@0: truelight@0: #define NUM_SSD_ENTRY 256 truelight@0: #define NUM_SSD_STACK 32 truelight@0: truelight@0: typedef struct SetSignalsData { truelight@0: int cur; truelight@0: int cur_stack; truelight@0: bool stop; truelight@0: bool has_presignal; truelight@0: truelight@0: // presignal info truelight@0: int presignal_exits; truelight@0: int presignal_exits_free; truelight@0: truelight@0: // these are used to keep track of the signals that change. truelight@0: byte bit[NUM_SSD_ENTRY]; truelight@0: TileIndex tile[NUM_SSD_ENTRY]; truelight@0: truelight@0: // these are used to keep track of the stack that modifies presignals recursively truelight@0: TileIndex next_tile[NUM_SSD_STACK]; truelight@0: byte next_dir[NUM_SSD_STACK]; truelight@0: truelight@0: } SetSignalsData; truelight@0: truelight@0: static bool SetSignalsEnumProc(uint tile, SetSignalsData *ssd, int track, uint length, byte *state) truelight@0: { truelight@0: // the tile has signals? truelight@0: if (IS_TILETYPE(tile, MP_RAILWAY)) { truelight@0: if ((_map5[tile]&RAIL_TYPE_MASK) == RAIL_TYPE_SIGNALS) { truelight@0: if ((_map3_lo[tile] & _signals_table_both[track]) != 0) { truelight@0: truelight@0: // is the signal pointing in to the segment existing? truelight@0: if ((_map3_lo[tile] & _signals_table[track]) != 0) { truelight@0: // yes, add the signal to the list of signals truelight@0: if (ssd->cur != NUM_SSD_ENTRY) { truelight@0: ssd->tile[ssd->cur] = tile; // remember the tile index truelight@0: ssd->bit[ssd->cur] = track; // and the controlling bit number truelight@0: ssd->cur++; truelight@0: } truelight@0: truelight@0: // remember if this block has a presignal. truelight@0: ssd->has_presignal |= (_map3_hi[tile]&1); truelight@0: } truelight@0: truelight@0: // is this an exit signal that points out from the segment? truelight@0: if ((_map3_hi[tile]&2) && _map3_lo[tile]&_signals_table_other[track]) { truelight@0: ssd->presignal_exits++; truelight@0: if ((_map2[tile]&_signals_table_other[track]) != 0) truelight@0: ssd->presignal_exits_free++; truelight@0: } truelight@0: truelight@0: return true; truelight@0: } truelight@0: } else if (IS_RAIL_DEPOT(_map5[tile])) truelight@0: return true; // don't look further if the tile is a depot truelight@0: } truelight@0: return false; truelight@0: } truelight@0: truelight@0: static void SetSignalsAfterProc(TrackPathFinder *tpf) truelight@0: { truelight@0: SetSignalsData *ssd = tpf->userdata; truelight@0: Vehicle *v; truelight@0: uint tile, hash, val, offs; truelight@0: TrackPathFinderLink *link; truelight@0: truelight@0: ssd->stop = false; truelight@0: truelight@0: // for each train, check if it is in the segment. truelight@0: // then we know that the signal should be red. truelight@0: FOR_ALL_VEHICLES(v) { truelight@0: if (v->type != VEH_Train) truelight@0: continue; truelight@0: truelight@0: tile = v->tile; truelight@0: if (v->u.rail.track == 0x40) { tile = GetVehicleOutOfTunnelTile(v); } truelight@0: truelight@0: hash = PATHFIND_HASH_TILE(tile); truelight@0: truelight@0: val = tpf->hash_head[hash]; truelight@0: if (val == 0) truelight@0: continue; truelight@0: truelight@0: if (!(val & 0x8000)) { truelight@0: if ((TileIndex)tile == tpf->hash_tile[hash]) truelight@0: goto check_val; truelight@0: } else { truelight@0: offs = tpf->hash_tile[hash]; truelight@0: do { truelight@0: link = PATHFIND_GET_LINK_PTR(tpf, offs); truelight@0: if ( (TileIndex)tile == link->tile) { truelight@0: val = link->flags; truelight@0: check_val:; truelight@0: // the train is on the track, in either direction? truelight@0: if (val & (v->u.rail.track + (v->u.rail.track<<8))) { truelight@0: ssd->stop = true; truelight@0: return; truelight@0: } truelight@0: break; truelight@0: } truelight@0: } while ((offs=link->next) != 0xFFFF); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static const byte _dir_from_track[14] = { truelight@0: 0,1,0,1,2,1, 0,0, truelight@0: 2,3,3,2,3,0, truelight@0: }; truelight@0: truelight@0: truelight@0: static void ChangeSignalStates(SetSignalsData *ssd) truelight@0: { truelight@0: int i; truelight@0: truelight@0: // thinking about presignals... truelight@0: // the presignal is green if, truelight@0: // if no train is in the segment AND truelight@0: // there is at least one green exit signal OR truelight@0: // there are no exit signals in the segment truelight@0: truelight@0: // then mark the signals in the segment accordingly truelight@0: for(i=0; i!=ssd->cur; i++) { truelight@0: uint tile = ssd->tile[i]; truelight@0: byte bit = _signals_table[ssd->bit[i]]; truelight@0: byte m2 = _map2[tile]; truelight@0: truelight@0: // presignals don't turn green if there is at least one presignal exit and none are free truelight@0: if (_map3_hi[tile] & 1) { truelight@0: int ex = ssd->presignal_exits, exfree = ssd->presignal_exits_free; truelight@0: truelight@0: // subtract for dual combo signals so they don't count themselves truelight@0: if (_map3_hi[tile]&2 && _map3_lo[tile]&_signals_table_other[ssd->bit[i]]) { truelight@0: ex--; truelight@0: if ((_map2[tile]&_signals_table_other[ssd->bit[i]]) != 0) exfree--; truelight@0: } truelight@0: truelight@0: // if we have exits and none are free, make red. truelight@0: if (ex && !exfree) goto make_red; truelight@0: } truelight@0: truelight@0: // check if the signal is unaffected. truelight@0: if (ssd->stop) { truelight@0: make_red: truelight@0: // turn red truelight@0: if ( (bit&m2) == 0 ) truelight@0: continue; truelight@0: } else { truelight@0: // turn green truelight@0: if ( (bit&m2) != 0 ) truelight@0: continue; truelight@0: } truelight@0: truelight@0: // Update signals on the other side of this exit signal, it changed. truelight@0: // If this segment has presignals, then we treat exit signals going into the segment as normal signals. truelight@0: if (_map3_hi[tile]&2 && (_map3_hi[tile]&1 || !ssd->has_presignal)) { truelight@0: if (ssd->cur_stack != NUM_SSD_STACK) { truelight@0: ssd->next_tile[ssd->cur_stack] = tile; truelight@0: ssd->next_dir[ssd->cur_stack] = _dir_from_track[ssd->bit[i]]; truelight@0: ssd->cur_stack++; truelight@0: } else { truelight@0: printf("NUM_SSD_STACK too small\n"); truelight@0: } truelight@0: } truelight@0: truelight@0: // it changed, so toggle it truelight@0: _map2[tile] = m2 ^ bit; truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: bool UpdateSignalsOnSegment(uint tile, byte direction) truelight@0: { truelight@0: SetSignalsData ssd; truelight@0: int result = -1; truelight@0: truelight@0: ssd.cur_stack = 0; truelight@0: direction>>=1; truelight@0: truelight@0: for(;;) { truelight@0: // go through one segment and update all signals pointing into that segment. truelight@0: ssd.cur = ssd.presignal_exits = ssd.presignal_exits_free = 0; truelight@0: ssd.has_presignal = false; truelight@0: truelight@0: FollowTrack(tile, 0xC000, direction, (TPFEnumProc*)SetSignalsEnumProc, SetSignalsAfterProc, &ssd); truelight@0: ChangeSignalStates(&ssd); truelight@0: truelight@0: // remember the result only for the first iteration. truelight@0: if (result < 0) result = ssd.stop; truelight@0: truelight@0: // if any exit signals were changed, we need to keep going to modify the stuff behind those. truelight@0: if(!ssd.cur_stack) truelight@0: break; truelight@0: truelight@0: // one or more exit signals were changed, so we need to update another segment too. truelight@0: tile = ssd.next_tile[--ssd.cur_stack]; truelight@0: direction = ssd.next_dir[ssd.cur_stack]; truelight@0: } truelight@0: truelight@0: return (bool)result; truelight@0: } truelight@0: truelight@0: void SetSignalsOnBothDir(uint tile, byte track) truelight@0: { truelight@0: static const byte _search_dir_1[6] = {1, 3, 1, 3, 5, 3}; truelight@0: static const byte _search_dir_2[6] = {5, 7, 7, 5, 7, 1}; truelight@0: truelight@0: UpdateSignalsOnSegment(tile, _search_dir_1[track]); truelight@0: UpdateSignalsOnSegment(tile, _search_dir_2[track]); truelight@0: } truelight@0: truelight@0: uint GetSlopeZ_Track(TileInfo *ti) truelight@0: { truelight@0: uint z = ti->z; truelight@0: int th = ti->tileh; truelight@0: truelight@0: // check if it's a foundation truelight@0: if (ti->tileh != 0) { truelight@0: if ((ti->map5 & 0x80) == 0) { truelight@0: uint f = GetRailFoundation(ti->tileh, ti->map5 & 0x3F); truelight@0: if (f != 0) { truelight@0: if (f < 15) { truelight@0: // leveled foundation truelight@0: return z + 8; truelight@0: } truelight@0: // inclined foundation truelight@0: th = _inclined_tileh[f - 15]; truelight@0: } truelight@0: } else if ((ti->map5 & 0xC0) == 0xC0) { truelight@0: // depot or checkpoint truelight@0: return z + 8; truelight@0: } truelight@0: return GetPartialZ(ti->x&0xF, ti->y&0xF, th) + z; truelight@0: } truelight@0: return z; truelight@0: } truelight@0: truelight@0: static void GetAcceptedCargo_Track(uint tile, AcceptedCargo *ac) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: truelight@0: static void AnimateTile_Track(uint tile) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: truelight@0: static void TileLoop_Track(uint tile) truelight@0: { truelight@0: byte a2; truelight@0: byte rail; truelight@0: byte m2; truelight@0: byte owner; truelight@0: truelight@0: m2 = _map2[tile] & 0xF; truelight@0: truelight@0: /* special code for alps landscape */ truelight@0: if (_opt.landscape == LT_HILLY) { truelight@0: /* convert into snow? */ truelight@0: if (GetTileZ(tile) > _opt.snow_line) { truelight@0: a2 = RAIL_GROUND_ICE_DESERT; truelight@0: goto modify_me; truelight@0: } truelight@0: truelight@0: /* special code for desert landscape */ truelight@0: } else if (_opt.landscape == LT_DESERT) { truelight@0: /* convert into desert? */ truelight@0: if (GetMapExtraBits(tile) == 1) { truelight@0: a2 = RAIL_GROUND_ICE_DESERT; truelight@0: goto modify_me; truelight@0: } truelight@0: } truelight@0: truelight@0: // Don't continue tile loop for depots truelight@0: if (_map5[tile] & RAIL_TYPE_SPECIAL) truelight@0: return; truelight@0: truelight@0: a2 = RAIL_GROUND_GREEN; truelight@0: truelight@0: if (m2 != RAIL_GROUND_BROWN) { /* wait until bottom is green */ truelight@0: /* determine direction of fence */ truelight@0: rail = _map5[tile] & RAIL_BIT_MASK; truelight@0: truelight@0: if (rail == RAIL_BIT_UPPER) { truelight@0: a2 = RAIL_GROUND_FENCE_HORIZ1; truelight@0: } else if (rail == RAIL_BIT_LOWER) { truelight@0: a2 = RAIL_GROUND_FENCE_HORIZ2; truelight@0: } else if (rail == RAIL_BIT_LEFT) { truelight@0: a2 = RAIL_GROUND_FENCE_VERT1; truelight@0: } else if (rail == RAIL_BIT_RIGHT) { truelight@0: a2 = RAIL_GROUND_FENCE_VERT2; truelight@0: } else { truelight@0: owner = _map_owner[tile]; truelight@0: truelight@0: if ( (!(rail&(RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LEFT)) && (rail&RAIL_BIT_DIAG1)) || rail==(RAIL_BIT_LOWER|RAIL_BIT_RIGHT)) { truelight@0: if (!IS_TILETYPE(tile + TILE_XY(0,-1), MP_RAILWAY) || truelight@0: owner != _map_owner[tile + TILE_XY(0,-1)] || truelight@0: (_map5[tile + TILE_XY(0,-1)]==RAIL_BIT_UPPER || _map5[tile + TILE_XY(0,-1)]==RAIL_BIT_LEFT)) truelight@0: a2 = RAIL_GROUND_FENCE_NW; truelight@0: } truelight@0: truelight@0: if ( (!(rail&(RAIL_BIT_DIAG2|RAIL_BIT_LOWER|RAIL_BIT_RIGHT)) && (rail&RAIL_BIT_DIAG1)) || rail==(RAIL_BIT_UPPER|RAIL_BIT_LEFT)) { truelight@0: if (!IS_TILETYPE(tile + TILE_XY(0,1), MP_RAILWAY) || truelight@0: owner != _map_owner[tile + TILE_XY(0,1)] || truelight@0: (_map5[tile + TILE_XY(0,1)]==RAIL_BIT_LOWER || _map5[tile + TILE_XY(0,1)]==RAIL_BIT_RIGHT)) truelight@0: a2 = (a2 == RAIL_GROUND_FENCE_NW) ? RAIL_GROUND_FENCE_SENW : RAIL_GROUND_FENCE_SE; truelight@0: } truelight@0: truelight@0: if ( (!(rail&(RAIL_BIT_DIAG1|RAIL_BIT_UPPER|RAIL_BIT_RIGHT)) && (rail&RAIL_BIT_DIAG2)) || rail==(RAIL_BIT_LOWER|RAIL_BIT_LEFT)) { truelight@0: if (!IS_TILETYPE(tile + TILE_XY(-1,0), MP_RAILWAY) || truelight@0: owner != _map_owner[tile + TILE_XY(-1,0)] || truelight@0: (_map5[tile + TILE_XY(-1,0)]==RAIL_BIT_UPPER || _map5[tile + TILE_XY(-1,0)]==RAIL_BIT_RIGHT)) truelight@0: a2 = RAIL_GROUND_FENCE_NE; truelight@0: } truelight@0: truelight@0: if ( (!(rail&(RAIL_BIT_DIAG1|RAIL_BIT_LOWER|RAIL_BIT_LEFT)) && (rail&RAIL_BIT_DIAG2)) || rail==(RAIL_BIT_UPPER|RAIL_BIT_RIGHT)) { truelight@0: if (!IS_TILETYPE(tile + TILE_XY(1,0), MP_RAILWAY) || truelight@0: owner != _map_owner[tile + TILE_XY(1,0)] || truelight@0: (_map5[tile + TILE_XY(1,0)]==RAIL_BIT_LOWER || _map5[tile + TILE_XY(1,0)]==RAIL_BIT_LEFT)) truelight@0: a2 = (a2 == RAIL_GROUND_FENCE_NE) ? RAIL_GROUND_FENCE_NESW : RAIL_GROUND_FENCE_SW; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: modify_me:; truelight@0: /* tile changed? */ truelight@0: if ( m2 != a2) { truelight@0: _map2[tile] = (_map2[tile] & ~RAIL_MAP2LO_GROUND_MASK) | a2; truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: static uint32 GetTileTrackStatus_Track(uint tile, int mode) { truelight@0: byte m5, a, b; truelight@0: uint32 ret; truelight@0: truelight@0: if (mode != 0) truelight@0: return 0; truelight@0: truelight@0: m5 = _map5[tile]; truelight@0: truelight@0: if (!(m5 & RAIL_TYPE_SPECIAL)) { truelight@0: ret = (m5 | (m5 << 8)) & 0x3F3F; truelight@0: if (!(m5 & RAIL_TYPE_SIGNALS)) { truelight@0: if ( (ret & 0xFF) == 3) truelight@0: ret |= 0x40; truelight@0: } else { truelight@0: /* has_signals */ truelight@0: truelight@0: a = _map3_lo[tile]; truelight@0: b = _map2[tile]; truelight@0: truelight@0: b &= a; truelight@0: truelight@0: if ((a & 0xC0) == 0) { b |= 0xC0; } truelight@0: if ((a & 0x30) == 0) { b |= 0x30; } truelight@0: truelight@0: if ( (b & 0x80) == 0) ret |= 0x10070000; truelight@0: if ( (b & 0x40) == 0) ret |= 0x7100000; truelight@0: if ( (b & 0x20) == 0) ret |= 0x20080000; truelight@0: if ( (b & 0x10) == 0) ret |= 0x8200000; truelight@0: } truelight@0: } else if (m5 & 0x40) { truelight@0: static const byte _train_spec_tracks[6] = {1,2,1,2,1,2}; truelight@0: m5 = _train_spec_tracks[m5 & 0x3F]; truelight@0: ret = (m5 << 8) + m5; truelight@0: } else truelight@0: return 0; truelight@0: truelight@0: return ret; truelight@0: } truelight@0: truelight@0: static void ClickTile_Track(uint tile) truelight@0: { truelight@0: if (IS_RAIL_DEPOT(_map5[tile])) truelight@0: ShowTrainDepotWindow(tile); truelight@0: else if (IS_RAIL_CHECKPOINT(_map5[tile])) truelight@0: ShowRenameCheckpointWindow(&_checkpoints[GetCheckpointByTile(tile)]); truelight@0: truelight@0: } truelight@0: truelight@0: static void GetTileDesc_Track(uint tile, TileDesc *td) truelight@0: { truelight@0: byte m5 = _map5[tile]; truelight@0: if (!(m5 & 0x80)) { truelight@0: if(!(m5 & 0x40)) // no signals truelight@0: td->str = STR_1021_RAILROAD_TRACK; truelight@0: else truelight@0: { truelight@0: switch(_map3_hi[tile] & 0x03) truelight@0: { truelight@0: case 0: td->str = STR_RAILROAD_TRACK_WITH_NORMAL_SIGNALS; truelight@0: break; truelight@0: case 1: td->str = STR_RAILROAD_TRACK_WITH_PRESIGNALS; truelight@0: break; truelight@0: case 2: td->str = STR_RAILROAD_TRACK_WITH_EXITSIGNALS; truelight@0: break; truelight@0: case 3: td->str = STR_RAILROAD_TRACK_WITH_COMBOSIGNALS; truelight@0: break; truelight@0: } truelight@0: } truelight@0: } else { truelight@0: td->str = m5 < 0xC4 ? STR_1023_RAILROAD_TRAIN_DEPOT : STR_LANDINFO_CHECKPOINT; truelight@0: } truelight@0: td->owner = _map_owner[tile]; truelight@0: } truelight@0: truelight@0: static void ChangeTileOwner_Track(uint tile, byte old_player, byte new_player) truelight@0: { truelight@0: if (_map_owner[tile] != old_player) truelight@0: return; truelight@0: truelight@0: if (new_player != 255) { truelight@0: _map_owner[tile] = new_player; truelight@0: } else { truelight@0: DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@0: } truelight@0: } truelight@0: truelight@0: static const byte _fractcoords_behind[4] = { 0x8F, 0x8, 0x80, 0xF8 }; truelight@0: static const byte _fractcoords_enter[4] = { 0x8A, 0x48, 0x84, 0xA8 }; truelight@0: static const byte _fractcoords_leave[4] = { 0x81, 0xD8, 0x8D, 0x18 }; truelight@0: static const byte _enter_directions[4] = {5, 7, 1, 3}; truelight@0: static const byte _leave_directions[4] = {1, 3, 5, 7}; truelight@0: static const byte _depot_track_mask[4] = {1, 2, 1, 2}; truelight@0: truelight@0: uint32 VehicleEnter_Track(Vehicle *v, uint tile, int x, int y) truelight@0: { truelight@0: byte fract_coord; truelight@0: int dir; truelight@0: truelight@0: // this routine applies only to trains in depot tiles truelight@0: if (v->type != VEH_Train || !IS_RAIL_DEPOT(_map5[tile])) truelight@0: return 0; truelight@0: truelight@0: /* depot direction */ truelight@0: dir = _map5[tile] & RAIL_DEPOT_DIR; truelight@0: truelight@0: fract_coord = (x & 0xF) + ((y & 0xF) << 4); truelight@0: if (_fractcoords_behind[dir] == fract_coord) { truelight@0: /* make sure a train is not entering the tile from behind */ truelight@0: return 8; truelight@0: } else if (_fractcoords_enter[dir] == fract_coord) { truelight@0: if (_enter_directions[dir] == v->direction) { truelight@0: /* enter the depot */ truelight@0: v->u.rail.track = 0x80, truelight@0: v->vehstatus |= VS_HIDDEN; /* hide it */ truelight@0: v->direction ^= 4; truelight@0: if (v->next == NULL) truelight@0: TrainEnterDepot(v, tile); truelight@0: v->tile = tile; truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, tile); truelight@0: return 4; truelight@0: } truelight@0: } else if (_fractcoords_leave[dir] == fract_coord) { truelight@0: if (_leave_directions[dir] == v->direction) { truelight@0: /* leave the depot? */ truelight@0: if ((v=v->next) != NULL) { truelight@0: v->vehstatus &= ~VS_HIDDEN; truelight@0: v->u.rail.track = _depot_track_mask[dir]; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: return 0; truelight@0: } truelight@0: truelight@0: void InitializeRail() truelight@0: { truelight@0: _last_built_train_depot_tile = 0; truelight@0: } truelight@0: truelight@0: const TileTypeProcs _tile_type_rail_procs = { truelight@0: DrawTile_Track, /* draw_tile_proc */ truelight@0: GetSlopeZ_Track, /* get_slope_z_proc */ truelight@0: ClearTile_Track, /* clear_tile_proc */ truelight@0: GetAcceptedCargo_Track, /* get_accepted_cargo_proc */ truelight@0: GetTileDesc_Track, /* get_tile_desc_proc */ truelight@0: GetTileTrackStatus_Track, /* get_tile_track_status_proc */ truelight@0: ClickTile_Track, /* click_tile_proc */ truelight@0: AnimateTile_Track, /* animate_tile_proc */ truelight@0: TileLoop_Track, /* tile_loop_clear */ truelight@0: ChangeTileOwner_Track, /* change_tile_owner_clear */ truelight@0: NULL, /* get_produced_cargo_proc */ truelight@0: VehicleEnter_Track, /* vehicle_enter_tile_proc */ truelight@0: NULL, /* vehicle_leave_tile_proc */ truelight@0: };