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 "window_type.h" rubidium@8144: #include "gfx_type.h" rubidium@8144: #include "command_type.h" rubidium@8144: #include "date_type.h" rubidium@8254: #include "player_type.h" rubidium@8144: #include "oldpool.h" rubidium@8144: #include "order.h" rubidium@8144: #include "cargopacket.h" rubidium@8144: #include "texteff.hpp" 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@8144: VF_TIMETABLE_STARTED, ///< Whether the vehicle has started running on the timetable yet. rubidium@8144: VF_AUTOFILL_TIMETABLE, ///< Whether the vehicle should fill in the timetable automatically. rubidium@8144: }; rubidium@8144: rubidium@8144: struct VehicleRail { 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@8144: uint16 cached_max_speed; // max speed of the consist. (minimum of the max speed of all vehicles in the consist) rubidium@8144: uint32 cached_power; // total power of the consist. peter1138@8178: bool cached_tilt; // train can tilt; feature provides a bonus in curves rubidium@8144: 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@8144: uint16 cached_total_length; ///< Length of the whole train, valid only for first engine. rubidium@8144: rubidium@8144: /* cached values, recalculated when the cargo on a train changes (in addition to the conditions above) */ rubidium@8144: uint32 cached_weight; // total weight of the consist. rubidium@8144: uint32 cached_veh_weight; // weight of the vehicle. rubidium@8144: uint32 cached_max_te; // max tractive effort of consist 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@8144: TrackBitsByte track; rubidium@8144: byte force_proceed; rubidium@8144: RailTypeByte railtype; rubidium@8236: RailTypes compatible_railtypes; rubidium@8144: rubidium@8144: byte flags; rubidium@8144: rubidium@8144: /* Link between the two ends of a multiheaded engine */ rubidium@8144: Vehicle *other_multiheaded_part; rubidium@8144: rubidium@8144: /* Cached wagon override spritegroup */ rubidium@8144: const struct SpriteGroup *cached_override; rubidium@8144: }; rubidium@8144: rubidium@8144: enum { 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, 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@8144: struct VehicleSpecial { 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: byte subtype; // subtype (Filled with values from EffectVehicles/TrainSubTypes/AircraftSubTypes) rubidium@8144: rubidium@8144: private: rubidium@8144: Vehicle *next; // pointer to the next vehicle in the chain rubidium@8144: Vehicle *previous; // NOSAVE: pointer to the previous vehicle in the chain rubidium@8144: Vehicle *first; // NOSAVE: pointer to the first vehicle in the chain rubidium@8144: public: rubidium@8144: friend const SaveLoad *GetVehicleDescription(VehicleType vt); // So we can use private/protected variables in the saveload code peter1138@8172: friend void AfterLoadVehicles(bool clear_te_id); // So we can set the previous and first pointers while loading rubidium@8144: friend bool LoadOldVehicle(LoadgameState *ls, int num); // So we can set the proper next pointer while loading rubidium@8144: rubidium@8144: 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@8144: UnitID unitnumber; // unit number, for display purposes only rubidium@8144: PlayerByte owner; // which player owns the vehicle? rubidium@8144: rubidium@8144: TileIndex tile; // Current tile index rubidium@8144: TileIndex dest_tile; // Heading for this tile 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@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 rubidium@8144: byte sprite_width; // width of vehicle sprite rubidium@8144: byte sprite_height; // height of vehicle sprite rubidium@8144: byte z_height; // z-height of vehicle sprite 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@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@8144: byte waiting_triggers; // triggers to be yet matched rubidium@8144: rubidium@8144: uint16 max_speed; // maximum speed rubidium@8144: uint16 cur_speed; // current speed rubidium@8144: byte subspeed; // fractional speed rubidium@8144: byte acceleration; // used by train & aircraft rubidium@8144: byte progress; rubidium@8144: uint32 motion_counter; rubidium@8144: rubidium@8144: byte vehstatus; // Status rubidium@8144: StationID last_station_visited; rubidium@8144: rubidium@8144: CargoID cargo_type; // type of cargo this vehicle is carrying rubidium@8144: uint16 cargo_cap; // total capacity rubidium@8144: byte cargo_subtype; ///< Used for livery refits (NewGRF variations) rubidium@8144: CargoList cargo; ///< The cargo this vehicle is carrying rubidium@8144: 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@8144: /* Begin Order-stuff */ rubidium@8144: Order current_order; ///< The current order (+ status, like: loading) rubidium@8144: VehicleOrderID cur_order_index; ///< The index to the current order rubidium@8144: rubidium@8144: Order *orders; ///< Pointer to the first order for this vehicle rubidium@8144: VehicleOrderID num_orders; ///< How many orders there are in the list rubidium@8144: rubidium@8144: Vehicle *next_shared; ///< If not NULL, this points to the next vehicle that shared the order rubidium@8144: Vehicle *prev_shared; ///< If not NULL, this points to the prev vehicle that shared the order rubidium@8144: /* End Order-stuff */ rubidium@8144: rubidium@8144: /* Boundaries for the current position in the world and a next hash link. rubidium@8144: * NOSAVE: All of those can be updated with VehiclePositionChanged() */ rubidium@8144: int32 left_coord; rubidium@8144: int32 top_coord; rubidium@8144: int32 right_coord; rubidium@8144: int32 bottom_coord; rubidium@8144: Vehicle *next_hash; rubidium@8144: Vehicle *next_new_hash; rubidium@8144: Vehicle **old_new_hash; rubidium@8144: rubidium@8144: /* Related to age and service time */ rubidium@8144: Date age; // Age in days rubidium@8144: Date max_age; // Maximum age rubidium@8144: Date date_of_last_service; rubidium@8144: Date service_interval; rubidium@8144: uint16 reliability; rubidium@8144: uint16 reliability_spd_dec; rubidium@8144: byte breakdown_ctr; rubidium@8144: byte breakdown_delay; rubidium@8144: byte breakdowns_since_last_service; rubidium@8144: byte breakdown_chance; rubidium@8144: Year build_year; rubidium@8144: rubidium@8144: bool leave_depot_instantly; // NOSAVE: stores if the vehicle needs to leave the depot it just entered. Used by autoreplace rubidium@8144: rubidium@8144: uint16 load_unload_time_rem; rubidium@8144: byte vehicle_flags; // Used for gradual loading and other miscellaneous things (@see VehicleFlags enum) rubidium@8144: smatz@8556: Money profit_this_year; ///< Profit this year << 8, low 8 bits are fract smatz@8556: Money profit_last_year; ///< Profit last year << 8, low 8 bits are fract rubidium@8144: Money value; rubidium@8144: rubidium@8144: GroupID group_id; ///< Index of group Pool array rubidium@8144: rubidium@8144: /* Used for timetabling. */ rubidium@8144: uint32 current_order_time; ///< How many ticks have passed since this order started. rubidium@8144: int32 lateness_counter; ///< How many ticks late (or early if negative) this vehicle is. rubidium@8144: rubidium@8144: SpriteID colormap; // NOSAVE: cached color mapping rubidium@8144: rubidium@8144: union { rubidium@8144: VehicleRail rail; rubidium@8144: VehicleAir air; rubidium@8144: VehicleRoad road; rubidium@8144: VehicleSpecial special; 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: * Invalidates the vehicle list window of this type of vehicle rubidium@8144: */ rubidium@8144: virtual WindowClass GetVehicleListWindowClass() const { return WC_NONE; } 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: */ rubidium@8144: virtual int 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: /** 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: belugas@8469: /** belugas@8469: * Check if we share our orders with another vehicle. belugas@8469: * This is done by checking the previous and next pointers in the shared chain. belugas@8469: * @return true if there are other vehicles sharing the same order belugas@8469: */ belugas@8469: inline bool IsOrderListShared() const { return this->next_shared != NULL || this->prev_shared != NULL; }; 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: * A special vehicle is one of the following: rubidium@8144: * - smoke rubidium@8144: * - electric sparks for trains rubidium@8144: * - explosions rubidium@8144: * - bulldozer (road works) rubidium@8144: * - bubbles (industry) rubidium@8144: */ rubidium@8144: struct SpecialVehicle : public Vehicle { rubidium@8144: /** Initializes the Vehicle to a special vehicle */ rubidium@8144: SpecialVehicle() { this->type = VEH_SPECIAL; } rubidium@8144: rubidium@8144: /** We want to 'destruct' the right class. */ rubidium@8144: virtual ~SpecialVehicle() {} rubidium@8144: rubidium@8144: const char *GetTypeString() const { return "special 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 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: #define BEGIN_ENUM_WAGONS(v) do { rubidium@8144: #define END_ENUM_WAGONS(v) } while ((v = v->Next()) != NULL); 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: /** Get the first vehicle of a shared-list, so we only have to walk forwards rubidium@8144: * @param v Vehicle to query rubidium@8144: * @return first vehicle of a shared-list rubidium@8144: */ rubidium@8144: static inline Vehicle *GetFirstVehicleFromSharedList(const Vehicle *v) rubidium@8144: { rubidium@8144: Vehicle *u = (Vehicle *)v; rubidium@8144: while (u->prev_shared != NULL) u = u->prev_shared; rubidium@8144: rubidium@8144: return u; 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 */