00001
00002
00003
00004
00005
00006
00007
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 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 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 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 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 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 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 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 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
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 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 int32 AIOrder::GetOrderCount(VehicleID vehicle_id)
00164 {
00165 return AIVehicle::IsValidVehicle(vehicle_id) ? ::Vehicle::Get(vehicle_id)->GetNumOrders() : -1;
00166 }
00167
00168 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
00179 if (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) return INVALID_TILE;
00180
00181 if (v->type != VEH_AIRCRAFT) return ::Depot::Get(order->GetDestination())->xy;
00182
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
00217 return wp->xy;
00218 }
00219 default: return INVALID_TILE;
00220 }
00221 }
00222
00223 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 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 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 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 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 AIOrder::StopLocation AIOrder::GetStopLocation(VehicleID vehicle_id, OrderPosition order_position)
00289 {
00290 if (!IsValidVehicleOrder(vehicle_id, order_position)) return STOPLOCATION_INVALID;
00291 if (AIVehicle::GetVehicleType(vehicle_id) != AIVehicle::VT_RAIL) return STOPLOCATION_INVALID;
00292 if (!IsGotoStationOrder(vehicle_id, order_position)) return STOPLOCATION_INVALID;
00293
00294 const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
00295 return (AIOrder::StopLocation)order->GetStopLocation();
00296 }
00297
00298 bool AIOrder::SetOrderJumpTo(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
00299 {
00300 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
00301 EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
00302 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to) && jump_to != ORDER_CURRENT);
00303
00304 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_DESTINATION | (jump_to << 4), CMD_MODIFY_ORDER);
00305 }
00306
00307 bool AIOrder::SetOrderCondition(VehicleID vehicle_id, OrderPosition order_position, OrderCondition condition)
00308 {
00309 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
00310 EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
00311 EnforcePrecondition(false, condition >= OC_LOAD_PERCENTAGE && condition <= OC_UNCONDITIONALLY);
00312
00313 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_VARIABLE | (condition << 4), CMD_MODIFY_ORDER);
00314 }
00315
00316 bool AIOrder::SetOrderCompareFunction(VehicleID vehicle_id, OrderPosition order_position, CompareFunction compare)
00317 {
00318 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
00319 EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
00320 EnforcePrecondition(false, compare >= CF_EQUALS && compare <= CF_IS_FALSE);
00321
00322 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_COMPARATOR | (compare << 4), CMD_MODIFY_ORDER);
00323 }
00324
00325 bool AIOrder::SetOrderCompareValue(VehicleID vehicle_id, OrderPosition order_position, int32 value)
00326 {
00327 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
00328 EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
00329 EnforcePrecondition(false, value >= 0 && value < 2048);
00330 if (GetOrderCondition(vehicle_id, order_position) == OC_MAX_SPEED) value = value * 10 / 16;
00331
00332 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_VALUE | (value << 4), CMD_MODIFY_ORDER);
00333 }
00334
00335 bool AIOrder::SetStopLocation(VehicleID vehicle_id, OrderPosition order_position, StopLocation stop_location)
00336 {
00337 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
00338 EnforcePrecondition(false, AIVehicle::GetVehicleType(vehicle_id) == AIVehicle::VT_RAIL);
00339 EnforcePrecondition(false, IsGotoStationOrder(vehicle_id, order_position));
00340 EnforcePrecondition(false, stop_location >= STOPLOCATION_NEAR && stop_location <= STOPLOCATION_FAR);
00341
00342 uint32 p1 = vehicle_id | (order_position << 16);
00343 uint32 p2 = MOF_STOP_LOCATION | (stop_location << 4);
00344 return AIObject::DoCommand(0, p1, p2, CMD_MODIFY_ORDER);
00345 }
00346
00347 bool AIOrder::AppendOrder(VehicleID vehicle_id, TileIndex destination, AIOrderFlags order_flags)
00348 {
00349 EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
00350 EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
00351
00352 return InsertOrder(vehicle_id, (AIOrder::OrderPosition)::Vehicle::Get(vehicle_id)->GetNumOrders(), destination, order_flags);
00353 }
00354
00355 bool AIOrder::AppendConditionalOrder(VehicleID vehicle_id, OrderPosition jump_to)
00356 {
00357 EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
00358 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to));
00359
00360 return InsertConditionalOrder(vehicle_id, (AIOrder::OrderPosition)::Vehicle::Get(vehicle_id)->GetNumOrders(), jump_to);
00361 }
00362
00363 bool AIOrder::InsertOrder(VehicleID vehicle_id, OrderPosition order_position, TileIndex destination, AIOrder::AIOrderFlags order_flags)
00364 {
00365
00366 if (order_position == ORDER_CURRENT) order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
00367
00368 EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
00369 EnforcePrecondition(false, order_position >= 0 && order_position <= ::Vehicle::Get(vehicle_id)->GetNumOrders());
00370 EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
00371
00372 Order order;
00373 OrderType ot = (order_flags & AIOF_GOTO_NEAREST_DEPOT) ? OT_GOTO_DEPOT : ::GetOrderTypeByTile(destination);
00374 switch (ot) {
00375 case OT_GOTO_DEPOT: {
00376 OrderDepotTypeFlags odtf = (OrderDepotTypeFlags)(ODTFB_PART_OF_ORDERS | ((order_flags & AIOF_SERVICE_IF_NEEDED) ? ODTFB_SERVICE : 0));
00377 OrderDepotActionFlags odaf = (OrderDepotActionFlags)(ODATF_SERVICE_ONLY | ((order_flags & AIOF_STOP_IN_DEPOT) ? ODATFB_HALT : 0));
00378 if (order_flags & AIOF_GOTO_NEAREST_DEPOT) odaf |= ODATFB_NEAREST_DEPOT;
00379 OrderNonStopFlags onsf = (OrderNonStopFlags)((order_flags & AIOF_NON_STOP_INTERMEDIATE) ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
00380 if (order_flags & AIOF_GOTO_NEAREST_DEPOT) {
00381 order.MakeGoToDepot(0, odtf, onsf, odaf);
00382 } else {
00383
00384
00385 if (::Vehicle::Get(vehicle_id)->type == VEH_AIRCRAFT) {
00386 if (!::IsTileType(destination, MP_STATION)) return false;
00387 order.MakeGoToDepot(::GetStationIndex(destination), odtf, onsf, odaf);
00388 } else {
00389 if (::IsTileType(destination, MP_STATION)) return false;
00390 order.MakeGoToDepot(::GetDepotIndex(destination), odtf, onsf, odaf);
00391 }
00392 }
00393 break;
00394 }
00395
00396 case OT_GOTO_STATION:
00397 order.MakeGoToStation(::GetStationIndex(destination));
00398 order.SetLoadType((OrderLoadFlags)GB(order_flags, 5, 3));
00399 order.SetUnloadType((OrderUnloadFlags)GB(order_flags, 2, 3));
00400 order.SetStopLocation(OSL_PLATFORM_FAR_END);
00401 break;
00402
00403 case OT_GOTO_WAYPOINT:
00404 order.MakeGoToWaypoint(::GetStationIndex(destination));
00405 break;
00406
00407 default:
00408 return false;
00409 }
00410
00411 order.SetNonStopType((OrderNonStopFlags)GB(order_flags, 0, 2));
00412
00413 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), order.Pack(), CMD_INSERT_ORDER);
00414 }
00415
00416 bool AIOrder::InsertConditionalOrder(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
00417 {
00418
00419 if (order_position == ORDER_CURRENT) order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
00420
00421 EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
00422 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to));
00423
00424 Order order;
00425 order.MakeConditional(jump_to);
00426
00427 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), order.Pack(), CMD_INSERT_ORDER);
00428 }
00429
00430 bool AIOrder::RemoveOrder(VehicleID vehicle_id, OrderPosition order_position)
00431 {
00432 order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
00433
00434 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
00435
00436 return AIObject::DoCommand(0, vehicle_id, order_position, CMD_DELETE_ORDER);
00437 }
00438
00439 bool AIOrder::SkipToOrder(VehicleID vehicle_id, OrderPosition next_order)
00440 {
00441 next_order = AIOrder::ResolveOrderPosition(vehicle_id, next_order);
00442
00443 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, next_order));
00444
00445 return AIObject::DoCommand(0, vehicle_id, next_order, CMD_SKIP_TO_ORDER);
00446 }
00447
00456 static void _DoCommandReturnSetOrderFlags(class AIInstance *instance)
00457 {
00458 AIObject::SetLastCommandRes(AIOrder::_SetOrderFlags());
00459 AIInstance::DoCommandReturn(instance);
00460 }
00461
00462 bool AIOrder::_SetOrderFlags()
00463 {
00464
00465 int retry = AIObject::GetCallbackVariable(3) - 1;
00466 if (retry < 0) {
00467 DEBUG(ai, 0, "Possible infinite loop in SetOrderFlags() detected");
00468 return false;
00469 }
00470 AIObject::SetCallbackVariable(3, retry);
00471
00472 VehicleID vehicle_id = (VehicleID)AIObject::GetCallbackVariable(0);
00473 OrderPosition order_position = (OrderPosition)AIObject::GetCallbackVariable(1);
00474 AIOrderFlags order_flags = (AIOrderFlags)AIObject::GetCallbackVariable(2);
00475
00476 order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
00477
00478 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
00479 EnforcePrecondition(false, AreOrderFlagsValid(GetOrderDestination(vehicle_id, order_position), order_flags));
00480
00481 const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
00482
00483 AIOrderFlags current = GetOrderFlags(vehicle_id, order_position);
00484
00485 if ((current & AIOF_NON_STOP_FLAGS) != (order_flags & AIOF_NON_STOP_FLAGS)) {
00486 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), (order_flags & AIOF_NON_STOP_FLAGS) << 4 | MOF_NON_STOP, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
00487 }
00488
00489 switch (order->GetType()) {
00490 case OT_GOTO_DEPOT:
00491 if ((current & AIOF_DEPOT_FLAGS) != (order_flags & AIOF_DEPOT_FLAGS)) {
00492 uint data = DA_ALWAYS_GO;
00493 if (order_flags & AIOF_SERVICE_IF_NEEDED) data = DA_SERVICE;
00494 if (order_flags & AIOF_STOP_IN_DEPOT) data = DA_STOP;
00495 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), (data << 4) | MOF_DEPOT_ACTION, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
00496 }
00497 break;
00498
00499 case OT_GOTO_STATION:
00500 if ((current & AIOF_UNLOAD_FLAGS) != (order_flags & AIOF_UNLOAD_FLAGS)) {
00501 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), (order_flags & AIOF_UNLOAD_FLAGS) << 2 | MOF_UNLOAD, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
00502 }
00503 if ((current & AIOF_LOAD_FLAGS) != (order_flags & AIOF_LOAD_FLAGS)) {
00504 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), (order_flags & AIOF_LOAD_FLAGS) >> 1 | MOF_LOAD, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
00505 }
00506 break;
00507
00508 default: break;
00509 }
00510
00511 assert(GetOrderFlags(vehicle_id, order_position) == order_flags);
00512
00513 return true;
00514 }
00515
00516 bool AIOrder::SetOrderFlags(VehicleID vehicle_id, OrderPosition order_position, AIOrder::AIOrderFlags order_flags)
00517 {
00518 AIObject::SetCallbackVariable(0, vehicle_id);
00519 AIObject::SetCallbackVariable(1, order_position);
00520 AIObject::SetCallbackVariable(2, order_flags);
00521
00522
00523 AIObject::SetCallbackVariable(3, 8);
00524 return AIOrder::_SetOrderFlags();
00525 }
00526
00527 bool AIOrder::MoveOrder(VehicleID vehicle_id, OrderPosition order_position_move, OrderPosition order_position_target)
00528 {
00529 order_position_move = AIOrder::ResolveOrderPosition(vehicle_id, order_position_move);
00530 order_position_target = AIOrder::ResolveOrderPosition(vehicle_id, order_position_target);
00531
00532 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position_move));
00533 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position_target));
00534
00535 return AIObject::DoCommand(0, vehicle_id, order_position_move | (order_position_target << 16), CMD_MOVE_ORDER);
00536 }
00537
00538 bool AIOrder::CopyOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
00539 {
00540 EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
00541 EnforcePrecondition(false, AIVehicle::IsValidVehicle(main_vehicle_id));
00542
00543 return AIObject::DoCommand(0, vehicle_id | (main_vehicle_id << 16), CO_COPY, CMD_CLONE_ORDER);
00544 }
00545
00546 bool AIOrder::ShareOrders(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 | (main_vehicle_id << 16), CO_SHARE, CMD_CLONE_ORDER);
00552 }
00553
00554 bool AIOrder::UnshareOrders(VehicleID vehicle_id)
00555 {
00556 EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
00557
00558 return AIObject::DoCommand(0, vehicle_id, CO_UNSHARE, CMD_CLONE_ORDER);
00559 }