Changeset - r19519:a08cec92f7c7
[Not reviewed]
master
0 3 0
frosch - 12 years ago 2012-07-29 16:45:34
frosch@openttd.org
(svn r24444) -Codechange: Base OrderBackup on BaseConsist.
3 files changed with 9 insertions and 22 deletions:
0 comments (0 inline, 0 general)
src/order_backup.cpp
Show inline comments
 
@@ -16,50 +16,46 @@
 
#include "network/network_func.h"
 
#include "order_backup.h"
 
#include "vehicle_base.h"
 
#include "window_func.h"
 
#include "station_map.h"
 

	
 
OrderBackupPool _order_backup_pool("BackupOrder");
 
INSTANTIATE_POOL_METHODS(OrderBackup)
 

	
 
/** Free everything that is allocated. */
 
OrderBackup::~OrderBackup()
 
{
 
	free(this->name);
 

	
 
	if (CleaningPool()) return;
 

	
 
	Order *o = this->orders;
 
	while (o != NULL) {
 
		Order *next = o->next;
 
		delete o;
 
		o = next;
 
	}
 
}
 

	
 
/**
 
 * Create an order backup for the given vehicle.
 
 * @param v    The vehicle to make a backup of.
 
 * @param user The user that is requesting the backup.
 
 */
 
OrderBackup::OrderBackup(const Vehicle *v, uint32 user)
 
{
 
	this->user             = user;
 
	this->tile             = v->tile;
 
	this->orderindex       = v->cur_implicit_order_index;
 
	this->group            = v->group_id;
 
	this->service_interval = v->service_interval;
 

	
 
	if (v->name != NULL) this->name = strdup(v->name);
 
	this->CopyConsistPropertiesFrom(v);
 

	
 
	/* If we have shared orders, store the vehicle we share the order with. */
 
	if (v->IsOrderListShared()) {
 
		this->clone = (v->FirstShared() == v) ? v->NextShared() : v->FirstShared();
 
	} else {
 
		/* Else copy the orders */
 
		Order **tail = &this->orders;
 

	
 
		/* Count the number of orders */
 
		const Order *order;
 
		FOR_VEHICLE_ORDERS(v, order) {
 
			Order *copy = new Order();
 
@@ -67,44 +63,39 @@ OrderBackup::OrderBackup(const Vehicle *
 
			*tail = copy;
 
			tail = &copy->next;
 
		}
 
	}
 
}
 

	
 
/**
 
 * Restore the data of this order to the given vehicle.
 
 * @param v The vehicle to restore to.
 
 */
 
void OrderBackup::DoRestore(Vehicle *v)
 
{
 
	/* If we have a custom name, process that */
 
	v->name = this->name;
 
	this->name = NULL;
 

	
 
	/* If we had shared orders, recover that */
 
	if (this->clone != NULL) {
 
		DoCommand(0, v->index | CO_SHARE << 30, this->clone->index, DC_EXEC, CMD_CLONE_ORDER);
 
	} else if (this->orders != NULL && OrderList::CanAllocateItem()) {
 
		v->orders.list = new OrderList(this->orders, v);
 
		this->orders = NULL;
 
		/* Make sure buoys/oil rigs are updated in the station list. */
 
		InvalidateWindowClassesData(WC_STATION_LIST, 0);
 
	}
 

	
 
	uint num_orders = v->GetNumOrders();
 
	if (num_orders != 0) {
 
		v->cur_real_order_index = v->cur_implicit_order_index = this->orderindex % num_orders;
 
		v->UpdateRealOrderIndex();
 
	}
 
	v->service_interval = this->service_interval;
 
	v->CopyConsistPropertiesFrom(this);
 

	
 
	/* Make sure orders are in range */
 
	v->UpdateRealOrderIndex();
 
	v->cur_implicit_order_index = v->cur_real_order_index;
 

	
 
	/* Restore vehicle group */
 
	DoCommand(0, this->group, v->index, DC_EXEC, CMD_ADD_VEHICLE_GROUP);
 
}
 

	
 
/**
 
 * Create an order backup for the given vehicle.
 
 * @param v    The vehicle to make a backup of.
 
 * @param user The user that is requesting the backup.
 
 * @note Will automatically remove any previous backups of this user.
 
 */
 
/* static */ void OrderBackup::Backup(const Vehicle *v, uint32 user)
src/order_backup.h
Show inline comments
 
@@ -4,58 +4,54 @@
 
 * This file is part of OpenTTD.
 
 * 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.
 
 * 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.
 
 * 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/>.
 
 */
 

	
 
/** @file order_backup.h Functions related to order backups. */
 

	
 
#ifndef ORDER_BACKUP_H
 
#define ORDER_BACKUP_H
 

	
 
#include "core/pool_type.hpp"
 
#include "date_type.h"
 
#include "group_type.h"
 
#include "order_type.h"
 
#include "tile_type.h"
 
#include "vehicle_type.h"
 
#include "base_consist.h"
 

	
 
/** Unique identifier for an order backup. */
 
typedef uint8 OrderBackupID;
 
struct OrderBackup;
 

	
 
/** The pool type for order backups. */
 
typedef Pool<OrderBackup, OrderBackupID, 1, 256> OrderBackupPool;
 
/** The pool with order backups. */
 
extern OrderBackupPool _order_backup_pool;
 

	
 
/** Flag to pass to the vehicle construction command when an order should be preserved. */
 
static const uint32 MAKE_ORDER_BACKUP_FLAG = 1U << 31;
 

	
 
/**
 
 * Data for backing up an order of a vehicle so it can be
 
 * restored after a vehicle is rebuilt in the same depot.
 
 */
 
struct OrderBackup : OrderBackupPool::PoolItem<&_order_backup_pool> {
 
struct OrderBackup : OrderBackupPool::PoolItem<&_order_backup_pool>, BaseConsist {
 
private:
 
	friend const struct SaveLoad *GetOrderBackupDescription(); ///< Saving and loading of order backups.
 
	friend void Load_BKOR();   ///< Creating empty orders upon savegame loading.
 
	uint32 user;               ///< The user that requested the backup.
 
	TileIndex tile;            ///< Tile of the depot where the order was changed.
 
	GroupID group;             ///< The group the vehicle was part of.
 
	Date service_interval;     ///< The service interval of the vehicle.
 
	char *name;                ///< The custom name of the vehicle.
 

	
 
	const Vehicle *clone;      ///< Vehicle this vehicle was a clone of.
 
	VehicleOrderID orderindex; ///< The order-index the vehicle had.
 
	Order *orders;             ///< The actual orders if the vehicle was not a clone.
 

	
 
	/** Creation for savegame restoration. */
 
	OrderBackup() {}
 
	OrderBackup(const Vehicle *v, uint32 user);
 

	
 
	void DoRestore(Vehicle *v);
 

	
 
public:
 
	~OrderBackup();
 

	
 
	static void Backup(const Vehicle *v, uint32 user);
src/saveload/order_sl.cpp
Show inline comments
 
@@ -241,25 +241,25 @@ static void Ptrs_ORDL()
 
	}
 
}
 

	
 
const SaveLoad *GetOrderBackupDescription()
 
{
 
	static const SaveLoad _order_backup_desc[] = {
 
		SLE_VAR(OrderBackup, user,                  SLE_UINT32),
 
		SLE_VAR(OrderBackup, tile,                  SLE_UINT32),
 
		SLE_VAR(OrderBackup, group,                 SLE_UINT16),
 
		SLE_VAR(OrderBackup, service_interval,      SLE_INT32),
 
		SLE_STR(OrderBackup, name,                  SLE_STR, 0),
 
		SLE_VAR(OrderBackup, clone,                 SLE_UINT16),
 
		SLE_VAR(OrderBackup, orderindex,            SLE_UINT8),
 
		SLE_VAR(OrderBackup, cur_real_order_index,  SLE_UINT8),
 
		SLE_REF(OrderBackup, orders,                REF_ORDER),
 
		SLE_END()
 
	};
 

	
 
	return _order_backup_desc;
 
}
 

	
 
static void Save_BKOR()
 
{
 
	/* We only save this when we're a network server
 
	 * as we want this information on our clients. For
 
	 * normal games this information isn't needed. */
0 comments (0 inline, 0 general)