order_backup.cpp

Go to the documentation of this file.
00001 /* $Id: order_backup.cpp 21438 2010-12-08 18:21:29Z 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 "core/pool_func.hpp"
00015 #include "network/network.h"
00016 #include "network/network_func.h"
00017 #include "order_backup.h"
00018 #include "vehicle_base.h"
00019 
00020 OrderBackupPool _order_backup_pool("BackupOrder");
00021 INSTANTIATE_POOL_METHODS(OrderBackup)
00022 
00023 OrderBackup::~OrderBackup()
00024 {
00025   free(this->name);
00026 
00027   if (CleaningPool()) return;
00028 
00029   Order *o = this->orders;
00030   while (o != NULL) {
00031     Order *next = o->next;
00032     delete o;
00033     o = next;
00034   }
00035 }
00036 
00037 OrderBackup::OrderBackup(const Vehicle *v, uint32 user)
00038 {
00039   this->user             = user;
00040   this->tile             = v->tile;
00041   this->orderindex       = v->cur_order_index;
00042   this->group            = v->group_id;
00043   this->service_interval = v->service_interval;
00044 
00045   if (v->name != NULL) this->name = strdup(v->name);
00046 
00047   /* If we have shared orders, store the vehicle we share the order with. */
00048   if (v->IsOrderListShared()) {
00049     this->clone = (v->FirstShared() == v) ? v->NextShared() : v->FirstShared();
00050   } else {
00051     /* Else copy the orders */
00052     Order **tail = &this->orders;
00053 
00054     /* Count the number of orders */
00055     const Order *order;
00056     FOR_VEHICLE_ORDERS(v, order) {
00057       Order *copy = new Order();
00058       copy->AssignOrder(*order);
00059       *tail = copy;
00060       tail = &copy->next;
00061     }
00062   }
00063 }
00064 
00065 void OrderBackup::DoRestore(Vehicle *v)
00066 {
00067   /* If we have a custom name, process that */
00068   v->name = this->name;
00069   this->name = NULL;
00070 
00071   /* If we had shared orders, recover that */
00072   if (this->clone != NULL) {
00073     DoCommand(0, v->index | CO_SHARE << 30, this->clone->index, DC_EXEC, CMD_CLONE_ORDER);
00074   } else if (this->orders != NULL && OrderList::CanAllocateItem()) {
00075     v->orders.list = new OrderList(this->orders, v);
00076     this->orders = NULL;
00077   }
00078 
00079   uint num_orders = v->GetNumOrders();
00080   if (num_orders != 0) v->cur_order_index = this->orderindex % num_orders;
00081   v->service_interval = this->service_interval;
00082 
00083   /* Restore vehicle group */
00084   DoCommand(0, this->group, v->index, DC_EXEC, CMD_ADD_VEHICLE_GROUP);
00085 }
00086 
00087 /* static */ void OrderBackup::Backup(const Vehicle *v, uint32 user)
00088 {
00089   /* Don't use reset as that broadcasts over the network to reset the variable,
00090    * which is what we are doing at the moment. */
00091   OrderBackup *ob;
00092   FOR_ALL_ORDER_BACKUPS(ob) {
00093     if (ob->user == user) delete ob;
00094   }
00095   new OrderBackup(v, user);
00096 }
00097 
00098 /* static */ void OrderBackup::Restore(Vehicle *v, uint32 user)
00099 {
00100   OrderBackup *ob;
00101   FOR_ALL_ORDER_BACKUPS(ob) {
00102     if (v->tile != ob->tile || ob->user != user) continue;
00103 
00104     ob->DoRestore(v);
00105     delete ob;
00106   }
00107 }
00108 
00109 /* static */ void OrderBackup::ResetOfUser(TileIndex tile, uint32 user)
00110 {
00111   OrderBackup *ob;
00112   FOR_ALL_ORDER_BACKUPS(ob) {
00113     if (ob->user == user && (ob->tile == tile || tile == INVALID_TILE)) delete ob;
00114   }
00115 }
00116 
00126 CommandCost CmdClearOrderBackup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00127 {
00128   /* No need to check anything. If the tile or user don't exist we just ignore it. */
00129   if (flags & DC_EXEC) OrderBackup::ResetOfUser(tile == 0 ? INVALID_TILE : tile, p2);
00130 
00131   return CommandCost();
00132 }
00133 
00134 /* static */ void OrderBackup::ResetUser(uint32 user)
00135 {
00136   assert(_network_server);
00137 
00138   OrderBackup *ob;
00139   FOR_ALL_ORDER_BACKUPS(ob) {
00140     /* If it's not an backup of us, so ignore it. */
00141     if (ob->user != user) continue;
00142 
00143     DoCommandP(0, 0, user, CMD_CLEAR_ORDER_BACKUP);
00144     return;
00145   }
00146 }
00147 
00148 /* static */ void OrderBackup::Reset(TileIndex t, bool from_gui)
00149 {
00150   /* The user has CLIENT_ID_SERVER as default when network play is not active,
00151    * but compiled it. A network client has its own variable for the unique
00152    * client/user identifier. Finally if networking isn't compiled in the
00153    * default is just plain and simple: 0. */
00154 #ifdef ENABLE_NETWORK
00155   uint32 user = _networking && !_network_server ? _network_own_client_id : CLIENT_ID_SERVER;
00156 #else
00157   uint32 user = 0;
00158 #endif
00159 
00160   OrderBackup *ob;
00161   FOR_ALL_ORDER_BACKUPS(ob) {
00162     /* If it's not an backup of us, so ignore it. */
00163     if (ob->user != user) continue;
00164     /* If it's not for our chosen tile either, ignore it. */
00165     if (t != INVALID_TILE && t != ob->tile) continue;
00166 
00167     if (from_gui) {
00168       /* We need to circumvent the "prevention" from this command being executed
00169        * while the game is paused, so use the internal method. Nor do we want
00170        * this command to get its cost estimated when shift is pressed. */
00171       DoCommandPInternal(ob->tile, 0, user, CMD_CLEAR_ORDER_BACKUP, NULL, NULL, true, false);
00172     } else {
00173       /* The command came from the game logic, i.e. the clearing of a tile.
00174        * In that case we have no need to actually sync this, just do it. */
00175       delete ob;
00176     }
00177   }
00178 }
00179 
00180 /* static */ void OrderBackup::ClearGroup(GroupID group)
00181 {
00182   OrderBackup *ob;
00183   FOR_ALL_ORDER_BACKUPS(ob) {
00184     if (ob->group == group) ob->group = DEFAULT_GROUP;
00185   }
00186 }
00187 
00188 /* static */ void OrderBackup::ClearVehicle(const Vehicle *v)
00189 {
00190   assert(v != NULL);
00191   OrderBackup *ob;
00192   FOR_ALL_ORDER_BACKUPS(ob) {
00193     if (ob->clone == v) {
00194       /* Get another item in the shared list. */
00195       ob->clone = (v->FirstShared() == v) ? v->NextShared() : v->FirstShared();
00196       /* But if that isn't there, remove it. */
00197       if (ob->clone == NULL) delete ob;
00198     }
00199   }
00200 }
00201 
00202 void InitializeOrderBackups()
00203 {
00204   _order_backup_pool.CleanPool();
00205 }

Generated on Sun Jan 9 16:01:58 2011 for OpenTTD by  doxygen 1.6.1