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