rubidium@8144: /* $Id$ */ rubidium@8144: rubidium@8144: /** @file vehicle_base.h Base class for all vehicles. */ rubidium@8144: rubidium@8144: #ifndef VEHICLE_BASE_H rubidium@8144: #define VEHICLE_BASE_H rubidium@8144: rubidium@8144: #include "vehicle_type.h" rubidium@8144: #include "track_type.h" rubidium@8144: #include "rail_type.h" rubidium@8144: #include "road_type.h" rubidium@8144: #include "cargo_type.h" rubidium@8144: #include "direction_type.h" rubidium@8144: #include "gfx_type.h" rubidium@8144: #include "command_type.h" rubidium@8144: #include "date_type.h" rubidium@10208: #include "company_base.h" rubidium@10208: #include "company_type.h" rubidium@8144: #include "oldpool.h" rubidium@8784: #include "order_base.h" rubidium@8144: #include "cargopacket.h" rubidium@8144: #include "texteff.hpp" rubidium@8770: #include "group_type.h" rubidium@8777: #include "engine_type.h" bjarni@9061: #include "order_func.h" rubidium@9126: #include "transport_type.h" rubidium@8144: rubidium@8144: /** Road vehicle states */ rubidium@8144: enum RoadVehicleStates { rubidium@8144: /* rubidium@8144: * Lower 4 bits are used for vehicle track direction. (Trackdirs) rubidium@8144: * When in a road stop (bit 5 or bit 6 set) these bits give the rubidium@8144: * track direction of the entry to the road stop. rubidium@8144: * As the entry direction will always be a diagonal rubidium@8144: * direction (X_NE, Y_SE, X_SW or Y_NW) only bits 0 and 3 rubidium@8144: * are needed to hold this direction. Bit 1 is then used to show rubidium@8144: * that the vehicle is using the second road stop bay. rubidium@8144: * Bit 2 is then used for drive-through stops to show the vehicle rubidium@8144: * is stopping at this road stop. rubidium@8144: */ rubidium@8144: rubidium@8144: /* Numeric values */ rubidium@8144: RVSB_IN_DEPOT = 0xFE, ///< The vehicle is in a depot rubidium@8144: RVSB_WORMHOLE = 0xFF, ///< The vehicle is in a tunnel and/or bridge rubidium@8144: rubidium@8144: /* Bit numbers */ rubidium@8144: RVS_USING_SECOND_BAY = 1, ///< Only used while in a road stop rubidium@8144: RVS_IS_STOPPING = 2, ///< Only used for drive-through stops. Vehicle will stop here rubidium@8144: RVS_DRIVE_SIDE = 4, ///< Only used when retrieving move data rubidium@8144: RVS_IN_ROAD_STOP = 5, ///< The vehicle is in a road stop rubidium@8144: RVS_IN_DT_ROAD_STOP = 6, ///< The vehicle is in a drive-through road stop rubidium@8144: rubidium@8144: /* Bit sets of the above specified bits */ rubidium@8144: RVSB_IN_ROAD_STOP = 1 << RVS_IN_ROAD_STOP, ///< The vehicle is in a road stop rubidium@8144: RVSB_IN_ROAD_STOP_END = RVSB_IN_ROAD_STOP + TRACKDIR_END, rubidium@8144: RVSB_IN_DT_ROAD_STOP = 1 << RVS_IN_DT_ROAD_STOP, ///< The vehicle is in a drive-through road stop rubidium@8144: RVSB_IN_DT_ROAD_STOP_END = RVSB_IN_DT_ROAD_STOP + TRACKDIR_END, rubidium@8144: rubidium@8144: RVSB_TRACKDIR_MASK = 0x0F, ///< The mask used to extract track dirs rubidium@8144: RVSB_ROAD_STOP_TRACKDIR_MASK = 0x09 ///< Only bits 0 and 3 are used to encode the trackdir for road stops rubidium@8144: }; rubidium@8144: rubidium@8144: enum VehStatus { rubidium@8144: VS_HIDDEN = 0x01, rubidium@8144: VS_STOPPED = 0x02, rubidium@8144: VS_UNCLICKABLE = 0x04, rubidium@8144: VS_DEFPAL = 0x08, rubidium@8144: VS_TRAIN_SLOWING = 0x10, rubidium@8144: VS_SHADOW = 0x20, rubidium@8144: VS_AIRCRAFT_BROKEN = 0x40, rubidium@8144: VS_CRASHED = 0x80, rubidium@8144: }; rubidium@8144: rubidium@8144: enum VehicleFlags { rubidium@8144: VF_LOADING_FINISHED, rubidium@8144: VF_CARGO_UNLOADING, rubidium@8144: VF_BUILT_AS_PROTOTYPE, rubidium@10341: VF_TIMETABLE_STARTED, ///< Whether the vehicle has started running on the timetable yet. rubidium@10341: VF_AUTOFILL_TIMETABLE, ///< Whether the vehicle should fill in the timetable automatically. rubidium@10341: VF_AUTOFILL_PRES_WAIT_TIME, ///< Whether non-destructive auto-fill should preserve waiting times rubidium@8144: }; rubidium@8144: rubidium@8144: struct VehicleRail { rubidium@9015: /* Link between the two ends of a multiheaded engine */ rubidium@9015: Vehicle *other_multiheaded_part; rubidium@9015: rubidium@9015: /* Cached wagon override spritegroup */ rubidium@9015: const struct SpriteGroup *cached_override; rubidium@9015: rubidium@8144: uint16 last_speed; // NOSAVE: only used in UI rubidium@8144: uint16 crash_anim_pos; rubidium@8144: rubidium@8144: /* cached values, recalculated on load and each time a vehicle is added to/removed from the consist. */ rubidium@9015: uint32 cached_power; ///< total power of the consist. rubidium@9015: uint16 cached_max_speed; ///< max speed of the consist. (minimum of the max speed of all vehicles in the consist) rubidium@8144: uint16 cached_total_length; ///< Length of the whole train, valid only for first engine. rubidium@9015: 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 rubidium@9015: bool cached_tilt; ///< train can tilt; feature provides a bonus in curves rubidium@8144: rubidium@8144: /* cached values, recalculated when the cargo on a train changes (in addition to the conditions above) */ rubidium@9015: uint32 cached_weight; ///< total weight of the consist. rubidium@9015: uint32 cached_veh_weight; ///< weight of the vehicle. rubidium@9015: uint32 cached_max_te; ///< max tractive effort of consist rubidium@9015: rubidium@8144: /** rubidium@8144: * Position/type of visual effect. rubidium@8144: * bit 0 - 3 = position of effect relative to vehicle. (0 = front, 8 = centre, 15 = rear) rubidium@8144: * bit 4 - 5 = type of effect. (0 = default for engine class, 1 = steam, 2 = diesel, 3 = electric) rubidium@8144: * bit 6 = disable visual effect. rubidium@8144: * bit 7 = disable powered wagons. rubidium@8144: */ rubidium@8144: byte cached_vis_effect; rubidium@8144: byte user_def_data; rubidium@8144: rubidium@8144: /* NOSAVE: for wagon override - id of the first engine in train rubidium@8144: * 0xffff == not in train */ rubidium@8144: EngineID first_engine; rubidium@8144: rubidium@9808: uint16 flags; rubidium@8144: TrackBitsByte track; rubidium@8144: byte force_proceed; rubidium@8144: RailTypeByte railtype; rubidium@8236: RailTypes compatible_railtypes; rubidium@8144: }; rubidium@8144: glx@8666: enum VehicleRailFlags { rubidium@8144: VRF_REVERSING = 0, rubidium@8144: rubidium@8144: /* used to calculate if train is going up or down */ rubidium@8144: VRF_GOINGUP = 1, rubidium@8144: VRF_GOINGDOWN = 2, rubidium@8144: rubidium@8144: /* used to store if a wagon is powered or not */ rubidium@8144: VRF_POWEREDWAGON = 3, rubidium@8144: rubidium@8144: /* used to reverse the visible direction of the vehicle */ rubidium@8144: VRF_REVERSE_DIRECTION = 4, rubidium@8144: rubidium@8144: /* used to mark train as lost because PF can't find the route */ rubidium@8144: VRF_NO_PATH_TO_DESTINATION = 5, rubidium@8144: rubidium@8144: /* used to mark that electric train engine is allowed to run on normal rail */ rubidium@8144: VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL = 6, glx@8666: glx@8938: /* used for vehicle var 0xFE bit 8 (toggled each time the train is reversed, accurate for first vehicle only) */ glx@8666: VRF_TOGGLE_REVERSE = 7, rubidium@9808: rubidium@9808: /* used to mark a train that can't get a path reservation */ rubidium@9808: VRF_TRAIN_STUCK = 8, rubidium@8144: }; rubidium@8144: rubidium@8144: struct VehicleAir { rubidium@8144: uint16 crashed_counter; rubidium@8144: uint16 cached_max_speed; rubidium@8144: byte pos; rubidium@8144: byte previous_pos; rubidium@8144: StationID targetairport; rubidium@8144: byte state; rubidium@8144: }; rubidium@8144: rubidium@8144: struct VehicleRoad { rubidium@8144: byte state; ///< @see RoadVehicleStates rubidium@8144: byte frame; rubidium@8144: uint16 blocked_ctr; rubidium@8144: byte overtaking; rubidium@8144: byte overtaking_ctr; rubidium@8144: uint16 crashed_ctr; rubidium@8144: byte reverse_ctr; rubidium@8144: struct RoadStop *slot; rubidium@8144: byte slot_age; rubidium@8144: EngineID first_engine; rubidium@8144: byte cached_veh_length; rubidium@8144: rubidium@8144: RoadType roadtype; rubidium@8144: RoadTypes compatible_roadtypes; rubidium@8144: }; rubidium@8144: rubidium@9008: struct VehicleEffect { rubidium@8144: uint16 animation_state; rubidium@8144: byte animation_substate; rubidium@8144: }; rubidium@8144: rubidium@8144: struct VehicleDisaster { rubidium@8144: uint16 image_override; rubidium@8144: VehicleID big_ufo_destroyer_target; rubidium@8144: }; rubidium@8144: rubidium@8144: struct VehicleShip { rubidium@8144: TrackBitsByte state; rubidium@8144: }; rubidium@8144: rubidium@8144: DECLARE_OLD_POOL(Vehicle, Vehicle, 9, 125) rubidium@8144: rubidium@8144: /* Some declarations of functions, so we can make them friendly */ rubidium@8144: struct SaveLoad; rubidium@8144: extern const SaveLoad *GetVehicleDescription(VehicleType vt); peter1138@8172: extern void AfterLoadVehicles(bool clear_te_id); rubidium@8144: struct LoadgameState; rubidium@8144: extern bool LoadOldVehicle(LoadgameState *ls, int num); rubidium@8144: rubidium@8144: struct Vehicle : PoolItem, BaseVehicle { rubidium@8144: private: rubidium@9015: Vehicle *next; ///< pointer to the next vehicle in the chain rubidium@9015: Vehicle *previous; ///< NOSAVE: pointer to the previous vehicle in the chain rubidium@9015: Vehicle *first; ///< NOSAVE: pointer to the first vehicle in the chain rubidium@9941: rubidium@9941: Vehicle *next_shared; ///< pointer to the next vehicle that shares the order rubidium@9941: Vehicle *previous_shared; ///< NOSAVE: pointer to the previous vehicle in the shared order chain rubidium@9941: Vehicle *first_shared; ///< NOSAVE: pointer to the first vehicle in the shared order chain rubidium@8144: public: rubidium@9015: friend const SaveLoad *GetVehicleDescription(VehicleType vt); ///< So we can use private/protected variables in the saveload code rubidium@9015: friend void AfterLoadVehicles(bool clear_te_id); ///< So we can set the previous and first pointers while loading rubidium@9015: friend bool LoadOldVehicle(LoadgameState *ls, int num); ///< So we can set the proper next pointer while loading rubidium@8144: rubidium@9015: Vehicle *depot_list; ///< NOSAVE: linked list to tell what vehicles entered a depot during the last tick. Used by autoreplace rubidium@8144: peter1138@8258: char *name; ///< Name of vehicle rubidium@8144: rubidium@9015: TileIndex tile; ///< Current tile index rubidium@9015: TileIndex dest_tile; ///< Heading for this tile rubidium@8144: rubidium@9015: Money profit_this_year; ///< Profit this year << 8, low 8 bits are fract rubidium@9015: Money profit_last_year; ///< Profit last year << 8, low 8 bits are fract rubidium@9015: Money value; rubidium@9015: rubidium@9015: /* Used for timetabling. */ rubidium@9015: uint32 current_order_time; ///< How many ticks have passed since this order started. rubidium@9015: int32 lateness_counter; ///< How many ticks late (or early if negative) this vehicle is. rubidium@9015: rubidium@9015: /* Boundaries for the current position in the world and a next hash link. rubidium@9015: * NOSAVE: All of those can be updated with VehiclePositionChanged() */ rubidium@9015: int32 left_coord; rubidium@9015: int32 top_coord; rubidium@9015: int32 right_coord; rubidium@9015: int32 bottom_coord; rubidium@9015: Vehicle *next_hash; rubidium@9015: Vehicle *next_new_hash; rubidium@9015: Vehicle **old_new_hash; rubidium@9015: rubidium@9015: SpriteID colormap; // NOSAVE: cached color mapping rubidium@9015: rubidium@9015: /* Related to age and service time */ rubidium@9015: Year build_year; rubidium@9015: Date age; // Age in days rubidium@9015: Date max_age; // Maximum age rubidium@9015: Date date_of_last_service; rubidium@9015: Date service_interval; rubidium@9015: uint16 reliability; rubidium@9015: uint16 reliability_spd_dec; rubidium@9015: byte breakdown_ctr; rubidium@9015: byte breakdown_delay; rubidium@9015: byte breakdowns_since_last_service; rubidium@9015: byte breakdown_chance; rubidium@8144: rubidium@8144: int32 x_pos; // coordinates rubidium@8144: int32 y_pos; rubidium@8144: byte z_pos; rubidium@8144: DirectionByte direction; // facing rubidium@8144: rubidium@10207: OwnerByte owner; // which company owns the vehicle? rubidium@8144: byte spritenum; // currently displayed sprite index rubidium@8144: // 0xfd == custom sprite, 0xfe == custom second head sprite rubidium@8144: // 0xff == reserved for another custom sprite rubidium@8144: uint16 cur_image; // sprite number for this vehicle frosch@8793: byte x_extent; // x-extent of vehicle bounding box frosch@8793: byte y_extent; // y-extent of vehicle bounding box frosch@8793: byte z_extent; // z-extent of vehicle bounding box rubidium@8144: int8 x_offs; // x offset for vehicle sprite rubidium@8144: int8 y_offs; // y offset for vehicle sprite rubidium@8144: EngineID engine_type; rubidium@8144: rubidium@8144: TextEffectID fill_percent_te_id; // a text-effect id to a loading indicator object rubidium@9015: UnitID unitnumber; // unit number, for display purposes only rubidium@9015: rubidium@9015: uint16 max_speed; ///< maximum speed rubidium@9015: uint16 cur_speed; ///< current speed rubidium@9015: byte subspeed; ///< fractional speed rubidium@9015: byte acceleration; ///< used by train & aircraft rubidium@9015: uint32 motion_counter; rubidium@9015: byte progress; rubidium@8144: rubidium@8144: /* for randomized variational spritegroups rubidium@8144: * bitmask used to resolve them; parts of it get reseeded when triggers rubidium@8144: * of corresponding spritegroups get matched */ rubidium@8144: byte random_bits; rubidium@9015: byte waiting_triggers; ///< triggers to be yet matched rubidium@8144: rubidium@8144: StationID last_station_visited; rubidium@8144: rubidium@9015: CargoID cargo_type; ///< type of cargo this vehicle is carrying rubidium@8144: byte cargo_subtype; ///< Used for livery refits (NewGRF variations) rubidium@9015: uint16 cargo_cap; ///< total capacity rubidium@8144: CargoList cargo; ///< The cargo this vehicle is carrying rubidium@8144: smatz@8556: byte day_counter; ///< Increased by one for each day smatz@8556: byte tick_counter; ///< Increased by one for each tick smatz@8556: byte running_ticks; ///< Number of ticks this vehicle was not stopped this day rubidium@8144: rubidium@9015: byte vehstatus; ///< Status rubidium@9015: Order current_order; ///< The current order (+ status, like: loading) rubidium@9015: VehicleOrderID num_orders; ///< How many orders there are in the list rubidium@8144: VehicleOrderID cur_order_index; ///< The index to the current order rubidium@8144: rubidium@9015: Order *orders; ///< Pointer to the first order for this vehicle rubidium@8144: rubidium@9015: bool leave_depot_instantly; ///< NOSAVE: stores if the vehicle needs to leave the depot it just entered. Used by autoreplace rubidium@8144: rubidium@9015: byte vehicle_flags; ///< Used for gradual loading and other miscellaneous things (@see VehicleFlags enum) rubidium@8144: uint16 load_unload_time_rem; rubidium@8144: rubidium@9015: GroupID group_id; ///< Index of group Pool array rubidium@8144: rubidium@9015: byte subtype; ///< subtype (Filled with values from EffectVehicles/TrainSubTypes/AircraftSubTypes) rubidium@8144: rubidium@8144: union { rubidium@8144: VehicleRail rail; rubidium@8144: VehicleAir air; rubidium@8144: VehicleRoad road; rubidium@9008: VehicleEffect effect; rubidium@8144: VehicleDisaster disaster; rubidium@8144: VehicleShip ship; rubidium@8144: } u; rubidium@8144: rubidium@8144: rubidium@8144: /** rubidium@8144: * Allocates a lot of vehicles. rubidium@8144: * @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@8144: * @param num number of vehicles to allocate room for rubidium@8144: * @return true if there is room to allocate all the vehicles rubidium@8144: */ rubidium@8144: static bool AllocateList(Vehicle **vl, int num); rubidium@8144: rubidium@8144: /** Create a new vehicle */ rubidium@8144: Vehicle(); rubidium@8144: rubidium@8144: /** Destroy all stuff that (still) needs the virtual functions to work properly */ rubidium@8144: void PreDestructor(); rubidium@8144: /** We want to 'destruct' the right class. */ rubidium@8144: virtual ~Vehicle(); rubidium@8144: rubidium@8144: void BeginLoading(); rubidium@8144: void LeaveStation(); rubidium@8144: rubidium@8144: /** rubidium@8144: * Handle the loading of the vehicle; when not it skips through dummy rubidium@8144: * orders and does nothing in all other cases. rubidium@8144: * @param mode is the non-first call for this vehicle in this tick? rubidium@8144: */ rubidium@8144: void HandleLoading(bool mode = false); rubidium@8144: rubidium@8144: /** rubidium@8144: * Get a string 'representation' of the vehicle type. rubidium@8144: * @return the string representation. rubidium@8144: */ rubidium@8144: virtual const char* GetTypeString() const { return "base vehicle"; } rubidium@8144: rubidium@8144: /** rubidium@8144: * Marks the vehicles to be redrawn and updates cached variables rubidium@8144: * rubidium@8144: * This method marks the area of the vehicle on the screen as dirty. rubidium@8144: * It can be use to repaint the vehicle. rubidium@8144: * rubidium@8144: * @ingroup dirty rubidium@8144: */ rubidium@8144: virtual void MarkDirty() {} rubidium@8144: rubidium@8144: /** rubidium@8144: * Updates the x and y offsets and the size of the sprite used rubidium@8144: * for this vehicle. rubidium@8144: * @param direction the direction the vehicle is facing rubidium@8144: */ rubidium@8144: virtual void UpdateDeltaXY(Direction direction) {} rubidium@8144: rubidium@8144: /** rubidium@8144: * Sets the expense type associated to this vehicle type rubidium@8144: * @param income whether this is income or (running) expenses of the vehicle rubidium@8144: */ rubidium@8144: virtual ExpensesType GetExpenseType(bool income) const { return EXPENSES_OTHER; } rubidium@8144: rubidium@8144: /** rubidium@8144: * Play the sound associated with leaving the station rubidium@8144: */ rubidium@8144: virtual void PlayLeaveStationSound() const {} rubidium@8144: rubidium@8144: /** rubidium@8144: * Whether this is the primary vehicle in the chain. rubidium@8144: */ rubidium@8144: virtual bool IsPrimaryVehicle() const { return false; } rubidium@8144: rubidium@8144: /** rubidium@8144: * Gets the sprite to show for the given direction rubidium@8144: * @param direction the direction the vehicle is facing rubidium@8144: * @return the sprite for the given vehicle in the given direction rubidium@8144: */ peter1138@9022: virtual SpriteID GetImage(Direction direction) const { return 0; } rubidium@8144: rubidium@8144: /** rubidium@8144: * Gets the speed in mph that can be sent into SetDParam for string processing. rubidium@8144: * @return the vehicle's speed rubidium@8144: */ rubidium@8144: virtual int GetDisplaySpeed() const { return 0; } rubidium@8144: rubidium@8144: /** rubidium@8144: * Gets the maximum speed in mph that can be sent into SetDParam for string processing. rubidium@8144: * @return the vehicle's maximum speed rubidium@8144: */ rubidium@8144: virtual int GetDisplayMaxSpeed() const { return 0; } rubidium@8144: rubidium@8144: /** rubidium@8144: * Gets the running cost of a vehicle rubidium@8144: * @return the vehicle's running cost rubidium@8144: */ rubidium@8144: virtual Money GetRunningCost() const { return 0; } rubidium@8144: rubidium@8144: /** rubidium@8144: * Check whether the vehicle is in the depot. rubidium@8144: * @return true if and only if the vehicle is in the depot. rubidium@8144: */ rubidium@8144: virtual bool IsInDepot() const { return false; } rubidium@8144: rubidium@8144: /** rubidium@8144: * Check whether the vehicle is in the depot *and* stopped. rubidium@8144: * @return true if and only if the vehicle is in the depot and stopped. rubidium@8144: */ rubidium@8144: virtual bool IsStoppedInDepot() const { return this->IsInDepot() && (this->vehstatus & VS_STOPPED) != 0; } rubidium@8144: rubidium@8144: /** rubidium@8144: * Calls the tick handler of the vehicle rubidium@8144: */ rubidium@8144: virtual void Tick() {}; rubidium@8144: rubidium@8144: /** glx@8467: * Calls the new day handler of the vehicle glx@8467: */ glx@8467: virtual void OnNewDay() {}; glx@8467: glx@8467: /** rubidium@8144: * Gets the running cost of a vehicle that can be sent into SetDParam for string processing. rubidium@8144: * @return the vehicle's running cost rubidium@8144: */ rubidium@8144: Money GetDisplayRunningCost() const { return (this->GetRunningCost() >> 8); } rubidium@8144: rubidium@8144: /** smatz@8614: * Gets the profit vehicle had this year. It can be sent into SetDParam for string processing. smatz@8614: * @return the vehicle's profit this year smatz@8614: */ smatz@8614: Money GetDisplayProfitThisYear() const { return (this->profit_this_year >> 8); } smatz@8614: smatz@8614: /** smatz@8614: * Gets the profit vehicle had last year. It can be sent into SetDParam for string processing. smatz@8614: * @return the vehicle's profit last year smatz@8614: */ smatz@8614: Money GetDisplayProfitLastYear() const { return (this->profit_last_year >> 8); } smatz@8614: smatz@8614: /** rubidium@8144: * Set the next vehicle of this vehicle. rubidium@8144: * @param next the next vehicle. NULL removes the next vehicle. rubidium@8144: */ rubidium@8144: void SetNext(Vehicle *next); rubidium@8144: rubidium@8144: /** rubidium@8144: * Get the next vehicle of this vehicle. rubidium@8144: * @note articulated parts are also counted as vehicles. rubidium@8144: * @return the next vehicle or NULL when there isn't a next vehicle. rubidium@8144: */ rubidium@8144: inline Vehicle *Next() const { return this->next; } rubidium@8144: rubidium@8144: /** rubidium@8144: * Get the previous vehicle of this vehicle. rubidium@8144: * @note articulated parts are also counted as vehicles. rubidium@8144: * @return the previous vehicle or NULL when there isn't a previous vehicle. rubidium@8144: */ rubidium@8144: inline Vehicle *Previous() const { return this->previous; } rubidium@8144: rubidium@8144: /** rubidium@8144: * Get the first vehicle of this vehicle chain. rubidium@8144: * @return the first vehicle of the chain. rubidium@8144: */ rubidium@8144: inline Vehicle *First() const { return this->first; } belugas@8469: rubidium@9941: rubidium@9941: /** rubidium@9941: * Adds this vehicle to a shared vehicle chain. rubidium@9941: * @param shared_chain a vehicle of the chain with shared vehicles. rubidium@9941: * @pre !this->IsOrderListShared() rubidium@9941: */ rubidium@9941: void AddToShared(Vehicle *shared_chain); rubidium@9941: rubidium@9941: /** rubidium@9941: * Removes the vehicle from the shared order list. rubidium@9941: */ rubidium@9941: void RemoveFromShared(); rubidium@9941: rubidium@9941: /** rubidium@9941: * Get the next vehicle of this vehicle. rubidium@9941: * @note articulated parts are also counted as vehicles. rubidium@9941: * @return the next vehicle or NULL when there isn't a next vehicle. rubidium@9941: */ rubidium@9941: inline Vehicle *NextShared() const { return this->next_shared; } rubidium@9941: rubidium@9941: /** rubidium@9941: * Get the first vehicle of this vehicle chain. rubidium@9941: * @return the first vehicle of the chain. rubidium@9941: */ rubidium@9941: inline Vehicle *FirstShared() const { return this->first_shared; } rubidium@9941: belugas@8469: /** belugas@8469: * Check if we share our orders with another vehicle. belugas@8469: * @return true if there are other vehicles sharing the same order belugas@8469: */ rubidium@9941: inline bool IsOrderListShared() const { return this->previous_shared != NULL || this->next_shared != NULL; }; bjarni@8628: frosch@9928: /** frosch@9928: * Copy certain configurations and statistics of a vehicle after successful autoreplace/renew frosch@9928: * The function shall copy everything that cannot be copied by a command (like orders / group etc), frosch@9928: * and that shall not be resetted for the new vehicle. frosch@9928: * @param src The old vehicle frosch@9928: */ frosch@9928: inline void CopyVehicleConfigAndStatistics(const Vehicle *src) frosch@9928: { frosch@9928: this->unitnumber = src->unitnumber; frosch@9928: frosch@9928: this->cur_order_index = src->cur_order_index; frosch@9928: this->current_order = src->current_order; frosch@9928: this->dest_tile = src->dest_tile; frosch@9928: frosch@9928: this->profit_this_year = src->profit_this_year; frosch@9928: this->profit_last_year = src->profit_last_year; frosch@9928: frosch@9928: this->current_order_time = src->current_order_time; frosch@9928: this->lateness_counter = src->lateness_counter; frosch@9928: frosch@9928: this->service_interval = src->service_interval; frosch@9928: } frosch@9928: rubidium@10207: bool NeedsAutorenewing(const Company *c) const; rubidium@8827: rubidium@8827: /** frosch@8862: * Check if the vehicle needs to go to a depot in near future (if a opportunity presents itself) for service or replacement. frosch@8862: * frosch@8862: * @see NeedsAutomaticServicing() frosch@8862: * @return true if the vehicle should go to a depot if a opportunity presents itself. frosch@8862: */ frosch@8862: bool NeedsServicing() const; frosch@8862: frosch@8862: /** frosch@8862: * Checks if the current order should be interupted for a service-in-depot-order. frosch@8862: * @see NeedsServicing() frosch@8862: * @return true if the current order should be interupted. frosch@8862: */ frosch@8862: bool NeedsAutomaticServicing() const; frosch@8862: frosch@8862: /** rubidium@8827: * Determine the location for the station where the vehicle goes to next. rubidium@8827: * Things done for example are allocating slots in a road stop or exact rubidium@8827: * location of the platform is determined for ships. rubidium@8827: * @param station the station to make the next location of the vehicle. rubidium@8827: * @return the location (tile) to aim for. rubidium@8827: */ rubidium@8827: virtual TileIndex GetOrderStationLocation(StationID station) { return INVALID_TILE; } rubidium@8890: rubidium@8890: /** rubidium@8890: * Find the closest depot for this vehicle and tell us the location, rubidium@8890: * DestinationID and whether we should reverse. rubidium@8890: * @param location where do we go to? rubidium@8890: * @param destination what hangar do we go to? rubidium@8890: * @param reverse should the vehicle be reversed? rubidium@8890: * @return true if a depot could be found. rubidium@8890: */ rubidium@8890: virtual bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse) { return false; } rubidium@8891: rubidium@8891: /** rubidium@8891: * Send this vehicle to the depot using the given command(s). rubidium@8891: * @param flags the command flags (like execute and such). rubidium@8891: * @param command the command to execute. rubidium@8891: * @return the cost of the depot action. rubidium@8891: */ rubidium@8891: CommandCost SendToDepot(uint32 flags, DepotCommand command); rubidium@8144: }; rubidium@8144: rubidium@8144: /** rubidium@8144: * This class 'wraps' Vehicle; you do not actually instantiate this class. rubidium@8144: * You create a Vehicle using AllocateVehicle, so it is added to the pool rubidium@8144: * and you reinitialize that to a Train using: rubidium@8144: * v = new (v) Train(); rubidium@8144: * rubidium@8144: * As side-effect the vehicle type is set correctly. rubidium@8144: */ rubidium@8144: struct DisasterVehicle : public Vehicle { rubidium@8144: /** Initializes the Vehicle to a disaster vehicle */ rubidium@8144: DisasterVehicle() { this->type = VEH_DISASTER; } rubidium@8144: rubidium@8144: /** We want to 'destruct' the right class. */ rubidium@8144: virtual ~DisasterVehicle() {} rubidium@8144: rubidium@8144: const char *GetTypeString() const { return "disaster vehicle"; } rubidium@8144: void UpdateDeltaXY(Direction direction); rubidium@8144: void Tick(); rubidium@8144: }; rubidium@8144: rubidium@8144: /** rubidium@8144: * This class 'wraps' Vehicle; you do not actually instantiate this class. rubidium@8144: * You create a Vehicle using AllocateVehicle, so it is added to the pool rubidium@8144: * and you reinitialize that to a Train using: rubidium@8144: * v = new (v) Train(); rubidium@8144: * rubidium@8144: * As side-effect the vehicle type is set correctly. rubidium@8144: */ rubidium@8144: struct InvalidVehicle : public Vehicle { rubidium@8144: /** Initializes the Vehicle to a invalid vehicle */ rubidium@8144: InvalidVehicle() { this->type = VEH_INVALID; } rubidium@8144: rubidium@8144: /** We want to 'destruct' the right class. */ rubidium@8144: virtual ~InvalidVehicle() {} rubidium@8144: rubidium@8144: const char *GetTypeString() const { return "invalid vehicle"; } rubidium@8144: void Tick() {} rubidium@8144: }; rubidium@8144: rubidium@8144: static inline VehicleID GetMaxVehicleIndex() rubidium@8144: { rubidium@8144: /* TODO - This isn't the real content of the function, but rubidium@8144: * with the new pool-system this will be replaced with one that rubidium@8144: * _really_ returns the highest index. Now it just returns rubidium@8144: * the next safe value we are sure about everything is below. rubidium@8144: */ rubidium@8144: return GetVehiclePoolSize() - 1; rubidium@8144: } rubidium@8144: rubidium@8144: static inline uint GetNumVehicles() rubidium@8144: { rubidium@8144: return GetVehiclePoolSize(); rubidium@8144: } rubidium@8144: rubidium@8144: #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()) rubidium@8144: #define FOR_ALL_VEHICLES(v) FOR_ALL_VEHICLES_FROM(v, 0) rubidium@8144: rubidium@8144: /** rubidium@8144: * Check if an index is a vehicle-index (so between 0 and max-vehicles) rubidium@8144: * @param index of the vehicle to query rubidium@8144: * @return Returns true if the vehicle-id is in range rubidium@8144: */ rubidium@8144: static inline bool IsValidVehicleID(uint index) rubidium@8144: { rubidium@8144: return index < GetVehiclePoolSize() && GetVehicle(index)->IsValid(); rubidium@8144: } rubidium@8144: rubidium@8144: /* Returns order 'index' of a vehicle or NULL when it doesn't exists */ rubidium@8144: static inline Order *GetVehicleOrder(const Vehicle *v, int index) rubidium@8144: { rubidium@8144: Order *order = v->orders; rubidium@8144: rubidium@8144: if (index < 0) return NULL; rubidium@8144: rubidium@8144: while (order != NULL && index-- > 0) rubidium@8144: order = order->next; rubidium@8144: rubidium@8144: return order; rubidium@8144: } rubidium@8144: rubidium@8144: /** rubidium@8144: * Returns the last order of a vehicle, or NULL if it doesn't exists rubidium@8144: * @param v Vehicle to query rubidium@8144: * @return last order of a vehicle, if available rubidium@8144: */ rubidium@8144: static inline Order *GetLastVehicleOrder(const Vehicle *v) rubidium@8144: { rubidium@8144: Order *order = v->orders; rubidium@8144: rubidium@8144: if (order == NULL) return NULL; rubidium@8144: rubidium@8144: while (order->next != NULL) rubidium@8144: order = order->next; rubidium@8144: rubidium@8144: return order; rubidium@8144: } rubidium@8144: rubidium@8144: /** rubidium@8144: * Returns the Trackdir on which the vehicle is currently located. rubidium@8144: * Works for trains and ships. rubidium@8144: * Currently works only sortof for road vehicles, since they have a fuzzy rubidium@8144: * concept of being "on" a trackdir. Dunno really what it returns for a road rubidium@8144: * vehicle that is halfway a tile, never really understood that part. For road rubidium@8144: * vehicles that are at the beginning or end of the tile, should just return rubidium@8144: * the diagonal trackdir on which they are driving. I _think_. rubidium@8144: * For other vehicles types, or vehicles with no clear trackdir (such as those rubidium@8144: * in depots), returns 0xFF. rubidium@8144: */ rubidium@8144: Trackdir GetVehicleTrackdir(const Vehicle* v); rubidium@8144: rubidium@8144: void CheckVehicle32Day(Vehicle *v); rubidium@8144: rubidium@8144: #endif /* VEHICLE_BASE_H */