tron@2186: /* $Id$ */ tron@2186: celestar@9908: /** @vehicle.h */ celestar@9908: 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: rubidium@6317: /** The returned bits of VehicleEnterTile. */ rubidium@6317: enum VehicleEnterTileStatus { rubidium@6317: VETS_ENTERED_STATION = 1, ///< The vehicle entered a station rubidium@6317: VETS_ENTERED_WORMHOLE = 2, ///< The vehicle either entered a bridge, tunnel or depot tile (this includes the last tile of the bridge/tunnel) rubidium@6317: VETS_CANNOT_ENTER = 3, ///< The vehicle cannot enter the tile rubidium@6317: rubidium@6317: /** rubidium@6317: * Shift the VehicleEnterTileStatus this many bits rubidium@6317: * to the right to get the station ID when rubidium@6317: * VETS_ENTERED_STATION is set rubidium@6317: */ rubidium@6317: VETS_STATION_ID_OFFSET = 8, rubidium@6317: rubidium@6317: /** Bit sets of the above specified bits */ rubidium@6317: VETSB_CONTINUE = 0, ///< The vehicle can continue normally rubidium@6317: VETSB_ENTERED_STATION = 1 << VETS_ENTERED_STATION, ///< The vehicle entered a station rubidium@6317: VETSB_ENTERED_WORMHOLE = 1 << VETS_ENTERED_WORMHOLE, ///< The vehicle either entered a bridge, tunnel or depot tile (this includes the last tile of the bridge/tunnel) rubidium@6317: VETSB_CANNOT_ENTER = 1 << VETS_CANNOT_ENTER, ///< The vehicle cannot enter the tile rubidium@6317: }; 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@1763: enum { celestar@9895: VEH_TRAIN, celestar@9895: VEH_ROAD, celestar@9895: VEH_SHIP, celestar@9895: VEH_AIRCRAFT, celestar@9895: VEH_SPECIAL, celestar@9895: VEH_DISASTER, celestar@9895: VEH_INVALID = 0xFF, 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: maedhros@6501: enum VehicleFlags { maedhros@6501: VF_LOADING_FINISHED, maedhros@6501: VF_CARGO_UNLOADING, maedhros@6502: VF_BUILT_AS_PROTOTYPE, peter1138@5211: }; peter1138@5211: matthijs@1752: /* Effect vehicle types */ celestar@9895: 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 celestar@9895: }; matthijs@1752: celestar@9895: 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: celestar@9908: /* 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: celestar@9908: /* 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: celestar@9908: /* NOSAVE: for wagon override - id of the first engine in train celestar@9908: * 0xffff == not in train */ Darkvater@1796: EngineID first_engine; truelight@0: rubidium@5838: TrackBitsByte track; truelight@0: byte force_proceed; rubidium@5838: RailTypeByte railtype; celestar@3355: RailTypeMask compatible_railtypes; truelight@0: truelight@0: byte flags; hackykid@2008: celestar@9908: /* Link between the two ends of a multiheaded engine */ bjarni@2676: Vehicle *other_multiheaded_part; celestar@9895: }; truelight@0: truelight@0: enum { rubidium@4344: VRF_REVERSING = 0, truelight@0: celestar@9908: /* used to calculate if train is going up or down */ rubidium@4344: VRF_GOINGUP = 1, rubidium@4344: VRF_GOINGDOWN = 2, hackykid@1908: celestar@9908: /* used to store if a wagon is powered or not */ rubidium@4344: VRF_POWEREDWAGON = 3, bjarni@3256: celestar@9908: /* used to reverse the visible direction of the vehicle */ bjarni@3256: VRF_REVERSE_DIRECTION = 4, KUDr@4870: celestar@9908: /* used to mark train as lost because PF can't find the route */ KUDr@4870: VRF_NO_PATH_TO_DESTINATION = 5, KUDr@5116: celestar@9908: /* 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: celestar@9895: struct VehicleAir { truelight@0: uint16 crashed_counter; truelight@0: byte pos; tron@4077: byte previous_pos; Darkvater@3347: StationID targetairport; truelight@0: byte state; celestar@9895: }; truelight@0: celestar@9895: struct VehicleRoad { celestar@9908: 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; celestar@9895: }; truelight@0: celestar@9895: struct VehicleSpecial { truelight@0: uint16 unk0; truelight@0: byte unk2; celestar@9895: }; truelight@0: celestar@9895: struct VehicleDisaster { truelight@0: uint16 image_override; truelight@0: uint16 unk2; celestar@9895: }; truelight@0: celestar@9895: struct VehicleShip { rubidium@5838: TrackBitsByte state; celestar@9895: }; truelight@0: truelight@0: truelight@0: struct Vehicle { rubidium@4344: byte type; // type, ie roadven,train,ship,aircraft,special Darkvater@6105: byte subtype; // subtype (Filled with values from EffectVehicles/TrainSubTypes/AircraftSubTypes) 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 celestar@9908: 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: celestar@9908: /* for randomized variational spritegroups celestar@9908: * bitmask used to resolve them; parts of it get reseeded when triggers celestar@9908: * 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 celestar@5934: TileIndex cargo_source_xy; //< stores the Tile where the source station is located, in case it is removed 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: celestar@9908: /* Boundaries for the current position in the world and a next hash link. celestar@9908: * 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; truelight@0: celestar@9908: /* 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@6139: uint16 cargo_paid_for; // How much of the cargo currently on board has been paid for. maedhros@6501: byte vehicle_flags; // Used for gradual loading and other miscellaneous things (@see VehicleFlags enum) truelight@193: truelight@0: int32 profit_this_year; truelight@0: int32 profit_last_year; richk@6524: int32 cargo_feeder_share; ///< value of feeder pickup to be paid for on delivery of cargo richk@6524: TileIndex cargo_loaded_at_xy; ///< tile index where feeder cargo was loaded 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; KUDr@5902: KUDr@5902: void BeginLoading(); KUDr@5902: void LeaveStation(); 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); celestar@9895: Vehicle *AllocateVehicle(); bjarni@2601: bool AllocateVehicles(Vehicle **vl, int num); celestar@9895: Vehicle *ForceAllocateVehicle(); celestar@9895: Vehicle *ForceAllocateSpecialVehicle(); truelight@0: void VehiclePositionChanged(Vehicle *v); celestar@9895: void AfterLoadVehicles(); 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); celestar@9895: void CallVehicleTicks(); truelight@1605: Vehicle *FindVehicleOnTileZ(TileIndex tile, byte z); truelight@0: celestar@9895: void InitializeTrains(); celestar@9895: byte VehicleRandomBits(); celestar@9895: void ResetVehiclePosHash(); 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); Darkvater@6117: SpriteID GetRotorImage(const Vehicle *v); truelight@0: tron@1359: Vehicle *CreateEffectVehicle(int x, int y, int z, EffectVehicle type); tron@1359: Vehicle *CreateEffectVehicleAbove(int x, int y, int z, EffectVehicle type); tron@1359: Vehicle *CreateEffectVehicleRel(const Vehicle *v, int x, int y, int z, EffectVehicle type); truelight@0: tron@1977: uint32 VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y); truelight@0: tron@3881: StringID VehicleInTheWayErrMsg(const Vehicle* v); rubidium@6191: Vehicle *FindVehicleBetween(TileIndex from, TileIndex to, byte z, bool without_crashed = false); truelight@0: tron@3172: bool UpdateSignalsOnSegment(TileIndex tile, DiagDirection direction); tron@1977: void SetSignalsOnBothDir(TileIndex tile, byte track); truelight@0: tron@2116: Vehicle *CheckClickOnVehicle(const ViewPort *vp, int x, int y); truelight@0: truelight@0: void DecreaseVehicleValue(Vehicle *v); truelight@0: void CheckVehicleBreakdown(Vehicle *v); truelight@0: void AgeVehicle(Vehicle *v); bjarni@2574: void VehicleEnteredDepotThisTick(Vehicle *v); truelight@0: truelight@0: void BeginVehicleMove(Vehicle *v); truelight@0: void EndVehicleMove(Vehicle *v); truelight@0: 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@5999: uint GenerateVehicleSortList(const Vehicle*** sort_list, uint16 *length_of_array, byte type, PlayerID owner, uint32 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@6195: void InvalidateAutoreplaceWindow(EngineID e); bjarni@6195: celestar@9895: int32 MaybeReplaceVehicle(Vehicle *v, bool check, bool display_costs); celestar@9895: 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: celestar@9895: struct GetNewVehiclePosResult { truelight@0: int x,y; tron@1977: TileIndex old_tile; tron@1977: TileIndex new_tile; celestar@9895: }; truelight@0: matthijs@1752: /** matthijs@1752: * Returns the Trackdir on which the vehicle is currently located. matthijs@1752: * Works for trains and ships. matthijs@1752: * Currently works only sortof for road vehicles, since they have a fuzzy matthijs@1752: * concept of being "on" a trackdir. Dunno really what it returns for a road matthijs@1752: * vehicle that is halfway a tile, never really understood that part. For road matthijs@1752: * vehicles that are at the beginning or end of the tile, should just return matthijs@1752: * the diagonal trackdir on which they are driving. I _think_. matthijs@1752: * For other vehicles types, or vehicles with no clear trackdir (such as those matthijs@1752: * in depots), returns 0xFF. matthijs@1752: */ matthijs@1944: Trackdir GetVehicleTrackdir(const Vehicle* v); matthijs@1752: truelight@0: /* returns true if staying in the same tile */ tron@6479: GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v); 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: celestar@9895: 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: celestar@9895: static inline uint GetNumVehicles() 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: { celestar@9895: return v->type != VEH_INVALID; 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); celestar@9895: v->type = VEH_INVALID; truelight@4404: } truelight@4404: bjarni@6026: static inline bool IsPlayerBuildableVehicleType(byte type) bjarni@5997: { bjarni@6026: switch (type) { celestar@9895: case VEH_TRAIN: celestar@9895: case VEH_ROAD: celestar@9895: case VEH_SHIP: celestar@9895: case VEH_AIRCRAFT: bjarni@5997: return true; bjarni@5997: } bjarni@5997: return false; bjarni@5997: } bjarni@5997: bjarni@6026: static inline bool IsPlayerBuildableVehicleType(const Vehicle *v) bjarni@6026: { bjarni@6026: return IsPlayerBuildableVehicleType(v->type); bjarni@6026: } bjarni@6026: 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) celestar@9908: * @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: { 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: celestar@9908: /** celestar@9908: * Returns the last order of a vehicle, or NULL if it doesn't exists celestar@9908: * @param v Vehicle to query celestar@9908: * @return last order of a vehicle, if available celestar@9908: */ 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: celestar@9908: /** Get the first vehicle of a shared-list, so we only have to walk forwards celestar@9908: * @param v Vehicle to query celestar@9908: * @return first vehicle of a shared-list celestar@9908: */ 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: celestar@9908: /* NOSAVE: Return values from various commands. */ bjarni@2564: VARDEF VehicleID _new_vehicle_id; peter1138@3008: VARDEF uint16 _returned_refit_capacity; truelight@0: rubidium@5838: static const VehicleID INVALID_VEHICLE = 0xFFFF; 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@5919: SpriteID GetEnginePalette(EngineID engine_type, PlayerID player); peter1138@3040: peter1138@3105: /** peter1138@3105: * Get the colour map for a vehicle. peter1138@3105: * @param v Vehicle to get colour map for peter1138@3105: * @return A ready-to-use palette modifier peter1138@3105: */ peter1138@5919: SpriteID GetVehiclePalette(const Vehicle *v); peter1138@3040: darkvater@755: /* A lot of code calls for the invalidation of the status bar, which is widget 5. darkvater@755: * Best is to have a virtual value for it when it needs to change again */ darkvater@755: #define STATUS_BAR 5 darkvater@755: bjarni@6043: extern const uint32 _veh_build_proc_table[]; bjarni@6043: extern const uint32 _veh_sell_proc_table[]; bjarni@6043: extern const uint32 _veh_refit_proc_table[]; Darkvater@4494: extern const uint32 _send_to_depot_proc_table[]; bjarni@6043: bjarni@6043: /* Functions to find the right command for certain vehicle type */ bjarni@6043: static inline uint32 GetCmdBuildVeh(byte type) bjarni@6043: { bjarni@6206: return _veh_build_proc_table[type]; bjarni@6043: } bjarni@6043: bjarni@6043: static inline uint32 GetCmdBuildVeh(const Vehicle *v) bjarni@6043: { bjarni@6043: return GetCmdBuildVeh(v->type); bjarni@6043: } bjarni@6043: bjarni@6043: static inline uint32 GetCmdSellVeh(byte type) bjarni@6043: { bjarni@6206: return _veh_sell_proc_table[type]; bjarni@6043: } bjarni@6043: bjarni@6043: static inline uint32 GetCmdSellVeh(const Vehicle *v) bjarni@6043: { bjarni@6043: return GetCmdSellVeh(v->type); bjarni@6043: } bjarni@6043: bjarni@6043: static inline uint32 GetCmdRefitVeh(byte type) bjarni@6043: { bjarni@6206: return _veh_refit_proc_table[type]; bjarni@6043: } bjarni@6043: bjarni@6043: static inline uint32 GetCmdRefitVeh(const Vehicle *v) bjarni@6043: { bjarni@6043: return GetCmdRefitVeh(v->type); bjarni@6043: } bjarni@6043: bjarni@6043: static inline uint32 GetCmdSendToDepot(byte type) bjarni@6043: { bjarni@6206: return _send_to_depot_proc_table[type]; bjarni@6043: } bjarni@6043: bjarni@6043: static inline uint32 GetCmdSendToDepot(const Vehicle *v) bjarni@6043: { bjarni@6043: return GetCmdSendToDepot(v->type); bjarni@6043: } bjarni@4451: truelight@0: #endif /* VEHICLE_H */