cargopacket_sl.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 "../vehicle_base.h"
00014 #include "../station_base.h"
00015 
00016 #include "saveload.h"
00017 
00021 /* static */ void CargoPacket::AfterLoad()
00022 {
00023   if (IsSavegameVersionBefore(44)) {
00024     Vehicle *v;
00025     /* If we remove a station while cargo from it is still en route, payment calculation will assume
00026      * 0, 0 to be the source of the cargo, resulting in very high payments usually. v->source_xy
00027      * stores the coordinates, preserving them even if the station is removed. However, if a game is loaded
00028      * where this situation exists, the cargo-source information is lost. in this case, we set the source
00029      * to the current tile of the vehicle to prevent excessive profits
00030      */
00031     FOR_ALL_VEHICLES(v) {
00032       const CargoPacketList *packets = v->cargo.Packets();
00033       for (VehicleCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
00034         CargoPacket *cp = *it;
00035         cp->source_xy = Station::IsValidID(cp->source) ? Station::Get(cp->source)->xy : v->tile;
00036         cp->loaded_at_xy = cp->source_xy;
00037       }
00038     }
00039 
00040     /* Store position of the station where the goods come from, so there
00041      * are no very high payments when stations get removed. However, if the
00042      * station where the goods came from is already removed, the source
00043      * information is lost. In that case we set it to the position of this
00044      * station */
00045     Station *st;
00046     FOR_ALL_STATIONS(st) {
00047       for (CargoID c = 0; c < NUM_CARGO; c++) {
00048         GoodsEntry *ge = &st->goods[c];
00049 
00050         const StationCargoPacketMap *packets = ge->cargo.Packets();
00051         for (StationCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
00052           CargoPacket *cp = *it;
00053           cp->source_xy = Station::IsValidID(cp->source) ? Station::Get(cp->source)->xy : st->xy;
00054           cp->loaded_at_xy = cp->source_xy;
00055         }
00056       }
00057     }
00058   }
00059 
00060   if (IsSavegameVersionBefore(120)) {
00061     /* CargoPacket's source should be either INVALID_STATION or a valid station */
00062     CargoPacket *cp;
00063     FOR_ALL_CARGOPACKETS(cp) {
00064       if (!Station::IsValidID(cp->source)) cp->source = INVALID_STATION;
00065     }
00066   }
00067 
00068   if (!IsSavegameVersionBefore(68)) {
00069     /* Only since version 68 we have cargo packets. Savegames from before used
00070      * 'new CargoPacket' + cargolist.Append so their caches are already
00071      * correct and do not need rebuilding. */
00072     Vehicle *v;
00073     FOR_ALL_VEHICLES(v) v->cargo.InvalidateCache();
00074 
00075     Station *st;
00076     FOR_ALL_STATIONS(st) {
00077       for (CargoID c = 0; c < NUM_CARGO; c++) st->goods[c].cargo.InvalidateCache();
00078     }
00079   }
00080 
00081   if (IsSavegameVersionBefore(181)) {
00082     Vehicle *v;
00083     FOR_ALL_VEHICLES(v) v->cargo.KeepAll();
00084   }
00085 }
00086 
00092 const SaveLoad *GetCargoPacketDesc()
00093 {
00094   static const SaveLoad _cargopacket_desc[] = {
00095          SLE_VAR(CargoPacket, source,          SLE_UINT16),
00096          SLE_VAR(CargoPacket, source_xy,       SLE_UINT32),
00097          SLE_VAR(CargoPacket, loaded_at_xy,    SLE_UINT32),
00098          SLE_VAR(CargoPacket, count,           SLE_UINT16),
00099          SLE_VAR(CargoPacket, days_in_transit, SLE_UINT8),
00100          SLE_VAR(CargoPacket, feeder_share,    SLE_INT64),
00101      SLE_CONDVAR(CargoPacket, source_type,     SLE_UINT8,  125, SL_MAX_VERSION),
00102      SLE_CONDVAR(CargoPacket, source_id,       SLE_UINT16, 125, SL_MAX_VERSION),
00103 
00104     /* Used to be paid_for, but that got changed. */
00105     SLE_CONDNULL(1, 0, 120),
00106 
00107     SLE_END()
00108   };
00109   return _cargopacket_desc;
00110 }
00111 
00115 static void Save_CAPA()
00116 {
00117   CargoPacket *cp;
00118 
00119   FOR_ALL_CARGOPACKETS(cp) {
00120     SlSetArrayIndex(cp->index);
00121     SlObject(cp, GetCargoPacketDesc());
00122   }
00123 }
00124 
00128 static void Load_CAPA()
00129 {
00130   int index;
00131 
00132   while ((index = SlIterateArray()) != -1) {
00133     CargoPacket *cp = new (index) CargoPacket();
00134     SlObject(cp, GetCargoPacketDesc());
00135   }
00136 }
00137 
00139 extern const ChunkHandler _cargopacket_chunk_handlers[] = {
00140   { 'CAPA', Save_CAPA, Load_CAPA, NULL, NULL, CH_ARRAY | CH_LAST},
00141 };