OpenTTD Source: src/autoreplace.cpp Source File

OpenTTD Source 15.3

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
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
19 EngineRenewPool _enginerenew_pool("EngineRenew");
20 INSTANTIATE_POOL_METHODS(EngineRenew)
21
22
26static EngineRenew *GetEngineReplacement(EngineRenewList erl, EngineID engine, GroupID group)
27{
28 EngineRenew *er = (EngineRenew *)erl;
29
30 while (er != nullptr) {
31 if (er->from == engine && GroupIsInGroup(group, er->group_id)) return er;
32 er = er->next;
33 }
34 return nullptr;
35}
36
42 void RemoveAllEngineReplacement(EngineRenewList *erl)
43{
44 EngineRenew *er = (EngineRenew *)(*erl);
45 EngineRenew *next;
46
47 while (er != nullptr) {
48 next = er->next;
49 delete er;
50 er = next;
51 }
52 *erl = nullptr; // Empty list
53}
54
64 EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group, bool *replace_when_old)
65{
66 const EngineRenew *er = GetEngineReplacement(erl, engine, group);
67 if (er == nullptr && (group == DEFAULT_GROUP || (Group::IsValidID(group) && !Group::Get(group)->flags.Test(GroupFlag::ReplaceProtection)))) {
68 /* We didn't find anything useful in the vehicle's own group so we will try ALL_GROUP */
69 er = GetEngineReplacement(erl, engine, ALL_GROUP);
70 }
71 if (replace_when_old != nullptr) {
72 if (er == nullptr) {
73 /* Not replacing */
74 *replace_when_old = false;
75 } else if (er->to == engine) {
76 /* When replacing with same model, only ever do it when old */
77 *replace_when_old = true;
78 } else {
79 /* Use player setting */
80 *replace_when_old = er->replace_when_old;
81 }
82 }
83 return er == nullptr ? EngineID::Invalid() : er->to;
84}
85
96 CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, bool replace_when_old, DoCommandFlags flags)
97{
98 /* Check if the old vehicle is already in the list */
99 EngineRenew *er = GetEngineReplacement(*erl, old_engine, group);
100 if (er != nullptr) {
101 if (flags.Test(DoCommandFlag::Execute)) {
102 er->to = new_engine;
103 er->replace_when_old = replace_when_old;
104 }
105 return CommandCost();
106 }
107
108 if (!EngineRenew::CanAllocateItem()) return CMD_ERROR;
109
110 if (flags.Test(DoCommandFlag::Execute)) {
111 /* Insert before the first element */
112 *erl = new EngineRenew(old_engine, new_engine, group, replace_when_old, *erl);
113 }
114
115 return CommandCost();
116}
117
126 CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, DoCommandFlags flags)
127{
128 EngineRenew *er = (EngineRenew *)(*erl);
129 EngineRenew *prev = nullptr;
130
131 while (er != nullptr) {
132 if (er->from == engine && er->group_id == group) {
133 if (flags.Test(DoCommandFlag::Execute)) {
134 if (prev == nullptr) { // First element
135 /* The second becomes the new first element */
136 *erl = (EngineRenewList)er->next;
137 } else {
138 /* Cut this element out */
139 prev->next = er->next;
140 }
141 delete er;
142 }
143 return CommandCost();
144 }
145 prev = er;
146 er = er->next;
147 }
148
149 return CMD_ERROR;
150}
RemoveEngineReplacement
CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, DoCommandFlags flags)
Remove an engine replacement from a given renewlist.
Definition autoreplace.cpp:126
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:26
_enginerenew_pool
EngineRenewPool _enginerenew_pool("EngineRenew")
The pool of autoreplace "orders".
RemoveAllEngineReplacement
void RemoveAllEngineReplacement(EngineRenewList *erl)
Remove all engine replacement settings for the company.
Definition autoreplace.cpp:42
AddEngineReplacement
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.
Definition autoreplace.cpp:96
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:64
autoreplace_base.h
Base class for autoreplaces/autorenews.
EngineRenewList
EngineRenew * EngineRenewList
A list to group EngineRenew directives together (such as per-company).
Definition autoreplace_type.h:18
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
CommandCost
Common return value for all commands.
Definition command_type.h:24
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
command_func.h
Functions related to commands.
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition command_func.h:28
DoCommandFlag::Execute
@ Execute
execute the given command
group.h
Base class for groups and group functions.
GroupFlag::ReplaceProtection
@ ReplaceProtection
If set, the global autoreplace has no effect on the group.
GroupIsInGroup
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Definition group_cmd.cpp:909
DEFAULT_GROUP
static constexpr GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition group_type.h:18
ALL_GROUP
static constexpr GroupID ALL_GROUP
All vehicles are in this group.
Definition group_type.h:17
pool_func.hpp
Some methods of Pool are placed here in order to reduce compilation time and binary size.
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition pool_func.hpp:210
safeguards.h
A number of safeguards to prevent using unsafe methods.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
EngineRenew
Struct to store engine replacements.
Definition autoreplace_base.h:33
EngineRenew::replace_when_old
bool replace_when_old
Do replacement only when vehicle is old.
Definition autoreplace_base.h:38
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF >
Pool::PoolItem<&_group_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
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:357
Pool::PoolItem<&_group_pool >::IsValidID
static bool IsValidID(auto index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition pool_type.hpp:376
Pool
Base class for all pools.
Definition pool_type.hpp:133