Changeset - r13929:bc7dece1f96b
[Not reviewed]
master
0 2 0
frosch - 15 years ago 2009-12-12 22:15:14
frosch@openttd.org
(svn r18471) -Codechange/Fix: [NoAI] Deduplicate code betweeen AIVehicle::SkipToVehicleOrder and AIOrder::SkipToOrder. They are the same. Also ORDER_CURRENT was not allowed for the latter, but well...
2 files changed with 3 insertions and 5 deletions:
0 comments (0 inline, 0 general)
src/ai/api/ai_order.cpp
Show inline comments
 
@@ -376,48 +376,50 @@ static const Order *ResolveOrder(Vehicle
 
{
 
	/* IsValidVehicleOrder is not good enough because it does not allow appending. */
 
	if (order_position == ORDER_CURRENT) order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
 

	
 
	EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
 
	EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to));
 

	
 
	Order order;
 
	order.MakeConditional(jump_to);
 

	
 
	return AIObject::DoCommand(0, vehicle_id | (order_position << 16), order.Pack(), CMD_INSERT_ORDER);
 
}
 

	
 
/* static */ bool AIOrder::RemoveOrder(VehicleID vehicle_id, OrderPosition order_position)
 
{
 
	order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
 

	
 
	EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
 

	
 
	return AIObject::DoCommand(0, vehicle_id, order_position, CMD_DELETE_ORDER);
 
}
 

	
 
/* static */ bool AIOrder::SkipToOrder(VehicleID vehicle_id, OrderPosition next_order)
 
{
 
	next_order = AIOrder::ResolveOrderPosition(vehicle_id, next_order);
 

	
 
	EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, next_order));
 

	
 
	return AIObject::DoCommand(0, vehicle_id, next_order, CMD_SKIP_TO_ORDER);
 
}
 

	
 
/**
 
 * Callback handler as SetOrderFlags possibly needs multiple DoCommand calls
 
 * to be able to set all order flags correctly. As we need to wait till the
 
 * command has completed before we know the next bits to change we need to
 
 * call the function multiple times. Each time it'll reduce the difference
 
 * between the wanted and the current order.
 
 * @param instance The AI we are doing the callback for.
 
 */
 
static void _DoCommandReturnSetOrderFlags(class AIInstance *instance)
 
{
 
	AIObject::SetLastCommandRes(AIOrder::_SetOrderFlags());
 
	AIInstance::DoCommandReturn(instance);
 
}
 

	
 
/* static */ bool AIOrder::_SetOrderFlags()
 
{
 
	/* Make sure we don't go into an infinite loop */
 
	int retry = AIObject::GetCallbackVariable(3) - 1;
 
	if (retry < 0) {
src/ai/api/ai_vehicle.cpp
Show inline comments
 
@@ -174,53 +174,49 @@
 
	return AIObject::DoCommand(0, vehicle_id, DEPOT_SERVICE, GetCmdSendToDepot(::Vehicle::Get(vehicle_id)));
 
}
 

	
 
/* static */ bool AIVehicle::IsInDepot(VehicleID vehicle_id)
 
{
 
	if (!IsValidVehicle(vehicle_id)) return false;
 
	return ::Vehicle::Get(vehicle_id)->IsInDepot();
 
}
 

	
 
/* static */ bool AIVehicle::IsStoppedInDepot(VehicleID vehicle_id)
 
{
 
	if (!IsValidVehicle(vehicle_id)) return false;
 
	return ::Vehicle::Get(vehicle_id)->IsStoppedInDepot();
 
}
 

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

	
 
	return AIObject::DoCommand(0, vehicle_id, 0, CMD_START_STOP_VEHICLE);
 
}
 

	
 
/* static */ bool AIVehicle::SkipToVehicleOrder(VehicleID vehicle_id, AIOrder::OrderPosition order_position)
 
{
 
	order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
 

	
 
	EnforcePrecondition(false, AIOrder::IsValidVehicleOrder(vehicle_id, order_position));
 

	
 
	return AIObject::DoCommand(0, vehicle_id, order_position, CMD_SKIP_TO_ORDER);
 
	return AIOrder::SkipToOrder(vehicle_id, order_position);
 
}
 

	
 
/* static */ bool AIVehicle::ReverseVehicle(VehicleID vehicle_id)
 
{
 
	EnforcePrecondition(false, IsValidVehicle(vehicle_id));
 
	EnforcePrecondition(false, ::Vehicle::Get(vehicle_id)->type == VEH_ROAD || ::Vehicle::Get(vehicle_id)->type == VEH_TRAIN);
 

	
 
	switch (::Vehicle::Get(vehicle_id)->type) {
 
		case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, 0, CMD_TURN_ROADVEH);
 
		case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, 0, CMD_REVERSE_TRAIN_DIRECTION);
 
		default: NOT_REACHED();
 
	}
 
}
 

	
 
/* static */ bool AIVehicle::SetName(VehicleID vehicle_id, const char *name)
 
{
 
	EnforcePrecondition(false, IsValidVehicle(vehicle_id));
 
	EnforcePrecondition(false, !::StrEmpty(name));
 
	EnforcePreconditionCustomError(false, ::strlen(name) < MAX_LENGTH_VEHICLE_NAME_BYTES, AIError::ERR_PRECONDITION_STRING_TOO_LONG);
 

	
 
	return AIObject::DoCommand(0, vehicle_id, 0, CMD_RENAME_VEHICLE, name);
 
}
 

	
 
/* static */ TileIndex AIVehicle::GetLocation(VehicleID vehicle_id)
0 comments (0 inline, 0 general)