Update: Translations from eints
[openttd-github.git] / src / script / api / script_order.cpp
blobb68f7fd67a7f23d8f81e6f3fe1bc6058746bfce8
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file script_order.cpp Implementation of ScriptOrder. */
10 #include "../../stdafx.h"
11 #include "script_order.hpp"
12 #include "script_cargo.hpp"
13 #include "script_map.hpp"
14 #include "../script_instance.hpp"
15 #include "../../debug.h"
16 #include "../../vehicle_base.h"
17 #include "../../roadstop_base.h"
18 #include "../../depot_base.h"
19 #include "../../station_base.h"
20 #include "../../waypoint_base.h"
21 #include "../../order_cmd.h"
23 #include "../../safeguards.h"
25 /**
26 * Gets the order type given a tile
27 * @param t the tile to get the order from
28 * @return the order type, or OT_END when there is no order
30 static OrderType GetOrderTypeByTile(TileIndex t)
32 if (!::IsValidTile(t)) return OT_END;
34 switch (::GetTileType(t)) {
35 default: break;
36 case MP_STATION:
37 if (IsBuoy(t) || IsRailWaypoint(t)) return OT_GOTO_WAYPOINT;
38 if (IsHangar(t)) return OT_GOTO_DEPOT;
39 return OT_GOTO_STATION;
41 case MP_WATER: if (::IsShipDepot(t)) return OT_GOTO_DEPOT; break;
42 case MP_ROAD: if (::GetRoadTileType(t) == ROAD_TILE_DEPOT) return OT_GOTO_DEPOT; break;
43 case MP_RAILWAY:
44 if (IsRailDepot(t)) return OT_GOTO_DEPOT;
45 break;
48 return OT_END;
51 /* static */ bool ScriptOrder::IsValidVehicleOrder(VehicleID vehicle_id, OrderPosition order_position)
53 return ScriptVehicle::IsPrimaryVehicle(vehicle_id) && order_position >= 0 && (order_position < ::Vehicle::Get(vehicle_id)->GetNumManualOrders() || order_position == ORDER_CURRENT);
56 /**
57 * Get the current order the vehicle is executing. If the current order is in
58 * the order list, return the order from the orderlist. If the current order
59 * was a manual order, return the current order.
61 static const Order *ResolveOrder(VehicleID vehicle_id, ScriptOrder::OrderPosition order_position)
63 const Vehicle *v = ::Vehicle::Get(vehicle_id);
64 if (order_position == ScriptOrder::ORDER_CURRENT) {
65 const Order *order = &v->current_order;
66 if (order->GetType() == OT_GOTO_DEPOT && !(order->GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) return order;
67 order_position = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position);
68 if (order_position == ScriptOrder::ORDER_INVALID) return nullptr;
70 const Order *order = v->GetFirstOrder();
71 assert(order != nullptr);
72 while (order->GetType() == OT_IMPLICIT) order = order->next;
73 while (order_position > 0) {
74 order_position = (ScriptOrder::OrderPosition)(order_position - 1);
75 order = order->next;
76 while (order->GetType() == OT_IMPLICIT) order = order->next;
78 return order;
81 /**
82 * Convert an ScriptOrder::OrderPosition (which is the manual order index) to an order index
83 * as expected by the OpenTTD commands.
84 * @param order_position The OrderPosition to convert.
85 * @return An OpenTTD-internal index for the same order.
87 static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOrder::OrderPosition order_position)
89 const Vehicle *v = ::Vehicle::Get(vehicle_id);
90 if (order_position == v->GetNumManualOrders()) return v->GetNumOrders();
92 assert(ScriptOrder::IsValidVehicleOrder(vehicle_id, order_position));
94 int res = (int)order_position;
95 const Order *order = v->orders->GetFirstOrder();
96 assert(order != nullptr);
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 != nullptr && 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 != nullptr && 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 != nullptr && 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 assert(order != nullptr);
138 return order->GetType() == OT_CONDITIONAL;
141 /* static */ bool ScriptOrder::IsVoidOrder(VehicleID vehicle_id, OrderPosition order_position)
143 if (order_position == ORDER_CURRENT) return false;
144 if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
146 const Order *order = ::ResolveOrder(vehicle_id, order_position);
147 assert(order != nullptr);
148 return order->GetType() == OT_DUMMY;
151 /* static */ bool ScriptOrder::IsRefitOrder(VehicleID vehicle_id, OrderPosition order_position)
153 if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
155 const Order *order = ::ResolveOrder(vehicle_id, order_position);
156 return order != nullptr && order->IsRefit();
159 /* static */ bool ScriptOrder::IsCurrentOrderPartOfOrderList(VehicleID vehicle_id)
161 if (!ScriptVehicle::IsPrimaryVehicle(vehicle_id)) return false;
162 if (GetOrderCount(vehicle_id) == 0) return false;
164 const Order *order = &::Vehicle::Get(vehicle_id)->current_order;
165 if (order->GetType() != OT_GOTO_DEPOT) return true;
166 return (order->GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0;
169 /* static */ ScriptOrder::OrderPosition ScriptOrder::ResolveOrderPosition(VehicleID vehicle_id, OrderPosition order_position)
171 if (!ScriptVehicle::IsPrimaryVehicle(vehicle_id)) return ORDER_INVALID;
173 int num_manual_orders = ::Vehicle::Get(vehicle_id)->GetNumManualOrders();
174 if (num_manual_orders == 0) return ORDER_INVALID;
176 if (order_position == ORDER_CURRENT) {
177 int cur_order_pos = ::Vehicle::Get(vehicle_id)->cur_real_order_index;
178 const Order *order = ::Vehicle::Get(vehicle_id)->GetFirstOrder();
179 assert(order != nullptr);
180 int num_implicit_orders = 0;
181 for (int i = 0; i < cur_order_pos; i++) {
182 if (order->GetType() == OT_IMPLICIT) num_implicit_orders++;
183 order = order->next;
185 int real_order_pos = cur_order_pos - num_implicit_orders;
186 assert(real_order_pos < num_manual_orders);
187 return (ScriptOrder::OrderPosition)real_order_pos;
189 return (order_position >= 0 && order_position < num_manual_orders) ? order_position : ORDER_INVALID;
193 /* static */ bool ScriptOrder::AreOrderFlagsValid(TileIndex destination, ScriptOrderFlags order_flags)
195 OrderType ot = (order_flags & OF_GOTO_NEAREST_DEPOT) ? OT_GOTO_DEPOT : ::GetOrderTypeByTile(destination);
196 switch (ot) {
197 case OT_GOTO_STATION:
198 return (order_flags & ~(OF_NON_STOP_FLAGS | OF_UNLOAD_FLAGS | OF_LOAD_FLAGS)) == 0 &&
199 /* Test the different mutual exclusive flags. */
200 ((order_flags & OF_TRANSFER) == 0 || (order_flags & OF_UNLOAD) == 0) &&
201 ((order_flags & OF_TRANSFER) == 0 || (order_flags & OF_NO_UNLOAD) == 0) &&
202 ((order_flags & OF_UNLOAD) == 0 || (order_flags & OF_NO_UNLOAD) == 0) &&
203 ((order_flags & OF_UNLOAD) == 0 || (order_flags & OF_NO_UNLOAD) == 0) &&
204 ((order_flags & OF_NO_UNLOAD) == 0 || (order_flags & OF_NO_LOAD) == 0) &&
205 ((order_flags & OF_FULL_LOAD_ANY) == 0 || (order_flags & OF_NO_LOAD) == 0);
207 case OT_GOTO_DEPOT:
208 return (order_flags & ~(OF_NON_STOP_FLAGS | OF_DEPOT_FLAGS)) == 0 &&
209 ((order_flags & OF_SERVICE_IF_NEEDED) == 0 || (order_flags & OF_STOP_IN_DEPOT) == 0);
211 case OT_GOTO_WAYPOINT: return (order_flags & ~(OF_NON_STOP_FLAGS)) == 0;
212 default: return false;
216 /* static */ bool ScriptOrder::IsValidConditionalOrder(OrderCondition condition, CompareFunction compare)
218 switch (condition) {
219 case OC_LOAD_PERCENTAGE:
220 case OC_RELIABILITY:
221 case OC_MAX_RELIABILITY:
222 case OC_MAX_SPEED:
223 case OC_AGE:
224 case OC_REMAINING_LIFETIME:
225 return compare >= CF_EQUALS && compare <= CF_MORE_EQUALS;
227 case OC_REQUIRES_SERVICE:
228 return compare == CF_IS_TRUE || compare == CF_IS_FALSE;
230 case OC_UNCONDITIONALLY:
231 return true;
233 default: return false;
237 /* static */ SQInteger ScriptOrder::GetOrderCount(VehicleID vehicle_id)
239 return ScriptVehicle::IsPrimaryVehicle(vehicle_id) ? ::Vehicle::Get(vehicle_id)->GetNumManualOrders() : -1;
242 /* static */ TileIndex ScriptOrder::GetOrderDestination(VehicleID vehicle_id, OrderPosition order_position)
244 if (!IsValidVehicleOrder(vehicle_id, order_position)) return INVALID_TILE;
246 const Order *order = ::ResolveOrder(vehicle_id, order_position);
247 if (order == nullptr || order->GetType() == OT_CONDITIONAL) return INVALID_TILE;
248 const Vehicle *v = ::Vehicle::Get(vehicle_id);
250 switch (order->GetType()) {
251 case OT_GOTO_DEPOT: {
252 /* We don't know where the nearest depot is... (yet) */
253 if (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) return INVALID_TILE;
255 if (v->type != VEH_AIRCRAFT) return ::Depot::Get(order->GetDestination())->xy;
256 /* Aircraft's hangars are referenced by StationID, not DepotID */
257 const Station *st = ::Station::Get(order->GetDestination());
258 if (!st->airport.HasHangar()) return INVALID_TILE;
259 return st->airport.GetHangarTile(0);
262 case OT_GOTO_STATION: {
263 const Station *st = ::Station::Get(order->GetDestination());
264 if (st->train_station.tile != INVALID_TILE) {
265 for (TileIndex t : st->train_station) {
266 if (st->TileBelongsToRailStation(t)) return t;
268 } else if (st->ship_station.tile != INVALID_TILE) {
269 for (TileIndex t : st->ship_station) {
270 if (IsTileType(t, MP_STATION) && (IsDock(t) || IsOilRig(t)) && GetStationIndex(t) == st->index) return t;
272 } else if (st->bus_stops != nullptr) {
273 return st->bus_stops->xy;
274 } else if (st->truck_stops != nullptr) {
275 return st->truck_stops->xy;
276 } else if (st->airport.tile != INVALID_TILE) {
277 for (TileIndex tile : st->airport) {
278 if (st->TileBelongsToAirport(tile) && !::IsHangar(tile)) return tile;
281 return INVALID_TILE;
284 case OT_GOTO_WAYPOINT: {
285 const Waypoint *wp = ::Waypoint::Get(order->GetDestination());
286 if (wp->train_station.tile != INVALID_TILE) {
287 for (TileIndex t : wp->train_station) {
288 if (wp->TileBelongsToRailStation(t)) return t;
291 /* If the waypoint has no rail waypoint tiles, it must have a buoy */
292 return wp->xy;
294 default: return INVALID_TILE;
298 /* static */ ScriptOrder::ScriptOrderFlags ScriptOrder::GetOrderFlags(VehicleID vehicle_id, OrderPosition order_position)
300 if (!IsValidVehicleOrder(vehicle_id, order_position)) return OF_INVALID;
302 const Order *order = ::ResolveOrder(vehicle_id, order_position);
303 if (order == nullptr || order->GetType() == OT_CONDITIONAL || order->GetType() == OT_DUMMY) return OF_INVALID;
305 ScriptOrderFlags order_flags = OF_NONE;
306 order_flags |= (ScriptOrderFlags)order->GetNonStopType();
307 switch (order->GetType()) {
308 case OT_GOTO_DEPOT:
309 if (order->GetDepotOrderType() & ODTFB_SERVICE) order_flags |= OF_SERVICE_IF_NEEDED;
310 if (order->GetDepotActionType() & ODATFB_HALT) order_flags |= OF_STOP_IN_DEPOT;
311 if (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) order_flags |= OF_GOTO_NEAREST_DEPOT;
312 break;
314 case OT_GOTO_STATION:
315 order_flags |= (ScriptOrderFlags)(order->GetLoadType() << 5);
316 order_flags |= (ScriptOrderFlags)(order->GetUnloadType() << 2);
317 break;
319 default: break;
322 return order_flags;
325 /* static */ ScriptOrder::OrderPosition ScriptOrder::GetOrderJumpTo(VehicleID vehicle_id, OrderPosition order_position)
327 if (!IsValidVehicleOrder(vehicle_id, order_position)) return ORDER_INVALID;
328 if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return ORDER_INVALID;
330 const Order *order = ::ResolveOrder(vehicle_id, order_position);
331 return (OrderPosition)order->GetConditionSkipToOrder();
334 /* static */ ScriptOrder::OrderCondition ScriptOrder::GetOrderCondition(VehicleID vehicle_id, OrderPosition order_position)
336 if (!IsValidVehicleOrder(vehicle_id, order_position)) return OC_INVALID;
337 if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return OC_INVALID;
339 const Order *order = ::ResolveOrder(vehicle_id, order_position);
340 return (OrderCondition)order->GetConditionVariable();
343 /* static */ ScriptOrder::CompareFunction ScriptOrder::GetOrderCompareFunction(VehicleID vehicle_id, OrderPosition order_position)
345 if (!IsValidVehicleOrder(vehicle_id, order_position)) return CF_INVALID;
346 if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return CF_INVALID;
348 const Order *order = ::ResolveOrder(vehicle_id, order_position);
349 return (CompareFunction)order->GetConditionComparator();
352 /* static */ SQInteger ScriptOrder::GetOrderCompareValue(VehicleID vehicle_id, OrderPosition order_position)
354 if (!IsValidVehicleOrder(vehicle_id, order_position)) return -1;
355 if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return -1;
357 const Order *order = ::ResolveOrder(vehicle_id, order_position);
358 SQInteger value = order->GetConditionValue();
359 if (order->GetConditionVariable() == OCV_MAX_SPEED) value = value * 16 / 10;
360 return value;
363 /* static */ ScriptOrder::StopLocation ScriptOrder::GetStopLocation(VehicleID vehicle_id, OrderPosition order_position)
365 if (!IsValidVehicleOrder(vehicle_id, order_position)) return STOPLOCATION_INVALID;
366 if (ScriptVehicle::GetVehicleType(vehicle_id) != ScriptVehicle::VT_RAIL) return STOPLOCATION_INVALID;
367 if (!IsGotoStationOrder(vehicle_id, order_position)) return STOPLOCATION_INVALID;
369 const Order *order = ::ResolveOrder(vehicle_id, order_position);
370 return (ScriptOrder::StopLocation)order->GetStopLocation();
373 /* static */ CargoID ScriptOrder::GetOrderRefit(VehicleID vehicle_id, OrderPosition order_position)
375 if (!IsValidVehicleOrder(vehicle_id, order_position)) return CARGO_NO_REFIT;
376 if (order_position != ORDER_CURRENT && !IsGotoStationOrder(vehicle_id, order_position) && !IsGotoDepotOrder(vehicle_id, order_position)) return CARGO_NO_REFIT;
378 const Order *order = ::ResolveOrder(vehicle_id, order_position);
379 return order->IsRefit() ? order->GetRefitCargo() : CARGO_NO_REFIT;
382 /* static */ bool ScriptOrder::SetOrderJumpTo(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
384 EnforceCompanyModeValid(false);
385 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
386 EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
387 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to) && jump_to != ORDER_CURRENT);
389 return ScriptObject::Command<CMD_MODIFY_ORDER>::Do(0, vehicle_id, order_position, MOF_COND_DESTINATION, jump_to);
392 /* static */ bool ScriptOrder::SetOrderCondition(VehicleID vehicle_id, OrderPosition order_position, OrderCondition condition)
394 EnforceCompanyModeValid(false);
395 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
396 EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
397 EnforcePrecondition(false, condition >= OC_LOAD_PERCENTAGE && condition <= OC_REMAINING_LIFETIME);
399 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
400 return ScriptObject::Command<CMD_MODIFY_ORDER>::Do(0, vehicle_id, order_pos, MOF_COND_VARIABLE, condition);
403 /* static */ bool ScriptOrder::SetOrderCompareFunction(VehicleID vehicle_id, OrderPosition order_position, CompareFunction compare)
405 EnforceCompanyModeValid(false);
406 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
407 EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
408 EnforcePrecondition(false, compare >= CF_EQUALS && compare <= CF_IS_FALSE);
410 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
411 return ScriptObject::Command<CMD_MODIFY_ORDER>::Do(0, vehicle_id, order_pos, MOF_COND_COMPARATOR, compare);
414 /* static */ bool ScriptOrder::SetOrderCompareValue(VehicleID vehicle_id, OrderPosition order_position, SQInteger value)
416 EnforceCompanyModeValid(false);
417 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
418 EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
419 EnforcePrecondition(false, value >= 0 && value < 2048);
420 if (GetOrderCondition(vehicle_id, order_position) == OC_MAX_SPEED) value = value * 10 / 16;
422 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
423 return ScriptObject::Command<CMD_MODIFY_ORDER>::Do(0, vehicle_id, order_pos, MOF_COND_VALUE, value);
426 /* static */ bool ScriptOrder::SetStopLocation(VehicleID vehicle_id, OrderPosition order_position, StopLocation stop_location)
428 EnforceCompanyModeValid(false);
429 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
430 EnforcePrecondition(false, ScriptVehicle::GetVehicleType(vehicle_id) == ScriptVehicle::VT_RAIL);
431 EnforcePrecondition(false, IsGotoStationOrder(vehicle_id, order_position));
432 EnforcePrecondition(false, stop_location >= STOPLOCATION_NEAR && stop_location <= STOPLOCATION_FAR);
434 order_position = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position);
436 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
437 return ScriptObject::Command<CMD_MODIFY_ORDER>::Do(0, vehicle_id, order_pos, MOF_STOP_LOCATION, stop_location);
440 /* static */ bool ScriptOrder::SetOrderRefit(VehicleID vehicle_id, OrderPosition order_position, CargoID refit_cargo)
442 EnforceCompanyModeValid(false);
443 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
444 EnforcePrecondition(false, IsGotoStationOrder(vehicle_id, order_position) || (IsGotoDepotOrder(vehicle_id, order_position) && refit_cargo != CARGO_AUTO_REFIT));
445 EnforcePrecondition(false, ScriptCargo::IsValidCargo(refit_cargo) || refit_cargo == CARGO_AUTO_REFIT || refit_cargo == CARGO_NO_REFIT);
447 return ScriptObject::Command<CMD_ORDER_REFIT>::Do(0, vehicle_id, ScriptOrderPositionToRealOrderPosition(vehicle_id, ScriptOrder::ResolveOrderPosition(vehicle_id, order_position)), refit_cargo);
450 /* static */ bool ScriptOrder::AppendOrder(VehicleID vehicle_id, TileIndex destination, ScriptOrderFlags order_flags)
452 EnforceCompanyModeValid(false);
453 EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(vehicle_id));
454 EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
456 return InsertOrder(vehicle_id, (ScriptOrder::OrderPosition)::Vehicle::Get(vehicle_id)->GetNumManualOrders(), destination, order_flags);
459 /* static */ bool ScriptOrder::AppendConditionalOrder(VehicleID vehicle_id, OrderPosition jump_to)
461 EnforceCompanyModeValid(false);
462 EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(vehicle_id));
463 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to));
465 return InsertConditionalOrder(vehicle_id, (ScriptOrder::OrderPosition)::Vehicle::Get(vehicle_id)->GetNumManualOrders(), jump_to);
468 /* static */ bool ScriptOrder::InsertOrder(VehicleID vehicle_id, OrderPosition order_position, TileIndex destination, ScriptOrder::ScriptOrderFlags order_flags)
470 /* IsValidVehicleOrder is not good enough because it does not allow appending. */
471 if (order_position == ORDER_CURRENT) order_position = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position);
473 EnforceCompanyModeValid(false);
474 EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(vehicle_id));
475 EnforcePrecondition(false, order_position >= 0 && order_position <= ::Vehicle::Get(vehicle_id)->GetNumManualOrders());
476 EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
478 Order order;
479 OrderType ot = (order_flags & OF_GOTO_NEAREST_DEPOT) ? OT_GOTO_DEPOT : ::GetOrderTypeByTile(destination);
480 switch (ot) {
481 case OT_GOTO_DEPOT: {
482 OrderDepotTypeFlags odtf = (OrderDepotTypeFlags)(ODTFB_PART_OF_ORDERS | ((order_flags & OF_SERVICE_IF_NEEDED) ? ODTFB_SERVICE : 0));
483 OrderDepotActionFlags odaf = (OrderDepotActionFlags)(ODATF_SERVICE_ONLY | ((order_flags & OF_STOP_IN_DEPOT) ? ODATFB_HALT : 0));
484 if (order_flags & OF_GOTO_NEAREST_DEPOT) odaf |= ODATFB_NEAREST_DEPOT;
485 OrderNonStopFlags onsf = (OrderNonStopFlags)((order_flags & OF_NON_STOP_INTERMEDIATE) ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
486 if (order_flags & OF_GOTO_NEAREST_DEPOT) {
487 order.MakeGoToDepot(INVALID_DEPOT, odtf, onsf, odaf);
488 } else {
489 /* Check explicitly if the order is to a station (for aircraft) or
490 * to a depot (other vehicle types). */
491 if (::Vehicle::Get(vehicle_id)->type == VEH_AIRCRAFT) {
492 if (!::IsTileType(destination, MP_STATION)) return false;
493 order.MakeGoToDepot(::GetStationIndex(destination), odtf, onsf, odaf);
494 } else {
495 if (::IsTileType(destination, MP_STATION)) return false;
496 order.MakeGoToDepot(::GetDepotIndex(destination), odtf, onsf, odaf);
499 break;
502 case OT_GOTO_STATION:
503 order.MakeGoToStation(::GetStationIndex(destination));
504 order.SetLoadType((OrderLoadFlags)GB(order_flags, 5, 3));
505 order.SetUnloadType((OrderUnloadFlags)GB(order_flags, 2, 3));
506 order.SetStopLocation(OSL_PLATFORM_FAR_END);
507 break;
509 case OT_GOTO_WAYPOINT:
510 order.MakeGoToWaypoint(::GetStationIndex(destination));
511 break;
513 default:
514 return false;
517 order.SetNonStopType((OrderNonStopFlags)GB(order_flags, 0, 2));
519 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
520 return ScriptObject::Command<CMD_INSERT_ORDER>::Do(0, vehicle_id, order_pos, order);
523 /* static */ bool ScriptOrder::InsertConditionalOrder(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
525 /* IsValidVehicleOrder is not good enough because it does not allow appending. */
526 if (order_position == ORDER_CURRENT) order_position = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position);
528 EnforceCompanyModeValid(false);
529 EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(vehicle_id));
530 EnforcePrecondition(false, order_position >= 0 && order_position <= ::Vehicle::Get(vehicle_id)->GetNumManualOrders());
531 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to) && jump_to != ORDER_CURRENT);
533 Order order;
534 order.MakeConditional(jump_to);
536 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
537 return ScriptObject::Command<CMD_INSERT_ORDER>::Do(0, vehicle_id, order_pos, order);
540 /* static */ bool ScriptOrder::RemoveOrder(VehicleID vehicle_id, OrderPosition order_position)
542 order_position = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position);
544 EnforceCompanyModeValid(false);
545 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
547 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
548 return ScriptObject::Command<CMD_DELETE_ORDER>::Do(0, vehicle_id, order_pos);
551 /* static */ bool ScriptOrder::SkipToOrder(VehicleID vehicle_id, OrderPosition next_order)
553 next_order = ScriptOrder::ResolveOrderPosition(vehicle_id, next_order);
555 EnforceCompanyModeValid(false);
556 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, next_order));
558 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, next_order);
559 return ScriptObject::Command<CMD_SKIP_TO_ORDER>::Do(0, vehicle_id, order_pos);
563 * Callback handler as SetOrderFlags possibly needs multiple DoCommand calls
564 * to be able to set all order flags correctly. As we need to wait till the
565 * command has completed before we know the next bits to change we need to
566 * call the function multiple times. Each time it'll reduce the difference
567 * between the wanted and the current order.
568 * @param instance The script instance we are doing the callback for.
570 static void _DoCommandReturnSetOrderFlags(class ScriptInstance *instance)
572 ScriptObject::SetLastCommandRes(ScriptOrder::_SetOrderFlags());
573 ScriptInstance::DoCommandReturn(instance);
576 /* static */ bool ScriptOrder::_SetOrderFlags()
578 /* Make sure we don't go into an infinite loop */
579 int retry = ScriptObject::GetCallbackVariable(3) - 1;
580 if (retry < 0) {
581 Debug(script, 0, "Possible infinite loop in SetOrderFlags() detected");
582 return false;
584 ScriptObject::SetCallbackVariable(3, retry);
586 VehicleID vehicle_id = (VehicleID)ScriptObject::GetCallbackVariable(0);
587 OrderPosition order_position = (OrderPosition)ScriptObject::GetCallbackVariable(1);
588 ScriptOrderFlags order_flags = (ScriptOrderFlags)ScriptObject::GetCallbackVariable(2);
590 order_position = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position);
592 EnforceCompanyModeValid(false);
593 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
594 EnforcePrecondition(false, AreOrderFlagsValid(GetOrderDestination(vehicle_id, order_position), order_flags));
596 const Order *order = ::ResolveOrder(vehicle_id, order_position);
597 int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
599 ScriptOrderFlags current = GetOrderFlags(vehicle_id, order_position);
601 EnforcePrecondition(false, (order_flags & OF_GOTO_NEAREST_DEPOT) == (current & OF_GOTO_NEAREST_DEPOT));
603 if ((current & OF_NON_STOP_FLAGS) != (order_flags & OF_NON_STOP_FLAGS)) {
604 return ScriptObject::Command<CMD_MODIFY_ORDER>::Do(&::_DoCommandReturnSetOrderFlags, vehicle_id, order_pos, MOF_NON_STOP, order_flags & OF_NON_STOP_FLAGS);
607 switch (order->GetType()) {
608 case OT_GOTO_DEPOT:
609 if ((current & OF_DEPOT_FLAGS) != (order_flags & OF_DEPOT_FLAGS)) {
610 uint data = DA_ALWAYS_GO;
611 if (order_flags & OF_SERVICE_IF_NEEDED) data = DA_SERVICE;
612 if (order_flags & OF_STOP_IN_DEPOT) data = DA_STOP;
613 return ScriptObject::Command<CMD_MODIFY_ORDER>::Do(&::_DoCommandReturnSetOrderFlags, vehicle_id, order_pos, MOF_DEPOT_ACTION, data);
615 break;
617 case OT_GOTO_STATION:
618 if ((current & OF_UNLOAD_FLAGS) != (order_flags & OF_UNLOAD_FLAGS)) {
619 return ScriptObject::Command<CMD_MODIFY_ORDER>::Do(&::_DoCommandReturnSetOrderFlags, vehicle_id, order_pos, MOF_UNLOAD, (order_flags & OF_UNLOAD_FLAGS) >> 2);
621 if ((current & OF_LOAD_FLAGS) != (order_flags & OF_LOAD_FLAGS)) {
622 return ScriptObject::Command<CMD_MODIFY_ORDER>::Do(&::_DoCommandReturnSetOrderFlags, vehicle_id, order_pos, MOF_LOAD, (order_flags & OF_LOAD_FLAGS) >> 5);
624 break;
626 default: break;
629 assert(GetOrderFlags(vehicle_id, order_position) == order_flags);
631 return true;
634 /* static */ bool ScriptOrder::SetOrderFlags(VehicleID vehicle_id, OrderPosition order_position, ScriptOrder::ScriptOrderFlags order_flags)
636 ScriptObject::SetCallbackVariable(0, vehicle_id);
637 ScriptObject::SetCallbackVariable(1, order_position);
638 ScriptObject::SetCallbackVariable(2, order_flags);
639 /* In case another client(s) change orders at the same time we could
640 * end in an infinite loop. This stops that from happening ever. */
641 ScriptObject::SetCallbackVariable(3, 8);
642 return ScriptOrder::_SetOrderFlags();
645 /* static */ bool ScriptOrder::MoveOrder(VehicleID vehicle_id, OrderPosition order_position_move, OrderPosition order_position_target)
647 order_position_move = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position_move);
648 order_position_target = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position_target);
650 EnforceCompanyModeValid(false);
651 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position_move));
652 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position_target));
653 EnforcePrecondition(false, order_position_move != order_position_target);
655 int order_pos_move = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position_move);
656 int order_pos_target = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position_target);
657 return ScriptObject::Command<CMD_MOVE_ORDER>::Do(0, vehicle_id, order_pos_move, order_pos_target);
660 /* static */ bool ScriptOrder::CopyOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
662 EnforceCompanyModeValid(false);
663 EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(vehicle_id));
664 EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(main_vehicle_id));
666 return ScriptObject::Command<CMD_CLONE_ORDER>::Do(0, CO_COPY, vehicle_id, main_vehicle_id);
669 /* static */ bool ScriptOrder::ShareOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
671 EnforceCompanyModeValid(false);
672 EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(vehicle_id));
673 EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(main_vehicle_id));
675 return ScriptObject::Command<CMD_CLONE_ORDER>::Do(0, CO_SHARE, vehicle_id, main_vehicle_id);
678 /* static */ bool ScriptOrder::UnshareOrders(VehicleID vehicle_id)
680 EnforceCompanyModeValid(false);
681 EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(vehicle_id));
683 return ScriptObject::Command<CMD_CLONE_ORDER>::Do(0, CO_UNSHARE, vehicle_id, 0);
686 /* static */ SQInteger ScriptOrder::GetOrderDistance(ScriptVehicle::VehicleType vehicle_type, TileIndex origin_tile, TileIndex dest_tile)
688 if (vehicle_type == ScriptVehicle::VT_AIR) {
689 if (ScriptTile::IsStationTile(origin_tile)) {
690 const Station *orig_station = ::Station::GetByTile(origin_tile);
691 if (orig_station != nullptr && orig_station->airport.tile != INVALID_TILE) origin_tile = orig_station->airport.tile;
693 if (ScriptTile::IsStationTile(dest_tile)) {
694 const Station *dest_station = ::Station::GetByTile(dest_tile);
695 if (dest_station != nullptr && dest_station->airport.tile != INVALID_TILE) dest_tile = dest_station->airport.tile;
698 return ScriptMap::DistanceSquare(origin_tile, dest_tile);
699 } else {
700 return ScriptMap::DistanceManhattan(origin_tile, dest_tile);