Add: INR currency (#8136)
[openttd-github.git] / src / order_backup.cpp
blobd537d8ce5c68f0ae257caa516e001d6a3f558c85
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file order_backup.cpp Handling of order backups. */
10 #include "stdafx.h"
11 #include "command_func.h"
12 #include "core/pool_func.hpp"
13 #include "network/network.h"
14 #include "network/network_func.h"
15 #include "order_backup.h"
16 #include "vehicle_base.h"
17 #include "window_func.h"
18 #include "station_map.h"
20 #include "safeguards.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;
31 while (o != nullptr) {
32 Order *next = o->next;
33 delete o;
34 o = next;
38 /**
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)
45 this->user = user;
46 this->tile = v->tile;
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();
54 } else {
55 /* Else copy the orders */
56 Order **tail = &this->orders;
58 /* Count the number of orders */
59 const Order *order;
60 FOR_VEHICLE_ORDERS(v, order) {
61 Order *copy = new Order();
62 copy->AssignOrder(*order);
63 *tail = copy;
64 tail = &copy->next;
69 /**
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 != nullptr) {
77 DoCommand(0, v->index | CO_SHARE << 30, this->clone->index, DC_EXEC, CMD_CLONE_ORDER);
78 } else if (this->orders != nullptr && OrderList::CanAllocateItem()) {
79 v->orders.list = new OrderList(this->orders, v);
80 this->orders = nullptr;
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);
95 /**
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. */
105 for (OrderBackup *ob : OrderBackup::Iterate()) {
106 if (ob->user == user) delete ob;
108 if (OrderBackup::CanAllocateItem()) {
109 new OrderBackup(v, user);
114 * Restore the data of this order to the given vehicle.
115 * @param v The vehicle to restore to.
116 * @param user The user that built the vehicle, thus wants to restore.
117 * @note After restoration the backup will automatically be removed.
119 /* static */ void OrderBackup::Restore(Vehicle *v, uint32 user)
121 for (OrderBackup *ob : OrderBackup::Iterate()) {
122 if (v->tile != ob->tile || ob->user != user) continue;
124 ob->DoRestore(v);
125 delete ob;
130 * Reset an OrderBackup given a tile and user.
131 * @param tile The tile associated with the OrderBackup.
132 * @param user The user associated with the OrderBackup.
133 * @note Must not be used from the GUI!
135 /* static */ void OrderBackup::ResetOfUser(TileIndex tile, uint32 user)
137 for (OrderBackup *ob : OrderBackup::Iterate()) {
138 if (ob->user == user && (ob->tile == tile || tile == INVALID_TILE)) delete ob;
143 * Clear an OrderBackup
144 * @param tile Tile related to the to-be-cleared OrderBackup.
145 * @param flags For command.
146 * @param p1 Unused.
147 * @param p2 User that had the OrderBackup.
148 * @param text Unused.
149 * @return The cost of this operation or an error.
151 CommandCost CmdClearOrderBackup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
153 /* No need to check anything. If the tile or user don't exist we just ignore it. */
154 if (flags & DC_EXEC) OrderBackup::ResetOfUser(tile == 0 ? INVALID_TILE : tile, p2);
156 return CommandCost();
160 * Reset an user's OrderBackup if needed.
161 * @param user The user associated with the OrderBackup.
162 * @pre _network_server.
163 * @note Must not be used from a command.
165 /* static */ void OrderBackup::ResetUser(uint32 user)
167 assert(_network_server);
169 for (OrderBackup *ob : OrderBackup::Iterate()) {
170 /* If it's not a backup of us, ignore it. */
171 if (ob->user != user) continue;
173 DoCommandP(0, 0, user, CMD_CLEAR_ORDER_BACKUP);
174 return;
179 * Reset the OrderBackups from GUI/game logic.
180 * @param t The tile of the order backup.
181 * @param from_gui Whether the call came from the GUI, i.e. whether
182 * it must be synced over the network.
184 /* static */ void OrderBackup::Reset(TileIndex t, bool from_gui)
186 /* The user has CLIENT_ID_SERVER as default when network play is not active,
187 * but compiled it. A network client has its own variable for the unique
188 * client/user identifier. Finally if networking isn't compiled in the
189 * default is just plain and simple: 0. */
190 uint32 user = _networking && !_network_server ? _network_own_client_id : CLIENT_ID_SERVER;
192 for (OrderBackup *ob : OrderBackup::Iterate()) {
193 /* If it's not a backup of us, ignore it. */
194 if (ob->user != user) continue;
195 /* If it's not for our chosen tile either, ignore it. */
196 if (t != INVALID_TILE && t != ob->tile) continue;
198 if (from_gui) {
199 /* We need to circumvent the "prevention" from this command being executed
200 * while the game is paused, so use the internal method. Nor do we want
201 * this command to get its cost estimated when shift is pressed. */
202 DoCommandPInternal(ob->tile, 0, user, CMD_CLEAR_ORDER_BACKUP, nullptr, nullptr, true, false);
203 } else {
204 /* The command came from the game logic, i.e. the clearing of a tile.
205 * In that case we have no need to actually sync this, just do it. */
206 delete ob;
212 * Clear the group of all backups having this group ID.
213 * @param group The group to clear.
215 /* static */ void OrderBackup::ClearGroup(GroupID group)
217 for (OrderBackup *ob : OrderBackup::Iterate()) {
218 if (ob->group == group) ob->group = DEFAULT_GROUP;
223 * Clear/update the (clone) vehicle from an order backup.
224 * @param v The vehicle to clear.
225 * @pre v != nullptr
226 * @note If it is not possible to set another vehicle as clone
227 * "example", then this backed up order will be removed.
229 /* static */ void OrderBackup::ClearVehicle(const Vehicle *v)
231 assert(v != nullptr);
232 for (OrderBackup *ob : OrderBackup::Iterate()) {
233 if (ob->clone == v) {
234 /* Get another item in the shared list. */
235 ob->clone = (v->FirstShared() == v) ? v->NextShared() : v->FirstShared();
236 /* But if that isn't there, remove it. */
237 if (ob->clone == nullptr) delete ob;
243 * Removes an order from all vehicles. Triggers when, say, a station is removed.
244 * @param type The type of the order (OT_GOTO_[STATION|DEPOT|WAYPOINT]).
245 * @param destination The destination. Can be a StationID, DepotID or WaypointID.
246 * @param hangar Only used for airports in the destination.
247 * When false, remove airport and hangar orders.
248 * When true, remove either airport or hangar order.
250 /* static */ void OrderBackup::RemoveOrder(OrderType type, DestinationID destination, bool hangar)
252 for (OrderBackup *ob : OrderBackup::Iterate()) {
253 for (Order *order = ob->orders; order != nullptr; order = order->next) {
254 OrderType ot = order->GetType();
255 if (ot == OT_GOTO_DEPOT && (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) continue;
256 if (ot == OT_GOTO_DEPOT && hangar && !IsHangarTile(ob->tile)) continue; // Not an aircraft? Can't have a hangar order.
257 if (ot == OT_IMPLICIT || (IsHangarTile(ob->tile) && ot == OT_GOTO_DEPOT && !hangar)) ot = OT_GOTO_STATION;
258 if (ot == type && order->GetDestination() == destination) {
259 /* Remove the order backup! If a station/depot gets removed, we can't/shouldn't restore those broken orders. */
260 delete ob;
261 break;