OpenTTD
order_backup.cpp
Go to the documentation of this file.
1 /* $Id: order_backup.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2 
3 /*
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/>.
8  */
9 
12 #include "stdafx.h"
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"
21 
22 #include "safeguards.h"
23 
24 OrderBackupPool _order_backup_pool("BackupOrder");
26 
27 
29 {
30  if (CleaningPool()) return;
31 
32  Order *o = this->orders;
33  while (o != NULL) {
34  Order *next = o->next;
35  delete o;
36  o = next;
37  }
38 }
39 
45 OrderBackup::OrderBackup(const Vehicle *v, uint32 user)
46 {
47  this->user = user;
48  this->tile = v->tile;
49  this->group = v->group_id;
50 
52 
53  /* If we have shared orders, store the vehicle we share the order with. */
54  if (v->IsOrderListShared()) {
55  this->clone = (v->FirstShared() == v) ? v->NextShared() : v->FirstShared();
56  } else {
57  /* Else copy the orders */
58  Order **tail = &this->orders;
59 
60  /* Count the number of orders */
61  const Order *order;
62  FOR_VEHICLE_ORDERS(v, order) {
63  Order *copy = new Order();
64  copy->AssignOrder(*order);
65  *tail = copy;
66  tail = &copy->next;
67  }
68  }
69 }
70 
76 {
77  /* If we had shared orders, recover that */
78  if (this->clone != NULL) {
79  DoCommand(0, v->index | CO_SHARE << 30, this->clone->index, DC_EXEC, CMD_CLONE_ORDER);
80  } else if (this->orders != NULL && OrderList::CanAllocateItem()) {
81  v->orders.list = new OrderList(this->orders, v);
82  this->orders = NULL;
83  /* Make sure buoys/oil rigs are updated in the station list. */
85  }
86 
88 
89  /* Make sure orders are in range */
92 
93  /* Restore vehicle group */
95 }
96 
103 /* static */ void OrderBackup::Backup(const Vehicle *v, uint32 user)
104 {
105  /* Don't use reset as that broadcasts over the network to reset the variable,
106  * which is what we are doing at the moment. */
107  OrderBackup *ob;
109  if (ob->user == user) delete ob;
110  }
112  new OrderBackup(v, user);
113  }
114 }
115 
122 /* static */ void OrderBackup::Restore(Vehicle *v, uint32 user)
123 {
124  OrderBackup *ob;
126  if (v->tile != ob->tile || ob->user != user) continue;
127 
128  ob->DoRestore(v);
129  delete ob;
130  }
131 }
132 
139 /* static */ void OrderBackup::ResetOfUser(TileIndex tile, uint32 user)
140 {
141  OrderBackup *ob;
143  if (ob->user == user && (ob->tile == tile || tile == INVALID_TILE)) delete ob;
144  }
145 }
146 
156 CommandCost CmdClearOrderBackup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
157 {
158  /* No need to check anything. If the tile or user don't exist we just ignore it. */
159  if (flags & DC_EXEC) OrderBackup::ResetOfUser(tile == 0 ? INVALID_TILE : tile, p2);
160 
161  return CommandCost();
162 }
163 
170 /* static */ void OrderBackup::ResetUser(uint32 user)
171 {
172  assert(_network_server);
173 
174  OrderBackup *ob;
176  /* If it's not an backup of us, so ignore it. */
177  if (ob->user != user) continue;
178 
179  DoCommandP(0, 0, user, CMD_CLEAR_ORDER_BACKUP);
180  return;
181  }
182 }
183 
190 /* static */ void OrderBackup::Reset(TileIndex t, bool from_gui)
191 {
192  /* The user has CLIENT_ID_SERVER as default when network play is not active,
193  * but compiled it. A network client has its own variable for the unique
194  * client/user identifier. Finally if networking isn't compiled in the
195  * default is just plain and simple: 0. */
196 #ifdef ENABLE_NETWORK
198 #else
199  uint32 user = 0;
200 #endif
201 
202  OrderBackup *ob;
204  /* If it's not an backup of us, so ignore it. */
205  if (ob->user != user) continue;
206  /* If it's not for our chosen tile either, ignore it. */
207  if (t != INVALID_TILE && t != ob->tile) continue;
208 
209  if (from_gui) {
210  /* We need to circumvent the "prevention" from this command being executed
211  * while the game is paused, so use the internal method. Nor do we want
212  * this command to get its cost estimated when shift is pressed. */
213  DoCommandPInternal(ob->tile, 0, user, CMD_CLEAR_ORDER_BACKUP, NULL, NULL, true, false);
214  } else {
215  /* The command came from the game logic, i.e. the clearing of a tile.
216  * In that case we have no need to actually sync this, just do it. */
217  delete ob;
218  }
219  }
220 }
221 
227 {
228  OrderBackup *ob;
230  if (ob->group == group) ob->group = DEFAULT_GROUP;
231  }
232 }
233 
241 /* static */ void OrderBackup::ClearVehicle(const Vehicle *v)
242 {
243  assert(v != NULL);
244  OrderBackup *ob;
246  if (ob->clone == v) {
247  /* Get another item in the shared list. */
248  ob->clone = (v->FirstShared() == v) ? v->NextShared() : v->FirstShared();
249  /* But if that isn't there, remove it. */
250  if (ob->clone == NULL) delete ob;
251  }
252  }
253 }
254 
260 /* static */ void OrderBackup::RemoveOrder(OrderType type, DestinationID destination)
261 {
262  OrderBackup *ob;
264  for (Order *order = ob->orders; order != NULL; order = order->next) {
265  OrderType ot = order->GetType();
266  if (ot == OT_GOTO_DEPOT && (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) continue;
267  if (ot == OT_IMPLICIT || (IsHangarTile(ob->tile) && ot == OT_GOTO_DEPOT)) ot = OT_GOTO_STATION;
268  if (ot == type && order->GetDestination() == destination) {
269  /* Remove the order backup! If a station/depot gets removed, we can't/shouldn't restore those broken orders. */
270  delete ob;
271  break;
272  }
273  }
274  }
275 }
clear the order backup of a given user/tile
Definition: command_type.h:225
bool _networking
are we in networking mode?
Definition: network.cpp:56
Maps accessors for stations.
Order * orders
The actual orders if the vehicle was not a clone.
Definition: order_backup.h:46
static void ResetUser(uint32 user)
Reset an user&#39;s OrderBackup if needed.
uint32 user
The user that requested the backup.
Definition: order_backup.h:41
OrderList * list
Pointer to the order list for this vehicle.
Definition: vehicle_base.h:321
TileIndex tile
Tile of the depot where the order was changed.
Definition: order_backup.h:42
Vehicle data structure.
Definition: vehicle_base.h:212
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
clone (and share) an order
Definition: command_type.h:271
Common return value for all commands.
Definition: command_type.h:25
Servers always have this ID.
Definition: network_type.h:45
Order * next
Pointer to next order. If NULL, end of list.
Definition: order_base.h:51
Vehicle * FirstShared() const
Get the first vehicle of this vehicle chain.
Definition: vehicle_base.h:676
static void ResetOfUser(TileIndex tile, uint32 user)
Reset an OrderBackup given a tile and user.
const Vehicle * clone
Vehicle this vehicle was a clone of.
Definition: order_backup.h:45
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition: window.cpp:3238
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:252
Some methods of Pool are placed here in order to reduce compilation time and binary size...
CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
Shorthand for calling the long DoCommand with a container.
Definition: command.cpp:436
#define FOR_ALL_ORDER_BACKUPS(var)
Iterator over all order backups.
Definition: order_backup.h:80
DoCommandFlag
List of flags for a command.
Definition: command_type.h:340
ClientID _network_own_client_id
Our client identifier.
Definition: network.cpp:63
Definition of base types and functions in a cross-platform compatible way.
void CopyConsistPropertiesFrom(const BaseConsist *src)
Copy properties of other BaseConsist.
A number of safeguards to prevent using unsafe methods.
uint16 GroupID
Type for all group identifiers.
Definition: group_type.h:15
CommandCost DoCommandPInternal(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, bool my_cmd, bool estimate_only)
Definition: command.cpp:623
void UpdateRealOrderIndex()
Skip implicit orders until cur_real_order_index is a non-implicit order.
Definition: vehicle_base.h:838
OrderBackupPool _order_backup_pool("BackupOrder")
The pool with order backups.
Functions related to order backups.
TileIndex tile
Current tile index.
Definition: vehicle_base.h:230
Basic functions/variables used all over the place.
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:527
OrderType
Order types.
Definition: order_type.h:37
Base class for all pools.
Definition: pool_type.hpp:83
Station list; Window numbers:
Definition: window_type.h:297
static void Reset(TileIndex tile=INVALID_TILE, bool from_gui=true)
Reset the OrderBackups from GUI/game logic.
static void RemoveOrder(OrderType type, DestinationID destination)
Removes an order from all vehicles.
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don&#39;t get linker errors.
Definition: pool_func.hpp:224
execute the given command
Definition: command_type.h:342
static const GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition: group_type.h:19
CommandCost CmdClearOrderBackup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Clear an OrderBackup.
void DoRestore(Vehicle *v)
Restore the data of this order to the given vehicle.
add a vehicle to a group
Definition: command_type.h:319
static void ClearVehicle(const Vehicle *v)
Clear/update the (clone) vehicle from an order backup.
Base class for all vehicles.
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
Send the vehicle to the nearest depot.
Definition: order_type.h:111
static void ClearGroup(GroupID group)
Clear the group of all backups having this group ID.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:216
void AssignOrder(const Order &other)
Assign data to an order (from another order) This function makes sure that the index is maintained co...
Definition: order_cmd.cpp:274
Functions related to commands.
Network functions used by other parts of OpenTTD.
bool _network_server
network-server is active
Definition: network.cpp:57
OrderBackup()
Creation for savegame restoration.
Definition: order_backup.h:49
VehicleOrderID GetNumOrders() const
Get the number of orders this vehicle has.
Definition: vehicle_base.h:688
bool IsOrderListShared() const
Check if we share our orders with another vehicle.
Definition: vehicle_base.h:682
static bool IsHangarTile(TileIndex t)
Is tile t an hangar tile?
Definition: station_map.h:327
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
GroupID group
The group the vehicle was part of.
Definition: order_backup.h:43
Vehicle * NextShared() const
Get the next vehicle of the shared vehicle chain.
Definition: vehicle_base.h:664
Window functions not directly related to making/drawing windows.
static void Restore(Vehicle *v, uint32 user)
Restore the data of this order to the given vehicle.
static void Backup(const Vehicle *v, uint32 user)
Create an order backup for the given vehicle.
union Vehicle::@46 orders
The orders currently assigned to the vehicle.
Data for backing up an order of a vehicle so it can be restored after a vehicle is rebuilt in the sam...
Definition: order_backup.h:37
VehicleOrderID cur_implicit_order_index
The index to the current implicit order.
Definition: base_consist.h:30
VehicleOrderID cur_real_order_index
The index to the current real (non-implicit) order.
Definition: base_consist.h:29
GroupID group_id
Index of group Pool array.
Definition: vehicle_base.h:326