ai_order.cpp

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

Generated on Sun Jan 23 01:49:00 2011 for OpenTTD by  doxygen 1.6.1