2009-01-04 15:32:25 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-04 15:32:25 +00:00
|
|
|
/** @file vehicle_sl.cpp Code handling saving and loading of vehicles */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../vehicle_func.h"
|
|
|
|
#include "../train.h"
|
|
|
|
#include "../roadveh.h"
|
|
|
|
#include "../ship.h"
|
|
|
|
#include "../aircraft.h"
|
2009-06-24 17:39:54 +00:00
|
|
|
#include "../station_base.h"
|
2009-01-04 15:32:25 +00:00
|
|
|
#include "../effectvehicle_base.h"
|
|
|
|
|
|
|
|
#include "saveload.h"
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
2011-01-31 20:27:33 +00:00
|
|
|
/**
|
2009-01-04 15:32:25 +00:00
|
|
|
* Link front and rear multiheaded engines to each other
|
|
|
|
* This is done when loading a savegame
|
|
|
|
*/
|
|
|
|
void ConnectMultiheadedTrains()
|
|
|
|
{
|
2009-05-26 22:10:13 +00:00
|
|
|
Train *v;
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2009-05-26 22:10:13 +00:00
|
|
|
FOR_ALL_TRAINS(v) {
|
|
|
|
v->other_multiheaded_part = NULL;
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
|
2009-05-26 22:10:13 +00:00
|
|
|
FOR_ALL_TRAINS(v) {
|
2009-07-01 23:49:13 +00:00
|
|
|
if (v->IsFrontEngine() || v->IsFreeWagon()) {
|
2009-01-04 15:32:25 +00:00
|
|
|
/* Two ways to associate multiheaded parts to each other:
|
|
|
|
* sequential-matching: Trains shall be arranged to look like <..>..<..>..<..>..
|
|
|
|
* bracket-matching: Free vehicle chains shall be arranged to look like ..<..<..>..<..>..>..
|
|
|
|
*
|
|
|
|
* Note: Old savegames might contain chains which do not comply with these rules, e.g.
|
|
|
|
* - the front and read parts have invalid orders
|
|
|
|
* - different engine types might be combined
|
|
|
|
* - there might be different amounts of front and rear parts.
|
|
|
|
*
|
|
|
|
* Note: The multiheaded parts need to be matched exactly like they are matched on the server, else desyncs will occur.
|
|
|
|
* This is why two matching strategies are needed.
|
|
|
|
*/
|
|
|
|
|
2009-07-01 22:22:01 +00:00
|
|
|
bool sequential_matching = v->IsFrontEngine();
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2009-07-02 12:10:39 +00:00
|
|
|
for (Train *u = v; u != NULL; u = u->GetNextVehicle()) {
|
2009-05-22 22:33:05 +00:00
|
|
|
if (u->other_multiheaded_part != NULL) continue; // we already linked this one
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2009-07-01 23:57:20 +00:00
|
|
|
if (u->IsMultiheaded()) {
|
|
|
|
if (!u->IsEngine()) {
|
2009-01-04 15:32:25 +00:00
|
|
|
/* we got a rear car without a front car. We will convert it to a front one */
|
2009-07-02 00:17:27 +00:00
|
|
|
u->SetEngine();
|
2009-01-04 15:32:25 +00:00
|
|
|
u->spritenum--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find a matching back part */
|
|
|
|
EngineID eid = u->engine_type;
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *w;
|
2009-01-04 15:32:25 +00:00
|
|
|
if (sequential_matching) {
|
2009-07-02 12:10:39 +00:00
|
|
|
for (w = u->GetNextVehicle(); w != NULL; w = w->GetNextVehicle()) {
|
2009-07-01 23:57:20 +00:00
|
|
|
if (w->engine_type != eid || w->other_multiheaded_part != NULL || !w->IsMultiheaded()) continue;
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
/* we found a car to partner with this engine. Now we will make sure it face the right way */
|
2009-07-01 23:57:20 +00:00
|
|
|
if (w->IsEngine()) {
|
2009-07-02 00:17:27 +00:00
|
|
|
w->ClearEngine();
|
2009-01-04 15:32:25 +00:00
|
|
|
w->spritenum++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint stack_pos = 0;
|
2009-07-02 12:10:39 +00:00
|
|
|
for (w = u->GetNextVehicle(); w != NULL; w = w->GetNextVehicle()) {
|
2009-07-01 23:57:20 +00:00
|
|
|
if (w->engine_type != eid || w->other_multiheaded_part != NULL || !w->IsMultiheaded()) continue;
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2009-07-01 23:57:20 +00:00
|
|
|
if (w->IsEngine()) {
|
2009-01-04 15:32:25 +00:00
|
|
|
stack_pos++;
|
|
|
|
} else {
|
|
|
|
if (stack_pos == 0) break;
|
|
|
|
stack_pos--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (w != NULL) {
|
2009-05-22 22:33:05 +00:00
|
|
|
w->other_multiheaded_part = u;
|
|
|
|
u->other_multiheaded_part = w;
|
2009-01-04 15:32:25 +00:00
|
|
|
} else {
|
|
|
|
/* we got a front car and no rear cars. We will fake this one for forget that it should have been multiheaded */
|
2009-07-02 00:17:27 +00:00
|
|
|
u->ClearMultiheaded();
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts all trains to the new subtype format introduced in savegame 16.2
|
|
|
|
* It also links multiheaded engines or make them forget they are multiheaded if no suitable partner is found
|
|
|
|
*/
|
|
|
|
void ConvertOldMultiheadToNew()
|
|
|
|
{
|
2009-05-26 22:45:48 +00:00
|
|
|
Train *t;
|
|
|
|
FOR_ALL_TRAINS(t) SetBit(t->subtype, 7); // indicates that it's the old format and needs to be converted in the next loop
|
|
|
|
|
|
|
|
FOR_ALL_TRAINS(t) {
|
|
|
|
if (HasBit(t->subtype, 7) && ((t->subtype & ~0x80) == 0 || (t->subtype & ~0x80) == 4)) {
|
2009-07-02 00:17:27 +00:00
|
|
|
for (Train *u = t; u != NULL; u = u->Next()) {
|
2009-05-26 22:45:48 +00:00
|
|
|
const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
|
|
|
|
|
|
|
|
ClrBit(u->subtype, 7);
|
|
|
|
switch (u->subtype) {
|
|
|
|
case 0: // TS_Front_Engine
|
2009-07-02 00:17:27 +00:00
|
|
|
if (rvi->railveh_type == RAILVEH_MULTIHEAD) u->SetMultiheaded();
|
|
|
|
u->SetFrontEngine();
|
|
|
|
u->SetEngine();
|
2009-05-26 22:45:48 +00:00
|
|
|
break;
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2009-05-26 22:45:48 +00:00
|
|
|
case 1: // TS_Artic_Part
|
|
|
|
u->subtype = 0;
|
2009-07-02 00:17:27 +00:00
|
|
|
u->SetArticulatedPart();
|
2009-05-26 22:45:48 +00:00
|
|
|
break;
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2009-05-26 22:45:48 +00:00
|
|
|
case 2: // TS_Not_First
|
|
|
|
u->subtype = 0;
|
|
|
|
if (rvi->railveh_type == RAILVEH_WAGON) {
|
|
|
|
/* normal wagon */
|
2009-07-02 00:17:27 +00:00
|
|
|
u->SetWagon();
|
2009-01-04 15:32:25 +00:00
|
|
|
break;
|
2009-05-26 22:45:48 +00:00
|
|
|
}
|
|
|
|
if (rvi->railveh_type == RAILVEH_MULTIHEAD && rvi->image_index == u->spritenum - 1) {
|
|
|
|
/* rear end of a multiheaded engine */
|
2009-07-02 00:17:27 +00:00
|
|
|
u->SetMultiheaded();
|
2009-05-26 22:45:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-07-02 00:17:27 +00:00
|
|
|
if (rvi->railveh_type == RAILVEH_MULTIHEAD) u->SetMultiheaded();
|
|
|
|
u->SetEngine();
|
2009-05-26 22:45:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4: // TS_Free_Car
|
|
|
|
u->subtype = 0;
|
2009-07-02 00:17:27 +00:00
|
|
|
u->SetWagon();
|
|
|
|
u->SetFreeWagon();
|
2009-05-26 22:45:48 +00:00
|
|
|
break;
|
2010-09-16 22:06:36 +00:00
|
|
|
default: SlErrorCorrupt("Invalid train subtype");
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** need to be called to load aircraft from old version */
|
|
|
|
void UpdateOldAircraft()
|
|
|
|
{
|
|
|
|
/* set airport_flags to 0 for all airports just to be sure */
|
|
|
|
Station *st;
|
|
|
|
FOR_ALL_STATIONS(st) {
|
2010-03-18 21:02:20 +00:00
|
|
|
st->airport.flags = 0; // reset airport
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
|
2009-05-26 22:10:13 +00:00
|
|
|
Aircraft *a;
|
|
|
|
FOR_ALL_AIRCRAFT(a) {
|
2009-01-23 15:53:24 +00:00
|
|
|
/* airplane has another vehicle with subtype 4 (shadow), helicopter also has 3 (rotor)
|
|
|
|
* skip those */
|
2009-07-13 16:37:27 +00:00
|
|
|
if (a->IsNormalAircraft()) {
|
2009-01-04 15:32:25 +00:00
|
|
|
/* airplane in terminal stopped doesn't hurt anyone, so goto next */
|
2009-06-01 11:43:36 +00:00
|
|
|
if ((a->vehstatus & VS_STOPPED) && a->state == 0) {
|
2009-05-22 20:07:26 +00:00
|
|
|
a->state = HANGAR;
|
2009-01-04 15:32:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-09-09 21:27:57 +00:00
|
|
|
AircraftLeaveHangar(a, a->direction); // make airplane visible if it was in a depot for example
|
2009-05-22 20:07:26 +00:00
|
|
|
a->vehstatus &= ~VS_STOPPED; // make airplane moving
|
2010-11-09 17:43:05 +00:00
|
|
|
UpdateAircraftCache(a);
|
|
|
|
a->cur_speed = a->vcache.cached_max_speed; // so aircraft don't have zero speed while in air
|
2009-05-22 20:07:26 +00:00
|
|
|
if (!a->current_order.IsType(OT_GOTO_STATION) && !a->current_order.IsType(OT_GOTO_DEPOT)) {
|
2009-01-23 15:37:44 +00:00
|
|
|
/* reset current order so aircraft doesn't have invalid "station-only" order */
|
2009-05-22 20:07:26 +00:00
|
|
|
a->current_order.MakeDummy();
|
2009-01-23 15:37:44 +00:00
|
|
|
}
|
2009-05-22 20:07:26 +00:00
|
|
|
a->state = FLYING;
|
|
|
|
AircraftNextAirportPos_and_Order(a); // move it to the entry point of the airport
|
|
|
|
GetNewVehiclePosResult gp = GetNewVehiclePos(a);
|
|
|
|
a->tile = 0; // aircraft in air is tile=0
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
/* correct speed of helicopter-rotors */
|
2009-05-22 20:07:26 +00:00
|
|
|
if (a->subtype == AIR_HELICOPTER) a->Next()->Next()->cur_speed = 32;
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
/* set new position x,y,z */
|
2009-05-22 20:07:26 +00:00
|
|
|
SetAircraftPosition(a, gp.x, gp.y, GetAircraftFlyingAltitude(a));
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-25 21:55:18 +00:00
|
|
|
/**
|
|
|
|
* Check all vehicles to ensure their engine type is valid
|
|
|
|
* for the currently loaded NewGRFs (that includes none...)
|
|
|
|
* This only makes a difference if NewGRFs are missing, otherwise
|
|
|
|
* all vehicles will be valid. This does not make such a game
|
|
|
|
* playable, it only prevents crash.
|
|
|
|
*/
|
|
|
|
static void CheckValidVehicles()
|
|
|
|
{
|
2009-05-22 15:13:50 +00:00
|
|
|
size_t total_engines = Engine::GetPoolSize();
|
2009-01-25 21:55:18 +00:00
|
|
|
EngineID first_engine[4] = { INVALID_ENGINE, INVALID_ENGINE, INVALID_ENGINE, INVALID_ENGINE };
|
|
|
|
|
|
|
|
Engine *e;
|
|
|
|
FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) { first_engine[VEH_TRAIN] = e->index; break; }
|
|
|
|
FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) { first_engine[VEH_ROAD] = e->index; break; }
|
|
|
|
FOR_ALL_ENGINES_OF_TYPE(e, VEH_SHIP) { first_engine[VEH_SHIP] = e->index; break; }
|
|
|
|
FOR_ALL_ENGINES_OF_TYPE(e, VEH_AIRCRAFT) { first_engine[VEH_AIRCRAFT] = e->index; break; }
|
|
|
|
|
|
|
|
Vehicle *v;
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
/* Test if engine types match */
|
|
|
|
switch (v->type) {
|
|
|
|
case VEH_TRAIN:
|
|
|
|
case VEH_ROAD:
|
|
|
|
case VEH_SHIP:
|
|
|
|
case VEH_AIRCRAFT:
|
2011-11-01 00:21:08 +00:00
|
|
|
if (v->engine_type >= total_engines || v->type != v->GetEngine()->type) {
|
2009-01-25 21:55:18 +00:00
|
|
|
v->engine_type = first_engine[v->type];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-03 20:55:08 +00:00
|
|
|
extern byte _age_cargo_skip_counter; // From misc_sl.cpp
|
|
|
|
|
2009-01-04 15:32:25 +00:00
|
|
|
/** Called after load to update coordinates */
|
|
|
|
void AfterLoadVehicles(bool part_of_load)
|
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
/* Reinstate the previous pointer */
|
|
|
|
if (v->Next() != NULL) v->Next()->previous = v;
|
|
|
|
if (v->NextShared() != NULL) v->NextShared()->previous_shared = v;
|
|
|
|
|
|
|
|
if (part_of_load) v->fill_percent_te_id = INVALID_TE_ID;
|
|
|
|
v->first = NULL;
|
2010-12-14 21:33:53 +00:00
|
|
|
if (v->IsGroundVehicle()) v->GetGroundVehicleCache()->first_engine = INVALID_ENGINE;
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* AfterLoadVehicles may also be called in case of NewGRF reload, in this
|
|
|
|
* case we may not convert orders again. */
|
|
|
|
if (part_of_load) {
|
|
|
|
/* Create shared vehicle chain for very old games (pre 5,2) and create
|
|
|
|
* OrderList from shared vehicle chains. For this to work correctly, the
|
|
|
|
* following conditions must be fulfilled:
|
|
|
|
* a) both next_shared and previous_shared are not set for pre 5,2 games
|
|
|
|
* b) both next_shared and previous_shared are set for later games
|
|
|
|
*/
|
|
|
|
std::map<Order*, OrderList*> mapping;
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->orders.old != NULL) {
|
2010-11-21 12:47:04 +00:00
|
|
|
if (IsSavegameVersionBefore(105)) { // Pre-105 didn't save an OrderList
|
2009-01-04 15:32:25 +00:00
|
|
|
if (mapping[v->orders.old] == NULL) {
|
|
|
|
/* This adds the whole shared vehicle chain for case b */
|
2011-02-08 18:34:13 +00:00
|
|
|
|
|
|
|
/* Creating an OrderList here is safe because the number of vehicles
|
|
|
|
* allowed in these savegames matches the number of OrderLists. As
|
|
|
|
* such each vehicle can get an OrderList and it will (still) fit. */
|
|
|
|
assert(OrderList::CanAllocateItem());
|
2009-01-04 15:32:25 +00:00
|
|
|
v->orders.list = mapping[v->orders.old] = new OrderList(v->orders.old, v);
|
|
|
|
} else {
|
|
|
|
v->orders.list = mapping[v->orders.old];
|
|
|
|
/* For old games (case a) we must create the shared vehicle chain */
|
2010-11-21 12:31:16 +00:00
|
|
|
if (IsSavegameVersionBefore(5, 2)) {
|
2009-01-04 15:32:25 +00:00
|
|
|
v->AddToShared(v->orders.list->GetFirstSharedVehicle());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // OrderList was saved as such, only recalculate not saved values
|
|
|
|
if (v->PreviousShared() == NULL) {
|
2009-05-22 15:13:50 +00:00
|
|
|
v->orders.list->Initialize(v->orders.list->first, v);
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
/* Fill the first pointers */
|
|
|
|
if (v->Previous() == NULL) {
|
|
|
|
for (Vehicle *u = v; u != NULL; u = u->Next()) {
|
|
|
|
u->first = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-20 09:35:16 +00:00
|
|
|
if (part_of_load) {
|
|
|
|
if (IsSavegameVersionBefore(105)) {
|
|
|
|
/* Before 105 there was no order for shared orders, thus it messed up horribly */
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->First() != v || v->orders.list != NULL || v->previous_shared != NULL || v->next_shared == NULL) continue;
|
|
|
|
|
2011-02-23 00:00:50 +00:00
|
|
|
/* As above, allocating OrderList here is safe. */
|
|
|
|
assert(OrderList::CanAllocateItem());
|
2011-01-20 09:35:16 +00:00
|
|
|
v->orders.list = new OrderList(NULL, v);
|
|
|
|
for (Vehicle *u = v; u != NULL; u = u->next_shared) {
|
|
|
|
u->orders.list = v->orders.list;
|
|
|
|
}
|
2009-05-03 11:57:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-20 09:35:16 +00:00
|
|
|
if (IsSavegameVersionBefore(157)) {
|
|
|
|
/* The road vehicle subtype was converted to a flag. */
|
|
|
|
RoadVehicle *rv;
|
|
|
|
FOR_ALL_ROADVEHICLES(rv) {
|
|
|
|
if (rv->subtype == 0) {
|
|
|
|
/* The road vehicle is at the front. */
|
|
|
|
rv->SetFrontEngine();
|
|
|
|
} else if (rv->subtype == 1) {
|
|
|
|
/* The road vehicle is an articulated part. */
|
|
|
|
rv->subtype = 0;
|
|
|
|
rv->SetArticulatedPart();
|
|
|
|
} else {
|
2011-02-20 20:58:32 +00:00
|
|
|
SlErrorCorrupt("Invalid road vehicle subtype");
|
2011-01-20 09:35:16 +00:00
|
|
|
}
|
2011-01-19 20:04:09 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-11 13:38:34 +00:00
|
|
|
|
|
|
|
if (IsSavegameVersionBefore(160)) {
|
|
|
|
/* In some old savegames there might be some "crap" stored. */
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
2011-07-02 21:20:37 +00:00
|
|
|
if (!v->IsPrimaryVehicle() && v->type != VEH_DISASTER) {
|
2011-02-11 13:38:34 +00:00
|
|
|
v->current_order.Free();
|
|
|
|
v->unitnumber = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-03 20:55:08 +00:00
|
|
|
|
|
|
|
if (IsSavegameVersionBefore(162)) {
|
|
|
|
/* Set the vehicle-local cargo age counter from the old global counter. */
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
v->cargo_age_counter = _age_cargo_skip_counter;
|
|
|
|
}
|
|
|
|
}
|
2011-01-19 20:04:09 +00:00
|
|
|
}
|
|
|
|
|
2009-01-25 21:55:18 +00:00
|
|
|
CheckValidVehicles();
|
|
|
|
|
2009-01-04 15:32:25 +00:00
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
assert(v->first != NULL);
|
|
|
|
|
2010-11-06 13:03:17 +00:00
|
|
|
switch (v->type) {
|
|
|
|
case VEH_TRAIN: {
|
|
|
|
Train *t = Train::From(v);
|
|
|
|
if (t->IsFrontEngine() || t->IsFreeWagon()) {
|
2011-01-26 17:31:07 +00:00
|
|
|
t->gcache.last_speed = t->cur_speed; // update displayed train speed
|
2010-11-06 13:03:17 +00:00
|
|
|
t->ConsistChanged(false);
|
|
|
|
}
|
|
|
|
break;
|
2009-07-01 22:22:01 +00:00
|
|
|
}
|
2010-11-06 13:03:17 +00:00
|
|
|
|
|
|
|
case VEH_ROAD: {
|
|
|
|
RoadVehicle *rv = RoadVehicle::From(v);
|
2011-01-19 18:44:13 +00:00
|
|
|
if (rv->IsFrontEngine()) {
|
2011-01-26 17:31:44 +00:00
|
|
|
rv->gcache.last_speed = rv->cur_speed; // update displayed road vehicle speed
|
2010-11-06 13:03:17 +00:00
|
|
|
RoadVehUpdateCache(rv);
|
|
|
|
if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) {
|
|
|
|
rv->CargoChanged();
|
|
|
|
}
|
2010-03-06 12:56:57 +00:00
|
|
|
}
|
2010-11-06 13:03:17 +00:00
|
|
|
break;
|
2009-07-02 08:59:27 +00:00
|
|
|
}
|
2010-11-06 13:03:17 +00:00
|
|
|
|
|
|
|
case VEH_SHIP:
|
|
|
|
Ship::From(v)->UpdateCache();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stop non-front engines */
|
2011-01-20 09:35:16 +00:00
|
|
|
if (part_of_load && IsSavegameVersionBefore(112)) {
|
2009-01-23 23:57:01 +00:00
|
|
|
FOR_ALL_VEHICLES(v) {
|
2009-07-01 23:57:20 +00:00
|
|
|
if (v->type == VEH_TRAIN) {
|
|
|
|
Train *t = Train::From(v);
|
2009-07-16 10:01:32 +00:00
|
|
|
if (!t->IsFrontEngine()) {
|
2009-07-01 23:57:20 +00:00
|
|
|
if (t->IsEngine()) t->vehstatus |= VS_STOPPED;
|
|
|
|
/* cur_speed is now relevant for non-front parts - nonzero breaks
|
|
|
|
* moving-wagons-inside-depot- and autoreplace- code */
|
|
|
|
t->cur_speed = 0;
|
|
|
|
}
|
2009-01-23 23:57:01 +00:00
|
|
|
}
|
2009-01-24 00:42:09 +00:00
|
|
|
/* trains weren't stopping gradually in old OTTD versions (and TTO/TTD)
|
|
|
|
* other vehicle types didn't have zero speed while stopped (even in 'recent' OTTD versions) */
|
2010-11-21 12:31:16 +00:00
|
|
|
if ((v->vehstatus & VS_STOPPED) && (v->type != VEH_TRAIN || IsSavegameVersionBefore(2, 1))) {
|
2009-01-24 00:42:09 +00:00
|
|
|
v->cur_speed = 0;
|
|
|
|
}
|
2009-01-23 23:57:01 +00:00
|
|
|
}
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
switch (v->type) {
|
2009-05-22 20:22:20 +00:00
|
|
|
case VEH_ROAD: {
|
2009-06-06 16:54:22 +00:00
|
|
|
RoadVehicle *rv = RoadVehicle::From(v);
|
2009-05-22 20:22:20 +00:00
|
|
|
rv->roadtype = HasBit(EngInfo(v->First()->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
|
|
|
|
rv->compatible_roadtypes = RoadTypeToRoadTypes(rv->roadtype);
|
2009-01-04 15:32:25 +00:00
|
|
|
/* FALL THROUGH */
|
2010-08-01 20:52:11 +00:00
|
|
|
}
|
|
|
|
|
2009-01-04 15:32:25 +00:00
|
|
|
case VEH_TRAIN:
|
|
|
|
case VEH_SHIP:
|
2011-11-01 16:51:47 +00:00
|
|
|
v->cur_image = v->GetImage(v->direction, EIT_ON_MAP);
|
2009-01-04 15:32:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_AIRCRAFT:
|
2009-07-13 16:37:27 +00:00
|
|
|
if (Aircraft::From(v)->IsNormalAircraft()) {
|
2011-11-01 16:51:47 +00:00
|
|
|
v->cur_image = v->GetImage(v->direction, EIT_ON_MAP);
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
/* The plane's shadow will have the same image as the plane */
|
|
|
|
Vehicle *shadow = v->Next();
|
|
|
|
shadow->cur_image = v->cur_image;
|
|
|
|
|
|
|
|
/* In the case of a helicopter we will update the rotor sprites */
|
|
|
|
if (v->subtype == AIR_HELICOPTER) {
|
|
|
|
Vehicle *rotor = shadow->Next();
|
2011-11-01 16:51:47 +00:00
|
|
|
rotor->cur_image = GetRotorImage(Aircraft::From(v), EIT_ON_MAP);
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
|
2011-12-13 00:43:35 +00:00
|
|
|
UpdateAircraftCache(Aircraft::From(v), true);
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
2011-11-21 20:51:43 +00:00
|
|
|
v->UpdateDeltaXY(v->direction);
|
2009-03-11 23:35:32 +00:00
|
|
|
v->coord.left = INVALID_COORD;
|
2012-01-02 13:44:30 +00:00
|
|
|
VehicleUpdatePosition(v);
|
|
|
|
VehicleUpdateViewport(v, false);
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-21 20:51:43 +00:00
|
|
|
bool TrainController(Train *v, Vehicle *nomove, bool reverse = true); // From train_cmd.cpp
|
|
|
|
void ReverseTrainDirection(Train *v);
|
|
|
|
void ReverseTrainSwapVeh(Train *v, int l, int r);
|
|
|
|
|
|
|
|
/** Fixup old train spacing. */
|
|
|
|
void FixupTrainLengths()
|
|
|
|
{
|
|
|
|
/* Vehicle center was moved from 4 units behind the front to half the length
|
|
|
|
* behind the front. Move vehicles so they end up on the same spot. */
|
|
|
|
Vehicle *v;
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->type == VEH_TRAIN && v->IsPrimaryVehicle()) {
|
|
|
|
/* The vehicle center is now more to the front depending on vehicle length,
|
|
|
|
* so we need to move all vehicles forward to cover the difference to the
|
|
|
|
* old center, otherwise wagon spacing in trains would be broken upon load. */
|
|
|
|
for (Train *u = Train::From(v); u != NULL; u = u->Next()) {
|
|
|
|
if (u->track == TRACK_BIT_DEPOT || (u->vehstatus & VS_CRASHED)) continue;
|
|
|
|
|
|
|
|
Train *next = u->Next();
|
|
|
|
|
|
|
|
/* Try to pull the vehicle half its length forward. */
|
|
|
|
int diff = (VEHICLE_LENGTH - u->gcache.cached_veh_length) / 2;
|
|
|
|
int done;
|
|
|
|
for (done = 0; done < diff; done++) {
|
|
|
|
if (!TrainController(u, next, false)) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next != NULL && done < diff && u->IsFrontEngine()) {
|
|
|
|
/* Pulling the front vehicle forwards failed, we either encountered a dead-end
|
|
|
|
* or a red signal. To fix this, we try to move the whole train the required
|
|
|
|
* space backwards and re-do the fix up of the front vehicle. */
|
|
|
|
|
|
|
|
/* Ignore any signals when backtracking. */
|
|
|
|
TrainForceProceeding old_tfp = u->force_proceed;
|
|
|
|
u->force_proceed = TFP_SIGNAL;
|
|
|
|
|
|
|
|
/* Swap start<>end, start+1<>end-1, ... */
|
|
|
|
int r = CountVehiclesInChain(u) - 1; // number of vehicles - 1
|
|
|
|
int l = 0;
|
|
|
|
do ReverseTrainSwapVeh(u, l++, r--); while (l <= r);
|
|
|
|
|
|
|
|
/* We moved the first vehicle which is now the last. Move it back to the
|
|
|
|
* original position as we will fix up the last vehicle later in the loop. */
|
|
|
|
for (int i = 0; i < done; i++) TrainController(u->Last(), NULL);
|
|
|
|
|
|
|
|
/* Move the train backwards to get space for the first vehicle. As the stopping
|
|
|
|
* distance from a line end is rounded up, move the train one unit more to cater
|
|
|
|
* for front vehicles with odd lengths. */
|
|
|
|
int moved;
|
|
|
|
for (moved = 0; moved < diff + 1; moved++) {
|
|
|
|
if (!TrainController(u, NULL, false)) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Swap start<>end, start+1<>end-1, ... again. */
|
|
|
|
r = CountVehiclesInChain(u) - 1; // number of vehicles - 1
|
|
|
|
l = 0;
|
|
|
|
do ReverseTrainSwapVeh(u, l++, r--); while (l <= r);
|
|
|
|
|
|
|
|
u->force_proceed = old_tfp;
|
|
|
|
|
|
|
|
/* Tracks are too short to fix the train length. The player has to fix the
|
|
|
|
* train in a depot. Bail out so we don't damage the vehicle chain any more. */
|
|
|
|
if (moved < diff + 1) break;
|
|
|
|
|
|
|
|
/* Re-do the correction for the first vehicle. */
|
|
|
|
for (done = 0; done < diff; done++) TrainController(u, next, false);
|
|
|
|
|
|
|
|
/* We moved one unit more backwards than needed for even-length front vehicles,
|
|
|
|
* try to move that unit forward again. We don't care if this step fails. */
|
|
|
|
TrainController(u, NULL, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the next wagon is still in a depot, check if it shouldn't be outside already. */
|
|
|
|
if (next != NULL && next->track == TRACK_BIT_DEPOT) {
|
|
|
|
int d = TicksToLeaveDepot(u);
|
|
|
|
if (d <= 0) {
|
|
|
|
/* Next vehicle should have left the depot already, show it and pull forward. */
|
|
|
|
next->vehstatus &= ~VS_HIDDEN;
|
|
|
|
next->track = TrackToTrackBits(GetRailDepotTrack(next->tile));
|
|
|
|
for (int i = 0; i >= d; i--) TrainController(next, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-08 12:47:54 +00:00
|
|
|
|
|
|
|
/* Update all cached properties after moving the vehicle chain around. */
|
|
|
|
Train::From(v)->ConsistChanged(true);
|
2011-11-21 20:51:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-04 15:32:25 +00:00
|
|
|
static uint8 _cargo_days;
|
|
|
|
static uint16 _cargo_source;
|
|
|
|
static uint32 _cargo_source_xy;
|
|
|
|
static uint16 _cargo_count;
|
|
|
|
static uint16 _cargo_paid_for;
|
|
|
|
static Money _cargo_feeder_share;
|
|
|
|
static uint32 _cargo_loaded_at_xy;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make it possible to make the saveload tables "friends" of other classes.
|
|
|
|
* @param vt the vehicle type. Can be VEH_END for the common vehicle description data
|
|
|
|
* @return the saveload description
|
|
|
|
*/
|
|
|
|
const SaveLoad *GetVehicleDescription(VehicleType vt)
|
|
|
|
{
|
|
|
|
/** Save and load of vehicles */
|
|
|
|
static const SaveLoad _common_veh_desc[] = {
|
|
|
|
SLE_VAR(Vehicle, subtype, SLE_UINT8),
|
|
|
|
|
|
|
|
SLE_REF(Vehicle, next, REF_VEHICLE_OLD),
|
|
|
|
SLE_CONDVAR(Vehicle, name, SLE_NAME, 0, 83),
|
2011-12-19 21:06:06 +00:00
|
|
|
SLE_CONDSTR(Vehicle, name, SLE_STR | SLF_ALLOW_CONTROL, 0, 84, SL_MAX_VERSION),
|
2009-01-04 15:32:25 +00:00
|
|
|
SLE_CONDVAR(Vehicle, unitnumber, SLE_FILE_U8 | SLE_VAR_U16, 0, 7),
|
|
|
|
SLE_CONDVAR(Vehicle, unitnumber, SLE_UINT16, 8, SL_MAX_VERSION),
|
|
|
|
SLE_VAR(Vehicle, owner, SLE_UINT8),
|
|
|
|
SLE_CONDVAR(Vehicle, tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Vehicle, tile, SLE_UINT32, 6, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Vehicle, dest_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Vehicle, dest_tile, SLE_UINT32, 6, SL_MAX_VERSION),
|
|
|
|
|
|
|
|
SLE_CONDVAR(Vehicle, x_pos, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Vehicle, x_pos, SLE_UINT32, 6, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Vehicle, y_pos, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Vehicle, y_pos, SLE_UINT32, 6, SL_MAX_VERSION),
|
2011-11-04 11:29:11 +00:00
|
|
|
SLE_CONDVAR(Vehicle, z_pos, SLE_FILE_U8 | SLE_VAR_I32, 0, 163),
|
2011-11-04 11:09:06 +00:00
|
|
|
SLE_CONDVAR(Vehicle, z_pos, SLE_INT32, 164, SL_MAX_VERSION),
|
2009-01-04 15:32:25 +00:00
|
|
|
SLE_VAR(Vehicle, direction, SLE_UINT8),
|
|
|
|
|
|
|
|
SLE_CONDNULL(2, 0, 57),
|
|
|
|
SLE_VAR(Vehicle, spritenum, SLE_UINT8),
|
|
|
|
SLE_CONDNULL(5, 0, 57),
|
|
|
|
SLE_VAR(Vehicle, engine_type, SLE_UINT16),
|
|
|
|
|
2010-11-09 17:43:05 +00:00
|
|
|
SLE_CONDNULL(2, 0, 151),
|
2009-01-04 15:32:25 +00:00
|
|
|
SLE_VAR(Vehicle, cur_speed, SLE_UINT16),
|
|
|
|
SLE_VAR(Vehicle, subspeed, SLE_UINT8),
|
|
|
|
SLE_VAR(Vehicle, acceleration, SLE_UINT8),
|
|
|
|
SLE_VAR(Vehicle, progress, SLE_UINT8),
|
|
|
|
|
|
|
|
SLE_VAR(Vehicle, vehstatus, SLE_UINT8),
|
|
|
|
SLE_CONDVAR(Vehicle, last_station_visited, SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
|
|
|
|
SLE_CONDVAR(Vehicle, last_station_visited, SLE_UINT16, 5, SL_MAX_VERSION),
|
|
|
|
|
|
|
|
SLE_VAR(Vehicle, cargo_type, SLE_UINT8),
|
|
|
|
SLE_CONDVAR(Vehicle, cargo_subtype, SLE_UINT8, 35, SL_MAX_VERSION),
|
|
|
|
SLEG_CONDVAR( _cargo_days, SLE_UINT8, 0, 67),
|
|
|
|
SLEG_CONDVAR( _cargo_source, SLE_FILE_U8 | SLE_VAR_U16, 0, 6),
|
|
|
|
SLEG_CONDVAR( _cargo_source, SLE_UINT16, 7, 67),
|
|
|
|
SLEG_CONDVAR( _cargo_source_xy, SLE_UINT32, 44, 67),
|
|
|
|
SLE_VAR(Vehicle, cargo_cap, SLE_UINT16),
|
|
|
|
SLEG_CONDVAR( _cargo_count, SLE_UINT16, 0, 67),
|
2009-10-06 23:01:35 +00:00
|
|
|
SLE_CONDLST(Vehicle, cargo.packets, REF_CARGO_PACKET, 68, SL_MAX_VERSION),
|
2011-08-03 20:55:08 +00:00
|
|
|
SLE_CONDVAR(Vehicle, cargo_age_counter, SLE_UINT16, 162, SL_MAX_VERSION),
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
SLE_VAR(Vehicle, day_counter, SLE_UINT8),
|
|
|
|
SLE_VAR(Vehicle, tick_counter, SLE_UINT8),
|
|
|
|
SLE_CONDVAR(Vehicle, running_ticks, SLE_UINT8, 88, SL_MAX_VERSION),
|
|
|
|
|
2011-05-18 12:19:58 +00:00
|
|
|
SLE_VAR(Vehicle, cur_implicit_order_index, SLE_UINT8),
|
2011-01-31 20:44:15 +00:00
|
|
|
SLE_CONDVAR(Vehicle, cur_real_order_index, SLE_UINT8, 158, SL_MAX_VERSION),
|
2009-01-04 15:32:25 +00:00
|
|
|
/* num_orders is now part of OrderList and is not saved but counted */
|
|
|
|
SLE_CONDNULL(1, 0, 104),
|
|
|
|
|
|
|
|
/* This next line is for version 4 and prior compatibility.. it temporarily reads
|
|
|
|
type and flags (which were both 4 bits) into type. Later on this is
|
|
|
|
converted correctly */
|
2009-07-24 19:09:48 +00:00
|
|
|
SLE_CONDVAR(Vehicle, current_order.type, SLE_UINT8, 0, 4),
|
|
|
|
SLE_CONDVAR(Vehicle, current_order.dest, SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
/* Orders for version 5 and on */
|
2009-07-24 19:09:48 +00:00
|
|
|
SLE_CONDVAR(Vehicle, current_order.type, SLE_UINT8, 5, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Vehicle, current_order.flags, SLE_UINT8, 5, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Vehicle, current_order.dest, SLE_UINT16, 5, SL_MAX_VERSION),
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
/* Refit in current order */
|
2009-07-24 19:09:48 +00:00
|
|
|
SLE_CONDVAR(Vehicle, current_order.refit_cargo, SLE_UINT8, 36, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Vehicle, current_order.refit_subtype, SLE_UINT8, 36, SL_MAX_VERSION),
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
/* Timetable in current order */
|
2009-07-24 19:09:48 +00:00
|
|
|
SLE_CONDVAR(Vehicle, current_order.wait_time, SLE_UINT16, 67, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Vehicle, current_order.travel_time, SLE_UINT16, 67, SL_MAX_VERSION),
|
2009-11-25 23:37:15 +00:00
|
|
|
SLE_CONDVAR(Vehicle, timetable_start, SLE_INT32, 129, SL_MAX_VERSION),
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
SLE_CONDREF(Vehicle, orders, REF_ORDER, 0, 104),
|
|
|
|
SLE_CONDREF(Vehicle, orders, REF_ORDERLIST, 105, SL_MAX_VERSION),
|
|
|
|
|
|
|
|
SLE_CONDVAR(Vehicle, age, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
|
|
|
|
SLE_CONDVAR(Vehicle, age, SLE_INT32, 31, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Vehicle, max_age, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
|
|
|
|
SLE_CONDVAR(Vehicle, max_age, SLE_INT32, 31, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Vehicle, date_of_last_service, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
|
|
|
|
SLE_CONDVAR(Vehicle, date_of_last_service, SLE_INT32, 31, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Vehicle, service_interval, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
|
|
|
|
SLE_CONDVAR(Vehicle, service_interval, SLE_INT32, 31, SL_MAX_VERSION),
|
|
|
|
SLE_VAR(Vehicle, reliability, SLE_UINT16),
|
|
|
|
SLE_VAR(Vehicle, reliability_spd_dec, SLE_UINT16),
|
|
|
|
SLE_VAR(Vehicle, breakdown_ctr, SLE_UINT8),
|
|
|
|
SLE_VAR(Vehicle, breakdown_delay, SLE_UINT8),
|
|
|
|
SLE_VAR(Vehicle, breakdowns_since_last_service, SLE_UINT8),
|
|
|
|
SLE_VAR(Vehicle, breakdown_chance, SLE_UINT8),
|
|
|
|
SLE_CONDVAR(Vehicle, build_year, SLE_FILE_U8 | SLE_VAR_I32, 0, 30),
|
|
|
|
SLE_CONDVAR(Vehicle, build_year, SLE_INT32, 31, SL_MAX_VERSION),
|
|
|
|
|
2010-01-09 14:43:08 +00:00
|
|
|
SLE_VAR(Vehicle, load_unload_ticks, SLE_UINT16),
|
2009-01-04 15:32:25 +00:00
|
|
|
SLEG_CONDVAR( _cargo_paid_for, SLE_UINT16, 45, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Vehicle, vehicle_flags, SLE_UINT8, 40, SL_MAX_VERSION),
|
|
|
|
|
|
|
|
SLE_CONDVAR(Vehicle, profit_this_year, SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
|
|
|
|
SLE_CONDVAR(Vehicle, profit_this_year, SLE_INT64, 65, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Vehicle, profit_last_year, SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
|
|
|
|
SLE_CONDVAR(Vehicle, profit_last_year, SLE_INT64, 65, SL_MAX_VERSION),
|
|
|
|
SLEG_CONDVAR( _cargo_feeder_share, SLE_FILE_I32 | SLE_VAR_I64, 51, 64),
|
|
|
|
SLEG_CONDVAR( _cargo_feeder_share, SLE_INT64, 65, 67),
|
|
|
|
SLEG_CONDVAR( _cargo_loaded_at_xy, SLE_UINT32, 51, 67),
|
|
|
|
SLE_CONDVAR(Vehicle, value, SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
|
|
|
|
SLE_CONDVAR(Vehicle, value, SLE_INT64, 65, SL_MAX_VERSION),
|
|
|
|
|
|
|
|
SLE_CONDVAR(Vehicle, random_bits, SLE_UINT8, 2, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Vehicle, waiting_triggers, SLE_UINT8, 2, SL_MAX_VERSION),
|
|
|
|
|
|
|
|
SLE_CONDREF(Vehicle, next_shared, REF_VEHICLE, 2, SL_MAX_VERSION),
|
|
|
|
SLE_CONDNULL(2, 2, 68),
|
|
|
|
SLE_CONDNULL(4, 69, 100),
|
|
|
|
|
|
|
|
SLE_CONDVAR(Vehicle, group_id, SLE_UINT16, 60, SL_MAX_VERSION),
|
|
|
|
|
|
|
|
SLE_CONDVAR(Vehicle, current_order_time, SLE_UINT32, 67, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Vehicle, lateness_counter, SLE_INT32, 67, SL_MAX_VERSION),
|
|
|
|
|
2010-08-02 18:24:09 +00:00
|
|
|
SLE_CONDNULL(10, 2, 143), // old reserved space
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static const SaveLoad _train_desc[] = {
|
|
|
|
SLE_WRITEBYTE(Vehicle, type, VEH_TRAIN),
|
2009-07-24 19:09:48 +00:00
|
|
|
SLE_VEH_INCLUDE(),
|
2009-05-22 22:33:05 +00:00
|
|
|
SLE_VAR(Train, crash_anim_pos, SLE_UINT16),
|
|
|
|
SLE_VAR(Train, force_proceed, SLE_UINT8),
|
|
|
|
SLE_VAR(Train, railtype, SLE_UINT8),
|
|
|
|
SLE_VAR(Train, track, SLE_UINT8),
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
SLE_CONDVAR(Train, flags, SLE_FILE_U8 | SLE_VAR_U16, 2, 99),
|
|
|
|
SLE_CONDVAR(Train, flags, SLE_UINT16, 100, SL_MAX_VERSION),
|
2009-01-04 15:32:25 +00:00
|
|
|
SLE_CONDNULL(2, 2, 59),
|
|
|
|
|
2010-01-09 14:43:08 +00:00
|
|
|
SLE_CONDVAR(Train, wait_counter, SLE_UINT16, 136, SL_MAX_VERSION),
|
|
|
|
|
2009-01-04 15:32:25 +00:00
|
|
|
SLE_CONDNULL(2, 2, 19),
|
2010-03-06 12:50:55 +00:00
|
|
|
SLE_CONDVAR(Train, gv_flags, SLE_UINT16, 139, SL_MAX_VERSION),
|
2010-08-02 18:24:09 +00:00
|
|
|
SLE_CONDNULL(11, 2, 143), // old reserved space
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SaveLoad _roadveh_desc[] = {
|
|
|
|
SLE_WRITEBYTE(Vehicle, type, VEH_ROAD),
|
2009-07-24 19:09:48 +00:00
|
|
|
SLE_VEH_INCLUDE(),
|
2009-05-22 20:22:20 +00:00
|
|
|
SLE_VAR(RoadVehicle, state, SLE_UINT8),
|
|
|
|
SLE_VAR(RoadVehicle, frame, SLE_UINT8),
|
|
|
|
SLE_VAR(RoadVehicle, blocked_ctr, SLE_UINT16),
|
|
|
|
SLE_VAR(RoadVehicle, overtaking, SLE_UINT8),
|
|
|
|
SLE_VAR(RoadVehicle, overtaking_ctr, SLE_UINT8),
|
|
|
|
SLE_VAR(RoadVehicle, crashed_ctr, SLE_UINT16),
|
|
|
|
SLE_VAR(RoadVehicle, reverse_ctr, SLE_UINT8),
|
|
|
|
|
2009-12-02 18:18:56 +00:00
|
|
|
SLE_CONDNULL(2, 6, 68),
|
2010-03-06 12:54:42 +00:00
|
|
|
SLE_CONDVAR(RoadVehicle, gv_flags, SLE_UINT16, 139, SL_MAX_VERSION),
|
2009-12-02 18:18:56 +00:00
|
|
|
SLE_CONDNULL(4, 69, 130),
|
|
|
|
SLE_CONDNULL(2, 6, 130),
|
2010-08-02 18:24:09 +00:00
|
|
|
SLE_CONDNULL(16, 2, 143), // old reserved space
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SaveLoad _ship_desc[] = {
|
|
|
|
SLE_WRITEBYTE(Vehicle, type, VEH_SHIP),
|
2009-07-24 19:09:48 +00:00
|
|
|
SLE_VEH_INCLUDE(),
|
2009-05-22 18:17:20 +00:00
|
|
|
SLE_VAR(Ship, state, SLE_UINT8),
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2010-08-02 18:24:09 +00:00
|
|
|
SLE_CONDNULL(16, 2, 143), // old reserved space
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SaveLoad _aircraft_desc[] = {
|
|
|
|
SLE_WRITEBYTE(Vehicle, type, VEH_AIRCRAFT),
|
2009-07-24 19:09:48 +00:00
|
|
|
SLE_VEH_INCLUDE(),
|
2009-05-22 20:07:26 +00:00
|
|
|
SLE_VAR(Aircraft, crashed_counter, SLE_UINT16),
|
|
|
|
SLE_VAR(Aircraft, pos, SLE_UINT8),
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2009-05-22 20:07:26 +00:00
|
|
|
SLE_CONDVAR(Aircraft, targetairport, SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
|
|
|
|
SLE_CONDVAR(Aircraft, targetairport, SLE_UINT16, 5, SL_MAX_VERSION),
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2009-05-22 20:07:26 +00:00
|
|
|
SLE_VAR(Aircraft, state, SLE_UINT8),
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2009-05-22 20:07:26 +00:00
|
|
|
SLE_CONDVAR(Aircraft, previous_pos, SLE_UINT8, 2, SL_MAX_VERSION),
|
2009-09-03 12:11:31 +00:00
|
|
|
SLE_CONDVAR(Aircraft, last_direction, SLE_UINT8, 2, SL_MAX_VERSION),
|
2009-09-09 00:03:35 +00:00
|
|
|
SLE_CONDVAR(Aircraft, number_consecutive_turns, SLE_UINT8, 2, SL_MAX_VERSION),
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2010-01-09 14:43:08 +00:00
|
|
|
SLE_CONDVAR(Aircraft, turn_counter, SLE_UINT8, 136, SL_MAX_VERSION),
|
2011-12-13 00:43:35 +00:00
|
|
|
SLE_CONDVAR(Aircraft, flags, SLE_UINT8, 167, SL_MAX_VERSION),
|
2010-01-09 14:43:08 +00:00
|
|
|
|
2010-08-02 18:24:09 +00:00
|
|
|
SLE_CONDNULL(13, 2, 143), // old reserved space
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SaveLoad _special_desc[] = {
|
|
|
|
SLE_WRITEBYTE(Vehicle, type, VEH_EFFECT),
|
|
|
|
|
|
|
|
SLE_VAR(Vehicle, subtype, SLE_UINT8),
|
|
|
|
|
|
|
|
SLE_CONDVAR(Vehicle, tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Vehicle, tile, SLE_UINT32, 6, SL_MAX_VERSION),
|
|
|
|
|
|
|
|
SLE_CONDVAR(Vehicle, x_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
|
|
|
|
SLE_CONDVAR(Vehicle, x_pos, SLE_INT32, 6, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Vehicle, y_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
|
|
|
|
SLE_CONDVAR(Vehicle, y_pos, SLE_INT32, 6, SL_MAX_VERSION),
|
2011-11-04 11:29:11 +00:00
|
|
|
SLE_CONDVAR(Vehicle, z_pos, SLE_FILE_U8 | SLE_VAR_I32, 0, 163),
|
2011-11-04 11:09:06 +00:00
|
|
|
SLE_CONDVAR(Vehicle, z_pos, SLE_INT32, 164, SL_MAX_VERSION),
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2011-01-04 12:04:09 +00:00
|
|
|
SLE_VAR(Vehicle, cur_image, SLE_FILE_U16 | SLE_VAR_U32),
|
2009-01-04 15:32:25 +00:00
|
|
|
SLE_CONDNULL(5, 0, 57),
|
|
|
|
SLE_VAR(Vehicle, progress, SLE_UINT8),
|
|
|
|
SLE_VAR(Vehicle, vehstatus, SLE_UINT8),
|
|
|
|
|
2009-05-22 18:56:25 +00:00
|
|
|
SLE_VAR(EffectVehicle, animation_state, SLE_UINT16),
|
|
|
|
SLE_VAR(EffectVehicle, animation_substate, SLE_UINT8),
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
SLE_CONDVAR(Vehicle, spritenum, SLE_UINT8, 2, SL_MAX_VERSION),
|
|
|
|
|
2010-08-02 18:24:09 +00:00
|
|
|
SLE_CONDNULL(15, 2, 143), // old reserved space
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SaveLoad _disaster_desc[] = {
|
|
|
|
SLE_WRITEBYTE(Vehicle, type, VEH_DISASTER),
|
|
|
|
|
|
|
|
SLE_REF(Vehicle, next, REF_VEHICLE_OLD),
|
|
|
|
|
|
|
|
SLE_VAR(Vehicle, subtype, SLE_UINT8),
|
|
|
|
SLE_CONDVAR(Vehicle, tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Vehicle, tile, SLE_UINT32, 6, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Vehicle, dest_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Vehicle, dest_tile, SLE_UINT32, 6, SL_MAX_VERSION),
|
|
|
|
|
|
|
|
SLE_CONDVAR(Vehicle, x_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
|
|
|
|
SLE_CONDVAR(Vehicle, x_pos, SLE_INT32, 6, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Vehicle, y_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
|
|
|
|
SLE_CONDVAR(Vehicle, y_pos, SLE_INT32, 6, SL_MAX_VERSION),
|
2011-11-04 11:29:11 +00:00
|
|
|
SLE_CONDVAR(Vehicle, z_pos, SLE_FILE_U8 | SLE_VAR_I32, 0, 163),
|
2011-11-04 11:09:06 +00:00
|
|
|
SLE_CONDVAR(Vehicle, z_pos, SLE_INT32, 164, SL_MAX_VERSION),
|
2009-01-04 15:32:25 +00:00
|
|
|
SLE_VAR(Vehicle, direction, SLE_UINT8),
|
|
|
|
|
|
|
|
SLE_CONDNULL(5, 0, 57),
|
|
|
|
SLE_VAR(Vehicle, owner, SLE_UINT8),
|
|
|
|
SLE_VAR(Vehicle, vehstatus, SLE_UINT8),
|
2009-07-24 19:09:48 +00:00
|
|
|
SLE_CONDVAR(Vehicle, current_order.dest, SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
|
|
|
|
SLE_CONDVAR(Vehicle, current_order.dest, SLE_UINT16, 5, SL_MAX_VERSION),
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2011-01-04 12:04:09 +00:00
|
|
|
SLE_VAR(Vehicle, cur_image, SLE_FILE_U16 | SLE_VAR_U32),
|
2009-01-04 15:32:25 +00:00
|
|
|
SLE_CONDVAR(Vehicle, age, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
|
|
|
|
SLE_CONDVAR(Vehicle, age, SLE_INT32, 31, SL_MAX_VERSION),
|
|
|
|
SLE_VAR(Vehicle, tick_counter, SLE_UINT8),
|
|
|
|
|
2009-05-22 18:57:11 +00:00
|
|
|
SLE_VAR(DisasterVehicle, image_override, SLE_UINT16),
|
|
|
|
SLE_VAR(DisasterVehicle, big_ufo_destroyer_target, SLE_UINT16),
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2010-08-02 18:24:09 +00:00
|
|
|
SLE_CONDNULL(16, 2, 143), // old reserved space
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-09-20 23:11:01 +00:00
|
|
|
static const SaveLoad * const _veh_descs[] = {
|
2009-01-04 15:32:25 +00:00
|
|
|
_train_desc,
|
|
|
|
_roadveh_desc,
|
|
|
|
_ship_desc,
|
|
|
|
_aircraft_desc,
|
|
|
|
_special_desc,
|
|
|
|
_disaster_desc,
|
|
|
|
_common_veh_desc,
|
|
|
|
};
|
|
|
|
|
|
|
|
return _veh_descs[vt];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Will be called when the vehicles need to be saved. */
|
|
|
|
static void Save_VEHS()
|
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
/* Write the vehicles */
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
SlSetArrayIndex(v->index);
|
|
|
|
SlObject(v, GetVehicleDescription(v->type));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Will be called when vehicles need to be loaded. */
|
|
|
|
void Load_VEHS()
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
_cargo_count = 0;
|
|
|
|
|
|
|
|
while ((index = SlIterateArray()) != -1) {
|
|
|
|
Vehicle *v;
|
|
|
|
VehicleType vtype = (VehicleType)SlReadByte();
|
|
|
|
|
|
|
|
switch (vtype) {
|
|
|
|
case VEH_TRAIN: v = new (index) Train(); break;
|
|
|
|
case VEH_ROAD: v = new (index) RoadVehicle(); break;
|
|
|
|
case VEH_SHIP: v = new (index) Ship(); break;
|
|
|
|
case VEH_AIRCRAFT: v = new (index) Aircraft(); break;
|
|
|
|
case VEH_EFFECT: v = new (index) EffectVehicle(); break;
|
|
|
|
case VEH_DISASTER: v = new (index) DisasterVehicle(); break;
|
2010-02-10 21:06:05 +00:00
|
|
|
case VEH_INVALID: // Savegame shouldn't contain invalid vehicles
|
2010-09-16 22:06:36 +00:00
|
|
|
default: SlErrorCorrupt("Invalid vehicle type");
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SlObject(v, GetVehicleDescription(vtype));
|
|
|
|
|
2011-02-08 18:34:13 +00:00
|
|
|
if (_cargo_count != 0 && IsCompanyBuildableVehicleType(v) && CargoPacket::CanAllocateItem()) {
|
2009-01-04 15:32:25 +00:00
|
|
|
/* Don't construct the packet with station here, because that'll fail with old savegames */
|
2009-10-18 17:52:44 +00:00
|
|
|
CargoPacket *cp = new CargoPacket(_cargo_count, _cargo_days, _cargo_source, _cargo_source_xy, _cargo_loaded_at_xy, _cargo_feeder_share);
|
2009-01-04 15:32:25 +00:00
|
|
|
v->cargo.Append(cp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Old savegames used 'last_station_visited = 0xFF' */
|
2010-11-21 12:47:04 +00:00
|
|
|
if (IsSavegameVersionBefore(5) && v->last_station_visited == 0xFF) {
|
2009-01-04 15:32:25 +00:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2010-11-21 12:47:04 +00:00
|
|
|
if (IsSavegameVersionBefore(5)) {
|
2009-01-04 15:32:25 +00:00
|
|
|
/* Convert the current_order.type (which is a mix of type and flags, because
|
|
|
|
* in those versions, they both were 4 bits big) to type and flags */
|
|
|
|
v->current_order.flags = GB(v->current_order.type, 4, 4);
|
|
|
|
v->current_order.type &= 0x0F;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advanced vehicle lists got added */
|
2010-11-21 12:47:04 +00:00
|
|
|
if (IsSavegameVersionBefore(60)) v->group_id = DEFAULT_GROUP;
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-09 10:40:33 +00:00
|
|
|
static void Ptrs_VEHS()
|
2009-05-17 16:28:29 +00:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
SlObject(v, GetVehicleDescription(v->type));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-04 15:32:25 +00:00
|
|
|
extern const ChunkHandler _veh_chunk_handlers[] = {
|
2010-06-13 14:11:59 +00:00
|
|
|
{ 'VEHS', Save_VEHS, Load_VEHS, Ptrs_VEHS, NULL, CH_SPARSE_ARRAY | CH_LAST},
|
2009-01-04 15:32:25 +00:00
|
|
|
};
|