OpenTTD Source: src/order_backup.cpp Source File

OpenTTD Source 12.2

order_backup.cpp
Go to the documentation of this file.
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 */
7
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"
19
20 #include "safeguards.h"
21
22 OrderBackupPool _order_backup_pool("BackupOrder");
23 INSTANTIATE_POOL_METHODS(OrderBackup)
24
25
26 OrderBackup::~OrderBackup()
27 {
28 if (CleaningPool()) return;
29
30 Order *o = this->orders;
31 while (o != nullptr) {
32 Order *next = o->next;
33 delete o;
34 o = next;
35 }
36 }
37
43 OrderBackup::OrderBackup(const Vehicle *v, uint32 user)
44 {
45 this->user = user;
46 this->tile = v->tile;
47 this->group = v->group_id;
48
49 this->CopyConsistPropertiesFrom(v);
50
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;
57
58 /* Count the number of orders */
59 for (const Order *order : v->Orders()) {
60 Order *copy = new Order();
61 copy->AssignOrder(*order);
62 *tail = copy;
63 tail = &copy->next;
64 }
65 }
66 }
67
72 void OrderBackup::DoRestore(Vehicle *v)
73 {
74 /* If we had shared orders, recover that */
75 if (this->clone != nullptr) {
76 DoCommand(0, v->index | CO_SHARE << 30, this->clone->index, DC_EXEC, CMD_CLONE_ORDER);
77 } else if (this->orders != nullptr && OrderList::CanAllocateItem()) {
78 v->orders.list = new OrderList(this->orders, v);
79 this->orders = nullptr;
80 /* Make sure buoys/oil rigs are updated in the station list. */
81 InvalidateWindowClassesData(WC_STATION_LIST, 0);
82 }
83
84 v->CopyConsistPropertiesFrom(this);
85
86 /* Make sure orders are in range */
87 v->UpdateRealOrderIndex();
88 if (v->cur_implicit_order_index >= v->GetNumOrders()) v->cur_implicit_order_index = v->cur_real_order_index;
89
90 /* Restore vehicle group */
91 DoCommand(0, this->group, v->index, DC_EXEC, CMD_ADD_VEHICLE_GROUP);
92 }
93
100 /* static */ void OrderBackup::Backup(const Vehicle *v, uint32 user)
101 {
102 /* Don't use reset as that broadcasts over the network to reset the variable,
103 * which is what we are doing at the moment. */
104 for (OrderBackup *ob : OrderBackup::Iterate()) {
105 if (ob->user == user) delete ob;
106 }
107 if (OrderBackup::CanAllocateItem()) {
108 new OrderBackup(v, user);
109 }
110 }
111
118 /* static */ void OrderBackup::Restore(Vehicle *v, uint32 user)
119 {
120 for (OrderBackup *ob : OrderBackup::Iterate()) {
121 if (v->tile != ob->tile || ob->user != user) continue;
122
123 ob->DoRestore(v);
124 delete ob;
125 }
126 }
127
134 /* static */ void OrderBackup::ResetOfUser(TileIndex tile, uint32 user)
135 {
136 for (OrderBackup *ob : OrderBackup::Iterate()) {
137 if (ob->user == user && (ob->tile == tile || tile == INVALID_TILE)) delete ob;
138 }
139 }
140
150 CommandCost CmdClearOrderBackup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
151 {
152 /* No need to check anything. If the tile or user don't exist we just ignore it. */
153 if (flags & DC_EXEC) OrderBackup::ResetOfUser(tile == 0 ? INVALID_TILE : tile, p2);
154
155 return CommandCost();
156 }
157
164 /* static */ void OrderBackup::ResetUser(uint32 user)
165 {
166 assert(_network_server);
167
168 for (OrderBackup *ob : OrderBackup::Iterate()) {
169 /* If it's not a backup of us, ignore it. */
170 if (ob->user != user) continue;
171
172 DoCommandP(0, 0, user, CMD_CLEAR_ORDER_BACKUP);
173 return;
174 }
175 }
176
183 /* static */ void OrderBackup::Reset(TileIndex t, bool from_gui)
184 {
185 /* The user has CLIENT_ID_SERVER as default when network play is not active,
186 * but compiled it. A network client has its own variable for the unique
187 * client/user identifier. Finally if networking isn't compiled in the
188 * default is just plain and simple: 0. */
189 uint32 user = _networking && !_network_server ? _network_own_client_id : CLIENT_ID_SERVER;
190
191 for (OrderBackup *ob : OrderBackup::Iterate()) {
192 /* If this is a GUI action, and it's not a backup of us, ignore it. */
193 if (from_gui && ob->user != user) continue;
194 /* If it's not for our chosen tile either, ignore it. */
195 if (t != INVALID_TILE && t != ob->tile) continue;
196
197 if (from_gui) {
198 /* We need to circumvent the "prevention" from this command being executed
199 * while the game is paused, so use the internal method. Nor do we want
200 * this command to get its cost estimated when shift is pressed. */
201 DoCommandPInternal(ob->tile, 0, user, CMD_CLEAR_ORDER_BACKUP, nullptr, {}, true, false);
202 } else {
203 /* The command came from the game logic, i.e. the clearing of a tile.
204 * In that case we have no need to actually sync this, just do it. */
205 delete ob;
206 }
207 }
208 }
209
214 /* static */ void OrderBackup::ClearGroup(GroupID group)
215 {
216 for (OrderBackup *ob : OrderBackup::Iterate()) {
217 if (ob->group == group) ob->group = DEFAULT_GROUP;
218 }
219 }
220
228 /* static */ void OrderBackup::ClearVehicle(const Vehicle *v)
229 {
230 assert(v != nullptr);
231 for (OrderBackup *ob : OrderBackup::Iterate()) {
232 if (ob->clone == v) {
233 /* Get another item in the shared list. */
234 ob->clone = (v->FirstShared() == v) ? v->NextShared() : v->FirstShared();
235 /* But if that isn't there, remove it. */
236 if (ob->clone == nullptr) delete ob;
237 }
238 }
239 }
240
249 /* static */ void OrderBackup::RemoveOrder(OrderType type, DestinationID destination, bool hangar)
250 {
251 for (OrderBackup *ob : OrderBackup::Iterate()) {
252 for (Order *order = ob->orders; order != nullptr; order = order->next) {
253 OrderType ot = order->GetType();
254 if (ot == OT_GOTO_DEPOT && (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) continue;
255 if (ot == OT_GOTO_DEPOT && hangar && !IsHangarTile(ob->tile)) continue; // Not an aircraft? Can't have a hangar order.
256 if (ot == OT_IMPLICIT || (IsHangarTile(ob->tile) && ot == OT_GOTO_DEPOT && !hangar)) ot = OT_GOTO_STATION;
257 if (ot == type && order->GetDestination() == destination) {
258 /* Remove the order backup! If a station/depot gets removed, we can't/shouldn't restore those broken orders. */
259 delete ob;
260 break;
261 }
262 }
263 }
264 }
BaseConsist::cur_implicit_order_index
VehicleOrderID cur_implicit_order_index
The index to the current implicit order.
Definition: base_consist.h:29
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
DoCommand
CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
Shorthand for calling the long DoCommand with a container.
Definition: command.cpp:450
CMD_ADD_VEHICLE_GROUP
@ CMD_ADD_VEHICLE_GROUP
add a vehicle to a group
Definition: command_type.h:324
command_func.h
_network_server
bool _network_server
network-server is active
Definition: network.cpp:58
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
Vehicle::group_id
GroupID group_id
Index of group Pool array.
Definition: vehicle_base.h:337
CmdClearOrderBackup
CommandCost CmdClearOrderBackup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
Clear an OrderBackup.
Definition: order_backup.cpp:150
vehicle_base.h
OrderBackup::ResetOfUser
static void ResetOfUser(TileIndex tile, uint32 user)
Reset an OrderBackup given a tile and user.
Definition: order_backup.cpp:134
Order::AssignOrder
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:272
OrderBackup::clone
const Vehicle * clone
Vehicle this vehicle was a clone of.
Definition: order_backup.h:44
OrderBackup::ClearVehicle
static void ClearVehicle(const Vehicle *v)
Clear/update the (clone) vehicle from an order backup.
Definition: order_backup.cpp:228
Vehicle::orders
union Vehicle::@49 orders
The orders currently assigned to the vehicle.
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:221
_order_backup_pool
OrderBackupPool _order_backup_pool("BackupOrder")
The pool with order backups.
CMD_CLEAR_ORDER_BACKUP
@ CMD_CLEAR_ORDER_BACKUP
clear the order backup of a given user/tile
Definition: command_type.h:224
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:348
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:346
DoCommandPInternal
CommandCost DoCommandPInternal(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const std::string &text, bool my_cmd, bool estimate_only)
Definition: command.cpp:634
Vehicle::UpdateRealOrderIndex
void UpdateRealOrderIndex()
Skip implicit orders until cur_real_order_index is a non-implicit order.
Definition: vehicle_base.h:855
Vehicle::Orders
IterateWrapper Orders() const
Returns an iterable ensemble of orders of a vehicle.
Definition: vehicle_base.h:1038
CommandCost
Common return value for all commands.
Definition: command_type.h:23
DoCommandP
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:541
Vehicle::tile
TileIndex tile
Current tile index.
Definition: vehicle_base.h:242
ODATFB_NEAREST_DEPOT
@ ODATFB_NEAREST_DEPOT
Send the vehicle to the nearest depot.
Definition: order_type.h:105
safeguards.h
OrderBackup::Restore
static void Restore(Vehicle *v, uint32 user)
Restore the data of this order to the given vehicle.
Definition: order_backup.cpp:118
DEFAULT_GROUP
static const GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition: group_type.h:17
CLIENT_ID_SERVER
@ CLIENT_ID_SERVER
Servers always have this ID.
Definition: network_type.h:49
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:57
stdafx.h
Vehicle::IsOrderListShared
bool IsOrderListShared() const
Check if we share our orders with another vehicle.
Definition: vehicle_base.h:696
OrderBackup::Reset
static void Reset(TileIndex tile=INVALID_TILE, bool from_gui=true)
Reset the OrderBackups from GUI/game logic.
Definition: order_backup.cpp:183
OrderBackup::user
uint32 user
The user that requested the backup.
Definition: order_backup.h:40
_network_own_client_id
ClientID _network_own_client_id
Our client identifier.
Definition: network.cpp:63
OrderBackup::DoRestore
void DoRestore(Vehicle *v)
Restore the data of this order to the given vehicle.
Definition: order_backup.cpp:72
Vehicle::list
OrderList * list
Pointer to the order list for this vehicle.
Definition: vehicle_base.h:332
BaseConsist::cur_real_order_index
VehicleOrderID cur_real_order_index
The index to the current real (non-implicit) order.
Definition: base_consist.h:28
Vehicle::FirstShared
Vehicle * FirstShared() const
Get the first vehicle of this vehicle chain.
Definition: vehicle_base.h:690
Pool::PoolItem<&_order_backup_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:386
Pool
Base class for all pools.
Definition: pool_type.hpp:81
GroupID
uint16 GroupID
Type for all group identifiers.
Definition: group_type.h:13
InvalidateWindowClassesData
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:3251
OrderList
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:253
Vehicle::NextShared
Vehicle * NextShared() const
Get the next vehicle of the shared vehicle chain.
Definition: vehicle_base.h:678
OrderBackup::orders
Order * orders
The actual orders if the vehicle was not a clone.
Definition: order_backup.h:45
OrderBackup::tile
TileIndex tile
Tile of the depot where the order was changed.
Definition: order_backup.h:41
Pool::PoolItem<&_orderlist_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:307
OrderBackup::ClearGroup
static void ClearGroup(GroupID group)
Clear the group of all backups having this group ID.
Definition: order_backup.cpp:214
OrderBackup::RemoveOrder
static void RemoveOrder(OrderType type, DestinationID destination, bool hangar)
Removes an order from all vehicles.
Definition: order_backup.cpp:249
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition: pool_func.hpp:224
OrderType
OrderType
Order types.
Definition: order_type.h:35
network.h
station_map.h
window_func.h
OrderBackup::Backup
static void Backup(const Vehicle *v, uint32 user)
Create an order backup for the given vehicle.
Definition: order_backup.cpp:100
OrderBackup::ResetUser
static void ResetUser(uint32 user)
Reset an user's OrderBackup if needed.
Definition: order_backup.cpp:164
INVALID_TILE
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:88
OrderBackup::group
GroupID group
The group the vehicle was part of.
Definition: order_backup.h:42
OrderBackup::OrderBackup
OrderBackup()
Creation for savegame restoration.
Definition: order_backup.h:48
OrderBackup
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:36
pool_func.hpp
order_backup.h
Vehicle::GetNumOrders
VehicleOrderID GetNumOrders() const
Get the number of orders this vehicle has.
Definition: vehicle_base.h:702
BaseConsist::CopyConsistPropertiesFrom
void CopyConsistPropertiesFrom(const BaseConsist *src)
Copy properties of other BaseConsist.
Definition: base_consist.cpp:22
WC_STATION_LIST
@ WC_STATION_LIST
Station list; Window numbers:
Definition: window_type.h:294
Order::next
Order * next
Pointer to next order. If nullptr, end of list.
Definition: order_base.h:52
Order
Definition: order_base.h:33
network_func.h
IsHangarTile
static bool IsHangarTile(TileIndex t)
Is tile t an hangar tile?
Definition: station_map.h:326
CMD_CLONE_ORDER
@ CMD_CLONE_ORDER
clone (and share) an order
Definition: command_type.h:273