OpenTTD Source 20260218-master-g2123fca5ea
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "command_func.h"
12#include "group.h"
13#include "autoreplace_base.h"
14#include "core/pool_func.hpp"
15
16#include "safeguards.h"
17
21
22
30static EngineRenew *GetEngineReplacement(EngineRenewList erl, EngineID engine, GroupID group)
31{
32 EngineRenew *er = (EngineRenew *)erl;
33
34 while (er != nullptr) {
35 if (er->from == engine && GroupIsInGroup(group, er->group_id)) return er;
36 er = er->next;
37 }
38 return nullptr;
39}
40
46{
47 EngineRenew *er = (EngineRenew *)(*erl);
48 EngineRenew *next;
49
50 while (er != nullptr) {
51 next = er->next;
52 delete er;
53 er = next;
54 }
55 *erl = nullptr; // Empty list
56}
57
67EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group, bool *replace_when_old)
68{
69 const EngineRenew *er = GetEngineReplacement(erl, engine, group);
70 if (er == nullptr && (group == DEFAULT_GROUP || (Group::IsValidID(group) && !Group::Get(group)->flags.Test(GroupFlag::ReplaceProtection)))) {
71 /* We didn't find anything useful in the vehicle's own group so we will try ALL_GROUP */
72 er = GetEngineReplacement(erl, engine, ALL_GROUP);
73 }
74 if (replace_when_old != nullptr) {
75 if (er == nullptr) {
76 /* Not replacing */
77 *replace_when_old = false;
78 } else if (er->to == engine) {
79 /* When replacing with same model, only ever do it when old */
80 *replace_when_old = true;
81 } else {
82 /* Use player setting */
83 *replace_when_old = er->replace_when_old;
84 }
85 }
86 return er == nullptr ? EngineID::Invalid() : er->to;
87}
88
99CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, bool replace_when_old, DoCommandFlags flags)
100{
101 /* Check if the old vehicle is already in the list */
102 EngineRenew *er = GetEngineReplacement(*erl, old_engine, group);
103 if (er != nullptr) {
104 if (flags.Test(DoCommandFlag::Execute)) {
105 er->to = new_engine;
106 er->replace_when_old = replace_when_old;
107 }
108 return CommandCost();
109 }
110
112
113 if (flags.Test(DoCommandFlag::Execute)) {
114 /* Insert before the first element */
115 *erl = EngineRenew::Create(old_engine, new_engine, group, replace_when_old, *erl);
116 }
117
118 return CommandCost();
119}
120
129CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, DoCommandFlags flags)
130{
131 EngineRenew *er = (EngineRenew *)(*erl);
132 EngineRenew *prev = nullptr;
133
134 while (er != nullptr) {
135 if (er->from == engine && er->group_id == group) {
136 if (flags.Test(DoCommandFlag::Execute)) {
137 if (prev == nullptr) { // First element
138 /* The second becomes the new first element */
139 *erl = (EngineRenewList)er->next;
140 } else {
141 /* Cut this element out */
142 prev->next = er->next;
143 }
144 delete er;
145 }
146 return CommandCost();
147 }
148 prev = er;
149 er = er->next;
150 }
151
152 return CMD_ERROR;
153}
CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, DoCommandFlags flags)
Remove an engine replacement from a given renewlist.
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...
EngineRenewPool _enginerenew_pool("EngineRenew")
The pool of autoreplace "orders".
void RemoveAllEngineReplacement(EngineRenewList *erl)
Remove all engine replacement settings for the company.
CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, bool replace_when_old, DoCommandFlags flags)
Add an engine replacement to the given renewlist.
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.
Base class for autoreplaces/autorenews.
Pool< EngineRenew, EngineRenewID, 16 > EngineRenewPool
Memory pool for engine renew elements.
EngineRenew * EngineRenewList
A list to group EngineRenew directives together (such as per-company).
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Common return value for all commands.
Functions related to commands.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
@ Execute
execute the given command
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF > EngineID
Unique identification number of an engine.
Definition engine_type.h:26
Base class for groups and group functions.
@ ReplaceProtection
If set, the global autoreplace has no effect on the group.
Definition group.h:68
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
static constexpr GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition group_type.h:18
static constexpr GroupID ALL_GROUP
All vehicles are in this group.
Definition group_type.h:17
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.
Definition of base types and functions in a cross-platform compatible way.
Struct to store engine replacements.
bool replace_when_old
Do replacement only when vehicle is old.
static Group * Get(auto index)
static bool IsValidID(auto index)