OpenTTD: src/autoreplace.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

autoreplace.cpp
Go to the documentation of this file.
1 /* $Id: autoreplace.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #include "stdafx.h"
13 #include "command_func.h"
14 #include "group.h"
15 #include "autoreplace_base.h"
16 #include "core/pool_func.hpp"
17
18 #include "safeguards.h"
19
21 EngineRenewPool _enginerenew_pool("EngineRenew");
22 INSTANTIATE_POOL_METHODS(EngineRenew)
23
24
28 static EngineRenew *GetEngineReplacement(EngineRenewList erl, EngineID engine, GroupID group)
29 {
30 EngineRenew *er = (EngineRenew *)erl;
31
32 while (er != NULL) {
33 if (er->from == engine && GroupIsInGroup(group, er->group_id)) return er;
34 er = er->next;
35 }
36 return NULL;
37 }
38
44 void RemoveAllEngineReplacement(EngineRenewList *erl)
45 {
46 EngineRenew *er = (EngineRenew *)(*erl);
47 EngineRenew *next;
48
49 while (er != NULL) {
50 next = er->next;
51 delete er;
52 er = next;
53 }
54 *erl = NULL; // Empty list
55 }
56
66 EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group, bool *replace_when_old)
67 {
68 const EngineRenew *er = GetEngineReplacement(erl, engine, group);
69 if (er == NULL && (group == DEFAULT_GROUP || (Group::IsValidID(group) && !Group::Get(group)->replace_protection))) {
70 /* We didn't find anything useful in the vehicle's own group so we will try ALL_GROUP */
71 er = GetEngineReplacement(erl, engine, ALL_GROUP);
72 }
73 if (replace_when_old != NULL) *replace_when_old = er == NULL ? false : er->replace_when_old;
74 return er == NULL ? INVALID_ENGINE : er->to;
75 }
76
87 CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, bool replace_when_old, DoCommandFlag flags)
88 {
89 /* Check if the old vehicle is already in the list */
90 EngineRenew *er = GetEngineReplacement(*erl, old_engine, group);
91 if (er != NULL) {
92 if (flags & DC_EXEC) {
93 er->to = new_engine;
94 er->replace_when_old = replace_when_old;
95 }
96 return CommandCost();
97 }
98
99 if (!EngineRenew::CanAllocateItem()) return CMD_ERROR;
100
101 if (flags & DC_EXEC) {
102 er = new EngineRenew(old_engine, new_engine);
103 er->group_id = group;
104 er->replace_when_old = replace_when_old;
105
106 /* Insert before the first element */
107 er->next = (EngineRenew *)(*erl);
108 *erl = (EngineRenewList)er;
109 }
110
111 return CommandCost();
112 }
113
122 CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, DoCommandFlag flags)
123 {
124 EngineRenew *er = (EngineRenew *)(*erl);
125 EngineRenew *prev = NULL;
126
127 while (er != NULL) {
128 if (er->from == engine && er->group_id == group) {
129 if (flags & DC_EXEC) {
130 if (prev == NULL) { // First element
131 /* The second becomes the new first element */
132 *erl = (EngineRenewList)er->next;
133 } else {
134 /* Cut this element out */
135 prev->next = er->next;
136 }
137 delete er;
138 }
139 return CommandCost();
140 }
141 prev = er;
142 er = er->next;
143 }
144
145 return CMD_ERROR;
146 }