tron@2186: /* $Id$ */ tron@2186: rubidium@8640: /** @file vehicle_base.h Base class for all vehicles. */ belugas@6919: rubidium@8640: #ifndef VEHICLE_BASE_H rubidium@8640: #define VEHICLE_BASE_H truelight@0: rubidium@8604: #include "vehicle_type.h" rubidium@8598: #include "track_type.h" rubidium@8598: #include "rail_type.h" rubidium@8598: #include "road_type.h" rubidium@8615: #include "cargo_type.h" rubidium@8634: #include "direction_type.h" rubidium@8602: #include "window_type.h" rubidium@8619: #include "gfx_type.h" rubidium@8619: #include "command_type.h" rubidium@8636: #include "date_type.h" rubidium@8750: #include "player_type.h" rubidium@8619: #include "oldpool.h" rubidium@8619: #include "order.h" rubidium@7506: #include "cargopacket.h" truelight@7494: #include "texteff.hpp" rubidium@6317: rubidium@6326: /** Road vehicle states */ rubidium@6326: enum RoadVehicleStates { rubidium@6326: /* rubidium@6326: * Lower 4 bits are used for vehicle track direction. (Trackdirs) rubidium@6487: * When in a road stop (bit 5 or bit 6 set) these bits give the rubidium@6326: * track direction of the entry to the road stop. rubidium@6326: * As the entry direction will always be a diagonal rubidium@6326: * direction (X_NE, Y_SE, X_SW or Y_NW) only bits 0 and 3 rubidium@6326: * are needed to hold this direction. Bit 1 is then used to show rubidium@6326: * that the vehicle is using the second road stop bay. rubidium@6487: * Bit 2 is then used for drive-through stops to show the vehicle rubidium@6487: * is stopping at this road stop. rubidium@6326: */ rubidium@6326: rubidium@6326: /* Numeric values */ rubidium@6326: RVSB_IN_DEPOT = 0xFE, ///< The vehicle is in a depot rubidium@6326: RVSB_WORMHOLE = 0xFF, ///< The vehicle is in a tunnel and/or bridge rubidium@6326: rubidium@6326: /* Bit numbers */ rubidium@6326: RVS_USING_SECOND_BAY = 1, ///< Only used while in a road stop rubidium@6338: RVS_IS_STOPPING = 2, ///< Only used for drive-through stops. Vehicle will stop here rubidium@6487: RVS_DRIVE_SIDE = 4, ///< Only used when retrieving move data rubidium@6326: RVS_IN_ROAD_STOP = 5, ///< The vehicle is in a road stop rubidium@6338: RVS_IN_DT_ROAD_STOP = 6, ///< The vehicle is in a drive-through road stop rubidium@6326: rubidium@6326: /* Bit sets of the above specified bits */ rubidium@6326: RVSB_IN_ROAD_STOP = 1 << RVS_IN_ROAD_STOP, ///< The vehicle is in a road stop rubidium@6326: RVSB_IN_ROAD_STOP_END = RVSB_IN_ROAD_STOP + TRACKDIR_END, rubidium@6338: RVSB_IN_DT_ROAD_STOP = 1 << RVS_IN_DT_ROAD_STOP, ///< The vehicle is in a drive-through road stop rubidium@6338: RVSB_IN_DT_ROAD_STOP_END = RVSB_IN_DT_ROAD_STOP + TRACKDIR_END, rubidium@6326: rubidium@6326: RVSB_TRACKDIR_MASK = 0x0F, ///< The mask used to extract track dirs rubidium@6326: RVSB_ROAD_STOP_TRACKDIR_MASK = 0x09 ///< Only bits 0 and 3 are used to encode the trackdir for road stops rubidium@6326: }; rubidium@6326: 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@6501: enum VehicleFlags { maedhros@6501: VF_LOADING_FINISHED, maedhros@6501: VF_CARGO_UNLOADING, maedhros@6502: VF_BUILT_AS_PROTOTYPE, maedhros@7562: VF_TIMETABLE_STARTED, ///< Whether the vehicle has started running on the timetable yet. maedhros@7562: VF_AUTOFILL_TIMETABLE, ///< Whether the vehicle should fill in the timetable automatically. peter1138@5211: }; peter1138@5211: rubidium@6574: struct VehicleRail { rubidium@4434: uint16 last_speed; // NOSAVE: only used in UI truelight@0: uint16 crash_anim_pos; truelight@193: belugas@6919: /* 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. peter1138@8674: bool cached_tilt; // train can tilt; feature provides a bonus in curves 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@6919: /* 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@5588: 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@8377: byte user_def_data; truelight@0: belugas@6919: /* NOSAVE: for wagon override - id of the first engine in train belugas@6919: * 0xffff == not in train */ Darkvater@1796: EngineID first_engine; truelight@0: rubidium@5838: TrackBitsByte track; truelight@0: byte force_proceed; rubidium@5838: RailTypeByte railtype; rubidium@8732: RailTypes compatible_railtypes; truelight@0: truelight@0: byte flags; hackykid@2008: belugas@6919: /* Link between the two ends of a multiheaded engine */ bjarni@2676: Vehicle *other_multiheaded_part; peter1138@7099: peter1138@7099: /* Cached wagon override spritegroup */ peter1138@7099: const struct SpriteGroup *cached_override; rubidium@6574: }; truelight@0: glx@9162: enum VehicleRailFlags { rubidium@4344: VRF_REVERSING = 0, truelight@0: belugas@6919: /* used to calculate if train is going up or down */ rubidium@4344: VRF_GOINGUP = 1, rubidium@4344: VRF_GOINGDOWN = 2, hackykid@1908: belugas@6919: /* used to store if a wagon is powered or not */ rubidium@4344: VRF_POWEREDWAGON = 3, bjarni@3256: belugas@6919: /* used to reverse the visible direction of the vehicle */ bjarni@3256: VRF_REVERSE_DIRECTION = 4, KUDr@4870: belugas@6919: /* used to mark train as lost because PF can't find the route */ KUDr@4870: VRF_NO_PATH_TO_DESTINATION = 5, KUDr@5116: belugas@6919: /* used to mark that electric train engine is allowed to run on normal rail */ KUDr@5116: VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL = 6, glx@9162: glx@9162: /* used for vehicle var 0xFE bit 8 (toggled each time the train is reversed) */ glx@9162: VRF_TOGGLE_REVERSE = 7, truelight@0: }; truelight@0: rubidium@6574: struct VehicleAir { truelight@0: uint16 crashed_counter; peter1138@6986: uint16 cached_max_speed; truelight@0: byte pos; tron@4077: byte previous_pos; Darkvater@3347: StationID targetairport; truelight@0: byte state; rubidium@6574: }; truelight@0: rubidium@6574: struct VehicleRoad { belugas@6919: 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@7353: EngineID first_engine; maedhros@7353: byte cached_veh_length; rubidium@7179: rubidium@7179: RoadType roadtype; rubidium@7179: RoadTypes compatible_roadtypes; rubidium@6574: }; truelight@0: rubidium@6574: struct VehicleSpecial { rubidium@7830: uint16 animation_state; rubidium@7830: byte animation_substate; rubidium@6574: }; truelight@0: rubidium@6574: struct VehicleDisaster { truelight@0: uint16 image_override; rubidium@7830: VehicleID big_ufo_destroyer_target; rubidium@6574: }; truelight@0: rubidium@6574: struct VehicleShip { rubidium@5838: TrackBitsByte state; rubidium@6574: }; truelight@0: rubidium@7894: DECLARE_OLD_POOL(Vehicle, Vehicle, 9, 125) truelight@0: rubidium@8033: /* Some declarations of functions, so we can make them friendly */ rubidium@7989: struct SaveLoad; rubidium@8033: extern const SaveLoad *GetVehicleDescription(VehicleType vt); peter1138@8668: extern void AfterLoadVehicles(bool clear_te_id); rubidium@8033: struct LoadgameState; rubidium@8033: extern bool LoadOldVehicle(LoadgameState *ls, int num); rubidium@7989: rubidium@8640: struct Vehicle : PoolItem, BaseVehicle { Darkvater@6105: byte subtype; // subtype (Filled with values from EffectVehicles/TrainSubTypes/AircraftSubTypes) truelight@0: rubidium@7989: private: rubidium@7988: Vehicle *next; // pointer to the next vehicle in the chain rubidium@7993: Vehicle *previous; // NOSAVE: pointer to the previous vehicle in the chain rubidium@7993: Vehicle *first; // NOSAVE: pointer to the first vehicle in the chain rubidium@7989: public: rubidium@7989: friend const SaveLoad *GetVehicleDescription(VehicleType vt); // So we can use private/protected variables in the saveload code peter1138@8668: friend void AfterLoadVehicles(bool clear_te_id); // So we can set the previous and first pointers while loading rubidium@8033: friend bool LoadOldVehicle(LoadgameState *ls, int num); // So we can set the proper next pointer while loading rubidium@7989: rubidium@7988: Vehicle *depot_list; // NOSAVE: linked list to tell what vehicles entered a depot during the last tick. Used by autoreplace truelight@0: peter1138@8754: char *name; ///< Name of vehicle truelight@0: rubidium@4344: UnitID unitnumber; // unit number, for display purposes only belugas@6919: 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@5838: 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@7494: TextEffectID fill_percent_te_id; // a text-effect id to a loading indicator object truelight@7494: belugas@6919: /* for randomized variational spritegroups belugas@6919: * bitmask used to resolve them; parts of it get reseeded when triggers belugas@6919: * 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@7506: CargoList cargo; ///< The cargo this vehicle is carrying rubidium@7506: truelight@0: smatz@9052: byte day_counter; ///< Increased by one for each day smatz@9052: byte tick_counter; ///< Increased by one for each tick smatz@9052: byte running_ticks; ///< Number of ticks this vehicle was not stopped this day 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@6919: /* Boundaries for the current position in the world and a next hash link. belugas@6919: * 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@5825: Vehicle *next_hash; peter1138@7367: Vehicle *next_new_hash; peter1138@7367: Vehicle **old_new_hash; truelight@0: belugas@6919: /* 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@6501: byte vehicle_flags; // Used for gradual loading and other miscellaneous things (@see VehicleFlags enum) truelight@193: smatz@9052: Money profit_this_year; ///< Profit this year << 8, low 8 bits are fract smatz@9052: Money profit_last_year; ///< Profit last year << 8, low 8 bits are fract rubidium@7449: Money value; truelight@0: rubidium@7139: GroupID group_id; ///< Index of group Pool array rubidium@7139: maedhros@7476: /* Used for timetabling. */ maedhros@7476: uint32 current_order_time; ///< How many ticks have passed since this order started. maedhros@7476: int32 lateness_counter; ///< How many ticks late (or early if negative) this vehicle is. maedhros@7476: glx@8298: SpriteID colormap; // NOSAVE: cached color mapping glx@8298: 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@5902: rubidium@7894: rubidium@7894: /** rubidium@7894: * Allocates a lot of vehicles. rubidium@7894: * @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@7894: * @param num number of vehicles to allocate room for rubidium@7894: * @return true if there is room to allocate all the vehicles rubidium@7894: */ rubidium@7894: static bool AllocateList(Vehicle **vl, int num); rubidium@7894: rubidium@7894: /** Create a new vehicle */ rubidium@7894: Vehicle(); rubidium@7894: rubidium@7908: /** Destroy all stuff that (still) needs the virtual functions to work properly */ rubidium@7908: void PreDestructor(); rubidium@7894: /** We want to 'destruct' the right class. */ rubidium@7894: virtual ~Vehicle(); rubidium@7894: KUDr@5902: void BeginLoading(); KUDr@5902: void LeaveStation(); rubidium@7048: rubidium@7048: /** rubidium@7090: * Handle the loading of the vehicle; when not it skips through dummy rubidium@7090: * orders and does nothing in all other cases. rubidium@7090: * @param mode is the non-first call for this vehicle in this tick? rubidium@7090: */ rubidium@7090: void HandleLoading(bool mode = false); rubidium@7090: rubidium@7090: /** rubidium@7048: * Get a string 'representation' of the vehicle type. rubidium@7048: * @return the string representation. rubidium@7048: */ rubidium@7896: virtual const char* GetTypeString() const { return "base vehicle"; } rubidium@7049: rubidium@7049: /** rubidium@7049: * Marks the vehicles to be redrawn and updates cached variables rubidium@8041: * rubidium@8041: * This method marks the area of the vehicle on the screen as dirty. rubidium@8041: * It can be use to repaint the vehicle. rubidium@8041: * rubidium@8041: * @ingroup dirty rubidium@7049: */ rubidium@7049: virtual void MarkDirty() {} rubidium@7054: rubidium@7054: /** rubidium@7054: * Updates the x and y offsets and the size of the sprite used rubidium@7054: * for this vehicle. rubidium@7054: * @param direction the direction the vehicle is facing rubidium@7054: */ rubidium@7054: virtual void UpdateDeltaXY(Direction direction) {} rubidium@7058: rubidium@7058: /** rubidium@7058: * Sets the expense type associated to this vehicle type rubidium@7058: * @param income whether this is income or (running) expenses of the vehicle rubidium@7058: */ rubidium@7059: virtual ExpensesType GetExpenseType(bool income) const { return EXPENSES_OTHER; } rubidium@7058: rubidium@7058: /** rubidium@7058: * Invalidates the vehicle list window of this type of vehicle rubidium@7058: */ rubidium@7059: virtual WindowClass GetVehicleListWindowClass() const { return WC_NONE; } rubidium@7089: rubidium@7089: /** rubidium@7089: * Play the sound associated with leaving the station rubidium@7089: */ rubidium@7089: virtual void PlayLeaveStationSound() const {} maedhros@7269: maedhros@7269: /** maedhros@7269: * Whether this is the primary vehicle in the chain. maedhros@7269: */ maedhros@7269: virtual bool IsPrimaryVehicle() const { return false; } maedhros@7269: maedhros@7269: /** rubidium@7630: * Gets the sprite to show for the given direction rubidium@7630: * @param direction the direction the vehicle is facing rubidium@7630: * @return the sprite for the given vehicle in the given direction rubidium@7630: */ rubidium@7630: virtual int GetImage(Direction direction) const { return 0; } rubidium@7631: rubidium@7631: /** rubidium@7973: * Gets the speed in mph that can be sent into SetDParam for string processing. rubidium@7973: * @return the vehicle's speed rubidium@7973: */ rubidium@7973: virtual int GetDisplaySpeed() const { return 0; } rubidium@7973: rubidium@7973: /** rubidium@7980: * Gets the maximum speed in mph that can be sent into SetDParam for string processing. rubidium@7980: * @return the vehicle's maximum speed rubidium@7980: */ rubidium@7980: virtual int GetDisplayMaxSpeed() const { return 0; } rubidium@7980: rubidium@7980: /** rubidium@7984: * Gets the running cost of a vehicle rubidium@7984: * @return the vehicle's running cost rubidium@7984: */ rubidium@7984: virtual Money GetRunningCost() const { return 0; } rubidium@7984: rubidium@7984: /** rubidium@7986: * Check whether the vehicle is in the depot. rubidium@7986: * @return true if and only if the vehicle is in the depot. rubidium@7986: */ rubidium@7986: virtual bool IsInDepot() const { return false; } rubidium@7986: rubidium@7986: /** rubidium@7986: * Check whether the vehicle is in the depot *and* stopped. rubidium@7986: * @return true if and only if the vehicle is in the depot and stopped. rubidium@7986: */ rubidium@7986: virtual bool IsStoppedInDepot() const { return this->IsInDepot() && (this->vehstatus & VS_STOPPED) != 0; } rubidium@7986: rubidium@7986: /** rubidium@7631: * Calls the tick handler of the vehicle rubidium@7631: */ rubidium@7896: virtual void Tick() {}; rubidium@7883: rubidium@7984: /** glx@8963: * Calls the new day handler of the vehicle glx@8963: */ glx@8963: virtual void OnNewDay() {}; glx@8963: glx@8963: /** rubidium@7984: * Gets the running cost of a vehicle that can be sent into SetDParam for string processing. rubidium@7984: * @return the vehicle's running cost rubidium@7984: */ rubidium@7984: Money GetDisplayRunningCost() const { return (this->GetRunningCost() >> 8); } rubidium@7984: rubidium@7988: /** smatz@9110: * Gets the profit vehicle had this year. It can be sent into SetDParam for string processing. smatz@9110: * @return the vehicle's profit this year smatz@9110: */ smatz@9110: Money GetDisplayProfitThisYear() const { return (this->profit_this_year >> 8); } smatz@9110: smatz@9110: /** smatz@9110: * Gets the profit vehicle had last year. It can be sent into SetDParam for string processing. smatz@9110: * @return the vehicle's profit last year smatz@9110: */ smatz@9110: Money GetDisplayProfitLastYear() const { return (this->profit_last_year >> 8); } smatz@9110: smatz@9110: /** rubidium@7988: * Set the next vehicle of this vehicle. rubidium@7988: * @param next the next vehicle. NULL removes the next vehicle. rubidium@7988: */ rubidium@7993: void SetNext(Vehicle *next); rubidium@7988: rubidium@7988: /** rubidium@7988: * Get the next vehicle of this vehicle. rubidium@7988: * @note articulated parts are also counted as vehicles. rubidium@7988: * @return the next vehicle or NULL when there isn't a next vehicle. rubidium@7988: */ rubidium@7988: inline Vehicle *Next() const { return this->next; } rubidium@7993: rubidium@7993: /** rubidium@7993: * Get the previous vehicle of this vehicle. rubidium@7993: * @note articulated parts are also counted as vehicles. rubidium@7993: * @return the previous vehicle or NULL when there isn't a previous vehicle. rubidium@7993: */ rubidium@7993: inline Vehicle *Previous() const { return this->previous; } rubidium@7993: rubidium@7993: /** rubidium@7993: * Get the first vehicle of this vehicle chain. rubidium@7993: * @return the first vehicle of the chain. rubidium@7993: */ rubidium@7993: inline Vehicle *First() const { return this->first; } belugas@8965: belugas@8965: /** belugas@8965: * Check if we share our orders with another vehicle. belugas@8965: * This is done by checking the previous and next pointers in the shared chain. belugas@8965: * @return true if there are other vehicles sharing the same order belugas@8965: */ belugas@8965: inline bool IsOrderListShared() const { return this->next_shared != NULL || this->prev_shared != NULL; }; bjarni@9124: bjarni@9124: bool NeedsAutorenewing(const Player *p) const; rubidium@7048: }; rubidium@7048: rubidium@7048: /** rubidium@7048: * This class 'wraps' Vehicle; you do not actually instantiate this class. rubidium@7048: * You create a Vehicle using AllocateVehicle, so it is added to the pool rubidium@7048: * and you reinitialize that to a Train using: rubidium@7048: * v = new (v) Train(); rubidium@7048: * rubidium@7048: * As side-effect the vehicle type is set correctly. rubidium@7048: * rubidium@7048: * A special vehicle is one of the following: rubidium@7048: * - smoke rubidium@7048: * - electric sparks for trains rubidium@7048: * - explosions rubidium@7048: * - bulldozer (road works) rubidium@7048: * - bubbles (industry) rubidium@7048: */ rubidium@7048: struct SpecialVehicle : public Vehicle { rubidium@7048: /** Initializes the Vehicle to a special vehicle */ rubidium@7048: SpecialVehicle() { this->type = VEH_SPECIAL; } rubidium@7048: rubidium@7048: /** We want to 'destruct' the right class. */ rubidium@7048: virtual ~SpecialVehicle() {} rubidium@7048: rubidium@7059: const char *GetTypeString() const { return "special vehicle"; } rubidium@7054: void UpdateDeltaXY(Direction direction); rubidium@7631: void Tick(); rubidium@7048: }; rubidium@7048: rubidium@7048: /** rubidium@7048: * This class 'wraps' Vehicle; you do not actually instantiate this class. rubidium@7048: * You create a Vehicle using AllocateVehicle, so it is added to the pool rubidium@7048: * and you reinitialize that to a Train using: rubidium@7048: * v = new (v) Train(); rubidium@7048: * rubidium@7048: * As side-effect the vehicle type is set correctly. rubidium@7048: */ rubidium@7048: struct DisasterVehicle : public Vehicle { rubidium@7048: /** Initializes the Vehicle to a disaster vehicle */ rubidium@7048: DisasterVehicle() { this->type = VEH_DISASTER; } rubidium@7048: rubidium@7048: /** We want to 'destruct' the right class. */ rubidium@7048: virtual ~DisasterVehicle() {} rubidium@7048: rubidium@7059: const char *GetTypeString() const { return "disaster vehicle"; } rubidium@7054: void UpdateDeltaXY(Direction direction); rubidium@7631: void Tick(); rubidium@7048: }; rubidium@7048: rubidium@7048: /** rubidium@7048: * This class 'wraps' Vehicle; you do not actually instantiate this class. rubidium@7048: * You create a Vehicle using AllocateVehicle, so it is added to the pool rubidium@7048: * and you reinitialize that to a Train using: rubidium@7048: * v = new (v) Train(); rubidium@7048: * rubidium@7048: * As side-effect the vehicle type is set correctly. rubidium@7048: */ rubidium@7048: struct InvalidVehicle : public Vehicle { rubidium@7048: /** Initializes the Vehicle to a invalid vehicle */ rubidium@7048: InvalidVehicle() { this->type = VEH_INVALID; } rubidium@7048: rubidium@7048: /** We want to 'destruct' the right class. */ rubidium@7048: virtual ~InvalidVehicle() {} rubidium@7048: rubidium@7059: const char *GetTypeString() const { return "invalid vehicle"; } rubidium@7631: void Tick() {} truelight@0: }; truelight@0: truelight@0: #define BEGIN_ENUM_WAGONS(v) do { rubidium@7988: #define END_ENUM_WAGONS(v) } while ((v = v->Next()) != NULL); truelight@0: rubidium@6573: 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@6573: static inline uint GetNumVehicles() matthijs@5247: { truelight@4354: return GetVehiclePoolSize(); truelight@4354: } truelight@4354: rubidium@7883: #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@6919: * @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@7883: 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@6919: /** belugas@6919: * Returns the last order of a vehicle, or NULL if it doesn't exists belugas@6919: * @param v Vehicle to query belugas@6919: * @return last order of a vehicle, if available belugas@6919: */ 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@6919: /** Get the first vehicle of a shared-list, so we only have to walk forwards belugas@6919: * @param v Vehicle to query belugas@6919: * @return first vehicle of a shared-list belugas@6919: */ 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: peter1138@3105: /** rubidium@8640: * Returns the Trackdir on which the vehicle is currently located. rubidium@8640: * Works for trains and ships. rubidium@8640: * Currently works only sortof for road vehicles, since they have a fuzzy rubidium@8640: * concept of being "on" a trackdir. Dunno really what it returns for a road rubidium@8640: * vehicle that is halfway a tile, never really understood that part. For road rubidium@8640: * vehicles that are at the beginning or end of the tile, should just return rubidium@8640: * the diagonal trackdir on which they are driving. I _think_. rubidium@8640: * For other vehicles types, or vehicles with no clear trackdir (such as those rubidium@8640: * in depots), returns 0xFF. peter1138@3105: */ rubidium@8640: Trackdir GetVehicleTrackdir(const Vehicle* v); bjarni@6043: rubidium@8640: void CheckVehicle32Day(Vehicle *v); bjarni@6043: rubidium@8640: #endif /* VEHICLE_BASE_H */