ai_order.cpp

Go to the documentation of this file.
00001 /* $Id: ai_order.cpp 21571 2010-12-21 15:23:54Z alberth $ */
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 
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::IsVoidOrder(VehicleID vehicle_id, OrderPosition order_position)
00103 {
00104   if (order_position == ORDER_CURRENT) return false;
00105   if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
00106 
00107   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
00108   return order->GetType() == OT_DUMMY;
00109 }
00110 
00111 /* static */ bool AIOrder::IsCurrentOrderPartOfOrderList(VehicleID vehicle_id)
00112 {
00113   if (AIVehicle::IsValidVehicle(vehicle_id)) return false;
00114   if (GetOrderCount(vehicle_id) == 0) return false;
00115 
00116   const Order *order = &::Vehicle::Get(vehicle_id)->current_order;
00117   if (order->GetType() != OT_GOTO_DEPOT) return true;
00118   return (order->GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0;
00119 }
00120 
00121 /* static */ AIOrder::OrderPosition AIOrder::ResolveOrderPosition(VehicleID vehicle_id, OrderPosition order_position)
00122 {
00123   if (!AIVehicle::IsValidVehicle(vehicle_id)) return ORDER_INVALID;
00124 
00125   if (order_position == ORDER_CURRENT) return (AIOrder::OrderPosition)::Vehicle::Get(vehicle_id)->cur_order_index;
00126   return (order_position >= 0 && order_position < ::Vehicle::Get(vehicle_id)->GetNumOrders()) ? order_position : ORDER_INVALID;
00127 }
00128 
00129 
00130 /* static */ bool AIOrder::AreOrderFlagsValid(TileIndex destination, AIOrderFlags order_flags)
00131 {
00132   OrderType ot = (order_flags & AIOF_GOTO_NEAREST_DEPOT) ? OT_GOTO_DEPOT : ::GetOrderTypeByTile(destination);
00133   switch (ot) {
00134     case OT_GOTO_STATION:
00135       return (order_flags & ~(AIOF_NON_STOP_FLAGS | AIOF_UNLOAD_FLAGS | AIOF_LOAD_FLAGS)) == 0 &&
00136           /* Test the different mutual exclusive flags. */
00137           ((order_flags & AIOF_TRANSFER)      == 0 || (order_flags & AIOF_UNLOAD)    == 0) &&
00138           ((order_flags & AIOF_TRANSFER)      == 0 || (order_flags & AIOF_NO_UNLOAD) == 0) &&
00139           ((order_flags & AIOF_UNLOAD)        == 0 || (order_flags & AIOF_NO_UNLOAD) == 0) &&
00140           ((order_flags & AIOF_UNLOAD)        == 0 || (order_flags & AIOF_NO_UNLOAD) == 0) &&
00141           ((order_flags & AIOF_NO_UNLOAD)     == 0 || (order_flags & AIOF_NO_LOAD)   == 0) &&
00142           ((order_flags & AIOF_FULL_LOAD_ANY) == 0 || (order_flags & AIOF_NO_LOAD)   == 0);
00143 
00144     case OT_GOTO_DEPOT:
00145       return (order_flags & ~(AIOF_NON_STOP_FLAGS | AIOF_DEPOT_FLAGS)) == 0 &&
00146           ((order_flags & AIOF_SERVICE_IF_NEEDED) == 0 || (order_flags & AIOF_STOP_IN_DEPOT) == 0);
00147 
00148     case OT_GOTO_WAYPOINT: return (order_flags & ~(AIOF_NON_STOP_FLAGS)) == 0;
00149     default:               return false;
00150   }
00151 }
00152 
00153 /* static */ bool AIOrder::IsValidConditionalOrder(OrderCondition condition, CompareFunction compare)
00154 {
00155   switch (condition) {
00156     case OC_LOAD_PERCENTAGE:
00157     case OC_RELIABILITY:
00158     case OC_MAX_SPEED:
00159     case OC_AGE:
00160       return compare >= CF_EQUALS && compare <= CF_MORE_EQUALS;
00161 
00162     case OC_REQUIRES_SERVICE:
00163       return compare == CF_IS_TRUE || compare == CF_IS_FALSE;
00164 
00165     case OC_UNCONDITIONALLY:
00166       return true;
00167 
00168     default: return false;
00169   }
00170 }
00171 
00172 /* static */ int32 AIOrder::GetOrderCount(VehicleID vehicle_id)
00173 {
00174   return AIVehicle::IsValidVehicle(vehicle_id) ? ::Vehicle::Get(vehicle_id)->GetNumOrders() : -1;
00175 }
00176 
00177 /* static */ TileIndex AIOrder::GetOrderDestination(VehicleID vehicle_id, OrderPosition order_position)
00178 {
00179   if (!IsValidVehicleOrder(vehicle_id, order_position)) return INVALID_TILE;
00180 
00181   const Order *order = ::ResolveOrder(vehicle_id, order_position);
00182   if (order == NULL || order->GetType() == OT_CONDITIONAL) return INVALID_TILE;
00183   const Vehicle *v = ::Vehicle::Get(vehicle_id);
00184 
00185   switch (order->GetType()) {
00186     case OT_GOTO_DEPOT: {
00187       /* We don't know where the nearest depot is... (yet) */
00188       if (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) return INVALID_TILE;
00189 
00190       if (v->type != VEH_AIRCRAFT) return ::Depot::Get(order->GetDestination())->xy;
00191       /* Aircraft's hangars are referenced by StationID, not DepotID */
00192       const Station *st = ::Station::Get(order->GetDestination());
00193       if (!st->airport.HasHangar()) return INVALID_TILE;
00194       return st->airport.GetHangarTile(0);
00195     }
00196 
00197     case OT_GOTO_STATION: {
00198       const Station *st = ::Station::Get(order->GetDestination());
00199       if (st->train_station.tile != INVALID_TILE) {
00200         TILE_AREA_LOOP(t, st->train_station) {
00201           if (st->TileBelongsToRailStation(t)) return t;
00202         }
00203       } else if (st->dock_tile != INVALID_TILE) {
00204         return st->dock_tile;
00205       } else if (st->bus_stops != NULL) {
00206         return st->bus_stops->xy;
00207       } else if (st->truck_stops != NULL) {
00208         return st->truck_stops->xy;
00209       } else if (st->airport.tile != INVALID_TILE) {
00210         TILE_AREA_LOOP(tile, st->airport) {
00211           if (st->TileBelongsToAirport(tile) && !::IsHangar(tile)) return tile;
00212         }
00213       }
00214       return INVALID_TILE;
00215     }
00216 
00217     case OT_GOTO_WAYPOINT: {
00218       const Waypoint *wp = ::Waypoint::Get(order->GetDestination());
00219       if (wp->train_station.tile != INVALID_TILE) {
00220         TILE_AREA_LOOP(t, wp->train_station) {
00221           if (wp->TileBelongsToRailStation(t)) return t;
00222         }
00223       }
00224       /* If the waypoint has no rail waypoint tiles, it must have a buoy */
00225       return wp->xy;
00226     }
00227     default:               return INVALID_TILE;
00228   }
00229 }
00230 
00231 /* static */ AIOrder::AIOrderFlags AIOrder::GetOrderFlags(VehicleID vehicle_id, OrderPosition order_position)
00232 {
00233   if (!IsValidVehicleOrder(vehicle_id, order_position)) return AIOF_INVALID;
00234 
00235   const Order *order = ::ResolveOrder(vehicle_id, order_position);
00236   if (order == NULL || order->GetType() == OT_CONDITIONAL || order->GetType() == OT_DUMMY) return AIOF_INVALID;
00237 
00238   AIOrderFlags order_flags = AIOF_NONE;
00239   order_flags |= (AIOrderFlags)order->GetNonStopType();
00240   switch (order->GetType()) {
00241     case OT_GOTO_DEPOT:
00242       if (order->GetDepotOrderType() & ODTFB_SERVICE) order_flags |= AIOF_SERVICE_IF_NEEDED;
00243       if (order->GetDepotActionType() & ODATFB_HALT) order_flags |= AIOF_STOP_IN_DEPOT;
00244       if (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) order_flags |= AIOF_GOTO_NEAREST_DEPOT;
00245       break;
00246 
00247     case OT_GOTO_STATION:
00248       order_flags |= (AIOrderFlags)(order->GetLoadType()   << 5);
00249       order_flags |= (AIOrderFlags)(order->GetUnloadType() << 2);
00250       break;
00251 
00252     default: break;
00253   }
00254 
00255   return order_flags;
00256 }
00257 
00258 /* static */ AIOrder::OrderPosition AIOrder::GetOrderJumpTo(VehicleID vehicle_id, OrderPosition order_position)
00259 {
00260   if (!IsValidVehicleOrder(vehicle_id, order_position)) return ORDER_INVALID;
00261   if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return ORDER_INVALID;
00262 
00263   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
00264   return (OrderPosition)order->GetConditionSkipToOrder();
00265 }
00266 
00267 /* static */ AIOrder::OrderCondition AIOrder::GetOrderCondition(VehicleID vehicle_id, OrderPosition order_position)
00268 {
00269   if (!IsValidVehicleOrder(vehicle_id, order_position)) return OC_INVALID;
00270   if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return OC_INVALID;
00271 
00272   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
00273   return (OrderCondition)order->GetConditionVariable();
00274 }
00275 
00276 /* static */ AIOrder::CompareFunction AIOrder::GetOrderCompareFunction(VehicleID vehicle_id, OrderPosition order_position)
00277 {
00278   if (!IsValidVehicleOrder(vehicle_id, order_position)) return CF_INVALID;
00279   if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return CF_INVALID;
00280 
00281   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
00282   return (CompareFunction)order->GetConditionComparator();
00283 }
00284 
00285 /* static */ int32 AIOrder::GetOrderCompareValue(VehicleID vehicle_id, OrderPosition order_position)
00286 {
00287   if (!IsValidVehicleOrder(vehicle_id, order_position)) return -1;
00288   if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return -1;
00289 
00290   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
00291   int32 value = order->GetConditionValue();
00292   if (order->GetConditionVariable() == OCV_MAX_SPEED) value = value * 16 / 10;
00293   return value;
00294 }
00295 
00296 /* static */ AIOrder::StopLocation AIOrder::GetStopLocation(VehicleID vehicle_id, OrderPosition order_position)
00297 {
00298   if (!IsValidVehicleOrder(vehicle_id, order_position)) return STOPLOCATION_INVALID;
00299   if (AIVehicle::GetVehicleType(vehicle_id) != AIVehicle::VT_RAIL) return STOPLOCATION_INVALID;
00300   if (!IsGotoStationOrder(vehicle_id, order_position)) return STOPLOCATION_INVALID;
00301 
00302   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
00303   return (AIOrder::StopLocation)order->GetStopLocation();
00304 }
00305 
00306 /* static */ bool AIOrder::SetOrderJumpTo(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
00307 {
00308   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
00309   EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
00310   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to) && jump_to != ORDER_CURRENT);
00311 
00312   return AIObject::DoCommand(0, vehicle_id | (order_position << 20), MOF_COND_DESTINATION | (jump_to << 4), CMD_MODIFY_ORDER);
00313 }
00314 
00315 /* static */ bool AIOrder::SetOrderCondition(VehicleID vehicle_id, OrderPosition order_position, OrderCondition condition)
00316 {
00317   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
00318   EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
00319   EnforcePrecondition(false, condition >= OC_LOAD_PERCENTAGE && condition <= OC_UNCONDITIONALLY);
00320 
00321   return AIObject::DoCommand(0, vehicle_id | (order_position << 20), MOF_COND_VARIABLE | (condition << 4), CMD_MODIFY_ORDER);
00322 }
00323 
00324 /* static */ bool AIOrder::SetOrderCompareFunction(VehicleID vehicle_id, OrderPosition order_position, CompareFunction compare)
00325 {
00326   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
00327   EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
00328   EnforcePrecondition(false, compare >= CF_EQUALS && compare <= CF_IS_FALSE);
00329 
00330   return AIObject::DoCommand(0, vehicle_id | (order_position << 20), MOF_COND_COMPARATOR | (compare << 4), CMD_MODIFY_ORDER);
00331 }
00332 
00333 /* static */ bool AIOrder::SetOrderCompareValue(VehicleID vehicle_id, OrderPosition order_position, int32 value)
00334 {
00335   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
00336   EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
00337   EnforcePrecondition(false, value >= 0 && value < 2048);
00338   if (GetOrderCondition(vehicle_id, order_position) == OC_MAX_SPEED) value = value * 10 / 16;
00339 
00340   return AIObject::DoCommand(0, vehicle_id | (order_position << 20), MOF_COND_VALUE | (value << 4), CMD_MODIFY_ORDER);
00341 }
00342 
00343 /* static */ bool AIOrder::SetStopLocation(VehicleID vehicle_id, OrderPosition order_position, StopLocation stop_location)
00344 {
00345   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
00346   EnforcePrecondition(false, AIVehicle::GetVehicleType(vehicle_id) == AIVehicle::VT_RAIL);
00347   EnforcePrecondition(false, IsGotoStationOrder(vehicle_id, order_position));
00348   EnforcePrecondition(false, stop_location >= STOPLOCATION_NEAR && stop_location <= STOPLOCATION_FAR);
00349 
00350   uint32 p1 = vehicle_id | (order_position << 20);
00351   uint32 p2 = MOF_STOP_LOCATION | (stop_location << 4);
00352   return AIObject::DoCommand(0, p1, p2, CMD_MODIFY_ORDER);
00353 }
00354 
00355 /* static */ bool AIOrder::AppendOrder(VehicleID vehicle_id, TileIndex destination, AIOrderFlags order_flags)
00356 {
00357   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
00358   EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
00359 
00360   return InsertOrder(vehicle_id, (AIOrder::OrderPosition)::Vehicle::Get(vehicle_id)->GetNumOrders(), destination, order_flags);
00361 }
00362 
00363 /* static */ bool AIOrder::AppendConditionalOrder(VehicleID vehicle_id, OrderPosition jump_to)
00364 {
00365   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
00366   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to));
00367 
00368   return InsertConditionalOrder(vehicle_id, (AIOrder::OrderPosition)::Vehicle::Get(vehicle_id)->GetNumOrders(), jump_to);
00369 }
00370 
00371 /* static */ bool AIOrder::InsertOrder(VehicleID vehicle_id, OrderPosition order_position, TileIndex destination, AIOrder::AIOrderFlags order_flags)
00372 {
00373   /* IsValidVehicleOrder is not good enough because it does not allow appending. */
00374   if (order_position == ORDER_CURRENT) order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
00375 
00376   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
00377   EnforcePrecondition(false, order_position >= 0 && order_position <= ::Vehicle::Get(vehicle_id)->GetNumOrders());
00378   EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
00379 
00380   Order order;
00381   OrderType ot = (order_flags & AIOF_GOTO_NEAREST_DEPOT) ? OT_GOTO_DEPOT : ::GetOrderTypeByTile(destination);
00382   switch (ot) {
00383     case OT_GOTO_DEPOT: {
00384       OrderDepotTypeFlags odtf = (OrderDepotTypeFlags)(ODTFB_PART_OF_ORDERS | ((order_flags & AIOF_SERVICE_IF_NEEDED) ? ODTFB_SERVICE : 0));
00385       OrderDepotActionFlags odaf = (OrderDepotActionFlags)(ODATF_SERVICE_ONLY | ((order_flags & AIOF_STOP_IN_DEPOT) ? ODATFB_HALT : 0));
00386       if (order_flags & AIOF_GOTO_NEAREST_DEPOT) odaf |= ODATFB_NEAREST_DEPOT;
00387       OrderNonStopFlags onsf = (OrderNonStopFlags)((order_flags & AIOF_NON_STOP_INTERMEDIATE) ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
00388       if (order_flags & AIOF_GOTO_NEAREST_DEPOT) {
00389         order.MakeGoToDepot(0, odtf, onsf, odaf);
00390       } else {
00391         /* Check explicitly if the order is to a station (for aircraft) or
00392          * to a depot (other vehicle types). */
00393         if (::Vehicle::Get(vehicle_id)->type == VEH_AIRCRAFT) {
00394           if (!::IsTileType(destination, MP_STATION)) return false;
00395           order.MakeGoToDepot(::GetStationIndex(destination), odtf, onsf, odaf);
00396         } else {
00397           if (::IsTileType(destination, MP_STATION)) return false;
00398           order.MakeGoToDepot(::GetDepotIndex(destination), odtf, onsf, odaf);
00399         }
00400       }
00401       break;
00402     }
00403 
00404     case OT_GOTO_STATION:
00405       order.MakeGoToStation(::GetStationIndex(destination));
00406       order.SetLoadType((OrderLoadFlags)GB(order_flags, 5, 3));
00407       order.SetUnloadType((OrderUnloadFlags)GB(order_flags, 2, 3));
00408       order.SetStopLocation(OSL_PLATFORM_FAR_END);
00409       break;
00410 
00411     case OT_GOTO_WAYPOINT:
00412       order.MakeGoToWaypoint(::GetStationIndex(destination));
00413       break;
00414 
00415     default:
00416       return false;
00417   }
00418 
00419   order.SetNonStopType((OrderNonStopFlags)GB(order_flags, 0, 2));
00420 
00421   return AIObject::DoCommand(0, vehicle_id | (order_position << 20), order.Pack(), CMD_INSERT_ORDER);
00422 }
00423 
00424 /* static */ bool AIOrder::InsertConditionalOrder(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
00425 {
00426   /* IsValidVehicleOrder is not good enough because it does not allow appending. */
00427   if (order_position == ORDER_CURRENT) order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
00428 
00429   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
00430   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to));
00431 
00432   Order order;
00433   order.MakeConditional(jump_to);
00434 
00435   return AIObject::DoCommand(0, vehicle_id | (order_position << 20), order.Pack(), CMD_INSERT_ORDER);
00436 }
00437 
00438 /* static */ bool AIOrder::RemoveOrder(VehicleID vehicle_id, OrderPosition order_position)
00439 {
00440   order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
00441 
00442   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
00443 
00444   return AIObject::DoCommand(0, vehicle_id, order_position, CMD_DELETE_ORDER);
00445 }
00446 
00447 /* static */ bool AIOrder::SkipToOrder(VehicleID vehicle_id, OrderPosition next_order)
00448 {
00449   next_order = AIOrder::ResolveOrderPosition(vehicle_id, next_order);
00450 
00451   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, next_order));
00452 
00453   return AIObject::DoCommand(0, vehicle_id, next_order, CMD_SKIP_TO_ORDER);
00454 }
00455 
00464 static void _DoCommandReturnSetOrderFlags(class AIInstance *instance)
00465 {
00466   AIObject::SetLastCommandRes(AIOrder::_SetOrderFlags());
00467   AIInstance::DoCommandReturn(instance);
00468 }
00469 
00470 /* static */ bool AIOrder::_SetOrderFlags()
00471 {
00472   /* Make sure we don't go into an infinite loop */
00473   int retry = AIObject::GetCallbackVariable(3) - 1;
00474   if (retry < 0) {
00475     DEBUG(ai, 0, "Possible infinite loop in SetOrderFlags() detected");
00476     return false;
00477   }
00478   AIObject::SetCallbackVariable(3, retry);
00479 
00480   VehicleID vehicle_id = (VehicleID)AIObject::GetCallbackVariable(0);
00481   OrderPosition order_position = (OrderPosition)AIObject::GetCallbackVariable(1);
00482   AIOrderFlags order_flags = (AIOrderFlags)AIObject::GetCallbackVariable(2);
00483 
00484   order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
00485 
00486   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
00487   EnforcePrecondition(false, AreOrderFlagsValid(GetOrderDestination(vehicle_id, order_position), order_flags));
00488 
00489   const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
00490 
00491   AIOrderFlags current = GetOrderFlags(vehicle_id, order_position);
00492 
00493   if ((current & AIOF_NON_STOP_FLAGS) != (order_flags & AIOF_NON_STOP_FLAGS)) {
00494     return AIObject::DoCommand(0, vehicle_id | (order_position << 20), (order_flags & AIOF_NON_STOP_FLAGS) << 4 | MOF_NON_STOP, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
00495   }
00496 
00497   switch (order->GetType()) {
00498     case OT_GOTO_DEPOT:
00499       if ((current & AIOF_DEPOT_FLAGS) != (order_flags & AIOF_DEPOT_FLAGS)) {
00500         uint data = DA_ALWAYS_GO;
00501         if (order_flags & AIOF_SERVICE_IF_NEEDED) data = DA_SERVICE;
00502         if (order_flags & AIOF_STOP_IN_DEPOT) data = DA_STOP;
00503         return AIObject::DoCommand(0, vehicle_id | (order_position << 20), (data << 4) | MOF_DEPOT_ACTION, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
00504       }
00505       break;
00506 
00507     case OT_GOTO_STATION:
00508       if ((current & AIOF_UNLOAD_FLAGS) != (order_flags & AIOF_UNLOAD_FLAGS)) {
00509         return AIObject::DoCommand(0, vehicle_id | (order_position << 20), (order_flags & AIOF_UNLOAD_FLAGS) << 2 | MOF_UNLOAD, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
00510       }
00511       if ((current & AIOF_LOAD_FLAGS) != (order_flags & AIOF_LOAD_FLAGS)) {
00512         return AIObject::DoCommand(0, vehicle_id | (order_position << 20), (order_flags & AIOF_LOAD_FLAGS) >> 1 | MOF_LOAD, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
00513       }
00514       break;
00515 
00516     default: break;
00517   }
00518 
00519   assert(GetOrderFlags(vehicle_id, order_position) == order_flags);
00520 
00521   return true;
00522 }
00523 
00524 /* static */ bool AIOrder::SetOrderFlags(VehicleID vehicle_id, OrderPosition order_position, AIOrder::AIOrderFlags order_flags)
00525 {
00526   AIObject::SetCallbackVariable(0, vehicle_id);
00527   AIObject::SetCallbackVariable(1, order_position);
00528   AIObject::SetCallbackVariable(2, order_flags);
00529   /* In case another client(s) change orders at the same time we could
00530    * end in an infinite loop. This stops that from happening ever. */
00531   AIObject::SetCallbackVariable(3, 8);
00532   return AIOrder::_SetOrderFlags();
00533 }
00534 
00535 /* static */ bool AIOrder::MoveOrder(VehicleID vehicle_id, OrderPosition order_position_move, OrderPosition order_position_target)
00536 {
00537   order_position_move   = AIOrder::ResolveOrderPosition(vehicle_id, order_position_move);
00538   order_position_target = AIOrder::ResolveOrderPosition(vehicle_id, order_position_target);
00539 
00540   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position_move));
00541   EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position_target));
00542 
00543   return AIObject::DoCommand(0, vehicle_id, order_position_move | (order_position_target << 16), CMD_MOVE_ORDER);
00544 }
00545 
00546 /* static */ bool AIOrder::CopyOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
00547 {
00548   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
00549   EnforcePrecondition(false, AIVehicle::IsValidVehicle(main_vehicle_id));
00550 
00551   return AIObject::DoCommand(0, vehicle_id | CO_COPY << 30, main_vehicle_id, CMD_CLONE_ORDER);
00552 }
00553 
00554 /* static */ bool AIOrder::ShareOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
00555 {
00556   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
00557   EnforcePrecondition(false, AIVehicle::IsValidVehicle(main_vehicle_id));
00558 
00559   return AIObject::DoCommand(0, vehicle_id | CO_SHARE << 30, main_vehicle_id, CMD_CLONE_ORDER);
00560 }
00561 
00562 /* static */ bool AIOrder::UnshareOrders(VehicleID vehicle_id)
00563 {
00564   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
00565 
00566   return AIObject::DoCommand(0, vehicle_id | CO_UNSHARE << 30, 0, CMD_CLONE_ORDER);
00567 }

Generated on Fri Dec 31 17:15:28 2010 for OpenTTD by  doxygen 1.6.1