(svn r20539) -Codechange: make order backups more self-contained.
parent
202839e58c
commit
e68cf6bd81
@ -0,0 +1,146 @@
|
||||
/* $Id$ */
|
||||
|
||||
/*
|
||||
* 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.cpp Handling of order backups. */
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "command_func.h"
|
||||
#include "core/pool_func.hpp"
|
||||
#include "order_backup.h"
|
||||
#include "order_base.h"
|
||||
#include "vehicle_base.h"
|
||||
#include "settings_type.h"
|
||||
|
||||
OrderBackupPool _order_backup_pool("BackupOrder");
|
||||
INSTANTIATE_POOL_METHODS(OrderBackup)
|
||||
|
||||
OrderBackup::~OrderBackup()
|
||||
{
|
||||
free(this->name);
|
||||
free(this->orders);
|
||||
}
|
||||
|
||||
OrderBackup::OrderBackup(const Vehicle *v)
|
||||
{
|
||||
this->tile = v->tile;
|
||||
this->orderindex = v->cur_order_index;
|
||||
this->group = v->group_id;
|
||||
this->service_interval = v->service_interval;
|
||||
|
||||
if (v->name != NULL) this->name = strdup(v->name);
|
||||
|
||||
/* If we have shared orders, store the vehicle we share the order with. */
|
||||
if (v->IsOrderListShared()) {
|
||||
const Vehicle *u = (v->FirstShared() == v) ? v->NextShared() : v->FirstShared();
|
||||
|
||||
this->clone = u->index;
|
||||
} else {
|
||||
/* Else copy the orders */
|
||||
|
||||
/* We do not have shared orders */
|
||||
this->clone = INVALID_VEHICLE;
|
||||
|
||||
/* Count the number of orders */
|
||||
uint cnt = 0;
|
||||
const Order *order;
|
||||
FOR_VEHICLE_ORDERS(v, order) cnt++;
|
||||
|
||||
/* Allocate memory for the orders plus an end-of-orders marker */
|
||||
this->orders = MallocT<Order>(cnt + 1);
|
||||
|
||||
Order *dest = this->orders;
|
||||
|
||||
/* Copy the orders */
|
||||
FOR_VEHICLE_ORDERS(v, order) {
|
||||
memcpy(dest, order, sizeof(Order));
|
||||
dest++;
|
||||
}
|
||||
/* End the list with an empty order */
|
||||
dest->Free();
|
||||
}
|
||||
}
|
||||
|
||||
void OrderBackup::RestoreTo(const Vehicle *v)
|
||||
{
|
||||
/* If we have a custom name, process that */
|
||||
if (this->name != NULL) DoCommandP(0, v->index, 0, CMD_RENAME_VEHICLE, NULL, this->name);
|
||||
|
||||
/* If we had shared orders, recover that */
|
||||
if (this->clone != INVALID_VEHICLE) {
|
||||
DoCommandP(0, v->index | (this->clone << 16), CO_SHARE, CMD_CLONE_ORDER);
|
||||
} else {
|
||||
|
||||
/* CMD_NO_TEST_IF_IN_NETWORK is used here, because CMD_INSERT_ORDER checks if the
|
||||
* order number is one more than the current amount of orders, and because
|
||||
* in network the commands are queued before send, the second insert always
|
||||
* fails in test mode. By bypassing the test-mode, that no longer is a problem. */
|
||||
for (uint i = 0; !this->orders[i].IsType(OT_NOTHING); i++) {
|
||||
Order o = this->orders[i];
|
||||
/* Conditional orders need to have their destination to be valid on insertion. */
|
||||
if (o.IsType(OT_CONDITIONAL)) o.SetConditionSkipToOrder(0);
|
||||
|
||||
if (!DoCommandP(0, v->index + (i << 16), o.Pack(),
|
||||
CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK)) {
|
||||
break;
|
||||
}
|
||||
|
||||
/* Copy timetable if enabled */
|
||||
if (_settings_game.order.timetabling && !DoCommandP(0, v->index | (i << 16) | (1 << 25),
|
||||
o.wait_time << 16 | o.travel_time,
|
||||
CMD_CHANGE_TIMETABLE | CMD_NO_TEST_IF_IN_NETWORK)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Fix the conditional orders' destination. */
|
||||
for (uint i = 0; !this->orders[i].IsType(OT_NOTHING); i++) {
|
||||
if (!this->orders[i].IsType(OT_CONDITIONAL)) continue;
|
||||
|
||||
if (!DoCommandP(0, v->index + (i << 16), MOF_LOAD | (this->orders[i].GetConditionSkipToOrder() << 4),
|
||||
CMD_MODIFY_ORDER | CMD_NO_TEST_IF_IN_NETWORK)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Restore vehicle order-index and service interval */
|
||||
DoCommandP(0, v->index, this->orderindex | (this->service_interval << 16), CMD_RESTORE_ORDER_INDEX);
|
||||
|
||||
/* Restore vehicle group */
|
||||
DoCommandP(0, this->group, v->index, CMD_ADD_VEHICLE_GROUP);
|
||||
|
||||
delete this;
|
||||
}
|
||||
|
||||
/* static */ OrderBackup *OrderBackup::GetByTile(TileIndex t)
|
||||
{
|
||||
OrderBackup *ob;
|
||||
FOR_ALL_ORDER_BACKUPS(ob) {
|
||||
if (ob->tile == t) return ob;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* static */ void OrderBackup::Reset()
|
||||
{
|
||||
_order_backup_pool.CleanPool();
|
||||
}
|
||||
|
||||
/* static */ void OrderBackup::ClearGroup(GroupID group)
|
||||
{
|
||||
OrderBackup *ob;
|
||||
FOR_ALL_ORDER_BACKUPS(ob) {
|
||||
if (ob->group == group) ob->group = DEFAULT_GROUP;
|
||||
}
|
||||
}
|
||||
|
||||
void InitializeOrderBackups()
|
||||
{
|
||||
_order_backup_pool.CleanPool();
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
/* $Id$ */
|
||||
|
||||
/*
|
||||
* 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 "order_type.h"
|
||||
#include "vehicle_type.h"
|
||||
#include "tile_type.h"
|
||||
#include "group_type.h"
|
||||
#include "company_type.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;
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
private:
|
||||
TileIndex tile; ///< Tile of the depot where the order was changed.
|
||||
GroupID group; ///< The group the vehicle was part of.
|
||||
uint16 service_interval; ///< The service interval of the vehicle.
|
||||
char *name; ///< The custom name of the vehicle.
|
||||
|
||||
VehicleID clone; ///< VehicleID this vehicle was a clone of, or INVALID_VEHICLE.
|
||||
VehicleOrderID orderindex; ///< The order-index the vehicle had.
|
||||
Order *orders; ///< The actual orders if the vehicle was not a clone.
|
||||
|
||||
public:
|
||||
/**
|
||||
* Create an order backup for the given vehicle.
|
||||
* @param v The vehicle to make a backup of.
|
||||
*/
|
||||
OrderBackup(const Vehicle *v);
|
||||
|
||||
/** Free everything that is allocated. */
|
||||
~OrderBackup();
|
||||
|
||||
/**
|
||||
* Restore the data of this order to the given vehicle.
|
||||
* @param v The vehicle to restore to.
|
||||
* @note After restoration the backup will automatically be removed.
|
||||
*/
|
||||
void RestoreTo(const Vehicle *v);
|
||||
|
||||
/**
|
||||
* Get the order backup associated with a given tile.
|
||||
* @param t The tile to get the order backup for.
|
||||
* @return The order backup, or NULL if it doesn't exist.
|
||||
*/
|
||||
static OrderBackup *GetByTile(TileIndex t);
|
||||
|
||||
/**
|
||||
* Reset the OrderBackups.
|
||||
*/
|
||||
static void Reset();
|
||||
|
||||
/**
|
||||
* Clear the group of all backups having this group ID.
|
||||
* @param group The group to clear
|
||||
*/
|
||||
static void ClearGroup(GroupID group);
|
||||
};
|
||||
|
||||
#define FOR_ALL_ORDER_BACKUPS_FROM(var, start) FOR_ALL_ITEMS_FROM(OrderBackup, order_backup_index, var, start)
|
||||
#define FOR_ALL_ORDER_BACKUPS(var) FOR_ALL_ORDER_BACKUPS_FROM(var, 0)
|
||||
|
||||
#endif /* ORDER_BACKUP_H */
|
Loading…
Reference in New Issue