# autoreplace.cpp

Go to the documentation of this file.

    00001 /* $Id: autoreplace.cpp 17248 2009-08-21 20:21:05Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "command_func.h"
    00014 #include "group.h"
    00015 #include "autoreplace_base.h"
    00016 #include "core/pool_func.hpp"
    00017
    00018 EngineRenewPool _enginerenew_pool("EngineRenew");
    00019 INSTANTIATE_POOL_METHODS(EngineRenew)
    00020
    00021
    00024 static EngineRenew *GetEngineReplacement(EngineRenewList erl, EngineID engine, GroupID group)
    00025 {
    00026   EngineRenew *er = (EngineRenew *)erl;
    00027
    00028   while (er) {
    00029     if (er->from == engine && er->group_id == group) return er;
    00030     er = er->next;
    00031   }
    00032   return NULL;
    00033 }
    00034
    00035 void RemoveAllEngineReplacement(EngineRenewList *erl)
    00036 {
    00037   EngineRenew *er = (EngineRenew *)(*erl);
    00038   EngineRenew *next;
    00039
    00040   while (er != NULL) {
    00041     next = er->next;
    00042     delete er;
    00043     er = next;
    00044   }
    00045   *erl = NULL; // Empty list
    00046 }
    00047
    00048 EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group)
    00049 {
    00050   const EngineRenew *er = GetEngineReplacement(erl, engine, group);
    00051   if (er == NULL && (group == DEFAULT_GROUP || (Group::IsValidID(group) && !Group::Get(group)->replace_protection))) {
    00052     /* We didn't find anything useful in the vehicle's own group so we will try ALL_GROUP */
    00053     er = GetEngineReplacement(erl, engine, ALL_GROUP);
    00054   }
    00055   return er == NULL ? INVALID_ENGINE : er->to;
    00056 }
    00057
    00058 CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, DoCommandFlag flags)
    00059 {
    00060   EngineRenew *er;
    00061
    00062   /* Check if the old vehicle is already in the list */
    00063   er = GetEngineReplacement(*erl, old_engine, group);
    00064   if (er != NULL) {
    00065     if (flags & DC_EXEC) er->to = new_engine;
    00066     return CommandCost();
    00067   }
    00068
    00069   if (!EngineRenew::CanAllocateItem()) return CMD_ERROR;
    00070
    00071   if (flags & DC_EXEC) {
    00072     er = new EngineRenew(old_engine, new_engine);
    00073     er->group_id = group;
    00074
    00075     /* Insert before the first element */
    00076     er->next = (EngineRenew *)(*erl);
    00077     *erl = (EngineRenewList)er;
    00078   }
    00079
    00080   return CommandCost();
    00081 }
    00082
    00083 CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, DoCommandFlag flags)
    00084 {
    00085   EngineRenew *er = (EngineRenew *)(*erl);
    00086   EngineRenew *prev = NULL;
    00087
    00088   while (er)
    00089   {
    00090     if (er->from == engine && er->group_id == group) {
    00091       if (flags & DC_EXEC) {
    00092         if (prev == NULL) { // First element
    00093           /* The second becomes the new first element */
    00094           *erl = (EngineRenewList)er->next;
    00095         } else {
    00096           /* Cut this element out */
    00097           prev->next = er->next;
    00098         }
    00099         delete er;
    00100       }
    00101       return CommandCost();
    00102     }
    00103     prev = er;
    00104     er = er->next;
    00105   }
    00106
    00107   return CMD_ERROR;
    00108 }
    00109
    00110 void InitializeEngineRenews()
    00111 {
    00112   _enginerenew_pool.CleanPool();
    00113 }