OpenTTD
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");
23 
24 
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 
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 
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 
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 }
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, DoCommandFlag flags)
Remove an engine replacement from a given renewlist.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:25
Base class for groups and group functions.
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Definition: group_cmd.cpp:720
bool replace_when_old
Do replacement only when vehicle is old.
EngineRenewPool _enginerenew_pool("EngineRenew")
The pool of autoreplace "orders".
Common return value for all commands.
Definition: command_type.h:25
Some methods of Pool are placed here in order to reduce compilation time and binary size...
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:28
DoCommandFlag
List of flags for a command.
Definition: command_type.h:340
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
uint16 GroupID
Type for all group identifiers.
Definition: group_type.h:15
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:87
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:66
EngineRenew * EngineRenewList
A list to group EngineRenew directives together (such as per-company).
Base class for all pools.
Definition: pool_type.hpp:83
void RemoveAllEngineReplacement(EngineRenewList *erl)
Remove all engine replacement settings for the company.
Definition: autoreplace.cpp:44
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don&#39;t get linker errors.
Definition: pool_func.hpp:224
execute the given command
Definition: command_type.h:342
static const EngineID INVALID_ENGINE
Constant denoting an invalid engine.
Definition: engine_type.h:174
static const GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition: group_type.h:19
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:22
Struct to store engine replacements.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:216
Functions related to commands.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-NULL) Titem.
Definition: pool_type.hpp:235
Base class for autoreplaces/autorenews.
static const GroupID ALL_GROUP
All vehicles are in this group.
Definition: group_type.h:18