# ai_order.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_order.cpp 18667 2009-12-30 18:07:28Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "ai_vehicle.hpp"
    00013 #include "../ai_instance.hpp"
    00014 #include "../../debug.h"
    00015 #include "../../vehicle_base.h"
    00016 #include "../../roadstop_base.h"
    00017 #include "../../depot_base.h"
    00018 #include "../../station_base.h"
    00019 #include "../../waypoint_base.h"
    00020
    00026 static OrderType GetOrderTypeByTile(TileIndex t)
    00027 {
    00028   if (!::IsValidTile(t)) return OT_END;
    00029
    00030   switch (::GetTileType(t)) {
    00031     default: break;
    00032     case MP_STATION:
    00033       if (IsBuoy(t) || IsRailWaypoint(t)) return OT_GOTO_WAYPOINT;
    00034       if (IsHangar(t)) return OT_GOTO_DEPOT;
    00035       return OT_GOTO_STATION;
    00036       break;
    00037     case MP_WATER:   if (::IsShipDepot(t)) return OT_GOTO_DEPOT; break;
    00038     case MP_ROAD:    if (::GetRoadTileType(t) == ROAD_TILE_DEPOT) return OT_GOTO_DEPOT; break;
    00039     case MP_RAILWAY:
    00040       if (IsRailDepot(t)) return OT_GOTO_DEPOT;
    00041       break;
    00042   }
    00043
    00044   return OT_END;
    00045 }
    00046
    00047 /* static */ bool AIOrder::IsValidVehicleOrder(VehicleID vehicle_id, OrderPosition order_position)
    00048 {
    00049   return AIVehicle::IsValidVehicle(vehicle_id) && order_position >= 0 && (order_position < ::Vehicle::Get(vehicle_id)->GetNumOrders() || order_position == ORDER_CURRENT);
    00050 }
    00051
    00057 static const Order *ResolveOrder(VehicleID vehicle_id, AIOrder::OrderPosition order_position)
    00058 {
    00059   const Vehicle *v = ::Vehicle::Get(vehicle_id);
    00060   if (order_position == AIOrder::ORDER_CURRENT) {
    00061     const Order *order = &v->current_order;
    00062     if (order->GetType() == OT_GOTO_DEPOT && !(order->GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) return order;
    00063     order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
    00064     if (order_position == AIOrder::ORDER_INVALID) return NULL;
    00065   }
    00066   return v->GetOrder(order_position);
    00067 }
    00068
    00069 /* static */ bool AIOrder::IsGotoStationOrder(VehicleID vehicle_id, OrderPosition order_position)
    00070 {
    00071   if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
    00072
    00073   const Order *order = ::ResolveOrder(vehicle_id, order_position);
    00074   return order != NULL && order->GetType() == OT_GOTO_STATION;
    00075 }
    00076
    00077 /* static */ bool AIOrder::IsGotoDepotOrder(VehicleID vehicle_id, OrderPosition order_position)
    00078 {
    00079   if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
    00080
    00081   const Order *order = ::ResolveOrder(vehicle_id, order_position);
    00082   return order != NULL && order->GetType() == OT_GOTO_DEPOT;
    00083 }
    00084
    00085 /* static */ bool AIOrder::IsGotoWaypointOrder(VehicleID vehicle_id, OrderPosition order_position)
    00086 {
    00087   if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
    00088
    00089   const Order *order = ::ResolveOrder(vehicle_id, order_position);
    00090   return order != NULL && order->GetType() == OT_GOTO_WAYPOINT;
    00091 }
    00092
    00093 /* static */ bool AIOrder::IsConditionalOrder(VehicleID vehicle_id, OrderPosition order_position)
    00094 {
    00095   if (order_position == ORDER_CURRENT) return false;
    00096   if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
    00097
    00098   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
    00099   return order->GetType() == OT_CONDITIONAL;
    00100 }
    00101
    00102 /* static */ bool AIOrder::IsCurrentOrderPartOfOrderList(VehicleID vehicle_id)
    00103 {
    00104   if (AIVehicle::IsValidVehicle(vehicle_id)) return false;
    00105   if (GetOrderCount(vehicle_id) == 0) return false;
    00106
    00107   const Order *order = &::Vehicle::Get(vehicle_id)->current_order;
    00108   if (order->GetType() != OT_GOTO_DEPOT) return true;
    00109   return (order->GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0;
    00110 }
    00111
    00112 /* static */ AIOrder::OrderPosition AIOrder::ResolveOrderPosition(VehicleID vehicle_id, OrderPosition order_position)
    00113 {
    00114   if (!AIVehicle::IsValidVehicle(vehicle_id)) return ORDER_INVALID;
    00115
    00116   if (order_position == ORDER_CURRENT) return (AIOrder::OrderPosition)::Vehicle::Get(vehicle_id)->cur_order_index;
    00117   return (order_position >= 0 && order_position < ::Vehicle::Get(vehicle_id)->GetNumOrders()) ? order_position : ORDER_INVALID;
    00118 }
    00119
    00120
    00121 /* static */ bool AIOrder::AreOrderFlagsValid(TileIndex destination, AIOrderFlags order_flags)
    00122 {
    00123   OrderType ot = (order_flags & AIOF_GOTO_NEAREST_DEPOT) ? OT_GOTO_DEPOT : ::GetOrderTypeByTile(destination);
    00124   switch (ot) {
    00125     case OT_GOTO_STATION:
    00126       return (order_flags & ~(AIOF_NON_STOP_FLAGS | AIOF_UNLOAD_FLAGS | AIOF_LOAD_FLAGS)) == 0 &&
    00127           /* Test the different mutual exclusive flags. */
    00128           ((order_flags & AIOF_TRANSFER)      == 0 || (order_flags & AIOF_UNLOAD)    == 0) &&
    00129           ((order_flags & AIOF_TRANSFER)      == 0 || (order_flags & AIOF_NO_UNLOAD) == 0) &&
    00130           ((order_flags & AIOF_UNLOAD)        == 0 || (order_flags & AIOF_NO_UNLOAD) == 0) &&
    00131           ((order_flags & AIOF_UNLOAD)        == 0 || (order_flags & AIOF_NO_UNLOAD) == 0) &&
    00132           ((order_flags & AIOF_NO_UNLOAD)     == 0 || (order_flags & AIOF_NO_LOAD)   == 0) &&
    00133           ((order_flags & AIOF_FULL_LOAD_ANY) == 0 || (order_flags & AIOF_NO_LOAD)   == 0);
    00134
    00135     case OT_GOTO_DEPOT:
    00136       return (order_flags & ~(AIOF_NON_STOP_FLAGS | AIOF_DEPOT_FLAGS)) == 0 &&
    00137           ((order_flags & AIOF_SERVICE_IF_NEEDED) == 0 || (order_flags & AIOF_STOP_IN_DEPOT) == 0);
    00138
    00139     case OT_GOTO_WAYPOINT: return (order_flags & ~(AIOF_NON_STOP_FLAGS)) == 0;
    00140     default:               return false;
    00141   }
    00142 }
    00143
    00144 /* static */ bool AIOrder::IsValidConditionalOrder(OrderCondition condition, CompareFunction compare)
    00145 {
    00146   switch (condition) {
    00147     case OC_LOAD_PERCENTAGE:
    00148     case OC_RELIABILITY:
    00149     case OC_MAX_SPEED:
    00150     case OC_AGE:
    00151       return compare >= CF_EQUALS && compare <= CF_MORE_EQUALS;
    00152
    00153     case OC_REQUIRES_SERVICE:
    00154       return compare == CF_IS_TRUE || compare == CF_IS_FALSE;
    00155
    00156     case OC_UNCONDITIONALLY:
    00157       return true;
    00158
    00159     default: return false;
    00160   }
    00161 }
    00162
    00163 /* static */ int32 AIOrder::GetOrderCount(VehicleID vehicle_id)
    00164 {
    00165   return AIVehicle::IsValidVehicle(vehicle_id) ? ::Vehicle::Get(vehicle_id)->GetNumOrders() : -1;
    00166 }
    00167
    00168 /* static */ TileIndex AIOrder::GetOrderDestination(VehicleID vehicle_id, OrderPosition order_position)
    00169 {
    00170   if (!IsValidVehicleOrder(vehicle_id, order_position)) return INVALID_TILE;
    00171
    00172   const Order *order = ::ResolveOrder(vehicle_id, order_position);
    00173   if (order == NULL || order->GetType() == OT_CONDITIONAL) return INVALID_TILE;
    00174   const Vehicle *v = ::Vehicle::Get(vehicle_id);
    00175
    00176   switch (order->GetType()) {
    00177     case OT_GOTO_DEPOT: {
    00178       /* We don't know where the nearest depot is... (yet) */
    00179       if (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) return INVALID_TILE;
    00180
    00181       if (v->type != VEH_AIRCRAFT) return ::Depot::Get(order->GetDestination())->xy;
    00182       /* Aircraft's hangars are referenced by StationID, not DepotID */
    00183       const Station *st = ::Station::Get(order->GetDestination());
    00184       const AirportFTAClass *airport = st->Airport();
    00185       if (airport == NULL || airport->nof_depots == 0) return INVALID_TILE;
    00186       return st->airport_tile + ::ToTileIndexDiff(st->Airport()->airport_depots[0]);
    00187     }
    00188
    00189     case OT_GOTO_STATION: {
    00190       const Station *st = ::Station::Get(order->GetDestination());
    00191       if (st->train_station.tile != INVALID_TILE) {
    00192         for (uint i = 0; i < st->train_station.w; i++) {
    00193           TileIndex t = st->train_station.tile + TileDiffXY(i, 0);
    00194           if (st->TileBelongsToRailStation(t)) return t;
    00195         }
    00196       } else if (st->dock_tile != INVALID_TILE) {
    00197         return st->dock_tile;
    00198       } else if (st->bus_stops != NULL) {
    00199         return st->bus_stops->xy;
    00200       } else if (st->truck_stops != NULL) {
    00201         return st->truck_stops->xy;
    00202       } else if (st->airport_tile != INVALID_TILE) {
    00203         const AirportFTAClass *fta = st->Airport();
    00204         TILE_LOOP(tile, fta->size_x, fta->size_y, st->airport_tile) {
    00205           if (!::IsHangar(tile)) return tile;
    00206         }
    00207       }
    00208       return INVALID_TILE;
    00209     }
    00210     case OT_GOTO_WAYPOINT: return ::Waypoint::Get(order->GetDestination())->xy;
    00211     default:               return INVALID_TILE;
    00212   }
    00213 }
    00214
    00215 /* static */ AIOrder::AIOrderFlags AIOrder::GetOrderFlags(VehicleID vehicle_id, OrderPosition order_position)
    00216 {
    00217   if (!IsValidVehicleOrder(vehicle_id, order_position)) return AIOF_INVALID;
    00218
    00219   const Order *order = ::ResolveOrder(vehicle_id, order_position);
    00220   if (order == NULL || order->GetType() == OT_CONDITIONAL) return AIOF_INVALID;
    00221
    00222   AIOrderFlags order_flags = AIOF_NONE;
    00223   order_flags |= (AIOrderFlags)order->GetNonStopType();
    00224   switch (order->GetType()) {
    00225     case OT_GOTO_DEPOT:
    00226       if (order->GetDepotOrderType() & ODTFB_SERVICE) order_flags |= AIOF_SERVICE_IF_NEEDED;
    00227       if (order->GetDepotActionType() & ODATFB_HALT) order_flags |= AIOF_STOP_IN_DEPOT;
    00228       if (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) order_flags |= AIOF_GOTO_NEAREST_DEPOT;
    00229       break;
    00230
    00231     case OT_GOTO_STATION:
    00232       order_flags |= (AIOrderFlags)(order->GetLoadType()   << 5);
    00233       order_flags |= (AIOrderFlags)(order->GetUnloadType() << 2);
    00234       break;
    00235
    00236     default: break;
    00237   }
    00238
    00239   return order_flags;
    00240 }
    00241
    00242 /* static */ AIOrder::OrderPosition AIOrder::GetOrderJumpTo(VehicleID vehicle_id, OrderPosition order_position)
    00243 {
    00244   if (!IsValidVehicleOrder(vehicle_id, order_position)) return ORDER_INVALID;
    00245   if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return ORDER_INVALID;
    00246
    00247   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
    00248   return (OrderPosition)order->GetConditionSkipToOrder();
    00249 }
    00250
    00251 /* static */ AIOrder::OrderCondition AIOrder::GetOrderCondition(VehicleID vehicle_id, OrderPosition order_position)
    00252 {
    00253   if (!IsValidVehicleOrder(vehicle_id, order_position)) return OC_INVALID;
    00254   if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return OC_INVALID;
    00255
    00256   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
    00257   return (OrderCondition)order->GetConditionVariable();
    00258 }
    00259
    00260 /* static */ AIOrder::CompareFunction AIOrder::GetOrderCompareFunction(VehicleID vehicle_id, OrderPosition order_position)
    00261 {
    00262   if (!IsValidVehicleOrder(vehicle_id, order_position)) return CF_INVALID;
    00263   if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return CF_INVALID;
    00264
    00265   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
    00266   return (CompareFunction)order->GetConditionComparator();
    00267 }
    00268
    00269 /* static */ int32 AIOrder::GetOrderCompareValue(VehicleID vehicle_id, OrderPosition order_position)
    00270 {
    00271   if (!IsValidVehicleOrder(vehicle_id, order_position)) return -1;
    00272   if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return -1;
    00273
    00274   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
    00275   int32 value = order->GetConditionValue();
    00276   if (order->GetConditionVariable() == OCV_MAX_SPEED) value = value * 16 / 10;
    00277   return value;
    00278 }
    00279
    00280 /* static */ bool AIOrder::SetOrderJumpTo(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
    00281 {
    00282   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
    00283   EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
    00284   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to) && jump_to != ORDER_CURRENT);
    00285
    00286   return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_DESTINATION | (jump_to << 4), CMD_MODIFY_ORDER);
    00287 }
    00288
    00289 /* static */ bool AIOrder::SetOrderCondition(VehicleID vehicle_id, OrderPosition order_position, OrderCondition condition)
    00290 {
    00291   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
    00292   EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
    00293   EnforcePrecondition(false, condition >= OC_LOAD_PERCENTAGE && condition <= OC_UNCONDITIONALLY);
    00294
    00295   return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_VARIABLE | (condition << 4), CMD_MODIFY_ORDER);
    00296 }
    00297
    00298 /* static */ bool AIOrder::SetOrderCompareFunction(VehicleID vehicle_id, OrderPosition order_position, CompareFunction compare)
    00299 {
    00300   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
    00301   EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
    00302   EnforcePrecondition(false, compare >= CF_EQUALS && compare <= CF_IS_FALSE);
    00303
    00304   return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_COMPARATOR | (compare << 4), CMD_MODIFY_ORDER);
    00305 }
    00306
    00307 /* static */ bool AIOrder::SetOrderCompareValue(VehicleID vehicle_id, OrderPosition order_position, int32 value)
    00308 {
    00309   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
    00310   EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
    00311   EnforcePrecondition(false, value >= 0 && value < 2048);
    00312   if (GetOrderCondition(vehicle_id, order_position) == OC_MAX_SPEED) value = value * 10 / 16;
    00313
    00314   return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_VALUE | (value << 4), CMD_MODIFY_ORDER);
    00315 }
    00316
    00317 /* static */ bool AIOrder::AppendOrder(VehicleID vehicle_id, TileIndex destination, AIOrderFlags order_flags)
    00318 {
    00319   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
    00320   EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
    00321
    00322   return InsertOrder(vehicle_id, (AIOrder::OrderPosition)::Vehicle::Get(vehicle_id)->GetNumOrders(), destination, order_flags);
    00323 }
    00324
    00325 /* static */ bool AIOrder::AppendConditionalOrder(VehicleID vehicle_id, OrderPosition jump_to)
    00326 {
    00327   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
    00328   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to));
    00329
    00330   return InsertConditionalOrder(vehicle_id, (AIOrder::OrderPosition)::Vehicle::Get(vehicle_id)->GetNumOrders(), jump_to);
    00331 }
    00332
    00333 /* static */ bool AIOrder::InsertOrder(VehicleID vehicle_id, OrderPosition order_position, TileIndex destination, AIOrder::AIOrderFlags order_flags)
    00334 {
    00335   /* IsValidVehicleOrder is not good enough because it does not allow appending. */
    00336   if (order_position == ORDER_CURRENT) order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
    00337
    00338   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
    00339   EnforcePrecondition(false, order_position >= 0 && order_position <= ::Vehicle::Get(vehicle_id)->GetNumOrders());
    00340   EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
    00341
    00342   Order order;
    00343   OrderType ot = (order_flags & AIOF_GOTO_NEAREST_DEPOT) ? OT_GOTO_DEPOT : ::GetOrderTypeByTile(destination);
    00344   switch (ot) {
    00345     case OT_GOTO_DEPOT: {
    00346       OrderDepotTypeFlags odtf = (OrderDepotTypeFlags)(ODTFB_PART_OF_ORDERS | ((order_flags & AIOF_SERVICE_IF_NEEDED) ? ODTFB_SERVICE : 0));
    00347       OrderDepotActionFlags odaf = (OrderDepotActionFlags)(ODATF_SERVICE_ONLY | ((order_flags & AIOF_STOP_IN_DEPOT) ? ODATFB_HALT : 0));
    00348       if (order_flags & AIOF_GOTO_NEAREST_DEPOT) odaf |= ODATFB_NEAREST_DEPOT;
    00349       OrderNonStopFlags onsf = (OrderNonStopFlags)((order_flags & AIOF_NON_STOP_INTERMEDIATE) ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
    00350       if (order_flags & AIOF_GOTO_NEAREST_DEPOT) {
    00351         order.MakeGoToDepot(0, odtf, onsf, odaf);
    00352       } else {
    00353         /* Check explicitly if the order is to a station (for aircraft) or
    00354          * to a depot (other vehicle types). */
    00355         if (::Vehicle::Get(vehicle_id)->type == VEH_AIRCRAFT) {
    00356           if (!::IsTileType(destination, MP_STATION)) return false;
    00357           order.MakeGoToDepot(::GetStationIndex(destination), odtf, onsf, odaf);
    00358         } else {
    00359           if (::IsTileType(destination, MP_STATION)) return false;
    00360           order.MakeGoToDepot(::GetDepotIndex(destination), odtf, onsf, odaf);
    00361         }
    00362       }
    00363       break;
    00364     }
    00365
    00366     case OT_GOTO_STATION:
    00367       order.MakeGoToStation(::GetStationIndex(destination));
    00368       order.SetLoadType((OrderLoadFlags)GB(order_flags, 5, 3));
    00369       order.SetUnloadType((OrderUnloadFlags)GB(order_flags, 2, 3));
    00370       order.SetStopLocation(OSL_PLATFORM_FAR_END);
    00371       break;
    00372
    00373     case OT_GOTO_WAYPOINT:
    00374       order.MakeGoToWaypoint(::GetStationIndex(destination));
    00375       break;
    00376
    00377     default:
    00378       return false;
    00379   }
    00380
    00381   order.SetNonStopType((OrderNonStopFlags)GB(order_flags, 0, 2));
    00382
    00383   return AIObject::DoCommand(0, vehicle_id | (order_position << 16), order.Pack(), CMD_INSERT_ORDER);
    00384 }
    00385
    00386 /* static */ bool AIOrder::InsertConditionalOrder(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
    00387 {
    00388   /* IsValidVehicleOrder is not good enough because it does not allow appending. */
    00389   if (order_position == ORDER_CURRENT) order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
    00390
    00391   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
    00392   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to));
    00393
    00394   Order order;
    00395   order.MakeConditional(jump_to);
    00396
    00397   return AIObject::DoCommand(0, vehicle_id | (order_position << 16), order.Pack(), CMD_INSERT_ORDER);
    00398 }
    00399
    00400 /* static */ bool AIOrder::RemoveOrder(VehicleID vehicle_id, OrderPosition order_position)
    00401 {
    00402   order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
    00403
    00404   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
    00405
    00406   return AIObject::DoCommand(0, vehicle_id, order_position, CMD_DELETE_ORDER);
    00407 }
    00408
    00409 /* static */ bool AIOrder::SkipToOrder(VehicleID vehicle_id, OrderPosition next_order)
    00410 {
    00411   next_order = AIOrder::ResolveOrderPosition(vehicle_id, next_order);
    00412
    00413   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, next_order));
    00414
    00415   return AIObject::DoCommand(0, vehicle_id, next_order, CMD_SKIP_TO_ORDER);
    00416 }
    00417
    00426 static void _DoCommandReturnSetOrderFlags(class AIInstance *instance)
    00427 {
    00428   AIObject::SetLastCommandRes(AIOrder::_SetOrderFlags());
    00429   AIInstance::DoCommandReturn(instance);
    00430 }
    00431
    00432 /* static */ bool AIOrder::_SetOrderFlags()
    00433 {
    00434   /* Make sure we don't go into an infinite loop */
    00435   int retry = AIObject::GetCallbackVariable(3) - 1;
    00436   if (retry < 0) {
    00437     DEBUG(ai, 0, "Possible infinite loop in SetOrderFlags() detected");
    00438     return false;
    00439   }
    00440   AIObject::SetCallbackVariable(3, retry);
    00441
    00442   VehicleID vehicle_id = (VehicleID)AIObject::GetCallbackVariable(0);
    00443   OrderPosition order_position = (OrderPosition)AIObject::GetCallbackVariable(1);
    00444   AIOrderFlags order_flags = (AIOrderFlags)AIObject::GetCallbackVariable(2);
    00445
    00446   order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
    00447
    00448   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
    00449   EnforcePrecondition(false, AreOrderFlagsValid(GetOrderDestination(vehicle_id, order_position), order_flags));
    00450
    00451   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
    00452
    00453   AIOrderFlags current = GetOrderFlags(vehicle_id, order_position);
    00454
    00455   if ((current & AIOF_NON_STOP_FLAGS) != (order_flags & AIOF_NON_STOP_FLAGS)) {
    00456     return AIObject::DoCommand(0, vehicle_id | (order_position << 16), (order_flags & AIOF_NON_STOP_FLAGS) << 4 | MOF_NON_STOP, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
    00457   }
    00458
    00459   switch (order->GetType()) {
    00460     case OT_GOTO_DEPOT:
    00461       if ((current & AIOF_DEPOT_FLAGS) != (order_flags & AIOF_DEPOT_FLAGS)) {
    00462         uint data = DA_ALWAYS_GO;
    00463         if (order_flags & AIOF_SERVICE_IF_NEEDED) data = DA_SERVICE;
    00464         if (order_flags & AIOF_STOP_IN_DEPOT) data = DA_STOP;
    00465         return AIObject::DoCommand(0, vehicle_id | (order_position << 16), (data << 4) | MOF_DEPOT_ACTION, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
    00466       }
    00467       break;
    00468
    00469     case OT_GOTO_STATION:
    00470       if ((current & AIOF_UNLOAD_FLAGS) != (order_flags & AIOF_UNLOAD_FLAGS)) {
    00471         return AIObject::DoCommand(0, vehicle_id | (order_position << 16), (order_flags & AIOF_UNLOAD_FLAGS) << 2 | MOF_UNLOAD, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
    00472       }
    00473       if ((current & AIOF_LOAD_FLAGS) != (order_flags & AIOF_LOAD_FLAGS)) {
    00474         return AIObject::DoCommand(0, vehicle_id | (order_position << 16), (order_flags & AIOF_LOAD_FLAGS) >> 1 | MOF_LOAD, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
    00475       }
    00476       break;
    00477
    00478     default: break;
    00479   }
    00480
    00481   assert(GetOrderFlags(vehicle_id, order_position) == order_flags);
    00482
    00483   return true;
    00484 }
    00485
    00486 /* static */ bool AIOrder::SetOrderFlags(VehicleID vehicle_id, OrderPosition order_position, AIOrder::AIOrderFlags order_flags)
    00487 {
    00488   AIObject::SetCallbackVariable(0, vehicle_id);
    00489   AIObject::SetCallbackVariable(1, order_position);
    00490   AIObject::SetCallbackVariable(2, order_flags);
    00491   /* In case another client(s) change orders at the same time we could
    00492    * end in an infinite loop. This stops that from happening ever. */
    00493   AIObject::SetCallbackVariable(3, 8);
    00494   return AIOrder::_SetOrderFlags();
    00495 }
    00496
    00497 /* static */ bool AIOrder::MoveOrder(VehicleID vehicle_id, OrderPosition order_position_move, OrderPosition order_position_target)
    00498 {
    00499   order_position_move   = AIOrder::ResolveOrderPosition(vehicle_id, order_position_move);
    00500   order_position_target = AIOrder::ResolveOrderPosition(vehicle_id, order_position_target);
    00501
    00502   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position_move));
    00503   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position_target));
    00504
    00505   return AIObject::DoCommand(0, vehicle_id, order_position_move | (order_position_target << 16), CMD_MOVE_ORDER);
    00506 }
    00507
    00508 /* static */ bool AIOrder::CopyOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
    00509 {
    00510   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
    00511   EnforcePrecondition(false, AIVehicle::IsValidVehicle(main_vehicle_id));
    00512
    00513   return AIObject::DoCommand(0, vehicle_id | (main_vehicle_id << 16), CO_COPY, CMD_CLONE_ORDER);
    00514 }
    00515
    00516 /* static */ bool AIOrder::ShareOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
    00517 {
    00518   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
    00519   EnforcePrecondition(false, AIVehicle::IsValidVehicle(main_vehicle_id));
    00520
    00521   return AIObject::DoCommand(0, vehicle_id | (main_vehicle_id << 16), CO_SHARE, CMD_CLONE_ORDER);
    00522 }
    00523
    00524 /* static */ bool AIOrder::UnshareOrders(VehicleID vehicle_id)
    00525 {
    00526   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
    00527
    00528   return AIObject::DoCommand(0, vehicle_id, CO_UNSHARE, CMD_CLONE_ORDER);
    00529 }