Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / script / api / script_order.cpp
blob6b694e396031b06259bcb3deedbb3a64643b81ee
1 /* $Id: script_order.cpp 26148 2013-12-08 15:13:06Z frosch $ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file script_order.cpp Implementation of ScriptOrder. */
12 #include "../../stdafx.h"
13 #include "script_order.hpp"
14 #include "script_cargo.hpp"
15 #include "script_map.hpp"
16 #include "../script_instance.hpp"
17 #include "../../debug.h"
18 #include "../../vehicle_base.h"
19 #include "../../roadstop_base.h"
20 #include "../../dock_base.h"
21 #include "../../depot_base.h"
22 #include "../../station_base.h"
23 #include "../../waypoint_base.h"
25 #include "../../safeguards.h"
27 /**
28 * Gets the order type given a tile
29 * @param t the tile to get the order from
30 * @return the order type, or OT_END when there is no order
32 static OrderType GetOrderTypeByTile(TileIndex t)
34 if (!::IsValidTile(t)) return OT_END;
36 switch (::GetTileType(t)) {
37 default: break;
38 case MP_STATION:
39 if (IsBuoy(t) || IsRailWaypoint(t)) return OT_GOTO_WAYPOINT;
40 if (IsHangar(t)) return OT_GOTO_DEPOT;
41 return OT_GOTO_STATION;
43 case MP_WATER: if (::IsShipDepot(t)) return OT_GOTO_DEPOT; break;
44 case MP_ROAD: if (::GetRoadTileType(t) == ROAD_TILE_DEPOT) return OT_GOTO_DEPOT; break;
45 case MP_RAILWAY:
46 if (IsRailDepot(t)) return OT_GOTO_DEPOT;
47 break;
50 return OT_END;
53 /* static */ bool ScriptOrder::IsValidVehicleOrder(VehicleID vehicle_id, OrderPosition order_position)
55 return ScriptVehicle::IsValidVehicle(vehicle_id) && order_position >= 0 && (order_position < ::Vehicle::Get(vehicle_id)->GetNumManualOrders() || order_position == ORDER_CURRENT);
58 /**
59 * Get the current order the vehicle is executing. If the current order is in
60 * the order list, return the order from the orderlist. If the current order
61 * was a manual order, return the current order.
63 static const Order *ResolveOrder(VehicleID vehicle_id, ScriptOrder::OrderPosition order_position)
65 const Vehicle *v = ::Vehicle::Get(vehicle_id);
66 if (order_position == ScriptOrder::ORDER_CURRENT) {
67 const Order *order = &v->current_order;
68 if (order->GetType() == OT_GOTO_DEPOT && !(order->GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) return order;
69 order_position = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position);
70 if (order_position == ScriptOrder::ORDER_INVALID) return NULL;
72 const Order *order = v->GetFirstOrder();
73 while (order->GetType() == OT_IMPLICIT) order = order->next;
74 while (order_position > 0) {
75 order_position = (ScriptOrder::OrderPosition)(order_position - 1);
76 order = order->next;
77 while (order->GetType() == OT_IMPLICIT) order = order->next;
79 return order;
82 /**
83 * Convert an ScriptOrder::OrderPosition (which is the manual order index) to an order index
84 * as expected by the OpenTTD commands.
85 * @param order_position The OrderPosition to convert.
86 * @return An OpenTTD-internal index for the same order.
88 static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOrder::OrderPosition order_position)
90 const Vehicle *v = ::Vehicle::Get(vehicle_id);
91 if (order_position == v->GetNumManualOrders()) return v->GetNumOrders();
93 assert(ScriptOrder::IsValidVehicleOrder(vehicle_id, order_position));
95 int res = (int)order_position;
96 const Order *order = v->GetFirstOrder();
97 for (; order->GetType() == OT_IMPLICIT; order = order->next) res++;
98 while (order_position > 0) {
99 order_position = (ScriptOrder::OrderPosition)(order_position - 1);
100 order = order->next;
101 for (; order->GetType() == OT_IMPLICIT; order = order->next) res++;
104 return res;
107 /* static */ bool ScriptOrder::IsGotoStationOrder(VehicleID vehicle_id, OrderPosition order_position)
109 if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
111 const Order *order = ::ResolveOrder(vehicle_id, order_position);
112 return order != NULL && order->GetType() == OT_GOTO_STATION;
115 /* static */ bool ScriptOrder::IsGotoDepotOrder(VehicleID vehicle_id, OrderPosition order_position)
117 if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
119 const Order *order = ::ResolveOrder(vehicle_id, order_position);
120 return order != NULL && order->GetType() == OT_GOTO_DEPOT;
123 /* static */ bool ScriptOrder::IsGotoWaypointOrder(VehicleID vehicle_id, OrderPosition order_position)
125 if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
127 const Order *order = ::ResolveOrder(vehicle_id, order_position);
128 return order != NULL && order->GetType() == OT_GOTO_WAYPOINT;
131 /* static */ bool ScriptOrder::IsConditionalOrder(VehicleID vehicle_id, OrderPosition order_position)
133 if (order_position == ORDER_CURRENT) return false;
134 if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
136 const Order *order = ::Vehicle::Get(vehicle_id)->GetOrder(ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position));
137 return order->GetType() == OT_CONDITIONAL;
140 /* static */ bool ScriptOrder::IsVoidOrder(VehicleID vehicle_id, OrderPosition order_position)
142 if (order_position == ORDER_CURRENT) return false;
143 if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
145 const Order *order = ::ResolveOrder(vehicle_id, order_position);
146 return order->GetType() == OT_DUMMY;
149 /* static */ bool ScriptOrder::IsRefitOrder(VehicleID vehicle_id, OrderPosition order_position)
151 if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
153 const Order *order = ::ResolveOrder(vehicle_id, order_position);
154 return order != NULL && order->IsRefit();
157 /* static */ bool ScriptOrder::IsCurrentOrderPartOfOrderList(VehicleID vehicle_id)
159 if (!ScriptVehicle::IsValidVehicle(vehicle_id)) return false;
160 if (GetOrderCount(vehicle_id) == 0) return false;
162 const Order *order = &::Vehicle::Get(vehicle_id)->current_order;
163 if (order->GetType() != OT_GOTO_DEPOT) return true;
164 return (order->GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0;
167 /* static */ ScriptOrder::OrderPosition ScriptOrder::ResolveOrderPosition(VehicleID vehicle_id, OrderPosition order_position)
169 if (!ScriptVehicle::IsValidVehicle(vehicle_id)) return ORDER_INVALID;
171 int num_manual_orders = ::Vehicle::Get(vehicle_id)->GetNumManualOrders();
172 if (num_manual_orders == 0) return ORDER_INVALID;
174 if (order_position == ORDER_CURRENT) {
175 int cur_order_pos = ::Vehicle::Get(vehicle_id)->cur_real_order_index;
176 const Order *order = ::Vehicle::Get(vehicle_id)->GetFirstOrder();
177 int num_implicit_orders = 0;
178 for (int i = 0; i < cur_order_pos; i++) {
179 if (order->GetType() == OT_IMPLICIT) num_implicit_orders++;
180 order = order->next;
182 int real_order_pos = cur_order_pos - num_implicit_orders;
183 assert(real_order_pos < num_manual_orders);
184 return (ScriptOrder::OrderPosition)real_order_pos;
186 return (order_position >= 0 && order_position < num_manual_orders) ? order_position : ORDER_INVALID;
190 /* static */ bool ScriptOrder::AreOrderFlagsValid(TileIndex destination, ScriptOrderFlags order_flags)
192 OrderType ot = (order_flags & OF_GOTO_NEAREST_DEPOT) ? OT_GOTO_DEPOT : ::GetOrderTypeByTile(destination);
193 switch (ot) {
194 case OT_GOTO_STATION:
195 return (order_flags & ~(OF_NON_STOP_FLAGS | OF_UNLOAD_FLAGS | OF_LOAD_FLAGS)) == 0 &&
196 /* Test the different mutual exclusive flags. */
197 ((order_flags & OF_TRANSFER) == 0 || (order_flags & OF_UNLOAD) == 0) &&
198 ((order_flags & OF_TRANSFER) == 0 || (order_flags & OF_NO_UNLOAD) == 0) &&
199 ((order_flags & OF_UNLOAD) == 0 || (order_flags & OF_NO_UNLOAD) == 0) &&
200 ((order_flags & OF_UNLOAD) == 0 || (order_flags & OF_NO_UNLOAD) == 0) &&
201 ((order_flags & OF_NO_UNLOAD) == 0 || (order_flags & OF_NO_LOAD) == 0) &&
202 ((order_flags & OF_FULL_LOAD_ANY) == 0 || (order_flags & OF_NO_LOAD) == 0);
204 case OT_GOTO_DEPOT:
205 return (order_flags & ~(OF_NON_STOP_FLAGS | OF_DEPOT_FLAGS)) == 0 &&
206 ((order_flags & OF_SERVICE_IF_NEEDED) == 0 || (order_flags & OF_STOP_IN_DEPOT) == 0);
208 case OT_GOTO_WAYPOINT: return (order_flags & ~(OF_NON_STOP_FLAGS)) == 0;
209 default: return false;
213 /* static */ bool ScriptOrder::IsValidConditionalOrder(OrderCondition condition, CompareFunction compare)
215 switch (condition) {
216 case OC_LOAD_PERCENTAGE:
217 case OC_RELIABILITY:
218 case OC_MAX_SPEED:
219 case OC_AGE:
220 case OC_REMAINING_LIFETIME:
221 return compare >= CF_EQUALS && compare <= CF_MORE_EQUALS;
223 case OC_REQUIRES_SERVICE:
224 return compare == CF_IS_TRUE || compare == CF_IS_FALSE;
226 case OC_UNCONDITIONALLY:
227 return true;
229 default: return false;
233 /* static */ int32 ScriptOrder::GetOrderCount(VehicleID vehicle_id)
235 return ScriptVehicle::IsValidVehicle(vehicle_id) ? ::Vehicle::Get(vehicle_id)->GetNumManualOrders() : -1;
238 /* static */ TileIndex ScriptOrder::GetOrderDestination(VehicleID vehicle_id, OrderPosition order_position)
240 if (!IsValidVehicleOrder(vehicle_id, order_position)) return INVALID_TILE;
242 const Order *order = ::ResolveOrder(vehicle_id, order_position);
243 if (order == NULL || order->GetType() == OT_CONDITIONAL) return INVALID_TILE;
244 const Vehicle *v = ::Vehicle::Get(vehicle_id);
246 switch (order->GetType()) {
247 case OT_GOTO_DEPOT: {
248 /* We don't know where the nearest depot is... (yet) */
249 if (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) return INVALID_TILE;
251 if (v->type != VEH_AIRCRAFT) return ::Depot::Get(order->GetDestination())->xy;
252 /* Aircraft's hangars are referenced by StationID, not DepotID */
253 const Station *st = ::Station::Get(order->GetDestination());
254 if (!st->airport.HasHangar()) return INVALID_TILE;
255 return st->airport.GetHangarTile(0);
258 case OT_GOTO_STATION: {
259 const Station *st = ::Station::Get(order->GetDestination());
260 if (st->train_station.tile != INVALID_TILE) {
261 TILE_AREA_LOOP(t, st->train_station) {
262 if (st->TileBelongsToRailStation(t)) return t;
264 } else if (st->docks != NULL) {
265 return st->docks->flat;
266 } else if (st->bus_stops != NULL) {
267 return st->bus_stops->xy;
268 } else if (st->truck_stops != NULL) {
269 return st->truck_stops->xy;
270 } else if (st->airport.tile != INVALID_TILE) {
271 TILE_AREA_LOOP(tile, st->airport) {
272 if (st->TileBelongsToAirport(tile) && !::IsHangar(tile)) return tile;
275 return INVALID_TILE;
278 case OT_GOTO_WAYPOINT: {
279 const Waypoint *wp = ::Waypoint::Get(order->GetDestination());
280 if (wp->train_station.tile != INVALID_TILE) {
281 TILE_AREA_LOOP(t, wp->train_station) {
282 if (wp->TileBelongsToRailStation(t)) return t;
285 /* If the waypoint has no rail waypoint tiles, it must have a buoy */
286 return wp->xy;
288 default: return INVALID_TILE;
292 /* static */ ScriptOrder::ScriptOrderFlags ScriptOrder::GetOrderFlags(VehicleID vehicle_id, OrderPosition order_position)
294 if (!IsValidVehicleOrder(vehicle_id, order_position)) return OF_INVALID;
296 const Order *order = ::ResolveOrder(vehicle_id, order_position);
297 if (order == NULL || order->GetType() == OT_CONDITIONAL || order->GetType() == OT_DUMMY) return OF_INVALID;
299 ScriptOrderFlags order_flags = OF_NONE;
300 order_flags |= (ScriptOrderFlags)order->GetNonStopType();
301 switch (order->GetType()) {
302 case OT_GOTO_DEPOT:
303 if (order->GetDepotOrderType() & ODTFB_SERVICE) order_flags |= OF_SERVICE_IF_NEEDED;
304 if (order->GetDepotActionType() & ODATFB_HALT) order_flags |= OF_STOP_IN_DEPOT;
305 if (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) order_flags |= OF_GOTO_NEAREST_DEPOT;
306 break;
308 case OT_GOTO_STATION:
309 order_flags |= (ScriptOrderFlags)(order->GetLoadType() << 5);
310 order_flags |= (ScriptOrderFlags)(order->GetUnloadType() << 2);
311 break;
313 default: break;
316 return order_flags;
319 /* static */ ScriptOrder::OrderPosition ScriptOrder::GetOrderJumpTo(VehicleID vehicle_id, OrderPosition order_position)
321 if (!IsValidVehicleOrder(vehicle_id, order_position)) return ORDER_INVALID;
322 if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return ORDER_INVALID;
324 const Order *order = ::ResolveOrder(vehicle_id, order_position);
325 return (OrderPosition)order->GetConditionSkipToOrder();
328 /* static */ ScriptOrder::OrderCondition ScriptOrder::GetOrderCondition(VehicleID vehicle_id, OrderPosition order_position)
330 if (!IsValidVehicleOrder(vehicle_id, order_position)) return OC_INVALID;
331 if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return OC_INVALID;
333 const Order *order = ::ResolveOrder(vehicle_id, order_position);
334 return (OrderCondition)order->GetConditionVariable();
337 /* static */ ScriptOrder::CompareFunction ScriptOrder::GetOrderCompareFunction(VehicleID vehicle_id, OrderPosition order_position)
339 if (!IsValidVehicleOrder(vehicle_id, order_position)) return CF_INVALID;
340 if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return CF_INVALID;
342 const Order *order = ::ResolveOrder(vehicle_id, order_position);
343 return (CompareFunction)order->GetConditionComparator();
346 /* static */ int32 ScriptOrder::GetOrderCompareValue(VehicleID vehicle_id, OrderPosition order_position)
348 if (!IsValidVehicleOrder(vehicle_id, order_position)) return -1;
349 if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return -1;
351 const Order *order = ::ResolveOrder(vehicle_id, order_position);
352 int32 value = order->GetConditionValue();
353 if (order->GetConditionVariable() == OCV_MAX_SPEED) value = value * 16 / 10;
354 return value;
357 /* static */ ScriptOrder::StopLocation ScriptOrder::GetStopLocation(VehicleID vehicle_id, OrderPosition order_position)
359 if (!IsValidVehicleOrder(vehicle_id, order_position)) return STOPLOCATION_INVALID;
360 if (ScriptVehicle::GetVehicleType(vehicle_id) != ScriptVehicle::VT_RAIL) return STOPLOCATION_INVALID;
361 if (!IsGotoStationOrder(vehicle_id, order_position)) return STOPLOCATION_INVALID;
363 const Order *order = ::ResolveOrder(vehicle_id, order_position);
364 return (ScriptOrder::StopLocation)order->GetStopLocation();
367 /* static */ CargoID ScriptOrder::GetOrderRefit(VehicleID vehicle_id, OrderPosition order_position)
369 if (!IsValidVehicleOrder(vehicle_id, order_position)) return CT_NO_REFIT;
370 if (order_position != ORDER_CURRENT && !IsGotoStationOrder(vehicle_id, order_position) && !IsGotoDepotOrder(vehicle_id, order_position)) return CT_NO_REFIT;
372 const Order *order = ::ResolveOrder(vehicle_id, order_position);
373 return order->IsRefit() ? order->GetRefitCargo() : (CargoID)CT_NO_REFIT;
376 /* static */ bool ScriptOrder::SetOrderJumpTo(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
378 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
379 EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
380 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to) && jump_to != ORDER_CURRENT);
382 return ScriptObject::DoCommand(0, vehicle_id | (order_position << 20), MOF_COND_DESTINATION | (jump_to << 4), CMD_MODIFY_ORDER);
385 /* static */ bool ScriptOrder::SetOrderCondition(VehicleID vehicle_id, OrderPosition order_position, OrderCondition condition)
387 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
388 EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
389 EnforcePrecondition(false, condition >= OC_LOAD_PERCENTAGE && condition <= OC_REMAINING_LIFETIME);
391 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
392 return ScriptObject::DoCommand(0, vehicle_id | (order_pos << 20), MOF_COND_VARIABLE | (condition << 4), CMD_MODIFY_ORDER);
395 /* static */ bool ScriptOrder::SetOrderCompareFunction(VehicleID vehicle_id, OrderPosition order_position, CompareFunction compare)
397 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
398 EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
399 EnforcePrecondition(false, compare >= CF_EQUALS && compare <= CF_IS_FALSE);
401 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
402 return ScriptObject::DoCommand(0, vehicle_id | (order_pos << 20), MOF_COND_COMPARATOR | (compare << 4), CMD_MODIFY_ORDER);
405 /* static */ bool ScriptOrder::SetOrderCompareValue(VehicleID vehicle_id, OrderPosition order_position, int32 value)
407 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
408 EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
409 EnforcePrecondition(false, value >= 0 && value < 2048);
410 if (GetOrderCondition(vehicle_id, order_position) == OC_MAX_SPEED) value = value * 10 / 16;
412 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
413 return ScriptObject::DoCommand(0, vehicle_id | (order_pos << 20), MOF_COND_VALUE | (value << 4), CMD_MODIFY_ORDER);
416 /* static */ bool ScriptOrder::SetStopLocation(VehicleID vehicle_id, OrderPosition order_position, StopLocation stop_location)
418 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
419 EnforcePrecondition(false, ScriptVehicle::GetVehicleType(vehicle_id) == ScriptVehicle::VT_RAIL);
420 EnforcePrecondition(false, IsGotoStationOrder(vehicle_id, order_position));
421 EnforcePrecondition(false, stop_location >= STOPLOCATION_NEAR && stop_location <= STOPLOCATION_FAR);
423 order_position = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position);
425 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
426 uint32 p1 = vehicle_id | (order_pos << 20);
427 uint32 p2 = MOF_STOP_LOCATION | (stop_location << 4);
428 return ScriptObject::DoCommand(0, p1, p2, CMD_MODIFY_ORDER);
431 /* static */ bool ScriptOrder::SetOrderRefit(VehicleID vehicle_id, OrderPosition order_position, CargoID refit_cargo)
433 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
434 EnforcePrecondition(false, IsGotoStationOrder(vehicle_id, order_position) || (IsGotoDepotOrder(vehicle_id, order_position) && refit_cargo != CT_AUTO_REFIT));
435 EnforcePrecondition(false, ScriptCargo::IsValidCargo(refit_cargo) || refit_cargo == CT_AUTO_REFIT || refit_cargo == CT_NO_REFIT);
437 uint32 p1 = vehicle_id;
438 uint32 p2 = refit_cargo | ScriptOrderPositionToRealOrderPosition(vehicle_id, ScriptOrder::ResolveOrderPosition(vehicle_id, order_position)) << 16;
439 return ScriptObject::DoCommand(0, p1, p2, CMD_ORDER_REFIT);
442 /* static */ bool ScriptOrder::AppendOrder(VehicleID vehicle_id, TileIndex destination, ScriptOrderFlags order_flags)
444 EnforcePrecondition(false, ScriptVehicle::IsValidVehicle(vehicle_id));
445 EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
447 return InsertOrder(vehicle_id, (ScriptOrder::OrderPosition)::Vehicle::Get(vehicle_id)->GetNumManualOrders(), destination, order_flags);
450 /* static */ bool ScriptOrder::AppendConditionalOrder(VehicleID vehicle_id, OrderPosition jump_to)
452 EnforcePrecondition(false, ScriptVehicle::IsValidVehicle(vehicle_id));
453 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to));
455 return InsertConditionalOrder(vehicle_id, (ScriptOrder::OrderPosition)::Vehicle::Get(vehicle_id)->GetNumManualOrders(), jump_to);
458 /* static */ bool ScriptOrder::InsertOrder(VehicleID vehicle_id, OrderPosition order_position, TileIndex destination, ScriptOrder::ScriptOrderFlags order_flags)
460 /* IsValidVehicleOrder is not good enough because it does not allow appending. */
461 if (order_position == ORDER_CURRENT) order_position = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position);
463 EnforcePrecondition(false, ScriptVehicle::IsValidVehicle(vehicle_id));
464 EnforcePrecondition(false, order_position >= 0 && order_position <= ::Vehicle::Get(vehicle_id)->GetNumManualOrders());
465 EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
467 Order order;
468 OrderType ot = (order_flags & OF_GOTO_NEAREST_DEPOT) ? OT_GOTO_DEPOT : ::GetOrderTypeByTile(destination);
469 switch (ot) {
470 case OT_GOTO_DEPOT: {
471 OrderDepotTypeFlags odtf = (OrderDepotTypeFlags)(ODTFB_PART_OF_ORDERS | ((order_flags & OF_SERVICE_IF_NEEDED) ? ODTFB_SERVICE : 0));
472 OrderDepotActionFlags odaf = (OrderDepotActionFlags)(ODATF_SERVICE_ONLY | ((order_flags & OF_STOP_IN_DEPOT) ? ODATFB_HALT : 0));
473 if (order_flags & OF_GOTO_NEAREST_DEPOT) odaf |= ODATFB_NEAREST_DEPOT;
474 OrderNonStopFlags onsf = (OrderNonStopFlags)((order_flags & OF_NON_STOP_INTERMEDIATE) ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
475 if (order_flags & OF_GOTO_NEAREST_DEPOT) {
476 order.MakeGoToDepot(0, odtf, onsf, odaf);
477 } else {
478 /* Check explicitly if the order is to a station (for aircraft) or
479 * to a depot (other vehicle types). */
480 if (::Vehicle::Get(vehicle_id)->type == VEH_AIRCRAFT) {
481 if (!::IsTileType(destination, MP_STATION)) return false;
482 order.MakeGoToDepot(::GetStationIndex(destination), odtf, onsf, odaf);
483 } else {
484 if (::IsTileType(destination, MP_STATION)) return false;
485 order.MakeGoToDepot(::GetDepotIndex(destination), odtf, onsf, odaf);
488 break;
491 case OT_GOTO_STATION:
492 order.MakeGoToStation(::GetStationIndex(destination));
493 order.SetLoadType((OrderLoadFlags)GB(order_flags, 5, 3));
494 order.SetUnloadType((OrderUnloadFlags)GB(order_flags, 2, 3));
495 order.SetStopLocation(OSL_PLATFORM_FAR_END);
496 break;
498 case OT_GOTO_WAYPOINT:
499 order.MakeGoToWaypoint(::GetStationIndex(destination));
500 break;
502 default:
503 return false;
506 order.SetNonStopType((OrderNonStopFlags)GB(order_flags, 0, 2));
508 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
509 return ScriptObject::DoCommand(0, vehicle_id | (order_pos << 20), order.Pack(), CMD_INSERT_ORDER);
512 /* static */ bool ScriptOrder::InsertConditionalOrder(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
514 /* IsValidVehicleOrder is not good enough because it does not allow appending. */
515 if (order_position == ORDER_CURRENT) order_position = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position);
517 EnforcePrecondition(false, ScriptVehicle::IsValidVehicle(vehicle_id));
518 EnforcePrecondition(false, order_position >= 0 && order_position <= ::Vehicle::Get(vehicle_id)->GetNumManualOrders());
519 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to) && jump_to != ORDER_CURRENT);
521 Order order;
522 order.MakeConditional(jump_to);
524 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
525 return ScriptObject::DoCommand(0, vehicle_id | (order_pos << 20), order.Pack(), CMD_INSERT_ORDER);
528 /* static */ bool ScriptOrder::RemoveOrder(VehicleID vehicle_id, OrderPosition order_position)
530 order_position = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position);
532 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
534 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
535 return ScriptObject::DoCommand(0, vehicle_id, order_pos, CMD_DELETE_ORDER);
538 /* static */ bool ScriptOrder::SkipToOrder(VehicleID vehicle_id, OrderPosition next_order)
540 next_order = ScriptOrder::ResolveOrderPosition(vehicle_id, next_order);
542 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, next_order));
544 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, next_order);
545 return ScriptObject::DoCommand(0, vehicle_id, order_pos, CMD_SKIP_TO_ORDER);
549 * Callback handler as SetOrderFlags possibly needs multiple DoCommand calls
550 * to be able to set all order flags correctly. As we need to wait till the
551 * command has completed before we know the next bits to change we need to
552 * call the function multiple times. Each time it'll reduce the difference
553 * between the wanted and the current order.
554 * @param instance The script instance we are doing the callback for.
556 static void _DoCommandReturnSetOrderFlags(class ScriptInstance *instance)
558 ScriptObject::SetLastCommandRes(ScriptOrder::_SetOrderFlags());
559 ScriptInstance::DoCommandReturn(instance);
562 /* static */ bool ScriptOrder::_SetOrderFlags()
564 /* Make sure we don't go into an infinite loop */
565 int retry = ScriptObject::GetCallbackVariable(3) - 1;
566 if (retry < 0) {
567 DEBUG(script, 0, "Possible infinite loop in SetOrderFlags() detected");
568 return false;
570 ScriptObject::SetCallbackVariable(3, retry);
572 VehicleID vehicle_id = (VehicleID)ScriptObject::GetCallbackVariable(0);
573 OrderPosition order_position = (OrderPosition)ScriptObject::GetCallbackVariable(1);
574 ScriptOrderFlags order_flags = (ScriptOrderFlags)ScriptObject::GetCallbackVariable(2);
576 order_position = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position);
578 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
579 EnforcePrecondition(false, AreOrderFlagsValid(GetOrderDestination(vehicle_id, order_position), order_flags));
581 const Order *order = ::ResolveOrder(vehicle_id, order_position);
582 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
584 ScriptOrderFlags current = GetOrderFlags(vehicle_id, order_position);
586 EnforcePrecondition(false, (order_flags & OF_GOTO_NEAREST_DEPOT) == (current & OF_GOTO_NEAREST_DEPOT));
588 if ((current & OF_NON_STOP_FLAGS) != (order_flags & OF_NON_STOP_FLAGS)) {
589 return ScriptObject::DoCommand(0, vehicle_id | (order_pos << 20), (order_flags & OF_NON_STOP_FLAGS) << 4 | MOF_NON_STOP, CMD_MODIFY_ORDER, NULL, &::_DoCommandReturnSetOrderFlags);
592 switch (order->GetType()) {
593 case OT_GOTO_DEPOT:
594 if ((current & OF_DEPOT_FLAGS) != (order_flags & OF_DEPOT_FLAGS)) {
595 uint data = DA_ALWAYS_GO;
596 if (order_flags & OF_SERVICE_IF_NEEDED) data = DA_SERVICE;
597 if (order_flags & OF_STOP_IN_DEPOT) data = DA_STOP;
598 return ScriptObject::DoCommand(0, vehicle_id | (order_pos << 20), (data << 4) | MOF_DEPOT_ACTION, CMD_MODIFY_ORDER, NULL, &::_DoCommandReturnSetOrderFlags);
600 break;
602 case OT_GOTO_STATION:
603 if ((current & OF_UNLOAD_FLAGS) != (order_flags & OF_UNLOAD_FLAGS)) {
604 return ScriptObject::DoCommand(0, vehicle_id | (order_pos << 20), (order_flags & OF_UNLOAD_FLAGS) << 2 | MOF_UNLOAD, CMD_MODIFY_ORDER, NULL, &::_DoCommandReturnSetOrderFlags);
606 if ((current & OF_LOAD_FLAGS) != (order_flags & OF_LOAD_FLAGS)) {
607 return ScriptObject::DoCommand(0, vehicle_id | (order_pos << 20), (order_flags & OF_LOAD_FLAGS) >> 1 | MOF_LOAD, CMD_MODIFY_ORDER, NULL, &::_DoCommandReturnSetOrderFlags);
609 break;
611 default: break;
614 assert(GetOrderFlags(vehicle_id, order_position) == order_flags);
616 return true;
619 /* static */ bool ScriptOrder::SetOrderFlags(VehicleID vehicle_id, OrderPosition order_position, ScriptOrder::ScriptOrderFlags order_flags)
621 ScriptObject::SetCallbackVariable(0, vehicle_id);
622 ScriptObject::SetCallbackVariable(1, order_position);
623 ScriptObject::SetCallbackVariable(2, order_flags);
624 /* In case another client(s) change orders at the same time we could
625 * end in an infinite loop. This stops that from happening ever. */
626 ScriptObject::SetCallbackVariable(3, 8);
627 return ScriptOrder::_SetOrderFlags();
630 /* static */ bool ScriptOrder::MoveOrder(VehicleID vehicle_id, OrderPosition order_position_move, OrderPosition order_position_target)
632 order_position_move = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position_move);
633 order_position_target = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position_target);
635 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position_move));
636 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position_target));
637 EnforcePrecondition(false, order_position_move != order_position_target);
639 int order_pos_move = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position_move);
640 int order_pos_target = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position_target);
641 return ScriptObject::DoCommand(0, vehicle_id, order_pos_move | (order_pos_target << 16), CMD_MOVE_ORDER);
644 /* static */ bool ScriptOrder::CopyOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
646 EnforcePrecondition(false, ScriptVehicle::IsValidVehicle(vehicle_id));
647 EnforcePrecondition(false, ScriptVehicle::IsValidVehicle(main_vehicle_id));
649 return ScriptObject::DoCommand(0, vehicle_id | CO_COPY << 30, main_vehicle_id, CMD_CLONE_ORDER);
652 /* static */ bool ScriptOrder::ShareOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
654 EnforcePrecondition(false, ScriptVehicle::IsValidVehicle(vehicle_id));
655 EnforcePrecondition(false, ScriptVehicle::IsValidVehicle(main_vehicle_id));
657 return ScriptObject::DoCommand(0, vehicle_id | CO_SHARE << 30, main_vehicle_id, CMD_CLONE_ORDER);
660 /* static */ bool ScriptOrder::UnshareOrders(VehicleID vehicle_id)
662 EnforcePrecondition(false, ScriptVehicle::IsValidVehicle(vehicle_id));
664 return ScriptObject::DoCommand(0, vehicle_id | CO_UNSHARE << 30, 0, CMD_CLONE_ORDER);
667 /* static */ uint ScriptOrder::GetOrderDistance(ScriptVehicle::VehicleType vehicle_type, TileIndex origin_tile, TileIndex dest_tile)
669 if (vehicle_type == ScriptVehicle::VT_AIR) {
670 if (ScriptTile::IsStationTile(origin_tile) && ::Station::GetByTile(origin_tile)->airport.tile != INVALID_TILE) origin_tile = ::Station::GetByTile(origin_tile)->airport.tile;
671 if (ScriptTile::IsStationTile(dest_tile) && ::Station::GetByTile(dest_tile)->airport.tile != INVALID_TILE) dest_tile = ::Station::GetByTile(dest_tile)->airport.tile;
673 return ScriptMap::DistanceSquare(origin_tile, dest_tile);
674 } else {
675 return ScriptMap::DistanceManhattan(origin_tile, dest_tile);