4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file order_backup.cpp Handling of order backups. */
13 #include "command_func.h"
14 #include "core/pool_func.hpp"
15 #include "network/network.h"
16 #include "network/network_func.h"
17 #include "order_backup.h"
18 #include "vehicle_base.h"
19 #include "window_func.h"
20 #include "station_map.h"
22 #include "safeguards.h"
24 OrderBackupPool
_order_backup_pool("BackupOrder");
25 INSTANTIATE_POOL_METHODS(OrderBackup
)
27 /** Free everything that is allocated. */
28 OrderBackup::~OrderBackup()
30 if (CleaningPool()) return;
32 Order
*o
= this->orders
;
33 while (o
!= nullptr) {
34 Order
*next
= o
->next
;
41 * Create an order backup for the given vehicle.
42 * @param v The vehicle to make a backup of.
43 * @param user The user that is requesting the backup.
45 OrderBackup::OrderBackup(const Vehicle
*v
, uint32 user
)
49 this->group
= v
->group_id
;
51 this->CopyConsistPropertiesFrom(v
);
53 /* If we have shared orders, store the vehicle we share the order with. */
54 if (v
->HasSharedOrdersList()) {
55 this->clone
= (v
->FirstShared() == v
) ? v
->NextShared() : v
->FirstShared();
57 /* Else copy the orders */
58 Order
**tail
= &this->orders
;
60 /* Count the number of orders */
62 FOR_VEHICLE_ORDERS(v
, order
) {
63 Order
*copy
= new Order();
64 copy
->AssignOrder(*order
);
72 * Restore the data of this order to the given vehicle.
73 * @param v The vehicle to restore to.
75 void OrderBackup::DoRestore(Vehicle
*v
)
77 /* If we had shared orders, recover that */
78 if (this->clone
!= nullptr) {
79 DoCommand(0, v
->index
| CO_SHARE
<< 30, this->clone
->index
, DC_EXEC
, CMD_CLONE_ORDER
);
80 } else if (this->orders
!= nullptr && OrderList::CanAllocateItem()) {
81 v
->SetOrdersList(new OrderList(this->orders
, v
));
82 this->orders
= nullptr;
83 /* Make sure buoys/oil rigs are updated in the station list. */
84 InvalidateWindowClassesData(WC_STATION_LIST
, 0);
87 v
->CopyConsistPropertiesFrom(this);
89 /* Make sure orders are in range */
90 v
->UpdateRealOrderIndex();
91 if (v
->cur_implicit_order_index
>= v
->GetNumOrders()) v
->cur_implicit_order_index
= v
->cur_real_order_index
;
92 if (v
->cur_timetable_order_index
>= v
->GetNumOrders()) v
->cur_timetable_order_index
= INVALID_VEH_ORDER_ID
;
94 /* Restore vehicle group */
95 DoCommand(0, this->group
, v
->index
, DC_EXEC
, CMD_ADD_VEHICLE_GROUP
);
99 * Create an order backup for the given vehicle.
100 * @param v The vehicle to make a backup of.
101 * @param user The user that is requesting the backup.
102 * @note Will automatically remove any previous backups of this user.
104 /* static */ void OrderBackup::Backup(const Vehicle
*v
, uint32 user
)
106 /* Don't use reset as that broadcasts over the network to reset the variable,
107 * which is what we are doing at the moment. */
109 FOR_ALL_ORDER_BACKUPS(ob
) {
110 if (ob
->user
== user
) delete ob
;
112 if (OrderBackup::CanAllocateItem()) {
113 new OrderBackup(v
, user
);
118 * Restore the data of this order to the given vehicle.
119 * @param v The vehicle to restore to.
120 * @param user The user that built the vehicle, thus wants to restore.
121 * @note After restoration the backup will automatically be removed.
123 /* static */ void OrderBackup::Restore(Vehicle
*v
, uint32 user
)
126 FOR_ALL_ORDER_BACKUPS(ob
) {
127 if (v
->tile
!= ob
->tile
|| ob
->user
!= user
) continue;
135 * Reset an OrderBackup given a tile and user.
136 * @param tile The tile associated with the OrderBackup.
137 * @param user The user associated with the OrderBackup.
138 * @note Must not be used from the GUI!
140 /* static */ void OrderBackup::ResetOfUser(TileIndex tile
, uint32 user
)
143 FOR_ALL_ORDER_BACKUPS(ob
) {
144 if (ob
->user
== user
&& (ob
->tile
== tile
|| tile
== INVALID_TILE
)) delete ob
;
149 * Clear an OrderBackup
150 * @param tile Tile related to the to-be-cleared OrderBackup.
151 * @param flags For command.
153 * @param p2 User that had the OrderBackup.
154 * @param text Unused.
155 * @return The cost of this operation or an error.
157 CommandCost
CmdClearOrderBackup(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
159 /* No need to check anything. If the tile or user don't exist we just ignore it. */
160 if (flags
& DC_EXEC
) OrderBackup::ResetOfUser(tile
== 0 ? INVALID_TILE
: tile
, p2
);
162 return CommandCost();
166 * Reset an user's OrderBackup if needed.
167 * @param user The user associated with the OrderBackup.
168 * @pre _network_server.
169 * @note Must not be used from a command.
171 /* static */ void OrderBackup::ResetUser(uint32 user
)
173 assert(_network_server
);
176 FOR_ALL_ORDER_BACKUPS(ob
) {
177 /* If it's not an backup of us, so ignore it. */
178 if (ob
->user
!= user
) continue;
180 DoCommandP(0, 0, user
, CMD_CLEAR_ORDER_BACKUP
);
186 * Reset the OrderBackups from GUI/game logic.
187 * @param t The tile of the order backup.
188 * @param from_gui Whether the call came from the GUI, i.e. whether
189 * it must be synced over the network.
191 /* static */ void OrderBackup::Reset(TileIndex t
, bool from_gui
)
193 /* The user has CLIENT_ID_SERVER as default when network play is not active,
194 * but compiled it. A network client has its own variable for the unique
195 * client/user identifier. Finally if networking isn't compiled in the
196 * default is just plain and simple: 0. */
197 #ifdef ENABLE_NETWORK
198 uint32 user
= _networking
&& !_network_server
? _network_own_client_id
: CLIENT_ID_SERVER
;
204 FOR_ALL_ORDER_BACKUPS(ob
) {
205 /* If it's not an backup of us, so ignore it. */
206 if (ob
->user
!= user
) continue;
207 /* If it's not for our chosen tile either, ignore it. */
208 if (t
!= INVALID_TILE
&& t
!= ob
->tile
) continue;
211 /* We need to circumvent the "prevention" from this command being executed
212 * while the game is paused, so use the internal method. Nor do we want
213 * this command to get its cost estimated when shift is pressed. */
214 DoCommandPInternal(ob
->tile
, 0, user
, CMD_CLEAR_ORDER_BACKUP
, nullptr, nullptr, true, false, 0);
216 /* The command came from the game logic, i.e. the clearing of a tile.
217 * In that case we have no need to actually sync this, just do it. */
224 * Clear the group of all backups having this group ID.
225 * @param group The group to clear.
227 /* static */ void OrderBackup::ClearGroup(GroupID group
)
230 FOR_ALL_ORDER_BACKUPS(ob
) {
231 if (ob
->group
== group
) ob
->group
= DEFAULT_GROUP
;
236 * Clear/update the (clone) vehicle from an order backup.
237 * @param v The vehicle to clear.
239 * @note If it is not possible to set another vehicle as clone
240 * "example", then this backed up order will be removed.
242 /* static */ void OrderBackup::ClearVehicle(const Vehicle
*v
)
244 assert(v
!= nullptr);
246 FOR_ALL_ORDER_BACKUPS(ob
) {
247 if (ob
->clone
== v
) {
248 /* Get another item in the shared list. */
249 ob
->clone
= (v
->FirstShared() == v
) ? v
->NextShared() : v
->FirstShared();
250 /* But if that isn't there, remove it. */
251 if (ob
->clone
== nullptr) delete ob
;
257 * Removes an order from all vehicles. Triggers when, say, a station is removed.
258 * @param type The type of the order (OT_GOTO_[STATION|DEPOT|WAYPOINT]).
259 * @param destination The destination. Can be a StationID, DepotID or WaypointID.
261 /* static */ void OrderBackup::RemoveOrder(OrderType type
, DestinationID destination
)
264 FOR_ALL_ORDER_BACKUPS(ob
) {
265 for (Order
*order
= ob
->orders
; order
!= nullptr; order
= order
->next
) {
266 OrderType ot
= order
->GetType();
267 if (ot
== OT_GOTO_DEPOT
&& (order
->GetDepotActionType() & ODATFB_NEAREST_DEPOT
) != 0) continue;
268 if (ot
== OT_IMPLICIT
|| (IsHangarTile(ob
->tile
) && ot
== OT_GOTO_DEPOT
)) ot
= OT_GOTO_STATION
;
269 if (ot
== type
&& order
->GetDestination() == destination
) {
270 /* Remove the order backup! If a station/depot gets removed, we can't/shouldn't restore those broken orders. */