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_func.h"
22 OrderBackupPool
_order_backup_pool("BackupOrder");
23 INSTANTIATE_POOL_METHODS(OrderBackup
)
25 /** Free everything that is allocated. */
26 OrderBackup::~OrderBackup()
28 if (CleaningPool()) return;
30 Order
*o
= this->orders
;
32 Order
*next
= o
->next
;
39 * Create an order backup for the given vehicle.
40 * @param v The vehicle to make a backup of.
41 * @param user The user that is requesting the backup.
43 OrderBackup::OrderBackup(const Vehicle
*v
, uint32 user
)
47 this->group
= v
->group_id
;
49 this->CopyConsistPropertiesFrom(v
);
51 /* If we have shared orders, store the vehicle we share the order with. */
52 if (v
->IsOrderListShared()) {
53 this->clone
= (v
->FirstShared() == v
) ? v
->NextShared() : v
->FirstShared();
55 /* Else copy the orders */
56 Order
**tail
= &this->orders
;
58 /* Count the number of orders */
60 FOR_VEHICLE_ORDERS(v
, order
) {
61 Order
*copy
= new Order();
62 copy
->AssignOrder(*order
);
70 * Restore the data of this order to the given vehicle.
71 * @param v The vehicle to restore to.
73 void OrderBackup::DoRestore(Vehicle
*v
)
75 /* If we had shared orders, recover that */
76 if (this->clone
!= NULL
) {
77 DoCommand(0, v
->index
| CO_SHARE
<< 30, this->clone
->index
, DC_EXEC
, CMD_CLONE_ORDER
);
78 } else if (this->orders
!= NULL
&& OrderList::CanAllocateItem()) {
79 v
->orders
.list
= new OrderList(this->orders
, v
);
81 /* Make sure buoys/oil rigs are updated in the station list. */
82 InvalidateWindowClassesData(WC_STATION_LIST
, 0);
85 v
->CopyConsistPropertiesFrom(this);
87 /* Make sure orders are in range */
88 v
->UpdateRealOrderIndex();
89 if (v
->cur_implicit_order_index
>= v
->GetNumOrders()) v
->cur_implicit_order_index
= v
->cur_real_order_index
;
91 /* Restore vehicle group */
92 DoCommand(0, this->group
, v
->index
, DC_EXEC
, CMD_ADD_VEHICLE_GROUP
);
96 * Create an order backup for the given vehicle.
97 * @param v The vehicle to make a backup of.
98 * @param user The user that is requesting the backup.
99 * @note Will automatically remove any previous backups of this user.
101 /* static */ void OrderBackup::Backup(const Vehicle
*v
, uint32 user
)
103 /* Don't use reset as that broadcasts over the network to reset the variable,
104 * which is what we are doing at the moment. */
106 FOR_ALL_ORDER_BACKUPS(ob
) {
107 if (ob
->user
== user
) delete ob
;
109 if (OrderBackup::CanAllocateItem()) {
110 new OrderBackup(v
, user
);
115 * Restore the data of this order to the given vehicle.
116 * @param v The vehicle to restore to.
117 * @param user The user that built the vehicle, thus wants to restore.
118 * @note After restoration the backup will automatically be removed.
120 /* static */ void OrderBackup::Restore(Vehicle
*v
, uint32 user
)
123 FOR_ALL_ORDER_BACKUPS(ob
) {
124 if (v
->tile
!= ob
->tile
|| ob
->user
!= user
) continue;
132 * Reset an OrderBackup given a tile and user.
133 * @param tile The tile associated with the OrderBackup.
134 * @param user The user associated with the OrderBackup.
135 * @note Must not be used from the GUI!
137 /* static */ void OrderBackup::ResetOfUser(TileIndex tile
, uint32 user
)
140 FOR_ALL_ORDER_BACKUPS(ob
) {
141 if (ob
->user
== user
&& (ob
->tile
== tile
|| tile
== INVALID_TILE
)) delete ob
;
146 * Clear an OrderBackup
147 * @param tile Tile related to the to-be-cleared OrderBackup.
148 * @param flags For command.
150 * @param p2 User that had the OrderBackup.
151 * @param text Unused.
152 * @return The cost of this operation or an error.
154 CommandCost
CmdClearOrderBackup(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
156 /* No need to check anything. If the tile or user don't exist we just ignore it. */
157 if (flags
& DC_EXEC
) OrderBackup::ResetOfUser(tile
== 0 ? INVALID_TILE
: tile
, p2
);
159 return CommandCost();
163 * Reset an user's OrderBackup if needed.
164 * @param user The user associated with the OrderBackup.
165 * @pre _network_server.
166 * @note Must not be used from a command.
168 /* static */ void OrderBackup::ResetUser(uint32 user
)
170 assert(_network_server
);
173 FOR_ALL_ORDER_BACKUPS(ob
) {
174 /* If it's not an backup of us, so ignore it. */
175 if (ob
->user
!= user
) continue;
177 DoCommandP(0, 0, user
, CMD_CLEAR_ORDER_BACKUP
);
183 * Reset the OrderBackups from GUI/game logic.
184 * @param t The tile of the order backup.
185 * @param from_gui Whether the call came from the GUI, i.e. whether
186 * it must be synced over the network.
188 /* static */ void OrderBackup::Reset(TileIndex t
, bool from_gui
)
190 /* The user has CLIENT_ID_SERVER as default when network play is not active,
191 * but compiled it. A network client has its own variable for the unique
192 * client/user identifier. Finally if networking isn't compiled in the
193 * default is just plain and simple: 0. */
194 #ifdef ENABLE_NETWORK
195 uint32 user
= _networking
&& !_network_server
? _network_own_client_id
: CLIENT_ID_SERVER
;
201 FOR_ALL_ORDER_BACKUPS(ob
) {
202 /* If it's not an backup of us, so ignore it. */
203 if (ob
->user
!= user
) continue;
204 /* If it's not for our chosen tile either, ignore it. */
205 if (t
!= INVALID_TILE
&& t
!= ob
->tile
) continue;
208 /* We need to circumvent the "prevention" from this command being executed
209 * while the game is paused, so use the internal method. Nor do we want
210 * this command to get its cost estimated when shift is pressed. */
211 DoCommandPInternal(ob
->tile
, 0, user
, CMD_CLEAR_ORDER_BACKUP
, NULL
, NULL
, true, false);
213 /* The command came from the game logic, i.e. the clearing of a tile.
214 * In that case we have no need to actually sync this, just do it. */
221 * Clear the group of all backups having this group ID.
222 * @param group The group to clear.
224 /* static */ void OrderBackup::ClearGroup(GroupID group
)
227 FOR_ALL_ORDER_BACKUPS(ob
) {
228 if (ob
->group
== group
) ob
->group
= DEFAULT_GROUP
;
233 * Clear/update the (clone) vehicle from an order backup.
234 * @param v The vehicle to clear.
236 * @note If it is not possible to set another vehicle as clone
237 * "example", then this backed up order will be removed.
239 /* static */ void OrderBackup::ClearVehicle(const Vehicle
*v
)
243 FOR_ALL_ORDER_BACKUPS(ob
) {
244 if (ob
->clone
== v
) {
245 /* Get another item in the shared list. */
246 ob
->clone
= (v
->FirstShared() == v
) ? v
->NextShared() : v
->FirstShared();
247 /* But if that isn't there, remove it. */
248 if (ob
->clone
== NULL
) delete ob
;
254 * Removes an order from all vehicles. Triggers when, say, a station is removed.
255 * @param type The type of the order (OT_GOTO_[STATION|DEPOT|WAYPOINT]).
256 * @param destination The destination. Can be a StationID, DepotID or WaypointID.
258 /* static */ void OrderBackup::RemoveOrder(OrderType type
, DestinationID destination
)
261 FOR_ALL_ORDER_BACKUPS(ob
) {
262 for (Order
*order
= ob
->orders
; order
!= NULL
; order
= order
->next
) {
263 OrderType ot
= order
->GetType();
264 if (ot
== OT_GOTO_DEPOT
&& (order
->GetDepotActionType() & ODATFB_NEAREST_DEPOT
) != 0) continue;
265 if (ot
== OT_IMPLICIT
|| (IsHangarTile(ob
->tile
) && ot
== OT_GOTO_DEPOT
)) ot
= OT_GOTO_STATION
;
266 if (ot
== type
&& order
->GetDestination() == destination
) {
267 /* Remove the order backup! If a station/depot gets removed, we can't/shouldn't restore those broken orders. */