tron@2186: /* $Id$ */ tron@2186: 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" tron@577: matthijs@1763: enum { rubidium@4344: VEH_Invalid = 0x00, rubidium@4344: VEH_Train = 0x10, rubidium@4344: VEH_Road = 0x11, rubidium@4344: VEH_Ship = 0x12, matthijs@1752: VEH_Aircraft = 0x13, rubidium@4344: VEH_Special = 0x14, matthijs@1752: VEH_Disaster = 0x15, matthijs@1763: } ; 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: peter1138@5211: enum LoadStatus { peter1138@5211: LS_LOADING_FINISHED, peter1138@5211: LS_CARGO_UNLOADING, peter1138@5251: LS_CARGO_PAID_FOR, peter1138@5211: }; peter1138@5211: matthijs@1752: /* Effect vehicle types */ matthijs@1752: typedef 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 matthijs@1752: } EffectVehicle; matthijs@1752: truelight@0: typedef struct VehicleRail { rubidium@4434: uint16 last_speed; // NOSAVE: only used in UI truelight@0: uint16 crash_anim_pos; truelight@193: uint16 days_since_order_progr; truelight@193: hackykid@1905: // 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: hackykid@1905: // 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; truelight@0: truelight@0: // NOSAVE: for wagon override - id of the first engine in train truelight@0: // 0xffff == not in train Darkvater@1796: EngineID first_engine; truelight@0: truelight@0: byte track; truelight@0: byte force_proceed; truelight@0: byte railtype; celestar@3355: RailTypeMask compatible_railtypes; truelight@0: truelight@0: byte flags; hackykid@2008: bjarni@2676: // Link between the two ends of a multiheaded engine bjarni@2676: Vehicle *other_multiheaded_part; truelight@0: } VehicleRail; truelight@0: truelight@0: enum { rubidium@4344: VRF_REVERSING = 0, truelight@0: truelight@0: // used to calculate if train is going up or down rubidium@4344: VRF_GOINGUP = 1, rubidium@4344: VRF_GOINGDOWN = 2, hackykid@1908: hackykid@1908: // used to store if a wagon is powered or not rubidium@4344: VRF_POWEREDWAGON = 3, bjarni@3256: bjarni@3256: // used to reverse the visible direction of the vehicle bjarni@3256: VRF_REVERSE_DIRECTION = 4, KUDr@4870: KUDr@4870: // used to mark train as lost because PF can't find the route KUDr@4870: VRF_NO_PATH_TO_DESTINATION = 5, KUDr@5116: KUDr@5116: // 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: truelight@0: typedef struct VehicleAir { truelight@0: uint16 crashed_counter; truelight@0: byte pos; tron@4077: byte previous_pos; Darkvater@3347: StationID targetairport; truelight@0: byte state; truelight@0: } VehicleAir; truelight@0: truelight@0: typedef struct VehicleRoad { truelight@0: byte state; 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; truelight@0: } VehicleRoad; truelight@0: truelight@0: typedef struct VehicleSpecial { truelight@0: uint16 unk0; truelight@0: byte unk2; truelight@0: } VehicleSpecial; truelight@0: truelight@0: typedef struct VehicleDisaster { truelight@0: uint16 image_override; truelight@0: uint16 unk2; truelight@0: } VehicleDisaster; truelight@0: truelight@0: typedef struct VehicleShip { truelight@0: byte state; truelight@0: } VehicleShip; truelight@0: truelight@0: truelight@0: struct Vehicle { rubidium@4344: byte type; // type, ie roadven,train,ship,aircraft,special rubidium@4344: byte subtype; // subtype (Filled with values from EffectVehicles or TrainSubTypes) truelight@0: rubidium@4344: VehicleID index; // NOSAVE: Index in vehicle array truelight@0: rubidium@4344: Vehicle *next; // next rubidium@4344: Vehicle *first; // NOSAVE: pointer to the first vehicle in the chain rubidium@4344: 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 rubidium@4344: PlayerID 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@4344: byte 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: tron@445: // for randomized variational spritegroups tron@445: // bitmask used to resolve them; parts of it get reseeded when triggers tron@445: // 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: byte cargo_days; // how many days have the pieces been in transit rubidium@4344: StationID cargo_source; // source of cargo rubidium@4344: uint16 cargo_cap; // total capacity rubidium@4344: uint16 cargo_count; // how many pieces are used peter1138@3870: byte cargo_subtype; ///< Used for livery refits (NewGRF variations) 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: truelight@0: // Boundaries for the current position in the world and a next hash link. truelight@0: // 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; tron@2634: VehicleID next_hash; truelight@0: truelight@0: // 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; peter1138@5211: byte load_status; truelight@193: truelight@0: int32 profit_this_year; truelight@0: int32 profit_last_year; truelight@0: uint32 value; truelight@0: 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; 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); tron@1093: Vehicle *AllocateVehicle(void); bjarni@2601: bool AllocateVehicles(Vehicle **vl, int num); tron@1093: Vehicle *ForceAllocateVehicle(void); tron@1093: Vehicle *ForceAllocateSpecialVehicle(void); truelight@0: void VehiclePositionChanged(Vehicle *v); tron@1093: void AfterLoadVehicles(void); truelight@0: Vehicle *GetLastVehicleInChain(Vehicle *v); tron@1475: Vehicle *GetPrevVehicleInChain(const Vehicle *v); tron@1475: Vehicle *GetFirstVehicleInChain(const Vehicle *v); tron@2630: 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); tron@1093: void CallVehicleTicks(void); truelight@1605: Vehicle *FindVehicleOnTileZ(TileIndex tile, byte z); truelight@0: tron@1093: void InitializeTrains(void); peter1138@2804: byte VehicleRandomBits(void); Darkvater@5352: void ResetVehiclePosHash(void); truelight@0: truelight@0: bool CanFillVehicle(Vehicle *v); peter1138@2704: bool CanRefitTo(EngineID engine_type, CargoID cid_to); peter1138@3973: CargoID FindFirstRefittableCargo(EngineID engine_type); bjarni@4544: int32 GetRefitCost(EngineID engine_type); truelight@0: truelight@0: void ViewportAddVehicles(DrawPixelInfo *dpi); truelight@0: truelight@0: /* train_cmd.h */ tron@3157: int GetTrainImage(const Vehicle* v, Direction direction); tron@3157: int GetAircraftImage(const Vehicle* v, Direction direction); tron@3157: int GetRoadVehImage(const Vehicle* v, Direction direction); tron@3157: int GetShipImage(const Vehicle* v, Direction direction); 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); truelight@0: Vehicle *FindVehicleBetween(TileIndex from, TileIndex to, byte z); 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: tron@2561: void ShowAircraftViewWindow(const Vehicle* v); truelight@0: truelight@1282: UnitID GetFreeUnitNumber(byte type); truelight@0: peter1138@5211: int LoadUnloadVehicle(Vehicle *v, bool just_arrived); truelight@0: hackykid@1905: void TrainConsistChanged(Vehicle *v); celestar@3355: void TrainPowerChanged(Vehicle *v); Darkvater@1790: int32 GetTrainRunningCost(const Vehicle *v); truelight@0: tron@1472: int CheckTrainStoppedInDepot(const Vehicle *v); truelight@0: tron@593: bool VehicleNeedsService(const Vehicle *v); tron@593: bjarni@4681: uint GenerateVehicleSortList(const Vehicle*** sort_list, uint16 *length_of_array, byte type, PlayerID owner, StationID station, OrderID order, uint16 depot_airport_index, uint16 window_type); bjarni@4635: void BuildDepotVehicleList(byte type, TileIndex tile, Vehicle ***engine_list, uint16 *engine_list_length, uint16 *engine_count, Vehicle ***wagon_list, uint16 *wagon_list_length, uint16 *wagon_count); bjarni@4506: int32 SendAllVehiclesToDepot(byte type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id); bjarni@4734: bool IsVehicleInDepot(const Vehicle *v); bjarni@4725: void VehicleEnterDepot(Vehicle *v); bjarni@4506: 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 { bjarni@4506: 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: truelight@0: typedef struct GetNewVehiclePosResult { truelight@0: int x,y; tron@1977: TileIndex old_tile; tron@1977: TileIndex new_tile; truelight@0: } GetNewVehiclePosResult; 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 */ Darkvater@2436: bool GetNewVehiclePos(const Vehicle *v, GetNewVehiclePosResult *gp); tron@3157: Direction GetDirectionTowards(const Vehicle* v, int x, int y); truelight@0: truelight@0: #define BEGIN_ENUM_WAGONS(v) do { truelight@0: #define END_ENUM_WAGONS(v) } while ( (v=v->next) != NULL); truelight@0: matthijs@5216: DECLARE_OLD_POOL(Vehicle, Vehicle, 9, 125) truelight@1279: matthijs@5247: static inline VehicleID GetMaxVehicleIndex(void) 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: matthijs@5247: static inline uint GetNumVehicles(void) matthijs@5247: { truelight@4354: return GetVehiclePoolSize(); truelight@4354: } truelight@4354: truelight@1279: /** matthijs@1330: * Check if a Vehicle really exists. matthijs@1330: */ Darkvater@1770: static inline bool IsValidVehicle(const Vehicle *v) matthijs@1330: { matthijs@1330: return v->type != 0; matthijs@1330: } matthijs@1330: truelight@4404: void DestroyVehicle(Vehicle *v); truelight@4404: truelight@4404: static inline void DeleteVehicle(Vehicle *v) truelight@4404: { truelight@4404: DestroyVehicle(v); truelight@4404: v->type = 0; truelight@4404: } truelight@4404: tron@4972: #define FOR_ALL_VEHICLES_FROM(v, start) for (v = GetVehicle(start); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) if (IsValidVehicle(v)) 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) truelight@1279: * truelight@1279: * @return Returns true if the vehicle-id is in range truelight@1279: */ truelight@4352: static inline bool IsValidVehicleID(uint index) bjarni@1237: { truelight@4352: return index < GetVehiclePoolSize() && IsValidVehicle(GetVehicle(index)); 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: truelight@1024: /* Returns the last order of a vehicle, or NULL if it doesn't exists */ 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: truelight@1024: /* Get the first vehicle of a shared-list, so we only have to walk forwards */ 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: truelight@0: // NOSAVE: Return values from various commands. bjarni@2564: VARDEF VehicleID _new_vehicle_id; peter1138@3008: VARDEF uint16 _returned_refit_capacity; truelight@0: truelight@4384: enum { truelight@4384: INVALID_VEHICLE = 0xFFFF, truelight@4384: }; truelight@0: 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@3105: PalSpriteID 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@3105: PalSpriteID 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: Darkvater@4494: extern const uint32 _send_to_depot_proc_table[]; bjarni@4451: #define CMD_SEND_TO_DEPOT(x) _send_to_depot_proc_table[ x - VEH_Train] bjarni@4451: truelight@0: #endif /* VEHICLE_H */