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/>.
8 /** @file autoreplace.cpp Management of replacement lists. */
11 #include "command_func.h"
13 #include "autoreplace_base.h"
14 #include "core/bitmath_func.hpp"
15 #include "core/pool_func.hpp"
17 #include "safeguards.h"
19 /** The pool of autoreplace "orders". */
20 EngineRenewPool
_enginerenew_pool("EngineRenew");
21 INSTANTIATE_POOL_METHODS(EngineRenew
)
24 * Retrieves the EngineRenew that specifies the replacement of the given
25 * engine type from the given renewlist
27 static EngineRenew
*GetEngineReplacement(EngineRenewList erl
, EngineID engine
, GroupID group
)
29 EngineRenew
*er
= (EngineRenew
*)erl
;
31 while (er
!= nullptr) {
32 if (er
->from
== engine
&& GroupIsInGroup(group
, er
->group_id
)) return er
;
39 * Remove all engine replacement settings for the company.
40 * @param erl The renewlist for a given company.
41 * @return The new renewlist for the company.
43 void RemoveAllEngineReplacement(EngineRenewList
*erl
)
45 EngineRenew
*er
= (EngineRenew
*)(*erl
);
48 while (er
!= nullptr) {
53 *erl
= nullptr; // Empty list
57 * Retrieve the engine replacement in a given renewlist for an original engine type.
58 * @param erl The renewlist to search in.
59 * @param engine Engine type to be replaced.
60 * @param group The group related to this replacement.
61 * @param[out] replace_when_old Set to true if the replacement should be done when old.
62 * @return The engine type to replace with, or INVALID_ENGINE if no
63 * replacement is in the list.
65 EngineID
EngineReplacement(EngineRenewList erl
, EngineID engine
, GroupID group
, bool *replace_when_old
)
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
);
72 if (replace_when_old
!= nullptr) {
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;
80 /* Use player setting */
81 *replace_when_old
= er
->replace_when_old
;
84 return er
== nullptr ? INVALID_ENGINE
: er
->to
;
88 * Add an engine replacement to the given renewlist.
89 * @param erl The renewlist to add to.
90 * @param old_engine The original engine type.
91 * @param new_engine The replacement engine type.
92 * @param group The group related to this replacement.
93 * @param replace_when_old Replace when old or always?
94 * @param flags The calling command flags.
95 * @return 0 on success, CMD_ERROR on failure.
97 CommandCost
AddEngineReplacement(EngineRenewList
*erl
, EngineID old_engine
, EngineID new_engine
, GroupID group
, bool replace_when_old
, DoCommandFlag flags
)
99 /* Check if the old vehicle is already in the list */
100 EngineRenew
*er
= GetEngineReplacement(*erl
, old_engine
, group
);
102 if (flags
& DC_EXEC
) {
104 er
->replace_when_old
= replace_when_old
;
106 return CommandCost();
109 if (!EngineRenew::CanAllocateItem()) return CMD_ERROR
;
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
;
116 /* Insert before the first element */
117 er
->next
= (EngineRenew
*)(*erl
);
118 *erl
= (EngineRenewList
)er
;
121 return CommandCost();
125 * Remove an engine replacement from a given renewlist.
126 * @param erl The renewlist from which to remove the replacement
127 * @param engine The original engine type.
128 * @param group The group related to this replacement.
129 * @param flags The calling command flags.
130 * @return 0 on success, CMD_ERROR on failure.
132 CommandCost
RemoveEngineReplacement(EngineRenewList
*erl
, EngineID engine
, GroupID group
, DoCommandFlag flags
)
134 EngineRenew
*er
= (EngineRenew
*)(*erl
);
135 EngineRenew
*prev
= nullptr;
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
;
144 /* Cut this element out */
145 prev
->next
= er
->next
;
149 return CommandCost();