tron@2186: /* $Id$ */ tron@2186: belugas@6423: /** @vehicle.h */ belugas@6423: truelight@0: #ifndef VEHICLE_H truelight@0: #define VEHICLE_H truelight@0: matthijs@5216: #include "oldpool.h" truelight@1024: #include "order.h" matthijs@1944: #include "rail.h" rubidium@6683: #include "road.h" rubidium@7010: #include "cargopacket.h" truelight@6998: #include "texteff.hpp" tron@577: rubidium@5991: /** The returned bits of VehicleEnterTile. */ rubidium@5991: enum VehicleEnterTileStatus { rubidium@5991: VETS_ENTERED_STATION = 1, ///< The vehicle entered a station rubidium@5991: VETS_ENTERED_WORMHOLE = 2, ///< The vehicle either entered a bridge, tunnel or depot tile (this includes the last tile of the bridge/tunnel) rubidium@5991: VETS_CANNOT_ENTER = 3, ///< The vehicle cannot enter the tile rubidium@5991: rubidium@5991: /** rubidium@5991: * Shift the VehicleEnterTileStatus this many bits rubidium@5991: * to the right to get the station ID when rubidium@5991: * VETS_ENTERED_STATION is set rubidium@5991: */ rubidium@5991: VETS_STATION_ID_OFFSET = 8, rubidium@5991: rubidium@5991: /** Bit sets of the above specified bits */ rubidium@5991: VETSB_CONTINUE = 0, ///< The vehicle can continue normally rubidium@5991: VETSB_ENTERED_STATION = 1 << VETS_ENTERED_STATION, ///< The vehicle entered a station rubidium@5991: VETSB_ENTERED_WORMHOLE = 1 << VETS_ENTERED_WORMHOLE, ///< The vehicle either entered a bridge, tunnel or depot tile (this includes the last tile of the bridge/tunnel) rubidium@5991: VETSB_CANNOT_ENTER = 1 << VETS_CANNOT_ENTER, ///< The vehicle cannot enter the tile rubidium@5991: }; rubidium@5991: rubidium@6000: /** Road vehicle states */ rubidium@6000: enum RoadVehicleStates { rubidium@6000: /* rubidium@6000: * Lower 4 bits are used for vehicle track direction. (Trackdirs) rubidium@6161: * When in a road stop (bit 5 or bit 6 set) these bits give the rubidium@6000: * track direction of the entry to the road stop. rubidium@6000: * As the entry direction will always be a diagonal rubidium@6000: * direction (X_NE, Y_SE, X_SW or Y_NW) only bits 0 and 3 rubidium@6000: * are needed to hold this direction. Bit 1 is then used to show rubidium@6000: * that the vehicle is using the second road stop bay. rubidium@6161: * Bit 2 is then used for drive-through stops to show the vehicle rubidium@6161: * is stopping at this road stop. rubidium@6000: */ rubidium@6000: rubidium@6000: /* Numeric values */ rubidium@6000: RVSB_IN_DEPOT = 0xFE, ///< The vehicle is in a depot rubidium@6000: RVSB_WORMHOLE = 0xFF, ///< The vehicle is in a tunnel and/or bridge rubidium@6000: rubidium@6000: /* Bit numbers */ rubidium@6000: RVS_USING_SECOND_BAY = 1, ///< Only used while in a road stop rubidium@6012: RVS_IS_STOPPING = 2, ///< Only used for drive-through stops. Vehicle will stop here rubidium@6161: RVS_DRIVE_SIDE = 4, ///< Only used when retrieving move data rubidium@6000: RVS_IN_ROAD_STOP = 5, ///< The vehicle is in a road stop rubidium@6012: RVS_IN_DT_ROAD_STOP = 6, ///< The vehicle is in a drive-through road stop rubidium@6000: rubidium@6000: /* Bit sets of the above specified bits */ rubidium@6000: RVSB_IN_ROAD_STOP = 1 << RVS_IN_ROAD_STOP, ///< The vehicle is in a road stop rubidium@6000: RVSB_IN_ROAD_STOP_END = RVSB_IN_ROAD_STOP + TRACKDIR_END, rubidium@6012: RVSB_IN_DT_ROAD_STOP = 1 << RVS_IN_DT_ROAD_STOP, ///< The vehicle is in a drive-through road stop rubidium@6012: RVSB_IN_DT_ROAD_STOP_END = RVSB_IN_DT_ROAD_STOP + TRACKDIR_END, rubidium@6000: rubidium@6000: RVSB_TRACKDIR_MASK = 0x0F, ///< The mask used to extract track dirs rubidium@6000: RVSB_ROAD_STOP_TRACKDIR_MASK = 0x09 ///< Only bits 0 and 3 are used to encode the trackdir for road stops rubidium@6000: }; rubidium@6000: rubidium@6621: enum VehicleType { rubidium@6259: VEH_TRAIN, rubidium@6259: VEH_ROAD, rubidium@6259: VEH_SHIP, rubidium@6259: VEH_AIRCRAFT, rubidium@6259: VEH_SPECIAL, rubidium@6259: VEH_DISASTER, rubidium@6621: VEH_END, rubidium@6259: VEH_INVALID = 0xFF, rubidium@6621: }; rubidium@7086: DECLARE_POSTFIX_INCREMENT(VehicleType); rubidium@6621: template <> struct EnumPropsT : MakeEnumPropsT {}; rubidium@6621: typedef TinyEnumT VehicleTypeByte; matthijs@1752: matthijs@1752: enum VehStatus { rubidium@4344: VS_HIDDEN = 0x01, rubidium@4344: VS_STOPPED = 0x02, rubidium@4344: VS_UNCLICKABLE = 0x04, rubidium@4344: VS_DEFPAL = 0x08, rubidium@4344: VS_TRAIN_SLOWING = 0x10, rubidium@4344: VS_SHADOW = 0x20, matthijs@1752: VS_AIRCRAFT_BROKEN = 0x40, rubidium@4344: VS_CRASHED = 0x80, matthijs@1752: }; matthijs@1752: maedhros@6175: enum VehicleFlags { maedhros@6175: VF_LOADING_FINISHED, maedhros@6175: VF_CARGO_UNLOADING, maedhros@6176: VF_BUILT_AS_PROTOTYPE, maedhros@7066: VF_TIMETABLE_STARTED, ///< Whether the vehicle has started running on the timetable yet. maedhros@7066: VF_AUTOFILL_TIMETABLE, ///< Whether the vehicle should fill in the timetable automatically. peter1138@5211: }; peter1138@5211: matthijs@1752: /* Effect vehicle types */ rubidium@6248: enum EffectVehicle { matthijs@1752: EV_CHIMNEY_SMOKE = 0, matthijs@1752: EV_STEAM_SMOKE = 1, matthijs@1752: EV_DIESEL_SMOKE = 2, matthijs@1752: EV_ELECTRIC_SPARK = 3, matthijs@1752: EV_SMOKE = 4, matthijs@1752: EV_EXPLOSION_LARGE = 5, matthijs@1752: EV_BREAKDOWN_SMOKE = 6, matthijs@1752: EV_EXPLOSION_SMALL = 7, matthijs@1752: EV_BULLDOZER = 8, matthijs@1752: EV_BUBBLE = 9 rubidium@6248: }; matthijs@1752: rubidium@6248: struct VehicleRail { rubidium@4434: uint16 last_speed; // NOSAVE: only used in UI truelight@0: uint16 crash_anim_pos; truelight@193: belugas@6423: /* cached values, recalculated on load and each time a vehicle is added to/removed from the consist. */ hackykid@1905: uint16 cached_max_speed; // max speed of the consist. (minimum of the max speed of all vehicles in the consist) hackykid@1905: uint32 cached_power; // total power of the consist. hackykid@1922: uint8 cached_veh_length; // length of this vehicle in units of 1/8 of normal length, cached because this can be set by a callback peter1138@2587: uint16 cached_total_length; ///< Length of the whole train, valid only for first engine. peter1138@2587: belugas@6423: /* cached values, recalculated when the cargo on a train changes (in addition to the conditions above) */ peter1138@5162: uint32 cached_weight; // total weight of the consist. peter1138@5162: uint32 cached_veh_weight; // weight of the vehicle. peter1138@5400: uint32 cached_max_te; // max tractive effort of consist peter1138@2595: /** peter1138@2595: * Position/type of visual effect. peter1138@2595: * bit 0 - 3 = position of effect relative to vehicle. (0 = front, 8 = centre, 15 = rear) peter1138@2595: * bit 4 - 5 = type of effect. (0 = default for engine class, 1 = steam, 2 = diesel, 3 = electric) peter1138@2595: * bit 6 = disable visual effect. peter1138@2595: * bit 7 = disable powered wagons. peter1138@2595: */ peter1138@2595: byte cached_vis_effect; glx@7881: byte user_def_data; truelight@0: belugas@6423: /* NOSAVE: for wagon override - id of the first engine in train belugas@6423: * 0xffff == not in train */ Darkvater@1796: EngineID first_engine; truelight@0: rubidium@5587: TrackBitsByte track; truelight@0: byte force_proceed; rubidium@5587: RailTypeByte railtype; celestar@3355: RailTypeMask compatible_railtypes; truelight@0: truelight@0: byte flags; hackykid@2008: belugas@6423: /* Link between the two ends of a multiheaded engine */ bjarni@2676: Vehicle *other_multiheaded_part; peter1138@6603: peter1138@6603: /* Cached wagon override spritegroup */ peter1138@6603: const struct SpriteGroup *cached_override; rubidium@6248: }; truelight@0: truelight@0: enum { rubidium@4344: VRF_REVERSING = 0, truelight@0: belugas@6423: /* used to calculate if train is going up or down */ rubidium@4344: VRF_GOINGUP = 1, rubidium@4344: VRF_GOINGDOWN = 2, hackykid@1908: belugas@6423: /* used to store if a wagon is powered or not */ rubidium@4344: VRF_POWEREDWAGON = 3, bjarni@3256: belugas@6423: /* used to reverse the visible direction of the vehicle */ bjarni@3256: VRF_REVERSE_DIRECTION = 4, KUDr@4870: belugas@6423: /* used to mark train as lost because PF can't find the route */ KUDr@4870: VRF_NO_PATH_TO_DESTINATION = 5, KUDr@5116: belugas@6423: /* used to mark that electric train engine is allowed to run on normal rail */ KUDr@5116: VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL = 6, truelight@0: }; truelight@0: rubidium@6248: struct VehicleAir { truelight@0: uint16 crashed_counter; peter1138@6490: uint16 cached_max_speed; truelight@0: byte pos; tron@4077: byte previous_pos; Darkvater@3347: StationID targetairport; truelight@0: byte state; rubidium@6248: }; truelight@0: rubidium@6248: struct VehicleRoad { belugas@6423: byte state; ///< @see RoadVehicleStates truelight@0: byte frame; peter1138@3009: uint16 blocked_ctr; truelight@0: byte overtaking; truelight@0: byte overtaking_ctr; truelight@0: uint16 crashed_ctr; truelight@0: byte reverse_ctr; celestar@1217: struct RoadStop *slot; celestar@1217: byte slot_age; maedhros@6857: EngineID first_engine; maedhros@6857: byte cached_veh_length; rubidium@6683: rubidium@6683: RoadType roadtype; rubidium@6683: RoadTypes compatible_roadtypes; rubidium@6248: }; truelight@0: rubidium@6248: struct VehicleSpecial { rubidium@7334: uint16 animation_state; rubidium@7334: byte animation_substate; rubidium@6248: }; truelight@0: rubidium@6248: struct VehicleDisaster { truelight@0: uint16 image_override; rubidium@7334: VehicleID big_ufo_destroyer_target; rubidium@6248: }; truelight@0: rubidium@6248: struct VehicleShip { rubidium@5587: TrackBitsByte state; rubidium@6248: }; truelight@0: rubidium@7398: struct Vehicle; rubidium@7398: DECLARE_OLD_POOL(Vehicle, Vehicle, 9, 125) truelight@0: rubidium@7537: /* Some declarations of functions, so we can make them friendly */ rubidium@7493: struct SaveLoad; rubidium@7537: extern const SaveLoad *GetVehicleDescription(VehicleType vt); rubidium@7537: extern void AfterLoadVehicles(); rubidium@7537: struct LoadgameState; rubidium@7537: extern bool LoadOldVehicle(LoadgameState *ls, int num); rubidium@7493: rubidium@7398: struct Vehicle : PoolItem { rubidium@6621: VehicleTypeByte type; ///< Type of vehicle Darkvater@5854: byte subtype; // subtype (Filled with values from EffectVehicles/TrainSubTypes/AircraftSubTypes) truelight@0: rubidium@7493: private: rubidium@7492: Vehicle *next; // pointer to the next vehicle in the chain rubidium@7497: Vehicle *previous; // NOSAVE: pointer to the previous vehicle in the chain rubidium@7497: Vehicle *first; // NOSAVE: pointer to the first vehicle in the chain rubidium@7493: public: rubidium@7493: friend const SaveLoad *GetVehicleDescription(VehicleType vt); // So we can use private/protected variables in the saveload code rubidium@7537: friend void AfterLoadVehicles(); // So we can set the previous and first pointers while loading rubidium@7537: friend bool LoadOldVehicle(LoadgameState *ls, int num); // So we can set the proper next pointer while loading rubidium@7493: rubidium@7492: Vehicle *depot_list; // NOSAVE: linked list to tell what vehicles entered a depot during the last tick. Used by autoreplace truelight@0: rubidium@4344: StringID string_id; // Displayed string truelight@0: rubidium@4344: UnitID unitnumber; // unit number, for display purposes only belugas@6423: PlayerByte owner; // which player owns the vehicle? rubidium@4344: rubidium@4344: TileIndex tile; // Current tile index rubidium@4344: TileIndex dest_tile; // Heading for this tile rubidium@4344: rubidium@4344: int32 x_pos; // coordinates tron@1174: int32 y_pos; truelight@0: byte z_pos; rubidium@5587: DirectionByte direction; // facing truelight@0: rubidium@4344: byte spritenum; // currently displayed sprite index rubidium@4344: // 0xfd == custom sprite, 0xfe == custom second head sprite rubidium@4344: // 0xff == reserved for another custom sprite rubidium@4344: uint16 cur_image; // sprite number for this vehicle rubidium@4344: byte sprite_width; // width of vehicle sprite rubidium@4344: byte sprite_height; // height of vehicle sprite rubidium@4344: byte z_height; // z-height of vehicle sprite rubidium@4344: int8 x_offs; // x offset for vehicle sprite rubidium@4344: int8 y_offs; // y offset for vehicle sprite Darkvater@1770: EngineID engine_type; truelight@0: truelight@6998: TextEffectID fill_percent_te_id; // a text-effect id to a loading indicator object truelight@6998: belugas@6423: /* for randomized variational spritegroups belugas@6423: * bitmask used to resolve them; parts of it get reseeded when triggers belugas@6423: * of corresponding spritegroups get matched */ tron@445: byte random_bits; rubidium@4344: byte waiting_triggers; // triggers to be yet matched tron@445: rubidium@4344: uint16 max_speed; // maximum speed rubidium@4344: uint16 cur_speed; // current speed rubidium@4344: byte subspeed; // fractional speed rubidium@4344: byte acceleration; // used by train & aircraft truelight@0: byte progress; peter1138@4656: uint32 motion_counter; truelight@0: rubidium@4344: byte vehstatus; // Status Darkvater@3347: StationID last_station_visited; truelight@193: rubidium@4344: CargoID cargo_type; // type of cargo this vehicle is carrying rubidium@4344: uint16 cargo_cap; // total capacity peter1138@3870: byte cargo_subtype; ///< Used for livery refits (NewGRF variations) rubidium@7010: CargoList cargo; ///< The cargo this vehicle is carrying rubidium@7010: truelight@0: rubidium@4344: byte day_counter; // increased by one for each day rubidium@4344: byte tick_counter; // increased by one for each tick truelight@0: truelight@1024: /* Begin Order-stuff */ peter1138@3173: Order current_order; ///< The current order (+ status, like: loading) truelight@4391: VehicleOrderID cur_order_index; ///< The index to the current order truelight@1024: peter1138@3173: Order *orders; ///< Pointer to the first order for this vehicle truelight@4391: VehicleOrderID num_orders; ///< How many orders there are in the list truelight@1024: peter1138@3173: Vehicle *next_shared; ///< If not NULL, this points to the next vehicle that shared the order peter1138@3173: Vehicle *prev_shared; ///< If not NULL, this points to the prev vehicle that shared the order truelight@1024: /* End Order-stuff */ truelight@0: belugas@6423: /* Boundaries for the current position in the world and a next hash link. belugas@6423: * NOSAVE: All of those can be updated with VehiclePositionChanged() */ tron@849: int32 left_coord; tron@849: int32 top_coord; tron@849: int32 right_coord; tron@849: int32 bottom_coord; peter1138@5574: Vehicle *next_hash; peter1138@6871: Vehicle *next_new_hash; peter1138@6871: Vehicle **old_new_hash; truelight@0: belugas@6423: /* Related to age and service time */ rubidium@4326: Date age; // Age in days rubidium@4326: Date max_age; // Maximum age rubidium@4289: Date date_of_last_service; rubidium@4289: Date service_interval; truelight@0: uint16 reliability; truelight@0: uint16 reliability_spd_dec; truelight@0: byte breakdown_ctr; truelight@0: byte breakdown_delay; truelight@0: byte breakdowns_since_last_service; truelight@0: byte breakdown_chance; rubidium@4326: Year build_year; truelight@0: rubidium@4344: bool leave_depot_instantly; // NOSAVE: stores if the vehicle needs to leave the depot it just entered. Used by autoreplace bjarni@2617: truelight@0: uint16 load_unload_time_rem; maedhros@6175: byte vehicle_flags; // Used for gradual loading and other miscellaneous things (@see VehicleFlags enum) truelight@193: rubidium@6953: Money profit_this_year; rubidium@6953: Money profit_last_year; rubidium@6953: Money value; truelight@0: rubidium@6643: GroupID group_id; ///< Index of group Pool array rubidium@6643: maedhros@6980: /* Used for timetabling. */ maedhros@6980: uint32 current_order_time; ///< How many ticks have passed since this order started. maedhros@6980: int32 lateness_counter; ///< How many ticks late (or early if negative) this vehicle is. maedhros@6980: glx@7802: SpriteID colormap; // NOSAVE: cached color mapping glx@7802: truelight@0: union { truelight@0: VehicleRail rail; truelight@0: VehicleAir air; truelight@0: VehicleRoad road; truelight@0: VehicleSpecial special; truelight@0: VehicleDisaster disaster; truelight@0: VehicleShip ship; truelight@0: } u; KUDr@5651: rubidium@7398: rubidium@7398: /** rubidium@7398: * Allocates a lot of vehicles. rubidium@7398: * @param vl pointer to an array of vehicles to get allocated. Can be NULL if the vehicles aren't needed (makes it test only) rubidium@7398: * @param num number of vehicles to allocate room for rubidium@7398: * @return true if there is room to allocate all the vehicles rubidium@7398: */ rubidium@7398: static bool AllocateList(Vehicle **vl, int num); rubidium@7398: rubidium@7398: /** Create a new vehicle */ rubidium@7398: Vehicle(); rubidium@7398: rubidium@7412: /** Destroy all stuff that (still) needs the virtual functions to work properly */ rubidium@7412: void PreDestructor(); rubidium@7398: /** We want to 'destruct' the right class. */ rubidium@7398: virtual ~Vehicle(); rubidium@7398: KUDr@5651: void BeginLoading(); KUDr@5651: void LeaveStation(); rubidium@6552: rubidium@6552: /** rubidium@6594: * Handle the loading of the vehicle; when not it skips through dummy rubidium@6594: * orders and does nothing in all other cases. rubidium@6594: * @param mode is the non-first call for this vehicle in this tick? rubidium@6594: */ rubidium@6594: void HandleLoading(bool mode = false); rubidium@6594: rubidium@6594: /** rubidium@6552: * Get a string 'representation' of the vehicle type. rubidium@6552: * @return the string representation. rubidium@6552: */ rubidium@7400: virtual const char* GetTypeString() const { return "base vehicle"; } rubidium@6553: rubidium@6553: /** rubidium@6553: * Marks the vehicles to be redrawn and updates cached variables rubidium@7545: * rubidium@7545: * This method marks the area of the vehicle on the screen as dirty. rubidium@7545: * It can be use to repaint the vehicle. rubidium@7545: * rubidium@7545: * @ingroup dirty rubidium@6553: */ rubidium@6553: virtual void MarkDirty() {} rubidium@6558: rubidium@6558: /** rubidium@6558: * Updates the x and y offsets and the size of the sprite used rubidium@6558: * for this vehicle. rubidium@6558: * @param direction the direction the vehicle is facing rubidium@6558: */ rubidium@6558: virtual void UpdateDeltaXY(Direction direction) {} rubidium@6562: rubidium@6562: /** rubidium@6562: * Sets the expense type associated to this vehicle type rubidium@6562: * @param income whether this is income or (running) expenses of the vehicle rubidium@6562: */ rubidium@6563: virtual ExpensesType GetExpenseType(bool income) const { return EXPENSES_OTHER; } rubidium@6562: rubidium@6562: /** rubidium@6562: * Invalidates the vehicle list window of this type of vehicle rubidium@6562: */ rubidium@6563: virtual WindowClass GetVehicleListWindowClass() const { return WC_NONE; } rubidium@6593: rubidium@6593: /** rubidium@6593: * Play the sound associated with leaving the station rubidium@6593: */ rubidium@6593: virtual void PlayLeaveStationSound() const {} maedhros@6773: maedhros@6773: /** maedhros@6773: * Whether this is the primary vehicle in the chain. maedhros@6773: */ maedhros@6773: virtual bool IsPrimaryVehicle() const { return false; } maedhros@6773: maedhros@6773: /** rubidium@7134: * Gets the sprite to show for the given direction rubidium@7134: * @param direction the direction the vehicle is facing rubidium@7134: * @return the sprite for the given vehicle in the given direction rubidium@7134: */ rubidium@7134: virtual int GetImage(Direction direction) const { return 0; } rubidium@7135: rubidium@7135: /** rubidium@7477: * Gets the speed in mph that can be sent into SetDParam for string processing. rubidium@7477: * @return the vehicle's speed rubidium@7477: */ rubidium@7477: virtual int GetDisplaySpeed() const { return 0; } rubidium@7477: rubidium@7477: /** rubidium@7484: * Gets the maximum speed in mph that can be sent into SetDParam for string processing. rubidium@7484: * @return the vehicle's maximum speed rubidium@7484: */ rubidium@7484: virtual int GetDisplayMaxSpeed() const { return 0; } rubidium@7484: rubidium@7484: /** rubidium@7488: * Gets the running cost of a vehicle rubidium@7488: * @return the vehicle's running cost rubidium@7488: */ rubidium@7488: virtual Money GetRunningCost() const { return 0; } rubidium@7488: rubidium@7488: /** rubidium@7490: * Check whether the vehicle is in the depot. rubidium@7490: * @return true if and only if the vehicle is in the depot. rubidium@7490: */ rubidium@7490: virtual bool IsInDepot() const { return false; } rubidium@7490: rubidium@7490: /** rubidium@7490: * Check whether the vehicle is in the depot *and* stopped. rubidium@7490: * @return true if and only if the vehicle is in the depot and stopped. rubidium@7490: */ rubidium@7490: virtual bool IsStoppedInDepot() const { return this->IsInDepot() && (this->vehstatus & VS_STOPPED) != 0; } rubidium@7490: rubidium@7490: /** rubidium@7135: * Calls the tick handler of the vehicle rubidium@7135: */ rubidium@7400: virtual void Tick() {}; rubidium@7387: rubidium@7488: /** rubidium@7488: * Gets the running cost of a vehicle that can be sent into SetDParam for string processing. rubidium@7488: * @return the vehicle's running cost rubidium@7488: */ rubidium@7488: Money GetDisplayRunningCost() const { return (this->GetRunningCost() >> 8); } rubidium@7488: rubidium@7492: /** rubidium@7492: * Is this vehicle a valid vehicle? rubidium@7492: * @return true if and only if the vehicle is valid. rubidium@7492: */ rubidium@7492: inline bool IsValid() const { return this->type != VEH_INVALID; } rubidium@7492: rubidium@7492: /** rubidium@7492: * Set the next vehicle of this vehicle. rubidium@7492: * @param next the next vehicle. NULL removes the next vehicle. rubidium@7492: */ rubidium@7497: void SetNext(Vehicle *next); rubidium@7492: rubidium@7492: /** rubidium@7492: * Get the next vehicle of this vehicle. rubidium@7492: * @note articulated parts are also counted as vehicles. rubidium@7492: * @return the next vehicle or NULL when there isn't a next vehicle. rubidium@7492: */ rubidium@7492: inline Vehicle *Next() const { return this->next; } rubidium@7497: rubidium@7497: /** rubidium@7497: * Get the previous vehicle of this vehicle. rubidium@7497: * @note articulated parts are also counted as vehicles. rubidium@7497: * @return the previous vehicle or NULL when there isn't a previous vehicle. rubidium@7497: */ rubidium@7497: inline Vehicle *Previous() const { return this->previous; } rubidium@7497: rubidium@7497: /** rubidium@7497: * Get the first vehicle of this vehicle chain. rubidium@7497: * @return the first vehicle of the chain. rubidium@7497: */ rubidium@7497: inline Vehicle *First() const { return this->first; } rubidium@6552: }; rubidium@6552: rubidium@6552: /** rubidium@6552: * This class 'wraps' Vehicle; you do not actually instantiate this class. rubidium@6552: * You create a Vehicle using AllocateVehicle, so it is added to the pool rubidium@6552: * and you reinitialize that to a Train using: rubidium@6552: * v = new (v) Train(); rubidium@6552: * rubidium@6552: * As side-effect the vehicle type is set correctly. rubidium@6552: * rubidium@6552: * A special vehicle is one of the following: rubidium@6552: * - smoke rubidium@6552: * - electric sparks for trains rubidium@6552: * - explosions rubidium@6552: * - bulldozer (road works) rubidium@6552: * - bubbles (industry) rubidium@6552: */ rubidium@6552: struct SpecialVehicle : public Vehicle { rubidium@6552: /** Initializes the Vehicle to a special vehicle */ rubidium@6552: SpecialVehicle() { this->type = VEH_SPECIAL; } rubidium@6552: rubidium@6552: /** We want to 'destruct' the right class. */ rubidium@6552: virtual ~SpecialVehicle() {} rubidium@6552: rubidium@6563: const char *GetTypeString() const { return "special vehicle"; } rubidium@6558: void UpdateDeltaXY(Direction direction); rubidium@7135: void Tick(); rubidium@6552: }; rubidium@6552: rubidium@6552: /** rubidium@6552: * This class 'wraps' Vehicle; you do not actually instantiate this class. rubidium@6552: * You create a Vehicle using AllocateVehicle, so it is added to the pool rubidium@6552: * and you reinitialize that to a Train using: rubidium@6552: * v = new (v) Train(); rubidium@6552: * rubidium@6552: * As side-effect the vehicle type is set correctly. rubidium@6552: */ rubidium@6552: struct DisasterVehicle : public Vehicle { rubidium@6552: /** Initializes the Vehicle to a disaster vehicle */ rubidium@6552: DisasterVehicle() { this->type = VEH_DISASTER; } rubidium@6552: rubidium@6552: /** We want to 'destruct' the right class. */ rubidium@6552: virtual ~DisasterVehicle() {} rubidium@6552: rubidium@6563: const char *GetTypeString() const { return "disaster vehicle"; } rubidium@6558: void UpdateDeltaXY(Direction direction); rubidium@7135: void Tick(); rubidium@6552: }; rubidium@6552: rubidium@6552: /** rubidium@6552: * This class 'wraps' Vehicle; you do not actually instantiate this class. rubidium@6552: * You create a Vehicle using AllocateVehicle, so it is added to the pool rubidium@6552: * and you reinitialize that to a Train using: rubidium@6552: * v = new (v) Train(); rubidium@6552: * rubidium@6552: * As side-effect the vehicle type is set correctly. rubidium@6552: */ rubidium@6552: struct InvalidVehicle : public Vehicle { rubidium@6552: /** Initializes the Vehicle to a invalid vehicle */ rubidium@6552: InvalidVehicle() { this->type = VEH_INVALID; } rubidium@6552: rubidium@6552: /** We want to 'destruct' the right class. */ rubidium@6552: virtual ~InvalidVehicle() {} rubidium@6552: rubidium@6563: const char *GetTypeString() const { return "invalid vehicle"; } rubidium@7135: void Tick() {} truelight@0: }; truelight@0: Darkvater@1765: #define is_custom_sprite(x) (x >= 0xFD) Darkvater@1765: #define IS_CUSTOM_FIRSTHEAD_SPRITE(x) (x == 0xFD) Darkvater@1765: #define IS_CUSTOM_SECONDHEAD_SPRITE(x) (x == 0xFE) truelight@0: truelight@0: typedef void *VehicleFromPosProc(Vehicle *v, void *data); truelight@0: bjarni@578: void VehicleServiceInDepot(Vehicle *v); truelight@0: void VehiclePositionChanged(Vehicle *v); truelight@0: Vehicle *GetLastVehicleInChain(Vehicle *v); rubidium@7318: uint CountVehiclesInChain(const Vehicle *v); bjarni@4574: bool IsEngineCountable(const Vehicle *v); truelight@0: void DeleteVehicleChain(Vehicle *v); truelight@0: void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc); peter1138@6871: void *VehicleFromPosXY(int x, int y, void *data, VehicleFromPosProc *proc); rubidium@6247: void CallVehicleTicks(); truelight@1605: Vehicle *FindVehicleOnTileZ(TileIndex tile, byte z); truelight@7014: uint8 CalcPercentVehicleFilled(Vehicle *v, StringID *color); truelight@0: rubidium@6247: void InitializeTrains(); rubidium@6247: byte VehicleRandomBits(); rubidium@6247: void ResetVehiclePosHash(); glx@7802: void ResetVehicleColorMap(); glx@7803: void CheckVehicle32Day(Vehicle *v); truelight@0: peter1138@2704: bool CanRefitTo(EngineID engine_type, CargoID cid_to); peter1138@3973: CargoID FindFirstRefittableCargo(EngineID engine_type); rubidium@6943: CommandCost GetRefitCost(EngineID engine_type); truelight@0: truelight@0: void ViewportAddVehicles(DrawPixelInfo *dpi); truelight@0: Darkvater@5866: SpriteID GetRotorImage(const Vehicle *v); truelight@0: tron@1359: Vehicle *CreateEffectVehicle(int x, int y, int z, EffectVehicle type); tron@1359: Vehicle *CreateEffectVehicleAbove(int x, int y, int z, EffectVehicle type); tron@1359: Vehicle *CreateEffectVehicleRel(const Vehicle *v, int x, int y, int z, EffectVehicle type); truelight@0: tron@1977: uint32 VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y); truelight@0: tron@3881: StringID VehicleInTheWayErrMsg(const Vehicle* v); rubidium@5940: Vehicle *FindVehicleBetween(TileIndex from, TileIndex to, byte z, bool without_crashed = false); truelight@0: tron@3172: bool UpdateSignalsOnSegment(TileIndex tile, DiagDirection direction); tron@1977: void SetSignalsOnBothDir(TileIndex tile, byte track); truelight@0: tron@2116: Vehicle *CheckClickOnVehicle(const ViewPort *vp, int x, int y); truelight@0: truelight@0: void DecreaseVehicleValue(Vehicle *v); truelight@0: void CheckVehicleBreakdown(Vehicle *v); truelight@0: void AgeVehicle(Vehicle *v); bjarni@2574: void VehicleEnteredDepotThisTick(Vehicle *v); truelight@0: truelight@0: void BeginVehicleMove(Vehicle *v); truelight@0: void EndVehicleMove(Vehicle *v); truelight@0: rubidium@6641: UnitID GetFreeUnitNumber(VehicleType type); truelight@0: hackykid@1905: void TrainConsistChanged(Vehicle *v); celestar@3355: void TrainPowerChanged(Vehicle *v); rubidium@6953: Money GetTrainRunningCost(const Vehicle *v); truelight@0: tron@593: bool VehicleNeedsService(const Vehicle *v); tron@593: rubidium@6641: uint GenerateVehicleSortList(const Vehicle*** sort_list, uint16 *length_of_array, VehicleType type, PlayerID owner, uint32 index, uint16 window_type); rubidium@6641: void BuildDepotVehicleList(VehicleType type, TileIndex tile, Vehicle ***engine_list, uint16 *engine_list_length, uint16 *engine_count, Vehicle ***wagon_list, uint16 *wagon_list_length, uint16 *wagon_count); rubidium@6943: CommandCost SendAllVehiclesToDepot(VehicleType type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id); bjarni@4725: void VehicleEnterDepot(Vehicle *v); bjarni@4506: peter1138@7096: void InvalidateAutoreplaceWindow(EngineID e, GroupID id_g); bjarni@5944: rubidium@6943: CommandCost MaybeReplaceVehicle(Vehicle *v, bool check, bool display_costs); rubidium@7086: bool CanBuildVehicleInfrastructure(VehicleType type); bjarni@6264: rubidium@7478: void CcCloneVehicle(bool success, TileIndex tile, uint32 p1, uint32 p2); rubidium@7478: bjarni@4506: /* Flags to add to p2 for goto depot commands */ bjarni@4506: /* Note: bits 8-10 are used for VLW flags */ bjarni@4506: enum { rubidium@6492: DEPOT_SERVICE = (1 << 0), // The vehicle will leave the depot right after arrival (serivce only) bjarni@4506: DEPOT_MASS_SEND = (1 << 1), // Tells that it's a mass send to depot command (type in VLW flag) bjarni@4506: DEPOT_DONT_CANCEL = (1 << 2), // Don't cancel current goto depot command if any bjarni@4506: DEPOT_LOCATE_HANGAR = (1 << 3), // Find another airport if the target one lacks a hangar bjarni@4506: }; bjarni@4463: rubidium@6248: struct GetNewVehiclePosResult { rubidium@7318: int x, y; tron@1977: TileIndex old_tile; tron@1977: TileIndex new_tile; rubidium@6248: }; truelight@0: matthijs@1752: /** matthijs@1752: * Returns the Trackdir on which the vehicle is currently located. matthijs@1752: * Works for trains and ships. matthijs@1752: * Currently works only sortof for road vehicles, since they have a fuzzy matthijs@1752: * concept of being "on" a trackdir. Dunno really what it returns for a road matthijs@1752: * vehicle that is halfway a tile, never really understood that part. For road matthijs@1752: * vehicles that are at the beginning or end of the tile, should just return matthijs@1752: * the diagonal trackdir on which they are driving. I _think_. matthijs@1752: * For other vehicles types, or vehicles with no clear trackdir (such as those matthijs@1752: * in depots), returns 0xFF. matthijs@1752: */ matthijs@1944: Trackdir GetVehicleTrackdir(const Vehicle* v); matthijs@1752: truelight@0: /* returns true if staying in the same tile */ tron@6153: GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v); rubidium@7318: Direction GetDirectionTowards(const Vehicle *v, int x, int y); truelight@0: truelight@0: #define BEGIN_ENUM_WAGONS(v) do { rubidium@7492: #define END_ENUM_WAGONS(v) } while ((v = v->Next()) != NULL); truelight@0: rubidium@6247: static inline VehicleID GetMaxVehicleIndex() truelight@4354: { truelight@4354: /* TODO - This isn't the real content of the function, but truelight@4354: * with the new pool-system this will be replaced with one that matthijs@5247: * _really_ returns the highest index. Now it just returns truelight@4354: * the next safe value we are sure about everything is below. truelight@4354: */ matthijs@5247: return GetVehiclePoolSize() - 1; matthijs@5247: } matthijs@5247: rubidium@6247: static inline uint GetNumVehicles() matthijs@5247: { truelight@4354: return GetVehiclePoolSize(); truelight@4354: } truelight@4354: rubidium@6641: static inline bool IsPlayerBuildableVehicleType(VehicleType type) bjarni@5746: { bjarni@5775: switch (type) { rubidium@6259: case VEH_TRAIN: rubidium@6259: case VEH_ROAD: rubidium@6259: case VEH_SHIP: rubidium@6259: case VEH_AIRCRAFT: bjarni@5746: return true; rubidium@6641: rubidium@6641: default: return false; bjarni@5746: } bjarni@5746: } bjarni@5746: bjarni@5775: static inline bool IsPlayerBuildableVehicleType(const Vehicle *v) bjarni@5775: { bjarni@5775: return IsPlayerBuildableVehicleType(v->type); bjarni@5775: } bjarni@5775: rubidium@7387: #define FOR_ALL_VEHICLES_FROM(v, start) for (v = GetVehicle(start); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) if (v->IsValid()) truelight@4346: #define FOR_ALL_VEHICLES(v) FOR_ALL_VEHICLES_FROM(v, 0) truelight@4346: matthijs@1330: /** truelight@1279: * Check if an index is a vehicle-index (so between 0 and max-vehicles) belugas@6423: * @param index of the vehicle to query truelight@1279: * @return Returns true if the vehicle-id is in range truelight@1279: */ truelight@4352: static inline bool IsValidVehicleID(uint index) bjarni@1237: { rubidium@7387: return index < GetVehiclePoolSize() && GetVehicle(index)->IsValid(); bjarni@1237: } bjarni@1237: truelight@1024: /* Returns order 'index' of a vehicle or NULL when it doesn't exists */ truelight@1024: static inline Order *GetVehicleOrder(const Vehicle *v, int index) truelight@1024: { truelight@1024: Order *order = v->orders; truelight@1024: Darkvater@1765: if (index < 0) return NULL; truelight@1024: truelight@1024: while (order != NULL && index-- > 0) truelight@1024: order = order->next; truelight@1024: truelight@1024: return order; truelight@1024: } truelight@1024: belugas@6423: /** belugas@6423: * Returns the last order of a vehicle, or NULL if it doesn't exists belugas@6423: * @param v Vehicle to query belugas@6423: * @return last order of a vehicle, if available belugas@6423: */ truelight@1024: static inline Order *GetLastVehicleOrder(const Vehicle *v) truelight@1024: { truelight@1024: Order *order = v->orders; truelight@1024: Darkvater@1765: if (order == NULL) return NULL; truelight@1024: truelight@1024: while (order->next != NULL) truelight@1024: order = order->next; truelight@1024: truelight@1024: return order; truelight@1024: } truelight@1024: belugas@6423: /** Get the first vehicle of a shared-list, so we only have to walk forwards belugas@6423: * @param v Vehicle to query belugas@6423: * @return first vehicle of a shared-list belugas@6423: */ Darkvater@4494: static inline Vehicle *GetFirstVehicleFromSharedList(const Vehicle *v) truelight@1024: { Darkvater@4494: Vehicle *u = (Vehicle *)v; Darkvater@4494: while (u->prev_shared != NULL) u = u->prev_shared; truelight@1024: truelight@1024: return u; truelight@1024: } truelight@0: belugas@6423: /* NOSAVE: Return values from various commands. */ bjarni@2564: VARDEF VehicleID _new_vehicle_id; peter1138@3008: VARDEF uint16 _returned_refit_capacity; truelight@0: rubidium@5587: static const VehicleID INVALID_VEHICLE = 0xFFFF; truelight@0: peter1138@6516: const struct Livery *GetEngineLivery(EngineID engine_type, PlayerID player, EngineID parent_engine_type, const Vehicle *v); peter1138@6516: peter1138@3105: /** peter1138@3105: * Get the colour map for an engine. This used for unbuilt engines in the user interface. peter1138@3105: * @param engine_type ID of engine peter1138@3105: * @param player ID of player peter1138@3105: * @return A ready-to-use palette modifier peter1138@3105: */ peter1138@5668: SpriteID GetEnginePalette(EngineID engine_type, PlayerID player); peter1138@3040: peter1138@3105: /** peter1138@3105: * Get the colour map for a vehicle. peter1138@3105: * @param v Vehicle to get colour map for peter1138@3105: * @return A ready-to-use palette modifier peter1138@3105: */ peter1138@5668: SpriteID GetVehiclePalette(const Vehicle *v); peter1138@3040: darkvater@755: /* A lot of code calls for the invalidation of the status bar, which is widget 5. darkvater@755: * Best is to have a virtual value for it when it needs to change again */ darkvater@755: #define STATUS_BAR 5 darkvater@755: bjarni@5792: extern const uint32 _veh_build_proc_table[]; bjarni@5792: extern const uint32 _veh_sell_proc_table[]; bjarni@5792: extern const uint32 _veh_refit_proc_table[]; Darkvater@4494: extern const uint32 _send_to_depot_proc_table[]; bjarni@5792: bjarni@5792: /* Functions to find the right command for certain vehicle type */ rubidium@6641: static inline uint32 GetCmdBuildVeh(VehicleType type) bjarni@5792: { bjarni@5955: return _veh_build_proc_table[type]; bjarni@5792: } bjarni@5792: bjarni@5792: static inline uint32 GetCmdBuildVeh(const Vehicle *v) bjarni@5792: { bjarni@5792: return GetCmdBuildVeh(v->type); bjarni@5792: } bjarni@5792: rubidium@6641: static inline uint32 GetCmdSellVeh(VehicleType type) bjarni@5792: { bjarni@5955: return _veh_sell_proc_table[type]; bjarni@5792: } bjarni@5792: bjarni@5792: static inline uint32 GetCmdSellVeh(const Vehicle *v) bjarni@5792: { bjarni@5792: return GetCmdSellVeh(v->type); bjarni@5792: } bjarni@5792: rubidium@6641: static inline uint32 GetCmdRefitVeh(VehicleType type) bjarni@5792: { bjarni@5955: return _veh_refit_proc_table[type]; bjarni@5792: } bjarni@5792: bjarni@5792: static inline uint32 GetCmdRefitVeh(const Vehicle *v) bjarni@5792: { bjarni@5792: return GetCmdRefitVeh(v->type); bjarni@5792: } bjarni@5792: rubidium@6641: static inline uint32 GetCmdSendToDepot(VehicleType type) bjarni@5792: { bjarni@5955: return _send_to_depot_proc_table[type]; bjarni@5792: } bjarni@5792: bjarni@5792: static inline uint32 GetCmdSendToDepot(const Vehicle *v) bjarni@5792: { bjarni@5792: return GetCmdSendToDepot(v->type); bjarni@5792: } bjarni@4451: truelight@0: #endif /* VEHICLE_H */