2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-09-13 15:49:29 +00:00
|
|
|
/** @file autoreplace.cpp Management of replacement lists. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "command_func.h"
|
|
|
|
#include "group.h"
|
|
|
|
#include "autoreplace_base.h"
|
2019-03-29 23:24:40 +00:00
|
|
|
#include "core/bitmath_func.hpp"
|
2009-05-22 15:13:50 +00:00
|
|
|
#include "core/pool_func.hpp"
|
2008-09-13 15:49:29 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2011-05-02 20:59:54 +00:00
|
|
|
/** The pool of autoreplace "orders". */
|
2009-05-22 15:13:50 +00:00
|
|
|
EngineRenewPool _enginerenew_pool("EngineRenew");
|
|
|
|
INSTANTIATE_POOL_METHODS(EngineRenew)
|
2008-09-13 15:49:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the EngineRenew that specifies the replacement of the given
|
2010-08-01 19:44:49 +00:00
|
|
|
* engine type from the given renewlist
|
|
|
|
*/
|
2008-09-13 15:49:29 +00:00
|
|
|
static EngineRenew *GetEngineReplacement(EngineRenewList erl, EngineID engine, GroupID group)
|
|
|
|
{
|
|
|
|
EngineRenew *er = (EngineRenew *)erl;
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
while (er != nullptr) {
|
2014-04-08 21:09:06 +00:00
|
|
|
if (er->from == engine && GroupIsInGroup(group, er->group_id)) return er;
|
2008-09-13 15:49:29 +00:00
|
|
|
er = er->next;
|
|
|
|
}
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2008-09-13 15:49:29 +00:00
|
|
|
}
|
|
|
|
|
2011-01-18 22:17:15 +00:00
|
|
|
/**
|
|
|
|
* Remove all engine replacement settings for the company.
|
|
|
|
* @param erl The renewlist for a given company.
|
|
|
|
* @return The new renewlist for the company.
|
|
|
|
*/
|
2008-09-13 15:49:29 +00:00
|
|
|
void RemoveAllEngineReplacement(EngineRenewList *erl)
|
|
|
|
{
|
|
|
|
EngineRenew *er = (EngineRenew *)(*erl);
|
|
|
|
EngineRenew *next;
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
while (er != nullptr) {
|
2008-09-13 15:49:29 +00:00
|
|
|
next = er->next;
|
|
|
|
delete er;
|
|
|
|
er = next;
|
|
|
|
}
|
2019-04-10 21:07:06 +00:00
|
|
|
*erl = nullptr; // Empty list
|
2008-09-13 15:49:29 +00:00
|
|
|
}
|
|
|
|
|
2011-01-18 22:17:15 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the engine replacement in a given renewlist for an original engine type.
|
|
|
|
* @param erl The renewlist to search in.
|
|
|
|
* @param engine Engine type to be replaced.
|
|
|
|
* @param group The group related to this replacement.
|
2012-04-17 19:44:02 +00:00
|
|
|
* @param[out] replace_when_old Set to true if the replacement should be done when old.
|
2011-01-18 22:17:15 +00:00
|
|
|
* @return The engine type to replace with, or INVALID_ENGINE if no
|
|
|
|
* replacement is in the list.
|
|
|
|
*/
|
2012-04-17 19:44:02 +00:00
|
|
|
EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group, bool *replace_when_old)
|
2008-09-13 15:49:29 +00:00
|
|
|
{
|
|
|
|
const EngineRenew *er = GetEngineReplacement(erl, engine, group);
|
2019-03-29 23:24:40 +00:00
|
|
|
if (er == nullptr && (group == DEFAULT_GROUP || (Group::IsValidID(group) && !HasBit(Group::Get(group)->flags, GroupFlags::GF_REPLACE_PROTECTION)))) {
|
2008-09-13 15:49:29 +00:00
|
|
|
/* We didn't find anything useful in the vehicle's own group so we will try ALL_GROUP */
|
|
|
|
er = GetEngineReplacement(erl, engine, ALL_GROUP);
|
|
|
|
}
|
2019-09-08 08:54:46 +00:00
|
|
|
if (replace_when_old != nullptr) {
|
|
|
|
if (er == nullptr) {
|
|
|
|
/* Not replacing */
|
|
|
|
*replace_when_old = false;
|
|
|
|
} else if (er->to == engine) {
|
|
|
|
/* When replacing with same model, only ever do it when old */
|
|
|
|
*replace_when_old = true;
|
|
|
|
} else {
|
|
|
|
/* Use player setting */
|
|
|
|
*replace_when_old = er->replace_when_old;
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 21:07:06 +00:00
|
|
|
return er == nullptr ? INVALID_ENGINE : er->to;
|
2008-09-13 15:49:29 +00:00
|
|
|
}
|
|
|
|
|
2011-01-18 22:17:15 +00:00
|
|
|
/**
|
|
|
|
* Add an engine replacement to the given renewlist.
|
|
|
|
* @param erl The renewlist to add to.
|
|
|
|
* @param old_engine The original engine type.
|
|
|
|
* @param new_engine The replacement engine type.
|
|
|
|
* @param group The group related to this replacement.
|
2012-04-17 19:44:02 +00:00
|
|
|
* @param replace_when_old Replace when old or always?
|
2011-01-18 22:17:15 +00:00
|
|
|
* @param flags The calling command flags.
|
|
|
|
* @return 0 on success, CMD_ERROR on failure.
|
|
|
|
*/
|
2012-04-17 19:44:02 +00:00
|
|
|
CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, bool replace_when_old, DoCommandFlag flags)
|
2008-09-13 15:49:29 +00:00
|
|
|
{
|
|
|
|
/* Check if the old vehicle is already in the list */
|
2010-03-20 16:47:07 +00:00
|
|
|
EngineRenew *er = GetEngineReplacement(*erl, old_engine, group);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (er != nullptr) {
|
2013-01-31 10:21:04 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
er->to = new_engine;
|
|
|
|
er->replace_when_old = replace_when_old;
|
|
|
|
}
|
2008-09-13 15:49:29 +00:00
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!EngineRenew::CanAllocateItem()) return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
er = new EngineRenew(old_engine, new_engine);
|
|
|
|
er->group_id = group;
|
2012-04-17 19:44:02 +00:00
|
|
|
er->replace_when_old = replace_when_old;
|
2008-09-13 15:49:29 +00:00
|
|
|
|
|
|
|
/* Insert before the first element */
|
|
|
|
er->next = (EngineRenew *)(*erl);
|
|
|
|
*erl = (EngineRenewList)er;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
2011-01-18 22:17:15 +00:00
|
|
|
/**
|
|
|
|
* Remove an engine replacement from a given renewlist.
|
|
|
|
* @param erl The renewlist from which to remove the replacement
|
|
|
|
* @param engine The original engine type.
|
|
|
|
* @param group The group related to this replacement.
|
|
|
|
* @param flags The calling command flags.
|
|
|
|
* @return 0 on success, CMD_ERROR on failure.
|
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, DoCommandFlag flags)
|
2008-09-13 15:49:29 +00:00
|
|
|
{
|
|
|
|
EngineRenew *er = (EngineRenew *)(*erl);
|
2019-04-10 21:07:06 +00:00
|
|
|
EngineRenew *prev = nullptr;
|
2008-09-13 15:49:29 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
while (er != nullptr) {
|
2008-09-13 15:49:29 +00:00
|
|
|
if (er->from == engine && er->group_id == group) {
|
|
|
|
if (flags & DC_EXEC) {
|
2019-04-10 21:07:06 +00:00
|
|
|
if (prev == nullptr) { // First element
|
2008-09-13 15:49:29 +00:00
|
|
|
/* The second becomes the new first element */
|
|
|
|
*erl = (EngineRenewList)er->next;
|
|
|
|
} else {
|
|
|
|
/* Cut this element out */
|
|
|
|
prev->next = er->next;
|
|
|
|
}
|
|
|
|
delete er;
|
|
|
|
}
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
prev = er;
|
|
|
|
er = er->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|