ai_order.cpp

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

Generated on Thu Feb 4 17:20:22 2010 for OpenTTD by  doxygen 1.5.6