OpenTTD Source 20261129-master-g72a8bdc861
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
12#include "autoreplace_func.h"
13#include "autoreplace_type.h"
14#include "command_func.h"
15#include "company_base.h"
16#include "group.h"
17
18#include "safeguards.h"
19
27{
28 /* Linear scan until it reaches or passes engine, instead of binary search (lower_bound) */
29 auto it = std::ranges::find_if(erl, [engine](const auto &er) { return er.from >= engine; });
30 if (it == erl.end() || it->from != engine) return nullptr;
31 return &*it;
32}
33
42const EngineRenew *GetEngineRenew(CompanyID company, GroupID group, EngineID engine, bool search)
43{
44 for (const Group *g = Group::GetIfValid(group); g != nullptr; g = Group::GetIfValid(g->parent)) {
45 if (g->flags.Test(GroupFlag::ReplaceProtection)) return nullptr;
46 const EngineRenew *er = GetEngineRenew(g->engine_renew_list, engine);
47 if (er != nullptr || !search) return er;
48 }
49
50 const Company *c = Company::Get(company);
51 if (group == DEFAULT_GROUP) {
53 if (er != nullptr || !search) return er;
54 }
55
56 return GetEngineRenew(c->all_engine_renew_list, engine);
57}
58
64{
65 Company *c = Company::GetIfValid(company);
66 if (c == nullptr) return;
67
68 c->all_engine_renew_list.clear();
70
71 for (Group *g : Group::Iterate()) {
72 if (g->owner != company) continue;
73 g->engine_renew_list.clear();
74 }
75}
76
83EngineRenewList &GetEngineRenewList(CompanyID company, GroupID group)
84{
85 if (group == ALL_GROUP) return Company::Get(company)->all_engine_renew_list;
86 if (group == DEFAULT_GROUP) return Company::Get(company)->ungrouped_engine_renew_list;
87
88 Group *g = Group::GetIfValid(group);
89 assert(g != nullptr);
90 return g->engine_renew_list;
91}
92
103CommandCost AddEngineReplacement(DoCommandFlags flags, CompanyID company, GroupID group, EngineID engine, EngineID new_engine, bool replace_when_old)
104{
105 if (flags.Test(DoCommandFlag::Execute)) {
106 /* Like-for-like replacements are only replaced when old. */
107 if (engine == new_engine) replace_when_old = true;
108
109 /* Check if the old vehicle is already in the list. */
110 EngineRenewList &erl = GetEngineRenewList(company, group);
111 auto it = std::ranges::lower_bound(erl, engine, std::less{}, &EngineRenew::from);
112 if (it == erl.end() || it->from != engine) {
113 /* Not present, create new entry. */
114 erl.emplace(it, engine, new_engine, replace_when_old);
115 } else {
116 /* Update existing entry. */
117 it->to = new_engine;
118 it->replace_when_old = replace_when_old;
119 }
120 }
121
122 return CommandCost();
123}
124
133CommandCost RemoveEngineReplacement(DoCommandFlags flags, CompanyID company, GroupID group, EngineID engine)
134{
135 EngineRenewList &erl = GetEngineRenewList(company, group);
136 auto it = std::ranges::lower_bound(erl, engine, std::less{}, &EngineRenew::from);
137 if (it == erl.end() || it->from != engine) return CMD_ERROR;
138
139 if (flags.Test(DoCommandFlag::Execute)) {
140 erl.erase(it);
141 }
142
143 return CommandCost();
144}
CommandCost AddEngineReplacement(DoCommandFlags flags, CompanyID company, GroupID group, EngineID engine, EngineID new_engine, bool replace_when_old)
Add an engine replacement to the given renewlist.
void RemoveAllEngineReplacementForCompany(CompanyID company)
Remove all engine replacements for a company.
EngineRenewList & GetEngineRenewList(CompanyID company, GroupID group)
Get the Engine Renew List for a company/group.
const EngineRenew * GetEngineRenew(const EngineRenewList &erl, EngineID engine)
Search within a single engine renew list for an engine to renew.
CommandCost RemoveEngineReplacement(DoCommandFlags flags, CompanyID company, GroupID group, EngineID engine)
Remove an engine replacement from a given renewlist.
Functions related to autoreplacing.
Types related to autoreplacing.
std::vector< 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
EnumBitSet< DoCommandFlag, uint16_t > DoCommandFlags
Bitset of DoCommandFlag elements.
Definition of stuff that is very close to a company, like the company struct itself.
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF > EngineID
Unique identification number of an engine.
Definition engine_type.h:25
Base class for groups and group functions.
@ ReplaceProtection
If set, the global autoreplace has no effect on the group.
Definition group.h:69
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
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
EngineRenewList all_engine_renew_list
Engine renewals of this company for all vehicles.
EngineRenewList ungrouped_engine_renew_list
Engine renewals of this company for ungrouped vehicles.
Information about engine autorenews and autoreplacements.
EngineID from
Engine to replace from.
Group data.
Definition group.h:77
EngineRenewList engine_renew_list
Engine renewals of this group.
Definition group.h:84
static Pool::IterateWrapper< Group > Iterate(size_t from=0)
static Company * Get(auto index)
static Group * GetIfValid(auto index)