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 enroute, 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 VehicleCargoList::List *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 StationCargoList::List *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 
00087 const SaveLoad *GetCargoPacketDesc()
00088 {
00089   static const SaveLoad _cargopacket_desc[] = {
00090          SLE_VAR(CargoPacket, source,          SLE_UINT16),
00091          SLE_VAR(CargoPacket, source_xy,       SLE_UINT32),
00092          SLE_VAR(CargoPacket, loaded_at_xy,    SLE_UINT32),
00093          SLE_VAR(CargoPacket, count,           SLE_UINT16),
00094          SLE_VAR(CargoPacket, days_in_transit, SLE_UINT8),
00095          SLE_VAR(CargoPacket, feeder_share,    SLE_INT64),
00096      SLE_CONDVAR(CargoPacket, source_type,     SLE_UINT8,  125, SL_MAX_VERSION),
00097      SLE_CONDVAR(CargoPacket, source_id,       SLE_UINT16, 125, SL_MAX_VERSION),
00098      SLE_CONDVAR(CargoPacket, dest_xy,         SLE_UINT32, 161, SL_MAX_VERSION),
00099      SLE_CONDVAR(CargoPacket, dest_id,         SLE_UINT16, 161, SL_MAX_VERSION),
00100      SLE_CONDVAR(CargoPacket, dest_type,       SLE_UINT8,  161, SL_MAX_VERSION),
00101      SLE_CONDVAR(CargoPacket, flags,           SLE_UINT8,  161, SL_MAX_VERSION),
00102      SLE_CONDVAR(CargoPacket, next_order,      SLE_UINT16, 161, SL_MAX_VERSION),
00103      SLE_CONDVAR(CargoPacket, next_station,    SLE_UINT16, 161, SL_MAX_VERSION),
00104 
00105     /* Used to be paid_for, but that got changed. */
00106     SLE_CONDNULL(1, 0, 120),
00107 
00108     SLE_END()
00109   };
00110   return _cargopacket_desc;
00111 }
00112 
00116 static void Save_CAPA()
00117 {
00118   CargoPacket *cp;
00119 
00120   FOR_ALL_CARGOPACKETS(cp) {
00121     SlSetArrayIndex(cp->index);
00122     SlObject(cp, GetCargoPacketDesc());
00123   }
00124 }
00125 
00129 static void Load_CAPA()
00130 {
00131   int index;
00132 
00133   while ((index = SlIterateArray()) != -1) {
00134     CargoPacket *cp = new (index) CargoPacket();
00135     SlObject(cp, GetCargoPacketDesc());
00136   }
00137 }
00138 
00140 extern const ChunkHandler _cargopacket_chunk_handlers[] = {
00141   { 'CAPA', Save_CAPA, Load_CAPA, NULL, NULL, CH_ARRAY | CH_LAST},
00142 };

Generated on Sun May 8 07:30:18 2011 for OpenTTD by  doxygen 1.6.1