# cargopacket_sl.cpp

Go to the documentation of this file.

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