# autoreplace.cpp

Go to the documentation of this file.

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