# ai_order.cpp

Go to the documentation of this file.

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