# ai_order.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_order.cpp 18860 2010-01-18 14:32:47Z yexo $ */
    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       if (st->GetAirportSpec()->nof_depots == 0) return INVALID_TILE;
    00185       return st->GetHangarTile(0);
    00186     }
    00187
    00188     case OT_GOTO_STATION: {
    00189       const Station *st = ::Station::Get(order->GetDestination());
    00190       if (st->train_station.tile != INVALID_TILE) {
    00191         for (uint i = 0; i < st->train_station.w; i++) {
    00192           TileIndex t = st->train_station.tile + TileDiffXY(i, 0);
    00193           if (st->TileBelongsToRailStation(t)) return t;
    00194         }
    00195       } else if (st->dock_tile != INVALID_TILE) {
    00196         return st->dock_tile;
    00197       } else if (st->bus_stops != NULL) {
    00198         return st->bus_stops->xy;
    00199       } else if (st->truck_stops != NULL) {
    00200         return st->truck_stops->xy;
    00201       } else if (st->airport_tile != INVALID_TILE) {
    00202         const AirportSpec *as = st->GetAirportSpec();
    00203         TILE_LOOP(tile, as->size_x, as->size_y, st->airport_tile) {
    00204           if (!::IsHangar(tile)) return tile;
    00205         }
    00206       }
    00207       return INVALID_TILE;
    00208     }
    00209     case OT_GOTO_WAYPOINT: return ::Waypoint::Get(order->GetDestination())->xy;
    00210     default:               return INVALID_TILE;
    00211   }
    00212 }
    00213
    00214 /* static */ AIOrder::AIOrderFlags AIOrder::GetOrderFlags(VehicleID vehicle_id, OrderPosition order_position)
    00215 {
    00216   if (!IsValidVehicleOrder(vehicle_id, order_position)) return AIOF_INVALID;
    00217
    00218   const Order *order = ::ResolveOrder(vehicle_id, order_position);
    00219   if (order == NULL || order->GetType() == OT_CONDITIONAL) return AIOF_INVALID;
    00220
    00221   AIOrderFlags order_flags = AIOF_NONE;
    00222   order_flags |= (AIOrderFlags)order->GetNonStopType();
    00223   switch (order->GetType()) {
    00224     case OT_GOTO_DEPOT:
    00225       if (order->GetDepotOrderType() & ODTFB_SERVICE) order_flags |= AIOF_SERVICE_IF_NEEDED;
    00226       if (order->GetDepotActionType() & ODATFB_HALT) order_flags |= AIOF_STOP_IN_DEPOT;
    00227       if (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) order_flags |= AIOF_GOTO_NEAREST_DEPOT;
    00228       break;
    00229
    00230     case OT_GOTO_STATION:
    00231       order_flags |= (AIOrderFlags)(order->GetLoadType()   << 5);
    00232       order_flags |= (AIOrderFlags)(order->GetUnloadType() << 2);
    00233       break;
    00234
    00235     default: break;
    00236   }
    00237
    00238   return order_flags;
    00239 }
    00240
    00241 /* static */ AIOrder::OrderPosition AIOrder::GetOrderJumpTo(VehicleID vehicle_id, OrderPosition order_position)
    00242 {
    00243   if (!IsValidVehicleOrder(vehicle_id, order_position)) return ORDER_INVALID;
    00244   if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return ORDER_INVALID;
    00245
    00246   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
    00247   return (OrderPosition)order->GetConditionSkipToOrder();
    00248 }
    00249
    00250 /* static */ AIOrder::OrderCondition AIOrder::GetOrderCondition(VehicleID vehicle_id, OrderPosition order_position)
    00251 {
    00252   if (!IsValidVehicleOrder(vehicle_id, order_position)) return OC_INVALID;
    00253   if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return OC_INVALID;
    00254
    00255   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
    00256   return (OrderCondition)order->GetConditionVariable();
    00257 }
    00258
    00259 /* static */ AIOrder::CompareFunction AIOrder::GetOrderCompareFunction(VehicleID vehicle_id, OrderPosition order_position)
    00260 {
    00261   if (!IsValidVehicleOrder(vehicle_id, order_position)) return CF_INVALID;
    00262   if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return CF_INVALID;
    00263
    00264   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
    00265   return (CompareFunction)order->GetConditionComparator();
    00266 }
    00267
    00268 /* static */ int32 AIOrder::GetOrderCompareValue(VehicleID vehicle_id, OrderPosition order_position)
    00269 {
    00270   if (!IsValidVehicleOrder(vehicle_id, order_position)) return -1;
    00271   if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return -1;
    00272
    00273   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
    00274   int32 value = order->GetConditionValue();
    00275   if (order->GetConditionVariable() == OCV_MAX_SPEED) value = value * 16 / 10;
    00276   return value;
    00277 }
    00278
    00279 /* static */ bool AIOrder::SetOrderJumpTo(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
    00280 {
    00281   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
    00282   EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
    00283   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to) && jump_to != ORDER_CURRENT);
    00284
    00285   return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_DESTINATION | (jump_to << 4), CMD_MODIFY_ORDER);
    00286 }
    00287
    00288 /* static */ bool AIOrder::SetOrderCondition(VehicleID vehicle_id, OrderPosition order_position, OrderCondition condition)
    00289 {
    00290   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
    00291   EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
    00292   EnforcePrecondition(false, condition >= OC_LOAD_PERCENTAGE && condition <= OC_UNCONDITIONALLY);
    00293
    00294   return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_VARIABLE | (condition << 4), CMD_MODIFY_ORDER);
    00295 }
    00296
    00297 /* static */ bool AIOrder::SetOrderCompareFunction(VehicleID vehicle_id, OrderPosition order_position, CompareFunction compare)
    00298 {
    00299   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
    00300   EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
    00301   EnforcePrecondition(false, compare >= CF_EQUALS && compare <= CF_IS_FALSE);
    00302
    00303   return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_COMPARATOR | (compare << 4), CMD_MODIFY_ORDER);
    00304 }
    00305
    00306 /* static */ bool AIOrder::SetOrderCompareValue(VehicleID vehicle_id, OrderPosition order_position, int32 value)
    00307 {
    00308   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
    00309   EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
    00310   EnforcePrecondition(false, value >= 0 && value < 2048);
    00311   if (GetOrderCondition(vehicle_id, order_position) == OC_MAX_SPEED) value = value * 10 / 16;
    00312
    00313   return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_VALUE | (value << 4), CMD_MODIFY_ORDER);
    00314 }
    00315
    00316 /* static */ bool AIOrder::AppendOrder(VehicleID vehicle_id, TileIndex destination, AIOrderFlags order_flags)
    00317 {
    00318   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
    00319   EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
    00320
    00321   return InsertOrder(vehicle_id, (AIOrder::OrderPosition)::Vehicle::Get(vehicle_id)->GetNumOrders(), destination, order_flags);
    00322 }
    00323
    00324 /* static */ bool AIOrder::AppendConditionalOrder(VehicleID vehicle_id, OrderPosition jump_to)
    00325 {
    00326   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
    00327   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to));
    00328
    00329   return InsertConditionalOrder(vehicle_id, (AIOrder::OrderPosition)::Vehicle::Get(vehicle_id)->GetNumOrders(), jump_to);
    00330 }
    00331
    00332 /* static */ bool AIOrder::InsertOrder(VehicleID vehicle_id, OrderPosition order_position, TileIndex destination, AIOrder::AIOrderFlags order_flags)
    00333 {
    00334   /* IsValidVehicleOrder is not good enough because it does not allow appending. */
    00335   if (order_position == ORDER_CURRENT) order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
    00336
    00337   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
    00338   EnforcePrecondition(false, order_position >= 0 && order_position <= ::Vehicle::Get(vehicle_id)->GetNumOrders());
    00339   EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
    00340
    00341   Order order;
    00342   OrderType ot = (order_flags & AIOF_GOTO_NEAREST_DEPOT) ? OT_GOTO_DEPOT : ::GetOrderTypeByTile(destination);
    00343   switch (ot) {
    00344     case OT_GOTO_DEPOT: {
    00345       OrderDepotTypeFlags odtf = (OrderDepotTypeFlags)(ODTFB_PART_OF_ORDERS | ((order_flags & AIOF_SERVICE_IF_NEEDED) ? ODTFB_SERVICE : 0));
    00346       OrderDepotActionFlags odaf = (OrderDepotActionFlags)(ODATF_SERVICE_ONLY | ((order_flags & AIOF_STOP_IN_DEPOT) ? ODATFB_HALT : 0));
    00347       if (order_flags & AIOF_GOTO_NEAREST_DEPOT) odaf |= ODATFB_NEAREST_DEPOT;
    00348       OrderNonStopFlags onsf = (OrderNonStopFlags)((order_flags & AIOF_NON_STOP_INTERMEDIATE) ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
    00349       if (order_flags & AIOF_GOTO_NEAREST_DEPOT) {
    00350         order.MakeGoToDepot(0, odtf, onsf, odaf);
    00351       } else {
    00352         /* Check explicitly if the order is to a station (for aircraft) or
    00353          * to a depot (other vehicle types). */
    00354         if (::Vehicle::Get(vehicle_id)->type == VEH_AIRCRAFT) {
    00355           if (!::IsTileType(destination, MP_STATION)) return false;
    00356           order.MakeGoToDepot(::GetStationIndex(destination), odtf, onsf, odaf);
    00357         } else {
    00358           if (::IsTileType(destination, MP_STATION)) return false;
    00359           order.MakeGoToDepot(::GetDepotIndex(destination), odtf, onsf, odaf);
    00360         }
    00361       }
    00362       break;
    00363     }
    00364
    00365     case OT_GOTO_STATION:
    00366       order.MakeGoToStation(::GetStationIndex(destination));
    00367       order.SetLoadType((OrderLoadFlags)GB(order_flags, 5, 3));
    00368       order.SetUnloadType((OrderUnloadFlags)GB(order_flags, 2, 3));
    00369       order.SetStopLocation(OSL_PLATFORM_FAR_END);
    00370       break;
    00371
    00372     case OT_GOTO_WAYPOINT:
    00373       order.MakeGoToWaypoint(::GetStationIndex(destination));
    00374       break;
    00375
    00376     default:
    00377       return false;
    00378   }
    00379
    00380   order.SetNonStopType((OrderNonStopFlags)GB(order_flags, 0, 2));
    00381
    00382   return AIObject::DoCommand(0, vehicle_id | (order_position << 16), order.Pack(), CMD_INSERT_ORDER);
    00383 }
    00384
    00385 /* static */ bool AIOrder::InsertConditionalOrder(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
    00386 {
    00387   /* IsValidVehicleOrder is not good enough because it does not allow appending. */
    00388   if (order_position == ORDER_CURRENT) order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
    00389
    00390   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
    00391   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to));
    00392
    00393   Order order;
    00394   order.MakeConditional(jump_to);
    00395
    00396   return AIObject::DoCommand(0, vehicle_id | (order_position << 16), order.Pack(), CMD_INSERT_ORDER);
    00397 }
    00398
    00399 /* static */ bool AIOrder::RemoveOrder(VehicleID vehicle_id, OrderPosition order_position)
    00400 {
    00401   order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
    00402
    00403   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
    00404
    00405   return AIObject::DoCommand(0, vehicle_id, order_position, CMD_DELETE_ORDER);
    00406 }
    00407
    00408 /* static */ bool AIOrder::SkipToOrder(VehicleID vehicle_id, OrderPosition next_order)
    00409 {
    00410   next_order = AIOrder::ResolveOrderPosition(vehicle_id, next_order);
    00411
    00412   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, next_order));
    00413
    00414   return AIObject::DoCommand(0, vehicle_id, next_order, CMD_SKIP_TO_ORDER);
    00415 }
    00416
    00425 static void _DoCommandReturnSetOrderFlags(class AIInstance *instance)
    00426 {
    00427   AIObject::SetLastCommandRes(AIOrder::_SetOrderFlags());
    00428   AIInstance::DoCommandReturn(instance);
    00429 }
    00430
    00431 /* static */ bool AIOrder::_SetOrderFlags()
    00432 {
    00433   /* Make sure we don't go into an infinite loop */
    00434   int retry = AIObject::GetCallbackVariable(3) - 1;
    00435   if (retry < 0) {
    00436     DEBUG(ai, 0, "Possible infinite loop in SetOrderFlags() detected");
    00437     return false;
    00438   }
    00439   AIObject::SetCallbackVariable(3, retry);
    00440
    00441   VehicleID vehicle_id = (VehicleID)AIObject::GetCallbackVariable(0);
    00442   OrderPosition order_position = (OrderPosition)AIObject::GetCallbackVariable(1);
    00443   AIOrderFlags order_flags = (AIOrderFlags)AIObject::GetCallbackVariable(2);
    00444
    00445   order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
    00446
    00447   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
    00448   EnforcePrecondition(false, AreOrderFlagsValid(GetOrderDestination(vehicle_id, order_position), order_flags));
    00449
    00450   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
    00451
    00452   AIOrderFlags current = GetOrderFlags(vehicle_id, order_position);
    00453
    00454   if ((current & AIOF_NON_STOP_FLAGS) != (order_flags & AIOF_NON_STOP_FLAGS)) {
    00455     return AIObject::DoCommand(0, vehicle_id | (order_position << 16), (order_flags & AIOF_NON_STOP_FLAGS) << 4 | MOF_NON_STOP, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
    00456   }
    00457
    00458   switch (order->GetType()) {
    00459     case OT_GOTO_DEPOT:
    00460       if ((current & AIOF_DEPOT_FLAGS) != (order_flags & AIOF_DEPOT_FLAGS)) {
    00461         uint data = DA_ALWAYS_GO;
    00462         if (order_flags & AIOF_SERVICE_IF_NEEDED) data = DA_SERVICE;
    00463         if (order_flags & AIOF_STOP_IN_DEPOT) data = DA_STOP;
    00464         return AIObject::DoCommand(0, vehicle_id | (order_position << 16), (data << 4) | MOF_DEPOT_ACTION, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
    00465       }
    00466       break;
    00467
    00468     case OT_GOTO_STATION:
    00469       if ((current & AIOF_UNLOAD_FLAGS) != (order_flags & AIOF_UNLOAD_FLAGS)) {
    00470         return AIObject::DoCommand(0, vehicle_id | (order_position << 16), (order_flags & AIOF_UNLOAD_FLAGS) << 2 | MOF_UNLOAD, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
    00471       }
    00472       if ((current & AIOF_LOAD_FLAGS) != (order_flags & AIOF_LOAD_FLAGS)) {
    00473         return AIObject::DoCommand(0, vehicle_id | (order_position << 16), (order_flags & AIOF_LOAD_FLAGS) >> 1 | MOF_LOAD, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
    00474       }
    00475       break;
    00476
    00477     default: break;
    00478   }
    00479
    00480   assert(GetOrderFlags(vehicle_id, order_position) == order_flags);
    00481
    00482   return true;
    00483 }
    00484
    00485 /* static */ bool AIOrder::SetOrderFlags(VehicleID vehicle_id, OrderPosition order_position, AIOrder::AIOrderFlags order_flags)
    00486 {
    00487   AIObject::SetCallbackVariable(0, vehicle_id);
    00488   AIObject::SetCallbackVariable(1, order_position);
    00489   AIObject::SetCallbackVariable(2, order_flags);
    00490   /* In case another client(s) change orders at the same time we could
    00491    * end in an infinite loop. This stops that from happening ever. */
    00492   AIObject::SetCallbackVariable(3, 8);
    00493   return AIOrder::_SetOrderFlags();
    00494 }
    00495
    00496 /* static */ bool AIOrder::MoveOrder(VehicleID vehicle_id, OrderPosition order_position_move, OrderPosition order_position_target)
    00497 {
    00498   order_position_move   = AIOrder::ResolveOrderPosition(vehicle_id, order_position_move);
    00499   order_position_target = AIOrder::ResolveOrderPosition(vehicle_id, order_position_target);
    00500
    00501   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position_move));
    00502   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position_target));
    00503
    00504   return AIObject::DoCommand(0, vehicle_id, order_position_move | (order_position_target << 16), CMD_MOVE_ORDER);
    00505 }
    00506
    00507 /* static */ bool AIOrder::CopyOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
    00508 {
    00509   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
    00510   EnforcePrecondition(false, AIVehicle::IsValidVehicle(main_vehicle_id));
    00511
    00512   return AIObject::DoCommand(0, vehicle_id | (main_vehicle_id << 16), CO_COPY, CMD_CLONE_ORDER);
    00513 }
    00514
    00515 /* static */ bool AIOrder::ShareOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
    00516 {
    00517   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
    00518   EnforcePrecondition(false, AIVehicle::IsValidVehicle(main_vehicle_id));
    00519
    00520   return AIObject::DoCommand(0, vehicle_id | (main_vehicle_id << 16), CO_SHARE, CMD_CLONE_ORDER);
    00521 }
    00522
    00523 /* static */ bool AIOrder::UnshareOrders(VehicleID vehicle_id)
    00524 {
    00525   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
    00526
    00527   return AIObject::DoCommand(0, vehicle_id, CO_UNSHARE, CMD_CLONE_ORDER);
    00528 }