Changeset - r12030:bf346482c342
[Not reviewed]
master
0 16 0
smatz - 15 years ago 2009-05-26 22:45:48
smatz@openttd.org
(svn r16442) -Codechange: use new Vehicle accessors at more places
16 files changed with 129 insertions and 155 deletions:
0 comments (0 inline, 0 general)
src/aircraft_cmd.cpp
Show inline comments
 
@@ -437,16 +437,15 @@ CommandCost CmdBuildAircraft(TileIndex t
 
 * @param p1 vehicle ID to be sold
 
 * @param p2 unused
 
 * @return result of operation.  Error or sold value
 
 */
 
CommandCost CmdSellAircraft(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	Vehicle *v = Vehicle::GetIfValid(p1);
 
	if (v == NULL) return CMD_ERROR;
 
	Aircraft *v = Aircraft::GetIfValid(p1);
 

	
 
	if (v->type != VEH_AIRCRAFT || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (!v->IsStoppedInDepot()) return_cmd_error(STR_ERROR_AIRCRAFT_MUST_BE_STOPPED);
 

	
 
	if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
 

	
 
	CommandCost ret(EXPENSES_NEW_VEHICLES, -v->value);
 

	
 
@@ -490,17 +489,15 @@ CommandCost CmdSendAircraftToHangar(Tile
 
	if (p2 & DEPOT_MASS_SEND) {
 
		/* Mass goto depot requested */
 
		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
 
		return SendAllVehiclesToDepot(VEH_AIRCRAFT, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1);
 
	}
 

	
 
	Vehicle *v = Vehicle::GetIfValid(p1);
 
	Aircraft *v = Aircraft::GetIfValid(p1);
 
	if (v == NULL) return CMD_ERROR;
 

	
 
	if (v->type != VEH_AIRCRAFT) return CMD_ERROR;
 

	
 
	return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
 
}
 

	
 

	
 
/** Refits an aircraft to the specified cargo type.
 
 * @param tile unused
 
@@ -513,16 +510,14 @@ CommandCost CmdSendAircraftToHangar(Tile
 
 * @return cost of refit or error
 
 */
 
CommandCost CmdRefitAircraft(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	byte new_subtype = GB(p2, 8, 8);
 

	
 
	Vehicle *v = Vehicle::GetIfValid(p1);
 
	if (v == NULL) return CMD_ERROR;
 

	
 
	if (v->type != VEH_AIRCRAFT || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	Aircraft *v = Aircraft::GetIfValid(p1);
 
	if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (!v->IsStoppedInDepot()) return_cmd_error(STR_ERROR_AIRCRAFT_MUST_BE_STOPPED);
 
	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_REFIT_DESTROYED_VEHICLE);
 

	
 
	/* Check cargo */
 
	CargoID new_cid = GB(p2, 0, 8);
 
	if (new_cid >= NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR;
src/disaster_cmd.cpp
Show inline comments
 
@@ -297,15 +297,15 @@ static bool DisasterTick_Ufo(DisasterVeh
 
		if (++v->age < 6) {
 
			v->dest_tile = RandomTile();
 
			return true;
 
		}
 
		v->current_order.SetDestination(1);
 

	
 
		Vehicle *u;
 
		FOR_ALL_VEHICLES(u) {
 
			if (u->type == VEH_ROAD && IsRoadVehFront(u)) {
 
		RoadVehicle *u;
 
		FOR_ALL_ROADVEHICLES(u) {
 
			if (IsRoadVehFront(u)) {
 
				v->dest_tile = u->index;
 
				v->age = 0;
 
				return true;
 
			}
 
		}
 

	
 
@@ -935,16 +935,16 @@ void StartupDisasters()
 
/** Marks all disasters targeting this industry in such a way
 
 * they won't call Industry::Get(v->dest_tile) on invalid industry anymore.
 
 * @param i deleted industry
 
 */
 
void ReleaseDisastersTargetingIndustry(IndustryID i)
 
{
 
	Vehicle *v;
 
	FOR_ALL_VEHICLES(v) {
 
	DisasterVehicle *v;
 
	FOR_ALL_DISASTERVEHICLES(v) {
 
		/* primary disaster vehicles that have chosen target */
 
		if (v->type == VEH_DISASTER && (v->subtype == ST_AIRPLANE || v->subtype == ST_HELICOPTER)) {
 
		if (v->subtype == ST_AIRPLANE || v->subtype == ST_HELICOPTER) {
 
			/* if it has chosen target, and it is this industry (yes, dest_tile is IndustryID here), set order to "leaving map peacefully" */
 
			if (v->current_order.GetDestination() > 0 && v->dest_tile == i) v->current_order.SetDestination(3);
 
		}
 
	}
 
}
 

	
src/effectvehicle_base.h
Show inline comments
 
@@ -34,7 +34,9 @@ struct EffectVehicle : public Specialize
 

	
 
	const char *GetTypeString() const { return "special vehicle"; }
 
	void UpdateDeltaXY(Direction direction);
 
	bool Tick();
 
};
 

	
 
#define FOR_ALL_EFFECTVEHICLES(var) FOR_ALL_VEHICLES_OF_TYPE(EffectVehicle, var)
 

	
 
#endif /* EFFECTVEHICLE_BASE_H */
src/road_cmd.cpp
Show inline comments
 
@@ -34,17 +34,15 @@
 
/**
 
 * Verify whether a road vehicle is available.
 
 * @return \c true if at least one road vehicle is available, \c false if not
 
 */
 
bool RoadVehiclesAreBuilt()
 
{
 
	const Vehicle *v;
 
	const RoadVehicle *rv;
 
	FOR_ALL_ROADVEHICLES(rv) return true;
 

	
 
	FOR_ALL_VEHICLES(v) {
 
		if (v->type == VEH_ROAD) return true;
 
	}
 
	return false;
 
}
 

	
 
#define M(x) (1 << (x))
 
/* Level crossings may only be built on these slopes */
 
static const uint32 VALID_LEVEL_CROSSING_SLOPES = (M(SLOPE_SEN) | M(SLOPE_ENW) | M(SLOPE_NWS) | M(SLOPE_NS) | M(SLOPE_WSE) | M(SLOPE_EW) | M(SLOPE_FLAT));
 
@@ -271,15 +269,15 @@ static CommandCost RemoveRoad(TileIndex 
 
			}
 

	
 
			if (flags & DC_EXEC) {
 
				if (HasRoadWorks(tile)) {
 
					/* flooding tile with road works, don't forget to remove the effect vehicle too */
 
					assert(_current_company == OWNER_WATER);
 
					Vehicle *v;
 
					FOR_ALL_VEHICLES(v) {
 
						if (v->type == VEH_EFFECT && TileVirtXY(v->x_pos, v->y_pos) == tile) {
 
					EffectVehicle *v;
 
					FOR_ALL_EFFECTVEHICLES(v) {
 
						if (TileVirtXY(v->x_pos, v->y_pos) == tile) {
 
							delete v;
 
						}
 
					}
 
				}
 
				if (present == ROAD_NONE) {
 
					RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt));
 
@@ -1537,19 +1535,12 @@ static const byte _roadveh_enter_depot_d
 
	TRACKDIR_X_SW, TRACKDIR_Y_NW, TRACKDIR_X_NE, TRACKDIR_Y_SE
 
};
 

	
 
static VehicleEnterTileStatus VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y)
 
{
 
	switch (GetRoadTileType(tile)) {
 
		case ROAD_TILE_CROSSING:
 
			if (v->type == VEH_TRAIN) {
 
				/* it should be barred */
 
				assert(IsCrossingBarred(tile));
 
			}
 
			break;
 

	
 
		case ROAD_TILE_DEPOT: {
 
			if (v->type != VEH_ROAD) break;
 

	
 
			RoadVehicle *rv = (RoadVehicle *)v;
 
			if (rv->frame == RVC_DEPOT_STOP_FRAME &&
 
					_roadveh_enter_depot_dir[GetRoadDepotDirection(tile)] == rv->state) {
src/roadveh_cmd.cpp
Show inline comments
 
@@ -309,14 +309,14 @@ bool RoadVehicle::IsStoppedInDepot() con
 
 * @param flags operation to perform
 
 * @param p1 vehicle ID to be sold
 
 * @param p2 unused
 
 */
 
CommandCost CmdSellRoadVeh(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	Vehicle *v = Vehicle::GetIfValid(p1);
 
	if (v == NULL || v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	RoadVehicle *v = RoadVehicle::GetIfValid(p1);
 
	if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
 

	
 
	if (!v->IsStoppedInDepot()) {
 
		return_cmd_error(STR_ERROR_ROAD_MUST_BE_STOPPED_INSIDE_DEPOT);
 
	}
 
@@ -415,14 +415,14 @@ CommandCost CmdSendRoadVehToDepot(TileIn
 
	if (p2 & DEPOT_MASS_SEND) {
 
		/* Mass goto depot requested */
 
		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
 
		return SendAllVehiclesToDepot(VEH_ROAD, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1);
 
	}
 

	
 
	Vehicle *v = Vehicle::GetIfValid(p1);
 
	if (v == NULL || v->type != VEH_ROAD) return CMD_ERROR;
 
	RoadVehicle *v = RoadVehicle::GetIfValid(p1);
 
	if (v == NULL) return CMD_ERROR;
 

	
 
	return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
 
}
 

	
 
/** Turn a roadvehicle around.
 
 * @param tile unused
 
@@ -1988,15 +1988,15 @@ CommandCost CmdRefitRoadVeh(TileIndex ti
 
	CargoID new_cid = GB(p2, 0, 8);
 
	byte new_subtype = GB(p2, 8, 8);
 
	bool only_this = HasBit(p2, 16);
 
	uint16 capacity = CALLBACK_FAILED;
 
	uint total_capacity = 0;
 

	
 
	Vehicle *v = Vehicle::GetIfValid(p1);
 
	RoadVehicle *v = RoadVehicle::GetIfValid(p1);
 

	
 
	if (v == NULL || v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (!v->IsStoppedInDepot()) return_cmd_error(STR_ERROR_ROAD_MUST_BE_STOPPED_INSIDE_DEPOT);
 
	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_REFIT_DESTROYED_VEHICLE);
 

	
 
	if (new_cid >= NUM_CARGO) return CMD_ERROR;
 

	
 
	for (; v != NULL; v = (only_this ? NULL : v->Next())) {
src/saveload/afterload.cpp
Show inline comments
 
@@ -311,15 +311,15 @@ static void CDECL HandleSavegameLoadCras
 
 */
 
static void FixOwnerOfRailTrack(TileIndex t)
 
{
 
	assert(!Company::IsValidID(GetTileOwner(t)) && (IsLevelCrossingTile(t) || IsPlainRailTile(t)));
 

	
 
	/* remove leftover rail piece from crossing (from very old savegames) */
 
	Vehicle *v = NULL, *w;
 
	FOR_ALL_VEHICLES(w) {
 
		if (w->type == VEH_TRAIN && w->tile == t) {
 
	Train *v = NULL, *w;
 
	FOR_ALL_TRAINS(w) {
 
		if (w->tile == t) {
 
			v = w;
 
			break;
 
		}
 
	}
 

	
 
	if (v != NULL) {
 
@@ -1283,16 +1283,16 @@ bool AfterLoadGame()
 
		FOR_ALL_STATIONS(st) {
 
			if (st->IsBuoy() && IsTileOwner(st->xy, OWNER_NONE) && TileHeight(st->xy) == 0) SetTileOwner(st->xy, OWNER_WATER);
 
		}
 
	}
 

	
 
	if (CheckSavegameVersion(50)) {
 
		Vehicle *v;
 
		Aircraft *v;
 
		/* Aircraft units changed from 8 mph to 1 km/h */
 
		FOR_ALL_VEHICLES(v) {
 
			if (v->type == VEH_AIRCRAFT && v->subtype <= AIR_AIRCRAFT) {
 
		FOR_ALL_AIRCRAFT(v) {
 
			if (v->subtype <= AIR_AIRCRAFT) {
 
				const AircraftVehicleInfo *avi = AircraftVehInfo(v->engine_type);
 
				v->cur_speed *= 129;
 
				v->cur_speed /= 10;
 
				v->max_speed = avi->max_speed;
 
				v->acceleration = avi->acceleration;
 
			}
 
@@ -1621,16 +1621,15 @@ bool AfterLoadGame()
 
		}
 
	}
 

	
 
	if (CheckSavegameVersion(62)) {
 
		/* Remove all trams from savegames without tram support.
 
		 * There would be trams without tram track under causing crashes sooner or later. */
 
		Vehicle *v;
 
		FOR_ALL_VEHICLES(v) {
 
			if (v->type == VEH_ROAD && v->First() == v &&
 
					HasBit(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM)) {
 
		RoadVehicle *v;
 
		FOR_ALL_ROADVEHICLES(v) {
 
			if (v->First() == v && HasBit(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM)) {
 
				if (_switch_mode_errorstr == INVALID_STRING_ID || _switch_mode_errorstr == STR_NEWGRF_COMPATIBLE_LOAD_WARNING) {
 
					_switch_mode_errorstr = STR_LOADGAME_REMOVED_TRAMS;
 
				}
 
				delete v;
 
			}
 
		}
 
@@ -1732,17 +1731,17 @@ bool AfterLoadGame()
 
		FOR_ALL_STATIONS(st) {
 
			st->indtype = IT_INVALID;
 
		}
 
	}
 

	
 
	if (CheckSavegameVersion(104)) {
 
		Vehicle *v;
 
		FOR_ALL_VEHICLES(v) {
 
		Aircraft *a;
 
		FOR_ALL_AIRCRAFT(a) {
 
			/* Set engine_type of shadow and rotor */
 
			if (v->type == VEH_AIRCRAFT && !IsNormalAircraft(v)) {
 
				v->engine_type = v->First()->engine_type;
 
			if (!IsNormalAircraft(a)) {
 
				a->engine_type = a->First()->engine_type;
 
			}
 
		}
 

	
 
		/* More companies ... */
 
		Company *c;
 
		FOR_ALL_COMPANIES(c) {
src/saveload/vehicle_sl.cpp
Show inline comments
 
@@ -97,61 +97,55 @@ void ConnectMultiheadedTrains()
 
/**
 
 *  Converts all trains to the new subtype format introduced in savegame 16.2
 
 *  It also links multiheaded engines or make them forget they are multiheaded if no suitable partner is found
 
 */
 
void ConvertOldMultiheadToNew()
 
{
 
	Vehicle *v;
 
	FOR_ALL_VEHICLES(v) {
 
		if (v->type == VEH_TRAIN) {
 
			SetBit(v->subtype, 7); // indicates that it's the old format and needs to be converted in the next loop
 
		}
 
	}
 
	Train *t;
 
	FOR_ALL_TRAINS(t) SetBit(t->subtype, 7); // indicates that it's the old format and needs to be converted in the next loop
 

	
 
	FOR_ALL_TRAINS(t) {
 
		if (HasBit(t->subtype, 7) && ((t->subtype & ~0x80) == 0 || (t->subtype & ~0x80) == 4)) {
 
			for (Vehicle *u = t; u != NULL; u = u->Next()) {
 
				const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
 

	
 
	FOR_ALL_VEHICLES(v) {
 
		if (v->type == VEH_TRAIN) {
 
			if (HasBit(v->subtype, 7) && ((v->subtype & ~0x80) == 0 || (v->subtype & ~0x80) == 4)) {
 
				for (Vehicle *u = v; u != NULL; u = u->Next()) {
 
					const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
 
				ClrBit(u->subtype, 7);
 
				switch (u->subtype) {
 
					case 0: // TS_Front_Engine
 
						if (rvi->railveh_type == RAILVEH_MULTIHEAD) SetMultiheaded(u);
 
						SetFrontEngine(u);
 
						SetTrainEngine(u);
 
						break;
 

	
 
					ClrBit(u->subtype, 7);
 
					switch (u->subtype) {
 
						case 0: // TS_Front_Engine
 
							if (rvi->railveh_type == RAILVEH_MULTIHEAD) SetMultiheaded(u);
 
							SetFrontEngine(u);
 
							SetTrainEngine(u);
 
							break;
 
					case 1: // TS_Artic_Part
 
						u->subtype = 0;
 
						SetArticulatedPart(u);
 
						break;
 

	
 
						case 1: // TS_Artic_Part
 
							u->subtype = 0;
 
							SetArticulatedPart(u);
 
					case 2: // TS_Not_First
 
						u->subtype = 0;
 
						if (rvi->railveh_type == RAILVEH_WAGON) {
 
							/* normal wagon */
 
							SetTrainWagon(u);
 
							break;
 
						}
 
						if (rvi->railveh_type == RAILVEH_MULTIHEAD && rvi->image_index == u->spritenum - 1) {
 
							/* rear end of a multiheaded engine */
 
							SetMultiheaded(u);
 
							break;
 
						}
 
						if (rvi->railveh_type == RAILVEH_MULTIHEAD) SetMultiheaded(u);
 
						SetTrainEngine(u);
 
						break;
 

	
 
						case 2: // TS_Not_First
 
							u->subtype = 0;
 
							if (rvi->railveh_type == RAILVEH_WAGON) {
 
								/* normal wagon */
 
								SetTrainWagon(u);
 
								break;
 
							}
 
							if (rvi->railveh_type == RAILVEH_MULTIHEAD && rvi->image_index == u->spritenum - 1) {
 
								/* rear end of a multiheaded engine */
 
								SetMultiheaded(u);
 
								break;
 
							}
 
							if (rvi->railveh_type == RAILVEH_MULTIHEAD) SetMultiheaded(u);
 
							SetTrainEngine(u);
 
							break;
 

	
 
						case 4: // TS_Free_Car
 
							u->subtype = 0;
 
							SetTrainWagon(u);
 
							SetFreeWagon(u);
 
							break;
 
						default: NOT_REACHED();
 
					}
 
					case 4: // TS_Free_Car
 
						u->subtype = 0;
 
						SetTrainWagon(u);
 
						SetFreeWagon(u);
 
						break;
 
					default: NOT_REACHED();
 
				}
 
			}
 
		}
 
	}
 
}
 

	
src/settings.cpp
Show inline comments
 
@@ -54,12 +54,13 @@
 
#include "settings_func.h"
 
#include "ini_type.h"
 
#include "ai/ai.hpp"
 
#include "ai/ai_config.hpp"
 
#include "newgrf.h"
 
#include "engine_base.h"
 
#include "ship.h"
 

	
 
#include "void_map.h"
 
#include "station_base.h"
 

	
 
#include "table/strings.h"
 
#include "table/settings.h"
 
@@ -875,15 +876,15 @@ static int32 CheckNoiseToleranceLevel(co
 
}
 

	
 
static bool CheckFreeformEdges(int32 p1)
 
{
 
	if (_game_mode == GM_MENU) return true;
 
	if (p1 != 0) {
 
		Vehicle *v;
 
		FOR_ALL_VEHICLES(v) {
 
			if (v->type == VEH_SHIP && (TileX(v->tile) == 0 || TileY(v->tile) == 0)) {
 
		Ship *s;
 
		FOR_ALL_SHIPS(s) {
 
			if (TileX(s->tile) == 0 || TileY(s->tile) == 0) {
 
				ShowErrorMessage(INVALID_STRING_ID, STR_CONFIG_SETTING_EDGES_NOT_EMPTY, 0, 0);
 
				return false;
 
			}
 
		}
 
		Station *st;
 
		FOR_ALL_STATIONS(st) {
src/ship.h
Show inline comments
 
@@ -46,7 +46,9 @@ struct Ship: public SpecializedVehicle<S
 
	void OnNewDay();
 
	Trackdir GetVehicleTrackdir() const;
 
	TileIndex GetOrderStationLocation(StationID station);
 
	bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse);
 
};
 

	
 
#define FOR_ALL_SHIPS(var) FOR_ALL_VEHICLES_OF_TYPE(Ship, var)
 

	
 
#endif /* SHIP_H */
src/ship_cmd.cpp
Show inline comments
 
@@ -835,14 +835,14 @@ CommandCost CmdBuildShip(TileIndex tile,
 
 * @param flags type of operation
 
 * @param p1 vehicle ID to be sold
 
 * @param p2 unused
 
 */
 
CommandCost CmdSellShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	Vehicle *v = Vehicle::GetIfValid(p1);
 
	if (v == NULL || v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	Ship *v = Ship::GetIfValid(p1);
 
	if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
 

	
 
	if (!v->IsStoppedInDepot()) {
 
		return_cmd_error(STR_ERROR_SHIP_MUST_BE_STOPPED_IN_DEPOT);
 
	}
 
@@ -881,14 +881,14 @@ CommandCost CmdSendShipToDepot(TileIndex
 
	if (p2 & DEPOT_MASS_SEND) {
 
		/* Mass goto depot requested */
 
		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
 
		return SendAllVehiclesToDepot(VEH_SHIP, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1);
 
	}
 

	
 
	Vehicle *v = Vehicle::GetIfValid(p1);
 
	if (v == NULL || v->type != VEH_SHIP) return CMD_ERROR;
 
	Ship *v = Ship::GetIfValid(p1);
 
	if (v == NULL) return CMD_ERROR;
 

	
 
	return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
 
}
 

	
 

	
 
/** Refits a ship to the specified cargo type.
 
@@ -905,15 +905,15 @@ CommandCost CmdRefitShip(TileIndex tile,
 
{
 
	CommandCost cost(EXPENSES_SHIP_RUN);
 
	CargoID new_cid = GB(p2, 0, 8); // gets the cargo number
 
	byte new_subtype = GB(p2, 8, 8);
 
	uint16 capacity = CALLBACK_FAILED;
 

	
 
	Vehicle *v = Vehicle::GetIfValid(p1);
 
	Ship *v = Ship::GetIfValid(p1);
 

	
 
	if (v == NULL || v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (!v->IsStoppedInDepot()) return_cmd_error(STR_ERROR_SHIP_MUST_BE_STOPPED_IN_DEPOT);
 
	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_REFIT_DESTROYED_VEHICLE);
 

	
 
	/* Check cargo */
 
	if (!ShipVehInfo(v->engine_type)->refittable) return CMD_ERROR;
 
	if (new_cid >= NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR;
src/smallmap_gui.cpp
Show inline comments
 
@@ -17,12 +17,13 @@
 
#include "strings_func.h"
 
#include "zoom_func.h"
 
#include "core/endian_func.hpp"
 
#include "vehicle_base.h"
 
#include "sound_func.h"
 
#include "window_func.h"
 
#include "effectvehicle_base.h"
 

	
 
#include "table/strings.h"
 
#include "table/sprites.h"
 

	
 
/** Widget numbers of the small map window. */
 
enum SmallMapWindowWidgets {
 
@@ -736,19 +737,15 @@ public:
 
			ptr = blitter->MoveTo(ptr, 2, 0);
 
			x += 2;
 
		}
 

	
 
		/* draw vehicles? */
 
		if (this->map_type == SMT_CONTOUR || this->map_type == SMT_VEHICLES) {
 
			Vehicle *v;
 
			bool skip;
 
			byte colour;
 

	
 
			FOR_ALL_VEHICLES(v) {
 
				if (v->type != VEH_EFFECT &&
 
						(v->vehstatus & (VS_HIDDEN | VS_UNCLICKABLE)) == 0) {
 
			EffectVehicle *v;
 
			FOR_ALL_EFFECTVEHICLES(v) {
 
				if ((v->vehstatus & (VS_HIDDEN | VS_UNCLICKABLE)) == 0) {
 
					/* Remap into flat coordinates. */
 
					Point pt = RemapCoords(
 
						v->x_pos / TILE_SIZE - this->scroll_x / TILE_SIZE, // divide each one separately because (a-b)/c != a/c-b/c in integer world
 
						v->y_pos / TILE_SIZE - this->scroll_y / TILE_SIZE, //    dtto
 
						0);
 
					x = pt.x;
 
@@ -756,13 +753,13 @@ public:
 

	
 
					/* Check if y is out of bounds? */
 
					y -= dpi->top;
 
					if (!IsInsideMM(y, 0, dpi->height)) continue;
 

	
 
					/* Default is to draw both pixels. */
 
					skip = false;
 
					bool skip = false;
 

	
 
					/* Offset X coordinate */
 
					x -= this->subscroll + 3 + dpi->left;
 

	
 
					if (x < 0) {
 
						/* if x+1 is 0, that means we're on the very left edge,
 
@@ -773,13 +770,13 @@ public:
 
						/* Check if we're at the very right edge, and if so draw only a single pixel */
 
						if (x != dpi->width - 1) continue;
 
						skip = true;
 
					}
 

	
 
					/* Calculate pointer to pixel and the colour */
 
					colour = (this->map_type == SMT_VEHICLES) ? _vehicle_type_colours[v->type] : 0xF;
 
					byte colour = (this->map_type == SMT_VEHICLES) ? _vehicle_type_colours[v->type] : 0xF;
 

	
 
					/* And draw either one or two pixels depending on clipping */
 
					blitter->SetPixel(dpi->dst_ptr, x, y, colour);
 
					if (!skip) blitter->SetPixel(dpi->dst_ptr, x + 1, y, colour);
 
				}
 
			}
src/station_cmd.cpp
Show inline comments
 
@@ -1544,17 +1544,15 @@ static CommandCost RemoveRoadStop(Statio
 
		}
 

	
 
		InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_ROADVEHS);
 
		delete cur_stop;
 

	
 
		/* Make sure no vehicle is going to the old roadstop */
 
		Vehicle *v;
 
		FOR_ALL_VEHICLES(v) {
 
			if (v->type == VEH_ROAD &&
 
					v->First() == v &&
 
					v->current_order.IsType(OT_GOTO_STATION) &&
 
		RoadVehicle *v;
 
		FOR_ALL_ROADVEHICLES(v) {
 
			if (v->First() == v && v->current_order.IsType(OT_GOTO_STATION) &&
 
					v->dest_tile == tile) {
 
				v->dest_tile = v->GetOrderStationLocation(st->index);
 
			}
 
		}
 

	
 
		DoClearSquare(tile);
 
@@ -1961,17 +1959,15 @@ static CommandCost RemoveAirport(Station
 
	const AirportFTAClass *afc = st->Airport();
 
	int w = afc->size_x;
 
	int h = afc->size_y;
 

	
 
	CommandCost cost(EXPENSES_CONSTRUCTION, w * h * _price.remove_airport);
 

	
 
	const Vehicle *v;
 
	FOR_ALL_VEHICLES(v) {
 
		if (!(v->type == VEH_AIRCRAFT && IsNormalAircraft(v))) continue;
 

	
 
		const Aircraft *a = (const Aircraft *)v;
 
	const Aircraft *a;
 
	FOR_ALL_AIRCRAFT(a) {
 
		if (!IsNormalAircraft(a)) continue;
 
		if (a->targetairport == st->index && a->state != FLYING) return CMD_ERROR;
 
	}
 

	
 
	BEGIN_TILE_LOOP(tile_cur, w, h, tile) {
 
		if (!EnsureNoVehicleOnGround(tile_cur)) return CMD_ERROR;
 

	
src/train_cmd.cpp
Show inline comments
 
@@ -182,17 +182,17 @@ static void RailVehicleLengthChanged(con
 
	}
 
}
 

	
 
/** Checks if lengths of all rail vehicles are valid. If not, shows an error message. */
 
void CheckTrainsLengths()
 
{
 
	const Vehicle *v;
 

	
 
	FOR_ALL_VEHICLES(v) {
 
		if (v->type == VEH_TRAIN && v->First() == v && !(v->vehstatus & VS_CRASHED)) {
 
			for (const Train *u = (const Train *)v, *w = (const Train *)v->Next(); w != NULL; u = w, w = w->Next()) {
 
	const Train *v;
 

	
 
	FOR_ALL_TRAINS(v) {
 
		if (v->First() == v && !(v->vehstatus & VS_CRASHED)) {
 
			for (const Train *u = v, *w = v->Next(); w != NULL; u = w, w = w->Next()) {
 
				if (u->track != TRACK_BIT_DEPOT) {
 
					if ((w->track != TRACK_BIT_DEPOT &&
 
							max(abs(u->x_pos - w->x_pos), abs(u->y_pos - w->y_pos)) != u->tcache.cached_veh_length) ||
 
							(w->track == TRACK_BIT_DEPOT && TicksToLeaveDepot(u) <= 0)) {
 
						SetDParam(0, v->index);
 
						SetDParam(1, v->owner);
 
@@ -651,17 +651,17 @@ static CommandCost CmdBuildRailWagon(Eng
 
		return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		Vehicle *u = NULL;
 

	
 
		Vehicle *w;
 
		FOR_ALL_VEHICLES(w) {
 
		Train *w;
 
		FOR_ALL_TRAINS(w) {
 
			/* do not connect new wagon with crashed/flooded consists */
 
			if (w->type == VEH_TRAIN && w->tile == tile &&
 
					IsFreeWagon(w) && w->engine_type == engine &&
 
			if (w->tile == tile && IsFreeWagon(w) &&
 
					w->engine_type == engine &&
 
					!HASBITS(w->vehstatus, VS_CRASHED)) {
 
				u = GetLastVehicleInChain(w);
 
				break;
 
			}
 
		}
 

	
 
@@ -729,18 +729,16 @@ static CommandCost CmdBuildRailWagon(Eng
 
	return value;
 
}
 

	
 
/** Move all free vehicles in the depot to the train */
 
static void NormalizeTrainVehInDepot(const Train *u)
 
{
 
	const Vehicle *v;
 

	
 
	FOR_ALL_VEHICLES(v) {
 
		if (v->type == VEH_TRAIN && IsFreeWagon(v) &&
 
				v->tile == u->tile &&
 
				((const Train *)v)->track == TRACK_BIT_DEPOT) {
 
	const Train *v;
 
	FOR_ALL_TRAINS(v) {
 
		if (IsFreeWagon(v) && v->tile == u->tile &&
 
				v->track == TRACK_BIT_DEPOT) {
 
			if (CmdFailed(DoCommand(0, v->index | (u->index << 16), 1, DC_EXEC,
 
					CMD_MOVE_RAIL_VEHICLE)))
 
				break;
 
		}
 
	}
 
}
 
@@ -2233,14 +2231,14 @@ CommandCost CmdSendTrainToDepot(TileInde
 
	if (p2 & DEPOT_MASS_SEND) {
 
		/* Mass goto depot requested */
 
		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
 
		return SendAllVehiclesToDepot(VEH_TRAIN, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1);
 
	}
 

	
 
	Vehicle *v = Vehicle::GetIfValid(p1);
 
	if (v == NULL || v->type != VEH_TRAIN) return CMD_ERROR;
 
	Train *v = Train::GetIfValid(p1);
 
	if (v == NULL) return CMD_ERROR;
 

	
 
	return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
 
}
 

	
 

	
 
void OnTick_Train()
 
@@ -3564,16 +3562,16 @@ static Vehicle *FindTrainCollideEnum(Veh
 
		/* crash both trains */
 
		tcc->num += TrainCrashed((Train *)tcc->v);
 
		tcc->num += TrainCrashed((Train *)coll);
 

	
 
		/* Try to reserve all tiles directly under the crashed trains.
 
		 * As there might be more than two trains involved, we have to do that for all vehicles */
 
		const Vehicle *u;
 
		FOR_ALL_VEHICLES(u) {
 
			if (u->type == VEH_TRAIN && HASBITS(u->vehstatus, VS_CRASHED) && (((const Train *)u)->track & TRACK_BIT_DEPOT) == TRACK_BIT_NONE) {
 
				TrackBits trackbits = ((const Train *)u)->track;
 
		const Train *u;
 
		FOR_ALL_TRAINS(u) {
 
			if (HASBITS(u->vehstatus, VS_CRASHED) && (u->track & TRACK_BIT_DEPOT) == TRACK_BIT_NONE) {
 
				TrackBits trackbits = u->track;
 
				if ((trackbits & TRACK_BIT_WORMHOLE) == TRACK_BIT_WORMHOLE) {
 
					/* Vehicle is inside a wormhole, v->track contains no useful value then. */
 
					trackbits |= DiagDirToDiagTrackBits(GetTunnelBridgeDirection(u->tile));
 
				}
 
				TryReserveRailTrack(u->tile, TrackBitsToTrack(trackbits));
 
			}
src/train_gui.cpp
Show inline comments
 
@@ -21,19 +21,18 @@
 
void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (!success) return;
 

	
 
	/* find a locomotive in the depot. */
 
	const Vehicle *found = NULL;
 
	const Vehicle *v;
 
	FOR_ALL_VEHICLES(v) {
 
		if (v->type == VEH_TRAIN && IsFrontEngine(v) &&
 
				v->tile == tile &&
 
				((const Train *)v)->track == TRACK_BIT_DEPOT) {
 
	const Train *t;
 
	FOR_ALL_TRAINS(t) {
 
		if (IsFrontEngine(t) && t->tile == tile &&
 
				t->track == TRACK_BIT_DEPOT) {
 
			if (found != NULL) return; // must be exactly one.
 
			found = v;
 
			found = t;
 
		}
 
	}
 

	
 
	/* if we found a loco, */
 
	if (found != NULL) {
 
		found = GetLastVehicleInChain(found);
src/vehicle_base.h
Show inline comments
 
@@ -488,12 +488,15 @@ public:
 
	inline Order *GetLastOrder() const
 
	{
 
		return (this->orders.list == NULL) ? NULL : this->orders.list->GetLastOrder();
 
	}
 
};
 

	
 
#define FOR_ALL_VEHICLES_FROM(var, start) FOR_ALL_ITEMS_FROM(Vehicle, vehicle_index, var, start)
 
#define FOR_ALL_VEHICLES(var) FOR_ALL_VEHICLES_FROM(var, 0)
 

	
 
/**
 
 * Class defining several overloaded accessors so we don't
 
 * have to cast vehicle types that often
 
 */
 
template <class T, VehicleType Type>
 
struct SpecializedVehicle : public Vehicle {
 
@@ -569,14 +572,13 @@ struct DisasterVehicle : public Speciali
 

	
 
	const char *GetTypeString() const { return "disaster vehicle"; }
 
	void UpdateDeltaXY(Direction direction);
 
	bool Tick();
 
};
 

	
 
#define FOR_ALL_VEHICLES_FROM(var, start) FOR_ALL_ITEMS_FROM(Vehicle, vehicle_index, var, start)
 
#define FOR_ALL_VEHICLES(var) FOR_ALL_VEHICLES_FROM(var, 0)
 
#define FOR_ALL_DISASTERVEHICLES(var) FOR_ALL_VEHICLES_OF_TYPE(DisasterVehicle, var)
 

	
 
/** Generates sequence of free UnitID numbers */
 
struct FreeUnitIDGenerator {
 
	bool *cache;  ///< array of occupied unit id numbers
 
	UnitID maxid; ///< maximum ID at the moment of constructor call
 
	UnitID curid; ///< last ID returned ; 0 if none
src/waypoint_cmd.cpp
Show inline comments
 
@@ -182,16 +182,14 @@ CommandCost CmdBuildTrainWaypoint(TileIn
 
		} else {
 
			/* Move existing (recently deleted) waypoint to the new location */
 

	
 
			/* First we update the destination for all vehicles that
 
			 * have the old waypoint in their orders. */
 
			Vehicle *v;
 
			FOR_ALL_VEHICLES(v) {
 
				if (v->type == VEH_TRAIN &&
 
						v->First() == v &&
 
						v->current_order.IsType(OT_GOTO_WAYPOINT) &&
 
			FOR_ALL_TRAINS(v) {
 
				if (v->First() == v && v->current_order.IsType(OT_GOTO_WAYPOINT) &&
 
						v->dest_tile == wp->xy) {
 
					v->dest_tile = tile;
 
				}
 
			}
 

	
 
			RedrawWaypointSign(wp);
0 comments (0 inline, 0 general)