tron@2186: /* $Id$ */ tron@2186: richk@10724: /** @file rail.h Rail specific functions. */ celestar@2232: matthijs@1942: #ifndef RAIL_H matthijs@1942: #define RAIL_H matthijs@1942: rubidium@6872: #include "rail_type.h" rubidium@6872: #include "track_type.h" rubidium@6872: #include "vehicle_type.h" rubidium@6872: #include "gfx_type.h" rubidium@6872: #include "core/bitmath_func.hpp" rubidium@6872: #include "economy_func.h" richk@10724: #include "slope_type.h" rubidium@5838: richk@10184: enum RailTypeFlag { richk@10184: RTF_CATENARY = 0, ///< Set if the rail type should have catenary drawn richk@10184: }; richk@10184: richk@10184: enum RailTypeFlags { richk@10184: RTFB_NONE = 0, richk@10184: RTFB_CATENARY = 1 << RTF_CATENARY, richk@10184: }; richk@10184: DECLARE_ENUM_AS_BIT_SET(RailTypeFlags); richk@10184: celestar@2233: /** This struct contains all the info that is needed to draw and construct tracks. celestar@2233: */ rubidium@6574: struct RailtypeInfo { celestar@2274: /** Struct containing the main sprites. @note not all sprites are listed, but only celestar@2274: * the ones used directly in the code */ celestar@2233: struct { celestar@2233: SpriteID track_y; ///< single piece of rail in Y direction, with ground celestar@2233: SpriteID track_ns; ///< two pieces of rail in North and South corner (East-West direction) celestar@2233: SpriteID ground; ///< ground sprite for a 3-way switch celestar@2233: SpriteID single_y; ///< single piece of rail in Y direction, without ground celestar@2233: SpriteID single_x; ///< single piece of rail in X direction celestar@2233: SpriteID single_n; ///< single piece of rail in the northern corner celestar@2233: SpriteID single_s; ///< single piece of rail in the southern corner celestar@2233: SpriteID single_e; ///< single piece of rail in the eastern corner celestar@2233: SpriteID single_w; ///< single piece of rail in the western corner richk@10994: SpriteID single_sloped;///< single piecs of rail for slopes tron@2511: SpriteID crossing; ///< level crossing, rail in X direction tron@2511: SpriteID tunnel; ///< tunnel sprites base celestar@2233: } base_sprites; celestar@2233: celestar@2274: /** struct containing the sprites for the rail GUI. @note only sprites referred to celestar@2274: * directly in the code are listed */ celestar@2274: struct { celestar@2274: SpriteID build_ns_rail; ///< button for building single rail in N-S direction celestar@2274: SpriteID build_x_rail; ///< button for building single rail in X direction celestar@2274: SpriteID build_ew_rail; ///< button for building single rail in E-W direction celestar@2274: SpriteID build_y_rail; ///< button for building single rail in Y direction celestar@2274: SpriteID auto_rail; ///< button for the autorail construction celestar@2274: SpriteID build_depot; ///< button for building depots celestar@2274: SpriteID build_tunnel; ///< button for building a tunnel celestar@2274: SpriteID convert_rail; ///< button for converting rail celestar@2274: } gui_sprites; celestar@2274: celestar@2274: struct { richk@6743: CursorID rail_ns; ///< Cursor for building rail in N-S direction richk@6743: CursorID rail_swne; ///< Cursor for building rail in X direction richk@6743: CursorID rail_ew; ///< Cursor for building rail in E-W direction richk@6743: CursorID rail_nwse; ///< Cursor for building rail in Y direction richk@6743: CursorID autorail; ///< Cursor for autorail tool richk@6743: CursorID depot; ///< Cursor for building a depot richk@6743: CursorID tunnel; ///< Cursor for building a tunnel richk@6743: CursorID convert; ///< Cursor for converting track tron@2514: } cursor; tron@2514: tron@2514: struct { celestar@2274: StringID toolbar_caption; celestar@2274: } strings; celestar@2274: celestar@2233: /** sprite number difference between a piece of track on a snowy ground and the corresponding one on normal ground */ celestar@2233: SpriteID snow_offset; celestar@2233: celestar@3355: /** bitmask to the OTHER railtypes on which an engine of THIS railtype generates power */ rubidium@6872: RailTypes powered_railtypes; celestar@3355: celestar@3355: /** bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel */ rubidium@6872: RailTypes compatible_railtypes; celestar@2254: celestar@2254: /** celestar@2254: * Offset between the current railtype and normal rail. This means that:

celestar@2254: * 1) All the sprites in a railset MUST be in the same order. This order celestar@2254: * is determined by normal rail. Check sprites 1005 and following for this order

celestar@2254: * 2) The position where the railtype is loaded must always be the same, otherwise richk@6719: * the offset will fail. celestar@2254: * @note: Something more flexible might be desirable in the future. celestar@2254: */ celestar@2254: SpriteID total_offset; celestar@2536: celestar@2536: /** tron@4077: * Bridge offset tron@4077: */ celestar@2536: SpriteID bridge_offset; peter1138@3503: peter1138@3503: /** peter1138@3503: * Offset to add to ground sprite when drawing custom waypoints / stations peter1138@3503: */ peter1138@3503: byte custom_ground_offset; celestar@2233: rubidium@6872: /** rubidium@6872: * Multiplier for curve maximum speed advantage rubidium@6872: */ rubidium@6872: byte curve_speed; richk@10184: richk@10184: /** richk@10184: * Bit mask of rail type flags richk@10184: */ richk@10184: RailTypeFlags flags; hackykid@2008: }; matthijs@1942: matthijs@1967: matthijs@1967: /** celestar@2233: * Returns a pointer to the Railtype information for a given railtype celestar@2233: * @param railtype the rail type which the information is requested for celestar@2233: * @return The pointer to the RailtypeInfo celestar@2233: */ ludde@2236: static inline const RailtypeInfo *GetRailTypeInfo(RailType railtype) celestar@2233: { rubidium@6488: extern RailtypeInfo _railtypes[RAILTYPE_END]; celestar@2233: assert(railtype < RAILTYPE_END); ludde@2236: return &_railtypes[railtype]; celestar@2233: } celestar@2233: celestar@2233: /** matthijs@2006: * Checks if an engine of the given RailType can drive on a tile with a given matthijs@2006: * RailType. This would normally just be an equality check, but for electric matthijs@2006: * rails (which also support non-electric engines). matthijs@2006: * @return Whether the engine can drive on this tile. matthijs@2006: * @param enginetype The RailType of the engine we are considering. matthijs@2006: * @param tiletype The RailType of the tile we are considering. matthijs@2006: */ matthijs@2006: static inline bool IsCompatibleRail(RailType enginetype, RailType tiletype) matthijs@2006: { rubidium@6871: return HasBit(GetRailTypeInfo(enginetype)->compatible_railtypes, tiletype); matthijs@2006: } matthijs@2006: richk@6743: /** richk@6743: * Checks if an engine of the given RailType got power on a tile with a given richk@6743: * RailType. This would normally just be an equality check, but for electric richk@6743: * rails (which also support non-electric engines). richk@6743: * @return Whether the engine got power on this tile. richk@6743: * @param enginetype The RailType of the engine we are considering. richk@6743: * @param tiletype The RailType of the tile we are considering. richk@6743: */ celestar@3355: static inline bool HasPowerOnRail(RailType enginetype, RailType tiletype) celestar@3355: { rubidium@6871: return HasBit(GetRailTypeInfo(enginetype)->powered_railtypes, tiletype); celestar@3355: } celestar@3355: rubidium@6871: rubidium@6871: extern int _railtype_cost_multiplier[RAILTYPE_END]; rubidium@6871: extern const int _default_railtype_cost_multiplier[RAILTYPE_END]; rubidium@6871: rubidium@6871: /** rubidium@6871: * Returns the cost of building the specified railtype. rubidium@6871: * @param railtype The railtype being built. rubidium@6871: * @return The cost multiplier. rubidium@6871: */ rubidium@6871: static inline Money RailBuildCost(RailType railtype) rubidium@6871: { rubidium@6871: assert(railtype < RAILTYPE_END); rubidium@6871: return (_price.build_rail * _railtype_cost_multiplier[railtype]) >> 3; rubidium@6871: } rubidium@6871: rubidium@6872: /** rubidium@6872: * Calculates the cost of rail conversion rubidium@6872: * @param from The railtype we are converting from rubidium@6872: * @param to The railtype we are converting to rubidium@6872: * @return Cost per TrackBit rubidium@6872: */ rubidium@6872: static inline Money RailConvertCost(RailType from, RailType to) rubidium@6872: { rubidium@6872: /* rail -> el. rail rubidium@6872: * calculate the price as 5 / 4 of (cost build el. rail) - (cost build rail) rubidium@6872: * (the price of workers to get to place is that 1/4) rubidium@6872: */ rubidium@6872: if (HasPowerOnRail(from, to)) { rubidium@6872: return ((RailBuildCost(to) - RailBuildCost(from)) * 5) >> 2; rubidium@6872: } rubidium@6872: rubidium@6872: /* el. rail -> rail rubidium@6872: * calculate the price as 1 / 4 of (cost build el. rail) - (cost build rail) rubidium@6872: * (the price of workers is 1 / 4 + price of copper sold to a recycle center) rubidium@6872: */ rubidium@6872: if (HasPowerOnRail(to, from)) { rubidium@6872: return (RailBuildCost(from) - RailBuildCost(to)) >> 2; rubidium@6872: } rubidium@6872: rubidium@6872: /* make the price the same as remove + build new type */ rubidium@6872: return RailBuildCost(to) + _price.remove_rail; rubidium@6872: } rubidium@6872: rubidium@6870: void *UpdateTrainPowerProc(Vehicle *v, void *data); tron@2520: void DrawTrainDepotSprite(int x, int y, int image, RailType railtype); tron@2520: void DrawDefaultWaypointSprite(int x, int y, RailType railtype); rubidium@6872: void *EnsureNoTrainOnTrackProc(Vehicle *v, void *data); richk@10184: int TicksToLeaveDepot(const Vehicle *v); richk@10184: richk@6743: Foundation GetRailFoundation(Slope tileh, TrackBits bits); KUDr@5116: KUDr@5116: rubidium@6872: /** rubidium@6872: * Finds out if a Player has a certain railtype available rubidium@6872: * @param p Player in question rubidium@6872: * @param railtype requested RailType rubidium@6872: * @return true if player has requested RailType available rubidium@6872: */ rubidium@6872: bool HasRailtypeAvail(const PlayerID p, const RailType railtype); rubidium@6872: rubidium@6872: /** rubidium@6872: * Validate functions for rail building. rubidium@6872: * @param rail the railtype to check. rubidium@6872: * @return true if the current player may build the rail. rubidium@6872: */ rubidium@6872: bool ValParamRailtype(const RailType rail); rubidium@6872: rubidium@6872: /** rubidium@6872: * Returns the "best" railtype a player can build. rubidium@6872: * As the AI doesn't know what the BEST one is, we have our own priority list rubidium@6872: * here. When adding new railtypes, modify this function rubidium@6872: * @param p the player "in action" rubidium@6872: * @return The "best" railtype a player has available rubidium@6872: */ rubidium@6872: RailType GetBestRailtype(const PlayerID p); rubidium@6872: rubidium@6872: /** rubidium@6872: * Get the rail types the given player can build. rubidium@6872: * @param p the player to get the rail types for. rubidium@6872: * @return the rail types. rubidium@6872: */ rubidium@6872: RailTypes GetPlayerRailtypes(const PlayerID p); rubidium@6872: celestar@2536: #endif /* RAIL_H */