# autoreplace.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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
    00019 EngineRenewPool _enginerenew_pool("EngineRenew");
    00020 INSTANTIATE_POOL_METHODS(EngineRenew)
    00021
    00022
    00026 static EngineRenew *GetEngineReplacement(EngineRenewList erl, EngineID engine, GroupID group)
    00027 {
    00028   EngineRenew *er = (EngineRenew *)erl;
    00029
    00030   while (er != NULL) {
    00031     if (er->from == engine && er->group_id == group) return er;
    00032     er = er->next;
    00033   }
    00034   return NULL;
    00035 }
    00036
    00042 void RemoveAllEngineReplacement(EngineRenewList *erl)
    00043 {
    00044   EngineRenew *er = (EngineRenew *)(*erl);
    00045   EngineRenew *next;
    00046
    00047   while (er != NULL) {
    00048     next = er->next;
    00049     delete er;
    00050     er = next;
    00051   }
    00052   *erl = NULL; // Empty list
    00053 }
    00054
    00063 EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group)
    00064 {
    00065   const EngineRenew *er = GetEngineReplacement(erl, engine, group);
    00066   if (er == NULL && (group == DEFAULT_GROUP || (Group::IsValidID(group) && !Group::Get(group)->replace_protection))) {
    00067     /* We didn't find anything useful in the vehicle's own group so we will try ALL_GROUP */
    00068     er = GetEngineReplacement(erl, engine, ALL_GROUP);
    00069   }
    00070   return er == NULL ? INVALID_ENGINE : er->to;
    00071 }
    00072
    00082 CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, DoCommandFlag flags)
    00083 {
    00084   /* Check if the old vehicle is already in the list */
    00085   EngineRenew *er = GetEngineReplacement(*erl, old_engine, group);
    00086   if (er != NULL) {
    00087     if (flags & DC_EXEC) er->to = new_engine;
    00088     return CommandCost();
    00089   }
    00090
    00091   if (!EngineRenew::CanAllocateItem()) return CMD_ERROR;
    00092
    00093   if (flags & DC_EXEC) {
    00094     er = new EngineRenew(old_engine, new_engine);
    00095     er->group_id = group;
    00096
    00097     /* Insert before the first element */
    00098     er->next = (EngineRenew *)(*erl);
    00099     *erl = (EngineRenewList)er;
    00100   }
    00101
    00102   return CommandCost();
    00103 }
    00104
    00113 CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, DoCommandFlag flags)
    00114 {
    00115   EngineRenew *er = (EngineRenew *)(*erl);
    00116   EngineRenew *prev = NULL;
    00117
    00118   while (er != NULL) {
    00119     if (er->from == engine && er->group_id == group) {
    00120       if (flags & DC_EXEC) {
    00121         if (prev == NULL) { // First element
    00122           /* The second becomes the new first element */
    00123           *erl = (EngineRenewList)er->next;
    00124         } else {
    00125           /* Cut this element out */
    00126           prev->next = er->next;
    00127         }
    00128         delete er;
    00129       }
    00130       return CommandCost();
    00131     }
    00132     prev = er;
    00133     er = er->next;
    00134   }
    00135
    00136   return CMD_ERROR;
    00137 }