Changeset - r15841:06d4b8da9773
[Not reviewed]
master
0 3 0
rubidium - 14 years ago 2010-08-18 00:03:08
rubidium@openttd.org
(svn r20533) -Codechange: shuffle a bit with the bits in the sell command
3 files changed with 12 insertions and 10 deletions:
0 comments (0 inline, 0 general)
src/ai/api/ai_vehicle.cpp
Show inline comments
 
@@ -128,36 +128,36 @@
 
{
 
	EnforcePrecondition(false, IsValidVehicle(vehicle_id) && AICargo::IsValidCargo(cargo));
 

	
 
	return AIObject::DoCommand(0, vehicle_id, cargo, GetCmdRefitVeh(::Vehicle::Get(vehicle_id)));
 
}
 

	
 

	
 
/* static */ bool AIVehicle::SellVehicle(VehicleID vehicle_id)
 
{
 
	EnforcePrecondition(false, IsValidVehicle(vehicle_id));
 

	
 
	const Vehicle *v = ::Vehicle::Get(vehicle_id);
 
	return AIObject::DoCommand(0, vehicle_id, v->type == VEH_TRAIN ? 1 : 0, GetCmdSellVeh(v));
 
	return AIObject::DoCommand(0, vehicle_id | (v->type == VEH_TRAIN ? 1 : 0) << 16, 0, GetCmdSellVeh(v));
 
}
 

	
 
/* static */ bool AIVehicle::_SellWagonInternal(VehicleID vehicle_id, int wagon, bool sell_attached_wagons)
 
{
 
	EnforcePrecondition(false, IsValidVehicle(vehicle_id) && wagon < GetNumWagons(vehicle_id));
 
	EnforcePrecondition(false, ::Vehicle::Get(vehicle_id)->type == VEH_TRAIN);
 

	
 
	const Train *v = ::Train::Get(vehicle_id);
 
	while (wagon-- > 0) v = v->GetNextUnit();
 

	
 
	return AIObject::DoCommand(0, v->index, sell_attached_wagons ? 1 : 0, CMD_SELL_VEHICLE);
 
	return AIObject::DoCommand(0, v->index | (sell_attached_wagons ? 1 : 0) << 16, 0, CMD_SELL_VEHICLE);
 
}
 

	
 
/* static */ bool AIVehicle::SellWagon(VehicleID vehicle_id, int wagon)
 
{
 
	return _SellWagonInternal(vehicle_id, wagon, false);
 
}
 

	
 
/* static */ bool AIVehicle::SellWagonChain(VehicleID vehicle_id, int wagon)
 
{
 
	return _SellWagonInternal(vehicle_id, wagon, true);
 
}
 

	
src/depot_gui.cpp
Show inline comments
 
@@ -974,25 +974,25 @@ struct DepotWindow : Window {
 
				this->sel = INVALID_VEHICLE;
 
				this->SetDirty();
 

	
 
				int sell_cmd = (v->type == VEH_TRAIN && (widget == DEPOT_WIDGET_SELL_CHAIN || _ctrl_pressed)) ? 1 : 0;
 

	
 
				bool is_engine = (v->type != VEH_TRAIN || Train::From(v)->IsFrontEngine());
 

	
 
				if (is_engine) {
 
					_backup_orders_tile = v->tile;
 
					BackupVehicleOrders(v);
 
				}
 

	
 
				if (!DoCommandP(v->tile, v->index, sell_cmd, GetCmdSellVeh(v->type)) && is_engine) _backup_orders_tile = 0;
 
				if (!DoCommandP(v->tile, v->index | sell_cmd << 16, 0, GetCmdSellVeh(v->type)) && is_engine) _backup_orders_tile = 0;
 
				break;
 
			}
 

	
 
			default:
 
				this->sel = INVALID_VEHICLE;
 
				this->SetDirty();
 
		}
 
		_cursor.vehchain = false;
 
	}
 

	
 
	virtual void OnTimeout()
 
	{
src/vehicle_cmd.cpp
Show inline comments
 
@@ -146,45 +146,47 @@ CommandCost CmdBuildVehicle(TileIndex ti
 
		Company::Get(_current_company)->num_engines[eid]++;
 
	}
 

	
 
	return value;
 
}
 

	
 
CommandCost CmdSellRailWagon(DoCommandFlag flags, Vehicle *v, uint16 data);
 

	
 
/**
 
 * Sell a vehicle.
 
 * @param tile unused.
 
 * @param flags for command.
 
 * @param p1 vehicle ID to be sold.
 
 * @param p1 various bitstuffed data.
 
 *  bits  0-15: vehicle ID being sold.
 
 *  bits 16-31: vehicle type specific bits passed on to the vehicle build functions.
 
 * @param p2 unused.
 
 * @param text unused.
 
 * @return the cost of this operation or an error.
 
 */
 
CommandCost CmdSellVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 16));
 
	if (v == NULL) return CMD_ERROR;
 

	
 
	Vehicle *front = v->First();
 

	
 
	CommandCost ret = CheckOwnership(front->owner);
 
	if (ret.Failed()) return ret;
 

	
 
	if (front->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
 

	
 
	if (!front->IsStoppedInDepot()) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED + front->type);
 

	
 
	if (v->type == VEH_TRAIN) {
 
		ret = CmdSellRailWagon(flags, v, GB(p2, 0, 16));
 
		ret = CmdSellRailWagon(flags, v, GB(p1, 16, 16));
 
	} else {
 
		ret = CommandCost(EXPENSES_NEW_VEHICLES, -front->value);
 

	
 
		if (flags & DC_EXEC) {
 
			delete front;
 
		}
 
	}
 

	
 
	return ret;
 
}
 

	
 
/**
 
@@ -317,25 +319,25 @@ CommandCost CmdDepotSellAllVehicles(Tile
 
	CommandCost cost(EXPENSES_NEW_VEHICLES);
 
	VehicleType vehicle_type = Extract<VehicleType, 0, 3>(p1);
 
	uint sell_command = GetCmdSellVeh(vehicle_type);
 

	
 
	if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
 

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

	
 
	CommandCost last_error = CMD_ERROR;
 
	bool had_success = false;
 
	for (uint i = 0; i < list.Length(); i++) {
 
		CommandCost ret = DoCommand(tile, list[i]->index, 1, flags, sell_command);
 
		CommandCost ret = DoCommand(tile, list[i]->index | (1 << 16), 0, flags, sell_command);
 
		if (ret.Succeeded()) {
 
			cost.AddCost(ret);
 
			had_success = true;
 
		} else {
 
			last_error = ret;
 
		}
 
	}
 

	
 
	return had_success ? cost : last_error;
 
}
 

	
 
/**
 
@@ -588,46 +590,46 @@ CommandCost CmdCloneVehicle(TileIndex ti
 
		 * vehicles is almost reached (e.g. max trains - 1) not all vehicles would
 
		 * be cloned. When the non-primary engines were build they were seen as
 
		 * 'new' vehicles whereas they would immediately be joined with a primary
 
		 * engine. This caused the vehicle to be not build as 'the limit' had been
 
		 * reached, resulting in partially build vehicles and such. */
 
		DoCommandFlag build_flags = flags;
 
		if ((flags & DC_EXEC) && !v->IsPrimaryVehicle()) build_flags |= DC_AUTOREPLACE;
 

	
 
		CommandCost cost = DoCommand(tile, v->engine_type | (1 << 16), 0, build_flags, GetCmdBuildVeh(v));
 

	
 
		if (cost.Failed()) {
 
			/* Can't build a part, then sell the stuff we already made; clear up the mess */
 
			if (w_front != NULL) DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
 
			if (w_front != NULL) DoCommand(w_front->tile, w_front->index | (1 << 16), 0, flags, GetCmdSellVeh(w_front));
 
			return cost;
 
		}
 

	
 
		total_cost.AddCost(cost);
 

	
 
		if (flags & DC_EXEC) {
 
			w = Vehicle::Get(_new_vehicle_id);
 

	
 
			if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION)) {
 
				SetBit(Train::From(w)->flags, VRF_REVERSE_DIRECTION);
 
			}
 

	
 
			if (v->type == VEH_TRAIN && !Train::From(v)->IsFrontEngine()) {
 
				/* this s a train car
 
				 * add this unit to the end of the train */
 
				CommandCost result = DoCommand(0, (w_rear->index << 16) | w->index, 1, flags, CMD_MOVE_RAIL_VEHICLE);
 
				if (result.Failed()) {
 
					/* The train can't be joined to make the same consist as the original.
 
					 * Sell what we already made (clean up) and return an error.           */
 
					DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
 
					DoCommand(w_front->tile, w->index,       1, flags, GetCmdSellVeh(w));
 
					DoCommand(w_front->tile, w_front->index | 1 << 16, 0, flags, GetCmdSellVeh(w_front));
 
					DoCommand(w_front->tile, w->index       | 1 << 16, 0, flags, GetCmdSellVeh(w));
 
					return result; // return error and the message returned from CMD_MOVE_RAIL_VEHICLE
 
				}
 
			} else {
 
				/* this is a front engine or not a train. */
 
				w_front = w;
 
				w->service_interval = v->service_interval;
 
			}
 
			w_rear = w; // trains needs to know the last car in the train, so they can add more in next loop
 
		}
 
	} while (v->type == VEH_TRAIN && (v = Train::From(v)->GetNextVehicle()) != NULL);
 

	
 
	if ((flags & DC_EXEC) && v_front->type == VEH_TRAIN) {
 
@@ -699,25 +701,25 @@ CommandCost CmdCloneVehicle(TileIndex ti
 
		 */
 
		DoCommand(0, (v_front->index << 16) | w_front->index, p2 & 1 ? CO_SHARE : CO_COPY, flags, CMD_CLONE_ORDER);
 

	
 
		/* Now clone the vehicle's name, if it has one. */
 
		if (v_front->name != NULL) CloneVehicleName(v_front, w_front);
 
	}
 

	
 
	/* Since we can't estimate the cost of cloning a vehicle accurately we must
 
	 * check whether the company has enough money manually. */
 
	if (!CheckCompanyHasMoney(total_cost)) {
 
		if (flags & DC_EXEC) {
 
			/* The vehicle has already been bought, so now it must be sold again. */
 
			DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
 
			DoCommand(w_front->tile, w_front->index | 1 << 16, 0, flags, GetCmdSellVeh(w_front));
 
		}
 
		return total_cost;
 
	}
 

	
 
	return total_cost;
 
}
 

	
 
/**
 
 * 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
0 comments (0 inline, 0 general)