OpenTTD Source 20250312-master-gcdcc6b491d
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"
15#include "order_backup.h"
16#include "vehicle_base.h"
17#include "window_func.h"
18#include "station_map.h"
19#include "order_cmd.h"
20#include "group_cmd.h"
21#include "vehicle_func.h"
22
23#include "safeguards.h"
24
25OrderBackupPool _order_backup_pool("BackupOrder");
27
28
30{
31 if (CleaningPool()) return;
32
33 Order *o = this->orders;
34 while (o != nullptr) {
35 Order *next = o->next;
36 delete o;
37 o = next;
38 }
39}
40
46OrderBackup::OrderBackup(const Vehicle *v, uint32_t user) : user(user), tile(v->tile), group(v->group_id)
47{
49
50 /* If we have shared orders, store the vehicle we share the order with. */
51 if (v->IsOrderListShared()) {
52 this->clone = (v->FirstShared() == v) ? v->NextShared() : v->FirstShared();
53 } else {
54 /* Else copy the orders */
55 Order **tail = &this->orders;
56
57 /* Count the number of orders */
58 for (const Order *order : v->Orders()) {
59 Order *copy = new Order();
60 copy->AssignOrder(*order);
61 *tail = copy;
62 tail = &copy->next;
63 }
64 }
65}
66
72{
73 /* If we had shared orders, recover that */
74 if (this->clone != nullptr) {
75 Command<CMD_CLONE_ORDER>::Do(DoCommandFlag::Execute, CO_SHARE, v->index, this->clone->index);
76 } else if (this->orders != nullptr && OrderList::CanAllocateItem()) {
77 v->orders = new OrderList(this->orders, v);
78 this->orders = nullptr;
79 /* Make sure buoys/oil rigs are updated in the station list. */
81 }
82
83 /* Remove backed up name if it's no longer unique. */
84 if (!IsUniqueVehicleName(this->name)) this->name.clear();
85
87
88 /* Make sure orders are in range */
91
92 /* Restore vehicle group */
94}
95
102/* static */ void OrderBackup::Backup(const Vehicle *v, uint32_t user)
103{
104 /* Don't use reset as that broadcasts over the network to reset the variable,
105 * which is what we are doing at the moment. */
106 for (OrderBackup *ob : OrderBackup::Iterate()) {
107 if (ob->user == user) delete ob;
108 }
110 new OrderBackup(v, user);
111 }
112}
113
120/* static */ void OrderBackup::Restore(Vehicle *v, uint32_t user)
121{
122 for (OrderBackup *ob : OrderBackup::Iterate()) {
123 if (v->tile != ob->tile || ob->user != user) continue;
124
125 ob->DoRestore(v);
126 delete ob;
127 }
128}
129
136/* static */ void OrderBackup::ResetOfUser(TileIndex tile, uint32_t user)
137{
138 for (OrderBackup *ob : OrderBackup::Iterate()) {
139 if (ob->user == user && (ob->tile == tile || tile == INVALID_TILE)) delete ob;
140 }
141}
142
151{
152 /* No need to check anything. If the tile or user don't exist we just ignore it. */
153 if (flags.Test(DoCommandFlag::Execute)) OrderBackup::ResetOfUser(tile == 0 ? INVALID_TILE : tile, user_id);
154
155 return CommandCost();
156}
157
164/* static */ void OrderBackup::ResetUser(uint32_t 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
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. */
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 Command<CMD_CLEAR_ORDER_BACKUP>::Unsafe<CommandCallback>(STR_NULL, nullptr, true, false, ob->tile, CommandTraits<CMD_CLEAR_ORDER_BACKUP>::Args{ ob->tile, static_cast<ClientID>(user) });
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}
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Common return value for all commands.
Enum-as-bit-set wrapper.
Functions related to commands.
@ Execute
execute the given command
Command definitions related to engine groups.
static constexpr GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition group_type.h:18
bool _networking
are we in networking mode?
Definition network.cpp:65
bool _network_server
network-server is active
Definition network.cpp:66
ClientID _network_own_client_id
Our client identifier.
Definition network.cpp:70
Basic functions/variables used all over the place.
Network functions used by other parts of OpenTTD.
ClientID
'Unique' identifier to be given to clients
@ CLIENT_ID_SERVER
Servers always have this ID.
CommandCost CmdClearOrderBackup(DoCommandFlags flags, TileIndex tile, ClientID user_id)
Clear an OrderBackup.
Functions related to order backups.
Command definitions related to orders.
@ ODATFB_NEAREST_DEPOT
Send the vehicle to the nearest depot.
Definition order_type.h:121
OrderType
Order types.
Definition order_type.h:51
Some methods of Pool are placed here in order to reduce compilation time and binary size.
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
A number of safeguards to prevent using unsafe methods.
Maps accessors for stations.
bool IsHangarTile(Tile t)
Is tile t an hangar tile?
Definition of base types and functions in a cross-platform compatible way.
std::string name
Name of vehicle.
VehicleOrderID cur_real_order_index
The index to the current real (non-implicit) order.
VehicleOrderID cur_implicit_order_index
The index to the current implicit order.
void CopyConsistPropertiesFrom(const BaseConsist *src)
Copy properties of other BaseConsist.
Defines the traits of a command.
Data for backing up an order of a vehicle so it can be restored after a vehicle is rebuilt in the sam...
static void Reset(TileIndex tile=INVALID_TILE, bool from_gui=true)
Reset the OrderBackups from GUI/game logic.
static void Restore(Vehicle *v, uint32_t user)
Restore the data of this order to the given vehicle.
void DoRestore(Vehicle *v)
Restore the data of this order to the given vehicle.
Order * orders
The actual orders if the vehicle was not a clone.
static void Backup(const Vehicle *v, uint32_t user)
Create an order backup for the given vehicle.
static void ClearVehicle(const Vehicle *v)
Clear/update the (clone) vehicle from an order backup.
const Vehicle * clone
Vehicle this vehicle was a clone of.
TileIndex tile
Tile of the depot where the order was changed.
uint32_t user
The user that requested the backup.
static void ClearGroup(GroupID group)
Clear the group of all backups having this group ID.
GroupID group
The group the vehicle was part of.
OrderBackup()
Creation for savegame restoration.
static void ResetOfUser(TileIndex tile, uint32_t user)
Reset an OrderBackup given a tile and user.
static void ResetUser(uint32_t user)
Reset an user's OrderBackup if needed.
static void RemoveOrder(OrderType type, DestinationID destination, bool hangar)
Removes an order from all vehicles.
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition order_base.h:258
Order * next
Pointer to next order. If nullptr, end of list.
Definition order_base.h:59
void AssignOrder(const Order &other)
Assign data to an order (from another order) This function makes sure that the index is maintained co...
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Tindex index
Index of this pool item.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Base class for all pools.
The information about a vehicle list.
Definition vehiclelist.h:32
Vehicle data structure.
bool IsOrderListShared() const
Check if we share our orders with another vehicle.
VehicleOrderID GetNumOrders() const
Get the number of orders this vehicle has.
OrderList * orders
Pointer to the order list for this vehicle.
Vehicle * NextShared() const
Get the next vehicle of the shared vehicle chain.
IterateWrapper Orders() const
Returns an iterable ensemble of orders of a vehicle.
Vehicle * FirstShared() const
Get the first vehicle of this vehicle chain.
TileIndex tile
Current tile index.
void UpdateRealOrderIndex()
Skip implicit orders until cur_real_order_index is a non-implicit order.
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
Base class for all vehicles.
bool IsUniqueVehicleName(const std::string &name)
Test if a name is unique among vehicle names.
Functions related to vehicles.
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:3241
Window functions not directly related to making/drawing windows.
@ WC_STATION_LIST
Station list; Window numbers: