2008-09-13 15:49:29 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
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"
|
2009-05-22 15:13:50 +00:00
|
|
|
#include "core/pool_func.hpp"
|
2008-09-13 15:49:29 +00:00
|
|
|
|
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
|
|
|
|
* engine type from the given renewlist */
|
|
|
|
static EngineRenew *GetEngineReplacement(EngineRenewList erl, EngineID engine, GroupID group)
|
|
|
|
{
|
|
|
|
EngineRenew *er = (EngineRenew *)erl;
|
|
|
|
|
|
|
|
while (er) {
|
|
|
|
if (er->from == engine && er->group_id == group) return er;
|
|
|
|
er = er->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveAllEngineReplacement(EngineRenewList *erl)
|
|
|
|
{
|
|
|
|
EngineRenew *er = (EngineRenew *)(*erl);
|
|
|
|
EngineRenew *next;
|
|
|
|
|
|
|
|
while (er != NULL) {
|
|
|
|
next = er->next;
|
|
|
|
delete er;
|
|
|
|
er = next;
|
|
|
|
}
|
|
|
|
*erl = NULL; // Empty list
|
|
|
|
}
|
|
|
|
|
|
|
|
EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group)
|
|
|
|
{
|
|
|
|
const EngineRenew *er = GetEngineReplacement(erl, engine, group);
|
2009-05-17 01:00:56 +00:00
|
|
|
if (er == NULL && (group == DEFAULT_GROUP || (Group::IsValidID(group) && !Group::Get(group)->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);
|
|
|
|
}
|
|
|
|
return er == NULL ? INVALID_ENGINE : er->to;
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, 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);
|
2008-09-13 15:49:29 +00:00
|
|
|
if (er != NULL) {
|
|
|
|
if (flags & DC_EXEC) er->to = new_engine;
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!EngineRenew::CanAllocateItem()) return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
er = new EngineRenew(old_engine, new_engine);
|
|
|
|
er->group_id = group;
|
|
|
|
|
|
|
|
/* Insert before the first element */
|
|
|
|
er->next = (EngineRenew *)(*erl);
|
|
|
|
*erl = (EngineRenewList)er;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
EngineRenew *prev = NULL;
|
|
|
|
|
2010-03-20 15:30:57 +00:00
|
|
|
while (er) {
|
2008-09-13 15:49:29 +00:00
|
|
|
if (er->from == engine && er->group_id == group) {
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
if (prev == NULL) { // First element
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeEngineRenews()
|
|
|
|
{
|
2009-05-22 15:13:50 +00:00
|
|
|
_enginerenew_pool.CleanPool();
|
2008-09-13 15:49:29 +00:00
|
|
|
}
|