Changeset - r8892:ec2887438e08
[Not reviewed]
master
0 1 0
rubidium - 16 years ago 2008-04-11 13:19:44
rubidium@openttd.org
(svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
1 file changed with 76 insertions and 102 deletions:
0 comments (0 inline, 0 general)
src/vehicle.cpp
Show inline comments
 
@@ -219,19 +219,19 @@ Vehicle *GetVehicleTunnelBridge(TileInde
 
	if (v != NULL) return v;
 

	
 
	return (Vehicle*)VehicleFromPos(endtile, NULL, &GetVehicleTunnelBridgeProc);
 
}
 

	
 

	
 
static void UpdateVehiclePosHash(Vehicle* v, int x, int y);
 
static void UpdateVehiclePosHash(Vehicle *v, int x, int y);
 

	
 
void VehiclePositionChanged(Vehicle *v)
 
{
 
	int img = v->cur_image;
 
	Point pt = RemapCoords(v->x_pos + v->x_offs, v->y_pos + v->y_offs, v->z_pos);
 
	const Sprite* spr = GetSprite(img);
 
	const Sprite *spr = GetSprite(img);
 

	
 
	pt.x += spr->x_offs;
 
	pt.y += spr->y_offs;
 

	
 
	UpdateVehiclePosHash(v, pt.x, pt.y);
 

	
 
@@ -459,13 +459,13 @@ static void UpdateNewVehiclePosHash(Vehi
 
	/* Remember current hash position */
 
	v->old_new_hash = new_hash;
 
}
 

	
 
static Vehicle *_vehicle_position_hash[0x1000];
 

	
 
static void UpdateVehiclePosHash(Vehicle* v, int x, int y)
 
static void UpdateVehiclePosHash(Vehicle *v, int x, int y)
 
{
 
	UpdateNewVehiclePosHash(v, x == INVALID_COORD);
 

	
 
	Vehicle **old_hash, **new_hash;
 
	int old_x = v->left_coord;
 
	int old_y = v->top_coord;
 
@@ -761,19 +761,15 @@ CommandCost GetRefitCost(EngineID engine
 
	return CommandCost(expense_type, (EngInfo(engine_type)->refit_cost * base_cost) >> 10);
 
}
 

	
 
static void DoDrawVehicle(const Vehicle *v)
 
{
 
	SpriteID image = v->cur_image;
 
	SpriteID pal;
 

	
 
	if (v->vehstatus & VS_DEFPAL) {
 
		pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
 
	} else {
 
		pal = PAL_NONE;
 
	}
 
	SpriteID pal = PAL_NONE;
 

	
 
	if (v->vehstatus & VS_DEFPAL) pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
 

	
 
	AddSortableSpriteToDraw(image, pal, v->x_pos + v->x_offs, v->y_pos + v->y_offs,
 
		v->x_extent, v->y_extent, v->z_extent, v->z_pos, (v->vehstatus & VS_SHADOW) != 0);
 
}
 

	
 
void ViewportAddVehicles(DrawPixelInfo *dpi)
 
@@ -1295,17 +1291,15 @@ static void BubbleTick(Vehicle *v)
 
	 * Warning: those effects can NOT use Random(), and have to use
 
	 *  InteractiveRandom(), because somehow someone forgot to save
 
	 *  spritenum to the savegame, and so it will cause desyncs in
 
	 *  multiplayer!! (that is: in ToyLand)
 
	 */
 
	uint et;
 
	const BubbleMovement *b;
 

	
 
	v->progress++;
 
	if ((v->progress & 3) != 0)
 
		return;
 
	if ((v->progress & 3) != 0) return;
 

	
 
	BeginVehicleMove(v);
 

	
 
	if (v->spritenum == 0) {
 
		v->cur_image++;
 
		if (v->cur_image < SPR_BUBBLE_GENERATE_3) {
 
@@ -1320,13 +1314,13 @@ static void BubbleTick(Vehicle *v)
 
		}
 
		et = 0;
 
	} else {
 
		et = v->engine_type + 1;
 
	}
 

	
 
	b = &_bubble_movement[v->spritenum - 1][et];
 
	const BubbleMovement *b = &_bubble_movement[v->spritenum - 1][et];
 

	
 
	if (b->y == 4 && b->x == 0) {
 
		EndVehicleMove(v);
 
		delete v;
 
		return;
 
	}
 
@@ -1391,15 +1385,13 @@ static EffectTickProc * const _effect_ti
 
	BubbleTick,
 
};
 

	
 

	
 
Vehicle *CreateEffectVehicle(int x, int y, int z, EffectVehicle type)
 
{
 
	Vehicle *v;
 

	
 
	v = new SpecialVehicle();
 
	Vehicle *v = new SpecialVehicle();
 
	if (v != NULL) {
 
		v->subtype = type;
 
		v->x_pos = x;
 
		v->y_pos = y;
 
		v->z_pos = z;
 
		v->tile = 0;
 
@@ -1434,27 +1426,25 @@ void SpecialVehicle::Tick()
 

	
 
Vehicle *CheckClickOnVehicle(const ViewPort *vp, int x, int y)
 
{
 
	Vehicle *found = NULL, *v;
 
	uint dist, best_dist = (uint)-1;
 

	
 
	if ( (uint)(x -= vp->left) >= (uint)vp->width ||
 
			 (uint)(y -= vp->top) >= (uint)vp->height)
 
				return NULL;
 
	if ((uint)(x -= vp->left) >= (uint)vp->width || (uint)(y -= vp->top) >= (uint)vp->height) return NULL;
 

	
 
	x = ScaleByZoom(x, vp->zoom) + vp->virtual_left;
 
	y = ScaleByZoom(y, vp->zoom) + vp->virtual_top;
 

	
 
	FOR_ALL_VEHICLES(v) {
 
		if ((v->vehstatus & (VS_HIDDEN|VS_UNCLICKABLE)) == 0 &&
 
				x >= v->left_coord && x <= v->right_coord &&
 
				y >= v->top_coord && y <= v->bottom_coord) {
 

	
 
			dist = max(
 
				abs( ((v->left_coord + v->right_coord)>>1) - x ),
 
				abs( ((v->top_coord + v->bottom_coord)>>1) - y )
 
				abs(((v->left_coord + v->right_coord) >> 1) - x),
 
				abs(((v->top_coord + v->bottom_coord) >> 1) - y)
 
			);
 

	
 
			if (dist < best_dist) {
 
				found = v;
 
				best_dist = dist;
 
			}
 
@@ -1491,29 +1481,27 @@ static const byte _breakdown_chance[64] 
 
	150, 170, 190, 210, 230, 250, 250, 250,
 
};
 

	
 
void CheckVehicleBreakdown(Vehicle *v)
 
{
 
	int rel, rel_old;
 
	uint32 r;
 
	int chance;
 

	
 
	/* decrease reliability */
 
	v->reliability = rel = max((rel_old = v->reliability) - v->reliability_spd_dec, 0);
 
	if ((rel_old >> 8) != (rel >> 8))
 
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
 

	
 
	if (v->breakdown_ctr != 0 || v->vehstatus & VS_STOPPED ||
 
			v->cur_speed < 5 || _game_mode == GM_MENU) {
 
		return;
 
	}
 

	
 
	r = Random();
 
	uint32 r = Random();
 

	
 
	/* increase chance of failure */
 
	chance = v->breakdown_chance + 1;
 
	int chance = v->breakdown_chance + 1;
 
	if (Chance16I(1,25,r)) chance += 25;
 
	v->breakdown_chance = min(255, chance);
 

	
 
	/* calculate reliability value to use in comparison */
 
	rel = v->reliability;
 
	if (v->type == VEH_SHIP) rel += 0x6666;
 
@@ -1582,13 +1570,12 @@ void AgeVehicle(Vehicle *v)
 
CommandCost CmdMassStartStopVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle **vl = NULL;
 
	uint16 engine_list_length = 0;
 
	uint16 engine_count = 0;
 
	CommandCost return_value = CMD_ERROR;
 
	uint i;
 
	uint stop_command;
 
	VehicleType vehicle_type = (VehicleType)GB(p2, 0, 5);
 
	bool start_stop = HasBit(p2, 5);
 
	bool vehicle_list_window = HasBit(p2, 6);
 

	
 
	switch (vehicle_type) {
 
@@ -1606,27 +1593,26 @@ CommandCost CmdMassStartStopVehicle(Tile
 
		engine_count = GenerateVehicleSortList((const Vehicle***)&vl, &engine_list_length, vehicle_type, _current_player, id, window_type);
 
	} else {
 
		/* Get the list of vehicles in the depot */
 
		BuildDepotVehicleList(vehicle_type, tile, &vl, &engine_list_length, &engine_count, NULL, NULL, NULL);
 
	}
 

	
 
	for (i = 0; i < engine_count; i++) {
 
	for (uint i = 0; i < engine_count; i++) {
 
		const Vehicle *v = vl[i];
 
		CommandCost ret;
 

	
 
		if (!!(v->vehstatus & VS_STOPPED) != start_stop) continue;
 

	
 
		if (!vehicle_list_window) {
 
			if (vehicle_type == VEH_TRAIN) {
 
				if (CheckTrainInDepot(v, false) == -1) continue;
 
			} else {
 
				if (!(v->vehstatus & VS_HIDDEN)) continue;
 
			}
 
		}
 

	
 
		ret = DoCommand(tile, v->index, 0, flags, stop_command);
 
		CommandCost ret = DoCommand(tile, v->index, 0, flags, stop_command);
 

	
 
		if (CmdSucceeded(ret)) {
 
			return_value = CommandCost();
 
			/* We know that the command is valid for at least one vehicle.
 
			 * If we haven't set DC_EXEC, then there is no point in continueing because it will be valid */
 
			if (!(flags & DC_EXEC)) break;
 
@@ -1650,13 +1636,13 @@ CommandCost CmdDepotSellAllVehicles(Tile
 
	uint16 engine_list_length = 0;
 
	uint16 engine_count = 0;
 
	uint16 wagon_list_length = 0;
 
	uint16 wagon_count = 0;
 

	
 
	CommandCost cost(EXPENSES_NEW_VEHICLES);
 
	uint i, sell_command, total_number_vehicles;
 
	uint  sell_command, total_number_vehicles;
 
	VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
 

	
 
	switch (vehicle_type) {
 
		case VEH_TRAIN:    sell_command = CMD_SELL_RAIL_WAGON; break;
 
		case VEH_ROAD:     sell_command = CMD_SELL_ROAD_VEH;   break;
 
		case VEH_SHIP:     sell_command = CMD_SELL_SHIP;       break;
 
@@ -1666,23 +1652,22 @@ CommandCost CmdDepotSellAllVehicles(Tile
 

	
 
	/* Get the list of vehicles in the depot */
 
	BuildDepotVehicleList(vehicle_type, tile, &engines, &engine_list_length, &engine_count,
 
						                      &wagons,  &wagon_list_length,  &wagon_count);
 

	
 
	total_number_vehicles = engine_count + wagon_count;
 
	for (i = 0; i < total_number_vehicles; i++) {
 
	for (uint i = 0; i < total_number_vehicles; i++) {
 
		const Vehicle *v;
 
		CommandCost ret;
 

	
 
		if (i < engine_count) {
 
			v = engines[i];
 
		} else {
 
			v = wagons[i - engine_count];
 
		}
 

	
 
		ret = DoCommand(tile, v->index, 1, flags, sell_command);
 
		CommandCost ret = DoCommand(tile, v->index, 1, flags, sell_command);
 

	
 
		if (CmdSucceeded(ret)) cost.AddCost(ret);
 
	}
 

	
 
	free(engines);
 
	free(wagons);
 
@@ -1701,36 +1686,33 @@ CommandCost CmdDepotSellAllVehicles(Tile
 
 */
 
CommandCost CmdDepotMassAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle **vl = NULL;
 
	uint16 engine_list_length = 0;
 
	uint16 engine_count = 0;
 
	uint i;
 
	CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES);
 
	VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
 
	bool all_or_nothing = HasBit(p2, 0);
 

	
 
	if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_player)) return CMD_ERROR;
 

	
 
	/* Get the list of vehicles in the depot */
 
	BuildDepotVehicleList(vehicle_type, tile, &vl, &engine_list_length, &engine_count, &vl, &engine_list_length, &engine_count);
 

	
 

	
 
	for (i = 0; i < engine_count; i++) {
 
	for (uint i = 0; i < engine_count; i++) {
 
		Vehicle *v = vl[i];
 
		bool stopped = !(v->vehstatus & VS_STOPPED);
 
		CommandCost ret;
 

	
 
		/* Ensure that the vehicle completely in the depot */
 
		if (!v->IsInDepot()) continue;
 

	
 
		if (stopped) {
 
			v->vehstatus |= VS_STOPPED; // Stop the vehicle
 
			v->leave_depot_instantly = true;
 
		}
 
		ret = MaybeReplaceVehicle(v, !(flags & DC_EXEC), false);
 
		CommandCost ret = MaybeReplaceVehicle(v, !(flags & DC_EXEC), false);
 

	
 
		if (CmdSucceeded(ret)) {
 
			cost.AddCost(ret);
 
		} else {
 
			if (all_or_nothing) {
 
				/* We failed to replace a vehicle even though we set all or nothing.
 
@@ -1761,24 +1743,22 @@ error:
 
 * @param flags type of operation
 
 * @param p1 the original vehicle's index
 
 * @param p2 1 = shared orders, else copied orders
 
 */
 
CommandCost CmdCloneVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v_front, *v;
 
	Vehicle *w_front, *w, *w_rear;
 
	CommandCost cost, total_cost(EXPENSES_NEW_VEHICLES);
 
	CommandCost total_cost(EXPENSES_NEW_VEHICLES);
 
	uint32 build_argument = 2;
 

	
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 
	v = GetVehicle(p1);
 
	v_front = v;
 
	w = NULL;
 
	w_front = NULL;
 
	w_rear = NULL;
 

	
 

	
 
	Vehicle *v = GetVehicle(p1);
 
	Vehicle *v_front = v;
 
	Vehicle *w = NULL;
 
	Vehicle *w_front = NULL;
 
	Vehicle *w_rear = NULL;
 

	
 
	/*
 
	 * v_front is the front engine in the original vehicle
 
	 * v is the car/vehicle of the original vehicle, that is currently being copied
 
	 * w_front is the front engine of the cloned vehicle
 
	 * w is the car/vehicle currently being cloned
 
@@ -1806,13 +1786,13 @@ CommandCost CmdCloneVehicle(TileIndex ti
 
	do {
 
		if (v->type == VEH_TRAIN && IsRearDualheaded(v)) {
 
			/* we build the rear ends of multiheaded trains with the front ones */
 
			continue;
 
		}
 

	
 
		cost = DoCommand(tile, v->engine_type, build_argument, flags, GetCmdBuildVeh(v));
 
		CommandCost cost = DoCommand(tile, v->engine_type, build_argument, flags, GetCmdBuildVeh(v));
 
		build_argument = 3; // ensure that we only assign a number to the first engine
 

	
 
		if (CmdFailed(cost)) return cost;
 

	
 
		total_cost.AddCost(cost);
 

	
 
@@ -1867,13 +1847,13 @@ CommandCost CmdCloneVehicle(TileIndex ti
 
	do {
 
		do {
 
			if (flags & DC_EXEC) {
 
				assert(w != NULL);
 

	
 
				if (w->cargo_type != v->cargo_type || w->cargo_subtype != v->cargo_subtype) {
 
					cost = DoCommand(0, w->index, v->cargo_type | (v->cargo_subtype << 8) | 1U << 16 , flags, GetCmdRefitVeh(v));
 
					CommandCost cost = DoCommand(0, w->index, v->cargo_type | (v->cargo_subtype << 8) | 1U << 16 , flags, GetCmdRefitVeh(v));
 
					if (CmdSucceeded(cost)) total_cost.AddCost(cost);
 
				}
 

	
 
				if (w->type == VEH_TRAIN && EngineHasArticPart(w)) {
 
					w = GetNextArticPart(w);
 
				} else if (w->type == VEH_ROAD && RoadVehHasArticPart(w)) {
 
@@ -2009,27 +1989,27 @@ void BuildDepotVehicleList(VehicleType t
 

	
 
		default: NOT_REACHED();
 
	}
 
}
 

	
 
/**
 
* @param sort_list list to store the list in. Either NULL or the length length_of_array tells
 
* @param length_of_array informs the length allocated for sort_list. This is not the same as the number of vehicles in the list. Needs to be 0 when sort_list is NULL
 
* @param type type of vehicle
 
* @param owner PlayerID of owner to generate a list for
 
* @param index This parameter has different meanings depending on window_type
 
    <ul>
 
      <li>VLW_STATION_LIST:  index of station to generate a list for</li>
 
      <li>VLW_SHARED_ORDERS: index of order to generate a list for<li>
 
      <li>VLW_STANDARD: not used<li>
 
      <li>VLW_DEPOT_LIST: TileIndex of the depot/hangar to make the list for</li>
 
      <li>VLW_GROUP_LIST: index of group to generate a list for</li>
 
    </ul>
 
* @param window_type tells what kind of window the list is for. Use the VLW flags in vehicle_gui.h
 
* @return the number of vehicles added to the list
 
*/
 
 * @param sort_list list to store the list in. Either NULL or the length length_of_array tells
 
 * @param length_of_array informs the length allocated for sort_list. This is not the same as the number of vehicles in the list. Needs to be 0 when sort_list is NULL
 
 * @param type type of vehicle
 
 * @param owner PlayerID of owner to generate a list for
 
 * @param index This parameter has different meanings depending on window_type
 
 *    <ul>
 
 *      <li>VLW_STATION_LIST:  index of station to generate a list for</li>
 
 *      <li>VLW_SHARED_ORDERS: index of order to generate a list for<li>
 
 *      <li>VLW_STANDARD: not used<li>
 
 *      <li>VLW_DEPOT_LIST: TileIndex of the depot/hangar to make the list for</li>
 
 *      <li>VLW_GROUP_LIST: index of group to generate a list for</li>
 
 *    </ul>
 
 * @param window_type tells what kind of window the list is for. Use the VLW flags in vehicle_gui.h
 
 * @return the number of vehicles added to the list
 
 */
 
uint GenerateVehicleSortList(const Vehicle ***sort_list, uint16 *length_of_array, VehicleType type, PlayerID owner, uint32 index, uint16 window_type)
 
{
 
	uint n = 0;
 
	const Vehicle *v;
 

	
 
	switch (window_type) {
 
@@ -2067,13 +2047,13 @@ uint GenerateVehicleSortList(const Vehic
 
		}
 

	
 
		case VLW_STANDARD: {
 
			FOR_ALL_VEHICLES(v) {
 
				if (v->type == type && v->owner == owner && v->IsPrimaryVehicle()) {
 
					/* TODO find a better estimate on the total number of vehicles for current player */
 
					if (n == *length_of_array) ExtendVehicleListSize(sort_list, length_of_array, GetNumVehicles()/4);
 
					if (n == *length_of_array) ExtendVehicleListSize(sort_list, length_of_array, GetNumVehicles() / 4);
 
					(*sort_list)[n++] = v;
 
				}
 
			}
 
			break;
 
		}
 

	
 
@@ -2117,30 +2097,30 @@ uint GenerateVehicleSortList(const Vehic
 
		*sort_list = ReallocT(*sort_list, (*length_of_array) * sizeof((*sort_list)[0]));
 
	}
 

	
 
	return n;
 
}
 

	
 
/** send all vehicles of type to depots
 
/**
 
 * Send all vehicles of type to depots
 
 * @param type type of vehicle
 
 * @param flags the flags used for DoCommand()
 
 * @param service should the vehicles only get service in the depots
 
 * @param owner PlayerID of owner of the vehicles to send
 
 * @param vlw_flag tells what kind of list requested the goto depot
 
 * @return 0 for success and CMD_ERROR if no vehicle is able to go to depot
 
 */
 
CommandCost SendAllVehiclesToDepot(VehicleType type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id)
 
{
 
	const Vehicle **sort_list = NULL;
 
	uint n, i;
 
	uint16 array_length = 0;
 

	
 
	n = GenerateVehicleSortList(&sort_list, &array_length, type, owner, id, vlw_flag);
 
	uint n = GenerateVehicleSortList(&sort_list, &array_length, type, owner, id, vlw_flag);
 

	
 
	/* Send all the vehicles to a depot */
 
	for (i = 0; i < n; i++) {
 
	for (uint i = 0; i < n; i++) {
 
		const Vehicle *v = sort_list[i];
 
		CommandCost ret = DoCommand(v->tile, v->index, (service ? 1 : 0) | DEPOT_DONT_CANCEL, flags, GetCmdSendToDepot(type));
 

	
 
		/* Return 0 if DC_EXEC is not set this is a valid goto depot command)
 
			* In this case we know that at least one vehicle can be sent to a depot
 
			* and we will issue the command. We can now safely quit the loop, knowing
 
@@ -2182,15 +2162,19 @@ uint8 CalcPercentVehicleFilled(Vehicle *
 
			unloading += HasBit(v->vehicle_flags, VF_CARGO_UNLOADING) ? 1 : 0;
 
			loading |= !(u->current_order.GetUnloadType() & OUFB_UNLOAD) && st->goods[v->cargo_type].days_since_pickup != 255;
 
			cars++;
 
		}
 
	}
 

	
 
	if (unloading == 0 && loading)          *color = STR_PERCENT_UP;
 
	else if (cars == unloading || !loading) *color = STR_PERCENT_DOWN;
 
	else                                    *color = STR_PERCENT_UP_DOWN;
 
	if (unloading == 0 && loading) {
 
		*color = STR_PERCENT_UP;
 
	} else if (cars == unloading || !loading) {
 
		*color = STR_PERCENT_DOWN;
 
	} else {
 
		*color = STR_PERCENT_UP_DOWN;
 
	}
 

	
 
	/* Train without capacity */
 
	if (max == 0) return 100;
 

	
 
	/* Return the percentage */
 
	return (count * 100) / max;
 
@@ -2239,24 +2223,20 @@ void VehicleEnterDepot(Vehicle *v)
 

	
 
	VehicleServiceInDepot(v);
 

	
 
	TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT);
 

	
 
	if (v->current_order.IsType(OT_GOTO_DEPOT)) {
 
		Order t;
 

	
 
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
 

	
 
		t = v->current_order;
 
		Order t = v->current_order;
 
		v->current_order.MakeDummy();
 

	
 
		if (t.IsRefit()) {
 
			CommandCost cost;
 

	
 
			_current_player = v->owner;
 
			cost = DoCommand(v->tile, v->index, t.GetRefitCargo() | t.GetRefitSubtype() << 8, DC_EXEC, GetCmdRefitVeh(v));
 
			CommandCost cost = DoCommand(v->tile, v->index, t.GetRefitCargo() | t.GetRefitSubtype() << 8, DC_EXEC, GetCmdRefitVeh(v));
 

	
 
			if (CmdFailed(cost)) {
 
				v->leave_depot_instantly = false; // We ensure that the vehicle stays in the depot
 
				if (v->owner == _local_player) {
 
					/* Notify the user that we stopped the vehicle */
 
					SetDParam(0, _vehicle_type_names[v->type]);
 
@@ -2332,17 +2312,15 @@ static bool IsUniqueVehicleName(const ch
 
 * @param flags type of operation
 
 * @param p1 vehicle ID to name
 
 * @param p2 unused
 
 */
 
CommandCost CmdNameVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 

	
 
	if (!IsValidVehicleID(p1) || StrEmpty(_cmd_text)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 
	Vehicle *v = GetVehicle(p1);
 

	
 
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (!IsUniqueVehicleName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
 

	
 
	if (flags & DC_EXEC) {
 
@@ -2361,18 +2339,17 @@ CommandCost CmdNameVehicle(TileIndex til
 
 * @param flags type of operation
 
 * @param p1 vehicle ID that is being service-interval-changed
 
 * @param p2 new service interval
 
 */
 
CommandCost CmdChangeServiceInt(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle* v;
 
	uint16 serv_int = GetServiceIntervalClamped(p2); /* Double check the service interval from the user-input */
 

	
 
	if (serv_int != p2 || !IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 
	Vehicle *v = GetVehicle(p1);
 

	
 
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		v->service_interval = serv_int;
 
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
 
@@ -2449,36 +2426,34 @@ GetNewVehiclePosResult GetNewVehiclePos(
 
static const Direction _new_direction_table[] = {
 
	DIR_N , DIR_NW, DIR_W ,
 
	DIR_NE, DIR_SE, DIR_SW,
 
	DIR_E , DIR_SE, DIR_S
 
};
 

	
 
Direction GetDirectionTowards(const Vehicle* v, int x, int y)
 
Direction GetDirectionTowards(const Vehicle *v, int x, int y)
 
{
 
	Direction dir;
 
	DirDiff dirdiff;
 
	int i = 0;
 

	
 
	if (y >= v->y_pos) {
 
		if (y != v->y_pos) i+=3;
 
		i+=3;
 
		if (y != v->y_pos) i += 3;
 
		i += 3;
 
	}
 

	
 
	if (x >= v->x_pos) {
 
		if (x != v->x_pos) i++;
 
		i++;
 
	}
 

	
 
	dir = v->direction;
 

	
 
	dirdiff = DirDifference(_new_direction_table[i], dir);
 
	Direction dir = v->direction;
 

	
 
	DirDiff dirdiff = DirDifference(_new_direction_table[i], dir);
 
	if (dirdiff == DIRDIFF_SAME) return dir;
 
	return ChangeDir(dir, dirdiff > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT);
 
}
 

	
 
Trackdir GetVehicleTrackdir(const Vehicle* v)
 
Trackdir GetVehicleTrackdir(const Vehicle *v)
 
{
 
	if (v->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
 

	
 
	switch (v->type) {
 
		case VEH_TRAIN:
 
			if (v->u.rail.track == TRACK_BIT_DEPOT) // We'll assume the train is facing outwards
 
@@ -2524,13 +2499,13 @@ uint32 VehicleEnterTile(Vehicle *v, Tile
 
{
 
	return _tile_type_procs[GetTileType(tile)]->vehicle_enter_tile_proc(v, tile, x, y);
 
}
 

	
 
UnitID GetFreeUnitNumber(VehicleType type)
 
{
 
	UnitID unit, max = 0;
 
	UnitID max = 0;
 
	const Vehicle *u;
 
	static bool *cache = NULL;
 
	static UnitID gmax = 0;
 

	
 
	switch (type) {
 
		case VEH_TRAIN:    max = _patches.max_trains; break;
 
@@ -2561,13 +2536,14 @@ UnitID GetFreeUnitNumber(VehicleType typ
 
	FOR_ALL_VEHICLES(u) {
 
		if (u->type == type && u->owner == _current_player && u->unitnumber != 0 && u->unitnumber <= max)
 
			cache[u->unitnumber] = true;
 
	}
 

	
 
	/* Find the first unused unit number */
 
	for (unit = 1; unit <= max; unit++) {
 
	UnitID unit = 1;
 
	for (; unit <= max; unit++) {
 
		if (!cache[unit]) break;
 
	}
 

	
 
	return unit;
 
}
 

	
 
@@ -2595,13 +2571,12 @@ bool CanBuildVehicleInfrastructure(Vehic
 
		case VEH_AIRCRAFT: max = _patches.max_aircraft; break;
 
		default: NOT_REACHED();
 
	}
 

	
 
	/* We can build vehicle infrastructure when we may build the vehicle type */
 
	if (max > 0) {
 

	
 
		/* Can we actually build the vehicle type? */
 
		EngineID e;
 
		FOR_ALL_ENGINEIDS_OF_TYPE(e, type) {
 
			if (HasBit(GetEngine(e)->player_avail, _local_player)) return true;
 
		}
 
		return false;
 
@@ -3246,13 +3221,13 @@ CommandCost Vehicle::SendToDepot(uint32 
 
	if (this->type == VEH_TRAIN && IsTileDepotType(this->tile, TRANSPORT_RAIL) && this->cur_speed == 0) return CMD_ERROR;
 

	
 
	TileIndex location;
 
	DestinationID destination;
 
	bool reverse;
 
	static const StringID no_depot[] = {STR_883A_UNABLE_TO_FIND_ROUTE_TO, STR_9019_UNABLE_TO_FIND_LOCAL_DEPOT, STR_981A_UNABLE_TO_FIND_LOCAL_DEPOT, STR_A012_CAN_T_SEND_AIRCRAFT_TO};
 
	if (!this->FindClosestDepot	(&location, &destination, &reverse)) return_cmd_error(no_depot[this->type]);
 
	if (!this->FindClosestDepot(&location, &destination, &reverse)) return_cmd_error(no_depot[this->type]);
 

	
 
	if (flags & DC_EXEC) {
 
		if (this->current_order.IsType(OT_LOADING)) this->LeaveStation();
 

	
 
		this->dest_tile = location;
 
		this->current_order.MakeGoToDepot(destination, ODTF_MANUAL);
 
@@ -3260,17 +3235,16 @@ CommandCost Vehicle::SendToDepot(uint32 
 
		InvalidateWindowWidget(WC_VEHICLE_VIEW, this->index, VVW_WIDGET_START_STOP_VEH);
 

	
 
		/* If there is no depot in front, reverse automatically (trains only) */
 
		if (this->type == VEH_TRAIN && reverse) DoCommand(this->tile, this->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
 

	
 
		if (this->type == VEH_AIRCRAFT && this->u.air.state == FLYING && this->u.air.targetairport != destination) {
 
				/* The aircraft is now heading for a different hangar than the next in the orders */
 
				extern void AircraftNextAirportPos_and_Order(Vehicle *v);
 
				AircraftNextAirportPos_and_Order(this);
 
			}
 

	
 
			/* The aircraft is now heading for a different hangar than the next in the orders */
 
			extern void AircraftNextAirportPos_and_Order(Vehicle *v);
 
			AircraftNextAirportPos_and_Order(this);
 
		}
 
	}
 

	
 
	return CommandCost();
 

	
 
}
 

	
0 comments (0 inline, 0 general)