/* $Id$ */
/** @file ai_order.cpp Implementation of AIOrder. */
#include "ai_order.hpp"
#include "ai_map.hpp"
#include "ai_vehicle.hpp"
#include "../../command_type.h"
#include "../../vehicle_base.h"
#include "../../depot.h"
#include "../../landscape.h"
#include "../../rail_map.h"
#include "../../road_map.h"
#include "../../station_map.h"
#include "../../water_map.h"
#include "../../waypoint.h"
/**
* Gets the order type given a tile
* @param t the tile to get the order from
* @return the order type, or OT_END when there is no order
*/
static OrderType GetOrderTypeByTile(TileIndex t)
{
if (!::IsValidTile(t)) return OT_END;
switch (::GetTileType(t)) {
default: break;
case MP_STATION: return OT_GOTO_STATION; break;
case MP_WATER: if (::IsShipDepot(t)) return OT_GOTO_DEPOT; break;
case MP_ROAD: if (::GetRoadTileType(t) == ROAD_TILE_DEPOT) return OT_GOTO_DEPOT; break;
case MP_RAILWAY:
switch (::GetRailTileType(t)) {
case RAIL_TILE_DEPOT: return OT_GOTO_DEPOT;
case RAIL_TILE_WAYPOINT: return OT_GOTO_WAYPOINT;
default: break;
}
break;
}
return OT_END;
}
/* static */ bool AIOrder::IsValidVehicleOrder(VehicleID vehicle_id, uint32 order_id)
{
return AIVehicle::IsValidVehicle(vehicle_id) && order_id < ::GetVehicle(vehicle_id)->num_orders;
}
/* static */ bool AIOrder::AreOrderFlagsValid(TileIndex destination, AIOrderFlags order_flags)
{
switch (::GetOrderTypeByTile(destination)) {
case OT_GOTO_DEPOT: return (order_flags & ~(AIOF_NON_STOP | AIOF_SERVICE_IF_NEEDED)) == 0;
case OT_GOTO_STATION: return (order_flags & ~(AIOF_NON_STOP | AIOF_TRANSFER | AIOF_UNLOAD | AIOF_FULL_LOAD)) == 0 && ((order_flags & AIOF_UNLOAD) == 0 || (order_flags & AIOF_FULL_LOAD) == 0);
case OT_GOTO_WAYPOINT: return (order_flags & ~(AIOF_NON_STOP)) == 0;
default: return false;
}
}
/* static */ int32 AIOrder::GetNumberOfOrders(VehicleID vehicle_id)
{
return AIVehicle::IsValidVehicle(vehicle_id) ? ::GetVehicle(vehicle_id)->num_orders : -1;
}
/* static */ TileIndex AIOrder::GetOrderDestination(VehicleID vehicle_id, uint32 order_id)
{
if (!IsValidVehicleOrder(vehicle_id, order_id)) return INVALID_TILE;
Order *order = ::GetVehicle(vehicle_id)->orders;
for (uint i = 0; i < order_id; i++) order = order->next;
switch (order->GetType()) {
case OT_GOTO_DEPOT: return ::GetDepot(order->GetDestination())->xy;
case OT_GOTO_STATION: return ::GetStation(order->GetDestination())->xy;
case OT_GOTO_WAYPOINT: return ::GetWaypoint(order->GetDestination())->xy;
default: return INVALID_TILE;
}
}
/* static */ AIOrder::AIOrderFlags AIOrder::GetOrderFlags(VehicleID vehicle_id, uint32 order_id)
{
if (!IsValidVehicleOrder(vehicle_id, order_id)) return AIOF_INVALID;
Order *order = ::GetVehicle(vehicle_id)->orders;
for (uint i = 0; i < order_id; i++) order = order->next;
AIOrderFlags order_flags= AIOF_NONE;
if (order->GetNonStopType() != OFB_NO_NON_STOP) order_flags |= AIOF_NON_STOP;
switch (order->GetType()) {
case OT_GOTO_DEPOT:
if (order->GetDepotOrderType() & OFB_SERVICE_IF_NEEDED) order_flags |= AIOF_SERVICE_IF_NEEDED;
break;
case OT_GOTO_STATION:
if (order->GetLoadType() & OFB_FULL_LOAD) order_flags |= AIOF_FULL_LOAD;
if (order->GetUnloadType() & OFB_UNLOAD) order_flags |= AIOF_UNLOAD;
if (order->GetUnloadType() & OFB_TRANSFER) order_flags |= AIOF_TRANSFER;
break;
default: break;
}
return order_flags;
}
/* static */ bool AIOrder::AppendOrder(VehicleID vehicle_id, TileIndex destination, AIOrderFlags order_flags)
{
EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
return InsertOrder(vehicle_id, ::GetVehicle(vehicle_id)->num_orders, destination, order_flags);
}
/* static */ bool AIOrder::InsertOrder(VehicleID vehicle_id, uint32 order_id, TileIndex destination, AIOrder::AIOrderFlags order_flags)
{
/* IsValidVehicleOrder is not good enough because it does not allow appending. */
EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
EnforcePrecondition(false, order_id <= ::GetVehicle(vehicle_id)->num_orders);
EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
Order order;
switch (::GetOrderTypeByTile(destination)) {
case OT_GOTO_DEPOT:
order.MakeGoToDepot(::GetDepotByTile(destination)->index, true);
if (order_flags & AIOF_SERVICE_IF_NEEDED) order.SetDepotOrderType(OFB_SERVICE_IF_NEEDED);
break;
case OT_GOTO_STATION:
order.MakeGoToStation(::GetStationIndex(destination));
if (order_flags & AIOF_FULL_LOAD) order.SetLoadType(OFB_FULL_LOAD);
order.SetUnloadType(((order_flags & AIOF_TRANSFER) ? OFB_TRANSFER : 0) | ((order_flags & AIOF_UNLOAD) ? OFB_UNLOAD : 0));
break;
case OT_GOTO_WAYPOINT:
order.MakeGoToWaypoint(::GetWaypointIndex(destination));
break;
default:
return false;
}
if (order_flags & AIOF_NON_STOP) order.SetNonStopType(OFB_NON_STOP);
return AIObject::DoCommand(0, vehicle_id | (order_id << 16), order.Pack(), CMD_INSERT_ORDER);
}
/* static */ bool AIOrder::RemoveOrder(VehicleID vehicle_id, uint32 order_id)
{
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_id));
return AIObject::DoCommand(0, vehicle_id, order_id, CMD_DELETE_ORDER);
}
/* static */ bool AIOrder::ChangeOrder(VehicleID vehicle_id, uint32 order_id, AIOrder::AIOrderFlags order_flags)
{
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_id));
EnforcePrecondition(false, AreOrderFlagsValid(GetOrderDestination(vehicle_id, order_id), order_flags));
for (;;) {
/* Loop as long as there is a difference between the requested and
* the current orders of the vehicle. */
AIOrderFlags current = GetOrderFlags(vehicle_id, order_id);
AIOrderFlags diff = current ^ order_flags;
if (diff == 0) return true;
bool res = AIObject::DoCommand(0, vehicle_id | (order_id << 16), FIND_FIRST_BIT(diff), CMD_MODIFY_ORDER);
if (!res) return false;
}
}
/* static */ bool AIOrder::MoveOrder(VehicleID vehicle_id, uint32 order_id_move, uint32 order_id_target)
{
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_id_move));
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_id_target));
return AIObject::DoCommand(0, vehicle_id, order_id_move | (order_id_target << 16), CMD_MOVE_ORDER);
}
/* static */ bool AIOrder::CopyOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
{
EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
EnforcePrecondition(false, AIVehicle::IsValidVehicle(main_vehicle_id));
return AIObject::DoCommand(0, vehicle_id | (main_vehicle_id << 16), CO_COPY, CMD_CLONE_ORDER);
}
/* static */ bool AIOrder::ShareOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
{
EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
EnforcePrecondition(false, AIVehicle::IsValidVehicle(main_vehicle_id));
return AIObject::DoCommand(0, vehicle_id | (main_vehicle_id << 16), CO_SHARE, CMD_CLONE_ORDER);
}
/* static */ bool AIOrder::UnshareOrders(VehicleID vehicle_id)
{
EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
return AIObject::DoCommand(0, vehicle_id, CO_UNSHARE, CMD_CLONE_ORDER);
}