src/vehicle.h
branchgamebalance
changeset 9895 7bd07f43b0e3
parent 6524 44e22a9b2c97
child 9908 0fa543611bbe
equal deleted inserted replaced
9894:70d78ac95d6c 9895:7bd07f43b0e3
    61 	RVSB_TRACKDIR_MASK           = 0x0F,                      ///< The mask used to extract track dirs
    61 	RVSB_TRACKDIR_MASK           = 0x0F,                      ///< The mask used to extract track dirs
    62 	RVSB_ROAD_STOP_TRACKDIR_MASK = 0x09                       ///< Only bits 0 and 3 are used to encode the trackdir for road stops
    62 	RVSB_ROAD_STOP_TRACKDIR_MASK = 0x09                       ///< Only bits 0 and 3 are used to encode the trackdir for road stops
    63 };
    63 };
    64 
    64 
    65 enum {
    65 enum {
    66 	VEH_Train,
    66 	VEH_TRAIN,
    67 	VEH_Road,
    67 	VEH_ROAD,
    68 	VEH_Ship,
    68 	VEH_SHIP,
    69 	VEH_Aircraft,
    69 	VEH_AIRCRAFT,
    70 	VEH_Special,
    70 	VEH_SPECIAL,
    71 	VEH_Disaster,
    71 	VEH_DISASTER,
    72 	VEH_Invalid = 0xFF,
    72 	VEH_INVALID = 0xFF,
    73 } ;
    73 } ;
    74 
    74 
    75 enum VehStatus {
    75 enum VehStatus {
    76 	VS_HIDDEN          = 0x01,
    76 	VS_HIDDEN          = 0x01,
    77 	VS_STOPPED         = 0x02,
    77 	VS_STOPPED         = 0x02,
    88 	VF_CARGO_UNLOADING,
    88 	VF_CARGO_UNLOADING,
    89 	VF_BUILT_AS_PROTOTYPE,
    89 	VF_BUILT_AS_PROTOTYPE,
    90 };
    90 };
    91 
    91 
    92 /* Effect vehicle types */
    92 /* Effect vehicle types */
    93 typedef enum EffectVehicle {
    93 enum EffectVehicle {
    94 	EV_CHIMNEY_SMOKE   = 0,
    94 	EV_CHIMNEY_SMOKE   = 0,
    95 	EV_STEAM_SMOKE     = 1,
    95 	EV_STEAM_SMOKE     = 1,
    96 	EV_DIESEL_SMOKE    = 2,
    96 	EV_DIESEL_SMOKE    = 2,
    97 	EV_ELECTRIC_SPARK  = 3,
    97 	EV_ELECTRIC_SPARK  = 3,
    98 	EV_SMOKE           = 4,
    98 	EV_SMOKE           = 4,
    99 	EV_EXPLOSION_LARGE = 5,
    99 	EV_EXPLOSION_LARGE = 5,
   100 	EV_BREAKDOWN_SMOKE = 6,
   100 	EV_BREAKDOWN_SMOKE = 6,
   101 	EV_EXPLOSION_SMALL = 7,
   101 	EV_EXPLOSION_SMALL = 7,
   102 	EV_BULLDOZER       = 8,
   102 	EV_BULLDOZER       = 8,
   103 	EV_BUBBLE          = 9
   103 	EV_BUBBLE          = 9
   104 } EffectVehicle;
   104 };
   105 
   105 
   106 typedef struct VehicleRail {
   106 struct VehicleRail {
   107 	uint16 last_speed; // NOSAVE: only used in UI
   107 	uint16 last_speed; // NOSAVE: only used in UI
   108 	uint16 crash_anim_pos;
   108 	uint16 crash_anim_pos;
   109 	uint16 days_since_order_progr;
   109 	uint16 days_since_order_progr;
   110 
   110 
   111 	// cached values, recalculated on load and each time a vehicle is added to/removed from the consist.
   111 	// cached values, recalculated on load and each time a vehicle is added to/removed from the consist.
   138 
   138 
   139 	byte flags;
   139 	byte flags;
   140 
   140 
   141 	// Link between the two ends of a multiheaded engine
   141 	// Link between the two ends of a multiheaded engine
   142 	Vehicle *other_multiheaded_part;
   142 	Vehicle *other_multiheaded_part;
   143 } VehicleRail;
   143 };
   144 
   144 
   145 enum {
   145 enum {
   146 	VRF_REVERSING         = 0,
   146 	VRF_REVERSING         = 0,
   147 
   147 
   148 	// used to calculate if train is going up or down
   148 	// used to calculate if train is going up or down
   160 
   160 
   161 	// used to mark that electric train engine is allowed to run on normal rail
   161 	// used to mark that electric train engine is allowed to run on normal rail
   162 	VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL = 6,
   162 	VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL = 6,
   163 };
   163 };
   164 
   164 
   165 typedef struct VehicleAir {
   165 struct VehicleAir {
   166 	uint16 crashed_counter;
   166 	uint16 crashed_counter;
   167 	byte pos;
   167 	byte pos;
   168 	byte previous_pos;
   168 	byte previous_pos;
   169 	StationID targetairport;
   169 	StationID targetairport;
   170 	byte state;
   170 	byte state;
   171 } VehicleAir;
   171 };
   172 
   172 
   173 typedef struct VehicleRoad {
   173 struct VehicleRoad {
   174 	byte state;             /// @see RoadVehicleStates
   174 	byte state;             /// @see RoadVehicleStates
   175 	byte frame;
   175 	byte frame;
   176 	uint16 blocked_ctr;
   176 	uint16 blocked_ctr;
   177 	byte overtaking;
   177 	byte overtaking;
   178 	byte overtaking_ctr;
   178 	byte overtaking_ctr;
   179 	uint16 crashed_ctr;
   179 	uint16 crashed_ctr;
   180 	byte reverse_ctr;
   180 	byte reverse_ctr;
   181 	struct RoadStop *slot;
   181 	struct RoadStop *slot;
   182 	byte slot_age;
   182 	byte slot_age;
   183 } VehicleRoad;
   183 };
   184 
   184 
   185 typedef struct VehicleSpecial {
   185 struct VehicleSpecial {
   186 	uint16 unk0;
   186 	uint16 unk0;
   187 	byte unk2;
   187 	byte unk2;
   188 } VehicleSpecial;
   188 };
   189 
   189 
   190 typedef struct VehicleDisaster {
   190 struct VehicleDisaster {
   191 	uint16 image_override;
   191 	uint16 image_override;
   192 	uint16 unk2;
   192 	uint16 unk2;
   193 } VehicleDisaster;
   193 };
   194 
   194 
   195 typedef struct VehicleShip {
   195 struct VehicleShip {
   196 	TrackBitsByte state;
   196 	TrackBitsByte state;
   197 } VehicleShip;
   197 };
   198 
   198 
   199 
   199 
   200 struct Vehicle {
   200 struct Vehicle {
   201 	byte type;               // type, ie roadven,train,ship,aircraft,special
   201 	byte type;               // type, ie roadven,train,ship,aircraft,special
   202 	byte subtype;            // subtype (Filled with values from EffectVehicles/TrainSubTypes/AircraftSubTypes)
   202 	byte subtype;            // subtype (Filled with values from EffectVehicles/TrainSubTypes/AircraftSubTypes)
   320 #define IS_CUSTOM_SECONDHEAD_SPRITE(x) (x == 0xFE)
   320 #define IS_CUSTOM_SECONDHEAD_SPRITE(x) (x == 0xFE)
   321 
   321 
   322 typedef void *VehicleFromPosProc(Vehicle *v, void *data);
   322 typedef void *VehicleFromPosProc(Vehicle *v, void *data);
   323 
   323 
   324 void VehicleServiceInDepot(Vehicle *v);
   324 void VehicleServiceInDepot(Vehicle *v);
   325 Vehicle *AllocateVehicle(void);
   325 Vehicle *AllocateVehicle();
   326 bool AllocateVehicles(Vehicle **vl, int num);
   326 bool AllocateVehicles(Vehicle **vl, int num);
   327 Vehicle *ForceAllocateVehicle(void);
   327 Vehicle *ForceAllocateVehicle();
   328 Vehicle *ForceAllocateSpecialVehicle(void);
   328 Vehicle *ForceAllocateSpecialVehicle();
   329 void VehiclePositionChanged(Vehicle *v);
   329 void VehiclePositionChanged(Vehicle *v);
   330 void AfterLoadVehicles(void);
   330 void AfterLoadVehicles();
   331 Vehicle *GetLastVehicleInChain(Vehicle *v);
   331 Vehicle *GetLastVehicleInChain(Vehicle *v);
   332 Vehicle *GetPrevVehicleInChain(const Vehicle *v);
   332 Vehicle *GetPrevVehicleInChain(const Vehicle *v);
   333 Vehicle *GetFirstVehicleInChain(const Vehicle *v);
   333 Vehicle *GetFirstVehicleInChain(const Vehicle *v);
   334 uint CountVehiclesInChain(const Vehicle* v);
   334 uint CountVehiclesInChain(const Vehicle* v);
   335 bool IsEngineCountable(const Vehicle *v);
   335 bool IsEngineCountable(const Vehicle *v);
   336 void DeleteVehicleChain(Vehicle *v);
   336 void DeleteVehicleChain(Vehicle *v);
   337 void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc);
   337 void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc);
   338 void CallVehicleTicks(void);
   338 void CallVehicleTicks();
   339 Vehicle *FindVehicleOnTileZ(TileIndex tile, byte z);
   339 Vehicle *FindVehicleOnTileZ(TileIndex tile, byte z);
   340 
   340 
   341 void InitializeTrains(void);
   341 void InitializeTrains();
   342 byte VehicleRandomBits(void);
   342 byte VehicleRandomBits();
   343 void ResetVehiclePosHash(void);
   343 void ResetVehiclePosHash();
   344 
   344 
   345 bool CanFillVehicle(Vehicle *v);
   345 bool CanFillVehicle(Vehicle *v);
   346 bool CanRefitTo(EngineID engine_type, CargoID cid_to);
   346 bool CanRefitTo(EngineID engine_type, CargoID cid_to);
   347 CargoID FindFirstRefittableCargo(EngineID engine_type);
   347 CargoID FindFirstRefittableCargo(EngineID engine_type);
   348 int32 GetRefitCost(EngineID engine_type);
   348 int32 GetRefitCost(EngineID engine_type);
   397 int32 SendAllVehiclesToDepot(byte type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id);
   397 int32 SendAllVehiclesToDepot(byte type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id);
   398 bool IsVehicleInDepot(const Vehicle *v);
   398 bool IsVehicleInDepot(const Vehicle *v);
   399 void VehicleEnterDepot(Vehicle *v);
   399 void VehicleEnterDepot(Vehicle *v);
   400 
   400 
   401 void InvalidateAutoreplaceWindow(EngineID e);
   401 void InvalidateAutoreplaceWindow(EngineID e);
       
   402 
       
   403 int32 MaybeReplaceVehicle(Vehicle *v, bool check, bool display_costs);
   402 
   404 
   403 /* Flags to add to p2 for goto depot commands */
   405 /* Flags to add to p2 for goto depot commands */
   404 /* Note: bits 8-10 are used for VLW flags */
   406 /* Note: bits 8-10 are used for VLW flags */
   405 enum {
   407 enum {
   406 	DEPOT_SERVICE       = (1 << 0),	// The vehicle will leave the depot right after arrival (serivce only)
   408 	DEPOT_SERVICE       = (1 << 0),	// The vehicle will leave the depot right after arrival (serivce only)
   407 	DEPOT_MASS_SEND     = (1 << 1), // Tells that it's a mass send to depot command (type in VLW flag)
   409 	DEPOT_MASS_SEND     = (1 << 1), // Tells that it's a mass send to depot command (type in VLW flag)
   408 	DEPOT_DONT_CANCEL   = (1 << 2), // Don't cancel current goto depot command if any
   410 	DEPOT_DONT_CANCEL   = (1 << 2), // Don't cancel current goto depot command if any
   409 	DEPOT_LOCATE_HANGAR = (1 << 3), // Find another airport if the target one lacks a hangar
   411 	DEPOT_LOCATE_HANGAR = (1 << 3), // Find another airport if the target one lacks a hangar
   410 };
   412 };
   411 
   413 
   412 typedef struct GetNewVehiclePosResult {
   414 struct GetNewVehiclePosResult {
   413 	int x,y;
   415 	int x,y;
   414 	TileIndex old_tile;
   416 	TileIndex old_tile;
   415 	TileIndex new_tile;
   417 	TileIndex new_tile;
   416 } GetNewVehiclePosResult;
   418 };
   417 
   419 
   418 /**
   420 /**
   419  * Returns the Trackdir on which the vehicle is currently located.
   421  * Returns the Trackdir on which the vehicle is currently located.
   420  * Works for trains and ships.
   422  * Works for trains and ships.
   421  * Currently works only sortof for road vehicles, since they have a fuzzy
   423  * Currently works only sortof for road vehicles, since they have a fuzzy
   435 #define BEGIN_ENUM_WAGONS(v) do {
   437 #define BEGIN_ENUM_WAGONS(v) do {
   436 #define END_ENUM_WAGONS(v) } while ( (v=v->next) != NULL);
   438 #define END_ENUM_WAGONS(v) } while ( (v=v->next) != NULL);
   437 
   439 
   438 DECLARE_OLD_POOL(Vehicle, Vehicle, 9, 125)
   440 DECLARE_OLD_POOL(Vehicle, Vehicle, 9, 125)
   439 
   441 
   440 static inline VehicleID GetMaxVehicleIndex(void)
   442 static inline VehicleID GetMaxVehicleIndex()
   441 {
   443 {
   442 	/* TODO - This isn't the real content of the function, but
   444 	/* TODO - This isn't the real content of the function, but
   443 	 *  with the new pool-system this will be replaced with one that
   445 	 *  with the new pool-system this will be replaced with one that
   444 	 *  _really_ returns the highest index. Now it just returns
   446 	 *  _really_ returns the highest index. Now it just returns
   445 	 *  the next safe value we are sure about everything is below.
   447 	 *  the next safe value we are sure about everything is below.
   446 	 */
   448 	 */
   447 	return GetVehiclePoolSize() - 1;
   449 	return GetVehiclePoolSize() - 1;
   448 }
   450 }
   449 
   451 
   450 static inline uint GetNumVehicles(void)
   452 static inline uint GetNumVehicles()
   451 {
   453 {
   452 	return GetVehiclePoolSize();
   454 	return GetVehiclePoolSize();
   453 }
   455 }
   454 
   456 
   455 /**
   457 /**
   456  * Check if a Vehicle really exists.
   458  * Check if a Vehicle really exists.
   457  */
   459  */
   458 static inline bool IsValidVehicle(const Vehicle *v)
   460 static inline bool IsValidVehicle(const Vehicle *v)
   459 {
   461 {
   460 	return v->type != VEH_Invalid;
   462 	return v->type != VEH_INVALID;
   461 }
   463 }
   462 
   464 
   463 void DestroyVehicle(Vehicle *v);
   465 void DestroyVehicle(Vehicle *v);
   464 
   466 
   465 static inline void DeleteVehicle(Vehicle *v)
   467 static inline void DeleteVehicle(Vehicle *v)
   466 {
   468 {
   467 	DestroyVehicle(v);
   469 	DestroyVehicle(v);
   468 	v->type = VEH_Invalid;
   470 	v->type = VEH_INVALID;
   469 }
   471 }
   470 
   472 
   471 static inline bool IsPlayerBuildableVehicleType(byte type)
   473 static inline bool IsPlayerBuildableVehicleType(byte type)
   472 {
   474 {
   473 	switch (type) {
   475 	switch (type) {
   474 		case VEH_Train:
   476 		case VEH_TRAIN:
   475 		case VEH_Road:
   477 		case VEH_ROAD:
   476 		case VEH_Ship:
   478 		case VEH_SHIP:
   477 		case VEH_Aircraft:
   479 		case VEH_AIRCRAFT:
   478 			return true;
   480 			return true;
   479 	}
   481 	}
   480 	return false;
   482 	return false;
   481 }
   483 }
   482 
   484