annotate src/ai/api/ai_order.cpp @ 13186:afdfdda87cd4 draft

(svn r17693) -Cleanup: remove some unneeded includes
author rubidium <rubidium@openttd.org>
date Sun, 04 Oct 2009 17:16:41 +0000
parents c2298b1d3f83
children 0a9f4129b777
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
1 /* $Id$ */
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
2
12778
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12748
diff changeset
3 /*
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12748
diff changeset
4 * This file is part of OpenTTD.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12748
diff changeset
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.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12748
diff changeset
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.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12748
diff changeset
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/>.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12748
diff changeset
8 */
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12748
diff changeset
9
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
10 /** @file ai_order.cpp Implementation of AIOrder. */
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
11
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
12 #include "ai_vehicle.hpp"
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
13 #include "../ai_instance.hpp"
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
14 #include "../../debug.h"
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
15 #include "../../vehicle_base.h"
12225
4f649aa97318 (svn r16640) -Codechange: move roadstop stuff to separate files
smatz <smatz@openttd.org>
parents: 12082
diff changeset
16 #include "../../roadstop_base.h"
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
17 #include "../../depot_base.h"
12228
240adc64d01a (svn r16643) -Codechange: replace GetStationByTile() by Station::GetByTile()
smatz <smatz@openttd.org>
parents: 12225
diff changeset
18 #include "../../station_base.h"
12475
c133b51890f4 (svn r16912) -Codechange: split waypoint.h in waypoint_base.h and waypoint_func.h
rubidium <rubidium@openttd.org>
parents: 12472
diff changeset
19 #include "../../waypoint_base.h"
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
20
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
21 /**
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
22 * Gets the order type given a tile
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
23 * @param t the tile to get the order from
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
24 * @return the order type, or OT_END when there is no order
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
25 */
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
26 static OrderType GetOrderTypeByTile(TileIndex t)
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
27 {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
28 if (!::IsValidTile(t)) return OT_END;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
29
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
30 switch (::GetTileType(t)) {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
31 default: break;
11720
4ffde677f62e (svn r16108) -Fix [NoAI]: When giving an aircraft a goto-hangar order don't let it be a normal goto-station order.
yexo <yexo@openttd.org>
parents: 11719
diff changeset
32 case MP_STATION:
12472
604a2cfc77ab (svn r16909) -Fix [FS#2996]: NewGRF stations would be triggering assertions all over the place when using the more advanced station types.
rubidium <rubidium@openttd.org>
parents: 12361
diff changeset
33 if (IsBuoy(t) || IsRailWaypoint(t)) return OT_GOTO_WAYPOINT;
11720
4ffde677f62e (svn r16108) -Fix [NoAI]: When giving an aircraft a goto-hangar order don't let it be a normal goto-station order.
yexo <yexo@openttd.org>
parents: 11719
diff changeset
34 if (IsHangar(t)) return OT_GOTO_DEPOT;
4ffde677f62e (svn r16108) -Fix [NoAI]: When giving an aircraft a goto-hangar order don't let it be a normal goto-station order.
yexo <yexo@openttd.org>
parents: 11719
diff changeset
35 return OT_GOTO_STATION;
4ffde677f62e (svn r16108) -Fix [NoAI]: When giving an aircraft a goto-hangar order don't let it be a normal goto-station order.
yexo <yexo@openttd.org>
parents: 11719
diff changeset
36 break;
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
37 case MP_WATER: if (::IsShipDepot(t)) return OT_GOTO_DEPOT; break;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
38 case MP_ROAD: if (::GetRoadTileType(t) == ROAD_TILE_DEPOT) return OT_GOTO_DEPOT; break;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
39 case MP_RAILWAY:
12472
604a2cfc77ab (svn r16909) -Fix [FS#2996]: NewGRF stations would be triggering assertions all over the place when using the more advanced station types.
rubidium <rubidium@openttd.org>
parents: 12361
diff changeset
40 if (IsRailDepot(t)) return OT_GOTO_DEPOT;
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
41 break;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
42 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
43
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
44 return OT_END;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
45 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
46
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
47 /* static */ bool AIOrder::IsValidVehicleOrder(VehicleID vehicle_id, OrderPosition order_position)
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
48 {
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11849
diff changeset
49 return AIVehicle::IsValidVehicle(vehicle_id) && order_position >= 0 && (order_position < ::Vehicle::Get(vehicle_id)->GetNumOrders() || order_position == ORDER_CURRENT);
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
50 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
51
11775
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
52 /**
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
53 * Get the current order the vehicle is executing. If the current order is in
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
54 * the order list, return the order from the orderlist. If the current order
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
55 * was a manual order, return the current order.
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
56 */
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
57 static const Order *ResolveOrder(VehicleID vehicle_id, AIOrder::OrderPosition order_position)
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
58 {
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11849
diff changeset
59 const Vehicle *v = ::Vehicle::Get(vehicle_id);
11775
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
60 if (order_position == AIOrder::ORDER_CURRENT) {
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
61 const Order *order = &v->current_order;
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
62 if (order->GetType() == OT_GOTO_DEPOT && !(order->GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) return order;
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
63 order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
64 if (order_position == AIOrder::ORDER_INVALID) return NULL;
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
65 }
11991
a4d3838349e6 (svn r16397) -Codechange: move GetVehicleOrder/GetLastVehicleOrder into Vehicle
rubidium <rubidium@openttd.org>
parents: 11922
diff changeset
66 return v->GetOrder(order_position);
11775
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
67 }
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
68
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
69 /* static */ bool AIOrder::IsGotoStationOrder(VehicleID vehicle_id, OrderPosition order_position)
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
70 {
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
71 if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
72
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
73 const Order *order = ::ResolveOrder(vehicle_id, order_position);
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
74 return order != NULL && order->GetType() == OT_GOTO_STATION;
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
75 }
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
76
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
77 /* static */ bool AIOrder::IsGotoDepotOrder(VehicleID vehicle_id, OrderPosition order_position)
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
78 {
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
79 if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
80
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
81 const Order *order = ::ResolveOrder(vehicle_id, order_position);
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
82 return order != NULL && order->GetType() == OT_GOTO_DEPOT;
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
83 }
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
84
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
85 /* static */ bool AIOrder::IsGotoWaypointOrder(VehicleID vehicle_id, OrderPosition order_position)
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
86 {
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
87 if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
88
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
89 const Order *order = ::ResolveOrder(vehicle_id, order_position);
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
90 return order != NULL && order->GetType() == OT_GOTO_WAYPOINT;
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
91 }
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
92
11175
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
93 /* static */ bool AIOrder::IsConditionalOrder(VehicleID vehicle_id, OrderPosition order_position)
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
94 {
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
95 if (order_position == ORDER_CURRENT) return false;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
96 if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
97
11991
a4d3838349e6 (svn r16397) -Codechange: move GetVehicleOrder/GetLastVehicleOrder into Vehicle
rubidium <rubidium@openttd.org>
parents: 11922
diff changeset
98 const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
11175
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
99 return order->GetType() == OT_CONDITIONAL;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
100 }
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
101
11775
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
102 /* static */ bool AIOrder::IsCurrentOrderPartOfOrderList(VehicleID vehicle_id)
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
103 {
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
104 if (AIVehicle::IsValidVehicle(vehicle_id)) return false;
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
105 if (GetOrderCount(vehicle_id) == 0) return false;
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
106
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11849
diff changeset
107 const Order *order = &::Vehicle::Get(vehicle_id)->current_order;
11775
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
108 if (order->GetType() != OT_GOTO_DEPOT) return true;
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
109 return (order->GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0;
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
110 }
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
111
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
112 /* static */ AIOrder::OrderPosition AIOrder::ResolveOrderPosition(VehicleID vehicle_id, OrderPosition order_position)
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
113 {
10768
0556b96dd731 (svn r15101) -Change [API CHANGE]: more consistant naming for consts:
truebrain <truebrain@openttd.org>
parents: 10696
diff changeset
114 if (!AIVehicle::IsValidVehicle(vehicle_id)) return ORDER_INVALID;
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
115
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11849
diff changeset
116 if (order_position == ORDER_CURRENT) return (AIOrder::OrderPosition)::Vehicle::Get(vehicle_id)->cur_order_index;
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11849
diff changeset
117 return (order_position >= 0 && order_position < ::Vehicle::Get(vehicle_id)->GetNumOrders()) ? order_position : ORDER_INVALID;
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
118 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
119
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
120
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
121 /* static */ bool AIOrder::AreOrderFlagsValid(TileIndex destination, AIOrderFlags order_flags)
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
122 {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
123 switch (::GetOrderTypeByTile(destination)) {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
124 case OT_GOTO_STATION:
12082
d092f17a921d (svn r16491) -Codechange: Added parentheses around bitwise operators for code style.
alberth <alberth@openttd.org>
parents: 11991
diff changeset
125 return (order_flags & ~(AIOF_NON_STOP_FLAGS | AIOF_UNLOAD_FLAGS | AIOF_LOAD_FLAGS)) == 0 &&
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
126 /* Test the different mutual exclusive flags. */
12082
d092f17a921d (svn r16491) -Codechange: Added parentheses around bitwise operators for code style.
alberth <alberth@openttd.org>
parents: 11991
diff changeset
127 ((order_flags & AIOF_TRANSFER) == 0 || (order_flags & AIOF_UNLOAD) == 0) &&
d092f17a921d (svn r16491) -Codechange: Added parentheses around bitwise operators for code style.
alberth <alberth@openttd.org>
parents: 11991
diff changeset
128 ((order_flags & AIOF_TRANSFER) == 0 || (order_flags & AIOF_NO_UNLOAD) == 0) &&
d092f17a921d (svn r16491) -Codechange: Added parentheses around bitwise operators for code style.
alberth <alberth@openttd.org>
parents: 11991
diff changeset
129 ((order_flags & AIOF_UNLOAD) == 0 || (order_flags & AIOF_NO_UNLOAD) == 0) &&
d092f17a921d (svn r16491) -Codechange: Added parentheses around bitwise operators for code style.
alberth <alberth@openttd.org>
parents: 11991
diff changeset
130 ((order_flags & AIOF_UNLOAD) == 0 || (order_flags & AIOF_NO_UNLOAD) == 0) &&
d092f17a921d (svn r16491) -Codechange: Added parentheses around bitwise operators for code style.
alberth <alberth@openttd.org>
parents: 11991
diff changeset
131 ((order_flags & AIOF_NO_UNLOAD) == 0 || (order_flags & AIOF_NO_LOAD) == 0) &&
d092f17a921d (svn r16491) -Codechange: Added parentheses around bitwise operators for code style.
alberth <alberth@openttd.org>
parents: 11991
diff changeset
132 ((order_flags & AIOF_FULL_LOAD_ANY) == 0 || (order_flags & AIOF_NO_LOAD) == 0);
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
133
11719
15d3dd64a19b (svn r16107) -Add [NoAI]: AIOF_STOP_IN_DEPOT to the orderflags in AIOrder to allow stop-in-depot orders
yexo <yexo@openttd.org>
parents: 11718
diff changeset
134 case OT_GOTO_DEPOT:
12082
d092f17a921d (svn r16491) -Codechange: Added parentheses around bitwise operators for code style.
alberth <alberth@openttd.org>
parents: 11991
diff changeset
135 return (order_flags & ~(AIOF_NON_STOP_FLAGS | AIOF_DEPOT_FLAGS)) == 0 &&
d092f17a921d (svn r16491) -Codechange: Added parentheses around bitwise operators for code style.
alberth <alberth@openttd.org>
parents: 11991
diff changeset
136 ((order_flags & AIOF_SERVICE_IF_NEEDED) == 0 || (order_flags & AIOF_STOP_IN_DEPOT) == 0);
11719
15d3dd64a19b (svn r16107) -Add [NoAI]: AIOF_STOP_IN_DEPOT to the orderflags in AIOrder to allow stop-in-depot orders
yexo <yexo@openttd.org>
parents: 11718
diff changeset
137
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
138 case OT_GOTO_WAYPOINT: return (order_flags & ~(AIOF_NON_STOP_FLAGS)) == 0;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
139 default: return false;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
140 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
141 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
142
11175
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
143 /* static */ bool AIOrder::IsValidConditionalOrder(OrderCondition condition, CompareFunction compare)
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
144 {
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
145 switch (condition) {
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
146 case OC_LOAD_PERCENTAGE:
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
147 case OC_RELIABILITY:
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
148 case OC_MAX_SPEED:
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
149 case OC_AGE:
11176
924587d9da82 (svn r15527) -Fix (r15526): A ')' too much.
yexo <yexo@openttd.org>
parents: 11175
diff changeset
150 return compare >= CF_EQUALS && compare <= CF_MORE_EQUALS;
11175
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
151
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
152 case OC_REQUIRES_SERVICE:
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
153 return compare == CF_IS_TRUE || compare == CF_IS_FALSE;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
154
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
155 case OC_UNCONDITIONALLY:
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
156 return true;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
157
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
158 default: return false;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
159 }
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
160 }
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
161
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
162 /* static */ int32 AIOrder::GetOrderCount(VehicleID vehicle_id)
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
163 {
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11849
diff changeset
164 return AIVehicle::IsValidVehicle(vehicle_id) ? ::Vehicle::Get(vehicle_id)->GetNumOrders() : -1;
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
165 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
166
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
167 /* static */ TileIndex AIOrder::GetOrderDestination(VehicleID vehicle_id, OrderPosition order_position)
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
168 {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
169 if (!IsValidVehicleOrder(vehicle_id, order_position)) return INVALID_TILE;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
170
11775
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
171 const Order *order = ::ResolveOrder(vehicle_id, order_position);
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
172 if (order == NULL || order->GetType() == OT_CONDITIONAL) return INVALID_TILE;
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11849
diff changeset
173 const Vehicle *v = ::Vehicle::Get(vehicle_id);
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
174
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
175 switch (order->GetType()) {
11721
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
176 case OT_GOTO_DEPOT: {
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11849
diff changeset
177 if (v->type != VEH_AIRCRAFT) return ::Depot::Get(order->GetDestination())->xy;
11721
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
178 /* Aircraft's hangars are referenced by StationID, not DepotID */
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11849
diff changeset
179 const Station *st = ::Station::Get(order->GetDestination());
11721
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
180 const AirportFTAClass *airport = st->Airport();
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
181 if (airport == NULL || airport->nof_depots == 0) return INVALID_TILE;
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
182 return st->airport_tile + ::ToTileIndexDiff(st->Airport()->airport_depots[0]);
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
183 }
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
184
11721
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
185 case OT_GOTO_STATION: {
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11849
diff changeset
186 const Station *st = ::Station::Get(order->GetDestination());
12510
3e976121ced2 (svn r16947) -Codechange: use TileArea instead of train_tile, trainst_w and trainst_h.
rubidium <rubidium@openttd.org>
parents: 12475
diff changeset
187 if (st->train_station.tile != INVALID_TILE) {
3e976121ced2 (svn r16947) -Codechange: use TileArea instead of train_tile, trainst_w and trainst_h.
rubidium <rubidium@openttd.org>
parents: 12475
diff changeset
188 for (uint i = 0; i < st->train_station.w; i++) {
3e976121ced2 (svn r16947) -Codechange: use TileArea instead of train_tile, trainst_w and trainst_h.
rubidium <rubidium@openttd.org>
parents: 12475
diff changeset
189 TileIndex t = st->train_station.tile + TileDiffXY(i, 0);
11721
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
190 if (st->TileBelongsToRailStation(t)) return t;
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
191 }
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
192 } else if (st->dock_tile != INVALID_TILE) {
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
193 return st->dock_tile;
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
194 } else if (st->bus_stops != NULL) {
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
195 return st->bus_stops->xy;
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
196 } else if (st->truck_stops != NULL) {
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
197 return st->truck_stops->xy;
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
198 } else if (st->airport_tile != INVALID_TILE) {
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
199 const AirportFTAClass *fta = st->Airport();
12529
f24901a2ad84 (svn r16966) -Codechange: BEGIN_TILE_LOOP and END_TILE_LOOP reworked into TILE_LOOP, which means no more duplication of parameters between BEGIN_TILE_LOOP and END_TILE_LOOP
rubidium <rubidium@openttd.org>
parents: 12511
diff changeset
200 TILE_LOOP(tile, fta->size_x, fta->size_y, st->airport_tile) {
11721
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
201 if (!::IsHangar(tile)) return tile;
12529
f24901a2ad84 (svn r16966) -Codechange: BEGIN_TILE_LOOP and END_TILE_LOOP reworked into TILE_LOOP, which means no more duplication of parameters between BEGIN_TILE_LOOP and END_TILE_LOOP
rubidium <rubidium@openttd.org>
parents: 12511
diff changeset
202 }
11721
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
203 }
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
204 return INVALID_TILE;
c92611d86f13 (svn r16109) -Fix [NoAI]: Make sure AIOrder::GetDestination always returns a tile belonging to the station
yexo <yexo@openttd.org>
parents: 11720
diff changeset
205 }
12511
699bacb57598 (svn r16948) -Codechange: some code reductions and usage of wrapper functions
rubidium <rubidium@openttd.org>
parents: 12510
diff changeset
206 case OT_GOTO_WAYPOINT: return ::Waypoint::Get(order->GetDestination())->xy;
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
207 default: return INVALID_TILE;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
208 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
209 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
210
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
211 /* static */ AIOrder::AIOrderFlags AIOrder::GetOrderFlags(VehicleID vehicle_id, OrderPosition order_position)
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
212 {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
213 if (!IsValidVehicleOrder(vehicle_id, order_position)) return AIOF_INVALID;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
214
11775
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
215 const Order *order = ::ResolveOrder(vehicle_id, order_position);
34e40a741e56 (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is.
yexo <yexo@openttd.org>
parents: 11721
diff changeset
216 if (order == NULL || order->GetType() == OT_CONDITIONAL) return AIOF_INVALID;
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
217
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
218 AIOrderFlags order_flags = AIOF_NONE;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
219 order_flags |= (AIOrderFlags)order->GetNonStopType();
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
220 switch (order->GetType()) {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
221 case OT_GOTO_DEPOT:
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
222 if (order->GetDepotOrderType() & ODTFB_SERVICE) order_flags |= AIOF_SERVICE_IF_NEEDED;
11719
15d3dd64a19b (svn r16107) -Add [NoAI]: AIOF_STOP_IN_DEPOT to the orderflags in AIOrder to allow stop-in-depot orders
yexo <yexo@openttd.org>
parents: 11718
diff changeset
223 if (order->GetDepotActionType() & ODATFB_HALT) order_flags |= AIOF_STOP_IN_DEPOT;
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
224 break;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
225
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
226 case OT_GOTO_STATION:
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
227 order_flags |= (AIOrderFlags)(order->GetLoadType() << 5);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
228 order_flags |= (AIOrderFlags)(order->GetUnloadType() << 2);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
229 break;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
230
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
231 default: break;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
232 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
233
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
234 return order_flags;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
235 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
236
11175
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
237 /* static */ AIOrder::OrderPosition AIOrder::GetOrderJumpTo(VehicleID vehicle_id, OrderPosition order_position)
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
238 {
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
239 if (!IsValidVehicleOrder(vehicle_id, order_position)) return ORDER_INVALID;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
240 if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return ORDER_INVALID;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
241
11991
a4d3838349e6 (svn r16397) -Codechange: move GetVehicleOrder/GetLastVehicleOrder into Vehicle
rubidium <rubidium@openttd.org>
parents: 11922
diff changeset
242 const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
11175
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
243 return (OrderPosition)order->GetConditionSkipToOrder();
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
244 }
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
245
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
246 /* static */ AIOrder::OrderCondition AIOrder::GetOrderCondition(VehicleID vehicle_id, OrderPosition order_position)
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
247 {
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
248 if (!IsValidVehicleOrder(vehicle_id, order_position)) return OC_INVALID;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
249 if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return OC_INVALID;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
250
11991
a4d3838349e6 (svn r16397) -Codechange: move GetVehicleOrder/GetLastVehicleOrder into Vehicle
rubidium <rubidium@openttd.org>
parents: 11922
diff changeset
251 const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
11175
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
252 return (OrderCondition)order->GetConditionVariable();
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
253 }
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
254
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
255 /* static */ AIOrder::CompareFunction AIOrder::GetOrderCompareFunction(VehicleID vehicle_id, OrderPosition order_position)
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
256 {
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
257 if (!IsValidVehicleOrder(vehicle_id, order_position)) return CF_INVALID;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
258 if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return CF_INVALID;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
259
11991
a4d3838349e6 (svn r16397) -Codechange: move GetVehicleOrder/GetLastVehicleOrder into Vehicle
rubidium <rubidium@openttd.org>
parents: 11922
diff changeset
260 const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
11175
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
261 return (CompareFunction)order->GetConditionComparator();
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
262 }
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
263
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
264 /* static */ int32 AIOrder::GetOrderCompareValue(VehicleID vehicle_id, OrderPosition order_position)
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
265 {
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
266 if (!IsValidVehicleOrder(vehicle_id, order_position)) return -1;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
267 if (order_position == ORDER_CURRENT || !IsConditionalOrder(vehicle_id, order_position)) return -1;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
268
11991
a4d3838349e6 (svn r16397) -Codechange: move GetVehicleOrder/GetLastVehicleOrder into Vehicle
rubidium <rubidium@openttd.org>
parents: 11922
diff changeset
269 const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
11175
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
270 int32 value = order->GetConditionValue();
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
271 if (order->GetConditionVariable() == OCV_MAX_SPEED) value = value * 16 / 10;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
272 return value;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
273 }
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
274
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
275 /* static */ bool AIOrder::SetOrderJumpTo(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
276 {
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
277 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
278 EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
279 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to) && jump_to != ORDER_CURRENT);
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
280
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
281 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_DESTINATION | (jump_to << 4), CMD_MODIFY_ORDER);
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
282 }
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
283
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
284 /* static */ bool AIOrder::SetOrderCondition(VehicleID vehicle_id, OrderPosition order_position, OrderCondition condition)
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
285 {
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
286 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
287 EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
288 EnforcePrecondition(false, condition >= OC_LOAD_PERCENTAGE && condition <= OC_UNCONDITIONALLY);
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
289
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
290 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_VARIABLE | (condition << 4), CMD_MODIFY_ORDER);
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
291 }
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
292
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
293 /* static */ bool AIOrder::SetOrderCompareFunction(VehicleID vehicle_id, OrderPosition order_position, CompareFunction compare)
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
294 {
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
295 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
296 EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
297 EnforcePrecondition(false, compare >= CF_EQUALS && compare <= CF_IS_FALSE);
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
298
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
299 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_COMPARATOR | (compare << 4), CMD_MODIFY_ORDER);
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
300 }
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
301
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
302 /* static */ bool AIOrder::SetOrderCompareValue(VehicleID vehicle_id, OrderPosition order_position, int32 value)
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
303 {
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
304 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
305 EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
306 EnforcePrecondition(false, value >= 0 && value < 2048);
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
307 if (GetOrderCondition(vehicle_id, order_position) == OC_MAX_SPEED) value = value * 10 / 16;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
308
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
309 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_VALUE | (value << 4), CMD_MODIFY_ORDER);
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
310 }
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
311
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
312 /* static */ bool AIOrder::AppendOrder(VehicleID vehicle_id, TileIndex destination, AIOrderFlags order_flags)
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
313 {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
314 EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
11175
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
315 EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
316
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11849
diff changeset
317 return InsertOrder(vehicle_id, (AIOrder::OrderPosition)::Vehicle::Get(vehicle_id)->GetNumOrders(), destination, order_flags);
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
318 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
319
11175
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
320 /* static */ bool AIOrder::AppendConditionalOrder(VehicleID vehicle_id, OrderPosition jump_to)
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
321 {
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
322 EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
323 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to));
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
324
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11849
diff changeset
325 return InsertConditionalOrder(vehicle_id, (AIOrder::OrderPosition)::Vehicle::Get(vehicle_id)->GetNumOrders(), jump_to);
11175
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
326 }
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
327
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
328 /* static */ bool AIOrder::InsertOrder(VehicleID vehicle_id, OrderPosition order_position, TileIndex destination, AIOrder::AIOrderFlags order_flags)
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
329 {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
330 /* IsValidVehicleOrder is not good enough because it does not allow appending. */
10768
0556b96dd731 (svn r15101) -Change [API CHANGE]: more consistant naming for consts:
truebrain <truebrain@openttd.org>
parents: 10696
diff changeset
331 if (order_position == ORDER_CURRENT) order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
332
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
333 EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11849
diff changeset
334 EnforcePrecondition(false, order_position >= 0 && order_position <= ::Vehicle::Get(vehicle_id)->GetNumOrders());
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
335 EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
336
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
337 Order order;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
338 switch (::GetOrderTypeByTile(destination)) {
11719
15d3dd64a19b (svn r16107) -Add [NoAI]: AIOF_STOP_IN_DEPOT to the orderflags in AIOrder to allow stop-in-depot orders
yexo <yexo@openttd.org>
parents: 11718
diff changeset
339 case OT_GOTO_DEPOT: {
15d3dd64a19b (svn r16107) -Add [NoAI]: AIOF_STOP_IN_DEPOT to the orderflags in AIOrder to allow stop-in-depot orders
yexo <yexo@openttd.org>
parents: 11718
diff changeset
340 OrderDepotTypeFlags odtf = (OrderDepotTypeFlags)(ODTFB_PART_OF_ORDERS | ((order_flags & AIOF_SERVICE_IF_NEEDED) ? ODTFB_SERVICE : 0));
15d3dd64a19b (svn r16107) -Add [NoAI]: AIOF_STOP_IN_DEPOT to the orderflags in AIOrder to allow stop-in-depot orders
yexo <yexo@openttd.org>
parents: 11718
diff changeset
341 OrderDepotActionFlags odaf = (OrderDepotActionFlags)(ODATF_SERVICE_ONLY | ((order_flags & AIOF_STOP_IN_DEPOT) ? ODATFB_HALT : 0));
11849
a90ffc1a219d (svn r16239) -Fix (r16199): Use the stop/non-stop intermediate orderflags AIs can give for goto-depot orders
yexo <yexo@openttd.org>
parents: 11809
diff changeset
342 OrderNonStopFlags onsf = (OrderNonStopFlags)((order_flags & AIOF_NON_STOP_INTERMEDIATE) ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
11720
4ffde677f62e (svn r16108) -Fix [NoAI]: When giving an aircraft a goto-hangar order don't let it be a normal goto-station order.
yexo <yexo@openttd.org>
parents: 11719
diff changeset
343 /* Check explicitly if the order is to a station (for aircraft) or
4ffde677f62e (svn r16108) -Fix [NoAI]: When giving an aircraft a goto-hangar order don't let it be a normal goto-station order.
yexo <yexo@openttd.org>
parents: 11719
diff changeset
344 * to a depot (other vehicle types). */
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11849
diff changeset
345 if (::Vehicle::Get(vehicle_id)->type == VEH_AIRCRAFT) {
11720
4ffde677f62e (svn r16108) -Fix [NoAI]: When giving an aircraft a goto-hangar order don't let it be a normal goto-station order.
yexo <yexo@openttd.org>
parents: 11719
diff changeset
346 if (!::IsTileType(destination, MP_STATION)) return false;
11849
a90ffc1a219d (svn r16239) -Fix (r16199): Use the stop/non-stop intermediate orderflags AIs can give for goto-depot orders
yexo <yexo@openttd.org>
parents: 11809
diff changeset
347 order.MakeGoToDepot(::GetStationIndex(destination), odtf, onsf, odaf);
11720
4ffde677f62e (svn r16108) -Fix [NoAI]: When giving an aircraft a goto-hangar order don't let it be a normal goto-station order.
yexo <yexo@openttd.org>
parents: 11719
diff changeset
348 } else {
4ffde677f62e (svn r16108) -Fix [NoAI]: When giving an aircraft a goto-hangar order don't let it be a normal goto-station order.
yexo <yexo@openttd.org>
parents: 11719
diff changeset
349 if (::IsTileType(destination, MP_STATION)) return false;
13001
c2298b1d3f83 (svn r17495) -Codechange: replace 'Depot::Get(GetDepotIndex(tile))->index' with GetDepotIndex(tile)
rubidium <rubidium@openttd.org>
parents: 12778
diff changeset
350 order.MakeGoToDepot(::GetDepotIndex(destination), odtf, onsf, odaf);
11720
4ffde677f62e (svn r16108) -Fix [NoAI]: When giving an aircraft a goto-hangar order don't let it be a normal goto-station order.
yexo <yexo@openttd.org>
parents: 11719
diff changeset
351 }
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
352 break;
11719
15d3dd64a19b (svn r16107) -Add [NoAI]: AIOF_STOP_IN_DEPOT to the orderflags in AIOrder to allow stop-in-depot orders
yexo <yexo@openttd.org>
parents: 11718
diff changeset
353 }
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
354
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
355 case OT_GOTO_STATION:
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
356 order.MakeGoToStation(::GetStationIndex(destination));
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
357 order.SetLoadType((OrderLoadFlags)GB(order_flags, 5, 3));
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
358 order.SetUnloadType((OrderUnloadFlags)GB(order_flags, 2, 3));
11679
af86fa342537 (svn r16062) -Fix (r16037): AIOrder::InsertOrder was broken. Default all AI orders to 'stop at far end'.
frosch <frosch@openttd.org>
parents: 11334
diff changeset
359 order.SetStopLocation(OSL_PLATFORM_FAR_END);
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
360 break;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
361
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
362 case OT_GOTO_WAYPOINT:
12472
604a2cfc77ab (svn r16909) -Fix [FS#2996]: NewGRF stations would be triggering assertions all over the place when using the more advanced station types.
rubidium <rubidium@openttd.org>
parents: 12361
diff changeset
363 order.MakeGoToWaypoint(::GetStationIndex(destination));
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
364 break;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
365
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
366 default:
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
367 return false;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
368 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
369
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
370 order.SetNonStopType((OrderNonStopFlags)GB(order_flags, 0, 2));
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
371
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
372 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), order.Pack(), CMD_INSERT_ORDER);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
373 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
374
11175
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
375 /* static */ bool AIOrder::InsertConditionalOrder(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
376 {
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
377 /* IsValidVehicleOrder is not good enough because it does not allow appending. */
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
378 if (order_position == ORDER_CURRENT) order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
379
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
380 EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
381 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to));
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
382
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
383 Order order;
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
384 order.MakeConditional(jump_to);
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
385
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
386 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), order.Pack(), CMD_INSERT_ORDER);
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
387 }
5575028fb7e3 (svn r15526) -Add [NoAI]: Support for conditional orders.
yexo <yexo@openttd.org>
parents: 10960
diff changeset
388
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
389 /* static */ bool AIOrder::RemoveOrder(VehicleID vehicle_id, OrderPosition order_position)
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
390 {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
391 order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
392
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
393 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
394
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
395 return AIObject::DoCommand(0, vehicle_id, order_position, CMD_DELETE_ORDER);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
396 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
397
11334
6a22ff18d5a5 (svn r15684) -Add [NoAI]: AIOrder::SkipToOrder().
yexo <yexo@openttd.org>
parents: 11267
diff changeset
398 /* static */ bool AIOrder::SkipToOrder(VehicleID vehicle_id, OrderPosition next_order)
6a22ff18d5a5 (svn r15684) -Add [NoAI]: AIOrder::SkipToOrder().
yexo <yexo@openttd.org>
parents: 11267
diff changeset
399 {
6a22ff18d5a5 (svn r15684) -Add [NoAI]: AIOrder::SkipToOrder().
yexo <yexo@openttd.org>
parents: 11267
diff changeset
400 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, next_order));
6a22ff18d5a5 (svn r15684) -Add [NoAI]: AIOrder::SkipToOrder().
yexo <yexo@openttd.org>
parents: 11267
diff changeset
401
6a22ff18d5a5 (svn r15684) -Add [NoAI]: AIOrder::SkipToOrder().
yexo <yexo@openttd.org>
parents: 11267
diff changeset
402 return AIObject::DoCommand(0, vehicle_id, next_order, CMD_SKIP_TO_ORDER);
6a22ff18d5a5 (svn r15684) -Add [NoAI]: AIOrder::SkipToOrder().
yexo <yexo@openttd.org>
parents: 11267
diff changeset
403 }
6a22ff18d5a5 (svn r15684) -Add [NoAI]: AIOrder::SkipToOrder().
yexo <yexo@openttd.org>
parents: 11267
diff changeset
404
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
405 /**
11267
1d456bf484a1 (svn r15616) -Change [API CHANGE]: Rename AIOrder::ChangeOrder() to AIOrder::SetOrderFlags().
yexo <yexo@openttd.org>
parents: 11176
diff changeset
406 * Callback handler as SetOrderFlags possibly needs multiple DoCommand calls
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
407 * to be able to set all order flags correctly. As we need to wait till the
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
408 * command has completed before we know the next bits to change we need to
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
409 * call the function multiple times. Each time it'll reduce the difference
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
410 * between the wanted and the current order.
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
411 * @param instance The AI we are doing the callback for.
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
412 */
11267
1d456bf484a1 (svn r15616) -Change [API CHANGE]: Rename AIOrder::ChangeOrder() to AIOrder::SetOrderFlags().
yexo <yexo@openttd.org>
parents: 11176
diff changeset
413 static void _DoCommandReturnSetOrderFlags(class AIInstance *instance)
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
414 {
11267
1d456bf484a1 (svn r15616) -Change [API CHANGE]: Rename AIOrder::ChangeOrder() to AIOrder::SetOrderFlags().
yexo <yexo@openttd.org>
parents: 11176
diff changeset
415 AIObject::SetLastCommandRes(AIOrder::_SetOrderFlags());
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
416 AIInstance::DoCommandReturn(instance);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
417 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
418
11267
1d456bf484a1 (svn r15616) -Change [API CHANGE]: Rename AIOrder::ChangeOrder() to AIOrder::SetOrderFlags().
yexo <yexo@openttd.org>
parents: 11176
diff changeset
419 /* static */ bool AIOrder::_SetOrderFlags()
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
420 {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
421 /* Make sure we don't go into an infinite loop */
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
422 int retry = AIObject::GetCallbackVariable(3) - 1;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
423 if (retry < 0) {
11267
1d456bf484a1 (svn r15616) -Change [API CHANGE]: Rename AIOrder::ChangeOrder() to AIOrder::SetOrderFlags().
yexo <yexo@openttd.org>
parents: 11176
diff changeset
424 DEBUG(ai, 0, "Possible infinite loop in SetOrderFlags() detected");
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
425 return false;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
426 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
427 AIObject::SetCallbackVariable(3, retry);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
428
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
429 VehicleID vehicle_id = (VehicleID)AIObject::GetCallbackVariable(0);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
430 OrderPosition order_position = (OrderPosition)AIObject::GetCallbackVariable(1);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
431 AIOrderFlags order_flags = (AIOrderFlags)AIObject::GetCallbackVariable(2);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
432
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
433 order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
434
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
435 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
436 EnforcePrecondition(false, AreOrderFlagsValid(GetOrderDestination(vehicle_id, order_position), order_flags));
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
437
11991
a4d3838349e6 (svn r16397) -Codechange: move GetVehicleOrder/GetLastVehicleOrder into Vehicle
rubidium <rubidium@openttd.org>
parents: 11922
diff changeset
438 const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
439
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
440 AIOrderFlags current = GetOrderFlags(vehicle_id, order_position);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
441
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
442 if ((current & AIOF_NON_STOP_FLAGS) != (order_flags & AIOF_NON_STOP_FLAGS)) {
11267
1d456bf484a1 (svn r15616) -Change [API CHANGE]: Rename AIOrder::ChangeOrder() to AIOrder::SetOrderFlags().
yexo <yexo@openttd.org>
parents: 11176
diff changeset
443 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), (order_flags & AIOF_NON_STOP_FLAGS) << 4 | MOF_NON_STOP, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
444 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
445
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
446 switch (order->GetType()) {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
447 case OT_GOTO_DEPOT:
11719
15d3dd64a19b (svn r16107) -Add [NoAI]: AIOF_STOP_IN_DEPOT to the orderflags in AIOrder to allow stop-in-depot orders
yexo <yexo@openttd.org>
parents: 11718
diff changeset
448 if ((current & AIOF_DEPOT_FLAGS) != (order_flags & AIOF_DEPOT_FLAGS)) {
11718
411b82321c8c (svn r16106) -Fix [NoAI]: AIOrder::SetOrderFlags always removed "Service if needed" from goto-depot orders.
yexo <yexo@openttd.org>
parents: 11679
diff changeset
449 uint data = DA_ALWAYS_GO;
411b82321c8c (svn r16106) -Fix [NoAI]: AIOrder::SetOrderFlags always removed "Service if needed" from goto-depot orders.
yexo <yexo@openttd.org>
parents: 11679
diff changeset
450 if (order_flags & AIOF_SERVICE_IF_NEEDED) data = DA_SERVICE;
11719
15d3dd64a19b (svn r16107) -Add [NoAI]: AIOF_STOP_IN_DEPOT to the orderflags in AIOrder to allow stop-in-depot orders
yexo <yexo@openttd.org>
parents: 11718
diff changeset
451 if (order_flags & AIOF_STOP_IN_DEPOT) data = DA_STOP;
11718
411b82321c8c (svn r16106) -Fix [NoAI]: AIOrder::SetOrderFlags always removed "Service if needed" from goto-depot orders.
yexo <yexo@openttd.org>
parents: 11679
diff changeset
452 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), (data << 4) | MOF_DEPOT_ACTION, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
453 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
454 break;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
455
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
456 case OT_GOTO_STATION:
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
457 if ((current & AIOF_UNLOAD_FLAGS) != (order_flags & AIOF_UNLOAD_FLAGS)) {
11267
1d456bf484a1 (svn r15616) -Change [API CHANGE]: Rename AIOrder::ChangeOrder() to AIOrder::SetOrderFlags().
yexo <yexo@openttd.org>
parents: 11176
diff changeset
458 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), (order_flags & AIOF_UNLOAD_FLAGS) << 2 | MOF_UNLOAD, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
459 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
460 if ((current & AIOF_LOAD_FLAGS) != (order_flags & AIOF_LOAD_FLAGS)) {
11267
1d456bf484a1 (svn r15616) -Change [API CHANGE]: Rename AIOrder::ChangeOrder() to AIOrder::SetOrderFlags().
yexo <yexo@openttd.org>
parents: 11176
diff changeset
461 return AIObject::DoCommand(0, vehicle_id | (order_position << 16), (order_flags & AIOF_LOAD_FLAGS) >> 1 | MOF_LOAD, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
462 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
463 break;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
464
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
465 default: break;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
466 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
467
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
468 assert(GetOrderFlags(vehicle_id, order_position) == order_flags);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
469
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
470 return true;
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
471 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
472
11267
1d456bf484a1 (svn r15616) -Change [API CHANGE]: Rename AIOrder::ChangeOrder() to AIOrder::SetOrderFlags().
yexo <yexo@openttd.org>
parents: 11176
diff changeset
473 /* static */ bool AIOrder::SetOrderFlags(VehicleID vehicle_id, OrderPosition order_position, AIOrder::AIOrderFlags order_flags)
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
474 {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
475 AIObject::SetCallbackVariable(0, vehicle_id);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
476 AIObject::SetCallbackVariable(1, order_position);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
477 AIObject::SetCallbackVariable(2, order_flags);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
478 /* In case another client(s) change orders at the same time we could
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
479 * end in an infinite loop. This stops that from happening ever. */
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
480 AIObject::SetCallbackVariable(3, 8);
11267
1d456bf484a1 (svn r15616) -Change [API CHANGE]: Rename AIOrder::ChangeOrder() to AIOrder::SetOrderFlags().
yexo <yexo@openttd.org>
parents: 11176
diff changeset
481 return AIOrder::_SetOrderFlags();
1d456bf484a1 (svn r15616) -Change [API CHANGE]: Rename AIOrder::ChangeOrder() to AIOrder::SetOrderFlags().
yexo <yexo@openttd.org>
parents: 11176
diff changeset
482 }
1d456bf484a1 (svn r15616) -Change [API CHANGE]: Rename AIOrder::ChangeOrder() to AIOrder::SetOrderFlags().
yexo <yexo@openttd.org>
parents: 11176
diff changeset
483
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
484 /* static */ bool AIOrder::MoveOrder(VehicleID vehicle_id, OrderPosition order_position_move, OrderPosition order_position_target)
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
485 {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
486 order_position_move = AIOrder::ResolveOrderPosition(vehicle_id, order_position_move);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
487 order_position_target = AIOrder::ResolveOrderPosition(vehicle_id, order_position_target);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
488
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
489 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position_move));
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
490 EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position_target));
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
491
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
492 return AIObject::DoCommand(0, vehicle_id, order_position_move | (order_position_target << 16), CMD_MOVE_ORDER);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
493 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
494
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
495 /* static */ bool AIOrder::CopyOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
496 {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
497 EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
498 EnforcePrecondition(false, AIVehicle::IsValidVehicle(main_vehicle_id));
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
499
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
500 return AIObject::DoCommand(0, vehicle_id | (main_vehicle_id << 16), CO_COPY, CMD_CLONE_ORDER);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
501 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
502
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
503 /* static */ bool AIOrder::ShareOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
504 {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
505 EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
506 EnforcePrecondition(false, AIVehicle::IsValidVehicle(main_vehicle_id));
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
507
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
508 return AIObject::DoCommand(0, vehicle_id | (main_vehicle_id << 16), CO_SHARE, CMD_CLONE_ORDER);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
509 }
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
510
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
511 /* static */ bool AIOrder::UnshareOrders(VehicleID vehicle_id)
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
512 {
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
513 EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
514
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
515 return AIObject::DoCommand(0, vehicle_id, CO_UNSHARE, CMD_CLONE_ORDER);
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
diff changeset
516 }