OpenTTD Source  20240917-master-g9ab0a47812
autoreplace.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 "group.h"
13 #include "autoreplace_base.h"
14 #include "core/bitmath_func.hpp"
15 #include "core/pool_func.hpp"
16 
17 #include "safeguards.h"
18 
20 EngineRenewPool _enginerenew_pool("EngineRenew");
22 
23 
28 {
29  EngineRenew *er = (EngineRenew *)erl;
30 
31  while (er != nullptr) {
32  if (er->from == engine && GroupIsInGroup(group, er->group_id)) return er;
33  er = er->next;
34  }
35  return nullptr;
36 }
37 
44 {
45  EngineRenew *er = (EngineRenew *)(*erl);
46  EngineRenew *next;
47 
48  while (er != nullptr) {
49  next = er->next;
50  delete er;
51  er = next;
52  }
53  *erl = nullptr; // Empty list
54 }
55 
65 EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group, bool *replace_when_old)
66 {
67  const EngineRenew *er = GetEngineReplacement(erl, engine, group);
68  if (er == nullptr && (group == DEFAULT_GROUP || (Group::IsValidID(group) && !HasBit(Group::Get(group)->flags, GroupFlags::GF_REPLACE_PROTECTION)))) {
69  /* We didn't find anything useful in the vehicle's own group so we will try ALL_GROUP */
70  er = GetEngineReplacement(erl, engine, ALL_GROUP);
71  }
72  if (replace_when_old != nullptr) {
73  if (er == nullptr) {
74  /* Not replacing */
75  *replace_when_old = false;
76  } else if (er->to == engine) {
77  /* When replacing with same model, only ever do it when old */
78  *replace_when_old = true;
79  } else {
80  /* Use player setting */
81  *replace_when_old = er->replace_when_old;
82  }
83  }
84  return er == nullptr ? INVALID_ENGINE : er->to;
85 }
86 
97 CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, bool replace_when_old, DoCommandFlag flags)
98 {
99  /* Check if the old vehicle is already in the list */
100  EngineRenew *er = GetEngineReplacement(*erl, old_engine, group);
101  if (er != nullptr) {
102  if (flags & DC_EXEC) {
103  er->to = new_engine;
104  er->replace_when_old = replace_when_old;
105  }
106  return CommandCost();
107  }
108 
110 
111  if (flags & DC_EXEC) {
112  er = new EngineRenew(old_engine, new_engine);
113  er->group_id = group;
114  er->replace_when_old = replace_when_old;
115 
116  /* Insert before the first element */
117  er->next = (EngineRenew *)(*erl);
118  *erl = (EngineRenewList)er;
119  }
120 
121  return CommandCost();
122 }
123 
133 {
134  EngineRenew *er = (EngineRenew *)(*erl);
135  EngineRenew *prev = nullptr;
136 
137  while (er != nullptr) {
138  if (er->from == engine && er->group_id == group) {
139  if (flags & DC_EXEC) {
140  if (prev == nullptr) { // First element
141  /* The second becomes the new first element */
142  *erl = (EngineRenewList)er->next;
143  } else {
144  /* Cut this element out */
145  prev->next = er->next;
146  }
147  delete er;
148  }
149  return CommandCost();
150  }
151  prev = er;
152  er = er->next;
153  }
154 
155  return CMD_ERROR;
156 }
INVALID_ENGINE
static const EngineID INVALID_ENGINE
Constant denoting an invalid engine.
Definition: engine_type.h:206
EngineRenew::replace_when_old
bool replace_when_old
Do replacement only when vehicle is old.
Definition: autoreplace_base.h:38
autoreplace_base.h
Pool::PoolItem<&_group_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:339
command_func.h
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:28
_enginerenew_pool
EngineRenewPool _enginerenew_pool("EngineRenew")
The pool of autoreplace "orders".
RemoveEngineReplacement
CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, DoCommandFlag flags)
Remove an engine replacement from a given renewlist.
Definition: autoreplace.cpp:132
EngineRenewList
EngineRenew * EngineRenewList
A list to group EngineRenew directives together (such as per-company).
Definition: autoreplace_type.h:13
group.h
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:376
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:374
ALL_GROUP
static const GroupID ALL_GROUP
All vehicles are in this group.
Definition: group_type.h:16
bitmath_func.hpp
CommandCost
Common return value for all commands.
Definition: command_type.h:23
EngineReplacement
EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group, bool *replace_when_old)
Retrieve the engine replacement in a given renewlist for an original engine type.
Definition: autoreplace.cpp:65
GetEngineReplacement
static EngineRenew * GetEngineReplacement(EngineRenewList erl, EngineID engine, GroupID group)
Retrieves the EngineRenew that specifies the replacement of the given engine type from the given rene...
Definition: autoreplace.cpp:27
RemoveAllEngineReplacement
void RemoveAllEngineReplacement(EngineRenewList *erl)
Remove all engine replacement settings for the company.
Definition: autoreplace.cpp:43
safeguards.h
DEFAULT_GROUP
static const GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition: group_type.h:17
EngineRenew
Struct to store engine replacements.
Definition: autoreplace_base.h:33
stdafx.h
Pool
Base class for all pools.
Definition: pool_type.hpp:80
AddEngineReplacement
CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, bool replace_when_old, DoCommandFlag flags)
Add an engine replacement to the given renewlist.
Definition: autoreplace.cpp:97
Pool::PoolItem<&_enginerenew_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:309
GroupID
uint16_t GroupID
Type for all group identifiers.
Definition: group_type.h:13
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition: pool_func.hpp:237
GF_REPLACE_PROTECTION
@ GF_REPLACE_PROTECTION
If set to true, the global autoreplace has no effect on the group.
Definition: group.h:66
EngineID
uint16_t EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
Pool::PoolItem<&_group_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:328
pool_func.hpp
GroupIsInGroup
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Definition: group_cmd.cpp:874
HasBit
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103