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