OpenTTD Source 20250312-master-gcdcc6b491d
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
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
65EngineID 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) && !Group::Get(group)->flags.Test(GroupFlag::ReplaceProtection)))) {
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 ? EngineID::Invalid() : er->to;
85}
86
97CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, bool replace_when_old, DoCommandFlags 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.Test(DoCommandFlag::Execute)) {
103 er->to = new_engine;
104 er->replace_when_old = replace_when_old;
105 }
106 return CommandCost();
107 }
108
110
111 if (flags.Test(DoCommandFlag::Execute)) {
112 /* Insert before the first element */
113 *erl = new EngineRenew(old_engine, new_engine, group, replace_when_old, *erl);
114 }
115
116 return CommandCost();
117}
118
128{
129 EngineRenew *er = (EngineRenew *)(*erl);
130 EngineRenew *prev = nullptr;
131
132 while (er != nullptr) {
133 if (er->from == engine && er->group_id == group) {
134 if (flags.Test(DoCommandFlag::Execute)) {
135 if (prev == nullptr) { // First element
136 /* The second becomes the new first element */
137 *erl = (EngineRenewList)er->next;
138 } else {
139 /* Cut this element out */
140 prev->next = er->next;
141 }
142 delete er;
143 }
144 return CommandCost();
145 }
146 prev = er;
147 er = er->next;
148 }
149
150 return CMD_ERROR;
151}
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.
EngineRenew * EngineRenewList
A list to group EngineRenew directives together (such as per-company).
Functions related to bit mathematics.
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.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
@ Execute
execute the given command
Base class for groups and group functions.
@ ReplaceProtection
If set, the global autoreplace has no effect on the group.
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 Titem * Get(auto index)
Returns Titem with given index.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
static bool IsValidID(auto index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Base class for all pools.