OpenTTD Source: src/autoreplace.cpp Source File

OpenTTD Source 1.11.0-RC1

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/pool_func.hpp"
15
16 #include "safeguards.h"
17
19 EngineRenewPool _enginerenew_pool("EngineRenew");
20 INSTANTIATE_POOL_METHODS(EngineRenew)
21
22
26 static 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)->replace_protection))) {
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) *replace_when_old = er == nullptr ? false : er->replace_when_old;
72 return er == nullptr ? INVALID_ENGINE : er->to;
73 }
74
85 CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, bool replace_when_old, DoCommandFlag flags)
86 {
87 /* Check if the old vehicle is already in the list */
88 EngineRenew *er = GetEngineReplacement(*erl, old_engine, group);
89 if (er != nullptr) {
90 if (flags & DC_EXEC) {
91 er->to = new_engine;
92 er->replace_when_old = replace_when_old;
93 }
94 return CommandCost();
95 }
96
97 if (!EngineRenew::CanAllocateItem()) return CMD_ERROR;
98
99 if (flags & DC_EXEC) {
100 er = new EngineRenew(old_engine, new_engine);
101 er->group_id = group;
102 er->replace_when_old = replace_when_old;
103
104 /* Insert before the first element */
105 er->next = (EngineRenew *)(*erl);
106 *erl = (EngineRenewList)er;
107 }
108
109 return CommandCost();
110 }
111
120 CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, DoCommandFlag flags)
121 {
122 EngineRenew *er = (EngineRenew *)(*erl);
123 EngineRenew *prev = nullptr;
124
125 while (er != nullptr) {
126 if (er->from == engine && er->group_id == group) {
127 if (flags & DC_EXEC) {
128 if (prev == nullptr) { // First element
129 /* The second becomes the new first element */
130 *erl = (EngineRenewList)er->next;
131 } else {
132 /* Cut this element out */
133 prev->next = er->next;
134 }
135 delete er;
136 }
137 return CommandCost();
138 }
139 prev = er;
140 er = er->next;
141 }
142
143 return CMD_ERROR;
144 }
INVALID_ENGINE
static const EngineID INVALID_ENGINE
Constant denoting an invalid engine.
Definition: engine_type.h:174
EngineRenew::replace_when_old
bool replace_when_old
Do replacement only when vehicle is old.
Definition: autoreplace_base.h:38
autoreplace_base.h
Pool::PoolItem<&_group_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:329
command_func.h
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:23
_enginerenew_pool
EngineRenewPool _enginerenew_pool("EngineRenew")
The pool of autoreplace "orders".
RemoveEngineReplacement
CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, DoCommandFlag flags)
Remove an engine replacement from a given renewlist.
Definition: autoreplace.cpp:120
EngineRenewList
EngineRenew * EngineRenewList
A list to group EngineRenew directives together (such as per-company).
Definition: autoreplace_type.h:13
group.h
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:348
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:346
ALL_GROUP
static const GroupID ALL_GROUP
All vehicles are in this group.
Definition: group_type.h:16
EngineID
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
CommandCost
Common return value for all commands.
Definition: command_type.h:23
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
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
RemoveAllEngineReplacement
void RemoveAllEngineReplacement(EngineRenewList *erl)
Remove all engine replacement settings for the company.
Definition: autoreplace.cpp:42
safeguards.h
DEFAULT_GROUP
static const GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition: group_type.h:17
EngineRenew
Struct to store engine replacements.
Definition: autoreplace_base.h:33
stdafx.h
Pool
Base class for all pools.
Definition: pool_type.hpp:81
GroupID
uint16 GroupID
Type for all group identifiers.
Definition: group_type.h:13
AddEngineReplacement
CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, bool replace_when_old, DoCommandFlag flags)
Add an engine replacement to the given renewlist.
Definition: autoreplace.cpp:85
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:299
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition: pool_func.hpp:224
Pool::PoolItem<&_group_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:318
pool_func.hpp
GroupIsInGroup
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Definition: group_cmd.cpp:859